-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #493 from wakatime/bugfix/fix-system-root-certs-on…
…-windows Load system root CA certs on Windows
- Loading branch information
Showing
3 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |