blob: 869d919adb81fc24235447d307372564a0b49d46 [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é-Gonnarddef0bbe2015-05-04 14:56:36 +0200195 if( ( ret = mbedtls_ssl_config_defaults( &conf ) ) != 0 )
196 {
197 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
198 goto exit;
199 }
200
201 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200202 {
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200203 printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200204 goto exit;
205 }
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_ssl_set_endpoint( &ssl, MBEDTLS_SSL_IS_SERVER );
208 mbedtls_ssl_set_transport( &ssl, MBEDTLS_SSL_TRANSPORT_DATAGRAM );
209 mbedtls_ssl_set_authmode( &ssl, MBEDTLS_SSL_VERIFY_NONE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_ssl_set_rng( &ssl, mbedtls_ctr_drbg_random, &ctr_drbg );
212 mbedtls_ssl_set_dbg( &ssl, my_debug, stdout );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#if defined(MBEDTLS_SSL_CACHE_C)
215 mbedtls_ssl_set_session_cache( &ssl, mbedtls_ssl_cache_get, &cache,
216 mbedtls_ssl_cache_set, &cache );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200217#endif
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
220 if( ( ret = mbedtls_ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 printf( " failed\n ! mbedtls_ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200223 goto exit;
224 }
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
227 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200230 goto exit;
231 }
232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_ssl_set_dtls_cookies( &ssl, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200234 &cookie_ctx );
235
236 printf( " ok\n" );
237
238reset:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#ifdef MBEDTLS_ERROR_C
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200240 if( ret != 0 )
241 {
242 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_strerror( ret, error_buf, 100 );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200244 printf("Last error was: %d - %s\n\n", ret, error_buf );
245 }
246#endif
247
248 if( client_fd != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_net_close( client_fd );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_ssl_session_reset( &ssl );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200252
253 /*
254 * 3. Wait until a client connects
255 */
256 client_fd = -1;
257
258 printf( " . Waiting for a remote connection ..." );
259 fflush( stdout );
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 if( ( ret = mbedtls_net_accept( listen_fd, &client_fd, client_ip ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200264 goto exit;
265 }
266
267 /* With UDP, bind_fd is hijacked by client_fd, so bind a new one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, 4433, MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200271 goto exit;
272 }
273
274 /* For HelloVerifyRequest cookies */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl, client_ip,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200276 sizeof( client_ip ) ) ) != 0 )
277 {
278 printf( " failed\n ! "
279 "ssl_set_client_tranport_id() returned -0x%x\n\n", -ret );
280 goto exit;
281 }
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_ssl_set_bio_timeout( &ssl, &client_fd,
284 mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200285 READ_TIMEOUT_MS );
286
287 printf( " ok\n" );
288
289 /*
290 * 5. Handshake
291 */
292 printf( " . Performing the DTLS handshake..." );
293 fflush( stdout );
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 do ret = mbedtls_ssl_handshake( &ssl );
296 while( ret == MBEDTLS_ERR_NET_WANT_READ ||
297 ret == MBEDTLS_ERR_NET_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200300 {
301 printf( " hello verification requested\n" );
302 ret = 0;
303 goto reset;
304 }
305 else if( ret != 0 )
306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200308 goto reset;
309 }
310
311 printf( " ok\n" );
312
313 /*
314 * 6. Read the echo Request
315 */
316 printf( " < Read from client:" );
317 fflush( stdout );
318
319 len = sizeof( buf ) - 1;
320 memset( buf, 0, sizeof( buf ) );
321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 do ret = mbedtls_ssl_read( &ssl, buf, len );
323 while( ret == MBEDTLS_ERR_NET_WANT_READ ||
324 ret == MBEDTLS_ERR_NET_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200325
326 if( ret <= 0 )
327 {
328 switch( ret )
329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 case MBEDTLS_ERR_NET_TIMEOUT:
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200331 printf( " timeout\n\n" );
332 goto reset;
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200335 printf( " connection was closed gracefully\n" );
336 ret = 0;
337 goto close_notify;
338
339 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 printf( " mbedtls_ssl_read returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200341 goto reset;
342 }
343 }
344
345 len = ret;
346 printf( " %d bytes read\n\n%s\n\n", len, buf );
347
348 /*
349 * 7. Write the 200 Response
350 */
351 printf( " > Write to client:" );
352 fflush( stdout );
353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 do ret = mbedtls_ssl_write( &ssl, buf, len );
355 while( ret == MBEDTLS_ERR_NET_WANT_READ ||
356 ret == MBEDTLS_ERR_NET_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200357
358 if( ret < 0 )
359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200361 goto exit;
362 }
363
364 len = ret;
365 printf( " %d bytes written\n\n%s\n\n", len, buf );
366
367 /*
368 * 8. Done, cleanly close the connection
369 */
370close_notify:
371 printf( " . Closing the connection..." );
372
373 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 do ret = mbedtls_ssl_close_notify( &ssl );
375 while( ret == MBEDTLS_ERR_NET_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200376 ret = 0;
377
378 printf( " done\n" );
379
380 goto reset;
381
382 /*
383 * Final clean-ups and exit
384 */
385exit:
386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#ifdef MBEDTLS_ERROR_C
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200388 if( ret != 0 )
389 {
390 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_strerror( ret, error_buf, 100 );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200392 printf( "Last error was: %d - %s\n\n", ret, error_buf );
393 }
394#endif
395
396 if( client_fd != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_net_close( client_fd );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_x509_crt_free( &srvcert );
400 mbedtls_pk_free( &pkey );
401 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200402 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_ssl_cookie_free( &cookie_ctx );
404#if defined(MBEDTLS_SSL_CACHE_C)
405 mbedtls_ssl_cache_free( &cache );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200406#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_ctr_drbg_free( &ctr_drbg );
408 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200409
410#if defined(_WIN32)
411 printf( " Press Enter to exit this program.\n" );
412 fflush( stdout ); getchar();
413#endif
414
415 /* Shell can not handle large exit numbers -> 1 for errors */
416 if( ret < 0 )
417 ret = 1;
418
419 return( ret );
420}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_DTLS &&
422 MBEDTLS_SSL_COOKIE_C && MBEDTLS_NET_C && MBEDTLS_ENTROPY_C &&
423 MBEDTLS_CTR_DRBG_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C
424 && MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C */