Skip to content

Commit

Permalink
Use workaround for windows x509.SystemCertPool() (#4018)
Browse files Browse the repository at this point in the history
Signed-off-by: Nikki Attea <[email protected]>
  • Loading branch information
Nikki Attea authored Sep 22, 2020
1 parent e6fae34 commit 792b588
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions asset/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func httpGet(ctx context.Context, path, trustedCAFile string, headers map[string
logger.Errorf("failed to append %s to RootCAs, using system certs only", trustedCAFile)
}

appendCerts(rootCAs)

client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
Expand Down
7 changes: 7 additions & 0 deletions asset/fetcher_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build !windows

package asset

import "crypto/x509"

func appendCerts(rootCAs *x509.CertPool) {}
45 changes: 45 additions & 0 deletions asset/fetcher_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// +build windows

package asset

import (
"crypto/x509"
"syscall"
"unsafe"
)

const (
// CRYPT_E_NOT_FOUND is an error code specific to windows cert pool.
// See https://github.com/golang/go/issues/16736#issuecomment-540373689.
CRYPT_E_NOT_FOUND = 0x80092004
)

func appendCerts(rootCAs *x509.CertPool) {
storeHandle, err := syscall.CertOpenSystemStore(0, syscall.StringToUTF16Ptr("Root"))
if err != nil {
logger.WithError(err).Error(syscall.GetLastError())
}

var cert *syscall.CertContext
for {
cert, err = syscall.CertEnumCertificatesInStore(storeHandle, cert)
if err != nil {
if errno, ok := err.(syscall.Errno); ok {
if errno == CRYPT_E_NOT_FOUND {
break
}
}
logger.WithError(err).Error(syscall.GetLastError())
}
if cert == nil {
break
}
// Copy the buf, since ParseCertificate does not create its own copy.
buf := (*[1 << 20]byte)(unsafe.Pointer(cert.EncodedCert))[:]
buf2 := make([]byte, cert.Length)
copy(buf2, buf)
if c, err := x509.ParseCertificate(buf2); err == nil {
rootCAs.AddCert(c)
}
}
}

0 comments on commit 792b588

Please sign in to comment.