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