blob: 5e3fd5a941acf01abfc0fdc4d05f760d119a7feb [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
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakkera9507c02011-02-12 15:27:28 +00009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
Peter Kolbus9a969b62018-12-11 13:55:56 -060013 !defined(MBEDTLS_X509_CRL_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
Chris Jonesee33c602020-12-16 11:52:37 +000014 defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +010015int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020016{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010018 "MBEDTLS_X509_CRL_PARSE_C and/or MBEDTLS_FS_IO not defined and/or "
19 "MBEDTLS_X509_REMOVE_INFO defined.\n");
20 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020021}
22#else
23
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/x509_crl.h"
Paul Bakkera9507c02011-02-12 15:27:28 +000025
Rich Evans18b78c72015-02-11 14:06:19 +000026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000029
30#define DFL_FILENAME "crl.pem"
31#define DFL_DEBUG_LEVEL 0
32
33#define USAGE \
34 "\n usage: crl_app param=<>...\n" \
35 "\n acceptable parameters:\n" \
36 " filename=%%s default: crl.pem\n" \
37 "\n"
38
Simon Butcher63cb97e2018-12-06 17:43:31 +000039
Paul Bakkera9507c02011-02-12 15:27:28 +000040/*
41 * global options
42 */
Gilles Peskine449bd832023-01-11 14:50:10 +010043struct options {
Paul Bakkeref3f8c72013-06-24 13:01:08 +020044 const char *filename; /* filename of the certificate file */
Paul Bakkera9507c02011-02-12 15:27:28 +000045} opt;
46
Gilles Peskine449bd832023-01-11 14:50:10 +010047int main(int argc, char *argv[])
Paul Bakkera9507c02011-02-12 15:27:28 +000048{
Andres Amaya Garcia898b2082018-04-29 21:47:51 +010049 int ret = 1;
50 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerb8ba90b2011-12-05 14:34:12 +000051 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 mbedtls_x509_crl crl;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010053 int i;
Paul Bakkera9507c02011-02-12 15:27:28 +000054 char *p, *q;
55
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +020056 /*
57 * Set to sane values
58 */
59 mbedtls_x509_crl_init(&crl);
60
Przemek Stekiel89c636e2023-04-14 09:26:39 +020061#if defined(MBEDTLS_USE_PSA_CRYPTO)
62 psa_status_t status = psa_crypto_init();
63 if (status != PSA_SUCCESS) {
64 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
65 (int) status);
66 goto exit;
67 }
68#endif /* MBEDTLS_USE_PSA_CRYPTO */
69
Aditya Deshpande644a5c02023-01-30 15:58:50 +000070 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +010071usage:
72 mbedtls_printf(USAGE);
Paul Bakkera9507c02011-02-12 15:27:28 +000073 goto exit;
74 }
75
76 opt.filename = DFL_FILENAME;
Paul Bakkera9507c02011-02-12 15:27:28 +000077
Gilles Peskine449bd832023-01-11 14:50:10 +010078 for (i = 1; i < argc; i++) {
Paul Bakkera9507c02011-02-12 15:27:28 +000079 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +010080 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkera9507c02011-02-12 15:27:28 +000081 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +010082 }
Paul Bakkera9507c02011-02-12 15:27:28 +000083 *q++ = '\0';
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085 if (strcmp(p, "filename") == 0) {
Paul Bakkera9507c02011-02-12 15:27:28 +000086 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +010087 } else {
Paul Bakkera9507c02011-02-12 15:27:28 +000088 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +010089 }
Paul Bakkera9507c02011-02-12 15:27:28 +000090 }
91
92 /*
93 * 1.1. Load the CRL
94 */
Gilles Peskine449bd832023-01-11 14:50:10 +010095 mbedtls_printf("\n . Loading the CRL ...");
96 fflush(stdout);
Paul Bakkera9507c02011-02-12 15:27:28 +000097
Gilles Peskine449bd832023-01-11 14:50:10 +010098 ret = mbedtls_x509_crl_parse_file(&crl, opt.filename);
Paul Bakkera9507c02011-02-12 15:27:28 +000099
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 if (ret != 0) {
101 mbedtls_printf(" failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret);
102 mbedtls_x509_crl_free(&crl);
Paul Bakkera9507c02011-02-12 15:27:28 +0000103 goto exit;
104 }
105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 mbedtls_printf(" ok\n");
Paul Bakkera9507c02011-02-12 15:27:28 +0000107
108 /*
109 * 1.2 Print the CRL
110 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_printf(" . CRL information ...\n");
112 ret = mbedtls_x509_crl_info((char *) buf, sizeof(buf) - 1, " ", &crl);
113 if (ret == -1) {
114 mbedtls_printf(" failed\n ! mbedtls_x509_crl_info returned %d\n\n", ret);
115 mbedtls_x509_crl_free(&crl);
Paul Bakkera9507c02011-02-12 15:27:28 +0000116 goto exit;
117 }
118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 mbedtls_printf("%s\n", buf);
Paul Bakkera9507c02011-02-12 15:27:28 +0000120
Andres Amaya Garcia898b2082018-04-29 21:47:51 +0100121 exit_code = MBEDTLS_EXIT_SUCCESS;
122
Paul Bakkera9507c02011-02-12 15:27:28 +0000123exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 mbedtls_x509_crl_free(&crl);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200125#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiela8c560a2023-04-19 10:15:26 +0200126 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200127#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkera9507c02011-02-12 15:27:28 +0000128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 mbedtls_exit(exit_code);
Paul Bakkera9507c02011-02-12 15:27:28 +0000130}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CRL_PARSE_C &&
132 MBEDTLS_FS_IO */