Skip to content
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

Root certs are not used on Windows #67

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions command_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"io"
"os"

"github.com/certifi/gocertifi"
"github.com/github/ietf-cms"
"github.com/pkg/errors"
)


func commandVerify() error {
sNewSig.emit()

Expand Down Expand Up @@ -165,25 +165,20 @@ func verifyDetached() error {
}

func verifyOpts() x509.VerifyOptions {
roots, err := x509.SystemCertPool()
if err != nil {
// SystemCertPool isn't implemented for Windows. fall back to mozilla trust
// store.
roots, err = gocertifi.CACerts()
if err != nil {
// Fall back to an empty store. Verification will likely fail.
roots = x509.NewCertPool()
}
}
var (
roots *x509.CertPool
)

for _, ident := range idents {
if cert, err := ident.Certificate(); err == nil {
roots.AddCert(cert)
}
// Depending on the operating system, enumerate the trusted root certificate store
err := parseRoots(roots)
if err != nil{
// Fall back to an empty store. Verifications will likely fail.
roots = x509.NewCertPool()
}

return x509.VerifyOptions{
Roots: roots,
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageAny},
}
}

22 changes: 22 additions & 0 deletions parse_roots.go
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 {
Copy link
Member

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 the SystemCertPool on macOS / Linux.

While the fallback path was primarily there for Windows support, macOS could still have a non-nil err if opening the root store failed.

Copy link
Author

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

if cert, err := ident.Certificate(); err == nil {
roots.AddCert(cert)
}
}
return nil
}
65 changes: 65 additions & 0 deletions parse_roots_windows.go
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a call to CertCloseStore anywhere, so I think this is leaking HCERTSTORE handles.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a defered call to CertCloseStore to resolve this.

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
}