Skip to content

Commit b691da9

Browse files
Revert "crypto/x509: reject serial numbers longer than 20 octets"
This reverts commit 8524931. Reason for revert: It turns out, basically no one in private PKIs can get this right. It causes way too much breakage, and every other impl also ignores it, so we'll continue to be in good company. Change-Id: I2da808b411ec12f72112c49079faf9f68ae465c9 Reviewed-on: https://go-review.googlesource.com/c/go/+/589615 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent 45446c8 commit b691da9

File tree

5 files changed

+1
-57
lines changed

5 files changed

+1
-57
lines changed

doc/godebug.md

-5
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,6 @@ Go 1.23 changed the behavior of
185185
serial numbers that are negative. This change can be reverted with
186186
the [`x509negativeserial` setting](/pkg/crypto/x509/#ParseCertificate).
187187

188-
Go 1.23 changed the behavior of
189-
[crypto/x509.ParseCertificate](/pkg/crypto/x509/#ParseCertificate) to reject
190-
serial numbers that are longer than 20 octets. This change can be reverted with
191-
the [`x509seriallength` setting](/pkg/crypto/x509/#ParseCertificate).
192-
193188
Go 1.23 re-enabled support in html/template for ECMAScript 6 template literals by default.
194189
The [`jstmpllitinterp` setting](/pkg/html/template#hdr-Security_Model) no longer has
195190
any effect.

src/crypto/x509/parser.go

+1-23
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,6 @@ func processExtensions(out *Certificate) error {
825825
}
826826

827827
var x509negativeserial = godebug.New("x509negativeserial")
828-
var x509seriallength = godebug.New("x509seriallength")
829828

830829
func parseCertificate(der []byte) (*Certificate, error) {
831830
cert := &Certificate{}
@@ -866,27 +865,10 @@ func parseCertificate(der []byte) (*Certificate, error) {
866865
return nil, errors.New("x509: invalid version")
867866
}
868867

869-
var serialBytes cryptobyte.String
870-
if !tbs.ReadASN1Element(&serialBytes, cryptobyte_asn1.INTEGER) {
871-
return nil, errors.New("x509: malformed serial number")
872-
}
873-
// We add two bytes for the tag and length (if the length was multi-byte,
874-
// which is possible, the length of the serial would be more than 256 bytes,
875-
// so this condition would trigger anyway).
876-
if len(serialBytes) > 20+2 {
877-
if x509seriallength.Value() != "1" {
878-
return nil, errors.New("x509: serial number too long (>20 octets)")
879-
} else {
880-
x509seriallength.IncNonDefault()
881-
}
882-
}
883868
serial := new(big.Int)
884-
if !serialBytes.ReadASN1Integer(serial) {
869+
if !tbs.ReadASN1Integer(serial) {
885870
return nil, errors.New("x509: malformed serial number")
886871
}
887-
// We do not reject zero serials, because they are unfortunately common
888-
// in important root certificates which will not expire for a number of
889-
// years.
890872
if serial.Sign() == -1 {
891873
if x509negativeserial.Value() != "1" {
892874
return nil, errors.New("x509: negative serial number")
@@ -1034,10 +1016,6 @@ func parseCertificate(der []byte) (*Certificate, error) {
10341016
// Before Go 1.23, ParseCertificate accepted certificates with negative serial
10351017
// numbers. This behavior can be restored by including "x509negativeserial=1" in
10361018
// the GODEBUG environment variable.
1037-
//
1038-
// Before Go 1.23, ParseCertificate accepted certificates with serial numbers
1039-
// longer than 20 octets. This behavior can be restored by including
1040-
// "x509seriallength=1" in the GODEBUG environment variable.
10411019
func ParseCertificate(der []byte) (*Certificate, error) {
10421020
cert, err := parseCertificate(der)
10431021
if err != nil {

src/crypto/x509/x509_test.go

-23
Original file line numberDiff line numberDiff line change
@@ -4086,26 +4086,3 @@ func TestRejectCriticalSKI(t *testing.T) {
40864086
t.Fatalf("ParseCertificate() unexpected error: %v, want: %s", err, expectedErr)
40874087
}
40884088
}
4089-
4090-
func TestSerialTooLong(t *testing.T) {
4091-
template := Certificate{
4092-
Subject: pkix.Name{CommonName: "Cert"},
4093-
NotBefore: time.Unix(1000, 0),
4094-
NotAfter: time.Unix(100000, 0),
4095-
}
4096-
for _, serial := range []*big.Int{
4097-
big.NewInt(0).SetBytes(bytes.Repeat([]byte{5}, 21)),
4098-
big.NewInt(0).SetBytes(bytes.Repeat([]byte{255}, 20)),
4099-
} {
4100-
template.SerialNumber = serial
4101-
certDER, err := CreateCertificate(rand.Reader, &template, &template, rsaPrivateKey.Public(), rsaPrivateKey)
4102-
if err != nil {
4103-
t.Fatalf("CreateCertificate() unexpected error: %v", err)
4104-
}
4105-
expectedErr := "x509: serial number too long (>20 octets)"
4106-
_, err = ParseCertificate(certDER)
4107-
if err == nil || err.Error() != expectedErr {
4108-
t.Fatalf("ParseCertificate() unexpected error: %v, want: %s", err, expectedErr)
4109-
}
4110-
}
4111-
}

src/internal/godebugs/table.go

-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ var All = []Info{
5757
{Name: "winsymlink", Package: "os", Changed: 22, Old: "0"},
5858
{Name: "x509keypairleaf", Package: "crypto/tls", Changed: 23, Old: "0"},
5959
{Name: "x509negativeserial", Package: "crypto/x509", Changed: 23, Old: "1"},
60-
{Name: "x509seriallength", Package: "crypto/x509", Changed: 23, Old: "1"},
6160
{Name: "x509sha1", Package: "crypto/x509"},
6261
{Name: "x509usefallbackroots", Package: "crypto/x509"},
6362
{Name: "x509usepolicies", Package: "crypto/x509"},

src/runtime/metrics/doc.go

-5
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,6 @@ Below is the full list of supported metrics, ordered lexicographically.
340340
package due to a non-default GODEBUG=x509negativeserial=...
341341
setting.
342342
343-
/godebug/non-default-behavior/x509seriallength:events
344-
The number of non-default behaviors executed by the crypto/x509
345-
package due to a non-default GODEBUG=x509seriallength=...
346-
setting.
347-
348343
/godebug/non-default-behavior/x509sha1:events
349344
The number of non-default behaviors executed by the crypto/x509
350345
package due to a non-default GODEBUG=x509sha1=... setting.

0 commit comments

Comments
 (0)