1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 type readASN1Test struct {
24 var readASN1TestData = []readASN1Test{
25 {"valid", []byte{0x30, 2, 1, 2}, 0x30, true, []byte{1, 2}},
26 {"truncated", []byte{0x30, 3, 1, 2}, 0x30, false, nil},
27 {"zero length of length", []byte{0x30, 0x80}, 0x30, false, nil},
28 {"invalid long form length", []byte{0x30, 0x81, 1, 1}, 0x30, false, nil},
29 {"non-minimal length", append([]byte{0x30, 0x82, 0, 0x80}, make([]byte, 0x80)...), 0x30, false, nil},
30 {"invalid tag", []byte{0xa1, 3, 0x4, 1, 1}, 31, false, nil},
31 {"high tag", []byte{0x1f, 0x81, 0x80, 0x01, 2, 1, 2}, 0xff /* actually 0x4001, but tag is uint8 */, false, nil},
34 func TestReadASN1(t *testing.T) {
35 for _, test := range readASN1TestData {
36 t.Run(test.name, func(t *testing.T) {
37 var in, out String = test.in, nil
38 ok := in.ReadASN1(&out, test.tag)
39 if ok != test.ok || ok && !bytes.Equal(out, test.out.([]byte)) {
40 t.Errorf("in.ReadASN1() = %v, want %v; out = %v, want %v", ok, test.ok, out, test.out)
46 func TestReadASN1Optional(t *testing.T) {
49 ok := empty.ReadOptionalASN1(nil, &present, 0xa0)
51 t.Errorf("empty.ReadOptionalASN1() = %v, want true; present = %v want false", ok, present)
54 var in, out String = []byte{0xa1, 3, 0x4, 1, 1}, nil
55 ok = in.ReadOptionalASN1(&out, &present, 0xa0)
57 t.Errorf("in.ReadOptionalASN1() = %v, want true, present = %v, want false", ok, present)
59 ok = in.ReadOptionalASN1(&out, &present, 0xa1)
60 wantBytes := []byte{4, 1, 1}
61 if !ok || !present || !bytes.Equal(out, wantBytes) {
62 t.Errorf("in.ReadOptionalASN1() = %v, want true; present = %v, want true; out = %v, want = %v", ok, present, out, wantBytes)
66 var optionalOctetStringTestData = []struct {
70 {readASN1Test{"empty", []byte{}, 0xa0, true, []byte{}}, false},
71 {readASN1Test{"invalid", []byte{0xa1, 3, 0x4, 2, 1}, 0xa1, false, []byte{}}, true},
72 {readASN1Test{"missing", []byte{0xa1, 3, 0x4, 1, 1}, 0xa0, true, []byte{}}, false},
73 {readASN1Test{"present", []byte{0xa1, 3, 0x4, 1, 1}, 0xa1, true, []byte{1}}, true},
76 func TestReadASN1OptionalOctetString(t *testing.T) {
77 for _, test := range optionalOctetStringTestData {
78 t.Run(test.name, func(t *testing.T) {
82 ok := in.ReadOptionalASN1OctetString(&out, &present, test.tag)
83 if ok != test.ok || present != test.present || !bytes.Equal(out, test.out.([]byte)) {
84 t.Errorf("in.ReadOptionalASN1OctetString() = %v, want %v; present = %v want %v; out = %v, want %v", ok, test.ok, present, test.present, out, test.out)
92 var optionalIntTestData = []readASN1Test{
93 {"empty", []byte{}, 0xa0, true, defaultInt},
94 {"invalid", []byte{0xa1, 3, 0x2, 2, 127}, 0xa1, false, 0},
95 {"missing", []byte{0xa1, 3, 0x2, 1, 127}, 0xa0, true, defaultInt},
96 {"present", []byte{0xa1, 3, 0x2, 1, 42}, 0xa1, true, 42},
99 func TestReadASN1OptionalInteger(t *testing.T) {
100 for _, test := range optionalIntTestData {
101 t.Run(test.name, func(t *testing.T) {
102 in := String(test.in)
104 ok := in.ReadOptionalASN1Integer(&out, test.tag, defaultInt)
105 if ok != test.ok || ok && out != test.out.(int) {
106 t.Errorf("in.ReadOptionalASN1Integer() = %v, want %v; out = %v, want %v", ok, test.ok, out, test.out)
112 func TestReadASN1IntegerSigned(t *testing.T) {
113 testData64 := []struct {
117 {[]byte{2, 3, 128, 0, 0}, -0x800000},
118 {[]byte{2, 2, 255, 0}, -256},
119 {[]byte{2, 2, 255, 127}, -129},
120 {[]byte{2, 1, 128}, -128},
121 {[]byte{2, 1, 255}, -1},
122 {[]byte{2, 1, 0}, 0},
123 {[]byte{2, 1, 1}, 1},
124 {[]byte{2, 1, 2}, 2},
125 {[]byte{2, 1, 127}, 127},
126 {[]byte{2, 2, 0, 128}, 128},
127 {[]byte{2, 2, 1, 0}, 256},
128 {[]byte{2, 4, 0, 128, 0, 0}, 0x800000},
130 for i, test := range testData64 {
131 in := String(test.in)
133 ok := in.ReadASN1Integer(&out)
134 if !ok || out != test.out {
135 t.Errorf("#%d: in.ReadASN1Integer() = %v, want true; out = %d, want %d", i, ok, out, test.out)
139 // Repeat the same cases, reading into a big.Int.
140 t.Run("big.Int", func(t *testing.T) {
141 for i, test := range testData64 {
142 in := String(test.in)
144 ok := in.ReadASN1Integer(&out)
145 if !ok || out.Int64() != test.out {
146 t.Errorf("#%d: in.ReadASN1Integer() = %v, want true; out = %d, want %d", i, ok, out.Int64(), test.out)
152 func TestReadASN1IntegerUnsigned(t *testing.T) {
153 testData := []struct {
157 {[]byte{2, 1, 0}, 0},
158 {[]byte{2, 1, 1}, 1},
159 {[]byte{2, 1, 2}, 2},
160 {[]byte{2, 1, 127}, 127},
161 {[]byte{2, 2, 0, 128}, 128},
162 {[]byte{2, 2, 1, 0}, 256},
163 {[]byte{2, 4, 0, 128, 0, 0}, 0x800000},
164 {[]byte{2, 8, 127, 255, 255, 255, 255, 255, 255, 255}, 0x7fffffffffffffff},
165 {[]byte{2, 9, 0, 128, 0, 0, 0, 0, 0, 0, 0}, 0x8000000000000000},
166 {[]byte{2, 9, 0, 255, 255, 255, 255, 255, 255, 255, 255}, 0xffffffffffffffff},
168 for i, test := range testData {
169 in := String(test.in)
171 ok := in.ReadASN1Integer(&out)
172 if !ok || out != test.out {
173 t.Errorf("#%d: in.ReadASN1Integer() = %v, want true; out = %d, want %d", i, ok, out, test.out)
178 func TestReadASN1IntegerInvalid(t *testing.T) {
179 testData := []String{
180 []byte{3, 1, 0}, // invalid tag
184 // not minimally encoded
186 []byte{2, 2, 0xff, 0xff},
189 for i, test := range testData {
191 if test.ReadASN1Integer(&out) {
192 t.Errorf("#%d: in.ReadASN1Integer() = true, want false (out = %d)", i, out)
197 func TestReadASN1ObjectIdentifier(t *testing.T) {
198 testData := []struct {
203 {[]byte{}, false, []int{}},
204 {[]byte{6, 0}, false, []int{}},
205 {[]byte{5, 1, 85}, false, []int{2, 5}},
206 {[]byte{6, 1, 85}, true, []int{2, 5}},
207 {[]byte{6, 2, 85, 0x02}, true, []int{2, 5, 2}},
208 {[]byte{6, 4, 85, 0x02, 0xc0, 0x00}, true, []int{2, 5, 2, 0x2000}},
209 {[]byte{6, 3, 0x81, 0x34, 0x03}, true, []int{2, 100, 3}},
210 {[]byte{6, 7, 85, 0x02, 0xc0, 0x80, 0x80, 0x80, 0x80}, false, []int{}},
213 for i, test := range testData {
214 in := String(test.in)
215 var out asn1.ObjectIdentifier
216 ok := in.ReadASN1ObjectIdentifier(&out)
217 if ok != test.ok || ok && !out.Equal(test.out) {
218 t.Errorf("#%d: in.ReadASN1ObjectIdentifier() = %v, want %v; out = %v, want %v", i, ok, test.ok, out, test.out)
223 func TestReadASN1GeneralizedTime(t *testing.T) {
224 testData := []struct {
229 {"20100102030405Z", true, time.Date(2010, 01, 02, 03, 04, 05, 0, time.UTC)},
230 {"20100102030405", false, time.Time{}},
231 {"20100102030405+0607", true, time.Date(2010, 01, 02, 03, 04, 05, 0, time.FixedZone("", 6*60*60+7*60))},
232 {"20100102030405-0607", true, time.Date(2010, 01, 02, 03, 04, 05, 0, time.FixedZone("", -6*60*60-7*60))},
233 /* These are invalid times. However, the time package normalises times
234 * and they were accepted in some versions. See #11134. */
235 {"00000100000000Z", false, time.Time{}},
236 {"20101302030405Z", false, time.Time{}},
237 {"20100002030405Z", false, time.Time{}},
238 {"20100100030405Z", false, time.Time{}},
239 {"20100132030405Z", false, time.Time{}},
240 {"20100231030405Z", false, time.Time{}},
241 {"20100102240405Z", false, time.Time{}},
242 {"20100102036005Z", false, time.Time{}},
243 {"20100102030460Z", false, time.Time{}},
244 {"-20100102030410Z", false, time.Time{}},
245 {"2010-0102030410Z", false, time.Time{}},
246 {"2010-0002030410Z", false, time.Time{}},
247 {"201001-02030410Z", false, time.Time{}},
248 {"20100102-030410Z", false, time.Time{}},
249 {"2010010203-0410Z", false, time.Time{}},
250 {"201001020304-10Z", false, time.Time{}},
252 for i, test := range testData {
253 in := String(append([]byte{asn1.TagGeneralizedTime, byte(len(test.in))}, test.in...))
255 ok := in.ReadASN1GeneralizedTime(&out)
256 if ok != test.ok || ok && !reflect.DeepEqual(out, test.out) {
257 t.Errorf("#%d: in.ReadASN1GeneralizedTime() = %v, want %v; out = %q, want %q", i, ok, test.ok, out, test.out)
262 func TestReadASN1BitString(t *testing.T) {
263 testData := []struct {
268 {[]byte{}, false, asn1.BitString{}},
269 {[]byte{0x00}, true, asn1.BitString{}},
270 {[]byte{0x07, 0x00}, true, asn1.BitString{Bytes: []byte{0}, BitLength: 1}},
271 {[]byte{0x07, 0x01}, false, asn1.BitString{}},
272 {[]byte{0x07, 0x40}, false, asn1.BitString{}},
273 {[]byte{0x08, 0x00}, false, asn1.BitString{}},
274 {[]byte{0xff}, false, asn1.BitString{}},
275 {[]byte{0xfe, 0x00}, false, asn1.BitString{}},
277 for i, test := range testData {
278 in := String(append([]byte{3, byte(len(test.in))}, test.in...))
279 var out asn1.BitString
280 ok := in.ReadASN1BitString(&out)
281 if ok != test.ok || ok && (!bytes.Equal(out.Bytes, test.out.Bytes) || out.BitLength != test.out.BitLength) {
282 t.Errorf("#%d: in.ReadASN1BitString() = %v, want %v; out = %v, want %v", i, ok, test.ok, out, test.out)