blob: 6ad4e661b5a9d4e6f849f10126dae7cb1c4223b4 [file] [log] [blame]
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001/*
2 * Simple DTLS server demonstration program
3 *
4 * Copyright (C) 2014, Brainspark B.V.
5 *
Manuel Pégourié-Gonnarde4d48902015-03-06 13:40:52 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02007 *
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é-Gonnarde63582a2014-10-14 11:47:21 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020027#endif
28
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"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000031#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_printf printf
33#define mbedtls_fprintf fprintf
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000034#endif
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) || \
37 !defined(MBEDTLS_SSL_COOKIE_C) || !defined(MBEDTLS_NET_C) || \
38 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
39 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \
40 !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020041
42#include <stdio.h>
43int main( void )
44{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045 printf( "MBEDTLS_SSL_SRV_C and/or MBEDTLS_SSL_PROTO_DTLS and/or "
46 "MBEDTLS_SSL_COOKIE_C and/or MBEDTLS_NET_C and/or "
47 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
48 "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or "
49 "MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020050 return( 0 );
51}
52#else
53
54#if defined(_WIN32)
55#include <windows.h>
56#endif
57
58#include <string.h>
59#include <stdlib.h>
60#include <stdio.h>
61
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/entropy.h"
63#include "mbedtls/ctr_drbg.h"
64#include "mbedtls/certs.h"
65#include "mbedtls/x509.h"
66#include "mbedtls/ssl.h"
67#include "mbedtls/ssl_cookie.h"
68#include "mbedtls/net.h"
69#include "mbedtls/error.h"
70#include "mbedtls/debug.h"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000073#include "mbedtls/ssl_cache.h"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020074#endif
75
76#define READ_TIMEOUT_MS 10000 /* 5 seconds */
77#define DEBUG_LEVEL 0
78
79static void my_debug( void *ctx, int level, const char *str )
80{
81 ((void) level);
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_fprintf( (FILE *) ctx, "%s", str );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020084 fflush( (FILE *) ctx );
85}
86
87int main( void )
88{
89 int ret, len;
90 int listen_fd;
91 int client_fd = -1;
92 unsigned char buf[1024];
93 const char *pers = "dtls_server";
94 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_ssl_cookie_ctx cookie_ctx;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_entropy_context entropy;
98 mbedtls_ctr_drbg_context ctr_drbg;
99 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200100 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_x509_crt srvcert;
102 mbedtls_pk_context pkey;
103#if defined(MBEDTLS_SSL_CACHE_C)
104 mbedtls_ssl_cache_context cache;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200105#endif
106
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200107 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200108 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_ssl_cookie_init( &cookie_ctx );
110#if defined(MBEDTLS_SSL_CACHE_C)
111 mbedtls_ssl_cache_init( &cache );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200112#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_x509_crt_init( &srvcert );
114 mbedtls_pk_init( &pkey );
115 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200116 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_DEBUG_C)
119 mbedtls_debug_set_threshold( DEBUG_LEVEL );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200120#endif
121
122 /*
123 * 1. Load the certificates and private RSA key
124 */
125 printf( "\n . Loading the server cert. and key..." );
126 fflush( stdout );
127
128 /*
129 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
131 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
134 mbedtls_test_srv_crt_len );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200135 if( ret != 0 )
136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200138 goto exit;
139 }
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
142 mbedtls_test_cas_pem_len );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200143 if( ret != 0 )
144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200146 goto exit;
147 }
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
150 mbedtls_test_srv_key_len, NULL, 0 );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200151 if( ret != 0 )
152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 printf( " failed\n ! mbedtls_pk_parse_key returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200154 goto exit;
155 }
156
157 printf( " ok\n" );
158
159 /*
160 * 2. Setup the "listening" UDP socket
161 */
162 printf( " . Bind on udp/*/4433 ..." );
163 fflush( stdout );
164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, 4433, MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200168 goto exit;
169 }
170
171 printf( " ok\n" );
172
173 /*
174 * 3. Seed the RNG
175 */
176 printf( " . Seeding the random number generator..." );
177 fflush( stdout );
178
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200179 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200180 (const unsigned char *) pers,
181 strlen( pers ) ) ) != 0 )
182 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200183 printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200184 goto exit;
185 }
186
187 printf( " ok\n" );
188
189 /*
190 * 4. Setup stuff
191 */
192 printf( " . Setting up the DTLS data..." );
193 fflush( stdout );
194
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200195 if( ( ret = mbedtls_ssl_config_defaults( &conf,
196 MBEDTLS_SSL_IS_SERVER,
197 MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200198 {
199 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
200 goto exit;
201 }
202
203 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200204 {
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200205 printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200206 goto exit;
207 }
208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_ssl_set_rng( &ssl, mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +0200210 mbedtls_ssl_set_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +0100213 mbedtls_ssl_set_session_cache( &conf, &cache,
214 mbedtls_ssl_cache_get,
215 mbedtls_ssl_cache_set );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200216#endif
217
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100218 mbedtls_ssl_set_ca_chain( &conf, srvcert.next, NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 if( ( ret = mbedtls_ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 printf( " failed\n ! mbedtls_ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200222 goto exit;
223 }
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
226 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200229 goto exit;
230 }
231
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +0200232 mbedtls_ssl_set_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200233 &cookie_ctx );
234
235 printf( " ok\n" );
236
237reset:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#ifdef MBEDTLS_ERROR_C
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200239 if( ret != 0 )
240 {
241 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_strerror( ret, error_buf, 100 );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200243 printf("Last error was: %d - %s\n\n", ret, error_buf );
244 }
245#endif
246
247 if( client_fd != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_net_close( client_fd );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_ssl_session_reset( &ssl );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200251
252 /*
253 * 3. Wait until a client connects
254 */
255 client_fd = -1;
256
257 printf( " . Waiting for a remote connection ..." );
258 fflush( stdout );
259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 if( ( ret = mbedtls_net_accept( listen_fd, &client_fd, client_ip ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200263 goto exit;
264 }
265
266 /* With UDP, bind_fd is hijacked by client_fd, so bind a new one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, 4433, MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200270 goto exit;
271 }
272
273 /* For HelloVerifyRequest cookies */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl, client_ip,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200275 sizeof( client_ip ) ) ) != 0 )
276 {
277 printf( " failed\n ! "
278 "ssl_set_client_tranport_id() returned -0x%x\n\n", -ret );
279 goto exit;
280 }
281
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +0100282 mbedtls_ssl_set_bio( &ssl, &client_fd,
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +0100283 mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200284
285 printf( " ok\n" );
286
287 /*
288 * 5. Handshake
289 */
290 printf( " . Performing the DTLS handshake..." );
291 fflush( stdout );
292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 do ret = mbedtls_ssl_handshake( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100294 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
295 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200298 {
299 printf( " hello verification requested\n" );
300 ret = 0;
301 goto reset;
302 }
303 else if( ret != 0 )
304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200306 goto reset;
307 }
308
309 printf( " ok\n" );
310
311 /*
312 * 6. Read the echo Request
313 */
314 printf( " < Read from client:" );
315 fflush( stdout );
316
317 len = sizeof( buf ) - 1;
318 memset( buf, 0, sizeof( buf ) );
319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 do ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100321 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
322 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200323
324 if( ret <= 0 )
325 {
326 switch( ret )
327 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100328 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200329 printf( " timeout\n\n" );
330 goto reset;
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200333 printf( " connection was closed gracefully\n" );
334 ret = 0;
335 goto close_notify;
336
337 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 printf( " mbedtls_ssl_read returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200339 goto reset;
340 }
341 }
342
343 len = ret;
344 printf( " %d bytes read\n\n%s\n\n", len, buf );
345
346 /*
347 * 7. Write the 200 Response
348 */
349 printf( " > Write to client:" );
350 fflush( stdout );
351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 do ret = mbedtls_ssl_write( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100353 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
354 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200355
356 if( ret < 0 )
357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200359 goto exit;
360 }
361
362 len = ret;
363 printf( " %d bytes written\n\n%s\n\n", len, buf );
364
365 /*
366 * 8. Done, cleanly close the connection
367 */
368close_notify:
369 printf( " . Closing the connection..." );
370
371 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100373 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200374 ret = 0;
375
376 printf( " done\n" );
377
378 goto reset;
379
380 /*
381 * Final clean-ups and exit
382 */
383exit:
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#ifdef MBEDTLS_ERROR_C
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200386 if( ret != 0 )
387 {
388 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_strerror( ret, error_buf, 100 );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200390 printf( "Last error was: %d - %s\n\n", ret, error_buf );
391 }
392#endif
393
394 if( client_fd != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_net_close( client_fd );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_x509_crt_free( &srvcert );
398 mbedtls_pk_free( &pkey );
399 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200400 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_ssl_cookie_free( &cookie_ctx );
402#if defined(MBEDTLS_SSL_CACHE_C)
403 mbedtls_ssl_cache_free( &cache );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200404#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_ctr_drbg_free( &ctr_drbg );
406 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200407
408#if defined(_WIN32)
409 printf( " Press Enter to exit this program.\n" );
410 fflush( stdout ); getchar();
411#endif
412
413 /* Shell can not handle large exit numbers -> 1 for errors */
414 if( ret < 0 )
415 ret = 1;
416
417 return( ret );
418}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_DTLS &&
420 MBEDTLS_SSL_COOKIE_C && MBEDTLS_NET_C && MBEDTLS_ENTROPY_C &&
421 MBEDTLS_CTR_DRBG_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C
422 && MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C */