blob: 2720b1d8efec99d3aec0aabdefad5491aa5737ef [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
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakkera9507c02011-02-12 15:27:28 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000024#else
Rich Evans18b78c72015-02-11 14:06:19 +000025#include <stdio.h>
Andres Amaya Garcia898b2082018-04-29 21:47:51 +010026#include <stdlib.h>
27#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010028#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010029#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia898b2082018-04-29 21:47:51 +010030#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
31#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
Peter Kolbus9a969b62018-12-11 13:55:56 -060034 !defined(MBEDTLS_X509_CRL_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
Chris Jonesee33c602020-12-16 11:52:37 +000035 defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020036int main( void )
37{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Chris Jonesee33c602020-12-16 11:52:37 +000039 "MBEDTLS_X509_CRL_PARSE_C and/or MBEDTLS_FS_IO not defined and/or "
40 "MBEDTLS_X509_REMOVE_INFO defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020041 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020042}
43#else
44
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/x509_crl.h"
Paul Bakkera9507c02011-02-12 15:27:28 +000046
Rich Evans18b78c72015-02-11 14:06:19 +000047#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000050
51#define DFL_FILENAME "crl.pem"
52#define DFL_DEBUG_LEVEL 0
53
54#define USAGE \
55 "\n usage: crl_app param=<>...\n" \
56 "\n acceptable parameters:\n" \
57 " filename=%%s default: crl.pem\n" \
58 "\n"
59
Simon Butcher63cb97e2018-12-06 17:43:31 +000060
Paul Bakkera9507c02011-02-12 15:27:28 +000061/*
62 * global options
63 */
64struct options
65{
Paul Bakkeref3f8c72013-06-24 13:01:08 +020066 const char *filename; /* filename of the certificate file */
Paul Bakkera9507c02011-02-12 15:27:28 +000067} opt;
68
Paul Bakkera9507c02011-02-12 15:27:28 +000069int main( int argc, char *argv[] )
70{
Andres Amaya Garcia898b2082018-04-29 21:47:51 +010071 int ret = 1;
72 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerb8ba90b2011-12-05 14:34:12 +000073 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_x509_crl crl;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010075 int i;
Paul Bakkera9507c02011-02-12 15:27:28 +000076 char *p, *q;
77
78 /*
79 * Set to sane values
80 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 mbedtls_x509_crl_init( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +000082
83 if( argc == 0 )
84 {
85 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 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
92 for( i = 1; i < argc; i++ )
93 {
Paul Bakkera9507c02011-02-12 15:27:28 +000094 p = argv[i];
95 if( ( q = strchr( p, '=' ) ) == NULL )
96 goto usage;
97 *q++ = '\0';
98
99 if( strcmp( p, "filename" ) == 0 )
100 opt.filename = q;
Paul Bakkera9507c02011-02-12 15:27:28 +0000101 else
102 goto usage;
103 }
104
105 /*
106 * 1.1. Load the CRL
107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_printf( "\n . Loading the CRL ..." );
Paul Bakkera9507c02011-02-12 15:27:28 +0000109 fflush( stdout );
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 ret = mbedtls_x509_crl_parse_file( &crl, opt.filename );
Paul Bakkera9507c02011-02-12 15:27:28 +0000112
113 if( ret != 0 )
114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_printf( " ok\n" );
Paul Bakkera9507c02011-02-12 15:27:28 +0000121
122 /*
123 * 1.2 Print the CRL
124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_printf( " . CRL information ...\n" );
126 ret = mbedtls_x509_crl_info( (char *) buf, sizeof( buf ) - 1, " ", &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000127 if( ret == -1 )
128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_printf( " failed\n ! mbedtls_x509_crl_info returned %d\n\n", ret );
130 mbedtls_x509_crl_free( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000131 goto exit;
132 }
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_printf( "%s\n", buf );
Paul Bakkera9507c02011-02-12 15:27:28 +0000135
Andres Amaya Garcia898b2082018-04-29 21:47:51 +0100136 exit_code = MBEDTLS_EXIT_SUCCESS;
137
Paul Bakkera9507c02011-02-12 15:27:28 +0000138exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_x509_crl_free( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000140
Paul Bakkercce9d772011-11-18 14:26:47 +0000141#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkera9507c02011-02-12 15:27:28 +0000143 fflush( stdout ); getchar();
144#endif
145
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200146 mbedtls_exit( exit_code );
Paul Bakkera9507c02011-02-12 15:27:28 +0000147}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CRL_PARSE_C &&
149 MBEDTLS_FS_IO */