ssl_client2.c 13 KB

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