blob: 8570a0e62a51afbc676a829bf28ef3f3c533dfd5 [file] [log] [blame]
Gilles Peskine2c1442e2021-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#include "mbedtls/build_info.h"
48
49#if defined(MBEDTLS_PLATFORM_C)
50#include "mbedtls/platform.h"
51#else
52#include <stdio.h>
53#include <stdlib.h>
54#define mbedtls_time time
55#define mbedtls_time_t time_t
56#define mbedtls_fprintf fprintf
57#define mbedtls_printf printf
58#define mbedtls_exit exit
59#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
60#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
61#endif /* MBEDTLS_PLATFORM_C */
62
63#if !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
64 !defined(MBEDTLS_TIMING_C)
65int main( void )
66{
67 mbedtls_printf("MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_FS_IO and/or "
68 "MBEDTLS_TIMING_C not defined.\n");
69 mbedtls_exit( 0 );
70}
71#else
72
73#include "mbedtls/error.h"
74#include "mbedtls/timing.h"
75#include "mbedtls/x509_crt.h"
76
77#include <stdio.h>
78#include <stdlib.h>
79#include <string.h>
80
81#define DFL_ITERATIONS 1
82#define DFL_PRIME_CACHE 1
83
84#define USAGE \
Gilles Peskine618a70e2021-07-30 13:00:10 +020085 "\n usage: load_roots param=<>... [--] FILE...\n" \
Gilles Peskine2c1442e2021-07-26 20:20:54 +020086 "\n acceptable parameters:\n" \
87 " iterations=%%d Iteration count (not including cache priming); default: 1\n" \
88 " prime=%%d Prime the disk read cache? Default: 1 (yes)\n" \
89 "\n"
90
91
92/*
93 * global options
94 */
95struct options
96{
97 const char **filenames; /* NULL-terminated list of file names */
98 unsigned iterations; /* Number of iterations to time */
99 int prime_cache; /* Prime the disk read cache? */
100} opt;
101
102
103int read_certificates( const char *const *filenames )
104{
105 mbedtls_x509_crt cas;
106 int ret = 0;
107 const char *const *cur;
108 char error_message[200];
109
110 mbedtls_x509_crt_init( &cas );
111
112 for( cur = filenames; *cur != NULL; cur++ )
113 {
114 ret = mbedtls_x509_crt_parse_file( &cas, *cur );
115 if( ret != 0 )
116 {
117 mbedtls_strerror( ret, error_message, sizeof( error_message ) );
Gilles Peskine98798442021-08-03 13:15:04 +0200118 printf( "\n%s: -0x%04x (%s)\n",
119 *cur, (unsigned) -ret, error_message );
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200120 goto exit;
121 }
122 }
123
124exit:
125 mbedtls_x509_crt_free( &cas );
126 return( ret == 0 );
127}
128
129int main( int argc, char *argv[] )
130{
131 int exit_code = MBEDTLS_EXIT_FAILURE;
132 unsigned i, j;
133 struct mbedtls_timing_hr_time timer;
134 unsigned long ms;
135
Gilles Peskine01997e12021-07-30 13:01:36 +0200136 if( argc <= 1 )
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200137 {
138 mbedtls_printf( USAGE );
139 goto exit;
140 }
141
142 opt.filenames = NULL;
143 opt.iterations = DFL_ITERATIONS;
144 opt.prime_cache = DFL_PRIME_CACHE;
145
146 for( i = 1; i < (unsigned) argc; i++ )
147 {
148 char *p = argv[i];
149 char *q = NULL;
150
151 if( strcmp( p, "--" ) == 0 )
152 break;
153 if( ( q = strchr( p, '=' ) ) == NULL )
154 break;
155 *q++ = '\0';
156
157 for( j = 0; p + j < q; j++ )
158 {
159 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
160 argv[i][j] |= 0x20;
161 }
162
163 if( strcmp( p, "iterations" ) == 0 )
164 {
165 opt.iterations = atoi( q );
166 }
167 else if( strcmp( p, "prime" ) == 0 )
168 {
169 opt.iterations = atoi( q ) != 0;
170 }
171 else
Gilles Peskine9a2114c2021-07-30 13:01:52 +0200172 {
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200173 mbedtls_printf( "Unknown option: %s\n", p );
Gilles Peskine9a2114c2021-07-30 13:01:52 +0200174 mbedtls_printf( USAGE );
175 goto exit;
176 }
Gilles Peskine2c1442e2021-07-26 20:20:54 +0200177 }
178
179 opt.filenames = (const char**) argv + i;
180 if( *opt.filenames == 0 )
181 {
182 mbedtls_printf( "Missing list of certificate files to parse\n" );
183 goto exit;
184 }
185
186 mbedtls_printf( "Parsing %u certificates", argc - i );
187 if( opt.prime_cache )
188 {
189 if( ! read_certificates( opt.filenames ) )
190 goto exit;
191 mbedtls_printf( " " );
192 }
193
194 (void) mbedtls_timing_get_timer( &timer, 1 );
195 for( i = 1; i <= opt.iterations; i++ )
196 {
197 if( ! read_certificates( opt.filenames ) )
198 goto exit;
199 mbedtls_printf( "." );
200 }
201 ms = mbedtls_timing_get_timer( &timer, 0 );
202 mbedtls_printf( "\n%u iterations -> %lu ms\n", opt.iterations, ms );
203 exit_code = MBEDTLS_EXIT_SUCCESS;
204
205exit:
206 mbedtls_exit( exit_code );
207}
208#endif /* necessary configuration */