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 |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 8 | #include "mbedtls/build_info.h" |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 9 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 10 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 11 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \ |
Peter Kolbus | 9a969b6 | 2018-12-11 13:55:56 -0600 | [diff] [blame] | 13 | !defined(MBEDTLS_X509_CRL_PARSE_C) || !defined(MBEDTLS_FS_IO) || \ |
Chris Jones | ee33c60 | 2020-12-16 11:52:37 +0000 | [diff] [blame] | 14 | defined(MBEDTLS_X509_REMOVE_INFO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 15 | int main(void) |
Manuel Pégourié-Gonnard | 8d649c6 | 2015-03-31 15:10:03 +0200 | [diff] [blame] | 16 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 17 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or " |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 18 | "MBEDTLS_X509_CRL_PARSE_C and/or MBEDTLS_FS_IO not defined and/or " |
| 19 | "MBEDTLS_X509_REMOVE_INFO defined.\n"); |
| 20 | mbedtls_exit(0); |
Manuel Pégourié-Gonnard | 8d649c6 | 2015-03-31 15:10:03 +0200 | [diff] [blame] | 21 | } |
| 22 | #else |
| 23 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/x509_crl.h" |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 25 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 29 | |
| 30 | #define DFL_FILENAME "crl.pem" |
| 31 | #define DFL_DEBUG_LEVEL 0 |
| 32 | |
| 33 | #define USAGE \ |
| 34 | "\n usage: crl_app param=<>...\n" \ |
| 35 | "\n acceptable parameters:\n" \ |
| 36 | " filename=%%s default: crl.pem\n" \ |
| 37 | "\n" |
| 38 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 39 | |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 40 | /* |
| 41 | * global options |
| 42 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 43 | struct options { |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 44 | const char *filename; /* filename of the certificate file */ |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 45 | } opt; |
| 46 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 47 | int main(int argc, char *argv[]) |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 48 | { |
Andres Amaya Garcia | 898b208 | 2018-04-29 21:47:51 +0100 | [diff] [blame] | 49 | int ret = 1; |
| 50 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Paul Bakker | b8ba90b | 2011-12-05 14:34:12 +0000 | [diff] [blame] | 51 | unsigned char buf[100000]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | mbedtls_x509_crl crl; |
Paul Bakker | c97f9f6 | 2013-11-30 15:13:02 +0100 | [diff] [blame] | 53 | int i; |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 54 | char *p, *q; |
| 55 | |
Przemek Stekiel | a0a1c1e | 2023-04-17 11:10:05 +0200 | [diff] [blame] | 56 | /* |
| 57 | * Set to sane values |
| 58 | */ |
| 59 | mbedtls_x509_crl_init(&crl); |
| 60 | |
Przemek Stekiel | 89c636e | 2023-04-14 09:26:39 +0200 | [diff] [blame] | 61 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 62 | psa_status_t status = psa_crypto_init(); |
| 63 | if (status != PSA_SUCCESS) { |
| 64 | mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n", |
| 65 | (int) status); |
| 66 | goto exit; |
| 67 | } |
| 68 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 69 | |
Aditya Deshpande | 644a5c0 | 2023-01-30 15:58:50 +0000 | [diff] [blame] | 70 | if (argc < 2) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | usage: |
| 72 | mbedtls_printf(USAGE); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 73 | goto exit; |
| 74 | } |
| 75 | |
| 76 | opt.filename = DFL_FILENAME; |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 77 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | for (i = 1; i < argc; i++) { |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 79 | p = argv[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | if ((q = strchr(p, '=')) == NULL) { |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 81 | goto usage; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | } |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 83 | *q++ = '\0'; |
| 84 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | if (strcmp(p, "filename") == 0) { |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 86 | opt.filename = q; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | } else { |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 88 | goto usage; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 89 | } |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /* |
| 93 | * 1.1. Load the CRL |
| 94 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | mbedtls_printf("\n . Loading the CRL ..."); |
| 96 | fflush(stdout); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 97 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 98 | ret = mbedtls_x509_crl_parse_file(&crl, opt.filename); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 99 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | if (ret != 0) { |
| 101 | mbedtls_printf(" failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret); |
| 102 | mbedtls_x509_crl_free(&crl); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 103 | goto exit; |
| 104 | } |
| 105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 106 | mbedtls_printf(" ok\n"); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * 1.2 Print the CRL |
| 110 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 111 | mbedtls_printf(" . CRL information ...\n"); |
| 112 | ret = mbedtls_x509_crl_info((char *) buf, sizeof(buf) - 1, " ", &crl); |
| 113 | if (ret == -1) { |
| 114 | mbedtls_printf(" failed\n ! mbedtls_x509_crl_info returned %d\n\n", ret); |
| 115 | mbedtls_x509_crl_free(&crl); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 116 | goto exit; |
| 117 | } |
| 118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 119 | mbedtls_printf("%s\n", buf); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 120 | |
Andres Amaya Garcia | 898b208 | 2018-04-29 21:47:51 +0100 | [diff] [blame] | 121 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 122 | |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 123 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | mbedtls_x509_crl_free(&crl); |
Przemek Stekiel | 758aef6 | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 125 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemek Stekiel | a8c560a | 2023-04-19 10:15:26 +0200 | [diff] [blame] | 126 | mbedtls_psa_crypto_free(); |
Przemek Stekiel | 758aef6 | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 127 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 129 | mbedtls_exit(exit_code); |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 130 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 131 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CRL_PARSE_C && |
| 132 | MBEDTLS_FS_IO */ |