x509_crl.c 21 KB

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