forked from adamlaska/boulder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
446 lines (403 loc) · 14.2 KB
/
main.go
File metadata and controls
446 lines (403 loc) · 14.2 KB
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package notmain
import (
"context"
"crypto/x509"
"encoding/asn1"
"encoding/hex"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
"time"
capb "github.com/letsencrypt/boulder/ca/proto"
"github.com/letsencrypt/boulder/cmd"
"github.com/letsencrypt/boulder/core"
berrors "github.com/letsencrypt/boulder/errors"
"github.com/letsencrypt/boulder/features"
bgrpc "github.com/letsencrypt/boulder/grpc"
"github.com/letsencrypt/boulder/issuance"
blog "github.com/letsencrypt/boulder/log"
"github.com/letsencrypt/boulder/metrics"
sapb "github.com/letsencrypt/boulder/sa/proto"
"google.golang.org/grpc"
)
var usageString = `
name:
orphan-finder - Reads orphaned certificates from a boulder-ca log or a der file and adds them to the database
usage:
orphan-finder parse-ca-log --config <path> --log-file <path>
orphan-finder parse-der --config <path> --der-file <path> --regID <registration-id>
command descriptions:
parse-ca-log Parses boulder-ca logs to add multiple orphaned certificates
parse-der Parses a single orphaned DER certificate file and adds it to the database
`
type Config struct {
TLS cmd.TLSConfig
SAService *cmd.GRPCClientConfig
OCSPGeneratorService *cmd.GRPCClientConfig
Syslog cmd.SyslogConfig
// Backdate specifies how to adjust a certificate's NotBefore date to get back
// to the original issued date. It should match the value used in
// `test/config/ca.json` for the CA "backdate" value.
Backdate cmd.ConfigDuration
// IssuerCerts is a list of paths to all intermediate certificates which may
// have been used to issue certificates in the last 90 days. These are used
// to form OCSP generation requests.
IssuerCerts []string
Features map[string]bool
}
type ocspGenerator interface {
GenerateOCSP(context.Context, *capb.GenerateOCSPRequest, ...grpc.CallOption) (*capb.OCSPResponse, error)
}
// orphanType is a numeric identifier for the type of orphan being processed.
type orphanType int
const (
// unknownOrphan indicates an orphan of an unknown type
unknownOrphan orphanType = iota
// certOrphan indicates an orphaned final certificate type
certOrphan
// precertOrphan indicates an orphaned precertificate type
precertOrphan
)
// String returns a human representation of the orphanType and the expected
// label in the orphaning message for that type, or "unknown" if it isn't
// a known orphan type.
func (t orphanType) String() string {
switch t {
case certOrphan:
return "certificate"
case precertOrphan:
return "precertificate"
default:
return "unknown"
}
}
// An orphaned cert log line must contain at least the following tokens:
// "orphaning", "(pre)?certificate", "cert=[\w+]", "issuerID=[\d+]", and "regID=[\d]".
// For example:
// `[AUDIT] Failed RPC to store at SA, orphaning precertificate: serial=[04asdf1234], cert=[MIIdeafbeef], issuerID=[112358], regID=[1001], orderID=[1002], err=[Timed out]`
// The orphan-finder does not care about the serial, error, or orderID.
type parsedLine struct {
certDER []byte
issuerID int64
regID int64
}
var (
derOrphan = regexp.MustCompile(`cert=\[([0-9a-f]+)\]`)
regOrphan = regexp.MustCompile(`regID=\[(\d+)\]`)
issuerOrphan = regexp.MustCompile(`issuerID=\[(\d+)\]`)
errAlreadyExists = fmt.Errorf("Certificate already exists in DB")
)
// orphanTypeForCert returns precertOrphan if the certificate has the RFC 6962
// CT poison extension, or certOrphan if it does not. If the certificate is nil
// unknownOrphan is returned.
func orphanTypeForCert(cert *x509.Certificate) orphanType {
if cert == nil {
return unknownOrphan
}
// RFC 6962 Section 3.1 - https://tools.ietf.org/html/rfc6962#section-3.1
poisonExt := asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 11129, 2, 4, 3}
for _, ext := range cert.Extensions {
if ext.Id.Equal(poisonExt) {
return precertOrphan
}
}
return certOrphan
}
// checkDER parses the provided DER bytes and uses the resulting certificate's
// serial to check if there is an existing precertificate or certificate for the
// provided DER. If there is a matching precert/cert serial then
// errAlreadyExists and the orphanType are returned. If there is no matching
// precert/cert serial then the parsed certificate and orphanType are returned.
func checkDER(sai sapb.StorageAuthorityCertificateClient, der []byte) (*x509.Certificate, orphanType, error) {
ctx := context.Background()
orphan, err := x509.ParseCertificate(der)
if err != nil {
return nil, unknownOrphan, fmt.Errorf("Failed to parse orphan DER: %s", err)
}
orphanSerial := core.SerialToString(orphan.SerialNumber)
orphanTyp := orphanTypeForCert(orphan)
switch orphanTyp {
case certOrphan:
_, err = sai.GetCertificate(ctx, &sapb.Serial{Serial: orphanSerial})
case precertOrphan:
_, err = sai.GetPrecertificate(ctx, &sapb.Serial{Serial: orphanSerial})
default:
err = errors.New("unknown orphan type")
}
if err == nil {
return nil, orphanTyp, errAlreadyExists
}
if errors.Is(err, berrors.NotFound) {
return orphan, orphanTyp, nil
}
return nil, orphanTyp, fmt.Errorf("Existing %s lookup failed: %s", orphanTyp, err)
}
func parseLogLine(line string, logger blog.Logger) (parsedLine, error) {
derStr := derOrphan.FindStringSubmatch(line)
if len(derStr) <= 1 {
return parsedLine{}, fmt.Errorf("unable to find cert der: %s", line)
}
der, err := hex.DecodeString(derStr[1])
if err != nil {
return parsedLine{}, fmt.Errorf("unable to decode hex der from [%s]: %s", line, err)
}
regStr := regOrphan.FindStringSubmatch(line)
if len(regStr) <= 1 {
return parsedLine{}, fmt.Errorf("unable to find regID: %s", line)
}
regID, err := strconv.ParseInt(regStr[1], 10, 64)
if err != nil {
return parsedLine{}, fmt.Errorf("unable to parse regID from [%s]: %s", line, err)
}
issuerStr := issuerOrphan.FindStringSubmatch(line)
if len(issuerStr) <= 1 {
return parsedLine{}, fmt.Errorf("unable to find issuerID: %s", line)
}
issuerID, err := strconv.ParseInt(issuerStr[1], 10, 64)
if err != nil {
return parsedLine{}, fmt.Errorf("unable to parse issuerID from [%s]: %s", line, err)
}
return parsedLine{
certDER: der,
regID: regID,
issuerID: issuerID,
}, nil
}
type orphanFinder struct {
sa sapb.StorageAuthorityCertificateClient
ca ocspGenerator
logger blog.Logger
issuers map[issuance.IssuerNameID]*issuance.Certificate
backdate time.Duration
}
func newOrphanFinder(configFile string) *orphanFinder {
configJSON, err := ioutil.ReadFile(configFile)
cmd.FailOnError(err, "Failed to read config file")
var conf Config
err = json.Unmarshal(configJSON, &conf)
cmd.FailOnError(err, "Failed to parse config file")
err = features.Set(conf.Features)
cmd.FailOnError(err, "Failed to set feature flags")
logger := cmd.NewLogger(conf.Syslog)
tlsConfig, err := conf.TLS.Load()
cmd.FailOnError(err, "TLS config")
clientMetrics := bgrpc.NewClientMetrics(metrics.NoopRegisterer)
saConn, err := bgrpc.ClientSetup(conf.SAService, tlsConfig, clientMetrics, cmd.Clock())
cmd.FailOnError(err, "Failed to load credentials and create gRPC connection to SA")
sac := sapb.NewStorageAuthorityClient(saConn)
caConn, err := bgrpc.ClientSetup(conf.OCSPGeneratorService, tlsConfig, clientMetrics, cmd.Clock())
cmd.FailOnError(err, "Failed to load credentials and create gRPC connection to CA")
cac := capb.NewOCSPGeneratorClient(caConn)
issuers := make(map[issuance.IssuerNameID]*issuance.Certificate)
for _, issuerCertPath := range conf.IssuerCerts {
c, err := issuance.LoadCertificate(issuerCertPath)
cmd.FailOnError(err, "Failed to load issuer certificate")
issuers[c.NameID()] = c
}
return &orphanFinder{
sa: sac,
ca: cac,
logger: logger,
issuers: issuers,
backdate: conf.Backdate.Duration,
}
}
// parseCALog reads a log file, and attempts to parse and store any orphans from
// each line of it. It outputs stats about how many cert and precert orphans it
// found, and how many it successfully stored.
func (opf *orphanFinder) parseCALog(logPath string) {
ctx := context.Background()
logData, err := ioutil.ReadFile(logPath)
cmd.FailOnError(err, "Failed to read log file")
var certOrphansFound, certOrphansAdded, precertOrphansFound, precertOrphansAdded int64
for _, line := range strings.Split(string(logData), "\n") {
if line == "" {
continue
}
found, added, typ := opf.storeLogLine(ctx, line)
var foundStat, addStat *int64
switch typ {
case certOrphan:
foundStat = &certOrphansFound
addStat = &certOrphansAdded
case precertOrphan:
foundStat = &precertOrphansFound
addStat = &precertOrphansAdded
default:
opf.logger.Errf("Found orphan type %s", typ)
continue
}
if found {
*foundStat++
if added {
*addStat++
}
}
}
opf.logger.Infof("Found %d certificate orphans and added %d to the database", certOrphansFound, certOrphansAdded)
opf.logger.Infof("Found %d precertificate orphans and added %d to the database", precertOrphansFound, precertOrphansAdded)
}
// storeLogLine attempts to parse one log line according to the format used when
// orphaning certificates and precertificates. It returns two booleans and the
// orphanType: The first boolean is true if the line was a match, and the second
// is true if the orphan was successfully added to the DB. As part of adding an
// orphan to the DB, it requests a fresh OCSP response from the CA to store
// alongside the precertificate/certificate.
func (opf *orphanFinder) storeLogLine(ctx context.Context, line string) (found bool, added bool, typ orphanType) {
// At a minimum, the log line should contain the word "orphaning" and the token
// "cert=". If it doesn't have those, short-circuit.
if (!strings.Contains(line, fmt.Sprintf("orphaning %s", certOrphan)) &&
!strings.Contains(line, fmt.Sprintf("orphaning %s", precertOrphan))) ||
!strings.Contains(line, "cert=") {
return false, false, unknownOrphan
}
parsed, err := parseLogLine(line, opf.logger)
if err != nil {
opf.logger.AuditErr(fmt.Sprintf("Couldn't parse log line: %s", err))
return true, false, unknownOrphan
}
// Parse the DER, determine the orphan type, and ensure it doesn't already
// exist in the DB
cert, typ, err := checkDER(opf.sa, parsed.certDER)
if err != nil {
logFunc := opf.logger.Errf
if err == errAlreadyExists {
logFunc = opf.logger.Infof
}
logFunc("%s, [%s]", err, line)
return true, false, typ
}
// generate an OCSP response
response, err := opf.generateOCSP(ctx, cert)
if err != nil {
opf.logger.AuditErrf("Couldn't generate OCSP: %s, [%s]", err, line)
return true, false, typ
}
// We use `cert.NotBefore` as the issued date to avoid the SA tagging this
// certificate with an issued date of the current time when we know it was an
// orphan issued in the past. Because certificates are backdated we need to
// add the backdate duration to find the true issued time.
issuedDate := cert.NotBefore.Add(opf.backdate)
switch typ {
case certOrphan:
_, err = opf.sa.AddCertificate(ctx, &sapb.AddCertificateRequest{
Der: parsed.certDER,
RegID: parsed.regID,
Ocsp: response,
Issued: issuedDate.UnixNano(),
})
case precertOrphan:
_, err = opf.sa.AddPrecertificate(ctx, &sapb.AddCertificateRequest{
Der: parsed.certDER,
RegID: parsed.regID,
Ocsp: response,
Issued: issuedDate.UnixNano(),
IssuerID: parsed.issuerID,
})
default:
// Shouldn't happen but be defensive anyway
err = errors.New("unknown orphan type")
}
if err != nil {
opf.logger.AuditErrf("Failed to store certificate: %s, [%s]", err, line)
return true, false, typ
}
return true, true, typ
}
// parseDER loads and attempts to store a single orphan from a single DER file.
func (opf *orphanFinder) parseDER(derPath string, regID int64) {
ctx := context.Background()
der, err := ioutil.ReadFile(derPath)
cmd.FailOnError(err, "Failed to read DER file")
cert, typ, err := checkDER(opf.sa, der)
cmd.FailOnError(err, "Pre-AddCertificate checks failed")
// Because certificates are backdated we need to add the backdate duration
// to find the true issued time.
issuedDate := cert.NotBefore.Add(1 * opf.backdate)
response, err := opf.generateOCSP(ctx, cert)
cmd.FailOnError(err, "Generating OCSP")
switch typ {
case certOrphan:
_, err = opf.sa.AddCertificate(ctx, &sapb.AddCertificateRequest{
Der: der,
RegID: regID,
Ocsp: response,
Issued: issuedDate.UnixNano(),
})
case precertOrphan:
_, err = opf.sa.AddPrecertificate(ctx, &sapb.AddCertificateRequest{
Der: der,
RegID: regID,
Ocsp: response,
Issued: issuedDate.UnixNano(),
})
default:
err = errors.New("unknown orphan type")
}
cmd.FailOnError(err, "Failed to add certificate to database")
}
// generateOCSP asks the CA to generate a new OCSP response for the given cert.
func (opf *orphanFinder) generateOCSP(ctx context.Context, cert *x509.Certificate) ([]byte, error) {
issuerID := issuance.GetIssuerNameID(cert)
_, ok := opf.issuers[issuerID]
if !ok {
return nil, errors.New("unrecognized issuer for orphan")
}
ocspResponse, err := opf.ca.GenerateOCSP(ctx, &capb.GenerateOCSPRequest{
Serial: core.SerialToString(cert.SerialNumber),
IssuerID: int64(issuerID),
Status: string(core.OCSPStatusGood),
Reason: 0,
RevokedAt: 0,
})
if err != nil {
return nil, err
}
return ocspResponse.Response, nil
}
func main() {
if len(os.Args) <= 2 {
fmt.Fprint(os.Stderr, usageString)
os.Exit(1)
}
command := os.Args[1]
flagSet := flag.NewFlagSet(command, flag.ContinueOnError)
configFile := flagSet.String("config", "", "File path to the configuration file for this service")
logPath := flagSet.String("log-file", "", "Path to boulder-ca log file to parse")
derPath := flagSet.String("der-file", "", "Path to DER certificate file")
regID := flagSet.Int64("regID", 0, "Registration ID of user who requested the certificate")
err := flagSet.Parse(os.Args[2:])
cmd.FailOnError(err, "Error parsing flagset")
usage := func() {
fmt.Fprintf(os.Stderr, "%s\nargs:", usageString)
flagSet.PrintDefaults()
os.Exit(1)
}
if *configFile == "" {
usage()
}
opf := newOrphanFinder(*configFile)
switch command {
case "parse-ca-log":
if *logPath == "" {
usage()
}
opf.parseCALog(*logPath)
case "parse-der":
if *derPath == "" || *regID == 0 {
usage()
}
opf.parseDER(*derPath, *regID)
default:
usage()
}
}
func init() {
cmd.RegisterCommand("orphan-finder", main)
}