aboutsummaryrefslogtreecommitdiff
path: root/programs/x509/load_roots.c
blob: b8b0ecdee56c7ff05d08723e0d93b721dbe4f6b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 *  Root CA reading application
 *
 *  Copyright The Mbed TLS Contributors
 *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
 *
 *  This file is provided under the Apache License 2.0, or the
 *  GNU General Public License v2.0 or later.
 *
 *  **********
 *  Apache License 2.0:
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may
 *  not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  **********
 *
 *  **********
 *  GNU General Public License v2.0 or later:
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *  **********
 */

#include "mbedtls/build_info.h"

#include "mbedtls/platform.h"

#if !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) ||  \
    !defined(MBEDTLS_TIMING_C)
int main( void )
{
    mbedtls_printf("MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_FS_IO and/or "
           "MBEDTLS_TIMING_C not defined.\n");
    mbedtls_exit( 0 );
}
#else

#include "mbedtls/error.h"
#include "mbedtls/timing.h"
#include "mbedtls/x509_crt.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DFL_ITERATIONS          1
#define DFL_PRIME_CACHE         1

#define USAGE \
    "\n usage: load_roots param=<>... [--] FILE...\n"   \
    "\n acceptable parameters:\n"                       \
    "    iterations=%%d        Iteration count (not including cache priming); default: 1\n"  \
    "    prime=%%d             Prime the disk read cache? Default: 1 (yes)\n"  \
    "\n"


/*
 * global options
 */
struct options
{
    const char **filenames;     /* NULL-terminated list of file names */
    unsigned iterations;        /* Number of iterations to time */
    int prime_cache;            /* Prime the disk read cache? */
} opt;


int read_certificates( const char *const *filenames )
{
    mbedtls_x509_crt cas;
    int ret = 0;
    const char *const *cur;

    mbedtls_x509_crt_init( &cas );

    for( cur = filenames; *cur != NULL; cur++ )
    {
        ret = mbedtls_x509_crt_parse_file( &cas, *cur );
        if( ret != 0 )
        {
#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
            char error_message[200];
            mbedtls_strerror( ret, error_message, sizeof( error_message ) );
            printf( "\n%s: -0x%04x (%s)\n",
                    *cur, (unsigned) -ret, error_message );
#else
            printf( "\n%s: -0x%04x\n",
                    *cur, (unsigned) -ret );
#endif
            goto exit;
        }
    }

exit:
    mbedtls_x509_crt_free( &cas );
    return( ret == 0 );
}

int main( int argc, char *argv[] )
{
    int exit_code = MBEDTLS_EXIT_FAILURE;
    unsigned i, j;
    struct mbedtls_timing_hr_time timer;
    unsigned long ms;

    if( argc <= 1 )
    {
        mbedtls_printf( USAGE );
        goto exit;
    }

    opt.filenames = NULL;
    opt.iterations = DFL_ITERATIONS;
    opt.prime_cache = DFL_PRIME_CACHE;

    for( i = 1; i < (unsigned) argc; i++ )
    {
        char *p = argv[i];
        char *q = NULL;

        if( strcmp( p, "--" ) == 0 )
            break;
        if( ( q = strchr( p, '=' ) ) == NULL )
            break;
        *q++ = '\0';

        for( j = 0; p + j < q; j++ )
        {
            if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
                argv[i][j] |= 0x20;
        }

        if( strcmp( p, "iterations" ) == 0 )
        {
            opt.iterations = atoi( q );
        }
        else if( strcmp( p, "prime" ) == 0 )
        {
            opt.iterations = atoi( q ) != 0;
        }
        else
        {
            mbedtls_printf( "Unknown option: %s\n", p );
            mbedtls_printf( USAGE );
            goto exit;
        }
    }

    opt.filenames = (const char**) argv + i;
    if( *opt.filenames == 0 )
    {
        mbedtls_printf( "Missing list of certificate files to parse\n" );
        goto exit;
    }

    mbedtls_printf( "Parsing %u certificates", argc - i );
    if( opt.prime_cache )
    {
        if( ! read_certificates( opt.filenames ) )
            goto exit;
        mbedtls_printf( " " );
    }

    (void) mbedtls_timing_get_timer( &timer, 1 );
    for( i = 1; i <= opt.iterations; i++ )
    {
        if( ! read_certificates( opt.filenames ) )
            goto exit;
        mbedtls_printf( "." );
    }
    ms = mbedtls_timing_get_timer( &timer, 0 );
    mbedtls_printf( "\n%u iterations -> %lu ms\n", opt.iterations, ms );
    exit_code = MBEDTLS_EXIT_SUCCESS;

exit:
    mbedtls_exit( exit_code );
}
#endif /* necessary configuration */