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