ssl_fork_server.c 12 KB

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