You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

675 regels
19 KiB

  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. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <vector>
  18. #include <openssl/crypto.h>
  19. #include <openssl/bytestring.h>
  20. #include "internal.h"
  21. #include "../internal.h"
  22. #include "../test/scoped_types.h"
  23. static bool TestSkip() {
  24. static const uint8_t kData[] = {1, 2, 3};
  25. CBS data;
  26. CBS_init(&data, kData, sizeof(kData));
  27. return CBS_len(&data) == 3 &&
  28. CBS_skip(&data, 1) &&
  29. CBS_len(&data) == 2 &&
  30. CBS_skip(&data, 2) &&
  31. CBS_len(&data) == 0 &&
  32. !CBS_skip(&data, 1);
  33. }
  34. static bool TestGetUint() {
  35. static const uint8_t kData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  36. uint8_t u8;
  37. uint16_t u16;
  38. uint32_t u32;
  39. CBS data;
  40. CBS_init(&data, kData, sizeof(kData));
  41. return CBS_get_u8(&data, &u8) &&
  42. u8 == 1 &&
  43. CBS_get_u16(&data, &u16) &&
  44. u16 == 0x203 &&
  45. CBS_get_u24(&data, &u32) &&
  46. u32 == 0x40506 &&
  47. CBS_get_u32(&data, &u32) &&
  48. u32 == 0x708090a &&
  49. !CBS_get_u8(&data, &u8);
  50. }
  51. static bool TestGetPrefixed() {
  52. static const uint8_t kData[] = {1, 2, 0, 2, 3, 4, 0, 0, 3, 3, 2, 1};
  53. uint8_t u8;
  54. uint16_t u16;
  55. uint32_t u32;
  56. CBS data, prefixed;
  57. CBS_init(&data, kData, sizeof(kData));
  58. return CBS_get_u8_length_prefixed(&data, &prefixed) &&
  59. CBS_len(&prefixed) == 1 &&
  60. CBS_get_u8(&prefixed, &u8) &&
  61. u8 == 2 &&
  62. CBS_get_u16_length_prefixed(&data, &prefixed) &&
  63. CBS_len(&prefixed) == 2 &&
  64. CBS_get_u16(&prefixed, &u16) &&
  65. u16 == 0x304 &&
  66. CBS_get_u24_length_prefixed(&data, &prefixed) &&
  67. CBS_len(&prefixed) == 3 &&
  68. CBS_get_u24(&prefixed, &u32) &&
  69. u32 == 0x30201;
  70. }
  71. static bool TestGetPrefixedBad() {
  72. static const uint8_t kData1[] = {2, 1};
  73. static const uint8_t kData2[] = {0, 2, 1};
  74. static const uint8_t kData3[] = {0, 0, 2, 1};
  75. CBS data, prefixed;
  76. CBS_init(&data, kData1, sizeof(kData1));
  77. if (CBS_get_u8_length_prefixed(&data, &prefixed)) {
  78. return false;
  79. }
  80. CBS_init(&data, kData2, sizeof(kData2));
  81. if (CBS_get_u16_length_prefixed(&data, &prefixed)) {
  82. return false;
  83. }
  84. CBS_init(&data, kData3, sizeof(kData3));
  85. if (CBS_get_u24_length_prefixed(&data, &prefixed)) {
  86. return false;
  87. }
  88. return true;
  89. }
  90. static bool TestGetASN1() {
  91. static const uint8_t kData1[] = {0x30, 2, 1, 2};
  92. static const uint8_t kData2[] = {0x30, 3, 1, 2};
  93. static const uint8_t kData3[] = {0x30, 0x80};
  94. static const uint8_t kData4[] = {0x30, 0x81, 1, 1};
  95. static const uint8_t kData5[] = {0x30, 0x82, 0, 1, 1};
  96. static const uint8_t kData6[] = {0xa1, 3, 0x4, 1, 1};
  97. static const uint8_t kData7[] = {0xa1, 3, 0x4, 2, 1};
  98. static const uint8_t kData8[] = {0xa1, 3, 0x2, 1, 1};
  99. static const uint8_t kData9[] = {0xa1, 3, 0x2, 1, 0xff};
  100. CBS data, contents;
  101. int present;
  102. uint64_t value;
  103. CBS_init(&data, kData1, sizeof(kData1));
  104. if (CBS_peek_asn1_tag(&data, 0x1) ||
  105. !CBS_peek_asn1_tag(&data, 0x30)) {
  106. return false;
  107. }
  108. if (!CBS_get_asn1(&data, &contents, 0x30) ||
  109. CBS_len(&contents) != 2 ||
  110. memcmp(CBS_data(&contents), "\x01\x02", 2) != 0) {
  111. return false;
  112. }
  113. CBS_init(&data, kData2, sizeof(kData2));
  114. // data is truncated
  115. if (CBS_get_asn1(&data, &contents, 0x30)) {
  116. return false;
  117. }
  118. CBS_init(&data, kData3, sizeof(kData3));
  119. // zero byte length of length
  120. if (CBS_get_asn1(&data, &contents, 0x30)) {
  121. return false;
  122. }
  123. CBS_init(&data, kData4, sizeof(kData4));
  124. // long form mistakenly used.
  125. if (CBS_get_asn1(&data, &contents, 0x30)) {
  126. return false;
  127. }
  128. CBS_init(&data, kData5, sizeof(kData5));
  129. // length takes too many bytes.
  130. if (CBS_get_asn1(&data, &contents, 0x30)) {
  131. return false;
  132. }
  133. CBS_init(&data, kData1, sizeof(kData1));
  134. // wrong tag.
  135. if (CBS_get_asn1(&data, &contents, 0x31)) {
  136. return false;
  137. }
  138. CBS_init(&data, NULL, 0);
  139. // peek at empty data.
  140. if (CBS_peek_asn1_tag(&data, 0x30)) {
  141. return false;
  142. }
  143. CBS_init(&data, NULL, 0);
  144. // optional elements at empty data.
  145. if (!CBS_get_optional_asn1(&data, &contents, &present, 0xa0) ||
  146. present ||
  147. !CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa0) ||
  148. present ||
  149. CBS_len(&contents) != 0 ||
  150. !CBS_get_optional_asn1_octet_string(&data, &contents, NULL, 0xa0) ||
  151. CBS_len(&contents) != 0 ||
  152. !CBS_get_optional_asn1_uint64(&data, &value, 0xa0, 42) ||
  153. value != 42) {
  154. return false;
  155. }
  156. CBS_init(&data, kData6, sizeof(kData6));
  157. // optional element.
  158. if (!CBS_get_optional_asn1(&data, &contents, &present, 0xa0) ||
  159. present ||
  160. !CBS_get_optional_asn1(&data, &contents, &present, 0xa1) ||
  161. !present ||
  162. CBS_len(&contents) != 3 ||
  163. memcmp(CBS_data(&contents), "\x04\x01\x01", 3) != 0) {
  164. return false;
  165. }
  166. CBS_init(&data, kData6, sizeof(kData6));
  167. // optional octet string.
  168. if (!CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa0) ||
  169. present ||
  170. CBS_len(&contents) != 0 ||
  171. !CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa1) ||
  172. !present ||
  173. CBS_len(&contents) != 1 ||
  174. CBS_data(&contents)[0] != 1) {
  175. return false;
  176. }
  177. CBS_init(&data, kData7, sizeof(kData7));
  178. // invalid optional octet string.
  179. if (CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa1)) {
  180. return false;
  181. }
  182. CBS_init(&data, kData8, sizeof(kData8));
  183. // optional octet string.
  184. if (!CBS_get_optional_asn1_uint64(&data, &value, 0xa0, 42) ||
  185. value != 42 ||
  186. !CBS_get_optional_asn1_uint64(&data, &value, 0xa1, 42) ||
  187. value != 1) {
  188. return false;
  189. }
  190. CBS_init(&data, kData9, sizeof(kData9));
  191. // invalid optional integer.
  192. if (CBS_get_optional_asn1_uint64(&data, &value, 0xa1, 42)) {
  193. return false;
  194. }
  195. return true;
  196. }
  197. static bool TestGetOptionalASN1Bool() {
  198. static const uint8_t kTrue[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0xff};
  199. static const uint8_t kFalse[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0x00};
  200. static const uint8_t kInvalid[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0x01};
  201. CBS data;
  202. CBS_init(&data, NULL, 0);
  203. int val = 2;
  204. if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0) ||
  205. val != 0) {
  206. return false;
  207. }
  208. CBS_init(&data, kTrue, sizeof(kTrue));
  209. val = 2;
  210. if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0) ||
  211. val != 1) {
  212. return false;
  213. }
  214. CBS_init(&data, kFalse, sizeof(kFalse));
  215. val = 2;
  216. if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1) ||
  217. val != 0) {
  218. return false;
  219. }
  220. CBS_init(&data, kInvalid, sizeof(kInvalid));
  221. if (CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1)) {
  222. return false;
  223. }
  224. return true;
  225. }
  226. static bool TestCBBBasic() {
  227. static const uint8_t kExpected[] = {1, 2, 3, 4, 5, 6, 7, 8};
  228. uint8_t *buf;
  229. size_t buf_len;
  230. CBB cbb;
  231. if (!CBB_init(&cbb, 100)) {
  232. return false;
  233. }
  234. CBB_cleanup(&cbb);
  235. if (!CBB_init(&cbb, 0)) {
  236. return false;
  237. }
  238. if (!CBB_add_u8(&cbb, 1) ||
  239. !CBB_add_u16(&cbb, 0x203) ||
  240. !CBB_add_u24(&cbb, 0x40506) ||
  241. !CBB_add_bytes(&cbb, (const uint8_t*) "\x07\x08", 2) ||
  242. !CBB_finish(&cbb, &buf, &buf_len)) {
  243. CBB_cleanup(&cbb);
  244. return false;
  245. }
  246. ScopedOpenSSLBytes scoper(buf);
  247. return buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) == 0;
  248. }
  249. static bool TestCBBFixed() {
  250. CBB cbb;
  251. uint8_t buf[1];
  252. uint8_t *out_buf;
  253. size_t out_size;
  254. if (!CBB_init_fixed(&cbb, NULL, 0) ||
  255. CBB_add_u8(&cbb, 1) ||
  256. !CBB_finish(&cbb, &out_buf, &out_size) ||
  257. out_buf != NULL ||
  258. out_size != 0) {
  259. return false;
  260. }
  261. if (!CBB_init_fixed(&cbb, buf, 1) ||
  262. !CBB_add_u8(&cbb, 1) ||
  263. CBB_add_u8(&cbb, 2) ||
  264. !CBB_finish(&cbb, &out_buf, &out_size) ||
  265. out_buf != buf ||
  266. out_size != 1 ||
  267. buf[0] != 1) {
  268. return false;
  269. }
  270. return true;
  271. }
  272. static bool TestCBBFinishChild() {
  273. CBB cbb, child;
  274. uint8_t *out_buf;
  275. size_t out_size;
  276. if (!CBB_init(&cbb, 16)) {
  277. return false;
  278. }
  279. if (!CBB_add_u8_length_prefixed(&cbb, &child) ||
  280. CBB_finish(&child, &out_buf, &out_size) ||
  281. !CBB_finish(&cbb, &out_buf, &out_size)) {
  282. CBB_cleanup(&cbb);
  283. return false;
  284. }
  285. ScopedOpenSSLBytes scoper(out_buf);
  286. return out_size == 1 && out_buf[0] == 0;
  287. }
  288. static bool TestCBBPrefixed() {
  289. static const uint8_t kExpected[] = {0, 1, 1, 0, 2, 2, 3, 0, 0, 3,
  290. 4, 5, 6, 5, 4, 1, 0, 1, 2};
  291. uint8_t *buf;
  292. size_t buf_len;
  293. CBB cbb, contents, inner_contents, inner_inner_contents;
  294. if (!CBB_init(&cbb, 0)) {
  295. return false;
  296. }
  297. if (!CBB_add_u8_length_prefixed(&cbb, &contents) ||
  298. !CBB_add_u8_length_prefixed(&cbb, &contents) ||
  299. !CBB_add_u8(&contents, 1) ||
  300. !CBB_add_u16_length_prefixed(&cbb, &contents) ||
  301. !CBB_add_u16(&contents, 0x203) ||
  302. !CBB_add_u24_length_prefixed(&cbb, &contents) ||
  303. !CBB_add_u24(&contents, 0x40506) ||
  304. !CBB_add_u8_length_prefixed(&cbb, &contents) ||
  305. !CBB_add_u8_length_prefixed(&contents, &inner_contents) ||
  306. !CBB_add_u8(&inner_contents, 1) ||
  307. !CBB_add_u16_length_prefixed(&inner_contents, &inner_inner_contents) ||
  308. !CBB_add_u8(&inner_inner_contents, 2) ||
  309. !CBB_finish(&cbb, &buf, &buf_len)) {
  310. CBB_cleanup(&cbb);
  311. return false;
  312. }
  313. ScopedOpenSSLBytes scoper(buf);
  314. return buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) == 0;
  315. }
  316. static bool TestCBBMisuse() {
  317. CBB cbb, child, contents;
  318. uint8_t *buf;
  319. size_t buf_len;
  320. if (!CBB_init(&cbb, 0)) {
  321. return false;
  322. }
  323. if (!CBB_add_u8_length_prefixed(&cbb, &child) ||
  324. !CBB_add_u8(&child, 1) ||
  325. !CBB_add_u8(&cbb, 2)) {
  326. CBB_cleanup(&cbb);
  327. return false;
  328. }
  329. // Since we wrote to |cbb|, |child| is now invalid and attempts to write to
  330. // it should fail.
  331. if (CBB_add_u8(&child, 1) ||
  332. CBB_add_u16(&child, 1) ||
  333. CBB_add_u24(&child, 1) ||
  334. CBB_add_u8_length_prefixed(&child, &contents) ||
  335. CBB_add_u16_length_prefixed(&child, &contents) ||
  336. CBB_add_asn1(&child, &contents, 1) ||
  337. CBB_add_bytes(&child, (const uint8_t*) "a", 1)) {
  338. fprintf(stderr, "CBB operation on invalid CBB did not fail.\n");
  339. CBB_cleanup(&cbb);
  340. return false;
  341. }
  342. if (!CBB_finish(&cbb, &buf, &buf_len)) {
  343. CBB_cleanup(&cbb);
  344. return false;
  345. }
  346. ScopedOpenSSLBytes scoper(buf);
  347. if (buf_len != 3 ||
  348. memcmp(buf, "\x01\x01\x02", 3) != 0) {
  349. return false;
  350. }
  351. return true;
  352. }
  353. static bool TestCBBASN1() {
  354. static const uint8_t kExpected[] = {0x30, 3, 1, 2, 3};
  355. uint8_t *buf;
  356. size_t buf_len;
  357. CBB cbb, contents, inner_contents;
  358. if (!CBB_init(&cbb, 0)) {
  359. return false;
  360. }
  361. if (!CBB_add_asn1(&cbb, &contents, 0x30) ||
  362. !CBB_add_bytes(&contents, (const uint8_t*) "\x01\x02\x03", 3) ||
  363. !CBB_finish(&cbb, &buf, &buf_len)) {
  364. CBB_cleanup(&cbb);
  365. return false;
  366. }
  367. ScopedOpenSSLBytes scoper(buf);
  368. if (buf_len != sizeof(kExpected) || memcmp(buf, kExpected, buf_len) != 0) {
  369. return false;
  370. }
  371. std::vector<uint8_t> test_data(100000, 0x42);
  372. if (!CBB_init(&cbb, 0)) {
  373. return false;
  374. }
  375. if (!CBB_add_asn1(&cbb, &contents, 0x30) ||
  376. !CBB_add_bytes(&contents, bssl::vector_data(&test_data), 130) ||
  377. !CBB_finish(&cbb, &buf, &buf_len)) {
  378. CBB_cleanup(&cbb);
  379. return false;
  380. }
  381. scoper.reset(buf);
  382. if (buf_len != 3 + 130 ||
  383. memcmp(buf, "\x30\x81\x82", 3) != 0 ||
  384. memcmp(buf + 3, bssl::vector_data(&test_data), 130) != 0) {
  385. return false;
  386. }
  387. if (!CBB_init(&cbb, 0)) {
  388. return false;
  389. }
  390. if (!CBB_add_asn1(&cbb, &contents, 0x30) ||
  391. !CBB_add_bytes(&contents, bssl::vector_data(&test_data), 1000) ||
  392. !CBB_finish(&cbb, &buf, &buf_len)) {
  393. CBB_cleanup(&cbb);
  394. return false;
  395. }
  396. scoper.reset(buf);
  397. if (buf_len != 4 + 1000 ||
  398. memcmp(buf, "\x30\x82\x03\xe8", 4) != 0 ||
  399. memcmp(buf + 4, bssl::vector_data(&test_data), 1000)) {
  400. return false;
  401. }
  402. if (!CBB_init(&cbb, 0)) {
  403. return false;
  404. }
  405. if (!CBB_add_asn1(&cbb, &contents, 0x30) ||
  406. !CBB_add_asn1(&contents, &inner_contents, 0x30) ||
  407. !CBB_add_bytes(&inner_contents, bssl::vector_data(&test_data), 100000) ||
  408. !CBB_finish(&cbb, &buf, &buf_len)) {
  409. CBB_cleanup(&cbb);
  410. return false;
  411. }
  412. scoper.reset(buf);
  413. if (buf_len != 5 + 5 + 100000 ||
  414. memcmp(buf, "\x30\x83\x01\x86\xa5\x30\x83\x01\x86\xa0", 10) != 0 ||
  415. memcmp(buf + 10, bssl::vector_data(&test_data), 100000)) {
  416. return false;
  417. }
  418. return true;
  419. }
  420. static bool DoBerConvert(const char *name,
  421. const uint8_t *der_expected, size_t der_len,
  422. const uint8_t *ber, size_t ber_len) {
  423. CBS in;
  424. uint8_t *out;
  425. size_t out_len;
  426. CBS_init(&in, ber, ber_len);
  427. if (!CBS_asn1_ber_to_der(&in, &out, &out_len)) {
  428. fprintf(stderr, "%s: CBS_asn1_ber_to_der failed.\n", name);
  429. return false;
  430. }
  431. ScopedOpenSSLBytes scoper(out);
  432. if (out == NULL) {
  433. if (ber_len != der_len ||
  434. memcmp(der_expected, ber, ber_len) != 0) {
  435. fprintf(stderr, "%s: incorrect unconverted result.\n", name);
  436. return false;
  437. }
  438. return true;
  439. }
  440. if (out_len != der_len ||
  441. memcmp(out, der_expected, der_len) != 0) {
  442. fprintf(stderr, "%s: incorrect converted result.\n", name);
  443. return false;
  444. }
  445. return true;
  446. }
  447. static bool TestBerConvert() {
  448. static const uint8_t kSimpleBER[] = {0x01, 0x01, 0x00};
  449. // kIndefBER contains a SEQUENCE with an indefinite length.
  450. static const uint8_t kIndefBER[] = {0x30, 0x80, 0x01, 0x01, 0x02, 0x00, 0x00};
  451. static const uint8_t kIndefDER[] = {0x30, 0x03, 0x01, 0x01, 0x02};
  452. // kOctetStringBER contains an indefinite length OCTETSTRING with two parts.
  453. // These parts need to be concatenated in DER form.
  454. static const uint8_t kOctetStringBER[] = {0x24, 0x80, 0x04, 0x02, 0, 1,
  455. 0x04, 0x02, 2, 3, 0x00, 0x00};
  456. static const uint8_t kOctetStringDER[] = {0x04, 0x04, 0, 1, 2, 3};
  457. // kNSSBER is part of a PKCS#12 message generated by NSS that uses indefinite
  458. // length elements extensively.
  459. static const uint8_t kNSSBER[] = {
  460. 0x30, 0x80, 0x02, 0x01, 0x03, 0x30, 0x80, 0x06, 0x09, 0x2a, 0x86, 0x48,
  461. 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x80, 0x24, 0x80, 0x04, 0x04,
  462. 0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x39,
  463. 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  464. 0x00, 0x04, 0x14, 0x84, 0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90,
  465. 0xc1, 0xb6, 0xe8, 0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04,
  466. 0x10, 0x38, 0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b,
  467. 0xf0, 0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0, 0x00, 0x00,
  468. };
  469. static const uint8_t kNSSDER[] = {
  470. 0x30, 0x53, 0x02, 0x01, 0x03, 0x30, 0x13, 0x06, 0x09, 0x2a, 0x86,
  471. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x06, 0x04, 0x04,
  472. 0x01, 0x02, 0x03, 0x04, 0x30, 0x39, 0x30, 0x21, 0x30, 0x09, 0x06,
  473. 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x84,
  474. 0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90, 0xc1, 0xb6, 0xe8,
  475. 0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04, 0x10, 0x38,
  476. 0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b, 0xf0,
  477. 0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0,
  478. };
  479. return DoBerConvert("kSimpleBER", kSimpleBER, sizeof(kSimpleBER),
  480. kSimpleBER, sizeof(kSimpleBER)) &&
  481. DoBerConvert("kIndefBER", kIndefDER, sizeof(kIndefDER), kIndefBER,
  482. sizeof(kIndefBER)) &&
  483. DoBerConvert("kOctetStringBER", kOctetStringDER,
  484. sizeof(kOctetStringDER), kOctetStringBER,
  485. sizeof(kOctetStringBER)) &&
  486. DoBerConvert("kNSSBER", kNSSDER, sizeof(kNSSDER), kNSSBER,
  487. sizeof(kNSSBER));
  488. }
  489. struct ASN1Uint64Test {
  490. uint64_t value;
  491. const char *encoding;
  492. size_t encoding_len;
  493. };
  494. static const ASN1Uint64Test kASN1Uint64Tests[] = {
  495. {0, "\x02\x01\x00", 3},
  496. {1, "\x02\x01\x01", 3},
  497. {127, "\x02\x01\x7f", 3},
  498. {128, "\x02\x02\x00\x80", 4},
  499. {0xdeadbeef, "\x02\x05\x00\xde\xad\xbe\xef", 7},
  500. {OPENSSL_U64(0x0102030405060708),
  501. "\x02\x08\x01\x02\x03\x04\x05\x06\x07\x08", 10},
  502. {OPENSSL_U64(0xffffffffffffffff),
  503. "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff", 11},
  504. };
  505. struct ASN1InvalidUint64Test {
  506. const char *encoding;
  507. size_t encoding_len;
  508. };
  509. static const ASN1InvalidUint64Test kASN1InvalidUint64Tests[] = {
  510. // Bad tag.
  511. {"\x03\x01\x00", 3},
  512. // Empty contents.
  513. {"\x02\x00", 2},
  514. // Negative number.
  515. {"\x02\x01\x80", 3},
  516. // Overflow.
  517. {"\x02\x09\x01\x00\x00\x00\x00\x00\x00\x00\x00", 11},
  518. // Leading zeros.
  519. {"\x02\x02\x00\x01", 4},
  520. };
  521. static bool TestASN1Uint64() {
  522. for (size_t i = 0; i < sizeof(kASN1Uint64Tests) / sizeof(kASN1Uint64Tests[0]);
  523. i++) {
  524. const ASN1Uint64Test *test = &kASN1Uint64Tests[i];
  525. CBS cbs;
  526. uint64_t value;
  527. CBB cbb;
  528. uint8_t *out;
  529. size_t len;
  530. CBS_init(&cbs, (const uint8_t *)test->encoding, test->encoding_len);
  531. if (!CBS_get_asn1_uint64(&cbs, &value) ||
  532. CBS_len(&cbs) != 0 ||
  533. value != test->value) {
  534. return false;
  535. }
  536. if (!CBB_init(&cbb, 0)) {
  537. return false;
  538. }
  539. if (!CBB_add_asn1_uint64(&cbb, test->value) ||
  540. !CBB_finish(&cbb, &out, &len)) {
  541. CBB_cleanup(&cbb);
  542. return false;
  543. }
  544. ScopedOpenSSLBytes scoper(out);
  545. if (len != test->encoding_len || memcmp(out, test->encoding, len) != 0) {
  546. return false;
  547. }
  548. }
  549. for (size_t i = 0;
  550. i < sizeof(kASN1InvalidUint64Tests) / sizeof(kASN1InvalidUint64Tests[0]);
  551. i++) {
  552. const ASN1InvalidUint64Test *test = &kASN1InvalidUint64Tests[i];
  553. CBS cbs;
  554. uint64_t value;
  555. CBS_init(&cbs, (const uint8_t *)test->encoding, test->encoding_len);
  556. if (CBS_get_asn1_uint64(&cbs, &value)) {
  557. return false;
  558. }
  559. }
  560. return true;
  561. }
  562. int main(void) {
  563. CRYPTO_library_init();
  564. if (!TestSkip() ||
  565. !TestGetUint() ||
  566. !TestGetPrefixed() ||
  567. !TestGetPrefixedBad() ||
  568. !TestGetASN1() ||
  569. !TestCBBBasic() ||
  570. !TestCBBFixed() ||
  571. !TestCBBFinishChild() ||
  572. !TestCBBMisuse() ||
  573. !TestCBBPrefixed() ||
  574. !TestCBBASN1() ||
  575. !TestBerConvert() ||
  576. !TestASN1Uint64() ||
  577. !TestGetOptionalASN1Bool()) {
  578. return 1;
  579. }
  580. printf("PASS\n");
  581. return 0;
  582. }