blob: a4b86f76e65bba1ce93e0da9dc0c2224ba227e40 [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;
100 mbedtls_x509_crt srvcert;
101 mbedtls_pk_context pkey;
102#if defined(MBEDTLS_SSL_CACHE_C)
103 mbedtls_ssl_cache_context cache;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200104#endif
105
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200106 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_ssl_cookie_init( &cookie_ctx );
108#if defined(MBEDTLS_SSL_CACHE_C)
109 mbedtls_ssl_cache_init( &cache );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200110#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_x509_crt_init( &srvcert );
112 mbedtls_pk_init( &pkey );
113 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200114 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116#if defined(MBEDTLS_DEBUG_C)
117 mbedtls_debug_set_threshold( DEBUG_LEVEL );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200118#endif
119
120 /*
121 * 1. Load the certificates and private RSA key
122 */
123 printf( "\n . Loading the server cert. and key..." );
124 fflush( stdout );
125
126 /*
127 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
129 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
132 mbedtls_test_srv_crt_len );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200133 if( ret != 0 )
134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200136 goto exit;
137 }
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
140 mbedtls_test_cas_pem_len );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200141 if( ret != 0 )
142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200144 goto exit;
145 }
146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
148 mbedtls_test_srv_key_len, NULL, 0 );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200149 if( ret != 0 )
150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 printf( " failed\n ! mbedtls_pk_parse_key returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200152 goto exit;
153 }
154
155 printf( " ok\n" );
156
157 /*
158 * 2. Setup the "listening" UDP socket
159 */
160 printf( " . Bind on udp/*/4433 ..." );
161 fflush( stdout );
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, 4433, MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200166 goto exit;
167 }
168
169 printf( " ok\n" );
170
171 /*
172 * 3. Seed the RNG
173 */
174 printf( " . Seeding the random number generator..." );
175 fflush( stdout );
176
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200177 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200178 (const unsigned char *) pers,
179 strlen( pers ) ) ) != 0 )
180 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200181 printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200182 goto exit;
183 }
184
185 printf( " ok\n" );
186
187 /*
188 * 4. Setup stuff
189 */
190 printf( " . Setting up the DTLS data..." );
191 fflush( stdout );
192
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200193 if( ( ret = mbedtls_ssl_setup( &ssl ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200194 {
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200195 printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200196 goto exit;
197 }
198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_ssl_set_endpoint( &ssl, MBEDTLS_SSL_IS_SERVER );
200 mbedtls_ssl_set_transport( &ssl, MBEDTLS_SSL_TRANSPORT_DATAGRAM );
201 mbedtls_ssl_set_authmode( &ssl, MBEDTLS_SSL_VERIFY_NONE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_ssl_set_rng( &ssl, mbedtls_ctr_drbg_random, &ctr_drbg );
204 mbedtls_ssl_set_dbg( &ssl, my_debug, stdout );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_SSL_CACHE_C)
207 mbedtls_ssl_set_session_cache( &ssl, mbedtls_ssl_cache_get, &cache,
208 mbedtls_ssl_cache_set, &cache );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200209#endif
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
212 if( ( ret = mbedtls_ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 printf( " failed\n ! mbedtls_ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200215 goto exit;
216 }
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
219 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 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_cookie_setup 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 mbedtls_ssl_set_dtls_cookies( &ssl, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200226 &cookie_ctx );
227
228 printf( " ok\n" );
229
230reset:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#ifdef MBEDTLS_ERROR_C
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200232 if( ret != 0 )
233 {
234 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_strerror( ret, error_buf, 100 );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200236 printf("Last error was: %d - %s\n\n", ret, error_buf );
237 }
238#endif
239
240 if( client_fd != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_net_close( client_fd );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_ssl_session_reset( &ssl );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200244
245 /*
246 * 3. Wait until a client connects
247 */
248 client_fd = -1;
249
250 printf( " . Waiting for a remote connection ..." );
251 fflush( stdout );
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 if( ( ret = mbedtls_net_accept( listen_fd, &client_fd, client_ip ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200256 goto exit;
257 }
258
259 /* With UDP, bind_fd is hijacked by client_fd, so bind a new one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, 4433, MBEDTLS_NET_PROTO_UDP ) ) != 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_bind returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200263 goto exit;
264 }
265
266 /* For HelloVerifyRequest cookies */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl, client_ip,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200268 sizeof( client_ip ) ) ) != 0 )
269 {
270 printf( " failed\n ! "
271 "ssl_set_client_tranport_id() returned -0x%x\n\n", -ret );
272 goto exit;
273 }
274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_ssl_set_bio_timeout( &ssl, &client_fd,
276 mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200277 READ_TIMEOUT_MS );
278
279 printf( " ok\n" );
280
281 /*
282 * 5. Handshake
283 */
284 printf( " . Performing the DTLS handshake..." );
285 fflush( stdout );
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 do ret = mbedtls_ssl_handshake( &ssl );
288 while( ret == MBEDTLS_ERR_NET_WANT_READ ||
289 ret == MBEDTLS_ERR_NET_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200292 {
293 printf( " hello verification requested\n" );
294 ret = 0;
295 goto reset;
296 }
297 else if( ret != 0 )
298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200300 goto reset;
301 }
302
303 printf( " ok\n" );
304
305 /*
306 * 6. Read the echo Request
307 */
308 printf( " < Read from client:" );
309 fflush( stdout );
310
311 len = sizeof( buf ) - 1;
312 memset( buf, 0, sizeof( buf ) );
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 do ret = mbedtls_ssl_read( &ssl, buf, len );
315 while( ret == MBEDTLS_ERR_NET_WANT_READ ||
316 ret == MBEDTLS_ERR_NET_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200317
318 if( ret <= 0 )
319 {
320 switch( ret )
321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 case MBEDTLS_ERR_NET_TIMEOUT:
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200323 printf( " timeout\n\n" );
324 goto reset;
325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200327 printf( " connection was closed gracefully\n" );
328 ret = 0;
329 goto close_notify;
330
331 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 printf( " mbedtls_ssl_read returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200333 goto reset;
334 }
335 }
336
337 len = ret;
338 printf( " %d bytes read\n\n%s\n\n", len, buf );
339
340 /*
341 * 7. Write the 200 Response
342 */
343 printf( " > Write to client:" );
344 fflush( stdout );
345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 do ret = mbedtls_ssl_write( &ssl, buf, len );
347 while( ret == MBEDTLS_ERR_NET_WANT_READ ||
348 ret == MBEDTLS_ERR_NET_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200349
350 if( ret < 0 )
351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200353 goto exit;
354 }
355
356 len = ret;
357 printf( " %d bytes written\n\n%s\n\n", len, buf );
358
359 /*
360 * 8. Done, cleanly close the connection
361 */
362close_notify:
363 printf( " . Closing the connection..." );
364
365 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 do ret = mbedtls_ssl_close_notify( &ssl );
367 while( ret == MBEDTLS_ERR_NET_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200368 ret = 0;
369
370 printf( " done\n" );
371
372 goto reset;
373
374 /*
375 * Final clean-ups and exit
376 */
377exit:
378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379#ifdef MBEDTLS_ERROR_C
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200380 if( ret != 0 )
381 {
382 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_strerror( ret, error_buf, 100 );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200384 printf( "Last error was: %d - %s\n\n", ret, error_buf );
385 }
386#endif
387
388 if( client_fd != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_net_close( client_fd );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_x509_crt_free( &srvcert );
392 mbedtls_pk_free( &pkey );
393 mbedtls_ssl_free( &ssl );
394 mbedtls_ssl_cookie_free( &cookie_ctx );
395#if defined(MBEDTLS_SSL_CACHE_C)
396 mbedtls_ssl_cache_free( &cache );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200397#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_ctr_drbg_free( &ctr_drbg );
399 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200400
401#if defined(_WIN32)
402 printf( " Press Enter to exit this program.\n" );
403 fflush( stdout ); getchar();
404#endif
405
406 /* Shell can not handle large exit numbers -> 1 for errors */
407 if( ret < 0 )
408 ret = 1;
409
410 return( ret );
411}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_DTLS &&
413 MBEDTLS_SSL_COOKIE_C && MBEDTLS_NET_C && MBEDTLS_ENTROPY_C &&
414 MBEDTLS_CTR_DRBG_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C
415 && MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C */