blob: 3509d8c8b6a86203f43140b2433542ff028b042a [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) || \
Peter Kolbusdc470ae2018-12-11 13:55:56 -060040 !defined(MBEDTLS_X509_CRL_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
Hanno Becker02a21932019-06-10 15:08:43 +010041 defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020042int main( void )
43{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Peter Kolbusdc470ae2018-12-11 13:55:56 -060045 "MBEDTLS_X509_CRL_PARSE_C and/or MBEDTLS_FS_IO and/or "
Hanno Becker02a21932019-06-10 15:08:43 +010046 "MBEDTLS_X509_REMOVE_INFO not defined.\n");
Andrzej Kurek825ebd42020-05-18 11:47:25 -040047 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020048}
49#else
50
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/x509_crl.h"
Paul Bakkera9507c02011-02-12 15:27:28 +000052
Rich Evans18b78c72015-02-11 14:06:19 +000053#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000056
57#define DFL_FILENAME "crl.pem"
58#define DFL_DEBUG_LEVEL 0
59
60#define USAGE \
61 "\n usage: crl_app param=<>...\n" \
62 "\n acceptable parameters:\n" \
63 " filename=%%s default: crl.pem\n" \
64 "\n"
65
Simon Butcher63cb97e2018-12-06 17:43:31 +000066
Paul Bakkera9507c02011-02-12 15:27:28 +000067/*
68 * global options
69 */
70struct options
71{
Paul Bakkeref3f8c72013-06-24 13:01:08 +020072 const char *filename; /* filename of the certificate file */
Paul Bakkera9507c02011-02-12 15:27:28 +000073} opt;
74
Jarno Lamsa642596e2019-10-04 12:52:42 +030075#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
76int mbedtls_hardware_poll( void *data, unsigned char *output,
77 size_t len, size_t *olen )
78{
79 size_t i;
80 (void) data;
81 for( i = 0; i < len; ++i )
82 output[i] = rand();
83 *olen = len;
84 return( 0 );
85}
86#endif
87
Paul Bakkera9507c02011-02-12 15:27:28 +000088int main( int argc, char *argv[] )
89{
Andres Amaya Garcia898b2082018-04-29 21:47:51 +010090 int ret = 1;
91 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerb8ba90b2011-12-05 14:34:12 +000092 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 mbedtls_x509_crl crl;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010094 int i;
Paul Bakkera9507c02011-02-12 15:27:28 +000095 char *p, *q;
96
97 /*
98 * Set to sane values
99 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_x509_crl_init( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000101
102 if( argc == 0 )
103 {
104 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_printf( USAGE );
Paul Bakkera9507c02011-02-12 15:27:28 +0000106 goto exit;
107 }
108
109 opt.filename = DFL_FILENAME;
Paul Bakkera9507c02011-02-12 15:27:28 +0000110
111 for( i = 1; i < argc; i++ )
112 {
Paul Bakkera9507c02011-02-12 15:27:28 +0000113 p = argv[i];
114 if( ( q = strchr( p, '=' ) ) == NULL )
115 goto usage;
116 *q++ = '\0';
117
118 if( strcmp( p, "filename" ) == 0 )
119 opt.filename = q;
Paul Bakkera9507c02011-02-12 15:27:28 +0000120 else
121 goto usage;
122 }
123
124 /*
125 * 1.1. Load the CRL
126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_printf( "\n . Loading the CRL ..." );
Paul Bakkera9507c02011-02-12 15:27:28 +0000128 fflush( stdout );
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 ret = mbedtls_x509_crl_parse_file( &crl, opt.filename );
Paul Bakkera9507c02011-02-12 15:27:28 +0000131
132 if( ret != 0 )
133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_printf( " failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret );
135 mbedtls_x509_crl_free( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000136 goto exit;
137 }
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_printf( " ok\n" );
Paul Bakkera9507c02011-02-12 15:27:28 +0000140
141 /*
142 * 1.2 Print the CRL
143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_printf( " . CRL information ...\n" );
145 ret = mbedtls_x509_crl_info( (char *) buf, sizeof( buf ) - 1, " ", &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000146 if( ret == -1 )
147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_printf( " failed\n ! mbedtls_x509_crl_info returned %d\n\n", ret );
149 mbedtls_x509_crl_free( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000150 goto exit;
151 }
152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_printf( "%s\n", buf );
Paul Bakkera9507c02011-02-12 15:27:28 +0000154
Andres Amaya Garcia898b2082018-04-29 21:47:51 +0100155 exit_code = MBEDTLS_EXIT_SUCCESS;
156
Paul Bakkera9507c02011-02-12 15:27:28 +0000157exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_x509_crl_free( &crl );
Paul Bakkera9507c02011-02-12 15:27:28 +0000159
Paul Bakkercce9d772011-11-18 14:26:47 +0000160#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkera9507c02011-02-12 15:27:28 +0000162 fflush( stdout ); getchar();
163#endif
164
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400165 mbedtls_exit( exit_code );
Paul Bakkera9507c02011-02-12 15:27:28 +0000166}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CRL_PARSE_C &&
168 MBEDTLS_FS_IO */