blob: fee8b693ce596c5fd87ea7e5dc62d235109550eb [file] [log] [blame]
Paul Bakkera9507c02011-02-12 15:27:28 +00001/*
2 * CRL reading application
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakkera9507c02011-02-12 15:27:28 +00006 */
7
Felix Conway998760a2025-03-24 11:37:33 +00008#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9
Bence Szépkútic662b362021-05-27 11:25:03 +020010#include "mbedtls/build_info.h"
Paul Bakkera9507c02011-02-12 15:27:28 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
Peter Kolbus9a969b62018-12-11 13:55:56 -060015 !defined(MBEDTLS_X509_CRL_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
Chris Jonesee33c602020-12-16 11:52:37 +000016 defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +010017int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020018{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010020 "MBEDTLS_X509_CRL_PARSE_C and/or MBEDTLS_FS_IO not defined and/or "
21 "MBEDTLS_X509_REMOVE_INFO defined.\n");
22 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020023}
24#else
25
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/x509_crl.h"
Paul Bakkera9507c02011-02-12 15:27:28 +000027
Rich Evans18b78c72015-02-11 14:06:19 +000028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000031
32#define DFL_FILENAME "crl.pem"
33#define DFL_DEBUG_LEVEL 0
34
35#define USAGE \
36 "\n usage: crl_app param=<>...\n" \
37 "\n acceptable parameters:\n" \
38 " filename=%%s default: crl.pem\n" \
39 "\n"
40
Simon Butcher63cb97e2018-12-06 17:43:31 +000041
Paul Bakkera9507c02011-02-12 15:27:28 +000042/*
43 * global options
44 */
Gilles Peskine449bd832023-01-11 14:50:10 +010045struct options {
Paul Bakkeref3f8c72013-06-24 13:01:08 +020046 const char *filename; /* filename of the certificate file */
Paul Bakkera9507c02011-02-12 15:27:28 +000047} opt;
48
Gilles Peskine449bd832023-01-11 14:50:10 +010049int main(int argc, char *argv[])
Paul Bakkera9507c02011-02-12 15:27:28 +000050{
Andres Amaya Garcia898b2082018-04-29 21:47:51 +010051 int ret = 1;
52 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerb8ba90b2011-12-05 14:34:12 +000053 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 mbedtls_x509_crl crl;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010055 int i;
Paul Bakkera9507c02011-02-12 15:27:28 +000056 char *p, *q;
57
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +020058 /*
59 * Set to sane values
60 */
61 mbedtls_x509_crl_init(&crl);
62
Przemek Stekiel89c636e2023-04-14 09:26:39 +020063#if defined(MBEDTLS_USE_PSA_CRYPTO)
64 psa_status_t status = psa_crypto_init();
65 if (status != PSA_SUCCESS) {
66 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
67 (int) status);
68 goto exit;
69 }
70#endif /* MBEDTLS_USE_PSA_CRYPTO */
71
Aditya Deshpande644a5c02023-01-30 15:58:50 +000072 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +010073usage:
74 mbedtls_printf(USAGE);
Paul Bakkera9507c02011-02-12 15:27:28 +000075 goto exit;
76 }
77
78 opt.filename = DFL_FILENAME;
Paul Bakkera9507c02011-02-12 15:27:28 +000079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 for (i = 1; i < argc; i++) {
Paul Bakkera9507c02011-02-12 15:27:28 +000081 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +010082 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkera9507c02011-02-12 15:27:28 +000083 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +010084 }
Paul Bakkera9507c02011-02-12 15:27:28 +000085 *q++ = '\0';
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if (strcmp(p, "filename") == 0) {
Paul Bakkera9507c02011-02-12 15:27:28 +000088 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +010089 } else {
Paul Bakkera9507c02011-02-12 15:27:28 +000090 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +010091 }
Paul Bakkera9507c02011-02-12 15:27:28 +000092 }
93
94 /*
95 * 1.1. Load the CRL
96 */
Gilles Peskine449bd832023-01-11 14:50:10 +010097 mbedtls_printf("\n . Loading the CRL ...");
98 fflush(stdout);
Paul Bakkera9507c02011-02-12 15:27:28 +000099
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 ret = mbedtls_x509_crl_parse_file(&crl, opt.filename);
Paul Bakkera9507c02011-02-12 15:27:28 +0000101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 if (ret != 0) {
103 mbedtls_printf(" failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret);
104 mbedtls_x509_crl_free(&crl);
Paul Bakkera9507c02011-02-12 15:27:28 +0000105 goto exit;
106 }
107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 mbedtls_printf(" ok\n");
Paul Bakkera9507c02011-02-12 15:27:28 +0000109
110 /*
111 * 1.2 Print the CRL
112 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 mbedtls_printf(" . CRL information ...\n");
114 ret = mbedtls_x509_crl_info((char *) buf, sizeof(buf) - 1, " ", &crl);
115 if (ret == -1) {
116 mbedtls_printf(" failed\n ! mbedtls_x509_crl_info returned %d\n\n", ret);
117 mbedtls_x509_crl_free(&crl);
Paul Bakkera9507c02011-02-12 15:27:28 +0000118 goto exit;
119 }
120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 mbedtls_printf("%s\n", buf);
Paul Bakkera9507c02011-02-12 15:27:28 +0000122
Andres Amaya Garcia898b2082018-04-29 21:47:51 +0100123 exit_code = MBEDTLS_EXIT_SUCCESS;
124
Paul Bakkera9507c02011-02-12 15:27:28 +0000125exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 mbedtls_x509_crl_free(&crl);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200127#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiela8c560a2023-04-19 10:15:26 +0200128 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200129#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkera9507c02011-02-12 15:27:28 +0000130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_exit(exit_code);
Paul Bakkera9507c02011-02-12 15:27:28 +0000132}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CRL_PARSE_C &&
134 MBEDTLS_FS_IO */