cert_pool.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // SystemCertPool returns a copy of the system cert pool.
  24. //
  25. // Any mutations to the returned pool are not written to disk and do
  26. // not affect any other pool.
  27. func SystemCertPool() (*CertPool, error) {
  28. if runtime.GOOS == "windows" {
  29. // Issue 16736, 18609:
  30. return nil, errors.New("crypto/x509: system root pool is not available on Windows")
  31. }
  32. return loadSystemRoots()
  33. }
  34. // findVerifiedParents attempts to find certificates in s which have signed the
  35. // given certificate. If any candidates were rejected then errCert will be set
  36. // to one of them, arbitrarily, and err will contain the reason that it was
  37. // rejected.
  38. func (s *CertPool) findVerifiedParents(cert *Certificate) (parents []int, errCert *Certificate, err error) {
  39. if s == nil {
  40. return
  41. }
  42. var candidates []int
  43. if len(cert.AuthorityKeyId) > 0 {
  44. candidates = s.bySubjectKeyId[string(cert.AuthorityKeyId)]
  45. }
  46. if len(candidates) == 0 {
  47. candidates = s.byName[string(cert.RawIssuer)]
  48. }
  49. for _, c := range candidates {
  50. if err = cert.CheckSignatureFrom(s.certs[c]); err == nil {
  51. parents = append(parents, c)
  52. } else {
  53. errCert = s.certs[c]
  54. }
  55. }
  56. return
  57. }
  58. func (s *CertPool) contains(cert *Certificate) bool {
  59. if s == nil {
  60. return false
  61. }
  62. candidates := s.byName[string(cert.RawSubject)]
  63. for _, c := range candidates {
  64. if s.certs[c].Equal(cert) {
  65. return true
  66. }
  67. }
  68. return false
  69. }
  70. // AddCert adds a certificate to a pool.
  71. func (s *CertPool) AddCert(cert *Certificate) {
  72. if cert == nil {
  73. panic("adding nil Certificate to CertPool")
  74. }
  75. // Check that the certificate isn't being added twice.
  76. if s.contains(cert) {
  77. return
  78. }
  79. n := len(s.certs)
  80. s.certs = append(s.certs, cert)
  81. if len(cert.SubjectKeyId) > 0 {
  82. keyId := string(cert.SubjectKeyId)
  83. s.bySubjectKeyId[keyId] = append(s.bySubjectKeyId[keyId], n)
  84. }
  85. name := string(cert.RawSubject)
  86. s.byName[name] = append(s.byName[name], n)
  87. }
  88. // AppendCertsFromPEM attempts to parse a series of PEM encoded certificates.
  89. // It appends any certificates found to s and reports whether any certificates
  90. // were successfully parsed.
  91. //
  92. // On many Linux systems, /etc/ssl/cert.pem will contain the system wide set
  93. // of root CAs in a format suitable for this function.
  94. func (s *CertPool) AppendCertsFromPEM(pemCerts []byte) (ok bool) {
  95. for len(pemCerts) > 0 {
  96. var block *pem.Block
  97. block, pemCerts = pem.Decode(pemCerts)
  98. if block == nil {
  99. break
  100. }
  101. if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
  102. continue
  103. }
  104. cert, err := ParseCertificate(block.Bytes)
  105. if IsFatal(err) {
  106. continue
  107. }
  108. s.AddCert(cert)
  109. ok = true
  110. }
  111. return
  112. }
  113. // Subjects returns a list of the DER-encoded subjects of
  114. // all of the certificates in the pool.
  115. func (s *CertPool) Subjects() [][]byte {
  116. res := make([][]byte, len(s.certs))
  117. for i, c := range s.certs {
  118. res[i] = c.RawSubject
  119. }
  120. return res
  121. }