-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release-branch.go1.10] crypto/x509: parse invalid DNS names and emai…
…l addresses. Go 1.10 requires that SANs in certificates are valid. However, a non-trivial number of (generally non-WebPKI) certificates have invalid strings in dnsName fields and some have even put those dnsName SANs in CA certificates. This change defers validity checking until name constraints are checked. Fixes #23995, #23711. Change-Id: I2e0ebb0898c047874a3547226b71e3029333b7f1 Reviewed-on: https://go-review.googlesource.com/96378 Run-TryBot: Adam Langley <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-on: https://go-review.googlesource.com/102783 Run-TryBot: Andrew Bonventre <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]>
- Loading branch information
Showing
3 changed files
with
82 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import ( | |
"crypto/rand" | ||
"crypto/x509/pkix" | ||
"encoding/asn1" | ||
"encoding/hex" | ||
"encoding/pem" | ||
"fmt" | ||
"io/ioutil" | ||
|
@@ -1482,6 +1483,64 @@ var nameConstraintsTests = []nameConstraintsTest{ | |
}, | ||
requestedEKUs: []ExtKeyUsage{ExtKeyUsageServerAuth}, | ||
}, | ||
|
||
// An invalid DNS SAN should be detected only at validation time so | ||
// that we can process CA certificates in the wild that have invalid SANs. | ||
// See https://github.com/golang/go/issues/23995 | ||
|
||
// #77: an invalid DNS or mail SAN will not be detected if name constaint | ||
// checking is not triggered. | ||
nameConstraintsTest{ | ||
roots: []constraintsSpec{ | ||
constraintsSpec{}, | ||
}, | ||
intermediates: [][]constraintsSpec{ | ||
[]constraintsSpec{ | ||
constraintsSpec{}, | ||
}, | ||
}, | ||
leaf: leafSpec{ | ||
sans: []string{"dns:this is invalid", "email:this @ is invalid"}, | ||
}, | ||
}, | ||
|
||
// #78: an invalid DNS SAN will be detected if any name constraint checking | ||
// is triggered. | ||
nameConstraintsTest{ | ||
roots: []constraintsSpec{ | ||
constraintsSpec{ | ||
bad: []string{"uri:"}, | ||
}, | ||
}, | ||
intermediates: [][]constraintsSpec{ | ||
[]constraintsSpec{ | ||
constraintsSpec{}, | ||
}, | ||
}, | ||
leaf: leafSpec{ | ||
sans: []string{"dns:this is invalid"}, | ||
}, | ||
expectedError: "cannot parse dnsName", | ||
}, | ||
|
||
// #79: an invalid email SAN will be detected if any name constraint | ||
// checking is triggered. | ||
nameConstraintsTest{ | ||
roots: []constraintsSpec{ | ||
constraintsSpec{ | ||
bad: []string{"uri:"}, | ||
}, | ||
}, | ||
intermediates: [][]constraintsSpec{ | ||
[]constraintsSpec{ | ||
constraintsSpec{}, | ||
}, | ||
}, | ||
leaf: leafSpec{ | ||
sans: []string{"email:this @ is invalid"}, | ||
}, | ||
expectedError: "cannot parse rfc822Name", | ||
}, | ||
} | ||
|
||
func makeConstraintsCACert(constraints constraintsSpec, name string, key *ecdsa.PrivateKey, parent *Certificate, parentKey *ecdsa.PrivateKey) (*Certificate, error) { | ||
|
@@ -1550,6 +1609,13 @@ func makeConstraintsLeafCert(leaf leafSpec, key *ecdsa.PrivateKey, parent *Certi | |
} | ||
template.IPAddresses = append(template.IPAddresses, ip) | ||
|
||
case strings.HasPrefix(name, "invalidip:"): | ||
ipBytes, err := hex.DecodeString(name[10:]) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot parse invalid IP: %s", err) | ||
} | ||
template.IPAddresses = append(template.IPAddresses, net.IP(ipBytes)) | ||
|
||
case strings.HasPrefix(name, "email:"): | ||
template.EmailAddresses = append(template.EmailAddresses, name[6:]) | ||
|
||
|
@@ -2011,12 +2077,13 @@ func TestBadNamesInConstraints(t *testing.T) { | |
} | ||
|
||
func TestBadNamesInSANs(t *testing.T) { | ||
// Bad names in SANs should not parse. | ||
// Bad names in URI and IP SANs should not parse. Bad DNS and email SANs | ||
// will parse and are tested in name constraint tests at the top of this | ||
// file. | ||
badNames := []string{ | ||
"dns:foo.com.", | ||
"email:[email protected].", | ||
"email:foo.com.", | ||
"uri:https://example.com./dsf", | ||
"invalidip:0102", | ||
"invalidip:0102030405", | ||
} | ||
|
||
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters