blob: e28f35a79d85232844e8eb768f186986286dc02d [file] [log] [blame]
Gilles Peskine82c04322021-07-26 20:20:54 +02001/*
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#if !defined(MBEDTLS_CONFIG_FILE)
48#include "mbedtls/config.h"
49#else
50#include MBEDTLS_CONFIG_FILE
51#endif
52
Gilles Peskine82c04322021-07-26 20:20:54 +020053#include "mbedtls/platform.h"
Gilles Peskine82c04322021-07-26 20:20:54 +020054
55#if !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
56 !defined(MBEDTLS_TIMING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057int main(void)
Gilles Peskine82c04322021-07-26 20:20:54 +020058{
59 mbedtls_printf("MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_FS_IO and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060 "MBEDTLS_TIMING_C not defined.\n");
61 mbedtls_exit(0);
Gilles Peskine82c04322021-07-26 20:20:54 +020062}
63#else
64
65#include "mbedtls/error.h"
66#include "mbedtls/timing.h"
67#include "mbedtls/x509_crt.h"
68
69#include <stdio.h>
70#include <stdlib.h>
71#include <string.h>
72
73#define DFL_ITERATIONS 1
74#define DFL_PRIME_CACHE 1
75
76#define USAGE \
Gilles Peskinedb928842021-07-30 13:00:10 +020077 "\n usage: load_roots param=<>... [--] FILE...\n" \
Gilles Peskine82c04322021-07-26 20:20:54 +020078 "\n acceptable parameters:\n" \
79 " iterations=%%d Iteration count (not including cache priming); default: 1\n" \
80 " prime=%%d Prime the disk read cache? Default: 1 (yes)\n" \
81 "\n"
82
83
84/*
85 * global options
86 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087struct options {
Gilles Peskine82c04322021-07-26 20:20:54 +020088 const char **filenames; /* NULL-terminated list of file names */
89 unsigned iterations; /* Number of iterations to time */
90 int prime_cache; /* Prime the disk read cache? */
91} opt;
92
93
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094int read_certificates(const char *const *filenames)
Gilles Peskine82c04322021-07-26 20:20:54 +020095{
96 mbedtls_x509_crt cas;
97 int ret = 0;
98 const char *const *cur;
Gilles Peskine82c04322021-07-26 20:20:54 +020099
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 mbedtls_x509_crt_init(&cas);
Gilles Peskine82c04322021-07-26 20:20:54 +0200101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102 for (cur = filenames; *cur != NULL; cur++) {
103 ret = mbedtls_x509_crt_parse_file(&cas, *cur);
104 if (ret != 0) {
Gilles Peskine3f0722c2021-08-06 14:37:01 +0200105#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
106 char error_message[200];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 mbedtls_strerror(ret, error_message, sizeof(error_message));
108 printf("\n%s: -0x%04x (%s)\n",
109 *cur, (unsigned) -ret, error_message);
Gilles Peskine3f0722c2021-08-06 14:37:01 +0200110#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100111 printf("\n%s: -0x%04x\n",
112 *cur, (unsigned) -ret);
Gilles Peskine3f0722c2021-08-06 14:37:01 +0200113#endif
Gilles Peskine82c04322021-07-26 20:20:54 +0200114 goto exit;
115 }
116 }
117
118exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 mbedtls_x509_crt_free(&cas);
120 return ret == 0;
Gilles Peskine82c04322021-07-26 20:20:54 +0200121}
122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123int main(int argc, char *argv[])
Gilles Peskine82c04322021-07-26 20:20:54 +0200124{
125 int exit_code = MBEDTLS_EXIT_FAILURE;
126 unsigned i, j;
127 struct mbedtls_timing_hr_time timer;
128 unsigned long ms;
129
Przemek Stekield381d2d2023-04-14 09:26:39 +0200130#if defined(MBEDTLS_USE_PSA_CRYPTO)
131 psa_status_t status = psa_crypto_init();
132 if (status != PSA_SUCCESS) {
133 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
134 (int) status);
135 goto exit;
136 }
137#endif /* MBEDTLS_USE_PSA_CRYPTO */
138
Przemek Stekielc4ddf922023-04-19 10:15:26 +0200139 if (argc <= 1) {
140 mbedtls_printf(USAGE);
141 goto exit;
142 }
143
Gilles Peskine82c04322021-07-26 20:20:54 +0200144 opt.filenames = NULL;
145 opt.iterations = DFL_ITERATIONS;
146 opt.prime_cache = DFL_PRIME_CACHE;
147
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 for (i = 1; i < (unsigned) argc; i++) {
Gilles Peskine82c04322021-07-26 20:20:54 +0200149 char *p = argv[i];
150 char *q = NULL;
151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152 if (strcmp(p, "--") == 0) {
Gilles Peskine82c04322021-07-26 20:20:54 +0200153 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154 }
155 if ((q = strchr(p, '=')) == NULL) {
Gilles Peskine82c04322021-07-26 20:20:54 +0200156 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157 }
Gilles Peskine82c04322021-07-26 20:20:54 +0200158 *q++ = '\0';
159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 for (j = 0; p + j < q; j++) {
161 if (argv[i][j] >= 'A' && argv[i][j] <= 'Z') {
Gilles Peskine82c04322021-07-26 20:20:54 +0200162 argv[i][j] |= 0x20;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163 }
Gilles Peskine82c04322021-07-26 20:20:54 +0200164 }
165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166 if (strcmp(p, "iterations") == 0) {
167 opt.iterations = atoi(q);
168 } else if (strcmp(p, "prime") == 0) {
169 opt.iterations = atoi(q) != 0;
170 } else {
171 mbedtls_printf("Unknown option: %s\n", p);
172 mbedtls_printf(USAGE);
Gilles Peskine38d41b92021-07-30 13:01:52 +0200173 goto exit;
174 }
Gilles Peskine82c04322021-07-26 20:20:54 +0200175 }
176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177 opt.filenames = (const char **) argv + i;
178 if (*opt.filenames == 0) {
179 mbedtls_printf("Missing list of certificate files to parse\n");
Gilles Peskine82c04322021-07-26 20:20:54 +0200180 goto exit;
181 }
182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100183 mbedtls_printf("Parsing %u certificates", argc - i);
184 if (opt.prime_cache) {
185 if (!read_certificates(opt.filenames)) {
Gilles Peskine82c04322021-07-26 20:20:54 +0200186 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 }
188 mbedtls_printf(" ");
Gilles Peskine82c04322021-07-26 20:20:54 +0200189 }
190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 (void) mbedtls_timing_get_timer(&timer, 1);
192 for (i = 1; i <= opt.iterations; i++) {
193 if (!read_certificates(opt.filenames)) {
Gilles Peskine82c04322021-07-26 20:20:54 +0200194 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195 }
196 mbedtls_printf(".");
Gilles Peskine82c04322021-07-26 20:20:54 +0200197 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 ms = mbedtls_timing_get_timer(&timer, 0);
199 mbedtls_printf("\n%u iterations -> %lu ms\n", opt.iterations, ms);
Gilles Peskine82c04322021-07-26 20:20:54 +0200200 exit_code = MBEDTLS_EXIT_SUCCESS;
201
202exit:
Przemek Stekield4d049b2023-04-19 13:47:43 +0200203#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielc4ddf922023-04-19 10:15:26 +0200204 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200205#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 mbedtls_exit(exit_code);
Gilles Peskine82c04322021-07-26 20:20:54 +0200207}
208#endif /* necessary configuration */