Skip to content

Commit 375031d

Browse files
rolandshoemakergopherbot
authored andcommitted
crypto/x509: don't match bare wildcard
When verifying the name "test", a SAN with a bare wildcard ("*") should not constitute a match. Updates #65085 Change-Id: I02151761e2f29f3e358708a3f723af32b0d79288 Reviewed-on: https://go-review.googlesource.com/c/go/+/585076 Reviewed-by: Filippo Valsorda <[email protected]> Reviewed-by: Damien Neil <[email protected]> Auto-Submit: Roland Shoemaker <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent cf06b1f commit 375031d

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/crypto/x509/verify.go

+5
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,11 @@ func validHostname(host string, isPattern bool) bool {
984984
if len(host) == 0 {
985985
return false
986986
}
987+
if host == "*" {
988+
// Bare wildcards are not allowed, they are not valid DNS names,
989+
// nor are they allowed per RFC 6125.
990+
return false
991+
}
987992

988993
for i, part := range strings.Split(host, ".") {
989994
if part == "" {

src/crypto/x509/verify_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -2811,3 +2811,29 @@ func TestVerifyNilPubKey(t *testing.T) {
28112811
t.Fatalf("buildChains returned unexpected error, got: %v, want %v", err, UnknownAuthorityError{})
28122812
}
28132813
}
2814+
2815+
func TestVerifyBareWildcard(t *testing.T) {
2816+
k, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
2817+
if err != nil {
2818+
t.Fatalf("failed to generate key: %s", err)
2819+
}
2820+
tmpl := &Certificate{
2821+
SerialNumber: big.NewInt(1),
2822+
Subject: pkix.Name{CommonName: "test"},
2823+
NotBefore: time.Now().Add(-time.Hour),
2824+
NotAfter: time.Now().Add(time.Hour),
2825+
DNSNames: []string{"*"},
2826+
}
2827+
cDER, err := CreateCertificate(rand.Reader, tmpl, tmpl, k.Public(), k)
2828+
if err != nil {
2829+
t.Fatalf("failed to create certificate: %s", err)
2830+
}
2831+
c, err := ParseCertificate(cDER)
2832+
if err != nil {
2833+
t.Fatalf("failed to parse certificate: %s", err)
2834+
}
2835+
2836+
if err := c.VerifyHostname("label"); err == nil {
2837+
t.Fatalf("VerifyHostname unexpected success with bare wildcard SAN")
2838+
}
2839+
}

0 commit comments

Comments
 (0)