x509_crl.c 21 KB

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