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