test_suite_x509parse.function 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/bignum.h"
  3. #include "mbedtls/x509.h"
  4. #include "mbedtls/x509_crt.h"
  5. #include "mbedtls/x509_crl.h"
  6. #include "mbedtls/x509_csr.h"
  7. #include "mbedtls/pem.h"
  8. #include "mbedtls/oid.h"
  9. #include "mbedtls/base64.h"
  10. #include "string.h"
  11. #if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
  12. #error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
  13. than the current threshold 19. To test larger values, please \
  14. adapt the script tests/data_files/dir-max/long.sh."
  15. #endif
  16. /* Test-only profile allowing all digests, PK algorithms, and curves. */
  17. const mbedtls_x509_crt_profile profile_all =
  18. {
  19. 0xFFFFFFFF, /* Any MD */
  20. 0xFFFFFFFF, /* Any PK alg */
  21. 0xFFFFFFFF, /* Any curve */
  22. 1024,
  23. };
  24. /* Profile for backward compatibility. Allows SHA-1, unlike the default
  25. profile. */
  26. const mbedtls_x509_crt_profile compat_profile =
  27. {
  28. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
  29. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
  30. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
  31. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
  32. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
  33. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
  34. 0xFFFFFFF, /* Any PK alg */
  35. 0xFFFFFFF, /* Any curve */
  36. 1024,
  37. };
  38. const mbedtls_x509_crt_profile profile_rsa3072 =
  39. {
  40. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
  41. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
  42. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
  43. MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA ),
  44. 0,
  45. 3072,
  46. };
  47. const mbedtls_x509_crt_profile profile_sha512 =
  48. {
  49. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
  50. 0xFFFFFFF, /* Any PK alg */
  51. 0xFFFFFFF, /* Any curve */
  52. 1024,
  53. };
  54. int verify_none( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
  55. {
  56. ((void) data);
  57. ((void) crt);
  58. ((void) certificate_depth);
  59. *flags |= MBEDTLS_X509_BADCERT_OTHER;
  60. return 0;
  61. }
  62. int verify_all( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
  63. {
  64. ((void) data);
  65. ((void) crt);
  66. ((void) certificate_depth);
  67. *flags = 0;
  68. return 0;
  69. }
  70. int verify_fatal( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
  71. {
  72. int *levels = (int *) data;
  73. ((void) crt);
  74. ((void) certificate_depth);
  75. /* Simulate a fatal error in the callback */
  76. if( *levels & ( 1 << certificate_depth ) )
  77. {
  78. *flags |= ( 1 << certificate_depth );
  79. return( -1 - certificate_depth );
  80. }
  81. return( 0 );
  82. }
  83. /* strsep() not available on Windows */
  84. char *mystrsep(char **stringp, const char *delim)
  85. {
  86. const char *p;
  87. char *ret = *stringp;
  88. if( *stringp == NULL )
  89. return( NULL );
  90. for( ; ; (*stringp)++ )
  91. {
  92. if( **stringp == '\0' )
  93. {
  94. *stringp = NULL;
  95. goto done;
  96. }
  97. for( p = delim; *p != '\0'; p++ )
  98. if( **stringp == *p )
  99. {
  100. **stringp = '\0';
  101. (*stringp)++;
  102. goto done;
  103. }
  104. }
  105. done:
  106. return( ret );
  107. }
  108. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  109. typedef struct {
  110. char buf[512];
  111. char *p;
  112. } verify_print_context;
  113. void verify_print_init( verify_print_context *ctx )
  114. {
  115. memset( ctx, 0, sizeof( verify_print_context ) );
  116. ctx->p = ctx->buf;
  117. }
  118. int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
  119. {
  120. int ret;
  121. verify_print_context *ctx = (verify_print_context *) data;
  122. char *p = ctx->p;
  123. size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
  124. ((void) flags);
  125. ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth );
  126. MBEDTLS_X509_SAFE_SNPRINTF;
  127. ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
  128. MBEDTLS_X509_SAFE_SNPRINTF;
  129. ret = mbedtls_snprintf( p, n, " - subject " );
  130. MBEDTLS_X509_SAFE_SNPRINTF;
  131. ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
  132. MBEDTLS_X509_SAFE_SNPRINTF;
  133. ret = mbedtls_snprintf( p, n, " - flags 0x%08x\n", *flags );
  134. MBEDTLS_X509_SAFE_SNPRINTF;
  135. ctx->p = p;
  136. return( 0 );
  137. }
  138. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  139. /* END_HEADER */
  140. /* BEGIN_DEPENDENCIES
  141. * depends_on:MBEDTLS_BIGNUM_C
  142. * END_DEPENDENCIES
  143. */
  144. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  145. void x509_cert_info( char * crt_file, char * result_str )
  146. {
  147. mbedtls_x509_crt crt;
  148. char buf[2000];
  149. int res;
  150. mbedtls_x509_crt_init( &crt );
  151. memset( buf, 0, 2000 );
  152. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  153. res = mbedtls_x509_crt_info( buf, 2000, "", &crt );
  154. TEST_ASSERT( res != -1 );
  155. TEST_ASSERT( res != -2 );
  156. TEST_ASSERT( strcmp( buf, result_str ) == 0 );
  157. exit:
  158. mbedtls_x509_crt_free( &crt );
  159. }
  160. /* END_CASE */
  161. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
  162. void mbedtls_x509_crl_info( char * crl_file, char * result_str )
  163. {
  164. mbedtls_x509_crl crl;
  165. char buf[2000];
  166. int res;
  167. mbedtls_x509_crl_init( &crl );
  168. memset( buf, 0, 2000 );
  169. TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
  170. res = mbedtls_x509_crl_info( buf, 2000, "", &crl );
  171. TEST_ASSERT( res != -1 );
  172. TEST_ASSERT( res != -2 );
  173. TEST_ASSERT( strcmp( buf, result_str ) == 0 );
  174. exit:
  175. mbedtls_x509_crl_free( &crl );
  176. }
  177. /* END_CASE */
  178. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
  179. void mbedtls_x509_crl_parse( char * crl_file, int result )
  180. {
  181. mbedtls_x509_crl crl;
  182. char buf[2000];
  183. mbedtls_x509_crl_init( &crl );
  184. memset( buf, 0, 2000 );
  185. TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == result );
  186. exit:
  187. mbedtls_x509_crl_free( &crl );
  188. }
  189. /* END_CASE */
  190. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C */
  191. void mbedtls_x509_csr_info( char * csr_file, char * result_str )
  192. {
  193. mbedtls_x509_csr csr;
  194. char buf[2000];
  195. int res;
  196. mbedtls_x509_csr_init( &csr );
  197. memset( buf, 0, 2000 );
  198. TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 );
  199. res = mbedtls_x509_csr_info( buf, 2000, "", &csr );
  200. TEST_ASSERT( res != -1 );
  201. TEST_ASSERT( res != -2 );
  202. TEST_ASSERT( strcmp( buf, result_str ) == 0 );
  203. exit:
  204. mbedtls_x509_csr_free( &csr );
  205. }
  206. /* END_CASE */
  207. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
  208. void x509_verify_info( int flags, char * prefix, char * result_str )
  209. {
  210. char buf[2000];
  211. int res;
  212. memset( buf, 0, sizeof( buf ) );
  213. res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
  214. TEST_ASSERT( res >= 0 );
  215. TEST_ASSERT( strcmp( buf, result_str ) == 0 );
  216. }
  217. /* END_CASE */
  218. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C */
  219. void x509_verify_restart( char *crt_file, char *ca_file,
  220. int result, int flags_result,
  221. int max_ops, int min_restart, int max_restart )
  222. {
  223. int ret, cnt_restart;
  224. mbedtls_x509_crt_restart_ctx rs_ctx;
  225. mbedtls_x509_crt crt;
  226. mbedtls_x509_crt ca;
  227. uint32_t flags = 0;
  228. /*
  229. * See comments on ecp_test_vect_restart() for op count precision.
  230. *
  231. * For reference, with mbed TLS 2.6 and default settings:
  232. * - ecdsa_verify() for P-256: ~ 6700
  233. * - ecdsa_verify() for P-384: ~ 18800
  234. * - x509_verify() for server5 -> test-ca2: ~ 18800
  235. * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
  236. */
  237. mbedtls_x509_crt_restart_init( &rs_ctx );
  238. mbedtls_x509_crt_init( &crt );
  239. mbedtls_x509_crt_init( &ca );
  240. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  241. TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
  242. mbedtls_ecp_set_max_ops( max_ops );
  243. cnt_restart = 0;
  244. do {
  245. ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
  246. &mbedtls_x509_crt_profile_default, NULL, &flags,
  247. NULL, NULL, &rs_ctx );
  248. } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
  249. TEST_ASSERT( ret == result );
  250. TEST_ASSERT( flags == (uint32_t) flags_result );
  251. TEST_ASSERT( cnt_restart >= min_restart );
  252. TEST_ASSERT( cnt_restart <= max_restart );
  253. /* Do we leak memory when aborting? */
  254. ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
  255. &mbedtls_x509_crt_profile_default, NULL, &flags,
  256. NULL, NULL, &rs_ctx );
  257. TEST_ASSERT( ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
  258. exit:
  259. mbedtls_x509_crt_restart_free( &rs_ctx );
  260. mbedtls_x509_crt_free( &crt );
  261. mbedtls_x509_crt_free( &ca );
  262. }
  263. /* END_CASE */
  264. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
  265. void x509_verify( char *crt_file, char *ca_file, char *crl_file,
  266. char *cn_name_str, int result, int flags_result,
  267. char *profile_str,
  268. char *verify_callback )
  269. {
  270. mbedtls_x509_crt crt;
  271. mbedtls_x509_crt ca;
  272. mbedtls_x509_crl crl;
  273. uint32_t flags = 0;
  274. int res;
  275. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
  276. char * cn_name = NULL;
  277. const mbedtls_x509_crt_profile *profile;
  278. mbedtls_x509_crt_init( &crt );
  279. mbedtls_x509_crt_init( &ca );
  280. mbedtls_x509_crl_init( &crl );
  281. if( strcmp( cn_name_str, "NULL" ) != 0 )
  282. cn_name = cn_name_str;
  283. if( strcmp( profile_str, "" ) == 0 )
  284. profile = &mbedtls_x509_crt_profile_default;
  285. else if( strcmp( profile_str, "next" ) == 0 )
  286. profile = &mbedtls_x509_crt_profile_next;
  287. else if( strcmp( profile_str, "suite_b" ) == 0 )
  288. profile = &mbedtls_x509_crt_profile_suiteb;
  289. else if( strcmp( profile_str, "compat" ) == 0 )
  290. profile = &compat_profile;
  291. else if( strcmp( profile_str, "all" ) == 0 )
  292. profile = &profile_all;
  293. else
  294. TEST_ASSERT( "Unknown algorithm profile" == 0 );
  295. if( strcmp( verify_callback, "NULL" ) == 0 )
  296. f_vrfy = NULL;
  297. else if( strcmp( verify_callback, "verify_none" ) == 0 )
  298. f_vrfy = verify_none;
  299. else if( strcmp( verify_callback, "verify_all" ) == 0 )
  300. f_vrfy = verify_all;
  301. else
  302. TEST_ASSERT( "No known verify callback selected" == 0 );
  303. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  304. TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
  305. TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
  306. res = mbedtls_x509_crt_verify_with_profile( &crt, &ca, &crl, profile, cn_name, &flags, f_vrfy, NULL );
  307. TEST_ASSERT( res == ( result ) );
  308. TEST_ASSERT( flags == (uint32_t)( flags_result ) );
  309. exit:
  310. mbedtls_x509_crt_free( &crt );
  311. mbedtls_x509_crt_free( &ca );
  312. mbedtls_x509_crl_free( &crl );
  313. }
  314. /* END_CASE */
  315. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  316. void x509_verify_callback( char *crt_file, char *ca_file, char *name,
  317. int exp_ret, char *exp_vrfy_out )
  318. {
  319. int ret;
  320. mbedtls_x509_crt crt;
  321. mbedtls_x509_crt ca;
  322. uint32_t flags = 0;
  323. verify_print_context vrfy_ctx;
  324. mbedtls_x509_crt_init( &crt );
  325. mbedtls_x509_crt_init( &ca );
  326. verify_print_init( &vrfy_ctx );
  327. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  328. TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
  329. if( strcmp( name, "NULL" ) == 0 )
  330. name = NULL;
  331. ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL,
  332. &compat_profile,
  333. name, &flags,
  334. verify_print, &vrfy_ctx );
  335. TEST_ASSERT( ret == exp_ret );
  336. TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
  337. exit:
  338. mbedtls_x509_crt_free( &crt );
  339. mbedtls_x509_crt_free( &ca );
  340. }
  341. /* END_CASE */
  342. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  343. void mbedtls_x509_dn_gets( char * crt_file, char * entity, char * result_str )
  344. {
  345. mbedtls_x509_crt crt;
  346. char buf[2000];
  347. int res = 0;
  348. mbedtls_x509_crt_init( &crt );
  349. memset( buf, 0, 2000 );
  350. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  351. if( strcmp( entity, "subject" ) == 0 )
  352. res = mbedtls_x509_dn_gets( buf, 2000, &crt.subject );
  353. else if( strcmp( entity, "issuer" ) == 0 )
  354. res = mbedtls_x509_dn_gets( buf, 2000, &crt.issuer );
  355. else
  356. TEST_ASSERT( "Unknown entity" == 0 );
  357. TEST_ASSERT( res != -1 );
  358. TEST_ASSERT( res != -2 );
  359. TEST_ASSERT( strcmp( buf, result_str ) == 0 );
  360. exit:
  361. mbedtls_x509_crt_free( &crt );
  362. }
  363. /* END_CASE */
  364. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  365. void mbedtls_x509_time_is_past( char * crt_file, char * entity, int result )
  366. {
  367. mbedtls_x509_crt crt;
  368. mbedtls_x509_crt_init( &crt );
  369. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  370. if( strcmp( entity, "valid_from" ) == 0 )
  371. TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_from ) == result );
  372. else if( strcmp( entity, "valid_to" ) == 0 )
  373. TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_to ) == result );
  374. else
  375. TEST_ASSERT( "Unknown entity" == 0 );
  376. exit:
  377. mbedtls_x509_crt_free( &crt );
  378. }
  379. /* END_CASE */
  380. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  381. void mbedtls_x509_time_is_future( char * crt_file, char * entity, int result )
  382. {
  383. mbedtls_x509_crt crt;
  384. mbedtls_x509_crt_init( &crt );
  385. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  386. if( strcmp( entity, "valid_from" ) == 0 )
  387. TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_from ) == result );
  388. else if( strcmp( entity, "valid_to" ) == 0 )
  389. TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_to ) == result );
  390. else
  391. TEST_ASSERT( "Unknown entity" == 0 );
  392. exit:
  393. mbedtls_x509_crt_free( &crt );
  394. }
  395. /* END_CASE */
  396. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
  397. void x509parse_crt_file( char * crt_file, int result )
  398. {
  399. mbedtls_x509_crt crt;
  400. mbedtls_x509_crt_init( &crt );
  401. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );
  402. exit:
  403. mbedtls_x509_crt_free( &crt );
  404. }
  405. /* END_CASE */
  406. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
  407. void x509parse_crt( data_t * buf, char * result_str, int result )
  408. {
  409. mbedtls_x509_crt crt;
  410. unsigned char output[2000];
  411. int res;
  412. mbedtls_x509_crt_init( &crt );
  413. memset( output, 0, 2000 );
  414. TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf->x, buf->len ) == ( result ) );
  415. if( ( result ) == 0 )
  416. {
  417. res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
  418. TEST_ASSERT( res != -1 );
  419. TEST_ASSERT( res != -2 );
  420. TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
  421. }
  422. exit:
  423. mbedtls_x509_crt_free( &crt );
  424. }
  425. /* END_CASE */
  426. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */
  427. void x509parse_crl( data_t * buf, char * result_str, int result )
  428. {
  429. mbedtls_x509_crl crl;
  430. unsigned char output[2000];
  431. int res;
  432. mbedtls_x509_crl_init( &crl );
  433. memset( output, 0, 2000 );
  434. TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf->x, buf->len ) == ( result ) );
  435. if( ( result ) == 0 )
  436. {
  437. res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
  438. TEST_ASSERT( res != -1 );
  439. TEST_ASSERT( res != -2 );
  440. TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
  441. }
  442. exit:
  443. mbedtls_x509_crl_free( &crl );
  444. }
  445. /* END_CASE */
  446. /* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
  447. void mbedtls_x509_csr_parse( data_t * csr_der, char * ref_out, int ref_ret )
  448. {
  449. mbedtls_x509_csr csr;
  450. char my_out[1000];
  451. int my_ret;
  452. mbedtls_x509_csr_init( &csr );
  453. memset( my_out, 0, sizeof( my_out ) );
  454. my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der->x, csr_der->len );
  455. TEST_ASSERT( my_ret == ref_ret );
  456. if( ref_ret == 0 )
  457. {
  458. size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
  459. TEST_ASSERT( my_out_len == strlen( ref_out ) );
  460. TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
  461. }
  462. exit:
  463. mbedtls_x509_csr_free( &csr );
  464. }
  465. /* END_CASE */
  466. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  467. void mbedtls_x509_crt_parse_path( char * crt_path, int ret, int nb_crt )
  468. {
  469. mbedtls_x509_crt chain, *cur;
  470. int i;
  471. mbedtls_x509_crt_init( &chain );
  472. TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
  473. /* Check how many certs we got */
  474. for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
  475. if( cur->raw.p != NULL )
  476. i++;
  477. TEST_ASSERT( i == nb_crt );
  478. exit:
  479. mbedtls_x509_crt_free( &chain );
  480. }
  481. /* END_CASE */
  482. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  483. void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
  484. int ret_chk, int flags_chk )
  485. {
  486. char file_buf[128];
  487. int ret;
  488. uint32_t flags;
  489. mbedtls_x509_crt trusted, chain;
  490. /*
  491. * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
  492. * with NN.crt signed by NN-1.crt
  493. */
  494. mbedtls_x509_crt_init( &trusted );
  495. mbedtls_x509_crt_init( &chain );
  496. /* Load trusted root */
  497. TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
  498. /* Load a chain with nb_int intermediates (from 01 to nb_int),
  499. * plus one "end-entity" cert (nb_int + 1) */
  500. ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
  501. nb_int + 1 );
  502. TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
  503. TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
  504. /* Try to verify that chain */
  505. ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags,
  506. NULL, NULL );
  507. TEST_ASSERT( ret == ret_chk );
  508. TEST_ASSERT( flags == (uint32_t) flags_chk );
  509. exit:
  510. mbedtls_x509_crt_free( &chain );
  511. mbedtls_x509_crt_free( &trusted );
  512. }
  513. /* END_CASE */
  514. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  515. void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
  516. int flags_result, int result,
  517. char *profile_name, int vrfy_fatal_lvls )
  518. {
  519. char* act;
  520. uint32_t flags;
  521. int res;
  522. mbedtls_x509_crt trusted, chain;
  523. const mbedtls_x509_crt_profile *profile = NULL;
  524. mbedtls_x509_crt_init( &chain );
  525. mbedtls_x509_crt_init( &trusted );
  526. while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
  527. TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
  528. TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
  529. if( strcmp( profile_name, "" ) == 0 )
  530. profile = &mbedtls_x509_crt_profile_default;
  531. else if( strcmp( profile_name, "next" ) == 0 )
  532. profile = &mbedtls_x509_crt_profile_next;
  533. else if( strcmp( profile_name, "suiteb" ) == 0 )
  534. profile = &mbedtls_x509_crt_profile_suiteb;
  535. else if( strcmp( profile_name, "rsa3072" ) == 0 )
  536. profile = &profile_rsa3072;
  537. else if( strcmp( profile_name, "sha512" ) == 0 )
  538. profile = &profile_sha512;
  539. res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile,
  540. NULL, &flags, verify_fatal, &vrfy_fatal_lvls );
  541. TEST_ASSERT( res == ( result ) );
  542. TEST_ASSERT( flags == (uint32_t)( flags_result ) );
  543. exit:
  544. mbedtls_x509_crt_free( &trusted );
  545. mbedtls_x509_crt_free( &chain );
  546. }
  547. /* END_CASE */
  548. /* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
  549. void x509_oid_desc( data_t * buf, char * ref_desc )
  550. {
  551. mbedtls_x509_buf oid;
  552. const char *desc = NULL;
  553. int ret;
  554. oid.tag = MBEDTLS_ASN1_OID;
  555. oid.p = buf->x;
  556. oid.len = buf->len;
  557. ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
  558. if( strcmp( ref_desc, "notfound" ) == 0 )
  559. {
  560. TEST_ASSERT( ret != 0 );
  561. TEST_ASSERT( desc == NULL );
  562. }
  563. else
  564. {
  565. TEST_ASSERT( ret == 0 );
  566. TEST_ASSERT( desc != NULL );
  567. TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
  568. }
  569. }
  570. /* END_CASE */
  571. /* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
  572. void x509_oid_numstr( data_t * oid_buf, char * numstr, int blen, int ret )
  573. {
  574. mbedtls_x509_buf oid;
  575. char num_buf[100];
  576. memset( num_buf, 0x2a, sizeof num_buf );
  577. oid.tag = MBEDTLS_ASN1_OID;
  578. oid.p = oid_buf->x;
  579. oid.len = oid_buf->len;
  580. TEST_ASSERT( (size_t) blen <= sizeof num_buf );
  581. TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
  582. if( ret >= 0 )
  583. {
  584. TEST_ASSERT( num_buf[ret] == 0 );
  585. TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
  586. }
  587. }
  588. /* END_CASE */
  589. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */
  590. void x509_check_key_usage( char * crt_file, int usage, int ret )
  591. {
  592. mbedtls_x509_crt crt;
  593. mbedtls_x509_crt_init( &crt );
  594. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  595. TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
  596. exit:
  597. mbedtls_x509_crt_free( &crt );
  598. }
  599. /* END_CASE */
  600. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
  601. void x509_check_extended_key_usage( char * crt_file, data_t * oid, int ret
  602. )
  603. {
  604. mbedtls_x509_crt crt;
  605. mbedtls_x509_crt_init( &crt );
  606. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  607. TEST_ASSERT( mbedtls_x509_crt_check_extended_key_usage( &crt, (const char *)oid->x, oid->len ) == ret );
  608. exit:
  609. mbedtls_x509_crt_free( &crt );
  610. }
  611. /* END_CASE */
  612. /* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
  613. void x509_get_time( int tag, char * time_str, int ret, int year, int mon,
  614. int day, int hour, int min, int sec )
  615. {
  616. mbedtls_x509_time time;
  617. unsigned char buf[21];
  618. unsigned char* start = buf;
  619. unsigned char* end = buf;
  620. memset( &time, 0x00, sizeof( time ) );
  621. *end = (unsigned char)tag; end++;
  622. *end = strlen( time_str );
  623. TEST_ASSERT( *end < 20 );
  624. end++;
  625. memcpy( end, time_str, (size_t)*(end - 1) );
  626. end += *(end - 1);
  627. TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
  628. if( ret == 0 )
  629. {
  630. TEST_ASSERT( year == time.year );
  631. TEST_ASSERT( mon == time.mon );
  632. TEST_ASSERT( day == time.day );
  633. TEST_ASSERT( hour == time.hour );
  634. TEST_ASSERT( min == time.min );
  635. TEST_ASSERT( sec == time.sec );
  636. }
  637. }
  638. /* END_CASE */
  639. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  640. void x509_parse_rsassa_pss_params( data_t * params, int params_tag,
  641. int ref_msg_md, int ref_mgf_md,
  642. int ref_salt_len, int ref_ret )
  643. {
  644. int my_ret;
  645. mbedtls_x509_buf buf;
  646. mbedtls_md_type_t my_msg_md, my_mgf_md;
  647. int my_salt_len;
  648. buf.p = params->x;
  649. buf.len = params->len;
  650. buf.tag = params_tag;
  651. my_ret = mbedtls_x509_get_rsassa_pss_params( &buf, &my_msg_md, &my_mgf_md,
  652. &my_salt_len );
  653. TEST_ASSERT( my_ret == ref_ret );
  654. if( ref_ret == 0 )
  655. {
  656. TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
  657. TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
  658. TEST_ASSERT( my_salt_len == ref_salt_len );
  659. }
  660. exit:
  661. ;;
  662. }
  663. /* END_CASE */
  664. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
  665. void x509_selftest( )
  666. {
  667. TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
  668. }
  669. /* END_CASE */