cert_pool.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "errors"
  8. "runtime"
  9. )
  10. // CertPool is a set of certificates.
  11. type CertPool struct {
  12. bySubjectKeyId map[string][]int
  13. byName map[string][]int
  14. certs []*Certificate
  15. }
  16. // NewCertPool returns a new, empty CertPool.
  17. func NewCertPool() *CertPool {
  18. return &CertPool{
  19. bySubjectKeyId: make(map[string][]int),
  20. byName: make(map[string][]int),
  21. }
  22. }
  23. func (s *CertPool) copy() *CertPool {
  24. p := &CertPool{
  25. bySubjectKeyId: make(map[string][]int, len(s.bySubjectKeyId)),
  26. byName: make(map[string][]int, len(s.byName)),
  27. certs: make([]*Certificate, len(s.certs)),
  28. }
  29. for k, v := range s.bySubjectKeyId {
  30. indexes := make([]int, len(v))
  31. copy(indexes, v)
  32. p.bySubjectKeyId[k] = indexes
  33. }
  34. for k, v := range s.byName {
  35. indexes := make([]int, len(v))
  36. copy(indexes, v)
  37. p.byName[k] = indexes
  38. }
  39. copy(p.certs, s.certs)
  40. return p
  41. }
  42. // SystemCertPool returns a copy of the system cert pool.
  43. //
  44. // Any mutations to the returned pool are not written to disk and do
  45. // not affect any other pool returned by SystemCertPool.
  46. //
  47. // New changes in the system cert pool might not be reflected
  48. // in subsequent calls.
  49. func SystemCertPool() (*CertPool, error) {
  50. if runtime.GOOS == "windows" {
  51. // Issue 16736, 18609:
  52. return nil, errors.New("crypto/x509: system root pool is not available on Windows")
  53. }
  54. if sysRoots := systemRootsPool(); sysRoots != nil {
  55. return sysRoots.copy(), nil
  56. }
  57. return loadSystemRoots()
  58. }
  59. // findPotentialParents returns the indexes of certificates in s which might
  60. // have signed cert. The caller must not modify the returned slice.
  61. func (s *CertPool) findPotentialParents(cert *Certificate) []int {
  62. if s == nil {
  63. return nil
  64. }
  65. var candidates []int
  66. if len(cert.AuthorityKeyId) > 0 {
  67. candidates = s.bySubjectKeyId[string(cert.AuthorityKeyId)]
  68. }
  69. if len(candidates) == 0 {
  70. candidates = s.byName[string(cert.RawIssuer)]
  71. }
  72. return candidates
  73. }
  74. func (s *CertPool) contains(cert *Certificate) bool {
  75. if s == nil {
  76. return false
  77. }
  78. candidates := s.byName[string(cert.RawSubject)]
  79. for _, c := range candidates {
  80. if s.certs[c].Equal(cert) {
  81. return true
  82. }
  83. }
  84. return false
  85. }
  86. // AddCert adds a certificate to a pool.
  87. func (s *CertPool) AddCert(cert *Certificate) {
  88. if cert == nil {
  89. panic("adding nil Certificate to CertPool")
  90. }
  91. // Check that the certificate isn't being added twice.
  92. if s.contains(cert) {
  93. return
  94. }
  95. n := len(s.certs)
  96. s.certs = append(s.certs, cert)
  97. if len(cert.SubjectKeyId) > 0 {
  98. keyId := string(cert.SubjectKeyId)
  99. s.bySubjectKeyId[keyId] = append(s.bySubjectKeyId[keyId], n)
  100. }
  101. name := string(cert.RawSubject)
  102. s.byName[name] = append(s.byName[name], n)
  103. }
  104. // AppendCertsFromPEM attempts to parse a series of PEM encoded certificates.
  105. // It appends any certificates found to s and reports whether any certificates
  106. // were successfully parsed.
  107. //
  108. // On many Linux systems, /etc/ssl/cert.pem will contain the system wide set
  109. // of root CAs in a format suitable for this function.
  110. func (s *CertPool) AppendCertsFromPEM(pemCerts []byte) (ok bool) {
  111. for len(pemCerts) > 0 {
  112. var block *pem.Block
  113. block, pemCerts = pem.Decode(pemCerts)
  114. if block == nil {
  115. break
  116. }
  117. if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
  118. continue
  119. }
  120. cert, err := ParseCertificate(block.Bytes)
  121. if IsFatal(err) {
  122. continue
  123. }
  124. s.AddCert(cert)
  125. ok = true
  126. }
  127. return
  128. }
  129. // Subjects returns a list of the DER-encoded subjects of
  130. // all of the certificates in the pool.
  131. func (s *CertPool) Subjects() [][]byte {
  132. res := make([][]byte, len(s.certs))
  133. for i, c := range s.certs {
  134. res[i] = c.RawSubject
  135. }
  136. return res
  137. }