Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/crypto/x509/name_constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,22 @@ var nameConstraintsTests = []nameConstraintsTest{
sans: []string{"dns:*.example.com"},
},
},
// #90: a two-label excluded constraint doesn't exclude unrelated wildcards
{
roots: []constraintsSpec{
{
bad: []string{"dns:blocked.example"},
},
},
intermediates: [][]constraintsSpec{
{
{},
},
},
leaf: leafSpec{
sans: []string{"dns:*.service.example"},
},
},
}

func makeConstraintsCACert(constraints constraintsSpec, name string, key *ecdsa.PrivateKey, parent *Certificate, parentKey *ecdsa.PrivateKey) (*Certificate, error) {
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/x509/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ func matchDomainConstraint(domain, constraint string, excluded bool, reversedDom
return false, nil
}

if excluded && wildcardDomain && len(domainLabels) > 1 && len(constraintLabels) > 1 {
if excluded && wildcardDomain && len(domainLabels) > 1 && len(constraintLabels) > 2 {
domainLabels = domainLabels[:len(domainLabels)-1]
constraintLabels = constraintLabels[:len(constraintLabels)-1]
}
Expand Down
46 changes: 46 additions & 0 deletions src/crypto/x509/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,52 @@ func TestNameConstraints(t *testing.T) {
}
}

func TestExcludedWildcardNameConstraints(t *testing.T) {
tests := []struct {
name string
domain string
constraint string
shouldMatch bool
}{
{
name: "excluded subdomain matches wildcard parent",
domain: "*.example.com",
constraint: "foo.example.com",
shouldMatch: true,
},
{
name: "excluded parent domain matches wildcard",
domain: "*.example.com",
constraint: "example.com",
shouldMatch: true,
},
{
name: "excluded unrelated two-label domain does not match wildcard",
domain: "*.service.example",
constraint: "blocked.example",
shouldMatch: false,
},
{
name: "excluded single-label domain does not match wildcard",
domain: "*.service.example",
constraint: "internal",
shouldMatch: false,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
match, err := matchDomainConstraint(tc.domain, tc.constraint, true, map[string][]string{}, map[string][]string{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if match != tc.shouldMatch {
t.Fatalf("matchDomainConstraint(%q, %q, excluded=true) = %v, want %v", tc.domain, tc.constraint, match, tc.shouldMatch)
}
})
}
}

const selfSignedWithCommonName = `-----BEGIN CERTIFICATE-----
MIIDCjCCAfKgAwIBAgIBADANBgkqhkiG9w0BAQsFADAaMQswCQYDVQQKEwJjYTEL
MAkGA1UEAxMCY2EwHhcNMTYwODI4MTcwOTE4WhcNMjEwODI3MTcwOTE4WjAcMQsw
Expand Down