blob: a95157067ee799452b1924a56c90ea48254ba858 [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
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkera9507c02011-02-12 15:27:28 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakkera9507c02011-02-12 15:27:28 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Andres Amaya Garcia898b2082018-04-29 21:47:51 +010032#include <stdlib.h>
33#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010034#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010035#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia898b2082018-04-29 21:47:51 +010036#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
37#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
40 !defined(MBEDTLS_X509_CRL_PARSE_C) || !defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020041int main( void )
42{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
44 "MBEDTLS_X509_CRL_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020045 return( 0 );
46}
47#else
48
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/x509_crl.h"
Paul Bakkera9507c02011-02-12 15:27:28 +000050
Rich Evans18b78c72015-02-11 14:06:19 +000051#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000054
55#define DFL_FILENAME "crl.pem"
56#define DFL_DEBUG_LEVEL 0
57
58#define USAGE \
59 "\n usage: crl_app param=<>...\n" \
60 "\n acceptable parameters:\n" \
61 " filename=%%s default: crl.pem\n" \
62 "\n"
63
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010064#if defined(MBEDTLS_CHECK_PARAMS)
65#define mbedtls_exit exit
66void mbedtls_param_failed( const char *failure_condition,
67 const char *file,
68 int line )
Simon Butcher63cb97e2018-12-06 17:43:31 +000069{
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010070 mbedtls_printf( "%s:%i: Input param failed - %s\n",
71 file, line, failure_condition );
Simon Butcher63cb97e2018-12-06 17:43:31 +000072 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
73}
74#endif
75
Paul Bakkera9507c02011-02-12 15:27:28 +000076/*
77 * global options
78 */
79struct options
80{
Paul Bakkeref3f8c72013-06-24 13:01:08 +020081 const char *filename; /* filename of the certificate file */
Paul Bakkera9507c02011-02-12 15:27:28 +000082} opt;
83
Paul Bakkera9507c02011-02-12 15:27:28 +000084int main( int argc, char *argv[] )
85{
Andres Amaya Garcia898b2082018-04-29 21:47:51 +010086 int ret = 1;
87 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerb8ba90b2011-12-05 14:34:12 +000088 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_x509_crl crl;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010090 int i;
Paul Bakkera9507c02011-02-12 15:27:28 +000091 char *p, *q;
92
93 /*
94 * Set to sane values
95 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_x509_crl_init( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +000097
98 if( argc == 0 )
99 {
100 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_printf( USAGE );
Paul Bakkera9507c02011-02-12 15:27:28 +0000102 goto exit;
103 }
104
105 opt.filename = DFL_FILENAME;
Paul Bakkera9507c02011-02-12 15:27:28 +0000106
107 for( i = 1; i < argc; i++ )
108 {
Paul Bakkera9507c02011-02-12 15:27:28 +0000109 p = argv[i];
110 if( ( q = strchr( p, '=' ) ) == NULL )
111 goto usage;
112 *q++ = '\0';
113
114 if( strcmp( p, "filename" ) == 0 )
115 opt.filename = q;
Paul Bakkera9507c02011-02-12 15:27:28 +0000116 else
117 goto usage;
118 }
119
120 /*
121 * 1.1. Load the CRL
122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_printf( "\n . Loading the CRL ..." );
Paul Bakkera9507c02011-02-12 15:27:28 +0000124 fflush( stdout );
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 ret = mbedtls_x509_crl_parse_file( &crl, opt.filename );
Paul Bakkera9507c02011-02-12 15:27:28 +0000127
128 if( ret != 0 )
129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_printf( " failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret );
131 mbedtls_x509_crl_free( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000132 goto exit;
133 }
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_printf( " ok\n" );
Paul Bakkera9507c02011-02-12 15:27:28 +0000136
137 /*
138 * 1.2 Print the CRL
139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_printf( " . CRL information ...\n" );
141 ret = mbedtls_x509_crl_info( (char *) buf, sizeof( buf ) - 1, " ", &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000142 if( ret == -1 )
143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_printf( " failed\n ! mbedtls_x509_crl_info 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( "%s\n", buf );
Paul Bakkera9507c02011-02-12 15:27:28 +0000150
Andres Amaya Garcia898b2082018-04-29 21:47:51 +0100151 exit_code = MBEDTLS_EXIT_SUCCESS;
152
Paul Bakkera9507c02011-02-12 15:27:28 +0000153exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_x509_crl_free( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000155
Paul Bakkercce9d772011-11-18 14:26:47 +0000156#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkera9507c02011-02-12 15:27:28 +0000158 fflush( stdout ); getchar();
159#endif
160
Andres Amaya Garcia898b2082018-04-29 21:47:51 +0100161 return( exit_code );
Paul Bakkera9507c02011-02-12 15:27:28 +0000162}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CRL_PARSE_C &&
164 MBEDTLS_FS_IO */