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