blob: 20e7688ea1a5a9591409de4598d1e5d234000ec0 [file] [log] [blame]
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001/*
2 * Simple DTLS client demonstration program
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020051#endif
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000055#else
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010056#include <stdio.h>
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020057#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_printf printf
59#define mbedtls_fprintf fprintf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020060#define mbedtls_exit exit
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000061#endif
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if !defined(MBEDTLS_SSL_CLI_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) || \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020064 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_TIMING_C) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
66 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \
Andres AGcef21e42017-02-02 17:01:10 +000067 !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020068int main( void )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020069{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_printf( "MBEDTLS_SSL_CLI_C and/or MBEDTLS_SSL_PROTO_DTLS and/or "
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020071 "MBEDTLS_NET_C and/or MBEDTLS_TIMING_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
73 "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or "
74 "MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.\n" );
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020075 mbedtls_exit( 0 );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020076}
77#else
78
79#include <string.h>
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020080
Andres AG788aa4a2016-09-14 14:32:09 +010081#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000082#include "mbedtls/debug.h"
83#include "mbedtls/ssl.h"
84#include "mbedtls/entropy.h"
85#include "mbedtls/ctr_drbg.h"
86#include "mbedtls/error.h"
87#include "mbedtls/certs.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020088#include "mbedtls/timing.h"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020089
Simon Butcher5c0204e2018-05-12 18:23:32 +010090/* Uncomment out the following line to default to IPv4 and disable IPv6 */
91//#define FORCE_IPV4
92
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020093#define SERVER_PORT "4433"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020094#define SERVER_NAME "localhost"
Simon Butcher5c0204e2018-05-12 18:23:32 +010095
96#ifdef FORCE_IPV4
97#define SERVER_ADDR "127.0.0.1" /* Forces IPv4 */
98#else
99#define SERVER_ADDR "::1"
100#endif
101
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200102#define MESSAGE "Echo this"
103
104#define READ_TIMEOUT_MS 1000
105#define MAX_RETRY 5
106
107#define DEBUG_LEVEL 0
108
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200109static void my_debug( void *ctx, int level,
110 const char *file, int line,
111 const char *str )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200112{
113 ((void) level);
114
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200115 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200116 fflush( (FILE *) ctx );
117}
118
119int main( int argc, char *argv[] )
120{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200121 int ret, len;
122 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200123 uint32_t flags;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200124 unsigned char buf[1024];
125 const char *pers = "dtls_client";
126 int retry_left = MAX_RETRY;
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_entropy_context entropy;
129 mbedtls_ctr_drbg_context ctr_drbg;
130 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200131 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_x509_crt cacert;
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200133 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200134
135 ((void) argc);
136 ((void) argv);
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_DEBUG_C)
139 mbedtls_debug_set_threshold( DEBUG_LEVEL );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200140#endif
141
142 /*
143 * 0. Initialize the RNG and the session data
144 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200145 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200146 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200147 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_x509_crt_init( &cacert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200149 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( "\n . Seeding the random number generator..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200152 fflush( stdout );
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200155 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200156 (const unsigned char *) pers,
157 strlen( pers ) ) ) != 0 )
158 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200159 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200160 goto exit;
161 }
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200164
165 /*
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200166 * 0. Load certificates
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_printf( " . Loading the CA root certificate ..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200169 fflush( stdout );
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
172 mbedtls_test_cas_pem_len );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200173 if( ret < 0 )
174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200176 goto exit;
177 }
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_printf( " ok (%d skipped)\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200180
181 /*
182 * 1. Start the connection
183 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200184 mbedtls_printf( " . Connecting to udp/%s/%s...", SERVER_NAME, SERVER_PORT );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200185 fflush( stdout );
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 if( ( ret = mbedtls_net_connect( &server_fd, SERVER_ADDR,
188 SERVER_PORT, MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200191 goto exit;
192 }
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200195
196 /*
197 * 2. Setup stuff
198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_printf( " . Setting up the DTLS structure..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200200 fflush( stdout );
201
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200202 if( ( ret = mbedtls_ssl_config_defaults( &conf,
203 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200204 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
205 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200206 {
207 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
208 goto exit;
209 }
210
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200211 /* OPTIONAL is usually a bad choice for security, but makes interop easier
212 * in this simplified example, in which the ca chain is hardcoded.
213 * Production code should set a proper ca chain and use REQUIRED. */
214 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
215 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
216 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
217 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
218
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200219 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200220 {
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200221 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200222 goto exit;
223 }
224
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100225 if( ( ret = mbedtls_ssl_set_hostname( &ssl, SERVER_NAME ) ) != 0 )
226 {
227 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
228 goto exit;
229 }
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200230
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +0100231 mbedtls_ssl_set_bio( &ssl, &server_fd,
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +0100232 mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200233
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200234 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
235 mbedtls_timing_get_delay );
236
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100237 mbedtls_printf( " ok\n" );
238
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200239 /*
240 * 4. Handshake
241 */
Xinyu Chene1a94a62016-11-22 14:56:18 +0800242 mbedtls_printf( " . Performing the DTLS handshake..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200243 fflush( stdout );
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 do ret = mbedtls_ssl_handshake( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100246 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
247 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200248
249 if( ret != 0 )
250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200252 goto exit;
253 }
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200256
257 /*
258 * 5. Verify the server certificate
259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 /* In real life, we would have used MBEDTLS_SSL_VERIFY_REQUIRED so that the
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200263 * handshake would not succeed if the peer's cert is bad. Even if we used
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 * MBEDTLS_SSL_VERIFY_OPTIONAL, we would bail out here if ret != 0 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200265 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200266 {
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200267 char vrfy_buf[512];
268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200270
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200271 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200272
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200273 mbedtls_printf( "%s\n", vrfy_buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200274 }
275 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200277
278 /*
279 * 6. Write the echo request
280 */
281send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_printf( " > Write to server:" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200283 fflush( stdout );
284
285 len = sizeof( MESSAGE ) - 1;
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 do ret = mbedtls_ssl_write( &ssl, (unsigned char *) MESSAGE, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100288 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
289 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200290
291 if( ret < 0 )
292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200294 goto exit;
295 }
296
297 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_printf( " %d bytes written\n\n%s\n\n", len, MESSAGE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200299
300 /*
301 * 7. Read the echo response
302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_printf( " < Read from server:" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200304 fflush( stdout );
305
306 len = sizeof( buf ) - 1;
307 memset( buf, 0, sizeof( buf ) );
308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 do ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100310 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
311 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200312
313 if( ret <= 0 )
314 {
315 switch( ret )
316 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100317 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 mbedtls_printf( " timeout\n\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200319 if( retry_left-- > 0 )
320 goto send_request;
321 goto exit;
322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
324 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200325 ret = 0;
326 goto close_notify;
327
328 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200330 goto exit;
331 }
332 }
333
334 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_printf( " %d bytes read\n\n%s\n\n", len, buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200336
337 /*
338 * 8. Done, cleanly close the connection
339 */
340close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200342
343 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100345 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200346 ret = 0;
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200349
350 /*
351 * 9. Final clean-ups and exit
352 */
353exit:
354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#ifdef MBEDTLS_ERROR_C
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200356 if( ret != 0 )
357 {
358 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_strerror( ret, error_buf, 100 );
360 mbedtls_printf( "Last error was: %d - %s\n\n", ret, error_buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200361 }
362#endif
363
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200364 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_x509_crt_free( &cacert );
367 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200368 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_ctr_drbg_free( &ctr_drbg );
370 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200371
372#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_printf( " + Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200374 fflush( stdout ); getchar();
375#endif
376
377 /* Shell can not handle large exit numbers -> 1 for errors */
378 if( ret < 0 )
379 ret = 1;
380
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200381 mbedtls_exit( ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200382}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_NET_C &&
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200384 MBEDTLD_TIMING_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C && MBEDTLS_CERTS_C &&
386 MBEDTLS_PEM_PARSE_C */