| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Simple DTLS client demonstration program | 
|  | 3 | * | 
| Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | *  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é-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 18 | * | 
| Manuel Pégourié-Gonnard | e4d4890 | 2015-03-06 13:40:52 +0000 | [diff] [blame] | 19 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 20 | */ | 
|  | 21 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 24 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 26 | #endif | 
|  | 27 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PLATFORM_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/platform.h" | 
| Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame] | 30 | #else | 
| Manuel Pégourié-Gonnard | 4b3e5ef | 2015-03-27 11:24:27 +0100 | [diff] [blame] | 31 | #include <stdio.h> | 
| Andrzej Kurek | 825ebd4 | 2020-05-18 11:47:25 -0400 | [diff] [blame] | 32 | #include <stdlib.h> | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #define mbedtls_printf     printf | 
|  | 34 | #define mbedtls_fprintf    fprintf | 
| Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 35 | #define mbedtls_exit            exit | 
|  | 36 | #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS | 
|  | 37 | #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE | 
| Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame] | 38 | #endif | 
|  | 39 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | #if !defined(MBEDTLS_SSL_CLI_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) ||    \ | 
| Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 41 | !defined(MBEDTLS_NET_C)  || !defined(MBEDTLS_TIMING_C) ||             \ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) ||        \ | 
|  | 43 | !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) ||      \ | 
| Andres AG | cef21e4 | 2017-02-02 17:01:10 +0000 | [diff] [blame] | 44 | !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C) | 
| Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 45 | int main( void ) | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 46 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | mbedtls_printf( "MBEDTLS_SSL_CLI_C and/or MBEDTLS_SSL_PROTO_DTLS and/or " | 
| Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 48 | "MBEDTLS_NET_C and/or MBEDTLS_TIMING_C and/or " | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or " | 
|  | 50 | "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or " | 
|  | 51 | "MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.\n" ); | 
| Andrzej Kurek | 825ebd4 | 2020-05-18 11:47:25 -0400 | [diff] [blame] | 52 | mbedtls_exit( 0 ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 53 | } | 
|  | 54 | #else | 
|  | 55 |  | 
|  | 56 | #include <string.h> | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 57 |  | 
| Andres AG | 788aa4a | 2016-09-14 14:32:09 +0100 | [diff] [blame] | 58 | #include "mbedtls/net_sockets.h" | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 59 | #include "mbedtls/debug.h" | 
|  | 60 | #include "mbedtls/ssl.h" | 
|  | 61 | #include "mbedtls/entropy.h" | 
|  | 62 | #include "mbedtls/ctr_drbg.h" | 
|  | 63 | #include "mbedtls/error.h" | 
|  | 64 | #include "mbedtls/certs.h" | 
| Manuel Pégourié-Gonnard | 56273da | 2015-05-26 12:19:45 +0200 | [diff] [blame] | 65 | #include "mbedtls/timing.h" | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 66 |  | 
| Simon Butcher | 6fd96ad | 2018-05-12 18:23:32 +0100 | [diff] [blame] | 67 | /* Uncomment out the following line to default to IPv4 and disable IPv6 */ | 
|  | 68 | //#define FORCE_IPV4 | 
|  | 69 |  | 
| Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 70 | #define SERVER_PORT "4433" | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 71 | #define SERVER_NAME "localhost" | 
| Simon Butcher | 6fd96ad | 2018-05-12 18:23:32 +0100 | [diff] [blame] | 72 |  | 
|  | 73 | #ifdef FORCE_IPV4 | 
|  | 74 | #define SERVER_ADDR "127.0.0.1"     /* Forces IPv4 */ | 
|  | 75 | #else | 
|  | 76 | #define SERVER_ADDR "::1" | 
|  | 77 | #endif | 
|  | 78 |  | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 79 | #define MESSAGE     "Echo this" | 
|  | 80 |  | 
|  | 81 | #define READ_TIMEOUT_MS 1000 | 
|  | 82 | #define MAX_RETRY       5 | 
|  | 83 |  | 
|  | 84 | #define DEBUG_LEVEL 0 | 
|  | 85 |  | 
| Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 86 |  | 
| Hanno Becker | 14a4a44 | 2019-07-02 17:00:34 +0100 | [diff] [blame] | 87 | #if defined(MBEDTLS_DEBUG_C) | 
| Manuel Pégourié-Gonnard | 61ee351 | 2015-06-23 17:35:03 +0200 | [diff] [blame] | 88 | static void my_debug( void *ctx, int level, | 
|  | 89 | const char *file, int line, | 
|  | 90 | const char *str ) | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 91 | { | 
|  | 92 | ((void) level); | 
|  | 93 |  | 
| Manuel Pégourié-Gonnard | 61ee351 | 2015-06-23 17:35:03 +0200 | [diff] [blame] | 94 | mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 95 | fflush(  (FILE *) ctx  ); | 
|  | 96 | } | 
| Hanno Becker | 14a4a44 | 2019-07-02 17:00:34 +0100 | [diff] [blame] | 97 | #endif /* MBEDTLS_DEBUG_C */ | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 98 |  | 
| Hanno Becker | 572d448 | 2019-07-23 13:47:53 +0100 | [diff] [blame] | 99 | #if defined(MBEDTLS_SSL_CONF_RNG) | 
|  | 100 | int rng_wrap( void *ctx, unsigned char *dst, size_t len ); | 
|  | 101 |  | 
|  | 102 | mbedtls_ctr_drbg_context *rng_ctx_global = NULL; | 
|  | 103 | int rng_wrap( void *ctx, unsigned char *dst, size_t len ) | 
|  | 104 | { | 
|  | 105 | /* We expect the NULL parameter here. */ | 
|  | 106 | if( ctx != NULL ) | 
|  | 107 | return( -1 ); | 
|  | 108 |  | 
|  | 109 | return( mbedtls_ctr_drbg_random( rng_ctx_global, dst, len ) ); | 
|  | 110 | } | 
|  | 111 | #endif /* MBEDTLS_SSL_CONF_RNG */ | 
|  | 112 |  | 
| Jarno Lamsa | 642596e | 2019-10-04 12:52:42 +0300 | [diff] [blame] | 113 | #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) | 
|  | 114 | int mbedtls_hardware_poll( void *data, unsigned char *output, | 
|  | 115 | size_t len, size_t *olen ) | 
|  | 116 | { | 
|  | 117 | size_t i; | 
|  | 118 | (void) data; | 
|  | 119 | for( i = 0; i < len; ++i ) | 
|  | 120 | output[i] = rand(); | 
|  | 121 | *olen = len; | 
|  | 122 | return( 0 ); | 
|  | 123 | } | 
|  | 124 | #endif | 
|  | 125 |  | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 126 | int main( int argc, char *argv[] ) | 
|  | 127 | { | 
| Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 128 | int ret, len; | 
|  | 129 | mbedtls_net_context server_fd; | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 130 | uint32_t flags; | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 131 | unsigned char buf[1024]; | 
|  | 132 | const char *pers = "dtls_client"; | 
|  | 133 | int retry_left = MAX_RETRY; | 
|  | 134 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 135 | mbedtls_entropy_context entropy; | 
|  | 136 | mbedtls_ctr_drbg_context ctr_drbg; | 
|  | 137 | mbedtls_ssl_context ssl; | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 138 | mbedtls_ssl_config conf; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | mbedtls_x509_crt cacert; | 
| Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 140 | mbedtls_timing_delay_context timer; | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 141 |  | 
|  | 142 | ((void) argc); | 
|  | 143 | ((void) argv); | 
|  | 144 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | #if defined(MBEDTLS_DEBUG_C) | 
|  | 146 | mbedtls_debug_set_threshold( DEBUG_LEVEL ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 147 | #endif | 
|  | 148 |  | 
|  | 149 | /* | 
|  | 150 | * 0. Initialize the RNG and the session data | 
|  | 151 | */ | 
| Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 152 | mbedtls_net_init( &server_fd ); | 
| Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 153 | mbedtls_ssl_init( &ssl ); | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 154 | mbedtls_ssl_config_init( &conf ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | mbedtls_x509_crt_init( &cacert ); | 
| Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 156 | mbedtls_ctr_drbg_init( &ctr_drbg ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 157 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | mbedtls_printf( "\n  . Seeding the random number generator..." ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 159 | fflush( stdout ); | 
|  | 160 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | mbedtls_entropy_init( &entropy ); | 
| Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 162 | if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 163 | (const unsigned char *) pers, | 
|  | 164 | strlen( pers ) ) ) != 0 ) | 
|  | 165 | { | 
| Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 166 | mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 167 | goto exit; | 
|  | 168 | } | 
|  | 169 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 170 | mbedtls_printf( " ok\n" ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 171 |  | 
|  | 172 | /* | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 173 | * 0. Load certificates | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 174 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | mbedtls_printf( "  . Loading the CA root certificate ..." ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 176 | fflush( stdout ); | 
|  | 177 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 178 | ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem, | 
|  | 179 | mbedtls_test_cas_pem_len ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 180 | if( ret < 0 ) | 
|  | 181 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 182 | mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse returned -0x%x\n\n", -ret ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 183 | goto exit; | 
|  | 184 | } | 
|  | 185 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 186 | mbedtls_printf( " ok (%d skipped)\n", ret ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 187 |  | 
|  | 188 | /* | 
|  | 189 | * 1. Start the connection | 
|  | 190 | */ | 
| Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 191 | mbedtls_printf( "  . Connecting to udp/%s/%s...", SERVER_NAME, SERVER_PORT ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 192 | fflush( stdout ); | 
|  | 193 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | if( ( ret = mbedtls_net_connect( &server_fd, SERVER_ADDR, | 
|  | 195 | SERVER_PORT, MBEDTLS_NET_PROTO_UDP ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 196 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 197 | mbedtls_printf( " failed\n  ! mbedtls_net_connect returned %d\n\n", ret ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 198 | goto exit; | 
|  | 199 | } | 
|  | 200 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | mbedtls_printf( " ok\n" ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 202 |  | 
|  | 203 | /* | 
|  | 204 | * 2. Setup stuff | 
|  | 205 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | mbedtls_printf( "  . Setting up the DTLS structure..." ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 207 | fflush( stdout ); | 
|  | 208 |  | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 209 | if( ( ret = mbedtls_ssl_config_defaults( &conf, | 
|  | 210 | MBEDTLS_SSL_IS_CLIENT, | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 211 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, | 
|  | 212 | MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 213 | { | 
|  | 214 | mbedtls_printf( " failed\n  ! mbedtls_ssl_config_defaults returned %d\n\n", ret ); | 
|  | 215 | goto exit; | 
|  | 216 | } | 
|  | 217 |  | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 218 | /* OPTIONAL is usually a bad choice for security, but makes interop easier | 
|  | 219 | * in this simplified example, in which the ca chain is hardcoded. | 
|  | 220 | * Production code should set a proper ca chain and use REQUIRED. */ | 
|  | 221 | mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL ); | 
|  | 222 | mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL ); | 
| Hanno Becker | 572d448 | 2019-07-23 13:47:53 +0100 | [diff] [blame] | 223 |  | 
|  | 224 | #if !defined(MBEDTLS_SSL_CONF_RNG) | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 225 | mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg ); | 
| Hanno Becker | 572d448 | 2019-07-23 13:47:53 +0100 | [diff] [blame] | 226 | #else | 
|  | 227 | rng_ctx_global = &ctr_drbg; | 
|  | 228 | #endif | 
|  | 229 |  | 
| Hanno Becker | 14a4a44 | 2019-07-02 17:00:34 +0100 | [diff] [blame] | 230 | #if defined(MBEDTLS_DEBUG_C) | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 231 | mbedtls_ssl_conf_dbg( &conf, my_debug, stdout ); | 
| Hanno Becker | 14a4a44 | 2019-07-02 17:00:34 +0100 | [diff] [blame] | 232 | #endif | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 233 |  | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 234 | if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 235 | { | 
| Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 236 | mbedtls_printf( " failed\n  ! mbedtls_ssl_setup returned %d\n\n", ret ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 237 | goto exit; | 
|  | 238 | } | 
| Teppo Järvelin | 4009d8f | 2019-08-19 14:48:09 +0300 | [diff] [blame] | 239 | #if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION) | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 240 | if( ( ret = mbedtls_ssl_set_hostname( &ssl, SERVER_NAME ) ) != 0 ) | 
|  | 241 | { | 
|  | 242 | mbedtls_printf( " failed\n  ! mbedtls_ssl_set_hostname returned %d\n\n", ret ); | 
|  | 243 | goto exit; | 
|  | 244 | } | 
| Teppo Järvelin | 4009d8f | 2019-08-19 14:48:09 +0300 | [diff] [blame] | 245 | #endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */ | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 246 |  | 
| Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 247 | #if !defined(MBEDTLS_SSL_CONF_RECV) && \ | 
|  | 248 | !defined(MBEDTLS_SSL_CONF_SEND) && \ | 
|  | 249 | !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT) | 
|  | 250 | mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL ); | 
|  | 251 | #else | 
|  | 252 | mbedtls_ssl_set_bio_ctx( &ssl, &server_fd ); | 
|  | 253 | #endif | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 254 |  | 
| Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 255 | #if defined(MBEDTLS_TIMING_C) | 
|  | 256 | #if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \ | 
|  | 257 | !defined(MBEDTLS_SSL_CONF_GET_TIMER) | 
| Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 258 | mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay, | 
|  | 259 | mbedtls_timing_get_delay ); | 
| Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 260 | #else | 
|  | 261 | mbedtls_ssl_set_timer_cb_ctx( &ssl, &timer ); | 
|  | 262 | #endif | 
|  | 263 | #endif | 
| Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 264 |  | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 265 | mbedtls_printf( " ok\n" ); | 
|  | 266 |  | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 267 | /* | 
|  | 268 | * 4. Handshake | 
|  | 269 | */ | 
| Xinyu Chen | e1a94a6 | 2016-11-22 14:56:18 +0800 | [diff] [blame] | 270 | mbedtls_printf( "  . Performing the DTLS handshake..." ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 271 | fflush( stdout ); | 
|  | 272 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 273 | do ret = mbedtls_ssl_handshake( &ssl ); | 
| Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 274 | while( ret == MBEDTLS_ERR_SSL_WANT_READ || | 
|  | 275 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 276 |  | 
|  | 277 | if( ret != 0 ) | 
|  | 278 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | mbedtls_printf( " failed\n  ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 280 | goto exit; | 
|  | 281 | } | 
|  | 282 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 283 | mbedtls_printf( " ok\n" ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 284 |  | 
|  | 285 | /* | 
|  | 286 | * 5. Verify the server certificate | 
|  | 287 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 288 | mbedtls_printf( "  . Verifying peer X.509 certificate..." ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 289 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 290 | /* In real life, we would have used MBEDTLS_SSL_VERIFY_REQUIRED so that the | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 291 | * handshake would not succeed if the peer's cert is bad.  Even if we used | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 292 | * MBEDTLS_SSL_VERIFY_OPTIONAL, we would bail out here if ret != 0 */ | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 293 | if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 294 | { | 
| Hanno Becker | 02a2193 | 2019-06-10 15:08:43 +0100 | [diff] [blame] | 295 | #if !defined(MBEDTLS_X509_REMOVE_INFO) | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 296 | char vrfy_buf[512]; | 
| Peter Kolbus | dc470ae | 2018-12-11 13:55:56 -0600 | [diff] [blame] | 297 | #endif | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 298 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 299 | mbedtls_printf( " failed\n" ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 300 |  | 
| Hanno Becker | 02a2193 | 2019-06-10 15:08:43 +0100 | [diff] [blame] | 301 | #if !defined(MBEDTLS_X509_REMOVE_INFO) | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 302 | mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), "  ! ", flags ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 303 |  | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 304 | mbedtls_printf( "%s\n", vrfy_buf ); | 
| Peter Kolbus | dc470ae | 2018-12-11 13:55:56 -0600 | [diff] [blame] | 305 | #endif | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 306 | } | 
|  | 307 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 308 | mbedtls_printf( " ok\n" ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 309 |  | 
|  | 310 | /* | 
|  | 311 | * 6. Write the echo request | 
|  | 312 | */ | 
|  | 313 | send_request: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 314 | mbedtls_printf( "  > Write to server:" ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 315 | fflush( stdout ); | 
|  | 316 |  | 
|  | 317 | len = sizeof( MESSAGE ) - 1; | 
|  | 318 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 319 | do ret = mbedtls_ssl_write( &ssl, (unsigned char *) MESSAGE, len ); | 
| Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 320 | while( ret == MBEDTLS_ERR_SSL_WANT_READ || | 
|  | 321 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 322 |  | 
|  | 323 | if( ret < 0 ) | 
|  | 324 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 325 | mbedtls_printf( " failed\n  ! mbedtls_ssl_write returned %d\n\n", ret ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 326 | goto exit; | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | len = ret; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 330 | mbedtls_printf( " %d bytes written\n\n%s\n\n", len, MESSAGE ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 331 |  | 
|  | 332 | /* | 
|  | 333 | * 7. Read the echo response | 
|  | 334 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 335 | mbedtls_printf( "  < Read from server:" ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 336 | fflush( stdout ); | 
|  | 337 |  | 
|  | 338 | len = sizeof( buf ) - 1; | 
|  | 339 | memset( buf, 0, sizeof( buf ) ); | 
|  | 340 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 341 | do ret = mbedtls_ssl_read( &ssl, buf, len ); | 
| Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 342 | while( ret == MBEDTLS_ERR_SSL_WANT_READ || | 
|  | 343 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 344 |  | 
|  | 345 | if( ret <= 0 ) | 
|  | 346 | { | 
|  | 347 | switch( ret ) | 
|  | 348 | { | 
| Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 349 | case MBEDTLS_ERR_SSL_TIMEOUT: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | mbedtls_printf( " timeout\n\n" ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 351 | if( retry_left-- > 0 ) | 
|  | 352 | goto send_request; | 
|  | 353 | goto exit; | 
|  | 354 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 355 | case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: | 
|  | 356 | mbedtls_printf( " connection was closed gracefully\n" ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 357 | ret = 0; | 
|  | 358 | goto close_notify; | 
|  | 359 |  | 
|  | 360 | default: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 361 | mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n\n", -ret ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 362 | goto exit; | 
|  | 363 | } | 
|  | 364 | } | 
|  | 365 |  | 
|  | 366 | len = ret; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 367 | mbedtls_printf( " %d bytes read\n\n%s\n\n", len, buf ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 368 |  | 
|  | 369 | /* | 
|  | 370 | * 8. Done, cleanly close the connection | 
|  | 371 | */ | 
|  | 372 | close_notify: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 373 | mbedtls_printf( "  . Closing the connection..." ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 374 |  | 
|  | 375 | /* No error checking, the connection might be closed already */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 376 | do ret = mbedtls_ssl_close_notify( &ssl ); | 
| Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 377 | while( ret == MBEDTLS_ERR_SSL_WANT_WRITE ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 378 | ret = 0; | 
|  | 379 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 380 | mbedtls_printf( " done\n" ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 381 |  | 
|  | 382 | /* | 
|  | 383 | * 9. Final clean-ups and exit | 
|  | 384 | */ | 
|  | 385 | exit: | 
|  | 386 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 387 | #ifdef MBEDTLS_ERROR_C | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 388 | if( ret != 0 ) | 
|  | 389 | { | 
|  | 390 | char error_buf[100]; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | mbedtls_strerror( ret, error_buf, 100 ); | 
|  | 392 | mbedtls_printf( "Last error was: %d - %s\n\n", ret, error_buf ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 393 | } | 
|  | 394 | #endif | 
|  | 395 |  | 
| Manuel Pégourié-Gonnard | 3d7d00a | 2015-06-30 15:55:03 +0200 | [diff] [blame] | 396 | mbedtls_net_free( &server_fd ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 397 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 398 | mbedtls_x509_crt_free( &cacert ); | 
|  | 399 | mbedtls_ssl_free( &ssl ); | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 400 | mbedtls_ssl_config_free( &conf ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 401 | mbedtls_ctr_drbg_free( &ctr_drbg ); | 
|  | 402 | mbedtls_entropy_free( &entropy ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 403 |  | 
|  | 404 | #if defined(_WIN32) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 405 | mbedtls_printf( "  + Press Enter to exit this program.\n" ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 406 | fflush( stdout ); getchar(); | 
|  | 407 | #endif | 
|  | 408 |  | 
|  | 409 | /* Shell can not handle large exit numbers -> 1 for errors */ | 
|  | 410 | if( ret < 0 ) | 
|  | 411 | ret = 1; | 
|  | 412 |  | 
| Andrzej Kurek | 825ebd4 | 2020-05-18 11:47:25 -0400 | [diff] [blame] | 413 | mbedtls_exit( ret ); | 
| Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 414 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 415 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_NET_C && | 
| Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 416 | MBEDTLD_TIMING_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 417 | MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C && MBEDTLS_CERTS_C && | 
|  | 418 | MBEDTLS_PEM_PARSE_C */ |