Raise parse error if certain strings are improperly encoded
If a UTF8String in the Subject CN isn't valid UTF-8, or if a DNS SAN is not ASCII, raise a parse error, since we don't know how to interpret the string.
This commit is contained in:
parent
2d2aa37202
commit
2426817cd5
4
asn1.go
4
asn1.go
|
@ -5,6 +5,7 @@ import (
|
|||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/asn1"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func stringFromByteSlice (chars []byte) string {
|
||||
|
@ -35,6 +36,9 @@ func decodeASN1String (value *asn1.RawValue) (string, error) {
|
|||
if !value.IsCompound && value.Class == 0 {
|
||||
if value.Tag == 12 {
|
||||
// UTF8String
|
||||
if !utf8.Valid(value.Bytes) {
|
||||
return "", errors.New("Malformed UTF8String")
|
||||
}
|
||||
return string(value.Bytes), nil
|
||||
} else if value.Tag == 19 || value.Tag == 22 || value.Tag == 20 {
|
||||
// * PrintableString - subset of ASCII
|
||||
|
|
12
x509.go
12
x509.go
|
@ -280,6 +280,15 @@ func (cert *Certificate) ParseTBSCertificate () (*TBSCertificate, error) {
|
|||
}
|
||||
|
||||
|
||||
func isAscii (bytes []byte) bool {
|
||||
for _, b := range bytes {
|
||||
if b > 127 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func parseSANExtension (value []byte) ([]string, error) {
|
||||
var dnsNames []string
|
||||
var seq asn1.RawValue
|
||||
|
@ -306,6 +315,9 @@ func parseSANExtension (value []byte) ([]string, error) {
|
|||
}
|
||||
switch val.Tag {
|
||||
case 2:
|
||||
if !isAscii(val.Bytes) {
|
||||
return nil, errors.New("failed to parse subjectAltName: DNS name contains non-ASCII characters")
|
||||
}
|
||||
dnsNames = append(dnsNames, string(val.Bytes))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue