load_roots.c 5.9 KB

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