Debian Codegraph @

internal/x509andr/root_windows.go

raw symbols SCIP
1 // Copyright 2012 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
5 package x509andr
6
7 import (
8 "errors"
9 "syscall"
10 "unsafe"
11 )
12
13 // Creates a new *syscall.CertContext representing the leaf certificate in an in-memory
14 // certificate store containing itself and all of the intermediate certificates specified
15 // in the opts.Intermediates CertPool.
16 //
17 // A pointer to the in-memory store is available in the returned CertContext's Store field.
18 // The store is automatically freed when the CertContext is freed using
19 // syscall.CertFreeCertificateContext.
20 func createStoreContext(leaf *Certificate, opts *VerifyOptions) (*syscall.CertContext, error) {
21 var storeCtx *syscall.CertContext
22
23 leafCtx, err := syscall.CertCreateCertificateContext(syscall.X509_ASN_ENCODING|syscall.PKCS_7_ASN_ENCODING, &leaf.Raw[0], uint32(len(leaf.Raw)))
24 if err != nil {
25 return nil, err
26 }
27 defer syscall.CertFreeCertificateContext(leafCtx)
28
29 handle, err := syscall.CertOpenStore(syscall.CERT_STORE_PROV_MEMORY, 0, 0, syscall.CERT_STORE_DEFER_CLOSE_UNTIL_LAST_FREE_FLAG, 0)
30 if err != nil {
31 return nil, err
32 }
33 defer syscall.CertCloseStore(handle, 0)
34
35 err = syscall.CertAddCertificateContextToStore(handle, leafCtx, syscall.CERT_STORE_ADD_ALWAYS, &storeCtx)
36 if err != nil {
37 return nil, err
38 }
39
40 if opts.Intermediates != nil {
41 for _, intermediate := range opts.Intermediates.certs {
42 ctx, err := syscall.CertCreateCertificateContext(syscall.X509_ASN_ENCODING|syscall.PKCS_7_ASN_ENCODING, &intermediate.Raw[0], uint32(len(intermediate.Raw)))
43 if err != nil {
44 return nil, err
45 }
46
47 err = syscall.CertAddCertificateContextToStore(handle, ctx, syscall.CERT_STORE_ADD_ALWAYS, nil)
48 syscall.CertFreeCertificateContext(ctx)
49 if err != nil {
50 return nil, err
51 }
52 }
53 }
54
55 return storeCtx, nil
56 }
57
58 // extractSimpleChain extracts the final certificate chain from a CertSimpleChain.
59 func extractSimpleChain(simpleChain **syscall.CertSimpleChain, count int) (chain []*Certificate, err error) {
60 if simpleChain == nil || count == 0 {
61 return nil, errors.New("x509: invalid simple chain")
62 }
63
64 simpleChains := (*[1 << 20]*syscall.CertSimpleChain)(unsafe.Pointer(simpleChain))[:]
65 lastChain := simpleChains[count-1]
66 elements := (*[1 << 20]*syscall.CertChainElement)(unsafe.Pointer(lastChain.Elements))[:]
67 for i := 0; i < int(lastChain.NumElements); i++ {
68 // Copy the buf, since ParseCertificate does not create its own copy.
69 cert := elements[i].CertContext
70 encodedCert := (*[1 << 20]byte)(unsafe.Pointer(cert.EncodedCert))[:]
71 buf := make([]byte, cert.Length)
72 copy(buf, encodedCert[:])
73 parsedCert, err := ParseCertificate(buf)
74 if err != nil {
75 return nil, err
76 }
77 chain = append(chain, parsedCert)
78 }
79
80 return chain, nil
81 }
82
83 // checkChainTrustStatus checks the trust status of the certificate chain, translating
84 // any errors it finds into Go errors in the process.
85 func checkChainTrustStatus(c *Certificate, chainCtx *syscall.CertChainContext) error {
86 if chainCtx.TrustStatus.ErrorStatus != syscall.CERT_TRUST_NO_ERROR {
87 status := chainCtx.TrustStatus.ErrorStatus
88 switch status {
89 case syscall.CERT_TRUST_IS_NOT_TIME_VALID:
90 return CertificateInvalidError{c, Expired}
91 default:
92 return UnknownAuthorityError{c, nil, nil}
93 }
94 }
95 return nil
96 }
97
98 // systemVerify is like Verify, except that it uses CryptoAPI calls
99 // to build certificate chains and verify them.
100 func (c *Certificate) systemVerify(opts *VerifyOptions) (chains [][]*Certificate, err error) {
101 hasDNSName := opts != nil && len(opts.DNSName) > 0
102
103 storeCtx, err := createStoreContext(c, opts)
104 if err != nil {
105 return nil, err
106 }
107 defer syscall.CertFreeCertificateContext(storeCtx)
108
109 para := new(syscall.CertChainPara)
110 para.Size = uint32(unsafe.Sizeof(*para))
111
112 // If there's a DNSName set in opts, assume we're verifying
113 // a certificate from a TLS server.
114 if hasDNSName {
115 oids := []*byte{
116 &syscall.OID_PKIX_KP_SERVER_AUTH[0],
117 // Both IE and Chrome allow certificates with
118 // Server Gated Crypto as well. Some certificates
119 // in the wild require them.
120 &syscall.OID_SERVER_GATED_CRYPTO[0],
121 &syscall.OID_SGC_NETSCAPE[0],
122 }
123 para.RequestedUsage.Type = syscall.USAGE_MATCH_TYPE_OR
124 para.RequestedUsage.Usage.Length = uint32(len(oids))
125 para.RequestedUsage.Usage.UsageIdentifiers = &oids[0]
126 } else {
127 para.RequestedUsage.Type = syscall.USAGE_MATCH_TYPE_AND
128 para.RequestedUsage.Usage.Length = 0
129 para.RequestedUsage.Usage.UsageIdentifiers = nil
130 }
131
132 var verifyTime *syscall.Filetime
133 if opts != nil && !opts.CurrentTime.IsZero() {
134 ft := syscall.NsecToFiletime(opts.CurrentTime.UnixNano())
135 verifyTime = &ft
136 }
137
138 // CertGetCertificateChain will traverse Windows's root stores
139 // in an attempt to build a verified certificate chain. Once
140 // it has found a verified chain, it stops. MSDN docs on
141 // CERT_CHAIN_CONTEXT:
142 //
143 // When a CERT_CHAIN_CONTEXT is built, the first simple chain
144 // begins with an end certificate and ends with a self-signed
145 // certificate. If that self-signed certificate is not a root
146 // or otherwise trusted certificate, an attempt is made to
147 // build a new chain. CTLs are used to create the new chain
148 // beginning with the self-signed certificate from the original
149 // chain as the end certificate of the new chain. This process
150 // continues building additional simple chains until the first
151 // self-signed certificate is a trusted certificate or until
152 // an additional simple chain cannot be built.
153 //
154 // The result is that we'll only get a single trusted chain to
155 // return to our caller.
156 var chainCtx *syscall.CertChainContext
157 err = syscall.CertGetCertificateChain(syscall.Handle(0), storeCtx, verifyTime, storeCtx.Store, para, 0, 0, &chainCtx)
158 if err != nil {
159 return nil, err
160 }
161 defer syscall.CertFreeCertificateChain(chainCtx)
162
163 err = checkChainTrustStatus(c, chainCtx)
164 if err != nil {
165 return nil, err
166 }
167
168 if hasDNSName {
169 err = checkChainSSLServerPolicy(c, chainCtx, opts)
170 if err != nil {
171 return nil, err
172 }
173 }
174
175 chain, err := extractSimpleChain(chainCtx.Chains, int(chainCtx.ChainCount))
176 if err != nil {
177 return nil, err
178 }
179
180 chains = append(chains, chain)
181
182 return chains, nil
183 }
184
185 func loadSystemRoots() (*CertPool, error) {
186 // TODO: restore this functionality on Windows. We tried to do
187 // it in Go 1.8 but had to revert it. See Issue 18609.
188 // Returning (nil, nil) was the old behavior, prior to CL 30578.
189 return nil, nil
190
191 const CRYPT_E_NOT_FOUND = 0x80092004
192
193 store, err := syscall.CertOpenSystemStore(0, syscall.StringToUTF16Ptr("ROOT"))
194 if err != nil {
195 return nil, err
196 }
197 defer syscall.CertCloseStore(store, 0)
198
199 roots := NewCertPool()
200 var cert *syscall.CertContext
201 for {
202 cert, err = syscall.CertEnumCertificatesInStore(store, cert)
203 if err != nil {
204 if errno, ok := err.(syscall.Errno); ok {
205 if errno == CRYPT_E_NOT_FOUND {
206 break
207 }
208 }
209 return nil, err
210 }
211 if cert == nil {
212 break
213 }
214 // Copy the buf, since ParseCertificate does not create its own copy.
215 buf := (*[1 << 20]byte)(unsafe.Pointer(cert.EncodedCert))[:]
216 buf2 := make([]byte, cert.Length)
217 copy(buf2, buf)
218 if c, err := ParseCertificate(buf2); err == nil {
219 roots.AddCert(c)
220 }
221 }
222 return roots, nil
223 }