blob: ca06bb37ef6cd1d40c585a5d871d03e528650223 [file] [log] [blame]
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001/*
2 * SSL client with options
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb60b95f2012-09-25 09:05:17 +000018 */
19
Mateusz Starzyk6c2e9b62021-05-19 17:54:54 +020020#define MBEDTLS_ALLOW_PRIVATE_ACCESS
21
Gilles Peskineab7ce962021-01-05 21:27:53 +010022#include "ssl_test_lib.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000023
Gilles Peskineab7ce962021-01-05 21:27:53 +010024#if defined(MBEDTLS_SSL_TEST_IMPOSSIBLE)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020025int main(void)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020026{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020027 mbedtls_printf(MBEDTLS_SSL_TEST_IMPOSSIBLE);
28 mbedtls_exit(0);
Gilles Peskineab7ce962021-01-05 21:27:53 +010029}
30#elif !defined(MBEDTLS_SSL_SRV_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020031int main(void)
Gilles Peskineab7ce962021-01-05 21:27:53 +010032{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020033 mbedtls_printf("MBEDTLS_SSL_SRV_C not defined.\n");
34 mbedtls_exit(0);
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020035}
Gilles Peskineb4df7542021-01-08 21:20:09 +010036#else /* !MBEDTLS_SSL_TEST_IMPOSSIBLE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010037
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020038# include <stdint.h>
Andres AG0b736db2017-02-08 14:05:57 +000039
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020040# if !defined(_MSC_VER)
41# include <inttypes.h>
42# endif
Rich Evans18b78c72015-02-11 14:06:19 +000043
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020044# if !defined(_WIN32)
45# include <signal.h>
46# endif
Rich Evans18b78c72015-02-11 14:06:19 +000047
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020048# if defined(MBEDTLS_SSL_CACHE_C)
49# include "mbedtls/ssl_cache.h"
50# endif
Paul Bakker0a597072012-09-25 21:55:46 +000051
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020052# if defined(MBEDTLS_SSL_TICKET_C)
53# include "mbedtls/ssl_ticket.h"
54# endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020055
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020056# if defined(MBEDTLS_SSL_COOKIE_C)
57# include "mbedtls/ssl_cookie.h"
58# endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020059
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020060# if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO)
61# define SNI_OPTION
62# endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020063
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020064# if defined(_WIN32)
65# include <windows.h>
66# endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020067
Simon Butchercce68be2018-07-23 14:26:09 +010068/* Size of memory to be allocated for the heap, when using the library's memory
69 * management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020070# define MEMORY_HEAP_SIZE 120000
Simon Butchercce68be2018-07-23 14:26:09 +010071
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020072# define DFL_SERVER_ADDR NULL
73# define DFL_SERVER_PORT "4433"
74# define DFL_RESPONSE_SIZE -1
75# define DFL_DEBUG_LEVEL 0
76# define DFL_NBIO 0
77# define DFL_EVENT 0
78# define DFL_READ_TIMEOUT 0
79# define DFL_CA_FILE ""
80# define DFL_CA_PATH ""
81# define DFL_CRT_FILE ""
82# define DFL_KEY_FILE ""
83# define DFL_KEY_PWD ""
84# define DFL_CRT_FILE2 ""
85# define DFL_KEY_FILE2 ""
86# define DFL_KEY_PWD2 ""
87# define DFL_ASYNC_OPERATIONS "-"
88# define DFL_ASYNC_PRIVATE_DELAY1 (-1)
89# define DFL_ASYNC_PRIVATE_DELAY2 (-1)
90# define DFL_ASYNC_PRIVATE_ERROR (0)
91# define DFL_PSK ""
92# define DFL_PSK_OPAQUE 0
93# define DFL_PSK_LIST_OPAQUE 0
94# define DFL_PSK_IDENTITY "Client_identity"
95# define DFL_ECJPAKE_PW NULL
96# define DFL_PSK_LIST NULL
97# define DFL_FORCE_CIPHER 0
98# define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
99# define DFL_ALLOW_LEGACY -2
100# define DFL_RENEGOTIATE 0
101# define DFL_RENEGO_DELAY -2
102# define DFL_RENEGO_PERIOD ((uint64_t)-1)
103# define DFL_EXCHANGES 1
104# define DFL_MIN_VERSION -1
105# define DFL_MAX_VERSION -1
106# define DFL_SHA1 -1
107# define DFL_CID_ENABLED 0
108# define DFL_CID_VALUE ""
109# define DFL_CID_ENABLED_RENEGO -1
110# define DFL_CID_VALUE_RENEGO NULL
111# define DFL_AUTH_MODE -1
112# define DFL_CERT_REQ_CA_LIST MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED
113# define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
114# define DFL_TRUNC_HMAC -1
115# define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
116# define DFL_TICKET_TIMEOUT 86400
117# define DFL_CACHE_MAX -1
118# define DFL_CACHE_TIMEOUT -1
119# define DFL_SNI NULL
120# define DFL_ALPN_STRING NULL
121# define DFL_CURVES NULL
122# define DFL_DHM_FILE NULL
123# define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
124# define DFL_COOKIES 1
125# define DFL_ANTI_REPLAY -1
126# define DFL_HS_TO_MIN 0
127# define DFL_HS_TO_MAX 0
128# define DFL_DTLS_MTU -1
129# define DFL_BADMAC_LIMIT -1
130# define DFL_DGRAM_PACKING 1
131# define DFL_EXTENDED_MS -1
132# define DFL_ETM -1
133# define DFL_SERIALIZE 0
134# define DFL_CONTEXT_FILE ""
135# define DFL_EXTENDED_MS_ENFORCE -1
136# define DFL_CA_CALLBACK 0
137# define DFL_EAP_TLS 0
138# define DFL_REPRODUCIBLE 0
139# define DFL_NSS_KEYLOG 0
140# define DFL_NSS_KEYLOG_FILE NULL
141# define DFL_QUERY_CONFIG_MODE 0
142# define DFL_USE_SRTP 0
143# define DFL_SRTP_FORCE_PROFILE 0
144# define DFL_SRTP_SUPPORT_MKI 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000145
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200146# define LONG_RESPONSE \
147 "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
148 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
149 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
150 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
151 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
152 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
153 "07-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah</p>\r\n"
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200154
Paul Bakker8e714d72013-07-18 11:05:13 +0200155/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
156 * packets (for fragmentation purposes) */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200157# define HTTP_RESPONSE \
158 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
159 "<h2>mbed TLS Test Server</h2>\r\n" \
160 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000161
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200162/*
163 * Size of the basic I/O buffer. Able to hold our default response.
164 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 * You will need to adapt the mbedtls_ssl_get_bytes_avail() test in ssl-opt.sh
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200166 * if you change this value to something outside the range <= 100 or > 500
167 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200168# define DFL_IO_BUF_LEN 200
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200169
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200170# if defined(MBEDTLS_X509_CRT_PARSE_C)
171# if defined(MBEDTLS_FS_IO)
172# define USAGE_IO \
173 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
174 " default: \"\" (pre-loaded)\n" \
175 " use \"none\" to skip loading any top-level CAs.\n" \
176 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
177 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
178 " use \"none\" to skip loading any top-level CAs.\n" \
179 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
180 " default: see note after key_file2\n" \
181 " key_file=%%s default: see note after key_file2\n" \
182 " key_pwd=%%s Password for key specified by key_file argument\n" \
183 " default: none\n" \
184 " crt_file2=%%s Your second cert and chain (in bottom to top order, top may be omitted)\n" \
185 " default: see note after key_file2\n" \
186 " key_file2=%%s default: see note below\n" \
187 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
188 " preloaded certificate(s) and key(s) are used if available\n" \
189 " key_pwd2=%%s Password for key specified by key_file2 argument\n" \
190 " default: none\n" \
191 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
192 " default: preloaded parameters\n"
193# else
194# define USAGE_IO \
195 "\n" \
196 " No file operations available (MBEDTLS_FS_IO not defined)\n" \
197 "\n"
198# endif /* MBEDTLS_FS_IO */
199# else
200# define USAGE_IO ""
201# endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200202
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200203# if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
204# define USAGE_SSL_ASYNC \
205 " async_operations=%%c... d=decrypt, s=sign (default: -=off)\n" \
206 " async_private_delay1=%%d Asynchronous delay for key_file or preloaded key\n" \
207 " async_private_delay2=%%d Asynchronous delay for key_file2 and sni\n" \
208 " default: -1 (not asynchronous)\n" \
209 " async_private_error=%%d Async callback error injection (default=0=none,\n" \
210 " 1=start, 2=cancel, 3=resume, negative=first time only)"
211# else
212# define USAGE_SSL_ASYNC ""
213# endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100214
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200215# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
216# define USAGE_CID \
217 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
218 " default: 0 (disabled)\n" \
219 " cid_renego=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension during renegotiation.\n" \
220 " default: same as 'cid' parameter\n" \
221 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
222 " default: \"\"\n" \
223 " cid_val_renego=%%s The CID to use for incoming messages (in hex, without 0x) after renegotiation.\n" \
224 " default: same as 'cid_val' parameter\n"
225# else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
226# define USAGE_CID ""
227# endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +0100228
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200229# if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
230# define USAGE_PSK_RAW \
231 " psk=%%s default: \"\" (disabled)\n" \
232 " The PSK values are in hex, without 0x.\n" \
233 " psk_list=%%s default: \"\"\n" \
234 " A list of (PSK identity, PSK value) pairs.\n" \
235 " The PSK values are in hex, without 0x.\n" \
236 " id1,psk1[,id2,psk2[,...]]\n" \
237 " psk_identity=%%s default: \"Client_identity\"\n"
238# if defined(MBEDTLS_USE_PSA_CRYPTO)
239# define USAGE_PSK_SLOT \
240 " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
241 " Enable this to store the PSK configured through command line\n" \
242 " parameter `psk` in a PSA-based key slot.\n" \
243 " Note: Currently only supported in conjunction with\n" \
244 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
245 " to force a particular PSK-only ciphersuite.\n" \
246 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
247 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
248 " with prepopulated key slots instead of importing raw key material.\n" \
249 " psk_list_opaque=%%d default: 0 (don't use opaque dynamic PSKs)\n" \
250 " Enable this to store the list of dynamically chosen PSKs configured\n" \
251 " through the command line parameter `psk_list` in PSA-based key slots.\n" \
252 " Note: Currently only supported in conjunction with\n" \
253 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
254 " to force a particular PSK-only ciphersuite.\n" \
255 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
256 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
257 " with prepopulated key slots instead of importing raw key material.\n"
258# else
259# define USAGE_PSK_SLOT ""
260# endif /* MBEDTLS_USE_PSA_CRYPTO */
261# define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
262# else
263# define USAGE_PSK ""
264# endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
265# if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
266# define USAGE_CA_CALLBACK \
267 " ca_callback=%%d default: 0 (disabled)\n" \
268 " Enable this to use the trusted certificate callback function\n"
269# else
270# define USAGE_CA_CALLBACK ""
271# endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
272# if defined(MBEDTLS_SSL_SESSION_TICKETS)
273# define USAGE_TICKETS \
274 " tickets=%%d default: 1 (enabled)\n" \
275 " ticket_timeout=%%d default: 86400 (one day)\n"
276# else
277# define USAGE_TICKETS ""
278# endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200279
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200280# if defined(MBEDTLS_SSL_EXPORT_KEYS)
281# define USAGE_EAP_TLS " eap_tls=%%d default: 0 (disabled)\n"
282# define USAGE_NSS_KEYLOG \
283 " nss_keylog=%%d default: 0 (disabled)\n" \
284 " This cannot be used with eap_tls=1\n"
285# define USAGE_NSS_KEYLOG_FILE " nss_keylog_file=%%s\n"
286# if defined(MBEDTLS_SSL_DTLS_SRTP)
287# define USAGE_SRTP \
288 " use_srtp=%%d default: 0 (disabled)\n" \
289 " srtp_force_profile=%%d default: 0 (all enabled)\n" \
290 " available profiles:\n" \
291 " 1 - SRTP_AES128_CM_HMAC_SHA1_80\n" \
292 " 2 - SRTP_AES128_CM_HMAC_SHA1_32\n" \
293 " 3 - SRTP_NULL_HMAC_SHA1_80\n" \
294 " 4 - SRTP_NULL_HMAC_SHA1_32\n" \
295 " support_mki=%%d default: 0 (not supported)\n"
296# else /* MBEDTLS_SSL_DTLS_SRTP */
297# define USAGE_SRTP ""
298# endif
299# else /* MBEDTLS_SSL_EXPORT_KEYS */
300# define USAGE_EAP_TLS ""
301# define USAGE_NSS_KEYLOG ""
302# define USAGE_NSS_KEYLOG_FILE ""
303# define USAGE_SRTP ""
304# endif /* MBEDTLS_SSL_EXPORT_KEYS */
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300305
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200306# if defined(MBEDTLS_SSL_CACHE_C)
307# define USAGE_CACHE \
308 " cache_max=%%d default: cache default (50)\n" \
309 " cache_timeout=%%d default: cache default (1d)\n"
310# else
311# define USAGE_CACHE ""
312# endif /* MBEDTLS_SSL_CACHE_C */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100313
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200314# if defined(SNI_OPTION)
315# if defined(MBEDTLS_X509_CRL_PARSE_C)
316# define SNI_CRL ",crl"
317# else
318# define SNI_CRL ""
319# endif
Ron Eldor80d04192019-04-04 15:02:01 +0300320
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200321# define USAGE_SNI \
322 " sni=%%s name1,cert1,key1,ca1" SNI_CRL \
323 ",auth1[,...]\n" \
324 " default: disabled\n"
325# else
326# define USAGE_SNI ""
327# endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100328
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200329# if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
330# define USAGE_MAX_FRAG_LEN \
331 " max_frag_len=%%d default: 16384 (tls default)\n" \
332 " options: 512, 1024, 2048, 4096\n"
333# else
334# define USAGE_MAX_FRAG_LEN ""
335# endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200336
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200337# if defined(MBEDTLS_SSL_ALPN)
338# define USAGE_ALPN \
339 " alpn=%%s default: \"\" (disabled)\n" \
340 " example: spdy/1,http/1.1\n"
341# else
342# define USAGE_ALPN ""
343# endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200344
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200345# if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
346# define USAGE_COOKIES \
347 " cookies=0/1/-1 default: 1 (enabled)\n" \
348 " 0: disabled, -1: library default (broken)\n"
349# else
350# define USAGE_COOKIES ""
351# endif
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200352
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200353# if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
354# define USAGE_ANTI_REPLAY \
355 " anti_replay=0/1 default: (library default: enabled)\n"
356# else
357# define USAGE_ANTI_REPLAY ""
358# endif
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200359
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200360# define USAGE_BADMAC_LIMIT \
361 " badmac_limit=%%d default: (library default: disabled)\n"
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200362
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200363# if defined(MBEDTLS_SSL_PROTO_DTLS)
364# define USAGE_DTLS \
365 " dtls=%%d default: 0 (TLS)\n" \
366 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
367 " range of DTLS handshake timeouts in millisecs\n" \
368 " mtu=%%d default: (library default: unlimited)\n" \
369 " dgram_packing=%%d default: 1 (allowed)\n" \
370 " allow or forbid packing of multiple\n" \
371 " records within a single datgram.\n"
372# else
373# define USAGE_DTLS ""
374# endif
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200375
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200376# if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
377# define USAGE_EMS \
378 " extended_ms=0/1 default: (library default: on)\n"
379# else
380# define USAGE_EMS ""
381# endif
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200382
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200383# if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
384# define USAGE_ETM \
385 " etm=0/1 default: (library default: on)\n"
386# else
387# define USAGE_ETM ""
388# endif
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100389
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200390# define USAGE_REPRODUCIBLE \
391 " reproducible=0/1 default: 0 (disabled)\n"
Philippe Antoine738153a2019-06-18 20:16:43 +0200392
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200393# if defined(MBEDTLS_SSL_RENEGOTIATION)
394# define USAGE_RENEGO \
395 " renegotiation=%%d default: 0 (disabled)\n" \
396 " renegotiate=%%d default: 0 (disabled)\n" \
397 " renego_delay=%%d default: -2 (library default)\n" \
398 " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n"
399# else
400# define USAGE_RENEGO ""
401# endif
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100402
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200403# if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
404# define USAGE_ECJPAKE \
405 " ecjpake_pw=%%s default: none (disabled)\n"
406# else
407# define USAGE_ECJPAKE ""
408# endif
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200409
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200410# if defined(MBEDTLS_ECP_C)
411# define USAGE_CURVES \
412 " curves=a,b,c,d default: \"default\" (library default)\n" \
413 " example: \"secp521r1,brainpoolP512r1\"\n" \
414 " - use \"none\" for empty list\n" \
415 " - see mbedtls_ecp_curve_list()\n" \
416 " for acceptable curve names\n"
417# else
418# define USAGE_CURVES ""
419# endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100420
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200421# if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
422# define USAGE_SERIALIZATION \
423 " serialize=%%d default: 0 (do not serialize/deserialize)\n" \
424 " options: 1 (serialize)\n" \
425 " 2 (serialize with re-initialization)\n" \
426 " context_file=%%s The file path to write a serialized connection\n" \
427 " in the form of base64 code (serialize option\n" \
428 " must be set)\n" \
429 " default: \"\" (do nothing)\n" \
430 " option: a file path\n"
431# else
432# define USAGE_SERIALIZATION ""
433# endif
Jarno Lamsabbc7b412019-06-04 11:06:31 +0300434
Gilles Peskineb2971ff2020-04-14 19:41:01 +0200435/* USAGE is arbitrarily split to stay under the portable string literal
436 * length limit: 4095 bytes in C99. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200437# define USAGE1 \
438 "\n usage: ssl_server2 param=<>...\n" \
439 "\n acceptable parameters:\n" \
440 " server_addr=%%s default: (all interfaces)\n" \
441 " server_port=%%d default: 4433\n" \
442 " debug_level=%%d default: 0 (disabled)\n" \
443 " buffer_size=%%d default: 200 \n" \
444 " (minimum: 1)\n" \
445 " response_size=%%d default: about 152 (basic response)\n" \
446 " (minimum: 0, max: 16384)\n" \
447 " increases buffer_size if bigger\n" \
448 " nbio=%%d default: 0 (blocking I/O)\n" \
449 " options: 1 (non-blocking), 2 (added delays)\n" \
450 " event=%%d default: 0 (loop)\n" \
451 " options: 1 (level-triggered, implies nbio=1),\n" \
452 " read_timeout=%%d default: 0 ms (no timeout)\n" \
453 "\n" USAGE_DTLS USAGE_SRTP USAGE_COOKIES USAGE_ANTI_REPLAY \
454 USAGE_BADMAC_LIMIT "\n"
455# define USAGE2 \
456 " auth_mode=%%s default: (library default: none)\n" \
457 " options: none, optional, required\n" \
458 " cert_req_ca_list=%%d default: 1 (send ca list)\n" \
459 " options: 1 (send ca list), 0 (don't send)\n" USAGE_IO \
460 "\n" USAGE_PSK USAGE_CA_CALLBACK USAGE_ECJPAKE "\n"
461# define USAGE3 \
462 " allow_legacy=%%d default: (library default: no)\n" USAGE_RENEGO \
463 " exchanges=%%d default: 1\n" \
464 "\n" USAGE_TICKETS USAGE_EAP_TLS USAGE_REPRODUCIBLE USAGE_NSS_KEYLOG \
465 USAGE_NSS_KEYLOG_FILE USAGE_CACHE USAGE_MAX_FRAG_LEN USAGE_ALPN \
466 USAGE_EMS USAGE_ETM USAGE_CURVES "\n"
467# define USAGE4 \
468 USAGE_SSL_ASYNC \
469 USAGE_SNI \
470 " allow_sha1=%%d default: 0\n" \
471 " min_version=%%s default: (library default: tls1_2)\n" \
472 " max_version=%%s default: (library default: tls1_2)\n" \
473 " force_version=%%s default: \"\" (none)\n" \
474 " options: tls1_2, dtls1_2\n" \
475 "\n" \
476 " force_ciphersuite=<name> default: all enabled\n" \
477 " query_config=<name> return 0 if the specified\n" \
478 " configuration macro is defined and 1\n" \
479 " otherwise. The expansion of the macro\n" \
480 " is printed if it is defined\n" USAGE_SERIALIZATION \
481 " acceptable ciphersuite names:\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000482
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200483# define ALPN_LIST_SIZE 10
484# define CURVE_LIST_SIZE 20
Hanno Becker8651a432017-06-09 16:13:22 +0100485
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200486# define PUT_UINT64_BE(out_be, in_le, i) \
487 { \
488 (out_be)[(i) + 0] = (unsigned char)(((in_le) >> 56) & 0xFF); \
489 (out_be)[(i) + 1] = (unsigned char)(((in_le) >> 48) & 0xFF); \
490 (out_be)[(i) + 2] = (unsigned char)(((in_le) >> 40) & 0xFF); \
491 (out_be)[(i) + 3] = (unsigned char)(((in_le) >> 32) & 0xFF); \
492 (out_be)[(i) + 4] = (unsigned char)(((in_le) >> 24) & 0xFF); \
493 (out_be)[(i) + 5] = (unsigned char)(((in_le) >> 16) & 0xFF); \
494 (out_be)[(i) + 6] = (unsigned char)(((in_le) >> 8) & 0xFF); \
495 (out_be)[(i) + 7] = (unsigned char)(((in_le) >> 0) & 0xFF); \
496 }
Andres AG692ad842017-01-19 16:30:57 +0000497
Manuel Pégourié-Gonnard6f19ce32021-06-16 12:08:34 +0200498/* This is global so it can be easily accessed by callback functions */
499rng_context_t rng;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500500
Rich Evans85b05ec2015-02-12 11:37:29 +0000501/*
502 * global options
503 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200504struct options {
505 const char *server_addr; /* address on which the ssl service runs */
506 const char *server_port; /* port on which the ssl service runs */
507 int debug_level; /* level of debugging */
508 int nbio; /* should I/O be blocking? */
509 int event; /* loop or event-driven IO? level or edge triggered? */
510 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
511 int response_size; /* pad response with header to requested size */
512 uint16_t buffer_size; /* IO buffer size */
513 const char *ca_file; /* the file with the CA certificate(s) */
514 const char *ca_path; /* the path with the CA certificate(s) reside */
515 const char *crt_file; /* the file with the server certificate */
516 const char *key_file; /* the file with the server key */
517 const char *key_pwd; /* the password for the server key */
518 const char *crt_file2; /* the file with the 2nd server certificate */
519 const char *key_file2; /* the file with the 2nd server key */
520 const char *key_pwd2; /* the password for the 2nd server key */
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100521 const char *async_operations; /* supported SSL asynchronous operations */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200522 int async_private_delay1; /* number of times f_async_resume needs to be
523 called for key 1, or -1 for no async */
524 int async_private_delay2; /* number of times f_async_resume needs to be
525 called for key 2, or -1 for no async */
526 int async_private_error; /* inject error in async private callback */
527# if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000528 int psk_opaque;
529 int psk_list_opaque;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200530# endif
531# if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
532 int ca_callback; /* Use callback for trusted certificate list */
533# endif
534 const char *psk; /* the pre-shared key */
535 const char *psk_identity; /* the pre-shared key identity */
536 char *psk_list; /* list of PSK id/key pairs for callback */
537 const char *ecjpake_pw; /* the EC J-PAKE password */
538 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
539 int renegotiation; /* enable / disable renegotiation */
540 int allow_legacy; /* allow legacy renegotiation */
541 int renegotiate; /* attempt renegotiation? */
542 int renego_delay; /* delay before enforcing renegotiation */
543 uint64_t renego_period; /* period for automatic renegotiation */
544 int exchanges; /* number of data exchanges */
545 int min_version; /* minimum protocol version accepted */
546 int max_version; /* maximum protocol version accepted */
547 int allow_sha1; /* flag for SHA-1 support */
548 int auth_mode; /* verify mode for connection */
549 int cert_req_ca_list; /* should we send the CA list? */
550 unsigned char mfl_code; /* code for maximum fragment length */
551 int trunc_hmac; /* accept truncated hmac? */
552 int tickets; /* enable / disable session tickets */
553 int ticket_timeout; /* session ticket lifetime */
554 int cache_max; /* max number of session cache entries */
555 int cache_timeout; /* expiration delay of session cache entries */
556 char *sni; /* string describing sni information */
557 const char *curves; /* list of supported elliptic curves */
558 const char *alpn_string; /* ALPN supported protocols */
559 const char *dhm_file; /* the file with the DH parameters */
560 int extended_ms; /* allow negotiation of extended MS? */
561 int etm; /* allow negotiation of encrypt-then-MAC? */
562 int transport; /* TLS or DTLS? */
563 int cookies; /* Use cookies for DTLS? -1 to break them */
564 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
565 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
566 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
567 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
568 int dgram_packing; /* allow/forbid datagram packing */
569 int badmac_limit; /* Limit of records with bad MAC */
570 int eap_tls; /* derive EAP-TLS keying material? */
571 int nss_keylog; /* export NSS key log material */
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100572 const char *nss_keylog_file; /* NSS key log file */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200573 int cid_enabled; /* whether to use the CID extension or not */
574 int cid_enabled_renego; /* whether to use the CID extension or not
575 * during renegotiation */
576 const char *cid_val; /* the CID to use for incoming messages */
577 int serialize; /* serialize/deserialize connection */
578 const char *context_file; /* the file to write a serialized connection
579 * in the form of base64 code (serialize
580 * option must be set) */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100581 const char *cid_val_renego; /* the CID to use for incoming messages
582 * after renegotiation */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200583 int reproducible; /* make communication reproducible */
584 int query_config_mode; /* whether to read config */
585 int use_srtp; /* Support SRTP */
586 int force_srtp_profile; /* SRTP protection profile to use or all */
587 int support_mki; /* The dtls mki mki support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000588} opt;
589
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200590# include "ssl_test_common_source.c"
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100591
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200592/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200593 * Return authmode from string, or -1 on error
594 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200595static int get_auth_mode(const char *s)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200596{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200597 if (strcmp(s, "none") == 0)
598 return MBEDTLS_SSL_VERIFY_NONE;
599 if (strcmp(s, "optional") == 0)
600 return MBEDTLS_SSL_VERIFY_OPTIONAL;
601 if (strcmp(s, "required") == 0)
602 return MBEDTLS_SSL_VERIFY_REQUIRED;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200603
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200604 return -1;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200605}
606
607/*
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200608 * Used by sni_parse and psk_parse to handle coma-separated lists
609 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200610# define GET_ITEM(dst) \
611 do { \
612 (dst) = p; \
613 while (*p != ',') \
614 if (++p > end) \
615 goto error; \
616 *p++ = '\0'; \
617 } while (0)
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200618
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200619# if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100620typedef struct _sni_entry sni_entry;
621
622struct _sni_entry {
623 const char *name;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 mbedtls_x509_crt *cert;
625 mbedtls_pk_context *key;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200626 mbedtls_x509_crt *ca;
627 mbedtls_x509_crl *crl;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200628 int authmode;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100629 sni_entry *next;
630};
631
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200632void sni_free(sni_entry *head)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100633{
634 sni_entry *cur = head, *next;
635
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200636 while (cur != NULL) {
637 mbedtls_x509_crt_free(cur->cert);
638 mbedtls_free(cur->cert);
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100639
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200640 mbedtls_pk_free(cur->key);
641 mbedtls_free(cur->key);
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100642
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200643 mbedtls_x509_crt_free(cur->ca);
644 mbedtls_free(cur->ca);
645# if defined(MBEDTLS_X509_CRL_PARSE_C)
646 mbedtls_x509_crl_free(cur->crl);
647 mbedtls_free(cur->crl);
648# endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100649 next = cur->next;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200650 mbedtls_free(cur);
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100651 cur = next;
652 }
653}
654
655/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200656 * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
657 * into a usable sni_entry list. For ca1, crl1, auth1, the special value
658 * '-' means unset. If ca1 is unset, then crl1 is ignored too.
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000659 *
660 * Modifies the input string! This is not production quality!
661 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200662sni_entry *sni_parse(char *sni_string)
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000663{
664 sni_entry *cur = NULL, *new = NULL;
665 char *p = sni_string;
666 char *end = p;
Ron Eldor80d04192019-04-04 15:02:01 +0300667 char *crt_file, *key_file, *ca_file, *auth_str;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200668# if defined(MBEDTLS_X509_CRL_PARSE_C)
Ron Eldor80d04192019-04-04 15:02:01 +0300669 char *crl_file;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200670# endif
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000671
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200672 while (*end != '\0')
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000673 ++end;
674 *end = ',';
675
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200676 while (p <= end) {
677 if ((new = mbedtls_calloc(1, sizeof(sni_entry))) == NULL) {
678 sni_free(cur);
679 return NULL;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000680 }
681
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200682 GET_ITEM(new->name);
683 GET_ITEM(crt_file);
684 GET_ITEM(key_file);
685 GET_ITEM(ca_file);
686# if defined(MBEDTLS_X509_CRL_PARSE_C)
687 GET_ITEM(crl_file);
688# endif
689 GET_ITEM(auth_str);
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000690
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200691 if ((new->cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt))) == NULL ||
692 (new->key = mbedtls_calloc(1, sizeof(mbedtls_pk_context))) == NULL)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200693 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000694
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200695 mbedtls_x509_crt_init(new->cert);
696 mbedtls_pk_init(new->key);
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000697
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200698 if (mbedtls_x509_crt_parse_file(new->cert, crt_file) != 0 ||
699 mbedtls_pk_parse_keyfile(new->key, key_file, "", rng_get, &rng) !=
700 0)
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000701 goto error;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200702
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200703 if (strcmp(ca_file, "-") != 0) {
704 if ((new->ca = mbedtls_calloc(1, sizeof(mbedtls_x509_crt))) == NULL)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200705 goto error;
706
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200707 mbedtls_x509_crt_init(new->ca);
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200708
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200709 if (mbedtls_x509_crt_parse_file(new->ca, ca_file) != 0)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200710 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000711 }
712
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200713# if defined(MBEDTLS_X509_CRL_PARSE_C)
714 if (strcmp(crl_file, "-") != 0) {
715 if ((new->crl = mbedtls_calloc(1, sizeof(mbedtls_x509_crl))) ==
716 NULL)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200717 goto error;
718
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200719 mbedtls_x509_crl_init(new->crl);
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200720
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200721 if (mbedtls_x509_crl_parse_file(new->crl, crl_file) != 0)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200722 goto error;
723 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200724# endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200725
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200726 if (strcmp(auth_str, "-") != 0) {
727 if ((new->authmode = get_auth_mode(auth_str)) < 0)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200728 goto error;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200729 } else
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200730 new->authmode = DFL_AUTH_MODE;
731
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000732 new->next = cur;
733 cur = new;
734 }
735
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200736 return cur;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000737
738error:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200739 sni_free(new);
740 sni_free(cur);
741 return NULL;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000742}
743
744/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100745 * SNI callback.
746 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200747int sni_callback(void *p_info,
748 mbedtls_ssl_context *ssl,
749 const unsigned char *name,
750 size_t name_len)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100751{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200752 const sni_entry *cur = (const sni_entry *)p_info;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100753
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200754 while (cur != NULL) {
755 if (name_len == strlen(cur->name) &&
756 memcmp(name, cur->name, name_len) == 0) {
757 if (cur->ca != NULL)
758 mbedtls_ssl_set_hs_ca_chain(ssl, cur->ca, cur->crl);
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200759
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200760 if (cur->authmode != DFL_AUTH_MODE)
761 mbedtls_ssl_set_hs_authmode(ssl, cur->authmode);
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200762
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200763 return mbedtls_ssl_set_hs_own_cert(ssl, cur->cert, cur->key);
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100764 }
765
Mateusz Starzyk55768f22021-05-27 14:57:03 +0200766 cur = cur->next;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100767 }
768
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200769 return -1;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100770}
771
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200772# endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100773
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200774# if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker554b6ea2019-04-30 14:18:06 +0100775
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200776typedef struct _psk_entry psk_entry;
777
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200778struct _psk_entry {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200779 const char *name;
780 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 unsigned char key[MBEDTLS_PSK_MAX_LEN];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200782# if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Cronadc2ff22020-09-16 16:49:27 +0200783 psa_key_id_t slot;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200784# endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200785 psk_entry *next;
786};
787
788/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000789 * Free a list of psk_entry's
790 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200791int psk_free(psk_entry *head)
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000792{
793 psk_entry *next;
794
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200795 while (head != NULL) {
796# if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc43b6ea2018-11-05 13:48:43 +0000797 psa_status_t status;
Ronald Cronadc2ff22020-09-16 16:49:27 +0200798 psa_key_id_t const slot = head->slot;
Hanno Beckerc43b6ea2018-11-05 13:48:43 +0000799
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200800 if (slot != 0) {
801 status = psa_destroy_key(slot);
802 if (status != PSA_SUCCESS)
803 return status;
Hanno Beckerc43b6ea2018-11-05 13:48:43 +0000804 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200805# endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc43b6ea2018-11-05 13:48:43 +0000806
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000807 next = head->next;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200808 mbedtls_free(head);
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000809 head = next;
810 }
Hanno Beckerc43b6ea2018-11-05 13:48:43 +0000811
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200812 return 0;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000813}
814
815/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200816 * Parse a string of pairs name1,key1[,name2,key2[,...]]
817 * into a usable psk_entry list.
818 *
819 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200820 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200821psk_entry *psk_parse(char *psk_string)
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200822{
823 psk_entry *cur = NULL, *new = NULL;
824 char *p = psk_string;
825 char *end = p;
826 char *key_hex;
827
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200828 while (*end != '\0')
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200829 ++end;
830 *end = ',';
831
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200832 while (p <= end) {
833 if ((new = mbedtls_calloc(1, sizeof(psk_entry))) == NULL)
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +0000834 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200835
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200836 memset(new, 0, sizeof(psk_entry));
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200837
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200838 GET_ITEM(new->name);
839 GET_ITEM(key_hex);
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200840
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200841 if (mbedtls_test_unhexify(new->key, MBEDTLS_PSK_MAX_LEN, key_hex,
842 &new->key_len) != 0)
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000843 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200844
845 new->next = cur;
846 cur = new;
847 }
848
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200849 return cur;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200850
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000851error:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200852 psk_free(new);
853 psk_free(cur);
854 return 0;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200855}
856
857/*
858 * PSK callback
859 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200860int psk_callback(void *p_info,
861 mbedtls_ssl_context *ssl,
862 const unsigned char *name,
863 size_t name_len)
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200864{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200865 psk_entry *cur = (psk_entry *)p_info;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200866
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200867 while (cur != NULL) {
868 if (name_len == strlen(cur->name) &&
869 memcmp(name, cur->name, name_len) == 0) {
870# if defined(MBEDTLS_USE_PSA_CRYPTO)
871 if (cur->slot != 0)
872 return mbedtls_ssl_set_hs_psk_opaque(ssl, cur->slot);
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100873 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200874# endif
875 return mbedtls_ssl_set_hs_psk(ssl, cur->key, cur->key_len);
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200876 }
877
Mateusz Starzyk55768f22021-05-27 14:57:03 +0200878 cur = cur->next;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200879 }
880
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200881 return -1;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200882}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200883# endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200884
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200885static mbedtls_net_context listen_fd, client_fd;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200886
887/* Interruption handler to ensure clean exit (for valgrind testing) */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200888# if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200889static int received_sigterm = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200890void term_handler(int sig)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200891{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200892 ((void)sig);
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200893 received_sigterm = 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200894 mbedtls_net_free(&listen_fd); /* causes mbedtls_net_accept() to abort */
895 mbedtls_net_free(&client_fd); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200896}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200897# endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200898
Gilles Peskinea36ac4f2018-04-26 08:05:02 +0200899/** Return true if \p ret is a status code indicating that there is an
900 * operation in progress on an SSL connection, and false if it indicates
901 * success or a fatal error.
902 *
903 * The possible operations in progress are:
904 *
905 * - A read, when the SSL input buffer does not contain a full message.
906 * - A write, when the SSL output buffer contains some data that has not
907 * been sent over the network yet.
908 * - An asynchronous callback that has not completed yet. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200909static int mbedtls_status_is_ssl_in_progress(int ret)
Gilles Peskinea36ac4f2018-04-26 08:05:02 +0200910{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200911 return (ret == MBEDTLS_ERR_SSL_WANT_READ ||
Gilles Peskinea36ac4f2018-04-26 08:05:02 +0200912 ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200913 ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS);
Gilles Peskinea36ac4f2018-04-26 08:05:02 +0200914}
915
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200916# if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
917typedef struct {
Gilles Peskine44817442018-06-13 18:09:28 +0200918 mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
919 mbedtls_pk_context *pk; /*!< Private key */
920 unsigned delay; /*!< Number of resume steps to go through */
921 unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100922} ssl_async_key_slot_t;
923
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200924typedef enum
925{
Gilles Peskinead28bf02018-04-26 00:19:16 +0200926 SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
927 SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200928 SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start
929 */
Gilles Peskinead28bf02018-04-26 00:19:16 +0200930 SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200931# define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100932} ssl_async_inject_error_t;
933
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200934typedef struct {
Gilles Peskinee2479892018-06-13 18:06:51 +0200935 ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100936 size_t slots_used;
937 ssl_async_inject_error_t inject_error;
938 int (*f_rng)(void *, unsigned char *, size_t);
939 void *p_rng;
940} ssl_async_key_context_t;
941
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200942int ssl_async_set_key(ssl_async_key_context_t *ctx,
943 mbedtls_x509_crt *cert,
944 mbedtls_pk_context *pk,
945 int pk_take_ownership,
946 unsigned delay)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100947{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200948 if (ctx->slots_used >= sizeof(ctx->slots) / sizeof(*ctx->slots))
949 return -1;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100950 ctx->slots[ctx->slots_used].cert = cert;
951 ctx->slots[ctx->slots_used].pk = pk;
952 ctx->slots[ctx->slots_used].delay = delay;
Gilles Peskine44817442018-06-13 18:09:28 +0200953 ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100954 ++ctx->slots_used;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200955 return 0;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100956}
957
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200958# define SSL_ASYNC_INPUT_MAX_SIZE 512
Gilles Peskined3268832018-04-26 06:23:59 +0200959
960typedef enum
961{
962 ASYNC_OP_SIGN,
963 ASYNC_OP_DECRYPT,
964} ssl_async_operation_type_t;
965/* Note that the enum above and the array below need to be kept in sync!
966 * `ssl_async_operation_names[op]` is the name of op for each value `op`
967 * of type `ssl_async_operation_type_t`. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200968static const char *const ssl_async_operation_names[] = {
Gilles Peskined3268832018-04-26 06:23:59 +0200969 "sign",
970 "decrypt",
971};
972
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200973typedef struct {
Gilles Peskine6331d782018-04-26 13:27:43 +0200974 unsigned slot;
Gilles Peskined3268832018-04-26 06:23:59 +0200975 ssl_async_operation_type_t operation_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100976 mbedtls_md_type_t md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100977 unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
978 size_t input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +0200979 unsigned remaining_delay;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100980} ssl_async_operation_context_t;
981
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200982static int ssl_async_start(mbedtls_ssl_context *ssl,
983 mbedtls_x509_crt *cert,
984 ssl_async_operation_type_t op_type,
985 mbedtls_md_type_t md_alg,
986 const unsigned char *input,
987 size_t input_len)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100988{
Gilles Peskine8f97af72018-04-26 11:46:10 +0200989 ssl_async_key_context_t *config_data =
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200990 mbedtls_ssl_conf_get_async_config_data(ssl->conf);
Gilles Peskine6331d782018-04-26 13:27:43 +0200991 unsigned slot;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100992 ssl_async_operation_context_t *ctx = NULL;
Gilles Peskined3268832018-04-26 06:23:59 +0200993 const char *op_name = ssl_async_operation_names[op_type];
Gilles Peskinef1127252018-04-24 13:05:39 +0200994
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100995 {
996 char dn[100];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200997 if (mbedtls_x509_dn_gets(dn, sizeof(dn), &cert->subject) > 0)
998 mbedtls_printf("Async %s callback: looking for DN=%s\n", op_name,
999 dn);
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001000 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001001
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001002 /* Look for a private key that matches the public key in cert.
1003 * Since this test code has the private key inside Mbed TLS,
1004 * we call mbedtls_pk_check_pair to match a private key with the
1005 * public key. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001006 for (slot = 0; slot < config_data->slots_used; slot++) {
1007 if (mbedtls_pk_check_pair(&cert->pk, config_data->slots[slot].pk,
1008 rng_get, &rng) == 0)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001009 break;
1010 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001011 if (slot == config_data->slots_used) {
1012 mbedtls_printf("Async %s callback: no key matches this certificate.\n",
1013 op_name);
1014 return MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001015 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001016 mbedtls_printf("Async %s callback: using key slot %u, delay=%u.\n", op_name,
1017 slot, config_data->slots[slot].delay);
Gilles Peskinef1127252018-04-24 13:05:39 +02001018
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001019 if (config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START) {
1020 mbedtls_printf("Async %s callback: injected error\n", op_name);
1021 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001022 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001023
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001024 if (input_len > SSL_ASYNC_INPUT_MAX_SIZE)
1025 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Gilles Peskinef1127252018-04-24 13:05:39 +02001026
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001027 ctx = mbedtls_calloc(1, sizeof(*ctx));
1028 if (ctx == NULL)
1029 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001030 ctx->slot = slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001031 ctx->operation_type = op_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001032 ctx->md_alg = md_alg;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001033 memcpy(ctx->input, input, input_len);
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001034 ctx->input_len = input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001035 ctx->remaining_delay = config_data->slots[slot].delay;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001036 mbedtls_ssl_set_async_operation_data(ssl, ctx);
Gilles Peskinef1127252018-04-24 13:05:39 +02001037
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001038 if (ctx->remaining_delay == 0)
1039 return 0;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001040 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001041 return MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001042}
1043
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001044static int ssl_async_sign(mbedtls_ssl_context *ssl,
1045 mbedtls_x509_crt *cert,
1046 mbedtls_md_type_t md_alg,
1047 const unsigned char *hash,
1048 size_t hash_len)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001049{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001050 return (ssl_async_start(ssl, cert, ASYNC_OP_SIGN, md_alg, hash, hash_len));
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001051}
1052
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001053static int ssl_async_decrypt(mbedtls_ssl_context *ssl,
1054 mbedtls_x509_crt *cert,
1055 const unsigned char *input,
1056 size_t input_len)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001057{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001058 return (ssl_async_start(ssl, cert, ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE, input,
1059 input_len));
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001060}
1061
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001062static int ssl_async_resume(mbedtls_ssl_context *ssl,
1063 unsigned char *output,
1064 size_t *output_len,
1065 size_t output_size)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001066{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001067 ssl_async_operation_context_t *ctx =
1068 mbedtls_ssl_get_async_operation_data(ssl);
Gilles Peskine8f97af72018-04-26 11:46:10 +02001069 ssl_async_key_context_t *config_data =
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001070 mbedtls_ssl_conf_get_async_config_data(ssl->conf);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001071 ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001072 int ret;
Gilles Peskinef5a99962018-04-30 16:37:23 +02001073 const char *op_name;
Gilles Peskinef1127252018-04-24 13:05:39 +02001074
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001075 if (ctx->remaining_delay > 0) {
Gilles Peskineceb541b2018-04-26 06:30:45 +02001076 --ctx->remaining_delay;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001077 mbedtls_printf("Async resume (slot %u): call %u more times.\n",
1078 ctx->slot, ctx->remaining_delay);
1079 return MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001080 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001081
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001082 switch (ctx->operation_type) {
Gilles Peskined3268832018-04-26 06:23:59 +02001083 case ASYNC_OP_DECRYPT:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001084 ret = mbedtls_pk_decrypt(key_slot->pk, ctx->input, ctx->input_len,
1085 output, output_len, output_size,
1086 config_data->f_rng, config_data->p_rng);
Gilles Peskined3268832018-04-26 06:23:59 +02001087 break;
1088 case ASYNC_OP_SIGN:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001089 ret = mbedtls_pk_sign(key_slot->pk, ctx->md_alg, ctx->input,
1090 ctx->input_len, output, output_size,
1091 output_len, config_data->f_rng,
1092 config_data->p_rng);
Gilles Peskined3268832018-04-26 06:23:59 +02001093 break;
1094 default:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001095 mbedtls_printf(
1096 "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
1097 ctx->slot, (long)ctx->operation_type);
1098 mbedtls_free(ctx);
1099 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Gilles Peskined3268832018-04-26 06:23:59 +02001100 break;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001101 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001102
Gilles Peskinef5a99962018-04-30 16:37:23 +02001103 op_name = ssl_async_operation_names[ctx->operation_type];
1104
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001105 if (config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME) {
1106 mbedtls_printf("Async resume callback: %s done but injected error\n",
1107 op_name);
1108 mbedtls_free(ctx);
1109 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001110 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001111
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001112 mbedtls_printf("Async resume (slot %u): %s done, status=%d.\n", ctx->slot,
1113 op_name, ret);
1114 mbedtls_free(ctx);
1115 return ret;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001116}
1117
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001118static void ssl_async_cancel(mbedtls_ssl_context *ssl)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001119{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001120 ssl_async_operation_context_t *ctx =
1121 mbedtls_ssl_get_async_operation_data(ssl);
1122 mbedtls_printf("Async cancel callback.\n");
1123 mbedtls_free(ctx);
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001124}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001125# endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001126
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001127# if defined(MBEDTLS_USE_PSA_CRYPTO)
1128static psa_status_t psa_setup_psk_key_slot(psa_key_id_t *slot,
1129 psa_algorithm_t alg,
1130 unsigned char *psk,
1131 size_t psk_len)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001132{
1133 psa_status_t status;
Janos Follathbe4efc22019-08-08 11:38:18 +01001134 psa_key_attributes_t key_attributes;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001135
Janos Follathbe4efc22019-08-08 11:38:18 +01001136 key_attributes = psa_key_attributes_init();
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001137 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
1138 psa_set_key_algorithm(&key_attributes, alg);
1139 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001140
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001141 status = psa_import_key(&key_attributes, psk, psk_len, slot);
1142 if (status != PSA_SUCCESS) {
1143 fprintf(stderr, "IMPORT\n");
1144 return status;
Hanno Becker1d911cd2018-11-15 13:06:09 +00001145 }
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001146
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001147 return PSA_SUCCESS;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001148}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001149# endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001150
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001151# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1152int report_cid_usage(mbedtls_ssl_context *ssl,
1153 const char *additional_description)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001154{
1155 int ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001156 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX];
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001157 size_t peer_cid_len;
1158 int cid_negotiated;
1159
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001160 if (opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM)
1161 return 0;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001162
1163 /* Check if the use of a CID has been negotiated */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001164 ret =
1165 mbedtls_ssl_get_peer_cid(ssl, &cid_negotiated, peer_cid, &peer_cid_len);
1166 if (ret != 0) {
1167 mbedtls_printf(
1168 " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
1169 (unsigned int)-ret);
1170 return ret;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001171 }
1172
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001173 if (cid_negotiated == MBEDTLS_SSL_CID_DISABLED) {
1174 if (opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED) {
1175 mbedtls_printf(
1176 "(%s) Use of Connection ID was not offered by client.\n",
1177 additional_description);
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001178 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001179 } else {
1180 size_t idx = 0;
1181 mbedtls_printf("(%s) Use of Connection ID has been negotiated.\n",
1182 additional_description);
1183 mbedtls_printf("(%s) Peer CID (length %u Bytes): ",
1184 additional_description, (unsigned)peer_cid_len);
1185 while (idx < peer_cid_len) {
1186 mbedtls_printf("%02x ", peer_cid[idx]);
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001187 idx++;
1188 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001189 mbedtls_printf("\n");
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001190 }
1191
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001192 return 0;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001193}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001194# endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001195
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001196int main(int argc, char *argv[])
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001197{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001198 int ret = 0, len, written, frags, exchanges_left;
gabor-mezei-arma9eecf12020-07-07 11:11:02 +02001199 int query_config_ret = 0;
Hanno Beckerdcc94e62019-07-03 17:05:43 +01001200 io_ctx_t io_ctx;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001201 unsigned char *buf = 0;
1202# if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1203# if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001204 psa_algorithm_t alg = 0;
Ronald Cronadc2ff22020-09-16 16:49:27 +02001205 psa_key_id_t psk_slot = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001206# endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +02001208 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +02001209 psk_entry *psk_info = NULL;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001210# endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001211 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001212 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02001213 size_t cliip_len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001214# if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 mbedtls_ssl_cookie_ctx cookie_ctx;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001216# endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001217
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001218# if defined(MBEDTLS_X509_CRT_PARSE_C)
1219 mbedtls_x509_crt_profile crt_profile_for_test =
1220 mbedtls_x509_crt_profile_default;
1221# endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001223 mbedtls_ssl_config conf;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001224# if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001225 mbedtls_timing_delay_context timer;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001226# endif
1227# if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001228 unsigned char renego_period[8] = { 0 };
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001229# endif
1230# if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +02001231 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 mbedtls_x509_crt cacert;
1233 mbedtls_x509_crt srvcert;
1234 mbedtls_pk_context pkey;
1235 mbedtls_x509_crt srvcert2;
1236 mbedtls_pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001237 int key_cert_init = 0, key_cert_init2 = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001238# if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001239 ssl_async_key_context_t ssl_async_keys;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001240# endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
1241# endif /* MBEDTLS_X509_CRT_PARSE_C */
1242# if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 mbedtls_dhm_context dhm;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001244# endif
1245# if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 mbedtls_ssl_cache_context cache;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001247# endif
1248# if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001249 mbedtls_ssl_ticket_context ticket_ctx;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001250# endif
1251# if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001252 sni_entry *sni_info = NULL;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001253# endif
1254# if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +01001255 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001256 const mbedtls_ecp_curve_info *curve_cur;
1257# endif
1258# if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +01001259 const char *alpn_list[ALPN_LIST_SIZE];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001260# endif
1261# if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Simon Butchercce68be2018-07-23 14:26:09 +01001262 unsigned char alloc_buf[MEMORY_HEAP_SIZE];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001263# endif
1264# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +01001265 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001266 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckera7d25422019-04-09 17:28:10 +01001267 size_t cid_len = 0;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001268 size_t cid_renego_len = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001269# endif
1270# if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02001271 unsigned char *context_buf = NULL;
Gilles Peskine4e8b5942019-09-20 19:12:31 +02001272 size_t context_buf_len = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001273# endif
Hanno Beckera7d25422019-04-09 17:28:10 +01001274
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001275 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001276 char *p, *q;
1277 const int *list;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001278# if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker5a9942e2018-11-12 17:47:48 +00001279 psa_status_t status;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001280# endif
1281# if defined(MBEDTLS_SSL_EXPORT_KEYS)
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001282 unsigned char eap_tls_keymaterial[16];
1283 unsigned char eap_tls_iv[8];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001284 const char *eap_tls_label = "client EAP encryption";
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001285 eap_tls_keys eap_tls_keying;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001286# if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor65b56ef2019-09-26 16:40:48 +03001287 /*! master keys and master salt for SRTP generated during handshake */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001288 unsigned char
1289 dtls_srtp_key_material[MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH];
1290 const char *dtls_srtp_label = "EXTRACTOR-dtls_srtp";
1291 dtls_srtp_keys dtls_srtp_keying;
1292 const mbedtls_ssl_srtp_profile default_profiles[] = {
1293 MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80,
1294 MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_32,
1295 MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_80, MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_32,
1296 MBEDTLS_TLS_SRTP_UNSET
1297 };
1298# endif /* MBEDTLS_SSL_DTLS_SRTP */
1299# endif /* MBEDTLS_SSL_EXPORT_KEYS */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001300
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001301# if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1302 mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
1303# if defined(MBEDTLS_MEMORY_DEBUG)
Piotr Nowicki0937ed22019-11-26 16:32:40 +01001304 size_t current_heap_memory, peak_heap_memory, heap_blocks;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001305# endif /* MBEDTLS_MEMORY_DEBUG */
1306# endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
Paul Bakker82024bf2013-07-04 11:52:32 +02001307
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001308# if defined(MBEDTLS_TEST_HOOKS)
1309 test_hooks_init();
1310# endif /* MBEDTLS_TEST_HOOKS */
Gilles Peskine53dea742021-02-02 22:55:06 +01001311
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001312 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +02001313 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001314 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001315 mbedtls_net_init(&client_fd);
1316 mbedtls_net_init(&listen_fd);
1317 mbedtls_ssl_init(&ssl);
1318 mbedtls_ssl_config_init(&conf);
1319 rng_init(&rng);
1320# if defined(MBEDTLS_X509_CRT_PARSE_C)
1321 mbedtls_x509_crt_init(&cacert);
1322 mbedtls_x509_crt_init(&srvcert);
1323 mbedtls_pk_init(&pkey);
1324 mbedtls_x509_crt_init(&srvcert2);
1325 mbedtls_pk_init(&pkey2);
1326# if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
1327 memset(&ssl_async_keys, 0, sizeof(ssl_async_keys));
1328# endif
1329# endif
1330# if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1331 mbedtls_dhm_init(&dhm);
1332# endif
1333# if defined(MBEDTLS_SSL_CACHE_C)
1334 mbedtls_ssl_cache_init(&cache);
1335# endif
1336# if defined(MBEDTLS_SSL_SESSION_TICKETS)
1337 mbedtls_ssl_ticket_init(&ticket_ctx);
1338# endif
1339# if defined(MBEDTLS_SSL_ALPN)
1340 memset((void *)alpn_list, 0, sizeof(alpn_list));
1341# endif
1342# if defined(MBEDTLS_SSL_COOKIE_C)
1343 mbedtls_ssl_cookie_init(&cookie_ctx);
1344# endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001345
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001346# if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker5a9942e2018-11-12 17:47:48 +00001347 status = psa_crypto_init();
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001348 if (status != PSA_SUCCESS) {
1349 mbedtls_fprintf(stderr,
1350 "Failed to initialize PSA Crypto implementation: %d\n",
1351 (int)status);
Hanno Becker5a9942e2018-11-12 17:47:48 +00001352 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1353 goto exit;
1354 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001355# endif /* MBEDTLS_USE_PSA_CRYPTO */
1356# if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
1357 mbedtls_test_enable_insecure_external_rng();
1358# endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Hanno Becker5a9942e2018-11-12 17:47:48 +00001359
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001360# if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001361 /* Abort cleanly on SIGTERM and SIGINT */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001362 signal(SIGTERM, term_handler);
1363 signal(SIGINT, term_handler);
1364# endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001365
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001366 if (argc == 0) {
1367usage:
1368 if (ret == 0)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001369 ret = 1;
1370
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001371 mbedtls_printf(USAGE1);
1372 mbedtls_printf(USAGE2);
1373 mbedtls_printf(USAGE3);
1374 mbedtls_printf(USAGE4);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 list = mbedtls_ssl_list_ciphersuites();
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001377 while (*list) {
1378 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name(*list));
Paul Bakkered27a042013-04-18 22:46:23 +02001379 list++;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001380 if (!*list)
Paul Bakkered27a042013-04-18 22:46:23 +02001381 break;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001382 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name(*list));
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001383 list++;
1384 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 mbedtls_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001386 goto exit;
1387 }
1388
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001389 opt.buffer_size = DFL_IO_BUF_LEN;
1390 opt.server_addr = DFL_SERVER_ADDR;
1391 opt.server_port = DFL_SERVER_PORT;
1392 opt.debug_level = DFL_DEBUG_LEVEL;
1393 opt.event = DFL_EVENT;
1394 opt.response_size = DFL_RESPONSE_SIZE;
1395 opt.nbio = DFL_NBIO;
1396 opt.cid_enabled = DFL_CID_ENABLED;
1397 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
1398 opt.cid_val = DFL_CID_VALUE;
1399 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
1400 opt.read_timeout = DFL_READ_TIMEOUT;
1401 opt.ca_file = DFL_CA_FILE;
1402 opt.ca_path = DFL_CA_PATH;
1403 opt.crt_file = DFL_CRT_FILE;
1404 opt.key_file = DFL_KEY_FILE;
1405 opt.key_pwd = DFL_KEY_PWD;
1406 opt.crt_file2 = DFL_CRT_FILE2;
1407 opt.key_file2 = DFL_KEY_FILE2;
1408 opt.key_pwd2 = DFL_KEY_PWD2;
1409 opt.async_operations = DFL_ASYNC_OPERATIONS;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001410 opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
1411 opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
1412 opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001413 opt.psk = DFL_PSK;
1414# if defined(MBEDTLS_USE_PSA_CRYPTO)
1415 opt.psk_opaque = DFL_PSK_OPAQUE;
1416 opt.psk_list_opaque = DFL_PSK_LIST_OPAQUE;
1417# endif
1418# if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1419 opt.ca_callback = DFL_CA_CALLBACK;
1420# endif
1421 opt.psk_identity = DFL_PSK_IDENTITY;
1422 opt.psk_list = DFL_PSK_LIST;
1423 opt.ecjpake_pw = DFL_ECJPAKE_PW;
1424 opt.force_ciphersuite[0] = DFL_FORCE_CIPHER;
1425 opt.renegotiation = DFL_RENEGOTIATION;
1426 opt.allow_legacy = DFL_ALLOW_LEGACY;
1427 opt.renegotiate = DFL_RENEGOTIATE;
1428 opt.renego_delay = DFL_RENEGO_DELAY;
1429 opt.renego_period = DFL_RENEGO_PERIOD;
1430 opt.exchanges = DFL_EXCHANGES;
1431 opt.min_version = DFL_MIN_VERSION;
1432 opt.max_version = DFL_MAX_VERSION;
1433 opt.allow_sha1 = DFL_SHA1;
1434 opt.auth_mode = DFL_AUTH_MODE;
1435 opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST;
1436 opt.mfl_code = DFL_MFL_CODE;
1437 opt.trunc_hmac = DFL_TRUNC_HMAC;
1438 opt.tickets = DFL_TICKETS;
1439 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
1440 opt.cache_max = DFL_CACHE_MAX;
1441 opt.cache_timeout = DFL_CACHE_TIMEOUT;
1442 opt.sni = DFL_SNI;
1443 opt.alpn_string = DFL_ALPN_STRING;
1444 opt.curves = DFL_CURVES;
1445 opt.dhm_file = DFL_DHM_FILE;
1446 opt.transport = DFL_TRANSPORT;
1447 opt.cookies = DFL_COOKIES;
1448 opt.anti_replay = DFL_ANTI_REPLAY;
1449 opt.hs_to_min = DFL_HS_TO_MIN;
1450 opt.hs_to_max = DFL_HS_TO_MAX;
1451 opt.dtls_mtu = DFL_DTLS_MTU;
1452 opt.dgram_packing = DFL_DGRAM_PACKING;
1453 opt.badmac_limit = DFL_BADMAC_LIMIT;
1454 opt.extended_ms = DFL_EXTENDED_MS;
1455 opt.etm = DFL_ETM;
1456 opt.serialize = DFL_SERIALIZE;
1457 opt.context_file = DFL_CONTEXT_FILE;
1458 opt.eap_tls = DFL_EAP_TLS;
1459 opt.reproducible = DFL_REPRODUCIBLE;
1460 opt.nss_keylog = DFL_NSS_KEYLOG;
1461 opt.nss_keylog_file = DFL_NSS_KEYLOG_FILE;
1462 opt.query_config_mode = DFL_QUERY_CONFIG_MODE;
1463 opt.use_srtp = DFL_USE_SRTP;
1464 opt.force_srtp_profile = DFL_SRTP_FORCE_PROFILE;
1465 opt.support_mki = DFL_SRTP_SUPPORT_MKI;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001466
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001467 for (i = 1; i < argc; i++) {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001468 p = argv[i];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001469 if ((q = strchr(p, '=')) == NULL)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001470 goto usage;
1471 *q++ = '\0';
1472
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001473 if (strcmp(p, "server_port") == 0)
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001474 opt.server_port = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001475 else if (strcmp(p, "server_addr") == 0)
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001476 opt.server_addr = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001477 else if (strcmp(p, "dtls") == 0) {
1478 int t = atoi(q);
1479 if (t == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001481 else if (t == 1)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001483 else
1484 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001485 } else if (strcmp(p, "debug_level") == 0) {
1486 opt.debug_level = atoi(q);
1487 if (opt.debug_level < 0 || opt.debug_level > 65535)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001488 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001489 } else if (strcmp(p, "nbio") == 0) {
1490 opt.nbio = atoi(q);
1491 if (opt.nbio < 0 || opt.nbio > 2)
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001492 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001493 } else if (strcmp(p, "event") == 0) {
1494 opt.event = atoi(q);
1495 if (opt.event < 0 || opt.event > 2)
Hanno Becker16970d22017-10-10 15:56:37 +01001496 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001497 } else if (strcmp(p, "read_timeout") == 0)
1498 opt.read_timeout = atoi(q);
1499 else if (strcmp(p, "buffer_size") == 0) {
1500 opt.buffer_size = atoi(q);
1501 if (opt.buffer_size < 1)
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001502 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001503 } else if (strcmp(p, "response_size") == 0) {
1504 opt.response_size = atoi(q);
1505 if (opt.response_size < 0 ||
1506 opt.response_size > MBEDTLS_SSL_OUT_CONTENT_LEN)
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001507 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001508 if (opt.buffer_size < opt.response_size)
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001509 opt.buffer_size = opt.response_size;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001510 } else if (strcmp(p, "ca_file") == 0)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001511 opt.ca_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001512 else if (strcmp(p, "ca_path") == 0)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001513 opt.ca_path = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001514 else if (strcmp(p, "crt_file") == 0)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001515 opt.crt_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001516 else if (strcmp(p, "key_file") == 0)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001517 opt.key_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001518 else if (strcmp(p, "key_pwd") == 0)
Hanno Beckere58a6302018-11-07 16:20:16 +00001519 opt.key_pwd = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001520 else if (strcmp(p, "crt_file2") == 0)
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001521 opt.crt_file2 = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001522 else if (strcmp(p, "key_file2") == 0)
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001523 opt.key_file2 = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001524 else if (strcmp(p, "key_pwd2") == 0)
Hanno Beckere58a6302018-11-07 16:20:16 +00001525 opt.key_pwd2 = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001526 else if (strcmp(p, "dhm_file") == 0)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001527 opt.dhm_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001528# if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
1529 else if (strcmp(p, "async_operations") == 0)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001530 opt.async_operations = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001531 else if (strcmp(p, "async_private_delay1") == 0)
1532 opt.async_private_delay1 = atoi(q);
1533 else if (strcmp(p, "async_private_delay2") == 0)
1534 opt.async_private_delay2 = atoi(q);
1535 else if (strcmp(p, "async_private_error") == 0) {
1536 int n = atoi(q);
1537 if (n < -SSL_ASYNC_INJECT_ERROR_MAX ||
1538 n > SSL_ASYNC_INJECT_ERROR_MAX) {
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001539 ret = 2;
1540 goto usage;
1541 }
1542 opt.async_private_error = n;
1543 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001544# endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
1545# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1546 else if (strcmp(p, "cid") == 0) {
1547 opt.cid_enabled = atoi(q);
1548 if (opt.cid_enabled != 0 && opt.cid_enabled != 1)
Hanno Beckera7d25422019-04-09 17:28:10 +01001549 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001550 } else if (strcmp(p, "cid_renego") == 0) {
1551 opt.cid_enabled_renego = atoi(q);
1552 if (opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001553 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001554 } else if (strcmp(p, "cid_val") == 0) {
Hanno Beckera7d25422019-04-09 17:28:10 +01001555 opt.cid_val = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001556 } else if (strcmp(p, "cid_val_renego") == 0) {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001557 opt.cid_val_renego = q;
1558 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001559# endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1560 else if (strcmp(p, "psk") == 0)
Paul Bakkerfbb17802013-04-17 19:10:21 +02001561 opt.psk = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001562# if defined(MBEDTLS_USE_PSA_CRYPTO)
1563 else if (strcmp(p, "psk_opaque") == 0)
1564 opt.psk_opaque = atoi(q);
1565 else if (strcmp(p, "psk_list_opaque") == 0)
1566 opt.psk_list_opaque = atoi(q);
1567# endif
1568# if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1569 else if (strcmp(p, "ca_callback") == 0)
1570 opt.ca_callback = atoi(q);
1571# endif
1572 else if (strcmp(p, "psk_identity") == 0)
Paul Bakkerfbb17802013-04-17 19:10:21 +02001573 opt.psk_identity = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001574 else if (strcmp(p, "psk_list") == 0)
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001575 opt.psk_list = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001576 else if (strcmp(p, "ecjpake_pw") == 0)
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001577 opt.ecjpake_pw = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001578 else if (strcmp(p, "force_ciphersuite") == 0) {
1579 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(q);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001580
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001581 if (opt.force_ciphersuite[0] == 0) {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001582 ret = 2;
1583 goto usage;
1584 }
1585 opt.force_ciphersuite[1] = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001586 } else if (strcmp(p, "curves") == 0)
Hanno Beckere6706e62017-05-15 16:05:15 +01001587 opt.curves = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001588 else if (strcmp(p, "renegotiation") == 0) {
1589 opt.renegotiation = (atoi(q)) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1590 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
1591 } else if (strcmp(p, "allow_legacy") == 0) {
1592 switch (atoi(q)) {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001593 case -1:
1594 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1595 break;
1596 case 0:
1597 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1598 break;
1599 case 1:
1600 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1601 break;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001602 default:
1603 goto usage;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001604 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001605 } else if (strcmp(p, "renegotiate") == 0) {
1606 opt.renegotiate = atoi(q);
1607 if (opt.renegotiate < 0 || opt.renegotiate > 1)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001608 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001609 } else if (strcmp(p, "renego_delay") == 0) {
1610 opt.renego_delay = atoi(q);
1611 } else if (strcmp(p, "renego_period") == 0) {
1612# if defined(_MSC_VER)
1613 opt.renego_period = _strtoui64(q, NULL, 10);
1614# else
1615 if (sscanf(q, "%" SCNu64, &opt.renego_period) != 1)
Andres AG0b736db2017-02-08 14:05:57 +00001616 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001617# endif /* _MSC_VER */
1618 if (opt.renego_period < 2)
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001619 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001620 } else if (strcmp(p, "exchanges") == 0) {
1621 opt.exchanges = atoi(q);
1622 if (opt.exchanges < 0)
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001623 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001624 } else if (strcmp(p, "min_version") == 0) {
1625 if (strcmp(q, "tls1_2") == 0 || strcmp(q, "dtls1_2") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001627 else
1628 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001629 } else if (strcmp(p, "max_version") == 0) {
1630 if (strcmp(q, "tls1_2") == 0 || strcmp(q, "dtls1_2") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001632 else
1633 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001634 } else if (strcmp(p, "allow_sha1") == 0) {
1635 switch (atoi(q)) {
1636 case 0:
1637 opt.allow_sha1 = 0;
1638 break;
1639 case 1:
1640 opt.allow_sha1 = 1;
1641 break;
1642 default:
1643 goto usage;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001644 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001645 } else if (strcmp(p, "force_version") == 0) {
1646 if (strcmp(q, "tls1_2") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1648 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001649 } else if (strcmp(q, "dtls1_2") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1651 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1652 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001653 } else
Paul Bakkerc1516be2013-06-29 16:01:32 +02001654 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001655 } else if (strcmp(p, "auth_mode") == 0) {
1656 if ((opt.auth_mode = get_auth_mode(q)) < 0)
Paul Bakker91ebfb52012-11-23 14:04:08 +01001657 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001658 } else if (strcmp(p, "cert_req_ca_list") == 0) {
1659 opt.cert_req_ca_list = atoi(q);
1660 if (opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1)
Janos Follath4817e272017-04-10 13:44:33 +01001661 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001662 } else if (strcmp(p, "max_frag_len") == 0) {
1663 if (strcmp(q, "512") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001665 else if (strcmp(q, "1024") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001667 else if (strcmp(q, "2048") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001669 else if (strcmp(q, "4096") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001671 else
1672 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001673 } else if (strcmp(p, "alpn") == 0) {
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001674 opt.alpn_string = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001675 } else if (strcmp(p, "trunc_hmac") == 0) {
1676 switch (atoi(q)) {
1677 case 0:
1678 opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED;
1679 break;
1680 case 1:
1681 opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
1682 break;
1683 default:
1684 goto usage;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001685 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001686 } else if (strcmp(p, "extended_ms") == 0) {
1687 switch (atoi(q)) {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001688 case 0:
1689 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1690 break;
1691 case 1:
1692 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1693 break;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001694 default:
1695 goto usage;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001696 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001697 } else if (strcmp(p, "etm") == 0) {
1698 switch (atoi(q)) {
1699 case 0:
1700 opt.etm = MBEDTLS_SSL_ETM_DISABLED;
1701 break;
1702 case 1:
1703 opt.etm = MBEDTLS_SSL_ETM_ENABLED;
1704 break;
1705 default:
1706 goto usage;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001707 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001708 } else if (strcmp(p, "tickets") == 0) {
1709 opt.tickets = atoi(q);
1710 if (opt.tickets < 0 || opt.tickets > 1)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001711 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001712 } else if (strcmp(p, "ticket_timeout") == 0) {
1713 opt.ticket_timeout = atoi(q);
1714 if (opt.ticket_timeout < 0)
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001715 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001716 } else if (strcmp(p, "cache_max") == 0) {
1717 opt.cache_max = atoi(q);
1718 if (opt.cache_max < 0)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001719 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001720 } else if (strcmp(p, "cache_timeout") == 0) {
1721 opt.cache_timeout = atoi(q);
1722 if (opt.cache_timeout < 0)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001723 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001724 } else if (strcmp(p, "cookies") == 0) {
1725 opt.cookies = atoi(q);
1726 if (opt.cookies < -1 || opt.cookies > 1)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001727 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001728 } else if (strcmp(p, "anti_replay") == 0) {
1729 opt.anti_replay = atoi(q);
1730 if (opt.anti_replay < 0 || opt.anti_replay > 1)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001731 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001732 } else if (strcmp(p, "badmac_limit") == 0) {
1733 opt.badmac_limit = atoi(q);
1734 if (opt.badmac_limit < 0)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001735 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001736 } else if (strcmp(p, "hs_timeout") == 0) {
1737 if ((p = strchr(q, '-')) == NULL)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001738 goto usage;
1739 *p++ = '\0';
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001740 opt.hs_to_min = atoi(q);
1741 opt.hs_to_max = atoi(p);
1742 if (opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001743 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001744 } else if (strcmp(p, "mtu") == 0) {
1745 opt.dtls_mtu = atoi(q);
1746 if (opt.dtls_mtu < 0)
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001747 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001748 } else if (strcmp(p, "dgram_packing") == 0) {
1749 opt.dgram_packing = atoi(q);
1750 if (opt.dgram_packing != 0 && opt.dgram_packing != 1) {
Hanno Beckere7675d02018-08-14 13:28:56 +01001751 goto usage;
1752 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001753 } else if (strcmp(p, "sni") == 0) {
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001754 opt.sni = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001755 } else if (strcmp(p, "query_config") == 0) {
gabor-mezei-arma9eecf12020-07-07 11:11:02 +02001756 opt.query_config_mode = 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001757 query_config_ret = query_config(q);
gabor-mezei-arm78595852020-04-28 10:40:30 +02001758 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001759 } else if (strcmp(p, "serialize") == 0) {
1760 opt.serialize = atoi(q);
1761 if (opt.serialize < 0 || opt.serialize > 2)
Jarno Lamsa9831c8a2019-05-29 13:33:32 +03001762 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001763 } else if (strcmp(p, "context_file") == 0) {
Piotr Nowicki3de298f2020-04-16 14:35:19 +02001764 opt.context_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001765 } else if (strcmp(p, "eap_tls") == 0) {
1766 opt.eap_tls = atoi(q);
1767 if (opt.eap_tls < 0 || opt.eap_tls > 1)
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001768 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001769 } else if (strcmp(p, "reproducible") == 0) {
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001770 opt.reproducible = 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001771 } else if (strcmp(p, "nss_keylog") == 0) {
1772 opt.nss_keylog = atoi(q);
1773 if (opt.nss_keylog < 0 || opt.nss_keylog > 1)
Hanno Becker48f3a3d2019-08-29 06:31:22 +01001774 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001775 } else if (strcmp(p, "nss_keylog_file") == 0) {
Hanno Becker48f3a3d2019-08-29 06:31:22 +01001776 opt.nss_keylog_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001777 } else if (strcmp(p, "use_srtp") == 0) {
1778 opt.use_srtp = atoi(q);
1779 } else if (strcmp(p, "srtp_force_profile") == 0) {
1780 opt.force_srtp_profile = atoi(q);
1781 } else if (strcmp(p, "support_mki") == 0) {
1782 opt.support_mki = atoi(q);
1783 } else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001784 goto usage;
1785 }
1786
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001787 if (opt.nss_keylog != 0 && opt.eap_tls != 0) {
1788 mbedtls_printf(
1789 "Error: eap_tls and nss_keylog options cannot be used together.\n");
Hanno Beckerbc5308c2019-09-09 11:38:51 +01001790 goto usage;
1791 }
1792
Hanno Becker16970d22017-10-10 15:56:37 +01001793 /* Event-driven IO is incompatible with the above custom
1794 * receive and send functions, as the polling builds on
1795 * refers to the underlying net_context. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001796 if (opt.event == 1 && opt.nbio != 1) {
1797 mbedtls_printf(
1798 "Warning: event-driven IO mandates nbio=1 - overwrite\n");
Hanno Becker16970d22017-10-10 15:56:37 +01001799 opt.nbio = 1;
1800 }
1801
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001802# if defined(MBEDTLS_DEBUG_C)
1803 mbedtls_debug_set_threshold(opt.debug_level);
1804# endif
1805 buf = mbedtls_calloc(1, opt.buffer_size + 1);
1806 if (buf == NULL) {
1807 mbedtls_printf("Could not allocate %u bytes\n", opt.buffer_size);
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001808 ret = 3;
1809 goto exit;
1810 }
Paul Bakkerc73079a2014-04-25 16:34:30 +02001811
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001812# if defined(MBEDTLS_USE_PSA_CRYPTO)
1813 if (opt.psk_opaque != 0) {
1814 if (strlen(opt.psk) == 0) {
1815 mbedtls_printf(
1816 "psk_opaque set but no psk to be imported specified.\n");
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001817 ret = 2;
1818 goto usage;
1819 }
1820
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001821 if (opt.force_ciphersuite[0] <= 0) {
1822 mbedtls_printf(
1823 "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n");
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001824 ret = 2;
1825 goto usage;
1826 }
1827 }
1828
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001829 if (opt.psk_list_opaque != 0) {
1830 if (opt.psk_list == NULL) {
1831 mbedtls_printf(
1832 "psk_slot set but no psk to be imported specified.\n");
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001833 ret = 2;
1834 goto usage;
1835 }
1836
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001837 if (opt.force_ciphersuite[0] <= 0) {
1838 mbedtls_printf(
1839 "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n");
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001840 ret = 2;
1841 goto usage;
1842 }
1843 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001844# endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001845
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001846 if (opt.force_ciphersuite[0] > 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001848 ciphersuite_info =
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001849 mbedtls_ssl_ciphersuite_from_id(opt.force_ciphersuite[0]);
Paul Bakkerc1516be2013-06-29 16:01:32 +02001850
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001851 if (opt.max_version != -1 &&
1852 ciphersuite_info->min_minor_ver > opt.max_version) {
1853 mbedtls_printf(
1854 "forced ciphersuite not allowed with this protocol version\n");
Paul Bakker5b55b792013-07-19 13:43:43 +02001855 ret = 2;
1856 goto usage;
1857 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001858 if (opt.min_version != -1 &&
1859 ciphersuite_info->max_minor_ver < opt.min_version) {
1860 mbedtls_printf(
1861 "forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +02001862 ret = 2;
1863 goto usage;
1864 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001865
1866 /* If we select a version that's not supported by
1867 * this suite, then there will be no common ciphersuite... */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001868 if (opt.max_version == -1 ||
1869 opt.max_version > ciphersuite_info->max_minor_ver) {
Paul Bakker5b55b792013-07-19 13:43:43 +02001870 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001871 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001872 if (opt.min_version < ciphersuite_info->min_minor_ver) {
Paul Bakker5b55b792013-07-19 13:43:43 +02001873 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001874 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001875
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001876# if defined(MBEDTLS_USE_PSA_CRYPTO)
1877 if (opt.psk_opaque != 0 || opt.psk_list_opaque != 0) {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001878 /* Ensure that the chosen ciphersuite is PSK-only; we must know
1879 * the ciphersuite in advance to set the correct policy for the
1880 * PSK key slot. This limitation might go away in the future. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001881 if (ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
1882 opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3) {
1883 mbedtls_printf(
1884 "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n");
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001885 ret = 2;
1886 goto usage;
1887 }
1888
1889 /* Determine KDF algorithm the opaque PSK will be used in. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001890# if defined(MBEDTLS_SHA384_C)
1891 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001892 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1893 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001894# endif /* MBEDTLS_SHA384_C */
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001895 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1896 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001897# endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerc1516be2013-06-29 16:01:32 +02001898 }
1899
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001900# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1901 if (mbedtls_test_unhexify(cid, sizeof(cid), opt.cid_val, &cid_len) != 0) {
1902 mbedtls_printf("CID not valid hex\n");
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001903 goto exit;
1904 }
1905
1906 /* Keep CID settings for renegotiation unless
1907 * specified otherwise. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001908 if (opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001909 opt.cid_enabled_renego = opt.cid_enabled;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001910 if (opt.cid_val_renego == DFL_CID_VALUE_RENEGO)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001911 opt.cid_val_renego = opt.cid_val;
1912
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001913 if (mbedtls_test_unhexify(cid_renego, sizeof(cid_renego),
1914 opt.cid_val_renego, &cid_renego_len) != 0) {
1915 mbedtls_printf("CID not valid hex\n");
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001916 goto exit;
1917 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001918# endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +01001919
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001920# if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001921 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001922 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001923 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001924 if (mbedtls_test_unhexify(psk, sizeof(psk), opt.psk, &psk_len) != 0) {
1925 mbedtls_printf("pre-shared key not valid hex\n");
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001926 goto exit;
1927 }
1928
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001929 if (opt.psk_list != NULL) {
1930 if ((psk_info = psk_parse(opt.psk_list)) == NULL) {
1931 mbedtls_printf("psk_list invalid");
Paul Bakkerfbb17802013-04-17 19:10:21 +02001932 goto exit;
1933 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001934 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001935# endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001936
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001937# if defined(MBEDTLS_ECP_C)
1938 if (opt.curves != NULL) {
1939 p = (char *)opt.curves;
Hanno Beckere6706e62017-05-15 16:05:15 +01001940 i = 0;
1941
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001942 if (strcmp(p, "none") == 0) {
Hanno Beckere6706e62017-05-15 16:05:15 +01001943 curve_list[0] = MBEDTLS_ECP_DP_NONE;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001944 } else if (strcmp(p, "default") != 0) {
Hanno Beckere6706e62017-05-15 16:05:15 +01001945 /* Leave room for a final NULL in curve list */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001946 while (i < CURVE_LIST_SIZE - 1 && *p != '\0') {
Hanno Beckere6706e62017-05-15 16:05:15 +01001947 q = p;
1948
1949 /* Terminate the current string */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001950 while (*p != ',' && *p != '\0')
Hanno Beckere6706e62017-05-15 16:05:15 +01001951 p++;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001952 if (*p == ',')
Hanno Beckere6706e62017-05-15 16:05:15 +01001953 *p++ = '\0';
1954
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001955 if ((curve_cur = mbedtls_ecp_curve_info_from_name(q)) != NULL) {
Hanno Beckere6706e62017-05-15 16:05:15 +01001956 curve_list[i++] = curve_cur->grp_id;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001957 } else {
1958 mbedtls_printf("unknown curve %s\n", q);
1959 mbedtls_printf("supported curves: ");
1960 for (curve_cur = mbedtls_ecp_curve_list();
Hanno Beckere6706e62017-05-15 16:05:15 +01001961 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001962 curve_cur++) {
1963 mbedtls_printf("%s ", curve_cur->name);
Hanno Beckere6706e62017-05-15 16:05:15 +01001964 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001965 mbedtls_printf("\n");
Hanno Beckere6706e62017-05-15 16:05:15 +01001966 goto exit;
1967 }
1968 }
1969
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001970 mbedtls_printf("Number of curves: %d\n", i);
Hanno Beckere6706e62017-05-15 16:05:15 +01001971
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001972 if (i == CURVE_LIST_SIZE - 1 && *p != '\0') {
1973 mbedtls_printf("curves list too long, maximum %d",
1974 CURVE_LIST_SIZE - 1);
Hanno Beckere6706e62017-05-15 16:05:15 +01001975 goto exit;
1976 }
1977
1978 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1979 }
1980 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001981# endif /* MBEDTLS_ECP_C */
Hanno Beckere6706e62017-05-15 16:05:15 +01001982
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001983# if defined(MBEDTLS_SSL_ALPN)
1984 if (opt.alpn_string != NULL) {
1985 p = (char *)opt.alpn_string;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001986 i = 0;
1987
1988 /* Leave room for a final NULL in alpn_list */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001989 while (i < ALPN_LIST_SIZE - 1 && *p != '\0') {
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001990 alpn_list[i++] = p;
1991
1992 /* Terminate the current string and move on to next one */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001993 while (*p != ',' && *p != '\0')
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001994 p++;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001995 if (*p == ',')
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001996 *p++ = '\0';
1997 }
1998 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001999# endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002000
Paul Bakkerfbb17802013-04-17 19:10:21 +02002001 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002002 * 0. Initialize the RNG and the session data
2003 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002004 mbedtls_printf("\n . Seeding the random number generator...");
2005 fflush(stdout);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002006
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002007 ret = rng_seed(&rng, opt.reproducible, pers);
2008 if (ret != 0)
Gilles Peskinedaa94c42021-01-13 18:38:27 +01002009 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002010 mbedtls_printf(" ok\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002011
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002012# if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002013 /*
2014 * 1.1. Load the trusted CA
2015 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002016 mbedtls_printf(" . Loading the CA root certificate ...");
2017 fflush(stdout);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002018
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002019 if (strcmp(opt.ca_path, "none") == 0 || strcmp(opt.ca_file, "none") == 0) {
Hanno Becker8174bdf2019-03-05 16:22:07 +00002020 ret = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002021 } else
2022# if defined(MBEDTLS_FS_IO)
2023 if (strlen(opt.ca_path))
2024 ret = mbedtls_x509_crt_parse_path(&cacert, opt.ca_path);
2025 else if (strlen(opt.ca_file))
2026 ret = mbedtls_x509_crt_parse_file(&cacert, opt.ca_file);
Hanno Becker8174bdf2019-03-05 16:22:07 +00002027 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002028# endif
Hanno Becker09b8cae2019-02-01 08:13:19 +00002029 {
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002030# if defined(MBEDTLS_PEM_PARSE_C)
2031 for (i = 0; mbedtls_test_cas[i] != NULL; i++) {
2032 ret = mbedtls_x509_crt_parse(
2033 &cacert, (const unsigned char *)mbedtls_test_cas[i],
2034 mbedtls_test_cas_len[i]);
2035 if (ret != 0)
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002036 break;
2037 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002038 if (ret == 0)
2039# endif /* MBEDTLS_PEM_PARSE_C */
2040 for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) {
2041 ret = mbedtls_x509_crt_parse_der(
2042 &cacert, (const unsigned char *)mbedtls_test_cas_der[i],
2043 mbedtls_test_cas_der_len[i]);
2044 if (ret != 0)
2045 break;
2046 }
Hanno Becker09b8cae2019-02-01 08:13:19 +00002047 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002048 if (ret < 0) {
2049 mbedtls_printf(
2050 " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
2051 (unsigned int)-ret);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002052 goto exit;
2053 }
2054
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002055 mbedtls_printf(" ok (%d skipped)\n", ret);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002056
2057 /*
2058 * 1.2. Load own certificate and private key
2059 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002060 mbedtls_printf(" . Loading the server cert. and key...");
2061 fflush(stdout);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002062
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002063# if defined(MBEDTLS_FS_IO)
2064 if (strlen(opt.crt_file) && strcmp(opt.crt_file, "none") != 0) {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002065 key_cert_init++;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002066 if ((ret = mbedtls_x509_crt_parse_file(&srvcert, opt.crt_file)) != 0) {
2067 mbedtls_printf(
2068 " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
2069 (unsigned int)-ret);
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002070 goto exit;
2071 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002072 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002073 if (strlen(opt.key_file) && strcmp(opt.key_file, "none") != 0) {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002074 key_cert_init++;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002075 if ((ret = mbedtls_pk_parse_keyfile(&pkey, opt.key_file, opt.key_pwd,
2076 rng_get, &rng)) != 0) {
2077 mbedtls_printf(
2078 " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n",
2079 (unsigned int)-ret);
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002080 goto exit;
2081 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002082 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002083 if (key_cert_init == 1) {
2084 mbedtls_printf(
2085 " failed\n ! crt_file without key_file or vice-versa\n\n");
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002086 goto exit;
2087 }
2088
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002089 if (strlen(opt.crt_file2) && strcmp(opt.crt_file2, "none") != 0) {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002090 key_cert_init2++;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002091 if ((ret = mbedtls_x509_crt_parse_file(&srvcert2, opt.crt_file2)) !=
2092 0) {
2093 mbedtls_printf(
2094 " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
2095 (unsigned int)-ret);
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002096 goto exit;
2097 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002098 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002099 if (strlen(opt.key_file2) && strcmp(opt.key_file2, "none") != 0) {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002100 key_cert_init2++;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002101 if ((ret = mbedtls_pk_parse_keyfile(&pkey2, opt.key_file2, opt.key_pwd2,
2102 rng_get, &rng)) != 0) {
2103 mbedtls_printf(
2104 " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
2105 (unsigned int)-ret);
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002106 goto exit;
2107 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002108 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002109 if (key_cert_init2 == 1) {
2110 mbedtls_printf(
2111 " failed\n ! crt_file2 without key_file2 or vice-versa\n\n");
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002112 goto exit;
2113 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002114# endif
2115 if (key_cert_init == 0 && strcmp(opt.crt_file, "none") != 0 &&
2116 strcmp(opt.key_file, "none") != 0 && key_cert_init2 == 0 &&
2117 strcmp(opt.crt_file2, "none") != 0 &&
2118 strcmp(opt.key_file2, "none") != 0) {
2119# if defined(MBEDTLS_RSA_C)
2120 if ((ret = mbedtls_x509_crt_parse(
2121 &srvcert, (const unsigned char *)mbedtls_test_srv_crt_rsa,
2122 mbedtls_test_srv_crt_rsa_len)) != 0) {
2123 mbedtls_printf(
2124 " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
2125 (unsigned int)-ret);
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002126 goto exit;
2127 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002128 if ((ret = mbedtls_pk_parse_key(
2129 &pkey, (const unsigned char *)mbedtls_test_srv_key_rsa,
2130 mbedtls_test_srv_key_rsa_len, NULL, 0, rng_get, &rng)) != 0) {
2131 mbedtls_printf(
2132 " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
2133 (unsigned int)-ret);
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002134 goto exit;
2135 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002136 key_cert_init = 2;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002137# endif /* MBEDTLS_RSA_C */
2138# if defined(MBEDTLS_ECDSA_C)
2139 if ((ret = mbedtls_x509_crt_parse(
2140 &srvcert2, (const unsigned char *)mbedtls_test_srv_crt_ec,
2141 mbedtls_test_srv_crt_ec_len)) != 0) {
2142 mbedtls_printf(" failed\n ! x509_crt_parse2 returned -0x%x\n\n",
2143 (unsigned int)-ret);
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002144 goto exit;
2145 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002146 if ((ret = mbedtls_pk_parse_key(
2147 &pkey2, (const unsigned char *)mbedtls_test_srv_key_ec,
2148 mbedtls_test_srv_key_ec_len, NULL, 0, rng_get, &rng)) != 0) {
2149 mbedtls_printf(" failed\n ! pk_parse_key2 returned -0x%x\n\n",
2150 (unsigned int)-ret);
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002151 goto exit;
2152 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002153 key_cert_init2 = 2;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002154# endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002155 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002156
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002157 mbedtls_printf(" ok\n");
2158# endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002159
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002160# if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
2161 if (opt.dhm_file != NULL) {
2162 mbedtls_printf(" . Loading DHM parameters...");
2163 fflush(stdout);
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002164
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002165 if ((ret = mbedtls_dhm_parse_dhmfile(&dhm, opt.dhm_file)) != 0) {
2166 mbedtls_printf(
2167 " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
2168 (unsigned int)-ret);
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002169 goto exit;
2170 }
2171
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002172 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002173 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002174# endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002175
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002176# if defined(SNI_OPTION)
2177 if (opt.sni != NULL) {
2178 mbedtls_printf(" . Setting up SNI information...");
2179 fflush(stdout);
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002180
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002181 if ((sni_info = sni_parse(opt.sni)) == NULL) {
2182 mbedtls_printf(" failed\n");
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002183 goto exit;
2184 }
2185
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002186 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002187 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002188# endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002189
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002190 /*
2191 * 2. Setup the listening TCP socket
2192 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002193 mbedtls_printf(" . Bind on %s://%s:%s/ ...",
2194 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" :
2195 "udp",
2196 opt.server_addr ? opt.server_addr : "*", opt.server_port);
2197 fflush(stdout);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002198
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002199 if ((ret = mbedtls_net_bind(&listen_fd, opt.server_addr, opt.server_port,
2200 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2201 MBEDTLS_NET_PROTO_TCP :
2202 MBEDTLS_NET_PROTO_UDP)) != 0) {
2203 mbedtls_printf(" failed\n ! mbedtls_net_bind returned -0x%x\n\n",
2204 (unsigned int)-ret);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002205 goto exit;
2206 }
2207
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002208 mbedtls_printf(" ok\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002209
2210 /*
2211 * 3. Setup stuff
2212 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002213 mbedtls_printf(" . Setting up the SSL/TLS structure...");
2214 fflush(stdout);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002215
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002216 if ((ret = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER,
2217 opt.transport,
2218 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
2219 mbedtls_printf(
2220 " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
2221 (unsigned int)-ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002222 goto exit;
2223 }
2224
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002225# if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02002226 /* The default algorithms profile disables SHA-1, but our tests still
2227 rely on it heavily. Hence we allow it here. A real-world server
2228 should use the default profile unless there is a good reason not to. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002229 if (opt.allow_sha1 > 0) {
2230 crt_profile_for_test.allowed_mds |=
2231 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1);
2232 mbedtls_ssl_conf_cert_profile(&conf, &crt_profile_for_test);
2233 mbedtls_ssl_conf_sig_hashes(&conf, ssl_sig_hashes_for_test);
Gilles Peskinebc70a182017-05-09 15:59:24 +02002234 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002235# endif /* MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskineef86ab22017-05-05 18:59:02 +02002236
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002237 if (opt.auth_mode != DFL_AUTH_MODE)
2238 mbedtls_ssl_conf_authmode(&conf, opt.auth_mode);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002239
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002240 if (opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST)
2241 mbedtls_ssl_conf_cert_req_ca_list(&conf, opt.cert_req_ca_list);
Janos Follath4817e272017-04-10 13:44:33 +01002242
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002243# if defined(MBEDTLS_SSL_PROTO_DTLS)
2244 if (opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX)
2245 mbedtls_ssl_conf_handshake_timeout(&conf, opt.hs_to_min, opt.hs_to_max);
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002246
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002247 if (opt.dgram_packing != DFL_DGRAM_PACKING)
2248 mbedtls_ssl_set_datagram_packing(&ssl, opt.dgram_packing);
2249# endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002250
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002251# if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2252 if ((ret = mbedtls_ssl_conf_max_frag_len(&conf, opt.mfl_code)) != 0) {
2253 mbedtls_printf(
2254 " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret);
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002255 goto exit;
Ron Eldor6ea64512018-07-02 10:08:07 +03002256 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002257# endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002258
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002259# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2260 if (opt.cid_enabled == 1 || opt.cid_enabled_renego == 1) {
2261 if (opt.cid_enabled == 1 && opt.cid_enabled_renego == 1 &&
2262 cid_len != cid_renego_len) {
2263 mbedtls_printf("CID length must not change during renegotiation\n");
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002264 goto usage;
2265 }
2266
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002267 if (opt.cid_enabled == 1)
2268 ret = mbedtls_ssl_conf_cid(&conf, cid_len,
2269 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE);
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002270 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002271 ret = mbedtls_ssl_conf_cid(&conf, cid_renego_len,
2272 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE);
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002273
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002274 if (ret != 0) {
2275 mbedtls_printf(
2276 " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
2277 (unsigned int)-ret);
Hanno Beckerad4a1372019-05-03 13:06:44 +01002278 goto exit;
2279 }
2280 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002281# endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerad4a1372019-05-03 13:06:44 +01002282
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002283# if defined(MBEDTLS_SSL_DTLS_SRTP)
2284 const mbedtls_ssl_srtp_profile forced_profile[] = {
2285 opt.force_srtp_profile, MBEDTLS_TLS_SRTP_UNSET
2286 };
2287 if (opt.use_srtp == 1) {
2288 if (opt.force_srtp_profile != 0) {
2289 ret = mbedtls_ssl_conf_dtls_srtp_protection_profiles(
2290 &conf, forced_profile);
2291 } else {
2292 ret = mbedtls_ssl_conf_dtls_srtp_protection_profiles(
2293 &conf, default_profiles);
Ron Eldor6ea64512018-07-02 10:08:07 +03002294 }
2295
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002296 if (ret != 0) {
2297 mbedtls_printf(
2298 " failed\n ! mbedtls_ssl_conf_dtls_srtp_protection_profiles returned %d\n\n",
2299 ret);
Ron Eldor6ea64512018-07-02 10:08:07 +03002300 goto exit;
2301 }
2302
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002303 mbedtls_ssl_conf_srtp_mki_value_supported(
2304 &conf, opt.support_mki ? MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED :
2305 MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED);
Ron Eldor12c6ead2018-07-03 15:08:32 +03002306
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002307 } else if (opt.force_srtp_profile != 0) {
2308 mbedtls_printf(
2309 " failed\n ! must enable use_srtp to force srtp profile\n\n");
Ron Eldor6ea64512018-07-02 10:08:07 +03002310 goto exit;
2311 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002312# endif /* MBEDTLS_SSL_DTLS_SRTP */
Ron Eldor6ea64512018-07-02 10:08:07 +03002313
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002314# if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2315 if (opt.extended_ms != DFL_EXTENDED_MS)
2316 mbedtls_ssl_conf_extended_master_secret(&conf, opt.extended_ms);
2317# endif
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002318
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002319# if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2320 if (opt.etm != DFL_ETM)
2321 mbedtls_ssl_conf_encrypt_then_mac(&conf, opt.etm);
2322# endif
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002323
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002324# if defined(MBEDTLS_SSL_ALPN)
2325 if (opt.alpn_string != NULL)
2326 if ((ret = mbedtls_ssl_conf_alpn_protocols(&conf, alpn_list)) != 0) {
2327 mbedtls_printf(
2328 " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
2329 ret);
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002330 goto exit;
2331 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002332# endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002333
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002334 if (opt.reproducible) {
2335# if defined(MBEDTLS_HAVE_TIME)
2336# if defined(MBEDTLS_PLATFORM_TIME_ALT)
2337 mbedtls_platform_set_time(dummy_constant_time);
2338# else
2339 fprintf(stderr,
2340 "Warning: reproducible option used without constant time\n");
2341# endif
2342# endif /* MBEDTLS_HAVE_TIME */
Philippe Antoine986b6f22019-06-07 15:04:32 +02002343 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002344 mbedtls_ssl_conf_rng(&conf, rng_get, &rng);
2345 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002346
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002347# if defined(MBEDTLS_SSL_CACHE_C)
2348 if (opt.cache_max != -1)
2349 mbedtls_ssl_cache_set_max_entries(&cache, opt.cache_max);
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002350
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002351 if (opt.cache_timeout != -1)
2352 mbedtls_ssl_cache_set_timeout(&cache, opt.cache_timeout);
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002353
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002354 mbedtls_ssl_conf_session_cache(&conf, &cache, mbedtls_ssl_cache_get,
2355 mbedtls_ssl_cache_set);
2356# endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002357
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002358# if defined(MBEDTLS_SSL_SESSION_TICKETS)
2359 if (opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED) {
2360 if ((ret = mbedtls_ssl_ticket_setup(&ticket_ctx, rng_get, &rng,
2361 MBEDTLS_CIPHER_AES_256_GCM,
2362 opt.ticket_timeout)) != 0) {
2363 mbedtls_printf(
2364 " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret);
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002365 goto exit;
2366 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01002367
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002368 mbedtls_ssl_conf_session_tickets_cb(&conf, mbedtls_ssl_ticket_write,
2369 mbedtls_ssl_ticket_parse,
2370 &ticket_ctx);
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002371 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002372# endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002373
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002374# if defined(MBEDTLS_SSL_PROTO_DTLS)
2375 if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2376# if defined(MBEDTLS_SSL_COOKIE_C)
2377 if (opt.cookies > 0) {
2378 if ((ret = mbedtls_ssl_cookie_setup(&cookie_ctx, rng_get, &rng)) !=
2379 0) {
2380 mbedtls_printf(
2381 " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n",
2382 ret);
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002383 goto exit;
2384 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002385
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002386 mbedtls_ssl_conf_dtls_cookies(&conf, mbedtls_ssl_cookie_write,
2387 mbedtls_ssl_cookie_check,
2388 &cookie_ctx);
2389 } else
2390# endif /* MBEDTLS_SSL_COOKIE_C */
2391# if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2392 if (opt.cookies == 0) {
2393 mbedtls_ssl_conf_dtls_cookies(&conf, NULL, NULL, NULL);
2394 } else
2395# endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002396 {
2397 ; /* Nothing to do */
2398 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002399
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002400# if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2401 if (opt.anti_replay != DFL_ANTI_REPLAY)
2402 mbedtls_ssl_conf_dtls_anti_replay(&conf, opt.anti_replay);
2403# endif
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002404
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002405 if (opt.badmac_limit != DFL_BADMAC_LIMIT)
2406 mbedtls_ssl_conf_dtls_badmac_limit(&conf, opt.badmac_limit);
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002407 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002408# endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002409
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002410 if (opt.force_ciphersuite[0] != DFL_FORCE_CIPHER)
2411 mbedtls_ssl_conf_ciphersuites(&conf, opt.force_ciphersuite);
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002412
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002413 if (opt.allow_legacy != DFL_ALLOW_LEGACY)
2414 mbedtls_ssl_conf_legacy_renegotiation(&conf, opt.allow_legacy);
2415# if defined(MBEDTLS_SSL_RENEGOTIATION)
2416 mbedtls_ssl_conf_renegotiation(&conf, opt.renegotiation);
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002417
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002418 if (opt.renego_delay != DFL_RENEGO_DELAY)
2419 mbedtls_ssl_conf_renegotiation_enforced(&conf, opt.renego_delay);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002420
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002421 if (opt.renego_period != DFL_RENEGO_PERIOD) {
2422 PUT_UINT64_BE(renego_period, opt.renego_period, 0);
2423 mbedtls_ssl_conf_renegotiation_period(&conf, renego_period);
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002424 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002425# endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002426
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002427# if defined(MBEDTLS_X509_CRT_PARSE_C)
2428 if (strcmp(opt.ca_path, "none") != 0 && strcmp(opt.ca_file, "none") != 0) {
2429# if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2430 if (opt.ca_callback != 0)
2431 mbedtls_ssl_conf_ca_cb(&conf, ca_callback, &cacert);
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02002432 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002433# endif
2434 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002435 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002436 if (key_cert_init) {
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002437 mbedtls_pk_context *pk = &pkey;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002438# if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
2439 if (opt.async_private_delay1 >= 0) {
2440 ret = ssl_async_set_key(&ssl_async_keys, &srvcert, pk, 0,
2441 opt.async_private_delay1);
2442 if (ret < 0) {
2443 mbedtls_printf(" Test error: ssl_async_set_key failed (%d)\n",
2444 ret);
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002445 goto exit;
2446 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002447 pk = NULL;
2448 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002449# endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
2450 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert, pk)) != 0) {
2451 mbedtls_printf(
2452 " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret);
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002453 goto exit;
2454 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002455 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002456 if (key_cert_init2) {
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002457 mbedtls_pk_context *pk = &pkey2;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002458# if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
2459 if (opt.async_private_delay2 >= 0) {
2460 ret = ssl_async_set_key(&ssl_async_keys, &srvcert2, pk, 0,
2461 opt.async_private_delay2);
2462 if (ret < 0) {
2463 mbedtls_printf(" Test error: ssl_async_set_key failed (%d)\n",
2464 ret);
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002465 goto exit;
2466 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002467 pk = NULL;
2468 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002469# endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
2470 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert2, pk)) != 0) {
2471 mbedtls_printf(
2472 " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret);
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002473 goto exit;
2474 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002475 }
2476
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002477# if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
2478 if (opt.async_operations[0] != '-') {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002479 mbedtls_ssl_async_sign_t *sign = NULL;
2480 mbedtls_ssl_async_decrypt_t *decrypt = NULL;
Gilles Peskine12ab5d42018-04-24 12:32:04 +02002481 const char *r;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002482 for (r = opt.async_operations; *r; r++) {
2483 switch (*r) {
2484 case 'd':
2485 decrypt = ssl_async_decrypt;
2486 break;
2487 case 's':
2488 sign = ssl_async_sign;
2489 break;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002490 }
2491 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002492 ssl_async_keys.inject_error = (opt.async_private_error < 0 ?
2493 -opt.async_private_error :
2494 opt.async_private_error);
Gilles Peskine535fb372021-01-13 18:59:46 +01002495 ssl_async_keys.f_rng = rng_get;
2496 ssl_async_keys.p_rng = &rng;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002497 mbedtls_ssl_conf_async_private_cb(&conf, sign, decrypt,
2498 ssl_async_resume, ssl_async_cancel,
2499 &ssl_async_keys);
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002500 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002501# endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
2502# endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +02002503
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002504# if defined(SNI_OPTION)
2505 if (opt.sni != NULL) {
2506 mbedtls_ssl_conf_sni(&conf, sni_callback, sni_info);
2507# if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
2508 if (opt.async_private_delay2 >= 0) {
Gilles Peskinee2479892018-06-13 18:06:51 +02002509 sni_entry *cur;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002510 for (cur = sni_info; cur != NULL; cur = cur->next) {
2511 ret = ssl_async_set_key(&ssl_async_keys, cur->cert, cur->key, 1,
2512 opt.async_private_delay2);
2513 if (ret < 0) {
2514 mbedtls_printf(
2515 " Test error: ssl_async_set_key failed (%d)\n", ret);
Gilles Peskinee2479892018-06-13 18:06:51 +02002516 goto exit;
2517 }
2518 cur->key = NULL;
Gilles Peskine166ce742018-04-30 10:30:49 +02002519 }
Gilles Peskine166ce742018-04-30 10:30:49 +02002520 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002521# endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine166ce742018-04-30 10:30:49 +02002522 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002523# endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002524
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002525# if defined(MBEDTLS_ECP_C)
2526 if (opt.curves != NULL && strcmp(opt.curves, "default") != 0) {
2527 mbedtls_ssl_conf_curves(&conf, curve_list);
Hanno Beckere6706e62017-05-15 16:05:15 +01002528 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002529# endif
Hanno Beckere6706e62017-05-15 16:05:15 +01002530
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002531# if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002532
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002533 if (strlen(opt.psk) != 0 && strlen(opt.psk_identity) != 0) {
2534# if defined(MBEDTLS_USE_PSA_CRYPTO)
2535 if (opt.psk_opaque != 0) {
Hanno Becker1d911cd2018-11-15 13:06:09 +00002536 /* The algorithm has already been determined earlier. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002537 status = psa_setup_psk_key_slot(&psk_slot, alg, psk, psk_len);
2538 if (status != PSA_SUCCESS) {
2539 fprintf(stderr, "SETUP FAIL\n");
Hanno Becker1d911cd2018-11-15 13:06:09 +00002540 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2541 goto exit;
2542 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002543 if ((ret = mbedtls_ssl_conf_psk_opaque(
2544 &conf, psk_slot, (const unsigned char *)opt.psk_identity,
2545 strlen(opt.psk_identity))) != 0) {
2546 mbedtls_printf(
2547 " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
2548 ret);
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002549 goto exit;
2550 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002551 } else
2552# endif /* MBEDTLS_USE_PSA_CRYPTO */
2553 if (psk_len > 0) {
2554 ret = mbedtls_ssl_conf_psk(&conf, psk, psk_len,
2555 (const unsigned char *)opt.psk_identity,
2556 strlen(opt.psk_identity));
2557 if (ret != 0) {
2558 mbedtls_printf(
2559 " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n",
2560 (unsigned int)-ret);
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002561 goto exit;
2562 }
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002563 }
2564 }
2565
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002566 if (opt.psk_list != NULL) {
2567# if defined(MBEDTLS_USE_PSA_CRYPTO)
2568 if (opt.psk_list_opaque != 0) {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002569 psk_entry *cur_psk;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002570 for (cur_psk = psk_info; cur_psk != NULL; cur_psk = cur_psk->next) {
2571 status = psa_setup_psk_key_slot(&cur_psk->slot, alg,
2572 cur_psk->key, cur_psk->key_len);
2573 if (status != PSA_SUCCESS) {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002574 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2575 goto exit;
2576 }
2577 }
2578 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002579# endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker1d911cd2018-11-15 13:06:09 +00002580
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002581 mbedtls_ssl_conf_psk_cb(&conf, psk_callback, psk_info);
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002582 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002583# endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002584
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002585# if defined(MBEDTLS_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00002586 /*
2587 * Use different group than default DHM group
2588 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002589# if defined(MBEDTLS_FS_IO)
2590 if (opt.dhm_file != NULL)
2591 ret = mbedtls_ssl_conf_dh_param_ctx(&conf, &dhm);
2592# endif
2593 if (ret != 0) {
2594 mbedtls_printf(
2595 " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n",
2596 (unsigned int)-ret);
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002597 goto exit;
2598 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002599# endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002600
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002601 if (opt.min_version != DFL_MIN_VERSION)
2602 mbedtls_ssl_conf_min_version(&conf, MBEDTLS_SSL_MAJOR_VERSION_3,
2603 opt.min_version);
Paul Bakker1d29fb52012-09-28 13:28:45 +00002604
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002605 if (opt.max_version != DFL_MIN_VERSION)
2606 mbedtls_ssl_conf_max_version(&conf, MBEDTLS_SSL_MAJOR_VERSION_3,
2607 opt.max_version);
Paul Bakkerc1516be2013-06-29 16:01:32 +02002608
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002609 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
2610 mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
2611 (unsigned int)-ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002612 goto exit;
2613 }
2614
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002615# if defined(MBEDTLS_SSL_EXPORT_KEYS)
2616 if (opt.eap_tls != 0) {
2617 mbedtls_ssl_set_export_keys_cb(&ssl, eap_tls_key_derivation,
2618 &eap_tls_keying);
2619 } else if (opt.nss_keylog != 0) {
2620 mbedtls_ssl_set_export_keys_cb(&ssl, nss_keylog_export, NULL);
Hanno Becker7e6c1782021-06-08 09:24:55 +01002621 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002622# if defined(MBEDTLS_SSL_DTLS_SRTP)
2623 else if (opt.use_srtp != 0) {
2624 mbedtls_ssl_set_export_keys_cb(&ssl, dtls_srtp_key_derivation,
2625 &dtls_srtp_keying);
Hanno Becker7e6c1782021-06-08 09:24:55 +01002626 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002627# endif /* MBEDTLS_SSL_DTLS_SRTP */
2628# endif /* MBEDTLS_SSL_EXPORT_KEYS */
Hanno Becker7e6c1782021-06-08 09:24:55 +01002629
Hanno Beckerdcc94e62019-07-03 17:05:43 +01002630 io_ctx.ssl = &ssl;
2631 io_ctx.net = &client_fd;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002632 mbedtls_ssl_set_bio(&ssl, &io_ctx, send_cb, recv_cb,
2633 opt.nbio == 0 ? recv_timeout_cb : NULL);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002634
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002635# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2636 if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2637 if ((ret = mbedtls_ssl_set_cid(&ssl, opt.cid_enabled, cid, cid_len)) !=
2638 0) {
2639 mbedtls_printf(" failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2640 ret);
Hanno Beckera7d25422019-04-09 17:28:10 +01002641 goto exit;
2642 }
2643 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002644# endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +01002645
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002646# if defined(MBEDTLS_SSL_PROTO_DTLS)
2647 if (opt.dtls_mtu != DFL_DTLS_MTU)
2648 mbedtls_ssl_set_mtu(&ssl, opt.dtls_mtu);
2649# endif
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002650
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002651# if defined(MBEDTLS_TIMING_C)
2652 mbedtls_ssl_set_timer_cb(&ssl, &timer, mbedtls_timing_set_delay,
2653 mbedtls_timing_get_delay);
2654# endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002655
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002656 mbedtls_printf(" ok\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002657
2658reset:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002659# if !defined(_WIN32)
2660 if (received_sigterm) {
2661 mbedtls_printf(" interrupted by SIGTERM (not in net_accept())\n");
2662 if (ret == MBEDTLS_ERR_NET_INVALID_CONTEXT)
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01002663 ret = 0;
2664
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002665 goto exit;
2666 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002667# endif
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002668
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002669 if (ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT) {
2670 mbedtls_printf(" ! Client initiated reconnection from same port\n");
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002671 goto handshake;
2672 }
2673
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002674# ifdef MBEDTLS_ERROR_C
2675 if (ret != 0) {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002676 char error_buf[100];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002677 mbedtls_strerror(ret, error_buf, 100);
2678 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002679 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002680# endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002681
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002682 mbedtls_net_free(&client_fd);
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002683
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002684 mbedtls_ssl_session_reset(&ssl);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002685
2686 /*
2687 * 3. Wait until a client connects
2688 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002689 mbedtls_printf(" . Waiting for a remote connection ...");
2690 fflush(stdout);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002691
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002692 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd, client_ip,
2693 sizeof(client_ip), &cliip_len)) != 0) {
2694# if !defined(_WIN32)
2695 if (received_sigterm) {
2696 mbedtls_printf(" interrupted by SIGTERM (in net_accept())\n");
2697 if (ret == MBEDTLS_ERR_NET_ACCEPT_FAILED)
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01002698 ret = 0;
2699
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002700 goto exit;
2701 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002702# endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002703
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002704 mbedtls_printf(" failed\n ! mbedtls_net_accept returned -0x%x\n\n",
2705 (unsigned int)-ret);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002706 goto exit;
2707 }
2708
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002709 if (opt.nbio > 0)
2710 ret = mbedtls_net_set_nonblock(&client_fd);
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002711 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002712 ret = mbedtls_net_set_block(&client_fd);
2713 if (ret != 0) {
2714 mbedtls_printf(" failed\n ! net_set_(non)block() returned -0x%x\n\n",
2715 (unsigned int)-ret);
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002716 goto exit;
2717 }
2718
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002719 mbedtls_ssl_conf_read_timeout(&conf, opt.read_timeout);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002720
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002721# if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2722 if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2723 if ((ret = mbedtls_ssl_set_client_transport_id(&ssl, client_ip,
2724 cliip_len)) != 0) {
2725 mbedtls_printf(
2726 " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n",
2727 (unsigned int)-ret);
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002728 goto exit;
2729 }
2730 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002731# endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002732
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002733# if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2734 if (opt.ecjpake_pw != DFL_ECJPAKE_PW) {
2735 if ((ret = mbedtls_ssl_set_hs_ecjpake_password(
2736 &ssl, (const unsigned char *)opt.ecjpake_pw,
2737 strlen(opt.ecjpake_pw))) != 0) {
2738 mbedtls_printf(
2739 " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
2740 ret);
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002741 goto exit;
2742 }
2743 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002744# endif
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002745
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002746 mbedtls_printf(" ok\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002747
2748 /*
2749 * 4. Handshake
2750 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002751handshake:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002752 mbedtls_printf(" . Performing the SSL/TLS handshake...");
2753 fflush(stdout);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002754
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002755 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
2756# if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
2757 if (ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
2758 ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL) {
2759 mbedtls_printf(" cancelling on injected error\n");
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002760 break;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002761 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002762# endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskineb44692f2018-04-24 12:18:19 +02002763
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002764 if (!mbedtls_status_is_ssl_in_progress(ret))
Hanno Becker16970d22017-10-10 15:56:37 +01002765 break;
2766
2767 /* For event-driven IO, wait for socket to become available */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002768 if (opt.event == 1 /* level triggered IO */) {
2769# if defined(MBEDTLS_TIMING_C)
2770 ret = idle(&client_fd, &timer, ret);
2771# else
2772 ret = idle(&client_fd, ret);
2773# endif
2774 if (ret != 0)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002775 goto reset;
Hanno Becker16970d22017-10-10 15:56:37 +01002776 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002777 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002778
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002779 if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) {
2780 mbedtls_printf(" hello verification requested\n");
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002781 ret = 0;
2782 goto reset;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002783 } else if (ret != 0) {
2784 mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2785 (unsigned int)-ret);
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002786
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002787# if defined(MBEDTLS_X509_CRT_PARSE_C)
2788 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002789 char vrfy_buf[512];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002790 flags = mbedtls_ssl_get_verify_result(&ssl);
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002791
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002792 x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002793
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002794 mbedtls_printf("%s\n", vrfy_buf);
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002795 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002796# endif
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002797
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002798# if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
2799 if (opt.async_private_error < 0)
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002800 /* Injected error only the first time round, to test reset */
2801 ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002802# endif
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002803 goto reset;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002804 } else /* ret == 0 */
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002805 {
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002806 mbedtls_printf(
2807 " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
2808 mbedtls_ssl_get_version(&ssl), mbedtls_ssl_get_ciphersuite(&ssl));
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002809 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002810
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002811 if ((ret = mbedtls_ssl_get_record_expansion(&ssl)) >= 0)
2812 mbedtls_printf(" [ Record expansion is %d ]\n", ret);
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002813 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002814 mbedtls_printf(" [ Record expansion is unknown ]\n");
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002815
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002816# if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2817 mbedtls_printf(" [ Maximum incoming record payload length is %u ]\n",
2818 (unsigned int)mbedtls_ssl_get_max_in_record_payload(&ssl));
2819 mbedtls_printf(" [ Maximum outgoing record payload length is %u ]\n",
2820 (unsigned int)mbedtls_ssl_get_max_out_record_payload(&ssl));
2821# endif
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002822
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002823# if defined(MBEDTLS_SSL_ALPN)
2824 if (opt.alpn_string != NULL) {
2825 const char *alp = mbedtls_ssl_get_alpn_protocol(&ssl);
2826 mbedtls_printf(" [ Application Layer Protocol is %s ]\n",
2827 alp ? alp : "(none)");
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002828 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002829# endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002830
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002831# if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002832 /*
Ron Eldor2a47be52017-06-20 15:23:23 +03002833 * 5. Verify the client certificate
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002834 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002835 mbedtls_printf(" . Verifying peer X.509 certificate...");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002836
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002837 if ((flags = mbedtls_ssl_get_verify_result(&ssl)) != 0) {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002838 char vrfy_buf[512];
2839
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002840 mbedtls_printf(" failed\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002841
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002842 x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
2843 mbedtls_printf("%s\n", vrfy_buf);
2844 } else
2845 mbedtls_printf(" ok\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002846
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002847# if !defined(MBEDTLS_X509_REMOVE_INFO)
2848 if (mbedtls_ssl_get_peer_cert(&ssl) != NULL) {
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02002849 char crt_buf[512];
2850
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002851 mbedtls_printf(" . Peer certificate information ...\n");
2852 mbedtls_x509_crt_info(crt_buf, sizeof(crt_buf), " ",
2853 mbedtls_ssl_get_peer_cert(&ssl));
2854 mbedtls_printf("%s\n", crt_buf);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002855 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002856# endif /* MBEDTLS_X509_REMOVE_INFO */
2857# endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002858
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002859# if defined(MBEDTLS_SSL_EXPORT_KEYS)
2860 if (opt.eap_tls != 0) {
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002861 size_t j = 0;
Ron Eldor51d3ab52019-05-12 14:54:30 +03002862
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002863 if ((ret = mbedtls_ssl_tls_prf(
2864 eap_tls_keying.tls_prf_type, eap_tls_keying.master_secret,
2865 sizeof(eap_tls_keying.master_secret), eap_tls_label,
2866 eap_tls_keying.randbytes, sizeof(eap_tls_keying.randbytes),
2867 eap_tls_keymaterial, sizeof(eap_tls_keymaterial))) != 0) {
2868 mbedtls_printf(
2869 " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2870 (unsigned int)-ret);
Ron Eldor65d8c262019-06-04 13:05:36 +03002871 goto reset;
Ron Eldor51d3ab52019-05-12 14:54:30 +03002872 }
2873
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002874 mbedtls_printf(" EAP-TLS key material is:");
2875 for (j = 0; j < sizeof(eap_tls_keymaterial); j++) {
2876 if (j % 8 == 0)
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002877 mbedtls_printf("\n ");
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002878 mbedtls_printf("%02x ", eap_tls_keymaterial[j]);
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002879 }
2880 mbedtls_printf("\n");
2881
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002882 if ((ret = mbedtls_ssl_tls_prf(eap_tls_keying.tls_prf_type, NULL, 0,
2883 eap_tls_label, eap_tls_keying.randbytes,
2884 sizeof(eap_tls_keying.randbytes),
2885 eap_tls_iv, sizeof(eap_tls_iv))) != 0) {
2886 mbedtls_printf(
2887 " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2888 (unsigned int)-ret);
2889 goto reset;
2890 }
Ron Eldor51d3ab52019-05-12 14:54:30 +03002891
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002892 mbedtls_printf(" EAP-TLS IV is:");
2893 for (j = 0; j < sizeof(eap_tls_iv); j++) {
2894 if (j % 8 == 0)
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002895 mbedtls_printf("\n ");
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002896 mbedtls_printf("%02x ", eap_tls_iv[j]);
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002897 }
2898 mbedtls_printf("\n");
2899 }
Ron Eldor65b56ef2019-09-26 16:40:48 +03002900
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002901# if defined(MBEDTLS_SSL_DTLS_SRTP)
2902 else if (opt.use_srtp != 0) {
Ron Eldor65b56ef2019-09-26 16:40:48 +03002903 size_t j = 0;
Johan Pascal2258a4f2020-10-28 13:53:09 +01002904 mbedtls_dtls_srtp_info dtls_srtp_negotiation_result;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002905 mbedtls_ssl_get_dtls_srtp_negotiation_result(
2906 &ssl, &dtls_srtp_negotiation_result);
Ron Eldor65b56ef2019-09-26 16:40:48 +03002907
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002908 if (dtls_srtp_negotiation_result.chosen_dtls_srtp_profile ==
2909 MBEDTLS_TLS_SRTP_UNSET) {
2910 mbedtls_printf(" Unable to negotiate "
2911 "the use of DTLS-SRTP\n");
2912 } else {
2913 if ((ret = mbedtls_ssl_tls_prf(
2914 dtls_srtp_keying.tls_prf_type,
2915 dtls_srtp_keying.master_secret,
2916 sizeof(dtls_srtp_keying.master_secret), dtls_srtp_label,
2917 dtls_srtp_keying.randbytes,
2918 sizeof(dtls_srtp_keying.randbytes), dtls_srtp_key_material,
2919 sizeof(dtls_srtp_key_material))) != 0) {
2920 mbedtls_printf(
2921 " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2922 (unsigned int)-ret);
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002923 goto exit;
2924 }
Ron Eldor65b56ef2019-09-26 16:40:48 +03002925
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002926 mbedtls_printf(" DTLS-SRTP key material is:");
2927 for (j = 0; j < sizeof(dtls_srtp_key_material); j++) {
2928 if (j % 8 == 0)
2929 mbedtls_printf("\n ");
2930 mbedtls_printf("%02x ", dtls_srtp_key_material[j]);
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002931 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002932 mbedtls_printf("\n");
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002933
Johan Pascal9bc50b02020-09-24 12:01:13 +02002934 /* produce a less readable output used to perform automatic checks
2935 * - compare client and server output
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002936 * - interop test with openssl which client produces this kind of
2937 * output
Johan Pascal9bc50b02020-09-24 12:01:13 +02002938 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002939 mbedtls_printf(" Keying material: ");
2940 for (j = 0; j < sizeof(dtls_srtp_key_material); j++) {
2941 mbedtls_printf("%02X", dtls_srtp_key_material[j]);
Johan Pascal9bc50b02020-09-24 12:01:13 +02002942 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002943 mbedtls_printf("\n");
Johan Pascal20c7db32020-10-26 22:45:58 +01002944
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002945 if (dtls_srtp_negotiation_result.mki_len > 0) {
2946 mbedtls_printf(" DTLS-SRTP mki value: ");
2947 for (j = 0; j < dtls_srtp_negotiation_result.mki_len; j++) {
2948 mbedtls_printf("%02X",
2949 dtls_srtp_negotiation_result.mki_value[j]);
Johan Pascal20c7db32020-10-26 22:45:58 +01002950 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002951 } else {
2952 mbedtls_printf(" DTLS-SRTP no mki value negotiated");
Johan Pascal20c7db32020-10-26 22:45:58 +01002953 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002954 mbedtls_printf("\n");
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002955 }
Ron Eldor65b56ef2019-09-26 16:40:48 +03002956 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002957# endif /* MBEDTLS_SSL_DTLS_SRTP */
2958# endif /* MBEDTLS_SSL_EXPORT_KEYS */
Hanno Beckera7d25422019-04-09 17:28:10 +01002959
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002960# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2961 ret = report_cid_usage(&ssl, "initial handshake");
2962 if (ret != 0)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002963 goto exit;
2964
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002965 if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2966 if ((ret = mbedtls_ssl_set_cid(&ssl, opt.cid_enabled_renego, cid_renego,
2967 cid_renego_len)) != 0) {
2968 mbedtls_printf(" failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2969 ret);
Hanno Beckera7d25422019-04-09 17:28:10 +01002970 goto exit;
2971 }
Hanno Beckera7d25422019-04-09 17:28:10 +01002972 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002973# endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +01002974
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002975# if defined(MBEDTLS_MEMORY_DEBUG)
2976 mbedtls_memory_buffer_alloc_cur_get(&current_heap_memory, &heap_blocks);
2977 mbedtls_memory_buffer_alloc_max_get(&peak_heap_memory, &heap_blocks);
2978 mbedtls_printf(
2979 "Heap memory usage after handshake: %lu bytes. Peak memory usage was %lu\n",
2980 (unsigned long)current_heap_memory, (unsigned long)peak_heap_memory);
2981# endif /* MBEDTLS_MEMORY_DEBUG */
Piotr Nowicki0937ed22019-11-26 16:32:40 +01002982
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002983 if (opt.exchanges == 0)
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02002984 goto close_notify;
2985
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00002986 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002987data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002988 /*
2989 * 6. Read the HTTP Request
2990 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002991 mbedtls_printf(" < Read from client:");
2992 fflush(stdout);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002993
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002994 /*
2995 * TLS and DTLS need different reading styles (stream vs datagram)
2996 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002997 if (opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM) {
2998 do {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002999 int terminated = 0;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003000 len = opt.buffer_size - 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003001 memset(buf, 0, opt.buffer_size);
3002 ret = mbedtls_ssl_read(&ssl, buf, len);
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003003
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003004 if (mbedtls_status_is_ssl_in_progress(ret)) {
3005 if (opt.event == 1 /* level triggered IO */) {
3006# if defined(MBEDTLS_TIMING_C)
3007 idle(&client_fd, &timer, ret);
3008# else
3009 idle(&client_fd, ret);
3010# endif
Hanno Becker16970d22017-10-10 15:56:37 +01003011 }
3012
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003013 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01003014 }
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003015
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003016 if (ret <= 0) {
3017 switch (ret) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003019 mbedtls_printf(" connection was closed gracefully\n");
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003020 goto close_notify;
3021
3022 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003023 case MBEDTLS_ERR_NET_CONN_RESET:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003024 mbedtls_printf(" connection was reset by peer\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025 ret = MBEDTLS_ERR_NET_CONN_RESET;
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003026 goto reset;
3027
3028 default:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003029 mbedtls_printf(" mbedtls_ssl_read returned -0x%x\n",
3030 (unsigned int)-ret);
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003031 goto reset;
3032 }
3033 }
3034
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003035 if (mbedtls_ssl_get_bytes_avail(&ssl) == 0) {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003036 len = ret;
3037 buf[len] = '\0';
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003038 mbedtls_printf(" %d bytes read\n\n%s\n", len, (char *)buf);
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003039
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003040 /* End of message should be detected according to the syntax of
3041 * the application protocol (eg HTTP), just use a dummy test
3042 * here. */
3043 if (buf[len - 1] == '\n')
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003044 terminated = 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003045 } else {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003046 int extra_len, ori_len;
3047 unsigned char *larger_buf;
3048
3049 ori_len = ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003050 extra_len = (int)mbedtls_ssl_get_bytes_avail(&ssl);
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003051
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003052 larger_buf = mbedtls_calloc(1, ori_len + extra_len + 1);
3053 if (larger_buf == NULL) {
3054 mbedtls_printf(" ! memory allocation failed\n");
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003055 ret = 1;
3056 goto reset;
3057 }
3058
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003059 memset(larger_buf, 0, ori_len + extra_len);
3060 memcpy(larger_buf, buf, ori_len);
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003061
3062 /* This read should never fail and get the whole cached data */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003063 ret = mbedtls_ssl_read(&ssl, larger_buf + ori_len, extra_len);
3064 if (ret != extra_len ||
3065 mbedtls_ssl_get_bytes_avail(&ssl) != 0) {
3066 mbedtls_printf(
3067 " ! mbedtls_ssl_read failed on cached data\n");
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003068 ret = 1;
3069 goto reset;
3070 }
3071
3072 larger_buf[ori_len + extra_len] = '\0';
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003073 mbedtls_printf(" %d bytes read (%d + %d)\n\n%s\n",
3074 ori_len + extra_len, ori_len, extra_len,
3075 (char *)larger_buf);
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003076
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003077 /* End of message should be detected according to the syntax of
3078 * the application protocol (eg HTTP), just use a dummy test
3079 * here. */
3080 if (larger_buf[ori_len + extra_len - 1] == '\n')
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003081 terminated = 1;
3082
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003083 mbedtls_free(larger_buf);
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003084 }
3085
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003086 if (terminated) {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003087 ret = 0;
3088 break;
3089 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003090 } while (1);
3091 } else /* Not stream, so datagram */
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003092 {
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003093 len = opt.buffer_size - 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003094 memset(buf, 0, opt.buffer_size);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003095
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003096 do {
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003097 /* Without the call to `mbedtls_ssl_check_pending`, it might
3098 * happen that the client sends application data in the same
3099 * datagram as the Finished message concluding the handshake.
3100 * In this case, the application data would be ready to be
3101 * processed while the underlying transport wouldn't signal
3102 * any further incoming data.
3103 *
3104 * See the test 'Event-driven I/O: session-id resume, UDP packing'
3105 * in tests/ssl-opt.sh.
3106 */
3107
3108 /* For event-driven IO, wait for socket to become available */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003109 if (mbedtls_ssl_check_pending(&ssl) == 0 &&
3110 opt.event == 1 /* level triggered IO */) {
3111# if defined(MBEDTLS_TIMING_C)
3112 idle(&client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ);
3113# else
3114 idle(&client_fd, MBEDTLS_ERR_SSL_WANT_READ);
3115# endif
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003116 }
3117
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003118 ret = mbedtls_ssl_read(&ssl, buf, len);
Hanno Becker16970d22017-10-10 15:56:37 +01003119
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003120 /* Note that even if `mbedtls_ssl_check_pending` returns true,
3121 * it can happen that the subsequent call to `mbedtls_ssl_read`
3122 * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
3123 * might be discarded (e.g. because they are retransmissions). */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003124 } while (mbedtls_status_is_ssl_in_progress(ret));
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003125
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003126 if (ret <= 0) {
3127 switch (ret) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003129 mbedtls_printf(" connection was closed gracefully\n");
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003130 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003131 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003132
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003133 default:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003134 mbedtls_printf(" mbedtls_ssl_read returned -0x%x\n",
3135 (unsigned int)-ret);
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003136 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003137 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003138 }
3139
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003140 len = ret;
3141 buf[len] = '\0';
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003142 mbedtls_printf(" %d bytes read\n\n%s", len, (char *)buf);
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003143 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003144 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003145
3146 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003147 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02003148 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003149 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003150# if defined(MBEDTLS_SSL_RENEGOTIATION)
3151 if (opt.renegotiate && exchanges_left == opt.exchanges) {
3152 mbedtls_printf(" . Requestion renegotiation...");
3153 fflush(stdout);
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003154
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003155 while ((ret = mbedtls_ssl_renegotiate(&ssl)) != 0) {
3156 if (!mbedtls_status_is_ssl_in_progress(ret)) {
3157 mbedtls_printf(
3158 " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
3159 ret);
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003160 goto reset;
3161 }
Hanno Becker16970d22017-10-10 15:56:37 +01003162
3163 /* For event-driven IO, wait for socket to become available */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003164 if (opt.event == 1 /* level triggered IO */) {
3165# if defined(MBEDTLS_TIMING_C)
3166 idle(&client_fd, &timer, ret);
3167# else
3168 idle(&client_fd, ret);
3169# endif
Hanno Becker16970d22017-10-10 15:56:37 +01003170 }
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003171 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003172
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003173 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003174 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003175# endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003176
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003177# if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3178 ret = report_cid_usage(&ssl, "after renegotiation");
3179 if (ret != 0)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003180 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003181# endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003182
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003183 /*
3184 * 7. Write the 200 Response
3185 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003186 mbedtls_printf(" > Write to client:");
3187 fflush(stdout);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003188
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003189 len =
3190 sprintf((char *)buf, HTTP_RESPONSE, mbedtls_ssl_get_ciphersuite(&ssl));
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003191
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003192 /* Add padding to the response to reach opt.response_size in length */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003193 if (opt.response_size != DFL_RESPONSE_SIZE && len < opt.response_size) {
3194 memset(buf + len, 'B', opt.response_size - len);
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003195 len += opt.response_size - len;
3196 }
3197
3198 /* Truncate if response size is smaller than the "natural" size */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003199 if (opt.response_size != DFL_RESPONSE_SIZE && len > opt.response_size) {
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003200 len = opt.response_size;
3201
3202 /* Still end with \r\n unless that's really not possible */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003203 if (len >= 2)
3204 buf[len - 2] = '\r';
3205 if (len >= 1)
3206 buf[len - 1] = '\n';
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003207 }
3208
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003209 if (opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM) {
3210 for (written = 0, frags = 0; written < len; written += ret, frags++) {
3211 while ((ret = mbedtls_ssl_write(&ssl, buf + written,
3212 len - written)) <= 0) {
3213 if (ret == MBEDTLS_ERR_NET_CONN_RESET) {
3214 mbedtls_printf(
3215 " failed\n ! peer closed the connection\n\n");
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003216 goto reset;
3217 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003218
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003219 if (!mbedtls_status_is_ssl_in_progress(ret)) {
3220 mbedtls_printf(
3221 " failed\n ! mbedtls_ssl_write returned %d\n\n", ret);
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003222 goto reset;
3223 }
Hanno Becker16970d22017-10-10 15:56:37 +01003224
3225 /* For event-driven IO, wait for socket to become available */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003226 if (opt.event == 1 /* level triggered IO */) {
3227# if defined(MBEDTLS_TIMING_C)
3228 idle(&client_fd, &timer, ret);
3229# else
3230 idle(&client_fd, ret);
3231# endif
Hanno Becker16970d22017-10-10 15:56:37 +01003232 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003233 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003234 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003235 } else /* Not stream, so datagram */
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003236 {
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003237 while (1) {
3238 ret = mbedtls_ssl_write(&ssl, buf, len);
Hanno Becker16970d22017-10-10 15:56:37 +01003239
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003240 if (!mbedtls_status_is_ssl_in_progress(ret))
Hanno Becker16970d22017-10-10 15:56:37 +01003241 break;
3242
3243 /* For event-driven IO, wait for socket to become available */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003244 if (opt.event == 1 /* level triggered IO */) {
3245# if defined(MBEDTLS_TIMING_C)
3246 idle(&client_fd, &timer, ret);
3247# else
3248 idle(&client_fd, ret);
3249# endif
Hanno Becker16970d22017-10-10 15:56:37 +01003250 }
3251 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003252
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003253 if (ret < 0) {
3254 mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n",
3255 ret);
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003256 goto reset;
3257 }
3258
3259 frags = 1;
3260 written = ret;
3261 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003262
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003263 buf[written] = '\0';
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003264 mbedtls_printf(" %d bytes written in %d fragments\n\n%s\n", written, frags,
3265 (char *)buf);
Manuel Pégourié-Gonnarda92ed482015-01-14 10:46:08 +01003266 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01003267
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003268 /*
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03003269 * 7b. Simulate serialize/deserialize and go back to data exchange
3270 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003271# if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
3272 if (opt.serialize != 0) {
Jarno Lamsa15b3a7a2019-06-06 15:10:07 +03003273 size_t buf_len;
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03003274
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003275 mbedtls_printf(" . Serializing live connection...");
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03003276
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003277 ret = mbedtls_ssl_context_save(&ssl, NULL, 0, &buf_len);
3278 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3279 mbedtls_printf(" failed\n ! mbedtls_ssl_context_save returned "
3280 "-0x%x\n\n",
3281 (unsigned int)-ret);
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03003282
3283 goto exit;
3284 }
3285
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003286 if ((context_buf = mbedtls_calloc(1, buf_len)) == NULL) {
3287 mbedtls_printf(" failed\n ! Couldn't allocate buffer for "
3288 "serialized context");
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03003289
3290 goto exit;
3291 }
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02003292 context_buf_len = buf_len;
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03003293
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003294 if ((ret = mbedtls_ssl_context_save(&ssl, context_buf, buf_len,
3295 &buf_len)) != 0) {
3296 mbedtls_printf(" failed\n ! mbedtls_ssl_context_save returned "
3297 "-0x%x\n\n",
3298 (unsigned int)-ret);
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03003299
3300 goto exit;
3301 }
3302
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003303 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003304
Piotr Nowicki3de298f2020-04-16 14:35:19 +02003305 /* Save serialized context to the 'opt.context_file' as a base64 code */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003306 if (0 < strlen(opt.context_file)) {
Piotr Nowicki3de298f2020-04-16 14:35:19 +02003307 FILE *b64_file;
3308 uint8_t *b64_buf;
3309 size_t b64_len;
3310
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003311 mbedtls_printf(" . Save serialized context to a file... ");
Piotr Nowicki3de298f2020-04-16 14:35:19 +02003312
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003313 mbedtls_base64_encode(NULL, 0, &b64_len, context_buf, buf_len);
Piotr Nowicki3de298f2020-04-16 14:35:19 +02003314
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003315 if ((b64_buf = mbedtls_calloc(1, b64_len)) == NULL) {
3316 mbedtls_printf("failed\n ! Couldn't allocate buffer for "
3317 "the base64 code\n");
Piotr Nowicki3de298f2020-04-16 14:35:19 +02003318 goto exit;
3319 }
3320
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003321 if ((ret = mbedtls_base64_encode(b64_buf, b64_len, &b64_len,
3322 context_buf, buf_len)) != 0) {
3323 mbedtls_printf("failed\n ! mbedtls_base64_encode returned "
3324 "-0x%x\n",
3325 (unsigned int)-ret);
3326 mbedtls_free(b64_buf);
Piotr Nowicki3de298f2020-04-16 14:35:19 +02003327 goto exit;
3328 }
3329
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003330 if ((b64_file = fopen(opt.context_file, "w")) == NULL) {
3331 mbedtls_printf("failed\n ! Cannot open '%s' for writing.\n",
3332 opt.context_file);
3333 mbedtls_free(b64_buf);
Piotr Nowicki3de298f2020-04-16 14:35:19 +02003334 goto exit;
3335 }
3336
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003337 if (b64_len != fwrite(b64_buf, 1, b64_len, b64_file)) {
3338 mbedtls_printf("failed\n ! fwrite(%ld bytes) failed\n",
3339 (long)b64_len);
3340 mbedtls_free(b64_buf);
3341 fclose(b64_file);
Piotr Nowicki3de298f2020-04-16 14:35:19 +02003342 goto exit;
3343 }
3344
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003345 mbedtls_free(b64_buf);
3346 fclose(b64_file);
Piotr Nowicki3de298f2020-04-16 14:35:19 +02003347
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003348 mbedtls_printf("ok\n");
Piotr Nowicki3de298f2020-04-16 14:35:19 +02003349 }
3350
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003351 /*
3352 * This simulates a workflow where you have a long-lived server
3353 * instance, potentially with a pool of ssl_context objects, and you
3354 * just want to re-use one while the connection is inactive: in that
3355 * case you can just reset() it, and then it's ready to receive
3356 * serialized data from another connection (or the same here).
3357 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003358 if (opt.serialize == 1) {
Manuel Pégourié-Gonnard9df5a822019-07-23 14:51:09 +02003359 /* nothing to do here, done by context_save() already */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003360 mbedtls_printf(" . Context has been reset... ok\n");
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003361 }
3362
3363 /*
3364 * This simulates a workflow where you have one server instance per
3365 * connection, and want to release it entire when the connection is
3366 * inactive, and spawn it again when needed again - this would happen
3367 * between ssl_free() and ssl_init() below, together with any other
3368 * teardown/startup code needed - for example, preparing the
3369 * ssl_config again (see section 3 "setup stuff" in this file).
3370 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003371 if (opt.serialize == 2) {
3372 mbedtls_printf(" . Freeing and reinitializing context...");
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003373
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003374 mbedtls_ssl_free(&ssl);
Jarno Lamsa304d61c2019-06-06 10:40:52 +03003375
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003376 mbedtls_ssl_init(&ssl);
Jarno Lamsa304d61c2019-06-06 10:40:52 +03003377
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003378 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
3379 mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned "
3380 "-0x%x\n\n",
3381 (unsigned int)-ret);
Jarno Lamsa304d61c2019-06-06 10:40:52 +03003382 goto exit;
3383 }
3384
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003385 /*
3386 * This illustrates the minimum amount of things you need to set
3387 * up, however you could set up much more if desired, for example
3388 * if you want to share your set up code between the case of
3389 * establishing a new connection and this case.
3390 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003391 if (opt.nbio == 2)
3392 mbedtls_ssl_set_bio(&ssl, &client_fd, delayed_send,
3393 delayed_recv, NULL);
Jarno Lamsa304d61c2019-06-06 10:40:52 +03003394 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003395 mbedtls_ssl_set_bio(
3396 &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv,
3397 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL);
Jarno Lamsa304d61c2019-06-06 10:40:52 +03003398
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003399# if defined(MBEDTLS_TIMING_C)
3400 mbedtls_ssl_set_timer_cb(&ssl, &timer, mbedtls_timing_set_delay,
3401 mbedtls_timing_get_delay);
3402# endif /* MBEDTLS_TIMING_C */
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003403
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003404 mbedtls_printf(" ok\n");
Jarno Lamsa304d61c2019-06-06 10:40:52 +03003405 }
3406
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003407 mbedtls_printf(" . Deserializing connection...");
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03003408
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003409 if ((ret = mbedtls_ssl_context_load(&ssl, context_buf, buf_len)) != 0) {
3410 mbedtls_printf("failed\n ! mbedtls_ssl_context_load returned "
3411 "-0x%x\n\n",
3412 (unsigned int)-ret);
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03003413
3414 goto exit;
3415 }
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003416
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003417 mbedtls_free(context_buf);
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02003418 context_buf = NULL;
3419 context_buf_len = 0;
3420
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003421 mbedtls_printf(" ok\n");
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03003422 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003423# endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03003424
3425 /*
3426 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003427 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003428 if (--exchanges_left > 0)
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003429 goto data_exchange;
3430
3431 /*
3432 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003433 */
3434close_notify:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003435 mbedtls_printf(" . Closing the connection...");
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01003436
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003437 /* No error checking, the connection might be closed already */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003438 do
3439 ret = mbedtls_ssl_close_notify(&ssl);
3440 while (ret == MBEDTLS_ERR_SSL_WANT_WRITE);
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003441 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003442
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003443 mbedtls_printf(" done\n");
Rich Evansf90016a2015-01-19 14:26:37 +00003444
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003445 goto reset;
3446
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003447 /*
3448 * Cleanup and exit
3449 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003450exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003451# ifdef MBEDTLS_ERROR_C
3452 if (ret != 0) {
gabor-mezei-armde472172020-09-04 14:44:25 +02003453 char error_buf[100];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003454 mbedtls_strerror(ret, error_buf, 100);
3455 mbedtls_printf("Last error was: -0x%X - %s\n\n", (unsigned int)-ret,
3456 error_buf);
gabor-mezei-armde472172020-09-04 14:44:25 +02003457 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003458# endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003459
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003460 if (opt.query_config_mode == DFL_QUERY_CONFIG_MODE) {
3461 mbedtls_printf(" . Cleaning up...");
3462 fflush(stdout);
gabor-mezei-arma9eecf12020-07-07 11:11:02 +02003463 }
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003464
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003465 mbedtls_net_free(&client_fd);
3466 mbedtls_net_free(&listen_fd);
Paul Bakker0c226102014-04-17 16:02:36 +02003467
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003468# if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3469 mbedtls_dhm_free(&dhm);
3470# endif
3471# if defined(MBEDTLS_X509_CRT_PARSE_C)
3472 mbedtls_x509_crt_free(&cacert);
3473 mbedtls_x509_crt_free(&srvcert);
3474 mbedtls_pk_free(&pkey);
3475 mbedtls_x509_crt_free(&srvcert2);
3476 mbedtls_pk_free(&pkey2);
3477# endif
3478# if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3479 for (i = 0; (size_t)i < ssl_async_keys.slots_used; i++) {
3480 if (ssl_async_keys.slots[i].pk_owned) {
3481 mbedtls_pk_free(ssl_async_keys.slots[i].pk);
3482 mbedtls_free(ssl_async_keys.slots[i].pk);
Gilles Peskine44817442018-06-13 18:09:28 +02003483 ssl_async_keys.slots[i].pk = NULL;
3484 }
3485 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003486# endif
3487# if defined(SNI_OPTION)
3488 sni_free(sni_info);
3489# endif
3490# if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
3491 ret = psk_free(psk_info);
3492 if ((ret != 0) && (opt.query_config_mode == DFL_QUERY_CONFIG_MODE))
3493 mbedtls_printf("Failed to list of opaque PSKs - error was %d\n", ret);
3494# endif
3495# if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3496 mbedtls_dhm_free(&dhm);
3497# endif
Paul Bakkered27a042013-04-18 22:46:23 +02003498
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003499# if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) && \
3500 defined(MBEDTLS_USE_PSA_CRYPTO)
3501 if (opt.psk_opaque != 0) {
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00003502 /* This is ok even if the slot hasn't been
3503 * initialized (we might have jumed here
3504 * immediately because of bad cmd line params,
3505 * for example). */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003506 status = psa_destroy_key(psk_slot);
3507 if ((status != PSA_SUCCESS) &&
3508 (opt.query_config_mode == DFL_QUERY_CONFIG_MODE)) {
3509 mbedtls_printf("Failed to destroy key slot %u - error was %d",
3510 (unsigned)psk_slot, (int)status);
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00003511 }
3512 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003513# endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED && \
3514 MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00003515
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003516 mbedtls_ssl_free(&ssl);
3517 mbedtls_ssl_config_free(&conf);
3518 rng_free(&rng);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003519
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003520# if defined(MBEDTLS_SSL_CACHE_C)
3521 mbedtls_ssl_cache_free(&cache);
3522# endif
3523# if defined(MBEDTLS_SSL_SESSION_TICKETS)
3524 mbedtls_ssl_ticket_free(&ticket_ctx);
3525# endif
3526# if defined(MBEDTLS_SSL_COOKIE_C)
3527 mbedtls_ssl_cookie_free(&cookie_ctx);
3528# endif
Paul Bakker0a597072012-09-25 21:55:46 +00003529
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003530 mbedtls_free(buf);
Hanno Becker095d9cf2018-10-09 12:39:13 +01003531
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003532# if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
3533 if (context_buf != NULL)
3534 mbedtls_platform_zeroize(context_buf, context_buf_len);
3535 mbedtls_free(context_buf);
3536# endif
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02003537
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003538# if defined(MBEDTLS_USE_PSA_CRYPTO)
3539 mbedtls_psa_crypto_free();
3540# endif
Gilles Peskine414e7172021-02-03 00:04:08 +01003541
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003542# if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskined0a46e52021-02-03 00:03:03 +01003543 /* Let test hooks detect errors such as resource leaks.
3544 * Don't do it in query_config mode, because some test code prints
3545 * information to stdout and this gets mixed with the regular output. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003546 if (opt.query_config_mode == DFL_QUERY_CONFIG_MODE) {
3547 if (test_hooks_failure_detected()) {
3548 if (ret == 0)
Gilles Peskined0a46e52021-02-03 00:03:03 +01003549 ret = 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003550 mbedtls_printf("Test hooks detected errors.\n");
Gilles Peskined0a46e52021-02-03 00:03:03 +01003551 }
Gilles Peskine53dea742021-02-02 22:55:06 +01003552 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003553 test_hooks_free();
3554# endif /* MBEDTLS_TEST_HOOKS */
Gilles Peskine53dea742021-02-02 22:55:06 +01003555
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003556# if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
3557# if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003558 mbedtls_memory_buffer_alloc_status();
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003559# endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 mbedtls_memory_buffer_alloc_free();
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003561# endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
Paul Bakker82024bf2013-07-04 11:52:32 +02003562
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003563 if (opt.query_config_mode == DFL_QUERY_CONFIG_MODE) {
3564 mbedtls_printf(" done.\n");
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003565
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003566# if defined(_WIN32)
3567 mbedtls_printf(" + Press Enter to exit this program.\n");
3568 fflush(stdout);
3569 getchar();
3570# endif
gabor-mezei-arma9eecf12020-07-07 11:11:02 +02003571 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003572
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02003573 // Shell can not handle large exit numbers -> 1 for errors
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003574 if (ret < 0)
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02003575 ret = 1;
3576
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003577 if (opt.query_config_mode == DFL_QUERY_CONFIG_MODE)
3578 mbedtls_exit(ret);
gabor-mezei-arma9eecf12020-07-07 11:11:02 +02003579 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003580 mbedtls_exit(query_config_ret);
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003581}
Gilles Peskineb4df7542021-01-08 21:20:09 +01003582#endif /* !MBEDTLS_SSL_TEST_IMPOSSIBLE && MBEDTLS_SSL_SRV_C */