| |
| |
| |
|
|
| package x509 |
|
|
| import ( |
| "bytes" |
| "crypto/sha256" |
| "encoding/pem" |
| "sync" |
| ) |
|
|
| type sum224 [sha256.Size224]byte |
|
|
| |
| type CertPool struct { |
| byName map[string][]int |
|
|
| |
| |
| lazyCerts []lazyCert |
|
|
| |
| |
| |
| |
| |
| haveSum map[sum224]bool |
|
|
| |
| |
| |
| |
| systemPool bool |
| } |
|
|
| |
| |
| type lazyCert struct { |
| |
| |
| |
| |
| rawSubject []byte |
|
|
| |
| |
| |
| constraint func([]*Certificate) error |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| getCert func() (*Certificate, error) |
| } |
|
|
| |
| func NewCertPool() *CertPool { |
| return &CertPool{ |
| byName: make(map[string][]int), |
| haveSum: make(map[sum224]bool), |
| } |
| } |
|
|
| |
| |
| func (s *CertPool) len() int { |
| if s == nil { |
| return 0 |
| } |
| return len(s.lazyCerts) |
| } |
|
|
| |
| func (s *CertPool) cert(n int) (*Certificate, func([]*Certificate) error, error) { |
| cert, err := s.lazyCerts[n].getCert() |
| return cert, s.lazyCerts[n].constraint, err |
| } |
|
|
| |
| func (s *CertPool) Clone() *CertPool { |
| p := &CertPool{ |
| byName: make(map[string][]int, len(s.byName)), |
| lazyCerts: make([]lazyCert, len(s.lazyCerts)), |
| haveSum: make(map[sum224]bool, len(s.haveSum)), |
| systemPool: s.systemPool, |
| } |
| for k, v := range s.byName { |
| indexes := make([]int, len(v)) |
| copy(indexes, v) |
| p.byName[k] = indexes |
| } |
| for k := range s.haveSum { |
| p.haveSum[k] = true |
| } |
| copy(p.lazyCerts, s.lazyCerts) |
| return p |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func SystemCertPool() (*CertPool, error) { |
| if sysRoots := systemRootsPool(); sysRoots != nil { |
| return sysRoots.Clone(), nil |
| } |
|
|
| return loadSystemRoots() |
| } |
|
|
| type potentialParent struct { |
| cert *Certificate |
| constraint func([]*Certificate) error |
| } |
|
|
| |
| |
| func (s *CertPool) findPotentialParents(cert *Certificate) []potentialParent { |
| if s == nil { |
| return nil |
| } |
|
|
| |
| |
| |
| |
| |
| |
| var matchingKeyID, oneKeyID, mismatchKeyID []potentialParent |
| for _, c := range s.byName[string(cert.RawIssuer)] { |
| candidate, constraint, err := s.cert(c) |
| if err != nil { |
| continue |
| } |
| kidMatch := bytes.Equal(candidate.SubjectKeyId, cert.AuthorityKeyId) |
| switch { |
| case kidMatch: |
| matchingKeyID = append(matchingKeyID, potentialParent{candidate, constraint}) |
| case (len(candidate.SubjectKeyId) == 0 && len(cert.AuthorityKeyId) > 0) || |
| (len(candidate.SubjectKeyId) > 0 && len(cert.AuthorityKeyId) == 0): |
| oneKeyID = append(oneKeyID, potentialParent{candidate, constraint}) |
| default: |
| mismatchKeyID = append(mismatchKeyID, potentialParent{candidate, constraint}) |
| } |
| } |
|
|
| found := len(matchingKeyID) + len(oneKeyID) + len(mismatchKeyID) |
| if found == 0 { |
| return nil |
| } |
| candidates := make([]potentialParent, 0, found) |
| candidates = append(candidates, matchingKeyID...) |
| candidates = append(candidates, oneKeyID...) |
| candidates = append(candidates, mismatchKeyID...) |
| return candidates |
| } |
|
|
| func (s *CertPool) contains(cert *Certificate) bool { |
| if s == nil { |
| return false |
| } |
| return s.haveSum[sha256.Sum224(cert.Raw)] |
| } |
|
|
| |
| func (s *CertPool) AddCert(cert *Certificate) { |
| if cert == nil { |
| panic("adding nil Certificate to CertPool") |
| } |
| s.addCertFunc(sha256.Sum224(cert.Raw), string(cert.RawSubject), func() (*Certificate, error) { |
| return cert, nil |
| }, nil) |
| } |
|
|
| |
| |
| |
| |
| |
| func (s *CertPool) addCertFunc(rawSum224 sum224, rawSubject string, getCert func() (*Certificate, error), constraint func([]*Certificate) error) { |
| if getCert == nil { |
| panic("getCert can't be nil") |
| } |
|
|
| |
| if s.haveSum[rawSum224] { |
| return |
| } |
|
|
| s.haveSum[rawSum224] = true |
| s.lazyCerts = append(s.lazyCerts, lazyCert{ |
| rawSubject: []byte(rawSubject), |
| getCert: getCert, |
| constraint: constraint, |
| }) |
| s.byName[rawSubject] = append(s.byName[rawSubject], len(s.lazyCerts)-1) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| func (s *CertPool) AppendCertsFromPEM(pemCerts []byte) (ok bool) { |
| for len(pemCerts) > 0 { |
| var block *pem.Block |
| block, pemCerts = pem.Decode(pemCerts) |
| if block == nil { |
| break |
| } |
| if block.Type != "CERTIFICATE" || len(block.Headers) != 0 { |
| continue |
| } |
|
|
| certBytes := block.Bytes |
| cert, err := ParseCertificate(certBytes) |
| if err != nil { |
| continue |
| } |
| var lazyCert struct { |
| sync.Once |
| v *Certificate |
| } |
| s.addCertFunc(sha256.Sum224(cert.Raw), string(cert.RawSubject), func() (*Certificate, error) { |
| lazyCert.Do(func() { |
| |
| lazyCert.v, _ = ParseCertificate(certBytes) |
| certBytes = nil |
| }) |
| return lazyCert.v, nil |
| }, nil) |
| ok = true |
| } |
|
|
| return ok |
| } |
|
|
| |
| |
| |
| |
| |
| func (s *CertPool) Subjects() [][]byte { |
| res := make([][]byte, s.len()) |
| for i, lc := range s.lazyCerts { |
| res[i] = lc.rawSubject |
| } |
| return res |
| } |
|
|
| |
| func (s *CertPool) Equal(other *CertPool) bool { |
| if s == nil || other == nil { |
| return s == other |
| } |
| if s.systemPool != other.systemPool || len(s.haveSum) != len(other.haveSum) { |
| return false |
| } |
| for h := range s.haveSum { |
| if !other.haveSum[h] { |
| return false |
| } |
| } |
| return true |
| } |
|
|
| |
| |
| |
| |
| |
| func (s *CertPool) AddCertWithConstraint(cert *Certificate, constraint func([]*Certificate) error) { |
| if cert == nil { |
| panic("adding nil Certificate to CertPool") |
| } |
| s.addCertFunc(sha256.Sum224(cert.Raw), string(cert.RawSubject), func() (*Certificate, error) { |
| return cert, nil |
| }, constraint) |
| } |
|
|