Skip to content

Commit 9d9d507

Browse files
cpugopherbot
authored andcommitted
x509roots/fallback/bundle: fix bundle test with Go 1.27+
In Go 1.27 we've updated crypto/x509/pkix to avoid hex-encoding attribute values that are string-typed. However, in TestBundle() we assert the parsed certificate subject CN matches expected and now the parsed value differs based on Go version. This commit introduces some small helpers that on Go 1.25/1.26 replicate the Go 1.27 behavior, decoding hex-encoded attribute values before making the comparison. In this way the test continues to pass without losing any coverage, or introducing duplicated per-version bundles. In the future when only Go 1.27+ are supported we can revert this extra machinery. Change-Id: I66bf6439e421169c0f9c750f88116b73ec5188fe Reviewed-on: https://go-review.googlesource.com/c/crypto/+/775760 Reviewed-by: Roland Shoemaker <roland@golang.org> LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Daniel McCarney <daniel@binaryparadox.net>
1 parent fd0b90d commit 9d9d507

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

x509roots/fallback/bundle/bundle_test.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ package bundle
77
import (
88
"crypto/sha256"
99
"crypto/x509"
10+
"encoding/asn1"
1011
"encoding/hex"
12+
"regexp"
1113
"testing"
1214
)
1315

@@ -19,7 +21,7 @@ func TestBundle(t *testing.T) {
1921
continue
2022
}
2123

22-
if unparsed.cn != cert.Subject.String() {
24+
if !subjectsEqual(unparsed.cn, cert.Subject.String()) {
2325
t.Errorf("unparsedCertificates[%v].cn = %q; want = %q", i, unparsed.cn, cert.Subject.String())
2426
}
2527

@@ -30,3 +32,34 @@ func TestBundle(t *testing.T) {
3032
}
3133
}
3234
}
35+
36+
// subjectsEqual reports whether two RFC 2253 DN strings match.
37+
//
38+
// It tolerates the rendering difference introduced in Go 1.27, where
39+
// string-typed attribute values for OIDs outside attributeTypeNames are
40+
// rendered as strings rather than hex-encoded DER (see Go CL 773800).
41+
//
42+
// This can be removed when Go 1.25/1.26 are no longer supported.
43+
func subjectsEqual(a, b string) bool {
44+
return a == b || normalizeHexValues(a) == normalizeHexValues(b)
45+
}
46+
47+
// normalizeHexValues rewrites any "oid=#hex" to the equivalent "oid=value"
48+
// rendering produced by Go 1.27+.
49+
func normalizeHexValues(s string) string {
50+
return hexAttrRE.ReplaceAllStringFunc(s, func(match string) string {
51+
m := hexAttrRE.FindStringSubmatch(match)
52+
der, err := hex.DecodeString(m[2])
53+
if err != nil {
54+
return match
55+
}
56+
var v string
57+
if rest, err := asn1.Unmarshal(der, &v); err != nil || len(rest) != 0 {
58+
return match
59+
}
60+
return m[1] + "=" + v
61+
})
62+
}
63+
64+
// hexAttrRE matches a "oid=#hex" attribute value in an RFC 2253 DN string.
65+
var hexAttrRE = regexp.MustCompile(`([\d.]+)=#([[:xdigit:]]+)`)

0 commit comments

Comments
 (0)