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