ssl_server.c 11 KB

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