OSDN Git Service

syscall: Convert errno to error after Exitsyscall.
[pf3gnuchains/gcc-fork.git] / libgo / go / crypto / tls / root_windows.go
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
5 package tls
6
7 import (
8         "crypto/x509"
9         "syscall"
10         "unsafe"
11 )
12
13 func loadStore(roots *x509.CertPool, name string) {
14         store, err := syscall.CertOpenSystemStore(syscall.InvalidHandle, syscall.StringToUTF16Ptr(name))
15         if err != nil {
16                 return
17         }
18         defer syscall.CertCloseStore(store, 0)
19
20         var cert *syscall.CertContext
21         for {
22                 cert, err = syscall.CertEnumCertificatesInStore(store, cert)
23                 if err != nil {
24                         return
25                 }
26
27                 buf := (*[1 << 20]byte)(unsafe.Pointer(cert.EncodedCert))[:]
28                 // ParseCertificate requires its own copy of certificate data to keep.
29                 buf2 := make([]byte, cert.Length)
30                 copy(buf2, buf)
31                 if c, err := x509.ParseCertificate(buf2); err == nil {
32                         roots.AddCert(c)
33                 }
34         }
35 }
36
37 func initDefaultRoots() {
38         roots := x509.NewCertPool()
39
40         // Roots
41         loadStore(roots, "ROOT")
42
43         // Intermediates
44         loadStore(roots, "CA")
45
46         varDefaultRoots = roots
47 }