-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign_certd.go
368 lines (325 loc) · 10.8 KB
/
sign_certd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package main
import (
"bytes"
"crypto/rand"
"encoding/base32"
"encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/bobveznat/ssh-ca-ss/ssh_ca"
"github.com/gorilla/mux"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"log"
"net"
"net/http"
"os"
"regexp"
"time"
)
const buildVersion string = "dev"
type certRequest struct {
// This struct tracks state for certificate requests. Imagine this one day
// being stored in a persistent data store.
request *ssh.Certificate
submitTime time.Time
environment string
signatures map[string]bool
certSigned bool
reason string
}
func newcertRequest() certRequest {
var cr certRequest
cr.submitTime = time.Now()
cr.certSigned = false
cr.signatures = make(map[string]bool)
return cr
}
type certRequestHandler struct {
Config map[string]ssh_ca.SignerdConfig
state map[string]certRequest
sshAgent agent.Agent
NextSerial chan uint64
}
type signingRequest struct {
config *ssh_ca.SignerdConfig
environment string
cert *ssh.Certificate
}
func (h *certRequestHandler) formBoilerplate(req *http.Request) (*ssh_ca.SignerdConfig, string, error) {
err := req.ParseForm()
if err != nil {
err := fmt.Errorf("%v", err)
return nil, "", err
}
if req.Form["environment"] == nil {
err := errors.New("Must specify environment")
return nil, "", err
}
environment := req.Form["environment"][0]
config, ok := h.Config[environment]
if !ok {
err := errors.New("Environment is not configured (is it valid?)")
return nil, "", err
}
return &config, environment, nil
}
func (h *certRequestHandler) createSigningRequest(rw http.ResponseWriter, req *http.Request) {
var requestData signingRequest
config, environment, err := h.formBoilerplate(req)
requestData.config = config
requestData.environment = environment
if err != nil {
http.Error(rw, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
err = h.extractCertFromRequest(req, &requestData, config.AuthorizedUsers)
if err != nil {
http.Error(rw, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
if req.Form["reason"][0] == "" {
http.Error(rw, "You forgot to send in a reason", http.StatusBadRequest)
return
}
requesterFp := ssh_ca.MakeFingerprint(requestData.cert.SignatureKey.Marshal())
requestID := make([]byte, 15)
rand.Reader.Read(requestID)
requestIDStr := base32.StdEncoding.EncodeToString(requestID)
requestData.cert.Serial = <-h.NextSerial
// We override keyid here so that its a server controlled value. Instead of
// letting a requester attempt to spoof it.
requestData.cert.KeyId = config.AuthorizedUsers[requesterFp]
certRequest := newcertRequest()
certRequest.request = requestData.cert
certRequest.environment = requestData.environment
certRequest.reason = req.Form["reason"][0]
h.state[requestIDStr] = certRequest
rw.WriteHeader(http.StatusCreated)
rw.Write([]byte(requestIDStr))
log.Printf("Cert request serial %d id %s env %s from %s (%s) @ %s principals %v valid from %d to %d for '%s'\n",
requestData.cert.Serial, requestIDStr, requestData.environment, requesterFp, config.AuthorizedUsers[requesterFp],
req.RemoteAddr, requestData.cert.ValidPrincipals, requestData.cert.ValidAfter, requestData.cert.ValidBefore, certRequest.reason)
return
}
func (h *certRequestHandler) extractCertFromRequest(req *http.Request, requestData *signingRequest, authorizedSigners map[string]string) error {
if req.PostForm["cert"] == nil || len(req.PostForm["cert"]) == 0 {
err := errors.New("Please specify exactly one cert request")
return err
}
rawCertRequest, err := base64.StdEncoding.DecodeString(req.PostForm["cert"][0])
if err != nil {
err := errors.New("Unable to base64 decode cert request")
return err
}
pubKey, err := ssh.ParsePublicKey(rawCertRequest)
if err != nil {
err := errors.New("Unable to parse cert request")
return err
}
cert := pubKey.(*ssh.Certificate)
requestData.cert = cert
var certChecker ssh.CertChecker
certChecker.IsAuthority = func(auth ssh.PublicKey) bool {
fingerprint := ssh_ca.MakeFingerprint(auth.Marshal())
_, ok := authorizedSigners[fingerprint]
return ok
}
err = certChecker.CheckCert(cert.ValidPrincipals[0], cert)
if err != nil {
err := fmt.Errorf("Cert not valid: %v", err)
return err
}
return nil
}
type listResponseElement struct {
Environment string
Reason string
CertBlob string
}
func newResponseElement(environment string, reason string, certBlob string) listResponseElement {
var element listResponseElement
element.Environment = environment
element.Reason = reason
element.CertBlob = certBlob
return element
}
func (h *certRequestHandler) listPendingRequests(rw http.ResponseWriter, req *http.Request) {
var certRequestID string
err := req.ParseForm()
if err != nil {
http.Error(rw, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
certRequestIDs, ok := req.Form["certRequestId"]
if ok {
certRequestID = certRequestIDs[0]
}
matched, _ := regexp.MatchString("^[A-Z2-7=]{24}$", certRequestID)
if certRequestID != "" && !matched {
http.Error(rw, "Invalid certRequestId", http.StatusBadRequest)
return
}
log.Printf("List pending requests received from %s for request id '%s'\n", req.RemoteAddr, certRequestID)
foundSomething := false
results := make(map[string]listResponseElement)
for k, v := range h.state {
encodedCert := base64.StdEncoding.EncodeToString(v.request.Marshal())
element := newResponseElement(v.environment, v.reason, encodedCert)
// Two ways to use this URL. If caller specified a certRequestId
// then we return only that one. Otherwise everything.
if certRequestID == "" {
results[k] = element
foundSomething = true
} else {
if certRequestID == k {
results[k] = element
foundSomething = true
break
}
}
}
if foundSomething {
output, err := json.Marshal(results)
if err != nil {
http.Error(rw, fmt.Sprintf("Trouble marshaling json response %v", err), http.StatusInternalServerError)
return
}
rw.Write(output)
} else {
http.Error(rw, fmt.Sprintf("No certs found."), http.StatusNotFound)
return
}
}
func (h *certRequestHandler) getRequestStatus(rw http.ResponseWriter, req *http.Request) {
uriVars := mux.Vars(req)
requestID := uriVars["requestID"]
type Response struct {
certSigned bool
cert string
}
if h.state[requestID].certSigned == true {
rw.Write([]byte(h.state[requestID].request.Type()))
rw.Write([]byte(" "))
rw.Write([]byte(base64.StdEncoding.EncodeToString(h.state[requestID].request.Marshal())))
rw.Write([]byte("\n"))
} else {
http.Error(rw, "Cert not signed yet.", http.StatusPreconditionFailed)
}
}
func (h *certRequestHandler) signRequest(rw http.ResponseWriter, req *http.Request) {
uriVars := mux.Vars(req)
requestID := uriVars["requestID"]
_, ok := h.state[requestID]
if !ok {
http.Error(rw, "Unknown request id", http.StatusNotFound)
return
}
var requestData signingRequest
config, environment, err := h.formBoilerplate(req)
requestData.config = config
requestData.environment = environment
if err != nil {
http.Error(rw, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
err = h.extractCertFromRequest(req, &requestData, config.AuthorizedSigners)
if err != nil {
log.Println("Invalid certificate signing request received, ignoring")
http.Error(rw, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
signerFp := ssh_ca.MakeFingerprint(requestData.cert.SignatureKey.Marshal())
// Verifying that the cert being posted to us here matches the one in the
// request. That is, that an attacker isn't use an old signature to sign a
// new/different request id
requestedCert := h.state[requestID].request
requestData.cert.SignatureKey = requestedCert.SignatureKey
requestData.cert.Signature = nil
requestedCert.Signature = nil
// Resetting the Nonce felt wrong. But it turns out that when the signer
// signs the request the act of signing generates a new Nonce. So it will
// never match.
requestedCert.Nonce = []byte("")
requestData.cert.Nonce = []byte("")
if !bytes.Equal(requestedCert.Marshal(), requestData.cert.Marshal()) {
log.Println("Signature was valid, but cert didn't match.")
log.Printf("Orig req: %#v\n", requestedCert)
log.Printf("Sign req: %#v\n", requestData.cert)
http.Error(rw, "Signature was valid, but cert didn't match.", http.StatusBadRequest)
return
}
h.state[requestID].signatures[signerFp] = true
log.Printf("Signature for serial %d id %s received from %s (%s) @ %s and determined valid\n",
requestData.cert.Serial, requestID, signerFp, config.AuthorizedSigners[signerFp], req.RemoteAddr)
if len(h.state[requestID].signatures) >= config.NumberSignersRequired {
log.Printf("Received %d signatures for %s, signing now.\n", len(h.state[requestID].signatures), requestID)
signers, err := h.sshAgent.Signers()
var signer *ssh.Signer
if err != nil {
log.Println("No keys found.")
} else {
for i := range signers {
fp := ssh_ca.MakeFingerprint(signers[i].PublicKey().Marshal())
if fp == config.SigningKeyFingerprint {
signer = &signers[i]
break
}
}
}
if signer == nil {
log.Printf("Couldn't find signing key for request %s, unable to sign request\n", requestID)
http.Error(rw, "Couldn't find signing key, unable to sign. Sorry.", http.StatusNotFound)
return
}
stateInfo := h.state[requestID]
stateInfo.request.SignCert(rand.Reader, *signer)
stateInfo.certSigned = true
// this is weird. see: https://code.google.com/p/go/issues/detail?id=3117
h.state[requestID] = stateInfo
}
}
func main() {
home := os.Getenv("HOME")
if home == "" {
home = "/"
}
configPath := home + "/.ssh_ca/sign_certd_config.json"
flag.StringVar(&configPath, "configPath", configPath, "Path to config json.")
flag.Parse()
config, err := ssh_ca.LoadSignerdConfig(configPath)
if err != nil {
log.Println("Load Config failed:", err)
os.Exit(1)
}
log.Println("Server running version", buildVersion)
log.Println("Server started with config", config)
log.Println("Using SSH agent at", os.Getenv("SSH_AUTH_SOCK"))
conn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
log.Println("Dial failed:", err)
os.Exit(1)
}
sshAgent := agent.NewClient(conn)
var requestHandler certRequestHandler
requestHandler.Config = config
requestHandler.state = make(map[string]certRequest)
requestHandler.NextSerial = make(chan uint64)
go func() {
var serial uint64
for serial = 1; ; serial++ {
requestHandler.NextSerial <- serial
}
}()
requestHandler.sshAgent = sshAgent
r := mux.NewRouter()
requests := r.Path("/cert/requests").Subrouter()
requests.Methods("POST").HandlerFunc(requestHandler.createSigningRequest)
requests.Methods("GET").HandlerFunc(requestHandler.listPendingRequests)
request := r.Path("/cert/requests/{requestID}").Subrouter()
request.Methods("GET").HandlerFunc(requestHandler.getRequestStatus)
request.Methods("POST").HandlerFunc(requestHandler.signRequest)
http.ListenAndServe(":8080", r)
}