blob: e3e0577735e8922c0271c317a641be684ca17e16 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakkera9507c02011-02-12 15:27:28 +000025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
29 !defined(MBEDTLS_X509_CRL_PARSE_C) || !defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010030int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020031{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010033 "MBEDTLS_X509_CRL_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
34 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020035}
36#else
37
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/x509_crl.h"
Paul Bakkera9507c02011-02-12 15:27:28 +000039
Rich Evans18b78c72015-02-11 14:06:19 +000040#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000043
44#define DFL_FILENAME "crl.pem"
45#define DFL_DEBUG_LEVEL 0
46
47#define USAGE \
48 "\n usage: crl_app param=<>...\n" \
49 "\n acceptable parameters:\n" \
50 " filename=%%s default: crl.pem\n" \
51 "\n"
52
Simon Butcher63cb97e2018-12-06 17:43:31 +000053
Paul Bakkera9507c02011-02-12 15:27:28 +000054/*
55 * global options
56 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057struct options {
Paul Bakkeref3f8c72013-06-24 13:01:08 +020058 const char *filename; /* filename of the certificate file */
Paul Bakkera9507c02011-02-12 15:27:28 +000059} opt;
60
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061int main(int argc, char *argv[])
Paul Bakkera9507c02011-02-12 15:27:28 +000062{
Andres Amaya Garcia898b2082018-04-29 21:47:51 +010063 int ret = 1;
64 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerb8ba90b2011-12-05 14:34:12 +000065 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 mbedtls_x509_crl crl;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010067 int i;
Paul Bakkera9507c02011-02-12 15:27:28 +000068 char *p, *q;
69
Przemek Stekielb00688f2023-04-17 11:10:05 +020070 /*
71 * Set to sane values
72 */
73 mbedtls_x509_crl_init(&crl);
74
Przemek Stekield381d2d2023-04-14 09:26:39 +020075#if defined(MBEDTLS_USE_PSA_CRYPTO)
76 psa_status_t status = psa_crypto_init();
77 if (status != PSA_SUCCESS) {
78 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
79 (int) status);
80 goto exit;
81 }
82#endif /* MBEDTLS_USE_PSA_CRYPTO */
83
Aditya Deshpande0504ac22023-01-30 15:58:50 +000084 if (argc < 2) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085usage:
86 mbedtls_printf(USAGE);
Paul Bakkera9507c02011-02-12 15:27:28 +000087 goto exit;
88 }
89
90 opt.filename = DFL_FILENAME;
Paul Bakkera9507c02011-02-12 15:27:28 +000091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 for (i = 1; i < argc; i++) {
Paul Bakkera9507c02011-02-12 15:27:28 +000093 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkera9507c02011-02-12 15:27:28 +000095 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 }
Paul Bakkera9507c02011-02-12 15:27:28 +000097 *q++ = '\0';
98
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 if (strcmp(p, "filename") == 0) {
Paul Bakkera9507c02011-02-12 15:27:28 +0000100 opt.filename = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 } else {
Paul Bakkera9507c02011-02-12 15:27:28 +0000102 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 }
Paul Bakkera9507c02011-02-12 15:27:28 +0000104 }
105
106 /*
107 * 1.1. Load the CRL
108 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 mbedtls_printf("\n . Loading the CRL ...");
110 fflush(stdout);
Paul Bakkera9507c02011-02-12 15:27:28 +0000111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 ret = mbedtls_x509_crl_parse_file(&crl, opt.filename);
Paul Bakkera9507c02011-02-12 15:27:28 +0000113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 if (ret != 0) {
115 mbedtls_printf(" failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret);
116 mbedtls_x509_crl_free(&crl);
Paul Bakkera9507c02011-02-12 15:27:28 +0000117 goto exit;
118 }
119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 mbedtls_printf(" ok\n");
Paul Bakkera9507c02011-02-12 15:27:28 +0000121
122 /*
123 * 1.2 Print the CRL
124 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 mbedtls_printf(" . CRL information ...\n");
126 ret = mbedtls_x509_crl_info((char *) buf, sizeof(buf) - 1, " ", &crl);
127 if (ret == -1) {
128 mbedtls_printf(" failed\n ! mbedtls_x509_crl_info returned %d\n\n", ret);
129 mbedtls_x509_crl_free(&crl);
Paul Bakkera9507c02011-02-12 15:27:28 +0000130 goto exit;
131 }
132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 mbedtls_printf("%s\n", buf);
Paul Bakkera9507c02011-02-12 15:27:28 +0000134
Andres Amaya Garcia898b2082018-04-29 21:47:51 +0100135 exit_code = MBEDTLS_EXIT_SUCCESS;
136
Paul Bakkera9507c02011-02-12 15:27:28 +0000137exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 mbedtls_x509_crl_free(&crl);
Przemek Stekield4d049b2023-04-19 13:47:43 +0200139#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielc4ddf922023-04-19 10:15:26 +0200140 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200141#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkera9507c02011-02-12 15:27:28 +0000142
Paul Bakkercce9d772011-11-18 14:26:47 +0000143#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 mbedtls_printf(" + Press Enter to exit this program.\n");
145 fflush(stdout); getchar();
Paul Bakkera9507c02011-02-12 15:27:28 +0000146#endif
147
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 mbedtls_exit(exit_code);
Paul Bakkera9507c02011-02-12 15:27:28 +0000149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CRL_PARSE_C &&
151 MBEDTLS_FS_IO */