test_suite_asn1parse.function 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /* BEGIN_HEADER */
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <limits.h>
  5. #include "mbedtls/bignum.h"
  6. #include "mbedtls/asn1.h"
  7. #if defined(MBEDTLS_ASN1_WRITE_C)
  8. #include "mbedtls/asn1write.h"
  9. #endif
  10. /* Used internally to report an error that indicates a bug in a parsing function. */
  11. #define ERR_PARSE_INCONSISTENCY INT_MAX
  12. /* Use this magic value in some tests to indicate that the expected result
  13. * should not be checked. */
  14. #define UNPREDICTABLE_RESULT 0x5552
  15. static int nested_parse(unsigned char **const p,
  16. const unsigned char *const end)
  17. {
  18. int ret;
  19. size_t len = 0;
  20. size_t len2 = 0;
  21. unsigned char *const start = *p;
  22. unsigned char *content_start;
  23. unsigned char tag;
  24. /* First get the length, skipping over the tag. */
  25. content_start = start + 1;
  26. ret = mbedtls_asn1_get_len(&content_start, end, &len);
  27. TEST_ASSERT(content_start <= end);
  28. if (ret != 0) {
  29. return ret;
  30. }
  31. /* Since we have a valid element start (tag and length), retrieve and
  32. * check the tag. */
  33. tag = start[0];
  34. TEST_EQUAL(mbedtls_asn1_get_tag(p, end, &len2, tag ^ 1),
  35. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  36. *p = start;
  37. TEST_EQUAL(mbedtls_asn1_get_tag(p, end, &len2, tag), 0);
  38. TEST_EQUAL(len, len2);
  39. TEST_ASSERT(*p == content_start);
  40. *p = content_start;
  41. switch (tag & 0x1f) {
  42. case MBEDTLS_ASN1_BOOLEAN:
  43. {
  44. int val = -257;
  45. *p = start;
  46. ret = mbedtls_asn1_get_bool(p, end, &val);
  47. if (ret == 0) {
  48. TEST_ASSERT(val == 0 || val == 1);
  49. }
  50. break;
  51. }
  52. case MBEDTLS_ASN1_INTEGER:
  53. {
  54. #if defined(MBEDTLS_BIGNUM_C)
  55. mbedtls_mpi mpi;
  56. mbedtls_mpi_init(&mpi);
  57. *p = start;
  58. ret = mbedtls_asn1_get_mpi(p, end, &mpi);
  59. mbedtls_mpi_free(&mpi);
  60. #else
  61. *p = start + 1;
  62. ret = mbedtls_asn1_get_len(p, end, &len);
  63. *p += len;
  64. #endif
  65. /* If we're sure that the number fits in an int, also
  66. * call mbedtls_asn1_get_int(). */
  67. if (ret == 0 && len < sizeof(int)) {
  68. int val = -257;
  69. unsigned char *q = start;
  70. ret = mbedtls_asn1_get_int(&q, end, &val);
  71. TEST_ASSERT(*p == q);
  72. }
  73. break;
  74. }
  75. case MBEDTLS_ASN1_BIT_STRING:
  76. {
  77. mbedtls_asn1_bitstring bs;
  78. *p = start;
  79. ret = mbedtls_asn1_get_bitstring(p, end, &bs);
  80. break;
  81. }
  82. case MBEDTLS_ASN1_SEQUENCE:
  83. {
  84. while (*p <= end && *p < content_start + len && ret == 0) {
  85. ret = nested_parse(p, content_start + len);
  86. }
  87. break;
  88. }
  89. case MBEDTLS_ASN1_OCTET_STRING:
  90. case MBEDTLS_ASN1_NULL:
  91. case MBEDTLS_ASN1_OID:
  92. case MBEDTLS_ASN1_UTF8_STRING:
  93. case MBEDTLS_ASN1_SET:
  94. case MBEDTLS_ASN1_PRINTABLE_STRING:
  95. case MBEDTLS_ASN1_T61_STRING:
  96. case MBEDTLS_ASN1_IA5_STRING:
  97. case MBEDTLS_ASN1_UTC_TIME:
  98. case MBEDTLS_ASN1_GENERALIZED_TIME:
  99. case MBEDTLS_ASN1_UNIVERSAL_STRING:
  100. case MBEDTLS_ASN1_BMP_STRING:
  101. default:
  102. /* No further testing implemented for this tag. */
  103. *p += len;
  104. return 0;
  105. }
  106. TEST_ASSERT(*p <= end);
  107. return ret;
  108. exit:
  109. return ERR_PARSE_INCONSISTENCY;
  110. }
  111. int get_len_step(const data_t *input, size_t buffer_size,
  112. size_t actual_length)
  113. {
  114. unsigned char *buf = NULL;
  115. unsigned char *p = NULL;
  116. unsigned char *end;
  117. size_t parsed_length;
  118. int ret;
  119. mbedtls_test_set_step(buffer_size);
  120. /* Allocate a new buffer of exactly the length to parse each time.
  121. * This gives memory sanitizers a chance to catch buffer overreads. */
  122. if (buffer_size == 0) {
  123. ASSERT_ALLOC(buf, 1);
  124. end = buf + 1;
  125. p = end;
  126. } else {
  127. ASSERT_ALLOC_WEAK(buf, buffer_size);
  128. if (buffer_size > input->len) {
  129. memcpy(buf, input->x, input->len);
  130. memset(buf + input->len, 'A', buffer_size - input->len);
  131. } else {
  132. memcpy(buf, input->x, buffer_size);
  133. }
  134. p = buf;
  135. end = buf + buffer_size;
  136. }
  137. ret = mbedtls_asn1_get_len(&p, end, &parsed_length);
  138. if (buffer_size >= input->len + actual_length) {
  139. TEST_EQUAL(ret, 0);
  140. TEST_ASSERT(p == buf + input->len);
  141. TEST_EQUAL(parsed_length, actual_length);
  142. } else {
  143. TEST_EQUAL(ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA);
  144. }
  145. mbedtls_free(buf);
  146. return 1;
  147. exit:
  148. mbedtls_free(buf);
  149. return 0;
  150. }
  151. typedef struct {
  152. const unsigned char *input_start;
  153. const char *description;
  154. } traverse_state_t;
  155. /* Value returned by traverse_callback if description runs out. */
  156. #define RET_TRAVERSE_STOP 1
  157. /* Value returned by traverse_callback if description has an invalid format
  158. * (see traverse_sequence_of). */
  159. #define RET_TRAVERSE_ERROR 2
  160. static int traverse_callback(void *ctx, int tag,
  161. unsigned char *content, size_t len)
  162. {
  163. traverse_state_t *state = ctx;
  164. size_t offset;
  165. const char *rest = state->description;
  166. unsigned long n;
  167. TEST_ASSERT(content > state->input_start);
  168. offset = content - state->input_start;
  169. mbedtls_test_set_step(offset);
  170. if (*rest == 0) {
  171. return RET_TRAVERSE_STOP;
  172. }
  173. n = strtoul(rest, (char **) &rest, 0);
  174. TEST_EQUAL(n, offset);
  175. TEST_EQUAL(*rest, ',');
  176. ++rest;
  177. n = strtoul(rest, (char **) &rest, 0);
  178. TEST_EQUAL(n, (unsigned) tag);
  179. TEST_EQUAL(*rest, ',');
  180. ++rest;
  181. n = strtoul(rest, (char **) &rest, 0);
  182. TEST_EQUAL(n, len);
  183. if (*rest == ',') {
  184. ++rest;
  185. }
  186. state->description = rest;
  187. return 0;
  188. exit:
  189. return RET_TRAVERSE_ERROR;
  190. }
  191. /* END_HEADER */
  192. /* BEGIN_DEPENDENCIES
  193. * depends_on:MBEDTLS_ASN1_PARSE_C
  194. * END_DEPENDENCIES
  195. */
  196. /* BEGIN_CASE */
  197. void parse_prefixes(const data_t *input,
  198. int full_result,
  199. int overfull_result)
  200. {
  201. /* full_result: expected result from parsing the given string. */
  202. /* overfull_result: expected_result from parsing the given string plus
  203. * some trailing garbage. This may be UNPREDICTABLE_RESULT to accept
  204. * any result: use this for invalid inputs that may or may not become
  205. * valid depending on what the trailing garbage is. */
  206. unsigned char *buf = NULL;
  207. unsigned char *p = NULL;
  208. size_t buffer_size;
  209. int ret;
  210. /* Test every prefix of the input, except the empty string.
  211. * The first byte of the string is the tag. Without a tag byte,
  212. * we wouldn't know what to parse the input as.
  213. * Also test the input followed by an extra byte.
  214. */
  215. for (buffer_size = 1; buffer_size <= input->len + 1; buffer_size++) {
  216. mbedtls_test_set_step(buffer_size);
  217. /* Allocate a new buffer of exactly the length to parse each time.
  218. * This gives memory sanitizers a chance to catch buffer overreads. */
  219. ASSERT_ALLOC(buf, buffer_size);
  220. memcpy(buf, input->x, buffer_size);
  221. p = buf;
  222. ret = nested_parse(&p, buf + buffer_size);
  223. if (ret == ERR_PARSE_INCONSISTENCY) {
  224. goto exit;
  225. }
  226. if (buffer_size < input->len) {
  227. TEST_EQUAL(ret, MBEDTLS_ERR_ASN1_OUT_OF_DATA);
  228. } else if (buffer_size == input->len) {
  229. TEST_EQUAL(ret, full_result);
  230. } else { /* ( buffer_size > input->len ) */
  231. if (overfull_result != UNPREDICTABLE_RESULT) {
  232. TEST_EQUAL(ret, overfull_result);
  233. }
  234. }
  235. if (ret == 0) {
  236. TEST_ASSERT(p == buf + input->len);
  237. }
  238. mbedtls_free(buf);
  239. buf = NULL;
  240. }
  241. exit:
  242. mbedtls_free(buf);
  243. }
  244. /* END_CASE */
  245. /* BEGIN_CASE */
  246. void get_len(const data_t *input, int actual_length_arg)
  247. {
  248. size_t actual_length = actual_length_arg;
  249. size_t buffer_size;
  250. /* Test prefixes of a buffer containing the given length string
  251. * followed by `actual_length` bytes of payload. To save a bit of
  252. * time, we skip some "boring" prefixes: we don't test prefixes where
  253. * the payload is truncated more than one byte away from either end,
  254. * and we only test the empty string on a 1-byte input.
  255. */
  256. for (buffer_size = 1; buffer_size <= input->len + 1; buffer_size++) {
  257. if (!get_len_step(input, buffer_size, actual_length)) {
  258. goto exit;
  259. }
  260. }
  261. if (!get_len_step(input, input->len + actual_length - 1, actual_length)) {
  262. goto exit;
  263. }
  264. if (!get_len_step(input, input->len + actual_length, actual_length)) {
  265. goto exit;
  266. }
  267. }
  268. /* END_CASE */
  269. /* BEGIN_CASE */
  270. void get_boolean(const data_t *input,
  271. int expected_value, int expected_result)
  272. {
  273. unsigned char *p = input->x;
  274. int val;
  275. int ret;
  276. ret = mbedtls_asn1_get_bool(&p, input->x + input->len, &val);
  277. TEST_EQUAL(ret, expected_result);
  278. if (expected_result == 0) {
  279. TEST_EQUAL(val, expected_value);
  280. TEST_ASSERT(p == input->x + input->len);
  281. }
  282. }
  283. /* END_CASE */
  284. /* BEGIN_CASE */
  285. void empty_integer(const data_t *input)
  286. {
  287. unsigned char *p;
  288. #if defined(MBEDTLS_BIGNUM_C)
  289. mbedtls_mpi actual_mpi;
  290. #endif
  291. int val;
  292. #if defined(MBEDTLS_BIGNUM_C)
  293. mbedtls_mpi_init(&actual_mpi);
  294. #endif
  295. /* An INTEGER with no content is not valid. */
  296. p = input->x;
  297. TEST_EQUAL(mbedtls_asn1_get_int(&p, input->x + input->len, &val),
  298. MBEDTLS_ERR_ASN1_INVALID_LENGTH);
  299. #if defined(MBEDTLS_BIGNUM_C)
  300. /* INTEGERs are sometimes abused as bitstrings, so the library accepts
  301. * an INTEGER with empty content and gives it the value 0. */
  302. p = input->x;
  303. TEST_EQUAL(mbedtls_asn1_get_mpi(&p, input->x + input->len, &actual_mpi),
  304. 0);
  305. TEST_EQUAL(mbedtls_mpi_cmp_int(&actual_mpi, 0), 0);
  306. #endif
  307. exit:
  308. #if defined(MBEDTLS_BIGNUM_C)
  309. mbedtls_mpi_free(&actual_mpi);
  310. #endif
  311. /*empty cleanup in some configurations*/;
  312. }
  313. /* END_CASE */
  314. /* BEGIN_CASE */
  315. void get_integer(const data_t *input,
  316. const char *expected_hex, int expected_result)
  317. {
  318. unsigned char *p;
  319. #if defined(MBEDTLS_BIGNUM_C)
  320. mbedtls_mpi expected_mpi;
  321. mbedtls_mpi actual_mpi;
  322. mbedtls_mpi complement;
  323. int expected_result_for_mpi = expected_result;
  324. #endif
  325. long expected_value;
  326. int expected_result_for_int = expected_result;
  327. int val;
  328. int ret;
  329. #if defined(MBEDTLS_BIGNUM_C)
  330. mbedtls_mpi_init(&expected_mpi);
  331. mbedtls_mpi_init(&actual_mpi);
  332. mbedtls_mpi_init(&complement);
  333. #endif
  334. errno = 0;
  335. expected_value = strtol(expected_hex, NULL, 16);
  336. if (expected_result == 0 &&
  337. (errno == ERANGE
  338. #if LONG_MAX > INT_MAX
  339. || expected_value > INT_MAX || expected_value < INT_MIN
  340. #endif
  341. )) {
  342. /* The library returns the dubious error code INVALID_LENGTH
  343. * for integers that are out of range. */
  344. expected_result_for_int = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
  345. }
  346. if (expected_result == 0 && expected_value < 0) {
  347. /* The library does not support negative INTEGERs and
  348. * returns the dubious error code INVALID_LENGTH.
  349. * Test that we preserve the historical behavior. If we
  350. * decide to change the behavior, we'll also change this test. */
  351. expected_result_for_int = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
  352. }
  353. p = input->x;
  354. ret = mbedtls_asn1_get_int(&p, input->x + input->len, &val);
  355. TEST_EQUAL(ret, expected_result_for_int);
  356. if (ret == 0) {
  357. TEST_EQUAL(val, expected_value);
  358. TEST_ASSERT(p == input->x + input->len);
  359. }
  360. #if defined(MBEDTLS_BIGNUM_C)
  361. ret = mbedtls_test_read_mpi(&expected_mpi, expected_hex);
  362. TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_MPI_BAD_INPUT_DATA);
  363. if (ret == MBEDTLS_ERR_MPI_BAD_INPUT_DATA) {
  364. /* The data overflows the maximum MPI size. */
  365. expected_result_for_mpi = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  366. }
  367. p = input->x;
  368. ret = mbedtls_asn1_get_mpi(&p, input->x + input->len, &actual_mpi);
  369. TEST_EQUAL(ret, expected_result_for_mpi);
  370. if (ret == 0) {
  371. if (expected_value >= 0) {
  372. TEST_ASSERT(mbedtls_mpi_cmp_mpi(&actual_mpi,
  373. &expected_mpi) == 0);
  374. } else {
  375. /* The library ignores the sign bit in ASN.1 INTEGERs
  376. * (which makes sense insofar as INTEGERs are sometimes
  377. * abused as bit strings), so the result of parsing them
  378. * is a positive integer such that expected_mpi +
  379. * actual_mpi = 2^n where n is the length of the content
  380. * of the INTEGER. (Leading ff octets don't matter for the
  381. * expected value, but they matter for the actual value.)
  382. * Test that we don't change from this behavior. If we
  383. * decide to fix the library to change the behavior on
  384. * negative INTEGERs, we'll fix this test code. */
  385. unsigned char *q = input->x + 1;
  386. size_t len;
  387. TEST_ASSERT(mbedtls_asn1_get_len(&q, input->x + input->len,
  388. &len) == 0);
  389. TEST_ASSERT(mbedtls_mpi_lset(&complement, 1) == 0);
  390. TEST_ASSERT(mbedtls_mpi_shift_l(&complement, len * 8) == 0);
  391. TEST_ASSERT(mbedtls_mpi_add_mpi(&complement, &complement,
  392. &expected_mpi) == 0);
  393. TEST_ASSERT(mbedtls_mpi_cmp_mpi(&complement,
  394. &actual_mpi) == 0);
  395. }
  396. TEST_ASSERT(p == input->x + input->len);
  397. }
  398. #endif
  399. exit:
  400. #if defined(MBEDTLS_BIGNUM_C)
  401. mbedtls_mpi_free(&expected_mpi);
  402. mbedtls_mpi_free(&actual_mpi);
  403. mbedtls_mpi_free(&complement);
  404. #endif
  405. /*empty cleanup in some configurations*/;
  406. }
  407. /* END_CASE */
  408. /* BEGIN_CASE */
  409. void get_enum(const data_t *input,
  410. const char *expected_hex, int expected_result)
  411. {
  412. unsigned char *p;
  413. long expected_value;
  414. int expected_result_for_enum = expected_result;
  415. int val;
  416. int ret;
  417. errno = 0;
  418. expected_value = strtol(expected_hex, NULL, 16);
  419. if (expected_result == 0 &&
  420. (errno == ERANGE
  421. #if LONG_MAX > INT_MAX
  422. || expected_value > INT_MAX || expected_value < INT_MIN
  423. #endif
  424. )) {
  425. /* The library returns the dubious error code INVALID_LENGTH
  426. * for integers that are out of range. */
  427. expected_result_for_enum = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
  428. }
  429. if (expected_result == 0 && expected_value < 0) {
  430. /* The library does not support negative INTEGERs and
  431. * returns the dubious error code INVALID_LENGTH.
  432. * Test that we preserve the historical behavior. If we
  433. * decide to change the behavior, we'll also change this test. */
  434. expected_result_for_enum = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
  435. }
  436. p = input->x;
  437. ret = mbedtls_asn1_get_enum(&p, input->x + input->len, &val);
  438. TEST_EQUAL(ret, expected_result_for_enum);
  439. if (ret == 0) {
  440. TEST_EQUAL(val, expected_value);
  441. TEST_ASSERT(p == input->x + input->len);
  442. }
  443. }
  444. /* END_CASE */
  445. /* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
  446. void get_mpi_too_large()
  447. {
  448. unsigned char *buf = NULL;
  449. unsigned char *p;
  450. mbedtls_mpi actual_mpi;
  451. size_t too_many_octets =
  452. MBEDTLS_MPI_MAX_LIMBS * sizeof(mbedtls_mpi_uint) + 1;
  453. size_t size = too_many_octets + 6;
  454. mbedtls_mpi_init(&actual_mpi);
  455. ASSERT_ALLOC(buf, size);
  456. buf[0] = 0x02; /* tag: INTEGER */
  457. buf[1] = 0x84; /* 4-octet length */
  458. buf[2] = (too_many_octets >> 24) & 0xff;
  459. buf[3] = (too_many_octets >> 16) & 0xff;
  460. buf[4] = (too_many_octets >> 8) & 0xff;
  461. buf[5] = too_many_octets & 0xff;
  462. buf[6] = 0x01; /* most significant octet */
  463. p = buf;
  464. TEST_EQUAL(mbedtls_asn1_get_mpi(&p, buf + size, &actual_mpi),
  465. MBEDTLS_ERR_MPI_ALLOC_FAILED);
  466. exit:
  467. mbedtls_mpi_free(&actual_mpi);
  468. mbedtls_free(buf);
  469. }
  470. /* END_CASE */
  471. /* BEGIN_CASE */
  472. void get_bitstring(const data_t *input,
  473. int expected_length, int expected_unused_bits,
  474. int expected_result, int expected_result_null)
  475. {
  476. mbedtls_asn1_bitstring bs = { 0xdead, 0x21, NULL };
  477. unsigned char *p = input->x;
  478. TEST_EQUAL(mbedtls_asn1_get_bitstring(&p, input->x + input->len, &bs),
  479. expected_result);
  480. if (expected_result == 0) {
  481. TEST_EQUAL(bs.len, (size_t) expected_length);
  482. TEST_EQUAL(bs.unused_bits, expected_unused_bits);
  483. TEST_ASSERT(bs.p != NULL);
  484. TEST_EQUAL(bs.p - input->x + bs.len, input->len);
  485. TEST_ASSERT(p == input->x + input->len);
  486. }
  487. p = input->x;
  488. TEST_EQUAL(mbedtls_asn1_get_bitstring_null(&p, input->x + input->len,
  489. &bs.len),
  490. expected_result_null);
  491. if (expected_result_null == 0) {
  492. TEST_EQUAL(bs.len, (size_t) expected_length);
  493. if (expected_result == 0) {
  494. TEST_ASSERT(p == input->x + input->len - bs.len);
  495. }
  496. }
  497. }
  498. /* END_CASE */
  499. /* BEGIN_CASE */
  500. void get_sequence_of(const data_t *input, int tag,
  501. const char *description,
  502. int expected_result)
  503. {
  504. /* The description string is a comma-separated list of integers.
  505. * For each element in the SEQUENCE in input, description contains
  506. * two integers: the offset of the element (offset from the start
  507. * of input to the tag of the element) and the length of the
  508. * element's contents.
  509. * "offset1,length1,..." */
  510. mbedtls_asn1_sequence head = { { 0, 0, NULL }, NULL };
  511. mbedtls_asn1_sequence *cur;
  512. unsigned char *p = input->x;
  513. const char *rest = description;
  514. unsigned long n;
  515. unsigned int step = 0;
  516. TEST_EQUAL(mbedtls_asn1_get_sequence_of(&p, input->x + input->len,
  517. &head, tag),
  518. expected_result);
  519. if (expected_result == 0) {
  520. TEST_ASSERT(p == input->x + input->len);
  521. if (!*rest) {
  522. TEST_EQUAL(head.buf.tag, 0);
  523. TEST_ASSERT(head.buf.p == NULL);
  524. TEST_EQUAL(head.buf.len, 0);
  525. TEST_ASSERT(head.next == NULL);
  526. } else {
  527. cur = &head;
  528. while (*rest) {
  529. mbedtls_test_set_step(step);
  530. TEST_ASSERT(cur != NULL);
  531. TEST_EQUAL(cur->buf.tag, tag);
  532. n = strtoul(rest, (char **) &rest, 0);
  533. TEST_EQUAL(n, (size_t) (cur->buf.p - input->x));
  534. ++rest;
  535. n = strtoul(rest, (char **) &rest, 0);
  536. TEST_EQUAL(n, cur->buf.len);
  537. if (*rest) {
  538. ++rest;
  539. }
  540. cur = cur->next;
  541. ++step;
  542. }
  543. TEST_ASSERT(cur == NULL);
  544. }
  545. }
  546. exit:
  547. mbedtls_asn1_sequence_free(head.next);
  548. }
  549. /* END_CASE */
  550. /* BEGIN_CASE */
  551. void traverse_sequence_of(const data_t *input,
  552. int tag_must_mask, int tag_must_val,
  553. int tag_may_mask, int tag_may_val,
  554. const char *description,
  555. int expected_result)
  556. {
  557. /* The description string is a comma-separated list of integers.
  558. * For each element in the SEQUENCE in input, description contains
  559. * three integers: the offset of the element's content (offset from
  560. * the start of input to the content of the element), the element's tag,
  561. * and the length of the element's contents.
  562. * "offset1,tag1,length1,..." */
  563. unsigned char *p = input->x;
  564. traverse_state_t traverse_state = { input->x, description };
  565. int ret;
  566. ret = mbedtls_asn1_traverse_sequence_of(&p, input->x + input->len,
  567. (uint8_t) tag_must_mask, (uint8_t) tag_must_val,
  568. (uint8_t) tag_may_mask, (uint8_t) tag_may_val,
  569. traverse_callback, &traverse_state);
  570. if (ret == RET_TRAVERSE_ERROR) {
  571. goto exit;
  572. }
  573. TEST_EQUAL(ret, expected_result);
  574. TEST_EQUAL(*traverse_state.description, 0);
  575. }
  576. /* END_CASE */
  577. /* BEGIN_CASE */
  578. void get_alg(const data_t *input,
  579. int oid_offset, int oid_length,
  580. int params_tag, int params_offset, int params_length,
  581. int total_length,
  582. int expected_result)
  583. {
  584. mbedtls_asn1_buf oid = { -1, 0, NULL };
  585. mbedtls_asn1_buf params = { -1, 0, NULL };
  586. unsigned char *p = input->x;
  587. int ret;
  588. TEST_EQUAL(mbedtls_asn1_get_alg(&p, input->x + input->len,
  589. &oid, &params),
  590. expected_result);
  591. if (expected_result == 0) {
  592. TEST_EQUAL(oid.tag, MBEDTLS_ASN1_OID);
  593. TEST_EQUAL(oid.p - input->x, oid_offset);
  594. TEST_EQUAL(oid.len, (size_t) oid_length);
  595. TEST_EQUAL(params.tag, params_tag);
  596. if (params_offset != 0) {
  597. TEST_EQUAL(params.p - input->x, params_offset);
  598. } else {
  599. TEST_ASSERT(params.p == NULL);
  600. }
  601. TEST_EQUAL(params.len, (size_t) params_length);
  602. TEST_EQUAL(p - input->x, total_length);
  603. }
  604. ret = mbedtls_asn1_get_alg_null(&p, input->x + input->len, &oid);
  605. if (expected_result == 0 && params_offset == 0) {
  606. TEST_EQUAL(oid.tag, MBEDTLS_ASN1_OID);
  607. TEST_EQUAL(oid.p - input->x, oid_offset);
  608. TEST_EQUAL(oid.len, (size_t) oid_length);
  609. TEST_EQUAL(p - input->x, total_length);
  610. } else {
  611. TEST_ASSERT(ret != 0);
  612. }
  613. }
  614. /* END_CASE */
  615. /* BEGIN_CASE */
  616. void find_named_data(data_t *oid0, data_t *oid1, data_t *oid2, data_t *oid3,
  617. data_t *needle, int from, int position)
  618. {
  619. mbedtls_asn1_named_data nd[] = {
  620. { { 0x06, oid0->len, oid0->x }, { 0, 0, NULL }, NULL, 0 },
  621. { { 0x06, oid1->len, oid1->x }, { 0, 0, NULL }, NULL, 0 },
  622. { { 0x06, oid2->len, oid2->x }, { 0, 0, NULL }, NULL, 0 },
  623. { { 0x06, oid3->len, oid3->x }, { 0, 0, NULL }, NULL, 0 },
  624. };
  625. mbedtls_asn1_named_data *pointers[ARRAY_LENGTH(nd) + 1];
  626. size_t i;
  627. const mbedtls_asn1_named_data *found;
  628. for (i = 0; i < ARRAY_LENGTH(nd); i++) {
  629. pointers[i] = &nd[i];
  630. }
  631. pointers[ARRAY_LENGTH(nd)] = NULL;
  632. for (i = 0; i < ARRAY_LENGTH(nd); i++) {
  633. nd[i].next = pointers[i+1];
  634. }
  635. found = mbedtls_asn1_find_named_data((const mbedtls_asn1_named_data *) pointers[from],
  636. (const char *) needle->x,
  637. needle->len);
  638. TEST_ASSERT(found == pointers[position]);
  639. }
  640. /* END_CASE */
  641. /* BEGIN_CASE depends_on:!MBEDTLS_DEPRECATED_REMOVED:!MBEDTLS_DEPRECATED_WARNING */
  642. void free_named_data_null()
  643. {
  644. mbedtls_asn1_free_named_data(NULL);
  645. goto exit; /* Silence unused label warning */
  646. }
  647. /* END_CASE */
  648. /* BEGIN_CASE depends_on:!MBEDTLS_DEPRECATED_REMOVED:!MBEDTLS_DEPRECATED_WARNING */
  649. void free_named_data(int with_oid, int with_val, int with_next)
  650. {
  651. mbedtls_asn1_named_data next =
  652. { { 0x06, 0, NULL }, { 0, 0xcafe, NULL }, NULL, 0 };
  653. mbedtls_asn1_named_data head =
  654. { { 0x06, 0, NULL }, { 0, 0, NULL }, NULL, 0 };
  655. if (with_oid) {
  656. ASSERT_ALLOC(head.oid.p, 1);
  657. }
  658. if (with_val) {
  659. ASSERT_ALLOC(head.val.p, 1);
  660. }
  661. if (with_next) {
  662. head.next = &next;
  663. }
  664. mbedtls_asn1_free_named_data(&head);
  665. TEST_ASSERT(head.oid.p == NULL);
  666. TEST_ASSERT(head.val.p == NULL);
  667. TEST_ASSERT(head.next == NULL);
  668. TEST_ASSERT(next.val.len == 0xcafe);
  669. exit:
  670. mbedtls_free(head.oid.p);
  671. mbedtls_free(head.val.p);
  672. }
  673. /* END_CASE */
  674. /* BEGIN_CASE */
  675. void free_named_data_list(int length)
  676. {
  677. mbedtls_asn1_named_data *head = NULL;
  678. int i;
  679. for (i = 0; i < length; i++) {
  680. mbedtls_asn1_named_data *new = NULL;
  681. ASSERT_ALLOC(new, 1);
  682. new->next = head;
  683. head = new;
  684. }
  685. mbedtls_asn1_free_named_data_list(&head);
  686. TEST_ASSERT(head == NULL);
  687. /* Most of the point of the test is that it doesn't leak memory.
  688. * So this test is only really useful under a memory leak detection
  689. * framework. */
  690. exit:
  691. mbedtls_asn1_free_named_data_list(&head);
  692. }
  693. /* END_CASE */