ssl_fork_server.c 12 KB

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