ssl_fork_server.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * SSL server demonstration program using fork() for handling multiple clients
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "mbedtls/build_info.h"
  20. #if defined(MBEDTLS_PLATFORM_C)
  21. #include "mbedtls/platform.h"
  22. #else
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #define mbedtls_fprintf fprintf
  26. #define mbedtls_printf printf
  27. #define mbedtls_time_t time_t
  28. #define mbedtls_exit exit
  29. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  30. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  31. #endif /* MBEDTLS_PLATFORM_C */
  32. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
  33. !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_C) || \
  34. !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
  35. !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
  36. !defined(MBEDTLS_TIMING_C) || !defined(MBEDTLS_FS_IO) || \
  37. !defined(MBEDTLS_PEM_PARSE_C)
  38. int main( int argc, char *argv[] )
  39. {
  40. ((void) argc);
  41. ((void) argv);
  42. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C "
  43. "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
  44. "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
  45. "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
  46. "MBEDTLS_TIMING_C and/or MBEDTLS_PEM_PARSE_C not defined.\n");
  47. mbedtls_exit( 0 );
  48. }
  49. #elif defined(_WIN32)
  50. int main( void )
  51. {
  52. mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
  53. "to work correctly.\n");
  54. mbedtls_exit( 0 );
  55. }
  56. #else
  57. #include "mbedtls/entropy.h"
  58. #include "mbedtls/ctr_drbg.h"
  59. #include "test/certs.h"
  60. #include "mbedtls/x509.h"
  61. #include "mbedtls/ssl.h"
  62. #include "mbedtls/net_sockets.h"
  63. #include "mbedtls/timing.h"
  64. #include <string.h>
  65. #include <signal.h>
  66. #if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
  67. #include <unistd.h>
  68. #endif
  69. #define HTTP_RESPONSE \
  70. "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
  71. "<h2>mbed TLS Test Server</h2>\r\n" \
  72. "<p>Successful connection using: %s</p>\r\n"
  73. #define DEBUG_LEVEL 0
  74. static void my_debug( void *ctx, int level,
  75. const char *file, int line,
  76. const char *str )
  77. {
  78. ((void) level);
  79. mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
  80. fflush( (FILE *) ctx );
  81. }
  82. int main( void )
  83. {
  84. int ret = 1, len, cnt = 0, pid;
  85. int exit_code = MBEDTLS_EXIT_FAILURE;
  86. mbedtls_net_context listen_fd, client_fd;
  87. unsigned char buf[1024];
  88. const char *pers = "ssl_fork_server";
  89. mbedtls_entropy_context entropy;
  90. mbedtls_ctr_drbg_context ctr_drbg;
  91. mbedtls_ssl_context ssl;
  92. mbedtls_ssl_config conf;
  93. mbedtls_x509_crt srvcert;
  94. mbedtls_pk_context pkey;
  95. mbedtls_net_init( &listen_fd );
  96. mbedtls_net_init( &client_fd );
  97. mbedtls_ssl_init( &ssl );
  98. mbedtls_ssl_config_init( &conf );
  99. mbedtls_entropy_init( &entropy );
  100. mbedtls_pk_init( &pkey );
  101. mbedtls_x509_crt_init( &srvcert );
  102. mbedtls_ctr_drbg_init( &ctr_drbg );
  103. signal( SIGCHLD, SIG_IGN );
  104. /*
  105. * 0. Initial seeding of the RNG
  106. */
  107. mbedtls_printf( "\n . Initial seeding of the random generator..." );
  108. fflush( stdout );
  109. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  110. (const unsigned char *) pers,
  111. strlen( pers ) ) ) != 0 )
  112. {
  113. mbedtls_printf( " failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret );
  114. goto exit;
  115. }
  116. mbedtls_printf( " ok\n" );
  117. /*
  118. * 1. Load the certificates and private RSA key
  119. */
  120. mbedtls_printf( " . Loading the server cert. and key..." );
  121. fflush( stdout );
  122. /*
  123. * This demonstration program uses embedded test certificates.
  124. * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
  125. * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
  126. */
  127. ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
  128. mbedtls_test_srv_crt_len );
  129. if( ret != 0 )
  130. {
  131. mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
  132. goto exit;
  133. }
  134. ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
  135. mbedtls_test_cas_pem_len );
  136. if( ret != 0 )
  137. {
  138. mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
  139. goto exit;
  140. }
  141. ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
  142. mbedtls_test_srv_key_len, NULL, 0,
  143. mbedtls_ctr_drbg_random, &ctr_drbg );
  144. if( ret != 0 )
  145. {
  146. mbedtls_printf( " failed! mbedtls_pk_parse_key returned %d\n\n", ret );
  147. goto exit;
  148. }
  149. mbedtls_printf( " ok\n" );
  150. /*
  151. * 1b. Prepare SSL configuration
  152. */
  153. mbedtls_printf( " . Configuring SSL..." );
  154. fflush( stdout );
  155. if( ( ret = mbedtls_ssl_config_defaults( &conf,
  156. MBEDTLS_SSL_IS_SERVER,
  157. MBEDTLS_SSL_TRANSPORT_STREAM,
  158. MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
  159. {
  160. mbedtls_printf( " failed! mbedtls_ssl_config_defaults returned %d\n\n", ret );
  161. goto exit;
  162. }
  163. mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
  164. mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
  165. mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
  166. if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
  167. {
  168. mbedtls_printf( " failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
  169. goto exit;
  170. }
  171. mbedtls_printf( " ok\n" );
  172. /*
  173. * 2. Setup the listening TCP socket
  174. */
  175. mbedtls_printf( " . Bind on https://localhost:4433/ ..." );
  176. fflush( stdout );
  177. if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
  178. {
  179. mbedtls_printf( " failed! mbedtls_net_bind returned %d\n\n", ret );
  180. goto exit;
  181. }
  182. mbedtls_printf( " ok\n" );
  183. while( 1 )
  184. {
  185. /*
  186. * 3. Wait until a client connects
  187. */
  188. mbedtls_net_init( &client_fd );
  189. mbedtls_ssl_init( &ssl );
  190. mbedtls_printf( " . Waiting for a remote connection ...\n" );
  191. fflush( stdout );
  192. if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
  193. NULL, 0, NULL ) ) != 0 )
  194. {
  195. mbedtls_printf( " failed! mbedtls_net_accept returned %d\n\n", ret );
  196. goto exit;
  197. }
  198. /*
  199. * 3.5. Forking server thread
  200. */
  201. mbedtls_printf( " . Forking to handle connection ..." );
  202. fflush( stdout );
  203. pid = fork();
  204. if( pid < 0 )
  205. {
  206. mbedtls_printf(" failed! fork returned %d\n\n", pid );
  207. goto exit;
  208. }
  209. if( pid != 0 )
  210. {
  211. mbedtls_printf( " ok\n" );
  212. mbedtls_net_close( &client_fd );
  213. if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
  214. (const unsigned char *) "parent",
  215. 6 ) ) != 0 )
  216. {
  217. mbedtls_printf( " failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret );
  218. goto exit;
  219. }
  220. continue;
  221. }
  222. mbedtls_net_close( &listen_fd );
  223. pid = getpid();
  224. /*
  225. * 4. Setup stuff
  226. */
  227. mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
  228. fflush( stdout );
  229. if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
  230. (const unsigned char *) "child",
  231. 5 ) ) != 0 )
  232. {
  233. mbedtls_printf(
  234. "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
  235. pid, ret );
  236. goto exit;
  237. }
  238. if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
  239. {
  240. mbedtls_printf(
  241. "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
  242. pid, ret );
  243. goto exit;
  244. }
  245. mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
  246. mbedtls_printf( "pid %d: SSL setup ok\n", pid );
  247. /*
  248. * 5. Handshake
  249. */
  250. mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
  251. fflush( stdout );
  252. while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
  253. {
  254. if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  255. {
  256. mbedtls_printf(
  257. "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
  258. pid, ret );
  259. goto exit;
  260. }
  261. }
  262. mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
  263. /*
  264. * 6. Read the HTTP Request
  265. */
  266. mbedtls_printf( "pid %d: Start reading from client.\n", pid );
  267. fflush( stdout );
  268. do
  269. {
  270. len = sizeof( buf ) - 1;
  271. memset( buf, 0, sizeof( buf ) );
  272. ret = mbedtls_ssl_read( &ssl, buf, len );
  273. if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
  274. continue;
  275. if( ret <= 0 )
  276. {
  277. switch( ret )
  278. {
  279. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  280. mbedtls_printf( "pid %d: connection was closed gracefully\n", pid );
  281. break;
  282. case MBEDTLS_ERR_NET_CONN_RESET:
  283. mbedtls_printf( "pid %d: connection was reset by peer\n", pid );
  284. break;
  285. default:
  286. mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
  287. break;
  288. }
  289. break;
  290. }
  291. len = ret;
  292. mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf );
  293. if( ret > 0 )
  294. break;
  295. }
  296. while( 1 );
  297. /*
  298. * 7. Write the 200 Response
  299. */
  300. mbedtls_printf( "pid %d: Start writing to client.\n", pid );
  301. fflush( stdout );
  302. len = sprintf( (char *) buf, HTTP_RESPONSE,
  303. mbedtls_ssl_get_ciphersuite( &ssl ) );
  304. while( cnt++ < 100 )
  305. {
  306. while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
  307. {
  308. if( ret == MBEDTLS_ERR_NET_CONN_RESET )
  309. {
  310. mbedtls_printf(
  311. "pid %d: Write failed! peer closed the connection\n\n", pid );
  312. goto exit;
  313. }
  314. if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  315. {
  316. mbedtls_printf(
  317. "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
  318. pid, ret );
  319. goto exit;
  320. }
  321. }
  322. len = ret;
  323. mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf );
  324. mbedtls_net_usleep( 1000000 );
  325. }
  326. mbedtls_ssl_close_notify( &ssl );
  327. goto exit;
  328. }
  329. exit_code = MBEDTLS_EXIT_SUCCESS;
  330. exit:
  331. mbedtls_net_free( &client_fd );
  332. mbedtls_net_free( &listen_fd );
  333. mbedtls_x509_crt_free( &srvcert );
  334. mbedtls_pk_free( &pkey );
  335. mbedtls_ssl_free( &ssl );
  336. mbedtls_ssl_config_free( &conf );
  337. mbedtls_ctr_drbg_free( &ctr_drbg );
  338. mbedtls_entropy_free( &entropy );
  339. #if defined(_WIN32)
  340. mbedtls_printf( " Press Enter to exit this program.\n" );
  341. fflush( stdout ); getchar();
  342. #endif
  343. mbedtls_exit( exit_code );
  344. }
  345. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
  346. MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
  347. MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
  348. ! _WIN32 */