blob: 1f50c258d6dbb74eb1c15dcb7c105e393ff1e93c [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é-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakker896ac222011-05-20 12:33:05 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker896ac222011-05-20 12:33:05 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker896ac222011-05-20 12:33:05 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_fprintf fprintf
34#define mbedtls_printf printf
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
Paul Bakker3c5ef712013-06-25 16:37:45 +020086static void my_debug( void *ctx, int level, const char *str )
Paul Bakker896ac222011-05-20 12:33:05 +000087{
88 if( level < DEBUG_LEVEL )
89 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_fprintf( (FILE *) ctx, "%s", str );
Paul Bakker896ac222011-05-20 12:33:05 +000091 fflush( (FILE *) ctx );
92 }
93}
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;
98 int listen_fd;
Manuel Pégourié-Gonnard68821da2013-09-16 12:34:33 +020099 int client_fd = -1;
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é-Gonnarddef0bbe2015-05-04 14:56:36 +0200110 mbedtls_ssl_init( &ssl );
111 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_entropy_init( &entropy );
113 mbedtls_pk_init( &pkey );
114 mbedtls_x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200115 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker0c226102014-04-17 16:02:36 +0200116
Paul Bakker896ac222011-05-20 12:33:05 +0000117 signal( SIGCHLD, SIG_IGN );
118
119 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000120 * 0. Initial seeding of the RNG
121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_printf( "\n . Initial seeding of the random generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000123 fflush( stdout );
124
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200125 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200126 (const unsigned char *) pers,
127 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000128 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200129 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000130 goto exit;
131 }
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_printf( " ok\n" );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000134
135 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000136 * 1. Load the certificates and private RSA key
137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000139 fflush( stdout );
140
Paul Bakker896ac222011-05-20 12:33:05 +0000141 /*
142 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
144 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
147 mbedtls_test_srv_crt_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000148 if( ret != 0 )
149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000151 goto exit;
152 }
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
155 mbedtls_test_cas_pem_len );
Paul Bakker896ac222011-05-20 12:33:05 +0000156 if( ret != 0 )
157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000159 goto exit;
160 }
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
163 mbedtls_test_srv_key_len, NULL, 0 );
Paul Bakker896ac222011-05-20 12:33:05 +0000164 if( ret != 0 )
165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000167 goto exit;
168 }
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000171
172 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200173 * 1b. Prepare SSL configuration
174 */
175 mbedtls_printf( " . Configuring SSL..." );
176 fflush( stdout );
177
178 if( ( ret = mbedtls_ssl_config_defaults( &conf,
179 MBEDTLS_SSL_IS_SERVER,
180 MBEDTLS_SSL_TRANSPORT_STREAM ) ) != 0 )
181 {
182 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
183 goto exit;
184 }
185
186 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
187 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
188
189 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
190 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
191 {
192 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
193 goto exit;
194 }
195
196 mbedtls_printf( " ok\n" );
197
198 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000199 * 2. Setup the listening TCP socket
200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_printf( " . Bind on https://localhost:4433/ ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000202 fflush( stdout );
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, 4433, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000207 goto exit;
208 }
209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000211
212 while( 1 )
213 {
214 /*
215 * 3. Wait until a client connects
216 */
217 client_fd = -1;
218 memset( &ssl, 0, sizeof( ssl ) );
219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_printf( " . Waiting for a remote connection ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000221 fflush( stdout );
222
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200223 if( ( ret = mbedtls_net_accept( listen_fd, &client_fd,
224 NULL, 0, NULL ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000227 goto exit;
228 }
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000231
232 /*
233 * 3.5. Forking server thread
234 */
235
236 pid = fork();
237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_printf( " . Forking to handle connection ..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000239 fflush( stdout );
240
241 if( pid < 0 )
242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_printf(" failed\n ! fork returned %d\n\n", pid );
Paul Bakker896ac222011-05-20 12:33:05 +0000244 goto exit;
245 }
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000248
249 if( pid != 0 )
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 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_reseed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000256 goto exit;
257 }
258
Paul Bakker896ac222011-05-20 12:33:05 +0000259 close( client_fd );
260 continue;
261 }
262
263 close( listen_fd );
264
265 /*
266 * 4. Setup stuff
267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_printf( " . Setting up the SSL data...." );
Paul Bakker896ac222011-05-20 12:33:05 +0000269 fflush( stdout );
270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200272 (const unsigned char *) "child",
273 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_reseed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000276 goto exit;
277 }
Paul Bakker0c226102014-04-17 16:02:36 +0200278
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200279 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
280 {
281 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
282 goto exit;
283 }
284
285 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
286
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200287 mbedtls_printf( " ok\n" );
288
Paul Bakker896ac222011-05-20 12:33:05 +0000289 /*
290 * 5. Handshake
291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000293 fflush( stdout );
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000296 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100297 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000300 goto exit;
301 }
302 }
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_printf( " ok\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000305
306 /*
307 * 6. Read the HTTP Request
308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_printf( " < Read from client:" );
Paul Bakker896ac222011-05-20 12:33:05 +0000310 fflush( stdout );
311
312 do
313 {
314 len = sizeof( buf ) - 1;
315 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 ret = mbedtls_ssl_read( &ssl, buf, len );
Paul Bakker896ac222011-05-20 12:33:05 +0000317
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100318 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000319 continue;
320
321 if( ret <= 0 )
322 {
323 switch( ret )
324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
326 mbedtls_printf( " connection was closed gracefully\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000327 break;
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 case MBEDTLS_ERR_NET_CONN_RESET:
330 mbedtls_printf( " connection was reset by peer\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000331 break;
332
333 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_printf( " mbedtls_ssl_read returned %d\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000335 break;
336 }
337
338 break;
339 }
340
341 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000343
344 if( ret > 0 )
345 break;
Paul Bakker896ac222011-05-20 12:33:05 +0000346 }
Manuel Pégourié-Gonnard401caad2015-02-14 15:53:24 +0000347 while( 1 );
Paul Bakker896ac222011-05-20 12:33:05 +0000348
349 /*
350 * 7. Write the 200 Response
351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_printf( " > Write to client:" );
Paul Bakker896ac222011-05-20 12:33:05 +0000353 fflush( stdout );
354
355 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000357
Paul Bakker030decd2014-04-17 16:03:23 +0200358 while( cnt++ < 100 )
Paul Bakker896ac222011-05-20 12:33:05 +0000359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
Paul Bakker896ac222011-05-20 12:33:05 +0000361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Paul Bakker896ac222011-05-20 12:33:05 +0000363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000365 goto exit;
366 }
367
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100368 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker896ac222011-05-20 12:33:05 +0000369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000371 goto exit;
372 }
373 }
374 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_printf( " %d bytes written\n\n%s\n", len, (char *) buf );
Paul Bakker896ac222011-05-20 12:33:05 +0000376
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200377 mbedtls_net_usleep( 1000000 );
Paul Bakker896ac222011-05-20 12:33:05 +0000378 }
379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_ssl_close_notify( &ssl );
Paul Bakker896ac222011-05-20 12:33:05 +0000381 goto exit;
382 }
383
384exit:
385
Paul Bakker0c226102014-04-17 16:02:36 +0200386 if( client_fd != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_net_close( client_fd );
Paul Bakker0c226102014-04-17 16:02:36 +0200388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_x509_crt_free( &srvcert );
390 mbedtls_pk_free( &pkey );
391 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200392 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_ctr_drbg_free( &ctr_drbg );
394 mbedtls_entropy_free( &entropy );
Paul Bakker896ac222011-05-20 12:33:05 +0000395
Paul Bakkercce9d772011-11-18 14:26:47 +0000396#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker896ac222011-05-20 12:33:05 +0000398 fflush( stdout ); getchar();
399#endif
400
401 return( ret );
402}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
404 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
405 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100406 ! _WIN32 */