aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/asn1
diff options
context:
space:
mode:
authorHiroshi Ioka <hirochachacha@gmail.com>2017-05-25 07:48:08 +0900
committerAdam Langley <agl@golang.org>2017-08-15 18:45:39 +0000
commitd47c9bce812dbbb2d2dde04054875ed5402c10e3 (patch)
tree456f043f482cb2d7e303f3b83251bc3525467bed /src/encoding/asn1
parent4a5f85babbe5ca78ad38cb32b1b1f0259c8c1cef (diff)
downloadgo-d47c9bce812dbbb2d2dde04054875ed5402c10e3.tar.gz
go-d47c9bce812dbbb2d2dde04054875ed5402c10e3.zip
encoding/asn1: handle application tag in Marshal
Fixes #20488 Change-Id: Iae963b612aea3d9e814b08f655e2eb019ece256e Reviewed-on: https://go-review.googlesource.com/44110 Reviewed-by: Adam Langley <agl@golang.org> Run-TryBot: Adam Langley <agl@golang.org>
Diffstat (limited to 'src/encoding/asn1')
-rw-r--r--src/encoding/asn1/marshal.go35
-rw-r--r--src/encoding/asn1/marshal_test.go6
2 files changed, 26 insertions, 15 deletions
diff --git a/src/encoding/asn1/marshal.go b/src/encoding/asn1/marshal.go
index fdadb3996e..bbd3ee7308 100644
--- a/src/encoding/asn1/marshal.go
+++ b/src/encoding/asn1/marshal.go
@@ -560,7 +560,6 @@ func makeField(v reflect.Value, params fieldParameters) (e encoder, err error) {
if !ok {
return nil, StructuralError{fmt.Sprintf("unknown Go type: %v", v.Type())}
}
- class := ClassUniversal
if params.timeType != 0 && tag != TagUTCTime {
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) {
bodyLen := t.body.Len()
- if params.explicit {
- t.tag = bytesEncoder(appendTagAndLength(t.scratch[:0], tagAndLength{class, tag, bodyLen, isCompound}))
+ class := ClassUniversal
+ if params.tag != nil {
+ if params.application {
+ class = ClassApplication
+ } else {
+ class = ClassContextSpecific
+ }
- tt := new(taggedEncoder)
+ if params.explicit {
+ t.tag = bytesEncoder(appendTagAndLength(t.scratch[:0], tagAndLength{ClassUniversal, tag, bodyLen, isCompound}))
- tt.body = t
+ tt := new(taggedEncoder)
- tt.tag = bytesEncoder(appendTagAndLength(tt.scratch[:0], tagAndLength{
- class: ClassContextSpecific,
- tag: *params.tag,
- length: bodyLen + t.tag.Len(),
- isCompound: true,
- }))
+ tt.body = t
- return tt, nil
- }
+ tt.tag = bytesEncoder(appendTagAndLength(tt.scratch[:0], tagAndLength{
+ class: class,
+ tag: *params.tag,
+ length: bodyLen + t.tag.Len(),
+ isCompound: true,
+ }))
+
+ return tt, nil
+ }
- if params.tag != nil {
// implicit tag.
tag = *params.tag
- class = ClassContextSpecific
}
t.tag = bytesEncoder(appendTagAndLength(t.scratch[:0], tagAndLength{class, tag, bodyLen, isCompound}))
diff --git a/src/encoding/asn1/marshal_test.go b/src/encoding/asn1/marshal_test.go
index 10db1aa575..87d358d64c 100644
--- a/src/encoding/asn1/marshal_test.go
+++ b/src/encoding/asn1/marshal_test.go
@@ -71,6 +71,11 @@ type defaultTest struct {
A int `asn1:"optional,default:1"`
}
+type applicationTest struct {
+ A int `asn1:"application,tag:0"`
+ B int `asn1:"application,tag:1,explicit"`
+}
+
type testSET []int
var PST = time.FixedZone("PST", -8*60*60)
@@ -152,6 +157,7 @@ var marshalTests = []marshalTest{
{defaultTest{0}, "3003020100"},
{defaultTest{1}, "3000"},
{defaultTest{2}, "3003020102"},
+ {applicationTest{1, 2}, "30084001016103020102"},
}
func TestMarshal(t *testing.T) {