Skip to content

Commit

Permalink
Merge pull request #493 from wakatime/bugfix/fix-system-root-certs-on…
Browse files Browse the repository at this point in the history
…-windows

Load system root CA certs on Windows
  • Loading branch information
alanhamlett authored Jul 6, 2021
2 parents ad1d61a + ff75397 commit 55b72ad
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=

// CACerts returns a root cert pool with the system's cacerts and LetsEncrypt's root certs.
func CACerts() *x509.CertPool {
certs, err := x509.SystemCertPool()
certs, err := loadSystemRoots()
if err != nil {
log.Warnf("unable to use system cert pool: %s", err)
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/api/transport_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// +build !windows

package api

import (
"crypto/x509"
)

func loadSystemRoots() (*x509.CertPool, error) {
return x509.SystemCertPool()
}
44 changes: 44 additions & 0 deletions pkg/api/transport_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// +build windows

package api

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

func loadSystemRoots() (*x509.CertPool, error) {
const CRYPT_E_NOT_FOUND = 0x80092004

store, err := syscall.CertOpenSystemStore(0, syscall.StringToUTF16Ptr("ROOT"))
if err != nil {
return nil, err
}
defer syscall.CertCloseStore(store, 0)

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

0 comments on commit 55b72ad

Please sign in to comment.