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