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