12 // Possible certificate files; stop after finding one.
13 var certFiles = []string{
14 "/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu/Gentoo etc.
15 "/etc/pki/tls/certs/ca-bundle.crt", // Fedora/RHEL 6
16 "/etc/ssl/ca-bundle.pem", // OpenSUSE
17 "/etc/pki/tls/cacert.pem", // OpenELEC
18 "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", // CentOS/RHEL 7
21 // CertPool is a set of certificates.
22 type CertPool struct {
23 bySubjectKeyId map[string][]int
24 byName map[string][]int
28 // NewCertPool returns a new, empty CertPool.
29 func NewCertPool() *CertPool {
31 bySubjectKeyId: make(map[string][]int),
32 byName: make(map[string][]int),
36 // Possible directories with certificate files; stop after successfully
37 // reading at least one file from a directory.
38 var certDirectories = []string{
39 "/etc/ssl/certs", // SLES10/SLES11, https://golang.org/issue/12139
40 "/system/etc/security/cacerts", // Android
49 func systemRootsPool() *CertPool {
50 once.Do(initSystemRoots)
54 func initSystemRoots() {
55 systemRoots, systemRootsErr = loadSystemRoots()
58 func (c *Certificate) systemVerify(opts *VerifyOptions) (chains [][]*Certificate, err error) {
62 func loadSystemRoots() (*CertPool, error) {
63 roots := NewCertPool()
65 for _, file := range certFiles {
66 data, err := ioutil.ReadFile(file)
68 roots.AppendCertsFromPEM(data)
71 if firstErr == nil && !os.IsNotExist(err) {
76 for _, directory := range certDirectories {
77 fis, err := ioutil.ReadDir(directory)
79 if firstErr == nil && !os.IsNotExist(err) {
85 for _, fi := range fis {
86 data, err := ioutil.ReadFile(directory + "/" + fi.Name())
87 if err == nil && roots.AppendCertsFromPEM(data) {
99 // SystemCertPool returns a copy of the system cert pool.
101 // Any mutations to the returned pool are not written to disk and do
102 // not affect any other pool.
103 func SystemCertPool() (*CertPool, error) {
104 if runtime.GOOS == "windows" {
105 // Issue 16736, 18609:
106 return nil, errors.New("crypto/x509: system root pool is not available on Windows")
109 return loadSystemRoots()
112 // findVerifiedParents attempts to find certificates in s which have signed the
113 // given certificate. If any candidates were rejected then errCert will be set
114 // to one of them, arbitrarily, and err will contain the reason that it was
116 func (s *CertPool) findVerifiedParents(cert *Certificate) (parents []int, errCert *Certificate, err error) {
122 if len(cert.AuthorityKeyId) > 0 {
123 candidates = s.bySubjectKeyId[string(cert.AuthorityKeyId)]
125 if len(candidates) == 0 {
126 candidates = s.byName[string(cert.RawIssuer)]
129 for _, c := range candidates {
130 if err = cert.CheckSignatureFrom(s.certs[c]); err == nil {
131 parents = append(parents, c)
140 func (s *CertPool) contains(cert *Certificate) bool {
145 candidates := s.byName[string(cert.RawSubject)]
146 for _, c := range candidates {
147 if s.certs[c].Equal(cert) {
155 // AddCert adds a certificate to a pool.
156 func (s *CertPool) AddCert(cert *Certificate) {
158 panic("adding nil Certificate to CertPool")
161 // Check that the certificate isn't being added twice.
162 if s.contains(cert) {
167 s.certs = append(s.certs, cert)
169 if len(cert.SubjectKeyId) > 0 {
170 keyId := string(cert.SubjectKeyId)
171 s.bySubjectKeyId[keyId] = append(s.bySubjectKeyId[keyId], n)
173 name := string(cert.RawSubject)
174 s.byName[name] = append(s.byName[name], n)
177 // AppendCertsFromPEM attempts to parse a series of PEM encoded certificates.
178 // It appends any certificates found to s and reports whether any certificates
179 // were successfully parsed.
181 // On many Linux systems, /etc/ssl/cert.pem will contain the system wide set
182 // of root CAs in a format suitable for this function.
183 func (s *CertPool) AppendCertsFromPEM(pemCerts []byte) (ok bool) {
184 for len(pemCerts) > 0 {
186 block, pemCerts = pem.Decode(pemCerts)
190 if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
194 cert, err := ParseCertificate(block.Bytes)
206 // Subjects returns a list of the DER-encoded subjects of
207 // all of the certificates in the pool.
208 func (s *CertPool) Subjects() [][]byte {
209 res := make([][]byte, len(s.certs))
210 for i, c := range s.certs {
211 res[i] = c.RawSubject