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