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