Skip to content

Commit 918765b

Browse files
rolandshoemakergopherbot
authored andcommitted
crypto/x509: switch default policy field to Policies
Switch from Certificate.PolicyIdentifiers to Certificate.Policies when marshalling. Fixes #67620 Change-Id: Ib627135a569f53d344b4ee2f892ba139506ce0d1 Reviewed-on: https://go-review.googlesource.com/c/go/+/629855 Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Roland Shoemaker <[email protected]>
1 parent fb43278 commit 918765b

File tree

5 files changed

+36
-13
lines changed

5 files changed

+36
-13
lines changed

doc/godebug.md

+8
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,14 @@ This currently only affects arm64 programs. For all other platforms it is a no-o
195195
Go 1.24 removed the `x509sha1` setting. `crypto/x509` no longer supports verifying
196196
signatures on certificates that use SHA-1 based signature algorithms.
197197

198+
Go 1.24 changes the default value of the [`x509usepolicies`
199+
setting.](/pkg/crypto/x509/#CreateCertificate) from `0` to `1`. When marshalling
200+
certificates, policies are now taken from the
201+
[`Certificate.Policies`](/pkg/crypto/x509/#Certificate.Policies) field rather
202+
than the
203+
[`Certificate.PolicyIdentifiers`](/pkg/crypto/x509/#Certificate.PolicyIdentifiers)
204+
field by default.
205+
198206
### Go 1.23
199207

200208
Go 1.23 changed the channels created by package time to be unbuffered
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
The default certificate policies field has changed from
2+
[Certificate.PolicyIdentifiers] to [Certificate.Policies]. When parsing
3+
certificates, both fields will be populated, but when creating certificates
4+
policies will now be taken from the [Certificate.Policies] field instead of the
5+
[Certificate.PolicyIdentifiers field]. This change can be reverted by setting
6+
`GODEBUG=x509usepolicies=0`.

src/crypto/x509/x509.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -786,9 +786,13 @@ type Certificate struct {
786786
// cannot be represented by asn1.ObjectIdentifier, it will not be included in
787787
// PolicyIdentifiers, but will be present in Policies, which contains all parsed
788788
// policy OIDs.
789+
// See CreateCertificate for context about how this field and the Policies field
790+
// interact.
789791
PolicyIdentifiers []asn1.ObjectIdentifier
790792

791793
// Policies contains all policy identifiers included in the certificate.
794+
// See CreateCertificate for context about how this field and the PolicyIdentifiers field
795+
// interact.
792796
// In Go 1.22, encoding/gob cannot handle and ignores this field.
793797
Policies []OID
794798

@@ -1259,7 +1263,7 @@ func buildCertExtensions(template *Certificate, subjectIsEmpty bool, authorityKe
12591263
n++
12601264
}
12611265

1262-
usePolicies := x509usepolicies.Value() == "1"
1266+
usePolicies := x509usepolicies.Value() != "0"
12631267
if ((!usePolicies && len(template.PolicyIdentifiers) > 0) || (usePolicies && len(template.Policies) > 0)) &&
12641268
!oidInExtensions(oidExtensionCertificatePolicies, template.ExtraExtensions) {
12651269
ret[n], err = marshalCertificatePolicies(template.Policies, template.PolicyIdentifiers)
@@ -1452,7 +1456,7 @@ func marshalCertificatePolicies(policies []OID, policyIdentifiers []asn1.ObjectI
14521456

14531457
b := cryptobyte.NewBuilder(make([]byte, 0, 128))
14541458
b.AddASN1(cryptobyte_asn1.SEQUENCE, func(child *cryptobyte.Builder) {
1455-
if x509usepolicies.Value() == "1" {
1459+
if x509usepolicies.Value() != "0" {
14561460
x509usepolicies.IncNonDefault()
14571461
for _, v := range policies {
14581462
child.AddASN1(cryptobyte_asn1.SEQUENCE, func(child *cryptobyte.Builder) {
@@ -1651,10 +1655,10 @@ var emptyASN1Subject = []byte{0x30, 0}
16511655
// If SubjectKeyId from template is empty and the template is a CA, SubjectKeyId
16521656
// will be generated from the hash of the public key.
16531657
//
1654-
// The PolicyIdentifier and Policies fields are both used to marshal certificate
1655-
// policy OIDs. By default, only the PolicyIdentifier is marshaled, but if the
1656-
// GODEBUG setting "x509usepolicies" has the value "1", the Policies field will
1657-
// be marshaled instead of the PolicyIdentifier field. The Policies field can
1658+
// The PolicyIdentifier and Policies fields can both be used to marshal certificate
1659+
// policy OIDs. By default, only the Policies is marshaled, but if the
1660+
// GODEBUG setting "x509usepolicies" has the value "0", the PolicyIdentifiers field will
1661+
// be marshaled instead of the Policies field. This changed in Go 1.24. The Policies field can
16581662
// be used to marshal policy OIDs which have components that are larger than 31
16591663
// bits.
16601664
func CreateCertificate(rand io.Reader, template, parent *Certificate, pub, priv any) ([]byte, error) {

src/crypto/x509/x509_test.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,6 @@ func TestCreateSelfSignedCertificate(t *testing.T) {
673673
IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1).To4(), net.ParseIP("2001:4860:0:2001::68")},
674674
URIs: []*url.URL{parseURI("https://foo.com/wibble#foo")},
675675

676-
PolicyIdentifiers: []asn1.ObjectIdentifier{[]int{1, 2, 3}},
677676
Policies: []OID{mustNewOIDFromInts([]uint64{1, 2, 3, math.MaxUint32, math.MaxUint64})},
678677
PermittedDNSDomains: []string{".example.com", "example.com"},
679678
ExcludedDNSDomains: []string{"bar.example.com"},
@@ -712,8 +711,8 @@ func TestCreateSelfSignedCertificate(t *testing.T) {
712711
continue
713712
}
714713

715-
if len(cert.PolicyIdentifiers) != 1 || !cert.PolicyIdentifiers[0].Equal(template.PolicyIdentifiers[0]) {
716-
t.Errorf("%s: failed to parse policy identifiers: got:%#v want:%#v", test.name, cert.PolicyIdentifiers, template.PolicyIdentifiers)
714+
if len(cert.Policies) != 1 || !cert.Policies[0].Equal(template.Policies[0]) {
715+
t.Errorf("%s: failed to parse policy identifiers: got:%#v want:%#v", test.name, cert.PolicyIdentifiers, template.Policies)
717716
}
718717

719718
if len(cert.PermittedDNSDomains) != 2 || cert.PermittedDNSDomains[0] != ".example.com" || cert.PermittedDNSDomains[1] != "example.com" {
@@ -3916,7 +3915,9 @@ func TestDuplicateAttributesCSR(t *testing.T) {
39163915
}
39173916
}
39183917

3919-
func TestCertificateOIDPolicies(t *testing.T) {
3918+
func TestCertificateOIDPoliciesGODEBUG(t *testing.T) {
3919+
t.Setenv("GODEBUG", "x509usepolicies=0")
3920+
39203921
template := Certificate{
39213922
SerialNumber: big.NewInt(1),
39223923
Subject: pkix.Name{CommonName: "Cert"},
@@ -3952,7 +3953,11 @@ func TestCertificateOIDPolicies(t *testing.T) {
39523953
}
39533954
}
39543955

3955-
func TestCertificatePoliciesGODEBUG(t *testing.T) {
3956+
func TestCertificatePolicies(t *testing.T) {
3957+
if x509usepolicies.Value() == "0" {
3958+
t.Skip("test relies on default x509usepolicies GODEBUG")
3959+
}
3960+
39563961
template := Certificate{
39573962
SerialNumber: big.NewInt(1),
39583963
Subject: pkix.Name{CommonName: "Cert"},
@@ -3962,7 +3967,7 @@ func TestCertificatePoliciesGODEBUG(t *testing.T) {
39623967
Policies: []OID{mustNewOIDFromInts([]uint64{1, 2, math.MaxUint32 + 1})},
39633968
}
39643969

3965-
expectPolicies := []OID{mustNewOIDFromInts([]uint64{1, 2, 3})}
3970+
expectPolicies := []OID{mustNewOIDFromInts([]uint64{1, 2, math.MaxUint32 + 1})}
39663971
certDER, err := CreateCertificate(rand.Reader, &template, &template, rsaPrivateKey.Public(), rsaPrivateKey)
39673972
if err != nil {
39683973
t.Fatalf("CreateCertificate() unexpected error: %v", err)

src/internal/godebugs/table.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ var All = []Info{
6363
{Name: "x509keypairleaf", Package: "crypto/tls", Changed: 23, Old: "0"},
6464
{Name: "x509negativeserial", Package: "crypto/x509", Changed: 23, Old: "1"},
6565
{Name: "x509usefallbackroots", Package: "crypto/x509"},
66-
{Name: "x509usepolicies", Package: "crypto/x509"},
66+
{Name: "x509usepolicies", Package: "crypto/x509", Changed: 24, Old: "0"},
6767
{Name: "zipinsecurepath", Package: "archive/zip"},
6868
}
6969

0 commit comments

Comments
 (0)