blob: f14944b04aa1097627f1f092fdd8dfd70611afba [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
Gilles Peskineab7ce962021-01-05 21:27:53 +010020#include "ssl_test_lib.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000021
Gilles Peskineab7ce962021-01-05 21:27:53 +010022#if defined(MBEDTLS_SSL_TEST_IMPOSSIBLE)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020023int main( void )
24{
Gilles Peskineab7ce962021-01-05 21:27:53 +010025 mbedtls_printf( MBEDTLS_SSL_TEST_IMPOSSIBLE );
26 mbedtls_exit( 0 );
27}
28#elif !defined(MBEDTLS_SSL_SRV_C)
29int main( void )
30{
31 mbedtls_printf( "MBEDTLS_SSL_SRV_C not defined.\n" );
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020032 mbedtls_exit( 0 );
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020033}
34#else
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010035
Andres AG788aa4a2016-09-14 14:32:09 +010036#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/ssl.h"
38#include "mbedtls/entropy.h"
39#include "mbedtls/ctr_drbg.h"
40#include "mbedtls/certs.h"
41#include "mbedtls/x509.h"
42#include "mbedtls/error.h"
43#include "mbedtls/debug.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020044#include "mbedtls/timing.h"
Piotr Nowicki3de298f2020-04-16 14:35:19 +020045#include "mbedtls/base64.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000046
Hanno Becker5a9942e2018-11-12 17:47:48 +000047#if defined(MBEDTLS_USE_PSA_CRYPTO)
48#include "psa/crypto.h"
Hanno Becker1d911cd2018-11-15 13:06:09 +000049#include "mbedtls/psa_util.h"
Hanno Becker5a9942e2018-11-12 17:47:48 +000050#endif
51
Ronald Cron7d866162020-06-18 10:36:26 +020052#include <test/helpers.h>
53
Rich Evans18b78c72015-02-11 14:06:19 +000054#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
Andres AG692ad842017-01-19 16:30:57 +000057#include <stdint.h>
Andres AG0b736db2017-02-08 14:05:57 +000058
59#if !defined(_MSC_VER)
Andres AG692ad842017-01-19 16:30:57 +000060#include <inttypes.h>
Andres AG0b736db2017-02-08 14:05:57 +000061#endif
Rich Evans18b78c72015-02-11 14:06:19 +000062
63#if !defined(_WIN32)
64#include <signal.h>
65#endif
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/ssl_cache.h"
Paul Bakker0a597072012-09-25 21:55:46 +000069#endif
70
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020071#if defined(MBEDTLS_SSL_TICKET_C)
72#include "mbedtls/ssl_ticket.h"
73#endif
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000076#include "mbedtls/ssl_cookie.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020077#endif
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000080#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker82024bf2013-07-04 11:52:32 +020081#endif
82
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020083#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO)
84#define SNI_OPTION
85#endif
86
87#if defined(_WIN32)
88#include <windows.h>
89#endif
90
Simon Butchercce68be2018-07-23 14:26:09 +010091/* Size of memory to be allocated for the heap, when using the library's memory
92 * management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */
93#define MEMORY_HEAP_SIZE 120000
94
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +010095#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020096#define DFL_SERVER_PORT "4433"
Andrzej Kurek30e731d2017-10-12 13:50:29 +020097#define DFL_RESPONSE_SIZE -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +000098#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010099#define DFL_NBIO 0
Hanno Becker16970d22017-10-10 15:56:37 +0100100#define DFL_EVENT 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200101#define DFL_READ_TIMEOUT 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000102#define DFL_CA_FILE ""
103#define DFL_CA_PATH ""
104#define DFL_CRT_FILE ""
105#define DFL_KEY_FILE ""
Hanno Beckere58a6302018-11-07 16:20:16 +0000106#define DFL_KEY_PWD ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200107#define DFL_CRT_FILE2 ""
108#define DFL_KEY_FILE2 ""
Hanno Beckere58a6302018-11-07 16:20:16 +0000109#define DFL_KEY_PWD2 ""
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100110#define DFL_ASYNC_OPERATIONS "-"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100111#define DFL_ASYNC_PRIVATE_DELAY1 ( -1 )
112#define DFL_ASYNC_PRIVATE_DELAY2 ( -1 )
Gilles Peskine3665f1d2018-01-05 21:22:12 +0100113#define DFL_ASYNC_PRIVATE_ERROR ( 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +0200114#define DFL_PSK ""
Hanno Becker1d911cd2018-11-15 13:06:09 +0000115#define DFL_PSK_OPAQUE 0
116#define DFL_PSK_LIST_OPAQUE 0
Paul Bakkerfbb17802013-04-17 19:10:21 +0200117#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200118#define DFL_ECJPAKE_PW NULL
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200119#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000120#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200121#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100123#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100124#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200125#define DFL_RENEGO_DELAY -2
Andres AG692ad842017-01-19 16:30:57 +0000126#define DFL_RENEGO_PERIOD ( (uint64_t)-1 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200127#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200128#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200129#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000130#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +0200131#define DFL_SHA1 -1
Hanno Beckera7d25422019-04-09 17:28:10 +0100132#define DFL_CID_ENABLED 0
133#define DFL_CID_VALUE ""
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100134#define DFL_CID_ENABLED_RENEGO -1
135#define DFL_CID_VALUE_RENEGO NULL
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100136#define DFL_AUTH_MODE -1
Janos Follath4817e272017-04-10 13:44:33 +0100137#define DFL_CERT_REQ_CA_LIST MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100139#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200141#define DFL_TICKET_TIMEOUT 86400
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100142#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100143#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100144#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200145#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100146#define DFL_CURVES NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200147#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200149#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200150#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200151#define DFL_HS_TO_MIN 0
152#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200153#define DFL_DTLS_MTU -1
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200154#define DFL_BADMAC_LIMIT -1
Hanno Beckere7675d02018-08-14 13:28:56 +0100155#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200156#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100157#define DFL_ETM -1
Jarno Lamsa9831c8a2019-05-29 13:33:32 +0300158#define DFL_SERIALIZE 0
Piotr Nowicki3de298f2020-04-16 14:35:19 +0200159#define DFL_CONTEXT_FILE ""
Jarno Lamsa9831c8a2019-05-29 13:33:32 +0300160#define DFL_EXTENDED_MS_ENFORCE -1
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200161#define DFL_CA_CALLBACK 0
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300162#define DFL_EAP_TLS 0
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200163#define DFL_REPRODUCIBLE 0
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100164#define DFL_NSS_KEYLOG 0
165#define DFL_NSS_KEYLOG_FILE NULL
gabor-mezei-arma9eecf12020-07-07 11:11:02 +0200166#define DFL_QUERY_CONFIG_MODE 0
Ron Eldor6ea64512018-07-02 10:08:07 +0300167#define DFL_USE_SRTP 0
Ron Eldor311b95a2018-07-08 08:50:31 +0300168#define DFL_SRTP_FORCE_PROFILE 0
Ron Eldor12c6ead2018-07-03 15:08:32 +0300169#define DFL_SRTP_SUPPORT_MKI 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000170
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200171#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
172 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
173 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
174 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
175 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
176 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
177 "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 +0200178
Paul Bakker8e714d72013-07-18 11:05:13 +0200179/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
180 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000181#define HTTP_RESPONSE \
182 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000183 "<h2>mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200184 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000185
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200186/*
187 * Size of the basic I/O buffer. Able to hold our default response.
188 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 * 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 +0200190 * if you change this value to something outside the range <= 100 or > 500
191 */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200192#define DFL_IO_BUF_LEN 200
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_X509_CRT_PARSE_C)
195#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000196#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100197 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
198 " default: \"\" (pre-loaded)\n" \
Hanno Becker422d1992019-05-01 11:16:28 +0100199 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100200 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
201 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
Hanno Becker422d1992019-05-01 11:16:28 +0100202 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100203 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200204 " default: see note after key_file2\n" \
205 " key_file=%%s default: see note after key_file2\n" \
Hanno Beckere58a6302018-11-07 16:20:16 +0000206 " key_pwd=%%s Password for key specified by key_file argument\n"\
207 " default: none\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200208 " crt_file2=%%s Your second cert and chain (in bottom to top order, top may be omitted)\n" \
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200209 " default: see note after key_file2\n" \
210 " key_file2=%%s default: see note below\n" \
211 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200212 " preloaded certificate(s) and key(s) are used if available\n" \
Hanno Beckere58a6302018-11-07 16:20:16 +0000213 " key_pwd2=%%s Password for key specified by key_file2 argument\n"\
214 " default: none\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200215 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
216 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000217#else
218#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200219 "\n" \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 " No file operations available (MBEDTLS_FS_IO not defined)\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200221 "\n"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200223#else
224#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200226
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200227#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100228#define USAGE_SSL_ASYNC \
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100229 " async_operations=%%c... d=decrypt, s=sign (default: -=off)\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100230 " async_private_delay1=%%d Asynchronous delay for key_file or preloaded key\n" \
Gilles Peskine166ce742018-04-30 10:30:49 +0200231 " async_private_delay2=%%d Asynchronous delay for key_file2 and sni\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100232 " default: -1 (not asynchronous)\n" \
Gilles Peskine60ee4ca2018-01-08 11:28:05 +0100233 " async_private_error=%%d Async callback error injection (default=0=none,\n" \
Gilles Peskinec9125722018-04-26 07:15:40 +0200234 " 1=start, 2=cancel, 3=resume, negative=first time only)"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100235#else
236#define USAGE_SSL_ASYNC ""
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200237#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100238
Hanno Beckera0e20d02019-05-15 14:03:01 +0100239#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +0100240#define USAGE_CID \
241 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
242 " default: 0 (disabled)\n" \
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100243 " cid_renego=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension during renegotiation.\n" \
Hanno Becker32798222019-05-23 17:01:06 +0100244 " default: same as 'cid' parameter\n" \
Hanno Beckera7d25422019-04-09 17:28:10 +0100245 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100246 " default: \"\"\n" \
247 " cid_val_renego=%%s The CID to use for incoming messages (in hex, without 0x) after renegotiation.\n" \
Hanno Becker32798222019-05-23 17:01:06 +0100248 " default: same as 'cid_val' parameter\n"
Hanno Beckera0e20d02019-05-15 14:03:01 +0100249#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +0100250#define USAGE_CID ""
Hanno Beckera0e20d02019-05-15 14:03:01 +0100251#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +0100252
Gilles Peskineeccd8882020-03-10 12:19:08 +0100253#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100254#define USAGE_PSK_RAW \
Piotr Nowicki9926eaf2019-11-20 14:54:36 +0100255 " psk=%%s default: \"\" (disabled)\n" \
256 " The PSK values are in hex, without 0x.\n" \
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100257 " psk_list=%%s default: \"\"\n" \
Andrzej Kurekb274f272019-02-05 05:06:35 -0500258 " A list of (PSK identity, PSK value) pairs.\n" \
259 " The PSK values are in hex, without 0x.\n" \
260 " id1,psk1[,id2,psk2[,...]]\n" \
261 " psk_identity=%%s default: \"Client_identity\"\n"
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100262#if defined(MBEDTLS_USE_PSA_CRYPTO)
263#define USAGE_PSK_SLOT \
Hanno Becker1d911cd2018-11-15 13:06:09 +0000264 " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
265 " Enable this to store the PSK configured through command line\n" \
266 " parameter `psk` in a PSA-based key slot.\n" \
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100267 " Note: Currently only supported in conjunction with\n" \
268 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
269 " to force a particular PSK-only ciphersuite.\n" \
270 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
271 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
272 " with prepopulated key slots instead of importing raw key material.\n" \
Hanno Becker1d911cd2018-11-15 13:06:09 +0000273 " psk_list_opaque=%%d default: 0 (don't use opaque dynamic PSKs)\n" \
274 " Enable this to store the list of dynamically chosen PSKs configured\n" \
275 " through the command line parameter `psk_list` in PSA-based key slots.\n" \
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100276 " Note: Currently only supported in conjunction with\n" \
277 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
278 " to force a particular PSK-only ciphersuite.\n" \
279 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
280 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
281 " with prepopulated key slots instead of importing raw key material.\n"
282#else
283#define USAGE_PSK_SLOT ""
284#endif /* MBEDTLS_USE_PSA_CRYPTO */
285#define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
Paul Bakkered27a042013-04-18 22:46:23 +0200286#else
287#define USAGE_PSK ""
Gilles Peskineeccd8882020-03-10 12:19:08 +0100288#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200289#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
290#define USAGE_CA_CALLBACK \
291 " ca_callback=%%d default: 0 (disabled)\n" \
292 " Enable this to use the trusted certificate callback function\n"
293#else
294#define USAGE_CA_CALLBACK ""
295#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200297#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100298 " tickets=%%d default: 1 (enabled)\n" \
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200299 " ticket_timeout=%%d default: 86400 (one day)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200300#else
301#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200303
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300304#if defined(MBEDTLS_SSL_EXPORT_KEYS)
305#define USAGE_EAP_TLS \
306 " eap_tls=%%d default: 0 (disabled)\n"
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100307#define USAGE_NSS_KEYLOG \
Hanno Beckerbc5308c2019-09-09 11:38:51 +0100308 " nss_keylog=%%d default: 0 (disabled)\n" \
309 " This cannot be used with eap_tls=1\n"
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100310#define USAGE_NSS_KEYLOG_FILE \
311 " nss_keylog_file=%%s\n"
Ron Eldor65b56ef2019-09-26 16:40:48 +0300312#if defined(MBEDTLS_SSL_DTLS_SRTP)
313#define USAGE_SRTP \
314 " use_srtp=%%d default: 0 (disabled)\n" \
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200315 " srtp_force_profile=%%d default: 0 (all enabled)\n" \
Ron Eldor65b56ef2019-09-26 16:40:48 +0300316 " available profiles:\n" \
317 " 1 - SRTP_AES128_CM_HMAC_SHA1_80\n" \
318 " 2 - SRTP_AES128_CM_HMAC_SHA1_32\n" \
319 " 3 - SRTP_NULL_HMAC_SHA1_80\n" \
320 " 4 - SRTP_NULL_HMAC_SHA1_32\n" \
321 " support_mki=%%d default: 0 (not supported)\n"
322#else /* MBEDTLS_SSL_DTLS_SRTP */
323#define USAGE_SRTP ""
324#endif
325#else /* MBEDTLS_SSL_EXPORT_KEYS */
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300326#define USAGE_EAP_TLS ""
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100327#define USAGE_NSS_KEYLOG ""
328#define USAGE_NSS_KEYLOG_FILE ""
Ron Eldor65b56ef2019-09-26 16:40:48 +0300329#define USAGE_SRTP ""
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300330#endif /* MBEDTLS_SSL_EXPORT_KEYS */
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100333#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100334 " cache_max=%%d default: cache default (50)\n" \
335 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100336#else
337#define USAGE_CACHE ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338#endif /* MBEDTLS_SSL_CACHE_C */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100339
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200340#if defined(SNI_OPTION)
Ron Eldor80d04192019-04-04 15:02:01 +0300341#if defined(MBEDTLS_X509_CRL_PARSE_C)
342#define SNI_CRL ",crl"
343#else
344#define SNI_CRL ""
345#endif
346
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100347#define USAGE_SNI \
Ron Eldor80d04192019-04-04 15:02:01 +0300348 " sni=%%s name1,cert1,key1,ca1"SNI_CRL",auth1[,...]\n" \
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200349 " default: disabled\n"
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100350#else
351#define USAGE_SNI ""
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200352#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200355#define USAGE_MAX_FRAG_LEN \
356 " max_frag_len=%%d default: 16384 (tls default)\n" \
357 " options: 512, 1024, 2048, 4096\n"
358#else
359#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100363#define USAGE_TRUNC_HMAC \
364 " trunc_hmac=%%d default: library default\n"
365#else
366#define USAGE_TRUNC_HMAC ""
367#endif
368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200370#define USAGE_ALPN \
371 " alpn=%%s default: \"\" (disabled)\n" \
372 " example: spdy/1,http/1.1\n"
373#else
374#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200378#define USAGE_COOKIES \
379 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200380 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200381#else
382#define USAGE_COOKIES ""
383#endif
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200386#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200387 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200388#else
389#define USAGE_ANTI_REPLAY ""
390#endif
391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200393#define USAGE_BADMAC_LIMIT \
394 " badmac_limit=%%d default: (library default: disabled)\n"
395#else
396#define USAGE_BADMAC_LIMIT ""
397#endif
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200400#define USAGE_DTLS \
401 " dtls=%%d default: 0 (TLS)\n" \
402 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200403 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Beckere7675d02018-08-14 13:28:56 +0100404 " mtu=%%d default: (library default: unlimited)\n" \
405 " dgram_packing=%%d default: 1 (allowed)\n" \
406 " allow or forbid packing of multiple\n" \
407 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200408#else
409#define USAGE_DTLS ""
410#endif
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200413#define USAGE_EMS \
414 " extended_ms=0/1 default: (library default: on)\n"
415#else
416#define USAGE_EMS ""
417#endif
418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100420#define USAGE_ETM \
421 " etm=0/1 default: (library default: on)\n"
422#else
423#define USAGE_ETM ""
424#endif
425
Philippe Antoine738153a2019-06-18 20:16:43 +0200426#define USAGE_REPRODUCIBLE \
427 " reproducible=0/1 default: 0 (disabled)\n"
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100430#define USAGE_RENEGO \
431 " renegotiation=%%d default: 0 (disabled)\n" \
432 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100433 " renego_delay=%%d default: -2 (library default)\n" \
Andres AG692ad842017-01-19 16:30:57 +0000434 " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100435#else
436#define USAGE_RENEGO ""
437#endif
438
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200439#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
440#define USAGE_ECJPAKE \
441 " ecjpake_pw=%%s default: none (disabled)\n"
442#else
443#define USAGE_ECJPAKE ""
444#endif
445
Hanno Beckere6706e62017-05-15 16:05:15 +0100446#if defined(MBEDTLS_ECP_C)
447#define USAGE_CURVES \
448 " curves=a,b,c,d default: \"default\" (library default)\n" \
449 " example: \"secp521r1,brainpoolP512r1\"\n" \
450 " - use \"none\" for empty list\n" \
451 " - see mbedtls_ecp_curve_list()\n" \
452 " for acceptable curve names\n"
453#else
454#define USAGE_CURVES ""
455#endif
456
Jarno Lamsabbc7b412019-06-04 11:06:31 +0300457#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
458#define USAGE_SERIALIZATION \
Piotr Nowicki3de298f2020-04-16 14:35:19 +0200459 " serialize=%%d default: 0 (do not serialize/deserialize)\n" \
460 " options: 1 (serialize)\n" \
461 " 2 (serialize with re-initialization)\n" \
462 " context_file=%%s The file path to write a serialized connection\n"\
463 " in the form of base64 code (serialize option\n" \
464 " must be set)\n" \
465 " default: \"\" (do nothing)\n" \
466 " option: a file path\n"
Jarno Lamsabbc7b412019-06-04 11:06:31 +0300467#else
468#define USAGE_SERIALIZATION ""
469#endif
470
Gilles Peskineb2971ff2020-04-14 19:41:01 +0200471/* USAGE is arbitrarily split to stay under the portable string literal
472 * length limit: 4095 bytes in C99. */
473#define USAGE1 \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000474 "\n usage: ssl_server2 param=<>...\n" \
475 "\n acceptable parameters:\n" \
Ron Eldor71f68c42017-09-26 11:29:11 +0300476 " server_addr=%%s default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000477 " server_port=%%d default: 4433\n" \
478 " debug_level=%%d default: 0 (disabled)\n" \
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200479 " buffer_size=%%d default: 200 \n" \
480 " (minimum: 1, max: 16385)\n" \
481 " response_size=%%d default: about 152 (basic response)\n" \
482 " (minimum: 0, max: 16384)\n" \
483 " increases buffer_size if bigger\n"\
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100484 " nbio=%%d default: 0 (blocking I/O)\n" \
485 " options: 1 (non-blocking), 2 (added delays)\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100486 " event=%%d default: 0 (loop)\n" \
487 " options: 1 (level-triggered, implies nbio=1),\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200488 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100489 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200490 USAGE_DTLS \
Ron Eldor6ea64512018-07-02 10:08:07 +0300491 USAGE_SRTP \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200492 USAGE_COOKIES \
493 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200494 USAGE_BADMAC_LIMIT \
Gilles Peskineb2971ff2020-04-14 19:41:01 +0200495 "\n"
496#define USAGE2 \
Manuel Pégourié-Gonnardee6139c2015-05-07 10:18:26 +0100497 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100498 " options: none, optional, required\n" \
Janos Follath4817e272017-04-10 13:44:33 +0100499 " cert_req_ca_list=%%d default: 1 (send ca list)\n" \
500 " options: 1 (send ca list), 0 (don't send)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000501 USAGE_IO \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100502 "\n" \
503 USAGE_PSK \
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200504 USAGE_CA_CALLBACK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200505 USAGE_ECJPAKE \
Gilles Peskineb2971ff2020-04-14 19:41:01 +0200506 "\n"
507#define USAGE3 \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100508 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100509 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200510 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200511 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100512 USAGE_TICKETS \
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300513 USAGE_EAP_TLS \
Philippe Antoine738153a2019-06-18 20:16:43 +0200514 USAGE_REPRODUCIBLE \
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100515 USAGE_NSS_KEYLOG \
516 USAGE_NSS_KEYLOG_FILE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100517 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100518 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100519 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200520 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200521 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100522 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100523 USAGE_CURVES \
Gilles Peskineb2971ff2020-04-14 19:41:01 +0200524 "\n"
525#define USAGE4 \
Hanno Becker34ce81f2020-08-17 09:40:54 +0100526 USAGE_SSL_ASYNC \
527 USAGE_SNI \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100528 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200529 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200530 " min_version=%%s default: (library default: tls1)\n" \
531 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200532 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100533 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200534 "\n" \
535 " version_suites=a,b,c,d per-version ciphersuites\n" \
536 " in order from ssl3 to tls1_2\n" \
537 " default: all enabled\n" \
538 " force_ciphersuite=<name> default: all enabled\n" \
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100539 " query_config=<name> return 0 if the specified\n" \
540 " configuration macro is defined and 1\n" \
541 " otherwise. The expansion of the macro\n" \
542 " is printed if it is defined\n" \
Jarno Lamsabbc7b412019-06-04 11:06:31 +0300543 USAGE_SERIALIZATION \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000544 " acceptable ciphersuite names:\n"
545
Hanno Becker8651a432017-06-09 16:13:22 +0100546#define ALPN_LIST_SIZE 10
547#define CURVE_LIST_SIZE 20
548
Andres AG692ad842017-01-19 16:30:57 +0000549#define PUT_UINT64_BE(out_be,in_le,i) \
550{ \
551 (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \
552 (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \
553 (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \
554 (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \
555 (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \
556 (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \
557 (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \
558 (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \
559}
560
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500561
Rich Evans85b05ec2015-02-12 11:37:29 +0000562/*
563 * global options
564 */
565struct options
566{
567 const char *server_addr; /* address on which the ssl service runs */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200568 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000569 int debug_level; /* level of debugging */
570 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100571 int event; /* loop or event-driven IO? level or edge triggered? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200573 int response_size; /* pad response with header to requested size */
574 uint16_t buffer_size; /* IO buffer size */
Rich Evans85b05ec2015-02-12 11:37:29 +0000575 const char *ca_file; /* the file with the CA certificate(s) */
576 const char *ca_path; /* the path with the CA certificate(s) reside */
577 const char *crt_file; /* the file with the server certificate */
578 const char *key_file; /* the file with the server key */
Hanno Beckere58a6302018-11-07 16:20:16 +0000579 const char *key_pwd; /* the password for the server key */
Rich Evans85b05ec2015-02-12 11:37:29 +0000580 const char *crt_file2; /* the file with the 2nd server certificate */
581 const char *key_file2; /* the file with the 2nd server key */
Hanno Beckere58a6302018-11-07 16:20:16 +0000582 const char *key_pwd2; /* the password for the 2nd server key */
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100583 const char *async_operations; /* supported SSL asynchronous operations */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100584 int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
585 int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
586 int async_private_error; /* inject error in async private callback */
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100587#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000588 int psk_opaque;
589 int psk_list_opaque;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100590#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200591#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000592 int ca_callback; /* Use callback for trusted certificate list */
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200593#endif
Rich Evans85b05ec2015-02-12 11:37:29 +0000594 const char *psk; /* the pre-shared key */
595 const char *psk_identity; /* the pre-shared key identity */
596 char *psk_list; /* list of PSK id/key pairs for callback */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200597 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000598 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
599 const char *version_suites; /* per-version ciphersuites */
600 int renegotiation; /* enable / disable renegotiation */
601 int allow_legacy; /* allow legacy renegotiation */
602 int renegotiate; /* attempt renegotiation? */
603 int renego_delay; /* delay before enforcing renegotiation */
Andres AG692ad842017-01-19 16:30:57 +0000604 uint64_t renego_period; /* period for automatic renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000605 int exchanges; /* number of data exchanges */
606 int min_version; /* minimum protocol version accepted */
607 int max_version; /* maximum protocol version accepted */
608 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200609 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000610 int auth_mode; /* verify mode for connection */
Janos Follath4817e272017-04-10 13:44:33 +0100611 int cert_req_ca_list; /* should we send the CA list? */
Rich Evans85b05ec2015-02-12 11:37:29 +0000612 unsigned char mfl_code; /* code for maximum fragment length */
613 int trunc_hmac; /* accept truncated hmac? */
614 int tickets; /* enable / disable session tickets */
615 int ticket_timeout; /* session ticket lifetime */
616 int cache_max; /* max number of session cache entries */
617 int cache_timeout; /* expiration delay of session cache entries */
618 char *sni; /* string describing sni information */
Hanno Beckere6706e62017-05-15 16:05:15 +0100619 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000620 const char *alpn_string; /* ALPN supported protocols */
621 const char *dhm_file; /* the file with the DH parameters */
622 int extended_ms; /* allow negotiation of extended MS? */
623 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000624 int transport; /* TLS or DTLS? */
625 int cookies; /* Use cookies for DTLS? -1 to break them */
626 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
627 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
628 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200629 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Hanno Beckere7675d02018-08-14 13:28:56 +0100630 int dgram_packing; /* allow/forbid datagram packing */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000631 int badmac_limit; /* Limit of records with bad MAC */
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300632 int eap_tls; /* derive EAP-TLS keying material? */
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100633 int nss_keylog; /* export NSS key log material */
634 const char *nss_keylog_file; /* NSS key log file */
Hanno Beckera7d25422019-04-09 17:28:10 +0100635 int cid_enabled; /* whether to use the CID extension or not */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100636 int cid_enabled_renego; /* whether to use the CID extension or not
637 * during renegotiation */
Hanno Beckera7d25422019-04-09 17:28:10 +0100638 const char *cid_val; /* the CID to use for incoming messages */
Jarno Lamsa9831c8a2019-05-29 13:33:32 +0300639 int serialize; /* serialize/deserialize connection */
Piotr Nowicki3de298f2020-04-16 14:35:19 +0200640 const char *context_file; /* the file to write a serialized connection
641 * in the form of base64 code (serialize
642 * option must be set) */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100643 const char *cid_val_renego; /* the CID to use for incoming messages
644 * after renegotiation */
Philippe Antoine154feb22019-06-11 17:50:23 +0200645 int reproducible; /* make communication reproducible */
gabor-mezei-arma9eecf12020-07-07 11:11:02 +0200646 int query_config_mode; /* whether to read config */
Ron Eldor6ea64512018-07-02 10:08:07 +0300647 int use_srtp; /* Support SRTP */
648 int force_srtp_profile; /* SRTP protection profile to use or all */
Ron Eldor12c6ead2018-07-03 15:08:32 +0300649 int support_mki; /* The dtls mki mki support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000650} opt;
651
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100652int query_config( const char *config );
653
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300654#if defined(MBEDTLS_SSL_EXPORT_KEYS)
655typedef struct eap_tls_keys
656{
657 unsigned char master_secret[48];
658 unsigned char randbytes[64];
Ron Eldor51d3ab52019-05-12 14:54:30 +0300659 mbedtls_tls_prf_types tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300660} eap_tls_keys;
661
662static int eap_tls_key_derivation ( void *p_expkey,
663 const unsigned char *ms,
664 const unsigned char *kb,
665 size_t maclen,
666 size_t keylen,
667 size_t ivlen,
Jaeden Amero63d813d2019-09-12 10:09:57 +0100668 const unsigned char client_random[32],
669 const unsigned char server_random[32],
Ron Eldor51d3ab52019-05-12 14:54:30 +0300670 mbedtls_tls_prf_types tls_prf_type )
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300671{
672 eap_tls_keys *keys = (eap_tls_keys *)p_expkey;
673
674 ( ( void ) kb );
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300675 memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) );
676 memcpy( keys->randbytes, client_random, 32 );
677 memcpy( keys->randbytes + 32, server_random, 32 );
Ron Eldor51d3ab52019-05-12 14:54:30 +0300678 keys->tls_prf_type = tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300679
Ron Eldorf75e2522019-05-14 20:38:49 +0300680 if( opt.debug_level > 2 )
681 {
Ron Eldor801faf02019-05-15 17:45:24 +0300682 mbedtls_printf("exported maclen is %u\n", (unsigned)maclen);
683 mbedtls_printf("exported keylen is %u\n", (unsigned)keylen);
684 mbedtls_printf("exported ivlen is %u\n", (unsigned)ivlen);
Ron Eldorf75e2522019-05-14 20:38:49 +0300685 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300686 return( 0 );
687}
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100688
689static int nss_keylog_export( void *p_expkey,
690 const unsigned char *ms,
691 const unsigned char *kb,
692 size_t maclen,
693 size_t keylen,
694 size_t ivlen,
Jaeden Amero63d813d2019-09-12 10:09:57 +0100695 const unsigned char client_random[32],
696 const unsigned char server_random[32],
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100697 mbedtls_tls_prf_types tls_prf_type )
698{
699 char nss_keylog_line[ 200 ];
700 size_t const client_random_len = 32;
701 size_t const master_secret_len = 48;
702 size_t len = 0;
703 size_t j;
704 int ret = 0;
705
706 ((void) p_expkey);
707 ((void) kb);
708 ((void) maclen);
709 ((void) keylen);
710 ((void) ivlen);
711 ((void) server_random);
712 ((void) tls_prf_type);
713
714 len += sprintf( nss_keylog_line + len,
715 "%s", "CLIENT_RANDOM " );
716
717 for( j = 0; j < client_random_len; j++ )
718 {
719 len += sprintf( nss_keylog_line + len,
720 "%02x", client_random[j] );
721 }
722
723 len += sprintf( nss_keylog_line + len, " " );
724
725 for( j = 0; j < master_secret_len; j++ )
726 {
727 len += sprintf( nss_keylog_line + len,
728 "%02x", ms[j] );
729 }
730
731 len += sprintf( nss_keylog_line + len, "\n" );
732 nss_keylog_line[ len ] = '\0';
733
734 mbedtls_printf( "\n" );
735 mbedtls_printf( "---------------- NSS KEYLOG -----------------\n" );
736 mbedtls_printf( "%s", nss_keylog_line );
737 mbedtls_printf( "---------------------------------------------\n" );
738
739 if( opt.nss_keylog_file != NULL )
740 {
741 FILE *f;
742
743 if( ( f = fopen( opt.nss_keylog_file, "a" ) ) == NULL )
744 {
745 ret = -1;
746 goto exit;
747 }
748
749 if( fwrite( nss_keylog_line, 1, len, f ) != len )
750 {
751 ret = -1;
k-stachowiakbbc1c692019-09-26 13:36:54 +0200752 fclose( f );
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100753 goto exit;
754 }
755
756 fclose( f );
757 }
758
759exit:
760 mbedtls_platform_zeroize( nss_keylog_line,
761 sizeof( nss_keylog_line ) );
762 return( ret );
763}
764
Ron Eldor65b56ef2019-09-26 16:40:48 +0300765#if defined( MBEDTLS_SSL_DTLS_SRTP )
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200766/* Supported SRTP mode needs a maximum of :
767 * - 16 bytes for key (AES-128)
768 * - 14 bytes SALT
769 * One for sender, one for receiver context
770 */
771#define MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH 60
Ron Eldor65b56ef2019-09-26 16:40:48 +0300772typedef struct dtls_srtp_keys
773{
774 unsigned char master_secret[48];
775 unsigned char randbytes[64];
776 mbedtls_tls_prf_types tls_prf_type;
777} dtls_srtp_keys;
778
779static int dtls_srtp_key_derivation( void *p_expkey,
780 const unsigned char *ms,
781 const unsigned char *kb,
782 size_t maclen,
783 size_t keylen,
784 size_t ivlen,
785 const unsigned char client_random[32],
786 const unsigned char server_random[32],
787 mbedtls_tls_prf_types tls_prf_type )
788{
789 dtls_srtp_keys *keys = (dtls_srtp_keys *)p_expkey;
790
791 ( ( void ) kb );
792 memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) );
793 memcpy( keys->randbytes, client_random, 32 );
794 memcpy( keys->randbytes + 32, server_random, 32 );
795 keys->tls_prf_type = tls_prf_type;
796
797 if( opt.debug_level > 2 )
798 {
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200799 mbedtls_printf( "exported maclen is %u\n", (unsigned) maclen );
800 mbedtls_printf( "exported keylen is %u\n", (unsigned) keylen );
801 mbedtls_printf( "exported ivlen is %u\n", (unsigned) ivlen );
Ron Eldor65b56ef2019-09-26 16:40:48 +0300802 }
803 return( 0 );
804}
805#endif /* MBEDTLS_SSL_DTLS_SRTP */
806
807#endif /* MBEDTLS_SSL_EXPORT_KEYS */
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300808
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200809static void my_debug( void *ctx, int level,
810 const char *file, int line,
811 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000812{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200813 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000814
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200815 /* Extract basename from file */
816 for( p = basename = file; *p != '\0'; p++ )
817 if( *p == '/' || *p == '\\' )
818 basename = p + 1;
819
820 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000821 fflush( (FILE *) ctx );
822}
823
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200824mbedtls_time_t dummy_constant_time( mbedtls_time_t* time )
825{
826 (void) time;
827 return 0x5af2a056;
828}
829
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200830int dummy_entropy( void *data, unsigned char *output, size_t len )
831{
832 size_t i;
Philippe Antoine12e85de2019-06-11 16:07:53 +0200833 int ret;
Philippe Antoined2235f22019-06-11 16:29:28 +0200834 (void) data;
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200835
Philippe Antoine3ca50852019-06-07 22:31:59 +0200836 ret = mbedtls_entropy_func( data, output, len );
Philippe Antoine986b6f22019-06-07 15:04:32 +0200837 for (i = 0; i < len; i++ ) {
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200838 //replace result with pseudo random
839 output[i] = (unsigned char) rand();
840 }
Philippe Antoine3ca50852019-06-07 22:31:59 +0200841 return( ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200842}
843
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200844#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000845int ca_callback( void *data, mbedtls_x509_crt const *child,
846 mbedtls_x509_crt **candidates)
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200847{
Hanno Beckercbb59032019-03-28 14:14:22 +0000848 int ret = 0;
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200849 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +0000850 mbedtls_x509_crt *first;
851
852 /* This is a test-only implementation of the CA callback
853 * which always returns the entire list of trusted certificates.
854 * Production implementations managing a large number of CAs
855 * should use an efficient presentation and lookup for the
856 * set of trusted certificates (such as a hashtable) and only
857 * return those trusted certificates which satisfy basic
858 * parental checks, such as the matching of child `Issuer`
859 * and parent `Subject` field. */
860 ((void) child);
861
862 first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
863 if( first == NULL )
864 {
865 ret = -1;
866 goto exit;
867 }
868 mbedtls_x509_crt_init( first );
869
870 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
871 {
872 ret = -1;
873 goto exit;
874 }
875
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200876 while( ca->next != NULL )
877 {
878 ca = ca->next;
Hanno Beckercbb59032019-03-28 14:14:22 +0000879 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
880 {
881 ret = -1;
882 goto exit;
883 }
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200884 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000885
886exit:
887
888 if( ret != 0 )
889 {
890 mbedtls_x509_crt_free( first );
891 mbedtls_free( first );
892 first = NULL;
893 }
894
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200895 *candidates = first;
Hanno Beckercbb59032019-03-28 14:14:22 +0000896 return( ret );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200897}
898#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
899
Rich Evans85b05ec2015-02-12 11:37:29 +0000900/*
901 * Test recv/send functions that make sure each try returns
902 * WANT_READ/WANT_WRITE at least once before sucesseding
903 */
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100904static int delayed_recv( void *ctx, unsigned char *buf, size_t len )
Rich Evans85b05ec2015-02-12 11:37:29 +0000905{
906 static int first_try = 1;
907 int ret;
908
909 if( first_try )
910 {
911 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100912 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000913 }
914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100916 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000917 first_try = 1; /* Next call will be a new operation */
918 return( ret );
919}
920
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100921static int delayed_send( void *ctx, const unsigned char *buf, size_t len )
Rich Evans85b05ec2015-02-12 11:37:29 +0000922{
923 static int first_try = 1;
924 int ret;
925
926 if( first_try )
927 {
928 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100929 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000930 }
931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100933 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000934 first_try = 1; /* Next call will be a new operation */
935 return( ret );
936}
937
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100938typedef struct
939{
940 mbedtls_ssl_context *ssl;
941 mbedtls_net_context *net;
942} io_ctx_t;
943
Hanno Becker4b6649e2019-07-03 17:14:41 +0100944#if defined(MBEDTLS_SSL_RECORD_CHECKING)
945static int ssl_check_record( mbedtls_ssl_context const *ssl,
946 unsigned char const *buf, size_t len )
947{
948 int ret;
949 unsigned char *tmp_buf;
950
951 /* Record checking may modify the input buffer,
952 * so make a copy. */
953 tmp_buf = mbedtls_calloc( 1, len );
954 if( tmp_buf == NULL )
955 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
956 memcpy( tmp_buf, buf, len );
957
958 ret = mbedtls_ssl_check_record( ssl, tmp_buf, len );
959 if( ret != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE )
960 {
961 int ret_repeated;
962
963 /* Test-only: Make sure that mbedtls_ssl_check_record()
964 * doesn't alter state. */
965 memcpy( tmp_buf, buf, len ); /* Restore buffer */
966 ret_repeated = mbedtls_ssl_check_record( ssl, tmp_buf, len );
967 if( ret != ret_repeated )
968 {
Hanno Becker91f83272019-07-24 13:59:07 +0100969 mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" );
970 return( -1 );
Hanno Becker4b6649e2019-07-03 17:14:41 +0100971 }
972
973 switch( ret )
974 {
975 case 0:
976 break;
977
978 case MBEDTLS_ERR_SSL_INVALID_RECORD:
979 if( opt.debug_level > 1 )
980 mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" );
981 break;
982
983 case MBEDTLS_ERR_SSL_INVALID_MAC:
984 if( opt.debug_level > 1 )
985 mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" );
986 break;
987
988 case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD:
989 if( opt.debug_level > 1 )
990 mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" );
991 break;
992
993 default:
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200994 mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", (unsigned int) -ret );
Hanno Becker4b6649e2019-07-03 17:14:41 +0100995 return( -1 );
996 }
997
998 /* Regardless of the outcome, forward the record to the stack. */
999 }
1000
Hanno Becker4b6649e2019-07-03 17:14:41 +01001001 mbedtls_free( tmp_buf );
1002
1003 return( 0 );
1004}
1005#endif /* MBEDTLS_SSL_RECORD_CHECKING */
1006
Hanno Beckerdcc94e62019-07-03 17:05:43 +01001007static int recv_cb( void *ctx, unsigned char *buf, size_t len )
1008{
1009 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
1010 size_t recv_len;
1011 int ret;
1012
1013 if( opt.nbio == 2 )
1014 ret = delayed_recv( io_ctx->net, buf, len );
1015 else
1016 ret = mbedtls_net_recv( io_ctx->net, buf, len );
1017 if( ret < 0 )
1018 return( ret );
1019 recv_len = (size_t) ret;
1020
1021 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1022 {
1023 /* Here's the place to do any datagram/record checking
1024 * in between receiving the packet from the underlying
1025 * transport and passing it on to the TLS stack. */
Hanno Becker4b6649e2019-07-03 17:14:41 +01001026#if defined(MBEDTLS_SSL_RECORD_CHECKING)
1027 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
1028 return( -1 );
1029#endif /* MBEDTLS_SSL_RECORD_CHECKING */
Hanno Beckerdcc94e62019-07-03 17:05:43 +01001030 }
1031
1032 return( (int) recv_len );
1033}
1034
1035static int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len,
1036 uint32_t timeout )
1037{
1038 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
1039 int ret;
1040 size_t recv_len;
1041
1042 ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout );
1043 if( ret < 0 )
1044 return( ret );
1045 recv_len = (size_t) ret;
1046
1047 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1048 {
1049 /* Here's the place to do any datagram/record checking
1050 * in between receiving the packet from the underlying
1051 * transport and passing it on to the TLS stack. */
Hanno Becker4b6649e2019-07-03 17:14:41 +01001052#if defined(MBEDTLS_SSL_RECORD_CHECKING)
1053 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
1054 return( -1 );
1055#endif /* MBEDTLS_SSL_RECORD_CHECKING */
Hanno Beckerdcc94e62019-07-03 17:05:43 +01001056 }
1057
1058 return( (int) recv_len );
1059}
1060
1061static int send_cb( void *ctx, unsigned char const *buf, size_t len )
1062{
1063 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
1064
1065 if( opt.nbio == 2 )
1066 return( delayed_send( io_ctx->net, buf, len ) );
1067
1068 return( mbedtls_net_send( io_ctx->net, buf, len ) );
1069}
Gilles Peskine0d980b82021-01-05 23:34:27 +01001070#include "ssl_test_common_source.c"
Hanno Beckerdcc94e62019-07-03 17:05:43 +01001071
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +02001072/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001073 * Return authmode from string, or -1 on error
1074 */
1075static int get_auth_mode( const char *s )
1076{
1077 if( strcmp( s, "none" ) == 0 )
1078 return( MBEDTLS_SSL_VERIFY_NONE );
1079 if( strcmp( s, "optional" ) == 0 )
1080 return( MBEDTLS_SSL_VERIFY_OPTIONAL );
1081 if( strcmp( s, "required" ) == 0 )
1082 return( MBEDTLS_SSL_VERIFY_REQUIRED );
1083
1084 return( -1 );
1085}
1086
1087/*
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +02001088 * Used by sni_parse and psk_parse to handle coma-separated lists
1089 */
1090#define GET_ITEM( dst ) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001091 do \
1092 { \
1093 (dst) = p; \
1094 while( *p != ',' ) \
1095 if( ++p > end ) \
1096 goto error; \
1097 *p++ = '\0'; \
1098 } while( 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +02001099
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001100#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001101typedef struct _sni_entry sni_entry;
1102
1103struct _sni_entry {
1104 const char *name;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 mbedtls_x509_crt *cert;
1106 mbedtls_pk_context *key;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001107 mbedtls_x509_crt* ca;
1108 mbedtls_x509_crl* crl;
1109 int authmode;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001110 sni_entry *next;
1111};
1112
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001113void sni_free( sni_entry *head )
1114{
1115 sni_entry *cur = head, *next;
1116
1117 while( cur != NULL )
1118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 mbedtls_x509_crt_free( cur->cert );
1120 mbedtls_free( cur->cert );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 mbedtls_pk_free( cur->key );
1123 mbedtls_free( cur->key );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001124
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001125 mbedtls_x509_crt_free( cur->ca );
1126 mbedtls_free( cur->ca );
Ron Eldor80d04192019-04-04 15:02:01 +03001127#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001128 mbedtls_x509_crl_free( cur->crl );
1129 mbedtls_free( cur->crl );
Ron Eldor80d04192019-04-04 15:02:01 +03001130#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001131 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 mbedtls_free( cur );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001133 cur = next;
1134 }
1135}
1136
1137/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001138 * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
1139 * into a usable sni_entry list. For ca1, crl1, auth1, the special value
1140 * '-' means unset. If ca1 is unset, then crl1 is ignored too.
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001141 *
1142 * Modifies the input string! This is not production quality!
1143 */
1144sni_entry *sni_parse( char *sni_string )
1145{
1146 sni_entry *cur = NULL, *new = NULL;
1147 char *p = sni_string;
1148 char *end = p;
Ron Eldor80d04192019-04-04 15:02:01 +03001149 char *crt_file, *key_file, *ca_file, *auth_str;
1150#if defined(MBEDTLS_X509_CRL_PARSE_C)
1151 char *crl_file;
1152#endif
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001153
1154 while( *end != '\0' )
1155 ++end;
1156 *end = ',';
1157
1158 while( p <= end )
1159 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001160 if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001161 {
1162 sni_free( cur );
1163 return( NULL );
1164 }
1165
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001166 GET_ITEM( new->name );
1167 GET_ITEM( crt_file );
1168 GET_ITEM( key_file );
1169 GET_ITEM( ca_file );
Ron Eldor80d04192019-04-04 15:02:01 +03001170#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001171 GET_ITEM( crl_file );
Ron Eldor80d04192019-04-04 15:02:01 +03001172#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001173 GET_ITEM( auth_str );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001174
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001175 if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
1176 ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001177 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 mbedtls_x509_crt_init( new->cert );
1180 mbedtls_pk_init( new->key );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
1183 mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001184 goto error;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001185
1186 if( strcmp( ca_file, "-" ) != 0 )
1187 {
1188 if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
1189 goto error;
1190
1191 mbedtls_x509_crt_init( new->ca );
1192
1193 if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
1194 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001195 }
1196
Ron Eldor80d04192019-04-04 15:02:01 +03001197#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001198 if( strcmp( crl_file, "-" ) != 0 )
1199 {
1200 if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
1201 goto error;
1202
1203 mbedtls_x509_crl_init( new->crl );
1204
1205 if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
1206 goto error;
1207 }
Ron Eldor80d04192019-04-04 15:02:01 +03001208#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001209
1210 if( strcmp( auth_str, "-" ) != 0 )
1211 {
1212 if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
1213 goto error;
1214 }
1215 else
1216 new->authmode = DFL_AUTH_MODE;
1217
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001218 new->next = cur;
1219 cur = new;
1220 }
1221
1222 return( cur );
1223
1224error:
1225 sni_free( new );
1226 sni_free( cur );
1227 return( NULL );
1228}
1229
1230/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001231 * SNI callback.
1232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001234 const unsigned char *name, size_t name_len )
1235{
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001236 const sni_entry *cur = (const sni_entry *) p_info;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001237
1238 while( cur != NULL )
1239 {
1240 if( name_len == strlen( cur->name ) &&
1241 memcmp( name, cur->name, name_len ) == 0 )
1242 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001243 if( cur->ca != NULL )
1244 mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
1245
1246 if( cur->authmode != DFL_AUTH_MODE )
1247 mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
1248
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001249 return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001250 }
1251
1252 cur = cur->next;
1253 }
1254
1255 return( -1 );
1256}
1257
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001258#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001259
Gilles Peskineeccd8882020-03-10 12:19:08 +01001260#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker554b6ea2019-04-30 14:18:06 +01001261
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001262typedef struct _psk_entry psk_entry;
1263
1264struct _psk_entry
1265{
1266 const char *name;
1267 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 unsigned char key[MBEDTLS_PSK_MAX_LEN];
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001269#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Cronadc2ff22020-09-16 16:49:27 +02001270 psa_key_id_t slot;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001271#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001272 psk_entry *next;
1273};
1274
1275/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001276 * Free a list of psk_entry's
1277 */
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001278int psk_free( psk_entry *head )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001279{
1280 psk_entry *next;
1281
1282 while( head != NULL )
1283 {
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001284#if defined(MBEDTLS_USE_PSA_CRYPTO)
1285 psa_status_t status;
Ronald Cronadc2ff22020-09-16 16:49:27 +02001286 psa_key_id_t const slot = head->slot;
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001287
Ronald Cronadc2ff22020-09-16 16:49:27 +02001288 if( slot != 0 )
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001289 {
1290 status = psa_destroy_key( slot );
1291 if( status != PSA_SUCCESS )
1292 return( status );
1293 }
1294#endif /* MBEDTLS_USE_PSA_CRYPTO */
1295
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001296 next = head->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 mbedtls_free( head );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001298 head = next;
1299 }
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001300
1301 return( 0 );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001302}
1303
1304/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001305 * Parse a string of pairs name1,key1[,name2,key2[,...]]
1306 * into a usable psk_entry list.
1307 *
1308 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001309 */
1310psk_entry *psk_parse( char *psk_string )
1311{
1312 psk_entry *cur = NULL, *new = NULL;
1313 char *p = psk_string;
1314 char *end = p;
1315 char *key_hex;
1316
1317 while( *end != '\0' )
1318 ++end;
1319 *end = ',';
1320
1321 while( p <= end )
1322 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001323 if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +00001324 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001325
1326 memset( new, 0, sizeof( psk_entry ) );
1327
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +02001328 GET_ITEM( new->name );
1329 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001330
Ronald Cron7d866162020-06-18 10:36:26 +02001331 if( mbedtls_test_unhexify( new->key, MBEDTLS_PSK_MAX_LEN,
1332 key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001333 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001334
1335 new->next = cur;
1336 cur = new;
1337 }
1338
1339 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001340
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001341error:
1342 psk_free( new );
1343 psk_free( cur );
1344 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001345}
1346
1347/*
1348 * PSK callback
1349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001351 const unsigned char *name, size_t name_len )
1352{
1353 psk_entry *cur = (psk_entry *) p_info;
1354
1355 while( cur != NULL )
1356 {
1357 if( name_len == strlen( cur->name ) &&
1358 memcmp( name, cur->name, name_len ) == 0 )
1359 {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001360#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Cronadc2ff22020-09-16 16:49:27 +02001361 if( cur->slot != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001362 return( mbedtls_ssl_set_hs_psk_opaque( ssl, cur->slot ) );
1363 else
1364#endif
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001365 return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001366 }
1367
1368 cur = cur->next;
1369 }
1370
1371 return( -1 );
1372}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001373#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001374
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001375static mbedtls_net_context listen_fd, client_fd;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +02001376
1377/* Interruption handler to ensure clean exit (for valgrind testing) */
1378#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001379static int received_sigterm = 0;
1380void term_handler( int sig )
1381{
1382 ((void) sig);
1383 received_sigterm = 1;
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001384 mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
1385 mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001386}
Paul Bakkerc1283d32014-08-18 11:05:51 +02001387#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001388
Gilles Peskine682df092017-05-11 19:01:11 +02001389#if defined(MBEDTLS_X509_CRT_PARSE_C)
1390static int ssl_sig_hashes_for_test[] = {
1391#if defined(MBEDTLS_SHA512_C)
1392 MBEDTLS_MD_SHA512,
1393 MBEDTLS_MD_SHA384,
1394#endif
1395#if defined(MBEDTLS_SHA256_C)
1396 MBEDTLS_MD_SHA256,
1397 MBEDTLS_MD_SHA224,
1398#endif
1399#if defined(MBEDTLS_SHA1_C)
1400 /* Allow SHA-1 as we use it extensively in tests. */
1401 MBEDTLS_MD_SHA1,
1402#endif
1403 MBEDTLS_MD_NONE
1404};
1405#endif /* MBEDTLS_X509_CRT_PARSE_C */
1406
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02001407/** Return true if \p ret is a status code indicating that there is an
1408 * operation in progress on an SSL connection, and false if it indicates
1409 * success or a fatal error.
1410 *
1411 * The possible operations in progress are:
1412 *
1413 * - A read, when the SSL input buffer does not contain a full message.
1414 * - A write, when the SSL output buffer contains some data that has not
1415 * been sent over the network yet.
1416 * - An asynchronous callback that has not completed yet. */
1417static int mbedtls_status_is_ssl_in_progress( int ret )
1418{
1419 return( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1420 ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
1421 ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1422}
1423
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001424#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001425typedef struct
1426{
Gilles Peskine44817442018-06-13 18:09:28 +02001427 mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
1428 mbedtls_pk_context *pk; /*!< Private key */
1429 unsigned delay; /*!< Number of resume steps to go through */
1430 unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001431} ssl_async_key_slot_t;
1432
1433typedef enum {
Gilles Peskinead28bf02018-04-26 00:19:16 +02001434 SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
1435 SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
1436 SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start */
1437 SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
Gilles Peskinec9125722018-04-26 07:15:40 +02001438#define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001439} ssl_async_inject_error_t;
1440
1441typedef struct
1442{
Gilles Peskinee2479892018-06-13 18:06:51 +02001443 ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001444 size_t slots_used;
1445 ssl_async_inject_error_t inject_error;
1446 int (*f_rng)(void *, unsigned char *, size_t);
1447 void *p_rng;
1448} ssl_async_key_context_t;
1449
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001450int ssl_async_set_key( ssl_async_key_context_t *ctx,
Gilles Peskine44817442018-06-13 18:09:28 +02001451 mbedtls_x509_crt *cert,
1452 mbedtls_pk_context *pk,
1453 int pk_take_ownership,
1454 unsigned delay )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001455{
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001456 if( ctx->slots_used >= sizeof( ctx->slots ) / sizeof( *ctx->slots ) )
1457 return( -1 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001458 ctx->slots[ctx->slots_used].cert = cert;
1459 ctx->slots[ctx->slots_used].pk = pk;
1460 ctx->slots[ctx->slots_used].delay = delay;
Gilles Peskine44817442018-06-13 18:09:28 +02001461 ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001462 ++ctx->slots_used;
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001463 return( 0 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001464}
1465
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001466#define SSL_ASYNC_INPUT_MAX_SIZE 512
Gilles Peskined3268832018-04-26 06:23:59 +02001467
1468typedef enum
1469{
1470 ASYNC_OP_SIGN,
1471 ASYNC_OP_DECRYPT,
1472} ssl_async_operation_type_t;
1473/* Note that the enum above and the array below need to be kept in sync!
1474 * `ssl_async_operation_names[op]` is the name of op for each value `op`
1475 * of type `ssl_async_operation_type_t`. */
1476static const char *const ssl_async_operation_names[] =
1477{
1478 "sign",
1479 "decrypt",
1480};
1481
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001482typedef struct
1483{
Gilles Peskine6331d782018-04-26 13:27:43 +02001484 unsigned slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001485 ssl_async_operation_type_t operation_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001486 mbedtls_md_type_t md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001487 unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
1488 size_t input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001489 unsigned remaining_delay;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001490} ssl_async_operation_context_t;
1491
Gilles Peskine8f97af72018-04-26 11:46:10 +02001492static int ssl_async_start( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001493 mbedtls_x509_crt *cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001494 ssl_async_operation_type_t op_type,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001495 mbedtls_md_type_t md_alg,
1496 const unsigned char *input,
1497 size_t input_len )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001498{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001499 ssl_async_key_context_t *config_data =
1500 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskine6331d782018-04-26 13:27:43 +02001501 unsigned slot;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001502 ssl_async_operation_context_t *ctx = NULL;
Gilles Peskined3268832018-04-26 06:23:59 +02001503 const char *op_name = ssl_async_operation_names[op_type];
Gilles Peskinef1127252018-04-24 13:05:39 +02001504
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001505 {
1506 char dn[100];
Gilles Peskined5d983e2018-06-15 14:05:10 +02001507 if( mbedtls_x509_dn_gets( dn, sizeof( dn ), &cert->subject ) > 0 )
1508 mbedtls_printf( "Async %s callback: looking for DN=%s\n",
1509 op_name, dn );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001510 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001511
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001512 /* Look for a private key that matches the public key in cert.
1513 * Since this test code has the private key inside Mbed TLS,
1514 * we call mbedtls_pk_check_pair to match a private key with the
1515 * public key. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001516 for( slot = 0; slot < config_data->slots_used; slot++ )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001517 {
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001518 if( mbedtls_pk_check_pair( &cert->pk,
1519 config_data->slots[slot].pk ) == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001520 break;
1521 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001522 if( slot == config_data->slots_used )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001523 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001524 mbedtls_printf( "Async %s callback: no key matches this certificate.\n",
1525 op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001526 return( MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH );
1527 }
Gilles Peskine6331d782018-04-26 13:27:43 +02001528 mbedtls_printf( "Async %s callback: using key slot %u, delay=%u.\n",
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001529 op_name, slot, config_data->slots[slot].delay );
Gilles Peskinef1127252018-04-24 13:05:39 +02001530
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001531 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001532 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001533 mbedtls_printf( "Async %s callback: injected error\n", op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001534 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1535 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001536
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001537 if( input_len > SSL_ASYNC_INPUT_MAX_SIZE )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001538 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Gilles Peskinef1127252018-04-24 13:05:39 +02001539
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001540 ctx = mbedtls_calloc( 1, sizeof( *ctx ) );
1541 if( ctx == NULL )
1542 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1543 ctx->slot = slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001544 ctx->operation_type = op_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001545 ctx->md_alg = md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001546 memcpy( ctx->input, input, input_len );
1547 ctx->input_len = input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001548 ctx->remaining_delay = config_data->slots[slot].delay;
Gilles Peskinea668c602018-04-30 11:54:39 +02001549 mbedtls_ssl_set_async_operation_data( ssl, ctx );
Gilles Peskinef1127252018-04-24 13:05:39 +02001550
Gilles Peskineceb541b2018-04-26 06:30:45 +02001551 if( ctx->remaining_delay == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001552 return( 0 );
1553 else
1554 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1555}
1556
Gilles Peskine8f97af72018-04-26 11:46:10 +02001557static int ssl_async_sign( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001558 mbedtls_x509_crt *cert,
1559 mbedtls_md_type_t md_alg,
1560 const unsigned char *hash,
1561 size_t hash_len )
1562{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001563 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001564 ASYNC_OP_SIGN, md_alg,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001565 hash, hash_len ) );
1566}
1567
Gilles Peskine8f97af72018-04-26 11:46:10 +02001568static int ssl_async_decrypt( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001569 mbedtls_x509_crt *cert,
1570 const unsigned char *input,
1571 size_t input_len )
1572{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001573 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001574 ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001575 input, input_len ) );
1576}
1577
Gilles Peskine8f97af72018-04-26 11:46:10 +02001578static int ssl_async_resume( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001579 unsigned char *output,
1580 size_t *output_len,
1581 size_t output_size )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001582{
Gilles Peskinea668c602018-04-30 11:54:39 +02001583 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine8f97af72018-04-26 11:46:10 +02001584 ssl_async_key_context_t *config_data =
1585 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001586 ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001587 int ret;
Gilles Peskinef5a99962018-04-30 16:37:23 +02001588 const char *op_name;
Gilles Peskinef1127252018-04-24 13:05:39 +02001589
Gilles Peskineceb541b2018-04-26 06:30:45 +02001590 if( ctx->remaining_delay > 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001591 {
Gilles Peskineceb541b2018-04-26 06:30:45 +02001592 --ctx->remaining_delay;
Gilles Peskine6331d782018-04-26 13:27:43 +02001593 mbedtls_printf( "Async resume (slot %u): call %u more times.\n",
Gilles Peskineceb541b2018-04-26 06:30:45 +02001594 ctx->slot, ctx->remaining_delay );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001595 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1596 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001597
Gilles Peskined3268832018-04-26 06:23:59 +02001598 switch( ctx->operation_type )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001599 {
Gilles Peskined3268832018-04-26 06:23:59 +02001600 case ASYNC_OP_DECRYPT:
Gilles Peskined3268832018-04-26 06:23:59 +02001601 ret = mbedtls_pk_decrypt( key_slot->pk,
1602 ctx->input, ctx->input_len,
1603 output, output_len, output_size,
1604 config_data->f_rng, config_data->p_rng );
1605 break;
1606 case ASYNC_OP_SIGN:
Gilles Peskined3268832018-04-26 06:23:59 +02001607 ret = mbedtls_pk_sign( key_slot->pk,
1608 ctx->md_alg,
1609 ctx->input, ctx->input_len,
1610 output, output_len,
1611 config_data->f_rng, config_data->p_rng );
1612 break;
1613 default:
Gilles Peskine6331d782018-04-26 13:27:43 +02001614 mbedtls_printf( "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
Gilles Peskined3268832018-04-26 06:23:59 +02001615 ctx->slot, (long) ctx->operation_type );
Gilles Peskine44817442018-06-13 18:09:28 +02001616 mbedtls_free( ctx );
Gilles Peskined3268832018-04-26 06:23:59 +02001617 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1618 break;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001619 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001620
Gilles Peskinef5a99962018-04-30 16:37:23 +02001621 op_name = ssl_async_operation_names[ctx->operation_type];
1622
Gilles Peskinec9125722018-04-26 07:15:40 +02001623 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001624 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001625 mbedtls_printf( "Async resume callback: %s done but injected error\n",
1626 op_name );
Gilles Peskine2636fad2018-06-12 14:17:39 +02001627 mbedtls_free( ctx );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001628 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1629 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001630
Gilles Peskine6331d782018-04-26 13:27:43 +02001631 mbedtls_printf( "Async resume (slot %u): %s done, status=%d.\n",
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001632 ctx->slot, op_name, ret );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001633 mbedtls_free( ctx );
1634 return( ret );
1635}
1636
Gilles Peskine8f97af72018-04-26 11:46:10 +02001637static void ssl_async_cancel( mbedtls_ssl_context *ssl )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001638{
Gilles Peskinea668c602018-04-30 11:54:39 +02001639 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001640 mbedtls_printf( "Async cancel callback.\n" );
1641 mbedtls_free( ctx );
1642}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001643#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001644
Hanno Becker16970d22017-10-10 15:56:37 +01001645/*
1646 * Wait for an event from the underlying transport or the timer
1647 * (Used in event-driven IO mode).
1648 */
1649#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001650int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001651 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001652#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001653int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001654 mbedtls_timing_delay_context *timer,
1655 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001656#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001657{
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001658 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +01001659 int poll_type = 0;
1660
1661 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
1662 poll_type = MBEDTLS_NET_POLL_WRITE;
1663 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
1664 poll_type = MBEDTLS_NET_POLL_READ;
1665#if !defined(MBEDTLS_TIMING_C)
1666 else
Hanno Beckeref527962018-03-15 15:49:24 +00001667 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001668#endif
1669
1670 while( 1 )
1671 {
Hanno Becker197a91c2017-10-31 10:58:53 +00001672 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +01001673#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001674 if( timer != NULL &&
1675 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +01001676 {
Hanno Becker16970d22017-10-10 15:56:37 +01001677 break;
1678 }
Hanno Becker197a91c2017-10-31 10:58:53 +00001679#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +01001680
Hanno Becker197a91c2017-10-31 10:58:53 +00001681 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001682 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +01001683 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001684 ret = mbedtls_net_poll( fd, poll_type, 0 );
1685 if( ret < 0 )
1686 return( ret );
1687 if( ret == poll_type )
1688 break;
Hanno Becker16970d22017-10-10 15:56:37 +01001689 }
1690 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001691
1692 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001693}
1694
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001695#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Cronadc2ff22020-09-16 16:49:27 +02001696static psa_status_t psa_setup_psk_key_slot( psa_key_id_t *slot,
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001697 psa_algorithm_t alg,
1698 unsigned char *psk,
1699 size_t psk_len )
1700{
1701 psa_status_t status;
Janos Follathbe4efc22019-08-08 11:38:18 +01001702 psa_key_attributes_t key_attributes;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001703
Janos Follathbe4efc22019-08-08 11:38:18 +01001704 key_attributes = psa_key_attributes_init();
1705 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1706 psa_set_key_algorithm( &key_attributes, alg );
1707 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001708
Janos Follathbe4efc22019-08-08 11:38:18 +01001709 status = psa_import_key( &key_attributes, psk, psk_len, slot );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001710 if( status != PSA_SUCCESS )
Hanno Becker1d911cd2018-11-15 13:06:09 +00001711 {
1712 fprintf( stderr, "IMPORT\n" );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001713 return( status );
Hanno Becker1d911cd2018-11-15 13:06:09 +00001714 }
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001715
1716 return( PSA_SUCCESS );
1717}
1718#endif /* MBEDTLS_USE_PSA_CRYPTO */
1719
Hanno Beckera0e20d02019-05-15 14:03:01 +01001720#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001721int report_cid_usage( mbedtls_ssl_context *ssl,
1722 const char *additional_description )
1723{
1724 int ret;
1725 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
1726 size_t peer_cid_len;
1727 int cid_negotiated;
1728
1729 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1730 return( 0 );
1731
1732 /* Check if the use of a CID has been negotiated */
1733 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
1734 peer_cid, &peer_cid_len );
1735 if( ret != 0 )
1736 {
1737 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +02001738 (unsigned int) -ret );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001739 return( ret );
1740 }
1741
1742 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
1743 {
1744 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
1745 {
1746 mbedtls_printf( "(%s) Use of Connection ID was not offered by client.\n",
1747 additional_description );
1748 }
1749 }
1750 else
1751 {
1752 size_t idx=0;
1753 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
1754 additional_description );
1755 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
1756 additional_description,
1757 (unsigned) peer_cid_len );
1758 while( idx < peer_cid_len )
1759 {
1760 mbedtls_printf( "%02x ", peer_cid[ idx ] );
1761 idx++;
1762 }
1763 mbedtls_printf( "\n" );
1764 }
1765
1766 return( 0 );
1767}
Hanno Beckera0e20d02019-05-15 14:03:01 +01001768#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001769
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001770int main( int argc, char *argv[] )
1771{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001772 int ret = 0, len, written, frags, exchanges_left;
gabor-mezei-arma9eecf12020-07-07 11:11:02 +02001773 int query_config_ret = 0;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001774 int version_suites[4][2];
Hanno Beckerdcc94e62019-07-03 17:05:43 +01001775 io_ctx_t io_ctx;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001776 unsigned char* buf = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01001777#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001778#if defined(MBEDTLS_USE_PSA_CRYPTO)
1779 psa_algorithm_t alg = 0;
Ronald Cronadc2ff22020-09-16 16:49:27 +02001780 psa_key_id_t psk_slot = 0;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001781#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +02001783 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +02001784 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +02001785#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001786 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001787 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02001788 size_t cliip_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789#if defined(MBEDTLS_SSL_COOKIE_C)
1790 mbedtls_ssl_cookie_ctx cookie_ctx;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001791#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001792
Gilles Peskineef86ab22017-05-05 18:59:02 +02001793#if defined(MBEDTLS_X509_CRT_PARSE_C)
1794 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
1795#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 mbedtls_entropy_context entropy;
1797 mbedtls_ctr_drbg_context ctr_drbg;
1798 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001799 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001800#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001801 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001802#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001804 unsigned char renego_period[8] = { 0 };
1805#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +02001807 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 mbedtls_x509_crt cacert;
1809 mbedtls_x509_crt srvcert;
1810 mbedtls_pk_context pkey;
1811 mbedtls_x509_crt srvcert2;
1812 mbedtls_pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001813 int key_cert_init = 0, key_cert_init2 = 0;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001814#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001815 ssl_async_key_context_t ssl_async_keys;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001816#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001817#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1819 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001820#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821#if defined(MBEDTLS_SSL_CACHE_C)
1822 mbedtls_ssl_cache_context cache;
Paul Bakker0a597072012-09-25 21:55:46 +00001823#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001824#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1825 mbedtls_ssl_ticket_context ticket_ctx;
1826#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001827#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001828 sni_entry *sni_info = NULL;
1829#endif
Hanno Beckere6706e62017-05-15 16:05:15 +01001830#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +01001831 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +01001832 const mbedtls_ecp_curve_info * curve_cur;
1833#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +01001835 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001836#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Simon Butchercce68be2018-07-23 14:26:09 +01001838 unsigned char alloc_buf[MEMORY_HEAP_SIZE];
Paul Bakker82024bf2013-07-04 11:52:32 +02001839#endif
Hanno Beckera0e20d02019-05-15 14:03:01 +01001840#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +01001841 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001842 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckera7d25422019-04-09 17:28:10 +01001843 size_t cid_len = 0;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001844 size_t cid_renego_len = 0;
Hanno Beckera7d25422019-04-09 17:28:10 +01001845#endif
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02001846#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1847 unsigned char *context_buf = NULL;
Gilles Peskine4e8b5942019-09-20 19:12:31 +02001848 size_t context_buf_len = 0;
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02001849#endif
Hanno Beckera7d25422019-04-09 17:28:10 +01001850
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001851 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001852 char *p, *q;
1853 const int *list;
Hanno Becker5a9942e2018-11-12 17:47:48 +00001854#if defined(MBEDTLS_USE_PSA_CRYPTO)
1855 psa_status_t status;
1856#endif
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001857#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1858 unsigned char eap_tls_keymaterial[16];
1859 unsigned char eap_tls_iv[8];
1860 const char* eap_tls_label = "client EAP encryption";
1861 eap_tls_keys eap_tls_keying;
Ron Eldor65b56ef2019-09-26 16:40:48 +03001862#if defined( MBEDTLS_SSL_DTLS_SRTP )
1863 /*! master keys and master salt for SRTP generated during handshake */
Johan Pascal85269572020-08-25 10:01:54 +02001864 unsigned char dtls_srtp_key_material[MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH];
Ron Eldor65b56ef2019-09-26 16:40:48 +03001865 const char* dtls_srtp_label = "EXTRACTOR-dtls_srtp";
1866 dtls_srtp_keys dtls_srtp_keying;
Johan Pascal9bc97ca2020-09-21 23:44:45 +02001867 const mbedtls_ssl_srtp_profile default_profiles[] = {
Johan Pascal43f94902020-09-22 12:25:52 +02001868 MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80,
1869 MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_32,
1870 MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_80,
Johan Pascal253d0262020-09-22 13:04:45 +02001871 MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_32,
1872 MBEDTLS_TLS_SRTP_UNSET
Johan Pascal9bc97ca2020-09-21 23:44:45 +02001873 };
Ron Eldor65b56ef2019-09-26 16:40:48 +03001874#endif /* MBEDTLS_SSL_DTLS_SRTP */
1875#endif /* MBEDTLS_SSL_EXPORT_KEYS */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1878 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Piotr Nowicki0937ed22019-11-26 16:32:40 +01001879#if defined(MBEDTLS_MEMORY_DEBUG)
1880 size_t current_heap_memory, peak_heap_memory, heap_blocks;
1881#endif /* MBEDTLS_MEMORY_DEBUG */
1882#endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
Paul Bakker82024bf2013-07-04 11:52:32 +02001883
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001884 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +02001885 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001886 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001887 mbedtls_net_init( &client_fd );
1888 mbedtls_net_init( &listen_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001889 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001890 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001891 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892#if defined(MBEDTLS_X509_CRT_PARSE_C)
1893 mbedtls_x509_crt_init( &cacert );
1894 mbedtls_x509_crt_init( &srvcert );
1895 mbedtls_pk_init( &pkey );
1896 mbedtls_x509_crt_init( &srvcert2 );
1897 mbedtls_pk_init( &pkey2 );
Gilles Peskine4d9ec4d2018-04-26 14:33:43 +02001898#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
1899 memset( &ssl_async_keys, 0, sizeof( ssl_async_keys ) );
1900#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001901#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1903 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001904#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905#if defined(MBEDTLS_SSL_CACHE_C)
1906 mbedtls_ssl_cache_init( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00001907#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001908#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1909 mbedtls_ssl_ticket_init( &ticket_ctx );
1910#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +02001912 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001913#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914#if defined(MBEDTLS_SSL_COOKIE_C)
1915 mbedtls_ssl_cookie_init( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001916#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001917
Hanno Becker5a9942e2018-11-12 17:47:48 +00001918#if defined(MBEDTLS_USE_PSA_CRYPTO)
1919 status = psa_crypto_init();
1920 if( status != PSA_SUCCESS )
1921 {
1922 mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
1923 (int) status );
1924 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1925 goto exit;
1926 }
1927#endif
1928
Paul Bakkerc1283d32014-08-18 11:05:51 +02001929#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001930 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001931 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001932 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +02001933#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001934
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001935 if( argc == 0 )
1936 {
1937 usage:
1938 if( ret == 0 )
1939 ret = 1;
1940
Gilles Peskineb2971ff2020-04-14 19:41:01 +02001941 mbedtls_printf( USAGE1 );
1942 mbedtls_printf( USAGE2 );
1943 mbedtls_printf( USAGE3 );
1944 mbedtls_printf( USAGE4 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 list = mbedtls_ssl_list_ciphersuites();
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001947 while( *list )
1948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001950 list++;
1951 if( !*list )
1952 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001954 list++;
1955 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 mbedtls_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001957 goto exit;
1958 }
1959
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001960 opt.buffer_size = DFL_IO_BUF_LEN;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001961 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001962 opt.server_port = DFL_SERVER_PORT;
1963 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker16970d22017-10-10 15:56:37 +01001964 opt.event = DFL_EVENT;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001965 opt.response_size = DFL_RESPONSE_SIZE;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001966 opt.nbio = DFL_NBIO;
Hanno Beckera7d25422019-04-09 17:28:10 +01001967 opt.cid_enabled = DFL_CID_ENABLED;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001968 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
Hanno Beckera7d25422019-04-09 17:28:10 +01001969 opt.cid_val = DFL_CID_VALUE;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001970 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001971 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001972 opt.ca_file = DFL_CA_FILE;
1973 opt.ca_path = DFL_CA_PATH;
1974 opt.crt_file = DFL_CRT_FILE;
1975 opt.key_file = DFL_KEY_FILE;
Hanno Beckere58a6302018-11-07 16:20:16 +00001976 opt.key_pwd = DFL_KEY_PWD;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001977 opt.crt_file2 = DFL_CRT_FILE2;
1978 opt.key_file2 = DFL_KEY_FILE2;
Hanno Beckere58a6302018-11-07 16:20:16 +00001979 opt.key_pwd2 = DFL_KEY_PWD2;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001980 opt.async_operations = DFL_ASYNC_OPERATIONS;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001981 opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
1982 opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
1983 opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
Paul Bakkerfbb17802013-04-17 19:10:21 +02001984 opt.psk = DFL_PSK;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001985#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001986 opt.psk_opaque = DFL_PSK_OPAQUE;
1987 opt.psk_list_opaque = DFL_PSK_LIST_OPAQUE;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001988#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001989#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1990 opt.ca_callback = DFL_CA_CALLBACK;
1991#endif
Paul Bakkerfbb17802013-04-17 19:10:21 +02001992 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001993 opt.psk_list = DFL_PSK_LIST;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001994 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001995 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001996 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001997 opt.renegotiation = DFL_RENEGOTIATION;
1998 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001999 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02002000 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002001 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02002002 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002003 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002004 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01002005 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02002006 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01002007 opt.auth_mode = DFL_AUTH_MODE;
Janos Follath4817e272017-04-10 13:44:33 +01002008 opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002009 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002010 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002011 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01002012 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002013 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002014 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002015 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002016 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01002017 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002018 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01002019 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002020 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002021 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002022 opt.hs_to_min = DFL_HS_TO_MIN;
2023 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002024 opt.dtls_mtu = DFL_DTLS_MTU;
Hanno Beckere7675d02018-08-14 13:28:56 +01002025 opt.dgram_packing = DFL_DGRAM_PACKING;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002026 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002027 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002028 opt.etm = DFL_ETM;
Jarno Lamsa9831c8a2019-05-29 13:33:32 +03002029 opt.serialize = DFL_SERIALIZE;
Piotr Nowicki3de298f2020-04-16 14:35:19 +02002030 opt.context_file = DFL_CONTEXT_FILE;
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002031 opt.eap_tls = DFL_EAP_TLS;
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002032 opt.reproducible = DFL_REPRODUCIBLE;
Hanno Becker48f3a3d2019-08-29 06:31:22 +01002033 opt.nss_keylog = DFL_NSS_KEYLOG;
2034 opt.nss_keylog_file = DFL_NSS_KEYLOG_FILE;
gabor-mezei-arma9eecf12020-07-07 11:11:02 +02002035 opt.query_config_mode = DFL_QUERY_CONFIG_MODE;
Ron Eldor6ea64512018-07-02 10:08:07 +03002036 opt.use_srtp = DFL_USE_SRTP;
2037 opt.force_srtp_profile = DFL_SRTP_FORCE_PROFILE;
Ron Eldor12c6ead2018-07-03 15:08:32 +03002038 opt.support_mki = DFL_SRTP_SUPPORT_MKI;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002039
2040 for( i = 1; i < argc; i++ )
2041 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002042 p = argv[i];
2043 if( ( q = strchr( p, '=' ) ) == NULL )
2044 goto usage;
2045 *q++ = '\0';
2046
2047 if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02002048 opt.server_port = q;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01002049 else if( strcmp( p, "server_addr" ) == 0 )
2050 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01002051 else if( strcmp( p, "dtls" ) == 0 )
2052 {
2053 int t = atoi( q );
2054 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01002056 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01002058 else
2059 goto usage;
2060 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002061 else if( strcmp( p, "debug_level" ) == 0 )
2062 {
2063 opt.debug_level = atoi( q );
2064 if( opt.debug_level < 0 || opt.debug_level > 65535 )
2065 goto usage;
2066 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002067 else if( strcmp( p, "nbio" ) == 0 )
2068 {
2069 opt.nbio = atoi( q );
2070 if( opt.nbio < 0 || opt.nbio > 2 )
2071 goto usage;
2072 }
Hanno Becker16970d22017-10-10 15:56:37 +01002073 else if( strcmp( p, "event" ) == 0 )
2074 {
2075 opt.event = atoi( q );
2076 if( opt.event < 0 || opt.event > 2 )
2077 goto usage;
2078 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002079 else if( strcmp( p, "read_timeout" ) == 0 )
2080 opt.read_timeout = atoi( q );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002081 else if( strcmp( p, "buffer_size" ) == 0 )
2082 {
2083 opt.buffer_size = atoi( q );
2084 if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
2085 goto usage;
2086 }
2087 else if( strcmp( p, "response_size" ) == 0 )
2088 {
2089 opt.response_size = atoi( q );
2090 if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
2091 goto usage;
2092 if( opt.buffer_size < opt.response_size )
2093 opt.buffer_size = opt.response_size;
2094 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002095 else if( strcmp( p, "ca_file" ) == 0 )
2096 opt.ca_file = q;
2097 else if( strcmp( p, "ca_path" ) == 0 )
2098 opt.ca_path = q;
2099 else if( strcmp( p, "crt_file" ) == 0 )
2100 opt.crt_file = q;
2101 else if( strcmp( p, "key_file" ) == 0 )
2102 opt.key_file = q;
Hanno Beckere58a6302018-11-07 16:20:16 +00002103 else if( strcmp( p, "key_pwd" ) == 0 )
2104 opt.key_pwd = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002105 else if( strcmp( p, "crt_file2" ) == 0 )
2106 opt.crt_file2 = q;
2107 else if( strcmp( p, "key_file2" ) == 0 )
2108 opt.key_file2 = q;
Hanno Beckere58a6302018-11-07 16:20:16 +00002109 else if( strcmp( p, "key_pwd2" ) == 0 )
2110 opt.key_pwd2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002111 else if( strcmp( p, "dhm_file" ) == 0 )
2112 opt.dhm_file = q;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002113#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002114 else if( strcmp( p, "async_operations" ) == 0 )
2115 opt.async_operations = q;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002116 else if( strcmp( p, "async_private_delay1" ) == 0 )
2117 opt.async_private_delay1 = atoi( q );
2118 else if( strcmp( p, "async_private_delay2" ) == 0 )
2119 opt.async_private_delay2 = atoi( q );
2120 else if( strcmp( p, "async_private_error" ) == 0 )
2121 {
2122 int n = atoi( q );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002123 if( n < -SSL_ASYNC_INJECT_ERROR_MAX ||
2124 n > SSL_ASYNC_INJECT_ERROR_MAX )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002125 {
2126 ret = 2;
2127 goto usage;
2128 }
2129 opt.async_private_error = n;
2130 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002131#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Hanno Beckera0e20d02019-05-15 14:03:01 +01002132#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +01002133 else if( strcmp( p, "cid" ) == 0 )
2134 {
2135 opt.cid_enabled = atoi( q );
2136 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
2137 goto usage;
2138 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002139 else if( strcmp( p, "cid_renego" ) == 0 )
2140 {
2141 opt.cid_enabled_renego = atoi( q );
2142 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
2143 goto usage;
2144 }
Hanno Beckera7d25422019-04-09 17:28:10 +01002145 else if( strcmp( p, "cid_val" ) == 0 )
2146 {
2147 opt.cid_val = q;
2148 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002149 else if( strcmp( p, "cid_val_renego" ) == 0 )
2150 {
2151 opt.cid_val_renego = q;
2152 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002153#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002154 else if( strcmp( p, "psk" ) == 0 )
2155 opt.psk = q;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002156#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002157 else if( strcmp( p, "psk_opaque" ) == 0 )
2158 opt.psk_opaque = atoi( q );
2159 else if( strcmp( p, "psk_list_opaque" ) == 0 )
2160 opt.psk_list_opaque = atoi( q );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002161#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02002162#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2163 else if( strcmp( p, "ca_callback" ) == 0)
2164 opt.ca_callback = atoi( q );
2165#endif
Paul Bakkerfbb17802013-04-17 19:10:21 +02002166 else if( strcmp( p, "psk_identity" ) == 0 )
2167 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002168 else if( strcmp( p, "psk_list" ) == 0 )
2169 opt.psk_list = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002170 else if( strcmp( p, "ecjpake_pw" ) == 0 )
2171 opt.ecjpake_pw = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002172 else if( strcmp( p, "force_ciphersuite" ) == 0 )
2173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002175
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02002176 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002177 {
2178 ret = 2;
2179 goto usage;
2180 }
2181 opt.force_ciphersuite[1] = 0;
2182 }
Hanno Beckere6706e62017-05-15 16:05:15 +01002183 else if( strcmp( p, "curves" ) == 0 )
2184 opt.curves = q;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002185 else if( strcmp( p, "version_suites" ) == 0 )
2186 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002187 else if( strcmp( p, "renegotiation" ) == 0 )
2188 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002189 opt.renegotiation = (atoi( q )) ?
2190 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
2191 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002192 }
2193 else if( strcmp( p, "allow_legacy" ) == 0 )
2194 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002195 switch( atoi( q ) )
2196 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002197 case -1:
2198 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
2199 break;
2200 case 0:
2201 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
2202 break;
2203 case 1:
2204 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
2205 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002206 default: goto usage;
2207 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002208 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002209 else if( strcmp( p, "renegotiate" ) == 0 )
2210 {
2211 opt.renegotiate = atoi( q );
2212 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
2213 goto usage;
2214 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02002215 else if( strcmp( p, "renego_delay" ) == 0 )
2216 {
2217 opt.renego_delay = atoi( q );
2218 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002219 else if( strcmp( p, "renego_period" ) == 0 )
2220 {
Andres AG0b736db2017-02-08 14:05:57 +00002221#if defined(_MSC_VER)
2222 opt.renego_period = _strtoui64( q, NULL, 10 );
2223#else
2224 if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
2225 goto usage;
2226#endif /* _MSC_VER */
2227 if( opt.renego_period < 2 )
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002228 goto usage;
2229 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02002230 else if( strcmp( p, "exchanges" ) == 0 )
2231 {
2232 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02002233 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02002234 goto usage;
2235 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00002236 else if( strcmp( p, "min_version" ) == 0 )
2237 {
2238 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002240 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01002242 else if( strcmp( q, "tls1_1" ) == 0 ||
2243 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01002245 else if( strcmp( q, "tls1_2" ) == 0 ||
2246 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002248 else
2249 goto usage;
2250 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002251 else if( strcmp( p, "max_version" ) == 0 )
2252 {
2253 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002255 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01002257 else if( strcmp( q, "tls1_1" ) == 0 ||
2258 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01002260 else if( strcmp( q, "tls1_2" ) == 0 ||
2261 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002263 else
2264 goto usage;
2265 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01002266 else if( strcmp( p, "arc4" ) == 0 )
2267 {
2268 switch( atoi( q ) )
2269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
2271 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01002272 default: goto usage;
2273 }
2274 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02002275 else if( strcmp( p, "allow_sha1" ) == 0 )
2276 {
2277 switch( atoi( q ) )
2278 {
2279 case 0: opt.allow_sha1 = 0; break;
2280 case 1: opt.allow_sha1 = 1; break;
2281 default: goto usage;
2282 }
2283 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002284 else if( strcmp( p, "force_version" ) == 0 )
2285 {
2286 if( strcmp( q, "ssl3" ) == 0 )
2287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
2289 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002290 }
2291 else if( strcmp( q, "tls1" ) == 0 )
2292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
2294 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002295 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002296 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02002297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
2299 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002300 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002301 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02002302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
2304 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002305 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002306 else if( strcmp( q, "dtls1" ) == 0 )
2307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
2309 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
2310 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002311 }
2312 else if( strcmp( q, "dtls1_2" ) == 0 )
2313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
2315 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
2316 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002317 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002318 else
2319 goto usage;
2320 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01002321 else if( strcmp( p, "auth_mode" ) == 0 )
2322 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02002323 if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
Paul Bakker91ebfb52012-11-23 14:04:08 +01002324 goto usage;
2325 }
Janos Follath4817e272017-04-10 13:44:33 +01002326 else if( strcmp( p, "cert_req_ca_list" ) == 0 )
2327 {
2328 opt.cert_req_ca_list = atoi( q );
2329 if( opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1 )
2330 goto usage;
2331 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002332 else if( strcmp( p, "max_frag_len" ) == 0 )
2333 {
2334 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002336 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002338 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002340 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002342 else
2343 goto usage;
2344 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002345 else if( strcmp( p, "alpn" ) == 0 )
2346 {
2347 opt.alpn_string = q;
2348 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002349 else if( strcmp( p, "trunc_hmac" ) == 0 )
2350 {
2351 switch( atoi( q ) )
2352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
2354 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002355 default: goto usage;
2356 }
2357 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002358 else if( strcmp( p, "extended_ms" ) == 0 )
2359 {
2360 switch( atoi( q ) )
2361 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002362 case 0:
2363 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
2364 break;
2365 case 1:
2366 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
2367 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002368 default: goto usage;
2369 }
2370 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002371 else if( strcmp( p, "etm" ) == 0 )
2372 {
2373 switch( atoi( q ) )
2374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
2376 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002377 default: goto usage;
2378 }
2379 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002380 else if( strcmp( p, "tickets" ) == 0 )
2381 {
2382 opt.tickets = atoi( q );
2383 if( opt.tickets < 0 || opt.tickets > 1 )
2384 goto usage;
2385 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01002386 else if( strcmp( p, "ticket_timeout" ) == 0 )
2387 {
2388 opt.ticket_timeout = atoi( q );
2389 if( opt.ticket_timeout < 0 )
2390 goto usage;
2391 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002392 else if( strcmp( p, "cache_max" ) == 0 )
2393 {
2394 opt.cache_max = atoi( q );
2395 if( opt.cache_max < 0 )
2396 goto usage;
2397 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002398 else if( strcmp( p, "cache_timeout" ) == 0 )
2399 {
2400 opt.cache_timeout = atoi( q );
2401 if( opt.cache_timeout < 0 )
2402 goto usage;
2403 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002404 else if( strcmp( p, "cookies" ) == 0 )
2405 {
2406 opt.cookies = atoi( q );
2407 if( opt.cookies < -1 || opt.cookies > 1)
2408 goto usage;
2409 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002410 else if( strcmp( p, "anti_replay" ) == 0 )
2411 {
2412 opt.anti_replay = atoi( q );
2413 if( opt.anti_replay < 0 || opt.anti_replay > 1)
2414 goto usage;
2415 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002416 else if( strcmp( p, "badmac_limit" ) == 0 )
2417 {
2418 opt.badmac_limit = atoi( q );
2419 if( opt.badmac_limit < 0 )
2420 goto usage;
2421 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002422 else if( strcmp( p, "hs_timeout" ) == 0 )
2423 {
2424 if( ( p = strchr( q, '-' ) ) == NULL )
2425 goto usage;
2426 *p++ = '\0';
2427 opt.hs_to_min = atoi( q );
2428 opt.hs_to_max = atoi( p );
2429 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
2430 goto usage;
2431 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002432 else if( strcmp( p, "mtu" ) == 0 )
2433 {
2434 opt.dtls_mtu = atoi( q );
2435 if( opt.dtls_mtu < 0 )
2436 goto usage;
2437 }
Hanno Beckere7675d02018-08-14 13:28:56 +01002438 else if( strcmp( p, "dgram_packing" ) == 0 )
2439 {
2440 opt.dgram_packing = atoi( q );
2441 if( opt.dgram_packing != 0 &&
2442 opt.dgram_packing != 1 )
2443 {
2444 goto usage;
2445 }
2446 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002447 else if( strcmp( p, "sni" ) == 0 )
2448 {
2449 opt.sni = q;
2450 }
Andres Amaya Garciabc818842018-10-16 21:08:38 +01002451 else if( strcmp( p, "query_config" ) == 0 )
2452 {
gabor-mezei-arma9eecf12020-07-07 11:11:02 +02002453 opt.query_config_mode = 1;
2454 query_config_ret = query_config( q );
gabor-mezei-arm78595852020-04-28 10:40:30 +02002455 goto exit;
Andres Amaya Garciabc818842018-10-16 21:08:38 +01002456 }
Jarno Lamsa9831c8a2019-05-29 13:33:32 +03002457 else if( strcmp( p, "serialize") == 0 )
2458 {
2459 opt.serialize = atoi( q );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03002460 if( opt.serialize < 0 || opt.serialize > 2)
Jarno Lamsa9831c8a2019-05-29 13:33:32 +03002461 goto usage;
2462 }
Piotr Nowicki3de298f2020-04-16 14:35:19 +02002463 else if( strcmp( p, "context_file") == 0 )
2464 {
2465 opt.context_file = q;
2466 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002467 else if( strcmp( p, "eap_tls" ) == 0 )
2468 {
2469 opt.eap_tls = atoi( q );
2470 if( opt.eap_tls < 0 || opt.eap_tls > 1 )
2471 goto usage;
2472 }
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002473 else if( strcmp( p, "reproducible" ) == 0 )
2474 {
2475 opt.reproducible = 1;
2476 }
Hanno Becker48f3a3d2019-08-29 06:31:22 +01002477 else if( strcmp( p, "nss_keylog" ) == 0 )
2478 {
2479 opt.nss_keylog = atoi( q );
2480 if( opt.nss_keylog < 0 || opt.nss_keylog > 1 )
2481 goto usage;
2482 }
2483 else if( strcmp( p, "nss_keylog_file" ) == 0 )
2484 {
2485 opt.nss_keylog_file = q;
2486 }
Ron Eldor6ea64512018-07-02 10:08:07 +03002487 else if( strcmp( p, "use_srtp" ) == 0 )
2488 {
2489 opt.use_srtp = atoi ( q );
2490 }
2491 else if( strcmp( p, "srtp_force_profile" ) == 0 )
2492 {
2493 opt.force_srtp_profile = atoi( q );
2494 }
Ron Eldor12c6ead2018-07-03 15:08:32 +03002495 else if( strcmp( p, "support_mki" ) == 0 )
Ron Eldor6ea64512018-07-02 10:08:07 +03002496 {
Ron Eldor12c6ead2018-07-03 15:08:32 +03002497 opt.support_mki = atoi( q );
Ron Eldor6ea64512018-07-02 10:08:07 +03002498 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002499 else
2500 goto usage;
2501 }
2502
Hanno Beckerbc5308c2019-09-09 11:38:51 +01002503 if( opt.nss_keylog != 0 && opt.eap_tls != 0 )
2504 {
2505 mbedtls_printf( "Error: eap_tls and nss_keylog options cannot be used together.\n" );
2506 goto usage;
2507 }
2508
Hanno Becker16970d22017-10-10 15:56:37 +01002509 /* Event-driven IO is incompatible with the above custom
2510 * receive and send functions, as the polling builds on
2511 * refers to the underlying net_context. */
2512 if( opt.event == 1 && opt.nbio != 1 )
2513 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002514 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01002515 opt.nbio = 1;
2516 }
2517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518#if defined(MBEDTLS_DEBUG_C)
2519 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02002520#endif
Andrzej Kurekda4029d2018-06-20 07:07:55 -04002521 buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002522 if( buf == NULL )
2523 {
Andrzej Kurek755890f2018-06-20 08:17:04 -04002524 mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002525 ret = 3;
2526 goto exit;
2527 }
Paul Bakkerc73079a2014-04-25 16:34:30 +02002528
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002529#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002530 if( opt.psk_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002531 {
2532 if( strlen( opt.psk ) == 0 )
2533 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00002534 mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002535 ret = 2;
2536 goto usage;
2537 }
2538
2539 if( opt.force_ciphersuite[0] <= 0 )
2540 {
2541 mbedtls_printf( "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n" );
2542 ret = 2;
2543 goto usage;
2544 }
2545 }
2546
Hanno Becker1d911cd2018-11-15 13:06:09 +00002547 if( opt.psk_list_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002548 {
2549 if( opt.psk_list == NULL )
2550 {
2551 mbedtls_printf( "psk_slot set but no psk to be imported specified.\n" );
2552 ret = 2;
2553 goto usage;
2554 }
2555
2556 if( opt.force_ciphersuite[0] <= 0 )
2557 {
2558 mbedtls_printf( "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n" );
2559 ret = 2;
2560 goto usage;
2561 }
2562 }
2563#endif /* MBEDTLS_USE_PSA_CRYPTO */
2564
Paul Bakkerc1516be2013-06-29 16:01:32 +02002565 if( opt.force_ciphersuite[0] > 0 )
2566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002568 ciphersuite_info =
2569 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002570
Paul Bakker5b55b792013-07-19 13:43:43 +02002571 if( opt.max_version != -1 &&
2572 ciphersuite_info->min_minor_ver > opt.max_version )
2573 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002574 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker5b55b792013-07-19 13:43:43 +02002575 ret = 2;
2576 goto usage;
2577 }
2578 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02002579 ciphersuite_info->max_minor_ver < opt.min_version )
2580 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002581 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002582 ret = 2;
2583 goto usage;
2584 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002585
2586 /* If we select a version that's not supported by
2587 * this suite, then there will be no common ciphersuite... */
2588 if( opt.max_version == -1 ||
2589 opt.max_version > ciphersuite_info->max_minor_ver )
2590 {
Paul Bakker5b55b792013-07-19 13:43:43 +02002591 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002592 }
Paul Bakker5b55b792013-07-19 13:43:43 +02002593 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002594 {
Paul Bakker5b55b792013-07-19 13:43:43 +02002595 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002596 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2598 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
2599 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002600 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002601
2602 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002608 ret = 2;
2609 goto usage;
2610 }
2611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002613 }
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002614
2615#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002616 if( opt.psk_opaque != 0 || opt.psk_list_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002617 {
2618 /* Ensure that the chosen ciphersuite is PSK-only; we must know
2619 * the ciphersuite in advance to set the correct policy for the
2620 * PSK key slot. This limitation might go away in the future. */
2621 if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
2622 opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 )
2623 {
2624 mbedtls_printf( "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n" );
2625 ret = 2;
2626 goto usage;
2627 }
2628
2629 /* Determine KDF algorithm the opaque PSK will be used in. */
2630#if defined(MBEDTLS_SHA512_C)
2631 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
2632 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2633 else
2634#endif /* MBEDTLS_SHA512_C */
2635 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2636 }
2637#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerc1516be2013-06-29 16:01:32 +02002638 }
2639
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002640 if( opt.version_suites != NULL )
2641 {
2642 const char *name[4] = { 0 };
2643
2644 /* Parse 4-element coma-separated list */
2645 for( i = 0, p = (char *) opt.version_suites;
2646 i < 4 && *p != '\0';
2647 i++ )
2648 {
2649 name[i] = p;
2650
2651 /* Terminate the current string and move on to next one */
2652 while( *p != ',' && *p != '\0' )
2653 p++;
2654 if( *p == ',' )
2655 *p++ = '\0';
2656 }
2657
2658 if( i != 4 )
2659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 mbedtls_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002661 ret = 1;
2662 goto exit;
2663 }
2664
2665 memset( version_suites, 0, sizeof( version_suites ) );
2666
2667 /* Get the suites identifiers from their name */
2668 for( i = 0; i < 4; i++ )
2669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002671
2672 if( version_suites[i][0] == 0 )
2673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002675 ret = 2;
2676 goto usage;
2677 }
2678 }
2679 }
2680
Hanno Beckera0e20d02019-05-15 14:03:01 +01002681#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Ronald Cron7d866162020-06-18 10:36:26 +02002682 if( mbedtls_test_unhexify( cid, sizeof( cid ),
2683 opt.cid_val, &cid_len ) != 0 )
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002684 {
2685 mbedtls_printf( "CID not valid hex\n" );
2686 goto exit;
2687 }
2688
2689 /* Keep CID settings for renegotiation unless
2690 * specified otherwise. */
2691 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
2692 opt.cid_enabled_renego = opt.cid_enabled;
2693 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
2694 opt.cid_val_renego = opt.cid_val;
2695
Ronald Cron7d866162020-06-18 10:36:26 +02002696 if( mbedtls_test_unhexify( cid_renego, sizeof( cid_renego ),
2697 opt.cid_val_renego, &cid_renego_len ) != 0 )
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002698 {
2699 mbedtls_printf( "CID not valid hex\n" );
2700 goto exit;
2701 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002702#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +01002703
Gilles Peskineeccd8882020-03-10 12:19:08 +01002704#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002705 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002706 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02002707 */
Ronald Cron7d866162020-06-18 10:36:26 +02002708 if( mbedtls_test_unhexify( psk, sizeof( psk ),
2709 opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 mbedtls_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002712 goto exit;
2713 }
2714
2715 if( opt.psk_list != NULL )
2716 {
2717 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 mbedtls_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002720 goto exit;
2721 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002722 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002723#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002724
Hanno Beckere6706e62017-05-15 16:05:15 +01002725#if defined(MBEDTLS_ECP_C)
2726 if( opt.curves != NULL )
2727 {
2728 p = (char *) opt.curves;
2729 i = 0;
2730
2731 if( strcmp( p, "none" ) == 0 )
2732 {
2733 curve_list[0] = MBEDTLS_ECP_DP_NONE;
2734 }
2735 else if( strcmp( p, "default" ) != 0 )
2736 {
2737 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01002738 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002739 {
2740 q = p;
2741
2742 /* Terminate the current string */
2743 while( *p != ',' && *p != '\0' )
2744 p++;
2745 if( *p == ',' )
2746 *p++ = '\0';
2747
2748 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
2749 {
2750 curve_list[i++] = curve_cur->grp_id;
2751 }
2752 else
2753 {
2754 mbedtls_printf( "unknown curve %s\n", q );
2755 mbedtls_printf( "supported curves: " );
2756 for( curve_cur = mbedtls_ecp_curve_list();
2757 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
2758 curve_cur++ )
2759 {
2760 mbedtls_printf( "%s ", curve_cur->name );
2761 }
2762 mbedtls_printf( "\n" );
2763 goto exit;
2764 }
2765 }
2766
2767 mbedtls_printf("Number of curves: %d\n", i );
2768
Hanno Becker8651a432017-06-09 16:13:22 +01002769 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002770 {
Hanno Becker8651a432017-06-09 16:13:22 +01002771 mbedtls_printf( "curves list too long, maximum %d",
2772 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01002773 goto exit;
2774 }
2775
2776 curve_list[i] = MBEDTLS_ECP_DP_NONE;
2777 }
2778 }
2779#endif /* MBEDTLS_ECP_C */
2780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002782 if( opt.alpn_string != NULL )
2783 {
2784 p = (char *) opt.alpn_string;
2785 i = 0;
2786
2787 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01002788 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002789 {
2790 alpn_list[i++] = p;
2791
2792 /* Terminate the current string and move on to next one */
2793 while( *p != ',' && *p != '\0' )
2794 p++;
2795 if( *p == ',' )
2796 *p++ = '\0';
2797 }
2798 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002800
Paul Bakkerfbb17802013-04-17 19:10:21 +02002801 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002802 * 0. Initialize the RNG and the session data
2803 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002805 fflush( stdout );
2806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807 mbedtls_entropy_init( &entropy );
Philippe Antoine986b6f22019-06-07 15:04:32 +02002808 if (opt.reproducible)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002809 {
Philippe Antoine738153a2019-06-18 20:16:43 +02002810 srand( 1 );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002811 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy,
Philippe Antoine986b6f22019-06-07 15:04:32 +02002812 &entropy, (const unsigned char *) pers,
2813 strlen( pers ) ) ) != 0 )
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002814 {
2815 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +02002816 (unsigned int) -ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002817 goto exit;
2818 }
Philippe Antoine986b6f22019-06-07 15:04:32 +02002819 }
2820 else
2821 {
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002822 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
Philippe Antoine986b6f22019-06-07 15:04:32 +02002823 &entropy, (const unsigned char *) pers,
2824 strlen( pers ) ) ) != 0 )
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002825 {
2826 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +02002827 (unsigned int) -ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002828 goto exit;
2829 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002830 }
2831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002835 /*
2836 * 1.1. Load the trusted CA
2837 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002839 fflush( stdout );
2840
Hanno Becker8174bdf2019-03-05 16:22:07 +00002841 if( strcmp( opt.ca_path, "none" ) == 0 ||
2842 strcmp( opt.ca_file, "none" ) == 0 )
2843 {
2844 ret = 0;
2845 }
2846 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002848 if( strlen( opt.ca_path ) )
Hanno Becker8174bdf2019-03-05 16:22:07 +00002849 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002850 else if( strlen( opt.ca_file ) )
Hanno Becker8174bdf2019-03-05 16:22:07 +00002851 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02002852 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002853#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854#if defined(MBEDTLS_CERTS_C)
Hanno Becker09b8cae2019-02-01 08:13:19 +00002855 {
2856#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859 ret = mbedtls_x509_crt_parse( &cacert,
2860 (const unsigned char *) mbedtls_test_cas[i],
2861 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002862 if( ret != 0 )
2863 break;
2864 }
Hanno Becker09b8cae2019-02-01 08:13:19 +00002865 if( ret == 0 )
2866#endif /* MBEDTLS_PEM_PARSE_C */
2867 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
2868 {
2869 ret = mbedtls_x509_crt_parse_der( &cacert,
2870 (const unsigned char *) mbedtls_test_cas_der[i],
2871 mbedtls_test_cas_der_len[i] );
2872 if( ret != 0 )
2873 break;
2874 }
2875 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002876#else
2877 {
2878 ret = 1;
Hanno Beckera0c5ceb2018-12-05 16:49:42 +00002879 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002880 }
Hanno Becker09b8cae2019-02-01 08:13:19 +00002881#endif /* MBEDTLS_CERTS_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002882 if( ret < 0 )
2883 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +02002884 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", (unsigned int) -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002885 goto exit;
2886 }
2887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002889
2890 /*
2891 * 1.2. Load own certificate and private key
2892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002894 fflush( stdout );
2895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002897 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002898 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002899 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +02002903 (unsigned int) -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002904 goto exit;
2905 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002906 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002907 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002908 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002909 key_cert_init++;
Hanno Beckere58a6302018-11-07 16:20:16 +00002910 if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file,
2911 opt.key_pwd ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002912 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +02002913 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", (unsigned int) -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002914 goto exit;
2915 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002916 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002917 if( key_cert_init == 1 )
2918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002920 goto exit;
2921 }
2922
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002923 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002924 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002925 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +02002929 (unsigned int) -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002930 goto exit;
2931 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002932 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002933 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002934 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002935 key_cert_init2++;
Hanno Beckere58a6302018-11-07 16:20:16 +00002936 if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2,
2937 opt.key_pwd2 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +02002940 (unsigned int) -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002941 goto exit;
2942 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002943 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002944 if( key_cert_init2 == 1 )
2945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002947 goto exit;
2948 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002949#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002950 if( key_cert_init == 0 &&
2951 strcmp( opt.crt_file, "none" ) != 0 &&
2952 strcmp( opt.key_file, "none" ) != 0 &&
2953 key_cert_init2 == 0 &&
2954 strcmp( opt.crt_file2, "none" ) != 0 &&
2955 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957#if !defined(MBEDTLS_CERTS_C)
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002958 mbedtls_printf( "Not certificated or key provided, and \nMBEDTLS_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002959 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002960#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961#if defined(MBEDTLS_RSA_C)
2962 if( ( ret = mbedtls_x509_crt_parse( &srvcert,
2963 (const unsigned char *) mbedtls_test_srv_crt_rsa,
2964 mbedtls_test_srv_crt_rsa_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002965 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002966 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +02002967 (unsigned int) -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002968 goto exit;
2969 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 if( ( ret = mbedtls_pk_parse_key( &pkey,
2971 (const unsigned char *) mbedtls_test_srv_key_rsa,
2972 mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002973 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002974 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +02002975 (unsigned int) -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002976 goto exit;
2977 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002978 key_cert_init = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979#endif /* MBEDTLS_RSA_C */
2980#if defined(MBEDTLS_ECDSA_C)
2981 if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
2982 (const unsigned char *) mbedtls_test_srv_crt_ec,
2983 mbedtls_test_srv_crt_ec_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002984 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002985 mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +02002986 (unsigned int) -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002987 goto exit;
2988 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 if( ( ret = mbedtls_pk_parse_key( &pkey2,
2990 (const unsigned char *) mbedtls_test_srv_key_ec,
2991 mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002992 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002993 mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +02002994 (unsigned int) -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002995 goto exit;
2996 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002997 key_cert_init2 = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998#endif /* MBEDTLS_ECDSA_C */
2999#endif /* MBEDTLS_CERTS_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02003000 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002 mbedtls_printf( " ok\n" );
3003#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003006 if( opt.dhm_file != NULL )
3007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008 mbedtls_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003009 fflush( stdout );
3010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011 if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003014 (unsigned int) -ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003015 goto exit;
3016 }
3017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003019 }
3020#endif
3021
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02003022#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003023 if( opt.sni != NULL )
3024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025 mbedtls_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003026 fflush( stdout );
3027
3028 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
3029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003031 goto exit;
3032 }
3033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003035 }
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02003036#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003037
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003038 /*
3039 * 2. Setup the listening TCP socket
3040 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02003041 mbedtls_printf( " . Bind on %s://%s:%s/ ...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01003043 opt.server_addr ? opt.server_addr : "*",
3044 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003045 fflush( stdout );
3046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047 if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
3048 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
3049 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003050 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003051 mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", (unsigned int) -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003052 goto exit;
3053 }
3054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003056
3057 /*
3058 * 3. Setup stuff
3059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003061 fflush( stdout );
3062
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003063 if( ( ret = mbedtls_ssl_config_defaults( &conf,
3064 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003065 opt.transport,
3066 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003067 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003068 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", (unsigned int) -ret );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003069 goto exit;
3070 }
3071
Gilles Peskineef86ab22017-05-05 18:59:02 +02003072#if defined(MBEDTLS_X509_CRT_PARSE_C)
3073 /* The default algorithms profile disables SHA-1, but our tests still
3074 rely on it heavily. Hence we allow it here. A real-world server
3075 should use the default profile unless there is a good reason not to. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02003076 if( opt.allow_sha1 > 0 )
3077 {
3078 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
3079 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
Gilles Peskine682df092017-05-11 19:01:11 +02003080 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
Gilles Peskinebc70a182017-05-09 15:59:24 +02003081 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02003082#endif /* MBEDTLS_X509_CRT_PARSE_C */
3083
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01003084 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003085 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003086
Janos Follath4817e272017-04-10 13:44:33 +01003087 if( opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST )
3088 mbedtls_ssl_conf_cert_req_ca_list( &conf, opt.cert_req_ca_list );
3089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02003091 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003092 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003093
Hanno Beckere7675d02018-08-14 13:28:56 +01003094 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01003095 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02003097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003099 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003100 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003101 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003102 goto exit;
Ron Eldor6ea64512018-07-02 10:08:07 +03003103 }
Paul Bakker05decb22013-08-15 13:33:48 +02003104#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02003105
Hanno Beckera0e20d02019-05-15 14:03:01 +01003106#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003107 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckerad4a1372019-05-03 13:06:44 +01003108 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003109 if( opt.cid_enabled == 1 &&
3110 opt.cid_enabled_renego == 1 &&
3111 cid_len != cid_renego_len )
3112 {
3113 mbedtls_printf( "CID length must not change during renegotiation\n" );
3114 goto usage;
3115 }
3116
3117 if( opt.cid_enabled == 1 )
Hanno Becker8367ccc2019-05-14 11:30:10 +01003118 ret = mbedtls_ssl_conf_cid( &conf, cid_len,
3119 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003120 else
Hanno Becker8367ccc2019-05-14 11:30:10 +01003121 ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
3122 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003123
Hanno Beckerad4a1372019-05-03 13:06:44 +01003124 if( ret != 0 )
3125 {
Hanno Beckerd5eed422019-05-23 16:58:22 +01003126 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003127 (unsigned int) -ret );
Hanno Beckerad4a1372019-05-03 13:06:44 +01003128 goto exit;
3129 }
3130 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003131#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerad4a1372019-05-03 13:06:44 +01003132
Ron Eldor6ea64512018-07-02 10:08:07 +03003133#if defined(MBEDTLS_SSL_DTLS_SRTP)
Bence Szépkúti880334c2020-12-14 14:42:13 +01003134 const mbedtls_ssl_srtp_profile forced_profile[] = { opt.force_srtp_profile, MBEDTLS_TLS_SRTP_UNSET };
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003135 if( opt.use_srtp == 1 )
Ron Eldor6ea64512018-07-02 10:08:07 +03003136 {
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003137 if( opt.force_srtp_profile != 0 )
Ron Eldor6ea64512018-07-02 10:08:07 +03003138 {
Johan Pascal253d0262020-09-22 13:04:45 +02003139 ret = mbedtls_ssl_conf_dtls_srtp_protection_profiles( &conf, forced_profile );
Ron Eldor6ea64512018-07-02 10:08:07 +03003140 }
3141 else
3142 {
Johan Pascal253d0262020-09-22 13:04:45 +02003143 ret = mbedtls_ssl_conf_dtls_srtp_protection_profiles( &conf, default_profiles );
Ron Eldor6ea64512018-07-02 10:08:07 +03003144 }
3145
3146 if( ret != 0 )
3147 {
3148 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_dtls_srtp_protection_profiles returned %d\n\n", ret );
3149 goto exit;
3150 }
3151
Ron Eldor12c6ead2018-07-03 15:08:32 +03003152 mbedtls_ssl_conf_srtp_mki_value_supported( &conf,
3153 opt.support_mki ?
3154 MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED :
3155 MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED );
3156
Ron Eldor6ea64512018-07-02 10:08:07 +03003157 }
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003158 else if( opt.force_srtp_profile != 0 )
Ron Eldor6ea64512018-07-02 10:08:07 +03003159 {
3160 mbedtls_printf( " failed\n ! must enable use_srtp to force srtp profile\n\n" );
3161 goto exit;
3162 }
3163#endif /* MBEDTLS_SSL_DTLS_SRTP */
3164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003166 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003167 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003168#endif
3169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003171 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003172 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003173#endif
3174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003176 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003177 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003178#endif
3179
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003180#if defined(MBEDTLS_SSL_EXPORT_KEYS)
3181 if( opt.eap_tls != 0 )
Hanno Becker48f3a3d2019-08-29 06:31:22 +01003182 {
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003183 mbedtls_ssl_conf_export_keys_ext_cb( &conf, eap_tls_key_derivation,
3184 &eap_tls_keying );
Hanno Becker48f3a3d2019-08-29 06:31:22 +01003185 }
3186 else if( opt.nss_keylog != 0 )
3187 {
3188 mbedtls_ssl_conf_export_keys_ext_cb( &conf,
3189 nss_keylog_export,
3190 NULL );
3191 }
Ron Eldor65b56ef2019-09-26 16:40:48 +03003192#if defined( MBEDTLS_SSL_DTLS_SRTP )
3193 else if( opt.use_srtp != 0 )
3194 {
3195 mbedtls_ssl_conf_export_keys_ext_cb( &conf, dtls_srtp_key_derivation,
3196 &dtls_srtp_keying );
3197 }
3198#endif /* MBEDTLS_SSL_DTLS_SRTP */
3199#endif /* MBEDTLS_SSL_EXPORT_KEYS */
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003202 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003203 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003204 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003205 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003206 goto exit;
3207 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003208#endif
3209
Philippe Antoine986b6f22019-06-07 15:04:32 +02003210 if (opt.reproducible)
3211 {
Philippe Antoinef91b3722019-06-11 16:02:43 +02003212#if defined(MBEDTLS_HAVE_TIME)
Philippe Antoineaa4d1522019-06-06 21:24:31 +02003213#if defined(MBEDTLS_PLATFORM_TIME_ALT)
3214 mbedtls_platform_set_time( dummy_constant_time );
3215#else
Philippe Antoine7c9d7242019-06-11 12:11:36 +02003216 fprintf( stderr, "Warning: reproducible option used without constant time\n" );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02003217#endif
Philippe Antoine0ff84fb2019-06-11 12:15:17 +02003218#endif
Philippe Antoine986b6f22019-06-07 15:04:32 +02003219 }
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003220 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
3221 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01003224 if( opt.cache_max != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225 mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01003226
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01003227 if( opt.cache_timeout != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01003229
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003230 mbedtls_ssl_conf_session_cache( &conf, &cache,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01003231 mbedtls_ssl_cache_get,
3232 mbedtls_ssl_cache_set );
Paul Bakker0a597072012-09-25 21:55:46 +00003233#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003236 if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003237 {
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003238 if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
3239 mbedtls_ctr_drbg_random, &ctr_drbg,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +02003240 MBEDTLS_CIPHER_AES_256_GCM,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003241 opt.ticket_timeout ) ) != 0 )
3242 {
3243 mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
3244 goto exit;
3245 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01003246
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003247 mbedtls_ssl_conf_session_tickets_cb( &conf,
3248 mbedtls_ssl_ticket_write,
3249 mbedtls_ssl_ticket_parse,
3250 &ticket_ctx );
3251 }
Paul Bakkera503a632013-08-14 13:48:06 +02003252#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254#if defined(MBEDTLS_SSL_PROTO_DTLS)
3255 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02003256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003258 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02003259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
3261 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263 mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003264 goto exit;
3265 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02003266
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003267 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003268 &cookie_ctx );
3269 }
3270 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271#endif /* MBEDTLS_SSL_COOKIE_C */
3272#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003273 if( opt.cookies == 0 )
3274 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003275 mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003276 }
3277 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003279 {
3280 ; /* Nothing to do */
3281 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003284 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003285 mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02003286#endif
3287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02003289 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003290 mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003291#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02003292 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02003294
Paul Bakker41c83d32013-03-20 14:39:14 +01003295 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003296 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00003297
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02003298#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00003299 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003300 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02003301#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003302
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02003303 if( opt.version_suites != NULL )
3304 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003305 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 MBEDTLS_SSL_MAJOR_VERSION_3,
3307 MBEDTLS_SSL_MINOR_VERSION_0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003308 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309 MBEDTLS_SSL_MAJOR_VERSION_3,
3310 MBEDTLS_SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003311 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 MBEDTLS_SSL_MAJOR_VERSION_3,
3313 MBEDTLS_SSL_MINOR_VERSION_2 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003314 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 MBEDTLS_SSL_MAJOR_VERSION_3,
3316 MBEDTLS_SSL_MINOR_VERSION_3 );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02003317 }
3318
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01003319 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003320 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003322 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01003323
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02003324 if( opt.renego_delay != DFL_RENEGO_DELAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003325 mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003326
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01003327 if( opt.renego_period != DFL_RENEGO_PERIOD )
3328 {
Andres AG692ad842017-01-19 16:30:57 +00003329 PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003330 mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01003331 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003332#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01003335 if( strcmp( opt.ca_path, "none" ) != 0 &&
3336 strcmp( opt.ca_file, "none" ) != 0 )
3337 {
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02003338#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3339 if( opt.ca_callback != 0 )
3340 mbedtls_ssl_conf_ca_cb( &conf, ca_callback, &cacert);
3341 else
3342#endif
3343 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01003344 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02003345 if( key_cert_init )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003346 {
3347 mbedtls_pk_context *pk = &pkey;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003348#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003349 if( opt.async_private_delay1 >= 0 )
3350 {
Gilles Peskine44817442018-06-13 18:09:28 +02003351 ret = ssl_async_set_key( &ssl_async_keys, &srvcert, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02003352 opt.async_private_delay1 );
3353 if( ret < 0 )
3354 {
3355 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
3356 ret );
3357 goto exit;
3358 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003359 pk = NULL;
3360 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003361#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003362 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003363 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003364 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003365 goto exit;
3366 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003367 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02003368 if( key_cert_init2 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003369 {
3370 mbedtls_pk_context *pk = &pkey2;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003371#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003372 if( opt.async_private_delay2 >= 0 )
3373 {
Gilles Peskine44817442018-06-13 18:09:28 +02003374 ret = ssl_async_set_key( &ssl_async_keys, &srvcert2, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02003375 opt.async_private_delay2 );
3376 if( ret < 0 )
3377 {
3378 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
3379 ret );
3380 goto exit;
3381 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003382 pk = NULL;
3383 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003384#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003385 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003386 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003387 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003388 goto exit;
3389 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003390 }
3391
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003392#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003393 if( opt.async_operations[0] != '-' )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003394 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003395 mbedtls_ssl_async_sign_t *sign = NULL;
3396 mbedtls_ssl_async_decrypt_t *decrypt = NULL;
Gilles Peskine12ab5d42018-04-24 12:32:04 +02003397 const char *r;
3398 for( r = opt.async_operations; *r; r++ )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003399 {
Gilles Peskine12ab5d42018-04-24 12:32:04 +02003400 switch( *r )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003401 {
3402 case 'd':
3403 decrypt = ssl_async_decrypt;
3404 break;
3405 case 's':
3406 sign = ssl_async_sign;
3407 break;
3408 }
3409 }
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003410 ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
3411 - opt.async_private_error :
3412 opt.async_private_error );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003413 ssl_async_keys.f_rng = mbedtls_ctr_drbg_random;
3414 ssl_async_keys.p_rng = &ctr_drbg;
3415 mbedtls_ssl_conf_async_private_cb( &conf,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003416 sign,
3417 decrypt,
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003418 ssl_async_resume,
3419 ssl_async_cancel,
3420 &ssl_async_keys );
3421 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003422#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003423#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +02003424
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02003425#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003426 if( opt.sni != NULL )
Gilles Peskine166ce742018-04-30 10:30:49 +02003427 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003428 mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
Gilles Peskine166ce742018-04-30 10:30:49 +02003429#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3430 if( opt.async_private_delay2 >= 0 )
3431 {
Gilles Peskinee2479892018-06-13 18:06:51 +02003432 sni_entry *cur;
3433 for( cur = sni_info; cur != NULL; cur = cur->next )
Gilles Peskine166ce742018-04-30 10:30:49 +02003434 {
Gilles Peskinee2479892018-06-13 18:06:51 +02003435 ret = ssl_async_set_key( &ssl_async_keys,
Gilles Peskine44817442018-06-13 18:09:28 +02003436 cur->cert, cur->key, 1,
Gilles Peskinee2479892018-06-13 18:06:51 +02003437 opt.async_private_delay2 );
3438 if( ret < 0 )
3439 {
3440 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
3441 ret );
3442 goto exit;
3443 }
3444 cur->key = NULL;
Gilles Peskine166ce742018-04-30 10:30:49 +02003445 }
Gilles Peskine166ce742018-04-30 10:30:49 +02003446 }
3447#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3448 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003449#endif
3450
Hanno Beckere6706e62017-05-15 16:05:15 +01003451#if defined(MBEDTLS_ECP_C)
3452 if( opt.curves != NULL &&
3453 strcmp( opt.curves, "default" ) != 0 )
3454 {
3455 mbedtls_ssl_conf_curves( &conf, curve_list );
3456 }
3457#endif
3458
Gilles Peskineeccd8882020-03-10 12:19:08 +01003459#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003460
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02003461 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
3462 {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003463#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00003464 if( opt.psk_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003465 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00003466 /* The algorithm has already been determined earlier. */
Janos Follathbe4efc22019-08-08 11:38:18 +01003467 status = psa_setup_psk_key_slot( &psk_slot, alg, psk, psk_len );
Hanno Becker1d911cd2018-11-15 13:06:09 +00003468 if( status != PSA_SUCCESS )
3469 {
3470 fprintf( stderr, "SETUP FAIL\n" );
3471 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
3472 goto exit;
3473 }
3474 if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, psk_slot,
3475 (const unsigned char *) opt.psk_identity,
3476 strlen( opt.psk_identity ) ) ) != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003477 {
3478 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
3479 ret );
3480 goto exit;
3481 }
3482 }
3483 else
3484#endif /* MBEDTLS_USE_PSA_CRYPTO */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01003485 if( psk_len > 0 )
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02003486 {
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01003487 ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
3488 (const unsigned char *) opt.psk_identity,
3489 strlen( opt.psk_identity ) );
3490 if( ret != 0 )
3491 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003492 mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", (unsigned int) -ret );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01003493 goto exit;
3494 }
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02003495 }
3496 }
3497
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02003498 if( opt.psk_list != NULL )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003499 {
3500#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00003501 if( opt.psk_list_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003502 {
3503 psk_entry *cur_psk;
3504 for( cur_psk = psk_info; cur_psk != NULL; cur_psk = cur_psk->next )
3505 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00003506
Janos Follathbe4efc22019-08-08 11:38:18 +01003507 status = psa_setup_psk_key_slot( &cur_psk->slot, alg,
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003508 cur_psk->key,
3509 cur_psk->key_len );
3510 if( status != PSA_SUCCESS )
3511 {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003512 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
3513 goto exit;
3514 }
3515 }
3516 }
3517#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker1d911cd2018-11-15 13:06:09 +00003518
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003519 mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003520 }
Paul Bakkered27a042013-04-18 22:46:23 +02003521#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523#if defined(MBEDTLS_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00003524 /*
3525 * Use different group than default DHM group
3526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003528 if( opt.dhm_file != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003529 ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003530#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003531 if( ret != 0 )
3532 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003533 mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", (unsigned int) -ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003534 goto exit;
3535 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003536#endif
3537
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02003538 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003539 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00003540
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02003541 if( opt.max_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003542 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakkerc1516be2013-06-29 16:01:32 +02003543
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003544 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
3545 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003546 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", (unsigned int) -ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003547 goto exit;
3548 }
3549
Hanno Beckerdcc94e62019-07-03 17:05:43 +01003550 io_ctx.ssl = &ssl;
3551 io_ctx.net = &client_fd;
3552 mbedtls_ssl_set_bio( &ssl, &io_ctx, send_cb, recv_cb,
3553 opt.nbio == 0 ? recv_timeout_cb : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003554
Hanno Beckera0e20d02019-05-15 14:03:01 +01003555#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +01003556 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3557 {
3558 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
3559 cid, cid_len ) ) != 0 )
3560 {
3561 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
3562 ret );
3563 goto exit;
3564 }
3565 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003566#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +01003567
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02003568#if defined(MBEDTLS_SSL_PROTO_DTLS)
3569 if( opt.dtls_mtu != DFL_DTLS_MTU )
3570 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
3571#endif
3572
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003573#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02003574 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
3575 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003576#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02003577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003579
3580reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02003581#if !defined(_WIN32)
3582 if( received_sigterm )
3583 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01003584 mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" );
3585 if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT )
3586 ret = 0;
3587
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02003588 goto exit;
3589 }
3590#endif
3591
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003592 if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
3593 {
3594 mbedtls_printf( " ! Client initiated reconnection from same port\n" );
3595 goto handshake;
3596 }
3597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003599 if( ret != 0 )
3600 {
3601 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 mbedtls_strerror( ret, error_buf, 100 );
3603 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003604 }
3605#endif
3606
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003607 mbedtls_net_free( &client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01003608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003609 mbedtls_ssl_session_reset( &ssl );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003610
3611 /*
3612 * 3. Wait until a client connects
3613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 mbedtls_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003615 fflush( stdout );
3616
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003617 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02003618 client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003619 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02003620#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003621 if( received_sigterm )
3622 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01003623 mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" );
3624 if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED )
3625 ret = 0;
3626
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003627 goto exit;
3628 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02003629#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003630
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003631 mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", (unsigned int) -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003632 goto exit;
3633 }
3634
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003635 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003636 ret = mbedtls_net_set_nonblock( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003637 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003638 ret = mbedtls_net_set_block( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003639 if( ret != 0 )
3640 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003641 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", (unsigned int) -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003642 goto exit;
3643 }
3644
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003645 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
3648 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003649 {
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02003650 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
3651 client_ip, cliip_len ) ) != 0 )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003652 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01003653 mbedtls_printf( " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003654 (unsigned int) -ret );
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003655 goto exit;
3656 }
3657 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003659
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02003660#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3661 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
3662 {
3663 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
3664 (const unsigned char *) opt.ecjpake_pw,
3665 strlen( opt.ecjpake_pw ) ) ) != 0 )
3666 {
3667 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
3668 goto exit;
3669 }
3670 }
3671#endif
3672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003674
3675 /*
3676 * 4. Handshake
3677 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003678handshake:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003680 fflush( stdout );
3681
Hanno Becker16970d22017-10-10 15:56:37 +01003682 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003683 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003684#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003685 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003686 ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003687 {
3688 mbedtls_printf( " cancelling on injected error\n" );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003689 break;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003690 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003691#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskineb44692f2018-04-24 12:18:19 +02003692
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003693 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003694 break;
3695
3696 /* For event-driven IO, wait for socket to become available */
3697 if( opt.event == 1 /* level triggered IO */ )
3698 {
3699#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003700 ret = idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003701#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003702 ret = idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003703#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003704 if( ret != 0 )
3705 goto reset;
Hanno Becker16970d22017-10-10 15:56:37 +01003706 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003707 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711 mbedtls_printf( " hello verification requested\n" );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003712 ret = 0;
3713 goto reset;
3714 }
3715 else if( ret != 0 )
3716 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003717 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", (unsigned int) -ret );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02003718
3719#if defined(MBEDTLS_X509_CRT_PARSE_C)
3720 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3721 {
3722 char vrfy_buf[512];
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02003723 flags = mbedtls_ssl_get_verify_result( &ssl );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02003724
3725 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
3726
3727 mbedtls_printf( "%s\n", vrfy_buf );
3728 }
3729#endif
3730
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003731#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003732 if( opt.async_private_error < 0 )
3733 /* Injected error only the first time round, to test reset */
3734 ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
3735#endif
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003736 goto reset;
3737 }
3738 else /* ret == 0 */
3739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
3741 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003742 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003744 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
3745 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02003746 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003747 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02003748
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003749#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003750 mbedtls_printf( " [ Maximum input fragment length is %u ]\n",
3751 (unsigned int) mbedtls_ssl_get_input_max_frag_len( &ssl ) );
3752 mbedtls_printf( " [ Maximum output fragment length is %u ]\n",
3753 (unsigned int) mbedtls_ssl_get_output_max_frag_len( &ssl ) );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003754#endif
3755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003756#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003757 if( opt.alpn_string != NULL )
3758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003759 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
3760 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003761 alp ? alp : "(none)" );
3762 }
3763#endif
3764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003766 /*
Ron Eldor2a47be52017-06-20 15:23:23 +03003767 * 5. Verify the client certificate
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003770
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003771 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003772 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003773 char vrfy_buf[512];
3774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003775 mbedtls_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003776
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003777 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003778
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003779 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003780 }
3781 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003783
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003784 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003785 {
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003786 char crt_buf[512];
3787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003788 mbedtls_printf( " . Peer certificate information ...\n" );
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003789 mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790 mbedtls_ssl_get_peer_cert( &ssl ) );
Manuel Pégourié-Gonnard67557172015-07-02 11:15:48 +02003791 mbedtls_printf( "%s\n", crt_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003792 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003793#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003794
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003795#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Ron Eldor51d3ab52019-05-12 14:54:30 +03003796 if( opt.eap_tls != 0 )
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003797 {
3798 size_t j = 0;
Ron Eldor51d3ab52019-05-12 14:54:30 +03003799
3800 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type,
3801 eap_tls_keying.master_secret,
3802 sizeof( eap_tls_keying.master_secret ),
3803 eap_tls_label,
3804 eap_tls_keying.randbytes,
3805 sizeof( eap_tls_keying.randbytes ),
3806 eap_tls_keymaterial,
3807 sizeof( eap_tls_keymaterial ) ) )
3808 != 0 )
3809 {
3810 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003811 (unsigned int) -ret );
Ron Eldor65d8c262019-06-04 13:05:36 +03003812 goto reset;
Ron Eldor51d3ab52019-05-12 14:54:30 +03003813 }
3814
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003815 mbedtls_printf( " EAP-TLS key material is:" );
3816 for( j = 0; j < sizeof( eap_tls_keymaterial ); j++ )
3817 {
3818 if( j % 8 == 0 )
3819 mbedtls_printf("\n ");
3820 mbedtls_printf("%02x ", eap_tls_keymaterial[j] );
3821 }
3822 mbedtls_printf("\n");
3823
Ron Eldor51d3ab52019-05-12 14:54:30 +03003824 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type, NULL, 0,
Ron Eldor51c45072019-05-15 17:49:54 +03003825 eap_tls_label,
3826 eap_tls_keying.randbytes,
3827 sizeof( eap_tls_keying.randbytes ),
3828 eap_tls_iv,
3829 sizeof( eap_tls_iv ) ) ) != 0 )
Ron Eldor51d3ab52019-05-12 14:54:30 +03003830 {
3831 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003832 (unsigned int) -ret );
Ron Eldor65d8c262019-06-04 13:05:36 +03003833 goto reset;
Ron Eldor51d3ab52019-05-12 14:54:30 +03003834 }
3835
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003836 mbedtls_printf( " EAP-TLS IV is:" );
3837 for( j = 0; j < sizeof( eap_tls_iv ); j++ )
3838 {
3839 if( j % 8 == 0 )
3840 mbedtls_printf("\n ");
3841 mbedtls_printf("%02x ", eap_tls_iv[j] );
3842 }
3843 mbedtls_printf("\n");
3844 }
Ron Eldor65b56ef2019-09-26 16:40:48 +03003845
3846#if defined( MBEDTLS_SSL_DTLS_SRTP )
3847 else if( opt.use_srtp != 0 )
3848 {
3849 size_t j = 0;
Johan Pascal2258a4f2020-10-28 13:53:09 +01003850 mbedtls_dtls_srtp_info dtls_srtp_negotiation_result;
3851 mbedtls_ssl_get_dtls_srtp_negotiation_result( &ssl, &dtls_srtp_negotiation_result );
Ron Eldor65b56ef2019-09-26 16:40:48 +03003852
Johan Pascal5fbe9e72020-10-29 10:49:21 +01003853 if( dtls_srtp_negotiation_result.chosen_dtls_srtp_profile
3854 == MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor65b56ef2019-09-26 16:40:48 +03003855 {
Johan Pascald576fdb2020-09-22 10:39:53 +02003856 mbedtls_printf( " Unable to negotiate "
3857 "the use of DTLS-SRTP\n" );
Ron Eldor65b56ef2019-09-26 16:40:48 +03003858 }
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003859 else
Ron Eldor65b56ef2019-09-26 16:40:48 +03003860 {
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003861 if( ( ret = mbedtls_ssl_tls_prf( dtls_srtp_keying.tls_prf_type,
3862 dtls_srtp_keying.master_secret,
3863 sizeof( dtls_srtp_keying.master_secret ),
3864 dtls_srtp_label,
3865 dtls_srtp_keying.randbytes,
3866 sizeof( dtls_srtp_keying.randbytes ),
3867 dtls_srtp_key_material,
3868 sizeof( dtls_srtp_key_material ) ) )
3869 != 0 )
3870 {
3871 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
3872 (unsigned int) -ret );
3873 goto exit;
3874 }
Ron Eldor65b56ef2019-09-26 16:40:48 +03003875
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003876 mbedtls_printf( " DTLS-SRTP key material is:" );
3877 for( j = 0; j < sizeof( dtls_srtp_key_material ); j++ )
3878 {
3879 if( j % 8 == 0 )
Johan Pascald576fdb2020-09-22 10:39:53 +02003880 mbedtls_printf( "\n " );
3881 mbedtls_printf( "%02x ", dtls_srtp_key_material[j] );
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003882 }
Johan Pascal9bc50b02020-09-24 12:01:13 +02003883 mbedtls_printf( "\n" );
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003884
Johan Pascal9bc50b02020-09-24 12:01:13 +02003885 /* produce a less readable output used to perform automatic checks
3886 * - compare client and server output
3887 * - interop test with openssl which client produces this kind of output
3888 */
3889 mbedtls_printf( " Keying material: " );
3890 for( j = 0; j < sizeof( dtls_srtp_key_material ); j++ )
3891 {
3892 mbedtls_printf( "%02X", dtls_srtp_key_material[j] );
3893 }
Johan Pascald576fdb2020-09-22 10:39:53 +02003894 mbedtls_printf( "\n" );
Johan Pascal20c7db32020-10-26 22:45:58 +01003895
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01003896 if ( dtls_srtp_negotiation_result.mki_len > 0 )
Johan Pascal20c7db32020-10-26 22:45:58 +01003897 {
3898 mbedtls_printf( " DTLS-SRTP mki value: " );
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01003899 for( j = 0; j < dtls_srtp_negotiation_result.mki_len; j++ )
Johan Pascal20c7db32020-10-26 22:45:58 +01003900 {
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01003901 mbedtls_printf( "%02X", dtls_srtp_negotiation_result.mki_value[j] );
Johan Pascal20c7db32020-10-26 22:45:58 +01003902 }
3903 }
3904 else
3905 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01003906 mbedtls_printf( " DTLS-SRTP no mki value negotiated" );
Johan Pascal20c7db32020-10-26 22:45:58 +01003907 }
3908 mbedtls_printf( "\n" );
3909
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003910 }
Ron Eldor65b56ef2019-09-26 16:40:48 +03003911 }
3912#endif /* MBEDTLS_SSL_DTLS_SRTP */
3913#endif /* MBEDTLS_SSL_EXPORT_KEYS */
Hanno Beckera7d25422019-04-09 17:28:10 +01003914
Hanno Beckera0e20d02019-05-15 14:03:01 +01003915#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003916 ret = report_cid_usage( &ssl, "initial handshake" );
3917 if( ret != 0 )
3918 goto exit;
3919
Hanno Beckera7d25422019-04-09 17:28:10 +01003920 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3921 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003922 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
3923 cid_renego, cid_renego_len ) ) != 0 )
Hanno Beckera7d25422019-04-09 17:28:10 +01003924 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003925 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
3926 ret );
Hanno Beckera7d25422019-04-09 17:28:10 +01003927 goto exit;
3928 }
Hanno Beckera7d25422019-04-09 17:28:10 +01003929 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003930#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +01003931
Piotr Nowicki0937ed22019-11-26 16:32:40 +01003932#if defined(MBEDTLS_MEMORY_DEBUG)
3933 mbedtls_memory_buffer_alloc_cur_get( &current_heap_memory, &heap_blocks );
3934 mbedtls_memory_buffer_alloc_max_get( &peak_heap_memory, &heap_blocks );
3935 mbedtls_printf( "Heap memory usage after handshake: %lu bytes. Peak memory usage was %lu\n",
3936 (unsigned long) current_heap_memory, (unsigned long) peak_heap_memory );
3937#endif /* MBEDTLS_MEMORY_DEBUG */
3938
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02003939 if( opt.exchanges == 0 )
3940 goto close_notify;
3941
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003942 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003943data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003944 /*
3945 * 6. Read the HTTP Request
3946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003947 mbedtls_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003948 fflush( stdout );
3949
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003950 /*
3951 * TLS and DTLS need different reading styles (stream vs datagram)
3952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003953 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003954 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003955 do
3956 {
3957 int terminated = 0;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003958 len = opt.buffer_size - 1;
3959 memset( buf, 0, opt.buffer_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003960 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003961
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003962 if( mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003963 {
3964 if( opt.event == 1 /* level triggered IO */ )
3965 {
3966#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003967 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003968#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003969 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003970#endif
3971 }
3972
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003973 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01003974 }
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003975
3976 if( ret <= 0 )
3977 {
3978 switch( ret )
3979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003980 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3981 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003982 goto close_notify;
3983
3984 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985 case MBEDTLS_ERR_NET_CONN_RESET:
3986 mbedtls_printf( " connection was reset by peer\n" );
3987 ret = MBEDTLS_ERR_NET_CONN_RESET;
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003988 goto reset;
3989
3990 default:
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003991 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", (unsigned int) -ret );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003992 goto reset;
3993 }
3994 }
3995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996 if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003997 {
3998 len = ret;
3999 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004000 mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02004001
4002 /* End of message should be detected according to the syntax of the
4003 * application protocol (eg HTTP), just use a dummy test here. */
4004 if( buf[len - 1] == '\n' )
4005 terminated = 1;
4006 }
4007 else
4008 {
4009 int extra_len, ori_len;
4010 unsigned char *larger_buf;
4011
4012 ori_len = ret;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02004013 extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02004014
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004015 larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02004016 if( larger_buf == NULL )
4017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018 mbedtls_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02004019 ret = 1;
4020 goto reset;
4021 }
4022
4023 memset( larger_buf, 0, ori_len + extra_len );
4024 memcpy( larger_buf, buf, ori_len );
4025
4026 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027 ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02004028 if( ret != extra_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004029 mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02004030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004031 mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02004032 ret = 1;
4033 goto reset;
4034 }
4035
4036 larger_buf[ori_len + extra_len] = '\0';
Kenneth Soerensen518d4352020-04-01 17:22:45 +02004037 mbedtls_printf( " %d bytes read (%d + %d)\n\n%s\n",
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02004038 ori_len + extra_len, ori_len, extra_len,
4039 (char *) larger_buf );
4040
4041 /* End of message should be detected according to the syntax of the
4042 * application protocol (eg HTTP), just use a dummy test here. */
4043 if( larger_buf[ori_len + extra_len - 1] == '\n' )
4044 terminated = 1;
4045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004046 mbedtls_free( larger_buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02004047 }
4048
4049 if( terminated )
4050 {
4051 ret = 0;
4052 break;
4053 }
4054 }
4055 while( 1 );
4056 }
4057 else /* Not stream, so datagram */
4058 {
Andrzej Kurek30e731d2017-10-12 13:50:29 +02004059 len = opt.buffer_size - 1;
4060 memset( buf, 0, opt.buffer_size );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004061
Gilles Peskineb44692f2018-04-24 12:18:19 +02004062 do
Hanno Becker16970d22017-10-10 15:56:37 +01004063 {
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00004064 /* Without the call to `mbedtls_ssl_check_pending`, it might
4065 * happen that the client sends application data in the same
4066 * datagram as the Finished message concluding the handshake.
4067 * In this case, the application data would be ready to be
4068 * processed while the underlying transport wouldn't signal
4069 * any further incoming data.
4070 *
4071 * See the test 'Event-driven I/O: session-id resume, UDP packing'
4072 * in tests/ssl-opt.sh.
4073 */
4074
4075 /* For event-driven IO, wait for socket to become available */
4076 if( mbedtls_ssl_check_pending( &ssl ) == 0 &&
4077 opt.event == 1 /* level triggered IO */ )
4078 {
4079#if defined(MBEDTLS_TIMING_C)
4080 idle( &client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ );
4081#else
4082 idle( &client_fd, MBEDTLS_ERR_SSL_WANT_READ );
4083#endif
4084 }
4085
Hanno Becker16970d22017-10-10 15:56:37 +01004086 ret = mbedtls_ssl_read( &ssl, buf, len );
4087
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00004088 /* Note that even if `mbedtls_ssl_check_pending` returns true,
4089 * it can happen that the subsequent call to `mbedtls_ssl_read`
4090 * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
4091 * might be discarded (e.g. because they are retransmissions). */
Hanno Becker16970d22017-10-10 15:56:37 +01004092 }
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02004093 while( mbedtls_status_is_ssl_in_progress( ret ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004094
4095 if( ret <= 0 )
4096 {
4097 switch( ret )
4098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
4100 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02004101 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02004102 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004103
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004104 default:
Kenneth Soerensen518d4352020-04-01 17:22:45 +02004105 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", (unsigned int) -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02004106 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004107 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004108 }
4109
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02004110 len = ret;
4111 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02004113 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004114 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004115
4116 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02004117 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02004118 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004120#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00004121 if( opt.renegotiate && exchanges_left == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02004122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123 mbedtls_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02004124 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02004125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004126 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02004127 {
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02004128 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02004129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004130 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02004131 goto reset;
4132 }
Hanno Becker16970d22017-10-10 15:56:37 +01004133
4134 /* For event-driven IO, wait for socket to become available */
4135 if( opt.event == 1 /* level triggered IO */ )
4136 {
4137#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00004138 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01004139#else
Hanno Becker197a91c2017-10-31 10:58:53 +00004140 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01004141#endif
4142 }
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02004143 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02004144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004145 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02004146 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004147#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02004148
Hanno Beckera0e20d02019-05-15 14:03:01 +01004149#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01004150 ret = report_cid_usage( &ssl, "after renegotiation" );
4151 if( ret != 0 )
4152 goto exit;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004153#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01004154
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02004155 /*
4156 * 7. Write the 200 Response
4157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004158 mbedtls_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004159 fflush( stdout );
4160
4161 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004162 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004163
Andrzej Kurek30e731d2017-10-12 13:50:29 +02004164 /* Add padding to the response to reach opt.response_size in length */
4165 if( opt.response_size != DFL_RESPONSE_SIZE &&
4166 len < opt.response_size )
4167 {
4168 memset( buf + len, 'B', opt.response_size - len );
4169 len += opt.response_size - len;
4170 }
4171
4172 /* Truncate if response size is smaller than the "natural" size */
4173 if( opt.response_size != DFL_RESPONSE_SIZE &&
4174 len > opt.response_size )
4175 {
4176 len = opt.response_size;
4177
4178 /* Still end with \r\n unless that's really not possible */
4179 if( len >= 2 ) buf[len - 2] = '\r';
4180 if( len >= 1 ) buf[len - 1] = '\n';
4181 }
4182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004184 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004185 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004188 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02004189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004190 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192 mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004193 goto reset;
4194 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004195
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02004196 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004199 goto reset;
4200 }
Hanno Becker16970d22017-10-10 15:56:37 +01004201
4202 /* For event-driven IO, wait for socket to become available */
4203 if( opt.event == 1 /* level triggered IO */ )
4204 {
4205#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00004206 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01004207#else
Hanno Becker197a91c2017-10-31 10:58:53 +00004208 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01004209#endif
4210 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02004211 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004212 }
4213 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004214 else /* Not stream, so datagram */
4215 {
Hanno Becker16970d22017-10-10 15:56:37 +01004216 while( 1 )
4217 {
4218 ret = mbedtls_ssl_write( &ssl, buf, len );
4219
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02004220 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01004221 break;
4222
4223 /* For event-driven IO, wait for socket to become available */
4224 if( opt.event == 1 /* level triggered IO */ )
4225 {
4226#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00004227 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01004228#else
Hanno Becker197a91c2017-10-31 10:58:53 +00004229 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01004230#endif
4231 }
4232 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004233
4234 if( ret < 0 )
4235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004236 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004237 goto reset;
4238 }
4239
4240 frags = 1;
4241 written = ret;
4242 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004243
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02004244 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004245 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Manuel Pégourié-Gonnarda92ed482015-01-14 10:46:08 +01004246 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01004247
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02004248 /*
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004249 * 7b. Simulate serialize/deserialize and go back to data exchange
4250 */
Jarno Lamsabbc7b412019-06-04 11:06:31 +03004251#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004252 if( opt.serialize != 0 )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004253 {
Jarno Lamsa15b3a7a2019-06-06 15:10:07 +03004254 size_t buf_len;
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004255
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004256 mbedtls_printf( " . Serializing live connection..." );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004257
Jarno Lamsa15b3a7a2019-06-06 15:10:07 +03004258 ret = mbedtls_ssl_context_save( &ssl, NULL, 0, &buf_len );
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004259 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004260 {
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004261 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
Kenneth Soerensen518d4352020-04-01 17:22:45 +02004262 "-0x%x\n\n", (unsigned int) -ret );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004263
4264 goto exit;
4265 }
4266
Jarno Lamsa15b3a7a2019-06-06 15:10:07 +03004267 if( ( context_buf = mbedtls_calloc( 1, buf_len ) ) == NULL )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004268 {
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004269 mbedtls_printf( " failed\n ! Couldn't allocate buffer for "
4270 "serialized context" );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004271
4272 goto exit;
4273 }
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02004274 context_buf_len = buf_len;
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004275
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004276 if( ( ret = mbedtls_ssl_context_save( &ssl, context_buf,
4277 buf_len, &buf_len ) ) != 0 )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004278 {
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004279 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
Kenneth Soerensen518d4352020-04-01 17:22:45 +02004280 "-0x%x\n\n", (unsigned int) -ret );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004281
4282 goto exit;
4283 }
4284
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004285 mbedtls_printf( " ok\n" );
4286
Piotr Nowicki3de298f2020-04-16 14:35:19 +02004287 /* Save serialized context to the 'opt.context_file' as a base64 code */
4288 if( 0 < strlen( opt.context_file ) )
4289 {
4290 FILE *b64_file;
4291 uint8_t *b64_buf;
4292 size_t b64_len;
4293
4294 mbedtls_printf( " . Save serialized context to a file... " );
4295
4296 mbedtls_base64_encode( NULL, 0, &b64_len, context_buf, buf_len );
4297
4298 if( ( b64_buf = mbedtls_calloc( 1, b64_len ) ) == NULL )
4299 {
4300 mbedtls_printf( "failed\n ! Couldn't allocate buffer for "
4301 "the base64 code\n" );
4302 goto exit;
4303 }
4304
4305 if( ( ret = mbedtls_base64_encode( b64_buf, b64_len, &b64_len,
4306 context_buf, buf_len ) ) != 0 )
4307 {
4308 mbedtls_printf( "failed\n ! mbedtls_base64_encode returned "
Kenneth Soerensen518d4352020-04-01 17:22:45 +02004309 "-0x%x\n", (unsigned int) -ret );
Piotr Nowicki3de298f2020-04-16 14:35:19 +02004310 mbedtls_free( b64_buf );
4311 goto exit;
4312 }
4313
4314 if( ( b64_file = fopen( opt.context_file, "w" ) ) == NULL )
4315 {
4316 mbedtls_printf( "failed\n ! Cannot open '%s' for writing.\n",
4317 opt.context_file );
4318 mbedtls_free( b64_buf );
4319 goto exit;
4320 }
4321
4322 if( b64_len != fwrite( b64_buf, 1, b64_len, b64_file ) )
4323 {
4324 mbedtls_printf( "failed\n ! fwrite(%ld bytes) failed\n",
4325 (long) b64_len );
4326 mbedtls_free( b64_buf );
4327 fclose( b64_file );
4328 goto exit;
4329 }
4330
4331 mbedtls_free( b64_buf );
4332 fclose( b64_file );
4333
4334 mbedtls_printf( "ok\n" );
4335 }
4336
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004337 /*
4338 * This simulates a workflow where you have a long-lived server
4339 * instance, potentially with a pool of ssl_context objects, and you
4340 * just want to re-use one while the connection is inactive: in that
4341 * case you can just reset() it, and then it's ready to receive
4342 * serialized data from another connection (or the same here).
4343 */
4344 if( opt.serialize == 1 )
4345 {
Manuel Pégourié-Gonnard9df5a822019-07-23 14:51:09 +02004346 /* nothing to do here, done by context_save() already */
Piotr Nowicki136bebf2020-04-17 14:37:00 +02004347 mbedtls_printf( " . Context has been reset... ok\n" );
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004348 }
4349
4350 /*
4351 * This simulates a workflow where you have one server instance per
4352 * connection, and want to release it entire when the connection is
4353 * inactive, and spawn it again when needed again - this would happen
4354 * between ssl_free() and ssl_init() below, together with any other
4355 * teardown/startup code needed - for example, preparing the
4356 * ssl_config again (see section 3 "setup stuff" in this file).
4357 */
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004358 if( opt.serialize == 2 )
4359 {
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004360 mbedtls_printf( " . Freeing and reinitializing context..." );
4361
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004362 mbedtls_ssl_free( &ssl );
4363
4364 mbedtls_ssl_init( &ssl );
4365
4366 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
4367 {
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004368 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned "
Kenneth Soerensen518d4352020-04-01 17:22:45 +02004369 "-0x%x\n\n", (unsigned int) -ret );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004370 goto exit;
4371 }
4372
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004373 /*
4374 * This illustrates the minimum amount of things you need to set
4375 * up, however you could set up much more if desired, for example
4376 * if you want to share your set up code between the case of
4377 * establishing a new connection and this case.
4378 */
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004379 if( opt.nbio == 2 )
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004380 mbedtls_ssl_set_bio( &ssl, &client_fd, delayed_send,
4381 delayed_recv, NULL );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004382 else
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004383 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send,
4384 mbedtls_net_recv,
4385 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004386
Jarno Lamsa8e253212019-06-13 11:45:06 +03004387#if defined(MBEDTLS_TIMING_C)
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004388 mbedtls_ssl_set_timer_cb( &ssl, &timer,
4389 mbedtls_timing_set_delay,
4390 mbedtls_timing_get_delay );
Jarno Lamsa8e253212019-06-13 11:45:06 +03004391#endif /* MBEDTLS_TIMING_C */
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004392
4393 mbedtls_printf( " ok\n" );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004394 }
4395
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004396 mbedtls_printf( " . Deserializing connection..." );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004397
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004398 if( ( ret = mbedtls_ssl_context_load( &ssl, context_buf,
4399 buf_len ) ) != 0 )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004400 {
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004401 mbedtls_printf( "failed\n ! mbedtls_ssl_context_load returned "
Kenneth Soerensen518d4352020-04-01 17:22:45 +02004402 "-0x%x\n\n", (unsigned int) -ret );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004403
4404 goto exit;
4405 }
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004406
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02004407 mbedtls_free( context_buf );
4408 context_buf = NULL;
4409 context_buf_len = 0;
4410
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004411 mbedtls_printf( " ok\n" );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004412 }
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004413#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004414
4415 /*
4416 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02004417 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00004418 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02004419 goto data_exchange;
4420
4421 /*
4422 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02004423 */
4424close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004425 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01004426
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02004427 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004428 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01004429 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02004430 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02004431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432 mbedtls_printf( " done\n" );
Rich Evansf90016a2015-01-19 14:26:37 +00004433
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004434 goto reset;
4435
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02004436 /*
4437 * Cleanup and exit
4438 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004439exit:
gabor-mezei-arma9eecf12020-07-07 11:11:02 +02004440#ifdef MBEDTLS_ERROR_C
gabor-mezei-armde472172020-09-04 14:44:25 +02004441 if( ret != 0 )
4442 {
4443 char error_buf[100];
4444 mbedtls_strerror( ret, error_buf, 100 );
4445 mbedtls_printf("Last error was: -0x%X - %s\n\n", (unsigned int) -ret, error_buf );
4446 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004447#endif
4448
gabor-mezei-armde472172020-09-04 14:44:25 +02004449 if( opt.query_config_mode == DFL_QUERY_CONFIG_MODE )
4450 {
gabor-mezei-arma9eecf12020-07-07 11:11:02 +02004451 mbedtls_printf( " . Cleaning up..." );
4452 fflush( stdout );
4453 }
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01004454
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02004455 mbedtls_net_free( &client_fd );
4456 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +02004457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004458#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
4459 mbedtls_dhm_free( &dhm );
Paul Bakkera317a982014-06-18 16:44:11 +02004460#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461#if defined(MBEDTLS_X509_CRT_PARSE_C)
4462 mbedtls_x509_crt_free( &cacert );
4463 mbedtls_x509_crt_free( &srvcert );
4464 mbedtls_pk_free( &pkey );
4465 mbedtls_x509_crt_free( &srvcert2 );
4466 mbedtls_pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02004467#endif
Gilles Peskine44817442018-06-13 18:09:28 +02004468#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
4469 for( i = 0; (size_t) i < ssl_async_keys.slots_used; i++ )
4470 {
4471 if( ssl_async_keys.slots[i].pk_owned )
4472 {
4473 mbedtls_pk_free( ssl_async_keys.slots[i].pk );
4474 mbedtls_free( ssl_async_keys.slots[i].pk );
4475 ssl_async_keys.slots[i].pk = NULL;
4476 }
4477 }
4478#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02004479#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01004480 sni_free( sni_info );
4481#endif
Gilles Peskineeccd8882020-03-10 12:19:08 +01004482#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
gabor-mezei-armde472172020-09-04 14:44:25 +02004483 ret = psk_free( psk_info );
4484 if( ( ret != 0 ) && ( opt.query_config_mode == DFL_QUERY_CONFIG_MODE ) )
4485 mbedtls_printf( "Failed to list of opaque PSKs - error was %d\n", ret );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02004486#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004487#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
4488 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02004489#endif
Paul Bakkered27a042013-04-18 22:46:23 +02004490
Gilles Peskineeccd8882020-03-10 12:19:08 +01004491#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) && \
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004492 defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00004493 if( opt.psk_opaque != 0 )
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004494 {
4495 /* This is ok even if the slot hasn't been
4496 * initialized (we might have jumed here
4497 * immediately because of bad cmd line params,
4498 * for example). */
Hanno Becker1d911cd2018-11-15 13:06:09 +00004499 status = psa_destroy_key( psk_slot );
gabor-mezei-armde472172020-09-04 14:44:25 +02004500 if( ( status != PSA_SUCCESS ) &&
4501 ( opt.query_config_mode == DFL_QUERY_CONFIG_MODE ) )
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004502 {
Ronald Cronadc2ff22020-09-16 16:49:27 +02004503 mbedtls_printf( "Failed to destroy key slot %u - error was %d",
Ronald Cronc9851142020-11-13 10:08:52 +01004504 (unsigned) psk_slot, (int) status );
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004505 }
4506 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01004507#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED &&
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004508 MBEDTLS_USE_PSA_CRYPTO */
4509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02004511 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512 mbedtls_ctr_drbg_free( &ctr_drbg );
4513 mbedtls_entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004515#if defined(MBEDTLS_SSL_CACHE_C)
4516 mbedtls_ssl_cache_free( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00004517#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004518#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4519 mbedtls_ssl_ticket_free( &ticket_ctx );
4520#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004521#if defined(MBEDTLS_SSL_COOKIE_C)
4522 mbedtls_ssl_cookie_free( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02004523#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004524
Hanno Becker095d9cf2018-10-09 12:39:13 +01004525 mbedtls_free( buf );
4526
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02004527#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
4528 if( context_buf != NULL )
4529 mbedtls_platform_zeroize( context_buf, context_buf_len );
4530 mbedtls_free( context_buf );
4531#endif
4532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004533#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
4534#if defined(MBEDTLS_MEMORY_DEBUG)
4535 mbedtls_memory_buffer_alloc_status();
Paul Bakker82024bf2013-07-04 11:52:32 +02004536#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004537 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +02004538#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02004539
gabor-mezei-arma9eecf12020-07-07 11:11:02 +02004540 if( opt.query_config_mode == DFL_QUERY_CONFIG_MODE )
4541 {
4542 mbedtls_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01004543
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004544#if defined(_WIN32)
gabor-mezei-arma9eecf12020-07-07 11:11:02 +02004545 mbedtls_printf( " + Press Enter to exit this program.\n" );
4546 fflush( stdout ); getchar();
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004547#endif
gabor-mezei-arma9eecf12020-07-07 11:11:02 +02004548 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004549
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02004550 // Shell can not handle large exit numbers -> 1 for errors
4551 if( ret < 0 )
4552 ret = 1;
4553
gabor-mezei-arma9eecf12020-07-07 11:11:02 +02004554 if( opt.query_config_mode == DFL_QUERY_CONFIG_MODE )
4555 mbedtls_exit( ret );
4556 else
4557 mbedtls_exit( query_config_ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004558}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004559#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
4560 MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02004561 MBEDTLS_CTR_DRBG_C */