blob: c29ab34a6015ace2ce97b9b25965ff9cc28b3a46 [file] [log] [blame]
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001/*
2 * Simple DTLS client demonstration program
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020018 *
Manuel Pégourié-Gonnarde4d48902015-03-06 13:40:52 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020026#endif
27
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000030#else
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010031#include <stdio.h>
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_CLI_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) || \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020037 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_TIMING_C) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
39 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \
Andres AGcef21e42017-02-02 17:01:10 +000040 !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020041int main( void )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020042{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 mbedtls_printf( "MBEDTLS_SSL_CLI_C and/or MBEDTLS_SSL_PROTO_DTLS and/or "
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020044 "MBEDTLS_NET_C and/or MBEDTLS_TIMING_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
46 "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or "
47 "MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020048 return( 0 );
49}
50#else
51
52#include <string.h>
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020053
Andres AG788aa4a2016-09-14 14:32:09 +010054#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/debug.h"
56#include "mbedtls/ssl.h"
57#include "mbedtls/entropy.h"
58#include "mbedtls/ctr_drbg.h"
59#include "mbedtls/error.h"
60#include "mbedtls/certs.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020061#include "mbedtls/timing.h"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020062
Simon Butcher6fd96ad2018-05-12 18:23:32 +010063/* Uncomment out the following line to default to IPv4 and disable IPv6 */
64//#define FORCE_IPV4
65
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020066#define SERVER_PORT "4433"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020067#define SERVER_NAME "localhost"
Simon Butcher6fd96ad2018-05-12 18:23:32 +010068
69#ifdef FORCE_IPV4
70#define SERVER_ADDR "127.0.0.1" /* Forces IPv4 */
71#else
72#define SERVER_ADDR "::1"
73#endif
74
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020075#define MESSAGE "Echo this"
76
77#define READ_TIMEOUT_MS 1000
78#define MAX_RETRY 5
79
80#define DEBUG_LEVEL 0
81
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020082static void my_debug( void *ctx, int level,
83 const char *file, int line,
84 const char *str )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020085{
86 ((void) level);
87
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020088 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020089 fflush( (FILE *) ctx );
90}
91
92int main( int argc, char *argv[] )
93{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020094 int ret, len;
95 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020096 uint32_t flags;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020097 unsigned char buf[1024];
98 const char *pers = "dtls_client";
99 int retry_left = MAX_RETRY;
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_entropy_context entropy;
102 mbedtls_ctr_drbg_context ctr_drbg;
103 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200104 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_x509_crt cacert;
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200106 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200107
108 ((void) argc);
109 ((void) argv);
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#if defined(MBEDTLS_DEBUG_C)
112 mbedtls_debug_set_threshold( DEBUG_LEVEL );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200113#endif
114
115 /*
116 * 0. Initialize the RNG and the session data
117 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200118 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200119 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200120 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_x509_crt_init( &cacert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200122 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_printf( "\n . Seeding the random number generator..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200125 fflush( stdout );
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200128 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200129 (const unsigned char *) pers,
130 strlen( pers ) ) ) != 0 )
131 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200132 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200133 goto exit;
134 }
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200137
138 /*
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200139 * 0. Load certificates
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_printf( " . Loading the CA root certificate ..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200142 fflush( stdout );
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
145 mbedtls_test_cas_pem_len );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200146 if( ret < 0 )
147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200149 goto exit;
150 }
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_printf( " ok (%d skipped)\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200153
154 /*
155 * 1. Start the connection
156 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200157 mbedtls_printf( " . Connecting to udp/%s/%s...", SERVER_NAME, SERVER_PORT );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200158 fflush( stdout );
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 if( ( ret = mbedtls_net_connect( &server_fd, SERVER_ADDR,
161 SERVER_PORT, MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200164 goto exit;
165 }
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200168
169 /*
170 * 2. Setup stuff
171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_printf( " . Setting up the DTLS structure..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200173 fflush( stdout );
174
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200175 if( ( ret = mbedtls_ssl_config_defaults( &conf,
176 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200177 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
178 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200179 {
180 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
181 goto exit;
182 }
183
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200184 /* OPTIONAL is usually a bad choice for security, but makes interop easier
185 * in this simplified example, in which the ca chain is hardcoded.
186 * Production code should set a proper ca chain and use REQUIRED. */
187 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
188 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
189 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
190 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
191
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200192 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200193 {
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200194 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200195 goto exit;
196 }
197
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100198 if( ( ret = mbedtls_ssl_set_hostname( &ssl, SERVER_NAME ) ) != 0 )
199 {
200 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
201 goto exit;
202 }
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200203
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +0100204 mbedtls_ssl_set_bio( &ssl, &server_fd,
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +0100205 mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200206
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200207 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
208 mbedtls_timing_get_delay );
209
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100210 mbedtls_printf( " ok\n" );
211
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200212 /*
213 * 4. Handshake
214 */
Xinyu Chene1a94a62016-11-22 14:56:18 +0800215 mbedtls_printf( " . Performing the DTLS handshake..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200216 fflush( stdout );
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 do ret = mbedtls_ssl_handshake( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100219 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
220 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200221
222 if( ret != 0 )
223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200225 goto exit;
226 }
227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200229
230 /*
231 * 5. Verify the server certificate
232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 /* In real life, we would have used MBEDTLS_SSL_VERIFY_REQUIRED so that the
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200236 * handshake would not succeed if the peer's cert is bad. Even if we used
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 * MBEDTLS_SSL_VERIFY_OPTIONAL, we would bail out here if ret != 0 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200238 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200239 {
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200240 char vrfy_buf[512];
241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200243
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200244 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200245
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200246 mbedtls_printf( "%s\n", vrfy_buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200247 }
248 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200250
251 /*
252 * 6. Write the echo request
253 */
254send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_printf( " > Write to server:" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200256 fflush( stdout );
257
258 len = sizeof( MESSAGE ) - 1;
259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 do ret = mbedtls_ssl_write( &ssl, (unsigned char *) MESSAGE, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100261 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
262 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200263
264 if( ret < 0 )
265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200267 goto exit;
268 }
269
270 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_printf( " %d bytes written\n\n%s\n\n", len, MESSAGE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200272
273 /*
274 * 7. Read the echo response
275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_printf( " < Read from server:" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200277 fflush( stdout );
278
279 len = sizeof( buf ) - 1;
280 memset( buf, 0, sizeof( buf ) );
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 do ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100283 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
284 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200285
286 if( ret <= 0 )
287 {
288 switch( ret )
289 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100290 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_printf( " timeout\n\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200292 if( retry_left-- > 0 )
293 goto send_request;
294 goto exit;
295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
297 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200298 ret = 0;
299 goto close_notify;
300
301 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200303 goto exit;
304 }
305 }
306
307 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_printf( " %d bytes read\n\n%s\n\n", len, buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200309
310 /*
311 * 8. Done, cleanly close the connection
312 */
313close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200315
316 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100318 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200319 ret = 0;
320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200322
323 /*
324 * 9. Final clean-ups and exit
325 */
326exit:
327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#ifdef MBEDTLS_ERROR_C
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200329 if( ret != 0 )
330 {
331 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 mbedtls_strerror( ret, error_buf, 100 );
333 mbedtls_printf( "Last error was: %d - %s\n\n", ret, error_buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200334 }
335#endif
336
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200337 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 mbedtls_x509_crt_free( &cacert );
340 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200341 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 mbedtls_ctr_drbg_free( &ctr_drbg );
343 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200344
345#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 mbedtls_printf( " + Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200347 fflush( stdout ); getchar();
348#endif
349
350 /* Shell can not handle large exit numbers -> 1 for errors */
351 if( ret < 0 )
352 ret = 1;
353
354 return( ret );
355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_NET_C &&
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200357 MBEDTLD_TIMING_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C && MBEDTLS_CERTS_C &&
359 MBEDTLS_PEM_PARSE_C */