Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Simple DTLS client demonstration program |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
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 | */ |
| 19 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 20 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 21 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 22 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 24 | #endif |
| 25 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 26 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #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] | 29 | !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_TIMING_C) || \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \ |
| 31 | !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \ |
Andres AG | cef21e4 | 2017-02-02 17:01:10 +0000 | [diff] [blame] | 32 | !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 33 | int main(void) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 34 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 35 | mbedtls_printf("MBEDTLS_SSL_CLI_C and/or MBEDTLS_SSL_PROTO_DTLS and/or " |
| 36 | "MBEDTLS_NET_C and/or MBEDTLS_TIMING_C and/or " |
| 37 | "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or " |
| 38 | "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or " |
| 39 | "MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.\n"); |
| 40 | mbedtls_exit(0); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 41 | } |
| 42 | #else |
| 43 | |
| 44 | #include <string.h> |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 45 | |
Andres AG | 788aa4a | 2016-09-14 14:32:09 +0100 | [diff] [blame] | 46 | #include "mbedtls/net_sockets.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 47 | #include "mbedtls/debug.h" |
| 48 | #include "mbedtls/ssl.h" |
| 49 | #include "mbedtls/entropy.h" |
| 50 | #include "mbedtls/ctr_drbg.h" |
| 51 | #include "mbedtls/error.h" |
| 52 | #include "mbedtls/certs.h" |
Manuel Pégourié-Gonnard | 56273da | 2015-05-26 12:19:45 +0200 | [diff] [blame] | 53 | #include "mbedtls/timing.h" |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 54 | |
Simon Butcher | 6fd96ad | 2018-05-12 18:23:32 +0100 | [diff] [blame] | 55 | /* Uncomment out the following line to default to IPv4 and disable IPv6 */ |
| 56 | //#define FORCE_IPV4 |
| 57 | |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 58 | #define SERVER_PORT "4433" |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 59 | #define SERVER_NAME "localhost" |
Simon Butcher | 6fd96ad | 2018-05-12 18:23:32 +0100 | [diff] [blame] | 60 | |
| 61 | #ifdef FORCE_IPV4 |
| 62 | #define SERVER_ADDR "127.0.0.1" /* Forces IPv4 */ |
| 63 | #else |
| 64 | #define SERVER_ADDR "::1" |
| 65 | #endif |
| 66 | |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 67 | #define MESSAGE "Echo this" |
| 68 | |
| 69 | #define READ_TIMEOUT_MS 1000 |
| 70 | #define MAX_RETRY 5 |
| 71 | |
| 72 | #define DEBUG_LEVEL 0 |
| 73 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 74 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 75 | static void my_debug(void *ctx, int level, |
| 76 | const char *file, int line, |
| 77 | const char *str) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 78 | { |
| 79 | ((void) level); |
| 80 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 81 | mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str); |
| 82 | fflush((FILE *) ctx); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 85 | int main(int argc, char *argv[]) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 86 | { |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 87 | int ret, len; |
| 88 | mbedtls_net_context server_fd; |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 89 | uint32_t flags; |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 90 | unsigned char buf[1024]; |
| 91 | const char *pers = "dtls_client"; |
| 92 | int retry_left = MAX_RETRY; |
| 93 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 94 | mbedtls_entropy_context entropy; |
| 95 | mbedtls_ctr_drbg_context ctr_drbg; |
| 96 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 97 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | mbedtls_x509_crt cacert; |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 99 | mbedtls_timing_delay_context timer; |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 100 | |
| 101 | ((void) argc); |
| 102 | ((void) argv); |
| 103 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | #if defined(MBEDTLS_DEBUG_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 105 | mbedtls_debug_set_threshold(DEBUG_LEVEL); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 106 | #endif |
| 107 | |
| 108 | /* |
| 109 | * 0. Initialize the RNG and the session data |
| 110 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 111 | mbedtls_net_init(&server_fd); |
| 112 | mbedtls_ssl_init(&ssl); |
| 113 | mbedtls_ssl_config_init(&conf); |
| 114 | mbedtls_x509_crt_init(&cacert); |
| 115 | mbedtls_ctr_drbg_init(&ctr_drbg); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 116 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 117 | mbedtls_printf("\n . Seeding the random number generator..."); |
| 118 | fflush(stdout); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 119 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 120 | mbedtls_entropy_init(&entropy); |
| 121 | if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, |
| 122 | (const unsigned char *) pers, |
| 123 | strlen(pers))) != 0) { |
| 124 | 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] | 125 | goto exit; |
| 126 | } |
| 127 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 128 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 129 | |
| 130 | /* |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 131 | * 0. Load certificates |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 132 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 133 | mbedtls_printf(" . Loading the CA root certificate ..."); |
| 134 | fflush(stdout); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 135 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 136 | ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_cas_pem, |
| 137 | mbedtls_test_cas_pem_len); |
| 138 | if (ret < 0) { |
| 139 | mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", |
| 140 | (unsigned int) -ret); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 141 | goto exit; |
| 142 | } |
| 143 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 144 | mbedtls_printf(" ok (%d skipped)\n", ret); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 145 | |
| 146 | /* |
| 147 | * 1. Start the connection |
| 148 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 149 | mbedtls_printf(" . Connecting to udp/%s/%s...", SERVER_NAME, SERVER_PORT); |
| 150 | fflush(stdout); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 151 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 152 | if ((ret = mbedtls_net_connect(&server_fd, SERVER_ADDR, |
| 153 | SERVER_PORT, MBEDTLS_NET_PROTO_UDP)) != 0) { |
| 154 | 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] | 155 | goto exit; |
| 156 | } |
| 157 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 158 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * 2. Setup stuff |
| 162 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 163 | mbedtls_printf(" . Setting up the DTLS structure..."); |
| 164 | fflush(stdout); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 165 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 166 | if ((ret = mbedtls_ssl_config_defaults(&conf, |
| 167 | MBEDTLS_SSL_IS_CLIENT, |
| 168 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 169 | MBEDTLS_SSL_PRESET_DEFAULT)) != 0) { |
| 170 | mbedtls_printf(" failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 171 | goto exit; |
| 172 | } |
| 173 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 174 | /* OPTIONAL is usually a bad choice for security, but makes interop easier |
| 175 | * in this simplified example, in which the ca chain is hardcoded. |
| 176 | * Production code should set a proper ca chain and use REQUIRED. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 177 | mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL); |
| 178 | mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL); |
| 179 | mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg); |
| 180 | mbedtls_ssl_conf_dbg(&conf, my_debug, stdout); |
| 181 | mbedtls_ssl_conf_read_timeout(&conf, READ_TIMEOUT_MS); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 182 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 183 | if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) { |
| 184 | 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] | 185 | goto exit; |
| 186 | } |
| 187 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 188 | if ((ret = mbedtls_ssl_set_hostname(&ssl, SERVER_NAME)) != 0) { |
| 189 | mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret); |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 190 | goto exit; |
| 191 | } |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 192 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 193 | mbedtls_ssl_set_bio(&ssl, &server_fd, |
| 194 | mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 195 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 196 | mbedtls_ssl_set_timer_cb(&ssl, &timer, mbedtls_timing_set_delay, |
| 197 | mbedtls_timing_get_delay); |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 198 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 199 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 200 | |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 201 | /* |
| 202 | * 4. Handshake |
| 203 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 204 | mbedtls_printf(" . Performing the DTLS handshake..."); |
| 205 | fflush(stdout); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 206 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 207 | do { |
| 208 | ret = mbedtls_ssl_handshake(&ssl); |
| 209 | } while (ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 210 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 211 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 212 | if (ret != 0) { |
| 213 | mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", |
| 214 | (unsigned int) -ret); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 215 | goto exit; |
| 216 | } |
| 217 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 218 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 219 | |
| 220 | /* |
| 221 | * 5. Verify the server certificate |
| 222 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 223 | mbedtls_printf(" . Verifying peer X.509 certificate..."); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 224 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | /* 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] | 226 | * 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] | 227 | * MBEDTLS_SSL_VERIFY_OPTIONAL, we would bail out here if ret != 0 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 228 | if ((flags = mbedtls_ssl_get_verify_result(&ssl)) != 0) { |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 229 | char vrfy_buf[512]; |
| 230 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 231 | mbedtls_printf(" failed\n"); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 232 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 233 | 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] | 234 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 235 | mbedtls_printf("%s\n", vrfy_buf); |
| 236 | } else { |
| 237 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 238 | } |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 239 | |
| 240 | /* |
| 241 | * 6. Write the echo request |
| 242 | */ |
| 243 | send_request: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 244 | mbedtls_printf(" > Write to server:"); |
| 245 | fflush(stdout); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 246 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 247 | len = sizeof(MESSAGE) - 1; |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 248 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 249 | do { |
| 250 | ret = mbedtls_ssl_write(&ssl, (unsigned char *) MESSAGE, len); |
| 251 | } while (ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 252 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 253 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 254 | if (ret < 0) { |
| 255 | 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] | 256 | goto exit; |
| 257 | } |
| 258 | |
| 259 | len = ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 260 | 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] | 261 | |
| 262 | /* |
| 263 | * 7. Read the echo response |
| 264 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 265 | mbedtls_printf(" < Read from server:"); |
| 266 | fflush(stdout); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 267 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 268 | len = sizeof(buf) - 1; |
| 269 | memset(buf, 0, sizeof(buf)); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 270 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 271 | do { |
| 272 | ret = mbedtls_ssl_read(&ssl, buf, len); |
| 273 | } while (ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 274 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 275 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 276 | if (ret <= 0) { |
| 277 | switch (ret) { |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 278 | case MBEDTLS_ERR_SSL_TIMEOUT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 279 | mbedtls_printf(" timeout\n\n"); |
| 280 | if (retry_left-- > 0) { |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 281 | goto send_request; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 282 | } |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 283 | goto exit; |
| 284 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 285 | case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 286 | mbedtls_printf(" connection was closed gracefully\n"); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 287 | ret = 0; |
| 288 | goto close_notify; |
| 289 | |
| 290 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 291 | mbedtls_printf(" mbedtls_ssl_read returned -0x%x\n\n", (unsigned int) -ret); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 292 | goto exit; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | len = ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 297 | 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] | 298 | |
| 299 | /* |
| 300 | * 8. Done, cleanly close the connection |
| 301 | */ |
| 302 | close_notify: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 303 | mbedtls_printf(" . Closing the connection..."); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 304 | |
| 305 | /* No error checking, the connection might be closed already */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 306 | do { |
| 307 | ret = mbedtls_ssl_close_notify(&ssl); |
| 308 | } while (ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 309 | ret = 0; |
| 310 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 311 | mbedtls_printf(" done\n"); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 312 | |
| 313 | /* |
| 314 | * 9. Final clean-ups and exit |
| 315 | */ |
| 316 | exit: |
| 317 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 318 | #ifdef MBEDTLS_ERROR_C |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 319 | if (ret != 0) { |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 320 | char error_buf[100]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 321 | mbedtls_strerror(ret, error_buf, 100); |
| 322 | 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] | 323 | } |
| 324 | #endif |
| 325 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 326 | mbedtls_net_free(&server_fd); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 327 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 328 | mbedtls_x509_crt_free(&cacert); |
| 329 | mbedtls_ssl_free(&ssl); |
| 330 | mbedtls_ssl_config_free(&conf); |
| 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) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 335 | mbedtls_printf(" + Press Enter to exit this program.\n"); |
| 336 | fflush(stdout); getchar(); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 337 | #endif |
| 338 | |
| 339 | /* Shell can not handle large exit numbers -> 1 for errors */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 340 | if (ret < 0) { |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 341 | ret = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 342 | } |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 343 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 344 | mbedtls_exit(ret); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 345 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 346 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_NET_C && |
Tom Cosgrove | 49f99bc | 2022-12-04 16:44:21 +0000 | [diff] [blame] | 347 | MBEDTLS_TIMING_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 348 | MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C && MBEDTLS_CERTS_C && |
| 349 | MBEDTLS_PEM_PARSE_C */ |