blob: b72981ca5d53f22bcdb4d098336f34213e85aa02 [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>
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020032#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_printf printf
34#define mbedtls_fprintf fprintf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020035#define mbedtls_exit exit
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 Stachowiaka0865222019-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 Butcher5c0204e2018-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 Butcher5c0204e2018-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
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020084static void my_debug( void *ctx, int level,
85 const char *file, int line,
86 const char *str )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020087{
88 ((void) level);
89
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020090 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020091 fflush( (FILE *) ctx );
92}
93
94int main( int argc, char *argv[] )
95{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020096 int ret, len;
97 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020098 uint32_t flags;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020099 unsigned char buf[1024];
100 const char *pers = "dtls_client";
101 int retry_left = MAX_RETRY;
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_entropy_context entropy;
104 mbedtls_ctr_drbg_context ctr_drbg;
105 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200106 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_x509_crt cacert;
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200108 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200109
110 ((void) argc);
111 ((void) argv);
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_DEBUG_C)
114 mbedtls_debug_set_threshold( DEBUG_LEVEL );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200115#endif
116
117 /*
118 * 0. Initialize the RNG and the session data
119 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200120 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200121 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200122 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_x509_crt_init( &cacert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200124 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_printf( "\n . Seeding the random number generator..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200127 fflush( stdout );
128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200130 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200131 (const unsigned char *) pers,
132 strlen( pers ) ) ) != 0 )
133 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200134 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200135 goto exit;
136 }
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200139
140 /*
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200141 * 0. Load certificates
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_printf( " . Loading the CA root certificate ..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200144 fflush( stdout );
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
147 mbedtls_test_cas_pem_len );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200148 if( ret < 0 )
149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200151 goto exit;
152 }
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_printf( " ok (%d skipped)\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200155
156 /*
157 * 1. Start the connection
158 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200159 mbedtls_printf( " . Connecting to udp/%s/%s...", SERVER_NAME, SERVER_PORT );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200160 fflush( stdout );
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 if( ( ret = mbedtls_net_connect( &server_fd, SERVER_ADDR,
163 SERVER_PORT, MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200166 goto exit;
167 }
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200170
171 /*
172 * 2. Setup stuff
173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_printf( " . Setting up the DTLS structure..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200175 fflush( stdout );
176
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200177 if( ( ret = mbedtls_ssl_config_defaults( &conf,
178 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200179 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
180 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200181 {
182 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
183 goto exit;
184 }
185
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200186 /* OPTIONAL is usually a bad choice for security, but makes interop easier
187 * in this simplified example, in which the ca chain is hardcoded.
188 * Production code should set a proper ca chain and use REQUIRED. */
189 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
190 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
191 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
192 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
193
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200194 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200195 {
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200196 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200197 goto exit;
198 }
199
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100200 if( ( ret = mbedtls_ssl_set_hostname( &ssl, SERVER_NAME ) ) != 0 )
201 {
202 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
203 goto exit;
204 }
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200205
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +0100206 mbedtls_ssl_set_bio( &ssl, &server_fd,
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +0100207 mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200208
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200209 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
210 mbedtls_timing_get_delay );
211
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100212 mbedtls_printf( " ok\n" );
213
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200214 /*
215 * 4. Handshake
216 */
Xinyu Chene1a94a62016-11-22 14:56:18 +0800217 mbedtls_printf( " . Performing the DTLS handshake..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200218 fflush( stdout );
219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 do ret = mbedtls_ssl_handshake( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100221 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
222 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200223
224 if( ret != 0 )
225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200227 goto exit;
228 }
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200231
232 /*
233 * 5. Verify the server certificate
234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 /* In real life, we would have used MBEDTLS_SSL_VERIFY_REQUIRED so that the
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200238 * handshake would not succeed if the peer's cert is bad. Even if we used
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 * MBEDTLS_SSL_VERIFY_OPTIONAL, we would bail out here if ret != 0 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200240 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200241 {
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200242 char vrfy_buf[512];
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200245
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200246 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200247
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200248 mbedtls_printf( "%s\n", vrfy_buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200249 }
250 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200252
253 /*
254 * 6. Write the echo request
255 */
256send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_printf( " > Write to server:" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200258 fflush( stdout );
259
260 len = sizeof( MESSAGE ) - 1;
261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 do ret = mbedtls_ssl_write( &ssl, (unsigned char *) MESSAGE, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100263 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
264 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200265
266 if( ret < 0 )
267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200269 goto exit;
270 }
271
272 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_printf( " %d bytes written\n\n%s\n\n", len, MESSAGE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200274
275 /*
276 * 7. Read the echo response
277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_printf( " < Read from server:" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200279 fflush( stdout );
280
281 len = sizeof( buf ) - 1;
282 memset( buf, 0, sizeof( buf ) );
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 do ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100285 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
286 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200287
288 if( ret <= 0 )
289 {
290 switch( ret )
291 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100292 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_printf( " timeout\n\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200294 if( retry_left-- > 0 )
295 goto send_request;
296 goto exit;
297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
299 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200300 ret = 0;
301 goto close_notify;
302
303 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200305 goto exit;
306 }
307 }
308
309 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 mbedtls_printf( " %d bytes read\n\n%s\n\n", len, buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200311
312 /*
313 * 8. Done, cleanly close the connection
314 */
315close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200317
318 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100320 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200321 ret = 0;
322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200324
325 /*
326 * 9. Final clean-ups and exit
327 */
328exit:
329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330#ifdef MBEDTLS_ERROR_C
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200331 if( ret != 0 )
332 {
333 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_strerror( ret, error_buf, 100 );
335 mbedtls_printf( "Last error was: %d - %s\n\n", ret, error_buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200336 }
337#endif
338
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200339 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 mbedtls_x509_crt_free( &cacert );
342 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200343 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 mbedtls_ctr_drbg_free( &ctr_drbg );
345 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200346
347#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_printf( " + Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200349 fflush( stdout ); getchar();
350#endif
351
352 /* Shell can not handle large exit numbers -> 1 for errors */
353 if( ret < 0 )
354 ret = 1;
355
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200356 mbedtls_exit( ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200357}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_NET_C &&
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200359 MBEDTLD_TIMING_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C && MBEDTLS_CERTS_C &&
361 MBEDTLS_PEM_PARSE_C */