blob: 545e2fbf39f17794d3cfa6e89bc36d968c3cc08b [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
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#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 Bakkercce9d772011-11-18 14:26:47 +000042int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000043{
Paul Bakkercce9d772011-11-18 14:26:47 +000044 ((void) argc);
45 ((void) argv);
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 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 Bakkerb892b132011-10-12 09:19:43 +000052 return( 0 );
53}
Paul Bakkercce9d772011-11-18 14:26:47 +000054#elif defined(_WIN32)
Rich Evans85b05ec2015-02-12 11:37:29 +000055int main( void )
Paul Bakkerb892b132011-10-12 09:19:43 +000056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
Paul Bakkerb892b132011-10-12 09:19:43 +000058 "to work correctly.\n");
59 return( 0 );
60}
61#else
Paul Bakker896ac222011-05-20 12:33:05 +000062
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010063#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 Bakker896ac222011-05-20 12:33:05 +000083#define DEBUG_LEVEL 0
84
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020085static void my_debug( void *ctx, int level,
86 const char *file, int line,
87 const char *str )
Paul Bakker896ac222011-05-20 12:33:05 +000088{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020089 ((void) level);
90
91 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
92 fflush( (FILE *) ctx );
Paul Bakker896ac222011-05-20 12:33:05 +000093}
94
Rich Evans85b05ec2015-02-12 11:37:29 +000095int main( void )
Paul Bakker896ac222011-05-20 12:33:05 +000096{
97 int ret, len, cnt = 0, pid;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020098 mbedtls_net_context listen_fd, client_fd;
Paul Bakker896ac222011-05-20 12:33:05 +000099 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200100 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +0000101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_entropy_context entropy;
103 mbedtls_ctr_drbg_context ctr_drbg;
104 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200105 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 mbedtls_x509_crt srvcert;
107 mbedtls_pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +0000108
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200109 mbedtls_net_init( &listen_fd );
110 mbedtls_net_init( &client_fd );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200111 mbedtls_ssl_init( &ssl );
112 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_entropy_init( &entropy );
114 mbedtls_pk_init( &pkey );
115 mbedtls_x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200116 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker0c226102014-04-17 16:02:36 +0200117
Paul Bakker896ac222011-05-20 12:33:05 +0000118 signal( SIGCHLD, SIG_IGN );
119
120 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000121 * 0. Initial seeding of the RNG
122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_printf( "\n . Initial seeding of the random generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000124 fflush( stdout );
125
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200126 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200127 (const unsigned char *) pers,
128 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000129 {
Janos Follath582a4612016-04-28 23:37:16 +0100130 mbedtls_printf( " failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000131 goto exit;
132 }
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_printf( " ok\n" );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000135
136 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000137 * 1. Load the certificates and private RSA key
138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000140 fflush( stdout );
141
Paul Bakker896ac222011-05-20 12:33:05 +0000142 /*
143 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 * 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 Bakker896ac222011-05-20 12:33:05 +0000146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
148 mbedtls_test_srv_crt_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000149 if( ret != 0 )
150 {
Janos Follath582a4612016-04-28 23:37:16 +0100151 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000152 goto exit;
153 }
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
156 mbedtls_test_cas_pem_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000157 if( ret != 0 )
158 {
Janos Follath582a4612016-04-28 23:37:16 +0100159 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000160 goto exit;
161 }
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
164 mbedtls_test_srv_key_len, NULL, 0 );
Paul Bakker896ac222011-05-20 12:33:05 +0000165 if( ret != 0 )
166 {
Janos Follath582a4612016-04-28 23:37:16 +0100167 mbedtls_printf( " failed! mbedtls_pk_parse_key returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000168 goto exit;
169 }
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000172
173 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200174 * 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é-Gonnardb31c5f62015-06-17 13:53:47 +0200181 MBEDTLS_SSL_TRANSPORT_STREAM,
182 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200183 {
Janos Follath582a4612016-04-28 23:37:16 +0100184 mbedtls_printf( " failed! mbedtls_ssl_config_defaults returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200185 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 {
Janos Follath582a4612016-04-28 23:37:16 +0100194 mbedtls_printf( " failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200195 goto exit;
196 }
197
198 mbedtls_printf( " ok\n" );
199
200 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000201 * 2. Setup the listening TCP socket
202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_printf( " . Bind on https://localhost:4433/ ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000204 fflush( stdout );
205
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200206 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000207 {
Janos Follath582a4612016-04-28 23:37:16 +0100208 mbedtls_printf( " failed! mbedtls_net_bind returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000209 goto exit;
210 }
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000213
214 while( 1 )
215 {
216 /*
217 * 3. Wait until a client connects
218 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200219 mbedtls_net_init( &client_fd );
220 mbedtls_ssl_init( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000221
Janos Follath582a4612016-04-28 23:37:16 +0100222 mbedtls_printf( " . Waiting for a remote connection ...\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000223 fflush( stdout );
224
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200225 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200226 NULL, 0, NULL ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000227 {
Janos Follath582a4612016-04-28 23:37:16 +0100228 mbedtls_printf( " failed! mbedtls_net_accept returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000229 goto exit;
230 }
231
Paul Bakker896ac222011-05-20 12:33:05 +0000232 /*
233 * 3.5. Forking server thread
234 */
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_printf( " . Forking to handle connection ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000237 fflush( stdout );
238
Janos Follath582a4612016-04-28 23:37:16 +0100239 pid = fork();
240
Paul Bakker896ac222011-05-20 12:33:05 +0000241 if( pid < 0 )
242 {
Janos Follath582a4612016-04-28 23:37:16 +0100243 mbedtls_printf(" failed! fork returned %d\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000244 goto exit;
245 }
246
Paul Bakker896ac222011-05-20 12:33:05 +0000247 if( pid != 0 )
248 {
Janos Follath582a4612016-04-28 23:37:16 +0100249 mbedtls_printf( " ok\n" );
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200252 (const unsigned char *) "parent",
253 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000254 {
Janos Follath582a4612016-04-28 23:37:16 +0100255 mbedtls_printf( " failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000256 goto exit;
257 }
258
Paul Bakker896ac222011-05-20 12:33:05 +0000259 continue;
260 }
261
Janos Follathfe049db2016-03-31 11:37:33 +0100262 mbedtls_net_init( &listen_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000263
Janos Follath582a4612016-04-28 23:37:16 +0100264 pid = getpid();
265
Paul Bakker896ac222011-05-20 12:33:05 +0000266 /*
267 * 4. Setup stuff
268 */
Janos Follath582a4612016-04-28 23:37:16 +0100269 mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000270 fflush( stdout );
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200273 (const unsigned char *) "child",
274 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000275 {
Janos Follath582a4612016-04-28 23:37:16 +0100276 mbedtls_printf(
277 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
278 pid, ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000279 goto exit;
280 }
Paul Bakker0c226102014-04-17 16:02:36 +0200281
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200282 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
283 {
Janos Follath582a4612016-04-28 23:37:16 +0100284 mbedtls_printf(
285 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
286 pid, ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200287 goto exit;
288 }
289
290 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
291
Janos Follath582a4612016-04-28 23:37:16 +0100292 mbedtls_printf( "pid %d: SSL setup ok\n", pid );
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200293
Paul Bakker896ac222011-05-20 12:33:05 +0000294 /*
295 * 5. Handshake
296 */
Janos Follath582a4612016-04-28 23:37:16 +0100297 mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000298 fflush( stdout );
299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000301 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100302 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000303 {
Janos Follath582a4612016-04-28 23:37:16 +0100304 mbedtls_printf(
305 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
306 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000307 goto exit;
308 }
309 }
310
Janos Follath582a4612016-04-28 23:37:16 +0100311 mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000312
313 /*
314 * 6. Read the HTTP Request
315 */
Janos Follath582a4612016-04-28 23:37:16 +0100316 mbedtls_printf( "pid %d: Start reading from client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000317 fflush( stdout );
318
319 do
320 {
321 len = sizeof( buf ) - 1;
322 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 ret = mbedtls_ssl_read( &ssl, buf, len );
Paul Bakker896ac222011-05-20 12:33:05 +0000324
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100325 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000326 continue;
327
328 if( ret <= 0 )
329 {
330 switch( ret )
331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Janos Follath582a4612016-04-28 23:37:16 +0100333 mbedtls_printf( "pid %d: connection was closed gracefully\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000334 break;
335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 case MBEDTLS_ERR_NET_CONN_RESET:
Janos Follath582a4612016-04-28 23:37:16 +0100337 mbedtls_printf( "pid %d: connection was reset by peer\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000338 break;
339
340 default:
Janos Follath582a4612016-04-28 23:37:16 +0100341 mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000342 break;
343 }
344
345 break;
346 }
347
348 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100349 mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf );
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000350
351 if( ret > 0 )
352 break;
Paul Bakker896ac222011-05-20 12:33:05 +0000353 }
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000354 while( 1 );
Paul Bakker896ac222011-05-20 12:33:05 +0000355
356 /*
357 * 7. Write the 200 Response
358 */
Janos Follath582a4612016-04-28 23:37:16 +0100359 mbedtls_printf( "pid %d: Start writing to client.\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000360 fflush( stdout );
361
362 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000364
Paul Bakker030decd2014-04-17 16:03:23 +0200365 while( cnt++ < 100 )
Paul Bakker896ac222011-05-20 12:33:05 +0000366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Paul Bakker896ac222011-05-20 12:33:05 +0000370 {
Janos Follath582a4612016-04-28 23:37:16 +0100371 mbedtls_printf(
372 "pid %d: Write failed! peer closed the connection\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000373 goto exit;
374 }
375
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100376 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000377 {
Janos Follath582a4612016-04-28 23:37:16 +0100378 mbedtls_printf(
379 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
380 pid, ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000381 goto exit;
382 }
383 }
384 len = ret;
Janos Follath582a4612016-04-28 23:37:16 +0100385 mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf );
Paul Bakker896ac222011-05-20 12:33:05 +0000386
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200387 mbedtls_net_usleep( 1000000 );
Paul Bakker896ac222011-05-20 12:33:05 +0000388 }
389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_ssl_close_notify( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000391 goto exit;
392 }
393
394exit:
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200395 mbedtls_net_free( &client_fd );
396 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +0200397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_x509_crt_free( &srvcert );
399 mbedtls_pk_free( &pkey );
400 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200401 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_ctr_drbg_free( &ctr_drbg );
403 mbedtls_entropy_free( &entropy );
Paul Bakker896ac222011-05-20 12:33:05 +0000404
Paul Bakkercce9d772011-11-18 14:26:47 +0000405#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000407 fflush( stdout ); getchar();
408#endif
409
410 return( ret );
411}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
413 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
414 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100415 ! _WIN32 */