blob: ed3669864a800e34c26a967cd79738b2d11ff968 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client with certificate authentication
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.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Mateusz Starzyk6c2e9b62021-05-19 17:54:54 +020020#define MBEDTLS_ALLOW_PRIVATE_ACCESS
21
Gilles Peskineab7ce962021-01-05 21:27:53 +010022#include "ssl_test_lib.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000023
Gilles Peskineab7ce962021-01-05 21:27:53 +010024#if defined(MBEDTLS_SSL_TEST_IMPOSSIBLE)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020025int main(void)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020026{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020027 mbedtls_printf(MBEDTLS_SSL_TEST_IMPOSSIBLE);
28 mbedtls_exit(0);
Gilles Peskineab7ce962021-01-05 21:27:53 +010029}
30#elif !defined(MBEDTLS_SSL_CLI_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020031int main(void)
Gilles Peskineab7ce962021-01-05 21:27:53 +010032{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020033 mbedtls_printf("MBEDTLS_SSL_CLI_C not defined.\n");
34 mbedtls_exit(0);
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020035}
Gilles Peskineb4df7542021-01-08 21:20:09 +010036#else /* !MBEDTLS_SSL_TEST_IMPOSSIBLE && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020037
Piotr Nowicki7d01ef62019-11-20 15:00:17 +010038/* Size of memory to be allocated for the heap, when using the library's memory
39 * management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020040# define MEMORY_HEAP_SIZE 120000
Piotr Nowicki7d01ef62019-11-20 15:00:17 +010041
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020042# define MAX_REQUEST_SIZE 20000
43# define MAX_REQUEST_SIZE_STR "20000"
Hanno Beckere4ad3e82017-09-18 15:05:46 +010044
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020045# define DFL_SERVER_NAME "localhost"
46# define DFL_SERVER_ADDR NULL
47# define DFL_SERVER_PORT "4433"
48# define DFL_REQUEST_PAGE "/"
49# define DFL_REQUEST_SIZE -1
50# define DFL_DEBUG_LEVEL 0
51# define DFL_CONTEXT_CRT_CB 0
52# define DFL_NBIO 0
53# define DFL_EVENT 0
54# define DFL_READ_TIMEOUT 0
55# define DFL_MAX_RESEND 0
56# define DFL_CA_FILE ""
57# define DFL_CA_PATH ""
58# define DFL_CRT_FILE ""
59# define DFL_KEY_FILE ""
60# define DFL_KEY_OPAQUE 0
61# define DFL_KEY_PWD ""
62# define DFL_PSK ""
63# define DFL_PSK_OPAQUE 0
64# define DFL_PSK_IDENTITY "Client_identity"
65# define DFL_ECJPAKE_PW NULL
66# define DFL_EC_MAX_OPS -1
67# define DFL_FORCE_CIPHER 0
68# define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
69# define DFL_ALLOW_LEGACY -2
70# define DFL_RENEGOTIATE 0
71# define DFL_EXCHANGES 1
72# define DFL_MIN_VERSION -1
73# define DFL_MAX_VERSION -1
74# define DFL_SHA1 -1
75# define DFL_AUTH_MODE -1
76# define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
77# define DFL_TRUNC_HMAC -1
78# define DFL_RECSPLIT -1
79# define DFL_DHMLEN -1
80# define DFL_RECONNECT 0
81# define DFL_RECO_DELAY 0
82# define DFL_RECO_MODE 1
83# define DFL_CID_ENABLED 0
84# define DFL_CID_VALUE ""
85# define DFL_CID_ENABLED_RENEGO -1
86# define DFL_CID_VALUE_RENEGO NULL
87# define DFL_RECONNECT_HARD 0
88# define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
89# define DFL_ALPN_STRING NULL
90# define DFL_CURVES NULL
91# define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
92# define DFL_HS_TO_MIN 0
93# define DFL_HS_TO_MAX 0
94# define DFL_DTLS_MTU -1
95# define DFL_DGRAM_PACKING 1
96# define DFL_FALLBACK -1
97# define DFL_EXTENDED_MS -1
98# define DFL_ETM -1
99# define DFL_SERIALIZE 0
100# define DFL_CONTEXT_FILE ""
101# define DFL_EXTENDED_MS_ENFORCE -1
102# define DFL_CA_CALLBACK 0
103# define DFL_EAP_TLS 0
104# define DFL_REPRODUCIBLE 0
105# define DFL_NSS_KEYLOG 0
106# define DFL_NSS_KEYLOG_FILE NULL
107# define DFL_SKIP_CLOSE_NOTIFY 0
108# define DFL_QUERY_CONFIG_MODE 0
109# define DFL_USE_SRTP 0
110# define DFL_SRTP_FORCE_PROFILE 0
111# define DFL_SRTP_MKI ""
Paul Bakker0e6975b2009-02-10 22:19:10 +0000112
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200113# define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
114# define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200116# if defined(MBEDTLS_X509_CRT_PARSE_C)
117# define USAGE_CONTEXT_CRT_CB \
118 " context_crt_cb=%%d This determines whether the CRT verification callback is bound\n" \
119 " to the SSL configuration of the SSL context.\n" \
120 " Possible values:\n" \
121 " - 0 (default): Use CRT callback bound to configuration\n" \
122 " - 1: Use CRT callback bound to SSL context\n"
123# else
124# define USAGE_CONTEXT_CRT_CB ""
125# endif /* MBEDTLS_X509_CRT_PARSE_C */
126# if defined(MBEDTLS_X509_CRT_PARSE_C)
127# if defined(MBEDTLS_FS_IO)
128# define USAGE_IO \
129 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
130 " default: \"\" (pre-loaded)\n" \
131 " use \"none\" to skip loading any top-level CAs.\n" \
132 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
133 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
134 " use \"none\" to skip loading any top-level CAs.\n" \
135 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
136 " default: \"\" (pre-loaded)\n" \
137 " key_file=%%s default: \"\" (pre-loaded)\n" \
138 " key_pwd=%%s Password for key specified by key_file argument\n" \
139 " default: none\n"
140# else
141# define USAGE_IO \
142 " No file operations available (MBEDTLS_FS_IO not defined)\n"
143# endif /* MBEDTLS_FS_IO */
144# else /* MBEDTLS_X509_CRT_PARSE_C */
145# define USAGE_IO ""
146# endif /* MBEDTLS_X509_CRT_PARSE_C */
147# if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
148# define USAGE_KEY_OPAQUE \
149 " key_opaque=%%d Handle your private key as if it were opaque\n" \
150 " default: 0 (disabled)\n"
151# else
152# define USAGE_KEY_OPAQUE ""
153# endif
Paul Bakkered27a042013-04-18 22:46:23 +0200154
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200155# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
156# define USAGE_CID \
157 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
158 " default: 0 (disabled)\n" \
159 " cid_renego=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension during renegotiation.\n" \
160 " default: same as 'cid' parameter\n" \
161 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
162 " default: \"\"\n" \
163 " cid_val_renego=%%s The CID to use for incoming messages (in hex, without 0x) after renegotiation.\n" \
164 " default: same as 'cid_val' parameter\n"
165# else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
166# define USAGE_CID ""
167# endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +0100168
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200169# if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
170# define USAGE_PSK_RAW \
171 " psk=%%s default: \"\" (disabled)\n" \
172 " The PSK values are in hex, without 0x.\n" \
173 " psk_identity=%%s default: \"Client_identity\"\n"
174# if defined(MBEDTLS_USE_PSA_CRYPTO)
175# define USAGE_PSK_SLOT \
176 " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
177 " Enable this to store the PSK configured through command line\n" \
178 " parameter `psk` in a PSA-based key slot.\n" \
179 " Note: Currently only supported in conjunction with\n" \
180 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
181 " to force a particular PSK-only ciphersuite.\n" \
182 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
183 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
184 " with prepopulated key slots instead of importing raw key material.\n"
185# else
186# define USAGE_PSK_SLOT ""
187# endif /* MBEDTLS_USE_PSA_CRYPTO */
188# define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
189# else
190# define USAGE_PSK ""
191# endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker1bac87c2019-03-29 12:02:26 +0000192
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200193# if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
194# define USAGE_CA_CALLBACK \
195 " ca_callback=%%d default: 0 (disabled)\n" \
196 " Enable this to use the trusted certificate callback function\n"
197# else
198# define USAGE_CA_CALLBACK ""
199# endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5690efc2011-05-26 13:16:06 +0000200
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200201# if defined(MBEDTLS_SSL_SESSION_TICKETS)
202# define USAGE_TICKETS " tickets=%%d default: 1 (enabled)\n"
203# else
204# define USAGE_TICKETS ""
205# endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200206
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200207# if defined(MBEDTLS_SSL_EXPORT_KEYS)
208# define USAGE_EAP_TLS " eap_tls=%%d default: 0 (disabled)\n"
209# define USAGE_NSS_KEYLOG \
210 " nss_keylog=%%d default: 0 (disabled)\n" \
211 " This cannot be used with eap_tls=1\n"
212# define USAGE_NSS_KEYLOG_FILE " nss_keylog_file=%%s\n"
213# if defined(MBEDTLS_SSL_DTLS_SRTP)
214# define USAGE_SRTP \
215 " use_srtp=%%d default: 0 (disabled)\n" \
216 " This cannot be used with eap_tls=1 or " \
217 " nss_keylog=1\n" \
218 " srtp_force_profile=%%d default: 0 (all enabled)\n" \
219 " available profiles:\n" \
220 " 1 - SRTP_AES128_CM_HMAC_SHA1_80\n" \
221 " 2 - SRTP_AES128_CM_HMAC_SHA1_32\n" \
222 " 3 - SRTP_NULL_HMAC_SHA1_80\n" \
223 " 4 - SRTP_NULL_HMAC_SHA1_32\n" \
224 " mki=%%s default: \"\" (in hex, without 0x)\n"
225# else /* MBEDTLS_SSL_DTLS_SRTP */
226# define USAGE_SRTP ""
227# endif
228# else /* MBEDTLS_SSL_EXPORT_KEYS */
229# define USAGE_EAP_TLS ""
230# define USAGE_NSS_KEYLOG ""
231# define USAGE_NSS_KEYLOG_FILE ""
232# define USAGE_SRTP ""
233# endif /* MBEDTLS_SSL_EXPORT_KEYS */
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300234
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200235# if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
236# define USAGE_MAX_FRAG_LEN \
237 " max_frag_len=%%d default: 16384 (tls default)\n" \
238 " options: 512, 1024, 2048, 4096\n"
239# else
240# define USAGE_MAX_FRAG_LEN ""
241# endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200242
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200243# if defined(MBEDTLS_DHM_C)
244# define USAGE_DHMLEN \
245 " dhmlen=%%d default: (library default: 1024 bits)\n"
246# else
247# define USAGE_DHMLEN
248# endif
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200249
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200250# if defined(MBEDTLS_SSL_ALPN)
251# define USAGE_ALPN \
252 " alpn=%%s default: \"\" (disabled)\n" \
253 " example: spdy/1,http/1.1\n"
254# else
255# define USAGE_ALPN ""
256# endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200257
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200258# if defined(MBEDTLS_ECP_C)
259# define USAGE_CURVES \
260 " curves=a,b,c,d default: \"default\" (library default)\n" \
261 " example: \"secp521r1,brainpoolP512r1\"\n" \
262 " - use \"none\" for empty list\n" \
263 " - see mbedtls_ecp_curve_list()\n" \
264 " for acceptable curve names\n"
265# else
266# define USAGE_CURVES ""
267# endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100268
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200269# if defined(MBEDTLS_SSL_PROTO_DTLS)
270# define USAGE_DTLS \
271 " dtls=%%d default: 0 (TLS)\n" \
272 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
273 " range of DTLS handshake timeouts in millisecs\n" \
274 " mtu=%%d default: (library default: unlimited)\n" \
275 " dgram_packing=%%d default: 1 (allowed)\n" \
276 " allow or forbid packing of multiple\n" \
277 " records within a single datgram.\n"
278# else
279# define USAGE_DTLS ""
280# endif
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200281
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200282# if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
283# define USAGE_EMS \
284 " extended_ms=0/1 default: (library default: on)\n"
285# else
286# define USAGE_EMS ""
287# endif
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200288
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200289# if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
290# define USAGE_ETM \
291 " etm=0/1 default: (library default: on)\n"
292# else
293# define USAGE_ETM ""
294# endif
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100295
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200296# define USAGE_REPRODUCIBLE \
297 " reproducible=0/1 default: 0 (disabled)\n"
Philippe Antoine738153a2019-06-18 20:16:43 +0200298
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200299# if defined(MBEDTLS_SSL_RENEGOTIATION)
300# define USAGE_RENEGO \
301 " renegotiation=%%d default: 0 (disabled)\n" \
302 " renegotiate=%%d default: 0 (disabled)\n"
303# else
304# define USAGE_RENEGO ""
305# endif
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100306
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200307# if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
308# define USAGE_ECJPAKE \
309 " ecjpake_pw=%%s default: none (disabled)\n"
310# else
311# define USAGE_ECJPAKE ""
312# endif
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200313
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200314# if defined(MBEDTLS_ECP_RESTARTABLE)
315# define USAGE_ECRESTART \
316 " ec_max_ops=%%s default: library default (restart disabled)\n"
317# else
318# define USAGE_ECRESTART ""
319# endif
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200320
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200321# if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
322# define USAGE_SERIALIZATION \
323 " serialize=%%d default: 0 (do not serialize/deserialize)\n" \
324 " options: 1 (serialize)\n" \
325 " 2 (serialize with re-initialization)\n" \
326 " context_file=%%s The file path to write a serialized connection\n" \
327 " in the form of base64 code (serialize option\n" \
328 " must be set)\n" \
329 " default: \"\" (do nothing)\n" \
330 " option: a file path\n"
331# else
332# define USAGE_SERIALIZATION ""
333# endif
Jarno Lamsabbc7b412019-06-04 11:06:31 +0300334
Gilles Peskineb2971ff2020-04-14 19:41:01 +0200335/* USAGE is arbitrarily split to stay under the portable string literal
336 * length limit: 4095 bytes in C99. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200337# define USAGE1 \
338 "\n usage: ssl_client2 param=<>...\n" \
339 "\n acceptable parameters:\n" \
340 " server_name=%%s default: localhost\n" \
341 " server_addr=%%s default: given by name\n" \
342 " server_port=%%d default: 4433\n" \
343 " request_page=%%s default: \".\"\n" \
344 " request_size=%%d default: about 34 (basic request)\n" \
345 " (minimum: 0, max: " MAX_REQUEST_SIZE_STR \
346 ")\n" \
347 " If 0, in the first exchange only an empty\n" \
348 " application data message is sent followed by\n" \
349 " a second non-empty message before attempting\n" \
350 " to read a response from the server\n" \
351 " debug_level=%%d default: 0 (disabled)\n" \
352 " nbio=%%d default: 0 (blocking I/O)\n" \
353 " options: 1 (non-blocking), 2 (added delays)\n" \
354 " event=%%d default: 0 (loop)\n" \
355 " options: 1 (level-triggered, implies nbio=1),\n" \
356 " read_timeout=%%d default: 0 ms (no timeout)\n" \
357 " max_resend=%%d default: 0 (no resend on timeout)\n" \
358 " skip_close_notify=%%d default: 0 (send close_notify)\n" \
359 "\n" USAGE_DTLS USAGE_CID USAGE_SRTP "\n"
360# define USAGE2 \
361 " auth_mode=%%s default: (library default: none)\n" \
362 " options: none, optional, required\n" USAGE_IO \
363 USAGE_KEY_OPAQUE USAGE_CA_CALLBACK \
364 "\n" USAGE_PSK USAGE_ECJPAKE USAGE_ECRESTART "\n"
365# define USAGE3 \
366 " allow_legacy=%%d default: (library default: no)\n" USAGE_RENEGO \
367 " exchanges=%%d default: 1\n" \
368 " reconnect=%%d number of reconnections using session resumption\n" \
369 " default: 0 (disabled)\n" \
370 " reco_delay=%%d default: 0 seconds\n" \
371 " reco_mode=%%d 0: copy session, 1: serialize session\n" \
372 " default: 1\n" \
373 " reconnect_hard=%%d default: 0 (disabled)\n" USAGE_TICKETS \
374 USAGE_EAP_TLS USAGE_MAX_FRAG_LEN USAGE_CONTEXT_CRT_CB USAGE_ALPN \
375 USAGE_EMS USAGE_ETM USAGE_REPRODUCIBLE USAGE_CURVES \
376 USAGE_DHMLEN "\n"
377# define USAGE4 \
378 " allow_sha1=%%d default: 0\n" \
379 " min_version=%%s default: (library default: tls1_2)\n" \
380 " max_version=%%s default: (library default: tls1_2)\n" \
381 " force_version=%%s default: \"\" (none)\n" \
382 " options: tls1_2, dtls1_2\n" \
383 "\n" \
384 " force_ciphersuite=<name> default: all enabled\n" \
385 " query_config=<name> return 0 if the specified\n" \
386 " configuration macro is defined and 1\n" \
387 " otherwise. The expansion of the macro\n" \
388 " is printed if it is defined\n" USAGE_SERIALIZATION \
389 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000390
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200391# define ALPN_LIST_SIZE 10
392# define CURVE_LIST_SIZE 20
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500393
Rich Evans85b05ec2015-02-12 11:37:29 +0000394/*
395 * global options
396 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200397struct options {
398 const char *server_name; /* hostname of the server (client only) */
399 const char *server_addr; /* address of the server (client only) */
400 const char *server_port; /* port on which the ssl service runs */
401 int debug_level; /* level of debugging */
402 int nbio; /* should I/O be blocking? */
403 int event; /* loop or event-driven IO? level or edge triggered? */
404 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
405 int max_resend; /* DTLS times to resend on read timeout */
406 const char *request_page; /* page on server to request */
407 int request_size; /* pad request with header to requested size */
408 const char *ca_file; /* the file with the CA certificate(s) */
409 const char *ca_path; /* the path with the CA certificate(s) reside */
410 const char *crt_file; /* the file with the client certificate */
411 const char *key_file; /* the file with the client key */
412 int key_opaque; /* handle private key as if it were opaque */
413# if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000414 int psk_opaque;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200415# endif
416# if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
417 int ca_callback; /* Use callback for trusted certificate list */
418# endif
419 const char *key_pwd; /* the password for the client key */
420 const char *psk; /* the pre-shared key */
421 const char *psk_identity; /* the pre-shared key identity */
422 const char *ecjpake_pw; /* the EC J-PAKE password */
423 int ec_max_ops; /* EC consecutive operations limit */
424 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
425 int renegotiation; /* enable / disable renegotiation */
426 int allow_legacy; /* allow legacy renegotiation */
427 int renegotiate; /* attempt renegotiation? */
428 int renego_delay; /* delay before enforcing renegotiation */
429 int exchanges; /* number of data exchanges */
430 int min_version; /* minimum protocol version accepted */
431 int max_version; /* maximum protocol version accepted */
432 int allow_sha1; /* flag for SHA-1 support */
433 int auth_mode; /* verify mode for connection */
434 unsigned char mfl_code; /* code for maximum fragment length */
435 int trunc_hmac; /* negotiate truncated hmac or not */
436 int recsplit; /* enable record splitting? */
437 int dhmlen; /* minimum DHM params len in bits */
438 int reconnect; /* attempt to resume session */
439 int reco_delay; /* delay in seconds before resuming session */
440 int reco_mode; /* how to keep the session around */
441 int reconnect_hard; /* unexpectedly reconnect from the same port */
442 int tickets; /* enable / disable session tickets */
443 const char *curves; /* list of supported elliptic curves */
444 const char *alpn_string; /* ALPN supported protocols */
445 int transport; /* TLS or DTLS? */
446 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
447 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
448 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
449 int fallback; /* is this a fallback connection? */
450 int dgram_packing; /* allow/forbid datagram packing */
451 int extended_ms; /* negotiate extended master secret? */
452 int etm; /* negotiate encrypt then mac? */
453 int context_crt_cb; /* use context-specific CRT verify callback */
454 int eap_tls; /* derive EAP-TLS keying material? */
455 int nss_keylog; /* export NSS key log material */
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100456 const char *nss_keylog_file; /* NSS key log file */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200457 int cid_enabled; /* whether to use the CID extension or not */
458 int cid_enabled_renego; /* whether to use the CID extension or not
459 * during renegotiation */
460 const char *cid_val; /* the CID to use for incoming messages */
461 int serialize; /* serialize/deserialize connection */
462 const char *context_file; /* the file to write a serialized connection
463 * in the form of base64 code (serialize
464 * option must be set) */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100465 const char *cid_val_renego; /* the CID to use for incoming messages
466 * after renegotiation */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200467 int reproducible; /* make communication reproducible */
468 int skip_close_notify; /* skip sending the close_notify alert */
469 int query_config_mode; /* whether to read config */
470 int use_srtp; /* Support SRTP */
471 int force_srtp_profile; /* SRTP protection profile to use or all */
472 const char *mki; /* The dtls mki value to use */
Rich Evans85b05ec2015-02-12 11:37:29 +0000473} opt;
474
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200475# include "ssl_test_common_source.c"
Hanno Becker8b1af2f2019-07-03 17:02:43 +0100476
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200477# if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckera1051b42019-02-26 11:38:29 +0000478static unsigned char peer_crt_info[1024];
Hanno Beckera9766c2c2019-02-25 17:43:18 +0000479
Rich Evans85b05ec2015-02-12 11:37:29 +0000480/*
481 * Enabled if debug_level > 1 in code below
482 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200483static int
484my_verify(void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags)
Rich Evans85b05ec2015-02-12 11:37:29 +0000485{
486 char buf[1024];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200487 ((void)data);
Rich Evans85b05ec2015-02-12 11:37:29 +0000488
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200489 mbedtls_printf("\nVerify requested for (Depth %d):\n", depth);
Hanno Becker7ac83f92020-10-09 10:48:22 +0100490
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200491# if !defined(MBEDTLS_X509_REMOVE_INFO)
492 mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
493 if (depth == 0)
494 memcpy(peer_crt_info, buf, sizeof(buf));
Hanno Beckera9766c2c2019-02-25 17:43:18 +0000495
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200496 if (opt.debug_level == 0)
497 return 0;
Hanno Beckera9766c2c2019-02-25 17:43:18 +0000498
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200499 mbedtls_printf("%s", buf);
500# else
501 ((void)crt);
502 ((void)depth);
503# endif
Rich Evans85b05ec2015-02-12 11:37:29 +0000504
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200505 if ((*flags) == 0)
506 mbedtls_printf(" This certificate has no flags\n");
507 else {
508 x509_crt_verify_info(buf, sizeof(buf), " ! ", *flags);
509 mbedtls_printf("%s\n", buf);
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100510 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000511
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200512 return 0;
Rich Evans85b05ec2015-02-12 11:37:29 +0000513}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200514# endif /* MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200515
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200516# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
517int report_cid_usage(mbedtls_ssl_context *ssl,
518 const char *additional_description)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100519{
520 int ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200521 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX];
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100522 size_t peer_cid_len;
523 int cid_negotiated;
524
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200525 if (opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM)
526 return 0;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100527
Hanno Becker6ae14c02019-05-22 16:59:25 +0100528 /* Check if the use of a CID has been negotiated,
529 * but don't ask for the CID value and length.
530 *
531 * Note: Here and below, we're demonstrating the various ways
532 * in which mbedtls_ssl_get_peer_cid() can be called,
533 * depending on whether or not the length/value of the
534 * peer's CID is needed.
535 *
536 * An actual application, however, should use
537 * just one call to mbedtls_ssl_get_peer_cid(). */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200538 ret = mbedtls_ssl_get_peer_cid(ssl, &cid_negotiated, NULL, NULL);
539 if (ret != 0) {
540 mbedtls_printf(
541 " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
542 (unsigned int)-ret);
543 return ret;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100544 }
545
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200546 if (cid_negotiated == MBEDTLS_SSL_CID_DISABLED) {
547 if (opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED) {
548 mbedtls_printf(
549 "(%s) Use of Connection ID was rejected by the server.\n",
550 additional_description);
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100551 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200552 } else {
553 size_t idx = 0;
554 mbedtls_printf("(%s) Use of Connection ID has been negotiated.\n",
555 additional_description);
Hanno Becker6ae14c02019-05-22 16:59:25 +0100556
557 /* Ask for just the length of the peer's CID. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200558 ret =
559 mbedtls_ssl_get_peer_cid(ssl, &cid_negotiated, NULL, &peer_cid_len);
560 if (ret != 0) {
561 mbedtls_printf(
562 " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
563 (unsigned int)-ret);
564 return ret;
Hanno Becker6ae14c02019-05-22 16:59:25 +0100565 }
566
567 /* Ask for just length + value of the peer's CID. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200568 ret = mbedtls_ssl_get_peer_cid(ssl, &cid_negotiated, peer_cid,
569 &peer_cid_len);
570 if (ret != 0) {
571 mbedtls_printf(
572 " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
573 (unsigned int)-ret);
574 return ret;
Hanno Becker6ae14c02019-05-22 16:59:25 +0100575 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200576 mbedtls_printf("(%s) Peer CID (length %u Bytes): ",
577 additional_description, (unsigned)peer_cid_len);
578 while (idx < peer_cid_len) {
579 mbedtls_printf("%02x ", peer_cid[idx]);
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100580 idx++;
581 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200582 mbedtls_printf("\n");
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100583 }
584
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200585 return 0;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100586}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200587# endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100588
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200589int main(int argc, char *argv[])
Paul Bakker5121ce52009-01-03 21:22:43 +0000590{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200591 int ret = 0, len, tail_len, i, written, frags, retry_left;
gabor-mezei-arma9eecf12020-07-07 11:11:02 +0200592 int query_config_ret = 0;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200593 mbedtls_net_context server_fd;
Hanno Becker8b1af2f2019-07-03 17:02:43 +0100594 io_ctx_t io_ctx;
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100595
596 unsigned char buf[MAX_REQUEST_SIZE + 1];
597
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200598# if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200600 size_t psk_len = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200601# endif
Hanno Becker90cb3592019-04-09 17:24:19 +0100602
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200603# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +0100604 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100605 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker90cb3592019-04-09 17:24:19 +0100606 size_t cid_len = 0;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100607 size_t cid_renego_len = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200608# endif
Hanno Becker90cb3592019-04-09 17:24:19 +0100609
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200610# if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100611 const char *alpn_list[ALPN_LIST_SIZE];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200612# endif
Piotr Nowicki7d01ef62019-11-20 15:00:17 +0100613
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200614# if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Piotr Nowicki7d01ef62019-11-20 15:00:17 +0100615 unsigned char alloc_buf[MEMORY_HEAP_SIZE];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200616# endif
Piotr Nowicki7d01ef62019-11-20 15:00:17 +0100617
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200618# if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +0100619 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100620 const mbedtls_ecp_curve_info *curve_cur;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200621# endif
622# if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascal85269572020-08-25 10:01:54 +0200623 unsigned char mki[MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200624 size_t mki_len = 0;
625# endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100626
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200627 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000628
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200629# if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Cronadc2ff22020-09-16 16:49:27 +0200630 psa_key_id_t slot = 0;
Hanno Beckere86964c2018-10-23 11:37:50 +0100631 psa_algorithm_t alg = 0;
Janos Follathbe4efc22019-08-08 11:38:18 +0100632 psa_key_attributes_t key_attributes;
Hanno Beckere86964c2018-10-23 11:37:50 +0100633 psa_status_t status;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200634# endif
Hanno Beckere86964c2018-10-23 11:37:50 +0100635
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200636# if defined(MBEDTLS_X509_CRT_PARSE_C)
637 mbedtls_x509_crt_profile crt_profile_for_test =
638 mbedtls_x509_crt_profile_default;
639# endif
Gilles Peskinef1cb75f2021-01-13 18:46:01 +0100640 rng_context_t rng;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200642 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +0200644 unsigned char *session_data = NULL;
645 size_t session_data_len = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200646# if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200647 mbedtls_timing_delay_context timer;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200648# endif
649# if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200650 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_x509_crt cacert;
652 mbedtls_x509_crt clicert;
653 mbedtls_pk_context pkey;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200654# if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Cronadc2ff22020-09-16 16:49:27 +0200655 psa_key_id_t key_slot = 0; /* invalid key slot */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200656# endif
657# endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker9caf2d22010-02-18 19:37:19 +0000658 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000659 const int *list;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200660# if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +0200661 unsigned char *context_buf = NULL;
662 size_t context_buf_len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200663# endif
664# if defined(MBEDTLS_SSL_EXPORT_KEYS)
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300665 unsigned char eap_tls_keymaterial[16];
666 unsigned char eap_tls_iv[8];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200667 const char *eap_tls_label = "client EAP encryption";
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300668 eap_tls_keys eap_tls_keying;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200669# if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor65b56ef2019-09-26 16:40:48 +0300670 /*! master keys and master salt for SRTP generated during handshake */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200671 unsigned char
672 dtls_srtp_key_material[MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH];
673 const char *dtls_srtp_label = "EXTRACTOR-dtls_srtp";
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200674 dtls_srtp_keys dtls_srtp_keying;
675 const mbedtls_ssl_srtp_profile default_profiles[] = {
Johan Pascal43f94902020-09-22 12:25:52 +0200676 MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80,
677 MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_32,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200678 MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_80, MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_32,
Johan Pascal253d0262020-09-22 13:04:45 +0200679 MBEDTLS_TLS_SRTP_UNSET
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200680 };
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200681# endif /* MBEDTLS_SSL_DTLS_SRTP */
682# endif /* MBEDTLS_SSL_EXPORT_KEYS */
Paul Bakker9caf2d22010-02-18 19:37:19 +0000683
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200684# if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
685 mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
686# endif
Piotr Nowicki7d01ef62019-11-20 15:00:17 +0100687
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200688# if defined(MBEDTLS_TEST_HOOKS)
689 test_hooks_init();
690# endif /* MBEDTLS_TEST_HOOKS */
Gilles Peskine53dea742021-02-02 22:55:06 +0100691
Paul Bakker1a207ec2011-02-06 13:22:40 +0000692 /*
693 * Make sure memory references are valid.
694 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200695 mbedtls_net_init(&server_fd);
696 mbedtls_ssl_init(&ssl);
697 mbedtls_ssl_config_init(&conf);
698 memset(&saved_session, 0, sizeof(mbedtls_ssl_session));
699 rng_init(&rng);
700# if defined(MBEDTLS_X509_CRT_PARSE_C)
701 mbedtls_x509_crt_init(&cacert);
702 mbedtls_x509_crt_init(&clicert);
703 mbedtls_pk_init(&pkey);
704# endif
705# if defined(MBEDTLS_SSL_ALPN)
706 memset((void *)alpn_list, 0, sizeof(alpn_list));
707# endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000708
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200709# if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerb2b468b2018-11-12 17:46:59 +0000710 status = psa_crypto_init();
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200711 if (status != PSA_SUCCESS) {
712 mbedtls_fprintf(stderr,
713 "Failed to initialize PSA Crypto implementation: %d\n",
714 (int)status);
Hanno Beckerb2b468b2018-11-12 17:46:59 +0000715 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
716 goto exit;
717 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200718# endif /* MBEDTLS_USE_PSA_CRYPTO */
719# if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
720 mbedtls_test_enable_insecure_external_rng();
721# endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Hanno Beckerb2b468b2018-11-12 17:46:59 +0000722
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200723 if (argc == 0) {
724usage:
725 if (ret == 0)
Paul Bakkerfab5c822012-02-06 16:45:10 +0000726 ret = 1;
727
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200728 mbedtls_printf(USAGE1);
729 mbedtls_printf(USAGE2);
730 mbedtls_printf(USAGE3);
731 mbedtls_printf(USAGE4);
Paul Bakker51936882011-02-20 16:05:58 +0000732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 list = mbedtls_ssl_list_ciphersuites();
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200734 while (*list) {
735 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name(*list));
Paul Bakkered27a042013-04-18 22:46:23 +0200736 list++;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200737 if (!*list)
Paul Bakkered27a042013-04-18 22:46:23 +0200738 break;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200739 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name(*list));
Paul Bakker51936882011-02-20 16:05:58 +0000740 list++;
741 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000743 goto exit;
744 }
745
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200746 opt.server_name = DFL_SERVER_NAME;
747 opt.server_addr = DFL_SERVER_ADDR;
748 opt.server_port = DFL_SERVER_PORT;
749 opt.debug_level = DFL_DEBUG_LEVEL;
750 opt.cid_enabled = DFL_CID_ENABLED;
751 opt.cid_val = DFL_CID_VALUE;
752 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
753 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
754 opt.nbio = DFL_NBIO;
755 opt.event = DFL_EVENT;
756 opt.context_crt_cb = DFL_CONTEXT_CRT_CB;
757 opt.read_timeout = DFL_READ_TIMEOUT;
758 opt.max_resend = DFL_MAX_RESEND;
759 opt.request_page = DFL_REQUEST_PAGE;
760 opt.request_size = DFL_REQUEST_SIZE;
761 opt.ca_file = DFL_CA_FILE;
762 opt.ca_path = DFL_CA_PATH;
763 opt.crt_file = DFL_CRT_FILE;
764 opt.key_file = DFL_KEY_FILE;
765 opt.key_opaque = DFL_KEY_OPAQUE;
766 opt.key_pwd = DFL_KEY_PWD;
767 opt.psk = DFL_PSK;
768# if defined(MBEDTLS_USE_PSA_CRYPTO)
769 opt.psk_opaque = DFL_PSK_OPAQUE;
770# endif
771# if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
772 opt.ca_callback = DFL_CA_CALLBACK;
773# endif
774 opt.psk_identity = DFL_PSK_IDENTITY;
775 opt.ecjpake_pw = DFL_ECJPAKE_PW;
776 opt.ec_max_ops = DFL_EC_MAX_OPS;
777 opt.force_ciphersuite[0] = DFL_FORCE_CIPHER;
778 opt.renegotiation = DFL_RENEGOTIATION;
779 opt.allow_legacy = DFL_ALLOW_LEGACY;
780 opt.renegotiate = DFL_RENEGOTIATE;
781 opt.exchanges = DFL_EXCHANGES;
782 opt.min_version = DFL_MIN_VERSION;
783 opt.max_version = DFL_MAX_VERSION;
784 opt.allow_sha1 = DFL_SHA1;
785 opt.auth_mode = DFL_AUTH_MODE;
786 opt.mfl_code = DFL_MFL_CODE;
787 opt.trunc_hmac = DFL_TRUNC_HMAC;
788 opt.recsplit = DFL_RECSPLIT;
789 opt.dhmlen = DFL_DHMLEN;
790 opt.reconnect = DFL_RECONNECT;
791 opt.reco_delay = DFL_RECO_DELAY;
792 opt.reco_mode = DFL_RECO_MODE;
793 opt.reconnect_hard = DFL_RECONNECT_HARD;
794 opt.tickets = DFL_TICKETS;
795 opt.alpn_string = DFL_ALPN_STRING;
796 opt.curves = DFL_CURVES;
797 opt.transport = DFL_TRANSPORT;
798 opt.hs_to_min = DFL_HS_TO_MIN;
799 opt.hs_to_max = DFL_HS_TO_MAX;
800 opt.dtls_mtu = DFL_DTLS_MTU;
801 opt.fallback = DFL_FALLBACK;
802 opt.extended_ms = DFL_EXTENDED_MS;
803 opt.etm = DFL_ETM;
804 opt.dgram_packing = DFL_DGRAM_PACKING;
805 opt.serialize = DFL_SERIALIZE;
806 opt.context_file = DFL_CONTEXT_FILE;
807 opt.eap_tls = DFL_EAP_TLS;
808 opt.reproducible = DFL_REPRODUCIBLE;
809 opt.nss_keylog = DFL_NSS_KEYLOG;
810 opt.nss_keylog_file = DFL_NSS_KEYLOG_FILE;
811 opt.skip_close_notify = DFL_SKIP_CLOSE_NOTIFY;
812 opt.query_config_mode = DFL_QUERY_CONFIG_MODE;
813 opt.use_srtp = DFL_USE_SRTP;
814 opt.force_srtp_profile = DFL_SRTP_FORCE_PROFILE;
815 opt.mki = DFL_SRTP_MKI;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000816
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200817 for (i = 1; i < argc; i++) {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000818 p = argv[i];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200819 if ((q = strchr(p, '=')) == NULL)
Paul Bakker9caf2d22010-02-18 19:37:19 +0000820 goto usage;
821 *q++ = '\0';
822
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200823 if (strcmp(p, "server_name") == 0)
Paul Bakker9caf2d22010-02-18 19:37:19 +0000824 opt.server_name = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200825 else if (strcmp(p, "server_addr") == 0)
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100826 opt.server_addr = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200827 else if (strcmp(p, "server_port") == 0)
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200828 opt.server_port = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200829 else if (strcmp(p, "dtls") == 0) {
830 int t = atoi(q);
831 if (t == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200833 else if (t == 1)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100835 else
836 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200837 } else if (strcmp(p, "debug_level") == 0) {
838 opt.debug_level = atoi(q);
839 if (opt.debug_level < 0 || opt.debug_level > 65535)
Paul Bakker9caf2d22010-02-18 19:37:19 +0000840 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200841 } else if (strcmp(p, "context_crt_cb") == 0) {
842 opt.context_crt_cb = atoi(q);
843 if (opt.context_crt_cb != 0 && opt.context_crt_cb != 1)
Hanno Beckerbb425db2019-04-03 12:59:58 +0100844 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200845 } else if (strcmp(p, "nbio") == 0) {
846 opt.nbio = atoi(q);
847 if (opt.nbio < 0 || opt.nbio > 2)
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100848 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200849 } else if (strcmp(p, "event") == 0) {
850 opt.event = atoi(q);
851 if (opt.event < 0 || opt.event > 2)
Hanno Becker16970d22017-10-10 15:56:37 +0100852 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200853 } else if (strcmp(p, "read_timeout") == 0)
854 opt.read_timeout = atoi(q);
855 else if (strcmp(p, "max_resend") == 0) {
856 opt.max_resend = atoi(q);
857 if (opt.max_resend < 0)
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200858 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200859 } else if (strcmp(p, "request_page") == 0)
Paul Bakker9caf2d22010-02-18 19:37:19 +0000860 opt.request_page = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200861 else if (strcmp(p, "request_size") == 0) {
862 opt.request_size = atoi(q);
863 if (opt.request_size < 0 || opt.request_size > MAX_REQUEST_SIZE)
Paul Bakker93c32b22014-04-25 13:40:05 +0200864 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200865 } else if (strcmp(p, "ca_file") == 0)
Paul Bakker5690efc2011-05-26 13:16:06 +0000866 opt.ca_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200867 else if (strcmp(p, "ca_path") == 0)
Paul Bakker8d914582012-06-04 12:46:42 +0000868 opt.ca_path = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200869 else if (strcmp(p, "crt_file") == 0)
Paul Bakker67968392010-07-18 08:28:20 +0000870 opt.crt_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200871 else if (strcmp(p, "key_file") == 0)
Paul Bakker67968392010-07-18 08:28:20 +0000872 opt.key_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200873 else if (strcmp(p, "key_pwd") == 0)
Hanno Becker2d3ac682020-08-17 09:42:37 +0100874 opt.key_pwd = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200875# if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
876 else if (strcmp(p, "key_opaque") == 0)
877 opt.key_opaque = atoi(q);
878# endif
879# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
880 else if (strcmp(p, "cid") == 0) {
881 opt.cid_enabled = atoi(q);
882 if (opt.cid_enabled != 0 && opt.cid_enabled != 1)
Hanno Becker90cb3592019-04-09 17:24:19 +0100883 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200884 } else if (strcmp(p, "cid_renego") == 0) {
885 opt.cid_enabled_renego = atoi(q);
886 if (opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100887 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200888 } else if (strcmp(p, "cid_val") == 0) {
Hanno Becker90cb3592019-04-09 17:24:19 +0100889 opt.cid_val = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200890 } else if (strcmp(p, "cid_val_renego") == 0) {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100891 opt.cid_val_renego = q;
892 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200893# endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
894 else if (strcmp(p, "psk") == 0)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200895 opt.psk = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200896# if defined(MBEDTLS_USE_PSA_CRYPTO)
897 else if (strcmp(p, "psk_opaque") == 0)
898 opt.psk_opaque = atoi(q);
899# endif
900# if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
901 else if (strcmp(p, "ca_callback") == 0)
902 opt.ca_callback = atoi(q);
903# endif
904 else if (strcmp(p, "psk_identity") == 0)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200905 opt.psk_identity = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200906 else if (strcmp(p, "ecjpake_pw") == 0)
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200907 opt.ecjpake_pw = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200908 else if (strcmp(p, "ec_max_ops") == 0)
909 opt.ec_max_ops = atoi(q);
910 else if (strcmp(p, "force_ciphersuite") == 0) {
911 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(q);
Paul Bakker51936882011-02-20 16:05:58 +0000912
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200913 if (opt.force_ciphersuite[0] == 0) {
Paul Bakkerfab5c822012-02-06 16:45:10 +0000914 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000915 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000916 }
Paul Bakker51936882011-02-20 16:05:58 +0000917 opt.force_ciphersuite[1] = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200918 } else if (strcmp(p, "renegotiation") == 0) {
919 opt.renegotiation = (atoi(q)) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED :
920 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
921 } else if (strcmp(p, "allow_legacy") == 0) {
922 switch (atoi(q)) {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100923 case -1:
924 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
925 break;
926 case 0:
927 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
928 break;
929 case 1:
930 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
931 break;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200932 default:
933 goto usage;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100934 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200935 } else if (strcmp(p, "renegotiate") == 0) {
936 opt.renegotiate = atoi(q);
937 if (opt.renegotiate < 0 || opt.renegotiate > 1)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100938 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200939 } else if (strcmp(p, "exchanges") == 0) {
940 opt.exchanges = atoi(q);
941 if (opt.exchanges < 1)
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200942 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200943 } else if (strcmp(p, "reconnect") == 0) {
944 opt.reconnect = atoi(q);
945 if (opt.reconnect < 0 || opt.reconnect > 2)
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200946 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200947 } else if (strcmp(p, "reco_delay") == 0) {
948 opt.reco_delay = atoi(q);
949 if (opt.reco_delay < 0)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100950 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200951 } else if (strcmp(p, "reco_mode") == 0) {
952 opt.reco_mode = atoi(q);
953 if (opt.reco_mode < 0)
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +0200954 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200955 } else if (strcmp(p, "reconnect_hard") == 0) {
956 opt.reconnect_hard = atoi(q);
957 if (opt.reconnect_hard < 0 || opt.reconnect_hard > 1)
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200958 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200959 } else if (strcmp(p, "tickets") == 0) {
960 opt.tickets = atoi(q);
961 if (opt.tickets < 0 || opt.tickets > 2)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200962 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200963 } else if (strcmp(p, "alpn") == 0) {
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200964 opt.alpn_string = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200965 } else if (strcmp(p, "extended_ms") == 0) {
966 switch (atoi(q)) {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100967 case 0:
968 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
969 break;
970 case 1:
971 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
972 break;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200973 default:
974 goto usage;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200975 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200976 } else if (strcmp(p, "curves") == 0)
Hanno Beckere6706e62017-05-15 16:05:15 +0100977 opt.curves = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200978 else if (strcmp(p, "etm") == 0) {
979 switch (atoi(q)) {
980 case 0:
981 opt.etm = MBEDTLS_SSL_ETM_DISABLED;
982 break;
983 case 1:
984 opt.etm = MBEDTLS_SSL_ETM_ENABLED;
985 break;
986 default:
987 goto usage;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100988 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200989 } else if (strcmp(p, "min_version") == 0) {
990 if (strcmp(q, "tls1_2") == 0 || strcmp(q, "dtls1_2") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000992 else
993 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200994 } else if (strcmp(p, "max_version") == 0) {
995 if (strcmp(q, "tls1_2") == 0 || strcmp(q, "dtls1_2") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000997 else
998 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200999 } else if (strcmp(p, "allow_sha1") == 0) {
1000 switch (atoi(q)) {
1001 case 0:
1002 opt.allow_sha1 = 0;
1003 break;
1004 case 1:
1005 opt.allow_sha1 = 1;
1006 break;
1007 default:
1008 goto usage;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001009 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001010 } else if (strcmp(p, "force_version") == 0) {
1011 if (strcmp(q, "tls1_2") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1013 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001014 } else if (strcmp(q, "dtls1_2") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1016 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1017 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001018 } else
Paul Bakker1d29fb52012-09-28 13:28:45 +00001019 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001020 } else if (strcmp(p, "auth_mode") == 0) {
1021 if (strcmp(q, "none") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001023 else if (strcmp(q, "optional") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001025 else if (strcmp(q, "required") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001027 else
1028 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001029 } else if (strcmp(p, "max_frag_len") == 0) {
1030 if (strcmp(q, "512") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001032 else if (strcmp(q, "1024") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001034 else if (strcmp(q, "2048") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001036 else if (strcmp(q, "4096") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001038 else
1039 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001040 } else if (strcmp(p, "trunc_hmac") == 0) {
1041 switch (atoi(q)) {
1042 case 0:
1043 opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED;
1044 break;
1045 case 1:
1046 opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
1047 break;
1048 default:
1049 goto usage;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001050 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001051 } else if (strcmp(p, "hs_timeout") == 0) {
1052 if ((p = strchr(q, '-')) == NULL)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001053 goto usage;
1054 *p++ = '\0';
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001055 opt.hs_to_min = atoi(q);
1056 opt.hs_to_max = atoi(p);
1057 if (opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001058 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001059 } else if (strcmp(p, "mtu") == 0) {
1060 opt.dtls_mtu = atoi(q);
1061 if (opt.dtls_mtu < 0)
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001062 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001063 } else if (strcmp(p, "dgram_packing") == 0) {
1064 opt.dgram_packing = atoi(q);
1065 if (opt.dgram_packing != 0 && opt.dgram_packing != 1) {
Hanno Becker4d615912018-08-14 13:33:30 +01001066 goto usage;
1067 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001068 } else if (strcmp(p, "recsplit") == 0) {
1069 opt.recsplit = atoi(q);
1070 if (opt.recsplit < 0 || opt.recsplit > 1)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001071 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001072 } else if (strcmp(p, "dhmlen") == 0) {
1073 opt.dhmlen = atoi(q);
1074 if (opt.dhmlen < 0)
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001075 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001076 } else if (strcmp(p, "query_config") == 0) {
gabor-mezei-arma9eecf12020-07-07 11:11:02 +02001077 opt.query_config_mode = 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001078 query_config_ret = query_config(q);
gabor-mezei-arm78595852020-04-28 10:40:30 +02001079 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001080 } else if (strcmp(p, "serialize") == 0) {
1081 opt.serialize = atoi(q);
1082 if (opt.serialize < 0 || opt.serialize > 2)
Jarno Lamsa9831c8a2019-05-29 13:33:32 +03001083 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001084 } else if (strcmp(p, "context_file") == 0) {
Piotr Nowicki3de298f2020-04-16 14:35:19 +02001085 opt.context_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001086 } else if (strcmp(p, "eap_tls") == 0) {
1087 opt.eap_tls = atoi(q);
1088 if (opt.eap_tls < 0 || opt.eap_tls > 1)
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001089 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001090 } else if (strcmp(p, "reproducible") == 0) {
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001091 opt.reproducible = 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001092 } else if (strcmp(p, "nss_keylog") == 0) {
1093 opt.nss_keylog = atoi(q);
1094 if (opt.nss_keylog < 0 || opt.nss_keylog > 1)
Hanno Becker48f3a3d2019-08-29 06:31:22 +01001095 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001096 } else if (strcmp(p, "nss_keylog_file") == 0) {
Hanno Becker48f3a3d2019-08-29 06:31:22 +01001097 opt.nss_keylog_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001098 } else if (strcmp(p, "skip_close_notify") == 0) {
1099 opt.skip_close_notify = atoi(q);
1100 if (opt.skip_close_notify < 0 || opt.skip_close_notify > 1)
Manuel Pégourié-Gonnard56941fe2020-02-17 11:04:33 +01001101 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001102 } else if (strcmp(p, "use_srtp") == 0) {
1103 opt.use_srtp = atoi(q);
1104 } else if (strcmp(p, "srtp_force_profile") == 0) {
1105 opt.force_srtp_profile = atoi(q);
1106 } else if (strcmp(p, "mki") == 0) {
Ron Eldor6ea64512018-07-02 10:08:07 +03001107 opt.mki = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001108 } else
Paul Bakker9caf2d22010-02-18 19:37:19 +00001109 goto usage;
1110 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001111
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001112 if (opt.nss_keylog != 0 && opt.eap_tls != 0) {
1113 mbedtls_printf(
1114 "Error: eap_tls and nss_keylog options cannot be used together.\n");
Hanno Beckerbc5308c2019-09-09 11:38:51 +01001115 goto usage;
1116 }
1117
Hanno Becker16970d22017-10-10 15:56:37 +01001118 /* Event-driven IO is incompatible with the above custom
1119 * receive and send functions, as the polling builds on
1120 * refers to the underlying net_context. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001121 if (opt.event == 1 && opt.nbio != 1) {
1122 mbedtls_printf(
1123 "Warning: event-driven IO mandates nbio=1 - overwrite\n");
Hanno Becker16970d22017-10-10 15:56:37 +01001124 opt.nbio = 1;
1125 }
1126
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001127# if defined(MBEDTLS_DEBUG_C)
1128 mbedtls_debug_set_threshold(opt.debug_level);
1129# endif
Paul Bakkerc73079a2014-04-25 16:34:30 +02001130
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001131# if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001133 * Unhexify the pre-shared key if any is given
1134 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001135 if (strlen(opt.psk)) {
1136 if (mbedtls_test_unhexify(psk, sizeof(psk), opt.psk, &psk_len) != 0) {
1137 mbedtls_printf("pre-shared key not valid\n");
Hanno Becker1f583ee2019-04-09 17:12:56 +01001138 goto exit;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001139 }
1140 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001141# endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001142
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001143# if defined(MBEDTLS_USE_PSA_CRYPTO)
1144 if (opt.psk_opaque != 0) {
1145 if (opt.psk == NULL) {
1146 mbedtls_printf(
1147 "psk_opaque set but no psk to be imported specified.\n");
Hanno Beckere86964c2018-10-23 11:37:50 +01001148 ret = 2;
1149 goto usage;
1150 }
1151
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001152 if (opt.force_ciphersuite[0] <= 0) {
1153 mbedtls_printf(
1154 "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n");
Hanno Beckere86964c2018-10-23 11:37:50 +01001155 ret = 2;
1156 goto usage;
1157 }
1158 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001159# endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckere86964c2018-10-23 11:37:50 +01001160
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001161 if (opt.force_ciphersuite[0] > 0) {
Hanno Beckere86964c2018-10-23 11:37:50 +01001162 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1163 ciphersuite_info =
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001164 mbedtls_ssl_ciphersuite_from_id(opt.force_ciphersuite[0]);
Hanno Beckere86964c2018-10-23 11:37:50 +01001165
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001166 if (opt.max_version != -1 &&
1167 ciphersuite_info->min_minor_ver > opt.max_version) {
1168 mbedtls_printf(
1169 "forced ciphersuite not allowed with this protocol version\n");
Hanno Beckere86964c2018-10-23 11:37:50 +01001170 ret = 2;
1171 goto usage;
1172 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001173 if (opt.min_version != -1 &&
1174 ciphersuite_info->max_minor_ver < opt.min_version) {
1175 mbedtls_printf(
1176 "forced ciphersuite not allowed with this protocol version\n");
Hanno Beckere86964c2018-10-23 11:37:50 +01001177 ret = 2;
1178 goto usage;
1179 }
1180
1181 /* If the server selects a version that's not supported by
1182 * this suite, then there will be no common ciphersuite... */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001183 if (opt.max_version == -1 ||
1184 opt.max_version > ciphersuite_info->max_minor_ver) {
Hanno Beckere86964c2018-10-23 11:37:50 +01001185 opt.max_version = ciphersuite_info->max_minor_ver;
1186 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001187 if (opt.min_version < ciphersuite_info->min_minor_ver) {
Hanno Beckere86964c2018-10-23 11:37:50 +01001188 opt.min_version = ciphersuite_info->min_minor_ver;
TRodziewicz4ca18aa2021-05-20 14:46:20 +02001189 /* DTLS starts with TLS 1.2 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001190 if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1191 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_3)
TRodziewicz0f82ec62021-05-12 17:49:18 +02001192 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Hanno Beckere86964c2018-10-23 11:37:50 +01001193 }
1194
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001195# if defined(MBEDTLS_USE_PSA_CRYPTO)
1196 if (opt.psk_opaque != 0) {
Hanno Beckere86964c2018-10-23 11:37:50 +01001197 /* Ensure that the chosen ciphersuite is PSK-only; we must know
1198 * the ciphersuite in advance to set the correct policy for the
1199 * PSK key slot. This limitation might go away in the future. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001200 if (ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
1201 opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3) {
1202 mbedtls_printf(
1203 "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n");
Hanno Beckere86964c2018-10-23 11:37:50 +01001204 ret = 2;
1205 goto usage;
1206 }
1207
1208 /* Determine KDF algorithm the opaque PSK will be used in. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001209# if defined(MBEDTLS_SHA384_C)
1210 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384)
Hanno Beckere86964c2018-10-23 11:37:50 +01001211 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1212 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001213# endif /* MBEDTLS_SHA384_C */
Hanno Beckere86964c2018-10-23 11:37:50 +01001214 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1215 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001216# endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckere86964c2018-10-23 11:37:50 +01001217 }
1218
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001219# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1220 if (mbedtls_test_unhexify(cid, sizeof(cid), opt.cid_val, &cid_len) != 0) {
1221 mbedtls_printf("CID not valid\n");
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001222 goto exit;
1223 }
1224
1225 /* Keep CID settings for renegotiation unless
1226 * specified otherwise. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001227 if (opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001228 opt.cid_enabled_renego = opt.cid_enabled;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001229 if (opt.cid_val_renego == DFL_CID_VALUE_RENEGO)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001230 opt.cid_val_renego = opt.cid_val;
1231
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001232 if (mbedtls_test_unhexify(cid_renego, sizeof(cid_renego),
1233 opt.cid_val_renego, &cid_renego_len) != 0) {
1234 mbedtls_printf("CID not valid\n");
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001235 goto exit;
1236 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001237# endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01001238
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001239# if defined(MBEDTLS_ECP_C)
1240 if (opt.curves != NULL) {
1241 p = (char *)opt.curves;
Hanno Beckere6706e62017-05-15 16:05:15 +01001242 i = 0;
1243
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001244 if (strcmp(p, "none") == 0) {
Hanno Beckere6706e62017-05-15 16:05:15 +01001245 curve_list[0] = MBEDTLS_ECP_DP_NONE;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001246 } else if (strcmp(p, "default") != 0) {
Hanno Beckere6706e62017-05-15 16:05:15 +01001247 /* Leave room for a final NULL in curve list */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001248 while (i < CURVE_LIST_SIZE - 1 && *p != '\0') {
Hanno Beckere6706e62017-05-15 16:05:15 +01001249 q = p;
1250
1251 /* Terminate the current string */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001252 while (*p != ',' && *p != '\0')
Hanno Beckere6706e62017-05-15 16:05:15 +01001253 p++;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001254 if (*p == ',')
Hanno Beckere6706e62017-05-15 16:05:15 +01001255 *p++ = '\0';
1256
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001257 if ((curve_cur = mbedtls_ecp_curve_info_from_name(q)) != NULL) {
Hanno Beckere6706e62017-05-15 16:05:15 +01001258 curve_list[i++] = curve_cur->grp_id;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001259 } else {
1260 mbedtls_printf("unknown curve %s\n", q);
1261 mbedtls_printf("supported curves: ");
1262 for (curve_cur = mbedtls_ecp_curve_list();
Hanno Beckere6706e62017-05-15 16:05:15 +01001263 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001264 curve_cur++) {
1265 mbedtls_printf("%s ", curve_cur->name);
Hanno Beckere6706e62017-05-15 16:05:15 +01001266 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001267 mbedtls_printf("\n");
Hanno Beckere6706e62017-05-15 16:05:15 +01001268 goto exit;
1269 }
1270 }
1271
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001272 mbedtls_printf("Number of curves: %d\n", i);
Hanno Beckere6706e62017-05-15 16:05:15 +01001273
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001274 if (i == CURVE_LIST_SIZE - 1 && *p != '\0') {
1275 mbedtls_printf("curves list too long, maximum %d",
1276 CURVE_LIST_SIZE - 1);
Hanno Beckere6706e62017-05-15 16:05:15 +01001277 goto exit;
1278 }
1279
1280 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1281 }
1282 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001283# endif /* MBEDTLS_ECP_C */
Hanno Beckere6706e62017-05-15 16:05:15 +01001284
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001285# if defined(MBEDTLS_SSL_ALPN)
1286 if (opt.alpn_string != NULL) {
1287 p = (char *)opt.alpn_string;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001288 i = 0;
1289
1290 /* Leave room for a final NULL in alpn_list */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001291 while (i < ALPN_LIST_SIZE - 1 && *p != '\0') {
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001292 alpn_list[i++] = p;
1293
1294 /* Terminate the current string and move on to next one */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001295 while (*p != ',' && *p != '\0')
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001296 p++;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001297 if (*p == ',')
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001298 *p++ = '\0';
1299 }
1300 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001301# endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001302
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001303 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001304 * 0. Initialize the RNG and the session data
1305 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001306 mbedtls_printf("\n . Seeding the random number generator...");
1307 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +00001308
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001309 ret = rng_seed(&rng, opt.reproducible, pers);
1310 if (ret != 0)
Gilles Peskinedaa94c42021-01-13 18:38:27 +01001311 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001312 mbedtls_printf(" ok\n");
Paul Bakker5121ce52009-01-03 21:22:43 +00001313
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001314# if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 /*
1316 * 1.1. Load the trusted CA
1317 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001318 mbedtls_printf(" . Loading the CA root certificate ...");
1319 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +00001320
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001321 if (strcmp(opt.ca_path, "none") == 0 || strcmp(opt.ca_file, "none") == 0) {
Hanno Becker623e7b42019-03-05 16:02:15 +00001322 ret = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001323 } else
1324# if defined(MBEDTLS_FS_IO)
1325 if (strlen(opt.ca_path))
1326 ret = mbedtls_x509_crt_parse_path(&cacert, opt.ca_path);
1327 else if (strlen(opt.ca_file))
1328 ret = mbedtls_x509_crt_parse_file(&cacert, opt.ca_file);
Hanno Becker623e7b42019-03-05 16:02:15 +00001329 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001330# endif
Hanno Becker2900b142019-02-01 08:15:06 +00001331 {
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001332# if defined(MBEDTLS_PEM_PARSE_C)
1333 for (i = 0; mbedtls_test_cas[i] != NULL; i++) {
1334 ret = mbedtls_x509_crt_parse(
1335 &cacert, (const unsigned char *)mbedtls_test_cas[i],
1336 mbedtls_test_cas_len[i]);
1337 if (ret != 0)
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001338 break;
1339 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001340 if (ret == 0)
1341# endif /* MBEDTLS_PEM_PARSE_C */
1342 for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) {
1343 ret = mbedtls_x509_crt_parse_der(
1344 &cacert, (const unsigned char *)mbedtls_test_cas_der[i],
1345 mbedtls_test_cas_der_len[i]);
1346 if (ret != 0)
1347 break;
1348 }
Hanno Becker2900b142019-02-01 08:15:06 +00001349 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001350 if (ret < 0) {
1351 mbedtls_printf(
1352 " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1353 (unsigned int)-ret);
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 goto exit;
1355 }
1356
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001357 mbedtls_printf(" ok (%d skipped)\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +00001358
1359 /*
1360 * 1.2. Load own certificate and private key
1361 *
1362 * (can be skipped if client authentication is not required)
1363 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001364 mbedtls_printf(" . Loading the client cert. and key...");
1365 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +00001366
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001367 if (strcmp(opt.crt_file, "none") == 0)
Hanno Becker623e7b42019-03-05 16:02:15 +00001368 ret = 0;
1369 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001370# if defined(MBEDTLS_FS_IO)
1371 if (strlen(opt.crt_file))
1372 ret = mbedtls_x509_crt_parse_file(&clicert, opt.crt_file);
Paul Bakkered27a042013-04-18 22:46:23 +02001373 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001374# endif
1375 ret = mbedtls_x509_crt_parse(
1376 &clicert, (const unsigned char *)mbedtls_test_cli_crt,
1377 mbedtls_test_cli_crt_len);
1378 if (ret != 0) {
1379 mbedtls_printf(
1380 " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1381 (unsigned int)-ret);
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 goto exit;
1383 }
1384
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001385 if (strcmp(opt.key_file, "none") == 0)
Hanno Becker623e7b42019-03-05 16:02:15 +00001386 ret = 0;
1387 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001388# if defined(MBEDTLS_FS_IO)
1389 if (strlen(opt.key_file))
1390 ret = mbedtls_pk_parse_keyfile(&pkey, opt.key_file, opt.key_pwd,
1391 rng_get, &rng);
Paul Bakker67968392010-07-18 08:28:20 +00001392 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001393# endif
1394 ret = mbedtls_pk_parse_key(&pkey,
1395 (const unsigned char *)mbedtls_test_cli_key,
1396 mbedtls_test_cli_key_len, NULL, 0, rng_get,
1397 &rng);
1398 if (ret != 0) {
1399 mbedtls_printf(" failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
1400 (unsigned int)-ret);
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 goto exit;
1402 }
1403
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001404# if defined(MBEDTLS_USE_PSA_CRYPTO)
1405 if (opt.key_opaque != 0) {
1406 if ((ret = mbedtls_pk_wrap_as_opaque(&pkey, &key_slot,
1407 PSA_ALG_SHA_256)) != 0) {
1408 mbedtls_printf(" failed\n ! "
1409 "mbedtls_pk_wrap_as_opaque returned -0x%x\n\n",
1410 (unsigned int)-ret);
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001411 goto exit;
1412 }
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001413 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001414# endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001415
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001416 mbedtls_printf(" ok (key type: %s)\n", mbedtls_pk_get_name(&pkey));
1417# endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001418
1419 /*
1420 * 2. Start the connection
1421 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001422 if (opt.server_addr == NULL)
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001423 opt.server_addr = opt.server_name;
1424
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001425 mbedtls_printf(" . Connecting to %s/%s/%s...",
1426 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" :
1427 "udp",
1428 opt.server_addr, opt.server_port);
1429 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001431 if ((ret =
1432 mbedtls_net_connect(&server_fd, opt.server_addr, opt.server_port,
1433 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1434 MBEDTLS_NET_PROTO_TCP :
1435 MBEDTLS_NET_PROTO_UDP)) != 0) {
1436 mbedtls_printf(" failed\n ! mbedtls_net_connect returned -0x%x\n\n",
1437 (unsigned int)-ret);
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 goto exit;
1439 }
1440
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001441 if (opt.nbio > 0)
1442 ret = mbedtls_net_set_nonblock(&server_fd);
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001443 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001444 ret = mbedtls_net_set_block(&server_fd);
1445 if (ret != 0) {
1446 mbedtls_printf(" failed\n ! net_set_(non)block() returned -0x%x\n\n",
1447 (unsigned int)-ret);
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001448 goto exit;
1449 }
1450
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001451 mbedtls_printf(" ok\n");
Paul Bakker5121ce52009-01-03 21:22:43 +00001452
1453 /*
1454 * 3. Setup stuff
1455 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001456 mbedtls_printf(" . Setting up the SSL/TLS structure...");
1457 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +00001458
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001459 if ((ret = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_CLIENT,
1460 opt.transport,
1461 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1462 mbedtls_printf(
1463 " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
1464 (unsigned int)-ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001465 goto exit;
1466 }
1467
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001468# if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001469 /* The default algorithms profile disables SHA-1, but our tests still
1470 rely on it heavily. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001471 if (opt.allow_sha1 > 0) {
1472 crt_profile_for_test.allowed_mds |=
1473 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1);
1474 mbedtls_ssl_conf_cert_profile(&conf, &crt_profile_for_test);
1475 mbedtls_ssl_conf_sig_hashes(&conf, ssl_sig_hashes_for_test);
Gilles Peskinebc70a182017-05-09 15:59:24 +02001476 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001477
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001478 if (opt.context_crt_cb == 0)
1479 mbedtls_ssl_conf_verify(&conf, my_verify, NULL);
Hanno Beckerbb425db2019-04-03 12:59:58 +01001480
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001481 memset(peer_crt_info, 0, sizeof(peer_crt_info));
1482# endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001483
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001484# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1485 if (opt.cid_enabled == 1 || opt.cid_enabled_renego == 1) {
1486 if (opt.cid_enabled == 1 && opt.cid_enabled_renego == 1 &&
1487 cid_len != cid_renego_len) {
1488 mbedtls_printf("CID length must not change during renegotiation\n");
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001489 goto usage;
1490 }
1491
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001492 if (opt.cid_enabled == 1)
1493 ret = mbedtls_ssl_conf_cid(&conf, cid_len,
1494 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE);
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001495 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001496 ret = mbedtls_ssl_conf_cid(&conf, cid_renego_len,
1497 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE);
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001498
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001499 if (ret != 0) {
1500 mbedtls_printf(
1501 " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
1502 (unsigned int)-ret);
Hanno Beckerad4a1372019-05-03 13:06:44 +01001503 goto exit;
1504 }
1505 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001506# endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerad4a1372019-05-03 13:06:44 +01001507
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001508 if (opt.auth_mode != DFL_AUTH_MODE)
1509 mbedtls_ssl_conf_authmode(&conf, opt.auth_mode);
Paul Bakker5121ce52009-01-03 21:22:43 +00001510
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001511# if defined(MBEDTLS_SSL_PROTO_DTLS)
1512 if (opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX)
1513 mbedtls_ssl_conf_handshake_timeout(&conf, opt.hs_to_min, opt.hs_to_max);
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001514
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001515 if (opt.dgram_packing != DFL_DGRAM_PACKING)
1516 mbedtls_ssl_set_datagram_packing(&ssl, opt.dgram_packing);
1517# endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001518
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001519# if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1520 if ((ret = mbedtls_ssl_conf_max_frag_len(&conf, opt.mfl_code)) != 0) {
1521 mbedtls_printf(
1522 " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret);
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001523 goto exit;
1524 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001525# endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001526
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001527# if defined(MBEDTLS_SSL_DTLS_SRTP)
1528 const mbedtls_ssl_srtp_profile forced_profile[] = {
1529 opt.force_srtp_profile, MBEDTLS_TLS_SRTP_UNSET
1530 };
1531 if (opt.use_srtp == 1) {
1532 if (opt.force_srtp_profile != 0) {
1533 ret = mbedtls_ssl_conf_dtls_srtp_protection_profiles(
1534 &conf, forced_profile);
1535 } else {
1536 ret = mbedtls_ssl_conf_dtls_srtp_protection_profiles(
1537 &conf, default_profiles);
Ron Eldor6ea64512018-07-02 10:08:07 +03001538 }
1539
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001540 if (ret != 0) {
1541 mbedtls_printf(
1542 " failed\n ! "
1543 "mbedtls_ssl_conf_dtls_srtp_protection_profiles returned %d\n\n",
1544 ret);
Ron Eldor6ea64512018-07-02 10:08:07 +03001545 goto exit;
1546 }
1547
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001548 } else if (opt.force_srtp_profile != 0) {
1549 mbedtls_printf(
1550 " failed\n ! must enable use_srtp to force srtp profile\n\n");
Ron Eldor6ea64512018-07-02 10:08:07 +03001551 goto exit;
1552 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001553# endif /* MBEDTLS_SSL_DTLS_SRTP */
Ron Eldor6ea64512018-07-02 10:08:07 +03001554
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001555# if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1556 if (opt.extended_ms != DFL_EXTENDED_MS)
1557 mbedtls_ssl_conf_extended_master_secret(&conf, opt.extended_ms);
1558# endif
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001559
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001560# if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1561 if (opt.etm != DFL_ETM)
1562 mbedtls_ssl_conf_encrypt_then_mac(&conf, opt.etm);
1563# endif
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001564
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001565# if defined(MBEDTLS_DHM_C)
1566 if (opt.dhmlen != DFL_DHMLEN)
1567 mbedtls_ssl_conf_dhm_min_bitlen(&conf, opt.dhmlen);
1568# endif
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001569
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001570# if defined(MBEDTLS_SSL_ALPN)
1571 if (opt.alpn_string != NULL)
1572 if ((ret = mbedtls_ssl_conf_alpn_protocols(&conf, alpn_list)) != 0) {
1573 mbedtls_printf(
1574 " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
1575 ret);
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001576 goto exit;
1577 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001578# endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001579
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001580 if (opt.reproducible) {
1581# if defined(MBEDTLS_HAVE_TIME)
1582# if defined(MBEDTLS_PLATFORM_TIME_ALT)
1583 mbedtls_platform_set_time(dummy_constant_time);
1584# else
1585 fprintf(stderr,
1586 "Warning: reproducible option used without constant time\n");
1587# endif
1588# endif /* MBEDTLS_HAVE_TIME */
Philippe Antoine986b6f22019-06-07 15:04:32 +02001589 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001590 mbedtls_ssl_conf_rng(&conf, rng_get, &rng);
1591 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001592
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001593 mbedtls_ssl_conf_read_timeout(&conf, opt.read_timeout);
Paul Bakker5121ce52009-01-03 21:22:43 +00001594
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001595# if defined(MBEDTLS_SSL_SESSION_TICKETS)
1596 mbedtls_ssl_conf_session_tickets(&conf, opt.tickets);
1597# endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001598
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001599 if (opt.force_ciphersuite[0] != DFL_FORCE_CIPHER)
1600 mbedtls_ssl_conf_ciphersuites(&conf, opt.force_ciphersuite);
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001601
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001602 if (opt.allow_legacy != DFL_ALLOW_LEGACY)
1603 mbedtls_ssl_conf_legacy_renegotiation(&conf, opt.allow_legacy);
1604# if defined(MBEDTLS_SSL_RENEGOTIATION)
1605 mbedtls_ssl_conf_renegotiation(&conf, opt.renegotiation);
1606# endif
Paul Bakker48916f92012-09-16 19:57:18 +00001607
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001608# if defined(MBEDTLS_X509_CRT_PARSE_C)
1609 if (strcmp(opt.ca_path, "none") != 0 && strcmp(opt.ca_file, "none") != 0) {
1610# if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1611 if (opt.ca_callback != 0)
1612 mbedtls_ssl_conf_ca_cb(&conf, ca_callback, &cacert);
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001613 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001614# endif
1615 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001616 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001617 if (strcmp(opt.crt_file, "none") != 0 &&
1618 strcmp(opt.key_file, "none") != 0) {
1619 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &clicert, &pkey)) != 0) {
1620 mbedtls_printf(
1621 " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret);
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001622 goto exit;
1623 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001624 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001625# endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +02001626
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001627# if defined(MBEDTLS_ECP_C)
1628 if (opt.curves != NULL && strcmp(opt.curves, "default") != 0) {
1629 mbedtls_ssl_conf_curves(&conf, curve_list);
Hanno Beckere6706e62017-05-15 16:05:15 +01001630 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001631# endif
Hanno Beckere6706e62017-05-15 16:05:15 +01001632
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001633# if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1634# if defined(MBEDTLS_USE_PSA_CRYPTO)
1635 if (opt.psk_opaque != 0) {
Janos Follathbe4efc22019-08-08 11:38:18 +01001636 key_attributes = psa_key_attributes_init();
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001637 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
1638 psa_set_key_algorithm(&key_attributes, alg);
1639 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Hanno Beckere86964c2018-10-23 11:37:50 +01001640
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001641 status = psa_import_key(&key_attributes, psk, psk_len, &slot);
1642 if (status != PSA_SUCCESS) {
Hanno Beckere86964c2018-10-23 11:37:50 +01001643 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1644 goto exit;
1645 }
1646
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001647 if ((ret = mbedtls_ssl_conf_psk_opaque(
1648 &conf, slot, (const unsigned char *)opt.psk_identity,
1649 strlen(opt.psk_identity))) != 0) {
1650 mbedtls_printf(
1651 " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
1652 ret);
1653 goto exit;
1654 }
1655 } else
1656# endif /* MBEDTLS_USE_PSA_CRYPTO */
1657 if (psk_len > 0) {
1658 ret = mbedtls_ssl_conf_psk(&conf, psk, psk_len,
1659 (const unsigned char *)opt.psk_identity,
1660 strlen(opt.psk_identity));
1661 if (ret != 0) {
1662 mbedtls_printf(" failed\n ! mbedtls_ssl_conf_psk returned %d\n\n",
1663 ret);
Hanno Beckere86964c2018-10-23 11:37:50 +01001664 goto exit;
1665 }
1666 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001667# endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02001668
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001669 if (opt.min_version != DFL_MIN_VERSION)
1670 mbedtls_ssl_conf_min_version(&conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1671 opt.min_version);
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001672
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001673 if (opt.max_version != DFL_MAX_VERSION)
1674 mbedtls_ssl_conf_max_version(&conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1675 opt.max_version);
Paul Bakker1d29fb52012-09-28 13:28:45 +00001676
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001677 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
1678 mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
1679 (unsigned int)-ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001680 goto exit;
1681 }
1682
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001683# if defined(MBEDTLS_SSL_EXPORT_KEYS)
1684 if (opt.eap_tls != 0) {
1685 mbedtls_ssl_set_export_keys_cb(&ssl, eap_tls_key_derivation,
1686 &eap_tls_keying);
1687 } else if (opt.nss_keylog != 0) {
1688 mbedtls_ssl_set_export_keys_cb(&ssl, nss_keylog_export, NULL);
Hanno Becker7e6c1782021-06-08 09:24:55 +01001689 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001690# if defined(MBEDTLS_SSL_DTLS_SRTP)
1691 else if (opt.use_srtp != 0) {
1692 mbedtls_ssl_set_export_keys_cb(&ssl, dtls_srtp_key_derivation,
1693 &dtls_srtp_keying);
Hanno Becker7e6c1782021-06-08 09:24:55 +01001694 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001695# endif /* MBEDTLS_SSL_DTLS_SRTP */
1696# endif /* MBEDTLS_SSL_EXPORT_KEYS */
Hanno Becker7e6c1782021-06-08 09:24:55 +01001697
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001698# if defined(MBEDTLS_X509_CRT_PARSE_C)
1699 if ((ret = mbedtls_ssl_set_hostname(&ssl, opt.server_name)) != 0) {
1700 mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
1701 ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001702 goto exit;
1703 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001704# endif
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001705
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001706# if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1707 if (opt.ecjpake_pw != DFL_ECJPAKE_PW) {
1708 if ((ret = mbedtls_ssl_set_hs_ecjpake_password(
1709 &ssl, (const unsigned char *)opt.ecjpake_pw,
1710 strlen(opt.ecjpake_pw))) != 0) {
1711 mbedtls_printf(
1712 " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
1713 ret);
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001714 goto exit;
1715 }
1716 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001717# endif
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001718
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001719# if defined(MBEDTLS_X509_CRT_PARSE_C)
1720 if (opt.context_crt_cb == 1)
1721 mbedtls_ssl_set_verify(&ssl, my_verify, NULL);
1722# endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Beckerbb425db2019-04-03 12:59:58 +01001723
Hanno Becker8b1af2f2019-07-03 17:02:43 +01001724 io_ctx.ssl = &ssl;
1725 io_ctx.net = &server_fd;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001726 mbedtls_ssl_set_bio(&ssl, &io_ctx, send_cb, recv_cb,
1727 opt.nbio == 0 ? recv_timeout_cb : NULL);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001728
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001729# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1730 if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1731 if ((ret = mbedtls_ssl_set_cid(&ssl, opt.cid_enabled, cid, cid_len)) !=
1732 0) {
1733 mbedtls_printf(" failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
1734 ret);
Hanno Becker90cb3592019-04-09 17:24:19 +01001735 goto exit;
1736 }
1737 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001738# endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01001739
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001740# if defined(MBEDTLS_SSL_PROTO_DTLS)
1741 if (opt.dtls_mtu != DFL_DTLS_MTU)
1742 mbedtls_ssl_set_mtu(&ssl, opt.dtls_mtu);
1743# endif
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001744
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001745# if defined(MBEDTLS_TIMING_C)
1746 mbedtls_ssl_set_timer_cb(&ssl, &timer, mbedtls_timing_set_delay,
1747 mbedtls_timing_get_delay);
1748# endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001749
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001750# if defined(MBEDTLS_ECP_RESTARTABLE)
1751 if (opt.ec_max_ops != DFL_EC_MAX_OPS)
1752 mbedtls_ecp_set_max_ops(opt.ec_max_ops);
1753# endif
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001754
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001755# if defined(MBEDTLS_SSL_DTLS_SRTP)
1756 if (opt.use_srtp != 0 && strlen(opt.mki) != 0) {
1757 if (mbedtls_test_unhexify(mki, sizeof(mki), opt.mki, &mki_len) != 0) {
1758 mbedtls_printf("mki value not valid hex\n");
Johan Pascal9bc97ca2020-09-21 23:44:45 +02001759 goto exit;
Ron Eldor6ea64512018-07-02 10:08:07 +03001760 }
1761
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001762 mbedtls_ssl_conf_srtp_mki_value_supported(
1763 &conf, MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED);
1764 if ((ret = mbedtls_ssl_dtls_srtp_set_mki_value(
1765 &ssl, mki, (uint16_t)strlen(opt.mki) / 2)) != 0) {
1766 mbedtls_printf(
1767 " failed\n ! mbedtls_ssl_dtls_srtp_set_mki_value returned %d\n\n",
1768 ret);
Ron Eldor6ea64512018-07-02 10:08:07 +03001769 goto exit;
1770 }
1771 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001772# endif
Ron Eldor6ea64512018-07-02 10:08:07 +03001773
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001774 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001775
Paul Bakker5121ce52009-01-03 21:22:43 +00001776 /*
1777 * 4. Handshake
1778 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001779 mbedtls_printf(" . Performing the SSL/TLS handshake...");
1780 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +00001781
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001782 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
1783 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001784 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001785 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
1786 mbedtls_printf(
1787 " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
1788 (unsigned int)-ret);
1789 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED)
1790 mbedtls_printf(" Unable to verify the server's certificate. "
1791 "Either it is invalid,\n"
1792 " or you didn't set ca_file or ca_path "
1793 "to an appropriate value.\n"
1794 " Alternatively, you may want to use "
1795 "auth_mode=optional for testing purposes.\n");
1796 mbedtls_printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +00001797 goto exit;
1798 }
Hanno Becker16970d22017-10-10 15:56:37 +01001799
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001800# if defined(MBEDTLS_ECP_RESTARTABLE)
1801 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS)
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02001802 continue;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001803# endif
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001804
Hanno Becker16970d22017-10-10 15:56:37 +01001805 /* For event-driven IO, wait for socket to become available */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001806 if (opt.event == 1 /* level triggered IO */) {
1807# if defined(MBEDTLS_TIMING_C)
1808 ret = idle(&server_fd, &timer, ret);
1809# else
1810 ret = idle(&server_fd, ret);
1811# endif
1812 if (ret != 0)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001813 goto exit;
Hanno Becker16970d22017-10-10 15:56:37 +01001814 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001815 }
1816
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001817 mbedtls_printf(" ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1818 mbedtls_ssl_get_version(&ssl),
1819 mbedtls_ssl_get_ciphersuite(&ssl));
Paul Bakker5121ce52009-01-03 21:22:43 +00001820
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001821 if ((ret = mbedtls_ssl_get_record_expansion(&ssl)) >= 0)
1822 mbedtls_printf(" [ Record expansion is %d ]\n", ret);
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001823 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001824 mbedtls_printf(" [ Record expansion is unknown ]\n");
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001825
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001826# if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1827 mbedtls_printf(" [ Maximum incoming record payload length is %u ]\n",
1828 (unsigned int)mbedtls_ssl_get_max_in_record_payload(&ssl));
1829 mbedtls_printf(" [ Maximum outgoing record payload length is %u ]\n",
1830 (unsigned int)mbedtls_ssl_get_max_out_record_payload(&ssl));
1831# endif
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02001832
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001833# if defined(MBEDTLS_SSL_ALPN)
1834 if (opt.alpn_string != NULL) {
1835 const char *alp = mbedtls_ssl_get_alpn_protocol(&ssl);
1836 mbedtls_printf(" [ Application Layer Protocol is %s ]\n",
1837 alp ? alp : "(none)");
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001838 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001839# endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001840
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001841# if defined(MBEDTLS_SSL_EXPORT_KEYS)
1842 if (opt.eap_tls != 0) {
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001843 size_t j = 0;
Ron Eldor51d3ab52019-05-12 14:54:30 +03001844
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001845 if ((ret = mbedtls_ssl_tls_prf(
1846 eap_tls_keying.tls_prf_type, eap_tls_keying.master_secret,
1847 sizeof(eap_tls_keying.master_secret), eap_tls_label,
1848 eap_tls_keying.randbytes, sizeof(eap_tls_keying.randbytes),
1849 eap_tls_keymaterial, sizeof(eap_tls_keymaterial))) != 0) {
1850 mbedtls_printf(
1851 " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
1852 (unsigned int)-ret);
Ron Eldor51d3ab52019-05-12 14:54:30 +03001853 goto exit;
1854 }
1855
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001856 mbedtls_printf(" EAP-TLS key material is:");
1857 for (j = 0; j < sizeof(eap_tls_keymaterial); j++) {
1858 if (j % 8 == 0)
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001859 mbedtls_printf("\n ");
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001860 mbedtls_printf("%02x ", eap_tls_keymaterial[j]);
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001861 }
1862 mbedtls_printf("\n");
1863
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001864 if ((ret = mbedtls_ssl_tls_prf(eap_tls_keying.tls_prf_type, NULL, 0,
1865 eap_tls_label, eap_tls_keying.randbytes,
1866 sizeof(eap_tls_keying.randbytes),
1867 eap_tls_iv, sizeof(eap_tls_iv))) != 0) {
1868 mbedtls_printf(
1869 " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
1870 (unsigned int)-ret);
1871 goto exit;
1872 }
Ron Eldor51d3ab52019-05-12 14:54:30 +03001873
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001874 mbedtls_printf(" EAP-TLS IV is:");
1875 for (j = 0; j < sizeof(eap_tls_iv); j++) {
1876 if (j % 8 == 0)
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001877 mbedtls_printf("\n ");
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001878 mbedtls_printf("%02x ", eap_tls_iv[j]);
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001879 }
1880 mbedtls_printf("\n");
1881 }
Ron Eldor65b56ef2019-09-26 16:40:48 +03001882
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001883# if defined(MBEDTLS_SSL_DTLS_SRTP)
1884 else if (opt.use_srtp != 0) {
Ron Eldor65b56ef2019-09-26 16:40:48 +03001885 size_t j = 0;
Johan Pascal2258a4f2020-10-28 13:53:09 +01001886 mbedtls_dtls_srtp_info dtls_srtp_negotiation_result;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001887 mbedtls_ssl_get_dtls_srtp_negotiation_result(
1888 &ssl, &dtls_srtp_negotiation_result);
Ron Eldor65b56ef2019-09-26 16:40:48 +03001889
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001890 if (dtls_srtp_negotiation_result.chosen_dtls_srtp_profile ==
1891 MBEDTLS_TLS_SRTP_UNSET) {
1892 mbedtls_printf(" Unable to negotiate "
1893 "the use of DTLS-SRTP\n");
1894 } else {
1895 if ((ret = mbedtls_ssl_tls_prf(
1896 dtls_srtp_keying.tls_prf_type,
1897 dtls_srtp_keying.master_secret,
1898 sizeof(dtls_srtp_keying.master_secret), dtls_srtp_label,
1899 dtls_srtp_keying.randbytes,
1900 sizeof(dtls_srtp_keying.randbytes), dtls_srtp_key_material,
1901 sizeof(dtls_srtp_key_material))) != 0) {
1902 mbedtls_printf(
1903 " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
1904 (unsigned int)-ret);
Johan Pascal9bc97ca2020-09-21 23:44:45 +02001905 goto exit;
1906 }
Ron Eldor65b56ef2019-09-26 16:40:48 +03001907
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001908 mbedtls_printf(" DTLS-SRTP key material is:");
1909 for (j = 0; j < sizeof(dtls_srtp_key_material); j++) {
1910 if (j % 8 == 0)
1911 mbedtls_printf("\n ");
1912 mbedtls_printf("%02x ", dtls_srtp_key_material[j]);
Johan Pascal9bc97ca2020-09-21 23:44:45 +02001913 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001914 mbedtls_printf("\n");
Johan Pascal9bc97ca2020-09-21 23:44:45 +02001915
Johan Pascal9bc50b02020-09-24 12:01:13 +02001916 /* produce a less readable output used to perform automatic checks
1917 * - compare client and server output
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001918 * - interop test with openssl which client produces this kind of
1919 * output
Johan Pascal9bc50b02020-09-24 12:01:13 +02001920 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001921 mbedtls_printf(" Keying material: ");
1922 for (j = 0; j < sizeof(dtls_srtp_key_material); j++) {
1923 mbedtls_printf("%02X", dtls_srtp_key_material[j]);
Johan Pascal9bc50b02020-09-24 12:01:13 +02001924 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001925 mbedtls_printf("\n");
Johan Pascal20c7db32020-10-26 22:45:58 +01001926
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001927 if (dtls_srtp_negotiation_result.mki_len > 0) {
1928 mbedtls_printf(" DTLS-SRTP mki value: ");
1929 for (j = 0; j < dtls_srtp_negotiation_result.mki_len; j++) {
1930 mbedtls_printf("%02X",
1931 dtls_srtp_negotiation_result.mki_value[j]);
Johan Pascal20c7db32020-10-26 22:45:58 +01001932 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001933 } else {
1934 mbedtls_printf(" DTLS-SRTP no mki value negotiated");
Johan Pascal20c7db32020-10-26 22:45:58 +01001935 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001936 mbedtls_printf("\n");
Johan Pascal9bc97ca2020-09-21 23:44:45 +02001937 }
Ron Eldor65b56ef2019-09-26 16:40:48 +03001938 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001939# endif /* MBEDTLS_SSL_DTLS_SRTP */
1940# endif /* MBEDTLS_SSL_EXPORT_KEYS */
1941 if (opt.reconnect != 0) {
1942 mbedtls_printf(" . Saving session for reuse...");
1943 fflush(stdout);
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001944
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001945 if (opt.reco_mode == 1) {
Hanno Becker494dc712021-05-14 15:08:51 +01001946 mbedtls_ssl_session exported_session;
1947
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02001948 /* free any previously saved data */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001949 if (session_data != NULL) {
1950 mbedtls_platform_zeroize(session_data, session_data_len);
1951 mbedtls_free(session_data);
Manuel Pégourié-Gonnard4d591d62019-05-24 10:26:41 +02001952 session_data = NULL;
1953 }
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02001954
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001955 mbedtls_ssl_session_init(&exported_session);
1956 ret = mbedtls_ssl_get_session(&ssl, &exported_session);
1957 if (ret != 0) {
Hanno Becker494dc712021-05-14 15:08:51 +01001958 mbedtls_printf(
1959 "failed\n ! mbedtls_ssl_get_session() returned -%#02x\n",
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001960 (unsigned)-ret);
Hanno Becker494dc712021-05-14 15:08:51 +01001961 goto exit;
1962 }
1963
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02001964 /* get size of the buffer needed */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001965 mbedtls_ssl_session_save(&exported_session, NULL, 0,
1966 &session_data_len);
1967 session_data = mbedtls_calloc(1, session_data_len);
1968 if (session_data == NULL) {
1969 mbedtls_printf(" failed\n ! alloc %u bytes for session data\n",
1970 (unsigned)session_data_len);
1971 mbedtls_ssl_session_free(&exported_session);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02001972 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1973 goto exit;
1974 }
1975
1976 /* actually save session data */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001977 if ((ret = mbedtls_ssl_session_save(&exported_session, session_data,
1978 session_data_len,
1979 &session_data_len)) != 0) {
1980 mbedtls_printf(
1981 " failed\n ! mbedtls_ssl_session_saved returned -0x%04x\n\n",
1982 (unsigned int)-ret);
1983 mbedtls_ssl_session_free(&exported_session);
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +02001984 goto exit;
1985 }
Hanno Becker494dc712021-05-14 15:08:51 +01001986
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001987 mbedtls_ssl_session_free(&exported_session);
1988 } else {
1989 if ((ret = mbedtls_ssl_get_session(&ssl, &saved_session)) != 0) {
1990 mbedtls_printf(
1991 " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
1992 (unsigned int)-ret);
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +02001993 goto exit;
1994 }
Manuel Pégourié-Gonnard21548632019-05-16 11:39:42 +02001995 }
1996
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001997 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02001998
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001999 if (opt.reco_mode == 1) {
2000 mbedtls_printf(" [ Saved %u bytes of session data]\n",
2001 (unsigned)session_data_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002002 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002003 }
2004
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002005# if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002006 /*
2007 * 5. Verify the server certificate
2008 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002009 mbedtls_printf(" . Verifying peer X.509 certificate...");
Paul Bakker5121ce52009-01-03 21:22:43 +00002010
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002011 if ((flags = mbedtls_ssl_get_verify_result(&ssl)) != 0) {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002012 char vrfy_buf[512];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002013 mbedtls_printf(" failed\n");
Paul Bakker5121ce52009-01-03 21:22:43 +00002014
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002015 x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00002016
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002017 mbedtls_printf("%s\n", vrfy_buf);
2018 } else
2019 mbedtls_printf(" ok\n");
Paul Bakker5121ce52009-01-03 21:22:43 +00002020
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002021# if !defined(MBEDTLS_X509_REMOVE_INFO)
2022 mbedtls_printf(" . Peer certificate information ...\n");
2023 mbedtls_printf("%s\n", peer_crt_info);
2024# endif /* !MBEDTLS_X509_REMOVE_INFO */
2025# endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002026
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002027# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2028 ret = report_cid_usage(&ssl, "initial handshake");
2029 if (ret != 0)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002030 goto exit;
2031
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002032 if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2033 if ((ret = mbedtls_ssl_set_cid(&ssl, opt.cid_enabled_renego, cid_renego,
2034 cid_renego_len)) != 0) {
2035 mbedtls_printf(" failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2036 ret);
gabor-mezei-arm78595852020-04-28 10:40:30 +02002037 goto exit;
Hanno Becker90cb3592019-04-09 17:24:19 +01002038 }
2039 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002040# endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01002041
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002042# if defined(MBEDTLS_SSL_RENEGOTIATION)
2043 if (opt.renegotiate) {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002044 /*
2045 * Perform renegotiation (this must be done when the server is waiting
2046 * for input from our side).
2047 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002048 mbedtls_printf(" . Performing renegotiation...");
2049 fflush(stdout);
2050 while ((ret = mbedtls_ssl_renegotiate(&ssl)) != 0) {
2051 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002052 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002053 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
2054 mbedtls_printf(
2055 " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
2056 ret);
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002057 goto exit;
2058 }
Hanno Becker16970d22017-10-10 15:56:37 +01002059
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002060# if defined(MBEDTLS_ECP_RESTARTABLE)
2061 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS)
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002062 continue;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002063# endif
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002064
Hanno Becker16970d22017-10-10 15:56:37 +01002065 /* For event-driven IO, wait for socket to become available */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002066 if (opt.event == 1 /* level triggered IO */) {
2067# if defined(MBEDTLS_TIMING_C)
2068 idle(&server_fd, &timer, ret);
2069# else
2070 idle(&server_fd, ret);
2071# endif
Hanno Becker16970d22017-10-10 15:56:37 +01002072 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002073 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002074 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002075 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002076# endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002077
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002078# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2079 ret = report_cid_usage(&ssl, "after renegotiation");
2080 if (ret != 0)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002081 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002082# endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002083
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 /*
2085 * 6. Write the GET request
2086 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002087 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002088send_request:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002089 mbedtls_printf(" > Write to server:");
2090 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +00002091
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002092 len = mbedtls_snprintf((char *)buf, sizeof(buf) - 1, GET_REQUEST,
2093 opt.request_page);
2094 tail_len = (int)strlen(GET_REQUEST_END);
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002095
2096 /* Add padding to GET request to reach opt.request_size in length */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002097 if (opt.request_size != DFL_REQUEST_SIZE &&
2098 len + tail_len < opt.request_size) {
2099 memset(buf + len, 'A', opt.request_size - len - tail_len);
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002100 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02002101 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002102
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002103 strncpy((char *)buf + len, GET_REQUEST_END, sizeof(buf) - len - 1);
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002104 len += tail_len;
2105
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02002106 /* Truncate if request size is smaller than the "natural" size */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002107 if (opt.request_size != DFL_REQUEST_SIZE && len > opt.request_size) {
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02002108 len = opt.request_size;
2109
2110 /* Still end with \r\n unless that's really not possible */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002111 if (len >= 2)
2112 buf[len - 2] = '\r';
2113 if (len >= 1)
2114 buf[len - 1] = '\n';
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02002115 }
2116
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002117 if (opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM) {
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002118 written = 0;
2119 frags = 0;
2120
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002121 do {
2122 while ((ret = mbedtls_ssl_write(&ssl, buf + written,
2123 len - written)) < 0) {
2124 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002125 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002126 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
2127 mbedtls_printf(
2128 " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
2129 (unsigned int)-ret);
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002130 goto exit;
2131 }
Hanno Becker16970d22017-10-10 15:56:37 +01002132
2133 /* For event-driven IO, wait for socket to become available */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002134 if (opt.event == 1 /* level triggered IO */) {
2135# if defined(MBEDTLS_TIMING_C)
2136 idle(&server_fd, &timer, ret);
2137# else
2138 idle(&server_fd, ret);
2139# endif
Hanno Becker16970d22017-10-10 15:56:37 +01002140 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002141 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002142
2143 frags++;
2144 written += ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002145 } while (written < len);
2146 } else /* Not stream, so datagram */
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002147 {
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002148 while (1) {
2149 ret = mbedtls_ssl_write(&ssl, buf, len);
Hanno Becker16970d22017-10-10 15:56:37 +01002150
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002151# if defined(MBEDTLS_ECP_RESTARTABLE)
2152 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002153 continue;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002154# endif
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002155
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002156 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
2157 ret != MBEDTLS_ERR_SSL_WANT_WRITE)
Hanno Becker16970d22017-10-10 15:56:37 +01002158 break;
2159
2160 /* For event-driven IO, wait for socket to become available */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002161 if (opt.event == 1 /* level triggered IO */) {
2162# if defined(MBEDTLS_TIMING_C)
2163 idle(&server_fd, &timer, ret);
2164# else
2165 idle(&server_fd, ret);
2166# endif
Hanno Becker16970d22017-10-10 15:56:37 +01002167 }
2168 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002169
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002170 if (ret < 0) {
2171 mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n",
2172 ret);
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002173 goto exit;
2174 }
2175
2176 frags = 1;
2177 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002178
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002179 if (written < len) {
2180 mbedtls_printf(
2181 " warning\n ! request didn't fit into single datagram and "
2182 "was truncated to size %u",
2183 (unsigned)written);
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002184 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002185 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002186
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002187 buf[written] = '\0';
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002188 mbedtls_printf(" %d bytes written in %d fragments\n\n%s\n", written, frags,
2189 (char *)buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002190
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002191 /* Send a non-empty request if request_size == 0 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002192 if (len == 0) {
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002193 opt.request_size = DFL_REQUEST_SIZE;
2194 goto send_request;
2195 }
2196
Paul Bakker5121ce52009-01-03 21:22:43 +00002197 /*
2198 * 7. Read the HTTP response
2199 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002200 mbedtls_printf(" < Read from server:");
2201 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +00002202
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002203 /*
2204 * TLS and DTLS need different reading styles (stream vs datagram)
2205 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002206 if (opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM) {
2207 do {
2208 len = sizeof(buf) - 1;
2209 memset(buf, 0, sizeof(buf));
2210 ret = mbedtls_ssl_read(&ssl, buf, len);
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002211
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002212# if defined(MBEDTLS_ECP_RESTARTABLE)
2213 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS)
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002214 continue;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002215# endif
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002216
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002217 if (ret == MBEDTLS_ERR_SSL_WANT_READ ||
2218 ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
Hanno Becker16970d22017-10-10 15:56:37 +01002219 /* For event-driven IO, wait for socket to become available */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002220 if (opt.event == 1 /* level triggered IO */) {
2221# if defined(MBEDTLS_TIMING_C)
2222 idle(&server_fd, &timer, ret);
2223# else
2224 idle(&server_fd, ret);
2225# endif
Hanno Becker16970d22017-10-10 15:56:37 +01002226 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002227 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01002228 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002229
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002230 if (ret <= 0) {
2231 switch (ret) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002233 mbedtls_printf(" connection was closed gracefully\n");
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002234 ret = 0;
2235 goto close_notify;
2236
2237 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 case MBEDTLS_ERR_NET_CONN_RESET:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002239 mbedtls_printf(" connection was reset by peer\n");
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002240 ret = 0;
2241 goto reconnect;
2242
2243 default:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002244 mbedtls_printf(" mbedtls_ssl_read returned -0x%x\n",
2245 (unsigned int)-ret);
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002246 goto exit;
2247 }
2248 }
2249
2250 len = ret;
2251 buf[len] = '\0';
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002252 mbedtls_printf(" %d bytes read\n\n%s", len, (char *)buf);
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002253
2254 /* End of message should be detected according to the syntax of the
2255 * application protocol (eg HTTP), just use a dummy test here. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002256 if (ret > 0 && buf[len - 1] == '\n') {
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002257 ret = 0;
2258 break;
2259 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002260 } while (1);
2261 } else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 {
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002263 len = sizeof(buf) - 1;
2264 memset(buf, 0, sizeof(buf));
Paul Bakker5121ce52009-01-03 21:22:43 +00002265
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002266 while (1) {
2267 ret = mbedtls_ssl_read(&ssl, buf, len);
Hanno Becker16970d22017-10-10 15:56:37 +01002268
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002269# if defined(MBEDTLS_ECP_RESTARTABLE)
2270 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002271 continue;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002272# endif
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002273
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002274 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
2275 ret != MBEDTLS_ERR_SSL_WANT_WRITE)
Hanno Becker16970d22017-10-10 15:56:37 +01002276 break;
2277
2278 /* For event-driven IO, wait for socket to become available */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002279 if (opt.event == 1 /* level triggered IO */) {
2280# if defined(MBEDTLS_TIMING_C)
2281 idle(&server_fd, &timer, ret);
2282# else
2283 idle(&server_fd, ret);
2284# endif
Hanno Becker16970d22017-10-10 15:56:37 +01002285 }
2286 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002287
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002288 if (ret <= 0) {
2289 switch (ret) {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002290 case MBEDTLS_ERR_SSL_TIMEOUT:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002291 mbedtls_printf(" timeout\n");
2292 if (retry_left-- > 0)
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02002293 goto send_request;
2294 goto exit;
2295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002297 mbedtls_printf(" connection was closed gracefully\n");
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002298 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002299 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00002300
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002301 default:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002302 mbedtls_printf(" mbedtls_ssl_read returned -0x%x\n",
2303 (unsigned int)-ret);
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002304 goto exit;
2305 }
Paul Bakker5690efc2011-05-26 13:16:06 +00002306 }
2307
Paul Bakker5121ce52009-01-03 21:22:43 +00002308 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02002309 buf[len] = '\0';
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002310 mbedtls_printf(" %d bytes read\n\n%s", len, (char *)buf);
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002311 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002312 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002313
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002314 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002315 * 7b. Simulate hard reset and reconnect from same port?
2316 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002317 if (opt.reconnect_hard != 0) {
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002318 opt.reconnect_hard = 0;
2319
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002320 mbedtls_printf(" . Restarting connection from same port...");
2321 fflush(stdout);
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002322
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002323# if defined(MBEDTLS_X509_CRT_PARSE_C)
2324 memset(peer_crt_info, 0, sizeof(peer_crt_info));
2325# endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Becker23699ef2019-02-26 12:36:53 +00002326
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002327 if ((ret = mbedtls_ssl_session_reset(&ssl)) != 0) {
2328 mbedtls_printf(
2329 " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2330 (unsigned int)-ret);
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002331 goto exit;
2332 }
2333
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002334 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
2335 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002336 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002337 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
2338 mbedtls_printf(
2339 " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2340 (unsigned int)-ret);
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002341 goto exit;
2342 }
Hanno Becker16970d22017-10-10 15:56:37 +01002343
2344 /* For event-driven IO, wait for socket to become available */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002345 if (opt.event == 1 /* level triggered IO */) {
2346# if defined(MBEDTLS_TIMING_C)
2347 idle(&server_fd, &timer, ret);
2348# else
2349 idle(&server_fd, ret);
2350# endif
Hanno Becker16970d22017-10-10 15:56:37 +01002351 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002352 }
2353
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002354 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002355
2356 goto send_request;
2357 }
2358
2359 /*
Jarno Lamsa77e66652019-05-29 15:15:08 +03002360 * 7c. Simulate serialize/deserialize and go back to data exchange
2361 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002362# if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2363 if (opt.serialize != 0) {
Jarno Lamsa15b3a7a2019-06-06 15:10:07 +03002364 size_t buf_len;
Jarno Lamsa77e66652019-05-29 15:15:08 +03002365
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002366 mbedtls_printf(" . Serializing live connection...");
Jarno Lamsa77e66652019-05-29 15:15:08 +03002367
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002368 ret = mbedtls_ssl_context_save(&ssl, NULL, 0, &buf_len);
2369 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
2370 mbedtls_printf(" failed\n ! mbedtls_ssl_context_save returned "
2371 "-0x%x\n\n",
2372 (unsigned int)-ret);
Jarno Lamsa77e66652019-05-29 15:15:08 +03002373
2374 goto exit;
2375 }
2376
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002377 if ((context_buf = mbedtls_calloc(1, buf_len)) == NULL) {
2378 mbedtls_printf(" failed\n ! Couldn't allocate buffer for "
2379 "serialized context");
Jarno Lamsa77e66652019-05-29 15:15:08 +03002380
2381 goto exit;
2382 }
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02002383 context_buf_len = buf_len;
Jarno Lamsa77e66652019-05-29 15:15:08 +03002384
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002385 if ((ret = mbedtls_ssl_context_save(&ssl, context_buf, buf_len,
2386 &buf_len)) != 0) {
2387 mbedtls_printf(" failed\n ! mbedtls_ssl_context_save returned "
2388 "-0x%x\n\n",
2389 (unsigned int)-ret);
Jarno Lamsa77e66652019-05-29 15:15:08 +03002390
2391 goto exit;
2392 }
2393
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002394 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02002395
Piotr Nowicki3de298f2020-04-16 14:35:19 +02002396 /* Save serialized context to the 'opt.context_file' as a base64 code */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002397 if (0 < strlen(opt.context_file)) {
Piotr Nowicki3de298f2020-04-16 14:35:19 +02002398 FILE *b64_file;
2399 uint8_t *b64_buf;
2400 size_t b64_len;
2401
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002402 mbedtls_printf(" . Save serialized context to a file... ");
Piotr Nowicki3de298f2020-04-16 14:35:19 +02002403
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002404 mbedtls_base64_encode(NULL, 0, &b64_len, context_buf, buf_len);
Piotr Nowicki3de298f2020-04-16 14:35:19 +02002405
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002406 if ((b64_buf = mbedtls_calloc(1, b64_len)) == NULL) {
2407 mbedtls_printf("failed\n ! Couldn't allocate buffer for "
2408 "the base64 code\n");
Piotr Nowicki3de298f2020-04-16 14:35:19 +02002409 goto exit;
2410 }
2411
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002412 if ((ret = mbedtls_base64_encode(b64_buf, b64_len, &b64_len,
2413 context_buf, buf_len)) != 0) {
2414 mbedtls_printf("failed\n ! mbedtls_base64_encode returned "
2415 "-0x%x\n",
2416 (unsigned int)-ret);
2417 mbedtls_free(b64_buf);
Piotr Nowicki3de298f2020-04-16 14:35:19 +02002418 goto exit;
2419 }
2420
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002421 if ((b64_file = fopen(opt.context_file, "w")) == NULL) {
2422 mbedtls_printf("failed\n ! Cannot open '%s' for writing.\n",
2423 opt.context_file);
2424 mbedtls_free(b64_buf);
Piotr Nowicki3de298f2020-04-16 14:35:19 +02002425 goto exit;
2426 }
2427
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002428 if (b64_len != fwrite(b64_buf, 1, b64_len, b64_file)) {
2429 mbedtls_printf("failed\n ! fwrite(%ld bytes) failed\n",
2430 (long)b64_len);
2431 mbedtls_free(b64_buf);
2432 fclose(b64_file);
Piotr Nowicki3de298f2020-04-16 14:35:19 +02002433 goto exit;
2434 }
2435
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002436 mbedtls_free(b64_buf);
2437 fclose(b64_file);
Piotr Nowicki3de298f2020-04-16 14:35:19 +02002438
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002439 mbedtls_printf("ok\n");
Piotr Nowicki3de298f2020-04-16 14:35:19 +02002440 }
2441
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002442 if (opt.serialize == 1) {
Manuel Pégourié-Gonnard9df5a822019-07-23 14:51:09 +02002443 /* nothing to do here, done by context_save() already */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002444 mbedtls_printf(" . Context has been reset... ok\n");
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02002445 }
2446
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002447 if (opt.serialize == 2) {
2448 mbedtls_printf(" . Freeing and reinitializing context...");
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02002449
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002450 mbedtls_ssl_free(&ssl);
Jarno Lamsa304d61c2019-06-06 10:40:52 +03002451
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002452 mbedtls_ssl_init(&ssl);
Jarno Lamsa304d61c2019-06-06 10:40:52 +03002453
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002454 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
2455 mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned "
2456 "-0x%x\n\n",
2457 (unsigned int)-ret);
Jarno Lamsa304d61c2019-06-06 10:40:52 +03002458 goto exit;
2459 }
2460
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002461 if (opt.nbio == 2)
2462 mbedtls_ssl_set_bio(&ssl, &server_fd, delayed_send,
2463 delayed_recv, NULL);
Jarno Lamsa304d61c2019-06-06 10:40:52 +03002464 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002465 mbedtls_ssl_set_bio(
2466 &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv,
2467 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL);
Jarno Lamsa304d61c2019-06-06 10:40:52 +03002468
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002469# if defined(MBEDTLS_TIMING_C)
2470 mbedtls_ssl_set_timer_cb(&ssl, &timer, mbedtls_timing_set_delay,
2471 mbedtls_timing_get_delay);
2472# endif /* MBEDTLS_TIMING_C */
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02002473
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002474 mbedtls_printf(" ok\n");
Jarno Lamsa304d61c2019-06-06 10:40:52 +03002475 }
2476
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002477 mbedtls_printf(" . Deserializing connection...");
Jarno Lamsa77e66652019-05-29 15:15:08 +03002478
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002479 if ((ret = mbedtls_ssl_context_load(&ssl, context_buf, buf_len)) != 0) {
2480 mbedtls_printf("failed\n ! mbedtls_ssl_context_load returned "
2481 "-0x%x\n\n",
2482 (unsigned int)-ret);
Jarno Lamsa77e66652019-05-29 15:15:08 +03002483
2484 goto exit;
2485 }
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02002486
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002487 mbedtls_free(context_buf);
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02002488 context_buf = NULL;
2489 context_buf_len = 0;
2490
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002491 mbedtls_printf(" ok\n");
Jarno Lamsa77e66652019-05-29 15:15:08 +03002492 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002493# endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jarno Lamsa77e66652019-05-29 15:15:08 +03002494
2495 /*
2496 * 7d. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002497 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002498 if (--opt.exchanges > 0)
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002499 goto send_request;
2500
2501 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002502 * 8. Done, cleanly close the connection
2503 */
2504close_notify:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002505 mbedtls_printf(" . Closing the connection...");
2506 fflush(stdout);
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002507
Manuel Pégourié-Gonnard56941fe2020-02-17 11:04:33 +01002508 /*
2509 * Most of the time sending a close_notify before closing is the right
2510 * thing to do. However, when the server already knows how many messages
2511 * are expected and closes the connection by itself, this alert becomes
2512 * redundant. Sometimes with DTLS this redundancy becomes a problem by
2513 * leading to a race condition where the server might close the connection
2514 * before seeing the alert, and since UDP is connection-less when the
2515 * alert arrives it will be seen as a new connection, which will fail as
2516 * the alert is clearly not a valid ClientHello. This may cause spurious
2517 * failures in tests that use DTLS and resumption with ssl_server2 in
2518 * ssl-opt.sh, avoided by enabling skip_close_notify client-side.
2519 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002520 if (opt.skip_close_notify == 0) {
Manuel Pégourié-Gonnard56941fe2020-02-17 11:04:33 +01002521 /* No error checking, the connection might be closed already */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002522 do
2523 ret = mbedtls_ssl_close_notify(&ssl);
2524 while (ret == MBEDTLS_ERR_SSL_WANT_WRITE);
Manuel Pégourié-Gonnard56941fe2020-02-17 11:04:33 +01002525 ret = 0;
2526 }
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002527
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002528 mbedtls_printf(" done\n");
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002529
2530 /*
2531 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002532 */
2533reconnect:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002534 if (opt.reconnect != 0) {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02002535 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002536
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002537 mbedtls_net_free(&server_fd);
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002538
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002539# if defined(MBEDTLS_TIMING_C)
2540 if (opt.reco_delay > 0)
2541 mbedtls_net_usleep(1000000 * opt.reco_delay);
2542# endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002543
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002544 mbedtls_printf(" . Reconnecting with saved session...");
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002545
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002546# if defined(MBEDTLS_X509_CRT_PARSE_C)
2547 memset(peer_crt_info, 0, sizeof(peer_crt_info));
2548# endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Beckera1051b42019-02-26 11:38:29 +00002549
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002550 if ((ret = mbedtls_ssl_session_reset(&ssl)) != 0) {
2551 mbedtls_printf(
2552 " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2553 (unsigned int)-ret);
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002554 goto exit;
2555 }
2556
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002557 if (opt.reco_mode == 1) {
2558 if ((ret = mbedtls_ssl_session_load(&saved_session, session_data,
2559 session_data_len)) != 0) {
2560 mbedtls_printf(
2561 " failed\n ! mbedtls_ssl_session_load returned -0x%x\n\n",
2562 (unsigned int)-ret);
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +02002563 goto exit;
2564 }
Manuel Pégourié-Gonnard21548632019-05-16 11:39:42 +02002565 }
2566
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002567 if ((ret = mbedtls_ssl_set_session(&ssl, &saved_session)) != 0) {
2568 mbedtls_printf(
2569 " failed\n ! mbedtls_ssl_set_session returned -0x%x\n\n",
2570 (unsigned int)-ret);
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002571 goto exit;
2572 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002573
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002574 if ((ret = mbedtls_net_connect(
2575 &server_fd, opt.server_addr, opt.server_port,
2576 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2577 MBEDTLS_NET_PROTO_TCP :
2578 MBEDTLS_NET_PROTO_UDP)) != 0) {
2579 mbedtls_printf(
2580 " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
2581 (unsigned int)-ret);
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002582 goto exit;
2583 }
2584
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002585 if (opt.nbio > 0)
2586 ret = mbedtls_net_set_nonblock(&server_fd);
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002587 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002588 ret = mbedtls_net_set_block(&server_fd);
2589 if (ret != 0) {
2590 mbedtls_printf(
2591 " failed\n ! net_set_(non)block() returned -0x%x\n\n",
2592 (unsigned int)-ret);
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002593 goto exit;
2594 }
2595
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002596 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
2597 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002598 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002599 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
2600 mbedtls_printf(
2601 " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2602 (unsigned int)-ret);
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002603 goto exit;
2604 }
2605 }
2606
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002607 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002608
2609 goto send_request;
2610 }
2611
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002612 /*
2613 * Cleanup and exit
2614 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002615exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002616# ifdef MBEDTLS_ERROR_C
2617 if (ret != 0) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002618 char error_buf[100];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002619 mbedtls_strerror(ret, error_buf, 100);
2620 mbedtls_printf("Last error was: -0x%X - %s\n\n", (unsigned int)-ret,
2621 error_buf);
Paul Bakkera3d195c2011-11-27 21:07:34 +00002622 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002623# endif
Paul Bakkera3d195c2011-11-27 21:07:34 +00002624
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002625 mbedtls_net_free(&server_fd);
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002626
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002627# if defined(MBEDTLS_X509_CRT_PARSE_C)
2628 mbedtls_x509_crt_free(&clicert);
2629 mbedtls_x509_crt_free(&cacert);
2630 mbedtls_pk_free(&pkey);
2631# if defined(MBEDTLS_USE_PSA_CRYPTO)
2632 psa_destroy_key(key_slot);
2633# endif
2634# endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002635
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002636# if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) && \
2637 defined(MBEDTLS_USE_PSA_CRYPTO)
2638 if (opt.psk_opaque != 0) {
Hanno Becker3f24ea92018-11-05 13:25:17 +00002639 /* This is ok even if the slot hasn't been
2640 * initialized (we might have jumed here
2641 * immediately because of bad cmd line params,
2642 * for example). */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002643 status = psa_destroy_key(slot);
2644 if ((status != PSA_SUCCESS) &&
2645 (opt.query_config_mode == DFL_QUERY_CONFIG_MODE)) {
2646 mbedtls_printf("Failed to destroy key slot %u - error was %d",
2647 (unsigned)slot, (int)status);
2648 if (ret == 0)
Hanno Becker3f24ea92018-11-05 13:25:17 +00002649 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2650 }
2651 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002652# endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED && \
2653 MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker3f24ea92018-11-05 13:25:17 +00002654
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002655 mbedtls_ssl_session_free(&saved_session);
2656 mbedtls_ssl_free(&ssl);
2657 mbedtls_ssl_config_free(&conf);
2658 rng_free(&rng);
2659 if (session_data != NULL)
2660 mbedtls_platform_zeroize(session_data, session_data_len);
2661 mbedtls_free(session_data);
2662# if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2663 if (context_buf != NULL)
2664 mbedtls_platform_zeroize(context_buf, context_buf_len);
2665 mbedtls_free(context_buf);
2666# endif
Gilles Peskineb8c48402021-02-08 20:28:22 +01002667
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002668# if defined(MBEDTLS_USE_PSA_CRYPTO)
2669 mbedtls_psa_crypto_free();
2670# endif
Gilles Peskine414e7172021-02-03 00:04:08 +01002671
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002672# if defined(MBEDTLS_TEST_HOOKS)
2673 if (test_hooks_failure_detected()) {
2674 if (ret == 0)
Gilles Peskine53dea742021-02-02 22:55:06 +01002675 ret = 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002676 mbedtls_printf("Test hooks detected errors.\n");
Gilles Peskine53dea742021-02-02 22:55:06 +01002677 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002678 test_hooks_free();
2679# endif /* MBEDTLS_TEST_HOOKS */
Gilles Peskine53dea742021-02-02 22:55:06 +01002680
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002681# if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
2682# if defined(MBEDTLS_MEMORY_DEBUG)
Piotr Nowicki7d01ef62019-11-20 15:00:17 +01002683 mbedtls_memory_buffer_alloc_status();
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002684# endif
Piotr Nowicki7d01ef62019-11-20 15:00:17 +01002685 mbedtls_memory_buffer_alloc_free();
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002686# endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
Piotr Nowicki7d01ef62019-11-20 15:00:17 +01002687
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002688# if defined(_WIN32)
2689 if (opt.query_config_mode == DFL_QUERY_CONFIG_MODE) {
2690 mbedtls_printf(" + Press Enter to exit this program.\n");
2691 fflush(stdout);
2692 getchar();
gabor-mezei-arma9eecf12020-07-07 11:11:02 +02002693 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002694# endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002695
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002696 // Shell can not handle large exit numbers -> 1 for errors
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002697 if (ret < 0)
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002698 ret = 1;
2699
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002700 if (opt.query_config_mode == DFL_QUERY_CONFIG_MODE)
2701 mbedtls_exit(ret);
gabor-mezei-arma9eecf12020-07-07 11:11:02 +02002702 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002703 mbedtls_exit(query_config_ret);
Paul Bakker5121ce52009-01-03 21:22:43 +00002704}
Gilles Peskineb4df7542021-01-08 21:20:09 +01002705#endif /* !MBEDTLS_SSL_TEST_IMPOSSIBLE && MBEDTLS_SSL_CLI_C */