Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 1 | /* |
Paul Bakker | cb79ae0b | 2011-05-20 12:44:16 +0000 | [diff] [blame] | 2 | * SSL server demonstration program using fork() for handling multiple clients |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 8 | #include "mbedtls/build_info.h" |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 9 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 10 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 11 | |
Gilles Peskine | c83e56c | 2024-09-04 17:47:14 +0200 | [diff] [blame^] | 12 | #if !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \ |
| 13 | !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_SSL_SRV_C) || \ |
| 14 | !defined(MBEDTLS_PEM_PARSE_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) |
| 15 | int main(void) |
Paul Bakker | b892b13 | 2011-10-12 09:19:43 +0000 | [diff] [blame] | 16 | { |
Gilles Peskine | c83e56c | 2024-09-04 17:47:14 +0200 | [diff] [blame^] | 17 | mbedtls_printf("MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or " |
| 18 | "MBEDTLS_NET_C and/or MBEDTLS_SSL_SRV_C and/or " |
| 19 | "MBEDTLS_PEM_PARSE_C and/or MBEDTLS_X509_CRT_PARSE_C " |
| 20 | "not defined.\n"); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 21 | mbedtls_exit(0); |
Paul Bakker | b892b13 | 2011-10-12 09:19:43 +0000 | [diff] [blame] | 22 | } |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 23 | #elif defined(_WIN32) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 24 | int main(void) |
Paul Bakker | b892b13 | 2011-10-12 09:19:43 +0000 | [diff] [blame] | 25 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | mbedtls_printf("_WIN32 defined. This application requires fork() and signals " |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 27 | "to work correctly.\n"); |
| 28 | mbedtls_exit(0); |
Paul Bakker | b892b13 | 2011-10-12 09:19:43 +0000 | [diff] [blame] | 29 | } |
| 30 | #else |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | 4b3e5ef | 2015-03-27 11:24:27 +0100 | [diff] [blame] | 32 | #include "mbedtls/entropy.h" |
| 33 | #include "mbedtls/ctr_drbg.h" |
Mateusz Starzyk | 1aec646 | 2021-02-08 15:34:42 +0100 | [diff] [blame] | 34 | #include "test/certs.h" |
Manuel Pégourié-Gonnard | 4b3e5ef | 2015-03-27 11:24:27 +0100 | [diff] [blame] | 35 | #include "mbedtls/x509.h" |
| 36 | #include "mbedtls/ssl.h" |
Andres AG | 788aa4a | 2016-09-14 14:32:09 +0100 | [diff] [blame] | 37 | #include "mbedtls/net_sockets.h" |
Manuel Pégourié-Gonnard | 4b3e5ef | 2015-03-27 11:24:27 +0100 | [diff] [blame] | 38 | #include "mbedtls/timing.h" |
| 39 | |
| 40 | #include <string.h> |
| 41 | #include <signal.h> |
| 42 | |
| 43 | #if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32) |
| 44 | #include <unistd.h> |
| 45 | #endif |
| 46 | |
| 47 | #define HTTP_RESPONSE \ |
| 48 | "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \ |
Gilles Peskine | e820c0a | 2023-08-03 17:45:20 +0200 | [diff] [blame] | 49 | "<h2>Mbed TLS Test Server</h2>\r\n" \ |
Manuel Pégourié-Gonnard | 4b3e5ef | 2015-03-27 11:24:27 +0100 | [diff] [blame] | 50 | "<p>Successful connection using: %s</p>\r\n" |
| 51 | |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 52 | #define DEBUG_LEVEL 0 |
| 53 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 54 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 55 | static void my_debug(void *ctx, int level, |
| 56 | const char *file, int line, |
| 57 | const char *str) |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 58 | { |
Manuel Pégourié-Gonnard | 61ee351 | 2015-06-23 17:35:03 +0200 | [diff] [blame] | 59 | ((void) level); |
| 60 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 61 | mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str); |
| 62 | fflush((FILE *) ctx); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 65 | int main(void) |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 66 | { |
Andres Amaya Garcia | 4be53b5 | 2018-04-29 20:57:21 +0100 | [diff] [blame] | 67 | int ret = 1, len, cnt = 0, pid; |
| 68 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 69 | mbedtls_net_context listen_fd, client_fd; |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 70 | unsigned char buf[1024]; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 71 | const char *pers = "ssl_fork_server"; |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 72 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 73 | mbedtls_entropy_context entropy; |
| 74 | mbedtls_ctr_drbg_context ctr_drbg; |
| 75 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 76 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 77 | mbedtls_x509_crt srvcert; |
| 78 | mbedtls_pk_context pkey; |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 79 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | mbedtls_net_init(&listen_fd); |
| 81 | mbedtls_net_init(&client_fd); |
| 82 | mbedtls_ssl_init(&ssl); |
| 83 | mbedtls_ssl_config_init(&conf); |
| 84 | mbedtls_entropy_init(&entropy); |
| 85 | mbedtls_pk_init(&pkey); |
| 86 | mbedtls_x509_crt_init(&srvcert); |
| 87 | mbedtls_ctr_drbg_init(&ctr_drbg); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 88 | |
Przemek Stekiel | a0a1c1e | 2023-04-17 11:10:05 +0200 | [diff] [blame] | 89 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 90 | psa_status_t status = psa_crypto_init(); |
| 91 | if (status != PSA_SUCCESS) { |
| 92 | mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n", |
| 93 | (int) status); |
| 94 | goto exit; |
| 95 | } |
| 96 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 97 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 98 | signal(SIGCHLD, SIG_IGN); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 99 | |
| 100 | /* |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 101 | * 0. Initial seeding of the RNG |
| 102 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 103 | mbedtls_printf("\n . Initial seeding of the random generator..."); |
| 104 | fflush(stdout); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 106 | if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, |
| 107 | (const unsigned char *) pers, |
| 108 | strlen(pers))) != 0) { |
| 109 | mbedtls_printf(" failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 110 | goto exit; |
| 111 | } |
| 112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 113 | mbedtls_printf(" ok\n"); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 114 | |
| 115 | /* |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 116 | * 1. Load the certificates and private RSA key |
| 117 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 118 | mbedtls_printf(" . Loading the server cert. and key..."); |
| 119 | fflush(stdout); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 120 | |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 121 | /* |
| 122 | * This demonstration program uses embedded test certificates. |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 123 | * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the |
| 124 | * server and CA certificates, as well as mbedtls_pk_parse_keyfile(). |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 125 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 126 | ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_srv_crt, |
| 127 | mbedtls_test_srv_crt_len); |
| 128 | if (ret != 0) { |
| 129 | mbedtls_printf(" failed! mbedtls_x509_crt_parse returned %d\n\n", ret); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 130 | goto exit; |
| 131 | } |
| 132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_cas_pem, |
| 134 | mbedtls_test_cas_pem_len); |
| 135 | if (ret != 0) { |
| 136 | mbedtls_printf(" failed! mbedtls_x509_crt_parse returned %d\n\n", ret); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 137 | goto exit; |
| 138 | } |
| 139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 140 | ret = mbedtls_pk_parse_key(&pkey, (const unsigned char *) mbedtls_test_srv_key, |
| 141 | mbedtls_test_srv_key_len, NULL, 0, |
| 142 | mbedtls_ctr_drbg_random, &ctr_drbg); |
| 143 | if (ret != 0) { |
| 144 | mbedtls_printf(" failed! mbedtls_pk_parse_key returned %d\n\n", ret); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 145 | goto exit; |
| 146 | } |
| 147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 148 | mbedtls_printf(" ok\n"); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 149 | |
| 150 | /* |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 151 | * 1b. Prepare SSL configuration |
| 152 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 153 | mbedtls_printf(" . Configuring SSL..."); |
| 154 | fflush(stdout); |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 156 | if ((ret = mbedtls_ssl_config_defaults(&conf, |
| 157 | MBEDTLS_SSL_IS_SERVER, |
| 158 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 159 | MBEDTLS_SSL_PRESET_DEFAULT)) != 0) { |
| 160 | mbedtls_printf(" failed! mbedtls_ssl_config_defaults returned %d\n\n", ret); |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 161 | goto exit; |
| 162 | } |
| 163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 164 | mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg); |
| 165 | mbedtls_ssl_conf_dbg(&conf, my_debug, stdout); |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 167 | mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, NULL); |
| 168 | if ((ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey)) != 0) { |
| 169 | mbedtls_printf(" failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret); |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 170 | goto exit; |
| 171 | } |
| 172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 173 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 174 | |
| 175 | /* |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 176 | * 2. Setup the listening TCP socket |
| 177 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 178 | mbedtls_printf(" . Bind on https://localhost:4433/ ..."); |
| 179 | fflush(stdout); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 181 | if ((ret = mbedtls_net_bind(&listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP)) != 0) { |
| 182 | mbedtls_printf(" failed! mbedtls_net_bind returned %d\n\n", ret); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 183 | goto exit; |
| 184 | } |
| 185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 186 | mbedtls_printf(" ok\n"); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 188 | while (1) { |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 189 | /* |
| 190 | * 3. Wait until a client connects |
| 191 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 192 | mbedtls_net_init(&client_fd); |
| 193 | mbedtls_ssl_init(&ssl); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | mbedtls_printf(" . Waiting for a remote connection ...\n"); |
| 196 | fflush(stdout); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 198 | if ((ret = mbedtls_net_accept(&listen_fd, &client_fd, |
| 199 | NULL, 0, NULL)) != 0) { |
| 200 | mbedtls_printf(" failed! mbedtls_net_accept returned %d\n\n", ret); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 201 | goto exit; |
| 202 | } |
| 203 | |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 204 | /* |
| 205 | * 3.5. Forking server thread |
| 206 | */ |
| 207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 208 | mbedtls_printf(" . Forking to handle connection ..."); |
| 209 | fflush(stdout); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 210 | |
Janos Follath | 582a461 | 2016-04-28 23:37:16 +0100 | [diff] [blame] | 211 | pid = fork(); |
| 212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 213 | if (pid < 0) { |
| 214 | mbedtls_printf(" failed! fork returned %d\n\n", pid); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 215 | goto exit; |
| 216 | } |
| 217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 218 | if (pid != 0) { |
| 219 | mbedtls_printf(" ok\n"); |
| 220 | mbedtls_net_close(&client_fd); |
Gilles Peskine | c83e56c | 2024-09-04 17:47:14 +0200 | [diff] [blame^] | 221 | fflush(stdout); |
Janos Follath | 582a461 | 2016-04-28 23:37:16 +0100 | [diff] [blame] | 222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 223 | if ((ret = mbedtls_ctr_drbg_reseed(&ctr_drbg, |
| 224 | (const unsigned char *) "parent", |
| 225 | 6)) != 0) { |
| 226 | mbedtls_printf(" failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 227 | goto exit; |
| 228 | } |
| 229 | |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 230 | continue; |
| 231 | } |
| 232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 233 | mbedtls_net_close(&listen_fd); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 234 | |
Janos Follath | 582a461 | 2016-04-28 23:37:16 +0100 | [diff] [blame] | 235 | pid = getpid(); |
| 236 | |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 237 | /* |
| 238 | * 4. Setup stuff |
| 239 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 240 | mbedtls_printf("pid %d: Setting up the SSL data.\n", pid); |
| 241 | fflush(stdout); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 243 | if ((ret = mbedtls_ctr_drbg_reseed(&ctr_drbg, |
| 244 | (const unsigned char *) "child", |
| 245 | 5)) != 0) { |
Janos Follath | 582a461 | 2016-04-28 23:37:16 +0100 | [diff] [blame] | 246 | mbedtls_printf( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 247 | "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n", |
| 248 | pid, ret); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 249 | goto exit; |
| 250 | } |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 252 | if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) { |
Janos Follath | 582a461 | 2016-04-28 23:37:16 +0100 | [diff] [blame] | 253 | mbedtls_printf( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n", |
| 255 | pid, ret); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 256 | goto exit; |
| 257 | } |
| 258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 259 | mbedtls_ssl_set_bio(&ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 261 | mbedtls_printf("pid %d: SSL setup ok\n", pid); |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 262 | |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 263 | /* |
| 264 | * 5. Handshake |
| 265 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 266 | mbedtls_printf("pid %d: Performing the SSL/TLS handshake.\n", pid); |
| 267 | fflush(stdout); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 269 | while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) { |
| 270 | if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { |
Janos Follath | 582a461 | 2016-04-28 23:37:16 +0100 | [diff] [blame] | 271 | mbedtls_printf( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 272 | "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n", |
| 273 | pid, ret); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 274 | goto exit; |
| 275 | } |
| 276 | } |
| 277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 278 | mbedtls_printf("pid %d: SSL handshake ok\n", pid); |
Gilles Peskine | c83e56c | 2024-09-04 17:47:14 +0200 | [diff] [blame^] | 279 | fflush(stdout); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 280 | |
| 281 | /* |
| 282 | * 6. Read the HTTP Request |
| 283 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | mbedtls_printf("pid %d: Start reading from client.\n", pid); |
| 285 | fflush(stdout); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 287 | do { |
| 288 | len = sizeof(buf) - 1; |
| 289 | memset(buf, 0, sizeof(buf)); |
| 290 | ret = mbedtls_ssl_read(&ssl, buf, len); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 292 | if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) { |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 293 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 294 | } |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 296 | if (ret <= 0) { |
| 297 | switch (ret) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 298 | case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 299 | mbedtls_printf("pid %d: connection was closed gracefully\n", pid); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 300 | break; |
| 301 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 302 | case MBEDTLS_ERR_NET_CONN_RESET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | mbedtls_printf("pid %d: connection was reset by peer\n", pid); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 304 | break; |
| 305 | |
| 306 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 307 | mbedtls_printf("pid %d: mbedtls_ssl_read returned %d\n", pid, ret); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 308 | break; |
| 309 | } |
Gilles Peskine | c83e56c | 2024-09-04 17:47:14 +0200 | [diff] [blame^] | 310 | fflush(stdout); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 311 | |
| 312 | break; |
| 313 | } |
| 314 | |
| 315 | len = ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 316 | mbedtls_printf("pid %d: %d bytes read\n\n%s", pid, len, (char *) buf); |
Gilles Peskine | c83e56c | 2024-09-04 17:47:14 +0200 | [diff] [blame^] | 317 | fflush(stdout); |
Manuel Pégourié-Gonnard | 401caad | 2015-02-14 15:53:24 +0000 | [diff] [blame] | 318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 319 | if (ret > 0) { |
Manuel Pégourié-Gonnard | 401caad | 2015-02-14 15:53:24 +0000 | [diff] [blame] | 320 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 321 | } |
| 322 | } while (1); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 323 | |
| 324 | /* |
| 325 | * 7. Write the 200 Response |
| 326 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 327 | mbedtls_printf("pid %d: Start writing to client.\n", pid); |
| 328 | fflush(stdout); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 330 | len = sprintf((char *) buf, HTTP_RESPONSE, |
| 331 | mbedtls_ssl_get_ciphersuite(&ssl)); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 332 | |
Gilles Peskine | c83e56c | 2024-09-04 17:47:14 +0200 | [diff] [blame^] | 333 | while (cnt++ < 10) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 334 | while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) { |
| 335 | if (ret == MBEDTLS_ERR_NET_CONN_RESET) { |
Janos Follath | 582a461 | 2016-04-28 23:37:16 +0100 | [diff] [blame] | 336 | mbedtls_printf( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 337 | "pid %d: Write failed! peer closed the connection\n\n", pid); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 338 | goto exit; |
| 339 | } |
| 340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 341 | if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { |
Janos Follath | 582a461 | 2016-04-28 23:37:16 +0100 | [diff] [blame] | 342 | mbedtls_printf( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 343 | "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n", |
| 344 | pid, ret); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 345 | goto exit; |
| 346 | } |
| 347 | } |
| 348 | len = ret; |
Gilles Peskine | c83e56c | 2024-09-04 17:47:14 +0200 | [diff] [blame^] | 349 | mbedtls_printf("pid %d: %d bytes written (cnt=%d)\n\n%s\n", |
| 350 | pid, len, cnt, (char *) buf); |
| 351 | fflush(stdout); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 353 | mbedtls_net_usleep(1000000); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 356 | mbedtls_ssl_close_notify(&ssl); |
Gilles Peskine | c83e56c | 2024-09-04 17:47:14 +0200 | [diff] [blame^] | 357 | mbedtls_printf("pid %d: shutting down\n", pid); |
| 358 | fflush(stdout); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 359 | goto exit; |
| 360 | } |
| 361 | |
Andres Amaya Garcia | 4be53b5 | 2018-04-29 20:57:21 +0100 | [diff] [blame] | 362 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 363 | |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 364 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 365 | mbedtls_net_free(&client_fd); |
| 366 | mbedtls_net_free(&listen_fd); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 367 | mbedtls_x509_crt_free(&srvcert); |
| 368 | mbedtls_pk_free(&pkey); |
| 369 | mbedtls_ssl_free(&ssl); |
| 370 | mbedtls_ssl_config_free(&conf); |
| 371 | mbedtls_ctr_drbg_free(&ctr_drbg); |
| 372 | mbedtls_entropy_free(&entropy); |
Przemek Stekiel | 758aef6 | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 373 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemek Stekiel | a8c560a | 2023-04-19 10:15:26 +0200 | [diff] [blame] | 374 | mbedtls_psa_crypto_free(); |
Przemek Stekiel | 758aef6 | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 375 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | mbedtls_exit(exit_code); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 378 | } |
Mateusz Starzyk | 1aec646 | 2021-02-08 15:34:42 +0100 | [diff] [blame] | 379 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 380 | MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && |
| 381 | MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C && |
Manuel Pégourié-Gonnard | 4b3e5ef | 2015-03-27 11:24:27 +0100 | [diff] [blame] | 382 | ! _WIN32 */ |