blob: 580c46c3caa72012f9b9ace679bdb7ad3d189f78 [file] [log] [blame]
Paul Bakkera9507c02011-02-12 15:27:28 +00001/*
2 * CRL reading application
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkera9507c02011-02-12 15:27:28 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakkera9507c02011-02-12 15:27:28 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000057#else
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
Andres Amaya Garcia898b2082018-04-29 21:47:51 +010059#include <stdlib.h>
60#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010061#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010062#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia898b2082018-04-29 21:47:51 +010063#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
64#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
67 !defined(MBEDTLS_X509_CRL_PARSE_C) || !defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020068int main( void )
69{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
71 "MBEDTLS_X509_CRL_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020072 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020073}
74#else
75
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000076#include "mbedtls/x509_crl.h"
Paul Bakkera9507c02011-02-12 15:27:28 +000077
Rich Evans18b78c72015-02-11 14:06:19 +000078#include <stdio.h>
79#include <stdlib.h>
80#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000081
82#define DFL_FILENAME "crl.pem"
83#define DFL_DEBUG_LEVEL 0
84
85#define USAGE \
86 "\n usage: crl_app param=<>...\n" \
87 "\n acceptable parameters:\n" \
88 " filename=%%s default: crl.pem\n" \
89 "\n"
90
Simon Butcher63cb97e2018-12-06 17:43:31 +000091
Paul Bakkera9507c02011-02-12 15:27:28 +000092/*
93 * global options
94 */
95struct options
96{
Paul Bakkeref3f8c72013-06-24 13:01:08 +020097 const char *filename; /* filename of the certificate file */
Paul Bakkera9507c02011-02-12 15:27:28 +000098} opt;
99
Paul Bakkera9507c02011-02-12 15:27:28 +0000100int main( int argc, char *argv[] )
101{
Andres Amaya Garcia898b2082018-04-29 21:47:51 +0100102 int ret = 1;
103 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerb8ba90b2011-12-05 14:34:12 +0000104 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_x509_crl crl;
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100106 int i;
Paul Bakkera9507c02011-02-12 15:27:28 +0000107 char *p, *q;
108
109 /*
110 * Set to sane values
111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_x509_crl_init( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000113
114 if( argc == 0 )
115 {
116 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_printf( USAGE );
Paul Bakkera9507c02011-02-12 15:27:28 +0000118 goto exit;
119 }
120
121 opt.filename = DFL_FILENAME;
Paul Bakkera9507c02011-02-12 15:27:28 +0000122
123 for( i = 1; i < argc; i++ )
124 {
Paul Bakkera9507c02011-02-12 15:27:28 +0000125 p = argv[i];
126 if( ( q = strchr( p, '=' ) ) == NULL )
127 goto usage;
128 *q++ = '\0';
129
130 if( strcmp( p, "filename" ) == 0 )
131 opt.filename = q;
Paul Bakkera9507c02011-02-12 15:27:28 +0000132 else
133 goto usage;
134 }
135
136 /*
137 * 1.1. Load the CRL
138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_printf( "\n . Loading the CRL ..." );
Paul Bakkera9507c02011-02-12 15:27:28 +0000140 fflush( stdout );
141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 ret = mbedtls_x509_crl_parse_file( &crl, opt.filename );
Paul Bakkera9507c02011-02-12 15:27:28 +0000143
144 if( ret != 0 )
145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_printf( " failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret );
147 mbedtls_x509_crl_free( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000148 goto exit;
149 }
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( " ok\n" );
Paul Bakkera9507c02011-02-12 15:27:28 +0000152
153 /*
154 * 1.2 Print the CRL
155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_printf( " . CRL information ...\n" );
157 ret = mbedtls_x509_crl_info( (char *) buf, sizeof( buf ) - 1, " ", &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000158 if( ret == -1 )
159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_printf( " failed\n ! mbedtls_x509_crl_info returned %d\n\n", ret );
161 mbedtls_x509_crl_free( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000162 goto exit;
163 }
164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_printf( "%s\n", buf );
Paul Bakkera9507c02011-02-12 15:27:28 +0000166
Andres Amaya Garcia898b2082018-04-29 21:47:51 +0100167 exit_code = MBEDTLS_EXIT_SUCCESS;
168
Paul Bakkera9507c02011-02-12 15:27:28 +0000169exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_x509_crl_free( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000171
Paul Bakkercce9d772011-11-18 14:26:47 +0000172#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkera9507c02011-02-12 15:27:28 +0000174 fflush( stdout ); getchar();
175#endif
176
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200177 mbedtls_exit( exit_code );
Paul Bakkera9507c02011-02-12 15:27:28 +0000178}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CRL_PARSE_C &&
180 MBEDTLS_FS_IO */