ssl_server.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * SSL server demonstration program
  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. #ifdef WIN32
  29. #include <windows.h>
  30. #endif
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #ifdef PRINTF_STDLIB
  34. #include <stdio.h>
  35. #endif
  36. #ifdef PRINTF_CUSTOM
  37. #include "tinystdio.h"
  38. #endif
  39. #include "polarssl/config.h"
  40. #include "polarssl/havege.h"
  41. #include "polarssl/certs.h"
  42. #include "polarssl/x509.h"
  43. #include "polarssl/ssl.h"
  44. #include "polarssl/net.h"
  45. #define HTTP_RESPONSE \
  46. "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
  47. "<h2>PolarSSL Test Server</h2>\r\n" \
  48. "<p>Successful connection using: %s</p>\r\n"
  49. /*
  50. * Computing a "safe" DH-1024 prime can take a very
  51. * long time, so a precomputed value is provided below.
  52. * You may run dh_genprime to generate a new value.
  53. */
  54. char *my_dhm_P =
  55. "E4004C1F94182000103D883A448B3F80" \
  56. "2CE4B44A83301270002C20D0321CFD00" \
  57. "11CCEF784C26A400F43DFB901BCA7538" \
  58. "F2C6B176001CF5A0FD16D2C48B1D0C1C" \
  59. "F6AC8E1DA6BCC3B4E1F96B0564965300" \
  60. "FFA1D0B601EB2800F489AA512C4B248C" \
  61. "01F76949A60BB7F00A40B1EAB64BDD48" \
  62. "E8A700D60B7F1200FA8E77B0A979DABF";
  63. char *my_dhm_G = "4";
  64. /*
  65. * Sorted by order of preference
  66. */
  67. int my_ciphersuites[] =
  68. {
  69. SSL_EDH_RSA_AES_256_SHA,
  70. SSL_EDH_RSA_CAMELLIA_256_SHA,
  71. SSL_EDH_RSA_AES_128_SHA,
  72. SSL_EDH_RSA_CAMELLIA_128_SHA,
  73. SSL_EDH_RSA_DES_168_SHA,
  74. SSL_RSA_AES_256_SHA,
  75. SSL_RSA_CAMELLIA_256_SHA,
  76. SSL_RSA_AES_128_SHA,
  77. SSL_RSA_CAMELLIA_128_SHA,
  78. SSL_RSA_DES_168_SHA,
  79. SSL_RSA_RC4_128_SHA,
  80. SSL_RSA_RC4_128_MD5,
  81. 0
  82. };
  83. #define DEBUG_LEVEL 0
  84. void my_debug( void *ctx, int level, const char *str )
  85. {
  86. if( level < DEBUG_LEVEL )
  87. {
  88. fprintf( (FILE *) ctx, "%s", str );
  89. fflush( (FILE *) ctx );
  90. }
  91. }
  92. /*
  93. * These session callbacks use a simple chained list
  94. * to store and retrieve the session information.
  95. */
  96. ssl_session *s_list_1st = NULL;
  97. ssl_session *cur, *prv;
  98. static int my_get_session( ssl_context *ssl )
  99. {
  100. time_t t = time( NULL );
  101. if( ssl->resume == 0 )
  102. return( 1 );
  103. cur = s_list_1st;
  104. prv = NULL;
  105. while( cur != NULL )
  106. {
  107. prv = cur;
  108. cur = cur->next;
  109. if( ssl->timeout != 0 && t - prv->start > ssl->timeout )
  110. continue;
  111. if( ssl->session->ciphersuite != prv->ciphersuite ||
  112. ssl->session->length != prv->length )
  113. continue;
  114. if( memcmp( ssl->session->id, prv->id, prv->length ) != 0 )
  115. continue;
  116. memcpy( ssl->session->master, prv->master, 48 );
  117. return( 0 );
  118. }
  119. return( 1 );
  120. }
  121. static int my_set_session( ssl_context *ssl )
  122. {
  123. time_t t = time( NULL );
  124. cur = s_list_1st;
  125. prv = NULL;
  126. while( cur != NULL )
  127. {
  128. if( ssl->timeout != 0 && t - cur->start > ssl->timeout )
  129. break; /* expired, reuse this slot */
  130. if( memcmp( ssl->session->id, cur->id, cur->length ) == 0 )
  131. break; /* client reconnected */
  132. prv = cur;
  133. cur = cur->next;
  134. }
  135. if( cur == NULL )
  136. {
  137. cur = (ssl_session *) malloc( sizeof( ssl_session ) );
  138. if( cur == NULL )
  139. return( 1 );
  140. if( prv == NULL )
  141. s_list_1st = cur;
  142. else prv->next = cur;
  143. }
  144. memcpy( cur, ssl->session, sizeof( ssl_session ) );
  145. return( 0 );
  146. }
  147. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \
  148. !defined(POLARSSL_HAVEGE_C) || !defined(POLARSSL_SSL_TLS_C) || \
  149. !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \
  150. !defined(POLARSSL_RSA_C)
  151. int main( void )
  152. {
  153. printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_HAVEGE_C "
  154. "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
  155. "POLARSSL_NET_C and/or POLARSSL_RSA_C not defined.\n");
  156. return( 0 );
  157. }
  158. #else
  159. int main( void )
  160. {
  161. int ret, len;
  162. int listen_fd;
  163. int client_fd;
  164. unsigned char buf[1024];
  165. havege_state hs;
  166. ssl_context ssl;
  167. ssl_session ssn;
  168. x509_cert srvcert;
  169. rsa_context rsa;
  170. /*
  171. * 1. Load the certificates and private RSA key
  172. */
  173. printf( "\n . Loading the server cert. and key..." );
  174. fflush( stdout );
  175. memset( &srvcert, 0, sizeof( x509_cert ) );
  176. /*
  177. * This demonstration program uses embedded test certificates.
  178. * Instead, you may want to use x509parse_crtfile() to read the
  179. * server and CA certificates, as well as x509parse_keyfile().
  180. */
  181. ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt,
  182. strlen( test_srv_crt ) );
  183. if( ret != 0 )
  184. {
  185. printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
  186. goto exit;
  187. }
  188. ret = x509parse_crt( &srvcert, (unsigned char *) test_ca_crt,
  189. strlen( test_ca_crt ) );
  190. if( ret != 0 )
  191. {
  192. printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
  193. goto exit;
  194. }
  195. rsa_init( &rsa, RSA_PKCS_V15, 0 );
  196. ret = x509parse_key( &rsa, (unsigned char *) test_srv_key,
  197. strlen( test_srv_key ), NULL, 0 );
  198. if( ret != 0 )
  199. {
  200. printf( " failed\n ! x509parse_key returned %d\n\n", ret );
  201. goto exit;
  202. }
  203. printf( " ok\n" );
  204. /*
  205. * 2. Setup the listening TCP socket
  206. */
  207. printf( " . Bind on https://localhost:4433/ ..." );
  208. fflush( stdout );
  209. if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
  210. {
  211. printf( " failed\n ! net_bind returned %d\n\n", ret );
  212. goto exit;
  213. }
  214. printf( " ok\n" );
  215. /*
  216. * 3. Wait until a client connects
  217. */
  218. #ifdef WIN32
  219. ShellExecute( NULL, "open", "https://localhost:4433/",
  220. NULL, NULL, SW_SHOWNORMAL );
  221. #endif
  222. client_fd = -1;
  223. memset( &ssl, 0, sizeof( ssl ) );
  224. accept:
  225. net_close( client_fd );
  226. ssl_free( &ssl );
  227. printf( " . Waiting for a remote connection ..." );
  228. fflush( stdout );
  229. if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
  230. {
  231. printf( " failed\n ! net_accept returned %d\n\n", ret );
  232. goto exit;
  233. }
  234. printf( " ok\n" );
  235. /*
  236. * 4. Setup stuff
  237. */
  238. printf( " . Setting up the RNG and SSL data...." );
  239. fflush( stdout );
  240. havege_init( &hs );
  241. if( ( ret = ssl_init( &ssl ) ) != 0 )
  242. {
  243. printf( " failed\n ! ssl_init returned %d\n\n", ret );
  244. goto accept;
  245. }
  246. printf( " ok\n" );
  247. ssl_set_endpoint( &ssl, SSL_IS_SERVER );
  248. ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
  249. ssl_set_rng( &ssl, havege_rand, &hs );
  250. ssl_set_dbg( &ssl, my_debug, stdout );
  251. ssl_set_bio( &ssl, net_recv, &client_fd,
  252. net_send, &client_fd );
  253. ssl_set_scb( &ssl, my_get_session,
  254. my_set_session );
  255. ssl_set_ciphersuites( &ssl, my_ciphersuites );
  256. ssl_set_session( &ssl, 1, 0, &ssn );
  257. memset( &ssn, 0, sizeof( ssl_session ) );
  258. ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
  259. ssl_set_own_cert( &ssl, &srvcert, &rsa );
  260. ssl_set_dh_param( &ssl, my_dhm_P, my_dhm_G );
  261. /*
  262. * 5. Handshake
  263. */
  264. printf( " . Performing the SSL/TLS handshake..." );
  265. fflush( stdout );
  266. while( ( ret = ssl_handshake( &ssl ) ) != 0 )
  267. {
  268. if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
  269. {
  270. printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
  271. goto accept;
  272. }
  273. }
  274. printf( " ok\n" );
  275. /*
  276. * 6. Read the HTTP Request
  277. */
  278. printf( " < Read from client:" );
  279. fflush( stdout );
  280. do
  281. {
  282. len = sizeof( buf ) - 1;
  283. memset( buf, 0, sizeof( buf ) );
  284. ret = ssl_read( &ssl, buf, len );
  285. if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
  286. continue;
  287. if( ret <= 0 )
  288. {
  289. switch( ret )
  290. {
  291. case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
  292. printf( " connection was closed gracefully\n" );
  293. break;
  294. case POLARSSL_ERR_NET_CONN_RESET:
  295. printf( " connection was reset by peer\n" );
  296. break;
  297. default:
  298. printf( " ssl_read returned %d\n", ret );
  299. break;
  300. }
  301. break;
  302. }
  303. len = ret;
  304. printf( " %d bytes read\n\n%s", len, (char *) buf );
  305. }
  306. while( 0 );
  307. /*
  308. * 7. Write the 200 Response
  309. */
  310. printf( " > Write to client:" );
  311. fflush( stdout );
  312. len = sprintf( (char *) buf, HTTP_RESPONSE,
  313. ssl_get_ciphersuite( &ssl ) );
  314. while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
  315. {
  316. if( ret == POLARSSL_ERR_NET_CONN_RESET )
  317. {
  318. printf( " failed\n ! peer closed the connection\n\n" );
  319. goto accept;
  320. }
  321. if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
  322. {
  323. printf( " failed\n ! ssl_write returned %d\n\n", ret );
  324. goto exit;
  325. }
  326. }
  327. len = ret;
  328. printf( " %d bytes written\n\n%s\n", len, (char *) buf );
  329. ssl_close_notify( &ssl );
  330. goto accept;
  331. exit:
  332. net_close( client_fd );
  333. x509_free( &srvcert );
  334. rsa_free( &rsa );
  335. ssl_free( &ssl );
  336. cur = s_list_1st;
  337. while( cur != NULL )
  338. {
  339. prv = cur;
  340. cur = cur->next;
  341. memset( prv, 0, sizeof( ssl_session ) );
  342. free( prv );
  343. }
  344. memset( &ssl, 0, sizeof( ssl_context ) );
  345. #ifdef WIN32
  346. printf( " Press Enter to exit this program.\n" );
  347. fflush( stdout ); getchar();
  348. #endif
  349. return( ret );
  350. }
  351. #endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_HAVEGE_C &&
  352. POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
  353. POLARSSL_RSA_C */