test_suite_x509parse.function 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/x509.h"
  3. #include "mbedtls/x509_crt.h"
  4. #include "mbedtls/x509_crl.h"
  5. #include "mbedtls/x509_csr.h"
  6. #include "mbedtls/pem.h"
  7. #include "mbedtls/oid.h"
  8. #include "mbedtls/base64.h"
  9. const mbedtls_x509_crt_profile compat_profile =
  10. {
  11. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
  12. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
  13. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
  14. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
  15. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
  16. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
  17. 0xFFFFFFF, /* Any PK alg */
  18. 0xFFFFFFF, /* Any curve */
  19. 1024,
  20. };
  21. int verify_none( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
  22. {
  23. ((void) data);
  24. ((void) crt);
  25. ((void) certificate_depth);
  26. *flags |= MBEDTLS_X509_BADCERT_OTHER;
  27. return 0;
  28. }
  29. int verify_all( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
  30. {
  31. ((void) data);
  32. ((void) crt);
  33. ((void) certificate_depth);
  34. *flags = 0;
  35. return 0;
  36. }
  37. /* strsep() not available on Windows */
  38. char *mystrsep(char **stringp, const char *delim)
  39. {
  40. const char *p;
  41. char *ret = *stringp;
  42. if( *stringp == NULL )
  43. return( NULL );
  44. for( ; ; (*stringp)++ )
  45. {
  46. if( **stringp == '\0' )
  47. {
  48. *stringp = NULL;
  49. goto done;
  50. }
  51. for( p = delim; *p != '\0'; p++ )
  52. if( **stringp == *p )
  53. {
  54. **stringp = '\0';
  55. (*stringp)++;
  56. goto done;
  57. }
  58. }
  59. done:
  60. return( ret );
  61. }
  62. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  63. typedef struct {
  64. char buf[512];
  65. char *p;
  66. } verify_print_context;
  67. void verify_print_init( verify_print_context *ctx )
  68. {
  69. memset( ctx, 0, sizeof( verify_print_context ) );
  70. ctx->p = ctx->buf;
  71. }
  72. int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
  73. {
  74. int ret;
  75. verify_print_context *ctx = (verify_print_context *) data;
  76. char *p = ctx->p;
  77. size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
  78. ((void) flags);
  79. ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth );
  80. MBEDTLS_X509_SAFE_SNPRINTF;
  81. ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
  82. MBEDTLS_X509_SAFE_SNPRINTF;
  83. ret = mbedtls_snprintf( p, n, " - subject " );
  84. MBEDTLS_X509_SAFE_SNPRINTF;
  85. ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
  86. MBEDTLS_X509_SAFE_SNPRINTF;
  87. ret = mbedtls_snprintf( p, n, "\n" );
  88. MBEDTLS_X509_SAFE_SNPRINTF;
  89. ctx->p = p;
  90. return( 0 );
  91. }
  92. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  93. /* END_HEADER */
  94. /* BEGIN_DEPENDENCIES
  95. * depends_on:MBEDTLS_BIGNUM_C
  96. * END_DEPENDENCIES
  97. */
  98. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  99. void x509_cert_info( char *crt_file, char *result_str )
  100. {
  101. mbedtls_x509_crt crt;
  102. char buf[2000];
  103. int res;
  104. mbedtls_x509_crt_init( &crt );
  105. memset( buf, 0, 2000 );
  106. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  107. res = mbedtls_x509_crt_info( buf, 2000, "", &crt );
  108. TEST_ASSERT( res != -1 );
  109. TEST_ASSERT( res != -2 );
  110. TEST_ASSERT( strcmp( buf, result_str ) == 0 );
  111. exit:
  112. mbedtls_x509_crt_free( &crt );
  113. }
  114. /* END_CASE */
  115. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
  116. void mbedtls_x509_crl_info( char *crl_file, char *result_str )
  117. {
  118. mbedtls_x509_crl crl;
  119. char buf[2000];
  120. int res;
  121. mbedtls_x509_crl_init( &crl );
  122. memset( buf, 0, 2000 );
  123. TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
  124. res = mbedtls_x509_crl_info( buf, 2000, "", &crl );
  125. TEST_ASSERT( res != -1 );
  126. TEST_ASSERT( res != -2 );
  127. TEST_ASSERT( strcmp( buf, result_str ) == 0 );
  128. exit:
  129. mbedtls_x509_crl_free( &crl );
  130. }
  131. /* END_CASE */
  132. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C */
  133. void mbedtls_x509_csr_info( char *csr_file, char *result_str )
  134. {
  135. mbedtls_x509_csr csr;
  136. char buf[2000];
  137. int res;
  138. mbedtls_x509_csr_init( &csr );
  139. memset( buf, 0, 2000 );
  140. TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 );
  141. res = mbedtls_x509_csr_info( buf, 2000, "", &csr );
  142. TEST_ASSERT( res != -1 );
  143. TEST_ASSERT( res != -2 );
  144. TEST_ASSERT( strcmp( buf, result_str ) == 0 );
  145. exit:
  146. mbedtls_x509_csr_free( &csr );
  147. }
  148. /* END_CASE */
  149. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
  150. void x509_verify_info( int flags, char *prefix, char *result_str )
  151. {
  152. char buf[2000];
  153. int res;
  154. memset( buf, 0, sizeof( buf ) );
  155. res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
  156. TEST_ASSERT( res >= 0 );
  157. TEST_ASSERT( strcmp( buf, result_str ) == 0 );
  158. }
  159. /* END_CASE */
  160. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
  161. void x509_verify( char *crt_file, char *ca_file, char *crl_file,
  162. char *cn_name_str, int result, int flags_result,
  163. char *verify_callback )
  164. {
  165. mbedtls_x509_crt crt;
  166. mbedtls_x509_crt ca;
  167. mbedtls_x509_crl crl;
  168. uint32_t flags = 0;
  169. int res;
  170. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
  171. char * cn_name = NULL;
  172. mbedtls_x509_crt_init( &crt );
  173. mbedtls_x509_crt_init( &ca );
  174. mbedtls_x509_crl_init( &crl );
  175. if( strcmp( cn_name_str, "NULL" ) != 0 )
  176. cn_name = cn_name_str;
  177. if( strcmp( verify_callback, "NULL" ) == 0 )
  178. f_vrfy = NULL;
  179. else if( strcmp( verify_callback, "verify_none" ) == 0 )
  180. f_vrfy = verify_none;
  181. else if( strcmp( verify_callback, "verify_all" ) == 0 )
  182. f_vrfy = verify_all;
  183. else
  184. TEST_ASSERT( "No known verify callback selected" == 0 );
  185. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  186. TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
  187. TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
  188. res = mbedtls_x509_crt_verify_with_profile( &crt, &ca, &crl, &compat_profile, cn_name, &flags, f_vrfy, NULL );
  189. TEST_ASSERT( res == ( result ) );
  190. TEST_ASSERT( flags == (uint32_t)( flags_result ) );
  191. exit:
  192. mbedtls_x509_crt_free( &crt );
  193. mbedtls_x509_crt_free( &ca );
  194. mbedtls_x509_crl_free( &crl );
  195. }
  196. /* END_CASE */
  197. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  198. void x509_verify_callback( char *crt_file, char *ca_file,
  199. int exp_ret, char *exp_vrfy_out )
  200. {
  201. int ret;
  202. mbedtls_x509_crt crt;
  203. mbedtls_x509_crt ca;
  204. uint32_t flags = 0;
  205. verify_print_context vrfy_ctx;
  206. mbedtls_x509_crt_init( &crt );
  207. mbedtls_x509_crt_init( &ca );
  208. verify_print_init( &vrfy_ctx );
  209. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  210. TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
  211. ret = mbedtls_x509_crt_verify( &crt, &ca, NULL, NULL, &flags,
  212. verify_print, &vrfy_ctx );
  213. TEST_ASSERT( ret == exp_ret );
  214. TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
  215. exit:
  216. mbedtls_x509_crt_free( &crt );
  217. mbedtls_x509_crt_free( &ca );
  218. }
  219. /* END_CASE */
  220. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  221. void mbedtls_x509_dn_gets( char *crt_file, char *entity, char *result_str )
  222. {
  223. mbedtls_x509_crt crt;
  224. char buf[2000];
  225. int res = 0;
  226. mbedtls_x509_crt_init( &crt );
  227. memset( buf, 0, 2000 );
  228. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  229. if( strcmp( entity, "subject" ) == 0 )
  230. res = mbedtls_x509_dn_gets( buf, 2000, &crt.subject );
  231. else if( strcmp( entity, "issuer" ) == 0 )
  232. res = mbedtls_x509_dn_gets( buf, 2000, &crt.issuer );
  233. else
  234. TEST_ASSERT( "Unknown entity" == 0 );
  235. TEST_ASSERT( res != -1 );
  236. TEST_ASSERT( res != -2 );
  237. TEST_ASSERT( strcmp( buf, result_str ) == 0 );
  238. exit:
  239. mbedtls_x509_crt_free( &crt );
  240. }
  241. /* END_CASE */
  242. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  243. void mbedtls_x509_time_is_past( char *crt_file, char *entity, int result )
  244. {
  245. mbedtls_x509_crt crt;
  246. mbedtls_x509_crt_init( &crt );
  247. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  248. if( strcmp( entity, "valid_from" ) == 0 )
  249. TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_from ) == result );
  250. else if( strcmp( entity, "valid_to" ) == 0 )
  251. TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_to ) == result );
  252. else
  253. TEST_ASSERT( "Unknown entity" == 0 );
  254. exit:
  255. mbedtls_x509_crt_free( &crt );
  256. }
  257. /* END_CASE */
  258. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  259. void mbedtls_x509_time_is_future( char *crt_file, char *entity, int result )
  260. {
  261. mbedtls_x509_crt crt;
  262. mbedtls_x509_crt_init( &crt );
  263. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  264. if( strcmp( entity, "valid_from" ) == 0 )
  265. TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_from ) == result );
  266. else if( strcmp( entity, "valid_to" ) == 0 )
  267. TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_to ) == result );
  268. else
  269. TEST_ASSERT( "Unknown entity" == 0 );
  270. exit:
  271. mbedtls_x509_crt_free( &crt );
  272. }
  273. /* END_CASE */
  274. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
  275. void x509parse_crt_file( char *crt_file, int result )
  276. {
  277. mbedtls_x509_crt crt;
  278. mbedtls_x509_crt_init( &crt );
  279. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );
  280. exit:
  281. mbedtls_x509_crt_free( &crt );
  282. }
  283. /* END_CASE */
  284. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
  285. void x509parse_crt( char *crt_data, char *result_str, int result )
  286. {
  287. mbedtls_x509_crt crt;
  288. unsigned char buf[2000];
  289. unsigned char output[2000];
  290. int data_len, res;
  291. mbedtls_x509_crt_init( &crt );
  292. memset( buf, 0, 2000 );
  293. memset( output, 0, 2000 );
  294. data_len = unhexify( buf, crt_data );
  295. TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf, data_len ) == ( result ) );
  296. if( ( result ) == 0 )
  297. {
  298. res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
  299. TEST_ASSERT( res != -1 );
  300. TEST_ASSERT( res != -2 );
  301. TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
  302. }
  303. exit:
  304. mbedtls_x509_crt_free( &crt );
  305. }
  306. /* END_CASE */
  307. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */
  308. void x509parse_crl( char *crl_data, char *result_str, int result )
  309. {
  310. mbedtls_x509_crl crl;
  311. unsigned char buf[2000];
  312. unsigned char output[2000];
  313. int data_len, res;
  314. mbedtls_x509_crl_init( &crl );
  315. memset( buf, 0, 2000 );
  316. memset( output, 0, 2000 );
  317. data_len = unhexify( buf, crl_data );
  318. TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf, data_len ) == ( result ) );
  319. if( ( result ) == 0 )
  320. {
  321. res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
  322. TEST_ASSERT( res != -1 );
  323. TEST_ASSERT( res != -2 );
  324. TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
  325. }
  326. exit:
  327. mbedtls_x509_crl_free( &crl );
  328. }
  329. /* END_CASE */
  330. /* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
  331. void mbedtls_x509_csr_parse( char *csr_der_hex, char *ref_out, int ref_ret )
  332. {
  333. mbedtls_x509_csr csr;
  334. unsigned char *csr_der = NULL;
  335. char my_out[1000];
  336. size_t csr_der_len;
  337. int my_ret;
  338. mbedtls_x509_csr_init( &csr );
  339. memset( my_out, 0, sizeof( my_out ) );
  340. csr_der = unhexify_alloc( csr_der_hex, &csr_der_len );
  341. my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der, csr_der_len );
  342. TEST_ASSERT( my_ret == ref_ret );
  343. if( ref_ret == 0 )
  344. {
  345. size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
  346. TEST_ASSERT( my_out_len == strlen( ref_out ) );
  347. TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
  348. }
  349. exit:
  350. mbedtls_x509_csr_free( &csr );
  351. mbedtls_free( csr_der );
  352. }
  353. /* END_CASE */
  354. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  355. void mbedtls_x509_crt_parse_path( char *crt_path, int ret, int nb_crt )
  356. {
  357. mbedtls_x509_crt chain, *cur;
  358. int i;
  359. mbedtls_x509_crt_init( &chain );
  360. TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
  361. /* Check how many certs we got */
  362. for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
  363. if( cur->raw.p != NULL )
  364. i++;
  365. TEST_ASSERT( i == nb_crt );
  366. exit:
  367. mbedtls_x509_crt_free( &chain );
  368. }
  369. /* END_CASE */
  370. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  371. void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca, int flags_result )
  372. {
  373. char* act;
  374. uint32_t flags;
  375. int result, res;
  376. mbedtls_x509_crt trusted, chain;
  377. result= flags_result?MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:0;
  378. mbedtls_x509_crt_init( &chain );
  379. mbedtls_x509_crt_init( &trusted );
  380. while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
  381. TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
  382. TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
  383. res = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags, NULL, NULL );
  384. TEST_ASSERT( res == ( result ) );
  385. TEST_ASSERT( flags == (uint32_t)( flags_result ) );
  386. exit:
  387. mbedtls_x509_crt_free( &trusted );
  388. mbedtls_x509_crt_free( &chain );
  389. }
  390. /* END_CASE */
  391. /* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
  392. void x509_oid_desc( char *oid_str, char *ref_desc )
  393. {
  394. mbedtls_x509_buf oid;
  395. const char *desc = NULL;
  396. unsigned char buf[20];
  397. int ret;
  398. memset( buf, 0, sizeof buf );
  399. oid.tag = MBEDTLS_ASN1_OID;
  400. oid.len = unhexify( buf, oid_str );
  401. oid.p = buf;
  402. ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
  403. if( strcmp( ref_desc, "notfound" ) == 0 )
  404. {
  405. TEST_ASSERT( ret != 0 );
  406. TEST_ASSERT( desc == NULL );
  407. }
  408. else
  409. {
  410. TEST_ASSERT( ret == 0 );
  411. TEST_ASSERT( desc != NULL );
  412. TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
  413. }
  414. }
  415. /* END_CASE */
  416. /* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
  417. void x509_oid_numstr( char *oid_str, char *numstr, int blen, int ret )
  418. {
  419. mbedtls_x509_buf oid;
  420. unsigned char oid_buf[20];
  421. char num_buf[100];
  422. memset( oid_buf, 0x00, sizeof oid_buf );
  423. memset( num_buf, 0x2a, sizeof num_buf );
  424. oid.tag = MBEDTLS_ASN1_OID;
  425. oid.len = unhexify( oid_buf, oid_str );
  426. oid.p = oid_buf;
  427. TEST_ASSERT( (size_t) blen <= sizeof num_buf );
  428. TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
  429. if( ret >= 0 )
  430. {
  431. TEST_ASSERT( num_buf[ret] == 0 );
  432. TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
  433. }
  434. }
  435. /* END_CASE */
  436. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */
  437. void x509_check_key_usage( char *crt_file, int usage, int ret )
  438. {
  439. mbedtls_x509_crt crt;
  440. mbedtls_x509_crt_init( &crt );
  441. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  442. TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
  443. exit:
  444. mbedtls_x509_crt_free( &crt );
  445. }
  446. /* END_CASE */
  447. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
  448. void x509_check_extended_key_usage( char *crt_file, char *usage_hex, int ret )
  449. {
  450. mbedtls_x509_crt crt;
  451. char oid[50];
  452. size_t len;
  453. mbedtls_x509_crt_init( &crt );
  454. len = unhexify( (unsigned char *) oid, usage_hex );
  455. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  456. TEST_ASSERT( mbedtls_x509_crt_check_extended_key_usage( &crt, oid, len ) == ret );
  457. exit:
  458. mbedtls_x509_crt_free( &crt );
  459. }
  460. /* END_CASE */
  461. /* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
  462. void x509_get_time( int tag, char *time_str, int ret,
  463. int year, int mon, int day,
  464. int hour, int min, int sec )
  465. {
  466. mbedtls_x509_time time;
  467. unsigned char buf[17];
  468. unsigned char* start = buf;
  469. unsigned char* end = buf;
  470. memset( &time, 0x00, sizeof( time ) );
  471. *end = (unsigned char)tag; end++;
  472. if( tag == MBEDTLS_ASN1_UTC_TIME )
  473. *end = 13;
  474. else
  475. *end = 15;
  476. end++;
  477. memcpy( end, time_str, (size_t)*(end - 1) );
  478. end += *(end - 1);
  479. TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
  480. if( ret == 0 )
  481. {
  482. TEST_ASSERT( year == time.year );
  483. TEST_ASSERT( mon == time.mon );
  484. TEST_ASSERT( day == time.day );
  485. TEST_ASSERT( hour == time.hour );
  486. TEST_ASSERT( min == time.min );
  487. TEST_ASSERT( sec == time.sec );
  488. }
  489. }
  490. /* END_CASE */
  491. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  492. void x509_parse_rsassa_pss_params( char *hex_params, int params_tag,
  493. int ref_msg_md, int ref_mgf_md,
  494. int ref_salt_len, int ref_ret )
  495. {
  496. int my_ret;
  497. mbedtls_x509_buf params;
  498. mbedtls_md_type_t my_msg_md, my_mgf_md;
  499. int my_salt_len;
  500. params.p = unhexify_alloc( hex_params, &params.len );
  501. params.tag = params_tag;
  502. my_ret = mbedtls_x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
  503. &my_salt_len );
  504. TEST_ASSERT( my_ret == ref_ret );
  505. if( ref_ret == 0 )
  506. {
  507. TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
  508. TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
  509. TEST_ASSERT( my_salt_len == ref_salt_len );
  510. }
  511. exit:
  512. mbedtls_free( params.p );
  513. }
  514. /* END_CASE */
  515. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
  516. void x509_selftest()
  517. {
  518. TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
  519. }
  520. /* END_CASE */