test_suite_asn1write.function 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/asn1write.h"
  3. #define GUARD_LEN 4
  4. #define GUARD_VAL 0x2a
  5. typedef struct
  6. {
  7. unsigned char *output;
  8. unsigned char *start;
  9. unsigned char *end;
  10. unsigned char *p;
  11. size_t size;
  12. } generic_write_data_t;
  13. int generic_write_start_step( generic_write_data_t *data )
  14. {
  15. mbedtls_test_set_step( data->size );
  16. ASSERT_ALLOC( data->output, data->size == 0 ? 1 : data->size );
  17. data->end = data->output + data->size;
  18. data->p = data->end;
  19. data->start = data->end - data->size;
  20. return( 1 );
  21. exit:
  22. return( 0 );
  23. }
  24. int generic_write_finish_step( generic_write_data_t *data,
  25. const data_t *expected, int ret )
  26. {
  27. int ok = 0;
  28. if( data->size < expected->len )
  29. {
  30. TEST_EQUAL( ret, MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  31. }
  32. else
  33. {
  34. TEST_EQUAL( ret, data->end - data->p );
  35. TEST_ASSERT( data->p >= data->start );
  36. TEST_ASSERT( data->p <= data->end );
  37. ASSERT_COMPARE( data->p, (size_t)( data->end - data->p ),
  38. expected->x, expected->len );
  39. }
  40. ok = 1;
  41. exit:
  42. mbedtls_free( data->output );
  43. data->output = NULL;
  44. return( ok );
  45. }
  46. /* END_HEADER */
  47. /* BEGIN_DEPENDENCIES
  48. * depends_on:MBEDTLS_ASN1_WRITE_C
  49. * END_DEPENDENCIES
  50. */
  51. /* BEGIN_CASE */
  52. void mbedtls_asn1_write_null( data_t *expected )
  53. {
  54. generic_write_data_t data = { NULL, NULL, NULL, NULL, 0 };
  55. int ret;
  56. for( data.size = 0; data.size < expected->len + 1; data.size++ )
  57. {
  58. if( ! generic_write_start_step( &data ) )
  59. goto exit;
  60. ret = mbedtls_asn1_write_null( &data.p, data.start );
  61. if( ! generic_write_finish_step( &data, expected, ret ) )
  62. goto exit;
  63. }
  64. exit:
  65. mbedtls_free( data.output );
  66. }
  67. /* END_CASE */
  68. /* BEGIN_CASE */
  69. void mbedtls_asn1_write_bool( int val, data_t *expected )
  70. {
  71. generic_write_data_t data = { NULL, NULL, NULL, NULL, 0 };
  72. int ret;
  73. for( data.size = 0; data.size < expected->len + 1; data.size++ )
  74. {
  75. if( ! generic_write_start_step( &data ) )
  76. goto exit;
  77. ret = mbedtls_asn1_write_bool( &data.p, data.start, val );
  78. if( ! generic_write_finish_step( &data, expected, ret ) )
  79. goto exit;
  80. }
  81. exit:
  82. mbedtls_free( data.output );
  83. }
  84. /* END_CASE */
  85. /* BEGIN_CASE */
  86. void mbedtls_asn1_write_int( int val, data_t *expected )
  87. {
  88. generic_write_data_t data = { NULL, NULL, NULL, NULL, 0 };
  89. int ret;
  90. for( data.size = 0; data.size < expected->len + 1; data.size++ )
  91. {
  92. if( ! generic_write_start_step( &data ) )
  93. goto exit;
  94. ret = mbedtls_asn1_write_int( &data.p, data.start, val );
  95. if( ! generic_write_finish_step( &data, expected, ret ) )
  96. goto exit;
  97. }
  98. exit:
  99. mbedtls_free( data.output );
  100. }
  101. /* END_CASE */
  102. /* BEGIN_CASE */
  103. void mbedtls_asn1_write_enum( int val, data_t *expected )
  104. {
  105. generic_write_data_t data = { NULL, NULL, NULL, NULL, 0 };
  106. int ret;
  107. for( data.size = 0; data.size < expected->len + 1; data.size++ )
  108. {
  109. if( ! generic_write_start_step( &data ) )
  110. goto exit;
  111. ret = mbedtls_asn1_write_enum( &data.p, data.start, val );
  112. if( ! generic_write_finish_step( &data, expected, ret ) )
  113. goto exit;
  114. }
  115. exit:
  116. mbedtls_free( data.output );
  117. }
  118. /* END_CASE */
  119. /* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
  120. void mbedtls_asn1_write_mpi( data_t *val, data_t *expected )
  121. {
  122. generic_write_data_t data = { NULL, NULL, NULL, NULL, 0 };
  123. mbedtls_mpi mpi;
  124. int ret;
  125. mbedtls_mpi_init( &mpi );
  126. TEST_ASSERT( mbedtls_mpi_read_binary( &mpi, val->x, val->len ) == 0 );
  127. for( data.size = 0; data.size < expected->len + 1; data.size++ )
  128. {
  129. if( ! generic_write_start_step( &data ) )
  130. goto exit;
  131. ret = mbedtls_asn1_write_mpi( &data.p, data.start, &mpi );
  132. if( ! generic_write_finish_step( &data, expected, ret ) )
  133. goto exit;
  134. if( expected->len > 10 && data.size == 8 )
  135. data.size = expected->len - 2;
  136. }
  137. exit:
  138. mbedtls_mpi_free( &mpi );
  139. mbedtls_free( data.output );
  140. }
  141. /* END_CASE */
  142. /* BEGIN_CASE */
  143. void mbedtls_asn1_write_string( int tag, data_t *content, data_t *expected )
  144. {
  145. generic_write_data_t data = { NULL, NULL, NULL, NULL, 0 };
  146. int ret;
  147. for( data.size = 0; data.size < expected->len + 1; data.size++ )
  148. {
  149. if( ! generic_write_start_step( &data ) )
  150. goto exit;
  151. switch( tag )
  152. {
  153. case MBEDTLS_ASN1_OCTET_STRING:
  154. ret = mbedtls_asn1_write_octet_string(
  155. &data.p, data.start, content->x, content->len );
  156. break;
  157. case MBEDTLS_ASN1_OID:
  158. ret = mbedtls_asn1_write_oid(
  159. &data.p, data.start,
  160. (const char *) content->x, content->len );
  161. break;
  162. case MBEDTLS_ASN1_UTF8_STRING:
  163. ret = mbedtls_asn1_write_utf8_string(
  164. &data.p, data.start,
  165. (const char *) content->x, content->len );
  166. break;
  167. case MBEDTLS_ASN1_PRINTABLE_STRING:
  168. ret = mbedtls_asn1_write_printable_string(
  169. &data.p, data.start,
  170. (const char *) content->x, content->len );
  171. break;
  172. case MBEDTLS_ASN1_IA5_STRING:
  173. ret = mbedtls_asn1_write_ia5_string(
  174. &data.p, data.start,
  175. (const char *) content->x, content->len );
  176. break;
  177. default:
  178. ret = mbedtls_asn1_write_tagged_string(
  179. &data.p, data.start, tag,
  180. (const char *) content->x, content->len );
  181. }
  182. if( ! generic_write_finish_step( &data, expected, ret ) )
  183. goto exit;
  184. if( expected->len > 10 && data.size == 8 )
  185. data.size = expected->len - 2;
  186. }
  187. exit:
  188. mbedtls_free( data.output );
  189. }
  190. /* END_CASE */
  191. /* BEGIN_CASE */
  192. void mbedtls_asn1_write_algorithm_identifier( data_t *oid,
  193. int par_len,
  194. data_t *expected )
  195. {
  196. generic_write_data_t data = { NULL, NULL, NULL, NULL, 0 };
  197. int ret;
  198. for( data.size = 0; data.size < expected->len + 1; data.size++ )
  199. {
  200. if( ! generic_write_start_step( &data ) )
  201. goto exit;
  202. ret = mbedtls_asn1_write_algorithm_identifier(
  203. &data.p, data.start,
  204. (const char *) oid->x, oid->len, par_len );
  205. /* If params_len != 0, mbedtls_asn1_write_algorithm_identifier()
  206. * assumes that the parameters are already present in the buffer
  207. * and returns a length that accounts for this, but our test
  208. * data omits the parameters. */
  209. if( ret >= 0 )
  210. ret -= par_len;
  211. if( ! generic_write_finish_step( &data, expected, ret ) )
  212. goto exit;
  213. }
  214. exit:
  215. mbedtls_free( data.output );
  216. }
  217. /* END_CASE */
  218. /* BEGIN_CASE depends_on:MBEDTLS_ASN1_PARSE_C */
  219. void mbedtls_asn1_write_len( int len, data_t * asn1, int buf_len,
  220. int result )
  221. {
  222. int ret;
  223. unsigned char buf[150];
  224. unsigned char *p;
  225. size_t i;
  226. size_t read_len;
  227. memset( buf, GUARD_VAL, sizeof( buf ) );
  228. p = buf + GUARD_LEN + buf_len;
  229. ret = mbedtls_asn1_write_len( &p, buf + GUARD_LEN, (size_t) len );
  230. TEST_ASSERT( ret == result );
  231. /* Check for buffer overwrite on both sides */
  232. for( i = 0; i < GUARD_LEN; i++ )
  233. {
  234. TEST_ASSERT( buf[i] == GUARD_VAL );
  235. TEST_ASSERT( buf[GUARD_LEN + buf_len + i] == GUARD_VAL );
  236. }
  237. if( result >= 0 )
  238. {
  239. TEST_ASSERT( p + asn1->len == buf + GUARD_LEN + buf_len );
  240. TEST_ASSERT( memcmp( p, asn1->x, asn1->len ) == 0 );
  241. /* Read back with mbedtls_asn1_get_len() to check */
  242. ret = mbedtls_asn1_get_len( &p, buf + GUARD_LEN + buf_len, &read_len );
  243. if( len == 0 )
  244. {
  245. TEST_ASSERT( ret == 0 );
  246. }
  247. else
  248. {
  249. /* Return will be MBEDTLS_ERR_ASN1_OUT_OF_DATA because the rest of
  250. * the buffer is missing
  251. */
  252. TEST_ASSERT( ret == MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  253. }
  254. TEST_ASSERT( read_len == (size_t) len );
  255. TEST_ASSERT( p == buf + GUARD_LEN + buf_len );
  256. }
  257. }
  258. /* END_CASE */
  259. /* BEGIN_CASE */
  260. void test_asn1_write_bitstrings( data_t *bitstring, int bits,
  261. data_t *expected, int is_named )
  262. {
  263. generic_write_data_t data = { NULL, NULL, NULL, NULL, 0 };
  264. int ret;
  265. int ( *func )( unsigned char **p, unsigned char *start,
  266. const unsigned char *buf, size_t bits ) =
  267. ( is_named ? mbedtls_asn1_write_named_bitstring :
  268. mbedtls_asn1_write_bitstring );
  269. for( data.size = 0; data.size < expected->len + 1; data.size++ )
  270. {
  271. if( ! generic_write_start_step( &data ) )
  272. goto exit;
  273. ret = ( *func )( &data.p, data.start, bitstring->x, bits );
  274. if( ! generic_write_finish_step( &data, expected, ret ) )
  275. goto exit;
  276. }
  277. exit:
  278. mbedtls_free( data.output );
  279. }
  280. /* END_CASE */
  281. /* BEGIN_CASE */
  282. void store_named_data_find( data_t *oid0, data_t *oid1,
  283. data_t *oid2, data_t *oid3,
  284. data_t *needle, int from, int position )
  285. {
  286. data_t *oid[4] = {oid0, oid1, oid2, oid3};
  287. mbedtls_asn1_named_data nd[] ={
  288. { {0x06, 0, NULL}, {0, 0, NULL}, NULL, 0 },
  289. { {0x06, 0, NULL}, {0, 0, NULL}, NULL, 0 },
  290. { {0x06, 0, NULL}, {0, 0, NULL}, NULL, 0 },
  291. { {0x06, 0, NULL}, {0, 0, NULL}, NULL, 0 },
  292. };
  293. mbedtls_asn1_named_data *pointers[ARRAY_LENGTH( nd ) + 1];
  294. size_t i;
  295. mbedtls_asn1_named_data *head = NULL;
  296. mbedtls_asn1_named_data *found = NULL;
  297. for( i = 0; i < ARRAY_LENGTH( nd ); i++ )
  298. pointers[i] = &nd[i];
  299. pointers[ARRAY_LENGTH( nd )] = NULL;
  300. for( i = 0; i < ARRAY_LENGTH( nd ); i++ )
  301. {
  302. ASSERT_ALLOC( nd[i].oid.p, oid[i]->len );
  303. memcpy( nd[i].oid.p, oid[i]->x, oid[i]->len );
  304. nd[i].oid.len = oid[i]->len;
  305. nd[i].next = pointers[i+1];
  306. }
  307. head = pointers[from];
  308. found = mbedtls_asn1_store_named_data( &head,
  309. (const char *) needle->x,
  310. needle->len,
  311. NULL, 0 );
  312. /* In any case, the existing list structure must be unchanged. */
  313. for( i = 0; i < ARRAY_LENGTH( nd ); i++ )
  314. TEST_ASSERT( nd[i].next == pointers[i+1] );
  315. if( position >= 0 )
  316. {
  317. /* position should have been found and modified. */
  318. TEST_ASSERT( head == pointers[from] );
  319. TEST_ASSERT( found == pointers[position] );
  320. }
  321. else
  322. {
  323. /* A new entry should have been created. */
  324. TEST_ASSERT( found == head );
  325. TEST_ASSERT( head->next == pointers[from] );
  326. for( i = 0; i < ARRAY_LENGTH( nd ); i++ )
  327. TEST_ASSERT( found != &nd[i] );
  328. }
  329. exit:
  330. if( found != NULL && found == head && found != pointers[from] )
  331. {
  332. mbedtls_free( found->oid.p );
  333. mbedtls_free( found );
  334. }
  335. for( i = 0; i < ARRAY_LENGTH( nd ); i++ )
  336. mbedtls_free( nd[i].oid.p );
  337. }
  338. /* END_CASE */
  339. /* BEGIN_CASE */
  340. void store_named_data_val_found( int old_len, int new_len )
  341. {
  342. mbedtls_asn1_named_data nd =
  343. { {0x06, 3, (unsigned char *) "OID"}, {0, 0, NULL}, NULL, 0 };
  344. mbedtls_asn1_named_data *head = &nd;
  345. mbedtls_asn1_named_data *found = NULL;
  346. unsigned char *old_val = NULL;
  347. unsigned char *new_val = (unsigned char *) "new value";
  348. if( old_len != 0 )
  349. {
  350. ASSERT_ALLOC( nd.val.p, (size_t) old_len );
  351. old_val = nd.val.p;
  352. nd.val.len = old_len;
  353. memset( old_val, 'x', old_len );
  354. }
  355. if( new_len <= 0 )
  356. {
  357. new_len = - new_len;
  358. new_val = NULL;
  359. }
  360. found = mbedtls_asn1_store_named_data( &head, "OID", 3,
  361. new_val, new_len );
  362. TEST_ASSERT( head == &nd );
  363. TEST_ASSERT( found == head );
  364. if( new_val != NULL)
  365. ASSERT_COMPARE( found->val.p, found->val.len,
  366. new_val, (size_t) new_len );
  367. if( new_len == 0)
  368. TEST_ASSERT( found->val.p == NULL );
  369. else if( new_len == old_len )
  370. TEST_ASSERT( found->val.p == old_val );
  371. else
  372. TEST_ASSERT( found->val.p != old_val );
  373. exit:
  374. mbedtls_free( nd.val.p );
  375. }
  376. /* END_CASE */
  377. /* BEGIN_CASE */
  378. void store_named_data_val_new( int new_len )
  379. {
  380. mbedtls_asn1_named_data *head = NULL;
  381. mbedtls_asn1_named_data *found = NULL;
  382. const unsigned char *oid = (unsigned char *) "OID";
  383. size_t oid_len = strlen( (const char *) oid );
  384. const unsigned char *new_val = (unsigned char *) "new value";
  385. if( new_len <= 0 )
  386. new_val = NULL;
  387. if( new_len < 0 )
  388. new_len = - new_len;
  389. found = mbedtls_asn1_store_named_data( &head,
  390. (const char *) oid, oid_len,
  391. new_val, (size_t) new_len );
  392. TEST_ASSERT( found != NULL );
  393. TEST_ASSERT( found == head );
  394. TEST_ASSERT( found->oid.p != oid );
  395. ASSERT_COMPARE( found->oid.p, found->oid.len, oid, oid_len );
  396. if( new_len == 0 )
  397. TEST_ASSERT( found->val.p == NULL );
  398. else if( new_val == NULL )
  399. TEST_ASSERT( found->val.p != NULL );
  400. else
  401. {
  402. TEST_ASSERT( found->val.p != new_val );
  403. ASSERT_COMPARE( found->val.p, found->val.len,
  404. new_val, (size_t) new_len );
  405. }
  406. exit:
  407. if( found != NULL )
  408. {
  409. mbedtls_free( found->oid.p );
  410. mbedtls_free( found->val.p );
  411. }
  412. mbedtls_free( found );
  413. }
  414. /* END_CASE */