blob: cb168126a228f39731227f831ff36511cba434a5 [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 \
85 "\n usage: load_roots param=<>... [--] {FILE|DIR}...\n" \
86 "\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 ) );
118 printf( "\n%s: -0x%04x (%s)\n", *cur, -ret, error_message );
119 goto exit;
120 }
121 }
122
123exit:
124 mbedtls_x509_crt_free( &cas );
125 return( ret == 0 );
126}
127
128int main( int argc, char *argv[] )
129{
130 int exit_code = MBEDTLS_EXIT_FAILURE;
131 unsigned i, j;
132 struct mbedtls_timing_hr_time timer;
133 unsigned long ms;
134
135 if( argc == 0 )
136 {
137 mbedtls_printf( USAGE );
138 goto exit;
139 }
140
141 opt.filenames = NULL;
142 opt.iterations = DFL_ITERATIONS;
143 opt.prime_cache = DFL_PRIME_CACHE;
144
145 for( i = 1; i < (unsigned) argc; i++ )
146 {
147 char *p = argv[i];
148 char *q = NULL;
149
150 if( strcmp( p, "--" ) == 0 )
151 break;
152 if( ( q = strchr( p, '=' ) ) == NULL )
153 break;
154 *q++ = '\0';
155
156 for( j = 0; p + j < q; j++ )
157 {
158 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
159 argv[i][j] |= 0x20;
160 }
161
162 if( strcmp( p, "iterations" ) == 0 )
163 {
164 opt.iterations = atoi( q );
165 }
166 else if( strcmp( p, "prime" ) == 0 )
167 {
168 opt.iterations = atoi( q ) != 0;
169 }
170 else
171 mbedtls_printf( "Unknown option: %s\n", p );
172 }
173
174 opt.filenames = (const char**) argv + i;
175 if( *opt.filenames == 0 )
176 {
177 mbedtls_printf( "Missing list of certificate files to parse\n" );
178 goto exit;
179 }
180
181 mbedtls_printf( "Parsing %u certificates", argc - i );
182 if( opt.prime_cache )
183 {
184 if( ! read_certificates( opt.filenames ) )
185 goto exit;
186 mbedtls_printf( " " );
187 }
188
189 (void) mbedtls_timing_get_timer( &timer, 1 );
190 for( i = 1; i <= opt.iterations; i++ )
191 {
192 if( ! read_certificates( opt.filenames ) )
193 goto exit;
194 mbedtls_printf( "." );
195 }
196 ms = mbedtls_timing_get_timer( &timer, 0 );
197 mbedtls_printf( "\n%u iterations -> %lu ms\n", opt.iterations, ms );
198 exit_code = MBEDTLS_EXIT_SUCCESS;
199
200exit:
201 mbedtls_exit( exit_code );
202}
203#endif /* necessary configuration */