blob: 689a0a741cd428845512132c2282ff188fb2f005 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 memset( &ssl, 0, sizeof(mbedtls_ssl_context) );
107 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é-Gonnarde63582a2014-10-14 11:47:21 +0200114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#if defined(MBEDTLS_DEBUG_C)
116 mbedtls_debug_set_threshold( DEBUG_LEVEL );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200117#endif
118
119 /*
120 * 1. Load the certificates and private RSA key
121 */
122 printf( "\n . Loading the server cert. and key..." );
123 fflush( stdout );
124
125 /*
126 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
128 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
131 mbedtls_test_srv_crt_len );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200132 if( ret != 0 )
133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200135 goto exit;
136 }
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
139 mbedtls_test_cas_pem_len );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200140 if( ret != 0 )
141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200143 goto exit;
144 }
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
147 mbedtls_test_srv_key_len, NULL, 0 );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200148 if( ret != 0 )
149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 printf( " failed\n ! mbedtls_pk_parse_key returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200151 goto exit;
152 }
153
154 printf( " ok\n" );
155
156 /*
157 * 2. Setup the "listening" UDP socket
158 */
159 printf( " . Bind on udp/*/4433 ..." );
160 fflush( stdout );
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, 4433, MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200165 goto exit;
166 }
167
168 printf( " ok\n" );
169
170 /*
171 * 3. Seed the RNG
172 */
173 printf( " . Seeding the random number generator..." );
174 fflush( stdout );
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 if( ( ret = mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200177 (const unsigned char *) pers,
178 strlen( pers ) ) ) != 0 )
179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 printf( " failed\n ! mbedtls_ctr_drbg_init returned %d\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200181 goto exit;
182 }
183
184 printf( " ok\n" );
185
186 /*
187 * 4. Setup stuff
188 */
189 printf( " . Setting up the DTLS data..." );
190 fflush( stdout );
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 if( ( ret = mbedtls_ssl_init( &ssl ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 printf( " failed\n ! mbedtls_ssl_init returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200195 goto exit;
196 }
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_ssl_set_endpoint( &ssl, MBEDTLS_SSL_IS_SERVER );
199 mbedtls_ssl_set_transport( &ssl, MBEDTLS_SSL_TRANSPORT_DATAGRAM );
200 mbedtls_ssl_set_authmode( &ssl, MBEDTLS_SSL_VERIFY_NONE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_ssl_set_rng( &ssl, mbedtls_ctr_drbg_random, &ctr_drbg );
203 mbedtls_ssl_set_dbg( &ssl, my_debug, stdout );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#if defined(MBEDTLS_SSL_CACHE_C)
206 mbedtls_ssl_set_session_cache( &ssl, mbedtls_ssl_cache_get, &cache,
207 mbedtls_ssl_cache_set, &cache );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200208#endif
209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
211 if( ( ret = mbedtls_ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 printf( " failed\n ! mbedtls_ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200214 goto exit;
215 }
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
218 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200221 goto exit;
222 }
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_ssl_set_dtls_cookies( &ssl, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200225 &cookie_ctx );
226
227 printf( " ok\n" );
228
229reset:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#ifdef MBEDTLS_ERROR_C
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200231 if( ret != 0 )
232 {
233 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_strerror( ret, error_buf, 100 );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200235 printf("Last error was: %d - %s\n\n", ret, error_buf );
236 }
237#endif
238
239 if( client_fd != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_net_close( client_fd );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_ssl_session_reset( &ssl );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200243
244 /*
245 * 3. Wait until a client connects
246 */
247 client_fd = -1;
248
249 printf( " . Waiting for a remote connection ..." );
250 fflush( stdout );
251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 if( ( ret = mbedtls_net_accept( listen_fd, &client_fd, client_ip ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200255 goto exit;
256 }
257
258 /* With UDP, bind_fd is hijacked by client_fd, so bind a new one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, 4433, MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200262 goto exit;
263 }
264
265 /* For HelloVerifyRequest cookies */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl, client_ip,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200267 sizeof( client_ip ) ) ) != 0 )
268 {
269 printf( " failed\n ! "
270 "ssl_set_client_tranport_id() returned -0x%x\n\n", -ret );
271 goto exit;
272 }
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 mbedtls_ssl_set_bio_timeout( &ssl, &client_fd,
275 mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200276 READ_TIMEOUT_MS );
277
278 printf( " ok\n" );
279
280 /*
281 * 5. Handshake
282 */
283 printf( " . Performing the DTLS handshake..." );
284 fflush( stdout );
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 do ret = mbedtls_ssl_handshake( &ssl );
287 while( ret == MBEDTLS_ERR_NET_WANT_READ ||
288 ret == MBEDTLS_ERR_NET_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200291 {
292 printf( " hello verification requested\n" );
293 ret = 0;
294 goto reset;
295 }
296 else if( ret != 0 )
297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200299 goto reset;
300 }
301
302 printf( " ok\n" );
303
304 /*
305 * 6. Read the echo Request
306 */
307 printf( " < Read from client:" );
308 fflush( stdout );
309
310 len = sizeof( buf ) - 1;
311 memset( buf, 0, sizeof( buf ) );
312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 do ret = mbedtls_ssl_read( &ssl, buf, len );
314 while( ret == MBEDTLS_ERR_NET_WANT_READ ||
315 ret == MBEDTLS_ERR_NET_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200316
317 if( ret <= 0 )
318 {
319 switch( ret )
320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 case MBEDTLS_ERR_NET_TIMEOUT:
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200322 printf( " timeout\n\n" );
323 goto reset;
324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200326 printf( " connection was closed gracefully\n" );
327 ret = 0;
328 goto close_notify;
329
330 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 printf( " mbedtls_ssl_read returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200332 goto reset;
333 }
334 }
335
336 len = ret;
337 printf( " %d bytes read\n\n%s\n\n", len, buf );
338
339 /*
340 * 7. Write the 200 Response
341 */
342 printf( " > Write to client:" );
343 fflush( stdout );
344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 do ret = mbedtls_ssl_write( &ssl, buf, len );
346 while( ret == MBEDTLS_ERR_NET_WANT_READ ||
347 ret == MBEDTLS_ERR_NET_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200348
349 if( ret < 0 )
350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200352 goto exit;
353 }
354
355 len = ret;
356 printf( " %d bytes written\n\n%s\n\n", len, buf );
357
358 /*
359 * 8. Done, cleanly close the connection
360 */
361close_notify:
362 printf( " . Closing the connection..." );
363
364 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 do ret = mbedtls_ssl_close_notify( &ssl );
366 while( ret == MBEDTLS_ERR_NET_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200367 ret = 0;
368
369 printf( " done\n" );
370
371 goto reset;
372
373 /*
374 * Final clean-ups and exit
375 */
376exit:
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378#ifdef MBEDTLS_ERROR_C
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200379 if( ret != 0 )
380 {
381 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_strerror( ret, error_buf, 100 );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200383 printf( "Last error was: %d - %s\n\n", ret, error_buf );
384 }
385#endif
386
387 if( client_fd != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_net_close( client_fd );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_x509_crt_free( &srvcert );
391 mbedtls_pk_free( &pkey );
392 mbedtls_ssl_free( &ssl );
393 mbedtls_ssl_cookie_free( &cookie_ctx );
394#if defined(MBEDTLS_SSL_CACHE_C)
395 mbedtls_ssl_cache_free( &cache );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200396#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_ctr_drbg_free( &ctr_drbg );
398 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200399
400#if defined(_WIN32)
401 printf( " Press Enter to exit this program.\n" );
402 fflush( stdout ); getchar();
403#endif
404
405 /* Shell can not handle large exit numbers -> 1 for errors */
406 if( ret < 0 )
407 ret = 1;
408
409 return( ret );
410}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_DTLS &&
412 MBEDTLS_SSL_COOKIE_C && MBEDTLS_NET_C && MBEDTLS_ENTROPY_C &&
413 MBEDTLS_CTR_DRBG_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C
414 && MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C */