blob: a975405ea2eb74841df90300d67b3eaea9355ffc [file] [log] [blame]
Gilles Peskine2c1442e2021-07-26 20:20:54 +02001/*
2 * Root CA reading application
3 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later OR GPL-2.0-or-later
Gilles Peskine2c1442e2021-07-26 20:20:54 +02006 *
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 Peskine2c1442e2021-07-26 20:20:54 +020013 * **********
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 Peskine2c1442e2021-07-26 20:20:54 +020037#include "mbedtls/platform.h"
Gilles Peskine2c1442e2021-07-26 20:20:54 +020038
39#if !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
40 !defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010041int main(void)
Gilles Peskine2c1442e2021-07-26 20:20:54 +020042{
43 mbedtls_printf("MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_FS_IO and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010044 "MBEDTLS_TIMING_C not defined.\n");
45 mbedtls_exit(0);
Gilles Peskine2c1442e2021-07-26 20:20:54 +020046}
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 Peskine618a70e2021-07-30 13:00:10 +020061 "\n usage: load_roots param=<>... [--] FILE...\n" \
Gilles Peskine2c1442e2021-07-26 20:20:54 +020062 "\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 Peskine449bd832023-01-11 14:50:10 +010071struct options {
Gilles Peskine2c1442e2021-07-26 20:20:54 +020072 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 Peskine449bd832023-01-11 14:50:10 +010078int read_certificates(const char *const *filenames)
Gilles Peskine2c1442e2021-07-26 20:20:54 +020079{
80 mbedtls_x509_crt cas;
81 int ret = 0;
82 const char *const *cur;
Gilles Peskine2c1442e2021-07-26 20:20:54 +020083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 mbedtls_x509_crt_init(&cas);
Gilles Peskine2c1442e2021-07-26 20:20:54 +020085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 for (cur = filenames; *cur != NULL; cur++) {
87 ret = mbedtls_x509_crt_parse_file(&cas, *cur);
88 if (ret != 0) {
Gilles Peskine680747b2021-08-06 14:37:01 +020089#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
90 char error_message[200];
Gilles Peskine449bd832023-01-11 14:50:10 +010091 mbedtls_strerror(ret, error_message, sizeof(error_message));
92 printf("\n%s: -0x%04x (%s)\n",
93 *cur, (unsigned) -ret, error_message);
Gilles Peskine680747b2021-08-06 14:37:01 +020094#else
Gilles Peskine449bd832023-01-11 14:50:10 +010095 printf("\n%s: -0x%04x\n",
96 *cur, (unsigned) -ret);
Gilles Peskine680747b2021-08-06 14:37:01 +020097#endif
Gilles Peskine2c1442e2021-07-26 20:20:54 +020098 goto exit;
99 }
100 }
101
102exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 mbedtls_x509_crt_free(&cas);
104 return ret == 0;
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200105}
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107int main(int argc, char *argv[])
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200108{
109 int exit_code = MBEDTLS_EXIT_FAILURE;
110 unsigned i, j;
111 struct mbedtls_timing_hr_time timer;
112 unsigned long ms;
113
Przemek Stekiel89c636e2023-04-14 09:26:39 +0200114#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 Stekiela8c560a2023-04-19 10:15:26 +0200123 if (argc <= 1) {
124 mbedtls_printf(USAGE);
125 goto exit;
126 }
127
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200128 opt.filenames = NULL;
129 opt.iterations = DFL_ITERATIONS;
130 opt.prime_cache = DFL_PRIME_CACHE;
131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 for (i = 1; i < (unsigned) argc; i++) {
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200133 char *p = argv[i];
134 char *q = NULL;
135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 if (strcmp(p, "--") == 0) {
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200137 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 }
139 if ((q = strchr(p, '=')) == NULL) {
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200140 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 }
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200142 *q++ = '\0';
143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 for (j = 0; p + j < q; j++) {
145 if (argv[i][j] >= 'A' && argv[i][j] <= 'Z') {
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200146 argv[i][j] |= 0x20;
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 }
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200148 }
149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 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 Peskine9a2114c2021-07-30 13:01:52 +0200157 goto exit;
158 }
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200159 }
160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 opt.filenames = (const char **) argv + i;
162 if (*opt.filenames == 0) {
163 mbedtls_printf("Missing list of certificate files to parse\n");
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200164 goto exit;
165 }
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 mbedtls_printf("Parsing %u certificates", argc - i);
168 if (opt.prime_cache) {
169 if (!read_certificates(opt.filenames)) {
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200170 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 }
172 mbedtls_printf(" ");
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200173 }
174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 (void) mbedtls_timing_get_timer(&timer, 1);
176 for (i = 1; i <= opt.iterations; i++) {
177 if (!read_certificates(opt.filenames)) {
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200178 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 }
180 mbedtls_printf(".");
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200181 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 ms = mbedtls_timing_get_timer(&timer, 0);
183 mbedtls_printf("\n%u iterations -> %lu ms\n", opt.iterations, ms);
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200184 exit_code = MBEDTLS_EXIT_SUCCESS;
185
186exit:
Przemek Stekiel758aef62023-04-19 13:47:43 +0200187#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiela8c560a2023-04-19 10:15:26 +0200188 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200189#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 mbedtls_exit(exit_code);
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200191}
192#endif /* necessary configuration */