blob: aa6f2855a3d16f9a48714bf2fa6b67970478fa94 [file] [log] [blame]
Paul Bakkera9507c02011-02-12 15:27:28 +00001/*
2 * CRL reading application
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkera9507c02011-02-12 15:27:28 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakkera9507c02011-02-12 15:27:28 +000045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakkera9507c02011-02-12 15:27:28 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000055#else
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
Andres Amaya Garcia898b2082018-04-29 21:47:51 +010057#include <stdlib.h>
58#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010059#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010060#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia898b2082018-04-29 21:47:51 +010061#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
62#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
65 !defined(MBEDTLS_X509_CRL_PARSE_C) || !defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020066int main( void )
67{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
69 "MBEDTLS_X509_CRL_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020070 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020071}
72#else
73
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000074#include "mbedtls/x509_crl.h"
Paul Bakkera9507c02011-02-12 15:27:28 +000075
Rich Evans18b78c72015-02-11 14:06:19 +000076#include <stdio.h>
77#include <stdlib.h>
78#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000079
80#define DFL_FILENAME "crl.pem"
81#define DFL_DEBUG_LEVEL 0
82
83#define USAGE \
84 "\n usage: crl_app param=<>...\n" \
85 "\n acceptable parameters:\n" \
86 " filename=%%s default: crl.pem\n" \
87 "\n"
88
Simon Butcher63cb97e2018-12-06 17:43:31 +000089
Paul Bakkera9507c02011-02-12 15:27:28 +000090/*
91 * global options
92 */
93struct options
94{
Paul Bakkeref3f8c72013-06-24 13:01:08 +020095 const char *filename; /* filename of the certificate file */
Paul Bakkera9507c02011-02-12 15:27:28 +000096} opt;
97
Paul Bakkera9507c02011-02-12 15:27:28 +000098int main( int argc, char *argv[] )
99{
Andres Amaya Garcia898b2082018-04-29 21:47:51 +0100100 int ret = 1;
101 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerb8ba90b2011-12-05 14:34:12 +0000102 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_x509_crl crl;
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100104 int i;
Paul Bakkera9507c02011-02-12 15:27:28 +0000105 char *p, *q;
106
107 /*
108 * Set to sane values
109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_x509_crl_init( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000111
112 if( argc == 0 )
113 {
114 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_printf( USAGE );
Paul Bakkera9507c02011-02-12 15:27:28 +0000116 goto exit;
117 }
118
119 opt.filename = DFL_FILENAME;
Paul Bakkera9507c02011-02-12 15:27:28 +0000120
121 for( i = 1; i < argc; i++ )
122 {
Paul Bakkera9507c02011-02-12 15:27:28 +0000123 p = argv[i];
124 if( ( q = strchr( p, '=' ) ) == NULL )
125 goto usage;
126 *q++ = '\0';
127
128 if( strcmp( p, "filename" ) == 0 )
129 opt.filename = q;
Paul Bakkera9507c02011-02-12 15:27:28 +0000130 else
131 goto usage;
132 }
133
134 /*
135 * 1.1. Load the CRL
136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_printf( "\n . Loading the CRL ..." );
Paul Bakkera9507c02011-02-12 15:27:28 +0000138 fflush( stdout );
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 ret = mbedtls_x509_crl_parse_file( &crl, opt.filename );
Paul Bakkera9507c02011-02-12 15:27:28 +0000141
142 if( ret != 0 )
143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_printf( " failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret );
145 mbedtls_x509_crl_free( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000146 goto exit;
147 }
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_printf( " ok\n" );
Paul Bakkera9507c02011-02-12 15:27:28 +0000150
151 /*
152 * 1.2 Print the CRL
153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_printf( " . CRL information ...\n" );
155 ret = mbedtls_x509_crl_info( (char *) buf, sizeof( buf ) - 1, " ", &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000156 if( ret == -1 )
157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_printf( " failed\n ! mbedtls_x509_crl_info returned %d\n\n", ret );
159 mbedtls_x509_crl_free( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000160 goto exit;
161 }
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_printf( "%s\n", buf );
Paul Bakkera9507c02011-02-12 15:27:28 +0000164
Andres Amaya Garcia898b2082018-04-29 21:47:51 +0100165 exit_code = MBEDTLS_EXIT_SUCCESS;
166
Paul Bakkera9507c02011-02-12 15:27:28 +0000167exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_x509_crl_free( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000169
Paul Bakkercce9d772011-11-18 14:26:47 +0000170#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkera9507c02011-02-12 15:27:28 +0000172 fflush( stdout ); getchar();
173#endif
174
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200175 mbedtls_exit( exit_code );
Paul Bakkera9507c02011-02-12 15:27:28 +0000176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CRL_PARSE_C &&
178 MBEDTLS_FS_IO */