Skip to content

Commit 1b89cd1

Browse files
committed
encoding/asn1: fix unmarshaling of implicitly tagged UTF-8 strings.
Fixes #8541. LGTM=rsc R=rsc CC=golang-codereviews https://golang.org/cl/153770043
1 parent 47094dc commit 1b89cd1

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/encoding/asn1/asn1.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,15 +640,19 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
640640
// when it sees a string, so if we see a different string type on the
641641
// wire, we change the universal type to match.
642642
if universalTag == tagPrintableString {
643-
switch t.tag {
644-
case tagIA5String, tagGeneralString, tagT61String, tagUTF8String:
645-
universalTag = t.tag
643+
if params.tag == nil {
644+
switch t.tag {
645+
case tagIA5String, tagGeneralString, tagT61String, tagUTF8String:
646+
universalTag = t.tag
647+
}
648+
} else if params.stringType != 0 {
649+
universalTag = params.stringType
646650
}
647651
}
648652

649653
// Special case for time: UTCTime and GeneralizedTime both map to the
650654
// Go type time.Time.
651-
if universalTag == tagUTCTime && t.tag == tagGeneralizedTime {
655+
if universalTag == tagUTCTime && params.tag == nil && t.tag == tagGeneralizedTime {
652656
universalTag = tagGeneralizedTime
653657
}
654658

src/encoding/asn1/asn1_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,10 @@ type TestContextSpecificTags2 struct {
392392
B int
393393
}
394394

395+
type TestContextSpecificTags3 struct {
396+
S string `asn1:"tag:1,utf8"`
397+
}
398+
395399
type TestElementsAfterString struct {
396400
S string
397401
A, B int
@@ -420,6 +424,7 @@ var unmarshalTestData = []struct {
420424
{[]byte{0x04, 0x04, 1, 2, 3, 4}, &RawValue{0, 4, false, []byte{1, 2, 3, 4}, []byte{4, 4, 1, 2, 3, 4}}},
421425
{[]byte{0x30, 0x03, 0x81, 0x01, 0x01}, &TestContextSpecificTags{1}},
422426
{[]byte{0x30, 0x08, 0xa1, 0x03, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02}, &TestContextSpecificTags2{1, 2}},
427+
{[]byte{0x30, 0x03, 0x81, 0x01, '@'}, &TestContextSpecificTags3{"@"}},
423428
{[]byte{0x01, 0x01, 0x00}, newBool(false)},
424429
{[]byte{0x01, 0x01, 0xff}, newBool(true)},
425430
{[]byte{0x30, 0x0b, 0x13, 0x03, 0x66, 0x6f, 0x6f, 0x02, 0x01, 0x22, 0x02, 0x01, 0x33}, &TestElementsAfterString{"foo", 0x22, 0x33}},

0 commit comments

Comments
 (0)