Skip to content

Commit 5214949

Browse files
crypto/x509: revert Policies marshaling behavior
Don't marshal Policies field. Updates #64248 Change-Id: I7e6d8b9ff1b3698bb4f585fa82fc4050eff3ae4d Reviewed-on: https://go-review.googlesource.com/c/go/+/546915 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent c002a5d commit 5214949

File tree

2 files changed

+3
-54
lines changed

2 files changed

+3
-54
lines changed

src/crypto/x509/x509.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,9 +1186,9 @@ func buildCertExtensions(template *Certificate, subjectIsEmpty bool, authorityKe
11861186
n++
11871187
}
11881188

1189-
if (len(template.PolicyIdentifiers) > 0 || len(template.Policies) > 0) &&
1189+
if len(template.PolicyIdentifiers) > 0 &&
11901190
!oidInExtensions(oidExtensionCertificatePolicies, template.ExtraExtensions) {
1191-
ret[n], err = marshalCertificatePolicies(template.Policies, template.PolicyIdentifiers)
1191+
ret[n], err = marshalCertificatePolicies(template.PolicyIdentifiers)
11921192
if err != nil {
11931193
return nil, err
11941194
}
@@ -1373,32 +1373,12 @@ func marshalBasicConstraints(isCA bool, maxPathLen int, maxPathLenZero bool) (pk
13731373
return ext, err
13741374
}
13751375

1376-
func marshalCertificatePolicies(policies []OID, policyIdentifiers []asn1.ObjectIdentifier) (pkix.Extension, error) {
1376+
func marshalCertificatePolicies(policyIdentifiers []asn1.ObjectIdentifier) (pkix.Extension, error) {
13771377
ext := pkix.Extension{Id: oidExtensionCertificatePolicies}
13781378

13791379
b := cryptobyte.NewBuilder(make([]byte, 0, 128))
13801380
b.AddASN1(cryptobyte_asn1.SEQUENCE, func(child *cryptobyte.Builder) {
1381-
// added is used to track OIDs which are duplicated in both Policies and PolicyIdentifiers
1382-
// so they can be skipped. Note that this explicitly doesn't check for duplicate OIDs in
1383-
// Policies or in PolicyIdentifiers themselves, as this would be considered breaking behavior.
1384-
added := map[string]bool{}
1385-
for _, v := range policies {
1386-
child.AddASN1(cryptobyte_asn1.SEQUENCE, func(child *cryptobyte.Builder) {
1387-
child.AddASN1(cryptobyte_asn1.OBJECT_IDENTIFIER, func(child *cryptobyte.Builder) {
1388-
oidStr := v.String()
1389-
added[oidStr] = true
1390-
if len(v.der) == 0 {
1391-
child.SetError(errors.New("invalid policy object identifier"))
1392-
return
1393-
}
1394-
child.AddBytes(v.der)
1395-
})
1396-
})
1397-
}
13981381
for _, v := range policyIdentifiers {
1399-
if added[v.String()] {
1400-
continue
1401-
}
14021382
child.AddASN1(cryptobyte_asn1.SEQUENCE, func(child *cryptobyte.Builder) {
14031383
child.AddASN1ObjectIdentifier(v)
14041384
})
@@ -1547,7 +1527,6 @@ var emptyASN1Subject = []byte{0x30, 0}
15471527
// - PermittedIPRanges
15481528
// - PermittedURIDomains
15491529
// - PolicyIdentifiers
1550-
// - Policies
15511530
// - SerialNumber
15521531
// - SignatureAlgorithm
15531532
// - Subject
@@ -1571,9 +1550,6 @@ var emptyASN1Subject = []byte{0x30, 0}
15711550
//
15721551
// If SubjectKeyId from template is empty and the template is a CA, SubjectKeyId
15731552
// will be generated from the hash of the public key.
1574-
//
1575-
// If both PolicyIdentifiers and Policies are populated, any OID which appears
1576-
// in both slices will only be added to the certificate policies extension once.
15771553
func CreateCertificate(rand io.Reader, template, parent *Certificate, pub, priv any) ([]byte, error) {
15781554
key, ok := priv.(crypto.Signer)
15791555
if !ok {

src/crypto/x509/x509_test.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3928,25 +3928,14 @@ func TestCertificateOIDPolicies(t *testing.T) {
39283928
NotBefore: time.Unix(1000, 0),
39293929
NotAfter: time.Unix(100000, 0),
39303930
PolicyIdentifiers: []asn1.ObjectIdentifier{[]int{1, 2, 3}},
3931-
Policies: []OID{
3932-
mustNewOIDFromInts(t, []uint64{1, 2, 3}),
3933-
mustNewOIDFromInts(t, []uint64{1, 2, 3, 4, 5}),
3934-
mustNewOIDFromInts(t, []uint64{1, 2, 3, math.MaxInt32}),
3935-
mustNewOIDFromInts(t, []uint64{1, 2, 3, math.MaxUint32, math.MaxUint64}),
3936-
},
39373931
}
39383932

39393933
var expectPolicyIdentifiers = []asn1.ObjectIdentifier{
39403934
[]int{1, 2, 3},
3941-
[]int{1, 2, 3, 4, 5},
3942-
[]int{1, 2, 3, math.MaxInt32},
39433935
}
39443936

39453937
var expectPolicies = []OID{
39463938
mustNewOIDFromInts(t, []uint64{1, 2, 3}),
3947-
mustNewOIDFromInts(t, []uint64{1, 2, 3, 4, 5}),
3948-
mustNewOIDFromInts(t, []uint64{1, 2, 3, math.MaxInt32}),
3949-
mustNewOIDFromInts(t, []uint64{1, 2, 3, math.MaxUint32, math.MaxUint64}),
39503939
}
39513940

39523941
certDER, err := CreateCertificate(rand.Reader, &template, &template, rsaPrivateKey.Public(), rsaPrivateKey)
@@ -3967,19 +3956,3 @@ func TestCertificateOIDPolicies(t *testing.T) {
39673956
t.Errorf("cert.Policies = %v, want: %v", cert.Policies, expectPolicies)
39683957
}
39693958
}
3970-
3971-
func TestInvalidPolicyOID(t *testing.T) {
3972-
template := Certificate{
3973-
SerialNumber: big.NewInt(1),
3974-
Subject: pkix.Name{CommonName: "Cert"},
3975-
NotBefore: time.Now(),
3976-
NotAfter: time.Now().Add(time.Hour),
3977-
PolicyIdentifiers: []asn1.ObjectIdentifier{[]int{1, 2, 3}},
3978-
Policies: []OID{OID{}},
3979-
}
3980-
_, err := CreateCertificate(rand.Reader, &template, &template, rsaPrivateKey.Public(), rsaPrivateKey)
3981-
expected := "invalid policy object identifier"
3982-
if err.Error() != expected {
3983-
t.Fatalf("CreateCertificate() unexpected error: %v, want: %v", err, expected)
3984-
}
3985-
}

0 commit comments

Comments
 (0)