-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use workaround for windows x509.SystemCertPool() (#4018)
Signed-off-by: Nikki Attea <[email protected]>
- Loading branch information
Nikki Attea
authored
Sep 22, 2020
1 parent
e6fae34
commit 792b588
Showing
3 changed files
with
54 additions
and
0 deletions.
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,7 @@ | ||
// +build !windows | ||
|
||
package asset | ||
|
||
import "crypto/x509" | ||
|
||
func appendCerts(rootCAs *x509.CertPool) {} |
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,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) | ||
} | ||
} | ||
} |