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