Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 154
Root certs are not used on Windows#67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
c6e2b3ca07e162af315089e78f91aa2798c071351b80e6d292e17a57d02f769a76a9f14b058f12639b83aacfb7e15177822e16ab6File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // +build !windows | ||
| package main | ||
| import ( | ||
| "crypto/x509" | ||
| "github.com/pkg/errors" | ||
| ) | ||
| func parseRoots(roots *x509.CertPool ) error{ | ||
| roots, err := x509.SystemCertPool() | ||
| if err != nil { | ||
| return errors.Wrap(err, "Failed to parse root store") | ||
| } | ||
| for _, ident := range idents { | ||
| if cert, err := ident.Certificate(); err == nil { | ||
| roots.AddCert(cert) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // +build windows | ||
| package main | ||
| import ( | ||
| "crypto/x509" | ||
| "github.com/certifi/gocertifi" | ||
| "github.com/pkg/errors" | ||
| "syscall" | ||
| "unsafe" | ||
| ) | ||
| const ( | ||
| CryptENotFound = 0x80092004 | ||
| ) | ||
| func parseRoots(roots *x509.CertPool) error{ | ||
| // The windows trust store is dynamically populated, to prevent issues with generally trusted | ||
| // roots not being enumerated, use the mozilla trust store as a baseline | ||
| roots, err := gocertifi.CACerts() | ||
| if err != nil { | ||
| roots = x509.NewCertPool() | ||
| } | ||
| // Enumerate the local machine trust store and add any missing certificates. | ||
| storeName, err:= syscall.UTF16PtrFromString("Root") | ||
| if err != nil { | ||
| return errors.Wrap(err, "Failed to get root store name") | ||
| } | ||
| storeHandle, err := syscall.CertOpenSystemStore(0, storeName) | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see a call to Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a defered call to | ||
| if err != nil { | ||
| return errors.New(syscall.GetLastError().Error()) | ||
| } | ||
| defer syscall.CertCloseStore(storeHandle, 0) | ||
| var cert *syscall.CertContext | ||
| for { | ||
| cert, err = syscall.CertEnumCertificatesInStore(storeHandle, cert) | ||
| if err != nil { | ||
| if errno, ok := err.(syscall.Errno); ok { | ||
| if errno == CryptENotFound { | ||
| break | ||
| } | ||
| } | ||
| return errors.New(syscall.GetLastError().Error()) | ||
| } | ||
| 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 { | ||
| // AddCert contains logic to prevent adding a duplicate certificate to the pool | ||
| roots.AddCert(c) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should also continue to fallback to
gocertifi.CACerts()if we cannot open theSystemCertPoolon macOS / Linux.While the fallback path was primarily there for Windows support, macOS could still have a non-nil
errif opening the root store failed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I’ll go ahead and get to work on implementing this