blob: c35c4e2654852d28d2b6d8e5eda51b69a563c634 [file] [log] [blame]
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001/*
2 * Simple DTLS client demonstration program
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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é-Gonnarde63582a2014-10-14 11:47:21 +020018 */
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020024#endif
25
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_SSL_CLI_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) || \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020029 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_TIMING_C) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
31 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \
Andres AGcef21e42017-02-02 17:01:10 +000032 !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010033int main(void)
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020034{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010035 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é-Gonnarde63582a2014-10-14 11:47:21 +020041}
42#else
43
44#include <string.h>
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020045
Andres AG788aa4a2016-09-14 14:32:09 +010046#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#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é-Gonnard56273da2015-05-26 12:19:45 +020053#include "mbedtls/timing.h"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020054
Simon Butcher6fd96ad2018-05-12 18:23:32 +010055/* Uncomment out the following line to default to IPv4 and disable IPv6 */
56//#define FORCE_IPV4
57
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020058#define SERVER_PORT "4433"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020059#define SERVER_NAME "localhost"
Simon Butcher6fd96ad2018-05-12 18:23:32 +010060
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é-Gonnarde63582a2014-10-14 11:47:21 +020067#define MESSAGE "Echo this"
68
69#define READ_TIMEOUT_MS 1000
70#define MAX_RETRY 5
71
72#define DEBUG_LEVEL 0
73
Simon Butcher63cb97e2018-12-06 17:43:31 +000074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075static void my_debug(void *ctx, int level,
76 const char *file, int line,
77 const char *str)
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020078{
79 ((void) level);
80
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010081 mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
82 fflush((FILE *) ctx);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020083}
84
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085int main(int argc, char *argv[])
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020086{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020087 int ret, len;
88 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020089 uint32_t flags;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020090 unsigned char buf[1024];
91 const char *pers = "dtls_client";
92 int retry_left = MAX_RETRY;
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_entropy_context entropy;
95 mbedtls_ctr_drbg_context ctr_drbg;
96 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020097 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_x509_crt cacert;
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020099 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200100
101 ((void) argc);
102 ((void) argv);
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 mbedtls_debug_set_threshold(DEBUG_LEVEL);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200106#endif
107
108 /*
109 * 0. Initialize the RNG and the session data
110 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100111 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);
Przemek Stekielb00688f2023-04-17 11:10:05 +0200116 mbedtls_entropy_init(&entropy);
117
118#if defined(MBEDTLS_USE_PSA_CRYPTO)
119 psa_status_t status = psa_crypto_init();
120 if (status != PSA_SUCCESS) {
121 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
122 (int) status);
123 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
124 goto exit;
125 }
126#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 mbedtls_printf("\n . Seeding the random number generator...");
129 fflush(stdout);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
132 (const unsigned char *) pers,
133 strlen(pers))) != 0) {
134 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200135 goto exit;
136 }
137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200139
140 /*
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200141 * 0. Load certificates
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200142 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 mbedtls_printf(" . Loading the CA root certificate ...");
144 fflush(stdout);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100146 ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_cas_pem,
147 mbedtls_test_cas_pem_len);
148 if (ret < 0) {
149 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
150 (unsigned int) -ret);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200151 goto exit;
152 }
153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154 mbedtls_printf(" ok (%d skipped)\n", ret);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200155
156 /*
157 * 1. Start the connection
158 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 mbedtls_printf(" . Connecting to udp/%s/%s...", SERVER_NAME, SERVER_PORT);
160 fflush(stdout);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 if ((ret = mbedtls_net_connect(&server_fd, SERVER_ADDR,
163 SERVER_PORT, MBEDTLS_NET_PROTO_UDP)) != 0) {
164 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200165 goto exit;
166 }
167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200169
170 /*
171 * 2. Setup stuff
172 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 mbedtls_printf(" . Setting up the DTLS structure...");
174 fflush(stdout);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176 if ((ret = mbedtls_ssl_config_defaults(&conf,
177 MBEDTLS_SSL_IS_CLIENT,
178 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
179 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
180 mbedtls_printf(" failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200181 goto exit;
182 }
183
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200184 /* OPTIONAL is usually a bad choice for security, but makes interop easier
185 * in this simplified example, in which the ca chain is hardcoded.
186 * Production code should set a proper ca chain and use REQUIRED. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
188 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
189 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
190 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
191 mbedtls_ssl_conf_read_timeout(&conf, READ_TIMEOUT_MS);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
194 mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned %d\n\n", ret);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200195 goto exit;
196 }
197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 if ((ret = mbedtls_ssl_set_hostname(&ssl, SERVER_NAME)) != 0) {
199 mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret);
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100200 goto exit;
201 }
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 mbedtls_ssl_set_bio(&ssl, &server_fd,
204 mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 mbedtls_ssl_set_timer_cb(&ssl, &timer, mbedtls_timing_set_delay,
207 mbedtls_timing_get_delay);
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100210
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200211 /*
212 * 4. Handshake
213 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214 mbedtls_printf(" . Performing the DTLS handshake...");
215 fflush(stdout);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200216
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100217 do {
218 ret = mbedtls_ssl_handshake(&ssl);
219 } while (ret == MBEDTLS_ERR_SSL_WANT_READ ||
220 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200221
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 if (ret != 0) {
223 mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
224 (unsigned int) -ret);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200225 goto exit;
226 }
227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200229
230 /*
231 * 5. Verify the server certificate
232 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 mbedtls_printf(" . Verifying peer X.509 certificate...");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 /* In real life, we would have used MBEDTLS_SSL_VERIFY_REQUIRED so that the
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200236 * handshake would not succeed if the peer's cert is bad. Even if we used
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 * MBEDTLS_SSL_VERIFY_OPTIONAL, we would bail out here if ret != 0 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 if ((flags = mbedtls_ssl_get_verify_result(&ssl)) != 0) {
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200239 char vrfy_buf[512];
240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 mbedtls_printf(" failed\n");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 mbedtls_printf("%s\n", vrfy_buf);
246 } else {
247 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200248 }
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200249
250 /*
251 * 6. Write the echo request
252 */
253send_request:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 mbedtls_printf(" > Write to server:");
255 fflush(stdout);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 len = sizeof(MESSAGE) - 1;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 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é-Gonnarde63582a2014-10-14 11:47:21 +0200263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 if (ret < 0) {
265 mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n", ret);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200266 goto exit;
267 }
268
269 len = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 mbedtls_printf(" %d bytes written\n\n%s\n\n", len, MESSAGE);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200271
272 /*
273 * 7. Read the echo response
274 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 mbedtls_printf(" < Read from server:");
276 fflush(stdout);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 len = sizeof(buf) - 1;
279 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 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é-Gonnarde63582a2014-10-14 11:47:21 +0200285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 if (ret <= 0) {
287 switch (ret) {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100288 case MBEDTLS_ERR_SSL_TIMEOUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 mbedtls_printf(" timeout\n\n");
290 if (retry_left-- > 0) {
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200291 goto send_request;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292 }
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200293 goto exit;
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100296 mbedtls_printf(" connection was closed gracefully\n");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200297 ret = 0;
298 goto close_notify;
299
300 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 mbedtls_printf(" mbedtls_ssl_read returned -0x%x\n\n", (unsigned int) -ret);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200302 goto exit;
303 }
304 }
305
306 len = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 mbedtls_printf(" %d bytes read\n\n%s\n\n", len, buf);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200308
309 /*
310 * 8. Done, cleanly close the connection
311 */
312close_notify:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 mbedtls_printf(" . Closing the connection...");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200314
315 /* No error checking, the connection might be closed already */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 do {
317 ret = mbedtls_ssl_close_notify(&ssl);
318 } while (ret == MBEDTLS_ERR_SSL_WANT_WRITE);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200319 ret = 0;
320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321 mbedtls_printf(" done\n");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200322
323 /*
324 * 9. Final clean-ups and exit
325 */
326exit:
327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#ifdef MBEDTLS_ERROR_C
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 if (ret != 0) {
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200330 char error_buf[100];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 mbedtls_strerror(ret, error_buf, 100);
332 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200333 }
334#endif
335
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 mbedtls_net_free(&server_fd);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 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 Stekielc4ddf922023-04-19 10:15:26 +0200342 mbedtls_psa_crypto_free();
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200343
344#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 mbedtls_printf(" + Press Enter to exit this program.\n");
346 fflush(stdout); getchar();
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200347#endif
348
349 /* Shell can not handle large exit numbers -> 1 for errors */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 if (ret < 0) {
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200351 ret = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100352 }
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 mbedtls_exit(ret);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_NET_C &&
Tom Cosgrove49f99bc2022-12-04 16:44:21 +0000357 MBEDTLS_TIMING_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C && MBEDTLS_CERTS_C &&
359 MBEDTLS_PEM_PARSE_C */