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:
Andrew Ayer 2016-04-24 09:11:28 -07:00
parent 2d2aa37202
commit 2426817cd5
2 changed files with 16 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"encoding/asn1" "encoding/asn1"
"unicode/utf8"
) )
func stringFromByteSlice (chars []byte) string { func stringFromByteSlice (chars []byte) string {
@ -35,6 +36,9 @@ func decodeASN1String (value *asn1.RawValue) (string, error) {
if !value.IsCompound && value.Class == 0 { if !value.IsCompound && value.Class == 0 {
if value.Tag == 12 { if value.Tag == 12 {
// UTF8String // UTF8String
if !utf8.Valid(value.Bytes) {
return "", errors.New("Malformed UTF8String")
}
return string(value.Bytes), nil return string(value.Bytes), nil
} else if value.Tag == 19 || value.Tag == 22 || value.Tag == 20 { } else if value.Tag == 19 || value.Tag == 22 || value.Tag == 20 {
// * PrintableString - subset of ASCII // * PrintableString - subset of ASCII

12
x509.go
View File

@ -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) { func parseSANExtension (value []byte) ([]string, error) {
var dnsNames []string var dnsNames []string
var seq asn1.RawValue var seq asn1.RawValue
@ -306,6 +315,9 @@ func parseSANExtension (value []byte) ([]string, error) {
} }
switch val.Tag { switch val.Tag {
case 2: 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)) dnsNames = append(dnsNames, string(val.Bytes))
} }
} }