Skip to content

Commit 4787e90

Browse files
committed
crypto/x509: rollback new CertificateRequest fields
In general, we don't want to encourage reading them from CSRs, and applications that really want to can parse the Extensions field. Note that this also fixes a bug where the error of parseKeyUsageExtension was not handled in parseCertificateRequest. Fixes #43477 Updates #37172 Change-Id: Ia5707b0e23cecc0aed57e419a1ca25e26eea6bbe Reviewed-on: https://go-review.googlesource.com/c/go/+/281235 Trust: Filippo Valsorda <[email protected]> Run-TryBot: Filippo Valsorda <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]>
1 parent c9658be commit 4787e90

File tree

4 files changed

+25
-132
lines changed

4 files changed

+25
-132
lines changed

api/go1.16.txt

-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
pkg archive/zip, method (*ReadCloser) Open(string) (fs.File, error)
22
pkg archive/zip, method (*Reader) Open(string) (fs.File, error)
33
pkg crypto/x509, method (SystemRootsError) Unwrap() error
4-
pkg crypto/x509, type CertificateRequest struct, BasicConstraintsValid bool
5-
pkg crypto/x509, type CertificateRequest struct, ExtKeyUsage []ExtKeyUsage
6-
pkg crypto/x509, type CertificateRequest struct, IsCA bool
7-
pkg crypto/x509, type CertificateRequest struct, KeyUsage KeyUsage
8-
pkg crypto/x509, type CertificateRequest struct, MaxPathLen int
9-
pkg crypto/x509, type CertificateRequest struct, MaxPathLenZero bool
10-
pkg crypto/x509, type CertificateRequest struct, PolicyIdentifiers []asn1.ObjectIdentifier
11-
pkg crypto/x509, type CertificateRequest struct, SubjectKeyId []uint8
12-
pkg crypto/x509, type CertificateRequest struct, UnknownExtKeyUsage []asn1.ObjectIdentifier
134
pkg debug/elf, const DT_ADDRRNGHI = 1879047935
145
pkg debug/elf, const DT_ADDRRNGHI DynTag
156
pkg debug/elf, const DT_ADDRRNGLO = 1879047680

doc/go1.16.html

-8
Original file line numberDiff line numberDiff line change
@@ -590,14 +590,6 @@ <h3 id="minor_library_changes">Minor changes to the library</h3>
590590
a malformed certificate.
591591
</p>
592592

593-
<p><!-- CL 233163 -->
594-
A number of additional fields have been added to the
595-
<a href="/pkg/crypto/x509/#CertificateRequest"><code>CertificateRequest</code></a> type.
596-
These fields are now parsed in <a href="/pkg/crypto/x509/#ParseCertificateRequest">
597-
<code>ParseCertificateRequest</code></a> and marshalled in
598-
<a href="/pkg/crypto/x509/#CreateCertificateRequest"><code>CreateCertificateRequest</code></a>.
599-
</p>
600-
601593
<p><!-- CL 257939 -->
602594
DSA signature verification is no longer supported. Note that DSA signature
603595
generation was never supported.

src/crypto/x509/x509.go

-84
Original file line numberDiff line numberDiff line change
@@ -2006,40 +2006,6 @@ func buildCSRExtensions(template *CertificateRequest) ([]pkix.Extension, error)
20062006
ret = append(ret, ext)
20072007
}
20082008

2009-
if (len(template.ExtKeyUsage) > 0 || len(template.UnknownExtKeyUsage) > 0) &&
2010-
!oidInExtensions(oidExtensionExtendedKeyUsage, template.ExtraExtensions) {
2011-
ext, err := marshalExtKeyUsage(template.ExtKeyUsage, template.UnknownExtKeyUsage)
2012-
if err != nil {
2013-
return nil, err
2014-
}
2015-
ret = append(ret, ext)
2016-
}
2017-
2018-
if template.BasicConstraintsValid && !oidInExtensions(oidExtensionBasicConstraints, template.ExtraExtensions) {
2019-
ext, err := marshalBasicConstraints(template.IsCA, template.MaxPathLen, template.MaxPathLenZero)
2020-
if err != nil {
2021-
return nil, err
2022-
}
2023-
ret = append(ret, ext)
2024-
}
2025-
2026-
if len(template.SubjectKeyId) > 0 && !oidInExtensions(oidExtensionSubjectKeyId, template.ExtraExtensions) {
2027-
skidBytes, err := asn1.Marshal(template.SubjectKeyId)
2028-
if err != nil {
2029-
return nil, err
2030-
}
2031-
ret = append(ret, pkix.Extension{Id: oidExtensionSubjectKeyId, Value: skidBytes})
2032-
}
2033-
2034-
if len(template.PolicyIdentifiers) > 0 &&
2035-
!oidInExtensions(oidExtensionCertificatePolicies, template.ExtraExtensions) {
2036-
ext, err := marshalCertificatePolicies(template.PolicyIdentifiers)
2037-
if err != nil {
2038-
return nil, err
2039-
}
2040-
ret = append(ret, ext)
2041-
}
2042-
20432009
return append(ret, template.ExtraExtensions...), nil
20442010
}
20452011

@@ -2438,37 +2404,6 @@ type CertificateRequest struct {
24382404
EmailAddresses []string
24392405
IPAddresses []net.IP
24402406
URIs []*url.URL
2441-
2442-
ExtKeyUsage []ExtKeyUsage // Sequence of extended key usages.
2443-
UnknownExtKeyUsage []asn1.ObjectIdentifier // Encountered extended key usages unknown to this package.
2444-
2445-
// BasicConstraintsValid indicates whether IsCA, MaxPathLen,
2446-
// and MaxPathLenZero are valid.
2447-
BasicConstraintsValid bool
2448-
IsCA bool
2449-
2450-
// MaxPathLen and MaxPathLenZero indicate the presence and
2451-
// value of the BasicConstraints' "pathLenConstraint".
2452-
//
2453-
// When parsing a certificate, a positive non-zero MaxPathLen
2454-
// means that the field was specified, -1 means it was unset,
2455-
// and MaxPathLenZero being true mean that the field was
2456-
// explicitly set to zero. The case of MaxPathLen==0 with MaxPathLenZero==false
2457-
// should be treated equivalent to -1 (unset).
2458-
//
2459-
// When generating a certificate, an unset pathLenConstraint
2460-
// can be requested with either MaxPathLen == -1 or using the
2461-
// zero value for both MaxPathLen and MaxPathLenZero.
2462-
MaxPathLen int
2463-
// MaxPathLenZero indicates that BasicConstraintsValid==true
2464-
// and MaxPathLen==0 should be interpreted as an actual
2465-
// maximum path length of zero. Otherwise, that combination is
2466-
// interpreted as MaxPathLen not being set.
2467-
MaxPathLenZero bool
2468-
2469-
SubjectKeyId []byte
2470-
2471-
PolicyIdentifiers []asn1.ObjectIdentifier
24722407
}
24732408

24742409
// These structures reflect the ASN.1 structure of X.509 certificate
@@ -2801,25 +2736,6 @@ func parseCertificateRequest(in *certificateRequest) (*CertificateRequest, error
28012736
}
28022737
case extension.Id.Equal(oidExtensionKeyUsage):
28032738
out.KeyUsage, err = parseKeyUsageExtension(extension.Value)
2804-
case extension.Id.Equal(oidExtensionExtendedKeyUsage):
2805-
out.ExtKeyUsage, out.UnknownExtKeyUsage, err = parseExtKeyUsageExtension(extension.Value)
2806-
if err != nil {
2807-
return nil, err
2808-
}
2809-
case extension.Id.Equal(oidExtensionBasicConstraints):
2810-
out.IsCA, out.MaxPathLen, err = parseBasicConstraintsExtension(extension.Value)
2811-
if err != nil {
2812-
return nil, err
2813-
}
2814-
out.BasicConstraintsValid = true
2815-
out.MaxPathLenZero = out.MaxPathLen == 0
2816-
case extension.Id.Equal(oidExtensionSubjectKeyId):
2817-
out.SubjectKeyId, err = parseSubjectKeyIdExtension(extension.Value)
2818-
if err != nil {
2819-
return nil, err
2820-
}
2821-
case extension.Id.Equal(oidExtensionCertificatePolicies):
2822-
out.PolicyIdentifiers, err = parseCertificatePoliciesExtension(extension.Value)
28232739
if err != nil {
28242740
return nil, err
28252741
}

src/crypto/x509/x509_test.go

+25-31
Original file line numberDiff line numberDiff line change
@@ -2964,44 +2964,38 @@ func certPoolEqual(a, b *CertPool) bool {
29642964
}
29652965

29662966
func TestCertificateRequestRoundtripFields(t *testing.T) {
2967+
urlA, err := url.Parse("https://example.com/_")
2968+
if err != nil {
2969+
t.Fatal(err)
2970+
}
2971+
urlB, err := url.Parse("https://example.org/_")
2972+
if err != nil {
2973+
t.Fatal(err)
2974+
}
29672975
in := &CertificateRequest{
2968-
KeyUsage: KeyUsageCertSign,
2969-
ExtKeyUsage: []ExtKeyUsage{ExtKeyUsageAny},
2970-
UnknownExtKeyUsage: []asn1.ObjectIdentifier{{1, 2, 3}},
2971-
BasicConstraintsValid: true,
2972-
IsCA: true,
2973-
MaxPathLen: 0,
2974-
MaxPathLenZero: true,
2975-
SubjectKeyId: []byte{1, 2, 3},
2976-
PolicyIdentifiers: []asn1.ObjectIdentifier{{1, 2, 3}},
2976+
DNSNames: []string{"example.com", "example.org"},
2977+
EmailAddresses: []string{"[email protected]", "[email protected]"},
2978+
IPAddresses: []net.IP{net.IPv4(192, 0, 2, 0), net.IPv6loopback},
2979+
URIs: []*url.URL{urlA, urlB},
2980+
KeyUsage: KeyUsageCertSign,
29772981
}
29782982
out := marshalAndParseCSR(t, in)
29792983

2980-
if in.KeyUsage != out.KeyUsage {
2981-
t.Fatalf("Unexpected KeyUsage: got %v, want %v", out.KeyUsage, in.KeyUsage)
2982-
}
2983-
if !reflect.DeepEqual(in.ExtKeyUsage, out.ExtKeyUsage) {
2984-
t.Fatalf("Unexpected ExtKeyUsage: got %v, want %v", out.ExtKeyUsage, in.ExtKeyUsage)
2985-
}
2986-
if !reflect.DeepEqual(in.UnknownExtKeyUsage, out.UnknownExtKeyUsage) {
2987-
t.Fatalf("Unexpected UnknownExtKeyUsage: got %v, want %v", out.UnknownExtKeyUsage, in.UnknownExtKeyUsage)
2984+
if !reflect.DeepEqual(in.DNSNames, out.DNSNames) {
2985+
t.Fatalf("Unexpected DNSNames: got %v, want %v", out.DNSNames, in.DNSNames)
29882986
}
2989-
if in.BasicConstraintsValid != out.BasicConstraintsValid {
2990-
t.Fatalf("Unexpected BasicConstraintsValid: got %v, want %v", out.BasicConstraintsValid, in.BasicConstraintsValid)
2987+
if !reflect.DeepEqual(in.EmailAddresses, out.EmailAddresses) {
2988+
t.Fatalf("Unexpected EmailAddresses: got %v, want %v", out.EmailAddresses, in.EmailAddresses)
29912989
}
2992-
if in.IsCA != out.IsCA {
2993-
t.Fatalf("Unexpected IsCA: got %v, want %v", out.IsCA, in.IsCA)
2990+
if len(in.IPAddresses) != len(out.IPAddresses) ||
2991+
!in.IPAddresses[0].Equal(out.IPAddresses[0]) ||
2992+
!in.IPAddresses[1].Equal(out.IPAddresses[1]) {
2993+
t.Fatalf("Unexpected IPAddresses: got %v, want %v", out.IPAddresses, in.IPAddresses)
29942994
}
2995-
if in.MaxPathLen != out.MaxPathLen {
2996-
t.Fatalf("Unexpected MaxPathLen: got %v, want %v", out.MaxPathLen, in.MaxPathLen)
2995+
if !reflect.DeepEqual(in.URIs, out.URIs) {
2996+
t.Fatalf("Unexpected URIs: got %v, want %v", out.URIs, in.URIs)
29972997
}
2998-
if in.MaxPathLenZero != out.MaxPathLenZero {
2999-
t.Fatalf("Unexpected MaxPathLenZero: got %v, want %v", out.MaxPathLenZero, in.MaxPathLenZero)
3000-
}
3001-
if !reflect.DeepEqual(in.SubjectKeyId, out.SubjectKeyId) {
3002-
t.Fatalf("Unexpected SubjectKeyId: got %v, want %v", out.SubjectKeyId, in.SubjectKeyId)
3003-
}
3004-
if !reflect.DeepEqual(in.PolicyIdentifiers, out.PolicyIdentifiers) {
3005-
t.Fatalf("Unexpected PolicyIdentifiers: got %v, want %v", out.PolicyIdentifiers, in.PolicyIdentifiers)
2998+
if in.KeyUsage != out.KeyUsage {
2999+
t.Fatalf("Unexpected KeyUsage: got %v, want %v", out.KeyUsage, in.KeyUsage)
30063000
}
30073001
}

0 commit comments

Comments
 (0)