-
Notifications
You must be signed in to change notification settings - Fork 1
/
certstats.go
89 lines (72 loc) · 2.21 KB
/
certstats.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
package certificate_searcher
import (
"encoding/hex"
"fmt"
"github.com/cespare/xxhash"
"github.com/steakknife/bloomfilter"
"log"
"strings"
"time"
)
type CertStats struct {
NoCTFingerprints *bloomfilter.Filter
ParentSPKISubjectCounts map[string]uint64
}
const maxElements uint64 = 10000000000
const probCollision float64 = 0.000000001
func NewCertStats() *CertStats {
bf, err := bloomfilter.NewOptimal(maxElements, probCollision)
if err != nil {
log.Fatal(err)
}
return &CertStats{
NoCTFingerprints: bf,
ParentSPKISubjectCounts: make(map[string]uint64),
}
}
// Adds the parent/child if not currently in the Bloom filter, returns whether added or not (already seen)
func (c *CertStats) AddParentChild(parentSPKI []byte, childTBSNoCT []byte) bool {
parentSPKIStr := hex.EncodeToString(parentSPKI)
if _, present := c.ParentSPKISubjectCounts[parentSPKIStr]; !present {
c.ParentSPKISubjectCounts[parentSPKIStr] = 0
}
hash := xxhash.New()
hash.Write(childTBSNoCT)
if c.NoCTFingerprints.Contains(hash) {
return false
}
c.NoCTFingerprints.Add(hash)
c.ParentSPKISubjectCounts[parentSPKIStr] += 1
return true
}
func (c CertStats) String() string {
var str strings.Builder
total := uint64(0)
for _, certificateCount := range c.ParentSPKISubjectCounts {
total += certificateCount
}
str.WriteString(fmt.Sprintf("%d total parent SPKI subjects, %d total certificates (TBSNoCT)\n", len(c.ParentSPKISubjectCounts), total))
for spkiSubject, certificateCount := range c.ParentSPKISubjectCounts {
str.WriteString(fmt.Sprintf("%s,%d\n", spkiSubject, certificateCount))
}
return str.String()
}
type CertInfo struct {
ValidityStart time.Time
ValidityEnd time.Time
ValidationLevel string
TBSNoCTFingerprint []byte
ParentSPKISubject []byte
}
func NewCertInfo(validationLevel string, validityStart time.Time, noCTFingerprint, parentSPKISubjFingerprint[]byte) *CertInfo {
certFP := make([]byte, len(noCTFingerprint))
parentFP := make([]byte, len(parentSPKISubjFingerprint))
copy(certFP, noCTFingerprint)
copy(parentFP, parentSPKISubjFingerprint)
return &CertInfo{
ValidationLevel: validationLevel,
ValidityStart: validityStart,
TBSNoCTFingerprint: certFP,
ParentSPKISubject: parentFP,
}
}