cert_app.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * Certificate reading application
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_PLATFORM_C)
  27. #include "mbedtls/platform.h"
  28. #else
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #define mbedtls_time time
  32. #define mbedtls_time_t time_t
  33. #define mbedtls_fprintf fprintf
  34. #define mbedtls_printf printf
  35. #define mbedtls_exit exit
  36. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  37. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  38. #endif /* MBEDTLS_PLATFORM_C */
  39. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
  40. !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
  41. !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
  42. !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
  43. !defined(MBEDTLS_CTR_DRBG_C)
  44. int main( void )
  45. {
  46. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
  47. "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
  48. "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
  49. "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_FS_IO and/or "
  50. "MBEDTLS_CTR_DRBG_C not defined.\n");
  51. return( 0 );
  52. }
  53. #else
  54. #include "mbedtls/entropy.h"
  55. #include "mbedtls/ctr_drbg.h"
  56. #include "mbedtls/net_sockets.h"
  57. #include "mbedtls/ssl.h"
  58. #include "mbedtls/x509.h"
  59. #include "mbedtls/debug.h"
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <string.h>
  63. #define MODE_NONE 0
  64. #define MODE_FILE 1
  65. #define MODE_SSL 2
  66. #define DFL_MODE MODE_NONE
  67. #define DFL_FILENAME "cert.crt"
  68. #define DFL_CA_FILE ""
  69. #define DFL_CRL_FILE ""
  70. #define DFL_CA_PATH ""
  71. #define DFL_SERVER_NAME "localhost"
  72. #define DFL_SERVER_PORT "4433"
  73. #define DFL_DEBUG_LEVEL 0
  74. #define DFL_PERMISSIVE 0
  75. #define USAGE_IO \
  76. " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
  77. " default: \"\" (none)\n" \
  78. " crl_file=%%s The single CRL file you want to use\n" \
  79. " default: \"\" (none)\n" \
  80. " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
  81. " default: \"\" (none) (overrides ca_file)\n"
  82. #define USAGE \
  83. "\n usage: cert_app param=<>...\n" \
  84. "\n acceptable parameters:\n" \
  85. " mode=file|ssl default: none\n" \
  86. " filename=%%s default: cert.crt\n" \
  87. USAGE_IO \
  88. " server_name=%%s default: localhost\n" \
  89. " server_port=%%d default: 4433\n" \
  90. " debug_level=%%d default: 0 (disabled)\n" \
  91. " permissive=%%d default: 0 (disabled)\n" \
  92. "\n"
  93. /*
  94. * global options
  95. */
  96. struct options
  97. {
  98. int mode; /* the mode to run the application in */
  99. const char *filename; /* filename of the certificate file */
  100. const char *ca_file; /* the file with the CA certificate(s) */
  101. const char *crl_file; /* the file with the CRL to use */
  102. const char *ca_path; /* the path with the CA certificate(s) reside */
  103. const char *server_name; /* hostname of the server (client only) */
  104. const char *server_port; /* port on which the ssl service runs */
  105. int debug_level; /* level of debugging */
  106. int permissive; /* permissive parsing */
  107. } opt;
  108. static void my_debug( void *ctx, int level,
  109. const char *file, int line,
  110. const char *str )
  111. {
  112. ((void) level);
  113. mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
  114. fflush( (FILE *) ctx );
  115. }
  116. static int my_verify( void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags )
  117. {
  118. char buf[1024];
  119. ((void) data);
  120. mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
  121. mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
  122. mbedtls_printf( "%s", buf );
  123. if ( ( *flags ) == 0 )
  124. mbedtls_printf( " This certificate has no flags\n" );
  125. else
  126. {
  127. mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
  128. mbedtls_printf( "%s\n", buf );
  129. }
  130. return( 0 );
  131. }
  132. int main( int argc, char *argv[] )
  133. {
  134. int ret = 1;
  135. int exit_code = MBEDTLS_EXIT_FAILURE;
  136. mbedtls_net_context server_fd;
  137. unsigned char buf[1024];
  138. mbedtls_entropy_context entropy;
  139. mbedtls_ctr_drbg_context ctr_drbg;
  140. mbedtls_ssl_context ssl;
  141. mbedtls_ssl_config conf;
  142. mbedtls_x509_crt cacert;
  143. mbedtls_x509_crl cacrl;
  144. int i, j;
  145. uint32_t flags;
  146. int verify = 0;
  147. char *p, *q;
  148. const char *pers = "cert_app";
  149. /*
  150. * Set to sane values
  151. */
  152. mbedtls_net_init( &server_fd );
  153. mbedtls_ctr_drbg_init( &ctr_drbg );
  154. mbedtls_ssl_init( &ssl );
  155. mbedtls_ssl_config_init( &conf );
  156. mbedtls_x509_crt_init( &cacert );
  157. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  158. mbedtls_x509_crl_init( &cacrl );
  159. #else
  160. /* Zeroize structure as CRL parsing is not supported and we have to pass
  161. it to the verify function */
  162. memset( &cacrl, 0, sizeof(mbedtls_x509_crl) );
  163. #endif
  164. if( argc == 0 )
  165. {
  166. usage:
  167. mbedtls_printf( USAGE );
  168. goto exit;
  169. }
  170. opt.mode = DFL_MODE;
  171. opt.filename = DFL_FILENAME;
  172. opt.ca_file = DFL_CA_FILE;
  173. opt.crl_file = DFL_CRL_FILE;
  174. opt.ca_path = DFL_CA_PATH;
  175. opt.server_name = DFL_SERVER_NAME;
  176. opt.server_port = DFL_SERVER_PORT;
  177. opt.debug_level = DFL_DEBUG_LEVEL;
  178. opt.permissive = DFL_PERMISSIVE;
  179. for( i = 1; i < argc; i++ )
  180. {
  181. p = argv[i];
  182. if( ( q = strchr( p, '=' ) ) == NULL )
  183. goto usage;
  184. *q++ = '\0';
  185. for( j = 0; p + j < q; j++ )
  186. {
  187. if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
  188. argv[i][j] |= 0x20;
  189. }
  190. if( strcmp( p, "mode" ) == 0 )
  191. {
  192. if( strcmp( q, "file" ) == 0 )
  193. opt.mode = MODE_FILE;
  194. else if( strcmp( q, "ssl" ) == 0 )
  195. opt.mode = MODE_SSL;
  196. else
  197. goto usage;
  198. }
  199. else if( strcmp( p, "filename" ) == 0 )
  200. opt.filename = q;
  201. else if( strcmp( p, "ca_file" ) == 0 )
  202. opt.ca_file = q;
  203. else if( strcmp( p, "crl_file" ) == 0 )
  204. opt.crl_file = q;
  205. else if( strcmp( p, "ca_path" ) == 0 )
  206. opt.ca_path = q;
  207. else if( strcmp( p, "server_name" ) == 0 )
  208. opt.server_name = q;
  209. else if( strcmp( p, "server_port" ) == 0 )
  210. opt.server_port = q;
  211. else if( strcmp( p, "debug_level" ) == 0 )
  212. {
  213. opt.debug_level = atoi( q );
  214. if( opt.debug_level < 0 || opt.debug_level > 65535 )
  215. goto usage;
  216. }
  217. else if( strcmp( p, "permissive" ) == 0 )
  218. {
  219. opt.permissive = atoi( q );
  220. if( opt.permissive < 0 || opt.permissive > 1 )
  221. goto usage;
  222. }
  223. else
  224. goto usage;
  225. }
  226. /*
  227. * 1.1. Load the trusted CA
  228. */
  229. mbedtls_printf( " . Loading the CA root certificate ..." );
  230. fflush( stdout );
  231. if( strlen( opt.ca_path ) )
  232. {
  233. if( ( ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path ) ) < 0 )
  234. {
  235. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_path returned -0x%x\n\n", -ret );
  236. goto exit;
  237. }
  238. verify = 1;
  239. }
  240. else if( strlen( opt.ca_file ) )
  241. {
  242. if( ( ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file ) ) < 0 )
  243. {
  244. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n", -ret );
  245. goto exit;
  246. }
  247. verify = 1;
  248. }
  249. mbedtls_printf( " ok (%d skipped)\n", ret );
  250. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  251. if( strlen( opt.crl_file ) )
  252. {
  253. if( ( ret = mbedtls_x509_crl_parse_file( &cacrl, opt.crl_file ) ) != 0 )
  254. {
  255. mbedtls_printf( " failed\n ! mbedtls_x509_crl_parse returned -0x%x\n\n", -ret );
  256. goto exit;
  257. }
  258. verify = 1;
  259. }
  260. #endif
  261. if( opt.mode == MODE_FILE )
  262. {
  263. mbedtls_x509_crt crt;
  264. mbedtls_x509_crt *cur = &crt;
  265. mbedtls_x509_crt_init( &crt );
  266. /*
  267. * 1.1. Load the certificate(s)
  268. */
  269. mbedtls_printf( "\n . Loading the certificate(s) ..." );
  270. fflush( stdout );
  271. ret = mbedtls_x509_crt_parse_file( &crt, opt.filename );
  272. if( ret < 0 )
  273. {
  274. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned %d\n\n", ret );
  275. mbedtls_x509_crt_free( &crt );
  276. goto exit;
  277. }
  278. if( opt.permissive == 0 && ret > 0 )
  279. {
  280. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse failed to parse %d certificates\n\n", ret );
  281. mbedtls_x509_crt_free( &crt );
  282. goto exit;
  283. }
  284. mbedtls_printf( " ok\n" );
  285. /*
  286. * 1.2 Print the certificate(s)
  287. */
  288. while( cur != NULL )
  289. {
  290. mbedtls_printf( " . Peer certificate information ...\n" );
  291. ret = mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
  292. cur );
  293. if( ret == -1 )
  294. {
  295. mbedtls_printf( " failed\n ! mbedtls_x509_crt_info returned %d\n\n", ret );
  296. mbedtls_x509_crt_free( &crt );
  297. goto exit;
  298. }
  299. mbedtls_printf( "%s\n", buf );
  300. cur = cur->next;
  301. }
  302. /*
  303. * 1.3 Verify the certificate
  304. */
  305. if( verify )
  306. {
  307. mbedtls_printf( " . Verifying X.509 certificate..." );
  308. if( ( ret = mbedtls_x509_crt_verify( &crt, &cacert, &cacrl, NULL, &flags,
  309. my_verify, NULL ) ) != 0 )
  310. {
  311. char vrfy_buf[512];
  312. mbedtls_printf( " failed\n" );
  313. mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
  314. mbedtls_printf( "%s\n", vrfy_buf );
  315. }
  316. else
  317. mbedtls_printf( " ok\n" );
  318. }
  319. mbedtls_x509_crt_free( &crt );
  320. }
  321. else if( opt.mode == MODE_SSL )
  322. {
  323. /*
  324. * 1. Initialize the RNG and the session data
  325. */
  326. mbedtls_printf( "\n . Seeding the random number generator..." );
  327. fflush( stdout );
  328. mbedtls_entropy_init( &entropy );
  329. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  330. (const unsigned char *) pers,
  331. strlen( pers ) ) ) != 0 )
  332. {
  333. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  334. goto ssl_exit;
  335. }
  336. mbedtls_printf( " ok\n" );
  337. #if defined(MBEDTLS_DEBUG_C)
  338. mbedtls_debug_set_threshold( opt.debug_level );
  339. #endif
  340. /*
  341. * 2. Start the connection
  342. */
  343. mbedtls_printf( " . SSL connection to tcp/%s/%s...", opt.server_name,
  344. opt.server_port );
  345. fflush( stdout );
  346. if( ( ret = mbedtls_net_connect( &server_fd, opt.server_name,
  347. opt.server_port, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
  348. {
  349. mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
  350. goto ssl_exit;
  351. }
  352. /*
  353. * 3. Setup stuff
  354. */
  355. if( ( ret = mbedtls_ssl_config_defaults( &conf,
  356. MBEDTLS_SSL_IS_CLIENT,
  357. MBEDTLS_SSL_TRANSPORT_STREAM,
  358. MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
  359. {
  360. mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
  361. goto exit;
  362. }
  363. if( verify )
  364. {
  365. mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_REQUIRED );
  366. mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
  367. mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
  368. }
  369. else
  370. mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_NONE );
  371. mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
  372. mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
  373. if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
  374. {
  375. mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
  376. goto ssl_exit;
  377. }
  378. if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
  379. {
  380. mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
  381. goto ssl_exit;
  382. }
  383. mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
  384. /*
  385. * 4. Handshake
  386. */
  387. while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
  388. {
  389. if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  390. {
  391. mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned %d\n\n", ret );
  392. goto ssl_exit;
  393. }
  394. }
  395. mbedtls_printf( " ok\n" );
  396. /*
  397. * 5. Print the certificate
  398. */
  399. mbedtls_printf( " . Peer certificate information ...\n" );
  400. ret = mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
  401. ssl.session->peer_cert );
  402. if( ret == -1 )
  403. {
  404. mbedtls_printf( " failed\n ! mbedtls_x509_crt_info returned %d\n\n", ret );
  405. goto ssl_exit;
  406. }
  407. mbedtls_printf( "%s\n", buf );
  408. mbedtls_ssl_close_notify( &ssl );
  409. ssl_exit:
  410. mbedtls_ssl_free( &ssl );
  411. mbedtls_ssl_config_free( &conf );
  412. }
  413. else
  414. goto usage;
  415. exit_code = MBEDTLS_EXIT_SUCCESS;
  416. exit:
  417. mbedtls_net_free( &server_fd );
  418. mbedtls_x509_crt_free( &cacert );
  419. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  420. mbedtls_x509_crl_free( &cacrl );
  421. #endif
  422. mbedtls_ctr_drbg_free( &ctr_drbg );
  423. mbedtls_entropy_free( &entropy );
  424. #if defined(_WIN32)
  425. mbedtls_printf( " + Press Enter to exit this program.\n" );
  426. fflush( stdout ); getchar();
  427. #endif
  428. return( exit_code );
  429. }
  430. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
  431. MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
  432. MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */