2021-07-26 20:20:54 +02:00
|
|
|
/*
|
|
|
|
* Root CA reading application
|
|
|
|
*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 21:43:00 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2021-07-26 20:20:54 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mbedtls/build_info.h"
|
|
|
|
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
|
|
|
#if !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
|
|
|
|
!defined(MBEDTLS_TIMING_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
int main(void)
|
2021-07-26 20:20:54 +02:00
|
|
|
{
|
|
|
|
mbedtls_printf("MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_FS_IO and/or "
|
2023-01-11 14:50:10 +01:00
|
|
|
"MBEDTLS_TIMING_C not defined.\n");
|
|
|
|
mbedtls_exit(0);
|
2021-07-26 20:20:54 +02:00
|
|
|
}
|
|
|
|
#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 \
|
2021-07-30 13:00:10 +02:00
|
|
|
"\n usage: load_roots param=<>... [--] FILE...\n" \
|
2021-07-26 20:20:54 +02:00
|
|
|
"\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
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
struct options {
|
2021-07-26 20:20:54 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int read_certificates(const char *const *filenames)
|
2021-07-26 20:20:54 +02:00
|
|
|
{
|
|
|
|
mbedtls_x509_crt cas;
|
|
|
|
int ret = 0;
|
|
|
|
const char *const *cur;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&cas);
|
2021-07-26 20:20:54 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (cur = filenames; *cur != NULL; cur++) {
|
|
|
|
ret = mbedtls_x509_crt_parse_file(&cas, *cur);
|
|
|
|
if (ret != 0) {
|
2021-08-06 14:37:01 +02:00
|
|
|
#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
|
|
|
|
char error_message[200];
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_strerror(ret, error_message, sizeof(error_message));
|
|
|
|
printf("\n%s: -0x%04x (%s)\n",
|
|
|
|
*cur, (unsigned) -ret, error_message);
|
2021-08-06 14:37:01 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
printf("\n%s: -0x%04x\n",
|
|
|
|
*cur, (unsigned) -ret);
|
2021-08-06 14:37:01 +02:00
|
|
|
#endif
|
2021-07-26 20:20:54 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&cas);
|
|
|
|
return ret == 0;
|
2021-07-26 20:20:54 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int main(int argc, char *argv[])
|
2021-07-26 20:20:54 +02:00
|
|
|
{
|
|
|
|
int exit_code = MBEDTLS_EXIT_FAILURE;
|
|
|
|
unsigned i, j;
|
|
|
|
struct mbedtls_timing_hr_time timer;
|
|
|
|
unsigned long ms;
|
|
|
|
|
2023-04-14 09:26:39 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_status_t status = psa_crypto_init();
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
|
|
|
|
(int) status);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2023-04-19 10:15:26 +02:00
|
|
|
if (argc <= 1) {
|
|
|
|
mbedtls_printf(USAGE);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2021-07-26 20:20:54 +02:00
|
|
|
opt.filenames = NULL;
|
|
|
|
opt.iterations = DFL_ITERATIONS;
|
|
|
|
opt.prime_cache = DFL_PRIME_CACHE;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 1; i < (unsigned) argc; i++) {
|
2021-07-26 20:20:54 +02:00
|
|
|
char *p = argv[i];
|
|
|
|
char *q = NULL;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(p, "--") == 0) {
|
2021-07-26 20:20:54 +02:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
if ((q = strchr(p, '=')) == NULL) {
|
2021-07-26 20:20:54 +02:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-07-26 20:20:54 +02:00
|
|
|
*q++ = '\0';
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (j = 0; p + j < q; j++) {
|
|
|
|
if (argv[i][j] >= 'A' && argv[i][j] <= 'Z') {
|
2021-07-26 20:20:54 +02:00
|
|
|
argv[i][j] |= 0x20;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-07-26 20:20:54 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
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);
|
2021-07-30 13:01:52 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2021-07-26 20:20:54 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
opt.filenames = (const char **) argv + i;
|
|
|
|
if (*opt.filenames == 0) {
|
|
|
|
mbedtls_printf("Missing list of certificate files to parse\n");
|
2021-07-26 20:20:54 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf("Parsing %u certificates", argc - i);
|
|
|
|
if (opt.prime_cache) {
|
|
|
|
if (!read_certificates(opt.filenames)) {
|
2021-07-26 20:20:54 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
mbedtls_printf(" ");
|
2021-07-26 20:20:54 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
(void) mbedtls_timing_get_timer(&timer, 1);
|
|
|
|
for (i = 1; i <= opt.iterations; i++) {
|
|
|
|
if (!read_certificates(opt.filenames)) {
|
2021-07-26 20:20:54 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
mbedtls_printf(".");
|
2021-07-26 20:20:54 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
ms = mbedtls_timing_get_timer(&timer, 0);
|
|
|
|
mbedtls_printf("\n%u iterations -> %lu ms\n", opt.iterations, ms);
|
2021-07-26 20:20:54 +02:00
|
|
|
exit_code = MBEDTLS_EXIT_SUCCESS;
|
|
|
|
|
|
|
|
exit:
|
2023-04-19 13:47:43 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-04-19 10:15:26 +02:00
|
|
|
mbedtls_psa_crypto_free();
|
2023-04-19 13:47:43 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_exit(exit_code);
|
2021-07-26 20:20:54 +02:00
|
|
|
}
|
|
|
|
#endif /* necessary configuration */
|