blob: 35b74682896ec283ecd66084117c51d5881f816d [file] [log] [blame]
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001/*
2 * Simple DTLS client 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é-Gonnard4b3e5ef2015-03-27 11:24:27 +010032#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_printf printf
34#define mbedtls_fprintf fprintf
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if !defined(MBEDTLS_SSL_CLI_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) || \
38 !defined(MBEDTLS_NET_C) || \
39 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
40 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \
41 !defined(MBEDTLS_CERTS_C)
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020042
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020043int main( int argc, char *argv[] )
44{
45 ((void) argc);
46 ((void) argv);
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 mbedtls_printf( "MBEDTLS_SSL_CLI_C and/or MBEDTLS_SSL_PROTO_DTLS and/or "
49 "MBEDTLS_NET_C and/or "
50 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
51 "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or "
52 "MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020053 return( 0 );
54}
55#else
56
57#include <string.h>
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020058
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/net.h"
60#include "mbedtls/debug.h"
61#include "mbedtls/ssl.h"
62#include "mbedtls/entropy.h"
63#include "mbedtls/ctr_drbg.h"
64#include "mbedtls/error.h"
65#include "mbedtls/certs.h"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020066
67#define SERVER_PORT 4433
68#define SERVER_NAME "localhost"
69#define SERVER_ADDR "127.0.0.1" /* forces IPv4 */
70#define MESSAGE "Echo this"
71
72#define READ_TIMEOUT_MS 1000
73#define MAX_RETRY 5
74
75#define DEBUG_LEVEL 0
76
77static void my_debug( void *ctx, int level, const char *str )
78{
79 ((void) level);
80
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 mbedtls_fprintf( (FILE *) ctx, "%s", str );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020082 fflush( (FILE *) ctx );
83}
84
85int main( int argc, char *argv[] )
86{
87 int ret, len, server_fd = -1;
88 unsigned char buf[1024];
89 const char *pers = "dtls_client";
90 int retry_left = MAX_RETRY;
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_entropy_context entropy;
93 mbedtls_ctr_drbg_context ctr_drbg;
94 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020095 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_x509_crt cacert;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020097
98 ((void) argc);
99 ((void) argv);
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#if defined(MBEDTLS_DEBUG_C)
102 mbedtls_debug_set_threshold( DEBUG_LEVEL );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200103#endif
104
105 /*
106 * 0. Initialize the RNG and the session data
107 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200108 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200109 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_x509_crt_init( &cacert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200111 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_printf( "\n . Seeding the random number generator..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200114 fflush( stdout );
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200117 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200118 (const unsigned char *) pers,
119 strlen( pers ) ) ) != 0 )
120 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200121 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200122 goto exit;
123 }
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200126
127 /*
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200128 * 0. Load certificates
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_printf( " . Loading the CA root certificate ..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200131 fflush( stdout );
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
134 mbedtls_test_cas_pem_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 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\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 mbedtls_printf( " ok (%d skipped)\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200142
143 /*
144 * 1. Start the connection
145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_printf( " . Connecting to udp/%s/%4d...", SERVER_NAME,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200147 SERVER_PORT );
148 fflush( stdout );
149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 if( ( ret = mbedtls_net_connect( &server_fd, SERVER_ADDR,
151 SERVER_PORT, MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200154 goto exit;
155 }
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200158
159 /*
160 * 2. Setup stuff
161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_printf( " . Setting up the DTLS structure..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200163 fflush( stdout );
164
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200165 if( ( ret = mbedtls_ssl_config_defaults( &conf,
166 MBEDTLS_SSL_IS_CLIENT,
167 MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200168 {
169 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
170 goto exit;
171 }
172
173 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200174 {
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200175 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200176 goto exit;
177 }
178
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200179 /* OPTIONAL is usually a bad choice for security, but makes interop easier
180 * in this simplified example, in which the ca chain is hardcoded.
181 * Production code should set a proper ca chain and use REQUIRED. */
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +0200182 mbedtls_ssl_set_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100183 mbedtls_ssl_set_ca_chain( &conf, &cacert, NULL );
184 if( ( ret = mbedtls_ssl_set_hostname( &ssl, SERVER_NAME ) ) != 0 )
185 {
186 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
187 goto exit;
188 }
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_ssl_set_rng( &ssl, mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +0200191 mbedtls_ssl_set_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200192
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +0100193 mbedtls_ssl_set_bio( &ssl, &server_fd,
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +0100194 mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200195
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100196 mbedtls_printf( " ok\n" );
197
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200198 /*
199 * 4. Handshake
200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200202 fflush( stdout );
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 do ret = mbedtls_ssl_handshake( &ssl );
205 while( ret == MBEDTLS_ERR_NET_WANT_READ ||
206 ret == MBEDTLS_ERR_NET_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200207
208 if( ret != 0 )
209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200211 goto exit;
212 }
213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200215
216 /*
217 * 5. Verify the server certificate
218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 /* In real life, we would have used MBEDTLS_SSL_VERIFY_REQUIRED so that the
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200222 * handshake would not succeed if the peer's cert is bad. Even if we used
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 * MBEDTLS_SSL_VERIFY_OPTIONAL, we would bail out here if ret != 0 */
224 if( ( ret = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200227
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100228 if( ( ret & MBEDTLS_X509_BADCERT_EXPIRED ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_printf( " ! server certificate has expired\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 if( ( ret & MBEDTLS_X509_BADCERT_REVOKED ) != 0 )
232 mbedtls_printf( " ! server certificate has been revoked\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 if( ( ret & MBEDTLS_X509_BADCERT_CN_MISMATCH ) != 0 )
235 mbedtls_printf( " ! CN mismatch (expected CN=%s)\n", SERVER_NAME );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 if( ( ret & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) != 0 )
238 mbedtls_printf( " ! self-signed or not signed by a trusted CA\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200241 }
242 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200244
245 /*
246 * 6. Write the echo request
247 */
248send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_printf( " > Write to server:" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200250 fflush( stdout );
251
252 len = sizeof( MESSAGE ) - 1;
253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 do ret = mbedtls_ssl_write( &ssl, (unsigned char *) MESSAGE, len );
255 while( ret == MBEDTLS_ERR_NET_WANT_READ ||
256 ret == MBEDTLS_ERR_NET_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200257
258 if( ret < 0 )
259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200261 goto exit;
262 }
263
264 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_printf( " %d bytes written\n\n%s\n\n", len, MESSAGE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200266
267 /*
268 * 7. Read the echo response
269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_printf( " < Read from server:" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200271 fflush( stdout );
272
273 len = sizeof( buf ) - 1;
274 memset( buf, 0, sizeof( buf ) );
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 do ret = mbedtls_ssl_read( &ssl, buf, len );
277 while( ret == MBEDTLS_ERR_NET_WANT_READ ||
278 ret == MBEDTLS_ERR_NET_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200279
280 if( ret <= 0 )
281 {
282 switch( ret )
283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 case MBEDTLS_ERR_NET_TIMEOUT:
285 mbedtls_printf( " timeout\n\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200286 if( retry_left-- > 0 )
287 goto send_request;
288 goto exit;
289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
291 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200292 ret = 0;
293 goto close_notify;
294
295 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200297 goto exit;
298 }
299 }
300
301 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_printf( " %d bytes read\n\n%s\n\n", len, buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200303
304 /*
305 * 8. Done, cleanly close the connection
306 */
307close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200309
310 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 do ret = mbedtls_ssl_close_notify( &ssl );
312 while( ret == MBEDTLS_ERR_NET_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200313 ret = 0;
314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200316
317 /*
318 * 9. Final clean-ups and exit
319 */
320exit:
321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#ifdef MBEDTLS_ERROR_C
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200323 if( ret != 0 )
324 {
325 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 mbedtls_strerror( ret, error_buf, 100 );
327 mbedtls_printf( "Last error was: %d - %s\n\n", ret, error_buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200328 }
329#endif
330
331 if( server_fd != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 mbedtls_net_close( server_fd );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_x509_crt_free( &cacert );
335 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200336 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_ctr_drbg_free( &ctr_drbg );
338 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200339
340#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 mbedtls_printf( " + Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200342 fflush( stdout ); getchar();
343#endif
344
345 /* Shell can not handle large exit numbers -> 1 for errors */
346 if( ret < 0 )
347 ret = 1;
348
349 return( ret );
350}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_NET_C &&
352 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
353 MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C && MBEDTLS_CERTS_C &&
354 MBEDTLS_PEM_PARSE_C */