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

Better error message for missing intermediates #182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
40 changes: 40 additions & 0 deletions lib/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"time"

Expand Down Expand Up @@ -109,6 +111,30 @@ func caBundle(caPath string) *x509.CertPool {
return bundle
}

func fetchAIA(cert *x509.Certificate) *x509.Certificate {
for _, url := range cert.IssuingCertificateURL {
Copy link
Contributor

Choose a reason for hiding this comment

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

rfc4325 says we should look at the file extension to see if it's .cer or .p7c. We could return if it's .p7c and only support .cer fetching

resp, err := http.Get(url)
if err != nil {
continue
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we log errors?

}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
continue
}

intermediate, err := x509.ParseCertificate(body)
if err != nil {
continue
}

return intermediate
}

return nil
}

func VerifyChain(certs []*x509.Certificate, ocspStaple []byte, dnsName, caPath string) SimpleVerification {
result := SimpleVerification{
Chains: [][]simpleVerifyCert{},
Expand All @@ -129,6 +155,20 @@ func VerifyChain(certs []*x509.Certificate, ocspStaple []byte, dnsName, caPath s
chains, err := certs[0].Verify(opts)
if err != nil {
result.Error = fmt.Sprintf("%s", err)

if len(intermediates.Subjects()) == 0 {
// No intermediates found, maybe broken chain. Let's try AIA fetching?
intermediate := fetchAIA(certs[0])
Copy link
Contributor

Choose a reason for hiding this comment

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

We're only trying the first one in the chain? I guess it's pretty rare to have more than 3 certs in a chain.

if intermediate != nil {
intermediates.AddCert(intermediate)
_, err := certs[0].Verify(opts)
if err == nil {
// Would work with proper intermediates.
result.Error = "server is not sending required intermediate certificate(s)"
}
}
}

return result
}

Expand Down