blob: ba398f9b37b84b5179e35610ca4437fe706717fc [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 ) );
Gilles Peskine85f31652021-08-03 13:15:04 +0200122 printf( "\n%s: -0x%04x (%s)\n",
123 *cur, (unsigned) -ret, error_message );
Gilles Peskine82c04322021-07-26 20:20:54 +0200124 goto exit;
125 }
126 }
127
128exit:
129 mbedtls_x509_crt_free( &cas );
130 return( ret == 0 );
131}
132
133int main( int argc, char *argv[] )
134{
135 int exit_code = MBEDTLS_EXIT_FAILURE;
136 unsigned i, j;
137 struct mbedtls_timing_hr_time timer;
138 unsigned long ms;
139
Gilles Peskine373c54e2021-07-30 13:01:36 +0200140 if( argc <= 1 )
Gilles Peskine82c04322021-07-26 20:20:54 +0200141 {
142 mbedtls_printf( USAGE );
143 goto exit;
144 }
145
146 opt.filenames = NULL;
147 opt.iterations = DFL_ITERATIONS;
148 opt.prime_cache = DFL_PRIME_CACHE;
149
150 for( i = 1; i < (unsigned) argc; i++ )
151 {
152 char *p = argv[i];
153 char *q = NULL;
154
155 if( strcmp( p, "--" ) == 0 )
156 break;
157 if( ( q = strchr( p, '=' ) ) == NULL )
158 break;
159 *q++ = '\0';
160
161 for( j = 0; p + j < q; j++ )
162 {
163 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
164 argv[i][j] |= 0x20;
165 }
166
167 if( strcmp( p, "iterations" ) == 0 )
168 {
169 opt.iterations = atoi( q );
170 }
171 else if( strcmp( p, "prime" ) == 0 )
172 {
173 opt.iterations = atoi( q ) != 0;
174 }
175 else
Gilles Peskine38d41b92021-07-30 13:01:52 +0200176 {
Gilles Peskine82c04322021-07-26 20:20:54 +0200177 mbedtls_printf( "Unknown option: %s\n", p );
Gilles Peskine38d41b92021-07-30 13:01:52 +0200178 mbedtls_printf( USAGE );
179 goto exit;
180 }
Gilles Peskine82c04322021-07-26 20:20:54 +0200181 }
182
183 opt.filenames = (const char**) argv + i;
184 if( *opt.filenames == 0 )
185 {
186 mbedtls_printf( "Missing list of certificate files to parse\n" );
187 goto exit;
188 }
189
190 mbedtls_printf( "Parsing %u certificates", argc - i );
191 if( opt.prime_cache )
192 {
193 if( ! read_certificates( opt.filenames ) )
194 goto exit;
195 mbedtls_printf( " " );
196 }
197
198 (void) mbedtls_timing_get_timer( &timer, 1 );
199 for( i = 1; i <= opt.iterations; i++ )
200 {
201 if( ! read_certificates( opt.filenames ) )
202 goto exit;
203 mbedtls_printf( "." );
204 }
205 ms = mbedtls_timing_get_timer( &timer, 0 );
206 mbedtls_printf( "\n%u iterations -> %lu ms\n", opt.iterations, ms );
207 exit_code = MBEDTLS_EXIT_SUCCESS;
208
209exit:
210 mbedtls_exit( exit_code );
211}
212#endif /* necessary configuration */