x509_crl.c 19 KB

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