cert_pool.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.
  4. package x509
  5. import (
  6. "encoding/pem"
  7. )
  8. // CertPool is a set of certificates.
  9. type CertPool struct {
  10. bySubjectKeyId map[string][]int
  11. byName map[string][]int
  12. bySHA256 map[string]int
  13. certs []*Certificate
  14. }
  15. // NewCertPool returns a new, empty CertPool.
  16. func NewCertPool() *CertPool {
  17. return &CertPool{
  18. bySubjectKeyId: make(map[string][]int),
  19. byName: make(map[string][]int),
  20. bySHA256: make(map[string]int),
  21. }
  22. }
  23. // findVerifiedParents attempts to find certificates in s which have signed the
  24. // given certificate. If any candidates were rejected then errCert will be set
  25. // to one of them, arbitrarily, and err will contain the reason that it was
  26. // rejected.
  27. func (s *CertPool) findVerifiedParents(cert *Certificate) (parents []int, errCert *Certificate, err error) {
  28. if s == nil {
  29. return
  30. }
  31. var candidates []int
  32. if len(cert.AuthorityKeyId) > 0 {
  33. candidates, _ = s.bySubjectKeyId[string(cert.AuthorityKeyId)]
  34. }
  35. if len(candidates) == 0 {
  36. candidates, _ = s.byName[string(cert.RawIssuer)]
  37. }
  38. for _, c := range candidates {
  39. if err = cert.CheckSignatureFrom(s.certs[c]); err == nil {
  40. cert.validSignature = true
  41. parents = append(parents, c)
  42. } else {
  43. errCert = s.certs[c]
  44. }
  45. }
  46. return
  47. }
  48. // Contains returns true if c is in s.
  49. func (s *CertPool) Contains(c *Certificate) bool {
  50. if s == nil {
  51. return false
  52. }
  53. _, ok := s.bySHA256[string(c.FingerprintSHA256)]
  54. return ok
  55. }
  56. // Covers returns true if all certs in pool are in s.
  57. func (s *CertPool) Covers(pool *CertPool) bool {
  58. if pool == nil {
  59. return true
  60. }
  61. for _, c := range pool.certs {
  62. if !s.Contains(c) {
  63. return false
  64. }
  65. }
  66. return true
  67. }
  68. // Certificates returns a list of parsed certificates in the pool.
  69. func (s *CertPool) Certificates() []*Certificate {
  70. out := make([]*Certificate, 0, len(s.certs))
  71. out = append(out, s.certs...)
  72. return out
  73. }
  74. // Size returns the number of unique certificates in the CertPool.
  75. func (s *CertPool) Size() int {
  76. if s == nil {
  77. return 0
  78. }
  79. return len(s.certs)
  80. }
  81. // Sum returns the union of two certificate pools as a new certificate pool.
  82. func (s *CertPool) Sum(other *CertPool) (sum *CertPool) {
  83. sum = NewCertPool()
  84. if s != nil {
  85. for _, c := range s.certs {
  86. sum.AddCert(c)
  87. }
  88. }
  89. if other != nil {
  90. for _, c := range other.certs {
  91. sum.AddCert(c)
  92. }
  93. }
  94. return
  95. }
  96. // AddCert adds a certificate to a pool.
  97. func (s *CertPool) AddCert(cert *Certificate) {
  98. if cert == nil {
  99. panic("adding nil Certificate to CertPool")
  100. }
  101. // Check that the certificate isn't being added twice.
  102. sha256fp := string(cert.FingerprintSHA256)
  103. if _, ok := s.bySHA256[sha256fp]; ok {
  104. return
  105. }
  106. n := len(s.certs)
  107. s.certs = append(s.certs, cert)
  108. if len(cert.SubjectKeyId) > 0 {
  109. keyId := string(cert.SubjectKeyId)
  110. s.bySubjectKeyId[keyId] = append(s.bySubjectKeyId[keyId], n)
  111. }
  112. name := string(cert.RawSubject)
  113. s.byName[name] = append(s.byName[name], n)
  114. s.bySHA256[sha256fp] = n
  115. }
  116. // AppendCertsFromPEM attempts to parse a series of PEM encoded certificates.
  117. // It appends any certificates found to s and reports whether any certificates
  118. // were successfully parsed.
  119. //
  120. // On many Linux systems, /etc/ssl/cert.pem will contain the system wide set
  121. // of root CAs in a format suitable for this function.
  122. func (s *CertPool) AppendCertsFromPEM(pemCerts []byte) (ok bool) {
  123. for len(pemCerts) > 0 {
  124. var block *pem.Block
  125. block, pemCerts = pem.Decode(pemCerts)
  126. if block == nil {
  127. break
  128. }
  129. if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
  130. continue
  131. }
  132. cert, err := ParseCertificate(block.Bytes)
  133. if err != nil {
  134. continue
  135. }
  136. s.AddCert(cert)
  137. ok = true
  138. }
  139. return
  140. }
  141. // Subjects returns a list of the DER-encoded subjects of
  142. // all of the certificates in the pool.
  143. func (s *CertPool) Subjects() [][]byte {
  144. res := make([][]byte, len(s.certs))
  145. for i, c := range s.certs {
  146. res[i] = c.RawSubject
  147. }
  148. return res
  149. }