Skip to content

Commit a0ea93d

Browse files
committed
crypto/x509: permit serial numbers to be negative.
Some software that produces certificates doesn't encode integers correctly and, about half the time, ends up producing certificates with serial numbers that are actually negative. This buggy software, sadly, appears to be common enough that we should let these errors pass. This change allows a Certificate.SerialNumber to be negative. Fixes #8265. Change-Id: Ief35dae23988fb6d5e2873e3c521366fb03c6af4 Reviewed-on: https://go-review.googlesource.com/17247 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 85bfa33 commit a0ea93d

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/crypto/x509/x509.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -909,10 +909,6 @@ func parseCertificate(in *certificate) (*Certificate, error) {
909909
return nil, err
910910
}
911911

912-
if in.TBSCertificate.SerialNumber.Sign() < 0 {
913-
return nil, errors.New("x509: negative serial number")
914-
}
915-
916912
out.Version = in.TBSCertificate.Version + 1
917913
out.SerialNumber = in.TBSCertificate.SerialNumber
918914

src/crypto/x509/x509_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,11 @@ func TestCreateSelfSignedCertificate(t *testing.T) {
343343
for _, test := range tests {
344344
commonName := "test.example.com"
345345
template := Certificate{
346-
SerialNumber: big.NewInt(1),
346+
// SerialNumber is negative to ensure that negative
347+
// values are parsed. This is due to the prevalence of
348+
// buggy code that produces certificates with negative
349+
// serial numbers.
350+
SerialNumber: big.NewInt(-1),
347351
Subject: pkix.Name{
348352
CommonName: commonName,
349353
Organization: []string{"Σ Acme Co"},

0 commit comments

Comments
 (0)