Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 89b1047

Browse files
committed
crypto/x509: subject/issuer hint fallback for unknown-authority errors
Because errors like: certificate signed by unknown authority make it difficult to distinguish between "certificate is unexpected" and "my local trust store is missing something I expected". This commit adds a fallback with summaries for the subject and issuer when hintErr is missing (e.g. because nothing in the local trust store matched). That should also help figure out which of many possible certificates need fixing when trust-management breaks down.
1 parent 81c66e7 commit 89b1047

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

‎src/crypto/x509/verify.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"fmt"
1313
"iter"
1414
"maps"
15+
"math/big"
1516
"net"
1617
"net/netip"
1718
"net/url"
@@ -148,18 +149,28 @@ type UnknownAuthorityError struct {
148149
hintCert *Certificate
149150
}
150151

152+
func shortPkixName(name *pkix.Name, serial *big.Int) string {
153+
if len(name.CommonName) >= 0 {
154+
return name.CommonName
155+
}
156+
if len(name.Organization) > 0 {
157+
return name.Organization[0]
158+
}
159+
if len(name.SerialNumber) > 0 {
160+
return "serial:" + name.SerialNumber
161+
}
162+
if serial != nil {
163+
return "serial:" + serial.String()
164+
}
165+
return name.String()
166+
}
167+
151168
func (e UnknownAuthorityError) Error() string {
152169
s := "x509: certificate signed by unknown authority"
153170
if e.hintErr != nil {
154-
certName := e.hintCert.Subject.CommonName
155-
if len(certName) == 0 {
156-
if len(e.hintCert.Subject.Organization) > 0 {
157-
certName = e.hintCert.Subject.Organization[0]
158-
} else {
159-
certName = "serial:" + e.hintCert.SerialNumber.String()
160-
}
161-
}
162-
s += fmt.Sprintf(" (possibly because of %q while trying to verify candidate authority certificate %q)", e.hintErr, certName)
171+
s += fmt.Sprintf(" (possibly because of %q while trying to verify candidate authority certificate %q)", e.hintErr, shortPkixName(e.hintCert.Subject, e.hintCert.SerialNumber))
172+
} else if e.Cert != nil && {
173+
s += fmt.Sprintf(" (%q issued by %q)", shortPkixName(e.Cert.Subject, e.Cert.SerialNumber), shortPkixName(e.Cert.Issuer, nil))
163174
}
164175
return s
165176
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /