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