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 | 53ebe13 | 2015-05-15 11:56:13 +0200 | [diff] [blame] | 4 | * Copyright (C) 2014-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | e4d4890 | 2015-03-06 13:40:52 +0000 | [diff] [blame] | 6 | * 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] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 25 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 27 | #endif |
| 28 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame] | 31 | #else |
Manuel Pégourié-Gonnard | 4b3e5ef | 2015-03-27 11:24:27 +0100 | [diff] [blame] | 32 | #include <stdio.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 | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #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] | 38 | !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_TIMING_C) || \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 39 | !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \ |
| 40 | !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \ |
| 41 | !defined(MBEDTLS_CERTS_C) |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 42 | int main( void ) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 43 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 44 | 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] | 45 | "MBEDTLS_NET_C and/or MBEDTLS_TIMING_C and/or " |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or " |
| 47 | "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or " |
| 48 | "MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.\n" ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 49 | return( 0 ); |
| 50 | } |
| 51 | #else |
| 52 | |
| 53 | #include <string.h> |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 54 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 55 | #include "mbedtls/net.h" |
| 56 | #include "mbedtls/debug.h" |
| 57 | #include "mbedtls/ssl.h" |
| 58 | #include "mbedtls/entropy.h" |
| 59 | #include "mbedtls/ctr_drbg.h" |
| 60 | #include "mbedtls/error.h" |
| 61 | #include "mbedtls/certs.h" |
Manuel Pégourié-Gonnard | 56273da | 2015-05-26 12:19:45 +0200 | [diff] [blame] | 62 | #include "mbedtls/timing.h" |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 63 | |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame^] | 64 | #define SERVER_PORT "4433" |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 65 | #define SERVER_NAME "localhost" |
| 66 | #define SERVER_ADDR "127.0.0.1" /* forces IPv4 */ |
| 67 | #define MESSAGE "Echo this" |
| 68 | |
| 69 | #define READ_TIMEOUT_MS 1000 |
| 70 | #define MAX_RETRY 5 |
| 71 | |
| 72 | #define DEBUG_LEVEL 0 |
| 73 | |
| 74 | static void my_debug( void *ctx, int level, const char *str ) |
| 75 | { |
| 76 | ((void) level); |
| 77 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 78 | mbedtls_fprintf( (FILE *) ctx, "%s", str ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 79 | fflush( (FILE *) ctx ); |
| 80 | } |
| 81 | |
| 82 | int main( int argc, char *argv[] ) |
| 83 | { |
| 84 | int ret, len, server_fd = -1; |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 85 | uint32_t flags; |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 86 | unsigned char buf[1024]; |
| 87 | const char *pers = "dtls_client"; |
| 88 | int retry_left = MAX_RETRY; |
| 89 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | mbedtls_entropy_context entropy; |
| 91 | mbedtls_ctr_drbg_context ctr_drbg; |
| 92 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 93 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 94 | mbedtls_x509_crt cacert; |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 95 | mbedtls_timing_delay_context timer; |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 96 | |
| 97 | ((void) argc); |
| 98 | ((void) argv); |
| 99 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 100 | #if defined(MBEDTLS_DEBUG_C) |
| 101 | mbedtls_debug_set_threshold( DEBUG_LEVEL ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 102 | #endif |
| 103 | |
| 104 | /* |
| 105 | * 0. Initialize the RNG and the session data |
| 106 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 107 | mbedtls_ssl_init( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 108 | mbedtls_ssl_config_init( &conf ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | mbedtls_x509_crt_init( &cacert ); |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 110 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 111 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | mbedtls_printf( "\n . Seeding the random number generator..." ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 113 | fflush( stdout ); |
| 114 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | mbedtls_entropy_init( &entropy ); |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 116 | 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] | 117 | (const unsigned char *) pers, |
| 118 | strlen( pers ) ) ) != 0 ) |
| 119 | { |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 120 | 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] | 121 | goto exit; |
| 122 | } |
| 123 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 125 | |
| 126 | /* |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 127 | * 0. Load certificates |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 128 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | mbedtls_printf( " . Loading the CA root certificate ..." ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 130 | fflush( stdout ); |
| 131 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem, |
| 133 | mbedtls_test_cas_pem_len ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 134 | if( ret < 0 ) |
| 135 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 136 | 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] | 137 | goto exit; |
| 138 | } |
| 139 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | mbedtls_printf( " ok (%d skipped)\n", ret ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * 1. Start the connection |
| 144 | */ |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame^] | 145 | 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] | 146 | fflush( stdout ); |
| 147 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 148 | if( ( ret = mbedtls_net_connect( &server_fd, SERVER_ADDR, |
| 149 | SERVER_PORT, MBEDTLS_NET_PROTO_UDP ) ) != 0 ) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 150 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | 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] | 152 | goto exit; |
| 153 | } |
| 154 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | * 2. Setup stuff |
| 159 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 160 | mbedtls_printf( " . Setting up the DTLS structure..." ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 161 | fflush( stdout ); |
| 162 | |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 163 | if( ( ret = mbedtls_ssl_config_defaults( &conf, |
| 164 | MBEDTLS_SSL_IS_CLIENT, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 165 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 166 | MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 ) |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 167 | { |
| 168 | mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret ); |
| 169 | goto exit; |
| 170 | } |
| 171 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 172 | /* OPTIONAL is usually a bad choice for security, but makes interop easier |
| 173 | * in this simplified example, in which the ca chain is hardcoded. |
| 174 | * Production code should set a proper ca chain and use REQUIRED. */ |
| 175 | mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL ); |
| 176 | mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL ); |
| 177 | mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg ); |
| 178 | mbedtls_ssl_conf_dbg( &conf, my_debug, stdout ); |
| 179 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 180 | if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 ) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 181 | { |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 182 | 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] | 183 | goto exit; |
| 184 | } |
| 185 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 186 | if( ( ret = mbedtls_ssl_set_hostname( &ssl, SERVER_NAME ) ) != 0 ) |
| 187 | { |
| 188 | mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret ); |
| 189 | goto exit; |
| 190 | } |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 191 | |
Manuel Pégourié-Gonnard | 1b511f9 | 2015-05-06 15:54:23 +0100 | [diff] [blame] | 192 | mbedtls_ssl_set_bio( &ssl, &server_fd, |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 193 | mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 194 | |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 195 | mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay, |
| 196 | mbedtls_timing_get_delay ); |
| 197 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 198 | mbedtls_printf( " ok\n" ); |
| 199 | |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 200 | /* |
| 201 | * 4. Handshake |
| 202 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 203 | mbedtls_printf( " . Performing the SSL/TLS handshake..." ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 204 | fflush( stdout ); |
| 205 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | do ret = mbedtls_ssl_handshake( &ssl ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 207 | while( ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 208 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 209 | |
| 210 | if( ret != 0 ) |
| 211 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 212 | 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] | 213 | goto exit; |
| 214 | } |
| 215 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 216 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 217 | |
| 218 | /* |
| 219 | * 5. Verify the server certificate |
| 220 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 221 | mbedtls_printf( " . Verifying peer X.509 certificate..." ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 222 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 223 | /* 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] | 224 | * 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] | 225 | * 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] | 226 | if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 227 | { |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 228 | char vrfy_buf[512]; |
| 229 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 230 | mbedtls_printf( " failed\n" ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 231 | |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 232 | 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] | 233 | |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 234 | mbedtls_printf( "%s\n", vrfy_buf ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 235 | } |
| 236 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 238 | |
| 239 | /* |
| 240 | * 6. Write the echo request |
| 241 | */ |
| 242 | send_request: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 243 | mbedtls_printf( " > Write to server:" ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 244 | fflush( stdout ); |
| 245 | |
| 246 | len = sizeof( MESSAGE ) - 1; |
| 247 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 248 | do ret = mbedtls_ssl_write( &ssl, (unsigned char *) MESSAGE, len ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 249 | while( ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 250 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 251 | |
| 252 | if( ret < 0 ) |
| 253 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | 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] | 255 | goto exit; |
| 256 | } |
| 257 | |
| 258 | len = ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | 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] | 260 | |
| 261 | /* |
| 262 | * 7. Read the echo response |
| 263 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 264 | mbedtls_printf( " < Read from server:" ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 265 | fflush( stdout ); |
| 266 | |
| 267 | len = sizeof( buf ) - 1; |
| 268 | memset( buf, 0, sizeof( buf ) ); |
| 269 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | do ret = mbedtls_ssl_read( &ssl, buf, len ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 271 | while( ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 272 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 273 | |
| 274 | if( ret <= 0 ) |
| 275 | { |
| 276 | switch( ret ) |
| 277 | { |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 278 | case MBEDTLS_ERR_SSL_TIMEOUT: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | mbedtls_printf( " timeout\n\n" ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 280 | if( retry_left-- > 0 ) |
| 281 | goto send_request; |
| 282 | goto exit; |
| 283 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 284 | case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: |
| 285 | mbedtls_printf( " connection was closed gracefully\n" ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 286 | ret = 0; |
| 287 | goto close_notify; |
| 288 | |
| 289 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 290 | 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] | 291 | goto exit; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | len = ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 296 | 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] | 297 | |
| 298 | /* |
| 299 | * 8. Done, cleanly close the connection |
| 300 | */ |
| 301 | close_notify: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 302 | mbedtls_printf( " . Closing the connection..." ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 303 | |
| 304 | /* No error checking, the connection might be closed already */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 305 | do ret = mbedtls_ssl_close_notify( &ssl ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 306 | while( ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 307 | ret = 0; |
| 308 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 309 | mbedtls_printf( " done\n" ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 310 | |
| 311 | /* |
| 312 | * 9. Final clean-ups and exit |
| 313 | */ |
| 314 | exit: |
| 315 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 316 | #ifdef MBEDTLS_ERROR_C |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 317 | if( ret != 0 ) |
| 318 | { |
| 319 | char error_buf[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 320 | mbedtls_strerror( ret, error_buf, 100 ); |
| 321 | 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] | 322 | } |
| 323 | #endif |
| 324 | |
| 325 | if( server_fd != -1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 326 | mbedtls_net_close( server_fd ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 327 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 328 | mbedtls_x509_crt_free( &cacert ); |
| 329 | mbedtls_ssl_free( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 330 | mbedtls_ssl_config_free( &conf ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 331 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 332 | mbedtls_entropy_free( &entropy ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 333 | |
| 334 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 335 | mbedtls_printf( " + Press Enter to exit this program.\n" ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 336 | fflush( stdout ); getchar(); |
| 337 | #endif |
| 338 | |
| 339 | /* Shell can not handle large exit numbers -> 1 for errors */ |
| 340 | if( ret < 0 ) |
| 341 | ret = 1; |
| 342 | |
| 343 | return( ret ); |
| 344 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 345 | #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] | 346 | MBEDTLD_TIMING_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 347 | MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C && MBEDTLS_CERTS_C && |
| 348 | MBEDTLS_PEM_PARSE_C */ |