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