blob: 363f38f73d1301d65b758088a235f019a95a7dc9 [file] [log] [blame]
Paul Bakker896ac222011-05-20 12:33:05 +00001/*
Paul Bakkercb79ae0b2011-05-20 12:44:16 +00002 * SSL server demonstration program using fork() for handling multiple clients
Paul Bakker896ac222011-05-20 12:33:05 +00003 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakker896ac222011-05-20 12:33:05 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker896ac222011-05-20 12:33:05 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker896ac222011-05-20 12:33:05 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_fprintf fprintf
33#define mbedtls_printf printf
-~- redtangent ~-~9fa2e862016-05-26 10:07:49 +010034#define mbedtls_time_t time_t
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_CERTS_C) || \
38 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_SSL_TLS_C) || \
39 !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_NET_C) || \
40 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
41 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_TIMING_C) || \
42 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_PEM_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000043int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000044{
Paul Bakkercce9d772011-11-18 14:26:47 +000045 ((void) argc);
46 ((void) argv);
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_CERTS_C and/or MBEDTLS_ENTROPY_C "
49 "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
50 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
51 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
52 "MBEDTLS_TIMING_C and/or MBEDTLS_PEM_PARSE_C not defined.\n");
Paul Bakkerb892b132011-10-12 09:19:43 +000053 return( 0 );
54}
Paul Bakkercce9d772011-11-18 14:26:47 +000055#elif defined(_WIN32)
Rich Evans85b05ec2015-02-12 11:37:29 +000056int main( void )
Paul Bakkerb892b132011-10-12 09:19:43 +000057{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
Paul Bakkerb892b132011-10-12 09:19:43 +000059 "to work correctly.\n");
60 return( 0 );
61}
62#else
Paul Bakker896ac222011-05-20 12:33:05 +000063
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010064#include "mbedtls/entropy.h"
65#include "mbedtls/ctr_drbg.h"
66#include "mbedtls/certs.h"
67#include "mbedtls/x509.h"
68#include "mbedtls/ssl.h"
69#include "mbedtls/net.h"
70#include "mbedtls/timing.h"
71
72#include <string.h>
73#include <signal.h>
74
75#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
76#include <unistd.h>
77#endif
78
79#define HTTP_RESPONSE \
80 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
81 "<h2>mbed TLS Test Server</h2>\r\n" \
82 "<p>Successful connection using: %s</p>\r\n"
83
Paul Bakker896ac222011-05-20 12:33:05 +000084#define DEBUG_LEVEL 0
85
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020086static void my_debug( void *ctx, int level,
87 const char *file, int line,
88 const char *str )
Paul Bakker896ac222011-05-20 12:33:05 +000089{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020090 ((void) level);
91
92 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
93 fflush( (FILE *) ctx );
Paul Bakker896ac222011-05-20 12:33:05 +000094}
95
Rich Evans85b05ec2015-02-12 11:37:29 +000096int main( void )
Paul Bakker896ac222011-05-20 12:33:05 +000097{
98 int ret, len, cnt = 0, pid;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020099 mbedtls_net_context listen_fd, client_fd;
Paul Bakker896ac222011-05-20 12:33:05 +0000100 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200101 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +0000102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_entropy_context entropy;
104 mbedtls_ctr_drbg_context ctr_drbg;
105 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200106 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_x509_crt srvcert;
108 mbedtls_pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +0000109
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200110 mbedtls_net_init( &listen_fd );
111 mbedtls_net_init( &client_fd );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200112 mbedtls_ssl_init( &ssl );
113 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_entropy_init( &entropy );
115 mbedtls_pk_init( &pkey );
116 mbedtls_x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200117 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker0c226102014-04-17 16:02:36 +0200118
Paul Bakker896ac222011-05-20 12:33:05 +0000119 signal( SIGCHLD, SIG_IGN );
120
121 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000122 * 0. Initial seeding of the RNG
123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_printf( "\n . Initial seeding of the random generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000125 fflush( stdout );
126
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200127 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200128 (const unsigned char *) pers,
129 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000130 {
Janos Follath582a4612016-04-28 23:37:16 +0100131 mbedtls_printf( " failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000132 goto exit;
133 }
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_printf( " ok\n" );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000136
137 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000138 * 1. Load the certificates and private RSA key
139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000141 fflush( stdout );
142
Paul Bakker896ac222011-05-20 12:33:05 +0000143 /*
144 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
146 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
149 mbedtls_test_srv_crt_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000150 if( ret != 0 )
151 {
Janos Follath582a4612016-04-28 23:37:16 +0100152 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000153 goto exit;
154 }
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
157 mbedtls_test_cas_pem_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000158 if( ret != 0 )
159 {
Janos Follath582a4612016-04-28 23:37:16 +0100160 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000161 goto exit;
162 }
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
165 mbedtls_test_srv_key_len, NULL, 0 );
Paul Bakker896ac222011-05-20 12:33:05 +0000166 if( ret != 0 )
167 {
Janos Follath582a4612016-04-28 23:37:16 +0100168 mbedtls_printf( " failed! mbedtls_pk_parse_key returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000169 goto exit;
170 }
171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000173
174 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200175 * 1b. Prepare SSL configuration
176 */
177 mbedtls_printf( " . Configuring SSL..." );
178 fflush( stdout );
179
180 if( ( ret = mbedtls_ssl_config_defaults( &conf,
181 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200182 MBEDTLS_SSL_TRANSPORT_STREAM,
183 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200184 {
Janos Follath582a4612016-04-28 23:37:16 +0100185 mbedtls_printf( " failed! mbedtls_ssl_config_defaults returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200186 goto exit;
187 }
188
189 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
190 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
191
192 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
193 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
194 {
Janos Follath582a4612016-04-28 23:37:16 +0100195 mbedtls_printf( " failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200196 goto exit;
197 }
198
199 mbedtls_printf( " ok\n" );
200
201 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000202 * 2. Setup the listening TCP socket
203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_printf( " . Bind on https://localhost:4433/ ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000205 fflush( stdout );
206
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200207 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000208 {
Janos Follath582a4612016-04-28 23:37:16 +0100209 mbedtls_printf( " failed! mbedtls_net_bind returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000210 goto exit;
211 }
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000214
215 while( 1 )
216 {
217 /*
218 * 3. Wait until a client connects
219 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200220 mbedtls_net_init( &client_fd );
221 mbedtls_ssl_init( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000222
Janos Follath582a4612016-04-28 23:37:16 +0100223 mbedtls_printf( " . Waiting for a remote connection ...\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000224 fflush( stdout );
225
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200226 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200227 NULL, 0, NULL ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000228 {
Janos Follath582a4612016-04-28 23:37:16 +0100229 mbedtls_printf( " failed! mbedtls_net_accept returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000230 goto exit;
231 }
232
Paul Bakker896ac222011-05-20 12:33:05 +0000233 /*
234 * 3.5. Forking server thread
235 */
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_printf( " . Forking to handle connection ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000238 fflush( stdout );
239
Janos Follath582a4612016-04-28 23:37:16 +0100240 pid = fork();
241
Paul Bakker896ac222011-05-20 12:33:05 +0000242 if( pid < 0 )
243 {
Janos Follath582a4612016-04-28 23:37:16 +0100244 mbedtls_printf(" failed! fork returned %d\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000245 goto exit;
246 }
247
Paul Bakker896ac222011-05-20 12:33:05 +0000248 if( pid != 0 )
249 {
Janos Follath582a4612016-04-28 23:37:16 +0100250 mbedtls_printf( " ok\n" );
251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200253 (const unsigned char *) "parent",
254 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000255 {
Janos Follath582a4612016-04-28 23:37:16 +0100256 mbedtls_printf( " failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000257 goto exit;
258 }
259
Paul Bakker896ac222011-05-20 12:33:05 +0000260 continue;
261 }
262
Janos Follathfe049db2016-03-31 11:37:33 +0100263 mbedtls_net_init( &listen_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000264
Janos Follath582a4612016-04-28 23:37:16 +0100265 pid = getpid();
266
Paul Bakker896ac222011-05-20 12:33:05 +0000267 /*
268 * 4. Setup stuff
269 */
Janos Follath582a4612016-04-28 23:37:16 +0100270 mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000271 fflush( stdout );
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200274 (const unsigned char *) "child",
275 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000276 {
Janos Follath582a4612016-04-28 23:37:16 +0100277 mbedtls_printf(
278 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
279 pid, ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000280 goto exit;
281 }
Paul Bakker0c226102014-04-17 16:02:36 +0200282
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200283 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
284 {
Janos Follath582a4612016-04-28 23:37:16 +0100285 mbedtls_printf(
286 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
287 pid, ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200288 goto exit;
289 }
290
291 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
292
Janos Follath582a4612016-04-28 23:37:16 +0100293 mbedtls_printf( "pid %d: SSL setup ok\n", pid );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200294
Paul Bakker896ac222011-05-20 12:33:05 +0000295 /*
296 * 5. Handshake
297 */
Janos Follath582a4612016-04-28 23:37:16 +0100298 mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000299 fflush( stdout );
300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000302 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100303 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000304 {
Janos Follath582a4612016-04-28 23:37:16 +0100305 mbedtls_printf(
306 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
307 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000308 goto exit;
309 }
310 }
311
Janos Follath582a4612016-04-28 23:37:16 +0100312 mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000313
314 /*
315 * 6. Read the HTTP Request
316 */
Janos Follath582a4612016-04-28 23:37:16 +0100317 mbedtls_printf( "pid %d: Start reading from client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000318 fflush( stdout );
319
320 do
321 {
322 len = sizeof( buf ) - 1;
323 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 ret = mbedtls_ssl_read( &ssl, buf, len );
Paul Bakker896ac222011-05-20 12:33:05 +0000325
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100326 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000327 continue;
328
329 if( ret <= 0 )
330 {
331 switch( ret )
332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Janos Follath582a4612016-04-28 23:37:16 +0100334 mbedtls_printf( "pid %d: connection was closed gracefully\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000335 break;
336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 case MBEDTLS_ERR_NET_CONN_RESET:
Janos Follath582a4612016-04-28 23:37:16 +0100338 mbedtls_printf( "pid %d: connection was reset by peer\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000339 break;
340
341 default:
Janos Follath582a4612016-04-28 23:37:16 +0100342 mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000343 break;
344 }
345
346 break;
347 }
348
349 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100350 mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf );
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000351
352 if( ret > 0 )
353 break;
Paul Bakker896ac222011-05-20 12:33:05 +0000354 }
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000355 while( 1 );
Paul Bakker896ac222011-05-20 12:33:05 +0000356
357 /*
358 * 7. Write the 200 Response
359 */
Janos Follath582a4612016-04-28 23:37:16 +0100360 mbedtls_printf( "pid %d: Start writing to client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000361 fflush( stdout );
362
363 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000365
Paul Bakker030decd2014-04-17 16:03:23 +0200366 while( cnt++ < 100 )
Paul Bakker896ac222011-05-20 12:33:05 +0000367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Paul Bakker896ac222011-05-20 12:33:05 +0000371 {
Janos Follath582a4612016-04-28 23:37:16 +0100372 mbedtls_printf(
373 "pid %d: Write failed! peer closed the connection\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000374 goto exit;
375 }
376
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100377 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000378 {
Janos Follath582a4612016-04-28 23:37:16 +0100379 mbedtls_printf(
380 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
381 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000382 goto exit;
383 }
384 }
385 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100386 mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf );
Paul Bakker896ac222011-05-20 12:33:05 +0000387
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200388 mbedtls_net_usleep( 1000000 );
Paul Bakker896ac222011-05-20 12:33:05 +0000389 }
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_ssl_close_notify( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000392 goto exit;
393 }
394
395exit:
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200396 mbedtls_net_free( &client_fd );
397 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +0200398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_x509_crt_free( &srvcert );
400 mbedtls_pk_free( &pkey );
401 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200402 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_ctr_drbg_free( &ctr_drbg );
404 mbedtls_entropy_free( &entropy );
Paul Bakker896ac222011-05-20 12:33:05 +0000405
Paul Bakkercce9d772011-11-18 14:26:47 +0000406#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000408 fflush( stdout ); getchar();
409#endif
410
411 return( ret );
412}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
414 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
415 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100416 ! _WIN32 */