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 | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later OR GPL-2.0-or-later |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
| 12 | * |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 13 | * ********** |
| 14 | * |
| 15 | * ********** |
| 16 | * GNU General Public License v2.0 or later: |
| 17 | * |
| 18 | * This program is free software; you can redistribute it and/or modify |
| 19 | * it under the terms of the GNU General Public License as published by |
| 20 | * the Free Software Foundation; either version 2 of the License, or |
| 21 | * (at your option) any later version. |
| 22 | * |
| 23 | * This program is distributed in the hope that it will be useful, |
| 24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 | * GNU General Public License for more details. |
| 27 | * |
| 28 | * You should have received a copy of the GNU General Public License along |
| 29 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 30 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 31 | * |
| 32 | * ********** |
| 33 | */ |
| 34 | |
| 35 | #include "mbedtls/build_info.h" |
| 36 | |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 37 | #include "mbedtls/platform.h" |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 38 | |
| 39 | #if !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \ |
| 40 | !defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 41 | int main(void) |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 42 | { |
| 43 | 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] | 44 | "MBEDTLS_TIMING_C not defined.\n"); |
| 45 | mbedtls_exit(0); |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 46 | } |
| 47 | #else |
| 48 | |
| 49 | #include "mbedtls/error.h" |
| 50 | #include "mbedtls/timing.h" |
| 51 | #include "mbedtls/x509_crt.h" |
| 52 | |
| 53 | #include <stdio.h> |
| 54 | #include <stdlib.h> |
| 55 | #include <string.h> |
| 56 | |
| 57 | #define DFL_ITERATIONS 1 |
| 58 | #define DFL_PRIME_CACHE 1 |
| 59 | |
| 60 | #define USAGE \ |
Gilles Peskine | 618a70e | 2021-07-30 13:00:10 +0200 | [diff] [blame] | 61 | "\n usage: load_roots param=<>... [--] FILE...\n" \ |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 62 | "\n acceptable parameters:\n" \ |
| 63 | " iterations=%%d Iteration count (not including cache priming); default: 1\n" \ |
| 64 | " prime=%%d Prime the disk read cache? Default: 1 (yes)\n" \ |
| 65 | "\n" |
| 66 | |
| 67 | |
| 68 | /* |
| 69 | * global options |
| 70 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | struct options { |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 72 | const char **filenames; /* NULL-terminated list of file names */ |
| 73 | unsigned iterations; /* Number of iterations to time */ |
| 74 | int prime_cache; /* Prime the disk read cache? */ |
| 75 | } opt; |
| 76 | |
| 77 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | int read_certificates(const char *const *filenames) |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 79 | { |
| 80 | mbedtls_x509_crt cas; |
| 81 | int ret = 0; |
| 82 | const char *const *cur; |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 83 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 84 | mbedtls_x509_crt_init(&cas); |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 85 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | for (cur = filenames; *cur != NULL; cur++) { |
| 87 | ret = mbedtls_x509_crt_parse_file(&cas, *cur); |
| 88 | if (ret != 0) { |
Gilles Peskine | 680747b | 2021-08-06 14:37:01 +0200 | [diff] [blame] | 89 | #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY) |
| 90 | char error_message[200]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 91 | mbedtls_strerror(ret, error_message, sizeof(error_message)); |
| 92 | printf("\n%s: -0x%04x (%s)\n", |
| 93 | *cur, (unsigned) -ret, error_message); |
Gilles Peskine | 680747b | 2021-08-06 14:37:01 +0200 | [diff] [blame] | 94 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | printf("\n%s: -0x%04x\n", |
| 96 | *cur, (unsigned) -ret); |
Gilles Peskine | 680747b | 2021-08-06 14:37:01 +0200 | [diff] [blame] | 97 | #endif |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 98 | goto exit; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 103 | mbedtls_x509_crt_free(&cas); |
| 104 | return ret == 0; |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | int main(int argc, char *argv[]) |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 108 | { |
| 109 | int exit_code = MBEDTLS_EXIT_FAILURE; |
| 110 | unsigned i, j; |
| 111 | struct mbedtls_timing_hr_time timer; |
| 112 | unsigned long ms; |
| 113 | |
Przemek Stekiel | 89c636e | 2023-04-14 09:26:39 +0200 | [diff] [blame] | 114 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 115 | psa_status_t status = psa_crypto_init(); |
| 116 | if (status != PSA_SUCCESS) { |
| 117 | mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n", |
| 118 | (int) status); |
| 119 | goto exit; |
| 120 | } |
| 121 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 122 | |
Przemek Stekiel | a8c560a | 2023-04-19 10:15:26 +0200 | [diff] [blame] | 123 | if (argc <= 1) { |
| 124 | mbedtls_printf(USAGE); |
| 125 | goto exit; |
| 126 | } |
| 127 | |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 128 | opt.filenames = NULL; |
| 129 | opt.iterations = DFL_ITERATIONS; |
| 130 | opt.prime_cache = DFL_PRIME_CACHE; |
| 131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 132 | for (i = 1; i < (unsigned) argc; i++) { |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 133 | char *p = argv[i]; |
| 134 | char *q = NULL; |
| 135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 136 | if (strcmp(p, "--") == 0) { |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 137 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 138 | } |
| 139 | if ((q = strchr(p, '=')) == NULL) { |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 140 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 141 | } |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 142 | *q++ = '\0'; |
| 143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | for (j = 0; p + j < q; j++) { |
| 145 | if (argv[i][j] >= 'A' && argv[i][j] <= 'Z') { |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 146 | argv[i][j] |= 0x20; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 147 | } |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | if (strcmp(p, "iterations") == 0) { |
| 151 | opt.iterations = atoi(q); |
| 152 | } else if (strcmp(p, "prime") == 0) { |
| 153 | opt.iterations = atoi(q) != 0; |
| 154 | } else { |
| 155 | mbedtls_printf("Unknown option: %s\n", p); |
| 156 | mbedtls_printf(USAGE); |
Gilles Peskine | 9a2114c | 2021-07-30 13:01:52 +0200 | [diff] [blame] | 157 | goto exit; |
| 158 | } |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 161 | opt.filenames = (const char **) argv + i; |
| 162 | if (*opt.filenames == 0) { |
| 163 | mbedtls_printf("Missing list of certificate files to parse\n"); |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 164 | goto exit; |
| 165 | } |
| 166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 167 | mbedtls_printf("Parsing %u certificates", argc - i); |
| 168 | if (opt.prime_cache) { |
| 169 | if (!read_certificates(opt.filenames)) { |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 170 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 171 | } |
| 172 | mbedtls_printf(" "); |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 175 | (void) mbedtls_timing_get_timer(&timer, 1); |
| 176 | for (i = 1; i <= opt.iterations; i++) { |
| 177 | if (!read_certificates(opt.filenames)) { |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 178 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 179 | } |
| 180 | mbedtls_printf("."); |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 181 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 182 | ms = mbedtls_timing_get_timer(&timer, 0); |
| 183 | mbedtls_printf("\n%u iterations -> %lu ms\n", opt.iterations, ms); |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 184 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 185 | |
| 186 | exit: |
Przemek Stekiel | 758aef6 | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 187 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemek Stekiel | a8c560a | 2023-04-19 10:15:26 +0200 | [diff] [blame] | 188 | mbedtls_psa_crypto_free(); |
Przemek Stekiel | 758aef6 | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 189 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 190 | mbedtls_exit(exit_code); |
Gilles Peskine | 2c1442e | 2021-07-26 20:20:54 +0200 | [diff] [blame] | 191 | } |
| 192 | #endif /* necessary configuration */ |