cert_app.c 15 KB

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