...
1
15
16 package x509
17
18 import (
19 "encoding/pem"
20 "errors"
21 "io/ioutil"
22 "os"
23 "runtime"
24 "sync"
25 )
26
27
28 var certFiles = []string{
29 "/etc/ssl/certs/ca-certificates.crt",
30 "/etc/pki/tls/certs/ca-bundle.crt",
31 "/etc/ssl/ca-bundle.pem",
32 "/etc/pki/tls/cacert.pem",
33 "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem",
34 }
35
36
37 type CertPool struct {
38 bySubjectKeyId map[string][]int
39 byName map[string][]int
40 certs []*Certificate
41 }
42
43
44 func NewCertPool() *CertPool {
45 return &CertPool{
46 bySubjectKeyId: make(map[string][]int),
47 byName: make(map[string][]int),
48 }
49 }
50
51
52
53 var certDirectories = []string{
54 "/etc/ssl/certs",
55 "/system/etc/security/cacerts",
56 }
57
58 var (
59 once sync.Once
60 systemRoots *CertPool
61 systemRootsErr error
62 )
63
64 func systemRootsPool() *CertPool {
65 once.Do(initSystemRoots)
66 return systemRoots
67 }
68
69 func initSystemRoots() {
70 systemRoots, systemRootsErr = loadSystemRoots()
71 }
72
73 func (c *Certificate) systemVerify(opts *VerifyOptions) (chains [][]*Certificate, err error) {
74 return nil, nil
75 }
76
77 func loadSystemRoots() (*CertPool, error) {
78 roots := NewCertPool()
79 var firstErr error
80 for _, file := range certFiles {
81 data, err := ioutil.ReadFile(file)
82 if err == nil {
83 roots.AppendCertsFromPEM(data)
84 return roots, nil
85 }
86 if firstErr == nil && !os.IsNotExist(err) {
87 firstErr = err
88 }
89 }
90
91 for _, directory := range certDirectories {
92 fis, err := ioutil.ReadDir(directory)
93 if err != nil {
94 if firstErr == nil && !os.IsNotExist(err) {
95 firstErr = err
96 }
97 continue
98 }
99 rootsAdded := false
100 for _, fi := range fis {
101 data, err := ioutil.ReadFile(directory + "/" + fi.Name())
102 if err == nil && roots.AppendCertsFromPEM(data) {
103 rootsAdded = true
104 }
105 }
106 if rootsAdded {
107 return roots, nil
108 }
109 }
110
111 return nil, firstErr
112 }
113
114
115
116
117
118 func SystemCertPool() (*CertPool, error) {
119 if runtime.GOOS == "windows" {
120
121 return nil, errors.New("crypto/x509: system root pool is not available on Windows")
122 }
123
124 return loadSystemRoots()
125 }
126
127
128
129
130
131 func (s *CertPool) findVerifiedParents(cert *Certificate) (parents []int, errCert *Certificate, err error) {
132 if s == nil {
133 return
134 }
135 var candidates []int
136
137 if len(cert.AuthorityKeyId) > 0 {
138 candidates = s.bySubjectKeyId[string(cert.AuthorityKeyId)]
139 }
140 if len(candidates) == 0 {
141 candidates = s.byName[string(cert.RawIssuer)]
142 }
143
144 for _, c := range candidates {
145 if err = cert.CheckSignatureFrom(s.certs[c]); err == nil {
146 parents = append(parents, c)
147 } else {
148 errCert = s.certs[c]
149 }
150 }
151
152 return
153 }
154
155 func (s *CertPool) contains(cert *Certificate) bool {
156 if s == nil {
157 return false
158 }
159
160 candidates := s.byName[string(cert.RawSubject)]
161 for _, c := range candidates {
162 if s.certs[c].Equal(cert) {
163 return true
164 }
165 }
166
167 return false
168 }
169
170
171 func (s *CertPool) AddCert(cert *Certificate) {
172 if cert == nil {
173 panic("adding nil Certificate to CertPool")
174 }
175
176
177 if s.contains(cert) {
178 return
179 }
180
181 n := len(s.certs)
182 s.certs = append(s.certs, cert)
183
184 if len(cert.SubjectKeyId) > 0 {
185 keyId := string(cert.SubjectKeyId)
186 s.bySubjectKeyId[keyId] = append(s.bySubjectKeyId[keyId], n)
187 }
188 name := string(cert.RawSubject)
189 s.byName[name] = append(s.byName[name], n)
190 }
191
192
193
194
195
196
197
198 func (s *CertPool) AppendCertsFromPEM(pemCerts []byte) (ok bool) {
199 for len(pemCerts) > 0 {
200 var block *pem.Block
201 block, pemCerts = pem.Decode(pemCerts)
202 if block == nil {
203 break
204 }
205 if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
206 continue
207 }
208
209 cert, err := ParseCertificate(block.Bytes)
210 if err != nil {
211 continue
212 }
213
214 s.AddCert(cert)
215 ok = true
216 }
217
218 return
219 }
220
221
222
223 func (s *CertPool) Subjects() [][]byte {
224 res := make([][]byte, len(s.certs))
225 for i, c := range s.certs {
226 res[i] = c.RawSubject
227 }
228 return res
229 }
230
View as plain text