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