x509_crl.c 22 KB

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