-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
196 lines (174 loc) · 4.05 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
package main
import (
"bytes"
"crypto/sha256"
"crypto/x509"
"encoding/pem"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/cloudflare/cfssl/crypto/pkcs7"
)
// ParseOneCertificateFromPEM attempts to parse one PEM encoded certificate object,
// either a raw x509 certificate or a PKCS #7 structure possibly containing
// multiple certificates, from the top of certsPEM, which itself may
// contain multiple PEM encoded certificate objects.
func ParseOneCertificateFromPEM(certsPEM []byte) ([]*x509.Certificate, []byte, error) {
block, rest := pem.Decode(certsPEM)
if block == nil {
return nil, rest, nil
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
pkcs7data, err := pkcs7.ParsePKCS7(block.Bytes)
if err != nil {
return nil, rest, err
}
if pkcs7data.ContentInfo != "SignedData" {
return nil, rest, errors.New("only PKCS #7 Signed Data Content Info supported for certificate parsing")
}
certs := pkcs7data.Content.SignedData.Certificates
if certs == nil {
return nil, rest, errors.New("PKCS #7 structure contains no certificates")
}
return certs, rest, nil
}
var certs = []*x509.Certificate{cert}
return certs, rest, nil
}
// ParseCertificatesPEM parses a sequence of PEM-encoded certificate and returns them,
// can handle PEM encoded PKCS #7 structures.
func ParseCertificatesPEM(certsPEM []byte) ([]*x509.Certificate, error) {
var certs []*x509.Certificate
var err error
certsPEM = bytes.TrimSpace(certsPEM)
for len(certsPEM) > 0 {
var cert []*x509.Certificate
cert, certsPEM, err = ParseOneCertificateFromPEM(certsPEM)
if err != nil {
return nil, err
} else if cert == nil {
break
}
certs = append(certs, cert...)
}
if len(certsPEM) > 0 {
return nil, err
}
return certs, nil
}
var sentinel = []byte("-----BEGIN CERTIFICATE-----")
func likelyCert(path string) bool {
f, err := os.Open(path)
if err != nil {
return false
}
buf := make([]byte, 512)
_, err = f.Read(buf)
if err != nil {
return false
}
return bytes.Contains(buf, sentinel)
}
type Pool struct {
bySPKI map[string]int
paths map[string][]string
certs []*x509.Certificate
}
func NewCertPool() *Pool {
return &Pool{
bySPKI: make(map[string]int),
paths: make(map[string][]string),
}
}
func (s *Pool) AddCert(path string, cert *x509.Certificate) {
spki := Fingerprint(cert)
s.paths[spki] = append(s.paths[spki], path)
if _, ok := s.bySPKI[spki]; !ok {
s.bySPKI[spki] = len(s.certs)
s.certs = append(s.certs, cert)
}
}
func Fingerprint(cert *x509.Certificate) string {
h := sha256.New()
h.Write(cert.RawSubjectPublicKeyInfo)
return fmt.Sprintf("%x", h.Sum(nil))
}
type Node struct {
Id string
Paths []string
Cert *x509.Certificate
Parent *Node
Children []*Node
}
func (n *Node) Print(indent int) {
spacer := strings.Repeat(" ", indent)
fmt.Printf("%s[%s]\n", spacer, n.Cert.Subject)
for _, path := range n.Paths {
fmt.Printf(" %s%s\n", spacer, path)
}
for _, c := range n.Children {
c.Print(indent + 1)
}
}
var (
flagPath string
)
func main() {
flag.StringVar(&flagPath, "path", ".", "directory to search for certificates")
flag.Parse()
pool := NewCertPool()
err := filepath.Walk(flagPath,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !likelyCert(path) {
return nil
}
buf, err := ioutil.ReadFile(path)
if err != nil {
return nil
}
certs, err := ParseCertificatesPEM(buf)
if err != nil {
return nil
}
for _, cert := range certs {
pool.AddCert(path, cert)
}
return nil
})
if err != nil {
log.Println(err)
}
nodes := make(map[string]*Node)
for spki, i := range pool.bySPKI {
nodes[spki] = &Node{
Id: spki,
Paths: pool.paths[spki],
Cert: pool.certs[i],
}
}
for a, c := range nodes {
for b, p := range nodes {
if a == b {
continue
}
if c.Cert.CheckSignatureFrom(p.Cert) == nil {
p.Children = append(p.Children, c)
c.Parent = p
}
}
}
for _, n := range nodes {
if len(n.Children) > 0 {
n.Print(0)
}
}
}