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