x509_crl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  2. * X.509 Certificate Revocation List (CRL) parsing
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. /*
  8. * The ITU-T X.509 standard defines a certificate format for PKI.
  9. *
  10. * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
  11. * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
  12. * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
  13. *
  14. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
  15. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
  16. */
  17. #include "common.h"
  18. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  19. #include "mbedtls/x509_crl.h"
  20. #include "mbedtls/error.h"
  21. #include "mbedtls/oid.h"
  22. #include "mbedtls/platform_util.h"
  23. #include <string.h>
  24. #if defined(MBEDTLS_PEM_PARSE_C)
  25. #include "mbedtls/pem.h"
  26. #endif
  27. #include "mbedtls/platform.h"
  28. #if defined(MBEDTLS_HAVE_TIME)
  29. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  30. #include <windows.h>
  31. #else
  32. #include <time.h>
  33. #endif
  34. #endif
  35. #if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
  36. #include <stdio.h>
  37. #endif
  38. /*
  39. * Version ::= INTEGER { v1(0), v2(1) }
  40. */
  41. static int x509_crl_get_version(unsigned char **p,
  42. const unsigned char *end,
  43. int *ver)
  44. {
  45. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  46. if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
  47. if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
  48. *ver = 0;
  49. return 0;
  50. }
  51. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
  52. }
  53. return 0;
  54. }
  55. /*
  56. * X.509 CRL v2 extensions
  57. *
  58. * We currently don't parse any extension's content, but we do check that the
  59. * list of extensions is well-formed and abort on critical extensions (that
  60. * are unsupported as we don't support any extension so far)
  61. */
  62. static int x509_get_crl_ext(unsigned char **p,
  63. const unsigned char *end,
  64. mbedtls_x509_buf *ext)
  65. {
  66. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  67. if (*p == end) {
  68. return 0;
  69. }
  70. /*
  71. * crlExtensions [0] EXPLICIT Extensions OPTIONAL
  72. * -- if present, version MUST be v2
  73. */
  74. if ((ret = mbedtls_x509_get_ext(p, end, ext, 0)) != 0) {
  75. return ret;
  76. }
  77. end = ext->p + ext->len;
  78. while (*p < end) {
  79. /*
  80. * Extension ::= SEQUENCE {
  81. * extnID OBJECT IDENTIFIER,
  82. * critical BOOLEAN DEFAULT FALSE,
  83. * extnValue OCTET STRING }
  84. */
  85. int is_critical = 0;
  86. const unsigned char *end_ext_data;
  87. size_t len;
  88. /* Get enclosing sequence tag */
  89. if ((ret = mbedtls_asn1_get_tag(p, end, &len,
  90. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  91. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  92. }
  93. end_ext_data = *p + len;
  94. /* Get OID (currently ignored) */
  95. if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
  96. MBEDTLS_ASN1_OID)) != 0) {
  97. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  98. }
  99. *p += len;
  100. /* Get optional critical */
  101. if ((ret = mbedtls_asn1_get_bool(p, end_ext_data,
  102. &is_critical)) != 0 &&
  103. (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
  104. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  105. }
  106. /* Data should be octet string type */
  107. if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
  108. MBEDTLS_ASN1_OCTET_STRING)) != 0) {
  109. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  110. }
  111. /* Ignore data so far and just check its length */
  112. *p += len;
  113. if (*p != end_ext_data) {
  114. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  115. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  116. }
  117. /* Abort on (unsupported) critical extensions */
  118. if (is_critical) {
  119. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  120. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  121. }
  122. }
  123. if (*p != end) {
  124. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  125. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  126. }
  127. return 0;
  128. }
  129. /*
  130. * X.509 CRL v2 entry extensions (no extensions parsed yet.)
  131. */
  132. static int x509_get_crl_entry_ext(unsigned char **p,
  133. const unsigned char *end,
  134. mbedtls_x509_buf *ext)
  135. {
  136. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  137. size_t len = 0;
  138. /* OPTIONAL */
  139. if (end <= *p) {
  140. return 0;
  141. }
  142. ext->tag = **p;
  143. ext->p = *p;
  144. /*
  145. * Get CRL-entry extension sequence header
  146. * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
  147. */
  148. if ((ret = mbedtls_asn1_get_tag(p, end, &ext->len,
  149. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  150. if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
  151. ext->p = NULL;
  152. return 0;
  153. }
  154. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  155. }
  156. end = *p + ext->len;
  157. if (end != *p + ext->len) {
  158. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  159. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  160. }
  161. while (*p < end) {
  162. if ((ret = mbedtls_asn1_get_tag(p, end, &len,
  163. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  164. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  165. }
  166. *p += len;
  167. }
  168. if (*p != end) {
  169. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  170. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  171. }
  172. return 0;
  173. }
  174. /*
  175. * X.509 CRL Entries
  176. */
  177. static int x509_get_entries(unsigned char **p,
  178. const unsigned char *end,
  179. mbedtls_x509_crl_entry *entry)
  180. {
  181. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  182. size_t entry_len;
  183. mbedtls_x509_crl_entry *cur_entry = entry;
  184. if (*p == end) {
  185. return 0;
  186. }
  187. if ((ret = mbedtls_asn1_get_tag(p, end, &entry_len,
  188. MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED)) != 0) {
  189. if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
  190. return 0;
  191. }
  192. return ret;
  193. }
  194. end = *p + entry_len;
  195. while (*p < end) {
  196. size_t len2;
  197. const unsigned char *end2;
  198. cur_entry->raw.tag = **p;
  199. if ((ret = mbedtls_asn1_get_tag(p, end, &len2,
  200. MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED)) != 0) {
  201. return ret;
  202. }
  203. cur_entry->raw.p = *p;
  204. cur_entry->raw.len = len2;
  205. end2 = *p + len2;
  206. if ((ret = mbedtls_x509_get_serial(p, end2, &cur_entry->serial)) != 0) {
  207. return ret;
  208. }
  209. if ((ret = mbedtls_x509_get_time(p, end2,
  210. &cur_entry->revocation_date)) != 0) {
  211. return ret;
  212. }
  213. if ((ret = x509_get_crl_entry_ext(p, end2,
  214. &cur_entry->entry_ext)) != 0) {
  215. return ret;
  216. }
  217. if (*p < end) {
  218. cur_entry->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crl_entry));
  219. if (cur_entry->next == NULL) {
  220. return MBEDTLS_ERR_X509_ALLOC_FAILED;
  221. }
  222. cur_entry = cur_entry->next;
  223. }
  224. }
  225. return 0;
  226. }
  227. /*
  228. * Parse one CRLs in DER format and append it to the chained list
  229. */
  230. int mbedtls_x509_crl_parse_der(mbedtls_x509_crl *chain,
  231. const unsigned char *buf, size_t buflen)
  232. {
  233. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  234. size_t len;
  235. unsigned char *p = NULL, *end = NULL;
  236. mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
  237. mbedtls_x509_crl *crl = chain;
  238. /*
  239. * Check for valid input
  240. */
  241. if (crl == NULL || buf == NULL) {
  242. return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  243. }
  244. memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
  245. memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
  246. memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
  247. /*
  248. * Add new CRL on the end of the chain if needed.
  249. */
  250. while (crl->version != 0 && crl->next != NULL) {
  251. crl = crl->next;
  252. }
  253. if (crl->version != 0 && crl->next == NULL) {
  254. crl->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crl));
  255. if (crl->next == NULL) {
  256. mbedtls_x509_crl_free(crl);
  257. return MBEDTLS_ERR_X509_ALLOC_FAILED;
  258. }
  259. mbedtls_x509_crl_init(crl->next);
  260. crl = crl->next;
  261. }
  262. /*
  263. * Copy raw DER-encoded CRL
  264. */
  265. if (buflen == 0) {
  266. return MBEDTLS_ERR_X509_INVALID_FORMAT;
  267. }
  268. p = mbedtls_calloc(1, buflen);
  269. if (p == NULL) {
  270. return MBEDTLS_ERR_X509_ALLOC_FAILED;
  271. }
  272. memcpy(p, buf, buflen);
  273. crl->raw.p = p;
  274. crl->raw.len = buflen;
  275. end = p + buflen;
  276. /*
  277. * CertificateList ::= SEQUENCE {
  278. * tbsCertList TBSCertList,
  279. * signatureAlgorithm AlgorithmIdentifier,
  280. * signatureValue BIT STRING }
  281. */
  282. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  283. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  284. mbedtls_x509_crl_free(crl);
  285. return MBEDTLS_ERR_X509_INVALID_FORMAT;
  286. }
  287. if (len != (size_t) (end - p)) {
  288. mbedtls_x509_crl_free(crl);
  289. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
  290. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  291. }
  292. /*
  293. * TBSCertList ::= SEQUENCE {
  294. */
  295. crl->tbs.p = p;
  296. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  297. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  298. mbedtls_x509_crl_free(crl);
  299. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
  300. }
  301. end = p + len;
  302. crl->tbs.len = end - crl->tbs.p;
  303. /*
  304. * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
  305. * -- if present, MUST be v2
  306. *
  307. * signature AlgorithmIdentifier
  308. */
  309. if ((ret = x509_crl_get_version(&p, end, &crl->version)) != 0 ||
  310. (ret = mbedtls_x509_get_alg(&p, end, &crl->sig_oid, &sig_params1)) != 0) {
  311. mbedtls_x509_crl_free(crl);
  312. return ret;
  313. }
  314. if (crl->version < 0 || crl->version > 1) {
  315. mbedtls_x509_crl_free(crl);
  316. return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
  317. }
  318. crl->version++;
  319. if ((ret = mbedtls_x509_get_sig_alg(&crl->sig_oid, &sig_params1,
  320. &crl->sig_md, &crl->sig_pk,
  321. &crl->sig_opts)) != 0) {
  322. mbedtls_x509_crl_free(crl);
  323. return MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG;
  324. }
  325. /*
  326. * issuer Name
  327. */
  328. crl->issuer_raw.p = p;
  329. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  330. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  331. mbedtls_x509_crl_free(crl);
  332. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
  333. }
  334. if ((ret = mbedtls_x509_get_name(&p, p + len, &crl->issuer)) != 0) {
  335. mbedtls_x509_crl_free(crl);
  336. return ret;
  337. }
  338. crl->issuer_raw.len = p - crl->issuer_raw.p;
  339. /*
  340. * thisUpdate Time
  341. * nextUpdate Time OPTIONAL
  342. */
  343. if ((ret = mbedtls_x509_get_time(&p, end, &crl->this_update)) != 0) {
  344. mbedtls_x509_crl_free(crl);
  345. return ret;
  346. }
  347. if ((ret = mbedtls_x509_get_time(&p, end, &crl->next_update)) != 0) {
  348. if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
  349. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) &&
  350. ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
  351. MBEDTLS_ERR_ASN1_OUT_OF_DATA))) {
  352. mbedtls_x509_crl_free(crl);
  353. return ret;
  354. }
  355. }
  356. /*
  357. * revokedCertificates SEQUENCE OF SEQUENCE {
  358. * userCertificate CertificateSerialNumber,
  359. * revocationDate Time,
  360. * crlEntryExtensions Extensions OPTIONAL
  361. * -- if present, MUST be v2
  362. * } OPTIONAL
  363. */
  364. if ((ret = x509_get_entries(&p, end, &crl->entry)) != 0) {
  365. mbedtls_x509_crl_free(crl);
  366. return ret;
  367. }
  368. /*
  369. * crlExtensions EXPLICIT Extensions OPTIONAL
  370. * -- if present, MUST be v2
  371. */
  372. if (crl->version == 2) {
  373. ret = x509_get_crl_ext(&p, end, &crl->crl_ext);
  374. if (ret != 0) {
  375. mbedtls_x509_crl_free(crl);
  376. return ret;
  377. }
  378. }
  379. if (p != end) {
  380. mbedtls_x509_crl_free(crl);
  381. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
  382. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  383. }
  384. end = crl->raw.p + crl->raw.len;
  385. /*
  386. * signatureAlgorithm AlgorithmIdentifier,
  387. * signatureValue BIT STRING
  388. */
  389. if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
  390. mbedtls_x509_crl_free(crl);
  391. return ret;
  392. }
  393. if (crl->sig_oid.len != sig_oid2.len ||
  394. memcmp(crl->sig_oid.p, sig_oid2.p, crl->sig_oid.len) != 0 ||
  395. sig_params1.len != sig_params2.len ||
  396. (sig_params1.len != 0 &&
  397. memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
  398. mbedtls_x509_crl_free(crl);
  399. return MBEDTLS_ERR_X509_SIG_MISMATCH;
  400. }
  401. if ((ret = mbedtls_x509_get_sig(&p, end, &crl->sig)) != 0) {
  402. mbedtls_x509_crl_free(crl);
  403. return ret;
  404. }
  405. if (p != end) {
  406. mbedtls_x509_crl_free(crl);
  407. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
  408. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  409. }
  410. return 0;
  411. }
  412. /*
  413. * Parse one or more CRLs and add them to the chained list
  414. */
  415. int mbedtls_x509_crl_parse(mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen)
  416. {
  417. #if defined(MBEDTLS_PEM_PARSE_C)
  418. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  419. size_t use_len = 0;
  420. mbedtls_pem_context pem;
  421. int is_pem = 0;
  422. if (chain == NULL || buf == NULL) {
  423. return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  424. }
  425. do {
  426. mbedtls_pem_init(&pem);
  427. // Avoid calling mbedtls_pem_read_buffer() on non-null-terminated
  428. // string
  429. if (buflen == 0 || buf[buflen - 1] != '\0') {
  430. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  431. } else {
  432. ret = mbedtls_pem_read_buffer(&pem,
  433. "-----BEGIN X509 CRL-----",
  434. "-----END X509 CRL-----",
  435. buf, NULL, 0, &use_len);
  436. }
  437. if (ret == 0) {
  438. /*
  439. * Was PEM encoded
  440. */
  441. is_pem = 1;
  442. buflen -= use_len;
  443. buf += use_len;
  444. if ((ret = mbedtls_x509_crl_parse_der(chain,
  445. pem.buf, pem.buflen)) != 0) {
  446. mbedtls_pem_free(&pem);
  447. return ret;
  448. }
  449. } else if (is_pem) {
  450. mbedtls_pem_free(&pem);
  451. return ret;
  452. }
  453. mbedtls_pem_free(&pem);
  454. }
  455. /* In the PEM case, buflen is 1 at the end, for the terminated NULL byte.
  456. * And a valid CRL cannot be less than 1 byte anyway. */
  457. while (is_pem && buflen > 1);
  458. if (is_pem) {
  459. return 0;
  460. } else
  461. #endif /* MBEDTLS_PEM_PARSE_C */
  462. return mbedtls_x509_crl_parse_der(chain, buf, buflen);
  463. }
  464. #if defined(MBEDTLS_FS_IO)
  465. /*
  466. * Load one or more CRLs and add them to the chained list
  467. */
  468. int mbedtls_x509_crl_parse_file(mbedtls_x509_crl *chain, const char *path)
  469. {
  470. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  471. size_t n;
  472. unsigned char *buf;
  473. if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
  474. return ret;
  475. }
  476. ret = mbedtls_x509_crl_parse(chain, buf, n);
  477. mbedtls_platform_zeroize(buf, n);
  478. mbedtls_free(buf);
  479. return ret;
  480. }
  481. #endif /* MBEDTLS_FS_IO */
  482. /*
  483. * Return an informational string about the certificate.
  484. */
  485. #define BEFORE_COLON 14
  486. #define BC "14"
  487. /*
  488. * Return an informational string about the CRL.
  489. */
  490. int mbedtls_x509_crl_info(char *buf, size_t size, const char *prefix,
  491. const mbedtls_x509_crl *crl)
  492. {
  493. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  494. size_t n;
  495. char *p;
  496. const mbedtls_x509_crl_entry *entry;
  497. p = buf;
  498. n = size;
  499. ret = mbedtls_snprintf(p, n, "%sCRL version : %d",
  500. prefix, crl->version);
  501. MBEDTLS_X509_SAFE_SNPRINTF;
  502. ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
  503. MBEDTLS_X509_SAFE_SNPRINTF;
  504. ret = mbedtls_x509_dn_gets(p, n, &crl->issuer);
  505. MBEDTLS_X509_SAFE_SNPRINTF;
  506. ret = mbedtls_snprintf(p, n, "\n%sthis update : " \
  507. "%04d-%02d-%02d %02d:%02d:%02d", prefix,
  508. crl->this_update.year, crl->this_update.mon,
  509. crl->this_update.day, crl->this_update.hour,
  510. crl->this_update.min, crl->this_update.sec);
  511. MBEDTLS_X509_SAFE_SNPRINTF;
  512. ret = mbedtls_snprintf(p, n, "\n%snext update : " \
  513. "%04d-%02d-%02d %02d:%02d:%02d", prefix,
  514. crl->next_update.year, crl->next_update.mon,
  515. crl->next_update.day, crl->next_update.hour,
  516. crl->next_update.min, crl->next_update.sec);
  517. MBEDTLS_X509_SAFE_SNPRINTF;
  518. entry = &crl->entry;
  519. ret = mbedtls_snprintf(p, n, "\n%sRevoked certificates:",
  520. prefix);
  521. MBEDTLS_X509_SAFE_SNPRINTF;
  522. while (entry != NULL && entry->raw.len != 0) {
  523. ret = mbedtls_snprintf(p, n, "\n%sserial number: ",
  524. prefix);
  525. MBEDTLS_X509_SAFE_SNPRINTF;
  526. ret = mbedtls_x509_serial_gets(p, n, &entry->serial);
  527. MBEDTLS_X509_SAFE_SNPRINTF;
  528. ret = mbedtls_snprintf(p, n, " revocation date: " \
  529. "%04d-%02d-%02d %02d:%02d:%02d",
  530. entry->revocation_date.year, entry->revocation_date.mon,
  531. entry->revocation_date.day, entry->revocation_date.hour,
  532. entry->revocation_date.min, entry->revocation_date.sec);
  533. MBEDTLS_X509_SAFE_SNPRINTF;
  534. entry = entry->next;
  535. }
  536. ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
  537. MBEDTLS_X509_SAFE_SNPRINTF;
  538. ret = mbedtls_x509_sig_alg_gets(p, n, &crl->sig_oid, crl->sig_pk, crl->sig_md,
  539. crl->sig_opts);
  540. MBEDTLS_X509_SAFE_SNPRINTF;
  541. ret = mbedtls_snprintf(p, n, "\n");
  542. MBEDTLS_X509_SAFE_SNPRINTF;
  543. return (int) (size - n);
  544. }
  545. /*
  546. * Initialize a CRL chain
  547. */
  548. void mbedtls_x509_crl_init(mbedtls_x509_crl *crl)
  549. {
  550. memset(crl, 0, sizeof(mbedtls_x509_crl));
  551. }
  552. /*
  553. * Unallocate all CRL data
  554. */
  555. void mbedtls_x509_crl_free(mbedtls_x509_crl *crl)
  556. {
  557. mbedtls_x509_crl *crl_cur = crl;
  558. mbedtls_x509_crl *crl_prv;
  559. mbedtls_x509_name *name_cur;
  560. mbedtls_x509_name *name_prv;
  561. mbedtls_x509_crl_entry *entry_cur;
  562. mbedtls_x509_crl_entry *entry_prv;
  563. if (crl == NULL) {
  564. return;
  565. }
  566. do {
  567. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  568. mbedtls_free(crl_cur->sig_opts);
  569. #endif
  570. name_cur = crl_cur->issuer.next;
  571. while (name_cur != NULL) {
  572. name_prv = name_cur;
  573. name_cur = name_cur->next;
  574. mbedtls_platform_zeroize(name_prv, sizeof(mbedtls_x509_name));
  575. mbedtls_free(name_prv);
  576. }
  577. entry_cur = crl_cur->entry.next;
  578. while (entry_cur != NULL) {
  579. entry_prv = entry_cur;
  580. entry_cur = entry_cur->next;
  581. mbedtls_platform_zeroize(entry_prv,
  582. sizeof(mbedtls_x509_crl_entry));
  583. mbedtls_free(entry_prv);
  584. }
  585. if (crl_cur->raw.p != NULL) {
  586. mbedtls_platform_zeroize(crl_cur->raw.p, crl_cur->raw.len);
  587. mbedtls_free(crl_cur->raw.p);
  588. }
  589. crl_cur = crl_cur->next;
  590. } while (crl_cur != NULL);
  591. crl_cur = crl;
  592. do {
  593. crl_prv = crl_cur;
  594. crl_cur = crl_cur->next;
  595. mbedtls_platform_zeroize(crl_prv, sizeof(mbedtls_x509_crl));
  596. if (crl_prv != crl) {
  597. mbedtls_free(crl_prv);
  598. }
  599. } while (crl_cur != NULL);
  600. }
  601. #endif /* MBEDTLS_X509_CRL_PARSE_C */