blob: f45d0b8915281c70103e1cd148765efcb536ed0e [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Paul Bakkera9507c02011-02-12 15:27:28 +000018 */
19
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakkera9507c02011-02-12 15:27:28 +000021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
Peter Kolbus9a969b62018-12-11 13:55:56 -060025 !defined(MBEDTLS_X509_CRL_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
Chris Jonesee33c602020-12-16 11:52:37 +000026 defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +010027int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020028{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010030 "MBEDTLS_X509_CRL_PARSE_C and/or MBEDTLS_FS_IO not defined and/or "
31 "MBEDTLS_X509_REMOVE_INFO defined.\n");
32 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020033}
34#else
35
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/x509_crl.h"
Paul Bakkera9507c02011-02-12 15:27:28 +000037
Rich Evans18b78c72015-02-11 14:06:19 +000038#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000041
42#define DFL_FILENAME "crl.pem"
43#define DFL_DEBUG_LEVEL 0
44
45#define USAGE \
46 "\n usage: crl_app param=<>...\n" \
47 "\n acceptable parameters:\n" \
48 " filename=%%s default: crl.pem\n" \
49 "\n"
50
Simon Butcher63cb97e2018-12-06 17:43:31 +000051
Paul Bakkera9507c02011-02-12 15:27:28 +000052/*
53 * global options
54 */
Gilles Peskine449bd832023-01-11 14:50:10 +010055struct options {
Paul Bakkeref3f8c72013-06-24 13:01:08 +020056 const char *filename; /* filename of the certificate file */
Paul Bakkera9507c02011-02-12 15:27:28 +000057} opt;
58
Gilles Peskine449bd832023-01-11 14:50:10 +010059int main(int argc, char *argv[])
Paul Bakkera9507c02011-02-12 15:27:28 +000060{
Andres Amaya Garcia898b2082018-04-29 21:47:51 +010061 int ret = 1;
62 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerb8ba90b2011-12-05 14:34:12 +000063 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 mbedtls_x509_crl crl;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010065 int i;
Paul Bakkera9507c02011-02-12 15:27:28 +000066 char *p, *q;
67
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +020068 /*
69 * Set to sane values
70 */
71 mbedtls_x509_crl_init(&crl);
72
Przemek Stekiel89c636e2023-04-14 09:26:39 +020073#if defined(MBEDTLS_USE_PSA_CRYPTO)
74 psa_status_t status = psa_crypto_init();
75 if (status != PSA_SUCCESS) {
76 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
77 (int) status);
78 goto exit;
79 }
80#endif /* MBEDTLS_USE_PSA_CRYPTO */
81
Aditya Deshpande644a5c02023-01-30 15:58:50 +000082 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +010083usage:
84 mbedtls_printf(USAGE);
Paul Bakkera9507c02011-02-12 15:27:28 +000085 goto exit;
86 }
87
88 opt.filename = DFL_FILENAME;
Paul Bakkera9507c02011-02-12 15:27:28 +000089
Gilles Peskine449bd832023-01-11 14:50:10 +010090 for (i = 1; i < argc; i++) {
Paul Bakkera9507c02011-02-12 15:27:28 +000091 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkera9507c02011-02-12 15:27:28 +000093 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +010094 }
Paul Bakkera9507c02011-02-12 15:27:28 +000095 *q++ = '\0';
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097 if (strcmp(p, "filename") == 0) {
Paul Bakkera9507c02011-02-12 15:27:28 +000098 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +010099 } else {
Paul Bakkera9507c02011-02-12 15:27:28 +0000100 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 }
Paul Bakkera9507c02011-02-12 15:27:28 +0000102 }
103
104 /*
105 * 1.1. Load the CRL
106 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 mbedtls_printf("\n . Loading the CRL ...");
108 fflush(stdout);
Paul Bakkera9507c02011-02-12 15:27:28 +0000109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 ret = mbedtls_x509_crl_parse_file(&crl, opt.filename);
Paul Bakkera9507c02011-02-12 15:27:28 +0000111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if (ret != 0) {
113 mbedtls_printf(" failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret);
114 mbedtls_x509_crl_free(&crl);
Paul Bakkera9507c02011-02-12 15:27:28 +0000115 goto exit;
116 }
117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 mbedtls_printf(" ok\n");
Paul Bakkera9507c02011-02-12 15:27:28 +0000119
120 /*
121 * 1.2 Print the CRL
122 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 mbedtls_printf(" . CRL information ...\n");
124 ret = mbedtls_x509_crl_info((char *) buf, sizeof(buf) - 1, " ", &crl);
125 if (ret == -1) {
126 mbedtls_printf(" failed\n ! mbedtls_x509_crl_info returned %d\n\n", ret);
127 mbedtls_x509_crl_free(&crl);
Paul Bakkera9507c02011-02-12 15:27:28 +0000128 goto exit;
129 }
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_printf("%s\n", buf);
Paul Bakkera9507c02011-02-12 15:27:28 +0000132
Andres Amaya Garcia898b2082018-04-29 21:47:51 +0100133 exit_code = MBEDTLS_EXIT_SUCCESS;
134
Paul Bakkera9507c02011-02-12 15:27:28 +0000135exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 mbedtls_x509_crl_free(&crl);
Paul Bakkera9507c02011-02-12 15:27:28 +0000137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 mbedtls_exit(exit_code);
Paul Bakkera9507c02011-02-12 15:27:28 +0000139}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CRL_PARSE_C &&
141 MBEDTLS_FS_IO */