crypto/tls: cleanup certificate load on windows

- correct syscall.CertEnumCertificatesInStore so it returns error
- remove "reflect" dependency

R=hectorchu, agl, rsc
CC=golang-dev, krautz
https://golang.org/cl/5441052
This commit is contained in:
Alex Brainman 2011-12-01 12:38:00 -05:00 committed by Adam Langley
parent c08ab14bad
commit ba4d79c54f

View File

@ -6,7 +6,6 @@ package tls
import ( import (
"crypto/x509" "crypto/x509"
"reflect"
"syscall" "syscall"
"unsafe" "unsafe"
) )
@ -16,29 +15,23 @@ func loadStore(roots *x509.CertPool, name string) {
if err != nil { if err != nil {
return return
} }
defer syscall.CertCloseStore(store, 0)
var cert *syscall.CertContext var cert *syscall.CertContext
for { for {
cert = syscall.CertEnumCertificatesInStore(store, cert) cert, err = syscall.CertEnumCertificatesInStore(store, cert)
if cert == nil { if err != nil {
break return
} }
var asn1Slice []byte buf := (*[1 << 20]byte)(unsafe.Pointer(cert.EncodedCert))[:]
hdrp := (*reflect.SliceHeader)(unsafe.Pointer(&asn1Slice)) // ParseCertificate requires its own copy of certificate data to keep.
hdrp.Data = cert.EncodedCert buf2 := make([]byte, cert.Length)
hdrp.Len = int(cert.Length) copy(buf2, buf)
hdrp.Cap = int(cert.Length) if c, err := x509.ParseCertificate(buf2); err == nil {
roots.AddCert(c)
buf := make([]byte, len(asn1Slice))
copy(buf, asn1Slice)
if cert, err := x509.ParseCertificate(buf); err == nil {
roots.AddCert(cert)
} }
} }
syscall.CertCloseStore(store, 0)
} }
func initDefaultRoots() { func initDefaultRoots() {