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