Home | History | Annotate | Download | only in asn1
      1 // Copyright 2009 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.
      4 
      5 package asn1
      6 
      7 import (
      8 	"bytes"
      9 	"fmt"
     10 	"math/big"
     11 	"reflect"
     12 	"strings"
     13 	"testing"
     14 	"time"
     15 )
     16 
     17 type boolTest struct {
     18 	in  []byte
     19 	ok  bool
     20 	out bool
     21 }
     22 
     23 var boolTestData = []boolTest{
     24 	{[]byte{0x00}, true, false},
     25 	{[]byte{0xff}, true, true},
     26 	{[]byte{0x00, 0x00}, false, false},
     27 	{[]byte{0xff, 0xff}, false, false},
     28 	{[]byte{0x01}, false, false},
     29 }
     30 
     31 func TestParseBool(t *testing.T) {
     32 	for i, test := range boolTestData {
     33 		ret, err := parseBool(test.in)
     34 		if (err == nil) != test.ok {
     35 			t.Errorf("#%d: Incorrect error result (did fail? %v, expected: %v)", i, err == nil, test.ok)
     36 		}
     37 		if test.ok && ret != test.out {
     38 			t.Errorf("#%d: Bad result: %v (expected %v)", i, ret, test.out)
     39 		}
     40 	}
     41 }
     42 
     43 type int64Test struct {
     44 	in  []byte
     45 	ok  bool
     46 	out int64
     47 }
     48 
     49 var int64TestData = []int64Test{
     50 	{[]byte{0x00}, true, 0},
     51 	{[]byte{0x7f}, true, 127},
     52 	{[]byte{0x00, 0x80}, true, 128},
     53 	{[]byte{0x01, 0x00}, true, 256},
     54 	{[]byte{0x80}, true, -128},
     55 	{[]byte{0xff, 0x7f}, true, -129},
     56 	{[]byte{0xff}, true, -1},
     57 	{[]byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, true, -9223372036854775808},
     58 	{[]byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, false, 0},
     59 	{[]byte{}, false, 0},
     60 	{[]byte{0x00, 0x7f}, false, 0},
     61 	{[]byte{0xff, 0xf0}, false, 0},
     62 }
     63 
     64 func TestParseInt64(t *testing.T) {
     65 	for i, test := range int64TestData {
     66 		ret, err := parseInt64(test.in)
     67 		if (err == nil) != test.ok {
     68 			t.Errorf("#%d: Incorrect error result (did fail? %v, expected: %v)", i, err == nil, test.ok)
     69 		}
     70 		if test.ok && ret != test.out {
     71 			t.Errorf("#%d: Bad result: %v (expected %v)", i, ret, test.out)
     72 		}
     73 	}
     74 }
     75 
     76 type int32Test struct {
     77 	in  []byte
     78 	ok  bool
     79 	out int32
     80 }
     81 
     82 var int32TestData = []int32Test{
     83 	{[]byte{0x00}, true, 0},
     84 	{[]byte{0x7f}, true, 127},
     85 	{[]byte{0x00, 0x80}, true, 128},
     86 	{[]byte{0x01, 0x00}, true, 256},
     87 	{[]byte{0x80}, true, -128},
     88 	{[]byte{0xff, 0x7f}, true, -129},
     89 	{[]byte{0xff}, true, -1},
     90 	{[]byte{0x80, 0x00, 0x00, 0x00}, true, -2147483648},
     91 	{[]byte{0x80, 0x00, 0x00, 0x00, 0x00}, false, 0},
     92 	{[]byte{}, false, 0},
     93 	{[]byte{0x00, 0x7f}, false, 0},
     94 	{[]byte{0xff, 0xf0}, false, 0},
     95 }
     96 
     97 func TestParseInt32(t *testing.T) {
     98 	for i, test := range int32TestData {
     99 		ret, err := parseInt32(test.in)
    100 		if (err == nil) != test.ok {
    101 			t.Errorf("#%d: Incorrect error result (did fail? %v, expected: %v)", i, err == nil, test.ok)
    102 		}
    103 		if test.ok && int32(ret) != test.out {
    104 			t.Errorf("#%d: Bad result: %v (expected %v)", i, ret, test.out)
    105 		}
    106 	}
    107 }
    108 
    109 var bigIntTests = []struct {
    110 	in     []byte
    111 	ok     bool
    112 	base10 string
    113 }{
    114 	{[]byte{0xff}, true, "-1"},
    115 	{[]byte{0x00}, true, "0"},
    116 	{[]byte{0x01}, true, "1"},
    117 	{[]byte{0x00, 0xff}, true, "255"},
    118 	{[]byte{0xff, 0x00}, true, "-256"},
    119 	{[]byte{0x01, 0x00}, true, "256"},
    120 	{[]byte{}, false, ""},
    121 	{[]byte{0x00, 0x7f}, false, ""},
    122 	{[]byte{0xff, 0xf0}, false, ""},
    123 }
    124 
    125 func TestParseBigInt(t *testing.T) {
    126 	for i, test := range bigIntTests {
    127 		ret, err := parseBigInt(test.in)
    128 		if (err == nil) != test.ok {
    129 			t.Errorf("#%d: Incorrect error result (did fail? %v, expected: %v)", i, err == nil, test.ok)
    130 		}
    131 		if test.ok {
    132 			if ret.String() != test.base10 {
    133 				t.Errorf("#%d: bad result from %x, got %s want %s", i, test.in, ret.String(), test.base10)
    134 			}
    135 			e, err := makeBigInt(ret)
    136 			if err != nil {
    137 				t.Errorf("%d: err=%q", i, err)
    138 				continue
    139 			}
    140 			result := make([]byte, e.Len())
    141 			e.Encode(result)
    142 			if !bytes.Equal(result, test.in) {
    143 				t.Errorf("#%d: got %x from marshaling %s, want %x", i, result, ret, test.in)
    144 			}
    145 		}
    146 	}
    147 }
    148 
    149 type bitStringTest struct {
    150 	in        []byte
    151 	ok        bool
    152 	out       []byte
    153 	bitLength int
    154 }
    155 
    156 var bitStringTestData = []bitStringTest{
    157 	{[]byte{}, false, []byte{}, 0},
    158 	{[]byte{0x00}, true, []byte{}, 0},
    159 	{[]byte{0x07, 0x00}, true, []byte{0x00}, 1},
    160 	{[]byte{0x07, 0x01}, false, []byte{}, 0},
    161 	{[]byte{0x07, 0x40}, false, []byte{}, 0},
    162 	{[]byte{0x08, 0x00}, false, []byte{}, 0},
    163 }
    164 
    165 func TestBitString(t *testing.T) {
    166 	for i, test := range bitStringTestData {
    167 		ret, err := parseBitString(test.in)
    168 		if (err == nil) != test.ok {
    169 			t.Errorf("#%d: Incorrect error result (did fail? %v, expected: %v)", i, err == nil, test.ok)
    170 		}
    171 		if err == nil {
    172 			if test.bitLength != ret.BitLength || !bytes.Equal(ret.Bytes, test.out) {
    173 				t.Errorf("#%d: Bad result: %v (expected %v %v)", i, ret, test.out, test.bitLength)
    174 			}
    175 		}
    176 	}
    177 }
    178 
    179 func TestBitStringAt(t *testing.T) {
    180 	bs := BitString{[]byte{0x82, 0x40}, 16}
    181 	if bs.At(0) != 1 {
    182 		t.Error("#1: Failed")
    183 	}
    184 	if bs.At(1) != 0 {
    185 		t.Error("#2: Failed")
    186 	}
    187 	if bs.At(6) != 1 {
    188 		t.Error("#3: Failed")
    189 	}
    190 	if bs.At(9) != 1 {
    191 		t.Error("#4: Failed")
    192 	}
    193 	if bs.At(-1) != 0 {
    194 		t.Error("#5: Failed")
    195 	}
    196 	if bs.At(17) != 0 {
    197 		t.Error("#6: Failed")
    198 	}
    199 }
    200 
    201 type bitStringRightAlignTest struct {
    202 	in    []byte
    203 	inlen int
    204 	out   []byte
    205 }
    206 
    207 var bitStringRightAlignTests = []bitStringRightAlignTest{
    208 	{[]byte{0x80}, 1, []byte{0x01}},
    209 	{[]byte{0x80, 0x80}, 9, []byte{0x01, 0x01}},
    210 	{[]byte{}, 0, []byte{}},
    211 	{[]byte{0xce}, 8, []byte{0xce}},
    212 	{[]byte{0xce, 0x47}, 16, []byte{0xce, 0x47}},
    213 	{[]byte{0x34, 0x50}, 12, []byte{0x03, 0x45}},
    214 }
    215 
    216 func TestBitStringRightAlign(t *testing.T) {
    217 	for i, test := range bitStringRightAlignTests {
    218 		bs := BitString{test.in, test.inlen}
    219 		out := bs.RightAlign()
    220 		if !bytes.Equal(out, test.out) {
    221 			t.Errorf("#%d got: %x want: %x", i, out, test.out)
    222 		}
    223 	}
    224 }
    225 
    226 type objectIdentifierTest struct {
    227 	in  []byte
    228 	ok  bool
    229 	out []int
    230 }
    231 
    232 var objectIdentifierTestData = []objectIdentifierTest{
    233 	{[]byte{}, false, []int{}},
    234 	{[]byte{85}, true, []int{2, 5}},
    235 	{[]byte{85, 0x02}, true, []int{2, 5, 2}},
    236 	{[]byte{85, 0x02, 0xc0, 0x00}, true, []int{2, 5, 2, 0x2000}},
    237 	{[]byte{0x81, 0x34, 0x03}, true, []int{2, 100, 3}},
    238 	{[]byte{85, 0x02, 0xc0, 0x80, 0x80, 0x80, 0x80}, false, []int{}},
    239 }
    240 
    241 func TestObjectIdentifier(t *testing.T) {
    242 	for i, test := range objectIdentifierTestData {
    243 		ret, err := parseObjectIdentifier(test.in)
    244 		if (err == nil) != test.ok {
    245 			t.Errorf("#%d: Incorrect error result (did fail? %v, expected: %v)", i, err == nil, test.ok)
    246 		}
    247 		if err == nil {
    248 			if !reflect.DeepEqual(test.out, ret) {
    249 				t.Errorf("#%d: Bad result: %v (expected %v)", i, ret, test.out)
    250 			}
    251 		}
    252 	}
    253 
    254 	if s := ObjectIdentifier([]int{1, 2, 3, 4}).String(); s != "1.2.3.4" {
    255 		t.Errorf("bad ObjectIdentifier.String(). Got %s, want 1.2.3.4", s)
    256 	}
    257 }
    258 
    259 type timeTest struct {
    260 	in  string
    261 	ok  bool
    262 	out time.Time
    263 }
    264 
    265 var utcTestData = []timeTest{
    266 	{"910506164540-0700", true, time.Date(1991, 05, 06, 16, 45, 40, 0, time.FixedZone("", -7*60*60))},
    267 	{"910506164540+0730", true, time.Date(1991, 05, 06, 16, 45, 40, 0, time.FixedZone("", 7*60*60+30*60))},
    268 	{"910506234540Z", true, time.Date(1991, 05, 06, 23, 45, 40, 0, time.UTC)},
    269 	{"9105062345Z", true, time.Date(1991, 05, 06, 23, 45, 0, 0, time.UTC)},
    270 	{"5105062345Z", true, time.Date(1951, 05, 06, 23, 45, 0, 0, time.UTC)},
    271 	{"a10506234540Z", false, time.Time{}},
    272 	{"91a506234540Z", false, time.Time{}},
    273 	{"9105a6234540Z", false, time.Time{}},
    274 	{"910506a34540Z", false, time.Time{}},
    275 	{"910506334a40Z", false, time.Time{}},
    276 	{"91050633444aZ", false, time.Time{}},
    277 	{"910506334461Z", false, time.Time{}},
    278 	{"910506334400Za", false, time.Time{}},
    279 	/* These are invalid times. However, the time package normalises times
    280 	 * and they were accepted in some versions. See #11134. */
    281 	{"000100000000Z", false, time.Time{}},
    282 	{"101302030405Z", false, time.Time{}},
    283 	{"100002030405Z", false, time.Time{}},
    284 	{"100100030405Z", false, time.Time{}},
    285 	{"100132030405Z", false, time.Time{}},
    286 	{"100231030405Z", false, time.Time{}},
    287 	{"100102240405Z", false, time.Time{}},
    288 	{"100102036005Z", false, time.Time{}},
    289 	{"100102030460Z", false, time.Time{}},
    290 	{"-100102030410Z", false, time.Time{}},
    291 	{"10-0102030410Z", false, time.Time{}},
    292 	{"10-0002030410Z", false, time.Time{}},
    293 	{"1001-02030410Z", false, time.Time{}},
    294 	{"100102-030410Z", false, time.Time{}},
    295 	{"10010203-0410Z", false, time.Time{}},
    296 	{"1001020304-10Z", false, time.Time{}},
    297 }
    298 
    299 func TestUTCTime(t *testing.T) {
    300 	for i, test := range utcTestData {
    301 		ret, err := parseUTCTime([]byte(test.in))
    302 		if err != nil {
    303 			if test.ok {
    304 				t.Errorf("#%d: parseUTCTime(%q) = error %v", i, test.in, err)
    305 			}
    306 			continue
    307 		}
    308 		if !test.ok {
    309 			t.Errorf("#%d: parseUTCTime(%q) succeeded, should have failed", i, test.in)
    310 			continue
    311 		}
    312 		const format = "Jan _2 15:04:05 -0700 2006" // ignore zone name, just offset
    313 		have := ret.Format(format)
    314 		want := test.out.Format(format)
    315 		if have != want {
    316 			t.Errorf("#%d: parseUTCTime(%q) = %s, want %s", i, test.in, have, want)
    317 		}
    318 	}
    319 }
    320 
    321 var generalizedTimeTestData = []timeTest{
    322 	{"20100102030405Z", true, time.Date(2010, 01, 02, 03, 04, 05, 0, time.UTC)},
    323 	{"20100102030405", false, time.Time{}},
    324 	{"20100102030405+0607", true, time.Date(2010, 01, 02, 03, 04, 05, 0, time.FixedZone("", 6*60*60+7*60))},
    325 	{"20100102030405-0607", true, time.Date(2010, 01, 02, 03, 04, 05, 0, time.FixedZone("", -6*60*60-7*60))},
    326 	/* These are invalid times. However, the time package normalises times
    327 	 * and they were accepted in some versions. See #11134. */
    328 	{"00000100000000Z", false, time.Time{}},
    329 	{"20101302030405Z", false, time.Time{}},
    330 	{"20100002030405Z", false, time.Time{}},
    331 	{"20100100030405Z", false, time.Time{}},
    332 	{"20100132030405Z", false, time.Time{}},
    333 	{"20100231030405Z", false, time.Time{}},
    334 	{"20100102240405Z", false, time.Time{}},
    335 	{"20100102036005Z", false, time.Time{}},
    336 	{"20100102030460Z", false, time.Time{}},
    337 	{"-20100102030410Z", false, time.Time{}},
    338 	{"2010-0102030410Z", false, time.Time{}},
    339 	{"2010-0002030410Z", false, time.Time{}},
    340 	{"201001-02030410Z", false, time.Time{}},
    341 	{"20100102-030410Z", false, time.Time{}},
    342 	{"2010010203-0410Z", false, time.Time{}},
    343 	{"201001020304-10Z", false, time.Time{}},
    344 }
    345 
    346 func TestGeneralizedTime(t *testing.T) {
    347 	for i, test := range generalizedTimeTestData {
    348 		ret, err := parseGeneralizedTime([]byte(test.in))
    349 		if (err == nil) != test.ok {
    350 			t.Errorf("#%d: Incorrect error result (did fail? %v, expected: %v)", i, err == nil, test.ok)
    351 		}
    352 		if err == nil {
    353 			if !reflect.DeepEqual(test.out, ret) {
    354 				t.Errorf("#%d: Bad result: %q  %v (expected %v)", i, test.in, ret, test.out)
    355 			}
    356 		}
    357 	}
    358 }
    359 
    360 type tagAndLengthTest struct {
    361 	in  []byte
    362 	ok  bool
    363 	out tagAndLength
    364 }
    365 
    366 var tagAndLengthData = []tagAndLengthTest{
    367 	{[]byte{0x80, 0x01}, true, tagAndLength{2, 0, 1, false}},
    368 	{[]byte{0xa0, 0x01}, true, tagAndLength{2, 0, 1, true}},
    369 	{[]byte{0x02, 0x00}, true, tagAndLength{0, 2, 0, false}},
    370 	{[]byte{0xfe, 0x00}, true, tagAndLength{3, 30, 0, true}},
    371 	{[]byte{0x1f, 0x1f, 0x00}, true, tagAndLength{0, 31, 0, false}},
    372 	{[]byte{0x1f, 0x81, 0x00, 0x00}, true, tagAndLength{0, 128, 0, false}},
    373 	{[]byte{0x1f, 0x81, 0x80, 0x01, 0x00}, true, tagAndLength{0, 0x4001, 0, false}},
    374 	{[]byte{0x00, 0x81, 0x80}, true, tagAndLength{0, 0, 128, false}},
    375 	{[]byte{0x00, 0x82, 0x01, 0x00}, true, tagAndLength{0, 0, 256, false}},
    376 	{[]byte{0x00, 0x83, 0x01, 0x00}, false, tagAndLength{}},
    377 	{[]byte{0x1f, 0x85}, false, tagAndLength{}},
    378 	{[]byte{0x30, 0x80}, false, tagAndLength{}},
    379 	// Superfluous zeros in the length should be an error.
    380 	{[]byte{0xa0, 0x82, 0x00, 0xff}, false, tagAndLength{}},
    381 	// Lengths up to the maximum size of an int should work.
    382 	{[]byte{0xa0, 0x84, 0x7f, 0xff, 0xff, 0xff}, true, tagAndLength{2, 0, 0x7fffffff, true}},
    383 	// Lengths that would overflow an int should be rejected.
    384 	{[]byte{0xa0, 0x84, 0x80, 0x00, 0x00, 0x00}, false, tagAndLength{}},
    385 	// Long length form may not be used for lengths that fit in short form.
    386 	{[]byte{0xa0, 0x81, 0x7f}, false, tagAndLength{}},
    387 	// Tag numbers which would overflow int32 are rejected. (The value below is 2^31.)
    388 	{[]byte{0x1f, 0x88, 0x80, 0x80, 0x80, 0x00, 0x00}, false, tagAndLength{}},
    389 	// Long tag number form may not be used for tags that fit in short form.
    390 	{[]byte{0x1f, 0x1e, 0x00}, false, tagAndLength{}},
    391 }
    392 
    393 func TestParseTagAndLength(t *testing.T) {
    394 	for i, test := range tagAndLengthData {
    395 		tagAndLength, _, err := parseTagAndLength(test.in, 0)
    396 		if (err == nil) != test.ok {
    397 			t.Errorf("#%d: Incorrect error result (did pass? %v, expected: %v)", i, err == nil, test.ok)
    398 		}
    399 		if err == nil && !reflect.DeepEqual(test.out, tagAndLength) {
    400 			t.Errorf("#%d: Bad result: %v (expected %v)", i, tagAndLength, test.out)
    401 		}
    402 	}
    403 }
    404 
    405 type parseFieldParametersTest struct {
    406 	in  string
    407 	out fieldParameters
    408 }
    409 
    410 func newInt(n int) *int { return &n }
    411 
    412 func newInt64(n int64) *int64 { return &n }
    413 
    414 func newString(s string) *string { return &s }
    415 
    416 func newBool(b bool) *bool { return &b }
    417 
    418 var parseFieldParametersTestData []parseFieldParametersTest = []parseFieldParametersTest{
    419 	{"", fieldParameters{}},
    420 	{"ia5", fieldParameters{stringType: TagIA5String}},
    421 	{"generalized", fieldParameters{timeType: TagGeneralizedTime}},
    422 	{"utc", fieldParameters{timeType: TagUTCTime}},
    423 	{"printable", fieldParameters{stringType: TagPrintableString}},
    424 	{"optional", fieldParameters{optional: true}},
    425 	{"explicit", fieldParameters{explicit: true, tag: new(int)}},
    426 	{"application", fieldParameters{application: true, tag: new(int)}},
    427 	{"optional,explicit", fieldParameters{optional: true, explicit: true, tag: new(int)}},
    428 	{"default:42", fieldParameters{defaultValue: newInt64(42)}},
    429 	{"tag:17", fieldParameters{tag: newInt(17)}},
    430 	{"optional,explicit,default:42,tag:17", fieldParameters{optional: true, explicit: true, defaultValue: newInt64(42), tag: newInt(17)}},
    431 	{"optional,explicit,default:42,tag:17,rubbish1", fieldParameters{true, true, false, newInt64(42), newInt(17), 0, 0, false, false}},
    432 	{"set", fieldParameters{set: true}},
    433 }
    434 
    435 func TestParseFieldParameters(t *testing.T) {
    436 	for i, test := range parseFieldParametersTestData {
    437 		f := parseFieldParameters(test.in)
    438 		if !reflect.DeepEqual(f, test.out) {
    439 			t.Errorf("#%d: Bad result: %v (expected %v)", i, f, test.out)
    440 		}
    441 	}
    442 }
    443 
    444 type TestObjectIdentifierStruct struct {
    445 	OID ObjectIdentifier
    446 }
    447 
    448 type TestContextSpecificTags struct {
    449 	A int `asn1:"tag:1"`
    450 }
    451 
    452 type TestContextSpecificTags2 struct {
    453 	A int `asn1:"explicit,tag:1"`
    454 	B int
    455 }
    456 
    457 type TestContextSpecificTags3 struct {
    458 	S string `asn1:"tag:1,utf8"`
    459 }
    460 
    461 type TestElementsAfterString struct {
    462 	S    string
    463 	A, B int
    464 }
    465 
    466 type TestBigInt struct {
    467 	X *big.Int
    468 }
    469 
    470 type TestSet struct {
    471 	Ints []int `asn1:"set"`
    472 }
    473 
    474 var unmarshalTestData = []struct {
    475 	in  []byte
    476 	out interface{}
    477 }{
    478 	{[]byte{0x02, 0x01, 0x42}, newInt(0x42)},
    479 	{[]byte{0x30, 0x08, 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d}, &TestObjectIdentifierStruct{[]int{1, 2, 840, 113549}}},
    480 	{[]byte{0x03, 0x04, 0x06, 0x6e, 0x5d, 0xc0}, &BitString{[]byte{110, 93, 192}, 18}},
    481 	{[]byte{0x30, 0x09, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x01, 0x03}, &[]int{1, 2, 3}},
    482 	{[]byte{0x02, 0x01, 0x10}, newInt(16)},
    483 	{[]byte{0x13, 0x04, 't', 'e', 's', 't'}, newString("test")},
    484 	{[]byte{0x16, 0x04, 't', 'e', 's', 't'}, newString("test")},
    485 	{[]byte{0x16, 0x04, 't', 'e', 's', 't'}, &RawValue{0, 22, false, []byte("test"), []byte("\x16\x04test")}},
    486 	{[]byte{0x04, 0x04, 1, 2, 3, 4}, &RawValue{0, 4, false, []byte{1, 2, 3, 4}, []byte{4, 4, 1, 2, 3, 4}}},
    487 	{[]byte{0x30, 0x03, 0x81, 0x01, 0x01}, &TestContextSpecificTags{1}},
    488 	{[]byte{0x30, 0x08, 0xa1, 0x03, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02}, &TestContextSpecificTags2{1, 2}},
    489 	{[]byte{0x30, 0x03, 0x81, 0x01, '@'}, &TestContextSpecificTags3{"@"}},
    490 	{[]byte{0x01, 0x01, 0x00}, newBool(false)},
    491 	{[]byte{0x01, 0x01, 0xff}, newBool(true)},
    492 	{[]byte{0x30, 0x0b, 0x13, 0x03, 0x66, 0x6f, 0x6f, 0x02, 0x01, 0x22, 0x02, 0x01, 0x33}, &TestElementsAfterString{"foo", 0x22, 0x33}},
    493 	{[]byte{0x30, 0x05, 0x02, 0x03, 0x12, 0x34, 0x56}, &TestBigInt{big.NewInt(0x123456)}},
    494 	{[]byte{0x30, 0x0b, 0x31, 0x09, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x01, 0x03}, &TestSet{Ints: []int{1, 2, 3}}},
    495 }
    496 
    497 func TestUnmarshal(t *testing.T) {
    498 	for i, test := range unmarshalTestData {
    499 		pv := reflect.New(reflect.TypeOf(test.out).Elem())
    500 		val := pv.Interface()
    501 		_, err := Unmarshal(test.in, val)
    502 		if err != nil {
    503 			t.Errorf("Unmarshal failed at index %d %v", i, err)
    504 		}
    505 		if !reflect.DeepEqual(val, test.out) {
    506 			t.Errorf("#%d:\nhave %#v\nwant %#v", i, val, test.out)
    507 		}
    508 	}
    509 }
    510 
    511 type Certificate struct {
    512 	TBSCertificate     TBSCertificate
    513 	SignatureAlgorithm AlgorithmIdentifier
    514 	SignatureValue     BitString
    515 }
    516 
    517 type TBSCertificate struct {
    518 	Version            int `asn1:"optional,explicit,default:0,tag:0"`
    519 	SerialNumber       RawValue
    520 	SignatureAlgorithm AlgorithmIdentifier
    521 	Issuer             RDNSequence
    522 	Validity           Validity
    523 	Subject            RDNSequence
    524 	PublicKey          PublicKeyInfo
    525 }
    526 
    527 type AlgorithmIdentifier struct {
    528 	Algorithm ObjectIdentifier
    529 }
    530 
    531 type RDNSequence []RelativeDistinguishedNameSET
    532 
    533 type RelativeDistinguishedNameSET []AttributeTypeAndValue
    534 
    535 type AttributeTypeAndValue struct {
    536 	Type  ObjectIdentifier
    537 	Value interface{}
    538 }
    539 
    540 type Validity struct {
    541 	NotBefore, NotAfter time.Time
    542 }
    543 
    544 type PublicKeyInfo struct {
    545 	Algorithm AlgorithmIdentifier
    546 	PublicKey BitString
    547 }
    548 
    549 func TestCertificate(t *testing.T) {
    550 	// This is a minimal, self-signed certificate that should parse correctly.
    551 	var cert Certificate
    552 	if _, err := Unmarshal(derEncodedSelfSignedCertBytes, &cert); err != nil {
    553 		t.Errorf("Unmarshal failed: %v", err)
    554 	}
    555 	if !reflect.DeepEqual(cert, derEncodedSelfSignedCert) {
    556 		t.Errorf("Bad result:\ngot: %+v\nwant: %+v", cert, derEncodedSelfSignedCert)
    557 	}
    558 }
    559 
    560 func TestCertificateWithNUL(t *testing.T) {
    561 	// This is the paypal NUL-hack certificate. It should fail to parse because
    562 	// NUL isn't a permitted character in a PrintableString.
    563 
    564 	var cert Certificate
    565 	if _, err := Unmarshal(derEncodedPaypalNULCertBytes, &cert); err == nil {
    566 		t.Error("Unmarshal succeeded, should not have")
    567 	}
    568 }
    569 
    570 type rawStructTest struct {
    571 	Raw RawContent
    572 	A   int
    573 }
    574 
    575 func TestRawStructs(t *testing.T) {
    576 	var s rawStructTest
    577 	input := []byte{0x30, 0x03, 0x02, 0x01, 0x50}
    578 
    579 	rest, err := Unmarshal(input, &s)
    580 	if len(rest) != 0 {
    581 		t.Errorf("incomplete parse: %x", rest)
    582 		return
    583 	}
    584 	if err != nil {
    585 		t.Error(err)
    586 		return
    587 	}
    588 	if s.A != 0x50 {
    589 		t.Errorf("bad value for A: got %d want %d", s.A, 0x50)
    590 	}
    591 	if !bytes.Equal([]byte(s.Raw), input) {
    592 		t.Errorf("bad value for Raw: got %x want %x", s.Raw, input)
    593 	}
    594 }
    595 
    596 type oiEqualTest struct {
    597 	first  ObjectIdentifier
    598 	second ObjectIdentifier
    599 	same   bool
    600 }
    601 
    602 var oiEqualTests = []oiEqualTest{
    603 	{
    604 		ObjectIdentifier{1, 2, 3},
    605 		ObjectIdentifier{1, 2, 3},
    606 		true,
    607 	},
    608 	{
    609 		ObjectIdentifier{1},
    610 		ObjectIdentifier{1, 2, 3},
    611 		false,
    612 	},
    613 	{
    614 		ObjectIdentifier{1, 2, 3},
    615 		ObjectIdentifier{10, 11, 12},
    616 		false,
    617 	},
    618 }
    619 
    620 func TestObjectIdentifierEqual(t *testing.T) {
    621 	for _, o := range oiEqualTests {
    622 		if s := o.first.Equal(o.second); s != o.same {
    623 			t.Errorf("ObjectIdentifier.Equal: got: %t want: %t", s, o.same)
    624 		}
    625 	}
    626 }
    627 
    628 var derEncodedSelfSignedCert = Certificate{
    629 	TBSCertificate: TBSCertificate{
    630 		Version:            0,
    631 		SerialNumber:       RawValue{Class: 0, Tag: 2, IsCompound: false, Bytes: []uint8{0x0, 0x8c, 0xc3, 0x37, 0x92, 0x10, 0xec, 0x2c, 0x98}, FullBytes: []byte{2, 9, 0x0, 0x8c, 0xc3, 0x37, 0x92, 0x10, 0xec, 0x2c, 0x98}},
    632 		SignatureAlgorithm: AlgorithmIdentifier{Algorithm: ObjectIdentifier{1, 2, 840, 113549, 1, 1, 5}},
    633 		Issuer: RDNSequence{
    634 			RelativeDistinguishedNameSET{AttributeTypeAndValue{Type: ObjectIdentifier{2, 5, 4, 6}, Value: "XX"}},
    635 			RelativeDistinguishedNameSET{AttributeTypeAndValue{Type: ObjectIdentifier{2, 5, 4, 8}, Value: "Some-State"}},
    636 			RelativeDistinguishedNameSET{AttributeTypeAndValue{Type: ObjectIdentifier{2, 5, 4, 7}, Value: "City"}},
    637 			RelativeDistinguishedNameSET{AttributeTypeAndValue{Type: ObjectIdentifier{2, 5, 4, 10}, Value: "Internet Widgits Pty Ltd"}},
    638 			RelativeDistinguishedNameSET{AttributeTypeAndValue{Type: ObjectIdentifier{2, 5, 4, 3}, Value: "false.example.com"}},
    639 			RelativeDistinguishedNameSET{AttributeTypeAndValue{Type: ObjectIdentifier{1, 2, 840, 113549, 1, 9, 1}, Value: "false (a] example.com"}},
    640 		},
    641 		Validity: Validity{
    642 			NotBefore: time.Date(2009, 10, 8, 00, 25, 53, 0, time.UTC),
    643 			NotAfter:  time.Date(2010, 10, 8, 00, 25, 53, 0, time.UTC),
    644 		},
    645 		Subject: RDNSequence{
    646 			RelativeDistinguishedNameSET{AttributeTypeAndValue{Type: ObjectIdentifier{2, 5, 4, 6}, Value: "XX"}},
    647 			RelativeDistinguishedNameSET{AttributeTypeAndValue{Type: ObjectIdentifier{2, 5, 4, 8}, Value: "Some-State"}},
    648 			RelativeDistinguishedNameSET{AttributeTypeAndValue{Type: ObjectIdentifier{2, 5, 4, 7}, Value: "City"}},
    649 			RelativeDistinguishedNameSET{AttributeTypeAndValue{Type: ObjectIdentifier{2, 5, 4, 10}, Value: "Internet Widgits Pty Ltd"}},
    650 			RelativeDistinguishedNameSET{AttributeTypeAndValue{Type: ObjectIdentifier{2, 5, 4, 3}, Value: "false.example.com"}},
    651 			RelativeDistinguishedNameSET{AttributeTypeAndValue{Type: ObjectIdentifier{1, 2, 840, 113549, 1, 9, 1}, Value: "false (a] example.com"}},
    652 		},
    653 		PublicKey: PublicKeyInfo{
    654 			Algorithm: AlgorithmIdentifier{Algorithm: ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1}},
    655 			PublicKey: BitString{
    656 				Bytes: []uint8{
    657 					0x30, 0x48, 0x2, 0x41, 0x0, 0xcd, 0xb7,
    658 					0x63, 0x9c, 0x32, 0x78, 0xf0, 0x6, 0xaa, 0x27, 0x7f, 0x6e, 0xaf, 0x42,
    659 					0x90, 0x2b, 0x59, 0x2d, 0x8c, 0xbc, 0xbe, 0x38, 0xa1, 0xc9, 0x2b, 0xa4,
    660 					0x69, 0x5a, 0x33, 0x1b, 0x1d, 0xea, 0xde, 0xad, 0xd8, 0xe9, 0xa5, 0xc2,
    661 					0x7e, 0x8c, 0x4c, 0x2f, 0xd0, 0xa8, 0x88, 0x96, 0x57, 0x72, 0x2a, 0x4f,
    662 					0x2a, 0xf7, 0x58, 0x9c, 0xf2, 0xc7, 0x70, 0x45, 0xdc, 0x8f, 0xde, 0xec,
    663 					0x35, 0x7d, 0x2, 0x3, 0x1, 0x0, 0x1,
    664 				},
    665 				BitLength: 592,
    666 			},
    667 		},
    668 	},
    669 	SignatureAlgorithm: AlgorithmIdentifier{Algorithm: ObjectIdentifier{1, 2, 840, 113549, 1, 1, 5}},
    670 	SignatureValue: BitString{
    671 		Bytes: []uint8{
    672 			0xa6, 0x7b, 0x6, 0xec, 0x5e, 0xce,
    673 			0x92, 0x77, 0x2c, 0xa4, 0x13, 0xcb, 0xa3, 0xca, 0x12, 0x56, 0x8f, 0xdc, 0x6c,
    674 			0x7b, 0x45, 0x11, 0xcd, 0x40, 0xa7, 0xf6, 0x59, 0x98, 0x4, 0x2, 0xdf, 0x2b,
    675 			0x99, 0x8b, 0xb9, 0xa4, 0xa8, 0xcb, 0xeb, 0x34, 0xc0, 0xf0, 0xa7, 0x8c, 0xf8,
    676 			0xd9, 0x1e, 0xde, 0x14, 0xa5, 0xed, 0x76, 0xbf, 0x11, 0x6f, 0xe3, 0x60, 0xaa,
    677 			0xfa, 0x88, 0x21, 0x49, 0x4, 0x35,
    678 		},
    679 		BitLength: 512,
    680 	},
    681 }
    682 
    683 var derEncodedSelfSignedCertBytes = []byte{
    684 	0x30, 0x82, 0x02, 0x18, 0x30,
    685 	0x82, 0x01, 0xc2, 0x02, 0x09, 0x00, 0x8c, 0xc3, 0x37, 0x92, 0x10, 0xec, 0x2c,
    686 	0x98, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
    687 	0x05, 0x05, 0x00, 0x30, 0x81, 0x92, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
    688 	0x04, 0x06, 0x13, 0x02, 0x58, 0x58, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
    689 	0x04, 0x08, 0x13, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74,
    690 	0x65, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x07, 0x13, 0x04, 0x43,
    691 	0x69, 0x74, 0x79, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13,
    692 	0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64,
    693 	0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x31,
    694 	0x1a, 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x11, 0x66, 0x61, 0x6c,
    695 	0x73, 0x65, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f,
    696 	0x6d, 0x31, 0x20, 0x30, 0x1e, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
    697 	0x01, 0x09, 0x01, 0x16, 0x11, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x40, 0x65, 0x78,
    698 	0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d,
    699 	0x30, 0x39, 0x31, 0x30, 0x30, 0x38, 0x30, 0x30, 0x32, 0x35, 0x35, 0x33, 0x5a,
    700 	0x17, 0x0d, 0x31, 0x30, 0x31, 0x30, 0x30, 0x38, 0x30, 0x30, 0x32, 0x35, 0x35,
    701 	0x33, 0x5a, 0x30, 0x81, 0x92, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
    702 	0x06, 0x13, 0x02, 0x58, 0x58, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
    703 	0x08, 0x13, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65,
    704 	0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x07, 0x13, 0x04, 0x43, 0x69,
    705 	0x74, 0x79, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x18,
    706 	0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67,
    707 	0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x31, 0x1a,
    708 	0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x11, 0x66, 0x61, 0x6c, 0x73,
    709 	0x65, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d,
    710 	0x31, 0x20, 0x30, 0x1e, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
    711 	0x09, 0x01, 0x16, 0x11, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x40, 0x65, 0x78, 0x61,
    712 	0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x5c, 0x30, 0x0d, 0x06,
    713 	0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
    714 	0x4b, 0x00, 0x30, 0x48, 0x02, 0x41, 0x00, 0xcd, 0xb7, 0x63, 0x9c, 0x32, 0x78,
    715 	0xf0, 0x06, 0xaa, 0x27, 0x7f, 0x6e, 0xaf, 0x42, 0x90, 0x2b, 0x59, 0x2d, 0x8c,
    716 	0xbc, 0xbe, 0x38, 0xa1, 0xc9, 0x2b, 0xa4, 0x69, 0x5a, 0x33, 0x1b, 0x1d, 0xea,
    717 	0xde, 0xad, 0xd8, 0xe9, 0xa5, 0xc2, 0x7e, 0x8c, 0x4c, 0x2f, 0xd0, 0xa8, 0x88,
    718 	0x96, 0x57, 0x72, 0x2a, 0x4f, 0x2a, 0xf7, 0x58, 0x9c, 0xf2, 0xc7, 0x70, 0x45,
    719 	0xdc, 0x8f, 0xde, 0xec, 0x35, 0x7d, 0x02, 0x03, 0x01, 0x00, 0x01, 0x30, 0x0d,
    720 	0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00,
    721 	0x03, 0x41, 0x00, 0xa6, 0x7b, 0x06, 0xec, 0x5e, 0xce, 0x92, 0x77, 0x2c, 0xa4,
    722 	0x13, 0xcb, 0xa3, 0xca, 0x12, 0x56, 0x8f, 0xdc, 0x6c, 0x7b, 0x45, 0x11, 0xcd,
    723 	0x40, 0xa7, 0xf6, 0x59, 0x98, 0x04, 0x02, 0xdf, 0x2b, 0x99, 0x8b, 0xb9, 0xa4,
    724 	0xa8, 0xcb, 0xeb, 0x34, 0xc0, 0xf0, 0xa7, 0x8c, 0xf8, 0xd9, 0x1e, 0xde, 0x14,
    725 	0xa5, 0xed, 0x76, 0xbf, 0x11, 0x6f, 0xe3, 0x60, 0xaa, 0xfa, 0x88, 0x21, 0x49,
    726 	0x04, 0x35,
    727 }
    728 
    729 var derEncodedPaypalNULCertBytes = []byte{
    730 	0x30, 0x82, 0x06, 0x44, 0x30,
    731 	0x82, 0x05, 0xad, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x03, 0x00, 0xf0, 0x9b,
    732 	0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05,
    733 	0x05, 0x00, 0x30, 0x82, 0x01, 0x12, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
    734 	0x04, 0x06, 0x13, 0x02, 0x45, 0x53, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55,
    735 	0x04, 0x08, 0x13, 0x09, 0x42, 0x61, 0x72, 0x63, 0x65, 0x6c, 0x6f, 0x6e, 0x61,
    736 	0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x07, 0x13, 0x09, 0x42, 0x61,
    737 	0x72, 0x63, 0x65, 0x6c, 0x6f, 0x6e, 0x61, 0x31, 0x29, 0x30, 0x27, 0x06, 0x03,
    738 	0x55, 0x04, 0x0a, 0x13, 0x20, 0x49, 0x50, 0x53, 0x20, 0x43, 0x65, 0x72, 0x74,
    739 	0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x41, 0x75, 0x74,
    740 	0x68, 0x6f, 0x72, 0x69, 0x74, 0x79, 0x20, 0x73, 0x2e, 0x6c, 0x2e, 0x31, 0x2e,
    741 	0x30, 0x2c, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x14, 0x25, 0x67, 0x65, 0x6e, 0x65,
    742 	0x72, 0x61, 0x6c, 0x40, 0x69, 0x70, 0x73, 0x63, 0x61, 0x2e, 0x63, 0x6f, 0x6d,
    743 	0x20, 0x43, 0x2e, 0x49, 0x2e, 0x46, 0x2e, 0x20, 0x20, 0x42, 0x2d, 0x42, 0x36,
    744 	0x32, 0x32, 0x31, 0x30, 0x36, 0x39, 0x35, 0x31, 0x2e, 0x30, 0x2c, 0x06, 0x03,
    745 	0x55, 0x04, 0x0b, 0x13, 0x25, 0x69, 0x70, 0x73, 0x43, 0x41, 0x20, 0x43, 0x4c,
    746 	0x41, 0x53, 0x45, 0x41, 0x31, 0x20, 0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69,
    747 	0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72,
    748 	0x69, 0x74, 0x79, 0x31, 0x2e, 0x30, 0x2c, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13,
    749 	0x25, 0x69, 0x70, 0x73, 0x43, 0x41, 0x20, 0x43, 0x4c, 0x41, 0x53, 0x45, 0x41,
    750 	0x31, 0x20, 0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69,
    751 	0x6f, 0x6e, 0x20, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x69, 0x74, 0x79, 0x31,
    752 	0x20, 0x30, 0x1e, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09,
    753 	0x01, 0x16, 0x11, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x40, 0x69, 0x70,
    754 	0x73, 0x63, 0x61, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x30, 0x39,
    755 	0x30, 0x32, 0x32, 0x34, 0x32, 0x33, 0x30, 0x34, 0x31, 0x37, 0x5a, 0x17, 0x0d,
    756 	0x31, 0x31, 0x30, 0x32, 0x32, 0x34, 0x32, 0x33, 0x30, 0x34, 0x31, 0x37, 0x5a,
    757 	0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
    758 	0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13,
    759 	0x0a, 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61, 0x31, 0x16,
    760 	0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x07, 0x13, 0x0d, 0x53, 0x61, 0x6e, 0x20,
    761 	0x46, 0x72, 0x61, 0x6e, 0x63, 0x69, 0x73, 0x63, 0x6f, 0x31, 0x11, 0x30, 0x0f,
    762 	0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x08, 0x53, 0x65, 0x63, 0x75, 0x72, 0x69,
    763 	0x74, 0x79, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x0b,
    764 	0x53, 0x65, 0x63, 0x75, 0x72, 0x65, 0x20, 0x55, 0x6e, 0x69, 0x74, 0x31, 0x2f,
    765 	0x30, 0x2d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x26, 0x77, 0x77, 0x77, 0x2e,
    766 	0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00, 0x73, 0x73,
    767 	0x6c, 0x2e, 0x73, 0x65, 0x63, 0x75, 0x72, 0x65, 0x63, 0x6f, 0x6e, 0x6e, 0x65,
    768 	0x63, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x63, 0x63, 0x30, 0x81, 0x9f, 0x30, 0x0d,
    769 	0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00,
    770 	0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xd2, 0x69,
    771 	0xfa, 0x6f, 0x3a, 0x00, 0xb4, 0x21, 0x1b, 0xc8, 0xb1, 0x02, 0xd7, 0x3f, 0x19,
    772 	0xb2, 0xc4, 0x6d, 0xb4, 0x54, 0xf8, 0x8b, 0x8a, 0xcc, 0xdb, 0x72, 0xc2, 0x9e,
    773 	0x3c, 0x60, 0xb9, 0xc6, 0x91, 0x3d, 0x82, 0xb7, 0x7d, 0x99, 0xff, 0xd1, 0x29,
    774 	0x84, 0xc1, 0x73, 0x53, 0x9c, 0x82, 0xdd, 0xfc, 0x24, 0x8c, 0x77, 0xd5, 0x41,
    775 	0xf3, 0xe8, 0x1e, 0x42, 0xa1, 0xad, 0x2d, 0x9e, 0xff, 0x5b, 0x10, 0x26, 0xce,
    776 	0x9d, 0x57, 0x17, 0x73, 0x16, 0x23, 0x38, 0xc8, 0xd6, 0xf1, 0xba, 0xa3, 0x96,
    777 	0x5b, 0x16, 0x67, 0x4a, 0x4f, 0x73, 0x97, 0x3a, 0x4d, 0x14, 0xa4, 0xf4, 0xe2,
    778 	0x3f, 0x8b, 0x05, 0x83, 0x42, 0xd1, 0xd0, 0xdc, 0x2f, 0x7a, 0xe5, 0xb6, 0x10,
    779 	0xb2, 0x11, 0xc0, 0xdc, 0x21, 0x2a, 0x90, 0xff, 0xae, 0x97, 0x71, 0x5a, 0x49,
    780 	0x81, 0xac, 0x40, 0xf3, 0x3b, 0xb8, 0x59, 0xb2, 0x4f, 0x02, 0x03, 0x01, 0x00,
    781 	0x01, 0xa3, 0x82, 0x03, 0x21, 0x30, 0x82, 0x03, 0x1d, 0x30, 0x09, 0x06, 0x03,
    782 	0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x11, 0x06, 0x09, 0x60, 0x86,
    783 	0x48, 0x01, 0x86, 0xf8, 0x42, 0x01, 0x01, 0x04, 0x04, 0x03, 0x02, 0x06, 0x40,
    784 	0x30, 0x0b, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x04, 0x04, 0x03, 0x02, 0x03, 0xf8,
    785 	0x30, 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a, 0x06, 0x08,
    786 	0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x30, 0x1d, 0x06, 0x03, 0x55,
    787 	0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x61, 0x8f, 0x61, 0x34, 0x43, 0x55, 0x14,
    788 	0x7f, 0x27, 0x09, 0xce, 0x4c, 0x8b, 0xea, 0x9b, 0x7b, 0x19, 0x25, 0xbc, 0x6e,
    789 	0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14,
    790 	0x0e, 0x07, 0x60, 0xd4, 0x39, 0xc9, 0x1b, 0x5b, 0x5d, 0x90, 0x7b, 0x23, 0xc8,
    791 	0xd2, 0x34, 0x9d, 0x4a, 0x9a, 0x46, 0x39, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d,
    792 	0x11, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1c, 0x06, 0x03, 0x55, 0x1d, 0x12, 0x04,
    793 	0x15, 0x30, 0x13, 0x81, 0x11, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x40,
    794 	0x69, 0x70, 0x73, 0x63, 0x61, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x72, 0x06, 0x09,
    795 	0x60, 0x86, 0x48, 0x01, 0x86, 0xf8, 0x42, 0x01, 0x0d, 0x04, 0x65, 0x16, 0x63,
    796 	0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20,
    797 	0x49, 0x6e, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x4e,
    798 	0x4f, 0x54, 0x20, 0x56, 0x41, 0x4c, 0x49, 0x44, 0x41, 0x54, 0x45, 0x44, 0x2e,
    799 	0x20, 0x43, 0x4c, 0x41, 0x53, 0x45, 0x41, 0x31, 0x20, 0x53, 0x65, 0x72, 0x76,
    800 	0x65, 0x72, 0x20, 0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74,
    801 	0x65, 0x20, 0x69, 0x73, 0x73, 0x75, 0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x68,
    802 	0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x69, 0x70,
    803 	0x73, 0x63, 0x61, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x30, 0x2f, 0x06, 0x09, 0x60,
    804 	0x86, 0x48, 0x01, 0x86, 0xf8, 0x42, 0x01, 0x02, 0x04, 0x22, 0x16, 0x20, 0x68,
    805 	0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x69, 0x70,
    806 	0x73, 0x63, 0x61, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x69, 0x70, 0x73, 0x63, 0x61,
    807 	0x32, 0x30, 0x30, 0x32, 0x2f, 0x30, 0x43, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
    808 	0x86, 0xf8, 0x42, 0x01, 0x04, 0x04, 0x36, 0x16, 0x34, 0x68, 0x74, 0x74, 0x70,
    809 	0x73, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x69, 0x70, 0x73, 0x63, 0x61,
    810 	0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x69, 0x70, 0x73, 0x63, 0x61, 0x32, 0x30, 0x30,
    811 	0x32, 0x2f, 0x69, 0x70, 0x73, 0x63, 0x61, 0x32, 0x30, 0x30, 0x32, 0x43, 0x4c,
    812 	0x41, 0x53, 0x45, 0x41, 0x31, 0x2e, 0x63, 0x72, 0x6c, 0x30, 0x46, 0x06, 0x09,
    813 	0x60, 0x86, 0x48, 0x01, 0x86, 0xf8, 0x42, 0x01, 0x03, 0x04, 0x39, 0x16, 0x37,
    814 	0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x69,
    815 	0x70, 0x73, 0x63, 0x61, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x69, 0x70, 0x73, 0x63,
    816 	0x61, 0x32, 0x30, 0x30, 0x32, 0x2f, 0x72, 0x65, 0x76, 0x6f, 0x63, 0x61, 0x74,
    817 	0x69, 0x6f, 0x6e, 0x43, 0x4c, 0x41, 0x53, 0x45, 0x41, 0x31, 0x2e, 0x68, 0x74,
    818 	0x6d, 0x6c, 0x3f, 0x30, 0x43, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x86, 0xf8,
    819 	0x42, 0x01, 0x07, 0x04, 0x36, 0x16, 0x34, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a,
    820 	0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x69, 0x70, 0x73, 0x63, 0x61, 0x2e, 0x63,
    821 	0x6f, 0x6d, 0x2f, 0x69, 0x70, 0x73, 0x63, 0x61, 0x32, 0x30, 0x30, 0x32, 0x2f,
    822 	0x72, 0x65, 0x6e, 0x65, 0x77, 0x61, 0x6c, 0x43, 0x4c, 0x41, 0x53, 0x45, 0x41,
    823 	0x31, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x3f, 0x30, 0x41, 0x06, 0x09, 0x60, 0x86,
    824 	0x48, 0x01, 0x86, 0xf8, 0x42, 0x01, 0x08, 0x04, 0x34, 0x16, 0x32, 0x68, 0x74,
    825 	0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x69, 0x70, 0x73,
    826 	0x63, 0x61, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x69, 0x70, 0x73, 0x63, 0x61, 0x32,
    827 	0x30, 0x30, 0x32, 0x2f, 0x70, 0x6f, 0x6c, 0x69, 0x63, 0x79, 0x43, 0x4c, 0x41,
    828 	0x53, 0x45, 0x41, 0x31, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x30, 0x81, 0x83, 0x06,
    829 	0x03, 0x55, 0x1d, 0x1f, 0x04, 0x7c, 0x30, 0x7a, 0x30, 0x39, 0xa0, 0x37, 0xa0,
    830 	0x35, 0x86, 0x33, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77,
    831 	0x2e, 0x69, 0x70, 0x73, 0x63, 0x61, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x69, 0x70,
    832 	0x73, 0x63, 0x61, 0x32, 0x30, 0x30, 0x32, 0x2f, 0x69, 0x70, 0x73, 0x63, 0x61,
    833 	0x32, 0x30, 0x30, 0x32, 0x43, 0x4c, 0x41, 0x53, 0x45, 0x41, 0x31, 0x2e, 0x63,
    834 	0x72, 0x6c, 0x30, 0x3d, 0xa0, 0x3b, 0xa0, 0x39, 0x86, 0x37, 0x68, 0x74, 0x74,
    835 	0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x62, 0x61, 0x63, 0x6b, 0x2e, 0x69,
    836 	0x70, 0x73, 0x63, 0x61, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x69, 0x70, 0x73, 0x63,
    837 	0x61, 0x32, 0x30, 0x30, 0x32, 0x2f, 0x69, 0x70, 0x73, 0x63, 0x61, 0x32, 0x30,
    838 	0x30, 0x32, 0x43, 0x4c, 0x41, 0x53, 0x45, 0x41, 0x31, 0x2e, 0x63, 0x72, 0x6c,
    839 	0x30, 0x32, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x01, 0x01, 0x04,
    840 	0x26, 0x30, 0x24, 0x30, 0x22, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07,
    841 	0x30, 0x01, 0x86, 0x16, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x6f, 0x63,
    842 	0x73, 0x70, 0x2e, 0x69, 0x70, 0x73, 0x63, 0x61, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
    843 	0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05,
    844 	0x05, 0x00, 0x03, 0x81, 0x81, 0x00, 0x68, 0xee, 0x79, 0x97, 0x97, 0xdd, 0x3b,
    845 	0xef, 0x16, 0x6a, 0x06, 0xf2, 0x14, 0x9a, 0x6e, 0xcd, 0x9e, 0x12, 0xf7, 0xaa,
    846 	0x83, 0x10, 0xbd, 0xd1, 0x7c, 0x98, 0xfa, 0xc7, 0xae, 0xd4, 0x0e, 0x2c, 0x9e,
    847 	0x38, 0x05, 0x9d, 0x52, 0x60, 0xa9, 0x99, 0x0a, 0x81, 0xb4, 0x98, 0x90, 0x1d,
    848 	0xae, 0xbb, 0x4a, 0xd7, 0xb9, 0xdc, 0x88, 0x9e, 0x37, 0x78, 0x41, 0x5b, 0xf7,
    849 	0x82, 0xa5, 0xf2, 0xba, 0x41, 0x25, 0x5a, 0x90, 0x1a, 0x1e, 0x45, 0x38, 0xa1,
    850 	0x52, 0x58, 0x75, 0x94, 0x26, 0x44, 0xfb, 0x20, 0x07, 0xba, 0x44, 0xcc, 0xe5,
    851 	0x4a, 0x2d, 0x72, 0x3f, 0x98, 0x47, 0xf6, 0x26, 0xdc, 0x05, 0x46, 0x05, 0x07,
    852 	0x63, 0x21, 0xab, 0x46, 0x9b, 0x9c, 0x78, 0xd5, 0x54, 0x5b, 0x3d, 0x0c, 0x1e,
    853 	0xc8, 0x64, 0x8c, 0xb5, 0x50, 0x23, 0x82, 0x6f, 0xdb, 0xb8, 0x22, 0x1c, 0x43,
    854 	0x96, 0x07, 0xa8, 0xbb,
    855 }
    856 
    857 var stringSliceTestData = [][]string{
    858 	{"foo", "bar"},
    859 	{"foo", "\\bar"},
    860 	{"foo", "\"bar\""},
    861 	{"foo", ""},
    862 }
    863 
    864 func TestStringSlice(t *testing.T) {
    865 	for _, test := range stringSliceTestData {
    866 		bs, err := Marshal(test)
    867 		if err != nil {
    868 			t.Error(err)
    869 		}
    870 
    871 		var res []string
    872 		_, err = Unmarshal(bs, &res)
    873 		if err != nil {
    874 			t.Error(err)
    875 		}
    876 
    877 		if fmt.Sprintf("%v", res) != fmt.Sprintf("%v", test) {
    878 			t.Errorf("incorrect marshal/unmarshal; %v != %v", res, test)
    879 		}
    880 	}
    881 }
    882 
    883 type explicitTaggedTimeTest struct {
    884 	Time time.Time `asn1:"explicit,tag:0"`
    885 }
    886 
    887 var explicitTaggedTimeTestData = []struct {
    888 	in  []byte
    889 	out explicitTaggedTimeTest
    890 }{
    891 	{[]byte{0x30, 0x11, 0xa0, 0xf, 0x17, 0xd, '9', '1', '0', '5', '0', '6', '1', '6', '4', '5', '4', '0', 'Z'},
    892 		explicitTaggedTimeTest{time.Date(1991, 05, 06, 16, 45, 40, 0, time.UTC)}},
    893 	{[]byte{0x30, 0x17, 0xa0, 0xf, 0x18, 0x13, '2', '0', '1', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '+', '0', '6', '0', '7'},
    894 		explicitTaggedTimeTest{time.Date(2010, 01, 02, 03, 04, 05, 0, time.FixedZone("", 6*60*60+7*60))}},
    895 }
    896 
    897 func TestExplicitTaggedTime(t *testing.T) {
    898 	// Test that a time.Time will match either tagUTCTime or
    899 	// tagGeneralizedTime.
    900 	for i, test := range explicitTaggedTimeTestData {
    901 		var got explicitTaggedTimeTest
    902 		_, err := Unmarshal(test.in, &got)
    903 		if err != nil {
    904 			t.Errorf("Unmarshal failed at index %d %v", i, err)
    905 		}
    906 		if !got.Time.Equal(test.out.Time) {
    907 			t.Errorf("#%d: got %v, want %v", i, got.Time, test.out.Time)
    908 		}
    909 	}
    910 }
    911 
    912 type implicitTaggedTimeTest struct {
    913 	Time time.Time `asn1:"tag:24"`
    914 }
    915 
    916 func TestImplicitTaggedTime(t *testing.T) {
    917 	// An implicitly tagged time value, that happens to have an implicit
    918 	// tag equal to a GENERALIZEDTIME, should still be parsed as a UTCTime.
    919 	// (There's no "timeType" in fieldParameters to determine what type of
    920 	// time should be expected when implicitly tagged.)
    921 	der := []byte{0x30, 0x0f, 0x80 | 24, 0xd, '9', '1', '0', '5', '0', '6', '1', '6', '4', '5', '4', '0', 'Z'}
    922 	var result implicitTaggedTimeTest
    923 	if _, err := Unmarshal(der, &result); err != nil {
    924 		t.Fatalf("Error while parsing: %s", err)
    925 	}
    926 	if expected := time.Date(1991, 05, 06, 16, 45, 40, 0, time.UTC); !result.Time.Equal(expected) {
    927 		t.Errorf("Wrong result. Got %v, want %v", result.Time, expected)
    928 	}
    929 }
    930 
    931 type truncatedExplicitTagTest struct {
    932 	Test int `asn1:"explicit,tag:0"`
    933 }
    934 
    935 func TestTruncatedExplicitTag(t *testing.T) {
    936 	// This crashed Unmarshal in the past. See #11154.
    937 	der := []byte{
    938 		0x30, // SEQUENCE
    939 		0x02, // two bytes long
    940 		0xa0, // context-specific, tag 0
    941 		0x30, // 48 bytes long
    942 	}
    943 
    944 	var result truncatedExplicitTagTest
    945 	if _, err := Unmarshal(der, &result); err == nil {
    946 		t.Error("Unmarshal returned without error")
    947 	}
    948 }
    949 
    950 type invalidUTF8Test struct {
    951 	Str string `asn1:"utf8"`
    952 }
    953 
    954 func TestUnmarshalInvalidUTF8(t *testing.T) {
    955 	data := []byte("0\x05\f\x03a\xc9c")
    956 	var result invalidUTF8Test
    957 	_, err := Unmarshal(data, &result)
    958 
    959 	const expectedSubstring = "UTF"
    960 	if err == nil {
    961 		t.Fatal("Successfully unmarshaled invalid UTF-8 data")
    962 	} else if !strings.Contains(err.Error(), expectedSubstring) {
    963 		t.Fatalf("Expected error to mention %q but error was %q", expectedSubstring, err.Error())
    964 	}
    965 }
    966 
    967 func TestMarshalNilValue(t *testing.T) {
    968 	nilValueTestData := []interface{}{
    969 		nil,
    970 		struct{ V interface{} }{},
    971 	}
    972 	for i, test := range nilValueTestData {
    973 		if _, err := Marshal(test); err == nil {
    974 			t.Fatalf("#%d: successfully marshaled nil value", i)
    975 		}
    976 	}
    977 }
    978 
    979 type unexported struct {
    980 	X int
    981 	y int
    982 }
    983 
    984 type exported struct {
    985 	X int
    986 	Y int
    987 }
    988 
    989 func TestUnexportedStructField(t *testing.T) {
    990 	want := StructuralError{"struct contains unexported fields"}
    991 
    992 	_, err := Marshal(unexported{X: 5, y: 1})
    993 	if err != want {
    994 		t.Errorf("got %v, want %v", err, want)
    995 	}
    996 
    997 	bs, err := Marshal(exported{X: 5, Y: 1})
    998 	if err != nil {
    999 		t.Fatal(err)
   1000 	}
   1001 	var u unexported
   1002 	_, err = Unmarshal(bs, &u)
   1003 	if err != want {
   1004 		t.Errorf("got %v, want %v", err, want)
   1005 	}
   1006 }
   1007