Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Root CA reading application |
| 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | f8be5f6 | 2023-11-02 20:43:00 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "mbedtls/build_info.h" |
| 9 | |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 10 | #include "mbedtls/platform.h" |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 11 | |
| 12 | #if !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \ |
| 13 | !defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 14 | int main(void) |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 15 | { |
| 16 | mbedtls_printf("MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_FS_IO and/or " |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 17 | "MBEDTLS_TIMING_C not defined.\n"); |
| 18 | mbedtls_exit(0); |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 19 | } |
| 20 | #else |
| 21 | |
| 22 | #include "mbedtls/error.h" |
| 23 | #include "mbedtls/timing.h" |
| 24 | #include "mbedtls/x509_crt.h" |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #define DFL_ITERATIONS 1 |
| 31 | #define DFL_PRIME_CACHE 1 |
| 32 | |
| 33 | #define USAGE \ |
Gilles Peskine | 618a70e | 2021-07-30 13:00:10 +0200 | [diff] [blame] | 34 | "\n usage: load_roots param=<>... [--] FILE...\n" \ |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 35 | "\n acceptable parameters:\n" \ |
| 36 | " iterations=%%d Iteration count (not including cache priming); default: 1\n" \ |
| 37 | " prime=%%d Prime the disk read cache? Default: 1 (yes)\n" \ |
| 38 | "\n" |
| 39 | |
| 40 | |
| 41 | /* |
| 42 | * global options |
| 43 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 44 | struct options { |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 45 | const char **filenames; /* NULL-terminated list of file names */ |
| 46 | unsigned iterations; /* Number of iterations to time */ |
| 47 | int prime_cache; /* Prime the disk read cache? */ |
| 48 | } opt; |
| 49 | |
| 50 | |
Michael Schuster | 6fa32fd | 2024-06-01 21:15:02 +0200 | [diff] [blame] | 51 | static int read_certificates(const char *const *filenames) |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 52 | { |
| 53 | mbedtls_x509_crt cas; |
| 54 | int ret = 0; |
| 55 | const char *const *cur; |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 56 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 57 | mbedtls_x509_crt_init(&cas); |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 58 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 59 | for (cur = filenames; *cur != NULL; cur++) { |
| 60 | ret = mbedtls_x509_crt_parse_file(&cas, *cur); |
| 61 | if (ret != 0) { |
Gilles Peskine | 680747b | 2021-08-06 14:37:01 +0200 | [diff] [blame] | 62 | #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY) |
| 63 | char error_message[200]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | mbedtls_strerror(ret, error_message, sizeof(error_message)); |
| 65 | printf("\n%s: -0x%04x (%s)\n", |
| 66 | *cur, (unsigned) -ret, error_message); |
Gilles Peskine | 680747b | 2021-08-06 14:37:01 +0200 | [diff] [blame] | 67 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 68 | printf("\n%s: -0x%04x\n", |
| 69 | *cur, (unsigned) -ret); |
Gilles Peskine | 680747b | 2021-08-06 14:37:01 +0200 | [diff] [blame] | 70 | #endif |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 71 | goto exit; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | mbedtls_x509_crt_free(&cas); |
| 77 | return ret == 0; |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | int main(int argc, char *argv[]) |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 81 | { |
| 82 | int exit_code = MBEDTLS_EXIT_FAILURE; |
| 83 | unsigned i, j; |
| 84 | struct mbedtls_timing_hr_time timer; |
| 85 | unsigned long ms; |
| 86 | |
Przemek Stekiel | 89c636e | 2023-04-14 09:26:39 +0200 | [diff] [blame] | 87 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 88 | psa_status_t status = psa_crypto_init(); |
| 89 | if (status != PSA_SUCCESS) { |
| 90 | mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n", |
| 91 | (int) status); |
| 92 | goto exit; |
| 93 | } |
| 94 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 95 | |
Przemek Stekiel | a8c560a | 2023-04-19 10:15:26 +0200 | [diff] [blame] | 96 | if (argc <= 1) { |
| 97 | mbedtls_printf(USAGE); |
| 98 | goto exit; |
| 99 | } |
| 100 | |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 101 | opt.filenames = NULL; |
| 102 | opt.iterations = DFL_ITERATIONS; |
| 103 | opt.prime_cache = DFL_PRIME_CACHE; |
| 104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 105 | for (i = 1; i < (unsigned) argc; i++) { |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 106 | char *p = argv[i]; |
| 107 | char *q = NULL; |
| 108 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 109 | if (strcmp(p, "--") == 0) { |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 110 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 111 | } |
| 112 | if ((q = strchr(p, '=')) == NULL) { |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 113 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | } |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 115 | *q++ = '\0'; |
| 116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 117 | for (j = 0; p + j < q; j++) { |
| 118 | if (argv[i][j] >= 'A' && argv[i][j] <= 'Z') { |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 119 | argv[i][j] |= 0x20; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 120 | } |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 123 | if (strcmp(p, "iterations") == 0) { |
| 124 | opt.iterations = atoi(q); |
| 125 | } else if (strcmp(p, "prime") == 0) { |
| 126 | opt.iterations = atoi(q) != 0; |
| 127 | } else { |
| 128 | mbedtls_printf("Unknown option: %s\n", p); |
| 129 | mbedtls_printf(USAGE); |
Gilles Peskine | 9a2114c | 2021-07-30 13:01:52 +0200 | [diff] [blame] | 130 | goto exit; |
| 131 | } |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 134 | opt.filenames = (const char **) argv + i; |
| 135 | if (*opt.filenames == 0) { |
| 136 | mbedtls_printf("Missing list of certificate files to parse\n"); |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 137 | goto exit; |
| 138 | } |
| 139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 140 | mbedtls_printf("Parsing %u certificates", argc - i); |
| 141 | if (opt.prime_cache) { |
| 142 | if (!read_certificates(opt.filenames)) { |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 143 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | } |
| 145 | mbedtls_printf(" "); |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 148 | (void) mbedtls_timing_get_timer(&timer, 1); |
| 149 | for (i = 1; i <= opt.iterations; i++) { |
| 150 | if (!read_certificates(opt.filenames)) { |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 151 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | } |
| 153 | mbedtls_printf("."); |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 154 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | ms = mbedtls_timing_get_timer(&timer, 0); |
| 156 | mbedtls_printf("\n%u iterations -> %lu ms\n", opt.iterations, ms); |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 157 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 158 | |
| 159 | exit: |
Przemek Stekiel | 758aef6 | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 160 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemek Stekiel | a8c560a | 2023-04-19 10:15:26 +0200 | [diff] [blame] | 161 | mbedtls_psa_crypto_free(); |
Przemek Stekiel | 758aef6 | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 162 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 163 | mbedtls_exit(exit_code); |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 164 | } |
| 165 | #endif /* necessary configuration */ |