cert_app.c 15 KB

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