1 /*
2 * SSL client with certificate authentication
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8 #define MBEDTLS_ALLOW_PRIVATE_ACCESS
9
10 #include "ssl_test_lib.h"
11
12 #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
13 #include "test/psa_crypto_helpers.h"
14 #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
15
16 #if defined(MBEDTLS_SSL_TEST_IMPOSSIBLE)
main(void)17 int main(void)
18 {
19 mbedtls_printf(MBEDTLS_SSL_TEST_IMPOSSIBLE);
20 mbedtls_exit(0);
21 }
22 #elif !defined(MBEDTLS_SSL_CLI_C)
main(void)23 int main(void)
24 {
25 mbedtls_printf("MBEDTLS_SSL_CLI_C not defined.\n");
26 mbedtls_exit(0);
27 }
28 #else /* !MBEDTLS_SSL_TEST_IMPOSSIBLE && MBEDTLS_SSL_CLI_C */
29
30 /* Size of memory to be allocated for the heap, when using the library's memory
31 * management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */
32 #define MEMORY_HEAP_SIZE 120000
33
34 #define MAX_REQUEST_SIZE 20000
35 #define MAX_REQUEST_SIZE_STR "20000"
36
37 #define DFL_SERVER_NAME "localhost"
38 #define DFL_SERVER_ADDR NULL
39 #define DFL_SERVER_PORT "4433"
40 #define DFL_REQUEST_PAGE "/"
41 #define DFL_REQUEST_SIZE -1
42 #define DFL_DEBUG_LEVEL 0
43 #define DFL_CONTEXT_CRT_CB 0
44 #define DFL_NBIO 0
45 #define DFL_EVENT 0
46 #define DFL_READ_TIMEOUT 0
47 #define DFL_MAX_RESEND 0
48 #define DFL_CA_FILE ""
49 #define DFL_CA_PATH ""
50 #define DFL_CRT_FILE ""
51 #define DFL_KEY_FILE ""
52 #define DFL_KEY_OPAQUE 0
53 #define DFL_KEY_PWD ""
54 #define DFL_PSK ""
55 #define DFL_EARLY_DATA -1
56 #define DFL_PSK_OPAQUE 0
57 #define DFL_PSK_IDENTITY "Client_identity"
58 #define DFL_ECJPAKE_PW NULL
59 #define DFL_ECJPAKE_PW_OPAQUE 0
60 #define DFL_EC_MAX_OPS -1
61 #define DFL_FORCE_CIPHER 0
62 #define DFL_TLS1_3_KEX_MODES MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL
63 #define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
64 #define DFL_ALLOW_LEGACY -2
65 #define DFL_RENEGOTIATE 0
66 #define DFL_EXCHANGES 1
67 #define DFL_MIN_VERSION -1
68 #define DFL_MAX_VERSION -1
69 #define DFL_SHA1 -1
70 #define DFL_AUTH_MODE -1
71 #define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
72 #define DFL_TRUNC_HMAC -1
73 #define DFL_RECSPLIT -1
74 #define DFL_DHMLEN -1
75 #define DFL_RECONNECT 0
76 #define DFL_RECO_SERVER_NAME NULL
77 #define DFL_RECO_DELAY 0
78 #define DFL_RECO_MODE 1
79 #define DFL_CID_ENABLED 0
80 #define DFL_CID_VALUE ""
81 #define DFL_CID_ENABLED_RENEGO -1
82 #define DFL_CID_VALUE_RENEGO NULL
83 #define DFL_RECONNECT_HARD 0
84 #define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
85 #define DFL_NEW_SESSION_TICKETS MBEDTLS_SSL_TLS1_3_SIGNAL_NEW_SESSION_TICKETS_ENABLED
86 #define DFL_ALPN_STRING NULL
87 #define DFL_GROUPS NULL
88 #define DFL_SIG_ALGS NULL
89 #define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
90 #define DFL_HS_TO_MIN 0
91 #define DFL_HS_TO_MAX 0
92 #define DFL_DTLS_MTU -1
93 #define DFL_DGRAM_PACKING 1
94 #define DFL_FALLBACK -1
95 #define DFL_EXTENDED_MS -1
96 #define DFL_ETM -1
97 #define DFL_SERIALIZE 0
98 #define DFL_CONTEXT_FILE ""
99 #define DFL_EXTENDED_MS_ENFORCE -1
100 #define DFL_CA_CALLBACK 0
101 #define DFL_EAP_TLS 0
102 #define DFL_REPRODUCIBLE 0
103 #define DFL_NSS_KEYLOG 0
104 #define DFL_NSS_KEYLOG_FILE NULL
105 #define DFL_SKIP_CLOSE_NOTIFY 0
106 #define DFL_QUERY_CONFIG_MODE 0
107 #define DFL_USE_SRTP 0
108 #define DFL_SRTP_FORCE_PROFILE 0
109 #define DFL_SRTP_MKI ""
110 #define DFL_KEY_OPAQUE_ALG "none"
111
112 #define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
113 #define GET_REQUEST_END "\r\n\r\n"
114
115 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
116 #define USAGE_CONTEXT_CRT_CB \
117 " context_crt_cb=%%d This determines whether the CRT verification callback is bound\n" \
118 " to the SSL configuration of the SSL context.\n" \
119 " Possible values:\n" \
120 " - 0 (default): Use CRT callback bound to configuration\n" \
121 " - 1: Use CRT callback bound to SSL context\n"
122 #else
123 #define USAGE_CONTEXT_CRT_CB ""
124 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
125 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
126 #if defined(MBEDTLS_FS_IO)
127 #define USAGE_IO \
128 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
129 " default: \"\" (pre-loaded)\n" \
130 " use \"none\" to skip loading any top-level CAs.\n" \
131 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
132 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
133 " use \"none\" to skip loading any top-level CAs.\n" \
134 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
135 " default: \"\" (pre-loaded)\n" \
136 " key_file=%%s default: \"\" (pre-loaded)\n" \
137 " key_pwd=%%s Password for key specified by key_file argument\n" \
138 " default: none\n"
139 #else
140 #define USAGE_IO \
141 " No file operations available (MBEDTLS_FS_IO not defined)\n"
142 #endif /* MBEDTLS_FS_IO */
143 #else /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
144 #define USAGE_IO ""
145 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
146 #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
147 #define USAGE_KEY_OPAQUE \
148 " key_opaque=%%d Handle your private key as if it were opaque\n" \
149 " default: 0 (disabled)\n"
150 #else
151 #define USAGE_KEY_OPAQUE ""
152 #endif
153
154 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
155 #define USAGE_CID \
156 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
157 " default: 0 (disabled)\n" \
158 " cid_renego=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension during renegotiation.\n" \
159 " default: same as 'cid' parameter\n" \
160 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
161 " default: \"\"\n" \
162 " cid_val_renego=%%s The CID to use for incoming messages (in hex, without 0x) after renegotiation.\n" \
163 " default: same as 'cid_val' parameter\n"
164 #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
165 #define USAGE_CID ""
166 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
167
168 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
169 #define USAGE_PSK_RAW \
170 " psk=%%s default: \"\" (disabled)\n" \
171 " The PSK values are in hex, without 0x.\n" \
172 " psk_identity=%%s default: \"Client_identity\"\n"
173 #if defined(MBEDTLS_USE_PSA_CRYPTO)
174 #define USAGE_PSK_SLOT \
175 " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
176 " Enable this to store the PSK configured through command line\n" \
177 " parameter `psk` in a PSA-based key slot.\n" \
178 " Note: Currently only supported in conjunction with\n" \
179 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
180 " to force a particular PSK-only ciphersuite.\n" \
181 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
182 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
183 " with prepopulated key slots instead of importing raw key material.\n"
184 #else
185 #define USAGE_PSK_SLOT ""
186 #endif /* MBEDTLS_USE_PSA_CRYPTO */
187 #define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
188 #else
189 #define USAGE_PSK ""
190 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
191
192 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
193 #define USAGE_CA_CALLBACK \
194 " ca_callback=%%d default: 0 (disabled)\n" \
195 " Enable this to use the trusted certificate callback function\n"
196 #else
197 #define USAGE_CA_CALLBACK ""
198 #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
199
200 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
201 #define USAGE_TICKETS \
202 " tickets=%%d default: 1 (enabled)\n" \
203 " new_session_tickets=%%d default: 1 (enabled)\n"
204 #else
205 #define USAGE_TICKETS ""
206 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
207
208 #define USAGE_EAP_TLS \
209 " eap_tls=%%d default: 0 (disabled)\n"
210 #define USAGE_NSS_KEYLOG \
211 " nss_keylog=%%d default: 0 (disabled)\n" \
212 " This cannot be used with eap_tls=1\n"
213 #define USAGE_NSS_KEYLOG_FILE \
214 " nss_keylog_file=%%s\n"
215 #if defined(MBEDTLS_SSL_DTLS_SRTP)
216 #define USAGE_SRTP \
217 " use_srtp=%%d default: 0 (disabled)\n" \
218 " This cannot be used with eap_tls=1 or " \
219 " nss_keylog=1\n" \
220 " srtp_force_profile=%%d default: 0 (all enabled)\n" \
221 " available profiles:\n" \
222 " 1 - SRTP_AES128_CM_HMAC_SHA1_80\n" \
223 " 2 - SRTP_AES128_CM_HMAC_SHA1_32\n" \
224 " 3 - SRTP_NULL_HMAC_SHA1_80\n" \
225 " 4 - SRTP_NULL_HMAC_SHA1_32\n" \
226 " mki=%%s default: \"\" (in hex, without 0x)\n"
227 #else /* MBEDTLS_SSL_DTLS_SRTP */
228 #define USAGE_SRTP ""
229 #endif
230
231 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
232 #define USAGE_MAX_FRAG_LEN \
233 " max_frag_len=%%d default: 16384 (tls default)\n" \
234 " options: 512, 1024, 2048, 4096\n"
235 #else
236 #define USAGE_MAX_FRAG_LEN ""
237 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
238
239 #if defined(MBEDTLS_DHM_C)
240 #define USAGE_DHMLEN \
241 " dhmlen=%%d default: (library default: 1024 bits)\n"
242 #else
243 #define USAGE_DHMLEN
244 #endif
245
246 #if defined(MBEDTLS_SSL_ALPN)
247 #define USAGE_ALPN \
248 " alpn=%%s default: \"\" (disabled)\n" \
249 " example: spdy/1,http/1.1\n"
250 #else
251 #define USAGE_ALPN ""
252 #endif /* MBEDTLS_SSL_ALPN */
253
254 #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) || \
255 (defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \
256 defined(PSA_WANT_ALG_FFDH))
257 #define USAGE_GROUPS \
258 " groups=a,b,c,d default: \"default\" (library default)\n" \
259 " example: \"secp521r1,brainpoolP512r1\"\n" \
260 " - use \"none\" for empty list\n" \
261 " - see mbedtls_ecp_curve_list()\n" \
262 " for acceptable EC group names\n" \
263 " - the following ffdh groups are supported:\n" \
264 " ffdhe2048, ffdhe3072, ffdhe4096, ffdhe6144,\n" \
265 " ffdhe8192\n"
266 #else
267 #define USAGE_GROUPS ""
268 #endif
269
270 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
271 #define USAGE_SIG_ALGS \
272 " sig_algs=a,b,c,d default: \"default\" (library default)\n" \
273 " example: \"ecdsa_secp256r1_sha256,ecdsa_secp384r1_sha384\"\n"
274 #else
275 #define USAGE_SIG_ALGS ""
276 #endif
277
278 #if defined(MBEDTLS_SSL_PROTO_DTLS)
279 #define USAGE_DTLS \
280 " dtls=%%d default: 0 (TLS)\n" \
281 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
282 " range of DTLS handshake timeouts in millisecs\n" \
283 " mtu=%%d default: (library default: unlimited)\n" \
284 " dgram_packing=%%d default: 1 (allowed)\n" \
285 " allow or forbid packing of multiple\n" \
286 " records within a single datgram.\n"
287 #else
288 #define USAGE_DTLS ""
289 #endif
290
291 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
292 #define USAGE_EMS \
293 " extended_ms=0/1 default: (library default: on)\n"
294 #else
295 #define USAGE_EMS ""
296 #endif
297
298 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
299 #define USAGE_ETM \
300 " etm=0/1 default: (library default: on)\n"
301 #else
302 #define USAGE_ETM ""
303 #endif
304
305 #define USAGE_REPRODUCIBLE \
306 " reproducible=0/1 default: 0 (disabled)\n"
307
308 #if defined(MBEDTLS_SSL_RENEGOTIATION)
309 #define USAGE_RENEGO \
310 " renegotiation=%%d default: 0 (disabled)\n" \
311 " renegotiate=%%d default: 0 (disabled)\n"
312 #else
313 #define USAGE_RENEGO ""
314 #endif
315
316 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
317 #if defined(MBEDTLS_USE_PSA_CRYPTO)
318 #define USAGE_ECJPAKE \
319 " ecjpake_pw=%%s default: none (disabled)\n" \
320 " ecjpake_pw_opaque=%%d default: 0 (disabled)\n"
321 #else /* MBEDTLS_USE_PSA_CRYPTO */
322 #define USAGE_ECJPAKE \
323 " ecjpake_pw=%%s default: none (disabled)\n"
324 #endif /* MBEDTLS_USE_PSA_CRYPTO */
325 #else /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
326 #define USAGE_ECJPAKE ""
327 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
328
329 #if defined(MBEDTLS_ECP_RESTARTABLE)
330 #define USAGE_ECRESTART \
331 " ec_max_ops=%%s default: library default (restart disabled)\n"
332 #else
333 #define USAGE_ECRESTART ""
334 #endif
335
336 #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
337 #define USAGE_SERIALIZATION \
338 " serialize=%%d default: 0 (do not serialize/deserialize)\n" \
339 " options: 1 (serialize)\n" \
340 " 2 (serialize with re-initialization)\n" \
341 " context_file=%%s The file path to write a serialized connection\n" \
342 " in the form of base64 code (serialize option\n" \
343 " must be set)\n" \
344 " default: \"\" (do nothing)\n" \
345 " option: a file path\n"
346 #else
347 #define USAGE_SERIALIZATION ""
348 #endif
349
350 #if defined(MBEDTLS_SSL_EARLY_DATA)
351 #define USAGE_EARLY_DATA \
352 " early_data=%%d default: library default\n" \
353 " options: 0 (disabled), 1 (enabled)\n"
354 #else
355 #define USAGE_EARLY_DATA ""
356 #endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_PROTO_TLS1_3 */
357
358 #define USAGE_KEY_OPAQUE_ALGS \
359 " key_opaque_algs=%%s Allowed opaque key algorithms.\n" \
360 " comma-separated pair of values among the following:\n" \
361 " rsa-sign-pkcs1, rsa-sign-pss, rsa-sign-pss-sha256,\n" \
362 " rsa-sign-pss-sha384, rsa-sign-pss-sha512, rsa-decrypt,\n" \
363 " ecdsa-sign, ecdh, none (only acceptable for\n" \
364 " the second value).\n" \
365
366 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
367 #define USAGE_TLS1_3_KEY_EXCHANGE_MODES \
368 " tls13_kex_modes=%%s default: all\n" \
369 " options: psk, psk_ephemeral, psk_all, ephemeral,\n" \
370 " ephemeral_all, all, psk_or_ephemeral\n"
371 #else
372 #define USAGE_TLS1_3_KEY_EXCHANGE_MODES ""
373 #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
374
375 /* USAGE is arbitrarily split to stay under the portable string literal
376 * length limit: 4095 bytes in C99. */
377 #define USAGE1 \
378 "\n usage: ssl_client2 param=<>...\n" \
379 "\n acceptable parameters:\n" \
380 " server_name=%%s default: localhost\n" \
381 " server_addr=%%s default: given by name\n" \
382 " server_port=%%d default: 4433\n" \
383 " request_page=%%s default: \".\"\n" \
384 " request_size=%%d default: about 34 (basic request)\n" \
385 " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
386 " If 0, in the first exchange only an empty\n" \
387 " application data message is sent followed by\n" \
388 " a second non-empty message before attempting\n" \
389 " to read a response from the server\n" \
390 " debug_level=%%d default: 0 (disabled)\n" \
391 " build_version=%%d default: none (disabled)\n" \
392 " option: 1 (print build version only and stop)\n" \
393 " nbio=%%d default: 0 (blocking I/O)\n" \
394 " options: 1 (non-blocking), 2 (added delays)\n" \
395 " event=%%d default: 0 (loop)\n" \
396 " options: 1 (level-triggered, implies nbio=1),\n" \
397 " read_timeout=%%d default: 0 ms (no timeout)\n" \
398 " max_resend=%%d default: 0 (no resend on timeout)\n" \
399 " skip_close_notify=%%d default: 0 (send close_notify)\n" \
400 "\n" \
401 USAGE_DTLS \
402 USAGE_CID \
403 USAGE_SRTP \
404 "\n"
405 #define USAGE2 \
406 " auth_mode=%%s default: (library default: none)\n" \
407 " options: none, optional, required\n" \
408 USAGE_IO \
409 USAGE_KEY_OPAQUE \
410 USAGE_CA_CALLBACK \
411 "\n" \
412 USAGE_PSK \
413 USAGE_ECJPAKE \
414 USAGE_ECRESTART \
415 "\n"
416 #define USAGE3 \
417 " allow_legacy=%%d default: (library default: no)\n" \
418 USAGE_RENEGO \
419 " exchanges=%%d default: 1\n" \
420 " reconnect=%%d number of reconnections using session resumption\n" \
421 " default: 0 (disabled)\n" \
422 " reco_server_name=%%s default: NULL\n" \
423 " reco_delay=%%d default: 0 milliseconds\n" \
424 " reco_mode=%%d 0: copy session, 1: serialize session\n" \
425 " default: 1\n" \
426 " reconnect_hard=%%d default: 0 (disabled)\n" \
427 USAGE_TICKETS \
428 USAGE_EAP_TLS \
429 USAGE_MAX_FRAG_LEN \
430 USAGE_CONTEXT_CRT_CB \
431 USAGE_ALPN \
432 USAGE_EMS \
433 USAGE_ETM \
434 USAGE_REPRODUCIBLE \
435 USAGE_GROUPS \
436 USAGE_SIG_ALGS \
437 USAGE_EARLY_DATA \
438 USAGE_DHMLEN \
439 USAGE_KEY_OPAQUE_ALGS \
440 "\n"
441
442 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
443 #define TLS1_3_VERSION_OPTIONS ", tls13"
444 #else /* MBEDTLS_SSL_PROTO_TLS1_3 */
445 #define TLS1_3_VERSION_OPTIONS ""
446 #endif /* !MBEDTLS_SSL_PROTO_TLS1_3 */
447
448 #define USAGE4 \
449 " allow_sha1=%%d default: 0\n" \
450 " min_version=%%s default: (library default: tls12)\n" \
451 " max_version=%%s default: (library default: tls12)\n" \
452 " force_version=%%s default: \"\" (none)\n" \
453 " options: tls12, dtls12" TLS1_3_VERSION_OPTIONS \
454 "\n\n" \
455 " force_ciphersuite=<name> default: all enabled\n" \
456 USAGE_TLS1_3_KEY_EXCHANGE_MODES \
457 " query_config=<name> return 0 if the specified\n" \
458 " configuration macro is defined and 1\n" \
459 " otherwise. The expansion of the macro\n" \
460 " is printed if it is defined\n" \
461 USAGE_SERIALIZATION \
462 "\n"
463
464 /*
465 * global options
466 */
467 struct options {
468 const char *server_name; /* hostname of the server (client only) */
469 const char *server_addr; /* address of the server (client only) */
470 const char *server_port; /* port on which the ssl service runs */
471 int debug_level; /* level of debugging */
472 int nbio; /* should I/O be blocking? */
473 int event; /* loop or event-driven IO? level or edge triggered? */
474 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
475 int max_resend; /* DTLS times to resend on read timeout */
476 const char *request_page; /* page on server to request */
477 int request_size; /* pad request with header to requested size */
478 const char *ca_file; /* the file with the CA certificate(s) */
479 const char *ca_path; /* the path with the CA certificate(s) reside */
480 const char *crt_file; /* the file with the client certificate */
481 const char *key_file; /* the file with the client key */
482 int key_opaque; /* handle private key as if it were opaque */
483 #if defined(MBEDTLS_USE_PSA_CRYPTO)
484 int psk_opaque;
485 #endif
486 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
487 int ca_callback; /* Use callback for trusted certificate list */
488 #endif
489 const char *key_pwd; /* the password for the client key */
490 const char *psk; /* the pre-shared key */
491 const char *psk_identity; /* the pre-shared key identity */
492 const char *ecjpake_pw; /* the EC J-PAKE password */
493 #if defined(MBEDTLS_USE_PSA_CRYPTO)
494 int ecjpake_pw_opaque; /* set to 1 to use the opaque method for setting the password */
495 #endif
496 int ec_max_ops; /* EC consecutive operations limit */
497 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
498 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
499 int tls13_kex_modes; /* supported TLS 1.3 key exchange modes */
500 #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
501 int renegotiation; /* enable / disable renegotiation */
502 int allow_legacy; /* allow legacy renegotiation */
503 int renegotiate; /* attempt renegotiation? */
504 int renego_delay; /* delay before enforcing renegotiation */
505 int exchanges; /* number of data exchanges */
506 int min_version; /* minimum protocol version accepted */
507 int max_version; /* maximum protocol version accepted */
508 int allow_sha1; /* flag for SHA-1 support */
509 int auth_mode; /* verify mode for connection */
510 unsigned char mfl_code; /* code for maximum fragment length */
511 int trunc_hmac; /* negotiate truncated hmac or not */
512 int recsplit; /* enable record splitting? */
513 int dhmlen; /* minimum DHM params len in bits */
514 int reconnect; /* attempt to resume session */
515 const char *reco_server_name; /* hostname of the server (re-connect) */
516 int reco_delay; /* delay in seconds before resuming session */
517 int reco_mode; /* how to keep the session around */
518 int reconnect_hard; /* unexpectedly reconnect from the same port */
519 int tickets; /* enable / disable session tickets (TLS 1.2) */
520 int new_session_tickets; /* enable / disable new session tickets (TLS 1.3) */
521 const char *groups; /* list of supported groups */
522 const char *sig_algs; /* supported TLS 1.3 signature algorithms */
523 const char *alpn_string; /* ALPN supported protocols */
524 int transport; /* TLS or DTLS? */
525 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
526 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
527 int dtls_mtu; /* UDP Maximum transport unit for DTLS */
528 int fallback; /* is this a fallback connection? */
529 int dgram_packing; /* allow/forbid datagram packing */
530 int extended_ms; /* negotiate extended master secret? */
531 int etm; /* negotiate encrypt then mac? */
532 int context_crt_cb; /* use context-specific CRT verify callback */
533 int eap_tls; /* derive EAP-TLS keying material? */
534 int nss_keylog; /* export NSS key log material */
535 const char *nss_keylog_file; /* NSS key log file */
536 int cid_enabled; /* whether to use the CID extension or not */
537 int cid_enabled_renego; /* whether to use the CID extension or not
538 * during renegotiation */
539 const char *cid_val; /* the CID to use for incoming messages */
540 int serialize; /* serialize/deserialize connection */
541 const char *context_file; /* the file to write a serialized connection
542 * in the form of base64 code (serialize
543 * option must be set) */
544 const char *cid_val_renego; /* the CID to use for incoming messages
545 * after renegotiation */
546 int reproducible; /* make communication reproducible */
547 int skip_close_notify; /* skip sending the close_notify alert */
548 #if defined(MBEDTLS_SSL_EARLY_DATA)
549 int early_data; /* early data enablement flag */
550 #endif
551 int query_config_mode; /* whether to read config */
552 int use_srtp; /* Support SRTP */
553 int force_srtp_profile; /* SRTP protection profile to use or all */
554 const char *mki; /* The dtls mki value to use */
555 const char *key_opaque_alg1; /* Allowed opaque key alg 1 */
556 const char *key_opaque_alg2; /* Allowed Opaque key alg 2 */
557 } opt;
558
559 #include "ssl_test_common_source.c"
560
561 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
562 static unsigned char peer_crt_info[1024];
563
564 /*
565 * Enabled if debug_level > 1 in code below
566 */
my_verify(void * data,mbedtls_x509_crt * crt,int depth,uint32_t * flags)567 static int my_verify(void *data, mbedtls_x509_crt *crt,
568 int depth, uint32_t *flags)
569 {
570 char buf[1024];
571 ((void) data);
572
573 mbedtls_printf("\nVerify requested for (Depth %d):\n", depth);
574
575 #if !defined(MBEDTLS_X509_REMOVE_INFO)
576 mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
577 if (depth == 0) {
578 memcpy(peer_crt_info, buf, sizeof(buf));
579 }
580
581 if (opt.debug_level == 0) {
582 return 0;
583 }
584
585 mbedtls_printf("%s", buf);
586 #else
587 ((void) crt);
588 ((void) depth);
589 #endif
590
591 if ((*flags) == 0) {
592 mbedtls_printf(" This certificate has no flags\n");
593 } else {
594 x509_crt_verify_info(buf, sizeof(buf), " ! ", *flags);
595 mbedtls_printf("%s\n", buf);
596 }
597
598 return 0;
599 }
600 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
601
602 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
report_cid_usage(mbedtls_ssl_context * ssl,const char * additional_description)603 static int report_cid_usage(mbedtls_ssl_context *ssl,
604 const char *additional_description)
605 {
606 int ret;
607 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX];
608 size_t peer_cid_len;
609 int cid_negotiated;
610
611 if (opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
612 return 0;
613 }
614
615 /* Check if the use of a CID has been negotiated,
616 * but don't ask for the CID value and length.
617 *
618 * Note: Here and below, we're demonstrating the various ways
619 * in which mbedtls_ssl_get_peer_cid() can be called,
620 * depending on whether or not the length/value of the
621 * peer's CID is needed.
622 *
623 * An actual application, however, should use
624 * just one call to mbedtls_ssl_get_peer_cid(). */
625 ret = mbedtls_ssl_get_peer_cid(ssl, &cid_negotiated,
626 NULL, NULL);
627 if (ret != 0) {
628 mbedtls_printf(" failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
629 (unsigned int) -ret);
630 return ret;
631 }
632
633 if (cid_negotiated == MBEDTLS_SSL_CID_DISABLED) {
634 if (opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED) {
635 mbedtls_printf("(%s) Use of Connection ID was rejected by the server.\n",
636 additional_description);
637 }
638 } else {
639 size_t idx = 0;
640 mbedtls_printf("(%s) Use of Connection ID has been negotiated.\n",
641 additional_description);
642
643 /* Ask for just the length of the peer's CID. */
644 ret = mbedtls_ssl_get_peer_cid(ssl, &cid_negotiated,
645 NULL, &peer_cid_len);
646 if (ret != 0) {
647 mbedtls_printf(" failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
648 (unsigned int) -ret);
649 return ret;
650 }
651
652 /* Ask for just length + value of the peer's CID. */
653 ret = mbedtls_ssl_get_peer_cid(ssl, &cid_negotiated,
654 peer_cid, &peer_cid_len);
655 if (ret != 0) {
656 mbedtls_printf(" failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
657 (unsigned int) -ret);
658 return ret;
659 }
660 mbedtls_printf("(%s) Peer CID (length %u Bytes): ",
661 additional_description,
662 (unsigned) peer_cid_len);
663 while (idx < peer_cid_len) {
664 mbedtls_printf("%02x ", peer_cid[idx]);
665 idx++;
666 }
667 mbedtls_printf("\n");
668 }
669
670 return 0;
671 }
672 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
673
ssl_save_session_serialize(mbedtls_ssl_context * ssl,unsigned char ** session_data,size_t * session_data_len)674 static int ssl_save_session_serialize(mbedtls_ssl_context *ssl,
675 unsigned char **session_data,
676 size_t *session_data_len)
677 {
678 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
679 mbedtls_ssl_session exported_session;
680
681 /* free any previously saved data */
682 if (*session_data != NULL) {
683 mbedtls_platform_zeroize(*session_data, *session_data_len);
684 mbedtls_free(*session_data);
685 *session_data = NULL;
686 *session_data_len = 0;
687 }
688
689 mbedtls_ssl_session_init(&exported_session);
690 ret = mbedtls_ssl_get_session(ssl, &exported_session);
691 if (ret != 0) {
692 mbedtls_printf(
693 "failed\n ! mbedtls_ssl_get_session() returned -%#02x\n",
694 (unsigned) -ret);
695 goto exit;
696 }
697
698 /* get size of the buffer needed */
699 (void) mbedtls_ssl_session_save(&exported_session, NULL, 0, session_data_len);
700 *session_data = mbedtls_calloc(1, *session_data_len);
701 if (*session_data == NULL) {
702 mbedtls_printf(" failed\n ! alloc %u bytes for session data\n",
703 (unsigned) *session_data_len);
704 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
705 goto exit;
706 }
707
708 /* actually save session data */
709 if ((ret = mbedtls_ssl_session_save(&exported_session,
710 *session_data, *session_data_len,
711 session_data_len)) != 0) {
712 mbedtls_printf(" failed\n ! mbedtls_ssl_session_saved returned -0x%04x\n\n",
713 (unsigned int) -ret);
714 goto exit;
715 }
716
717 exit:
718 mbedtls_ssl_session_free(&exported_session);
719 return ret;
720 }
721
722 /*
723 * Build HTTP request
724 */
build_http_request(unsigned char * buf,size_t buf_size,size_t * request_len)725 static int build_http_request(unsigned char *buf, size_t buf_size, size_t *request_len)
726 {
727 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
728 size_t len, tail_len, request_size;
729
730 ret = mbedtls_snprintf((char *) buf, buf_size, GET_REQUEST, opt.request_page);
731 if (ret < 0) {
732 return ret;
733 }
734
735 len = (size_t) ret;
736 tail_len = strlen(GET_REQUEST_END);
737 if (opt.request_size != DFL_REQUEST_SIZE) {
738 request_size = (size_t) opt.request_size;
739 } else {
740 request_size = len + tail_len;
741 }
742
743 if (request_size > buf_size) {
744 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
745 }
746
747 /* Add padding to GET request to reach opt.request_size in length */
748 if (opt.request_size != DFL_REQUEST_SIZE &&
749 len + tail_len < request_size) {
750 memset(buf + len, 'A', request_size - len - tail_len);
751 len = request_size - tail_len;
752 }
753
754 strncpy((char *) buf + len, GET_REQUEST_END, buf_size - len);
755 len += tail_len;
756
757 /* Truncate if request size is smaller than the "natural" size */
758 if (opt.request_size != DFL_REQUEST_SIZE &&
759 len > request_size) {
760 len = request_size;
761
762 /* Still end with \r\n unless that's really not possible */
763 if (len >= 2) {
764 buf[len - 2] = '\r';
765 }
766 if (len >= 1) {
767 buf[len - 1] = '\n';
768 }
769 }
770
771 *request_len = len;
772
773 return 0;
774 }
775
main(int argc,char * argv[])776 int main(int argc, char *argv[])
777 {
778 int ret = 0, i;
779 size_t len, written, frags, retry_left;
780 int query_config_ret = 0;
781 mbedtls_net_context server_fd;
782 io_ctx_t io_ctx;
783
784 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
785 uint16_t sig_alg_list[SIG_ALG_LIST_SIZE];
786 #endif
787
788 unsigned char buf[MAX_REQUEST_SIZE + 1];
789
790 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
791 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
792 size_t psk_len = 0;
793 #endif
794
795 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
796 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
797 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
798 size_t cid_len = 0;
799 size_t cid_renego_len = 0;
800 #endif
801
802 #if defined(MBEDTLS_SSL_ALPN)
803 const char *alpn_list[ALPN_LIST_SIZE];
804 #endif
805
806 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
807 unsigned char alloc_buf[MEMORY_HEAP_SIZE];
808 #endif
809 uint16_t group_list[GROUP_LIST_SIZE];
810 #if defined(MBEDTLS_SSL_DTLS_SRTP)
811 unsigned char mki[MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH];
812 size_t mki_len = 0;
813 #endif
814
815 const char *pers = "ssl_client2";
816
817 #if defined(MBEDTLS_USE_PSA_CRYPTO)
818 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
819 mbedtls_svc_key_id_t slot = MBEDTLS_SVC_KEY_ID_INIT;
820 psa_algorithm_t alg = 0;
821 psa_key_attributes_t key_attributes;
822 #endif
823 psa_status_t status;
824 #endif
825
826 rng_context_t rng;
827 mbedtls_ssl_context ssl;
828 mbedtls_ssl_config conf;
829 mbedtls_ssl_session saved_session;
830 unsigned char *session_data = NULL;
831 size_t session_data_len = 0;
832 #if defined(MBEDTLS_TIMING_C)
833 mbedtls_timing_delay_context timer;
834 #endif
835 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
836 uint32_t flags;
837 mbedtls_x509_crt cacert;
838 mbedtls_x509_crt clicert;
839 mbedtls_pk_context pkey;
840 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
841 #if defined(MBEDTLS_USE_PSA_CRYPTO)
842 mbedtls_svc_key_id_t key_slot = MBEDTLS_SVC_KEY_ID_INIT; /* invalid key slot */
843 #endif
844 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
845 char *p, *q;
846 const int *list;
847 #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
848 unsigned char *context_buf = NULL;
849 size_t context_buf_len;
850 #endif
851 unsigned char eap_tls_keymaterial[16];
852 unsigned char eap_tls_iv[8];
853 const char *eap_tls_label = "client EAP encryption";
854 eap_tls_keys eap_tls_keying;
855 #if defined(MBEDTLS_SSL_DTLS_SRTP)
856 /*! master keys and master salt for SRTP generated during handshake */
857 unsigned char dtls_srtp_key_material[MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH];
858 const char *dtls_srtp_label = "EXTRACTOR-dtls_srtp";
859 dtls_srtp_keys dtls_srtp_keying;
860 const mbedtls_ssl_srtp_profile default_profiles[] = {
861 MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80,
862 MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_32,
863 MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_80,
864 MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_32,
865 MBEDTLS_TLS_SRTP_UNSET
866 };
867 #endif /* MBEDTLS_SSL_DTLS_SRTP */
868 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
869 defined(MBEDTLS_USE_PSA_CRYPTO)
870 mbedtls_svc_key_id_t ecjpake_pw_slot = MBEDTLS_SVC_KEY_ID_INIT; /* ecjpake password key slot */
871 #endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
872
873 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
874 mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
875 #endif
876
877 #if defined(MBEDTLS_TEST_HOOKS)
878 test_hooks_init();
879 #endif /* MBEDTLS_TEST_HOOKS */
880
881 /*
882 * Make sure memory references are valid.
883 */
884 mbedtls_net_init(&server_fd);
885 mbedtls_ssl_init(&ssl);
886 mbedtls_ssl_config_init(&conf);
887 mbedtls_ssl_session_init(&saved_session);
888 rng_init(&rng);
889 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
890 mbedtls_x509_crt_init(&cacert);
891 mbedtls_x509_crt_init(&clicert);
892 mbedtls_pk_init(&pkey);
893 #endif
894 #if defined(MBEDTLS_SSL_ALPN)
895 memset((void *) alpn_list, 0, sizeof(alpn_list));
896 #endif
897
898 /* For builds with TLS 1.3 enabled but not MBEDTLS_USE_PSA_CRYPTO,
899 * we deliberately do not call psa_crypto_init() here, to test that
900 * the library is backward-compatible with versions prior to 3.6.0
901 * where calling psa_crypto_init() was not required to open a TLS
902 * connection in the default configuration. See
903 * https://github.com/Mbed-TLS/mbedtls/issues/9072 and
904 * mbedtls_ssl_tls13_crypto_init().
905 */
906 #if defined(MBEDTLS_USE_PSA_CRYPTO)
907 status = psa_crypto_init();
908 if (status != PSA_SUCCESS) {
909 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
910 (int) status);
911 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
912 goto exit;
913 }
914 #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
915 #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
916 mbedtls_test_enable_insecure_external_rng();
917 #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
918
919 opt.server_name = DFL_SERVER_NAME;
920 opt.server_addr = DFL_SERVER_ADDR;
921 opt.server_port = DFL_SERVER_PORT;
922 opt.debug_level = DFL_DEBUG_LEVEL;
923 opt.cid_enabled = DFL_CID_ENABLED;
924 opt.cid_val = DFL_CID_VALUE;
925 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
926 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
927 opt.nbio = DFL_NBIO;
928 opt.event = DFL_EVENT;
929 opt.context_crt_cb = DFL_CONTEXT_CRT_CB;
930 opt.read_timeout = DFL_READ_TIMEOUT;
931 opt.max_resend = DFL_MAX_RESEND;
932 opt.request_page = DFL_REQUEST_PAGE;
933 opt.request_size = DFL_REQUEST_SIZE;
934 opt.ca_file = DFL_CA_FILE;
935 opt.ca_path = DFL_CA_PATH;
936 opt.crt_file = DFL_CRT_FILE;
937 opt.key_file = DFL_KEY_FILE;
938 opt.key_opaque = DFL_KEY_OPAQUE;
939 opt.key_pwd = DFL_KEY_PWD;
940 opt.psk = DFL_PSK;
941 #if defined(MBEDTLS_USE_PSA_CRYPTO)
942 opt.psk_opaque = DFL_PSK_OPAQUE;
943 #endif
944 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
945 opt.ca_callback = DFL_CA_CALLBACK;
946 #endif
947 opt.psk_identity = DFL_PSK_IDENTITY;
948 opt.ecjpake_pw = DFL_ECJPAKE_PW;
949 #if defined(MBEDTLS_USE_PSA_CRYPTO)
950 opt.ecjpake_pw_opaque = DFL_ECJPAKE_PW_OPAQUE;
951 #endif
952 opt.ec_max_ops = DFL_EC_MAX_OPS;
953 opt.force_ciphersuite[0] = DFL_FORCE_CIPHER;
954 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
955 opt.tls13_kex_modes = DFL_TLS1_3_KEX_MODES;
956 #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
957 opt.renegotiation = DFL_RENEGOTIATION;
958 opt.allow_legacy = DFL_ALLOW_LEGACY;
959 opt.renegotiate = DFL_RENEGOTIATE;
960 opt.exchanges = DFL_EXCHANGES;
961 opt.min_version = DFL_MIN_VERSION;
962 opt.max_version = DFL_MAX_VERSION;
963 opt.allow_sha1 = DFL_SHA1;
964 opt.auth_mode = DFL_AUTH_MODE;
965 opt.mfl_code = DFL_MFL_CODE;
966 opt.trunc_hmac = DFL_TRUNC_HMAC;
967 opt.recsplit = DFL_RECSPLIT;
968 opt.dhmlen = DFL_DHMLEN;
969 opt.reconnect = DFL_RECONNECT;
970 opt.reco_server_name = DFL_RECO_SERVER_NAME;
971 opt.reco_delay = DFL_RECO_DELAY;
972 opt.reco_mode = DFL_RECO_MODE;
973 opt.reconnect_hard = DFL_RECONNECT_HARD;
974 opt.tickets = DFL_TICKETS;
975 opt.new_session_tickets = DFL_NEW_SESSION_TICKETS;
976 opt.alpn_string = DFL_ALPN_STRING;
977 opt.groups = DFL_GROUPS;
978 opt.sig_algs = DFL_SIG_ALGS;
979 #if defined(MBEDTLS_SSL_EARLY_DATA)
980 opt.early_data = DFL_EARLY_DATA;
981 #endif
982 opt.transport = DFL_TRANSPORT;
983 opt.hs_to_min = DFL_HS_TO_MIN;
984 opt.hs_to_max = DFL_HS_TO_MAX;
985 opt.dtls_mtu = DFL_DTLS_MTU;
986 opt.fallback = DFL_FALLBACK;
987 opt.extended_ms = DFL_EXTENDED_MS;
988 opt.etm = DFL_ETM;
989 opt.dgram_packing = DFL_DGRAM_PACKING;
990 opt.serialize = DFL_SERIALIZE;
991 opt.context_file = DFL_CONTEXT_FILE;
992 opt.eap_tls = DFL_EAP_TLS;
993 opt.reproducible = DFL_REPRODUCIBLE;
994 opt.nss_keylog = DFL_NSS_KEYLOG;
995 opt.nss_keylog_file = DFL_NSS_KEYLOG_FILE;
996 opt.skip_close_notify = DFL_SKIP_CLOSE_NOTIFY;
997 opt.query_config_mode = DFL_QUERY_CONFIG_MODE;
998 opt.use_srtp = DFL_USE_SRTP;
999 opt.force_srtp_profile = DFL_SRTP_FORCE_PROFILE;
1000 opt.mki = DFL_SRTP_MKI;
1001 opt.key_opaque_alg1 = DFL_KEY_OPAQUE_ALG;
1002 opt.key_opaque_alg2 = DFL_KEY_OPAQUE_ALG;
1003
1004 p = q = NULL;
1005 if (argc < 1) {
1006 usage:
1007 if (p != NULL && q != NULL) {
1008 printf("unrecognized value for '%s': '%s'\n", p, q);
1009 } else if (p != NULL && q == NULL) {
1010 printf("unrecognized param: '%s'\n", p);
1011 }
1012
1013 mbedtls_printf("usage: ssl_client2 [param=value] [...]\n");
1014 mbedtls_printf(" ssl_client2 help[_theme]\n");
1015 mbedtls_printf("'help' lists acceptable 'param' and 'value'\n");
1016 mbedtls_printf("'help_ciphersuites' lists available ciphersuites\n");
1017 mbedtls_printf("\n");
1018
1019 if (ret == 0) {
1020 ret = 1;
1021 }
1022 goto exit;
1023 }
1024
1025 for (i = 1; i < argc; i++) {
1026 p = argv[i];
1027
1028 if (strcmp(p, "help") == 0) {
1029 mbedtls_printf(USAGE1);
1030 mbedtls_printf(USAGE2);
1031 mbedtls_printf(USAGE3);
1032 mbedtls_printf(USAGE4);
1033
1034 ret = 0;
1035 goto exit;
1036 }
1037 if (strcmp(p, "help_ciphersuites") == 0) {
1038 mbedtls_printf(" acceptable ciphersuite names:\n");
1039 for (list = mbedtls_ssl_list_ciphersuites();
1040 *list != 0;
1041 list++) {
1042 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name(*list));
1043 }
1044
1045 ret = 0;
1046 goto exit;
1047 }
1048
1049 if ((q = strchr(p, '=')) == NULL) {
1050 mbedtls_printf("param requires a value: '%s'\n", p);
1051 p = NULL; // avoid "unrecnognized param" message
1052 goto usage;
1053 }
1054 *q++ = '\0';
1055
1056 if (strcmp(p, "server_name") == 0) {
1057 opt.server_name = q;
1058 } else if (strcmp(p, "server_addr") == 0) {
1059 opt.server_addr = q;
1060 } else if (strcmp(p, "server_port") == 0) {
1061 opt.server_port = q;
1062 } else if (strcmp(p, "dtls") == 0) {
1063 int t = atoi(q);
1064 if (t == 0) {
1065 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
1066 } else if (t == 1) {
1067 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
1068 } else {
1069 goto usage;
1070 }
1071 } else if (strcmp(p, "debug_level") == 0) {
1072 opt.debug_level = atoi(q);
1073 if (opt.debug_level < 0 || opt.debug_level > 65535) {
1074 goto usage;
1075 }
1076 } else if (strcmp(p, "build_version") == 0) {
1077 if (strcmp(q, "1") == 0) {
1078 mbedtls_printf("build version: %s (build %d)\n",
1079 MBEDTLS_VERSION_STRING_FULL,
1080 MBEDTLS_VERSION_NUMBER);
1081 goto exit;
1082 }
1083 } else if (strcmp(p, "context_crt_cb") == 0) {
1084 opt.context_crt_cb = atoi(q);
1085 if (opt.context_crt_cb != 0 && opt.context_crt_cb != 1) {
1086 goto usage;
1087 }
1088 } else if (strcmp(p, "nbio") == 0) {
1089 opt.nbio = atoi(q);
1090 if (opt.nbio < 0 || opt.nbio > 2) {
1091 goto usage;
1092 }
1093 } else if (strcmp(p, "event") == 0) {
1094 opt.event = atoi(q);
1095 if (opt.event < 0 || opt.event > 2) {
1096 goto usage;
1097 }
1098 } else if (strcmp(p, "read_timeout") == 0) {
1099 opt.read_timeout = atoi(q);
1100 } else if (strcmp(p, "max_resend") == 0) {
1101 opt.max_resend = atoi(q);
1102 if (opt.max_resend < 0) {
1103 goto usage;
1104 }
1105 } else if (strcmp(p, "request_page") == 0) {
1106 opt.request_page = q;
1107 } else if (strcmp(p, "request_size") == 0) {
1108 opt.request_size = atoi(q);
1109 if (opt.request_size < 0 ||
1110 opt.request_size > MAX_REQUEST_SIZE) {
1111 goto usage;
1112 }
1113 } else if (strcmp(p, "ca_file") == 0) {
1114 opt.ca_file = q;
1115 } else if (strcmp(p, "ca_path") == 0) {
1116 opt.ca_path = q;
1117 } else if (strcmp(p, "crt_file") == 0) {
1118 opt.crt_file = q;
1119 } else if (strcmp(p, "key_file") == 0) {
1120 opt.key_file = q;
1121 } else if (strcmp(p, "key_pwd") == 0) {
1122 opt.key_pwd = q;
1123 }
1124 #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1125 else if (strcmp(p, "key_opaque") == 0) {
1126 opt.key_opaque = atoi(q);
1127 }
1128 #endif
1129 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1130 else if (strcmp(p, "cid") == 0) {
1131 opt.cid_enabled = atoi(q);
1132 if (opt.cid_enabled != 0 && opt.cid_enabled != 1) {
1133 goto usage;
1134 }
1135 } else if (strcmp(p, "cid_renego") == 0) {
1136 opt.cid_enabled_renego = atoi(q);
1137 if (opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1) {
1138 goto usage;
1139 }
1140 } else if (strcmp(p, "cid_val") == 0) {
1141 opt.cid_val = q;
1142 } else if (strcmp(p, "cid_val_renego") == 0) {
1143 opt.cid_val_renego = q;
1144 }
1145 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1146 else if (strcmp(p, "psk") == 0) {
1147 opt.psk = q;
1148 }
1149 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1150 else if (strcmp(p, "psk_opaque") == 0) {
1151 opt.psk_opaque = atoi(q);
1152 }
1153 #endif
1154 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1155 else if (strcmp(p, "ca_callback") == 0) {
1156 opt.ca_callback = atoi(q);
1157 }
1158 #endif
1159 else if (strcmp(p, "psk_identity") == 0) {
1160 opt.psk_identity = q;
1161 } else if (strcmp(p, "ecjpake_pw") == 0) {
1162 opt.ecjpake_pw = q;
1163 }
1164 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1165 else if (strcmp(p, "ecjpake_pw_opaque") == 0) {
1166 opt.ecjpake_pw_opaque = atoi(q);
1167 }
1168 #endif
1169 else if (strcmp(p, "ec_max_ops") == 0) {
1170 opt.ec_max_ops = atoi(q);
1171 } else if (strcmp(p, "force_ciphersuite") == 0) {
1172 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(q);
1173
1174 if (opt.force_ciphersuite[0] == 0) {
1175 ret = 2;
1176 goto usage;
1177 }
1178 opt.force_ciphersuite[1] = 0;
1179 } else if (strcmp(p, "renegotiation") == 0) {
1180 opt.renegotiation = (atoi(q)) ?
1181 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1182 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
1183 } else if (strcmp(p, "allow_legacy") == 0) {
1184 switch (atoi(q)) {
1185 case -1:
1186 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1187 break;
1188 case 0:
1189 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1190 break;
1191 case 1:
1192 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1193 break;
1194 default: goto usage;
1195 }
1196 } else if (strcmp(p, "renegotiate") == 0) {
1197 opt.renegotiate = atoi(q);
1198 if (opt.renegotiate < 0 || opt.renegotiate > 1) {
1199 goto usage;
1200 }
1201 } else if (strcmp(p, "exchanges") == 0) {
1202 opt.exchanges = atoi(q);
1203 if (opt.exchanges < 1) {
1204 goto usage;
1205 }
1206 } else if (strcmp(p, "reconnect") == 0) {
1207 opt.reconnect = atoi(q);
1208 if (opt.reconnect < 0 || opt.reconnect > 2) {
1209 goto usage;
1210 }
1211 } else if (strcmp(p, "reco_server_name") == 0) {
1212 opt.reco_server_name = q;
1213 } else if (strcmp(p, "reco_delay") == 0) {
1214 opt.reco_delay = atoi(q);
1215 if (opt.reco_delay < 0) {
1216 goto usage;
1217 }
1218 } else if (strcmp(p, "reco_mode") == 0) {
1219 opt.reco_mode = atoi(q);
1220 if (opt.reco_mode < 0) {
1221 goto usage;
1222 }
1223 } else if (strcmp(p, "reconnect_hard") == 0) {
1224 opt.reconnect_hard = atoi(q);
1225 if (opt.reconnect_hard < 0 || opt.reconnect_hard > 1) {
1226 goto usage;
1227 }
1228 } else if (strcmp(p, "tickets") == 0) {
1229 opt.tickets = atoi(q);
1230 if (opt.tickets < 0) {
1231 goto usage;
1232 }
1233 } else if (strcmp(p, "new_session_tickets") == 0) {
1234 opt.new_session_tickets = atoi(q);
1235 if (opt.new_session_tickets < 0) {
1236 goto usage;
1237 }
1238 } else if (strcmp(p, "alpn") == 0) {
1239 opt.alpn_string = q;
1240 } else if (strcmp(p, "extended_ms") == 0) {
1241 switch (atoi(q)) {
1242 case 0:
1243 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1244 break;
1245 case 1:
1246 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1247 break;
1248 default: goto usage;
1249 }
1250 } else if (strcmp(p, "groups") == 0) {
1251 opt.groups = q;
1252 }
1253 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1254 else if (strcmp(p, "sig_algs") == 0) {
1255 opt.sig_algs = q;
1256 }
1257 #endif
1258 else if (strcmp(p, "etm") == 0) {
1259 switch (atoi(q)) {
1260 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1261 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
1262 default: goto usage;
1263 }
1264 }
1265
1266 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1267 #if defined(MBEDTLS_SSL_EARLY_DATA)
1268 else if (strcmp(p, "early_data") == 0) {
1269 switch (atoi(q)) {
1270 case 0:
1271 opt.early_data = MBEDTLS_SSL_EARLY_DATA_DISABLED;
1272 break;
1273 case 1:
1274 opt.early_data = MBEDTLS_SSL_EARLY_DATA_ENABLED;
1275 break;
1276 default: goto usage;
1277 }
1278 }
1279 #endif /* MBEDTLS_SSL_EARLY_DATA */
1280
1281 else if (strcmp(p, "tls13_kex_modes") == 0) {
1282 if (strcmp(q, "psk") == 0) {
1283 opt.tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1284 } else if (strcmp(q, "psk_ephemeral") == 0) {
1285 opt.tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1286 } else if (strcmp(q, "ephemeral") == 0) {
1287 opt.tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1288 } else if (strcmp(q, "ephemeral_all") == 0) {
1289 opt.tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ALL;
1290 } else if (strcmp(q, "psk_all") == 0) {
1291 opt.tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
1292 } else if (strcmp(q, "all") == 0) {
1293 opt.tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
1294 } else if (strcmp(q, "psk_or_ephemeral") == 0) {
1295 opt.tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK |
1296 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1297 } else {
1298 goto usage;
1299 }
1300 }
1301 #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1302 else if (strcmp(p, "min_version") == 0) {
1303 if (strcmp(q, "tls12") == 0 ||
1304 strcmp(q, "dtls12") == 0) {
1305 opt.min_version = MBEDTLS_SSL_VERSION_TLS1_2;
1306 }
1307 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1308 else if (strcmp(q, "tls13") == 0) {
1309 opt.min_version = MBEDTLS_SSL_VERSION_TLS1_3;
1310 }
1311 #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1312 else {
1313 goto usage;
1314 }
1315 } else if (strcmp(p, "max_version") == 0) {
1316 if (strcmp(q, "tls12") == 0 ||
1317 strcmp(q, "dtls12") == 0) {
1318 opt.max_version = MBEDTLS_SSL_VERSION_TLS1_2;
1319 }
1320 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1321 else if (strcmp(q, "tls13") == 0) {
1322 opt.max_version = MBEDTLS_SSL_VERSION_TLS1_3;
1323 }
1324 #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1325 else {
1326 goto usage;
1327 }
1328 } else if (strcmp(p, "allow_sha1") == 0) {
1329 switch (atoi(q)) {
1330 case 0: opt.allow_sha1 = 0; break;
1331 case 1: opt.allow_sha1 = 1; break;
1332 default: goto usage;
1333 }
1334 } else if (strcmp(p, "force_version") == 0) {
1335 if (strcmp(q, "tls12") == 0) {
1336 opt.min_version = MBEDTLS_SSL_VERSION_TLS1_2;
1337 opt.max_version = MBEDTLS_SSL_VERSION_TLS1_2;
1338 } else if (strcmp(q, "dtls12") == 0) {
1339 opt.min_version = MBEDTLS_SSL_VERSION_TLS1_2;
1340 opt.max_version = MBEDTLS_SSL_VERSION_TLS1_2;
1341 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
1342 }
1343 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1344 else if (strcmp(q, "tls13") == 0) {
1345 opt.min_version = MBEDTLS_SSL_VERSION_TLS1_3;
1346 opt.max_version = MBEDTLS_SSL_VERSION_TLS1_3;
1347 }
1348 #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1349 else {
1350 goto usage;
1351 }
1352 } else if (strcmp(p, "auth_mode") == 0) {
1353 if (strcmp(q, "none") == 0) {
1354 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
1355 } else if (strcmp(q, "optional") == 0) {
1356 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
1357 } else if (strcmp(q, "required") == 0) {
1358 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
1359 } else {
1360 goto usage;
1361 }
1362 } else if (strcmp(p, "max_frag_len") == 0) {
1363 if (strcmp(q, "512") == 0) {
1364 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
1365 } else if (strcmp(q, "1024") == 0) {
1366 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
1367 } else if (strcmp(q, "2048") == 0) {
1368 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
1369 } else if (strcmp(q, "4096") == 0) {
1370 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
1371 } else {
1372 goto usage;
1373 }
1374 } else if (strcmp(p, "trunc_hmac") == 0) {
1375 switch (atoi(q)) {
1376 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1377 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
1378 default: goto usage;
1379 }
1380 } else if (strcmp(p, "hs_timeout") == 0) {
1381 if ((p = strchr(q, '-')) == NULL) {
1382 goto usage;
1383 }
1384 *p++ = '\0';
1385 opt.hs_to_min = atoi(q);
1386 opt.hs_to_max = atoi(p);
1387 if (opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min) {
1388 goto usage;
1389 }
1390 } else if (strcmp(p, "mtu") == 0) {
1391 opt.dtls_mtu = atoi(q);
1392 if (opt.dtls_mtu < 0) {
1393 goto usage;
1394 }
1395 } else if (strcmp(p, "dgram_packing") == 0) {
1396 opt.dgram_packing = atoi(q);
1397 if (opt.dgram_packing != 0 &&
1398 opt.dgram_packing != 1) {
1399 goto usage;
1400 }
1401 } else if (strcmp(p, "recsplit") == 0) {
1402 opt.recsplit = atoi(q);
1403 if (opt.recsplit < 0 || opt.recsplit > 1) {
1404 goto usage;
1405 }
1406 } else if (strcmp(p, "dhmlen") == 0) {
1407 opt.dhmlen = atoi(q);
1408 if (opt.dhmlen < 0) {
1409 goto usage;
1410 }
1411 } else if (strcmp(p, "query_config") == 0) {
1412 opt.query_config_mode = 1;
1413 query_config_ret = query_config(q);
1414 goto exit;
1415 } else if (strcmp(p, "serialize") == 0) {
1416 opt.serialize = atoi(q);
1417 if (opt.serialize < 0 || opt.serialize > 2) {
1418 goto usage;
1419 }
1420 } else if (strcmp(p, "context_file") == 0) {
1421 opt.context_file = q;
1422 } else if (strcmp(p, "eap_tls") == 0) {
1423 opt.eap_tls = atoi(q);
1424 if (opt.eap_tls < 0 || opt.eap_tls > 1) {
1425 goto usage;
1426 }
1427 } else if (strcmp(p, "reproducible") == 0) {
1428 opt.reproducible = 1;
1429 } else if (strcmp(p, "nss_keylog") == 0) {
1430 opt.nss_keylog = atoi(q);
1431 if (opt.nss_keylog < 0 || opt.nss_keylog > 1) {
1432 goto usage;
1433 }
1434 } else if (strcmp(p, "nss_keylog_file") == 0) {
1435 opt.nss_keylog_file = q;
1436 } else if (strcmp(p, "skip_close_notify") == 0) {
1437 opt.skip_close_notify = atoi(q);
1438 if (opt.skip_close_notify < 0 || opt.skip_close_notify > 1) {
1439 goto usage;
1440 }
1441 } else if (strcmp(p, "use_srtp") == 0) {
1442 opt.use_srtp = atoi(q);
1443 } else if (strcmp(p, "srtp_force_profile") == 0) {
1444 opt.force_srtp_profile = atoi(q);
1445 } else if (strcmp(p, "mki") == 0) {
1446 opt.mki = q;
1447 } else if (strcmp(p, "key_opaque_algs") == 0) {
1448 if (key_opaque_alg_parse(q, &opt.key_opaque_alg1,
1449 &opt.key_opaque_alg2) != 0) {
1450 goto usage;
1451 }
1452 } else {
1453 /* This signals that the problem is with p not q */
1454 q = NULL;
1455 goto usage;
1456 }
1457 }
1458 /* This signals that any further errors are not with a single option */
1459 p = q = NULL;
1460
1461 if (opt.nss_keylog != 0 && opt.eap_tls != 0) {
1462 mbedtls_printf("Error: eap_tls and nss_keylog options cannot be used together.\n");
1463 goto usage;
1464 }
1465
1466 /* Event-driven IO is incompatible with the above custom
1467 * receive and send functions, as the polling builds on
1468 * refers to the underlying net_context. */
1469 if (opt.event == 1 && opt.nbio != 1) {
1470 mbedtls_printf("Warning: event-driven IO mandates nbio=1 - overwrite\n");
1471 opt.nbio = 1;
1472 }
1473
1474 #if defined(MBEDTLS_DEBUG_C)
1475 mbedtls_debug_set_threshold(opt.debug_level);
1476 #endif
1477
1478 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
1479 /*
1480 * Unhexify the pre-shared key if any is given
1481 */
1482 if (strlen(opt.psk)) {
1483 if (mbedtls_test_unhexify(psk, sizeof(psk),
1484 opt.psk, &psk_len) != 0) {
1485 mbedtls_printf("pre-shared key not valid\n");
1486 goto exit;
1487 }
1488 }
1489 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
1490
1491 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1492 if (opt.psk_opaque != 0) {
1493 if (opt.psk == NULL) {
1494 mbedtls_printf("psk_opaque set but no psk to be imported specified.\n");
1495 ret = 2;
1496 goto usage;
1497 }
1498
1499 if (opt.force_ciphersuite[0] <= 0) {
1500 mbedtls_printf(
1501 "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n");
1502 ret = 2;
1503 goto usage;
1504 }
1505 }
1506 #endif /* MBEDTLS_USE_PSA_CRYPTO */
1507
1508 if (opt.force_ciphersuite[0] > 0) {
1509 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1510 ciphersuite_info =
1511 mbedtls_ssl_ciphersuite_from_id(opt.force_ciphersuite[0]);
1512
1513 if (opt.max_version != -1 &&
1514 ciphersuite_info->min_tls_version > opt.max_version) {
1515 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
1516 ret = 2;
1517 goto usage;
1518 }
1519 if (opt.min_version != -1 &&
1520 ciphersuite_info->max_tls_version < opt.min_version) {
1521 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
1522 ret = 2;
1523 goto usage;
1524 }
1525
1526 /* If the server selects a version that's not supported by
1527 * this suite, then there will be no common ciphersuite... */
1528 if (opt.max_version == -1 ||
1529 opt.max_version > ciphersuite_info->max_tls_version) {
1530 opt.max_version = ciphersuite_info->max_tls_version;
1531 }
1532 if (opt.min_version < ciphersuite_info->min_tls_version) {
1533 opt.min_version = ciphersuite_info->min_tls_version;
1534 /* DTLS starts with TLS 1.2 */
1535 if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1536 opt.min_version < MBEDTLS_SSL_VERSION_TLS1_2) {
1537 opt.min_version = MBEDTLS_SSL_VERSION_TLS1_2;
1538 }
1539 }
1540
1541 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1542 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
1543 if (opt.psk_opaque != 0) {
1544 /* Determine KDF algorithm the opaque PSK will be used in. */
1545 #if defined(MBEDTLS_MD_CAN_SHA384)
1546 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
1547 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1548 } else
1549 #endif /* MBEDTLS_MD_CAN_SHA384 */
1550 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1551 }
1552 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
1553 #endif /* MBEDTLS_USE_PSA_CRYPTO */
1554 }
1555
1556 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1557 if (mbedtls_test_unhexify(cid, sizeof(cid),
1558 opt.cid_val, &cid_len) != 0) {
1559 mbedtls_printf("CID not valid\n");
1560 goto exit;
1561 }
1562
1563 /* Keep CID settings for renegotiation unless
1564 * specified otherwise. */
1565 if (opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO) {
1566 opt.cid_enabled_renego = opt.cid_enabled;
1567 }
1568 if (opt.cid_val_renego == DFL_CID_VALUE_RENEGO) {
1569 opt.cid_val_renego = opt.cid_val;
1570 }
1571
1572 if (mbedtls_test_unhexify(cid_renego, sizeof(cid_renego),
1573 opt.cid_val_renego, &cid_renego_len) != 0) {
1574 mbedtls_printf("CID not valid\n");
1575 goto exit;
1576 }
1577 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1578
1579 if (opt.groups != NULL) {
1580 if (parse_groups(opt.groups, group_list, GROUP_LIST_SIZE) != 0) {
1581 goto exit;
1582 }
1583 }
1584
1585 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1586 if (opt.sig_algs != NULL) {
1587 p = (char *) opt.sig_algs;
1588 i = 0;
1589
1590 /* Leave room for a final MBEDTLS_TLS1_3_SIG_NONE in signature algorithm list (sig_alg_list). */
1591 while (i < SIG_ALG_LIST_SIZE - 1 && *p != '\0') {
1592 q = p;
1593
1594 /* Terminate the current string */
1595 while (*p != ',' && *p != '\0') {
1596 p++;
1597 }
1598 if (*p == ',') {
1599 *p++ = '\0';
1600 }
1601
1602 if (strcmp(q, "rsa_pkcs1_sha256") == 0) {
1603 sig_alg_list[i++] = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256;
1604 } else if (strcmp(q, "rsa_pkcs1_sha384") == 0) {
1605 sig_alg_list[i++] = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384;
1606 } else if (strcmp(q, "rsa_pkcs1_sha512") == 0) {
1607 sig_alg_list[i++] = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512;
1608 } else if (strcmp(q, "ecdsa_secp256r1_sha256") == 0) {
1609 sig_alg_list[i++] = MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
1610 } else if (strcmp(q, "ecdsa_secp384r1_sha384") == 0) {
1611 sig_alg_list[i++] = MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
1612 } else if (strcmp(q, "ecdsa_secp521r1_sha512") == 0) {
1613 sig_alg_list[i++] = MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
1614 } else if (strcmp(q, "rsa_pss_rsae_sha256") == 0) {
1615 sig_alg_list[i++] = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256;
1616 } else if (strcmp(q, "rsa_pss_rsae_sha384") == 0) {
1617 sig_alg_list[i++] = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384;
1618 } else if (strcmp(q, "rsa_pss_rsae_sha512") == 0) {
1619 sig_alg_list[i++] = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512;
1620 } else if (strcmp(q, "ed25519") == 0) {
1621 sig_alg_list[i++] = MBEDTLS_TLS1_3_SIG_ED25519;
1622 } else if (strcmp(q, "ed448") == 0) {
1623 sig_alg_list[i++] = MBEDTLS_TLS1_3_SIG_ED448;
1624 } else if (strcmp(q, "rsa_pss_pss_sha256") == 0) {
1625 sig_alg_list[i++] = MBEDTLS_TLS1_3_SIG_RSA_PSS_PSS_SHA256;
1626 } else if (strcmp(q, "rsa_pss_pss_sha384") == 0) {
1627 sig_alg_list[i++] = MBEDTLS_TLS1_3_SIG_RSA_PSS_PSS_SHA384;
1628 } else if (strcmp(q, "rsa_pss_pss_sha512") == 0) {
1629 sig_alg_list[i++] = MBEDTLS_TLS1_3_SIG_RSA_PSS_PSS_SHA512;
1630 } else if (strcmp(q, "rsa_pkcs1_sha1") == 0) {
1631 sig_alg_list[i++] = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA1;
1632 } else if (strcmp(q, "ecdsa_sha1") == 0) {
1633 sig_alg_list[i++] = MBEDTLS_TLS1_3_SIG_ECDSA_SHA1;
1634 } else {
1635 ret = -1;
1636 mbedtls_printf("unknown signature algorithm \"%s\"\n", q);
1637 mbedtls_print_supported_sig_algs();
1638 goto exit;
1639 }
1640 }
1641
1642 if (i == (SIG_ALG_LIST_SIZE - 1) && *p != '\0') {
1643 mbedtls_printf("signature algorithm list too long, maximum %d",
1644 SIG_ALG_LIST_SIZE - 1);
1645 goto exit;
1646 }
1647
1648 sig_alg_list[i] = MBEDTLS_TLS1_3_SIG_NONE;
1649 }
1650 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
1651
1652 #if defined(MBEDTLS_SSL_ALPN)
1653 if (opt.alpn_string != NULL) {
1654 p = (char *) opt.alpn_string;
1655 i = 0;
1656
1657 /* Leave room for a final NULL in alpn_list */
1658 while (i < ALPN_LIST_SIZE - 1 && *p != '\0') {
1659 alpn_list[i++] = p;
1660
1661 /* Terminate the current string and move on to next one */
1662 while (*p != ',' && *p != '\0') {
1663 p++;
1664 }
1665 if (*p == ',') {
1666 *p++ = '\0';
1667 }
1668 }
1669 }
1670 #endif /* MBEDTLS_SSL_ALPN */
1671
1672 mbedtls_printf("build version: %s (build %d)\n",
1673 MBEDTLS_VERSION_STRING_FULL, MBEDTLS_VERSION_NUMBER);
1674
1675 /*
1676 * 0. Initialize the RNG and the session data
1677 */
1678 mbedtls_printf("\n . Seeding the random number generator...");
1679 fflush(stdout);
1680
1681 ret = rng_seed(&rng, opt.reproducible, pers);
1682 if (ret != 0) {
1683 goto exit;
1684 }
1685 mbedtls_printf(" ok\n");
1686
1687 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1688 /*
1689 * 1.1. Load the trusted CA
1690 */
1691 mbedtls_printf(" . Loading the CA root certificate ...");
1692 fflush(stdout);
1693
1694 if (strcmp(opt.ca_path, "none") == 0 ||
1695 strcmp(opt.ca_file, "none") == 0) {
1696 ret = 0;
1697 } else
1698 #if defined(MBEDTLS_FS_IO)
1699 if (strlen(opt.ca_path)) {
1700 ret = mbedtls_x509_crt_parse_path(&cacert, opt.ca_path);
1701 } else if (strlen(opt.ca_file)) {
1702 ret = mbedtls_x509_crt_parse_file(&cacert, opt.ca_file);
1703 } else
1704 #endif
1705 {
1706 #if defined(MBEDTLS_PEM_PARSE_C)
1707 for (i = 0; mbedtls_test_cas[i] != NULL; i++) {
1708 ret = mbedtls_x509_crt_parse(&cacert,
1709 (const unsigned char *) mbedtls_test_cas[i],
1710 mbedtls_test_cas_len[i]);
1711 if (ret != 0) {
1712 break;
1713 }
1714 }
1715 #endif /* MBEDTLS_PEM_PARSE_C */
1716 if (ret == 0) {
1717 for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) {
1718 ret = mbedtls_x509_crt_parse_der(&cacert,
1719 (const unsigned char *) mbedtls_test_cas_der[i],
1720 mbedtls_test_cas_der_len[i]);
1721 if (ret != 0) {
1722 break;
1723 }
1724 }
1725 }
1726 }
1727 if (ret < 0) {
1728 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1729 (unsigned int) -ret);
1730 goto exit;
1731 }
1732
1733 mbedtls_printf(" ok (%d skipped)\n", ret);
1734
1735 /*
1736 * 1.2. Load own certificate and private key
1737 *
1738 * (can be skipped if client authentication is not required)
1739 */
1740 mbedtls_printf(" . Loading the client cert. and key...");
1741 fflush(stdout);
1742
1743 if (strcmp(opt.crt_file, "none") == 0) {
1744 ret = 0;
1745 } else
1746 #if defined(MBEDTLS_FS_IO)
1747 if (strlen(opt.crt_file)) {
1748 ret = mbedtls_x509_crt_parse_file(&clicert, opt.crt_file);
1749 } else
1750 #endif
1751 { ret = mbedtls_x509_crt_parse(&clicert,
1752 (const unsigned char *) mbedtls_test_cli_crt,
1753 mbedtls_test_cli_crt_len); }
1754 if (ret != 0) {
1755 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1756 (unsigned int) -ret);
1757 goto exit;
1758 }
1759
1760 if (strcmp(opt.key_file, "none") == 0) {
1761 ret = 0;
1762 } else
1763 #if defined(MBEDTLS_FS_IO)
1764 if (strlen(opt.key_file)) {
1765 ret = mbedtls_pk_parse_keyfile(&pkey, opt.key_file, opt.key_pwd, rng_get, &rng);
1766 } else
1767 #endif
1768 { ret = mbedtls_pk_parse_key(&pkey,
1769 (const unsigned char *) mbedtls_test_cli_key,
1770 mbedtls_test_cli_key_len, NULL, 0, rng_get, &rng); }
1771 if (ret != 0) {
1772 mbedtls_printf(" failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
1773 (unsigned int) -ret);
1774 goto exit;
1775 }
1776
1777 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1778 if (opt.key_opaque != 0) {
1779 psa_algorithm_t psa_alg, psa_alg2 = PSA_ALG_NONE;
1780 psa_key_usage_t usage = 0;
1781
1782 if (key_opaque_set_alg_usage(opt.key_opaque_alg1,
1783 opt.key_opaque_alg2,
1784 &psa_alg, &psa_alg2,
1785 &usage,
1786 mbedtls_pk_get_type(&pkey)) == 0) {
1787 ret = pk_wrap_as_opaque(&pkey, psa_alg, psa_alg2, usage, &key_slot);
1788 if (ret != 0) {
1789 mbedtls_printf(" failed\n ! "
1790 "mbedtls_pk_get_psa_attributes returned -0x%x\n\n",
1791 (unsigned int) -ret);
1792 goto exit;
1793 }
1794 }
1795 }
1796 #endif /* MBEDTLS_USE_PSA_CRYPTO */
1797
1798 mbedtls_printf(" ok (key type: %s)\n",
1799 strlen(opt.key_file) || strlen(opt.key_opaque_alg1) ?
1800 mbedtls_pk_get_name(&pkey) : "none");
1801 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
1802
1803 /*
1804 * 2. Setup stuff
1805 */
1806 mbedtls_printf(" . Setting up the SSL/TLS structure...");
1807 fflush(stdout);
1808
1809 if ((ret = mbedtls_ssl_config_defaults(&conf,
1810 MBEDTLS_SSL_IS_CLIENT,
1811 opt.transport,
1812 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
1813 mbedtls_printf(" failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
1814 (unsigned int) -ret);
1815 goto exit;
1816 }
1817
1818 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1819 /* The default algorithms profile disables SHA-1, but our tests still
1820 rely on it heavily. */
1821 if (opt.allow_sha1 > 0) {
1822 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1);
1823 mbedtls_ssl_conf_cert_profile(&conf, &crt_profile_for_test);
1824 mbedtls_ssl_conf_sig_algs(&conf, ssl_sig_algs_for_test);
1825 }
1826 if (opt.context_crt_cb == 0) {
1827 mbedtls_ssl_conf_verify(&conf, my_verify, NULL);
1828 }
1829
1830 memset(peer_crt_info, 0, sizeof(peer_crt_info));
1831 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
1832
1833 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1834 if (opt.cid_enabled == 1 || opt.cid_enabled_renego == 1) {
1835 if (opt.cid_enabled == 1 &&
1836 opt.cid_enabled_renego == 1 &&
1837 cid_len != cid_renego_len) {
1838 mbedtls_printf("CID length must not change during renegotiation\n");
1839 goto usage;
1840 }
1841
1842 if (opt.cid_enabled == 1) {
1843 ret = mbedtls_ssl_conf_cid(&conf, cid_len,
1844 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE);
1845 } else {
1846 ret = mbedtls_ssl_conf_cid(&conf, cid_renego_len,
1847 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE);
1848 }
1849
1850 if (ret != 0) {
1851 mbedtls_printf(" failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
1852 (unsigned int) -ret);
1853 goto exit;
1854 }
1855 }
1856 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1857
1858 if (opt.auth_mode != DFL_AUTH_MODE) {
1859 mbedtls_ssl_conf_authmode(&conf, opt.auth_mode);
1860 }
1861
1862 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1863 if (opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX) {
1864 mbedtls_ssl_conf_handshake_timeout(&conf, opt.hs_to_min,
1865 opt.hs_to_max);
1866 }
1867
1868 if (opt.dgram_packing != DFL_DGRAM_PACKING) {
1869 mbedtls_ssl_set_datagram_packing(&ssl, opt.dgram_packing);
1870 }
1871 #endif /* MBEDTLS_SSL_PROTO_DTLS */
1872
1873 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1874 if ((ret = mbedtls_ssl_conf_max_frag_len(&conf, opt.mfl_code)) != 0) {
1875 mbedtls_printf(" failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n",
1876 ret);
1877 goto exit;
1878 }
1879 #endif
1880
1881 #if defined(MBEDTLS_SSL_DTLS_SRTP)
1882 const mbedtls_ssl_srtp_profile forced_profile[] =
1883 { opt.force_srtp_profile, MBEDTLS_TLS_SRTP_UNSET };
1884 if (opt.use_srtp == 1) {
1885 if (opt.force_srtp_profile != 0) {
1886 ret = mbedtls_ssl_conf_dtls_srtp_protection_profiles(&conf, forced_profile);
1887 } else {
1888 ret = mbedtls_ssl_conf_dtls_srtp_protection_profiles(&conf, default_profiles);
1889 }
1890
1891 if (ret != 0) {
1892 mbedtls_printf(" failed\n ! "
1893 "mbedtls_ssl_conf_dtls_srtp_protection_profiles returned %d\n\n",
1894 ret);
1895 goto exit;
1896 }
1897
1898 } else if (opt.force_srtp_profile != 0) {
1899 mbedtls_printf(" failed\n ! must enable use_srtp to force srtp profile\n\n");
1900 goto exit;
1901 }
1902 #endif /* MBEDTLS_SSL_DTLS_SRTP */
1903
1904 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1905 if (opt.extended_ms != DFL_EXTENDED_MS) {
1906 mbedtls_ssl_conf_extended_master_secret(&conf, opt.extended_ms);
1907 }
1908 #endif
1909
1910 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1911 if (opt.etm != DFL_ETM) {
1912 mbedtls_ssl_conf_encrypt_then_mac(&conf, opt.etm);
1913 }
1914 #endif
1915
1916 #if defined(MBEDTLS_DHM_C)
1917 if (opt.dhmlen != DFL_DHMLEN) {
1918 mbedtls_ssl_conf_dhm_min_bitlen(&conf, opt.dhmlen);
1919 }
1920 #endif
1921
1922 #if defined(MBEDTLS_SSL_ALPN)
1923 if (opt.alpn_string != NULL) {
1924 if ((ret = mbedtls_ssl_conf_alpn_protocols(&conf, alpn_list)) != 0) {
1925 mbedtls_printf(" failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
1926 ret);
1927 goto exit;
1928 }
1929 }
1930 #endif
1931
1932 if (opt.reproducible) {
1933 #if defined(MBEDTLS_HAVE_TIME)
1934 #if defined(MBEDTLS_PLATFORM_TIME_ALT)
1935 mbedtls_platform_set_time(dummy_constant_time);
1936 #else
1937 fprintf(stderr, "Warning: reproducible option used without constant time\n");
1938 #endif
1939 #endif /* MBEDTLS_HAVE_TIME */
1940 }
1941 mbedtls_ssl_conf_rng(&conf, rng_get, &rng);
1942 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
1943
1944 mbedtls_ssl_conf_read_timeout(&conf, opt.read_timeout);
1945
1946 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
1947 mbedtls_ssl_conf_session_tickets(&conf, opt.tickets);
1948 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1949 mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets(
1950 &conf, opt.new_session_tickets);
1951 #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1952 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
1953
1954 if (opt.force_ciphersuite[0] != DFL_FORCE_CIPHER) {
1955 mbedtls_ssl_conf_ciphersuites(&conf, opt.force_ciphersuite);
1956 }
1957
1958 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1959 mbedtls_ssl_conf_tls13_key_exchange_modes(&conf, opt.tls13_kex_modes);
1960 #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1961
1962 if (opt.allow_legacy != DFL_ALLOW_LEGACY) {
1963 mbedtls_ssl_conf_legacy_renegotiation(&conf, opt.allow_legacy);
1964 }
1965 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1966 mbedtls_ssl_conf_renegotiation(&conf, opt.renegotiation);
1967 #endif
1968
1969 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1970 if (strcmp(opt.ca_path, "none") != 0 &&
1971 strcmp(opt.ca_file, "none") != 0) {
1972 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1973 if (opt.ca_callback != 0) {
1974 mbedtls_ssl_conf_ca_cb(&conf, ca_callback, &cacert);
1975 } else
1976 #endif
1977 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
1978 }
1979 if (strcmp(opt.crt_file, "none") != 0 &&
1980 strcmp(opt.key_file, "none") != 0) {
1981 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &clicert, &pkey)) != 0) {
1982 mbedtls_printf(" failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
1983 ret);
1984 goto exit;
1985 }
1986 }
1987 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
1988
1989 #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) || \
1990 (defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \
1991 defined(PSA_WANT_ALG_FFDH))
1992 if (opt.groups != NULL &&
1993 strcmp(opt.groups, "default") != 0) {
1994 mbedtls_ssl_conf_groups(&conf, group_list);
1995 }
1996 #endif
1997
1998 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1999 if (opt.sig_algs != NULL) {
2000 mbedtls_ssl_conf_sig_algs(&conf, sig_alg_list);
2001 }
2002 #endif
2003
2004 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
2005 #if defined(MBEDTLS_USE_PSA_CRYPTO)
2006 if (opt.psk_opaque != 0) {
2007 key_attributes = psa_key_attributes_init();
2008 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2009 psa_set_key_algorithm(&key_attributes, alg);
2010 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
2011
2012 status = psa_import_key(&key_attributes, psk, psk_len, &slot);
2013 if (status != PSA_SUCCESS) {
2014 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2015 goto exit;
2016 }
2017
2018 if ((ret = mbedtls_ssl_conf_psk_opaque(&conf, slot,
2019 (const unsigned char *) opt.psk_identity,
2020 strlen(opt.psk_identity))) != 0) {
2021 mbedtls_printf(" failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
2022 ret);
2023 goto exit;
2024 }
2025 } else
2026 #endif /* MBEDTLS_USE_PSA_CRYPTO */
2027 if (psk_len > 0) {
2028 ret = mbedtls_ssl_conf_psk(&conf, psk, psk_len,
2029 (const unsigned char *) opt.psk_identity,
2030 strlen(opt.psk_identity));
2031 if (ret != 0) {
2032 mbedtls_printf(" failed\n ! mbedtls_ssl_conf_psk returned %d\n\n", ret);
2033 goto exit;
2034 }
2035 }
2036 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
2037
2038 if (opt.min_version != DFL_MIN_VERSION) {
2039 mbedtls_ssl_conf_min_tls_version(&conf, opt.min_version);
2040 }
2041
2042 if (opt.max_version != DFL_MAX_VERSION) {
2043 mbedtls_ssl_conf_max_tls_version(&conf, opt.max_version);
2044 }
2045
2046 #if defined(MBEDTLS_SSL_EARLY_DATA)
2047 if (opt.early_data != DFL_EARLY_DATA) {
2048 mbedtls_ssl_conf_early_data(&conf, opt.early_data);
2049 }
2050 #endif /* MBEDTLS_SSL_EARLY_DATA */
2051
2052 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
2053 mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
2054 (unsigned int) -ret);
2055 goto exit;
2056 }
2057
2058 if (opt.eap_tls != 0) {
2059 mbedtls_ssl_set_export_keys_cb(&ssl, eap_tls_key_derivation,
2060 &eap_tls_keying);
2061 } else if (opt.nss_keylog != 0) {
2062 mbedtls_ssl_set_export_keys_cb(&ssl,
2063 nss_keylog_export,
2064 NULL);
2065 }
2066 #if defined(MBEDTLS_SSL_DTLS_SRTP)
2067 else if (opt.use_srtp != 0) {
2068 mbedtls_ssl_set_export_keys_cb(&ssl, dtls_srtp_key_derivation,
2069 &dtls_srtp_keying);
2070 }
2071 #endif /* MBEDTLS_SSL_DTLS_SRTP */
2072
2073 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2074 if ((ret = mbedtls_ssl_set_hostname(&ssl, opt.server_name)) != 0) {
2075 mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
2076 ret);
2077 goto exit;
2078 }
2079 #endif
2080
2081 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2082 if (opt.ecjpake_pw != DFL_ECJPAKE_PW) {
2083 #if defined(MBEDTLS_USE_PSA_CRYPTO)
2084 if (opt.ecjpake_pw_opaque != DFL_ECJPAKE_PW_OPAQUE) {
2085 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2086
2087 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
2088 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
2089 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
2090
2091 status = psa_import_key(&attributes,
2092 (const unsigned char *) opt.ecjpake_pw,
2093 strlen(opt.ecjpake_pw),
2094 &ecjpake_pw_slot);
2095 if (status != PSA_SUCCESS) {
2096 mbedtls_printf(" failed\n ! psa_import_key returned %d\n\n",
2097 status);
2098 goto exit;
2099 }
2100 if ((ret = mbedtls_ssl_set_hs_ecjpake_password_opaque(&ssl,
2101 ecjpake_pw_slot)) != 0) {
2102 mbedtls_printf(
2103 " failed\n ! mbedtls_ssl_set_hs_ecjpake_password_opaque returned %d\n\n",
2104 ret);
2105 goto exit;
2106 }
2107 mbedtls_printf("using opaque password\n");
2108 } else
2109 #endif /* MBEDTLS_USE_PSA_CRYPTO */
2110 {
2111 if ((ret = mbedtls_ssl_set_hs_ecjpake_password(&ssl,
2112 (const unsigned char *) opt.ecjpake_pw,
2113 strlen(opt.ecjpake_pw))) != 0) {
2114 mbedtls_printf(" failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
2115 ret);
2116 goto exit;
2117 }
2118 }
2119 }
2120 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2121
2122 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2123 if (opt.context_crt_cb == 1) {
2124 mbedtls_ssl_set_verify(&ssl, my_verify, NULL);
2125 }
2126 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2127
2128 io_ctx.ssl = &ssl;
2129 io_ctx.net = &server_fd;
2130 mbedtls_ssl_set_bio(&ssl, &io_ctx, send_cb, recv_cb,
2131 opt.nbio == 0 ? recv_timeout_cb : NULL);
2132
2133 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2134 if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2135 if ((ret = mbedtls_ssl_set_cid(&ssl, opt.cid_enabled,
2136 cid, cid_len)) != 0) {
2137 mbedtls_printf(" failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2138 ret);
2139 goto exit;
2140 }
2141 }
2142 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
2143
2144 #if defined(MBEDTLS_SSL_PROTO_DTLS)
2145 if (opt.dtls_mtu != DFL_DTLS_MTU) {
2146 mbedtls_ssl_set_mtu(&ssl, opt.dtls_mtu);
2147 }
2148 #endif
2149
2150 #if defined(MBEDTLS_TIMING_C)
2151 mbedtls_ssl_set_timer_cb(&ssl, &timer, mbedtls_timing_set_delay,
2152 mbedtls_timing_get_delay);
2153 #endif
2154
2155 #if defined(MBEDTLS_ECP_RESTARTABLE)
2156 if (opt.ec_max_ops != DFL_EC_MAX_OPS) {
2157 mbedtls_ecp_set_max_ops(opt.ec_max_ops);
2158 }
2159 #endif
2160
2161 #if defined(MBEDTLS_SSL_DTLS_SRTP)
2162 if (opt.use_srtp != 0 && strlen(opt.mki) != 0) {
2163 if (mbedtls_test_unhexify(mki, sizeof(mki),
2164 opt.mki, &mki_len) != 0) {
2165 mbedtls_printf("mki value not valid hex\n");
2166 goto exit;
2167 }
2168
2169 mbedtls_ssl_conf_srtp_mki_value_supported(&conf, MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED);
2170 if ((ret = mbedtls_ssl_dtls_srtp_set_mki_value(&ssl, mki,
2171 (uint16_t) strlen(opt.mki) / 2)) != 0) {
2172 mbedtls_printf(" failed\n ! mbedtls_ssl_dtls_srtp_set_mki_value returned %d\n\n", ret);
2173 goto exit;
2174 }
2175 }
2176 #endif
2177
2178 mbedtls_printf(" ok\n");
2179
2180 /*
2181 * 3. Start the connection
2182 */
2183 if (opt.server_addr == NULL) {
2184 opt.server_addr = opt.server_name;
2185 }
2186
2187 mbedtls_printf(" . Connecting to %s/%s/%s...",
2188 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
2189 opt.server_addr, opt.server_port);
2190 fflush(stdout);
2191
2192 if ((ret = mbedtls_net_connect(&server_fd,
2193 opt.server_addr, opt.server_port,
2194 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2195 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP)) != 0) {
2196 mbedtls_printf(" failed\n ! mbedtls_net_connect returned -0x%x\n\n",
2197 (unsigned int) -ret);
2198 goto exit;
2199 }
2200
2201 if (opt.nbio > 0) {
2202 ret = mbedtls_net_set_nonblock(&server_fd);
2203 } else {
2204 ret = mbedtls_net_set_block(&server_fd);
2205 }
2206 if (ret != 0) {
2207 mbedtls_printf(" failed\n ! net_set_(non)block() returned -0x%x\n\n",
2208 (unsigned int) -ret);
2209 goto exit;
2210 }
2211
2212 mbedtls_printf(" ok\n");
2213
2214 /*
2215 * 4. Handshake
2216 */
2217 mbedtls_printf(" . Performing the SSL/TLS handshake...");
2218 fflush(stdout);
2219
2220 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
2221 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
2222 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
2223 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
2224 mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
2225 (unsigned int) -ret);
2226 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2227 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
2228 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE) {
2229 mbedtls_printf(
2230 " Unable to verify the server's certificate. "
2231 "Either it is invalid,\n"
2232 " or you didn't set ca_file or ca_path "
2233 "to an appropriate value.\n"
2234 " Alternatively, you may want to use "
2235 "auth_mode=optional for testing purposes if "
2236 "not using TLS 1.3.\n"
2237 " For TLS 1.3 server, try `ca_path=/etc/ssl/certs/`"
2238 "or other folder that has root certificates\n");
2239
2240 flags = mbedtls_ssl_get_verify_result(&ssl);
2241 char vrfy_buf[512];
2242 x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
2243 mbedtls_printf("%s\n", vrfy_buf);
2244 }
2245 #endif
2246 mbedtls_printf("\n");
2247 goto exit;
2248 }
2249
2250 #if defined(MBEDTLS_ECP_RESTARTABLE)
2251 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
2252 continue;
2253 }
2254 #endif
2255
2256 /* For event-driven IO, wait for socket to become available */
2257 if (opt.event == 1 /* level triggered IO */) {
2258 #if defined(MBEDTLS_TIMING_C)
2259 ret = idle(&server_fd, &timer, ret);
2260 #else
2261 ret = idle(&server_fd, ret);
2262 #endif
2263 if (ret != 0) {
2264 goto exit;
2265 }
2266 }
2267 }
2268
2269 {
2270 int suite_id = mbedtls_ssl_get_ciphersuite_id_from_ssl(&ssl);
2271 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2272 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(suite_id);
2273
2274 mbedtls_printf(" ok\n [ Protocol is %s ]\n"
2275 " [ Ciphersuite is %s ]\n"
2276 " [ Key size is %u ]\n",
2277 mbedtls_ssl_get_version(&ssl),
2278 mbedtls_ssl_ciphersuite_get_name(ciphersuite_info),
2279 (unsigned int)
2280 mbedtls_ssl_ciphersuite_get_cipher_key_bitlen(ciphersuite_info));
2281 }
2282
2283 if ((ret = mbedtls_ssl_get_record_expansion(&ssl)) >= 0) {
2284 mbedtls_printf(" [ Record expansion is %d ]\n", ret);
2285 } else {
2286 mbedtls_printf(" [ Record expansion is unknown ]\n");
2287 }
2288
2289 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2290 mbedtls_printf(" [ Maximum incoming record payload length is %u ]\n",
2291 (unsigned int) mbedtls_ssl_get_max_in_record_payload(&ssl));
2292 mbedtls_printf(" [ Maximum outgoing record payload length is %u ]\n",
2293 (unsigned int) mbedtls_ssl_get_max_out_record_payload(&ssl));
2294 #endif
2295
2296 #if defined(MBEDTLS_SSL_ALPN)
2297 if (opt.alpn_string != NULL) {
2298 const char *alp = mbedtls_ssl_get_alpn_protocol(&ssl);
2299 mbedtls_printf(" [ Application Layer Protocol is %s ]\n",
2300 alp ? alp : "(none)");
2301 }
2302 #endif
2303
2304 if (opt.eap_tls != 0) {
2305 size_t j = 0;
2306
2307 if ((ret = mbedtls_ssl_tls_prf(eap_tls_keying.tls_prf_type,
2308 eap_tls_keying.master_secret,
2309 sizeof(eap_tls_keying.master_secret),
2310 eap_tls_label,
2311 eap_tls_keying.randbytes,
2312 sizeof(eap_tls_keying.randbytes),
2313 eap_tls_keymaterial,
2314 sizeof(eap_tls_keymaterial)))
2315 != 0) {
2316 mbedtls_printf(" failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2317 (unsigned int) -ret);
2318 goto exit;
2319 }
2320
2321 mbedtls_printf(" EAP-TLS key material is:");
2322 for (j = 0; j < sizeof(eap_tls_keymaterial); j++) {
2323 if (j % 8 == 0) {
2324 mbedtls_printf("\n ");
2325 }
2326 mbedtls_printf("%02x ", eap_tls_keymaterial[j]);
2327 }
2328 mbedtls_printf("\n");
2329
2330 if ((ret = mbedtls_ssl_tls_prf(eap_tls_keying.tls_prf_type, NULL, 0,
2331 eap_tls_label,
2332 eap_tls_keying.randbytes,
2333 sizeof(eap_tls_keying.randbytes),
2334 eap_tls_iv,
2335 sizeof(eap_tls_iv))) != 0) {
2336 mbedtls_printf(" failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2337 (unsigned int) -ret);
2338 goto exit;
2339 }
2340
2341 mbedtls_printf(" EAP-TLS IV is:");
2342 for (j = 0; j < sizeof(eap_tls_iv); j++) {
2343 if (j % 8 == 0) {
2344 mbedtls_printf("\n ");
2345 }
2346 mbedtls_printf("%02x ", eap_tls_iv[j]);
2347 }
2348 mbedtls_printf("\n");
2349 }
2350
2351 #if defined(MBEDTLS_SSL_DTLS_SRTP)
2352 else if (opt.use_srtp != 0) {
2353 size_t j = 0;
2354 mbedtls_dtls_srtp_info dtls_srtp_negotiation_result;
2355 mbedtls_ssl_get_dtls_srtp_negotiation_result(&ssl, &dtls_srtp_negotiation_result);
2356
2357 if (dtls_srtp_negotiation_result.chosen_dtls_srtp_profile
2358 == MBEDTLS_TLS_SRTP_UNSET) {
2359 mbedtls_printf(" Unable to negotiate "
2360 "the use of DTLS-SRTP\n");
2361 } else {
2362 if ((ret = mbedtls_ssl_tls_prf(dtls_srtp_keying.tls_prf_type,
2363 dtls_srtp_keying.master_secret,
2364 sizeof(dtls_srtp_keying.master_secret),
2365 dtls_srtp_label,
2366 dtls_srtp_keying.randbytes,
2367 sizeof(dtls_srtp_keying.randbytes),
2368 dtls_srtp_key_material,
2369 sizeof(dtls_srtp_key_material)))
2370 != 0) {
2371 mbedtls_printf(" failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2372 (unsigned int) -ret);
2373 goto exit;
2374 }
2375
2376 mbedtls_printf(" DTLS-SRTP key material is:");
2377 for (j = 0; j < sizeof(dtls_srtp_key_material); j++) {
2378 if (j % 8 == 0) {
2379 mbedtls_printf("\n ");
2380 }
2381 mbedtls_printf("%02x ", dtls_srtp_key_material[j]);
2382 }
2383 mbedtls_printf("\n");
2384
2385 /* produce a less readable output used to perform automatic checks
2386 * - compare client and server output
2387 * - interop test with openssl which client produces this kind of output
2388 */
2389 mbedtls_printf(" Keying material: ");
2390 for (j = 0; j < sizeof(dtls_srtp_key_material); j++) {
2391 mbedtls_printf("%02X", dtls_srtp_key_material[j]);
2392 }
2393 mbedtls_printf("\n");
2394
2395 if (dtls_srtp_negotiation_result.mki_len > 0) {
2396 mbedtls_printf(" DTLS-SRTP mki value: ");
2397 for (j = 0; j < dtls_srtp_negotiation_result.mki_len; j++) {
2398 mbedtls_printf("%02X", dtls_srtp_negotiation_result.mki_value[j]);
2399 }
2400 } else {
2401 mbedtls_printf(" DTLS-SRTP no mki value negotiated");
2402 }
2403 mbedtls_printf("\n");
2404 }
2405 }
2406 #endif /* MBEDTLS_SSL_DTLS_SRTP */
2407 if (opt.reconnect != 0 && ssl.tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
2408 mbedtls_printf(" . Saving session for reuse...");
2409 fflush(stdout);
2410
2411 if (opt.reco_mode == 1) {
2412 if ((ret = ssl_save_session_serialize(&ssl,
2413 &session_data, &session_data_len)) != 0) {
2414 mbedtls_printf(" failed\n ! ssl_save_session_serialize returned -0x%04x\n\n",
2415 (unsigned int) -ret);
2416 goto exit;
2417 }
2418
2419 } else {
2420 if ((ret = mbedtls_ssl_get_session(&ssl, &saved_session)) != 0) {
2421 mbedtls_printf(" failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
2422 (unsigned int) -ret);
2423 goto exit;
2424 }
2425 }
2426
2427 mbedtls_printf(" ok\n");
2428
2429 if (opt.reco_mode == 1) {
2430 mbedtls_printf(" [ Saved %u bytes of session data]\n",
2431 (unsigned) session_data_len);
2432 }
2433 }
2434
2435 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2436 /*
2437 * 5. Verify the server certificate
2438 */
2439 mbedtls_printf(" . Verifying peer X.509 certificate...");
2440
2441 if ((flags = mbedtls_ssl_get_verify_result(&ssl)) != 0) {
2442 char vrfy_buf[512];
2443 mbedtls_printf(" failed\n");
2444
2445 x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf),
2446 " ! ", flags);
2447
2448 mbedtls_printf("%s\n", vrfy_buf);
2449 } else {
2450 mbedtls_printf(" ok\n");
2451 }
2452
2453 #if !defined(MBEDTLS_X509_REMOVE_INFO)
2454 mbedtls_printf(" . Peer certificate information ...\n");
2455 mbedtls_printf("%s\n", peer_crt_info);
2456 #endif /* !MBEDTLS_X509_REMOVE_INFO */
2457 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2458
2459 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2460 ret = report_cid_usage(&ssl, "initial handshake");
2461 if (ret != 0) {
2462 goto exit;
2463 }
2464
2465 if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2466 if ((ret = mbedtls_ssl_set_cid(&ssl, opt.cid_enabled_renego,
2467 cid_renego,
2468 cid_renego_len)) != 0) {
2469 mbedtls_printf(" failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2470 ret);
2471 goto exit;
2472 }
2473 }
2474 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
2475
2476 #if defined(MBEDTLS_SSL_RENEGOTIATION)
2477 if (opt.renegotiate) {
2478 /*
2479 * Perform renegotiation (this must be done when the server is waiting
2480 * for input from our side).
2481 */
2482 mbedtls_printf(" . Performing renegotiation...");
2483 fflush(stdout);
2484 while ((ret = mbedtls_ssl_renegotiate(&ssl)) != 0) {
2485 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
2486 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
2487 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
2488 mbedtls_printf(" failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
2489 ret);
2490 goto exit;
2491 }
2492
2493 #if defined(MBEDTLS_ECP_RESTARTABLE)
2494 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
2495 continue;
2496 }
2497 #endif
2498
2499 /* For event-driven IO, wait for socket to become available */
2500 if (opt.event == 1 /* level triggered IO */) {
2501 #if defined(MBEDTLS_TIMING_C)
2502 idle(&server_fd, &timer, ret);
2503 #else
2504 idle(&server_fd, ret);
2505 #endif
2506 }
2507
2508 }
2509 mbedtls_printf(" ok\n");
2510 }
2511 #endif /* MBEDTLS_SSL_RENEGOTIATION */
2512
2513 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2514 ret = report_cid_usage(&ssl, "after renegotiation");
2515 if (ret != 0) {
2516 goto exit;
2517 }
2518 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
2519
2520 /*
2521 * 6. Write the GET request
2522 */
2523 retry_left = opt.max_resend;
2524 send_request:
2525 mbedtls_printf(" > Write to server:");
2526 fflush(stdout);
2527
2528 ret = build_http_request(buf, sizeof(buf) - 1, &len);
2529 if (ret != 0) {
2530 goto exit;
2531 }
2532
2533 if (opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM) {
2534 written = 0;
2535 frags = 0;
2536
2537 do {
2538 while ((ret = mbedtls_ssl_write(&ssl, buf + written,
2539 len - written)) < 0) {
2540 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
2541 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
2542 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
2543 mbedtls_printf(" failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
2544 (unsigned int) -ret);
2545 goto exit;
2546 }
2547
2548 /* For event-driven IO, wait for socket to become available */
2549 if (opt.event == 1 /* level triggered IO */) {
2550 #if defined(MBEDTLS_TIMING_C)
2551 idle(&server_fd, &timer, ret);
2552 #else
2553 idle(&server_fd, ret);
2554 #endif
2555 }
2556 }
2557
2558 frags++;
2559 written += ret;
2560 } while (written < len);
2561 } else { /* Not stream, so datagram */
2562 while (1) {
2563 ret = mbedtls_ssl_write(&ssl, buf, len);
2564
2565 #if defined(MBEDTLS_ECP_RESTARTABLE)
2566 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
2567 continue;
2568 }
2569 #endif
2570
2571 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
2572 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
2573 break;
2574 }
2575
2576 /* For event-driven IO, wait for socket to become available */
2577 if (opt.event == 1 /* level triggered IO */) {
2578 #if defined(MBEDTLS_TIMING_C)
2579 idle(&server_fd, &timer, ret);
2580 #else
2581 idle(&server_fd, ret);
2582 #endif
2583 }
2584 }
2585
2586 if (ret < 0) {
2587 mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n",
2588 ret);
2589 goto exit;
2590 }
2591
2592 frags = 1;
2593 written = ret;
2594
2595 if (written < len) {
2596 mbedtls_printf(" warning\n ! request didn't fit into single datagram and "
2597 "was truncated to size %u", (unsigned) written);
2598 }
2599 }
2600
2601 buf[written] = '\0';
2602 mbedtls_printf(
2603 " %" MBEDTLS_PRINTF_SIZET " bytes written in %" MBEDTLS_PRINTF_SIZET " fragments\n\n%s\n",
2604 written,
2605 frags,
2606 (char *) buf);
2607
2608 /* Send a non-empty request if request_size == 0 */
2609 if (len == 0) {
2610 opt.request_size = DFL_REQUEST_SIZE;
2611 goto send_request;
2612 }
2613
2614 /*
2615 * 7. Read the HTTP response
2616 */
2617
2618 /*
2619 * TLS and DTLS need different reading styles (stream vs datagram)
2620 */
2621 if (opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM) {
2622 #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
2623 int ticket_id = 0;
2624 #endif
2625 do {
2626 len = sizeof(buf) - 1;
2627 memset(buf, 0, sizeof(buf));
2628 ret = mbedtls_ssl_read(&ssl, buf, len);
2629
2630 #if defined(MBEDTLS_ECP_RESTARTABLE)
2631 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
2632 continue;
2633 }
2634 #endif
2635
2636 if (ret == MBEDTLS_ERR_SSL_WANT_READ ||
2637 ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
2638 /* For event-driven IO, wait for socket to become available */
2639 if (opt.event == 1 /* level triggered IO */) {
2640 #if defined(MBEDTLS_TIMING_C)
2641 idle(&server_fd, &timer, ret);
2642 #else
2643 idle(&server_fd, ret);
2644 #endif
2645 }
2646 continue;
2647 }
2648
2649 if (ret <= 0) {
2650 switch (ret) {
2651 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2652 mbedtls_printf(" connection was closed gracefully\n");
2653 ret = 0;
2654 goto close_notify;
2655
2656 case 0:
2657 case MBEDTLS_ERR_NET_CONN_RESET:
2658 mbedtls_printf(" connection was reset by peer\n");
2659 ret = 0;
2660 goto reconnect;
2661
2662 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
2663
2664 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
2665 case MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET:
2666 /* We were waiting for application data but got
2667 * a NewSessionTicket instead. */
2668 mbedtls_printf(" got new session ticket ( %d ).\n",
2669 ticket_id++);
2670 if (opt.reconnect != 0) {
2671 mbedtls_printf(" . Saving session for reuse...");
2672 fflush(stdout);
2673
2674 if (opt.reco_mode == 1) {
2675 if ((ret = ssl_save_session_serialize(&ssl,
2676 &session_data,
2677 &session_data_len)) != 0) {
2678 mbedtls_printf(
2679 " failed\n ! ssl_save_session_serialize returned -0x%04x\n\n",
2680 (unsigned int) -ret);
2681 goto exit;
2682 }
2683 } else {
2684 if ((ret = mbedtls_ssl_get_session(&ssl, &saved_session)) != 0) {
2685 mbedtls_printf(
2686 " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
2687 (unsigned int) -ret);
2688 goto exit;
2689 }
2690 }
2691
2692 mbedtls_printf(" ok\n");
2693
2694 if (opt.reco_mode == 1) {
2695 mbedtls_printf(" [ Saved %u bytes of session data]\n",
2696 (unsigned) session_data_len);
2697 }
2698 }
2699 continue;
2700 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
2701
2702 #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2703
2704 default:
2705 mbedtls_printf(" mbedtls_ssl_read returned -0x%x\n",
2706 (unsigned int) -ret);
2707 goto exit;
2708 }
2709 }
2710
2711 len = ret;
2712 buf[len] = '\0';
2713 mbedtls_printf(" < Read from server: %" MBEDTLS_PRINTF_SIZET " bytes read\n\n%s",
2714 len,
2715 (char *) buf);
2716 fflush(stdout);
2717 /* End of message should be detected according to the syntax of the
2718 * application protocol (eg HTTP), just use a dummy test here. */
2719 if (ret > 0 && buf[len-1] == '\n') {
2720 ret = 0;
2721 break;
2722 }
2723 } while (1);
2724 } else { /* Not stream, so datagram */
2725 len = sizeof(buf) - 1;
2726 memset(buf, 0, sizeof(buf));
2727
2728 while (1) {
2729 ret = mbedtls_ssl_read(&ssl, buf, len);
2730
2731 #if defined(MBEDTLS_ECP_RESTARTABLE)
2732 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
2733 continue;
2734 }
2735 #endif
2736
2737 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
2738 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
2739 break;
2740 }
2741
2742 /* For event-driven IO, wait for socket to become available */
2743 if (opt.event == 1 /* level triggered IO */) {
2744 #if defined(MBEDTLS_TIMING_C)
2745 idle(&server_fd, &timer, ret);
2746 #else
2747 idle(&server_fd, ret);
2748 #endif
2749 }
2750 }
2751
2752 if (ret <= 0) {
2753 switch (ret) {
2754 case MBEDTLS_ERR_SSL_TIMEOUT:
2755 mbedtls_printf(" timeout\n");
2756 if (retry_left-- > 0) {
2757 goto send_request;
2758 }
2759 goto exit;
2760
2761 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2762 mbedtls_printf(" connection was closed gracefully\n");
2763 ret = 0;
2764 goto close_notify;
2765
2766 default:
2767 mbedtls_printf(" mbedtls_ssl_read returned -0x%x\n", (unsigned int) -ret);
2768 goto exit;
2769 }
2770 }
2771
2772 len = ret;
2773 buf[len] = '\0';
2774 mbedtls_printf(" < Read from server: %" MBEDTLS_PRINTF_SIZET " bytes read\n\n%s",
2775 len,
2776 (char *) buf);
2777 ret = 0;
2778 }
2779
2780 /*
2781 * 7b. Simulate hard reset and reconnect from same port?
2782 */
2783 if (opt.reconnect_hard != 0) {
2784 opt.reconnect_hard = 0;
2785
2786 mbedtls_printf(" . Restarting connection from same port...");
2787 fflush(stdout);
2788
2789 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2790 memset(peer_crt_info, 0, sizeof(peer_crt_info));
2791 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2792
2793 if ((ret = mbedtls_ssl_session_reset(&ssl)) != 0) {
2794 mbedtls_printf(" failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2795 (unsigned int) -ret);
2796 goto exit;
2797 }
2798
2799 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
2800 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
2801 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
2802 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
2803 mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2804 (unsigned int) -ret);
2805 goto exit;
2806 }
2807
2808 /* For event-driven IO, wait for socket to become available */
2809 if (opt.event == 1 /* level triggered IO */) {
2810 #if defined(MBEDTLS_TIMING_C)
2811 idle(&server_fd, &timer, ret);
2812 #else
2813 idle(&server_fd, ret);
2814 #endif
2815 }
2816 }
2817
2818 mbedtls_printf(" ok\n");
2819
2820 goto send_request;
2821 }
2822
2823 /*
2824 * 7c. Simulate serialize/deserialize and go back to data exchange
2825 */
2826 #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2827 if (opt.serialize != 0) {
2828 size_t buf_len;
2829
2830 mbedtls_printf(" . Serializing live connection...");
2831
2832 ret = mbedtls_ssl_context_save(&ssl, NULL, 0, &buf_len);
2833 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
2834 mbedtls_printf(" failed\n ! mbedtls_ssl_context_save returned "
2835 "-0x%x\n\n", (unsigned int) -ret);
2836
2837 goto exit;
2838 }
2839
2840 if ((context_buf = mbedtls_calloc(1, buf_len)) == NULL) {
2841 mbedtls_printf(" failed\n ! Couldn't allocate buffer for "
2842 "serialized context");
2843
2844 goto exit;
2845 }
2846 context_buf_len = buf_len;
2847
2848 if ((ret = mbedtls_ssl_context_save(&ssl, context_buf,
2849 buf_len, &buf_len)) != 0) {
2850 mbedtls_printf(" failed\n ! mbedtls_ssl_context_save returned "
2851 "-0x%x\n\n", (unsigned int) -ret);
2852
2853 goto exit;
2854 }
2855
2856 mbedtls_printf(" ok\n");
2857
2858 /* Save serialized context to the 'opt.context_file' as a base64 code */
2859 if (0 < strlen(opt.context_file)) {
2860 FILE *b64_file;
2861 uint8_t *b64_buf;
2862 size_t b64_len;
2863
2864 mbedtls_printf(" . Save serialized context to a file... ");
2865
2866 mbedtls_base64_encode(NULL, 0, &b64_len, context_buf, buf_len);
2867
2868 if ((b64_buf = mbedtls_calloc(1, b64_len)) == NULL) {
2869 mbedtls_printf("failed\n ! Couldn't allocate buffer for "
2870 "the base64 code\n");
2871 goto exit;
2872 }
2873
2874 if ((ret = mbedtls_base64_encode(b64_buf, b64_len, &b64_len,
2875 context_buf, buf_len)) != 0) {
2876 mbedtls_printf("failed\n ! mbedtls_base64_encode returned "
2877 "-0x%x\n", (unsigned int) -ret);
2878 mbedtls_free(b64_buf);
2879 goto exit;
2880 }
2881
2882 if ((b64_file = fopen(opt.context_file, "w")) == NULL) {
2883 mbedtls_printf("failed\n ! Cannot open '%s' for writing.\n",
2884 opt.context_file);
2885 mbedtls_free(b64_buf);
2886 goto exit;
2887 }
2888
2889 if (b64_len != fwrite(b64_buf, 1, b64_len, b64_file)) {
2890 mbedtls_printf("failed\n ! fwrite(%ld bytes) failed\n",
2891 (long) b64_len);
2892 mbedtls_free(b64_buf);
2893 fclose(b64_file);
2894 goto exit;
2895 }
2896
2897 mbedtls_free(b64_buf);
2898 fclose(b64_file);
2899
2900 mbedtls_printf("ok\n");
2901 }
2902
2903 if (opt.serialize == 1) {
2904 /* nothing to do here, done by context_save() already */
2905 mbedtls_printf(" . Context has been reset... ok\n");
2906 }
2907
2908 if (opt.serialize == 2) {
2909 mbedtls_printf(" . Freeing and reinitializing context...");
2910
2911 mbedtls_ssl_free(&ssl);
2912
2913 mbedtls_ssl_init(&ssl);
2914
2915 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
2916 mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned "
2917 "-0x%x\n\n", (unsigned int) -ret);
2918 goto exit;
2919 }
2920
2921 if (opt.nbio == 2) {
2922 mbedtls_ssl_set_bio(&ssl, &server_fd, delayed_send,
2923 delayed_recv, NULL);
2924 } else {
2925 mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send,
2926 mbedtls_net_recv,
2927 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL);
2928 }
2929
2930 #if defined(MBEDTLS_TIMING_C)
2931 mbedtls_ssl_set_timer_cb(&ssl, &timer,
2932 mbedtls_timing_set_delay,
2933 mbedtls_timing_get_delay);
2934 #endif /* MBEDTLS_TIMING_C */
2935
2936 mbedtls_printf(" ok\n");
2937 }
2938
2939 mbedtls_printf(" . Deserializing connection...");
2940
2941 if ((ret = mbedtls_ssl_context_load(&ssl, context_buf,
2942 buf_len)) != 0) {
2943 mbedtls_printf("failed\n ! mbedtls_ssl_context_load returned "
2944 "-0x%x\n\n", (unsigned int) -ret);
2945
2946 goto exit;
2947 }
2948
2949 mbedtls_free(context_buf);
2950 context_buf = NULL;
2951 context_buf_len = 0;
2952
2953 mbedtls_printf(" ok\n");
2954 }
2955 #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
2956
2957 /*
2958 * 7d. Continue doing data exchanges?
2959 */
2960 if (--opt.exchanges > 0) {
2961 goto send_request;
2962 }
2963
2964 /*
2965 * 8. Done, cleanly close the connection
2966 */
2967 close_notify:
2968 mbedtls_printf(" . Closing the connection...");
2969 fflush(stdout);
2970
2971 /*
2972 * Most of the time sending a close_notify before closing is the right
2973 * thing to do. However, when the server already knows how many messages
2974 * are expected and closes the connection by itself, this alert becomes
2975 * redundant. Sometimes with DTLS this redundancy becomes a problem by
2976 * leading to a race condition where the server might close the connection
2977 * before seeing the alert, and since UDP is connection-less when the
2978 * alert arrives it will be seen as a new connection, which will fail as
2979 * the alert is clearly not a valid ClientHello. This may cause spurious
2980 * failures in tests that use DTLS and resumption with ssl_server2 in
2981 * ssl-opt.sh, avoided by enabling skip_close_notify client-side.
2982 */
2983 if (opt.skip_close_notify == 0) {
2984 /* No error checking, the connection might be closed already */
2985 do {
2986 ret = mbedtls_ssl_close_notify(&ssl);
2987 } while (ret == MBEDTLS_ERR_SSL_WANT_WRITE);
2988 ret = 0;
2989 }
2990
2991 mbedtls_printf(" done\n");
2992
2993 /*
2994 * 9. Reconnect?
2995 */
2996 reconnect:
2997 if (opt.reconnect != 0) {
2998 --opt.reconnect;
2999
3000 mbedtls_net_free(&server_fd);
3001
3002 #if defined(MBEDTLS_TIMING_C)
3003 if (opt.reco_delay > 0) {
3004 mbedtls_net_usleep(1000 * opt.reco_delay);
3005 }
3006 #endif
3007
3008 mbedtls_printf(" . Reconnecting with saved session...");
3009
3010 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
3011 memset(peer_crt_info, 0, sizeof(peer_crt_info));
3012 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
3013
3014 if ((ret = mbedtls_ssl_session_reset(&ssl)) != 0) {
3015 mbedtls_printf(" failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
3016 (unsigned int) -ret);
3017 goto exit;
3018 }
3019
3020 if (opt.reco_mode == 1) {
3021 if ((ret = mbedtls_ssl_session_load(&saved_session,
3022 session_data,
3023 session_data_len)) != 0) {
3024 mbedtls_printf(" failed\n ! mbedtls_ssl_session_load returned -0x%x\n\n",
3025 (unsigned int) -ret);
3026 goto exit;
3027 }
3028 }
3029
3030 if ((ret = mbedtls_ssl_set_session(&ssl, &saved_session)) != 0) {
3031 mbedtls_printf(" failed\n ! mbedtls_ssl_set_session returned -0x%x\n\n",
3032 (unsigned int) -ret);
3033 goto exit;
3034 }
3035
3036 #if defined(MBEDTLS_X509_CRT_PARSE_C)
3037 if (opt.reco_server_name != NULL &&
3038 (ret = mbedtls_ssl_set_hostname(&ssl,
3039 opt.reco_server_name)) != 0) {
3040 mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
3041 ret);
3042 goto exit;
3043 }
3044 #endif
3045
3046 if ((ret = mbedtls_net_connect(&server_fd,
3047 opt.server_addr, opt.server_port,
3048 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
3049 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP)) != 0) {
3050 mbedtls_printf(" failed\n ! mbedtls_net_connect returned -0x%x\n\n",
3051 (unsigned int) -ret);
3052 goto exit;
3053 }
3054
3055 if (opt.nbio > 0) {
3056 ret = mbedtls_net_set_nonblock(&server_fd);
3057 } else {
3058 ret = mbedtls_net_set_block(&server_fd);
3059 }
3060 if (ret != 0) {
3061 mbedtls_printf(" failed\n ! net_set_(non)block() returned -0x%x\n\n",
3062 (unsigned int) -ret);
3063 goto exit;
3064 }
3065
3066 ret = build_http_request(buf, sizeof(buf) - 1, &len);
3067 if (ret != 0) {
3068 goto exit;
3069 }
3070
3071 #if defined(MBEDTLS_SSL_EARLY_DATA)
3072 if (ssl.conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED) {
3073 frags = 0;
3074 written = 0;
3075 do {
3076 while ((ret = mbedtls_ssl_write_early_data(&ssl, buf + written,
3077 len - written)) < 0) {
3078 if (ret == MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA) {
3079 goto end_of_early_data;
3080 }
3081 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
3082 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
3083 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
3084 mbedtls_printf(" failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
3085 (unsigned int) -ret);
3086 goto exit;
3087 }
3088
3089 /* For event-driven IO, wait for socket to become available */
3090 if (opt.event == 1 /* level triggered IO */) {
3091 #if defined(MBEDTLS_TIMING_C)
3092 idle(&server_fd, &timer, ret);
3093 #else
3094 idle(&server_fd, ret);
3095 #endif
3096 }
3097 }
3098
3099 frags++;
3100 written += ret;
3101 } while (written < len);
3102
3103 end_of_early_data:
3104
3105 buf[written] = '\0';
3106 mbedtls_printf(
3107 " %" MBEDTLS_PRINTF_SIZET " bytes of early data written in %" MBEDTLS_PRINTF_SIZET " fragments\n\n%s\n",
3108 written,
3109 frags,
3110 (char *) buf);
3111 }
3112 #endif /* MBEDTLS_SSL_EARLY_DATA */
3113
3114 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
3115 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
3116 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
3117 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
3118 mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
3119 (unsigned int) -ret);
3120 goto exit;
3121 }
3122 }
3123
3124 mbedtls_printf(" ok\n");
3125
3126 goto send_request;
3127 }
3128
3129 /*
3130 * Cleanup and exit
3131 */
3132 exit:
3133 #ifdef MBEDTLS_ERROR_C
3134 if (ret != 0) {
3135 char error_buf[100];
3136 mbedtls_strerror(ret, error_buf, 100);
3137 mbedtls_printf("Last error was: -0x%X - %s\n\n", (unsigned int) -ret, error_buf);
3138 }
3139 #endif
3140
3141 mbedtls_net_free(&server_fd);
3142
3143 mbedtls_ssl_free(&ssl);
3144 mbedtls_ssl_config_free(&conf);
3145 mbedtls_ssl_session_free(&saved_session);
3146
3147 if (session_data != NULL) {
3148 mbedtls_platform_zeroize(session_data, session_data_len);
3149 }
3150 mbedtls_free(session_data);
3151 #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
3152 if (context_buf != NULL) {
3153 mbedtls_platform_zeroize(context_buf, context_buf_len);
3154 }
3155 mbedtls_free(context_buf);
3156 #endif
3157
3158 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
3159 mbedtls_x509_crt_free(&clicert);
3160 mbedtls_x509_crt_free(&cacert);
3161 mbedtls_pk_free(&pkey);
3162 #if defined(MBEDTLS_USE_PSA_CRYPTO)
3163 psa_destroy_key(key_slot);
3164 #endif
3165 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
3166
3167 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) && \
3168 defined(MBEDTLS_USE_PSA_CRYPTO)
3169 if (opt.psk_opaque != 0) {
3170 /* This is ok even if the slot hasn't been
3171 * initialized (we might have jumed here
3172 * immediately because of bad cmd line params,
3173 * for example). */
3174 status = psa_destroy_key(slot);
3175 if ((status != PSA_SUCCESS) &&
3176 (opt.query_config_mode == DFL_QUERY_CONFIG_MODE)) {
3177 mbedtls_printf("Failed to destroy key slot %u - error was %d",
3178 (unsigned) MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot),
3179 (int) status);
3180 if (ret == 0) {
3181 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
3182 }
3183 }
3184 }
3185 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED &&
3186 MBEDTLS_USE_PSA_CRYPTO */
3187
3188 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
3189 defined(MBEDTLS_USE_PSA_CRYPTO)
3190 /*
3191 * In case opaque keys it's the user responsibility to keep the key valid
3192 * for the duration of the handshake and destroy it at the end
3193 */
3194 if ((opt.ecjpake_pw_opaque != DFL_ECJPAKE_PW_OPAQUE)) {
3195 psa_key_attributes_t check_attributes = PSA_KEY_ATTRIBUTES_INIT;
3196
3197 /* Verify that the key is still valid before destroying it */
3198 if (psa_get_key_attributes(ecjpake_pw_slot, &check_attributes) !=
3199 PSA_SUCCESS) {
3200 if (ret == 0) {
3201 ret = 1;
3202 }
3203 mbedtls_printf("The EC J-PAKE password key has unexpectedly been already destroyed\n");
3204 } else {
3205 psa_destroy_key(ecjpake_pw_slot);
3206 }
3207 }
3208 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO */
3209
3210 #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
3211 const char *message = mbedtls_test_helper_is_psa_leaking();
3212 if (message) {
3213 if (ret == 0) {
3214 ret = 1;
3215 }
3216 mbedtls_printf("PSA memory leak detected: %s\n", message);
3217 }
3218 #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
3219
3220 /* For builds with MBEDTLS_TEST_USE_PSA_CRYPTO_RNG psa crypto
3221 * resources are freed by rng_free(). */
3222 /* For builds with MBEDTLS_SSL_PROTO_TLS1_3, PSA may have been
3223 * initialized under the hood by the TLS layer. See
3224 * mbedtls_ssl_tls13_crypto_init(). */
3225 #if (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)) && \
3226 !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
3227 mbedtls_psa_crypto_free();
3228 #endif
3229
3230 rng_free(&rng);
3231
3232 #if defined(MBEDTLS_TEST_HOOKS)
3233 if (test_hooks_failure_detected()) {
3234 if (ret == 0) {
3235 ret = 1;
3236 }
3237 mbedtls_printf("Test hooks detected errors.\n");
3238 }
3239 test_hooks_free();
3240 #endif /* MBEDTLS_TEST_HOOKS */
3241
3242 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
3243 #if defined(MBEDTLS_MEMORY_DEBUG)
3244 mbedtls_memory_buffer_alloc_status();
3245 #endif
3246 mbedtls_memory_buffer_alloc_free();
3247 #endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
3248
3249 // Shell can not handle large exit numbers -> 1 for errors
3250 if (ret < 0) {
3251 ret = 1;
3252 }
3253
3254 if (opt.query_config_mode == DFL_QUERY_CONFIG_MODE) {
3255 mbedtls_exit(ret);
3256 } else {
3257 mbedtls_exit(query_config_ret);
3258 }
3259 }
3260 #endif /* !MBEDTLS_SSL_TEST_IMPOSSIBLE && MBEDTLS_SSL_CLI_C */
3261