-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
400 lines (366 loc) · 10.6 KB
/
main.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright 2016 SMFS Inc. DBA GRIMM. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
// Implementation of an OCSP responder defined by RFC 6960
package main
import (
"flag"
"bufio"
"bytes"
"crypto"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"gitlab.monarch-ares.com/samuel.lock/gocspd/crypto"
"io/ioutil"
"log"
"math/big"
"net/http"
"os"
"strings"
"time"
)
type OCSPResponder struct {
IndexFile string
RespKeyFile string
RespCertFile string
CaCertFile string
LogFile string
LogToStdout bool
Strict bool
Port int
Address string
Ssl bool
IndexEntries []IndexEntry
IndexModTime time.Time
CaCert *x509.Certificate
RespCert *x509.Certificate
NonceList [][]byte
}
// I decided on these defaults based on what I was using
func Responder() *OCSPResponder {
return &OCSPResponder{
IndexFile: "index.txt",
RespKeyFile: "responder.key",
RespCertFile: "responder.crt",
CaCertFile: "ca.crt",
LogFile: "/var/log/gocsp-responder.log",
LogToStdout: false,
Strict: false,
Port: 8888,
Address: "",
Ssl: false,
IndexEntries: nil,
IndexModTime: time.Time{},
CaCert: nil,
RespCert: nil,
NonceList: nil,
}
}
// Creates an OCSP http handler and returns it
func (self *OCSPResponder) makeHandler() func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log.Print(fmt.Sprintf("Got %s request from %s", r.Method, r.RemoteAddr))
if self.Strict && r.Header.Get("Content-Type") != "application/ocsp-request" {
log.Println("Strict mode requires correct Content-Type header")
w.WriteHeader(http.StatusBadRequest)
return
}
b := new(bytes.Buffer)
switch r.Method {
case "POST":
b.ReadFrom(r.Body)
case "GET":
log.Println(r.URL.Path)
gd, err := base64.StdEncoding.DecodeString(r.URL.Path[1:])
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
r := bytes.NewReader(gd)
b.ReadFrom(r)
default:
log.Println("Unsupported request method")
w.WriteHeader(http.StatusBadRequest)
return
}
// parse request, verify, create response
w.Header().Set("Content-Type", "application/ocsp-response")
resp, err := self.verify(b.Bytes())
if err != nil {
log.Print(err)
// technically we should return an ocsp error response. but this is probably fine
w.WriteHeader(http.StatusBadRequest)
return
}
log.Print("Writing response")
w.Write(resp)
}
}
// I only know of two types, but more can be added later
const (
StatusValid = 'V'
StatusRevoked = 'R'
StatusExpired = 'E'
)
type IndexEntry struct {
Status byte
Serial *big.Int // wow I totally called it
// revocation reason may need to be added
IssueTime time.Time
RevocationTime time.Time
DistinguishedName string
}
// function to parse the index file
func (self *OCSPResponder) parseIndex() error {
var t string = "060102150405Z"
finfo, err := os.Stat(self.IndexFile)
if err == nil {
// if the file modtime has changed, then reload the index file
if finfo.ModTime().After(self.IndexModTime) {
log.Print("Index has changed. Updating")
self.IndexModTime = finfo.ModTime()
// clear index entries
self.IndexEntries = self.IndexEntries[:0]
} else {
// the index has not changed. just return
return nil
}
} else {
return err
}
// open and parse the index file
if file, err := os.Open(self.IndexFile); err == nil {
defer file.Close()
s := bufio.NewScanner(file)
for s.Scan() {
var ie IndexEntry
ln := strings.Fields(s.Text())
ie.Status = []byte(ln[0])[0]
ie.IssueTime, _ = time.Parse(t, ln[1])
if ie.Status == StatusValid {
ie.Serial, _ = new(big.Int).SetString(ln[2], 16)
ie.DistinguishedName = ln[4]
ie.RevocationTime = time.Time{} //doesn't matter
} else if ie.Status == StatusRevoked {
ie.Serial, _ = new(big.Int).SetString(ln[3], 16)
ie.DistinguishedName = ln[5]
ie.RevocationTime, _ = time.Parse(t, ln[2])
} else {
// invalid status or bad line. just carry on
continue
}
self.IndexEntries = append(self.IndexEntries, ie)
}
} else {
return err
}
return nil
}
// updates the index if necessary and then searches for the given index in the
// index list
func (self *OCSPResponder) getIndexEntry(s *big.Int) (*IndexEntry, error) {
log.Println(fmt.Sprintf("Looking for serial 0x%x", s))
if err := self.parseIndex(); err != nil {
return nil, err
}
for _, ent := range self.IndexEntries {
if ent.Serial.Cmp(s) == 0 {
return &ent, nil
}
}
return nil, errors.New(fmt.Sprintf("Serial 0x%x not found", s))
}
// parses a pem encoded x509 certificate
func parseCertFile(filename string) (*x509.Certificate, error) {
ct, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
block, _ := pem.Decode(ct)
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, err
}
return cert, nil
}
// parses a PEM encoded PKCS8 private key (RSA only)
func parseKeyFile(filename string) (interface{}, error) {
kt, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
block, _ := pem.Decode(kt)
key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
key, err = x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
}
return key, nil
}
// takes a list of extensions and returns the nonce extension if it is present
func checkForNonceExtension(exts []pkix.Extension) *pkix.Extension {
nonce_oid := asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 48, 1, 2}
for _, ext := range exts {
if ext.Id.Equal(nonce_oid) {
log.Println("Detected nonce extension")
return &ext
}
}
return nil
}
func (self *OCSPResponder) verifyIssuer(req *ocsp.Request) error {
h := req.HashAlgorithm.New()
h.Write(self.CaCert.RawSubject)
if bytes.Compare(h.Sum(nil), req.IssuerNameHash) != 0 {
return errors.New("Issuer name does not match")
}
h.Reset()
var publicKeyInfo struct {
Algorithm pkix.AlgorithmIdentifier
PublicKey asn1.BitString
}
if _, err := asn1.Unmarshal(self.CaCert.RawSubjectPublicKeyInfo, &publicKeyInfo); err != nil {
return err
}
h.Write(publicKeyInfo.PublicKey.RightAlign())
if bytes.Compare(h.Sum(nil), req.IssuerKeyHash) != 0 {
return errors.New("Issuer key hash does not match")
}
return nil
}
// takes the der encoded ocsp request, verifies it, and creates a response
func (self *OCSPResponder) verify(rawreq []byte) ([]byte, error) {
var status int
var revokedAt time.Time
// parse the request
req, exts, err := ocsp.ParseRequest(rawreq)
if err != nil {
log.Println(err)
return nil, err
}
//make sure the request is valid
if err := self.verifyIssuer(req); err != nil {
log.Println(err)
return nil, err
}
// get the index entry, if it exists
ent, err := self.getIndexEntry(req.SerialNumber)
if err != nil {
log.Println(err)
status = ocsp.Unknown
} else {
log.Print(fmt.Sprintf("Found entry %+v", ent))
if ent.Status == StatusRevoked {
log.Print("This certificate is revoked")
status = ocsp.Revoked
revokedAt = ent.RevocationTime
} else if ent.Status == StatusValid {
log.Print("This certificate is valid")
status = ocsp.Good
}
}
// parse key file
// perhaps I should zero this out after use
keyi, err := parseKeyFile(self.RespKeyFile)
if err != nil {
return nil, err
}
key, ok := keyi.(crypto.Signer)
if !ok {
return nil, errors.New("Could not make key a signer")
}
// check for nonce extension
var responseExtensions []pkix.Extension
nonce := checkForNonceExtension(exts)
// check if the nonce has been used before
if self.NonceList == nil {
self.NonceList = make([][]byte, 10)
}
if nonce != nil {
for _, n := range self.NonceList {
if bytes.Compare(n, nonce.Value) == 0 {
return nil, errors.New("This nonce has already been used")
}
}
self.NonceList = append(self.NonceList, nonce.Value)
responseExtensions = append(responseExtensions, *nonce)
}
// construct response template
rtemplate := ocsp.Response{
Status: status,
SerialNumber: req.SerialNumber,
Certificate: self.RespCert,
RevocationReason: ocsp.Unspecified,
IssuerHash: req.HashAlgorithm,
RevokedAt: revokedAt,
ThisUpdate: time.Now().AddDate(0, 0, -1).UTC(),
//adding 1 day after the current date. This ocsp library sets the default date to epoch which makes ocsp clients freak out.
NextUpdate: time.Now().AddDate(0, 0, 1).UTC(),
Extensions: exts,
}
// make a response to return
resp, err := ocsp.CreateResponse(self.CaCert, self.RespCert, rtemplate, key)
if err != nil {
return nil, err
}
return resp, err
}
// setup an ocsp server instance with configured values
func (self *OCSPResponder) Serve() error {
// setup logging
if !self.LogToStdout {
lf, err := os.OpenFile(self.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
if err != nil {
log.Fatal("Could not open log file " + self.LogFile)
}
defer lf.Close()
log.SetOutput(lf)
}
//the certs should not change, so lets keep them in memory
cacert, err := parseCertFile(self.CaCertFile)
if err != nil {
log.Fatal(err)
return err
}
respcert, err := parseCertFile(self.RespCertFile)
if err != nil {
log.Fatal(err)
return err
}
self.CaCert = cacert
self.RespCert = respcert
// get handler and serve
handler := self.makeHandler()
http.HandleFunc("/", handler)
listenOn := fmt.Sprintf("%s:%d", self.Address, self.Port)
log.Println(fmt.Sprintf("GOCSP-Responder starting on %s with SSL:%t", listenOn, self.Ssl))
if self.Ssl {
http.ListenAndServeTLS(listenOn, self.RespCertFile, self.RespKeyFile, nil)
} else {
http.ListenAndServe(listenOn, nil)
}
return nil
}
func main() {
resp := Responder()
flag.StringVar(&resp.IndexFile, "index", resp.IndexFile, "CA index filename")
flag.StringVar(&resp.CaCertFile, "cacert", resp.CaCertFile, "CA certificate filename")
flag.StringVar(&resp.RespCertFile, "rcert", resp.RespCertFile, "responder certificate filename")
flag.StringVar(&resp.RespKeyFile, "rkey", resp.RespKeyFile, "responder key filename")
flag.StringVar(&resp.LogFile, "logfile", resp.LogFile, "file to log to")
flag.StringVar(&resp.Address, "bind", resp.Address, "bind address")
flag.IntVar(&resp.Port, "port", resp.Port, "listening port")
flag.BoolVar(&resp.Ssl, "ssl", resp.Ssl, "use SSL, this is not widely supported and not recommended")
flag.BoolVar(&resp.Strict, "strict", resp.Strict, "require content type HTTP header")
flag.BoolVar(&resp.LogToStdout, "stdout", resp.LogToStdout, "log to stdout, not the log file")
flag.Parse()
resp.Serve()
}