Source file
src/crypto/x509/cert_pool.go
1
2
3
4
5 package x509
6
7 import (
8 "bytes"
9 "crypto/sha256"
10 "encoding/pem"
11 "sync"
12 )
13
14 type sum224 [sha256.Size224]byte
15
16
17 type CertPool struct {
18 byName map[string][]int
19
20
21
22 lazyCerts []lazyCert
23
24
25
26
27
28
29 haveSum map[sum224]bool
30
31
32
33
34
35 systemPool bool
36 }
37
38
39
40 type lazyCert struct {
41
42
43
44
45 rawSubject []byte
46
47
48
49
50 constraint func([]*Certificate) error
51
52
53
54
55
56
57
58
59
60 getCert func() (*Certificate, error)
61 }
62
63
64 func NewCertPool() *CertPool {
65 return &CertPool{
66 byName: make(map[string][]int),
67 haveSum: make(map[sum224]bool),
68 }
69 }
70
71
72
73 func (s *CertPool) len() int {
74 if s == nil {
75 return 0
76 }
77 return len(s.lazyCerts)
78 }
79
80
81 func (s *CertPool) cert(n int) (*Certificate, func([]*Certificate) error, error) {
82 cert, err := s.lazyCerts[n].getCert()
83 return cert, s.lazyCerts[n].constraint, err
84 }
85
86
87 func (s *CertPool) Clone() *CertPool {
88 p := &CertPool{
89 byName: make(map[string][]int, len(s.byName)),
90 lazyCerts: make([]lazyCert, len(s.lazyCerts)),
91 haveSum: make(map[sum224]bool, len(s.haveSum)),
92 systemPool: s.systemPool,
93 }
94 for k, v := range s.byName {
95 indexes := make([]int, len(v))
96 copy(indexes, v)
97 p.byName[k] = indexes
98 }
99 for k := range s.haveSum {
100 p.haveSum[k] = true
101 }
102 copy(p.lazyCerts, s.lazyCerts)
103 return p
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 func SystemCertPool() (*CertPool, error) {
122 if sysRoots := systemRootsPool(); sysRoots != nil {
123 return sysRoots.Clone(), nil
124 }
125
126 return loadSystemRoots()
127 }
128
129 type potentialParent struct {
130 cert *Certificate
131 constraint func([]*Certificate) error
132 }
133
134
135
136 func (s *CertPool) findPotentialParents(cert *Certificate) []potentialParent {
137 if s == nil {
138 return nil
139 }
140
141
142
143
144
145
146
147 var matchingKeyID, oneKeyID, mismatchKeyID []potentialParent
148 for _, c := range s.byName[string(cert.RawIssuer)] {
149 candidate, constraint, err := s.cert(c)
150 if err != nil {
151 continue
152 }
153 kidMatch := bytes.Equal(candidate.SubjectKeyId, cert.AuthorityKeyId)
154 switch {
155 case kidMatch:
156 matchingKeyID = append(matchingKeyID, potentialParent{candidate, constraint})
157 case (len(candidate.SubjectKeyId) == 0 && len(cert.AuthorityKeyId) > 0) ||
158 (len(candidate.SubjectKeyId) > 0 && len(cert.AuthorityKeyId) == 0):
159 oneKeyID = append(oneKeyID, potentialParent{candidate, constraint})
160 default:
161 mismatchKeyID = append(mismatchKeyID, potentialParent{candidate, constraint})
162 }
163 }
164
165 found := len(matchingKeyID) + len(oneKeyID) + len(mismatchKeyID)
166 if found == 0 {
167 return nil
168 }
169 candidates := make([]potentialParent, 0, found)
170 candidates = append(candidates, matchingKeyID...)
171 candidates = append(candidates, oneKeyID...)
172 candidates = append(candidates, mismatchKeyID...)
173 return candidates
174 }
175
176 func (s *CertPool) contains(cert *Certificate) bool {
177 if s == nil {
178 return false
179 }
180 return s.haveSum[sha256.Sum224(cert.Raw)]
181 }
182
183
184 func (s *CertPool) AddCert(cert *Certificate) {
185 if cert == nil {
186 panic("adding nil Certificate to CertPool")
187 }
188 s.addCertFunc(sha256.Sum224(cert.Raw), string(cert.RawSubject), func() (*Certificate, error) {
189 return cert, nil
190 }, nil)
191 }
192
193
194
195
196
197
198 func (s *CertPool) addCertFunc(rawSum224 sum224, rawSubject string, getCert func() (*Certificate, error), constraint func([]*Certificate) error) {
199 if getCert == nil {
200 panic("getCert can't be nil")
201 }
202
203
204 if s.haveSum[rawSum224] {
205 return
206 }
207
208 s.haveSum[rawSum224] = true
209 s.lazyCerts = append(s.lazyCerts, lazyCert{
210 rawSubject: []byte(rawSubject),
211 getCert: getCert,
212 constraint: constraint,
213 })
214 s.byName[rawSubject] = append(s.byName[rawSubject], len(s.lazyCerts)-1)
215 }
216
217
218
219
220
221
222
223 func (s *CertPool) AppendCertsFromPEM(pemCerts []byte) (ok bool) {
224 for len(pemCerts) > 0 {
225 var block *pem.Block
226 block, pemCerts = pem.Decode(pemCerts)
227 if block == nil {
228 break
229 }
230 if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
231 continue
232 }
233
234 certBytes := block.Bytes
235 cert, err := ParseCertificate(certBytes)
236 if err != nil {
237 continue
238 }
239 var lazyCert struct {
240 sync.Once
241 v *Certificate
242 }
243 s.addCertFunc(sha256.Sum224(cert.Raw), string(cert.RawSubject), func() (*Certificate, error) {
244 lazyCert.Do(func() {
245
246 lazyCert.v, _ = ParseCertificate(certBytes)
247 certBytes = nil
248 })
249 return lazyCert.v, nil
250 }, nil)
251 ok = true
252 }
253
254 return ok
255 }
256
257
258
259
260
261
262 func (s *CertPool) Subjects() [][]byte {
263 res := make([][]byte, s.len())
264 for i, lc := range s.lazyCerts {
265 res[i] = lc.rawSubject
266 }
267 return res
268 }
269
270
271 func (s *CertPool) Equal(other *CertPool) bool {
272 if s == nil || other == nil {
273 return s == other
274 }
275 if s.systemPool != other.systemPool || len(s.haveSum) != len(other.haveSum) {
276 return false
277 }
278 for h := range s.haveSum {
279 if !other.haveSum[h] {
280 return false
281 }
282 }
283 return true
284 }
285
286
287
288
289
290
291 func (s *CertPool) AddCertWithConstraint(cert *Certificate, constraint func([]*Certificate) error) {
292 if cert == nil {
293 panic("adding nil Certificate to CertPool")
294 }
295 s.addCertFunc(sha256.Sum224(cert.Raw), string(cert.RawSubject), func() (*Certificate, error) {
296 return cert, nil
297 }, constraint)
298 }
299
View as plain text