test_suite_x509parse.function 43 KB

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