blob: 05430f06e6f8d31140b66654770e7bc4cae4778d [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02006 */
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/config.h"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020010#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020012#endif
13
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if !defined(MBEDTLS_SSL_CLI_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) || \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020017 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_TIMING_C) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
19 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \
Andres AGcef21e42017-02-02 17:01:10 +000020 !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010021int main(void)
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020022{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010023 mbedtls_printf("MBEDTLS_SSL_CLI_C and/or MBEDTLS_SSL_PROTO_DTLS and/or "
24 "MBEDTLS_NET_C and/or MBEDTLS_TIMING_C and/or "
25 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
26 "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or "
27 "MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.\n");
28 mbedtls_exit(0);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020029}
30#else
31
32#include <string.h>
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020033
Andres AG788aa4a2016-09-14 14:32:09 +010034#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/debug.h"
36#include "mbedtls/ssl.h"
37#include "mbedtls/entropy.h"
38#include "mbedtls/ctr_drbg.h"
39#include "mbedtls/error.h"
40#include "mbedtls/certs.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020041#include "mbedtls/timing.h"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020042
Simon Butcher6fd96ad2018-05-12 18:23:32 +010043/* Uncomment out the following line to default to IPv4 and disable IPv6 */
44//#define FORCE_IPV4
45
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020046#define SERVER_PORT "4433"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020047#define SERVER_NAME "localhost"
Simon Butcher6fd96ad2018-05-12 18:23:32 +010048
49#ifdef FORCE_IPV4
50#define SERVER_ADDR "127.0.0.1" /* Forces IPv4 */
51#else
52#define SERVER_ADDR "::1"
53#endif
54
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020055#define MESSAGE "Echo this"
56
57#define READ_TIMEOUT_MS 1000
58#define MAX_RETRY 5
59
60#define DEBUG_LEVEL 0
61
Simon Butcher63cb97e2018-12-06 17:43:31 +000062
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010063static void my_debug(void *ctx, int level,
64 const char *file, int line,
65 const char *str)
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020066{
67 ((void) level);
68
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069 mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
70 fflush((FILE *) ctx);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020071}
72
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073int main(int argc, char *argv[])
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020074{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020075 int ret, len;
76 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020077 uint32_t flags;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020078 unsigned char buf[1024];
79 const char *pers = "dtls_client";
80 int retry_left = MAX_RETRY;
81
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_entropy_context entropy;
83 mbedtls_ctr_drbg_context ctr_drbg;
84 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020085 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_x509_crt cacert;
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020087 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020088
89 ((void) argc);
90 ((void) argv);
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 mbedtls_debug_set_threshold(DEBUG_LEVEL);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020094#endif
95
96 /*
97 * 0. Initialize the RNG and the session data
98 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 mbedtls_net_init(&server_fd);
100 mbedtls_ssl_init(&ssl);
101 mbedtls_ssl_config_init(&conf);
102 mbedtls_x509_crt_init(&cacert);
103 mbedtls_ctr_drbg_init(&ctr_drbg);
Przemek Stekielb00688f2023-04-17 11:10:05 +0200104 mbedtls_entropy_init(&entropy);
105
106#if defined(MBEDTLS_USE_PSA_CRYPTO)
107 psa_status_t status = psa_crypto_init();
108 if (status != PSA_SUCCESS) {
109 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
110 (int) status);
111 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
112 goto exit;
113 }
114#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 mbedtls_printf("\n . Seeding the random number generator...");
117 fflush(stdout);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
120 (const unsigned char *) pers,
121 strlen(pers))) != 0) {
122 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200123 goto exit;
124 }
125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200127
128 /*
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200129 * 0. Load certificates
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200130 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 mbedtls_printf(" . Loading the CA root certificate ...");
132 fflush(stdout);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200133
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_cas_pem,
135 mbedtls_test_cas_pem_len);
136 if (ret < 0) {
137 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
138 (unsigned int) -ret);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200139 goto exit;
140 }
141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142 mbedtls_printf(" ok (%d skipped)\n", ret);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200143
144 /*
145 * 1. Start the connection
146 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 mbedtls_printf(" . Connecting to udp/%s/%s...", SERVER_NAME, SERVER_PORT);
148 fflush(stdout);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 if ((ret = mbedtls_net_connect(&server_fd, SERVER_ADDR,
151 SERVER_PORT, MBEDTLS_NET_PROTO_UDP)) != 0) {
152 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200153 goto exit;
154 }
155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200157
158 /*
159 * 2. Setup stuff
160 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 mbedtls_printf(" . Setting up the DTLS structure...");
162 fflush(stdout);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164 if ((ret = mbedtls_ssl_config_defaults(&conf,
165 MBEDTLS_SSL_IS_CLIENT,
166 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
167 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
168 mbedtls_printf(" failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200169 goto exit;
170 }
171
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200172 /* OPTIONAL is usually a bad choice for security, but makes interop easier
173 * in this simplified example, in which the ca chain is hardcoded.
174 * Production code should set a proper ca chain and use REQUIRED. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
176 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
177 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
178 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
179 mbedtls_ssl_conf_read_timeout(&conf, READ_TIMEOUT_MS);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
182 mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned %d\n\n", ret);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200183 goto exit;
184 }
185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 if ((ret = mbedtls_ssl_set_hostname(&ssl, SERVER_NAME)) != 0) {
187 mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret);
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100188 goto exit;
189 }
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 mbedtls_ssl_set_bio(&ssl, &server_fd,
192 mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 mbedtls_ssl_set_timer_cb(&ssl, &timer, mbedtls_timing_set_delay,
195 mbedtls_timing_get_delay);
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100197 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100198
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200199 /*
200 * 4. Handshake
201 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 mbedtls_printf(" . Performing the DTLS handshake...");
203 fflush(stdout);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 do {
206 ret = mbedtls_ssl_handshake(&ssl);
207 } while (ret == MBEDTLS_ERR_SSL_WANT_READ ||
208 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 if (ret != 0) {
211 mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
212 (unsigned int) -ret);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200213 goto exit;
214 }
215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200217
218 /*
219 * 5. Verify the server certificate
220 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 mbedtls_printf(" . Verifying peer X.509 certificate...");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 /* In real life, we would have used MBEDTLS_SSL_VERIFY_REQUIRED so that the
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200224 * handshake would not succeed if the peer's cert is bad. Even if we used
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 * MBEDTLS_SSL_VERIFY_OPTIONAL, we would bail out here if ret != 0 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 if ((flags = mbedtls_ssl_get_verify_result(&ssl)) != 0) {
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200227 char vrfy_buf[512];
228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 mbedtls_printf(" failed\n");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 mbedtls_printf("%s\n", vrfy_buf);
234 } else {
235 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200236 }
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200237
238 /*
239 * 6. Write the echo request
240 */
241send_request:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242 mbedtls_printf(" > Write to server:");
243 fflush(stdout);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 len = sizeof(MESSAGE) - 1;
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 do {
248 ret = mbedtls_ssl_write(&ssl, (unsigned char *) MESSAGE, len);
249 } while (ret == MBEDTLS_ERR_SSL_WANT_READ ||
250 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 if (ret < 0) {
253 mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n", ret);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200254 goto exit;
255 }
256
257 len = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 mbedtls_printf(" %d bytes written\n\n%s\n\n", len, MESSAGE);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200259
260 /*
261 * 7. Read the echo response
262 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 mbedtls_printf(" < Read from server:");
264 fflush(stdout);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 len = sizeof(buf) - 1;
267 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 do {
270 ret = mbedtls_ssl_read(&ssl, buf, len);
271 } while (ret == MBEDTLS_ERR_SSL_WANT_READ ||
272 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 if (ret <= 0) {
275 switch (ret) {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100276 case MBEDTLS_ERR_SSL_TIMEOUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 mbedtls_printf(" timeout\n\n");
278 if (retry_left-- > 0) {
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200279 goto send_request;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 }
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200281 goto exit;
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 mbedtls_printf(" connection was closed gracefully\n");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200285 goto close_notify;
286
287 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 mbedtls_printf(" mbedtls_ssl_read returned -0x%x\n\n", (unsigned int) -ret);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200289 goto exit;
290 }
291 }
292
293 len = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 mbedtls_printf(" %d bytes read\n\n%s\n\n", len, buf);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200295
296 /*
297 * 8. Done, cleanly close the connection
298 */
299close_notify:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 mbedtls_printf(" . Closing the connection...");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200301
302 /* No error checking, the connection might be closed already */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 do {
304 ret = mbedtls_ssl_close_notify(&ssl);
305 } while (ret == MBEDTLS_ERR_SSL_WANT_WRITE);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200306 ret = 0;
307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308 mbedtls_printf(" done\n");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200309
310 /*
311 * 9. Final clean-ups and exit
312 */
313exit:
314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#ifdef MBEDTLS_ERROR_C
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 if (ret != 0) {
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200317 char error_buf[100];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 mbedtls_strerror(ret, error_buf, 100);
319 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200320 }
321#endif
322
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 mbedtls_net_free(&server_fd);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100324 mbedtls_x509_crt_free(&cacert);
325 mbedtls_ssl_free(&ssl);
326 mbedtls_ssl_config_free(&conf);
327 mbedtls_ctr_drbg_free(&ctr_drbg);
328 mbedtls_entropy_free(&entropy);
Przemek Stekield4d049b2023-04-19 13:47:43 +0200329#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielc4ddf922023-04-19 10:15:26 +0200330 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200331#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200332
333#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334 mbedtls_printf(" + Press Enter to exit this program.\n");
335 fflush(stdout); getchar();
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200336#endif
337
338 /* Shell can not handle large exit numbers -> 1 for errors */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 if (ret < 0) {
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200340 ret = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 }
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 mbedtls_exit(ret);
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200344}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_NET_C &&
Tom Cosgrove49f99bc2022-12-04 16:44:21 +0000346 MBEDTLS_TIMING_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C && MBEDTLS_CERTS_C &&
348 MBEDTLS_PEM_PARSE_C */