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