ssl_fork_server.c 12 KB

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