"reflect"
"strconv"
"time"
+ "unicode/utf8"
)
// A StructuralError suggests that the ASN.1 data is valid, but the Go type
// parseUTF8String parses a ASN.1 UTF8String (raw UTF-8) from the given byte
// array and returns it.
func parseUTF8String(bytes []byte) (ret string, err error) {
+ if !utf8.Valid(bytes) {
+ return "", errors.New("asn1: invalid UTF-8 string")
+ }
return string(bytes), nil
}
"fmt"
"math/big"
"reflect"
+ "strings"
"testing"
"time"
)
t.Error("Unmarshal returned without error")
}
}
+
+type invalidUTF8Test struct {
+ Str string `asn1:"utf8"`
+}
+
+func TestUnmarshalInvalidUTF8(t *testing.T) {
+ data := []byte("0\x05\f\x03a\xc9c")
+ var result invalidUTF8Test
+ _, err := Unmarshal(data, &result)
+
+ const expectedSubstring = "UTF"
+ if err == nil {
+ t.Fatal("Successfully unmarshaled invalid UTF-8 data")
+ } else if !strings.Contains(err.Error(), expectedSubstring) {
+ t.Fatalf("Expected error to mention %q but error was %q", expectedSubstring, err.Error())
+ }
+}