cert_pool.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. certs []*Certificate
  13. }
  14. // NewCertPool returns a new, empty CertPool.
  15. func NewCertPool() *CertPool {
  16. return &CertPool{
  17. make(map[string][]int),
  18. make(map[string][]int),
  19. nil,
  20. }
  21. }
  22. // findVerifiedParents attempts to find certificates in s which have signed the
  23. // given certificate. If any candidates were rejected then errCert will be set
  24. // to one of them, arbitrarily, and err will contain the reason that it was
  25. // rejected.
  26. func (s *CertPool) findVerifiedParents(cert *Certificate) (parents []int, errCert *Certificate, err error) {
  27. if s == nil {
  28. return
  29. }
  30. var candidates []int
  31. if len(cert.AuthorityKeyId) > 0 {
  32. candidates = s.bySubjectKeyId[string(cert.AuthorityKeyId)]
  33. }
  34. if len(candidates) == 0 {
  35. candidates = s.byName[string(cert.RawIssuer)]
  36. }
  37. for _, c := range candidates {
  38. if err = cert.CheckSignatureFrom(s.certs[c]); err == nil {
  39. parents = append(parents, c)
  40. } else {
  41. errCert = s.certs[c]
  42. }
  43. }
  44. return
  45. }
  46. // AddCert adds a certificate to a pool.
  47. func (s *CertPool) AddCert(cert *Certificate) {
  48. if cert == nil {
  49. panic("adding nil Certificate to CertPool")
  50. }
  51. // Check that the certificate isn't being added twice.
  52. for _, c := range s.certs {
  53. if c.Equal(cert) {
  54. return
  55. }
  56. }
  57. n := len(s.certs)
  58. s.certs = append(s.certs, cert)
  59. if len(cert.SubjectKeyId) > 0 {
  60. keyId := string(cert.SubjectKeyId)
  61. s.bySubjectKeyId[keyId] = append(s.bySubjectKeyId[keyId], n)
  62. }
  63. name := string(cert.RawSubject)
  64. s.byName[name] = append(s.byName[name], n)
  65. }
  66. // AppendCertsFromPEM attempts to parse a series of PEM encoded certificates.
  67. // It appends any certificates found to s and returns true if any certificates
  68. // were successfully parsed.
  69. //
  70. // On many Linux systems, /etc/ssl/cert.pem will contain the system wide set
  71. // of root CAs in a format suitable for this function.
  72. func (s *CertPool) AppendCertsFromPEM(pemCerts []byte) (ok bool) {
  73. for len(pemCerts) > 0 {
  74. var block *pem.Block
  75. block, pemCerts = pem.Decode(pemCerts)
  76. if block == nil {
  77. break
  78. }
  79. if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
  80. continue
  81. }
  82. cert, err := ParseCertificate(block.Bytes)
  83. if err != nil {
  84. continue
  85. }
  86. s.AddCert(cert)
  87. ok = true
  88. }
  89. return
  90. }
  91. // Subjects returns a list of the DER-encoded subjects of
  92. // all of the certificates in the pool.
  93. func (s *CertPool) Subjects() (res [][]byte) {
  94. res = make([][]byte, len(s.certs))
  95. for i, c := range s.certs {
  96. res[i] = c.RawSubject
  97. }
  98. return
  99. }