forked from google/certificate-transparency-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
323 lines (289 loc) · 9.89 KB
/
scanner.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
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package scanner holds code for iterating through the contents of a CT log.
package scanner
import (
"container/list"
"context"
"fmt"
"log"
"sync"
"sync/atomic"
"time"
ct "github.com/google/certificate-transparency-go"
"github.com/google/certificate-transparency-go/client"
"github.com/google/certificate-transparency-go/x509"
)
// ScannerOptions holds configuration options for the Scanner
type ScannerOptions struct {
// Custom matcher for x509 Certificates, functor will be called for each
// Certificate found during scanning.
Matcher Matcher
// Match precerts only (Matcher still applies to precerts)
PrecertOnly bool
// Number of entries to request in one batch from the Log
BatchSize int
// Number of concurrent matchers to run
NumWorkers int
// Number of concurrent fethers to run
ParallelFetch int
// Log entry index to start fetching & matching at
StartIndex int64
// Don't print any status messages to stdout
Quiet bool
}
// DefaultScannerOptions creates a new ScannerOptions struct with sensible defaults.
func DefaultScannerOptions() *ScannerOptions {
return &ScannerOptions{
Matcher: &MatchAll{},
PrecertOnly: false,
BatchSize: 1000,
NumWorkers: 1,
ParallelFetch: 1,
StartIndex: 0,
Quiet: false,
}
}
// Scanner is a tool to scan all the entries in a CT Log.
type Scanner struct {
// Client used to talk to the CT log instance
logClient *client.LogClient
// Configuration options for this Scanner instance
opts ScannerOptions
// Counter of the number of certificates scanned
certsProcessed int64
// Counter of the number of precertificates encountered during the scan.
precertsSeen int64
unparsableEntries int64
entriesWithNonFatalErrors int64
Log func(msg string)
}
// entryInfo represents information about a log entry
type entryInfo struct {
// The index of the entry containing the LeafInput in the log
index int64
// The log entry returned by the log server
entry ct.LeafEntry
}
// fetchRange represents a range of certs to fetch from a CT log
type fetchRange struct {
start int64 // inclusive
end int64 // inclusive
}
// Takes the error returned by either x509.ParseCertificate() or
// x509.ParseTBSCertificate() and determines if it's non-fatal or otherwise.
// In the case of non-fatal errors, the error will be logged,
// entriesWithNonFatalErrors will be incremented, and the return value will be
// false.
// Fatal errors will cause the function to return true.
// When err is nil, this method does nothing.
func (s *Scanner) isCertErrorFatal(err error, logEntry *ct.LogEntry, index int64) bool {
if err == nil {
// No error to handle
return false
} else if _, ok := err.(x509.NonFatalErrors); ok {
atomic.AddInt64(&s.entriesWithNonFatalErrors, 1)
// We'll make a note, but continue.
s.Log(fmt.Sprintf("Non-fatal error in %+v at index %d: %s", logEntry.Leaf.TimestampedEntry.EntryType, index, err.Error()))
return false
}
return true
}
// Processes the given entry in the specified log.
func (s *Scanner) processEntry(index int64, entry ct.LeafEntry, foundCert func(*ct.LogEntry), foundPrecert func(*ct.LogEntry)) error {
atomic.AddInt64(&s.certsProcessed, 1)
logEntry, err := ct.LogEntryFromLeaf(index, &entry)
if s.isCertErrorFatal(err, logEntry, index) {
return fmt.Errorf("failed to parse [pre-]certificate in MerkleTreeLeaf: %v", err)
}
switch {
case logEntry.X509Cert != nil:
if s.opts.PrecertOnly {
// Only interested in precerts and this is an X.509 cert, early-out.
return nil
}
if s.opts.Matcher.CertificateMatches(logEntry.X509Cert) {
foundCert(logEntry)
}
case logEntry.Precert != nil:
if s.opts.Matcher.PrecertificateMatches(logEntry.Precert) {
foundPrecert(logEntry)
}
atomic.AddInt64(&s.precertsSeen, 1)
default:
return fmt.Errorf("saw unknown entry type: %v", logEntry.Leaf.TimestampedEntry.EntryType)
}
return nil
}
// Worker function to match certs.
// Accepts MatcherJobs over the entries channel, and processes them.
// Returns true over the done channel when the entries channel is closed.
func (s *Scanner) matcherJob(entries <-chan entryInfo, foundCert func(*ct.LogEntry), foundPrecert func(*ct.LogEntry)) {
for e := range entries {
if err := s.processEntry(e.index, e.entry, foundCert, foundPrecert); err != nil {
atomic.AddInt64(&s.unparsableEntries, 1)
s.Log(fmt.Sprintf("Failed to parse entry at index %d: %s", e.index, err.Error()))
}
}
}
// Worker function for fetcher jobs.
// Accepts cert ranges to fetch over the ranges channel, and if the fetch is
// successful sends the individual LeafInputs out (as MatcherJobs) into the
// entries channel for the matchers to chew on.
// Will retry failed attempts to retrieve ranges indefinitely.
// Sends true over the done channel when the ranges channel is closed.
func (s *Scanner) fetcherJob(ctx context.Context, ranges <-chan fetchRange, entries chan<- entryInfo) {
for r := range ranges {
success := false
// TODO(alcutter): give up after a while:
for !success {
resp, err := s.logClient.GetRawEntries(ctx, r.start, r.end)
if err != nil {
s.Log(fmt.Sprintf("Problem fetching from log: %s", err.Error()))
continue
}
for _, leafEntry := range resp.Entries {
entries <- entryInfo{r.start, leafEntry}
r.start++
}
if r.start > r.end {
// Only complete if we actually got all the leaves we were
// expecting -- Logs MAY return fewer than the number of
// leaves requested.
success = true
}
}
}
}
func min(a int64, b int64) int64 {
if a < b {
return a
}
return b
}
func max(a int64, b int64) int64 {
if a > b {
return a
}
return b
}
// Pretty prints the passed in number of seconds into a more human readable
// string.
func humanTime(seconds int) string {
nanos := time.Duration(seconds) * time.Second
hours := int(nanos / (time.Hour))
nanos %= time.Hour
minutes := int(nanos / time.Minute)
nanos %= time.Minute
seconds = int(nanos / time.Second)
s := ""
if hours > 0 {
s += fmt.Sprintf("%d hours ", hours)
}
if minutes > 0 {
s += fmt.Sprintf("%d minutes ", minutes)
}
if seconds > 0 {
s += fmt.Sprintf("%d seconds ", seconds)
}
return s
}
// Scan performs a scan against the Log.
// For each x509 certificate found, foundCert will be called with the
// index of the entry and certificate itself as arguments. For each precert
// found, foundPrecert will be called with the index of the entry and the raw
// precert string as the arguments.
//
// This method blocks until the scan is complete.
func (s *Scanner) Scan(ctx context.Context, foundCert func(*ct.LogEntry), foundPrecert func(*ct.LogEntry)) error {
s.Log("Starting up...\n")
s.certsProcessed = 0
s.precertsSeen = 0
s.unparsableEntries = 0
s.entriesWithNonFatalErrors = 0
latestSth, err := s.logClient.GetSTH(ctx)
if err != nil {
return fmt.Errorf("failed to GetSTH(): %v", err)
}
s.Log(fmt.Sprintf("Got STH with %d certs", latestSth.TreeSize))
ticker := time.NewTicker(time.Second)
startTime := time.Now()
fetches := make(chan fetchRange, 1000)
jobs := make(chan entryInfo, 100000)
go func() {
for range ticker.C {
certsProcessed := atomic.LoadInt64(&s.certsProcessed)
throughput := float64(certsProcessed) / time.Since(startTime).Seconds()
remainingCerts := int64(latestSth.TreeSize) - int64(s.opts.StartIndex) - certsProcessed
remainingSeconds := int(float64(remainingCerts) / throughput)
remainingString := humanTime(remainingSeconds)
s.Log(fmt.Sprintf("Processed: %d certs (to index %d). Throughput: %3.2f ETA: %s\n", certsProcessed,
s.opts.StartIndex+int64(certsProcessed), throughput, remainingString))
}
}()
var ranges list.List
for start := s.opts.StartIndex; start < int64(latestSth.TreeSize); {
end := min(start+int64(s.opts.BatchSize), int64(latestSth.TreeSize)) - 1
ranges.PushBack(fetchRange{start, end})
start = end + 1
}
var fetcherWG sync.WaitGroup
var matcherWG sync.WaitGroup
// Start matcher workers
for w := 0; w < s.opts.NumWorkers; w++ {
matcherWG.Add(1)
go func(w int) {
defer matcherWG.Done()
s.matcherJob(jobs, foundCert, foundPrecert)
s.Log(fmt.Sprintf("Matcher %d finished", w))
}(w)
}
// Start fetcher workers
for w := 0; w < s.opts.ParallelFetch; w++ {
fetcherWG.Add(1)
go func(w int) {
defer fetcherWG.Done()
s.fetcherJob(ctx, fetches, jobs)
s.Log(fmt.Sprintf("Fetcher %d finished", w))
}(w)
}
for r := ranges.Front(); r != nil; r = r.Next() {
fetches <- r.Value.(fetchRange)
}
close(fetches)
fetcherWG.Wait()
close(jobs)
matcherWG.Wait()
s.Log(fmt.Sprintf("Completed %d certs in %s", atomic.LoadInt64(&s.certsProcessed), humanTime(int(time.Since(startTime).Seconds()))))
s.Log(fmt.Sprintf("Saw %d precerts", atomic.LoadInt64(&s.precertsSeen)))
s.Log(fmt.Sprintf("%d unparsable entries, %d non-fatal errors", atomic.LoadInt64(&s.unparsableEntries), atomic.LoadInt64(&s.entriesWithNonFatalErrors)))
return nil
}
// NewScanner creates a new Scanner instance using client to talk to the log,
// taking configuration options from opts.
func NewScanner(client *client.LogClient, opts ScannerOptions) *Scanner {
var scanner Scanner
scanner.logClient = client
// Set a default match-everything regex if none was provided:
if opts.Matcher == nil {
opts.Matcher = &MatchAll{}
}
if opts.Quiet {
scanner.Log = func(msg string) {}
} else {
scanner.Log = func(msg string) { log.Print(msg) }
}
scanner.opts = opts
return &scanner
}