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