ssl_client2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * SSL client with certificate authentication
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #ifndef _CRT_SECURE_NO_DEPRECATE
  26. #define _CRT_SECURE_NO_DEPRECATE 1
  27. #endif
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #ifdef PRINTF_STDLIB
  31. #include <stdio.h>
  32. #endif
  33. #ifdef PRINTF_CUSTOM
  34. #include "tinystdio.h"
  35. #endif
  36. #include "polarssl/config.h"
  37. #include "polarssl/net.h"
  38. #include "polarssl/ssl.h"
  39. #include "polarssl/havege.h"
  40. #include "polarssl/certs.h"
  41. #include "polarssl/x509.h"
  42. #define DFL_SERVER_NAME "localhost"
  43. #define DFL_SERVER_PORT 4433
  44. #define DFL_REQUEST_PAGE "/"
  45. #define DFL_DEBUG_LEVEL 0
  46. #define DFL_CA_FILE ""
  47. #define DFL_CRT_FILE ""
  48. #define DFL_KEY_FILE ""
  49. #define DFL_FORCE_CIPHER 0
  50. #define GET_REQUEST "GET %s HTTP/1.0\r\n\r\n"
  51. /*
  52. * global options
  53. */
  54. struct options
  55. {
  56. char *server_name; /* hostname of the server (client only) */
  57. int server_port; /* port on which the ssl service runs */
  58. int debug_level; /* level of debugging */
  59. char *request_page; /* page on server to request */
  60. char *ca_file; /* the file with the CA certificate(s) */
  61. char *crt_file; /* the file with the client certificate */
  62. char *key_file; /* the file with the client key */
  63. int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
  64. } opt;
  65. void my_debug( void *ctx, int level, const char *str )
  66. {
  67. if( level < opt.debug_level )
  68. {
  69. fprintf( (FILE *) ctx, "%s", str );
  70. fflush( (FILE *) ctx );
  71. }
  72. }
  73. #if defined(POLARSSL_FS_IO)
  74. #define USAGE_IO \
  75. " ca_file=%%s default: \"\" (pre-loaded)\n" \
  76. " crt_file=%%s default: \"\" (pre-loaded)\n" \
  77. " key_file=%%s default: \"\" (pre-loaded)\n"
  78. #else
  79. #define USAGE_IO \
  80. " No file operations available (POLARSSL_FS_IO not defined)\n"
  81. #endif /* POLARSSL_FS_IO */
  82. #define USAGE \
  83. "\n usage: ssl_client2 param=<>...\n" \
  84. "\n acceptable parameters:\n" \
  85. " server_name=%%s default: localhost\n" \
  86. " server_port=%%d default: 4433\n" \
  87. " debug_level=%%d default: 0 (disabled)\n" \
  88. USAGE_IO \
  89. " request_page=%%s default: \".\"\n" \
  90. " force_ciphersuite=<name> default: all enabled\n"\
  91. " acceptable ciphersuite names:\n"
  92. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_HAVEGE_C) || \
  93. !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
  94. !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C)
  95. int main( void )
  96. {
  97. printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
  98. "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
  99. "POLARSSL_NET_C and/or POLARSSL_RSA_C not defined.\n");
  100. return( 0 );
  101. }
  102. #else
  103. int main( int argc, char *argv[] )
  104. {
  105. int ret = 0, len, server_fd;
  106. unsigned char buf[1024];
  107. havege_state hs;
  108. ssl_context ssl;
  109. ssl_session ssn;
  110. x509_cert cacert;
  111. x509_cert clicert;
  112. rsa_context rsa;
  113. int i;
  114. size_t j, n;
  115. char *p, *q;
  116. const int *list;
  117. /*
  118. * Make sure memory references are valid.
  119. */
  120. server_fd = 0;
  121. memset( &ssn, 0, sizeof( ssl_session ) );
  122. memset( &ssl, 0, sizeof( ssl_context ) );
  123. memset( &cacert, 0, sizeof( x509_cert ) );
  124. memset( &clicert, 0, sizeof( x509_cert ) );
  125. memset( &rsa, 0, sizeof( rsa_context ) );
  126. if( argc == 0 )
  127. {
  128. usage:
  129. printf( USAGE );
  130. list = ssl_list_ciphersuites();
  131. while( *list )
  132. {
  133. printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
  134. list++;
  135. }
  136. printf("\n");
  137. goto exit;
  138. }
  139. opt.server_name = DFL_SERVER_NAME;
  140. opt.server_port = DFL_SERVER_PORT;
  141. opt.debug_level = DFL_DEBUG_LEVEL;
  142. opt.request_page = DFL_REQUEST_PAGE;
  143. opt.ca_file = DFL_CA_FILE;
  144. opt.crt_file = DFL_CRT_FILE;
  145. opt.key_file = DFL_KEY_FILE;
  146. opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
  147. for( i = 1; i < argc; i++ )
  148. {
  149. n = strlen( argv[i] );
  150. for( j = 0; j < n; j++ )
  151. {
  152. if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
  153. argv[i][j] |= 0x20;
  154. }
  155. p = argv[i];
  156. if( ( q = strchr( p, '=' ) ) == NULL )
  157. goto usage;
  158. *q++ = '\0';
  159. if( strcmp( p, "server_name" ) == 0 )
  160. opt.server_name = q;
  161. else if( strcmp( p, "server_port" ) == 0 )
  162. {
  163. opt.server_port = atoi( q );
  164. if( opt.server_port < 1 || opt.server_port > 65535 )
  165. goto usage;
  166. }
  167. else if( strcmp( p, "debug_level" ) == 0 )
  168. {
  169. opt.debug_level = atoi( q );
  170. if( opt.debug_level < 0 || opt.debug_level > 65535 )
  171. goto usage;
  172. }
  173. else if( strcmp( p, "request_page" ) == 0 )
  174. opt.request_page = q;
  175. else if( strcmp( p, "ca_file" ) == 0 )
  176. opt.ca_file = q;
  177. else if( strcmp( p, "crt_file" ) == 0 )
  178. opt.crt_file = q;
  179. else if( strcmp( p, "key_file" ) == 0 )
  180. opt.key_file = q;
  181. else if( strcmp( p, "force_ciphersuite" ) == 0 )
  182. {
  183. opt.force_ciphersuite[0] = -1;
  184. opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
  185. if( opt.force_ciphersuite[0] <= 0 )
  186. goto usage;
  187. opt.force_ciphersuite[1] = 0;
  188. }
  189. else
  190. goto usage;
  191. }
  192. /*
  193. * 0. Initialize the RNG and the session data
  194. */
  195. havege_init( &hs );
  196. /*
  197. * 1.1. Load the trusted CA
  198. */
  199. printf( "\n . Loading the CA root certificate ..." );
  200. fflush( stdout );
  201. #if defined(POLARSSL_FS_IO)
  202. if( strlen( opt.ca_file ) )
  203. ret = x509parse_crtfile( &cacert, opt.ca_file );
  204. else
  205. #endif
  206. #if defined(POLARSSL_CERTS_C)
  207. ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
  208. strlen( test_ca_crt ) );
  209. #else
  210. {
  211. ret = 1;
  212. printf("POLARSSL_CERTS_C not defined.");
  213. }
  214. #endif
  215. if( ret != 0 )
  216. {
  217. printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
  218. goto exit;
  219. }
  220. printf( " ok\n" );
  221. /*
  222. * 1.2. Load own certificate and private key
  223. *
  224. * (can be skipped if client authentication is not required)
  225. */
  226. printf( " . Loading the client cert. and key..." );
  227. fflush( stdout );
  228. #if defined(POLARSSL_FS_IO)
  229. if( strlen( opt.crt_file ) )
  230. ret = x509parse_crtfile( &clicert, opt.crt_file );
  231. else
  232. #endif
  233. #if defined(POLARSSL_CERTS_C)
  234. ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
  235. strlen( test_cli_crt ) );
  236. #else
  237. {
  238. ret = 1;
  239. printf("POLARSSL_CERTS_C not defined.");
  240. }
  241. #endif
  242. if( ret != 0 )
  243. {
  244. printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
  245. goto exit;
  246. }
  247. #if defined(POLARSSL_FS_IO)
  248. if( strlen( opt.key_file ) )
  249. ret = x509parse_keyfile( &rsa, opt.key_file, "" );
  250. else
  251. #endif
  252. #if defined(POLARSSL_CERTS_C)
  253. ret = x509parse_key( &rsa, (unsigned char *) test_cli_key,
  254. strlen( test_cli_key ), NULL, 0 );
  255. #else
  256. {
  257. ret = 1;
  258. printf("POLARSSL_CERTS_C not defined.");
  259. }
  260. #endif
  261. if( ret != 0 )
  262. {
  263. printf( " failed\n ! x509parse_key returned %d\n\n", ret );
  264. goto exit;
  265. }
  266. printf( " ok\n" );
  267. /*
  268. * 2. Start the connection
  269. */
  270. printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
  271. opt.server_port );
  272. fflush( stdout );
  273. if( ( ret = net_connect( &server_fd, opt.server_name,
  274. opt.server_port ) ) != 0 )
  275. {
  276. printf( " failed\n ! net_connect returned %d\n\n", ret );
  277. goto exit;
  278. }
  279. printf( " ok\n" );
  280. /*
  281. * 3. Setup stuff
  282. */
  283. printf( " . Setting up the SSL/TLS structure..." );
  284. fflush( stdout );
  285. havege_init( &hs );
  286. if( ( ret = ssl_init( &ssl ) ) != 0 )
  287. {
  288. printf( " failed\n ! ssl_init returned %d\n\n", ret );
  289. goto exit;
  290. }
  291. printf( " ok\n" );
  292. ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
  293. ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
  294. ssl_set_rng( &ssl, havege_rand, &hs );
  295. ssl_set_dbg( &ssl, my_debug, stdout );
  296. ssl_set_bio( &ssl, net_recv, &server_fd,
  297. net_send, &server_fd );
  298. if( opt.force_ciphersuite[0] == DFL_FORCE_CIPHER )
  299. ssl_set_ciphersuites( &ssl, ssl_default_ciphersuites );
  300. else
  301. ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
  302. ssl_set_session( &ssl, 1, 600, &ssn );
  303. ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
  304. ssl_set_own_cert( &ssl, &clicert, &rsa );
  305. ssl_set_hostname( &ssl, opt.server_name );
  306. /*
  307. * 4. Handshake
  308. */
  309. printf( " . Performing the SSL/TLS handshake..." );
  310. fflush( stdout );
  311. while( ( ret = ssl_handshake( &ssl ) ) != 0 )
  312. {
  313. if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
  314. {
  315. printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
  316. goto exit;
  317. }
  318. }
  319. printf( " ok\n [ Ciphersuite is %s ]\n",
  320. ssl_get_ciphersuite( &ssl ) );
  321. /*
  322. * 5. Verify the server certificate
  323. */
  324. printf( " . Verifying peer X.509 certificate..." );
  325. if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
  326. {
  327. printf( " failed\n" );
  328. if( ( ret & BADCERT_EXPIRED ) != 0 )
  329. printf( " ! server certificate has expired\n" );
  330. if( ( ret & BADCERT_REVOKED ) != 0 )
  331. printf( " ! server certificate has been revoked\n" );
  332. if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
  333. printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
  334. if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
  335. printf( " ! self-signed or not signed by a trusted CA\n" );
  336. printf( "\n" );
  337. }
  338. else
  339. printf( " ok\n" );
  340. printf( " . Peer certificate information ...\n" );
  341. x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl.peer_cert );
  342. printf( "%s\n", buf );
  343. /*
  344. * 6. Write the GET request
  345. */
  346. printf( " > Write to server:" );
  347. fflush( stdout );
  348. len = sprintf( (char *) buf, GET_REQUEST, opt.request_page );
  349. while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
  350. {
  351. if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
  352. {
  353. printf( " failed\n ! ssl_write returned %d\n\n", ret );
  354. goto exit;
  355. }
  356. }
  357. len = ret;
  358. printf( " %d bytes written\n\n%s", len, (char *) buf );
  359. /*
  360. * 7. Read the HTTP response
  361. */
  362. printf( " < Read from server:" );
  363. fflush( stdout );
  364. do
  365. {
  366. len = sizeof( buf ) - 1;
  367. memset( buf, 0, sizeof( buf ) );
  368. ret = ssl_read( &ssl, buf, len );
  369. if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
  370. continue;
  371. if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
  372. break;
  373. if( ret < 0 )
  374. {
  375. printf( "failed\n ! ssl_read returned %d\n\n", ret );
  376. break;
  377. }
  378. if( ret == 0 )
  379. {
  380. printf("\n\nEOF\n\n");
  381. break;
  382. }
  383. len = ret;
  384. printf( " %d bytes read\n\n%s", len, (char *) buf );
  385. }
  386. while( 1 );
  387. ssl_close_notify( &ssl );
  388. exit:
  389. if( server_fd )
  390. net_close( server_fd );
  391. x509_free( &clicert );
  392. x509_free( &cacert );
  393. rsa_free( &rsa );
  394. ssl_free( &ssl );
  395. memset( &ssl, 0, sizeof( ssl ) );
  396. #ifdef WIN32
  397. printf( " + Press Enter to exit this program.\n" );
  398. fflush( stdout ); getchar();
  399. #endif
  400. return( ret );
  401. }
  402. #endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_SSL_TLS_C &&
  403. POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C */