Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * CRL reading application |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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 Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 30 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
Andres Amaya Garcia | 898b208 | 2018-04-29 21:47:51 +0100 | [diff] [blame] | 32 | #include <stdlib.h> |
| 33 | #define mbedtls_printf printf |
Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 34 | #define mbedtls_exit exit |
Andres Amaya Garcia | 7d42965 | 2018-04-30 22:42:33 +0100 | [diff] [blame] | 35 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
Andres Amaya Garcia | 898b208 | 2018-04-29 21:47:51 +0100 | [diff] [blame] | 36 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 37 | #endif /* MBEDTLS_PLATFORM_C */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 38 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 39 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \ |
| 40 | !defined(MBEDTLS_X509_CRL_PARSE_C) || !defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 8d649c6 | 2015-03-31 15:10:03 +0200 | [diff] [blame] | 41 | int main( void ) |
| 42 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | 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é-Gonnard | 8d649c6 | 2015-03-31 15:10:03 +0200 | [diff] [blame] | 45 | return( 0 ); |
| 46 | } |
| 47 | #else |
| 48 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 49 | #include "mbedtls/x509_crl.h" |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 50 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 51 | #include <stdio.h> |
| 52 | #include <stdlib.h> |
| 53 | #include <string.h> |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 54 | |
| 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 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 64 | |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 65 | /* |
| 66 | * global options |
| 67 | */ |
| 68 | struct options |
| 69 | { |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 70 | const char *filename; /* filename of the certificate file */ |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 71 | } opt; |
| 72 | |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 73 | int main( int argc, char *argv[] ) |
| 74 | { |
Andres Amaya Garcia | 898b208 | 2018-04-29 21:47:51 +0100 | [diff] [blame] | 75 | int ret = 1; |
| 76 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Paul Bakker | b8ba90b | 2011-12-05 14:34:12 +0000 | [diff] [blame] | 77 | unsigned char buf[100000]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 78 | mbedtls_x509_crl crl; |
Paul Bakker | c97f9f6 | 2013-11-30 15:13:02 +0100 | [diff] [blame] | 79 | int i; |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 80 | char *p, *q; |
| 81 | |
| 82 | /* |
| 83 | * Set to sane values |
| 84 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 85 | mbedtls_x509_crl_init( &crl ); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 86 | |
| 87 | if( argc == 0 ) |
| 88 | { |
| 89 | usage: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | mbedtls_printf( USAGE ); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 91 | goto exit; |
| 92 | } |
| 93 | |
| 94 | opt.filename = DFL_FILENAME; |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 95 | |
| 96 | for( i = 1; i < argc; i++ ) |
| 97 | { |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 98 | p = argv[i]; |
| 99 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 100 | goto usage; |
| 101 | *q++ = '\0'; |
| 102 | |
| 103 | if( strcmp( p, "filename" ) == 0 ) |
| 104 | opt.filename = q; |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 105 | else |
| 106 | goto usage; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * 1.1. Load the CRL |
| 111 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | mbedtls_printf( "\n . Loading the CRL ..." ); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 113 | fflush( stdout ); |
| 114 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | ret = mbedtls_x509_crl_parse_file( &crl, opt.filename ); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 116 | |
| 117 | if( ret != 0 ) |
| 118 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 119 | mbedtls_printf( " failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret ); |
| 120 | mbedtls_x509_crl_free( &crl ); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 121 | goto exit; |
| 122 | } |
| 123 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | mbedtls_printf( " ok\n" ); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * 1.2 Print the CRL |
| 128 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | mbedtls_printf( " . CRL information ...\n" ); |
| 130 | ret = mbedtls_x509_crl_info( (char *) buf, sizeof( buf ) - 1, " ", &crl ); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 131 | if( ret == -1 ) |
| 132 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | mbedtls_printf( " failed\n ! mbedtls_x509_crl_info returned %d\n\n", ret ); |
| 134 | mbedtls_x509_crl_free( &crl ); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 135 | goto exit; |
| 136 | } |
| 137 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 138 | mbedtls_printf( "%s\n", buf ); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 139 | |
Andres Amaya Garcia | 898b208 | 2018-04-29 21:47:51 +0100 | [diff] [blame] | 140 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 141 | |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 142 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | mbedtls_x509_crl_free( &crl ); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 144 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 145 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 146 | mbedtls_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 147 | fflush( stdout ); getchar(); |
| 148 | #endif |
| 149 | |
Andres Amaya Garcia | 898b208 | 2018-04-29 21:47:51 +0100 | [diff] [blame] | 150 | return( exit_code ); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 151 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CRL_PARSE_C && |
| 153 | MBEDTLS_FS_IO */ |