Skip to content

Commit d47c9bc

Browse files
hirochachachaagl
authored andcommitted
encoding/asn1: handle application tag in Marshal
Fixes #20488 Change-Id: Iae963b612aea3d9e814b08f655e2eb019ece256e Reviewed-on: https://go-review.googlesource.com/44110 Reviewed-by: Adam Langley <[email protected]> Run-TryBot: Adam Langley <[email protected]>
1 parent 4a5f85b commit d47c9bc

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

src/encoding/asn1/marshal.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,6 @@ func makeField(v reflect.Value, params fieldParameters) (e encoder, err error) {
560560
if !ok {
561561
return nil, StructuralError{fmt.Sprintf("unknown Go type: %v", v.Type())}
562562
}
563-
class := ClassUniversal
564563

565564
if params.timeType != 0 && tag != TagUTCTime {
566565
return nil, StructuralError{"explicit time type given to non-time member"}
@@ -610,27 +609,33 @@ func makeField(v reflect.Value, params fieldParameters) (e encoder, err error) {
610609

611610
bodyLen := t.body.Len()
612611

613-
if params.explicit {
614-
t.tag = bytesEncoder(appendTagAndLength(t.scratch[:0], tagAndLength{class, tag, bodyLen, isCompound}))
612+
class := ClassUniversal
613+
if params.tag != nil {
614+
if params.application {
615+
class = ClassApplication
616+
} else {
617+
class = ClassContextSpecific
618+
}
615619

616-
tt := new(taggedEncoder)
620+
if params.explicit {
621+
t.tag = bytesEncoder(appendTagAndLength(t.scratch[:0], tagAndLength{ClassUniversal, tag, bodyLen, isCompound}))
617622

618-
tt.body = t
623+
tt := new(taggedEncoder)
619624

620-
tt.tag = bytesEncoder(appendTagAndLength(tt.scratch[:0], tagAndLength{
621-
class: ClassContextSpecific,
622-
tag: *params.tag,
623-
length: bodyLen + t.tag.Len(),
624-
isCompound: true,
625-
}))
625+
tt.body = t
626626

627-
return tt, nil
628-
}
627+
tt.tag = bytesEncoder(appendTagAndLength(tt.scratch[:0], tagAndLength{
628+
class: class,
629+
tag: *params.tag,
630+
length: bodyLen + t.tag.Len(),
631+
isCompound: true,
632+
}))
633+
634+
return tt, nil
635+
}
629636

630-
if params.tag != nil {
631637
// implicit tag.
632638
tag = *params.tag
633-
class = ClassContextSpecific
634639
}
635640

636641
t.tag = bytesEncoder(appendTagAndLength(t.scratch[:0], tagAndLength{class, tag, bodyLen, isCompound}))

src/encoding/asn1/marshal_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ type defaultTest struct {
7171
A int `asn1:"optional,default:1"`
7272
}
7373

74+
type applicationTest struct {
75+
A int `asn1:"application,tag:0"`
76+
B int `asn1:"application,tag:1,explicit"`
77+
}
78+
7479
type testSET []int
7580

7681
var PST = time.FixedZone("PST", -8*60*60)
@@ -152,6 +157,7 @@ var marshalTests = []marshalTest{
152157
{defaultTest{0}, "3003020100"},
153158
{defaultTest{1}, "3000"},
154159
{defaultTest{2}, "3003020102"},
160+
{applicationTest{1, 2}, "30084001016103020102"},
155161
}
156162

157163
func TestMarshal(t *testing.T) {

0 commit comments

Comments
 (0)