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