asn1_test.go 38 KB

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