Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

823 рядки
23 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. #if !defined(__STDC_CONSTANT_MACROS)
  15. #define __STDC_CONSTANT_MACROS
  16. #endif
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <vector>
  21. #include <openssl/crypto.h>
  22. #include <openssl/bytestring.h>
  23. #include "internal.h"
  24. #include "../test/scoped_types.h"
  25. static bool TestSkip() {
  26. static const uint8_t kData[] = {1, 2, 3};
  27. CBS data;
  28. CBS_init(&data, kData, sizeof(kData));
  29. return CBS_len(&data) == 3 &&
  30. CBS_skip(&data, 1) &&
  31. CBS_len(&data) == 2 &&
  32. CBS_skip(&data, 2) &&
  33. CBS_len(&data) == 0 &&
  34. !CBS_skip(&data, 1);
  35. }
  36. static bool TestGetUint() {
  37. static const uint8_t kData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  38. uint8_t u8;
  39. uint16_t u16;
  40. uint32_t u32;
  41. CBS data;
  42. CBS_init(&data, kData, sizeof(kData));
  43. return CBS_get_u8(&data, &u8) &&
  44. u8 == 1 &&
  45. CBS_get_u16(&data, &u16) &&
  46. u16 == 0x203 &&
  47. CBS_get_u24(&data, &u32) &&
  48. u32 == 0x40506 &&
  49. CBS_get_u32(&data, &u32) &&
  50. u32 == 0x708090a &&
  51. !CBS_get_u8(&data, &u8);
  52. }
  53. static bool TestGetPrefixed() {
  54. static const uint8_t kData[] = {1, 2, 0, 2, 3, 4, 0, 0, 3, 3, 2, 1};
  55. uint8_t u8;
  56. uint16_t u16;
  57. uint32_t u32;
  58. CBS data, prefixed;
  59. CBS_init(&data, kData, sizeof(kData));
  60. return CBS_get_u8_length_prefixed(&data, &prefixed) &&
  61. CBS_len(&prefixed) == 1 &&
  62. CBS_get_u8(&prefixed, &u8) &&
  63. u8 == 2 &&
  64. CBS_get_u16_length_prefixed(&data, &prefixed) &&
  65. CBS_len(&prefixed) == 2 &&
  66. CBS_get_u16(&prefixed, &u16) &&
  67. u16 == 0x304 &&
  68. CBS_get_u24_length_prefixed(&data, &prefixed) &&
  69. CBS_len(&prefixed) == 3 &&
  70. CBS_get_u24(&prefixed, &u32) &&
  71. u32 == 0x30201;
  72. }
  73. static bool TestGetPrefixedBad() {
  74. static const uint8_t kData1[] = {2, 1};
  75. static const uint8_t kData2[] = {0, 2, 1};
  76. static const uint8_t kData3[] = {0, 0, 2, 1};
  77. CBS data, prefixed;
  78. CBS_init(&data, kData1, sizeof(kData1));
  79. if (CBS_get_u8_length_prefixed(&data, &prefixed)) {
  80. return false;
  81. }
  82. CBS_init(&data, kData2, sizeof(kData2));
  83. if (CBS_get_u16_length_prefixed(&data, &prefixed)) {
  84. return false;
  85. }
  86. CBS_init(&data, kData3, sizeof(kData3));
  87. if (CBS_get_u24_length_prefixed(&data, &prefixed)) {
  88. return false;
  89. }
  90. return true;
  91. }
  92. static bool TestGetASN1() {
  93. static const uint8_t kData1[] = {0x30, 2, 1, 2};
  94. static const uint8_t kData2[] = {0x30, 3, 1, 2};
  95. static const uint8_t kData3[] = {0x30, 0x80};
  96. static const uint8_t kData4[] = {0x30, 0x81, 1, 1};
  97. static const uint8_t kData5[4 + 0x80] = {0x30, 0x82, 0, 0x80};
  98. static const uint8_t kData6[] = {0xa1, 3, 0x4, 1, 1};
  99. static const uint8_t kData7[] = {0xa1, 3, 0x4, 2, 1};
  100. static const uint8_t kData8[] = {0xa1, 3, 0x2, 1, 1};
  101. static const uint8_t kData9[] = {0xa1, 3, 0x2, 1, 0xff};
  102. CBS data, contents;
  103. int present;
  104. uint64_t value;
  105. CBS_init(&data, kData1, sizeof(kData1));
  106. if (CBS_peek_asn1_tag(&data, 0x1) ||
  107. !CBS_peek_asn1_tag(&data, 0x30)) {
  108. return false;
  109. }
  110. if (!CBS_get_asn1(&data, &contents, 0x30) ||
  111. CBS_len(&contents) != 2 ||
  112. memcmp(CBS_data(&contents), "\x01\x02", 2) != 0) {
  113. return false;
  114. }
  115. CBS_init(&data, kData2, sizeof(kData2));
  116. // data is truncated
  117. if (CBS_get_asn1(&data, &contents, 0x30)) {
  118. return false;
  119. }
  120. CBS_init(&data, kData3, sizeof(kData3));
  121. // zero byte length of length
  122. if (CBS_get_asn1(&data, &contents, 0x30)) {
  123. return false;
  124. }
  125. CBS_init(&data, kData4, sizeof(kData4));
  126. // long form mistakenly used.
  127. if (CBS_get_asn1(&data, &contents, 0x30)) {
  128. return false;
  129. }
  130. CBS_init(&data, kData5, sizeof(kData5));
  131. // length takes too many bytes.
  132. if (CBS_get_asn1(&data, &contents, 0x30)) {
  133. return false;
  134. }
  135. CBS_init(&data, kData1, sizeof(kData1));
  136. // wrong tag.
  137. if (CBS_get_asn1(&data, &contents, 0x31)) {
  138. return false;
  139. }
  140. CBS_init(&data, NULL, 0);
  141. // peek at empty data.
  142. if (CBS_peek_asn1_tag(&data, 0x30)) {
  143. return false;
  144. }
  145. CBS_init(&data, NULL, 0);
  146. // optional elements at empty data.
  147. if (!CBS_get_optional_asn1(&data, &contents, &present, 0xa0) ||
  148. present ||
  149. !CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa0) ||
  150. present ||
  151. CBS_len(&contents) != 0 ||
  152. !CBS_get_optional_asn1_octet_string(&data, &contents, NULL, 0xa0) ||
  153. CBS_len(&contents) != 0 ||
  154. !CBS_get_optional_asn1_uint64(&data, &value, 0xa0, 42) ||
  155. value != 42) {
  156. return false;
  157. }
  158. CBS_init(&data, kData6, sizeof(kData6));
  159. // optional element.
  160. if (!CBS_get_optional_asn1(&data, &contents, &present, 0xa0) ||
  161. present ||
  162. !CBS_get_optional_asn1(&data, &contents, &present, 0xa1) ||
  163. !present ||
  164. CBS_len(&contents) != 3 ||
  165. memcmp(CBS_data(&contents), "\x04\x01\x01", 3) != 0) {
  166. return false;
  167. }
  168. CBS_init(&data, kData6, sizeof(kData6));
  169. // optional octet string.
  170. if (!CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa0) ||
  171. present ||
  172. CBS_len(&contents) != 0 ||
  173. !CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa1) ||
  174. !present ||
  175. CBS_len(&contents) != 1 ||
  176. CBS_data(&contents)[0] != 1) {
  177. return false;
  178. }
  179. CBS_init(&data, kData7, sizeof(kData7));
  180. // invalid optional octet string.
  181. if (CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa1)) {
  182. return false;
  183. }
  184. CBS_init(&data, kData8, sizeof(kData8));
  185. // optional octet string.
  186. if (!CBS_get_optional_asn1_uint64(&data, &value, 0xa0, 42) ||
  187. value != 42 ||
  188. !CBS_get_optional_asn1_uint64(&data, &value, 0xa1, 42) ||
  189. value != 1) {
  190. return false;
  191. }
  192. CBS_init(&data, kData9, sizeof(kData9));
  193. // invalid optional integer.
  194. if (CBS_get_optional_asn1_uint64(&data, &value, 0xa1, 42)) {
  195. return false;
  196. }
  197. return true;
  198. }
  199. static bool TestGetOptionalASN1Bool() {
  200. static const uint8_t kTrue[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0xff};
  201. static const uint8_t kFalse[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0x00};
  202. static const uint8_t kInvalid[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0x01};
  203. CBS data;
  204. CBS_init(&data, NULL, 0);
  205. int val = 2;
  206. if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0) ||
  207. val != 0) {
  208. return false;
  209. }
  210. CBS_init(&data, kTrue, sizeof(kTrue));
  211. val = 2;
  212. if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0) ||
  213. val != 1) {
  214. return false;
  215. }
  216. CBS_init(&data, kFalse, sizeof(kFalse));
  217. val = 2;
  218. if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1) ||
  219. val != 0) {
  220. return false;
  221. }
  222. CBS_init(&data, kInvalid, sizeof(kInvalid));
  223. if (CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1)) {
  224. return false;
  225. }
  226. return true;
  227. }
  228. static bool TestCBBBasic() {
  229. static const uint8_t kExpected[] = {1, 2, 3, 4, 5, 6, 7, 8};
  230. uint8_t *buf;
  231. size_t buf_len;
  232. CBB cbb;
  233. if (!CBB_init(&cbb, 100)) {
  234. return false;
  235. }
  236. CBB_cleanup(&cbb);
  237. if (!CBB_init(&cbb, 0)) {
  238. return false;
  239. }
  240. if (!CBB_add_u8(&cbb, 1) ||
  241. !CBB_add_u16(&cbb, 0x203) ||
  242. !CBB_add_u24(&cbb, 0x40506) ||
  243. !CBB_add_bytes(&cbb, (const uint8_t*) "\x07\x08", 2) ||
  244. !CBB_finish(&cbb, &buf, &buf_len)) {
  245. CBB_cleanup(&cbb);
  246. return false;
  247. }
  248. ScopedOpenSSLBytes scoper(buf);
  249. return buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) == 0;
  250. }
  251. static bool TestCBBFixed() {
  252. CBB cbb;
  253. uint8_t buf[1];
  254. uint8_t *out_buf;
  255. size_t out_size;
  256. if (!CBB_init_fixed(&cbb, NULL, 0) ||
  257. CBB_add_u8(&cbb, 1) ||
  258. !CBB_finish(&cbb, &out_buf, &out_size) ||
  259. out_buf != NULL ||
  260. out_size != 0) {
  261. return false;
  262. }
  263. if (!CBB_init_fixed(&cbb, buf, 1) ||
  264. !CBB_add_u8(&cbb, 1) ||
  265. CBB_add_u8(&cbb, 2) ||
  266. !CBB_finish(&cbb, &out_buf, &out_size) ||
  267. out_buf != buf ||
  268. out_size != 1 ||
  269. buf[0] != 1) {
  270. return false;
  271. }
  272. return true;
  273. }
  274. static bool TestCBBFinishChild() {
  275. CBB cbb, child;
  276. uint8_t *out_buf;
  277. size_t out_size;
  278. if (!CBB_init(&cbb, 16)) {
  279. return false;
  280. }
  281. if (!CBB_add_u8_length_prefixed(&cbb, &child) ||
  282. CBB_finish(&child, &out_buf, &out_size) ||
  283. !CBB_finish(&cbb, &out_buf, &out_size)) {
  284. CBB_cleanup(&cbb);
  285. return false;
  286. }
  287. ScopedOpenSSLBytes scoper(out_buf);
  288. return out_size == 1 && out_buf[0] == 0;
  289. }
  290. static bool TestCBBPrefixed() {
  291. static const uint8_t kExpected[] = {0, 1, 1, 0, 2, 2, 3, 0, 0, 3,
  292. 4, 5, 6, 5, 4, 1, 0, 1, 2};
  293. uint8_t *buf;
  294. size_t buf_len;
  295. CBB cbb, contents, inner_contents, inner_inner_contents;
  296. if (!CBB_init(&cbb, 0) ||
  297. CBB_len(&cbb) != 0 ||
  298. !CBB_add_u8_length_prefixed(&cbb, &contents) ||
  299. !CBB_add_u8_length_prefixed(&cbb, &contents) ||
  300. !CBB_add_u8(&contents, 1) ||
  301. CBB_len(&contents) != 1 ||
  302. !CBB_flush(&cbb) ||
  303. CBB_len(&cbb) != 3 ||
  304. !CBB_add_u16_length_prefixed(&cbb, &contents) ||
  305. !CBB_add_u16(&contents, 0x203) ||
  306. !CBB_add_u24_length_prefixed(&cbb, &contents) ||
  307. !CBB_add_u24(&contents, 0x40506) ||
  308. !CBB_add_u8_length_prefixed(&cbb, &contents) ||
  309. !CBB_add_u8_length_prefixed(&contents, &inner_contents) ||
  310. !CBB_add_u8(&inner_contents, 1) ||
  311. !CBB_add_u16_length_prefixed(&inner_contents, &inner_inner_contents) ||
  312. !CBB_add_u8(&inner_inner_contents, 2) ||
  313. !CBB_finish(&cbb, &buf, &buf_len)) {
  314. CBB_cleanup(&cbb);
  315. return false;
  316. }
  317. ScopedOpenSSLBytes scoper(buf);
  318. return buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) == 0;
  319. }
  320. static bool TestCBBDiscardChild() {
  321. ScopedCBB cbb;
  322. CBB contents, inner_contents, inner_inner_contents;
  323. if (!CBB_init(cbb.get(), 0) ||
  324. !CBB_add_u8(cbb.get(), 0xaa)) {
  325. return false;
  326. }
  327. // Discarding |cbb|'s children preserves the byte written.
  328. CBB_discard_child(cbb.get());
  329. if (!CBB_add_u8_length_prefixed(cbb.get(), &contents) ||
  330. !CBB_add_u8_length_prefixed(cbb.get(), &contents) ||
  331. !CBB_add_u8(&contents, 0xbb) ||
  332. !CBB_add_u16_length_prefixed(cbb.get(), &contents) ||
  333. !CBB_add_u16(&contents, 0xcccc) ||
  334. !CBB_add_u24_length_prefixed(cbb.get(), &contents) ||
  335. !CBB_add_u24(&contents, 0xdddddd) ||
  336. !CBB_add_u8_length_prefixed(cbb.get(), &contents) ||
  337. !CBB_add_u8(&contents, 0xff) ||
  338. !CBB_add_u8_length_prefixed(&contents, &inner_contents) ||
  339. !CBB_add_u8(&inner_contents, 0x42) ||
  340. !CBB_add_u16_length_prefixed(&inner_contents, &inner_inner_contents) ||
  341. !CBB_add_u8(&inner_inner_contents, 0x99)) {
  342. return false;
  343. }
  344. // Discard everything from |inner_contents| down.
  345. CBB_discard_child(&contents);
  346. uint8_t *buf;
  347. size_t buf_len;
  348. if (!CBB_finish(cbb.get(), &buf, &buf_len)) {
  349. return false;
  350. }
  351. ScopedOpenSSLBytes scoper(buf);
  352. static const uint8_t kExpected[] = {
  353. 0xaa,
  354. 0,
  355. 1, 0xbb,
  356. 0, 2, 0xcc, 0xcc,
  357. 0, 0, 3, 0xdd, 0xdd, 0xdd,
  358. 1, 0xff,
  359. };
  360. return buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) == 0;
  361. }
  362. static bool TestCBBMisuse() {
  363. CBB cbb, child, contents;
  364. uint8_t *buf;
  365. size_t buf_len;
  366. if (!CBB_init(&cbb, 0)) {
  367. return false;
  368. }
  369. if (!CBB_add_u8_length_prefixed(&cbb, &child) ||
  370. !CBB_add_u8(&child, 1) ||
  371. !CBB_add_u8(&cbb, 2)) {
  372. CBB_cleanup(&cbb);
  373. return false;
  374. }
  375. // Since we wrote to |cbb|, |child| is now invalid and attempts to write to
  376. // it should fail.
  377. if (CBB_add_u8(&child, 1) ||
  378. CBB_add_u16(&child, 1) ||
  379. CBB_add_u24(&child, 1) ||
  380. CBB_add_u8_length_prefixed(&child, &contents) ||
  381. CBB_add_u16_length_prefixed(&child, &contents) ||
  382. CBB_add_asn1(&child, &contents, 1) ||
  383. CBB_add_bytes(&child, (const uint8_t*) "a", 1)) {
  384. fprintf(stderr, "CBB operation on invalid CBB did not fail.\n");
  385. CBB_cleanup(&cbb);
  386. return false;
  387. }
  388. if (!CBB_finish(&cbb, &buf, &buf_len)) {
  389. CBB_cleanup(&cbb);
  390. return false;
  391. }
  392. ScopedOpenSSLBytes scoper(buf);
  393. if (buf_len != 3 ||
  394. memcmp(buf, "\x01\x01\x02", 3) != 0) {
  395. return false;
  396. }
  397. return true;
  398. }
  399. static bool TestCBBASN1() {
  400. static const uint8_t kExpected[] = {0x30, 3, 1, 2, 3};
  401. uint8_t *buf;
  402. size_t buf_len;
  403. CBB cbb, contents, inner_contents;
  404. if (!CBB_init(&cbb, 0)) {
  405. return false;
  406. }
  407. if (!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. CBB_cleanup(&cbb);
  411. return false;
  412. }
  413. ScopedOpenSSLBytes scoper(buf);
  414. if (buf_len != sizeof(kExpected) || memcmp(buf, kExpected, buf_len) != 0) {
  415. return false;
  416. }
  417. std::vector<uint8_t> test_data(100000, 0x42);
  418. if (!CBB_init(&cbb, 0)) {
  419. return false;
  420. }
  421. if (!CBB_add_asn1(&cbb, &contents, 0x30) ||
  422. !CBB_add_bytes(&contents, test_data.data(), 130) ||
  423. !CBB_finish(&cbb, &buf, &buf_len)) {
  424. CBB_cleanup(&cbb);
  425. return false;
  426. }
  427. scoper.reset(buf);
  428. if (buf_len != 3 + 130 ||
  429. memcmp(buf, "\x30\x81\x82", 3) != 0 ||
  430. memcmp(buf + 3, test_data.data(), 130) != 0) {
  431. return false;
  432. }
  433. if (!CBB_init(&cbb, 0)) {
  434. return false;
  435. }
  436. if (!CBB_add_asn1(&cbb, &contents, 0x30) ||
  437. !CBB_add_bytes(&contents, test_data.data(), 1000) ||
  438. !CBB_finish(&cbb, &buf, &buf_len)) {
  439. CBB_cleanup(&cbb);
  440. return false;
  441. }
  442. scoper.reset(buf);
  443. if (buf_len != 4 + 1000 ||
  444. memcmp(buf, "\x30\x82\x03\xe8", 4) != 0 ||
  445. memcmp(buf + 4, test_data.data(), 1000)) {
  446. return false;
  447. }
  448. if (!CBB_init(&cbb, 0)) {
  449. return false;
  450. }
  451. if (!CBB_add_asn1(&cbb, &contents, 0x30) ||
  452. !CBB_add_asn1(&contents, &inner_contents, 0x30) ||
  453. !CBB_add_bytes(&inner_contents, test_data.data(), 100000) ||
  454. !CBB_finish(&cbb, &buf, &buf_len)) {
  455. CBB_cleanup(&cbb);
  456. return false;
  457. }
  458. scoper.reset(buf);
  459. if (buf_len != 5 + 5 + 100000 ||
  460. memcmp(buf, "\x30\x83\x01\x86\xa5\x30\x83\x01\x86\xa0", 10) != 0 ||
  461. memcmp(buf + 10, test_data.data(), 100000)) {
  462. return false;
  463. }
  464. return true;
  465. }
  466. static bool DoBerConvert(const char *name,
  467. const uint8_t *der_expected, size_t der_len,
  468. const uint8_t *ber, size_t ber_len) {
  469. CBS in;
  470. uint8_t *out;
  471. size_t out_len;
  472. CBS_init(&in, ber, ber_len);
  473. if (!CBS_asn1_ber_to_der(&in, &out, &out_len)) {
  474. fprintf(stderr, "%s: CBS_asn1_ber_to_der failed.\n", name);
  475. return false;
  476. }
  477. ScopedOpenSSLBytes scoper(out);
  478. if (out == NULL) {
  479. if (ber_len != der_len ||
  480. memcmp(der_expected, ber, ber_len) != 0) {
  481. fprintf(stderr, "%s: incorrect unconverted result.\n", name);
  482. return false;
  483. }
  484. return true;
  485. }
  486. if (out_len != der_len ||
  487. memcmp(out, der_expected, der_len) != 0) {
  488. fprintf(stderr, "%s: incorrect converted result.\n", name);
  489. return false;
  490. }
  491. return true;
  492. }
  493. static bool TestBerConvert() {
  494. static const uint8_t kSimpleBER[] = {0x01, 0x01, 0x00};
  495. // kIndefBER contains a SEQUENCE with an indefinite length.
  496. static const uint8_t kIndefBER[] = {0x30, 0x80, 0x01, 0x01, 0x02, 0x00, 0x00};
  497. static const uint8_t kIndefDER[] = {0x30, 0x03, 0x01, 0x01, 0x02};
  498. // kOctetStringBER contains an indefinite length OCTET STRING with two parts.
  499. // These parts need to be concatenated in DER form.
  500. static const uint8_t kOctetStringBER[] = {0x24, 0x80, 0x04, 0x02, 0, 1,
  501. 0x04, 0x02, 2, 3, 0x00, 0x00};
  502. static const uint8_t kOctetStringDER[] = {0x04, 0x04, 0, 1, 2, 3};
  503. // kNSSBER is part of a PKCS#12 message generated by NSS that uses indefinite
  504. // length elements extensively.
  505. static const uint8_t kNSSBER[] = {
  506. 0x30, 0x80, 0x02, 0x01, 0x03, 0x30, 0x80, 0x06, 0x09, 0x2a, 0x86, 0x48,
  507. 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x80, 0x24, 0x80, 0x04, 0x04,
  508. 0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x39,
  509. 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  510. 0x00, 0x04, 0x14, 0x84, 0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90,
  511. 0xc1, 0xb6, 0xe8, 0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04,
  512. 0x10, 0x38, 0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b,
  513. 0xf0, 0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0, 0x00, 0x00,
  514. };
  515. static const uint8_t kNSSDER[] = {
  516. 0x30, 0x53, 0x02, 0x01, 0x03, 0x30, 0x13, 0x06, 0x09, 0x2a, 0x86,
  517. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x06, 0x04, 0x04,
  518. 0x01, 0x02, 0x03, 0x04, 0x30, 0x39, 0x30, 0x21, 0x30, 0x09, 0x06,
  519. 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x84,
  520. 0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90, 0xc1, 0xb6, 0xe8,
  521. 0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04, 0x10, 0x38,
  522. 0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b, 0xf0,
  523. 0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0,
  524. };
  525. // kConstructedStringBER contains a deeply-nested constructed OCTET STRING.
  526. // The BER conversion collapses this to one level deep, but not completely.
  527. static const uint8_t kConstructedStringBER[] = {
  528. 0xa0, 0x10, 0x24, 0x06, 0x04, 0x01, 0x00, 0x04, 0x01,
  529. 0x01, 0x24, 0x06, 0x04, 0x01, 0x02, 0x04, 0x01, 0x03,
  530. };
  531. static const uint8_t kConstructedStringDER[] = {
  532. 0xa0, 0x08, 0x04, 0x02, 0x00, 0x01, 0x04, 0x02, 0x02, 0x03,
  533. };
  534. return DoBerConvert("kSimpleBER", kSimpleBER, sizeof(kSimpleBER),
  535. kSimpleBER, sizeof(kSimpleBER)) &&
  536. DoBerConvert("kIndefBER", kIndefDER, sizeof(kIndefDER), kIndefBER,
  537. sizeof(kIndefBER)) &&
  538. DoBerConvert("kOctetStringBER", kOctetStringDER,
  539. sizeof(kOctetStringDER), kOctetStringBER,
  540. sizeof(kOctetStringBER)) &&
  541. DoBerConvert("kNSSBER", kNSSDER, sizeof(kNSSDER), kNSSBER,
  542. sizeof(kNSSBER)) &&
  543. DoBerConvert("kConstructedStringBER", kConstructedStringDER,
  544. sizeof(kConstructedStringDER), kConstructedStringBER,
  545. sizeof(kConstructedStringBER));
  546. }
  547. struct ImplicitStringTest {
  548. const char *in;
  549. size_t in_len;
  550. bool ok;
  551. const char *out;
  552. size_t out_len;
  553. };
  554. static const ImplicitStringTest kImplicitStringTests[] = {
  555. // A properly-encoded string.
  556. {"\x80\x03\x61\x61\x61", 5, true, "aaa", 3},
  557. // An implicit-tagged string.
  558. {"\xa0\x09\x04\x01\x61\x04\x01\x61\x04\x01\x61", 11, true, "aaa", 3},
  559. // |CBS_get_asn1_implicit_string| only accepts one level deep of nesting.
  560. {"\xa0\x0b\x24\x06\x04\x01\x61\x04\x01\x61\x04\x01\x61", 13, false, nullptr,
  561. 0},
  562. // The outer tag must match.
  563. {"\x81\x03\x61\x61\x61", 5, false, nullptr, 0},
  564. {"\xa1\x09\x04\x01\x61\x04\x01\x61\x04\x01\x61", 11, false, nullptr, 0},
  565. // The inner tag must match.
  566. {"\xa1\x09\x0c\x01\x61\x0c\x01\x61\x0c\x01\x61", 11, false, nullptr, 0},
  567. };
  568. static bool TestImplicitString() {
  569. for (const auto &test : kImplicitStringTests) {
  570. uint8_t *storage = nullptr;
  571. CBS in, out;
  572. CBS_init(&in, reinterpret_cast<const uint8_t *>(test.in), test.in_len);
  573. int ok = CBS_get_asn1_implicit_string(&in, &out, &storage,
  574. CBS_ASN1_CONTEXT_SPECIFIC | 0,
  575. CBS_ASN1_OCTETSTRING);
  576. ScopedOpenSSLBytes scoper(storage);
  577. if (static_cast<bool>(ok) != test.ok) {
  578. fprintf(stderr, "CBS_get_asn1_implicit_string unexpectedly %s\n",
  579. ok ? "succeeded" : "failed");
  580. return false;
  581. }
  582. if (ok && (CBS_len(&out) != test.out_len ||
  583. memcmp(CBS_data(&out), test.out, test.out_len) != 0)) {
  584. fprintf(stderr, "CBS_get_asn1_implicit_string gave the wrong output\n");
  585. return false;
  586. }
  587. }
  588. return true;
  589. }
  590. struct ASN1Uint64Test {
  591. uint64_t value;
  592. const char *encoding;
  593. size_t encoding_len;
  594. };
  595. static const ASN1Uint64Test kASN1Uint64Tests[] = {
  596. {0, "\x02\x01\x00", 3},
  597. {1, "\x02\x01\x01", 3},
  598. {127, "\x02\x01\x7f", 3},
  599. {128, "\x02\x02\x00\x80", 4},
  600. {0xdeadbeef, "\x02\x05\x00\xde\xad\xbe\xef", 7},
  601. {UINT64_C(0x0102030405060708),
  602. "\x02\x08\x01\x02\x03\x04\x05\x06\x07\x08", 10},
  603. {UINT64_C(0xffffffffffffffff),
  604. "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff", 11},
  605. };
  606. struct ASN1InvalidUint64Test {
  607. const char *encoding;
  608. size_t encoding_len;
  609. };
  610. static const ASN1InvalidUint64Test kASN1InvalidUint64Tests[] = {
  611. // Bad tag.
  612. {"\x03\x01\x00", 3},
  613. // Empty contents.
  614. {"\x02\x00", 2},
  615. // Negative number.
  616. {"\x02\x01\x80", 3},
  617. // Overflow.
  618. {"\x02\x09\x01\x00\x00\x00\x00\x00\x00\x00\x00", 11},
  619. // Leading zeros.
  620. {"\x02\x02\x00\x01", 4},
  621. };
  622. static bool TestASN1Uint64() {
  623. for (size_t i = 0; i < sizeof(kASN1Uint64Tests) / sizeof(kASN1Uint64Tests[0]);
  624. i++) {
  625. const ASN1Uint64Test *test = &kASN1Uint64Tests[i];
  626. CBS cbs;
  627. uint64_t value;
  628. CBB cbb;
  629. uint8_t *out;
  630. size_t len;
  631. CBS_init(&cbs, (const uint8_t *)test->encoding, test->encoding_len);
  632. if (!CBS_get_asn1_uint64(&cbs, &value) ||
  633. CBS_len(&cbs) != 0 ||
  634. value != test->value) {
  635. return false;
  636. }
  637. if (!CBB_init(&cbb, 0)) {
  638. return false;
  639. }
  640. if (!CBB_add_asn1_uint64(&cbb, test->value) ||
  641. !CBB_finish(&cbb, &out, &len)) {
  642. CBB_cleanup(&cbb);
  643. return false;
  644. }
  645. ScopedOpenSSLBytes scoper(out);
  646. if (len != test->encoding_len || memcmp(out, test->encoding, len) != 0) {
  647. return false;
  648. }
  649. }
  650. for (size_t i = 0;
  651. i < sizeof(kASN1InvalidUint64Tests) / sizeof(kASN1InvalidUint64Tests[0]);
  652. i++) {
  653. const ASN1InvalidUint64Test *test = &kASN1InvalidUint64Tests[i];
  654. CBS cbs;
  655. uint64_t value;
  656. CBS_init(&cbs, (const uint8_t *)test->encoding, test->encoding_len);
  657. if (CBS_get_asn1_uint64(&cbs, &value)) {
  658. return false;
  659. }
  660. }
  661. return true;
  662. }
  663. static bool TestZero() {
  664. CBB cbb;
  665. CBB_zero(&cbb);
  666. // Calling |CBB_cleanup| on a zero-state |CBB| must not crash.
  667. CBB_cleanup(&cbb);
  668. return true;
  669. }
  670. static bool TestCBBReserve() {
  671. uint8_t buf[10];
  672. uint8_t *ptr;
  673. size_t len;
  674. ScopedCBB cbb;
  675. if (!CBB_init_fixed(cbb.get(), buf, sizeof(buf)) ||
  676. // Too large.
  677. CBB_reserve(cbb.get(), &ptr, 11) ||
  678. // Successfully reserve the entire space.
  679. !CBB_reserve(cbb.get(), &ptr, 10) ||
  680. ptr != buf ||
  681. // Advancing under the maximum bytes is legal.
  682. !CBB_did_write(cbb.get(), 5) ||
  683. !CBB_finish(cbb.get(), NULL, &len) ||
  684. len != 5) {
  685. return false;
  686. }
  687. return true;
  688. }
  689. int main(void) {
  690. CRYPTO_library_init();
  691. if (!TestSkip() ||
  692. !TestGetUint() ||
  693. !TestGetPrefixed() ||
  694. !TestGetPrefixedBad() ||
  695. !TestGetASN1() ||
  696. !TestCBBBasic() ||
  697. !TestCBBFixed() ||
  698. !TestCBBFinishChild() ||
  699. !TestCBBMisuse() ||
  700. !TestCBBPrefixed() ||
  701. !TestCBBDiscardChild() ||
  702. !TestCBBASN1() ||
  703. !TestBerConvert() ||
  704. !TestImplicitString() ||
  705. !TestASN1Uint64() ||
  706. !TestGetOptionalASN1Bool() ||
  707. !TestZero() ||
  708. !TestCBBReserve()) {
  709. return 1;
  710. }
  711. printf("PASS\n");
  712. return 0;
  713. }