2016-05-04 20:53:48 +02:00
|
|
|
// Copyright (C) 2016 Opsmate, Inc.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
// Public License, v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
//
|
|
|
|
// This software is distributed WITHOUT A WARRANTY OF ANY KIND.
|
|
|
|
// See the Mozilla Public License for details.
|
|
|
|
|
2016-05-04 20:49:07 +02:00
|
|
|
package certspotter
|
2016-03-17 00:58:00 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/asn1"
|
2016-07-28 20:55:46 +02:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2016-04-24 18:11:28 +02:00
|
|
|
"unicode/utf8"
|
2016-03-17 00:58:00 +01:00
|
|
|
)
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func stringFromByteSlice(chars []byte) string {
|
2016-03-17 00:58:00 +01:00
|
|
|
runes := make([]rune, len(chars))
|
|
|
|
for i, ch := range chars {
|
|
|
|
runes[i] = rune(ch)
|
|
|
|
}
|
|
|
|
return string(runes)
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func stringFromUint16Slice(chars []uint16) string {
|
2016-03-17 00:58:00 +01:00
|
|
|
runes := make([]rune, len(chars))
|
|
|
|
for i, ch := range chars {
|
|
|
|
runes[i] = rune(ch)
|
|
|
|
}
|
|
|
|
return string(runes)
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func stringFromUint32Slice(chars []uint32) string {
|
2016-03-17 00:58:00 +01:00
|
|
|
runes := make([]rune, len(chars))
|
|
|
|
for i, ch := range chars {
|
|
|
|
runes[i] = rune(ch)
|
|
|
|
}
|
|
|
|
return string(runes)
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func decodeASN1String(value *asn1.RawValue) (string, error) {
|
2016-03-17 00:58:00 +01:00
|
|
|
if !value.IsCompound && value.Class == 0 {
|
|
|
|
if value.Tag == 12 {
|
|
|
|
// UTF8String
|
2016-04-24 18:11:28 +02:00
|
|
|
if !utf8.Valid(value.Bytes) {
|
|
|
|
return "", errors.New("Malformed UTF8String")
|
|
|
|
}
|
2016-03-17 00:58:00 +01:00
|
|
|
return string(value.Bytes), nil
|
|
|
|
} else if value.Tag == 19 || value.Tag == 22 || value.Tag == 20 {
|
|
|
|
// * PrintableString - subset of ASCII
|
|
|
|
// * IA5String - ASCII
|
|
|
|
// * TeletexString - 8 bit charset; not quite ISO-8859-1, but often treated as such
|
|
|
|
|
|
|
|
// Don't enforce character set rules. Allow any 8 bit character, since
|
|
|
|
// CAs routinely mess this up
|
|
|
|
return stringFromByteSlice(value.Bytes), nil
|
|
|
|
} else if value.Tag == 30 {
|
|
|
|
// BMPString - Unicode, encoded in big-endian format using two octets
|
2016-07-28 20:55:46 +02:00
|
|
|
runes := make([]uint16, len(value.Bytes)/2)
|
2016-03-17 00:58:00 +01:00
|
|
|
if err := binary.Read(bytes.NewReader(value.Bytes), binary.BigEndian, runes); err != nil {
|
|
|
|
return "", errors.New("Malformed BMPString: " + err.Error())
|
|
|
|
}
|
|
|
|
return stringFromUint16Slice(runes), nil
|
|
|
|
} else if value.Tag == 28 {
|
|
|
|
// UniversalString - Unicode, encoded in big-endian format using four octets
|
2016-07-28 20:55:46 +02:00
|
|
|
runes := make([]uint32, len(value.Bytes)/4)
|
2016-03-17 00:58:00 +01:00
|
|
|
if err := binary.Read(bytes.NewReader(value.Bytes), binary.BigEndian, runes); err != nil {
|
|
|
|
return "", errors.New("Malformed UniversalString: " + err.Error())
|
|
|
|
}
|
|
|
|
return stringFromUint32Slice(runes), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", errors.New("Not a string")
|
|
|
|
}
|