1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 // CertPool is a set of certificates.
12 type CertPool struct {
13 bySubjectKeyId map[string][]int
14 byName map[string][]int
18 // NewCertPool returns a new, empty CertPool.
19 func NewCertPool() *CertPool {
21 make(map[string][]int),
22 make(map[string][]int),
27 // findVerifiedParents attempts to find certificates in s which have signed the
28 // given certificate. If any candidates were rejected then errCert will be set
29 // to one of them, arbitrarily, and err will contain the reason that it was
31 func (s *CertPool) findVerifiedParents(cert *Certificate) (parents []int, errCert *Certificate, err error) {
37 if len(cert.AuthorityKeyId) > 0 {
38 candidates = s.bySubjectKeyId[string(cert.AuthorityKeyId)]
40 if len(candidates) == 0 {
41 candidates = s.byName[string(cert.RawIssuer)]
44 for _, c := range candidates {
45 if err = cert.CheckSignatureFrom(s.certs[c]); err == nil {
46 parents = append(parents, c)
55 // AddCert adds a certificate to a pool.
56 func (s *CertPool) AddCert(cert *Certificate) {
58 panic("adding nil Certificate to CertPool")
61 // Check that the certificate isn't being added twice.
62 for _, c := range s.certs {
69 s.certs = append(s.certs, cert)
71 if len(cert.SubjectKeyId) > 0 {
72 keyId := string(cert.SubjectKeyId)
73 s.bySubjectKeyId[keyId] = append(s.bySubjectKeyId[keyId], n)
75 name := string(cert.RawSubject)
76 s.byName[name] = append(s.byName[name], n)
79 // AppendCertsFromPEM attempts to parse a series of PEM encoded certificates.
80 // It appends any certificates found to s and returns true if any certificates
81 // were successfully parsed.
83 // On many Linux systems, /etc/ssl/cert.pem will contain the system wide set
84 // of root CAs in a format suitable for this function.
85 func (s *CertPool) AppendCertsFromPEM(pemCerts []byte) (ok bool) {
86 for len(pemCerts) > 0 {
88 block, pemCerts = pem.Decode(pemCerts)
92 if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
96 cert, err := ParseCertificate(block.Bytes)
108 // Subjects returns a list of the DER-encoded subjects of
109 // all of the certificates in the pool.
110 func (s *CertPool) Subjects() (res [][]byte) {
111 res = make([][]byte, len(s.certs))
112 for i, c := range s.certs {
113 res[i] = c.RawSubject