Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / third_party / boringssl / src / crypto / bytestring / bytestring_test.c
1 /* Copyright (c) 2014, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 #include <openssl/crypto.h>
19 #include <openssl/bytestring.h>
20
21 #include "internal.h"
22 #include "../internal.h"
23
24
25 static int test_skip(void) {
26   static const uint8_t kData[] = {1, 2, 3};
27   CBS data;
28
29   CBS_init(&data, kData, sizeof(kData));
30   return CBS_len(&data) == 3 &&
31       CBS_skip(&data, 1) &&
32       CBS_len(&data) == 2 &&
33       CBS_skip(&data, 2) &&
34       CBS_len(&data) == 0 &&
35       !CBS_skip(&data, 1);
36 }
37
38 static int test_get_u(void) {
39   static const uint8_t kData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
40   uint8_t u8;
41   uint16_t u16;
42   uint32_t u32;
43   CBS data;
44
45   CBS_init(&data, kData, sizeof(kData));
46   return CBS_get_u8(&data, &u8) &&
47     u8 == 1 &&
48     CBS_get_u16(&data, &u16) &&
49     u16 == 0x203 &&
50     CBS_get_u24(&data, &u32) &&
51     u32 == 0x40506 &&
52     CBS_get_u32(&data, &u32) &&
53     u32 == 0x708090a &&
54     !CBS_get_u8(&data, &u8);
55 }
56
57 static int test_get_prefixed(void) {
58   static const uint8_t kData[] = {1, 2, 0, 2, 3, 4, 0, 0, 3, 3, 2, 1};
59   uint8_t u8;
60   uint16_t u16;
61   uint32_t u32;
62   CBS data, prefixed;
63
64   CBS_init(&data, kData, sizeof(kData));
65   return CBS_get_u8_length_prefixed(&data, &prefixed) &&
66     CBS_len(&prefixed) == 1 &&
67     CBS_get_u8(&prefixed, &u8) &&
68     u8 == 2 &&
69     CBS_get_u16_length_prefixed(&data, &prefixed) &&
70     CBS_len(&prefixed) == 2 &&
71     CBS_get_u16(&prefixed, &u16) &&
72     u16 == 0x304 &&
73     CBS_get_u24_length_prefixed(&data, &prefixed) &&
74     CBS_len(&prefixed) == 3 &&
75     CBS_get_u24(&prefixed, &u32) &&
76     u32 == 0x30201;
77 }
78
79 static int test_get_prefixed_bad(void) {
80   static const uint8_t kData1[] = {2, 1};
81   static const uint8_t kData2[] = {0, 2, 1};
82   static const uint8_t kData3[] = {0, 0, 2, 1};
83   CBS data, prefixed;
84
85   CBS_init(&data, kData1, sizeof(kData1));
86   if (CBS_get_u8_length_prefixed(&data, &prefixed)) {
87     return 0;
88   }
89
90   CBS_init(&data, kData2, sizeof(kData2));
91   if (CBS_get_u16_length_prefixed(&data, &prefixed)) {
92     return 0;
93   }
94
95   CBS_init(&data, kData3, sizeof(kData3));
96   if (CBS_get_u24_length_prefixed(&data, &prefixed)) {
97     return 0;
98   }
99
100   return 1;
101 }
102
103 static int test_get_asn1(void) {
104   static const uint8_t kData1[] = {0x30, 2, 1, 2};
105   static const uint8_t kData2[] = {0x30, 3, 1, 2};
106   static const uint8_t kData3[] = {0x30, 0x80};
107   static const uint8_t kData4[] = {0x30, 0x81, 1, 1};
108   static const uint8_t kData5[] = {0x30, 0x82, 0, 1, 1};
109   static const uint8_t kData6[] = {0xa1, 3, 0x4, 1, 1};
110   static const uint8_t kData7[] = {0xa1, 3, 0x4, 2, 1};
111   static const uint8_t kData8[] = {0xa1, 3, 0x2, 1, 1};
112   static const uint8_t kData9[] = {0xa1, 3, 0x2, 1, 0xff};
113
114   CBS data, contents;
115   int present;
116   uint64_t value;
117
118   CBS_init(&data, kData1, sizeof(kData1));
119   if (CBS_peek_asn1_tag(&data, 0x1) ||
120       !CBS_peek_asn1_tag(&data, 0x30)) {
121     return 0;
122   }
123   if (!CBS_get_asn1(&data, &contents, 0x30) ||
124       CBS_len(&contents) != 2 ||
125       memcmp(CBS_data(&contents), "\x01\x02", 2) != 0) {
126     return 0;
127   }
128
129   CBS_init(&data, kData2, sizeof(kData2));
130   /* data is truncated */
131   if (CBS_get_asn1(&data, &contents, 0x30)) {
132     return 0;
133   }
134
135   CBS_init(&data, kData3, sizeof(kData3));
136   /* zero byte length of length */
137   if (CBS_get_asn1(&data, &contents, 0x30)) {
138     return 0;
139   }
140
141   CBS_init(&data, kData4, sizeof(kData4));
142   /* long form mistakenly used. */
143   if (CBS_get_asn1(&data, &contents, 0x30)) {
144     return 0;
145   }
146
147   CBS_init(&data, kData5, sizeof(kData5));
148   /* length takes too many bytes. */
149   if (CBS_get_asn1(&data, &contents, 0x30)) {
150     return 0;
151   }
152
153   CBS_init(&data, kData1, sizeof(kData1));
154   /* wrong tag. */
155   if (CBS_get_asn1(&data, &contents, 0x31)) {
156     return 0;
157   }
158
159   CBS_init(&data, NULL, 0);
160   /* peek at empty data. */
161   if (CBS_peek_asn1_tag(&data, 0x30)) {
162     return 0;
163   }
164
165   CBS_init(&data, NULL, 0);
166   /* optional elements at empty data. */
167   if (!CBS_get_optional_asn1(&data, &contents, &present, 0xa0) ||
168       present ||
169       !CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa0) ||
170       present ||
171       CBS_len(&contents) != 0 ||
172       !CBS_get_optional_asn1_octet_string(&data, &contents, NULL, 0xa0) ||
173       CBS_len(&contents) != 0 ||
174       !CBS_get_optional_asn1_uint64(&data, &value, 0xa0, 42) ||
175       value != 42) {
176     return 0;
177   }
178
179   CBS_init(&data, kData6, sizeof(kData6));
180   /* optional element. */
181   if (!CBS_get_optional_asn1(&data, &contents, &present, 0xa0) ||
182       present ||
183       !CBS_get_optional_asn1(&data, &contents, &present, 0xa1) ||
184       !present ||
185       CBS_len(&contents) != 3 ||
186       memcmp(CBS_data(&contents), "\x04\x01\x01", 3) != 0) {
187     return 0;
188   }
189
190   CBS_init(&data, kData6, sizeof(kData6));
191   /* optional octet string. */
192   if (!CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa0) ||
193       present ||
194       CBS_len(&contents) != 0 ||
195       !CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa1) ||
196       !present ||
197       CBS_len(&contents) != 1 ||
198       CBS_data(&contents)[0] != 1) {
199     return 0;
200   }
201
202   CBS_init(&data, kData7, sizeof(kData7));
203   /* invalid optional octet string. */
204   if (CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa1)) {
205     return 0;
206   }
207
208   CBS_init(&data, kData8, sizeof(kData8));
209   /* optional octet string. */
210   if (!CBS_get_optional_asn1_uint64(&data, &value, 0xa0, 42) ||
211       value != 42 ||
212       !CBS_get_optional_asn1_uint64(&data, &value, 0xa1, 42) ||
213       value != 1) {
214     return 0;
215   }
216
217   CBS_init(&data, kData9, sizeof(kData9));
218   /* invalid optional integer. */
219   if (CBS_get_optional_asn1_uint64(&data, &value, 0xa1, 42)) {
220     return 0;
221   }
222
223   return 1;
224 }
225
226 static int test_get_optional_asn1_bool(void) {
227   CBS data;
228   int val;
229
230   static const uint8_t kTrue[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0xff};
231   static const uint8_t kFalse[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0x00};
232   static const uint8_t kInvalid[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0x01};
233
234   CBS_init(&data, NULL, 0);
235   val = 2;
236   if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0) ||
237       val != 0) {
238     return 0;
239   }
240
241   CBS_init(&data, kTrue, sizeof(kTrue));
242   val = 2;
243   if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0) ||
244       val != 1) {
245     return 0;
246   }
247
248   CBS_init(&data, kFalse, sizeof(kFalse));
249   val = 2;
250   if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1) ||
251       val != 0) {
252     return 0;
253   }
254
255   CBS_init(&data, kInvalid, sizeof(kInvalid));
256   if (CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1)) {
257     return 0;
258   }
259
260   return 1;
261 }
262
263 static int test_cbb_basic(void) {
264   static const uint8_t kExpected[] = {1, 2, 3, 4, 5, 6, 7, 8};
265   uint8_t *buf;
266   size_t buf_len;
267   int ok;
268   CBB cbb;
269
270   if (!CBB_init(&cbb, 100)) {
271     return 0;
272   }
273   CBB_cleanup(&cbb);
274
275   if (!CBB_init(&cbb, 0) ||
276       !CBB_add_u8(&cbb, 1) ||
277       !CBB_add_u16(&cbb, 0x203) ||
278       !CBB_add_u24(&cbb, 0x40506) ||
279       !CBB_add_bytes(&cbb, (const uint8_t*) "\x07\x08", 2) ||
280       !CBB_finish(&cbb, &buf, &buf_len)) {
281     return 0;
282   }
283
284   ok = buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) == 0;
285   free(buf);
286   return ok;
287 }
288
289 static int test_cbb_fixed(void) {
290   CBB cbb;
291   uint8_t buf[1];
292   uint8_t *out_buf;
293   size_t out_size;
294
295   if (!CBB_init_fixed(&cbb, NULL, 0) ||
296       CBB_add_u8(&cbb, 1) ||
297       !CBB_finish(&cbb, &out_buf, &out_size) ||
298       out_buf != NULL ||
299       out_size != 0) {
300     return 0;
301   }
302
303   if (!CBB_init_fixed(&cbb, buf, 1) ||
304       !CBB_add_u8(&cbb, 1) ||
305       CBB_add_u8(&cbb, 2) ||
306       !CBB_finish(&cbb, &out_buf, &out_size) ||
307       out_buf != buf ||
308       out_size != 1 ||
309       buf[0] != 1) {
310     return 0;
311   }
312
313   return 1;
314 }
315
316 static int test_cbb_finish_child(void) {
317   CBB cbb, child;
318   uint8_t *out_buf;
319   size_t out_size;
320
321   if (!CBB_init(&cbb, 16) ||
322       !CBB_add_u8_length_prefixed(&cbb, &child) ||
323       CBB_finish(&child, &out_buf, &out_size) ||
324       !CBB_finish(&cbb, &out_buf, &out_size) ||
325       out_size != 1 ||
326       out_buf[0] != 0) {
327     return 0;
328   }
329
330   free(out_buf);
331   return 1;
332 }
333
334 static int test_cbb_prefixed(void) {
335   static const uint8_t kExpected[] = {0, 1, 1, 0, 2, 2, 3, 0, 0, 3,
336                                       4, 5, 6, 5, 4, 1, 0, 1, 2};
337   uint8_t *buf;
338   size_t buf_len;
339   CBB cbb, contents, inner_contents, inner_inner_contents;
340   int ok;
341
342   if (!CBB_init(&cbb, 0) ||
343       !CBB_add_u8_length_prefixed(&cbb, &contents) ||
344       !CBB_add_u8_length_prefixed(&cbb, &contents) ||
345       !CBB_add_u8(&contents, 1) ||
346       !CBB_add_u16_length_prefixed(&cbb, &contents) ||
347       !CBB_add_u16(&contents, 0x203) ||
348       !CBB_add_u24_length_prefixed(&cbb, &contents) ||
349       !CBB_add_u24(&contents, 0x40506) ||
350       !CBB_add_u8_length_prefixed(&cbb, &contents) ||
351       !CBB_add_u8_length_prefixed(&contents, &inner_contents) ||
352       !CBB_add_u8(&inner_contents, 1) ||
353       !CBB_add_u16_length_prefixed(&inner_contents, &inner_inner_contents) ||
354       !CBB_add_u8(&inner_inner_contents, 2) ||
355       !CBB_finish(&cbb, &buf, &buf_len)) {
356     return 0;
357   }
358
359   ok = buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) == 0;
360   free(buf);
361   return ok;
362 }
363
364 static int test_cbb_misuse(void) {
365   CBB cbb, child, contents;
366   uint8_t *buf;
367   size_t buf_len;
368
369   if (!CBB_init(&cbb, 0) ||
370       !CBB_add_u8_length_prefixed(&cbb, &child) ||
371       !CBB_add_u8(&child, 1) ||
372       !CBB_add_u8(&cbb, 2)) {
373     return 0;
374   }
375
376   /* Since we wrote to |cbb|, |child| is now invalid and attempts to write to
377    * it should fail. */
378   if (CBB_add_u8(&child, 1) ||
379       CBB_add_u16(&child, 1) ||
380       CBB_add_u24(&child, 1) ||
381       CBB_add_u8_length_prefixed(&child, &contents) ||
382       CBB_add_u16_length_prefixed(&child, &contents) ||
383       CBB_add_asn1(&child, &contents, 1) ||
384       CBB_add_bytes(&child, (const uint8_t*) "a", 1)) {
385     fprintf(stderr, "CBB operation on invalid CBB did not fail.\n");
386     return 0;
387   }
388
389   if (!CBB_finish(&cbb, &buf, &buf_len) ||
390       buf_len != 3 ||
391       memcmp(buf, "\x01\x01\x02", 3) != 0) {
392     return 0;
393   }
394
395   free(buf);
396
397   return 1;
398 }
399
400 static int test_cbb_asn1(void) {
401   static const uint8_t kExpected[] = {0x30, 3, 1, 2, 3};
402   uint8_t *buf, *test_data;
403   size_t buf_len;
404   CBB cbb, contents, inner_contents;
405
406   if (!CBB_init(&cbb, 0) ||
407       !CBB_add_asn1(&cbb, &contents, 0x30) ||
408       !CBB_add_bytes(&contents, (const uint8_t*) "\x01\x02\x03", 3) ||
409       !CBB_finish(&cbb, &buf, &buf_len)) {
410     return 0;
411   }
412
413   if (buf_len != sizeof(kExpected) || memcmp(buf, kExpected, buf_len) != 0) {
414     return 0;
415   }
416   free(buf);
417
418   test_data = malloc(100000);
419   memset(test_data, 0x42, 100000);
420
421   if (!CBB_init(&cbb, 0) ||
422       !CBB_add_asn1(&cbb, &contents, 0x30) ||
423       !CBB_add_bytes(&contents, test_data, 130) ||
424       !CBB_finish(&cbb, &buf, &buf_len)) {
425     return 0;
426   }
427
428   if (buf_len != 3 + 130 ||
429       memcmp(buf, "\x30\x81\x82", 3) != 0 ||
430       memcmp(buf + 3, test_data, 130) != 0) {
431     return 0;
432   }
433   free(buf);
434
435   if (!CBB_init(&cbb, 0) ||
436       !CBB_add_asn1(&cbb, &contents, 0x30) ||
437       !CBB_add_bytes(&contents, test_data, 1000) ||
438       !CBB_finish(&cbb, &buf, &buf_len)) {
439     return 0;
440   }
441
442   if (buf_len != 4 + 1000 ||
443       memcmp(buf, "\x30\x82\x03\xe8", 4) != 0 ||
444       memcmp(buf + 4, test_data, 1000)) {
445     return 0;
446   }
447   free(buf);
448
449   if (!CBB_init(&cbb, 0) ||
450       !CBB_add_asn1(&cbb, &contents, 0x30) ||
451       !CBB_add_asn1(&contents, &inner_contents, 0x30) ||
452       !CBB_add_bytes(&inner_contents, test_data, 100000) ||
453       !CBB_finish(&cbb, &buf, &buf_len)) {
454     return 0;
455   }
456
457   if (buf_len != 5 + 5 + 100000 ||
458       memcmp(buf, "\x30\x83\x01\x86\xa5\x30\x83\x01\x86\xa0", 10) != 0 ||
459       memcmp(buf + 10, test_data, 100000)) {
460     return 0;
461   }
462   free(buf);
463
464   free(test_data);
465   return 1;
466 }
467
468 static int do_ber_convert(const char *name,
469                           const uint8_t *der_expected, size_t der_len,
470                           const uint8_t *ber, size_t ber_len) {
471   CBS in;
472   uint8_t *out;
473   size_t out_len;
474
475   CBS_init(&in, ber, ber_len);
476   if (!CBS_asn1_ber_to_der(&in, &out, &out_len)) {
477     fprintf(stderr, "%s: CBS_asn1_ber_to_der failed.\n", name);
478     return 0;
479   }
480
481   if (out == NULL) {
482     if (ber_len != der_len ||
483         memcmp(der_expected, ber, ber_len) != 0) {
484       fprintf(stderr, "%s: incorrect unconverted result.\n", name);
485       return 0;
486     }
487
488     return 1;
489   }
490
491   if (out_len != der_len ||
492       memcmp(out, der_expected, der_len) != 0) {
493     fprintf(stderr, "%s: incorrect converted result.\n", name);
494     return 0;
495   }
496
497   free(out);
498   return 1;
499 }
500
501 static int test_ber_convert(void) {
502   static const uint8_t kSimpleBER[] = {0x01, 0x01, 0x00};
503
504   /* kIndefBER contains a SEQUENCE with an indefinite length. */
505   static const uint8_t kIndefBER[] = {0x30, 0x80, 0x01, 0x01, 0x02, 0x00, 0x00};
506   static const uint8_t kIndefDER[] = {0x30, 0x03, 0x01, 0x01, 0x02};
507
508   /* kOctetStringBER contains an indefinite length OCTETSTRING with two parts.
509    * These parts need to be concatenated in DER form. */
510   static const uint8_t kOctetStringBER[] = {0x24, 0x80, 0x04, 0x02, 0,    1,
511                                             0x04, 0x02, 2,    3,    0x00, 0x00};
512   static const uint8_t kOctetStringDER[] = {0x04, 0x04, 0, 1, 2, 3};
513
514   /* kNSSBER is part of a PKCS#12 message generated by NSS that uses indefinite
515    * length elements extensively. */
516   static const uint8_t kNSSBER[] = {
517       0x30, 0x80, 0x02, 0x01, 0x03, 0x30, 0x80, 0x06, 0x09, 0x2a, 0x86, 0x48,
518       0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x80, 0x24, 0x80, 0x04, 0x04,
519       0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x39,
520       0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
521       0x00, 0x04, 0x14, 0x84, 0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90,
522       0xc1, 0xb6, 0xe8, 0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04,
523       0x10, 0x38, 0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b,
524       0xf0, 0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0, 0x00, 0x00,
525   };
526
527   static const uint8_t kNSSDER[] = {
528       0x30, 0x53, 0x02, 0x01, 0x03, 0x30, 0x13, 0x06, 0x09, 0x2a, 0x86,
529       0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x06, 0x04, 0x04,
530       0x01, 0x02, 0x03, 0x04, 0x30, 0x39, 0x30, 0x21, 0x30, 0x09, 0x06,
531       0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x84,
532       0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90, 0xc1, 0xb6, 0xe8,
533       0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04, 0x10, 0x38,
534       0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b, 0xf0,
535       0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0,
536   };
537
538   return do_ber_convert("kSimpleBER", kSimpleBER, sizeof(kSimpleBER),
539                         kSimpleBER, sizeof(kSimpleBER)) &&
540          do_ber_convert("kIndefBER", kIndefDER, sizeof(kIndefDER), kIndefBER,
541                         sizeof(kIndefBER)) &&
542          do_ber_convert("kOctetStringBER", kOctetStringDER,
543                         sizeof(kOctetStringDER), kOctetStringBER,
544                         sizeof(kOctetStringBER)) &&
545          do_ber_convert("kNSSBER", kNSSDER, sizeof(kNSSDER), kNSSBER,
546                         sizeof(kNSSBER));
547 }
548
549 typedef struct {
550   uint64_t value;
551   const char *encoding;
552   size_t encoding_len;
553 } ASN1_UINT64_TEST;
554
555 static const ASN1_UINT64_TEST kAsn1Uint64Tests[] = {
556   {0, "\x02\x01\x00", 3},
557   {1, "\x02\x01\x01", 3},
558   {127, "\x02\x01\x7f", 3},
559   {128, "\x02\x02\x00\x80", 4},
560   {0xdeadbeef, "\x02\x05\x00\xde\xad\xbe\xef", 7},
561   {OPENSSL_U64(0x0102030405060708),
562    "\x02\x08\x01\x02\x03\x04\x05\x06\x07\x08", 10},
563   {OPENSSL_U64(0xffffffffffffffff),
564     "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff", 11},
565 };
566
567 typedef struct {
568   const char *encoding;
569   size_t encoding_len;
570 } ASN1_INVALID_UINT64_TEST;
571
572 static const ASN1_INVALID_UINT64_TEST kAsn1InvalidUint64Tests[] = {
573   /* Bad tag. */
574   {"\x03\x01\x00", 3},
575   /* Empty contents. */
576   {"\x02\x00", 2},
577   /* Negative number. */
578   {"\x02\x01\x80", 3},
579   /* Overflow */
580   {"\x02\x09\x01\x00\x00\x00\x00\x00\x00\x00\x00", 11},
581 };
582
583 static int test_asn1_uint64(void) {
584   size_t i;
585
586   for (i = 0; i < sizeof(kAsn1Uint64Tests) / sizeof(kAsn1Uint64Tests[0]); i++) {
587     const ASN1_UINT64_TEST *test = &kAsn1Uint64Tests[i];
588     CBS cbs;
589     uint64_t value;
590     CBB cbb;
591     uint8_t *out;
592     size_t len;
593
594     CBS_init(&cbs, (const uint8_t *)test->encoding, test->encoding_len);
595     if (!CBS_get_asn1_uint64(&cbs, &value) ||
596         CBS_len(&cbs) != 0 ||
597         value != test->value) {
598       return 0;
599     }
600
601     if (!CBB_init(&cbb, 0)) {
602       return 0;
603     }
604     if (!CBB_add_asn1_uint64(&cbb, test->value) ||
605         !CBB_finish(&cbb, &out, &len)) {
606       CBB_cleanup(&cbb);
607       return 0;
608     }
609     if (len != test->encoding_len || memcmp(out, test->encoding, len) != 0) {
610       free(out);
611       return 0;
612     }
613     free(out);
614   }
615
616   for (i = 0;
617        i < sizeof(kAsn1InvalidUint64Tests) / sizeof(kAsn1InvalidUint64Tests[0]);
618        i++) {
619     const ASN1_INVALID_UINT64_TEST *test = &kAsn1InvalidUint64Tests[i];
620     CBS cbs;
621     uint64_t value;
622
623     CBS_init(&cbs, (const uint8_t *)test->encoding, test->encoding_len);
624     if (CBS_get_asn1_uint64(&cbs, &value)) {
625       return 0;
626     }
627   }
628
629   return 1;
630 }
631
632 int main(void) {
633   CRYPTO_library_init();
634
635   if (!test_skip() ||
636       !test_get_u() ||
637       !test_get_prefixed() ||
638       !test_get_prefixed_bad() ||
639       !test_get_asn1() ||
640       !test_cbb_basic() ||
641       !test_cbb_fixed() ||
642       !test_cbb_finish_child() ||
643       !test_cbb_misuse() ||
644       !test_cbb_prefixed() ||
645       !test_cbb_asn1() ||
646       !test_ber_convert() ||
647       !test_asn1_uint64() ||
648       !test_get_optional_asn1_bool()) {
649     return 1;
650   }
651
652   printf("PASS\n");
653   return 0;
654 }