blob: 4f1104b8a352510e10d5a27fc8444c6c8b845f22 [file] [log] [blame]
Paul Bakkera9507c02011-02-12 15:27:28 +00001/*
2 * CRL reading application
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-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úti4e9f7122020-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 Garcia8fe4d912018-04-29 21:47:51 +010057#include <stdlib.h>
58#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020059#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010060#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia8fe4d912018-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 Stachowiaka0865222019-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
Paul Bakkera9507c02011-02-12 15:27:28 +000089/*
90 * global options
91 */
92struct options
93{
Paul Bakkeref3f8c72013-06-24 13:01:08 +020094 const char *filename; /* filename of the certificate file */
Paul Bakkera9507c02011-02-12 15:27:28 +000095} opt;
96
Paul Bakkera9507c02011-02-12 15:27:28 +000097int main( int argc, char *argv[] )
98{
Andres Amaya Garcia8fe4d912018-04-29 21:47:51 +010099 int ret = 1;
100 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerb8ba90b2011-12-05 14:34:12 +0000101 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_x509_crl crl;
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100103 int i;
Paul Bakkera9507c02011-02-12 15:27:28 +0000104 char *p, *q;
105
106 /*
107 * Set to sane values
108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_x509_crl_init( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000110
111 if( argc == 0 )
112 {
113 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_printf( USAGE );
Paul Bakkera9507c02011-02-12 15:27:28 +0000115 goto exit;
116 }
117
118 opt.filename = DFL_FILENAME;
Paul Bakkera9507c02011-02-12 15:27:28 +0000119
120 for( i = 1; i < argc; i++ )
121 {
Paul Bakkera9507c02011-02-12 15:27:28 +0000122 p = argv[i];
123 if( ( q = strchr( p, '=' ) ) == NULL )
124 goto usage;
125 *q++ = '\0';
126
127 if( strcmp( p, "filename" ) == 0 )
128 opt.filename = q;
Paul Bakkera9507c02011-02-12 15:27:28 +0000129 else
130 goto usage;
131 }
132
133 /*
134 * 1.1. Load the CRL
135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_printf( "\n . Loading the CRL ..." );
Paul Bakkera9507c02011-02-12 15:27:28 +0000137 fflush( stdout );
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 ret = mbedtls_x509_crl_parse_file( &crl, opt.filename );
Paul Bakkera9507c02011-02-12 15:27:28 +0000140
141 if( ret != 0 )
142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_printf( " failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret );
144 mbedtls_x509_crl_free( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000145 goto exit;
146 }
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_printf( " ok\n" );
Paul Bakkera9507c02011-02-12 15:27:28 +0000149
150 /*
151 * 1.2 Print the CRL
152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_printf( " . CRL information ...\n" );
154 ret = mbedtls_x509_crl_info( (char *) buf, sizeof( buf ) - 1, " ", &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000155 if( ret == -1 )
156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_printf( " failed\n ! mbedtls_x509_crl_info returned %d\n\n", ret );
158 mbedtls_x509_crl_free( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000159 goto exit;
160 }
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_printf( "%s\n", buf );
Paul Bakkera9507c02011-02-12 15:27:28 +0000163
Andres Amaya Garcia8fe4d912018-04-29 21:47:51 +0100164 exit_code = MBEDTLS_EXIT_SUCCESS;
165
Paul Bakkera9507c02011-02-12 15:27:28 +0000166exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_x509_crl_free( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000168
Paul Bakkercce9d772011-11-18 14:26:47 +0000169#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkera9507c02011-02-12 15:27:28 +0000171 fflush( stdout ); getchar();
172#endif
173
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200174 mbedtls_exit( exit_code );
Paul Bakkera9507c02011-02-12 15:27:28 +0000175}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CRL_PARSE_C &&
177 MBEDTLS_FS_IO */