blob: 336d6958c3d4fdfc6f5b04a0f93b82ea571e0482 [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é-Gonnard3ef6a6d2018-12-10 14:31:45 +010034#define mbedtls_exit exit
35#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
36#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000037#endif
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if !defined(MBEDTLS_SSL_CLI_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) || \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020040 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_TIMING_C) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
42 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \
Andres AGcef21e42017-02-02 17:01:10 +000043 !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020044int main( void )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020045{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 mbedtls_printf( "MBEDTLS_SSL_CLI_C and/or MBEDTLS_SSL_PROTO_DTLS and/or "
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020047 "MBEDTLS_NET_C and/or MBEDTLS_TIMING_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
49 "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or "
50 "MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020051 return( 0 );
52}
53#else
54
55#include <string.h>
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020056
Andres AG788aa4a2016-09-14 14:32:09 +010057#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/debug.h"
59#include "mbedtls/ssl.h"
60#include "mbedtls/entropy.h"
61#include "mbedtls/ctr_drbg.h"
62#include "mbedtls/error.h"
63#include "mbedtls/certs.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020064#include "mbedtls/timing.h"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020065
Simon Butcher6fd96ad2018-05-12 18:23:32 +010066/* Uncomment out the following line to default to IPv4 and disable IPv6 */
67//#define FORCE_IPV4
68
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020069#define SERVER_PORT "4433"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020070#define SERVER_NAME "localhost"
Simon Butcher6fd96ad2018-05-12 18:23:32 +010071
72#ifdef FORCE_IPV4
73#define SERVER_ADDR "127.0.0.1" /* Forces IPv4 */
74#else
75#define SERVER_ADDR "::1"
76#endif
77
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020078#define MESSAGE "Echo this"
79
80#define READ_TIMEOUT_MS 1000
81#define MAX_RETRY 5
82
83#define DEBUG_LEVEL 0
84
Simon Butcher63cb97e2018-12-06 17:43:31 +000085
Hanno Becker14a4a442019-07-02 17:00:34 +010086#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020087static void my_debug( void *ctx, int level,
88 const char *file, int line,
89 const char *str )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020090{
91 ((void) level);
92
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020093 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020094 fflush( (FILE *) ctx );
95}
Hanno Becker14a4a442019-07-02 17:00:34 +010096#endif /* MBEDTLS_DEBUG_C */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020097
Hanno Becker572d4482019-07-23 13:47:53 +010098#if defined(MBEDTLS_SSL_CONF_RNG)
99int rng_wrap( void *ctx, unsigned char *dst, size_t len );
100
101mbedtls_ctr_drbg_context *rng_ctx_global = NULL;
102int rng_wrap( void *ctx, unsigned char *dst, size_t len )
103{
104 /* We expect the NULL parameter here. */
105 if( ctx != NULL )
106 return( -1 );
107
108 return( mbedtls_ctr_drbg_random( rng_ctx_global, dst, len ) );
109}
110#endif /* MBEDTLS_SSL_CONF_RNG */
111
Jarno Lamsa642596e2019-10-04 12:52:42 +0300112#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
113int mbedtls_hardware_poll( void *data, unsigned char *output,
114 size_t len, size_t *olen )
115{
116 size_t i;
117 (void) data;
118 for( i = 0; i < len; ++i )
119 output[i] = rand();
120 *olen = len;
121 return( 0 );
122}
123#endif
124
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200125int main( int argc, char *argv[] )
126{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200127 int ret, len;
128 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200129 uint32_t flags;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200130 unsigned char buf[1024];
131 const char *pers = "dtls_client";
132 int retry_left = MAX_RETRY;
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_entropy_context entropy;
135 mbedtls_ctr_drbg_context ctr_drbg;
136 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200137 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_x509_crt cacert;
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200139 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200140
141 ((void) argc);
142 ((void) argv);
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_DEBUG_C)
145 mbedtls_debug_set_threshold( DEBUG_LEVEL );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200146#endif
147
148 /*
149 * 0. Initialize the RNG and the session data
150 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200151 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200152 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200153 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_x509_crt_init( &cacert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200155 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_printf( "\n . Seeding the random number generator..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200158 fflush( stdout );
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200161 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200162 (const unsigned char *) pers,
163 strlen( pers ) ) ) != 0 )
164 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200165 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\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 /*
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200172 * 0. Load certificates
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_printf( " . Loading the CA root certificate ..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200175 fflush( stdout );
176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
178 mbedtls_test_cas_pem_len );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200179 if( ret < 0 )
180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200182 goto exit;
183 }
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_printf( " ok (%d skipped)\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200186
187 /*
188 * 1. Start the connection
189 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200190 mbedtls_printf( " . Connecting to udp/%s/%s...", SERVER_NAME, SERVER_PORT );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200191 fflush( stdout );
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 if( ( ret = mbedtls_net_connect( &server_fd, SERVER_ADDR,
194 SERVER_PORT, MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200197 goto exit;
198 }
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200201
202 /*
203 * 2. Setup stuff
204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_printf( " . Setting up the DTLS structure..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200206 fflush( stdout );
207
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200208 if( ( ret = mbedtls_ssl_config_defaults( &conf,
209 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200210 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
211 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200212 {
213 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
214 goto exit;
215 }
216
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200217 /* OPTIONAL is usually a bad choice for security, but makes interop easier
218 * in this simplified example, in which the ca chain is hardcoded.
219 * Production code should set a proper ca chain and use REQUIRED. */
220 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
221 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Hanno Becker572d4482019-07-23 13:47:53 +0100222
223#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200224 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
Hanno Becker572d4482019-07-23 13:47:53 +0100225#else
226 rng_ctx_global = &ctr_drbg;
227#endif
228
Hanno Becker14a4a442019-07-02 17:00:34 +0100229#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200230 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Hanno Becker14a4a442019-07-02 17:00:34 +0100231#endif
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200232
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200233 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200234 {
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200235 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200236 goto exit;
237 }
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300238#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100239 if( ( ret = mbedtls_ssl_set_hostname( &ssl, SERVER_NAME ) ) != 0 )
240 {
241 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
242 goto exit;
243 }
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300244#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200245
Hanno Beckera58a8962019-06-13 16:11:15 +0100246#if !defined(MBEDTLS_SSL_CONF_RECV) && \
247 !defined(MBEDTLS_SSL_CONF_SEND) && \
248 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
249 mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
250#else
251 mbedtls_ssl_set_bio_ctx( &ssl, &server_fd );
252#endif
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200253
Hanno Becker0ae6b242019-06-13 16:45:36 +0100254#if defined(MBEDTLS_TIMING_C)
255#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
256 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200257 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
258 mbedtls_timing_get_delay );
Hanno Becker0ae6b242019-06-13 16:45:36 +0100259#else
260 mbedtls_ssl_set_timer_cb_ctx( &ssl, &timer );
261#endif
262#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200263
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100264 mbedtls_printf( " ok\n" );
265
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200266 /*
267 * 4. Handshake
268 */
Xinyu Chene1a94a62016-11-22 14:56:18 +0800269 mbedtls_printf( " . Performing the DTLS handshake..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200270 fflush( stdout );
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 do ret = mbedtls_ssl_handshake( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100273 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
274 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200275
276 if( ret != 0 )
277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200279 goto exit;
280 }
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200283
284 /*
285 * 5. Verify the server certificate
286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 /* In real life, we would have used MBEDTLS_SSL_VERIFY_REQUIRED so that the
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200290 * handshake would not succeed if the peer's cert is bad. Even if we used
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 * MBEDTLS_SSL_VERIFY_OPTIONAL, we would bail out here if ret != 0 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200292 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200293 {
Hanno Becker02a21932019-06-10 15:08:43 +0100294#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200295 char vrfy_buf[512];
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600296#endif
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200299
Hanno Becker02a21932019-06-10 15:08:43 +0100300#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200301 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200302
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200303 mbedtls_printf( "%s\n", vrfy_buf );
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600304#endif
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200305 }
306 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200308
309 /*
310 * 6. Write the echo request
311 */
312send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_printf( " > Write to server:" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200314 fflush( stdout );
315
316 len = sizeof( MESSAGE ) - 1;
317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 do ret = mbedtls_ssl_write( &ssl, (unsigned char *) MESSAGE, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100319 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
320 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200321
322 if( ret < 0 )
323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200325 goto exit;
326 }
327
328 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_printf( " %d bytes written\n\n%s\n\n", len, MESSAGE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200330
331 /*
332 * 7. Read the echo response
333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_printf( " < Read from server:" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200335 fflush( stdout );
336
337 len = sizeof( buf ) - 1;
338 memset( buf, 0, sizeof( buf ) );
339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 do ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100341 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
342 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200343
344 if( ret <= 0 )
345 {
346 switch( ret )
347 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100348 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_printf( " timeout\n\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200350 if( retry_left-- > 0 )
351 goto send_request;
352 goto exit;
353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
355 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200356 ret = 0;
357 goto close_notify;
358
359 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200361 goto exit;
362 }
363 }
364
365 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_printf( " %d bytes read\n\n%s\n\n", len, buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200367
368 /*
369 * 8. Done, cleanly close the connection
370 */
371close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200373
374 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100376 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200377 ret = 0;
378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200380
381 /*
382 * 9. Final clean-ups and exit
383 */
384exit:
385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386#ifdef MBEDTLS_ERROR_C
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200387 if( ret != 0 )
388 {
389 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_strerror( ret, error_buf, 100 );
391 mbedtls_printf( "Last error was: %d - %s\n\n", ret, error_buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200392 }
393#endif
394
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200395 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_x509_crt_free( &cacert );
398 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200399 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_ctr_drbg_free( &ctr_drbg );
401 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200402
403#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 mbedtls_printf( " + Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200405 fflush( stdout ); getchar();
406#endif
407
408 /* Shell can not handle large exit numbers -> 1 for errors */
409 if( ret < 0 )
410 ret = 1;
411
412 return( ret );
413}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_NET_C &&
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200415 MBEDTLD_TIMING_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C && MBEDTLS_CERTS_C &&
417 MBEDTLS_PEM_PARSE_C */