test_suite_x509parse.function 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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 "mbedtls/error.h"
  11. #include "string.h"
  12. #if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
  13. #error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
  14. than the current threshold 19. To test larger values, please \
  15. adapt the script tests/data_files/dir-max/long.sh."
  16. #endif
  17. /* Test-only profile allowing all digests, PK algorithms, and curves. */
  18. const mbedtls_x509_crt_profile profile_all =
  19. {
  20. 0xFFFFFFFF, /* Any MD */
  21. 0xFFFFFFFF, /* Any PK alg */
  22. 0xFFFFFFFF, /* Any curve */
  23. 1024,
  24. };
  25. /* Profile for backward compatibility. Allows SHA-1, unlike the default
  26. profile. */
  27. const mbedtls_x509_crt_profile compat_profile =
  28. {
  29. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
  30. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
  31. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
  32. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
  33. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
  34. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
  35. 0xFFFFFFF, /* Any PK alg */
  36. 0xFFFFFFF, /* Any curve */
  37. 1024,
  38. };
  39. const mbedtls_x509_crt_profile profile_rsa3072 =
  40. {
  41. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
  42. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
  43. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
  44. MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA ),
  45. 0,
  46. 3072,
  47. };
  48. const mbedtls_x509_crt_profile profile_sha512 =
  49. {
  50. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
  51. 0xFFFFFFF, /* Any PK alg */
  52. 0xFFFFFFF, /* Any curve */
  53. 1024,
  54. };
  55. int verify_none( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
  56. {
  57. ((void) data);
  58. ((void) crt);
  59. ((void) certificate_depth);
  60. *flags |= MBEDTLS_X509_BADCERT_OTHER;
  61. return 0;
  62. }
  63. int verify_all( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
  64. {
  65. ((void) data);
  66. ((void) crt);
  67. ((void) certificate_depth);
  68. *flags = 0;
  69. return 0;
  70. }
  71. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  72. int ca_callback_fail( void *data, mbedtls_x509_crt const *child, mbedtls_x509_crt **candidates )
  73. {
  74. ((void) data);
  75. ((void) child);
  76. ((void) candidates);
  77. return -1;
  78. }
  79. int ca_callback( void *data, mbedtls_x509_crt const *child,
  80. mbedtls_x509_crt **candidates )
  81. {
  82. int ret = 0;
  83. mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
  84. mbedtls_x509_crt *first;
  85. /* This is a test-only implementation of the CA callback
  86. * which always returns the entire list of trusted certificates.
  87. * Production implementations managing a large number of CAs
  88. * should use an efficient presentation and lookup for the
  89. * set of trusted certificates (such as a hashtable) and only
  90. * return those trusted certificates which satisfy basic
  91. * parental checks, such as the matching of child `Issuer`
  92. * and parent `Subject` field. */
  93. ((void) child);
  94. first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
  95. if( first == NULL )
  96. {
  97. ret = -1;
  98. goto exit;
  99. }
  100. mbedtls_x509_crt_init( first );
  101. if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
  102. {
  103. ret = -1;
  104. goto exit;
  105. }
  106. while( ca->next != NULL )
  107. {
  108. ca = ca->next;
  109. if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
  110. {
  111. ret = -1;
  112. goto exit;
  113. }
  114. }
  115. exit:
  116. if( ret != 0 )
  117. {
  118. mbedtls_x509_crt_free( first );
  119. mbedtls_free( first );
  120. first = NULL;
  121. }
  122. *candidates = first;
  123. return( ret );
  124. }
  125. #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
  126. int verify_fatal( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
  127. {
  128. int *levels = (int *) data;
  129. ((void) crt);
  130. ((void) certificate_depth);
  131. /* Simulate a fatal error in the callback */
  132. if( *levels & ( 1 << certificate_depth ) )
  133. {
  134. *flags |= ( 1 << certificate_depth );
  135. return( -1 - certificate_depth );
  136. }
  137. return( 0 );
  138. }
  139. /* strsep() not available on Windows */
  140. char *mystrsep(char **stringp, const char *delim)
  141. {
  142. const char *p;
  143. char *ret = *stringp;
  144. if( *stringp == NULL )
  145. return( NULL );
  146. for( ; ; (*stringp)++ )
  147. {
  148. if( **stringp == '\0' )
  149. {
  150. *stringp = NULL;
  151. goto done;
  152. }
  153. for( p = delim; *p != '\0'; p++ )
  154. if( **stringp == *p )
  155. {
  156. **stringp = '\0';
  157. (*stringp)++;
  158. goto done;
  159. }
  160. }
  161. done:
  162. return( ret );
  163. }
  164. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  165. typedef struct {
  166. char buf[512];
  167. char *p;
  168. } verify_print_context;
  169. void verify_print_init( verify_print_context *ctx )
  170. {
  171. memset( ctx, 0, sizeof( verify_print_context ) );
  172. ctx->p = ctx->buf;
  173. }
  174. int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
  175. {
  176. int ret;
  177. verify_print_context *ctx = (verify_print_context *) data;
  178. char *p = ctx->p;
  179. size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
  180. ((void) flags);
  181. ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth );
  182. MBEDTLS_X509_SAFE_SNPRINTF;
  183. ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
  184. MBEDTLS_X509_SAFE_SNPRINTF;
  185. ret = mbedtls_snprintf( p, n, " - subject " );
  186. MBEDTLS_X509_SAFE_SNPRINTF;
  187. ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
  188. MBEDTLS_X509_SAFE_SNPRINTF;
  189. ret = mbedtls_snprintf( p, n, " - flags 0x%08x\n", *flags );
  190. MBEDTLS_X509_SAFE_SNPRINTF;
  191. ctx->p = p;
  192. return( 0 );
  193. }
  194. int verify_parse_san( mbedtls_x509_subject_alternative_name *san,
  195. char **buf, size_t *size )
  196. {
  197. int ret;
  198. size_t i;
  199. char *p = *buf;
  200. size_t n = *size;
  201. ret = mbedtls_snprintf( p, n, "type : %d", san->type );
  202. MBEDTLS_X509_SAFE_SNPRINTF;
  203. switch( san->type )
  204. {
  205. case( MBEDTLS_X509_SAN_OTHER_NAME ):
  206. ret = mbedtls_snprintf( p, n, "\notherName :");
  207. MBEDTLS_X509_SAFE_SNPRINTF;
  208. if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
  209. &san->san.other_name.value.hardware_module_name.oid ) != 0 )
  210. {
  211. ret = mbedtls_snprintf( p, n, " hardware module name :" );
  212. MBEDTLS_X509_SAFE_SNPRINTF;
  213. ret = mbedtls_snprintf( p, n, " hardware type : " );
  214. MBEDTLS_X509_SAFE_SNPRINTF;
  215. ret = mbedtls_oid_get_numeric_string( p, n,
  216. &san->san.other_name.value.hardware_module_name.oid );
  217. MBEDTLS_X509_SAFE_SNPRINTF;
  218. ret = mbedtls_snprintf( p, n, ", hardware serial number : " );
  219. MBEDTLS_X509_SAFE_SNPRINTF;
  220. if( san->san.other_name.value.hardware_module_name.val.len >= n )
  221. {
  222. *p = '\0';
  223. return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
  224. }
  225. for( i=0; i < san->san.other_name.value.hardware_module_name.val.len; i++ )
  226. {
  227. *p++ = san->san.other_name.value.hardware_module_name.val.p[i];
  228. }
  229. n -= san->san.other_name.value.hardware_module_name.val.len;
  230. }
  231. break;/* MBEDTLS_OID_ON_HW_MODULE_NAME */
  232. case( MBEDTLS_X509_SAN_DNS_NAME ):
  233. ret = mbedtls_snprintf( p, n, "\ndNSName : " );
  234. MBEDTLS_X509_SAFE_SNPRINTF;
  235. if( san->san.unstructured_name.len >= n )
  236. {
  237. *p = '\0';
  238. return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
  239. }
  240. n -= san->san.unstructured_name.len;
  241. for( i = 0; i < san->san.unstructured_name.len; i++ )
  242. *p++ = san->san.unstructured_name.p[i];
  243. break;/* MBEDTLS_X509_SAN_DNS_NAME */
  244. default:
  245. /*
  246. * Should not happen.
  247. */
  248. return( -1 );
  249. }
  250. ret = mbedtls_snprintf( p, n, "\n" );
  251. MBEDTLS_X509_SAFE_SNPRINTF;
  252. *size = n;
  253. *buf = p;
  254. return( 0 );
  255. }
  256. int parse_crt_ext_cb( void *p_ctx, mbedtls_x509_crt const *crt, mbedtls_x509_buf const *oid,
  257. int critical, const unsigned char *cp, const unsigned char *end )
  258. {
  259. ( void ) crt;
  260. ( void ) critical;
  261. mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *)p_ctx;
  262. if( oid->tag == MBEDTLS_ASN1_OID &&
  263. MBEDTLS_OID_CMP( MBEDTLS_OID_CERTIFICATE_POLICIES, oid ) == 0 )
  264. {
  265. /* Handle unknown certificate policy */
  266. int ret, parse_ret = 0;
  267. size_t len;
  268. unsigned char **p = (unsigned char **)&cp;
  269. /* Get main sequence tag */
  270. ret = mbedtls_asn1_get_tag( p, end, &len,
  271. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
  272. if( ret != 0 )
  273. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  274. if( *p + len != end )
  275. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  276. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  277. /*
  278. * Cannot be an empty sequence.
  279. */
  280. if( len == 0 )
  281. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  282. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  283. while( *p < end )
  284. {
  285. const unsigned char *policy_end;
  286. /*
  287. * Get the policy sequence
  288. */
  289. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  290. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  291. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  292. policy_end = *p + len;
  293. if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
  294. MBEDTLS_ASN1_OID ) ) != 0 )
  295. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  296. /*
  297. * Recognize exclusively the policy with OID 1
  298. */
  299. if( len != 1 || *p[0] != 1 )
  300. parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
  301. *p += len;
  302. /*
  303. * If there is an optional qualifier, then *p < policy_end
  304. * Check the Qualifier len to verify it doesn't exceed policy_end.
  305. */
  306. if( *p < policy_end )
  307. {
  308. if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
  309. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  310. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  311. /*
  312. * Skip the optional policy qualifiers.
  313. */
  314. *p += len;
  315. }
  316. if( *p != policy_end )
  317. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  318. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  319. }
  320. if( *p != end )
  321. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  322. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  323. return( parse_ret );
  324. }
  325. else if( new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len &&
  326. memcmp( new_oid->p, oid->p, oid->len ) == 0 )
  327. return( 0 );
  328. else
  329. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  330. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
  331. }
  332. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  333. /* END_HEADER */
  334. /* BEGIN_DEPENDENCIES
  335. * depends_on:MBEDTLS_BIGNUM_C
  336. * END_DEPENDENCIES
  337. */
  338. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  339. void x509_parse_san( char * crt_file, char * result_str )
  340. {
  341. int ret;
  342. mbedtls_x509_crt crt;
  343. mbedtls_x509_subject_alternative_name san;
  344. mbedtls_x509_sequence *cur = NULL;
  345. char buf[2000];
  346. char *p = buf;
  347. size_t n = sizeof( buf );
  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( crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
  352. {
  353. cur = &crt.subject_alt_names;
  354. while( cur != NULL )
  355. {
  356. ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
  357. TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
  358. /*
  359. * If san type not supported, ignore.
  360. */
  361. if( ret == 0)
  362. TEST_ASSERT( verify_parse_san( &san, &p, &n ) == 0 );
  363. cur = cur->next;
  364. }
  365. }
  366. TEST_ASSERT( strcmp( buf, result_str ) == 0 );
  367. exit:
  368. mbedtls_x509_crt_free( &crt );
  369. }
  370. /* END_CASE */
  371. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  372. void x509_cert_info( char * crt_file, char * result_str )
  373. {
  374. mbedtls_x509_crt crt;
  375. char buf[2000];
  376. int res;
  377. mbedtls_x509_crt_init( &crt );
  378. memset( buf, 0, 2000 );
  379. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  380. res = mbedtls_x509_crt_info( buf, 2000, "", &crt );
  381. TEST_ASSERT( res != -1 );
  382. TEST_ASSERT( res != -2 );
  383. TEST_ASSERT( strcmp( buf, result_str ) == 0 );
  384. exit:
  385. mbedtls_x509_crt_free( &crt );
  386. }
  387. /* END_CASE */
  388. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
  389. void mbedtls_x509_crl_info( char * crl_file, char * result_str )
  390. {
  391. mbedtls_x509_crl crl;
  392. char buf[2000];
  393. int res;
  394. mbedtls_x509_crl_init( &crl );
  395. memset( buf, 0, 2000 );
  396. TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
  397. res = mbedtls_x509_crl_info( buf, 2000, "", &crl );
  398. TEST_ASSERT( res != -1 );
  399. TEST_ASSERT( res != -2 );
  400. TEST_ASSERT( strcmp( buf, result_str ) == 0 );
  401. exit:
  402. mbedtls_x509_crl_free( &crl );
  403. }
  404. /* END_CASE */
  405. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
  406. void mbedtls_x509_crl_parse( char * crl_file, int result )
  407. {
  408. mbedtls_x509_crl crl;
  409. char buf[2000];
  410. mbedtls_x509_crl_init( &crl );
  411. memset( buf, 0, 2000 );
  412. TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == result );
  413. exit:
  414. mbedtls_x509_crl_free( &crl );
  415. }
  416. /* END_CASE */
  417. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C */
  418. void mbedtls_x509_csr_info( char * csr_file, char * result_str )
  419. {
  420. mbedtls_x509_csr csr;
  421. char buf[2000];
  422. int res;
  423. mbedtls_x509_csr_init( &csr );
  424. memset( buf, 0, 2000 );
  425. TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 );
  426. res = mbedtls_x509_csr_info( buf, 2000, "", &csr );
  427. TEST_ASSERT( res != -1 );
  428. TEST_ASSERT( res != -2 );
  429. TEST_ASSERT( strcmp( buf, result_str ) == 0 );
  430. exit:
  431. mbedtls_x509_csr_free( &csr );
  432. }
  433. /* END_CASE */
  434. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
  435. void x509_verify_info( int flags, char * prefix, char * result_str )
  436. {
  437. char buf[2000];
  438. int res;
  439. memset( buf, 0, sizeof( buf ) );
  440. res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
  441. TEST_ASSERT( res >= 0 );
  442. TEST_ASSERT( strcmp( buf, result_str ) == 0 );
  443. }
  444. /* END_CASE */
  445. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C */
  446. void x509_verify_restart( char *crt_file, char *ca_file,
  447. int result, int flags_result,
  448. int max_ops, int min_restart, int max_restart )
  449. {
  450. int ret, cnt_restart;
  451. mbedtls_x509_crt_restart_ctx rs_ctx;
  452. mbedtls_x509_crt crt;
  453. mbedtls_x509_crt ca;
  454. uint32_t flags = 0;
  455. /*
  456. * See comments on ecp_test_vect_restart() for op count precision.
  457. *
  458. * For reference, with mbed TLS 2.6 and default settings:
  459. * - ecdsa_verify() for P-256: ~ 6700
  460. * - ecdsa_verify() for P-384: ~ 18800
  461. * - x509_verify() for server5 -> test-ca2: ~ 18800
  462. * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
  463. */
  464. mbedtls_x509_crt_restart_init( &rs_ctx );
  465. mbedtls_x509_crt_init( &crt );
  466. mbedtls_x509_crt_init( &ca );
  467. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  468. TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
  469. mbedtls_ecp_set_max_ops( max_ops );
  470. cnt_restart = 0;
  471. do {
  472. ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
  473. &mbedtls_x509_crt_profile_default, NULL, &flags,
  474. NULL, NULL, &rs_ctx );
  475. } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
  476. TEST_ASSERT( ret == result );
  477. TEST_ASSERT( flags == (uint32_t) flags_result );
  478. TEST_ASSERT( cnt_restart >= min_restart );
  479. TEST_ASSERT( cnt_restart <= max_restart );
  480. /* Do we leak memory when aborting? */
  481. ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
  482. &mbedtls_x509_crt_profile_default, NULL, &flags,
  483. NULL, NULL, &rs_ctx );
  484. TEST_ASSERT( ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
  485. exit:
  486. mbedtls_x509_crt_restart_free( &rs_ctx );
  487. mbedtls_x509_crt_free( &crt );
  488. mbedtls_x509_crt_free( &ca );
  489. }
  490. /* END_CASE */
  491. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
  492. void x509_verify( char *crt_file, char *ca_file, char *crl_file,
  493. char *cn_name_str, int result, int flags_result,
  494. char *profile_str,
  495. char *verify_callback )
  496. {
  497. mbedtls_x509_crt crt;
  498. mbedtls_x509_crt ca;
  499. mbedtls_x509_crl crl;
  500. uint32_t flags = 0;
  501. int res;
  502. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
  503. char * cn_name = NULL;
  504. const mbedtls_x509_crt_profile *profile;
  505. mbedtls_x509_crt_init( &crt );
  506. mbedtls_x509_crt_init( &ca );
  507. mbedtls_x509_crl_init( &crl );
  508. USE_PSA_INIT( );
  509. if( strcmp( cn_name_str, "NULL" ) != 0 )
  510. cn_name = cn_name_str;
  511. if( strcmp( profile_str, "" ) == 0 )
  512. profile = &mbedtls_x509_crt_profile_default;
  513. else if( strcmp( profile_str, "next" ) == 0 )
  514. profile = &mbedtls_x509_crt_profile_next;
  515. else if( strcmp( profile_str, "suite_b" ) == 0 )
  516. profile = &mbedtls_x509_crt_profile_suiteb;
  517. else if( strcmp( profile_str, "compat" ) == 0 )
  518. profile = &compat_profile;
  519. else if( strcmp( profile_str, "all" ) == 0 )
  520. profile = &profile_all;
  521. else
  522. TEST_ASSERT( "Unknown algorithm profile" == 0 );
  523. if( strcmp( verify_callback, "NULL" ) == 0 )
  524. f_vrfy = NULL;
  525. else if( strcmp( verify_callback, "verify_none" ) == 0 )
  526. f_vrfy = verify_none;
  527. else if( strcmp( verify_callback, "verify_all" ) == 0 )
  528. f_vrfy = verify_all;
  529. else
  530. TEST_ASSERT( "No known verify callback selected" == 0 );
  531. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  532. TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
  533. TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
  534. res = mbedtls_x509_crt_verify_with_profile( &crt, &ca, &crl, profile, cn_name, &flags, f_vrfy, NULL );
  535. TEST_ASSERT( res == ( result ) );
  536. TEST_ASSERT( flags == (uint32_t)( flags_result ) );
  537. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  538. /* CRLs aren't supported with CA callbacks, so skip the CA callback
  539. * version of the test if CRLs are in use. */
  540. if( crl_file == NULL || strcmp( crl_file, "" ) == 0 )
  541. {
  542. flags = 0;
  543. res = mbedtls_x509_crt_verify_with_ca_cb( &crt, ca_callback, &ca, profile, cn_name, &flags, f_vrfy, NULL );
  544. TEST_ASSERT( res == ( result ) );
  545. TEST_ASSERT( flags == (uint32_t)( flags_result ) );
  546. }
  547. #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
  548. exit:
  549. mbedtls_x509_crt_free( &crt );
  550. mbedtls_x509_crt_free( &ca );
  551. mbedtls_x509_crl_free( &crl );
  552. USE_PSA_DONE( );
  553. }
  554. /* END_CASE */
  555. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
  556. void x509_verify_ca_cb_failure( char *crt_file, char *ca_file, char *name,
  557. int exp_ret )
  558. {
  559. int ret;
  560. mbedtls_x509_crt crt;
  561. mbedtls_x509_crt ca;
  562. uint32_t flags = 0;
  563. mbedtls_x509_crt_init( &crt );
  564. mbedtls_x509_crt_init( &ca );
  565. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  566. TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
  567. if( strcmp( name, "NULL" ) == 0 )
  568. name = NULL;
  569. ret = mbedtls_x509_crt_verify_with_ca_cb( &crt, ca_callback_fail, &ca,
  570. &compat_profile, name, &flags,
  571. NULL, NULL );
  572. TEST_ASSERT( ret == exp_ret );
  573. TEST_ASSERT( flags == (uint32_t)( -1 ) );
  574. exit:
  575. mbedtls_x509_crt_free( &crt );
  576. mbedtls_x509_crt_free( &ca );
  577. }
  578. /* END_CASE */
  579. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  580. void x509_verify_callback( char *crt_file, char *ca_file, char *name,
  581. int exp_ret, char *exp_vrfy_out )
  582. {
  583. int ret;
  584. mbedtls_x509_crt crt;
  585. mbedtls_x509_crt ca;
  586. uint32_t flags = 0;
  587. verify_print_context vrfy_ctx;
  588. mbedtls_x509_crt_init( &crt );
  589. mbedtls_x509_crt_init( &ca );
  590. verify_print_init( &vrfy_ctx );
  591. USE_PSA_INIT( );
  592. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  593. TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
  594. if( strcmp( name, "NULL" ) == 0 )
  595. name = NULL;
  596. ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL,
  597. &compat_profile,
  598. name, &flags,
  599. verify_print, &vrfy_ctx );
  600. TEST_ASSERT( ret == exp_ret );
  601. TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
  602. exit:
  603. mbedtls_x509_crt_free( &crt );
  604. mbedtls_x509_crt_free( &ca );
  605. USE_PSA_DONE( );
  606. }
  607. /* END_CASE */
  608. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  609. void mbedtls_x509_dn_gets( char * crt_file, char * entity, char * result_str )
  610. {
  611. mbedtls_x509_crt crt;
  612. char buf[2000];
  613. int res = 0;
  614. mbedtls_x509_crt_init( &crt );
  615. memset( buf, 0, 2000 );
  616. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  617. if( strcmp( entity, "subject" ) == 0 )
  618. res = mbedtls_x509_dn_gets( buf, 2000, &crt.subject );
  619. else if( strcmp( entity, "issuer" ) == 0 )
  620. res = mbedtls_x509_dn_gets( buf, 2000, &crt.issuer );
  621. else
  622. TEST_ASSERT( "Unknown entity" == 0 );
  623. TEST_ASSERT( res != -1 );
  624. TEST_ASSERT( res != -2 );
  625. TEST_ASSERT( strcmp( buf, result_str ) == 0 );
  626. exit:
  627. mbedtls_x509_crt_free( &crt );
  628. }
  629. /* END_CASE */
  630. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  631. void mbedtls_x509_time_is_past( char * crt_file, char * entity, int result )
  632. {
  633. mbedtls_x509_crt crt;
  634. mbedtls_x509_crt_init( &crt );
  635. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  636. if( strcmp( entity, "valid_from" ) == 0 )
  637. TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_from ) == result );
  638. else if( strcmp( entity, "valid_to" ) == 0 )
  639. TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_to ) == result );
  640. else
  641. TEST_ASSERT( "Unknown entity" == 0 );
  642. exit:
  643. mbedtls_x509_crt_free( &crt );
  644. }
  645. /* END_CASE */
  646. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  647. void mbedtls_x509_time_is_future( char * crt_file, char * entity, int result )
  648. {
  649. mbedtls_x509_crt crt;
  650. mbedtls_x509_crt_init( &crt );
  651. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  652. if( strcmp( entity, "valid_from" ) == 0 )
  653. TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_from ) == result );
  654. else if( strcmp( entity, "valid_to" ) == 0 )
  655. TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_to ) == result );
  656. else
  657. TEST_ASSERT( "Unknown entity" == 0 );
  658. exit:
  659. mbedtls_x509_crt_free( &crt );
  660. }
  661. /* END_CASE */
  662. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
  663. void x509parse_crt_file( char * crt_file, int result )
  664. {
  665. mbedtls_x509_crt crt;
  666. mbedtls_x509_crt_init( &crt );
  667. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );
  668. exit:
  669. mbedtls_x509_crt_free( &crt );
  670. }
  671. /* END_CASE */
  672. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
  673. void x509parse_crt( data_t * buf, char * result_str, int result )
  674. {
  675. mbedtls_x509_crt crt;
  676. unsigned char output[2000];
  677. int res;
  678. mbedtls_x509_crt_init( &crt );
  679. memset( output, 0, 2000 );
  680. TEST_ASSERT( mbedtls_x509_crt_parse_der( &crt, buf->x, buf->len ) == ( result ) );
  681. if( ( result ) == 0 )
  682. {
  683. res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
  684. TEST_ASSERT( res != -1 );
  685. TEST_ASSERT( res != -2 );
  686. TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
  687. }
  688. mbedtls_x509_crt_free( &crt );
  689. mbedtls_x509_crt_init( &crt );
  690. memset( output, 0, 2000 );
  691. TEST_ASSERT( mbedtls_x509_crt_parse_der_nocopy( &crt, buf->x, buf->len ) == ( result ) );
  692. if( ( result ) == 0 )
  693. {
  694. res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
  695. TEST_ASSERT( res != -1 );
  696. TEST_ASSERT( res != -2 );
  697. TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
  698. }
  699. mbedtls_x509_crt_free( &crt );
  700. mbedtls_x509_crt_init( &crt );
  701. memset( output, 0, 2000 );
  702. TEST_ASSERT( mbedtls_x509_crt_parse_der_with_ext_cb( &crt, buf->x, buf->len, 0, NULL, NULL ) == ( result ) );
  703. if( ( result ) == 0 )
  704. {
  705. res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
  706. TEST_ASSERT( res != -1 );
  707. TEST_ASSERT( res != -2 );
  708. TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
  709. }
  710. mbedtls_x509_crt_free( &crt );
  711. mbedtls_x509_crt_init( &crt );
  712. memset( output, 0, 2000 );
  713. TEST_ASSERT( mbedtls_x509_crt_parse_der_with_ext_cb( &crt, buf->x, buf->len, 1, NULL, NULL ) == ( result ) );
  714. if( ( result ) == 0 )
  715. {
  716. res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
  717. TEST_ASSERT( res != -1 );
  718. TEST_ASSERT( res != -2 );
  719. TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
  720. }
  721. exit:
  722. mbedtls_x509_crt_free( &crt );
  723. }
  724. /* END_CASE */
  725. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
  726. void x509parse_crt_cb( data_t * buf, char * result_str, int result )
  727. {
  728. mbedtls_x509_crt crt;
  729. mbedtls_x509_buf oid;
  730. unsigned char output[2000];
  731. int res;
  732. oid.tag = MBEDTLS_ASN1_OID;
  733. oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
  734. oid.p = (unsigned char *)MBEDTLS_OID_PKIX "\x01\x1F";
  735. mbedtls_x509_crt_init( &crt );
  736. memset( output, 0, 2000 );
  737. TEST_ASSERT( mbedtls_x509_crt_parse_der_with_ext_cb( &crt, buf->x, buf->len, 0, parse_crt_ext_cb, &oid ) == ( result ) );
  738. if( ( result ) == 0 )
  739. {
  740. res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
  741. TEST_ASSERT( res != -1 );
  742. TEST_ASSERT( res != -2 );
  743. TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
  744. }
  745. mbedtls_x509_crt_free( &crt );
  746. mbedtls_x509_crt_init( &crt );
  747. memset( output, 0, 2000 );
  748. TEST_ASSERT( mbedtls_x509_crt_parse_der_with_ext_cb( &crt, buf->x, buf->len, 1, parse_crt_ext_cb, &oid ) == ( result ) );
  749. if( ( result ) == 0 )
  750. {
  751. res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
  752. TEST_ASSERT( res != -1 );
  753. TEST_ASSERT( res != -2 );
  754. TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
  755. }
  756. exit:
  757. mbedtls_x509_crt_free( &crt );
  758. }
  759. /* END_CASE */
  760. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */
  761. void x509parse_crl( data_t * buf, char * result_str, int result )
  762. {
  763. mbedtls_x509_crl crl;
  764. unsigned char output[2000];
  765. int res;
  766. mbedtls_x509_crl_init( &crl );
  767. memset( output, 0, 2000 );
  768. TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf->x, buf->len ) == ( result ) );
  769. if( ( result ) == 0 )
  770. {
  771. res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
  772. TEST_ASSERT( res != -1 );
  773. TEST_ASSERT( res != -2 );
  774. TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
  775. }
  776. exit:
  777. mbedtls_x509_crl_free( &crl );
  778. }
  779. /* END_CASE */
  780. /* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
  781. void mbedtls_x509_csr_parse( data_t * csr_der, char * ref_out, int ref_ret )
  782. {
  783. mbedtls_x509_csr csr;
  784. char my_out[1000];
  785. int my_ret;
  786. mbedtls_x509_csr_init( &csr );
  787. memset( my_out, 0, sizeof( my_out ) );
  788. my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der->x, csr_der->len );
  789. TEST_ASSERT( my_ret == ref_ret );
  790. if( ref_ret == 0 )
  791. {
  792. size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
  793. TEST_ASSERT( my_out_len == strlen( ref_out ) );
  794. TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
  795. }
  796. exit:
  797. mbedtls_x509_csr_free( &csr );
  798. }
  799. /* END_CASE */
  800. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  801. void mbedtls_x509_crt_parse_path( char * crt_path, int ret, int nb_crt )
  802. {
  803. mbedtls_x509_crt chain, *cur;
  804. int i;
  805. mbedtls_x509_crt_init( &chain );
  806. TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
  807. /* Check how many certs we got */
  808. for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
  809. if( cur->raw.p != NULL )
  810. i++;
  811. TEST_ASSERT( i == nb_crt );
  812. exit:
  813. mbedtls_x509_crt_free( &chain );
  814. }
  815. /* END_CASE */
  816. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  817. void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
  818. int ret_chk, int flags_chk )
  819. {
  820. char file_buf[128];
  821. int ret;
  822. uint32_t flags;
  823. mbedtls_x509_crt trusted, chain;
  824. /*
  825. * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
  826. * with NN.crt signed by NN-1.crt
  827. */
  828. mbedtls_x509_crt_init( &trusted );
  829. mbedtls_x509_crt_init( &chain );
  830. USE_PSA_INIT( );
  831. /* Load trusted root */
  832. TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
  833. /* Load a chain with nb_int intermediates (from 01 to nb_int),
  834. * plus one "end-entity" cert (nb_int + 1) */
  835. ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
  836. nb_int + 1 );
  837. TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
  838. TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
  839. /* Try to verify that chain */
  840. ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags,
  841. NULL, NULL );
  842. TEST_ASSERT( ret == ret_chk );
  843. TEST_ASSERT( flags == (uint32_t) flags_chk );
  844. exit:
  845. mbedtls_x509_crt_free( &chain );
  846. mbedtls_x509_crt_free( &trusted );
  847. USE_PSA_DONE( );
  848. }
  849. /* END_CASE */
  850. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
  851. void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
  852. int flags_result, int result,
  853. char *profile_name, int vrfy_fatal_lvls )
  854. {
  855. char* act;
  856. uint32_t flags;
  857. int res;
  858. mbedtls_x509_crt trusted, chain;
  859. const mbedtls_x509_crt_profile *profile = NULL;
  860. mbedtls_x509_crt_init( &chain );
  861. mbedtls_x509_crt_init( &trusted );
  862. USE_PSA_INIT( );
  863. while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
  864. TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
  865. TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
  866. if( strcmp( profile_name, "" ) == 0 )
  867. profile = &mbedtls_x509_crt_profile_default;
  868. else if( strcmp( profile_name, "next" ) == 0 )
  869. profile = &mbedtls_x509_crt_profile_next;
  870. else if( strcmp( profile_name, "suiteb" ) == 0 )
  871. profile = &mbedtls_x509_crt_profile_suiteb;
  872. else if( strcmp( profile_name, "rsa3072" ) == 0 )
  873. profile = &profile_rsa3072;
  874. else if( strcmp( profile_name, "sha512" ) == 0 )
  875. profile = &profile_sha512;
  876. res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile,
  877. NULL, &flags, verify_fatal, &vrfy_fatal_lvls );
  878. TEST_ASSERT( res == ( result ) );
  879. TEST_ASSERT( flags == (uint32_t)( flags_result ) );
  880. exit:
  881. mbedtls_x509_crt_free( &trusted );
  882. mbedtls_x509_crt_free( &chain );
  883. USE_PSA_DONE( );
  884. }
  885. /* END_CASE */
  886. /* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
  887. void x509_oid_desc( data_t * buf, char * ref_desc )
  888. {
  889. mbedtls_x509_buf oid;
  890. const char *desc = NULL;
  891. int ret;
  892. oid.tag = MBEDTLS_ASN1_OID;
  893. oid.p = buf->x;
  894. oid.len = buf->len;
  895. ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
  896. if( strcmp( ref_desc, "notfound" ) == 0 )
  897. {
  898. TEST_ASSERT( ret != 0 );
  899. TEST_ASSERT( desc == NULL );
  900. }
  901. else
  902. {
  903. TEST_ASSERT( ret == 0 );
  904. TEST_ASSERT( desc != NULL );
  905. TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
  906. }
  907. }
  908. /* END_CASE */
  909. /* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
  910. void x509_oid_numstr( data_t * oid_buf, char * numstr, int blen, int ret )
  911. {
  912. mbedtls_x509_buf oid;
  913. char num_buf[100];
  914. memset( num_buf, 0x2a, sizeof num_buf );
  915. oid.tag = MBEDTLS_ASN1_OID;
  916. oid.p = oid_buf->x;
  917. oid.len = oid_buf->len;
  918. TEST_ASSERT( (size_t) blen <= sizeof num_buf );
  919. TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
  920. if( ret >= 0 )
  921. {
  922. TEST_ASSERT( num_buf[ret] == 0 );
  923. TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
  924. }
  925. }
  926. /* END_CASE */
  927. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */
  928. void x509_check_key_usage( char * crt_file, int usage, int ret )
  929. {
  930. mbedtls_x509_crt crt;
  931. mbedtls_x509_crt_init( &crt );
  932. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  933. TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
  934. exit:
  935. mbedtls_x509_crt_free( &crt );
  936. }
  937. /* END_CASE */
  938. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
  939. void x509_check_extended_key_usage( char * crt_file, data_t * oid, int ret
  940. )
  941. {
  942. mbedtls_x509_crt crt;
  943. mbedtls_x509_crt_init( &crt );
  944. TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
  945. TEST_ASSERT( mbedtls_x509_crt_check_extended_key_usage( &crt, (const char *)oid->x, oid->len ) == ret );
  946. exit:
  947. mbedtls_x509_crt_free( &crt );
  948. }
  949. /* END_CASE */
  950. /* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
  951. void x509_get_time( int tag, char * time_str, int ret, int year, int mon,
  952. int day, int hour, int min, int sec )
  953. {
  954. mbedtls_x509_time time;
  955. unsigned char buf[21];
  956. unsigned char* start = buf;
  957. unsigned char* end = buf;
  958. memset( &time, 0x00, sizeof( time ) );
  959. *end = (unsigned char)tag; end++;
  960. *end = strlen( time_str );
  961. TEST_ASSERT( *end < 20 );
  962. end++;
  963. memcpy( end, time_str, (size_t)*(end - 1) );
  964. end += *(end - 1);
  965. TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
  966. if( ret == 0 )
  967. {
  968. TEST_ASSERT( year == time.year );
  969. TEST_ASSERT( mon == time.mon );
  970. TEST_ASSERT( day == time.day );
  971. TEST_ASSERT( hour == time.hour );
  972. TEST_ASSERT( min == time.min );
  973. TEST_ASSERT( sec == time.sec );
  974. }
  975. }
  976. /* END_CASE */
  977. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  978. void x509_parse_rsassa_pss_params( data_t * params, int params_tag,
  979. int ref_msg_md, int ref_mgf_md,
  980. int ref_salt_len, int ref_ret )
  981. {
  982. int my_ret;
  983. mbedtls_x509_buf buf;
  984. mbedtls_md_type_t my_msg_md, my_mgf_md;
  985. int my_salt_len;
  986. buf.p = params->x;
  987. buf.len = params->len;
  988. buf.tag = params_tag;
  989. my_ret = mbedtls_x509_get_rsassa_pss_params( &buf, &my_msg_md, &my_mgf_md,
  990. &my_salt_len );
  991. TEST_ASSERT( my_ret == ref_ret );
  992. if( ref_ret == 0 )
  993. {
  994. TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
  995. TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
  996. TEST_ASSERT( my_salt_len == ref_salt_len );
  997. }
  998. exit:
  999. ;;
  1000. }
  1001. /* END_CASE */
  1002. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
  1003. void x509_selftest( )
  1004. {
  1005. TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
  1006. }
  1007. /* END_CASE */