blob: 8c302a0a55fa844fd9b2715ece5fb7c1a8a05e7b [file] [log] [blame]
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001/*
2 * Simple DTLS client demonstration program
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 */
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020024#endif
25
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000028#else
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010029#include <stdio.h>
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020030#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#define mbedtls_printf printf
32#define mbedtls_fprintf fprintf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010033#define mbedtls_exit exit
34#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
35#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000036#endif
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if !defined(MBEDTLS_SSL_CLI_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) || \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020039 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_TIMING_C) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
41 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \
Andres AGcef21e42017-02-02 17:01:10 +000042 !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020043int main( void )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020044{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045 mbedtls_printf( "MBEDTLS_SSL_CLI_C and/or MBEDTLS_SSL_PROTO_DTLS and/or "
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020046 "MBEDTLS_NET_C and/or MBEDTLS_TIMING_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 "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" );
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020050 mbedtls_exit( 0 );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020051}
52#else
53
54#include <string.h>
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020055
Andres AG788aa4a2016-09-14 14:32:09 +010056#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/debug.h"
58#include "mbedtls/ssl.h"
59#include "mbedtls/entropy.h"
60#include "mbedtls/ctr_drbg.h"
61#include "mbedtls/error.h"
62#include "mbedtls/certs.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020063#include "mbedtls/timing.h"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020064
Simon Butcher6fd96ad2018-05-12 18:23:32 +010065/* Uncomment out the following line to default to IPv4 and disable IPv6 */
66//#define FORCE_IPV4
67
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020068#define SERVER_PORT "4433"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020069#define SERVER_NAME "localhost"
Simon Butcher6fd96ad2018-05-12 18:23:32 +010070
71#ifdef FORCE_IPV4
72#define SERVER_ADDR "127.0.0.1" /* Forces IPv4 */
73#else
74#define SERVER_ADDR "::1"
75#endif
76
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020077#define MESSAGE "Echo this"
78
79#define READ_TIMEOUT_MS 1000
80#define MAX_RETRY 5
81
82#define DEBUG_LEVEL 0
83
Simon Butcher63cb97e2018-12-06 17:43:31 +000084
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020085static void my_debug( void *ctx, int level,
86 const char *file, int line,
87 const char *str )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020088{
89 ((void) level);
90
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020091 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020092 fflush( (FILE *) ctx );
93}
94
95int main( int argc, char *argv[] )
96{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020097 int ret, len;
98 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020099 uint32_t flags;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200100 unsigned char buf[1024];
101 const char *pers = "dtls_client";
102 int retry_left = MAX_RETRY;
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_entropy_context entropy;
105 mbedtls_ctr_drbg_context ctr_drbg;
106 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200107 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_x509_crt cacert;
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200109 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200110
111 ((void) argc);
112 ((void) argv);
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_DEBUG_C)
115 mbedtls_debug_set_threshold( DEBUG_LEVEL );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200116#endif
117
118 /*
119 * 0. Initialize the RNG and the session data
120 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200121 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200122 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200123 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_x509_crt_init( &cacert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200125 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_printf( "\n . Seeding the random number generator..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200128 fflush( stdout );
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200131 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200132 (const unsigned char *) pers,
133 strlen( pers ) ) ) != 0 )
134 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200135 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200136 goto exit;
137 }
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200140
141 /*
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200142 * 0. Load certificates
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_printf( " . Loading the CA root certificate ..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200145 fflush( stdout );
146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
148 mbedtls_test_cas_pem_len );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200149 if( ret < 0 )
150 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200151 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", (unsigned int) -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200152 goto exit;
153 }
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_printf( " ok (%d skipped)\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200156
157 /*
158 * 1. Start the connection
159 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200160 mbedtls_printf( " . Connecting to udp/%s/%s...", SERVER_NAME, SERVER_PORT );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200161 fflush( stdout );
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 if( ( ret = mbedtls_net_connect( &server_fd, SERVER_ADDR,
164 SERVER_PORT, MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200167 goto exit;
168 }
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200171
172 /*
173 * 2. Setup stuff
174 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_printf( " . Setting up the DTLS structure..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200176 fflush( stdout );
177
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200178 if( ( ret = mbedtls_ssl_config_defaults( &conf,
179 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200180 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
181 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200182 {
183 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
184 goto exit;
185 }
186
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200187 /* OPTIONAL is usually a bad choice for security, but makes interop easier
188 * in this simplified example, in which the ca chain is hardcoded.
189 * Production code should set a proper ca chain and use REQUIRED. */
190 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
191 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
192 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
193 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Ryan LaPointe59244e82021-03-01 10:02:35 -0500194 mbedtls_ssl_conf_read_timeout( &conf, READ_TIMEOUT_MS );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200195
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200196 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200197 {
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200198 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200199 goto exit;
200 }
201
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100202 if( ( ret = mbedtls_ssl_set_hostname( &ssl, SERVER_NAME ) ) != 0 )
203 {
204 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
205 goto exit;
206 }
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200207
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +0100208 mbedtls_ssl_set_bio( &ssl, &server_fd,
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +0100209 mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200210
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200211 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
212 mbedtls_timing_get_delay );
213
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100214 mbedtls_printf( " ok\n" );
215
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200216 /*
217 * 4. Handshake
218 */
Xinyu Chene1a94a62016-11-22 14:56:18 +0800219 mbedtls_printf( " . Performing the DTLS handshake..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200220 fflush( stdout );
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 do ret = mbedtls_ssl_handshake( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100223 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
224 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200225
226 if( ret != 0 )
227 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200228 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", (unsigned int) -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200229 goto exit;
230 }
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200233
234 /*
235 * 5. Verify the server certificate
236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 /* In real life, we would have used MBEDTLS_SSL_VERIFY_REQUIRED so that the
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200240 * handshake would not succeed if the peer's cert is bad. Even if we used
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 * MBEDTLS_SSL_VERIFY_OPTIONAL, we would bail out here if ret != 0 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200242 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200243 {
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200244 char vrfy_buf[512];
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200247
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200248 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200249
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200250 mbedtls_printf( "%s\n", vrfy_buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200251 }
252 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200254
255 /*
256 * 6. Write the echo request
257 */
258send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_printf( " > Write to server:" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200260 fflush( stdout );
261
262 len = sizeof( MESSAGE ) - 1;
263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 do ret = mbedtls_ssl_write( &ssl, (unsigned char *) MESSAGE, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100265 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
266 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200267
268 if( ret < 0 )
269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200271 goto exit;
272 }
273
274 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_printf( " %d bytes written\n\n%s\n\n", len, MESSAGE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200276
277 /*
278 * 7. Read the echo response
279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_printf( " < Read from server:" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200281 fflush( stdout );
282
283 len = sizeof( buf ) - 1;
284 memset( buf, 0, sizeof( buf ) );
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 do ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100287 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
288 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200289
290 if( ret <= 0 )
291 {
292 switch( ret )
293 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100294 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_printf( " timeout\n\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200296 if( retry_left-- > 0 )
297 goto send_request;
298 goto exit;
299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
301 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200302 ret = 0;
303 goto close_notify;
304
305 default:
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200306 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n\n", (unsigned int) -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200307 goto exit;
308 }
309 }
310
311 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_printf( " %d bytes read\n\n%s\n\n", len, buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200313
314 /*
315 * 8. Done, cleanly close the connection
316 */
317close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200319
320 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100322 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200323 ret = 0;
324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200326
327 /*
328 * 9. Final clean-ups and exit
329 */
330exit:
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#ifdef MBEDTLS_ERROR_C
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200333 if( ret != 0 )
334 {
335 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_strerror( ret, error_buf, 100 );
337 mbedtls_printf( "Last error was: %d - %s\n\n", ret, error_buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200338 }
339#endif
340
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200341 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_x509_crt_free( &cacert );
344 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200345 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 mbedtls_ctr_drbg_free( &ctr_drbg );
347 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200348
349#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 mbedtls_printf( " + Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200351 fflush( stdout ); getchar();
352#endif
353
354 /* Shell can not handle large exit numbers -> 1 for errors */
355 if( ret < 0 )
356 ret = 1;
357
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200358 mbedtls_exit( ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200359}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_NET_C &&
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200361 MBEDTLD_TIMING_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C && MBEDTLS_CERTS_C &&
363 MBEDTLS_PEM_PARSE_C */