blob: 1d03fe283b06899c0f8afe9214103d42ceaa4a19 [file] [log] [blame]
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001/*
2 * SSL client with options
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb60b95f2012-09-25 09:05:17 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020031#include <stdio.h>
Simon Butcherd3138c32016-04-27 01:26:50 +010032#include <stdlib.h>
Hanno Beckerd6d100b2019-03-30 06:27:43 +000033#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#define mbedtls_free free
Simon Butcherd3138c32016-04-27 01:26:50 +010035#define mbedtls_time time
36#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020037#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#define mbedtls_fprintf fprintf
39#define mbedtls_printf printf
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050040#define mbedtls_exit exit
41#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
42#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evans18b78c72015-02-11 14:06:19 +000043#endif
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020044
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020045#if !defined(MBEDTLS_ENTROPY_C) || \
46 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020047 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020048int main( void )
49{
50 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
51 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020052 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020053 return( 0 );
54}
55#else
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010056
Andres AG788aa4a2016-09-14 14:32:09 +010057#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/ssl.h"
59#include "mbedtls/entropy.h"
60#include "mbedtls/ctr_drbg.h"
61#include "mbedtls/certs.h"
62#include "mbedtls/x509.h"
63#include "mbedtls/error.h"
64#include "mbedtls/debug.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020065#include "mbedtls/timing.h"
Piotr Nowicki3de298f2020-04-16 14:35:19 +020066#include "mbedtls/base64.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000067
Hanno Becker5a9942e2018-11-12 17:47:48 +000068#if defined(MBEDTLS_USE_PSA_CRYPTO)
69#include "psa/crypto.h"
Hanno Becker1d911cd2018-11-15 13:06:09 +000070#include "mbedtls/psa_util.h"
Hanno Becker5a9942e2018-11-12 17:47:48 +000071#endif
72
Rich Evans18b78c72015-02-11 14:06:19 +000073#include <stdio.h>
74#include <stdlib.h>
75#include <string.h>
Andres AG692ad842017-01-19 16:30:57 +000076#include <stdint.h>
Andres AG0b736db2017-02-08 14:05:57 +000077
78#if !defined(_MSC_VER)
Andres AG692ad842017-01-19 16:30:57 +000079#include <inttypes.h>
Andres AG0b736db2017-02-08 14:05:57 +000080#endif
Rich Evans18b78c72015-02-11 14:06:19 +000081
82#if !defined(_WIN32)
83#include <signal.h>
84#endif
85
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000087#include "mbedtls/ssl_cache.h"
Paul Bakker0a597072012-09-25 21:55:46 +000088#endif
89
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020090#if defined(MBEDTLS_SSL_TICKET_C)
91#include "mbedtls/ssl_ticket.h"
92#endif
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000095#include "mbedtls/ssl_cookie.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020096#endif
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000099#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker82024bf2013-07-04 11:52:32 +0200100#endif
101
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200102#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO)
103#define SNI_OPTION
104#endif
105
106#if defined(_WIN32)
107#include <windows.h>
108#endif
109
Simon Butchercce68be2018-07-23 14:26:09 +0100110/* Size of memory to be allocated for the heap, when using the library's memory
111 * management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */
112#define MEMORY_HEAP_SIZE 120000
113
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100114#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200115#define DFL_SERVER_PORT "4433"
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200116#define DFL_RESPONSE_SIZE -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000117#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100118#define DFL_NBIO 0
Hanno Becker16970d22017-10-10 15:56:37 +0100119#define DFL_EVENT 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200120#define DFL_READ_TIMEOUT 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000121#define DFL_CA_FILE ""
122#define DFL_CA_PATH ""
123#define DFL_CRT_FILE ""
124#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200125#define DFL_CRT_FILE2 ""
126#define DFL_KEY_FILE2 ""
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100127#define DFL_ASYNC_OPERATIONS "-"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100128#define DFL_ASYNC_PRIVATE_DELAY1 ( -1 )
129#define DFL_ASYNC_PRIVATE_DELAY2 ( -1 )
Gilles Peskine3665f1d2018-01-05 21:22:12 +0100130#define DFL_ASYNC_PRIVATE_ERROR ( 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +0200131#define DFL_PSK ""
Hanno Becker1d911cd2018-11-15 13:06:09 +0000132#define DFL_PSK_OPAQUE 0
133#define DFL_PSK_LIST_OPAQUE 0
Paul Bakkerfbb17802013-04-17 19:10:21 +0200134#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200135#define DFL_ECJPAKE_PW NULL
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200136#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000137#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200138#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100140#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100141#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200142#define DFL_RENEGO_DELAY -2
Andres AG692ad842017-01-19 16:30:57 +0000143#define DFL_RENEGO_PERIOD ( (uint64_t)-1 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200144#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200145#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200146#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000147#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +0200148#define DFL_SHA1 -1
Hanno Beckera7d25422019-04-09 17:28:10 +0100149#define DFL_CID_ENABLED 0
150#define DFL_CID_VALUE ""
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100151#define DFL_CID_ENABLED_RENEGO -1
152#define DFL_CID_VALUE_RENEGO NULL
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100153#define DFL_AUTH_MODE -1
Janos Follath4817e272017-04-10 13:44:33 +0100154#define DFL_CERT_REQ_CA_LIST MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100156#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200158#define DFL_TICKET_TIMEOUT 86400
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100159#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100160#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100161#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200162#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100163#define DFL_CURVES NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200164#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200166#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200167#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200168#define DFL_HS_TO_MIN 0
169#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200170#define DFL_DTLS_MTU -1
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200171#define DFL_BADMAC_LIMIT -1
Hanno Beckere7675d02018-08-14 13:28:56 +0100172#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200173#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100174#define DFL_ETM -1
Jarno Lamsa9831c8a2019-05-29 13:33:32 +0300175#define DFL_SERIALIZE 0
Piotr Nowicki3de298f2020-04-16 14:35:19 +0200176#define DFL_CONTEXT_FILE ""
Jarno Lamsa9831c8a2019-05-29 13:33:32 +0300177#define DFL_EXTENDED_MS_ENFORCE -1
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200178#define DFL_CA_CALLBACK 0
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300179#define DFL_EAP_TLS 0
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200180#define DFL_REPRODUCIBLE 0
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100181#define DFL_NSS_KEYLOG 0
182#define DFL_NSS_KEYLOG_FILE NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000183
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200184#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
185 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
186 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
187 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
188 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
189 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
190 "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 +0200191
Paul Bakker8e714d72013-07-18 11:05:13 +0200192/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
193 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000194#define HTTP_RESPONSE \
195 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000196 "<h2>mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200197 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000198
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200199/*
200 * Size of the basic I/O buffer. Able to hold our default response.
201 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 * 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 +0200203 * if you change this value to something outside the range <= 100 or > 500
204 */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200205#define DFL_IO_BUF_LEN 200
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#if defined(MBEDTLS_X509_CRT_PARSE_C)
208#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000209#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100210 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
211 " default: \"\" (pre-loaded)\n" \
Hanno Becker422d1992019-05-01 11:16:28 +0100212 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100213 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
214 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
Hanno Becker422d1992019-05-01 11:16:28 +0100215 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100216 " 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 +0200217 " default: see note after key_file2\n" \
218 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200219 " 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 +0200220 " default: see note after key_file2\n" \
221 " key_file2=%%s default: see note below\n" \
222 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200223 " preloaded certificate(s) and key(s) are used if available\n" \
224 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
225 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000226#else
227#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200228 "\n" \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 " No file operations available (MBEDTLS_FS_IO not defined)\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200230 "\n"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200232#else
233#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200235
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200236#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100237#define USAGE_SSL_ASYNC \
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100238 " async_operations=%%c... d=decrypt, s=sign (default: -=off)\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100239 " async_private_delay1=%%d Asynchronous delay for key_file or preloaded key\n" \
Gilles Peskine166ce742018-04-30 10:30:49 +0200240 " async_private_delay2=%%d Asynchronous delay for key_file2 and sni\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100241 " default: -1 (not asynchronous)\n" \
Gilles Peskine60ee4ca2018-01-08 11:28:05 +0100242 " async_private_error=%%d Async callback error injection (default=0=none,\n" \
Gilles Peskinec9125722018-04-26 07:15:40 +0200243 " 1=start, 2=cancel, 3=resume, negative=first time only)"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100244#else
245#define USAGE_SSL_ASYNC ""
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200246#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100247
Hanno Beckera0e20d02019-05-15 14:03:01 +0100248#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +0100249#define USAGE_CID \
250 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
251 " default: 0 (disabled)\n" \
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100252 " 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 +0100253 " default: same as 'cid' parameter\n" \
Hanno Beckera7d25422019-04-09 17:28:10 +0100254 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100255 " default: \"\"\n" \
256 " 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 +0100257 " default: same as 'cid_val' parameter\n"
Hanno Beckera0e20d02019-05-15 14:03:01 +0100258#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +0100259#define USAGE_CID ""
Hanno Beckera0e20d02019-05-15 14:03:01 +0100260#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +0100261
Gilles Peskineeccd8882020-03-10 12:19:08 +0100262#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100263#define USAGE_PSK_RAW \
Piotr Nowicki9926eaf2019-11-20 14:54:36 +0100264 " psk=%%s default: \"\" (disabled)\n" \
265 " The PSK values are in hex, without 0x.\n" \
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100266 " psk_list=%%s default: \"\"\n" \
Andrzej Kurekb274f272019-02-05 05:06:35 -0500267 " A list of (PSK identity, PSK value) pairs.\n" \
268 " The PSK values are in hex, without 0x.\n" \
269 " id1,psk1[,id2,psk2[,...]]\n" \
270 " psk_identity=%%s default: \"Client_identity\"\n"
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100271#if defined(MBEDTLS_USE_PSA_CRYPTO)
272#define USAGE_PSK_SLOT \
Hanno Becker1d911cd2018-11-15 13:06:09 +0000273 " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
274 " Enable this to store the PSK configured through command line\n" \
275 " parameter `psk` in a PSA-based key slot.\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" \
Hanno Becker1d911cd2018-11-15 13:06:09 +0000282 " psk_list_opaque=%%d default: 0 (don't use opaque dynamic PSKs)\n" \
283 " Enable this to store the list of dynamically chosen PSKs configured\n" \
284 " through the command line parameter `psk_list` in PSA-based key slots.\n" \
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100285 " Note: Currently only supported in conjunction with\n" \
286 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
287 " to force a particular PSK-only ciphersuite.\n" \
288 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
289 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
290 " with prepopulated key slots instead of importing raw key material.\n"
291#else
292#define USAGE_PSK_SLOT ""
293#endif /* MBEDTLS_USE_PSA_CRYPTO */
294#define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
Paul Bakkered27a042013-04-18 22:46:23 +0200295#else
296#define USAGE_PSK ""
Gilles Peskineeccd8882020-03-10 12:19:08 +0100297#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200298#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
299#define USAGE_CA_CALLBACK \
300 " ca_callback=%%d default: 0 (disabled)\n" \
301 " Enable this to use the trusted certificate callback function\n"
302#else
303#define USAGE_CA_CALLBACK ""
304#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200306#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100307 " tickets=%%d default: 1 (enabled)\n" \
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200308 " ticket_timeout=%%d default: 86400 (one day)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200309#else
310#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200312
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300313#if defined(MBEDTLS_SSL_EXPORT_KEYS)
314#define USAGE_EAP_TLS \
315 " eap_tls=%%d default: 0 (disabled)\n"
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100316#define USAGE_NSS_KEYLOG \
Hanno Beckerbc5308c2019-09-09 11:38:51 +0100317 " nss_keylog=%%d default: 0 (disabled)\n" \
318 " This cannot be used with eap_tls=1\n"
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100319#define USAGE_NSS_KEYLOG_FILE \
320 " nss_keylog_file=%%s\n"
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300321#else
322#define USAGE_EAP_TLS ""
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100323#define USAGE_NSS_KEYLOG ""
324#define USAGE_NSS_KEYLOG_FILE ""
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300325#endif /* MBEDTLS_SSL_EXPORT_KEYS */
326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100328#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100329 " cache_max=%%d default: cache default (50)\n" \
330 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100331#else
332#define USAGE_CACHE ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#endif /* MBEDTLS_SSL_CACHE_C */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100334
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200335#if defined(SNI_OPTION)
Ron Eldor80d04192019-04-04 15:02:01 +0300336#if defined(MBEDTLS_X509_CRL_PARSE_C)
337#define SNI_CRL ",crl"
338#else
339#define SNI_CRL ""
340#endif
341
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100342#define USAGE_SNI \
Ron Eldor80d04192019-04-04 15:02:01 +0300343 " sni=%%s name1,cert1,key1,ca1"SNI_CRL",auth1[,...]\n" \
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200344 " default: disabled\n"
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100345#else
346#define USAGE_SNI ""
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200347#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200350#define USAGE_MAX_FRAG_LEN \
351 " max_frag_len=%%d default: 16384 (tls default)\n" \
352 " options: 512, 1024, 2048, 4096\n"
353#else
354#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100358#define USAGE_TRUNC_HMAC \
359 " trunc_hmac=%%d default: library default\n"
360#else
361#define USAGE_TRUNC_HMAC ""
362#endif
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200365#define USAGE_ALPN \
366 " alpn=%%s default: \"\" (disabled)\n" \
367 " example: spdy/1,http/1.1\n"
368#else
369#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200373#define USAGE_COOKIES \
374 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200375 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200376#else
377#define USAGE_COOKIES ""
378#endif
379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200381#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200382 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200383#else
384#define USAGE_ANTI_REPLAY ""
385#endif
386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200388#define USAGE_BADMAC_LIMIT \
389 " badmac_limit=%%d default: (library default: disabled)\n"
390#else
391#define USAGE_BADMAC_LIMIT ""
392#endif
393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200395#define USAGE_DTLS \
396 " dtls=%%d default: 0 (TLS)\n" \
397 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200398 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Beckere7675d02018-08-14 13:28:56 +0100399 " mtu=%%d default: (library default: unlimited)\n" \
400 " dgram_packing=%%d default: 1 (allowed)\n" \
401 " allow or forbid packing of multiple\n" \
402 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200403#else
404#define USAGE_DTLS ""
405#endif
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200408#define USAGE_EMS \
409 " extended_ms=0/1 default: (library default: on)\n"
410#else
411#define USAGE_EMS ""
412#endif
413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100415#define USAGE_ETM \
416 " etm=0/1 default: (library default: on)\n"
417#else
418#define USAGE_ETM ""
419#endif
420
Philippe Antoine738153a2019-06-18 20:16:43 +0200421#define USAGE_REPRODUCIBLE \
422 " reproducible=0/1 default: 0 (disabled)\n"
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100425#define USAGE_RENEGO \
426 " renegotiation=%%d default: 0 (disabled)\n" \
427 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100428 " renego_delay=%%d default: -2 (library default)\n" \
Andres AG692ad842017-01-19 16:30:57 +0000429 " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100430#else
431#define USAGE_RENEGO ""
432#endif
433
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200434#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
435#define USAGE_ECJPAKE \
436 " ecjpake_pw=%%s default: none (disabled)\n"
437#else
438#define USAGE_ECJPAKE ""
439#endif
440
Hanno Beckere6706e62017-05-15 16:05:15 +0100441#if defined(MBEDTLS_ECP_C)
442#define USAGE_CURVES \
443 " curves=a,b,c,d default: \"default\" (library default)\n" \
444 " example: \"secp521r1,brainpoolP512r1\"\n" \
445 " - use \"none\" for empty list\n" \
446 " - see mbedtls_ecp_curve_list()\n" \
447 " for acceptable curve names\n"
448#else
449#define USAGE_CURVES ""
450#endif
451
Jarno Lamsabbc7b412019-06-04 11:06:31 +0300452#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
453#define USAGE_SERIALIZATION \
Piotr Nowicki3de298f2020-04-16 14:35:19 +0200454 " serialize=%%d default: 0 (do not serialize/deserialize)\n" \
455 " options: 1 (serialize)\n" \
456 " 2 (serialize with re-initialization)\n" \
457 " context_file=%%s The file path to write a serialized connection\n"\
458 " in the form of base64 code (serialize option\n" \
459 " must be set)\n" \
460 " default: \"\" (do nothing)\n" \
461 " option: a file path\n"
Jarno Lamsabbc7b412019-06-04 11:06:31 +0300462#else
463#define USAGE_SERIALIZATION ""
464#endif
465
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000466#define USAGE \
467 "\n usage: ssl_server2 param=<>...\n" \
468 "\n acceptable parameters:\n" \
Ron Eldor71f68c42017-09-26 11:29:11 +0300469 " server_addr=%%s default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000470 " server_port=%%d default: 4433\n" \
471 " debug_level=%%d default: 0 (disabled)\n" \
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200472 " buffer_size=%%d default: 200 \n" \
473 " (minimum: 1, max: 16385)\n" \
474 " response_size=%%d default: about 152 (basic response)\n" \
475 " (minimum: 0, max: 16384)\n" \
476 " increases buffer_size if bigger\n"\
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100477 " nbio=%%d default: 0 (blocking I/O)\n" \
478 " options: 1 (non-blocking), 2 (added delays)\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100479 " event=%%d default: 0 (loop)\n" \
480 " options: 1 (level-triggered, implies nbio=1),\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200481 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100482 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200483 USAGE_DTLS \
484 USAGE_COOKIES \
485 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200486 USAGE_BADMAC_LIMIT \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200487 "\n" \
Manuel Pégourié-Gonnardee6139c2015-05-07 10:18:26 +0100488 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100489 " options: none, optional, required\n" \
Janos Follath4817e272017-04-10 13:44:33 +0100490 " cert_req_ca_list=%%d default: 1 (send ca list)\n" \
491 " options: 1 (send ca list), 0 (don't send)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000492 USAGE_IO \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100493 USAGE_SSL_ASYNC \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100494 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100495 "\n" \
496 USAGE_PSK \
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200497 USAGE_CA_CALLBACK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200498 USAGE_ECJPAKE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100499 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100500 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100501 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200502 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200503 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100504 USAGE_TICKETS \
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300505 USAGE_EAP_TLS \
Philippe Antoine738153a2019-06-18 20:16:43 +0200506 USAGE_REPRODUCIBLE \
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100507 USAGE_NSS_KEYLOG \
508 USAGE_NSS_KEYLOG_FILE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100509 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100510 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100511 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200512 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200513 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100514 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100515 USAGE_CURVES \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100516 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100517 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200518 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200519 " min_version=%%s default: (library default: tls1)\n" \
520 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200521 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100522 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200523 "\n" \
524 " version_suites=a,b,c,d per-version ciphersuites\n" \
525 " in order from ssl3 to tls1_2\n" \
526 " default: all enabled\n" \
527 " force_ciphersuite=<name> default: all enabled\n" \
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100528 " query_config=<name> return 0 if the specified\n" \
529 " configuration macro is defined and 1\n" \
530 " otherwise. The expansion of the macro\n" \
531 " is printed if it is defined\n" \
Jarno Lamsabbc7b412019-06-04 11:06:31 +0300532 USAGE_SERIALIZATION \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000533 " acceptable ciphersuite names:\n"
534
Hanno Becker8651a432017-06-09 16:13:22 +0100535#define ALPN_LIST_SIZE 10
536#define CURVE_LIST_SIZE 20
537
Andres AG692ad842017-01-19 16:30:57 +0000538#define PUT_UINT64_BE(out_be,in_le,i) \
539{ \
540 (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \
541 (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \
542 (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \
543 (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \
544 (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \
545 (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \
546 (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \
547 (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \
548}
549
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500550
Rich Evans85b05ec2015-02-12 11:37:29 +0000551/*
552 * global options
553 */
554struct options
555{
556 const char *server_addr; /* address on which the ssl service runs */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200557 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000558 int debug_level; /* level of debugging */
559 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100560 int event; /* loop or event-driven IO? level or edge triggered? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200562 int response_size; /* pad response with header to requested size */
563 uint16_t buffer_size; /* IO buffer size */
Rich Evans85b05ec2015-02-12 11:37:29 +0000564 const char *ca_file; /* the file with the CA certificate(s) */
565 const char *ca_path; /* the path with the CA certificate(s) reside */
566 const char *crt_file; /* the file with the server certificate */
567 const char *key_file; /* the file with the server key */
568 const char *crt_file2; /* the file with the 2nd server certificate */
569 const char *key_file2; /* the file with the 2nd server key */
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100570 const char *async_operations; /* supported SSL asynchronous operations */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100571 int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
572 int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
573 int async_private_error; /* inject error in async private callback */
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100574#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000575 int psk_opaque;
576 int psk_list_opaque;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100577#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200578#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000579 int ca_callback; /* Use callback for trusted certificate list */
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200580#endif
Rich Evans85b05ec2015-02-12 11:37:29 +0000581 const char *psk; /* the pre-shared key */
582 const char *psk_identity; /* the pre-shared key identity */
583 char *psk_list; /* list of PSK id/key pairs for callback */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200584 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000585 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
586 const char *version_suites; /* per-version ciphersuites */
587 int renegotiation; /* enable / disable renegotiation */
588 int allow_legacy; /* allow legacy renegotiation */
589 int renegotiate; /* attempt renegotiation? */
590 int renego_delay; /* delay before enforcing renegotiation */
Andres AG692ad842017-01-19 16:30:57 +0000591 uint64_t renego_period; /* period for automatic renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000592 int exchanges; /* number of data exchanges */
593 int min_version; /* minimum protocol version accepted */
594 int max_version; /* maximum protocol version accepted */
595 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200596 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000597 int auth_mode; /* verify mode for connection */
Janos Follath4817e272017-04-10 13:44:33 +0100598 int cert_req_ca_list; /* should we send the CA list? */
Rich Evans85b05ec2015-02-12 11:37:29 +0000599 unsigned char mfl_code; /* code for maximum fragment length */
600 int trunc_hmac; /* accept truncated hmac? */
601 int tickets; /* enable / disable session tickets */
602 int ticket_timeout; /* session ticket lifetime */
603 int cache_max; /* max number of session cache entries */
604 int cache_timeout; /* expiration delay of session cache entries */
605 char *sni; /* string describing sni information */
Hanno Beckere6706e62017-05-15 16:05:15 +0100606 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000607 const char *alpn_string; /* ALPN supported protocols */
608 const char *dhm_file; /* the file with the DH parameters */
609 int extended_ms; /* allow negotiation of extended MS? */
610 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000611 int transport; /* TLS or DTLS? */
612 int cookies; /* Use cookies for DTLS? -1 to break them */
613 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
614 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
615 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200616 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Hanno Beckere7675d02018-08-14 13:28:56 +0100617 int dgram_packing; /* allow/forbid datagram packing */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000618 int badmac_limit; /* Limit of records with bad MAC */
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300619 int eap_tls; /* derive EAP-TLS keying material? */
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100620 int nss_keylog; /* export NSS key log material */
621 const char *nss_keylog_file; /* NSS key log file */
Hanno Beckera7d25422019-04-09 17:28:10 +0100622 int cid_enabled; /* whether to use the CID extension or not */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100623 int cid_enabled_renego; /* whether to use the CID extension or not
624 * during renegotiation */
Hanno Beckera7d25422019-04-09 17:28:10 +0100625 const char *cid_val; /* the CID to use for incoming messages */
Jarno Lamsa9831c8a2019-05-29 13:33:32 +0300626 int serialize; /* serialize/deserialize connection */
Piotr Nowicki3de298f2020-04-16 14:35:19 +0200627 const char *context_file; /* the file to write a serialized connection
628 * in the form of base64 code (serialize
629 * option must be set) */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100630 const char *cid_val_renego; /* the CID to use for incoming messages
631 * after renegotiation */
Philippe Antoine154feb22019-06-11 17:50:23 +0200632 int reproducible; /* make communication reproducible */
Rich Evans85b05ec2015-02-12 11:37:29 +0000633} opt;
634
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100635int query_config( const char *config );
636
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300637#if defined(MBEDTLS_SSL_EXPORT_KEYS)
638typedef struct eap_tls_keys
639{
640 unsigned char master_secret[48];
641 unsigned char randbytes[64];
Ron Eldor51d3ab52019-05-12 14:54:30 +0300642 mbedtls_tls_prf_types tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300643} eap_tls_keys;
644
645static int eap_tls_key_derivation ( void *p_expkey,
646 const unsigned char *ms,
647 const unsigned char *kb,
648 size_t maclen,
649 size_t keylen,
650 size_t ivlen,
Jaeden Amero63d813d2019-09-12 10:09:57 +0100651 const unsigned char client_random[32],
652 const unsigned char server_random[32],
Ron Eldor51d3ab52019-05-12 14:54:30 +0300653 mbedtls_tls_prf_types tls_prf_type )
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300654{
655 eap_tls_keys *keys = (eap_tls_keys *)p_expkey;
656
657 ( ( void ) kb );
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300658 memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) );
659 memcpy( keys->randbytes, client_random, 32 );
660 memcpy( keys->randbytes + 32, server_random, 32 );
Ron Eldor51d3ab52019-05-12 14:54:30 +0300661 keys->tls_prf_type = tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300662
Ron Eldorf75e2522019-05-14 20:38:49 +0300663 if( opt.debug_level > 2 )
664 {
Ron Eldor801faf02019-05-15 17:45:24 +0300665 mbedtls_printf("exported maclen is %u\n", (unsigned)maclen);
666 mbedtls_printf("exported keylen is %u\n", (unsigned)keylen);
667 mbedtls_printf("exported ivlen is %u\n", (unsigned)ivlen);
Ron Eldorf75e2522019-05-14 20:38:49 +0300668 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300669 return( 0 );
670}
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100671
672static int nss_keylog_export( void *p_expkey,
673 const unsigned char *ms,
674 const unsigned char *kb,
675 size_t maclen,
676 size_t keylen,
677 size_t ivlen,
Jaeden Amero63d813d2019-09-12 10:09:57 +0100678 const unsigned char client_random[32],
679 const unsigned char server_random[32],
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100680 mbedtls_tls_prf_types tls_prf_type )
681{
682 char nss_keylog_line[ 200 ];
683 size_t const client_random_len = 32;
684 size_t const master_secret_len = 48;
685 size_t len = 0;
686 size_t j;
687 int ret = 0;
688
689 ((void) p_expkey);
690 ((void) kb);
691 ((void) maclen);
692 ((void) keylen);
693 ((void) ivlen);
694 ((void) server_random);
695 ((void) tls_prf_type);
696
697 len += sprintf( nss_keylog_line + len,
698 "%s", "CLIENT_RANDOM " );
699
700 for( j = 0; j < client_random_len; j++ )
701 {
702 len += sprintf( nss_keylog_line + len,
703 "%02x", client_random[j] );
704 }
705
706 len += sprintf( nss_keylog_line + len, " " );
707
708 for( j = 0; j < master_secret_len; j++ )
709 {
710 len += sprintf( nss_keylog_line + len,
711 "%02x", ms[j] );
712 }
713
714 len += sprintf( nss_keylog_line + len, "\n" );
715 nss_keylog_line[ len ] = '\0';
716
717 mbedtls_printf( "\n" );
718 mbedtls_printf( "---------------- NSS KEYLOG -----------------\n" );
719 mbedtls_printf( "%s", nss_keylog_line );
720 mbedtls_printf( "---------------------------------------------\n" );
721
722 if( opt.nss_keylog_file != NULL )
723 {
724 FILE *f;
725
726 if( ( f = fopen( opt.nss_keylog_file, "a" ) ) == NULL )
727 {
728 ret = -1;
729 goto exit;
730 }
731
732 if( fwrite( nss_keylog_line, 1, len, f ) != len )
733 {
734 ret = -1;
k-stachowiakbbc1c692019-09-26 13:36:54 +0200735 fclose( f );
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100736 goto exit;
737 }
738
739 fclose( f );
740 }
741
742exit:
743 mbedtls_platform_zeroize( nss_keylog_line,
744 sizeof( nss_keylog_line ) );
745 return( ret );
746}
747
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300748#endif
749
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200750static void my_debug( void *ctx, int level,
751 const char *file, int line,
752 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000753{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200754 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000755
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200756 /* Extract basename from file */
757 for( p = basename = file; *p != '\0'; p++ )
758 if( *p == '/' || *p == '\\' )
759 basename = p + 1;
760
761 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000762 fflush( (FILE *) ctx );
763}
764
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200765mbedtls_time_t dummy_constant_time( mbedtls_time_t* time )
766{
767 (void) time;
768 return 0x5af2a056;
769}
770
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200771int dummy_entropy( void *data, unsigned char *output, size_t len )
772{
773 size_t i;
Philippe Antoine12e85de2019-06-11 16:07:53 +0200774 int ret;
Philippe Antoined2235f22019-06-11 16:29:28 +0200775 (void) data;
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200776
Philippe Antoine3ca50852019-06-07 22:31:59 +0200777 ret = mbedtls_entropy_func( data, output, len );
Philippe Antoine986b6f22019-06-07 15:04:32 +0200778 for (i = 0; i < len; i++ ) {
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200779 //replace result with pseudo random
780 output[i] = (unsigned char) rand();
781 }
Philippe Antoine3ca50852019-06-07 22:31:59 +0200782 return( ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200783}
784
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200785#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000786int ca_callback( void *data, mbedtls_x509_crt const *child,
787 mbedtls_x509_crt **candidates)
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200788{
Hanno Beckercbb59032019-03-28 14:14:22 +0000789 int ret = 0;
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200790 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +0000791 mbedtls_x509_crt *first;
792
793 /* This is a test-only implementation of the CA callback
794 * which always returns the entire list of trusted certificates.
795 * Production implementations managing a large number of CAs
796 * should use an efficient presentation and lookup for the
797 * set of trusted certificates (such as a hashtable) and only
798 * return those trusted certificates which satisfy basic
799 * parental checks, such as the matching of child `Issuer`
800 * and parent `Subject` field. */
801 ((void) child);
802
803 first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
804 if( first == NULL )
805 {
806 ret = -1;
807 goto exit;
808 }
809 mbedtls_x509_crt_init( first );
810
811 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
812 {
813 ret = -1;
814 goto exit;
815 }
816
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200817 while( ca->next != NULL )
818 {
819 ca = ca->next;
Hanno Beckercbb59032019-03-28 14:14:22 +0000820 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
821 {
822 ret = -1;
823 goto exit;
824 }
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200825 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000826
827exit:
828
829 if( ret != 0 )
830 {
831 mbedtls_x509_crt_free( first );
832 mbedtls_free( first );
833 first = NULL;
834 }
835
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200836 *candidates = first;
Hanno Beckercbb59032019-03-28 14:14:22 +0000837 return( ret );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200838}
839#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
840
Rich Evans85b05ec2015-02-12 11:37:29 +0000841/*
842 * Test recv/send functions that make sure each try returns
843 * WANT_READ/WANT_WRITE at least once before sucesseding
844 */
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100845static int delayed_recv( void *ctx, unsigned char *buf, size_t len )
Rich Evans85b05ec2015-02-12 11:37:29 +0000846{
847 static int first_try = 1;
848 int ret;
849
850 if( first_try )
851 {
852 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100853 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000854 }
855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100857 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000858 first_try = 1; /* Next call will be a new operation */
859 return( ret );
860}
861
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100862static int delayed_send( void *ctx, const unsigned char *buf, size_t len )
Rich Evans85b05ec2015-02-12 11:37:29 +0000863{
864 static int first_try = 1;
865 int ret;
866
867 if( first_try )
868 {
869 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100870 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000871 }
872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100874 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000875 first_try = 1; /* Next call will be a new operation */
876 return( ret );
877}
878
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100879typedef struct
880{
881 mbedtls_ssl_context *ssl;
882 mbedtls_net_context *net;
883} io_ctx_t;
884
Hanno Becker4b6649e2019-07-03 17:14:41 +0100885#if defined(MBEDTLS_SSL_RECORD_CHECKING)
886static int ssl_check_record( mbedtls_ssl_context const *ssl,
887 unsigned char const *buf, size_t len )
888{
889 int ret;
890 unsigned char *tmp_buf;
891
892 /* Record checking may modify the input buffer,
893 * so make a copy. */
894 tmp_buf = mbedtls_calloc( 1, len );
895 if( tmp_buf == NULL )
896 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
897 memcpy( tmp_buf, buf, len );
898
899 ret = mbedtls_ssl_check_record( ssl, tmp_buf, len );
900 if( ret != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE )
901 {
902 int ret_repeated;
903
904 /* Test-only: Make sure that mbedtls_ssl_check_record()
905 * doesn't alter state. */
906 memcpy( tmp_buf, buf, len ); /* Restore buffer */
907 ret_repeated = mbedtls_ssl_check_record( ssl, tmp_buf, len );
908 if( ret != ret_repeated )
909 {
Hanno Becker91f83272019-07-24 13:59:07 +0100910 mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" );
911 return( -1 );
Hanno Becker4b6649e2019-07-03 17:14:41 +0100912 }
913
914 switch( ret )
915 {
916 case 0:
917 break;
918
919 case MBEDTLS_ERR_SSL_INVALID_RECORD:
920 if( opt.debug_level > 1 )
921 mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" );
922 break;
923
924 case MBEDTLS_ERR_SSL_INVALID_MAC:
925 if( opt.debug_level > 1 )
926 mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" );
927 break;
928
929 case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD:
930 if( opt.debug_level > 1 )
931 mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" );
932 break;
933
934 default:
935 mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", -ret );
936 return( -1 );
937 }
938
939 /* Regardless of the outcome, forward the record to the stack. */
940 }
941
Hanno Becker4b6649e2019-07-03 17:14:41 +0100942 mbedtls_free( tmp_buf );
943
944 return( 0 );
945}
946#endif /* MBEDTLS_SSL_RECORD_CHECKING */
947
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100948static int recv_cb( void *ctx, unsigned char *buf, size_t len )
949{
950 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
951 size_t recv_len;
952 int ret;
953
954 if( opt.nbio == 2 )
955 ret = delayed_recv( io_ctx->net, buf, len );
956 else
957 ret = mbedtls_net_recv( io_ctx->net, buf, len );
958 if( ret < 0 )
959 return( ret );
960 recv_len = (size_t) ret;
961
962 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
963 {
964 /* Here's the place to do any datagram/record checking
965 * in between receiving the packet from the underlying
966 * transport and passing it on to the TLS stack. */
Hanno Becker4b6649e2019-07-03 17:14:41 +0100967#if defined(MBEDTLS_SSL_RECORD_CHECKING)
968 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
969 return( -1 );
970#endif /* MBEDTLS_SSL_RECORD_CHECKING */
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100971 }
972
973 return( (int) recv_len );
974}
975
976static int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len,
977 uint32_t timeout )
978{
979 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
980 int ret;
981 size_t recv_len;
982
983 ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout );
984 if( ret < 0 )
985 return( ret );
986 recv_len = (size_t) ret;
987
988 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
989 {
990 /* Here's the place to do any datagram/record checking
991 * in between receiving the packet from the underlying
992 * transport and passing it on to the TLS stack. */
Hanno Becker4b6649e2019-07-03 17:14:41 +0100993#if defined(MBEDTLS_SSL_RECORD_CHECKING)
994 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
995 return( -1 );
996#endif /* MBEDTLS_SSL_RECORD_CHECKING */
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100997 }
998
999 return( (int) recv_len );
1000}
1001
1002static int send_cb( void *ctx, unsigned char const *buf, size_t len )
1003{
1004 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
1005
1006 if( opt.nbio == 2 )
1007 return( delayed_send( io_ctx->net, buf, len ) );
1008
1009 return( mbedtls_net_send( io_ctx->net, buf, len ) );
1010}
1011
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +02001012/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001013 * Return authmode from string, or -1 on error
1014 */
1015static int get_auth_mode( const char *s )
1016{
1017 if( strcmp( s, "none" ) == 0 )
1018 return( MBEDTLS_SSL_VERIFY_NONE );
1019 if( strcmp( s, "optional" ) == 0 )
1020 return( MBEDTLS_SSL_VERIFY_OPTIONAL );
1021 if( strcmp( s, "required" ) == 0 )
1022 return( MBEDTLS_SSL_VERIFY_REQUIRED );
1023
1024 return( -1 );
1025}
1026
1027/*
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +02001028 * Used by sni_parse and psk_parse to handle coma-separated lists
1029 */
1030#define GET_ITEM( dst ) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001031 do \
1032 { \
1033 (dst) = p; \
1034 while( *p != ',' ) \
1035 if( ++p > end ) \
1036 goto error; \
1037 *p++ = '\0'; \
1038 } while( 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +02001039
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001040#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001041typedef struct _sni_entry sni_entry;
1042
1043struct _sni_entry {
1044 const char *name;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 mbedtls_x509_crt *cert;
1046 mbedtls_pk_context *key;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001047 mbedtls_x509_crt* ca;
1048 mbedtls_x509_crl* crl;
1049 int authmode;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001050 sni_entry *next;
1051};
1052
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001053void sni_free( sni_entry *head )
1054{
1055 sni_entry *cur = head, *next;
1056
1057 while( cur != NULL )
1058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 mbedtls_x509_crt_free( cur->cert );
1060 mbedtls_free( cur->cert );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 mbedtls_pk_free( cur->key );
1063 mbedtls_free( cur->key );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001064
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001065 mbedtls_x509_crt_free( cur->ca );
1066 mbedtls_free( cur->ca );
Ron Eldor80d04192019-04-04 15:02:01 +03001067#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001068 mbedtls_x509_crl_free( cur->crl );
1069 mbedtls_free( cur->crl );
Ron Eldor80d04192019-04-04 15:02:01 +03001070#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001071 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 mbedtls_free( cur );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001073 cur = next;
1074 }
1075}
1076
1077/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001078 * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
1079 * into a usable sni_entry list. For ca1, crl1, auth1, the special value
1080 * '-' means unset. If ca1 is unset, then crl1 is ignored too.
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001081 *
1082 * Modifies the input string! This is not production quality!
1083 */
1084sni_entry *sni_parse( char *sni_string )
1085{
1086 sni_entry *cur = NULL, *new = NULL;
1087 char *p = sni_string;
1088 char *end = p;
Ron Eldor80d04192019-04-04 15:02:01 +03001089 char *crt_file, *key_file, *ca_file, *auth_str;
1090#if defined(MBEDTLS_X509_CRL_PARSE_C)
1091 char *crl_file;
1092#endif
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001093
1094 while( *end != '\0' )
1095 ++end;
1096 *end = ',';
1097
1098 while( p <= end )
1099 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001100 if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001101 {
1102 sni_free( cur );
1103 return( NULL );
1104 }
1105
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001106 GET_ITEM( new->name );
1107 GET_ITEM( crt_file );
1108 GET_ITEM( key_file );
1109 GET_ITEM( ca_file );
Ron Eldor80d04192019-04-04 15:02:01 +03001110#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001111 GET_ITEM( crl_file );
Ron Eldor80d04192019-04-04 15:02:01 +03001112#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001113 GET_ITEM( auth_str );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001114
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001115 if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
1116 ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001117 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 mbedtls_x509_crt_init( new->cert );
1120 mbedtls_pk_init( new->key );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
1123 mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001124 goto error;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001125
1126 if( strcmp( ca_file, "-" ) != 0 )
1127 {
1128 if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
1129 goto error;
1130
1131 mbedtls_x509_crt_init( new->ca );
1132
1133 if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
1134 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001135 }
1136
Ron Eldor80d04192019-04-04 15:02:01 +03001137#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001138 if( strcmp( crl_file, "-" ) != 0 )
1139 {
1140 if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
1141 goto error;
1142
1143 mbedtls_x509_crl_init( new->crl );
1144
1145 if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
1146 goto error;
1147 }
Ron Eldor80d04192019-04-04 15:02:01 +03001148#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001149
1150 if( strcmp( auth_str, "-" ) != 0 )
1151 {
1152 if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
1153 goto error;
1154 }
1155 else
1156 new->authmode = DFL_AUTH_MODE;
1157
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001158 new->next = cur;
1159 cur = new;
1160 }
1161
1162 return( cur );
1163
1164error:
1165 sni_free( new );
1166 sni_free( cur );
1167 return( NULL );
1168}
1169
1170/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001171 * SNI callback.
1172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001174 const unsigned char *name, size_t name_len )
1175{
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001176 const sni_entry *cur = (const sni_entry *) p_info;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001177
1178 while( cur != NULL )
1179 {
1180 if( name_len == strlen( cur->name ) &&
1181 memcmp( name, cur->name, name_len ) == 0 )
1182 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001183 if( cur->ca != NULL )
1184 mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
1185
1186 if( cur->authmode != DFL_AUTH_MODE )
1187 mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
1188
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001189 return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001190 }
1191
1192 cur = cur->next;
1193 }
1194
1195 return( -1 );
1196}
1197
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001198#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001199
Gilles Peskineeccd8882020-03-10 12:19:08 +01001200#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) || \
Hanno Beckera0e20d02019-05-15 14:03:01 +01001201 defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001202
Hanno Becker1eeca412018-10-15 12:01:35 +01001203#define HEX2NUM( c ) \
1204 do \
1205 { \
1206 if( (c) >= '0' && (c) <= '9' ) \
1207 (c) -= '0'; \
1208 else if( (c) >= 'a' && (c) <= 'f' ) \
1209 (c) -= 'a' - 10; \
1210 else if( (c) >= 'A' && (c) <= 'F' ) \
1211 (c) -= 'A' - 10; \
1212 else \
1213 return( -1 ); \
1214 } while( 0 )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001215
1216/*
1217 * Convert a hex string to bytes.
1218 * Return 0 on success, -1 on error.
1219 */
1220int unhexify( unsigned char *output, const char *input, size_t *olen )
1221{
1222 unsigned char c;
1223 size_t j;
1224
1225 *olen = strlen( input );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 if( *olen % 2 != 0 || *olen / 2 > MBEDTLS_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001227 return( -1 );
1228 *olen /= 2;
1229
1230 for( j = 0; j < *olen * 2; j += 2 )
1231 {
1232 c = input[j];
1233 HEX2NUM( c );
1234 output[ j / 2 ] = c << 4;
1235
1236 c = input[j + 1];
1237 HEX2NUM( c );
1238 output[ j / 2 ] |= c;
1239 }
1240
1241 return( 0 );
1242}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001243
Hanno Becker554b6ea2019-04-30 14:18:06 +01001244#endif
1245
Gilles Peskineeccd8882020-03-10 12:19:08 +01001246#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker554b6ea2019-04-30 14:18:06 +01001247
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001248typedef struct _psk_entry psk_entry;
1249
1250struct _psk_entry
1251{
1252 const char *name;
1253 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 unsigned char key[MBEDTLS_PSK_MAX_LEN];
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001255#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001256 psa_key_handle_t slot;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001257#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001258 psk_entry *next;
1259};
1260
1261/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001262 * Free a list of psk_entry's
1263 */
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001264int psk_free( psk_entry *head )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001265{
1266 psk_entry *next;
1267
1268 while( head != NULL )
1269 {
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001270#if defined(MBEDTLS_USE_PSA_CRYPTO)
1271 psa_status_t status;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001272 psa_key_handle_t const slot = head->slot;
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001273
1274 if( slot != 0 )
1275 {
1276 status = psa_destroy_key( slot );
1277 if( status != PSA_SUCCESS )
1278 return( status );
1279 }
1280#endif /* MBEDTLS_USE_PSA_CRYPTO */
1281
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001282 next = head->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 mbedtls_free( head );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001284 head = next;
1285 }
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001286
1287 return( 0 );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001288}
1289
1290/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001291 * Parse a string of pairs name1,key1[,name2,key2[,...]]
1292 * into a usable psk_entry list.
1293 *
1294 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001295 */
1296psk_entry *psk_parse( char *psk_string )
1297{
1298 psk_entry *cur = NULL, *new = NULL;
1299 char *p = psk_string;
1300 char *end = p;
1301 char *key_hex;
1302
1303 while( *end != '\0' )
1304 ++end;
1305 *end = ',';
1306
1307 while( p <= end )
1308 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001309 if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +00001310 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001311
1312 memset( new, 0, sizeof( psk_entry ) );
1313
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +02001314 GET_ITEM( new->name );
1315 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001316
1317 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001318 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001319
1320 new->next = cur;
1321 cur = new;
1322 }
1323
1324 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001325
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001326error:
1327 psk_free( new );
1328 psk_free( cur );
1329 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001330}
1331
1332/*
1333 * PSK callback
1334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001336 const unsigned char *name, size_t name_len )
1337{
1338 psk_entry *cur = (psk_entry *) p_info;
1339
1340 while( cur != NULL )
1341 {
1342 if( name_len == strlen( cur->name ) &&
1343 memcmp( name, cur->name, name_len ) == 0 )
1344 {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001345#if defined(MBEDTLS_USE_PSA_CRYPTO)
1346 if( cur->slot != 0 )
1347 return( mbedtls_ssl_set_hs_psk_opaque( ssl, cur->slot ) );
1348 else
1349#endif
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001350 return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001351 }
1352
1353 cur = cur->next;
1354 }
1355
1356 return( -1 );
1357}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001358#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001359
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001360static mbedtls_net_context listen_fd, client_fd;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +02001361
1362/* Interruption handler to ensure clean exit (for valgrind testing) */
1363#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001364static int received_sigterm = 0;
1365void term_handler( int sig )
1366{
1367 ((void) sig);
1368 received_sigterm = 1;
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001369 mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
1370 mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001371}
Paul Bakkerc1283d32014-08-18 11:05:51 +02001372#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001373
Gilles Peskine682df092017-05-11 19:01:11 +02001374#if defined(MBEDTLS_X509_CRT_PARSE_C)
1375static int ssl_sig_hashes_for_test[] = {
1376#if defined(MBEDTLS_SHA512_C)
1377 MBEDTLS_MD_SHA512,
1378 MBEDTLS_MD_SHA384,
1379#endif
1380#if defined(MBEDTLS_SHA256_C)
1381 MBEDTLS_MD_SHA256,
1382 MBEDTLS_MD_SHA224,
1383#endif
1384#if defined(MBEDTLS_SHA1_C)
1385 /* Allow SHA-1 as we use it extensively in tests. */
1386 MBEDTLS_MD_SHA1,
1387#endif
1388 MBEDTLS_MD_NONE
1389};
1390#endif /* MBEDTLS_X509_CRT_PARSE_C */
1391
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02001392/** Return true if \p ret is a status code indicating that there is an
1393 * operation in progress on an SSL connection, and false if it indicates
1394 * success or a fatal error.
1395 *
1396 * The possible operations in progress are:
1397 *
1398 * - A read, when the SSL input buffer does not contain a full message.
1399 * - A write, when the SSL output buffer contains some data that has not
1400 * been sent over the network yet.
1401 * - An asynchronous callback that has not completed yet. */
1402static int mbedtls_status_is_ssl_in_progress( int ret )
1403{
1404 return( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1405 ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
1406 ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1407}
1408
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001409#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001410typedef struct
1411{
Gilles Peskine44817442018-06-13 18:09:28 +02001412 mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
1413 mbedtls_pk_context *pk; /*!< Private key */
1414 unsigned delay; /*!< Number of resume steps to go through */
1415 unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001416} ssl_async_key_slot_t;
1417
1418typedef enum {
Gilles Peskinead28bf02018-04-26 00:19:16 +02001419 SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
1420 SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
1421 SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start */
1422 SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
Gilles Peskinec9125722018-04-26 07:15:40 +02001423#define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001424} ssl_async_inject_error_t;
1425
1426typedef struct
1427{
Gilles Peskinee2479892018-06-13 18:06:51 +02001428 ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001429 size_t slots_used;
1430 ssl_async_inject_error_t inject_error;
1431 int (*f_rng)(void *, unsigned char *, size_t);
1432 void *p_rng;
1433} ssl_async_key_context_t;
1434
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001435int ssl_async_set_key( ssl_async_key_context_t *ctx,
Gilles Peskine44817442018-06-13 18:09:28 +02001436 mbedtls_x509_crt *cert,
1437 mbedtls_pk_context *pk,
1438 int pk_take_ownership,
1439 unsigned delay )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001440{
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001441 if( ctx->slots_used >= sizeof( ctx->slots ) / sizeof( *ctx->slots ) )
1442 return( -1 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001443 ctx->slots[ctx->slots_used].cert = cert;
1444 ctx->slots[ctx->slots_used].pk = pk;
1445 ctx->slots[ctx->slots_used].delay = delay;
Gilles Peskine44817442018-06-13 18:09:28 +02001446 ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001447 ++ctx->slots_used;
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001448 return( 0 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001449}
1450
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001451#define SSL_ASYNC_INPUT_MAX_SIZE 512
Gilles Peskined3268832018-04-26 06:23:59 +02001452
1453typedef enum
1454{
1455 ASYNC_OP_SIGN,
1456 ASYNC_OP_DECRYPT,
1457} ssl_async_operation_type_t;
1458/* Note that the enum above and the array below need to be kept in sync!
1459 * `ssl_async_operation_names[op]` is the name of op for each value `op`
1460 * of type `ssl_async_operation_type_t`. */
1461static const char *const ssl_async_operation_names[] =
1462{
1463 "sign",
1464 "decrypt",
1465};
1466
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001467typedef struct
1468{
Gilles Peskine6331d782018-04-26 13:27:43 +02001469 unsigned slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001470 ssl_async_operation_type_t operation_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001471 mbedtls_md_type_t md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001472 unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
1473 size_t input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001474 unsigned remaining_delay;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001475} ssl_async_operation_context_t;
1476
Gilles Peskine8f97af72018-04-26 11:46:10 +02001477static int ssl_async_start( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001478 mbedtls_x509_crt *cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001479 ssl_async_operation_type_t op_type,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001480 mbedtls_md_type_t md_alg,
1481 const unsigned char *input,
1482 size_t input_len )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001483{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001484 ssl_async_key_context_t *config_data =
1485 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskine6331d782018-04-26 13:27:43 +02001486 unsigned slot;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001487 ssl_async_operation_context_t *ctx = NULL;
Gilles Peskined3268832018-04-26 06:23:59 +02001488 const char *op_name = ssl_async_operation_names[op_type];
Gilles Peskinef1127252018-04-24 13:05:39 +02001489
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001490 {
1491 char dn[100];
Gilles Peskined5d983e2018-06-15 14:05:10 +02001492 if( mbedtls_x509_dn_gets( dn, sizeof( dn ), &cert->subject ) > 0 )
1493 mbedtls_printf( "Async %s callback: looking for DN=%s\n",
1494 op_name, dn );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001495 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001496
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001497 /* Look for a private key that matches the public key in cert.
1498 * Since this test code has the private key inside Mbed TLS,
1499 * we call mbedtls_pk_check_pair to match a private key with the
1500 * public key. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001501 for( slot = 0; slot < config_data->slots_used; slot++ )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001502 {
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001503 if( mbedtls_pk_check_pair( &cert->pk,
1504 config_data->slots[slot].pk ) == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001505 break;
1506 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001507 if( slot == config_data->slots_used )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001508 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001509 mbedtls_printf( "Async %s callback: no key matches this certificate.\n",
1510 op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001511 return( MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH );
1512 }
Gilles Peskine6331d782018-04-26 13:27:43 +02001513 mbedtls_printf( "Async %s callback: using key slot %u, delay=%u.\n",
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001514 op_name, slot, config_data->slots[slot].delay );
Gilles Peskinef1127252018-04-24 13:05:39 +02001515
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001516 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001517 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001518 mbedtls_printf( "Async %s callback: injected error\n", op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001519 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1520 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001521
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001522 if( input_len > SSL_ASYNC_INPUT_MAX_SIZE )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Gilles Peskinef1127252018-04-24 13:05:39 +02001524
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001525 ctx = mbedtls_calloc( 1, sizeof( *ctx ) );
1526 if( ctx == NULL )
1527 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1528 ctx->slot = slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001529 ctx->operation_type = op_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001530 ctx->md_alg = md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001531 memcpy( ctx->input, input, input_len );
1532 ctx->input_len = input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001533 ctx->remaining_delay = config_data->slots[slot].delay;
Gilles Peskinea668c602018-04-30 11:54:39 +02001534 mbedtls_ssl_set_async_operation_data( ssl, ctx );
Gilles Peskinef1127252018-04-24 13:05:39 +02001535
Gilles Peskineceb541b2018-04-26 06:30:45 +02001536 if( ctx->remaining_delay == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001537 return( 0 );
1538 else
1539 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1540}
1541
Gilles Peskine8f97af72018-04-26 11:46:10 +02001542static int ssl_async_sign( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001543 mbedtls_x509_crt *cert,
1544 mbedtls_md_type_t md_alg,
1545 const unsigned char *hash,
1546 size_t hash_len )
1547{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001548 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001549 ASYNC_OP_SIGN, md_alg,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001550 hash, hash_len ) );
1551}
1552
Gilles Peskine8f97af72018-04-26 11:46:10 +02001553static int ssl_async_decrypt( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001554 mbedtls_x509_crt *cert,
1555 const unsigned char *input,
1556 size_t input_len )
1557{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001558 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001559 ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001560 input, input_len ) );
1561}
1562
Gilles Peskine8f97af72018-04-26 11:46:10 +02001563static int ssl_async_resume( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001564 unsigned char *output,
1565 size_t *output_len,
1566 size_t output_size )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001567{
Gilles Peskinea668c602018-04-30 11:54:39 +02001568 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine8f97af72018-04-26 11:46:10 +02001569 ssl_async_key_context_t *config_data =
1570 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001571 ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001572 int ret;
Gilles Peskinef5a99962018-04-30 16:37:23 +02001573 const char *op_name;
Gilles Peskinef1127252018-04-24 13:05:39 +02001574
Gilles Peskineceb541b2018-04-26 06:30:45 +02001575 if( ctx->remaining_delay > 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001576 {
Gilles Peskineceb541b2018-04-26 06:30:45 +02001577 --ctx->remaining_delay;
Gilles Peskine6331d782018-04-26 13:27:43 +02001578 mbedtls_printf( "Async resume (slot %u): call %u more times.\n",
Gilles Peskineceb541b2018-04-26 06:30:45 +02001579 ctx->slot, ctx->remaining_delay );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001580 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1581 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001582
Gilles Peskined3268832018-04-26 06:23:59 +02001583 switch( ctx->operation_type )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001584 {
Gilles Peskined3268832018-04-26 06:23:59 +02001585 case ASYNC_OP_DECRYPT:
Gilles Peskined3268832018-04-26 06:23:59 +02001586 ret = mbedtls_pk_decrypt( key_slot->pk,
1587 ctx->input, ctx->input_len,
1588 output, output_len, output_size,
1589 config_data->f_rng, config_data->p_rng );
1590 break;
1591 case ASYNC_OP_SIGN:
Gilles Peskined3268832018-04-26 06:23:59 +02001592 ret = mbedtls_pk_sign( key_slot->pk,
1593 ctx->md_alg,
1594 ctx->input, ctx->input_len,
1595 output, output_len,
1596 config_data->f_rng, config_data->p_rng );
1597 break;
1598 default:
Gilles Peskine6331d782018-04-26 13:27:43 +02001599 mbedtls_printf( "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
Gilles Peskined3268832018-04-26 06:23:59 +02001600 ctx->slot, (long) ctx->operation_type );
Gilles Peskine44817442018-06-13 18:09:28 +02001601 mbedtls_free( ctx );
Gilles Peskined3268832018-04-26 06:23:59 +02001602 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1603 break;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001604 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001605
Gilles Peskinef5a99962018-04-30 16:37:23 +02001606 op_name = ssl_async_operation_names[ctx->operation_type];
1607
Gilles Peskinec9125722018-04-26 07:15:40 +02001608 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001609 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001610 mbedtls_printf( "Async resume callback: %s done but injected error\n",
1611 op_name );
Gilles Peskine2636fad2018-06-12 14:17:39 +02001612 mbedtls_free( ctx );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001613 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1614 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001615
Gilles Peskine6331d782018-04-26 13:27:43 +02001616 mbedtls_printf( "Async resume (slot %u): %s done, status=%d.\n",
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001617 ctx->slot, op_name, ret );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001618 mbedtls_free( ctx );
1619 return( ret );
1620}
1621
Gilles Peskine8f97af72018-04-26 11:46:10 +02001622static void ssl_async_cancel( mbedtls_ssl_context *ssl )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001623{
Gilles Peskinea668c602018-04-30 11:54:39 +02001624 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001625 mbedtls_printf( "Async cancel callback.\n" );
1626 mbedtls_free( ctx );
1627}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001628#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001629
Hanno Becker16970d22017-10-10 15:56:37 +01001630/*
1631 * Wait for an event from the underlying transport or the timer
1632 * (Used in event-driven IO mode).
1633 */
1634#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001635int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001636 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001637#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001638int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001639 mbedtls_timing_delay_context *timer,
1640 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001641#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001642{
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001643 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +01001644 int poll_type = 0;
1645
1646 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
1647 poll_type = MBEDTLS_NET_POLL_WRITE;
1648 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
1649 poll_type = MBEDTLS_NET_POLL_READ;
1650#if !defined(MBEDTLS_TIMING_C)
1651 else
Hanno Beckeref527962018-03-15 15:49:24 +00001652 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001653#endif
1654
1655 while( 1 )
1656 {
Hanno Becker197a91c2017-10-31 10:58:53 +00001657 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +01001658#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001659 if( timer != NULL &&
1660 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +01001661 {
Hanno Becker16970d22017-10-10 15:56:37 +01001662 break;
1663 }
Hanno Becker197a91c2017-10-31 10:58:53 +00001664#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +01001665
Hanno Becker197a91c2017-10-31 10:58:53 +00001666 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001667 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +01001668 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001669 ret = mbedtls_net_poll( fd, poll_type, 0 );
1670 if( ret < 0 )
1671 return( ret );
1672 if( ret == poll_type )
1673 break;
Hanno Becker16970d22017-10-10 15:56:37 +01001674 }
1675 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001676
1677 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001678}
1679
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001680#if defined(MBEDTLS_USE_PSA_CRYPTO)
Janos Follathbe4efc22019-08-08 11:38:18 +01001681static psa_status_t psa_setup_psk_key_slot( psa_key_handle_t *slot,
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001682 psa_algorithm_t alg,
1683 unsigned char *psk,
1684 size_t psk_len )
1685{
1686 psa_status_t status;
Janos Follathbe4efc22019-08-08 11:38:18 +01001687 psa_key_attributes_t key_attributes;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001688
Janos Follathbe4efc22019-08-08 11:38:18 +01001689 key_attributes = psa_key_attributes_init();
1690 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1691 psa_set_key_algorithm( &key_attributes, alg );
1692 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001693
Janos Follathbe4efc22019-08-08 11:38:18 +01001694 status = psa_import_key( &key_attributes, psk, psk_len, slot );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001695 if( status != PSA_SUCCESS )
Hanno Becker1d911cd2018-11-15 13:06:09 +00001696 {
1697 fprintf( stderr, "IMPORT\n" );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001698 return( status );
Hanno Becker1d911cd2018-11-15 13:06:09 +00001699 }
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001700
1701 return( PSA_SUCCESS );
1702}
1703#endif /* MBEDTLS_USE_PSA_CRYPTO */
1704
Hanno Beckera0e20d02019-05-15 14:03:01 +01001705#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001706int report_cid_usage( mbedtls_ssl_context *ssl,
1707 const char *additional_description )
1708{
1709 int ret;
1710 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
1711 size_t peer_cid_len;
1712 int cid_negotiated;
1713
1714 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1715 return( 0 );
1716
1717 /* Check if the use of a CID has been negotiated */
1718 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
1719 peer_cid, &peer_cid_len );
1720 if( ret != 0 )
1721 {
1722 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
1723 -ret );
1724 return( ret );
1725 }
1726
1727 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
1728 {
1729 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
1730 {
1731 mbedtls_printf( "(%s) Use of Connection ID was not offered by client.\n",
1732 additional_description );
1733 }
1734 }
1735 else
1736 {
1737 size_t idx=0;
1738 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
1739 additional_description );
1740 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
1741 additional_description,
1742 (unsigned) peer_cid_len );
1743 while( idx < peer_cid_len )
1744 {
1745 mbedtls_printf( "%02x ", peer_cid[ idx ] );
1746 idx++;
1747 }
1748 mbedtls_printf( "\n" );
1749 }
1750
1751 return( 0 );
1752}
Hanno Beckera0e20d02019-05-15 14:03:01 +01001753#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001754
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001755int main( int argc, char *argv[] )
1756{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001757 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001758 int version_suites[4][2];
Hanno Beckerdcc94e62019-07-03 17:05:43 +01001759 io_ctx_t io_ctx;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001760 unsigned char* buf = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01001761#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001762#if defined(MBEDTLS_USE_PSA_CRYPTO)
1763 psa_algorithm_t alg = 0;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001764 psa_key_handle_t psk_slot = 0;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001765#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +02001767 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +02001768 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +02001769#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001770 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001771 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02001772 size_t cliip_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#if defined(MBEDTLS_SSL_COOKIE_C)
1774 mbedtls_ssl_cookie_ctx cookie_ctx;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001775#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001776
Gilles Peskineef86ab22017-05-05 18:59:02 +02001777#if defined(MBEDTLS_X509_CRT_PARSE_C)
1778 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
1779#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 mbedtls_entropy_context entropy;
1781 mbedtls_ctr_drbg_context ctr_drbg;
1782 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001783 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001784#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001785 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001786#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001788 unsigned char renego_period[8] = { 0 };
1789#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +02001791 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 mbedtls_x509_crt cacert;
1793 mbedtls_x509_crt srvcert;
1794 mbedtls_pk_context pkey;
1795 mbedtls_x509_crt srvcert2;
1796 mbedtls_pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001797 int key_cert_init = 0, key_cert_init2 = 0;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001798#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001799 ssl_async_key_context_t ssl_async_keys;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001800#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001801#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1803 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001804#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805#if defined(MBEDTLS_SSL_CACHE_C)
1806 mbedtls_ssl_cache_context cache;
Paul Bakker0a597072012-09-25 21:55:46 +00001807#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001808#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1809 mbedtls_ssl_ticket_context ticket_ctx;
1810#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001811#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001812 sni_entry *sni_info = NULL;
1813#endif
Hanno Beckere6706e62017-05-15 16:05:15 +01001814#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +01001815 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +01001816 const mbedtls_ecp_curve_info * curve_cur;
1817#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +01001819 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001820#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Simon Butchercce68be2018-07-23 14:26:09 +01001822 unsigned char alloc_buf[MEMORY_HEAP_SIZE];
Paul Bakker82024bf2013-07-04 11:52:32 +02001823#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001824
Hanno Beckera0e20d02019-05-15 14:03:01 +01001825#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +01001826 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001827 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckera7d25422019-04-09 17:28:10 +01001828 size_t cid_len = 0;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001829 size_t cid_renego_len = 0;
Hanno Beckera7d25422019-04-09 17:28:10 +01001830#endif
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02001831#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1832 unsigned char *context_buf = NULL;
Gilles Peskine4e8b5942019-09-20 19:12:31 +02001833 size_t context_buf_len = 0;
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02001834#endif
Hanno Beckera7d25422019-04-09 17:28:10 +01001835
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001836 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001837 char *p, *q;
1838 const int *list;
Hanno Becker5a9942e2018-11-12 17:47:48 +00001839#if defined(MBEDTLS_USE_PSA_CRYPTO)
1840 psa_status_t status;
1841#endif
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001842#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1843 unsigned char eap_tls_keymaterial[16];
1844 unsigned char eap_tls_iv[8];
1845 const char* eap_tls_label = "client EAP encryption";
1846 eap_tls_keys eap_tls_keying;
1847#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1850 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Piotr Nowicki0937ed22019-11-26 16:32:40 +01001851#if defined(MBEDTLS_MEMORY_DEBUG)
1852 size_t current_heap_memory, peak_heap_memory, heap_blocks;
1853#endif /* MBEDTLS_MEMORY_DEBUG */
1854#endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
Paul Bakker82024bf2013-07-04 11:52:32 +02001855
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001856 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +02001857 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001858 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001859 mbedtls_net_init( &client_fd );
1860 mbedtls_net_init( &listen_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001861 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001862 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001863 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864#if defined(MBEDTLS_X509_CRT_PARSE_C)
1865 mbedtls_x509_crt_init( &cacert );
1866 mbedtls_x509_crt_init( &srvcert );
1867 mbedtls_pk_init( &pkey );
1868 mbedtls_x509_crt_init( &srvcert2 );
1869 mbedtls_pk_init( &pkey2 );
Gilles Peskine4d9ec4d2018-04-26 14:33:43 +02001870#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
1871 memset( &ssl_async_keys, 0, sizeof( ssl_async_keys ) );
1872#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001873#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1875 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001876#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877#if defined(MBEDTLS_SSL_CACHE_C)
1878 mbedtls_ssl_cache_init( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00001879#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001880#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1881 mbedtls_ssl_ticket_init( &ticket_ctx );
1882#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +02001884 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001885#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886#if defined(MBEDTLS_SSL_COOKIE_C)
1887 mbedtls_ssl_cookie_init( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001888#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001889
Hanno Becker5a9942e2018-11-12 17:47:48 +00001890#if defined(MBEDTLS_USE_PSA_CRYPTO)
1891 status = psa_crypto_init();
1892 if( status != PSA_SUCCESS )
1893 {
1894 mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
1895 (int) status );
1896 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1897 goto exit;
1898 }
1899#endif
1900
Paul Bakkerc1283d32014-08-18 11:05:51 +02001901#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001902 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001903 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001904 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +02001905#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001906
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001907 if( argc == 0 )
1908 {
1909 usage:
1910 if( ret == 0 )
1911 ret = 1;
1912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 mbedtls_printf( USAGE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 list = mbedtls_ssl_list_ciphersuites();
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001916 while( *list )
1917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001919 list++;
1920 if( !*list )
1921 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001923 list++;
1924 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 mbedtls_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001926 goto exit;
1927 }
1928
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001929 opt.buffer_size = DFL_IO_BUF_LEN;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001930 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001931 opt.server_port = DFL_SERVER_PORT;
1932 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker16970d22017-10-10 15:56:37 +01001933 opt.event = DFL_EVENT;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001934 opt.response_size = DFL_RESPONSE_SIZE;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001935 opt.nbio = DFL_NBIO;
Hanno Beckera7d25422019-04-09 17:28:10 +01001936 opt.cid_enabled = DFL_CID_ENABLED;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001937 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
Hanno Beckera7d25422019-04-09 17:28:10 +01001938 opt.cid_val = DFL_CID_VALUE;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001939 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001940 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001941 opt.ca_file = DFL_CA_FILE;
1942 opt.ca_path = DFL_CA_PATH;
1943 opt.crt_file = DFL_CRT_FILE;
1944 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001945 opt.crt_file2 = DFL_CRT_FILE2;
1946 opt.key_file2 = DFL_KEY_FILE2;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001947 opt.async_operations = DFL_ASYNC_OPERATIONS;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001948 opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
1949 opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
1950 opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
Paul Bakkerfbb17802013-04-17 19:10:21 +02001951 opt.psk = DFL_PSK;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001952#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001953 opt.psk_opaque = DFL_PSK_OPAQUE;
1954 opt.psk_list_opaque = DFL_PSK_LIST_OPAQUE;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001955#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001956#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1957 opt.ca_callback = DFL_CA_CALLBACK;
1958#endif
Paul Bakkerfbb17802013-04-17 19:10:21 +02001959 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001960 opt.psk_list = DFL_PSK_LIST;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001961 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001962 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001963 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001964 opt.renegotiation = DFL_RENEGOTIATION;
1965 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001966 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001967 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001968 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001969 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001970 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001971 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001972 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001973 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001974 opt.auth_mode = DFL_AUTH_MODE;
Janos Follath4817e272017-04-10 13:44:33 +01001975 opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001976 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001977 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001978 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001979 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001980 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001981 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001982 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001983 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001984 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001985 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001986 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001987 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001988 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001989 opt.hs_to_min = DFL_HS_TO_MIN;
1990 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001991 opt.dtls_mtu = DFL_DTLS_MTU;
Hanno Beckere7675d02018-08-14 13:28:56 +01001992 opt.dgram_packing = DFL_DGRAM_PACKING;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001993 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001994 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001995 opt.etm = DFL_ETM;
Jarno Lamsa9831c8a2019-05-29 13:33:32 +03001996 opt.serialize = DFL_SERIALIZE;
Piotr Nowicki3de298f2020-04-16 14:35:19 +02001997 opt.context_file = DFL_CONTEXT_FILE;
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001998 opt.eap_tls = DFL_EAP_TLS;
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001999 opt.reproducible = DFL_REPRODUCIBLE;
Hanno Becker48f3a3d2019-08-29 06:31:22 +01002000 opt.nss_keylog = DFL_NSS_KEYLOG;
2001 opt.nss_keylog_file = DFL_NSS_KEYLOG_FILE;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002002
2003 for( i = 1; i < argc; i++ )
2004 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002005 p = argv[i];
2006 if( ( q = strchr( p, '=' ) ) == NULL )
2007 goto usage;
2008 *q++ = '\0';
2009
2010 if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02002011 opt.server_port = q;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01002012 else if( strcmp( p, "server_addr" ) == 0 )
2013 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01002014 else if( strcmp( p, "dtls" ) == 0 )
2015 {
2016 int t = atoi( q );
2017 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01002019 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01002021 else
2022 goto usage;
2023 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002024 else if( strcmp( p, "debug_level" ) == 0 )
2025 {
2026 opt.debug_level = atoi( q );
2027 if( opt.debug_level < 0 || opt.debug_level > 65535 )
2028 goto usage;
2029 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002030 else if( strcmp( p, "nbio" ) == 0 )
2031 {
2032 opt.nbio = atoi( q );
2033 if( opt.nbio < 0 || opt.nbio > 2 )
2034 goto usage;
2035 }
Hanno Becker16970d22017-10-10 15:56:37 +01002036 else if( strcmp( p, "event" ) == 0 )
2037 {
2038 opt.event = atoi( q );
2039 if( opt.event < 0 || opt.event > 2 )
2040 goto usage;
2041 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002042 else if( strcmp( p, "read_timeout" ) == 0 )
2043 opt.read_timeout = atoi( q );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002044 else if( strcmp( p, "buffer_size" ) == 0 )
2045 {
2046 opt.buffer_size = atoi( q );
2047 if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
2048 goto usage;
2049 }
2050 else if( strcmp( p, "response_size" ) == 0 )
2051 {
2052 opt.response_size = atoi( q );
2053 if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
2054 goto usage;
2055 if( opt.buffer_size < opt.response_size )
2056 opt.buffer_size = opt.response_size;
2057 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002058 else if( strcmp( p, "ca_file" ) == 0 )
2059 opt.ca_file = q;
2060 else if( strcmp( p, "ca_path" ) == 0 )
2061 opt.ca_path = q;
2062 else if( strcmp( p, "crt_file" ) == 0 )
2063 opt.crt_file = q;
2064 else if( strcmp( p, "key_file" ) == 0 )
2065 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002066 else if( strcmp( p, "crt_file2" ) == 0 )
2067 opt.crt_file2 = q;
2068 else if( strcmp( p, "key_file2" ) == 0 )
2069 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002070 else if( strcmp( p, "dhm_file" ) == 0 )
2071 opt.dhm_file = q;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002072#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002073 else if( strcmp( p, "async_operations" ) == 0 )
2074 opt.async_operations = q;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002075 else if( strcmp( p, "async_private_delay1" ) == 0 )
2076 opt.async_private_delay1 = atoi( q );
2077 else if( strcmp( p, "async_private_delay2" ) == 0 )
2078 opt.async_private_delay2 = atoi( q );
2079 else if( strcmp( p, "async_private_error" ) == 0 )
2080 {
2081 int n = atoi( q );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002082 if( n < -SSL_ASYNC_INJECT_ERROR_MAX ||
2083 n > SSL_ASYNC_INJECT_ERROR_MAX )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002084 {
2085 ret = 2;
2086 goto usage;
2087 }
2088 opt.async_private_error = n;
2089 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002090#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Hanno Beckera0e20d02019-05-15 14:03:01 +01002091#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +01002092 else if( strcmp( p, "cid" ) == 0 )
2093 {
2094 opt.cid_enabled = atoi( q );
2095 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
2096 goto usage;
2097 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002098 else if( strcmp( p, "cid_renego" ) == 0 )
2099 {
2100 opt.cid_enabled_renego = atoi( q );
2101 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
2102 goto usage;
2103 }
Hanno Beckera7d25422019-04-09 17:28:10 +01002104 else if( strcmp( p, "cid_val" ) == 0 )
2105 {
2106 opt.cid_val = q;
2107 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002108 else if( strcmp( p, "cid_val_renego" ) == 0 )
2109 {
2110 opt.cid_val_renego = q;
2111 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002112#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002113 else if( strcmp( p, "psk" ) == 0 )
2114 opt.psk = q;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002115#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002116 else if( strcmp( p, "psk_opaque" ) == 0 )
2117 opt.psk_opaque = atoi( q );
2118 else if( strcmp( p, "psk_list_opaque" ) == 0 )
2119 opt.psk_list_opaque = atoi( q );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002120#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02002121#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2122 else if( strcmp( p, "ca_callback" ) == 0)
2123 opt.ca_callback = atoi( q );
2124#endif
Paul Bakkerfbb17802013-04-17 19:10:21 +02002125 else if( strcmp( p, "psk_identity" ) == 0 )
2126 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002127 else if( strcmp( p, "psk_list" ) == 0 )
2128 opt.psk_list = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002129 else if( strcmp( p, "ecjpake_pw" ) == 0 )
2130 opt.ecjpake_pw = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002131 else if( strcmp( p, "force_ciphersuite" ) == 0 )
2132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002134
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02002135 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002136 {
2137 ret = 2;
2138 goto usage;
2139 }
2140 opt.force_ciphersuite[1] = 0;
2141 }
Hanno Beckere6706e62017-05-15 16:05:15 +01002142 else if( strcmp( p, "curves" ) == 0 )
2143 opt.curves = q;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002144 else if( strcmp( p, "version_suites" ) == 0 )
2145 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002146 else if( strcmp( p, "renegotiation" ) == 0 )
2147 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002148 opt.renegotiation = (atoi( q )) ?
2149 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
2150 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002151 }
2152 else if( strcmp( p, "allow_legacy" ) == 0 )
2153 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002154 switch( atoi( q ) )
2155 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002156 case -1:
2157 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
2158 break;
2159 case 0:
2160 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
2161 break;
2162 case 1:
2163 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
2164 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002165 default: goto usage;
2166 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002167 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002168 else if( strcmp( p, "renegotiate" ) == 0 )
2169 {
2170 opt.renegotiate = atoi( q );
2171 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
2172 goto usage;
2173 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02002174 else if( strcmp( p, "renego_delay" ) == 0 )
2175 {
2176 opt.renego_delay = atoi( q );
2177 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002178 else if( strcmp( p, "renego_period" ) == 0 )
2179 {
Andres AG0b736db2017-02-08 14:05:57 +00002180#if defined(_MSC_VER)
2181 opt.renego_period = _strtoui64( q, NULL, 10 );
2182#else
2183 if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
2184 goto usage;
2185#endif /* _MSC_VER */
2186 if( opt.renego_period < 2 )
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002187 goto usage;
2188 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02002189 else if( strcmp( p, "exchanges" ) == 0 )
2190 {
2191 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02002192 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02002193 goto usage;
2194 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00002195 else if( strcmp( p, "min_version" ) == 0 )
2196 {
2197 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002199 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01002201 else if( strcmp( q, "tls1_1" ) == 0 ||
2202 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01002204 else if( strcmp( q, "tls1_2" ) == 0 ||
2205 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002207 else
2208 goto usage;
2209 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002210 else if( strcmp( p, "max_version" ) == 0 )
2211 {
2212 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002214 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01002216 else if( strcmp( q, "tls1_1" ) == 0 ||
2217 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01002219 else if( strcmp( q, "tls1_2" ) == 0 ||
2220 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002222 else
2223 goto usage;
2224 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01002225 else if( strcmp( p, "arc4" ) == 0 )
2226 {
2227 switch( atoi( q ) )
2228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
2230 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01002231 default: goto usage;
2232 }
2233 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02002234 else if( strcmp( p, "allow_sha1" ) == 0 )
2235 {
2236 switch( atoi( q ) )
2237 {
2238 case 0: opt.allow_sha1 = 0; break;
2239 case 1: opt.allow_sha1 = 1; break;
2240 default: goto usage;
2241 }
2242 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002243 else if( strcmp( p, "force_version" ) == 0 )
2244 {
2245 if( strcmp( q, "ssl3" ) == 0 )
2246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
2248 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002249 }
2250 else if( strcmp( q, "tls1" ) == 0 )
2251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
2253 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002254 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002255 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02002256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
2258 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002259 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002260 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02002261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
2263 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002264 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002265 else if( strcmp( q, "dtls1" ) == 0 )
2266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
2268 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
2269 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002270 }
2271 else if( strcmp( q, "dtls1_2" ) == 0 )
2272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
2274 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
2275 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002276 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002277 else
2278 goto usage;
2279 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01002280 else if( strcmp( p, "auth_mode" ) == 0 )
2281 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02002282 if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
Paul Bakker91ebfb52012-11-23 14:04:08 +01002283 goto usage;
2284 }
Janos Follath4817e272017-04-10 13:44:33 +01002285 else if( strcmp( p, "cert_req_ca_list" ) == 0 )
2286 {
2287 opt.cert_req_ca_list = atoi( q );
2288 if( opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1 )
2289 goto usage;
2290 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002291 else if( strcmp( p, "max_frag_len" ) == 0 )
2292 {
2293 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002295 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002297 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002299 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002301 else
2302 goto usage;
2303 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002304 else if( strcmp( p, "alpn" ) == 0 )
2305 {
2306 opt.alpn_string = q;
2307 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002308 else if( strcmp( p, "trunc_hmac" ) == 0 )
2309 {
2310 switch( atoi( q ) )
2311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
2313 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002314 default: goto usage;
2315 }
2316 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002317 else if( strcmp( p, "extended_ms" ) == 0 )
2318 {
2319 switch( atoi( q ) )
2320 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002321 case 0:
2322 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
2323 break;
2324 case 1:
2325 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
2326 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002327 default: goto usage;
2328 }
2329 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002330 else if( strcmp( p, "etm" ) == 0 )
2331 {
2332 switch( atoi( q ) )
2333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
2335 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002336 default: goto usage;
2337 }
2338 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002339 else if( strcmp( p, "tickets" ) == 0 )
2340 {
2341 opt.tickets = atoi( q );
2342 if( opt.tickets < 0 || opt.tickets > 1 )
2343 goto usage;
2344 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01002345 else if( strcmp( p, "ticket_timeout" ) == 0 )
2346 {
2347 opt.ticket_timeout = atoi( q );
2348 if( opt.ticket_timeout < 0 )
2349 goto usage;
2350 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002351 else if( strcmp( p, "cache_max" ) == 0 )
2352 {
2353 opt.cache_max = atoi( q );
2354 if( opt.cache_max < 0 )
2355 goto usage;
2356 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002357 else if( strcmp( p, "cache_timeout" ) == 0 )
2358 {
2359 opt.cache_timeout = atoi( q );
2360 if( opt.cache_timeout < 0 )
2361 goto usage;
2362 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002363 else if( strcmp( p, "cookies" ) == 0 )
2364 {
2365 opt.cookies = atoi( q );
2366 if( opt.cookies < -1 || opt.cookies > 1)
2367 goto usage;
2368 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002369 else if( strcmp( p, "anti_replay" ) == 0 )
2370 {
2371 opt.anti_replay = atoi( q );
2372 if( opt.anti_replay < 0 || opt.anti_replay > 1)
2373 goto usage;
2374 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002375 else if( strcmp( p, "badmac_limit" ) == 0 )
2376 {
2377 opt.badmac_limit = atoi( q );
2378 if( opt.badmac_limit < 0 )
2379 goto usage;
2380 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002381 else if( strcmp( p, "hs_timeout" ) == 0 )
2382 {
2383 if( ( p = strchr( q, '-' ) ) == NULL )
2384 goto usage;
2385 *p++ = '\0';
2386 opt.hs_to_min = atoi( q );
2387 opt.hs_to_max = atoi( p );
2388 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
2389 goto usage;
2390 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002391 else if( strcmp( p, "mtu" ) == 0 )
2392 {
2393 opt.dtls_mtu = atoi( q );
2394 if( opt.dtls_mtu < 0 )
2395 goto usage;
2396 }
Hanno Beckere7675d02018-08-14 13:28:56 +01002397 else if( strcmp( p, "dgram_packing" ) == 0 )
2398 {
2399 opt.dgram_packing = atoi( q );
2400 if( opt.dgram_packing != 0 &&
2401 opt.dgram_packing != 1 )
2402 {
2403 goto usage;
2404 }
2405 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002406 else if( strcmp( p, "sni" ) == 0 )
2407 {
2408 opt.sni = q;
2409 }
Andres Amaya Garciabc818842018-10-16 21:08:38 +01002410 else if( strcmp( p, "query_config" ) == 0 )
2411 {
2412 return query_config( q );
2413 }
Jarno Lamsa9831c8a2019-05-29 13:33:32 +03002414 else if( strcmp( p, "serialize") == 0 )
2415 {
2416 opt.serialize = atoi( q );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03002417 if( opt.serialize < 0 || opt.serialize > 2)
Jarno Lamsa9831c8a2019-05-29 13:33:32 +03002418 goto usage;
2419 }
Piotr Nowicki3de298f2020-04-16 14:35:19 +02002420 else if( strcmp( p, "context_file") == 0 )
2421 {
2422 opt.context_file = q;
2423 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002424 else if( strcmp( p, "eap_tls" ) == 0 )
2425 {
2426 opt.eap_tls = atoi( q );
2427 if( opt.eap_tls < 0 || opt.eap_tls > 1 )
2428 goto usage;
2429 }
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002430 else if( strcmp( p, "reproducible" ) == 0 )
2431 {
2432 opt.reproducible = 1;
2433 }
Hanno Becker48f3a3d2019-08-29 06:31:22 +01002434 else if( strcmp( p, "nss_keylog" ) == 0 )
2435 {
2436 opt.nss_keylog = atoi( q );
2437 if( opt.nss_keylog < 0 || opt.nss_keylog > 1 )
2438 goto usage;
2439 }
2440 else if( strcmp( p, "nss_keylog_file" ) == 0 )
2441 {
2442 opt.nss_keylog_file = q;
2443 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002444 else
2445 goto usage;
2446 }
2447
Hanno Beckerbc5308c2019-09-09 11:38:51 +01002448 if( opt.nss_keylog != 0 && opt.eap_tls != 0 )
2449 {
2450 mbedtls_printf( "Error: eap_tls and nss_keylog options cannot be used together.\n" );
2451 goto usage;
2452 }
2453
Hanno Becker16970d22017-10-10 15:56:37 +01002454 /* Event-driven IO is incompatible with the above custom
2455 * receive and send functions, as the polling builds on
2456 * refers to the underlying net_context. */
2457 if( opt.event == 1 && opt.nbio != 1 )
2458 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002459 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01002460 opt.nbio = 1;
2461 }
2462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463#if defined(MBEDTLS_DEBUG_C)
2464 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02002465#endif
Andrzej Kurekda4029d2018-06-20 07:07:55 -04002466 buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002467 if( buf == NULL )
2468 {
Andrzej Kurek755890f2018-06-20 08:17:04 -04002469 mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002470 ret = 3;
2471 goto exit;
2472 }
Paul Bakkerc73079a2014-04-25 16:34:30 +02002473
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002474#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002475 if( opt.psk_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002476 {
2477 if( strlen( opt.psk ) == 0 )
2478 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00002479 mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002480 ret = 2;
2481 goto usage;
2482 }
2483
2484 if( opt.force_ciphersuite[0] <= 0 )
2485 {
2486 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" );
2487 ret = 2;
2488 goto usage;
2489 }
2490 }
2491
Hanno Becker1d911cd2018-11-15 13:06:09 +00002492 if( opt.psk_list_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002493 {
2494 if( opt.psk_list == NULL )
2495 {
2496 mbedtls_printf( "psk_slot set but no psk to be imported specified.\n" );
2497 ret = 2;
2498 goto usage;
2499 }
2500
2501 if( opt.force_ciphersuite[0] <= 0 )
2502 {
2503 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" );
2504 ret = 2;
2505 goto usage;
2506 }
2507 }
2508#endif /* MBEDTLS_USE_PSA_CRYPTO */
2509
Paul Bakkerc1516be2013-06-29 16:01:32 +02002510 if( opt.force_ciphersuite[0] > 0 )
2511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002513 ciphersuite_info =
2514 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002515
Paul Bakker5b55b792013-07-19 13:43:43 +02002516 if( opt.max_version != -1 &&
2517 ciphersuite_info->min_minor_ver > opt.max_version )
2518 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002519 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker5b55b792013-07-19 13:43:43 +02002520 ret = 2;
2521 goto usage;
2522 }
2523 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02002524 ciphersuite_info->max_minor_ver < opt.min_version )
2525 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002526 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002527 ret = 2;
2528 goto usage;
2529 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002530
2531 /* If we select a version that's not supported by
2532 * this suite, then there will be no common ciphersuite... */
2533 if( opt.max_version == -1 ||
2534 opt.max_version > ciphersuite_info->max_minor_ver )
2535 {
Paul Bakker5b55b792013-07-19 13:43:43 +02002536 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002537 }
Paul Bakker5b55b792013-07-19 13:43:43 +02002538 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002539 {
Paul Bakker5b55b792013-07-19 13:43:43 +02002540 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002541 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2543 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
2544 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002545 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002546
2547 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002553 ret = 2;
2554 goto usage;
2555 }
2556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002558 }
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002559
2560#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002561 if( opt.psk_opaque != 0 || opt.psk_list_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002562 {
2563 /* Ensure that the chosen ciphersuite is PSK-only; we must know
2564 * the ciphersuite in advance to set the correct policy for the
2565 * PSK key slot. This limitation might go away in the future. */
2566 if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
2567 opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 )
2568 {
2569 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" );
2570 ret = 2;
2571 goto usage;
2572 }
2573
2574 /* Determine KDF algorithm the opaque PSK will be used in. */
2575#if defined(MBEDTLS_SHA512_C)
2576 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
2577 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2578 else
2579#endif /* MBEDTLS_SHA512_C */
2580 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2581 }
2582#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerc1516be2013-06-29 16:01:32 +02002583 }
2584
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002585 if( opt.version_suites != NULL )
2586 {
2587 const char *name[4] = { 0 };
2588
2589 /* Parse 4-element coma-separated list */
2590 for( i = 0, p = (char *) opt.version_suites;
2591 i < 4 && *p != '\0';
2592 i++ )
2593 {
2594 name[i] = p;
2595
2596 /* Terminate the current string and move on to next one */
2597 while( *p != ',' && *p != '\0' )
2598 p++;
2599 if( *p == ',' )
2600 *p++ = '\0';
2601 }
2602
2603 if( i != 4 )
2604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 mbedtls_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002606 ret = 1;
2607 goto exit;
2608 }
2609
2610 memset( version_suites, 0, sizeof( version_suites ) );
2611
2612 /* Get the suites identifiers from their name */
2613 for( i = 0; i < 4; i++ )
2614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002616
2617 if( version_suites[i][0] == 0 )
2618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002620 ret = 2;
2621 goto usage;
2622 }
2623 }
2624 }
2625
Hanno Beckera0e20d02019-05-15 14:03:01 +01002626#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002627 if( unhexify( cid, opt.cid_val, &cid_len ) != 0 )
2628 {
2629 mbedtls_printf( "CID not valid hex\n" );
2630 goto exit;
2631 }
2632
2633 /* Keep CID settings for renegotiation unless
2634 * specified otherwise. */
2635 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
2636 opt.cid_enabled_renego = opt.cid_enabled;
2637 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
2638 opt.cid_val_renego = opt.cid_val;
2639
2640 if( unhexify( cid_renego, opt.cid_val_renego, &cid_renego_len ) != 0 )
2641 {
2642 mbedtls_printf( "CID not valid hex\n" );
2643 goto exit;
2644 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002645#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +01002646
Gilles Peskineeccd8882020-03-10 12:19:08 +01002647#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002648 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002649 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02002650 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002651 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 mbedtls_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002654 goto exit;
2655 }
2656
2657 if( opt.psk_list != NULL )
2658 {
2659 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 mbedtls_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002662 goto exit;
2663 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002664 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002665#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002666
Hanno Beckere6706e62017-05-15 16:05:15 +01002667#if defined(MBEDTLS_ECP_C)
2668 if( opt.curves != NULL )
2669 {
2670 p = (char *) opt.curves;
2671 i = 0;
2672
2673 if( strcmp( p, "none" ) == 0 )
2674 {
2675 curve_list[0] = MBEDTLS_ECP_DP_NONE;
2676 }
2677 else if( strcmp( p, "default" ) != 0 )
2678 {
2679 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01002680 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002681 {
2682 q = p;
2683
2684 /* Terminate the current string */
2685 while( *p != ',' && *p != '\0' )
2686 p++;
2687 if( *p == ',' )
2688 *p++ = '\0';
2689
2690 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
2691 {
2692 curve_list[i++] = curve_cur->grp_id;
2693 }
2694 else
2695 {
2696 mbedtls_printf( "unknown curve %s\n", q );
2697 mbedtls_printf( "supported curves: " );
2698 for( curve_cur = mbedtls_ecp_curve_list();
2699 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
2700 curve_cur++ )
2701 {
2702 mbedtls_printf( "%s ", curve_cur->name );
2703 }
2704 mbedtls_printf( "\n" );
2705 goto exit;
2706 }
2707 }
2708
2709 mbedtls_printf("Number of curves: %d\n", i );
2710
Hanno Becker8651a432017-06-09 16:13:22 +01002711 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002712 {
Hanno Becker8651a432017-06-09 16:13:22 +01002713 mbedtls_printf( "curves list too long, maximum %d",
2714 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01002715 goto exit;
2716 }
2717
2718 curve_list[i] = MBEDTLS_ECP_DP_NONE;
2719 }
2720 }
2721#endif /* MBEDTLS_ECP_C */
2722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002724 if( opt.alpn_string != NULL )
2725 {
2726 p = (char *) opt.alpn_string;
2727 i = 0;
2728
2729 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01002730 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002731 {
2732 alpn_list[i++] = p;
2733
2734 /* Terminate the current string and move on to next one */
2735 while( *p != ',' && *p != '\0' )
2736 p++;
2737 if( *p == ',' )
2738 *p++ = '\0';
2739 }
2740 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002742
Paul Bakkerfbb17802013-04-17 19:10:21 +02002743 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002744 * 0. Initialize the RNG and the session data
2745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002747 fflush( stdout );
2748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 mbedtls_entropy_init( &entropy );
Philippe Antoine986b6f22019-06-07 15:04:32 +02002750 if (opt.reproducible)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002751 {
Philippe Antoine738153a2019-06-18 20:16:43 +02002752 srand( 1 );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002753 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy,
Philippe Antoine986b6f22019-06-07 15:04:32 +02002754 &entropy, (const unsigned char *) pers,
2755 strlen( pers ) ) ) != 0 )
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002756 {
2757 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
Philippe Antoine986b6f22019-06-07 15:04:32 +02002758 -ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002759 goto exit;
2760 }
Philippe Antoine986b6f22019-06-07 15:04:32 +02002761 }
2762 else
2763 {
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002764 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
Philippe Antoine986b6f22019-06-07 15:04:32 +02002765 &entropy, (const unsigned char *) pers,
2766 strlen( pers ) ) ) != 0 )
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002767 {
2768 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
Philippe Antoine986b6f22019-06-07 15:04:32 +02002769 -ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002770 goto exit;
2771 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002772 }
2773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002777 /*
2778 * 1.1. Load the trusted CA
2779 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002781 fflush( stdout );
2782
Hanno Becker8174bdf2019-03-05 16:22:07 +00002783 if( strcmp( opt.ca_path, "none" ) == 0 ||
2784 strcmp( opt.ca_file, "none" ) == 0 )
2785 {
2786 ret = 0;
2787 }
2788 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002790 if( strlen( opt.ca_path ) )
Hanno Becker8174bdf2019-03-05 16:22:07 +00002791 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002792 else if( strlen( opt.ca_file ) )
Hanno Becker8174bdf2019-03-05 16:22:07 +00002793 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02002794 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002795#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796#if defined(MBEDTLS_CERTS_C)
Hanno Becker09b8cae2019-02-01 08:13:19 +00002797 {
2798#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801 ret = mbedtls_x509_crt_parse( &cacert,
2802 (const unsigned char *) mbedtls_test_cas[i],
2803 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002804 if( ret != 0 )
2805 break;
2806 }
Hanno Becker09b8cae2019-02-01 08:13:19 +00002807 if( ret == 0 )
2808#endif /* MBEDTLS_PEM_PARSE_C */
2809 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
2810 {
2811 ret = mbedtls_x509_crt_parse_der( &cacert,
2812 (const unsigned char *) mbedtls_test_cas_der[i],
2813 mbedtls_test_cas_der_len[i] );
2814 if( ret != 0 )
2815 break;
2816 }
2817 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002818#else
2819 {
2820 ret = 1;
Hanno Beckera0c5ceb2018-12-05 16:49:42 +00002821 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002822 }
Hanno Becker09b8cae2019-02-01 08:13:19 +00002823#endif /* MBEDTLS_CERTS_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002824 if( ret < 0 )
2825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002827 goto exit;
2828 }
2829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002831
2832 /*
2833 * 1.2. Load own certificate and private key
2834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002836 fflush( stdout );
2837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002839 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002840 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002841 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002845 -ret );
2846 goto exit;
2847 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002848 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002849 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002850 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002851 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002855 goto exit;
2856 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002857 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002858 if( key_cert_init == 1 )
2859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860 mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002861 goto exit;
2862 }
2863
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002864 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002865 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002866 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002870 -ret );
2871 goto exit;
2872 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002873 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002874 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002875 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002876 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002880 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002881 goto exit;
2882 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002883 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002884 if( key_cert_init2 == 1 )
2885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886 mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002887 goto exit;
2888 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002889#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002890 if( key_cert_init == 0 &&
2891 strcmp( opt.crt_file, "none" ) != 0 &&
2892 strcmp( opt.key_file, "none" ) != 0 &&
2893 key_cert_init2 == 0 &&
2894 strcmp( opt.crt_file2, "none" ) != 0 &&
2895 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897#if !defined(MBEDTLS_CERTS_C)
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002898 mbedtls_printf( "Not certificated or key provided, and \nMBEDTLS_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002899 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002900#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901#if defined(MBEDTLS_RSA_C)
2902 if( ( ret = mbedtls_x509_crt_parse( &srvcert,
2903 (const unsigned char *) mbedtls_test_srv_crt_rsa,
2904 mbedtls_test_srv_crt_rsa_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002905 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002906 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
2907 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002908 goto exit;
2909 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 if( ( ret = mbedtls_pk_parse_key( &pkey,
2911 (const unsigned char *) mbedtls_test_srv_key_rsa,
2912 mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002913 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002914 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
2915 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002916 goto exit;
2917 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002918 key_cert_init = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919#endif /* MBEDTLS_RSA_C */
2920#if defined(MBEDTLS_ECDSA_C)
2921 if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
2922 (const unsigned char *) mbedtls_test_srv_crt_ec,
2923 mbedtls_test_srv_crt_ec_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002924 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002925 mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n",
2926 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002927 goto exit;
2928 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929 if( ( ret = mbedtls_pk_parse_key( &pkey2,
2930 (const unsigned char *) mbedtls_test_srv_key_ec,
2931 mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002932 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002933 mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n",
2934 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002935 goto exit;
2936 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002937 key_cert_init2 = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938#endif /* MBEDTLS_ECDSA_C */
2939#endif /* MBEDTLS_CERTS_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002940 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 mbedtls_printf( " ok\n" );
2943#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002946 if( opt.dhm_file != NULL )
2947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948 mbedtls_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002949 fflush( stdout );
2950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002954 -ret );
2955 goto exit;
2956 }
2957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002959 }
2960#endif
2961
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002962#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002963 if( opt.sni != NULL )
2964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 mbedtls_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002966 fflush( stdout );
2967
2968 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
2969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002971 goto exit;
2972 }
2973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002975 }
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002976#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002977
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002978 /*
2979 * 2. Setup the listening TCP socket
2980 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02002981 mbedtls_printf( " . Bind on %s://%s:%s/ ...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002983 opt.server_addr ? opt.server_addr : "*",
2984 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002985 fflush( stdout );
2986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
2988 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2989 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991 mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002992 goto exit;
2993 }
2994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002996
2997 /*
2998 * 3. Setup stuff
2999 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003001 fflush( stdout );
3002
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003003 if( ( ret = mbedtls_ssl_config_defaults( &conf,
3004 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003005 opt.transport,
3006 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003007 {
3008 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
3009 goto exit;
3010 }
3011
Gilles Peskineef86ab22017-05-05 18:59:02 +02003012#if defined(MBEDTLS_X509_CRT_PARSE_C)
3013 /* The default algorithms profile disables SHA-1, but our tests still
3014 rely on it heavily. Hence we allow it here. A real-world server
3015 should use the default profile unless there is a good reason not to. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02003016 if( opt.allow_sha1 > 0 )
3017 {
3018 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
3019 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
Gilles Peskine682df092017-05-11 19:01:11 +02003020 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
Gilles Peskinebc70a182017-05-09 15:59:24 +02003021 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02003022#endif /* MBEDTLS_X509_CRT_PARSE_C */
3023
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01003024 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003025 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003026
Janos Follath4817e272017-04-10 13:44:33 +01003027 if( opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST )
3028 mbedtls_ssl_conf_cert_req_ca_list( &conf, opt.cert_req_ca_list );
3029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02003031 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 +02003032 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003033
Hanno Beckere7675d02018-08-14 13:28:56 +01003034 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01003035 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02003037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003039 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003040 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003041 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003042 goto exit;
3043 };
Paul Bakker05decb22013-08-15 13:33:48 +02003044#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02003045
Hanno Beckera0e20d02019-05-15 14:03:01 +01003046#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003047 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckerad4a1372019-05-03 13:06:44 +01003048 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003049 if( opt.cid_enabled == 1 &&
3050 opt.cid_enabled_renego == 1 &&
3051 cid_len != cid_renego_len )
3052 {
3053 mbedtls_printf( "CID length must not change during renegotiation\n" );
3054 goto usage;
3055 }
3056
3057 if( opt.cid_enabled == 1 )
Hanno Becker8367ccc2019-05-14 11:30:10 +01003058 ret = mbedtls_ssl_conf_cid( &conf, cid_len,
3059 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003060 else
Hanno Becker8367ccc2019-05-14 11:30:10 +01003061 ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
3062 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003063
Hanno Beckerad4a1372019-05-03 13:06:44 +01003064 if( ret != 0 )
3065 {
Hanno Beckerd5eed422019-05-23 16:58:22 +01003066 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
3067 -ret );
Hanno Beckerad4a1372019-05-03 13:06:44 +01003068 goto exit;
3069 }
3070 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003071#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerad4a1372019-05-03 13:06:44 +01003072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003074 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003075 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003076#endif
3077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003079 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003080 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003081#endif
3082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003084 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003085 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003086#endif
3087
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003088#if defined(MBEDTLS_SSL_EXPORT_KEYS)
3089 if( opt.eap_tls != 0 )
Hanno Becker48f3a3d2019-08-29 06:31:22 +01003090 {
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003091 mbedtls_ssl_conf_export_keys_ext_cb( &conf, eap_tls_key_derivation,
3092 &eap_tls_keying );
Hanno Becker48f3a3d2019-08-29 06:31:22 +01003093 }
3094 else if( opt.nss_keylog != 0 )
3095 {
3096 mbedtls_ssl_conf_export_keys_ext_cb( &conf,
3097 nss_keylog_export,
3098 NULL );
3099 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003100#endif
3101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003103 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003104 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003105 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003106 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003107 goto exit;
3108 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003109#endif
3110
Philippe Antoine986b6f22019-06-07 15:04:32 +02003111 if (opt.reproducible)
3112 {
Philippe Antoinef91b3722019-06-11 16:02:43 +02003113#if defined(MBEDTLS_HAVE_TIME)
Philippe Antoineaa4d1522019-06-06 21:24:31 +02003114#if defined(MBEDTLS_PLATFORM_TIME_ALT)
3115 mbedtls_platform_set_time( dummy_constant_time );
3116#else
Philippe Antoine7c9d7242019-06-11 12:11:36 +02003117 fprintf( stderr, "Warning: reproducible option used without constant time\n" );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02003118#endif
Philippe Antoine0ff84fb2019-06-11 12:15:17 +02003119#endif
Philippe Antoine986b6f22019-06-07 15:04:32 +02003120 }
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003121 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
3122 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01003125 if( opt.cache_max != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01003127
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01003128 if( opt.cache_timeout != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129 mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01003130
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003131 mbedtls_ssl_conf_session_cache( &conf, &cache,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01003132 mbedtls_ssl_cache_get,
3133 mbedtls_ssl_cache_set );
Paul Bakker0a597072012-09-25 21:55:46 +00003134#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003137 if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003138 {
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003139 if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
3140 mbedtls_ctr_drbg_random, &ctr_drbg,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +02003141 MBEDTLS_CIPHER_AES_256_GCM,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003142 opt.ticket_timeout ) ) != 0 )
3143 {
3144 mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
3145 goto exit;
3146 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01003147
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003148 mbedtls_ssl_conf_session_tickets_cb( &conf,
3149 mbedtls_ssl_ticket_write,
3150 mbedtls_ssl_ticket_parse,
3151 &ticket_ctx );
3152 }
Paul Bakkera503a632013-08-14 13:48:06 +02003153#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155#if defined(MBEDTLS_SSL_PROTO_DTLS)
3156 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02003157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003159 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02003160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
3162 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164 mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003165 goto exit;
3166 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02003167
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003168 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003169 &cookie_ctx );
3170 }
3171 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172#endif /* MBEDTLS_SSL_COOKIE_C */
3173#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003174 if( opt.cookies == 0 )
3175 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003176 mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003177 }
3178 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003180 {
3181 ; /* Nothing to do */
3182 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003185 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003186 mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02003187#endif
3188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02003190 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003191 mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003192#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02003193 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02003195
Paul Bakker41c83d32013-03-20 14:39:14 +01003196 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003197 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00003198
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02003199#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00003200 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003201 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02003202#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003203
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02003204 if( opt.version_suites != NULL )
3205 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003206 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 MBEDTLS_SSL_MAJOR_VERSION_3,
3208 MBEDTLS_SSL_MINOR_VERSION_0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003209 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210 MBEDTLS_SSL_MAJOR_VERSION_3,
3211 MBEDTLS_SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003212 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213 MBEDTLS_SSL_MAJOR_VERSION_3,
3214 MBEDTLS_SSL_MINOR_VERSION_2 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003215 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 MBEDTLS_SSL_MAJOR_VERSION_3,
3217 MBEDTLS_SSL_MINOR_VERSION_3 );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02003218 }
3219
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01003220 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003221 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003223 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01003224
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02003225 if( opt.renego_delay != DFL_RENEGO_DELAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003226 mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003227
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01003228 if( opt.renego_period != DFL_RENEGO_PERIOD )
3229 {
Andres AG692ad842017-01-19 16:30:57 +00003230 PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003231 mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01003232 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003233#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01003236 if( strcmp( opt.ca_path, "none" ) != 0 &&
3237 strcmp( opt.ca_file, "none" ) != 0 )
3238 {
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02003239#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3240 if( opt.ca_callback != 0 )
3241 mbedtls_ssl_conf_ca_cb( &conf, ca_callback, &cacert);
3242 else
3243#endif
3244 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01003245 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02003246 if( key_cert_init )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003247 {
3248 mbedtls_pk_context *pk = &pkey;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003249#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003250 if( opt.async_private_delay1 >= 0 )
3251 {
Gilles Peskine44817442018-06-13 18:09:28 +02003252 ret = ssl_async_set_key( &ssl_async_keys, &srvcert, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02003253 opt.async_private_delay1 );
3254 if( ret < 0 )
3255 {
3256 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
3257 ret );
3258 goto exit;
3259 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003260 pk = NULL;
3261 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003262#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003263 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003264 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003265 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003266 goto exit;
3267 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003268 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02003269 if( key_cert_init2 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003270 {
3271 mbedtls_pk_context *pk = &pkey2;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003272#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003273 if( opt.async_private_delay2 >= 0 )
3274 {
Gilles Peskine44817442018-06-13 18:09:28 +02003275 ret = ssl_async_set_key( &ssl_async_keys, &srvcert2, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02003276 opt.async_private_delay2 );
3277 if( ret < 0 )
3278 {
3279 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
3280 ret );
3281 goto exit;
3282 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003283 pk = NULL;
3284 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003285#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003286 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003287 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003288 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003289 goto exit;
3290 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003291 }
3292
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003293#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003294 if( opt.async_operations[0] != '-' )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003295 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003296 mbedtls_ssl_async_sign_t *sign = NULL;
3297 mbedtls_ssl_async_decrypt_t *decrypt = NULL;
Gilles Peskine12ab5d42018-04-24 12:32:04 +02003298 const char *r;
3299 for( r = opt.async_operations; *r; r++ )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003300 {
Gilles Peskine12ab5d42018-04-24 12:32:04 +02003301 switch( *r )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003302 {
3303 case 'd':
3304 decrypt = ssl_async_decrypt;
3305 break;
3306 case 's':
3307 sign = ssl_async_sign;
3308 break;
3309 }
3310 }
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003311 ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
3312 - opt.async_private_error :
3313 opt.async_private_error );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003314 ssl_async_keys.f_rng = mbedtls_ctr_drbg_random;
3315 ssl_async_keys.p_rng = &ctr_drbg;
3316 mbedtls_ssl_conf_async_private_cb( &conf,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003317 sign,
3318 decrypt,
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003319 ssl_async_resume,
3320 ssl_async_cancel,
3321 &ssl_async_keys );
3322 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003323#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003324#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +02003325
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02003326#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003327 if( opt.sni != NULL )
Gilles Peskine166ce742018-04-30 10:30:49 +02003328 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003329 mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
Gilles Peskine166ce742018-04-30 10:30:49 +02003330#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3331 if( opt.async_private_delay2 >= 0 )
3332 {
Gilles Peskinee2479892018-06-13 18:06:51 +02003333 sni_entry *cur;
3334 for( cur = sni_info; cur != NULL; cur = cur->next )
Gilles Peskine166ce742018-04-30 10:30:49 +02003335 {
Gilles Peskinee2479892018-06-13 18:06:51 +02003336 ret = ssl_async_set_key( &ssl_async_keys,
Gilles Peskine44817442018-06-13 18:09:28 +02003337 cur->cert, cur->key, 1,
Gilles Peskinee2479892018-06-13 18:06:51 +02003338 opt.async_private_delay2 );
3339 if( ret < 0 )
3340 {
3341 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
3342 ret );
3343 goto exit;
3344 }
3345 cur->key = NULL;
Gilles Peskine166ce742018-04-30 10:30:49 +02003346 }
Gilles Peskine166ce742018-04-30 10:30:49 +02003347 }
3348#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3349 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003350#endif
3351
Hanno Beckere6706e62017-05-15 16:05:15 +01003352#if defined(MBEDTLS_ECP_C)
3353 if( opt.curves != NULL &&
3354 strcmp( opt.curves, "default" ) != 0 )
3355 {
3356 mbedtls_ssl_conf_curves( &conf, curve_list );
3357 }
3358#endif
3359
Gilles Peskineeccd8882020-03-10 12:19:08 +01003360#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003361
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02003362 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
3363 {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003364#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00003365 if( opt.psk_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003366 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00003367 /* The algorithm has already been determined earlier. */
Janos Follathbe4efc22019-08-08 11:38:18 +01003368 status = psa_setup_psk_key_slot( &psk_slot, alg, psk, psk_len );
Hanno Becker1d911cd2018-11-15 13:06:09 +00003369 if( status != PSA_SUCCESS )
3370 {
3371 fprintf( stderr, "SETUP FAIL\n" );
3372 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
3373 goto exit;
3374 }
3375 if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, psk_slot,
3376 (const unsigned char *) opt.psk_identity,
3377 strlen( opt.psk_identity ) ) ) != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003378 {
3379 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
3380 ret );
3381 goto exit;
3382 }
3383 }
3384 else
3385#endif /* MBEDTLS_USE_PSA_CRYPTO */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01003386 if( psk_len > 0 )
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02003387 {
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01003388 ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
3389 (const unsigned char *) opt.psk_identity,
3390 strlen( opt.psk_identity ) );
3391 if( ret != 0 )
3392 {
3393 mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", - ret );
3394 goto exit;
3395 }
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02003396 }
3397 }
3398
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02003399 if( opt.psk_list != NULL )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003400 {
3401#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00003402 if( opt.psk_list_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003403 {
3404 psk_entry *cur_psk;
3405 for( cur_psk = psk_info; cur_psk != NULL; cur_psk = cur_psk->next )
3406 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00003407
Janos Follathbe4efc22019-08-08 11:38:18 +01003408 status = psa_setup_psk_key_slot( &cur_psk->slot, alg,
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003409 cur_psk->key,
3410 cur_psk->key_len );
3411 if( status != PSA_SUCCESS )
3412 {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003413 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
3414 goto exit;
3415 }
3416 }
3417 }
3418#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker1d911cd2018-11-15 13:06:09 +00003419
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003420 mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003421 }
Paul Bakkered27a042013-04-18 22:46:23 +02003422#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424#if defined(MBEDTLS_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00003425 /*
3426 * Use different group than default DHM group
3427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003429 if( opt.dhm_file != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003430 ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003431#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003432 if( ret != 0 )
3433 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003434 mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003435 goto exit;
3436 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003437#endif
3438
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02003439 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003440 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00003441
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02003442 if( opt.max_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003443 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakkerc1516be2013-06-29 16:01:32 +02003444
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003445 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
3446 {
3447 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
3448 goto exit;
3449 }
3450
Hanno Beckerdcc94e62019-07-03 17:05:43 +01003451 io_ctx.ssl = &ssl;
3452 io_ctx.net = &client_fd;
3453 mbedtls_ssl_set_bio( &ssl, &io_ctx, send_cb, recv_cb,
3454 opt.nbio == 0 ? recv_timeout_cb : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003455
Hanno Beckera0e20d02019-05-15 14:03:01 +01003456#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +01003457 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3458 {
3459 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
3460 cid, cid_len ) ) != 0 )
3461 {
3462 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
3463 ret );
3464 goto exit;
3465 }
3466 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003467#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +01003468
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02003469#if defined(MBEDTLS_SSL_PROTO_DTLS)
3470 if( opt.dtls_mtu != DFL_DTLS_MTU )
3471 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
3472#endif
3473
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003474#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02003475 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
3476 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003477#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02003478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003480
3481reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02003482#if !defined(_WIN32)
3483 if( received_sigterm )
3484 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01003485 mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" );
3486 if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT )
3487 ret = 0;
3488
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02003489 goto exit;
3490 }
3491#endif
3492
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003493 if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
3494 {
3495 mbedtls_printf( " ! Client initiated reconnection from same port\n" );
3496 goto handshake;
3497 }
3498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003500 if( ret != 0 )
3501 {
3502 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 mbedtls_strerror( ret, error_buf, 100 );
3504 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003505 }
3506#endif
3507
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003508 mbedtls_net_free( &client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01003509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510 mbedtls_ssl_session_reset( &ssl );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003511
3512 /*
3513 * 3. Wait until a client connects
3514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 mbedtls_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003516 fflush( stdout );
3517
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003518 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02003519 client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003520 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02003521#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003522 if( received_sigterm )
3523 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01003524 mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" );
3525 if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED )
3526 ret = 0;
3527
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003528 goto exit;
3529 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02003530#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003533 goto exit;
3534 }
3535
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003536 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003537 ret = mbedtls_net_set_nonblock( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003538 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003539 ret = mbedtls_net_set_block( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003540 if( ret != 0 )
3541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003543 goto exit;
3544 }
3545
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003546 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
3549 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003550 {
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02003551 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
3552 client_ip, cliip_len ) ) != 0 )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003553 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01003554 mbedtls_printf( " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n",
3555 -ret );
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003556 goto exit;
3557 }
3558 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003560
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02003561#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3562 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
3563 {
3564 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
3565 (const unsigned char *) opt.ecjpake_pw,
3566 strlen( opt.ecjpake_pw ) ) ) != 0 )
3567 {
3568 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
3569 goto exit;
3570 }
3571 }
3572#endif
3573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003575
3576 /*
3577 * 4. Handshake
3578 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003579handshake:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003581 fflush( stdout );
3582
Hanno Becker16970d22017-10-10 15:56:37 +01003583 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003584 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003585#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003586 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003587 ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003588 {
3589 mbedtls_printf( " cancelling on injected error\n" );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003590 break;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003591 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003592#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskineb44692f2018-04-24 12:18:19 +02003593
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003594 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003595 break;
3596
3597 /* For event-driven IO, wait for socket to become available */
3598 if( opt.event == 1 /* level triggered IO */ )
3599 {
3600#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003601 ret = idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003602#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003603 ret = idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003604#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003605 if( ret != 0 )
3606 goto reset;
Hanno Becker16970d22017-10-10 15:56:37 +01003607 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003608 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 mbedtls_printf( " hello verification requested\n" );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003613 ret = 0;
3614 goto reset;
3615 }
3616 else if( ret != 0 )
3617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02003619
3620#if defined(MBEDTLS_X509_CRT_PARSE_C)
3621 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3622 {
3623 char vrfy_buf[512];
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02003624 flags = mbedtls_ssl_get_verify_result( &ssl );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02003625
3626 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
3627
3628 mbedtls_printf( "%s\n", vrfy_buf );
3629 }
3630#endif
3631
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003632#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003633 if( opt.async_private_error < 0 )
3634 /* Injected error only the first time round, to test reset */
3635 ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
3636#endif
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003637 goto reset;
3638 }
3639 else /* ret == 0 */
3640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
3642 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003643 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003645 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
3646 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02003647 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02003649
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003650#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003651 mbedtls_printf( " [ Maximum input fragment length is %u ]\n",
3652 (unsigned int) mbedtls_ssl_get_input_max_frag_len( &ssl ) );
3653 mbedtls_printf( " [ Maximum output fragment length is %u ]\n",
3654 (unsigned int) mbedtls_ssl_get_output_max_frag_len( &ssl ) );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003655#endif
3656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003658 if( opt.alpn_string != NULL )
3659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
3661 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003662 alp ? alp : "(none)" );
3663 }
3664#endif
3665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003667 /*
Ron Eldor2a47be52017-06-20 15:23:23 +03003668 * 5. Verify the client certificate
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003671
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003672 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003673 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003674 char vrfy_buf[512];
3675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003676 mbedtls_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003677
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003678 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003679
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003680 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003681 }
3682 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003684
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003685 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003686 {
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003687 char crt_buf[512];
3688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689 mbedtls_printf( " . Peer certificate information ...\n" );
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003690 mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003691 mbedtls_ssl_get_peer_cert( &ssl ) );
Manuel Pégourié-Gonnard67557172015-07-02 11:15:48 +02003692 mbedtls_printf( "%s\n", crt_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003693 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003695
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003696#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Ron Eldor51d3ab52019-05-12 14:54:30 +03003697 if( opt.eap_tls != 0 )
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003698 {
3699 size_t j = 0;
Ron Eldor51d3ab52019-05-12 14:54:30 +03003700
3701 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type,
3702 eap_tls_keying.master_secret,
3703 sizeof( eap_tls_keying.master_secret ),
3704 eap_tls_label,
3705 eap_tls_keying.randbytes,
3706 sizeof( eap_tls_keying.randbytes ),
3707 eap_tls_keymaterial,
3708 sizeof( eap_tls_keymaterial ) ) )
3709 != 0 )
3710 {
3711 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
3712 -ret );
3713 goto exit;
3714 }
3715
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003716 mbedtls_printf( " EAP-TLS key material is:" );
3717 for( j = 0; j < sizeof( eap_tls_keymaterial ); j++ )
3718 {
3719 if( j % 8 == 0 )
3720 mbedtls_printf("\n ");
3721 mbedtls_printf("%02x ", eap_tls_keymaterial[j] );
3722 }
3723 mbedtls_printf("\n");
3724
Ron Eldor51d3ab52019-05-12 14:54:30 +03003725 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type, NULL, 0,
Ron Eldor51c45072019-05-15 17:49:54 +03003726 eap_tls_label,
3727 eap_tls_keying.randbytes,
3728 sizeof( eap_tls_keying.randbytes ),
3729 eap_tls_iv,
3730 sizeof( eap_tls_iv ) ) ) != 0 )
Ron Eldor51d3ab52019-05-12 14:54:30 +03003731 {
3732 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
3733 -ret );
3734 goto exit;
3735 }
3736
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003737 mbedtls_printf( " EAP-TLS IV is:" );
3738 for( j = 0; j < sizeof( eap_tls_iv ); j++ )
3739 {
3740 if( j % 8 == 0 )
3741 mbedtls_printf("\n ");
3742 mbedtls_printf("%02x ", eap_tls_iv[j] );
3743 }
3744 mbedtls_printf("\n");
3745 }
3746#endif
Hanno Beckera7d25422019-04-09 17:28:10 +01003747
Hanno Beckera0e20d02019-05-15 14:03:01 +01003748#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003749 ret = report_cid_usage( &ssl, "initial handshake" );
3750 if( ret != 0 )
3751 goto exit;
3752
Hanno Beckera7d25422019-04-09 17:28:10 +01003753 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3754 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003755 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
3756 cid_renego, cid_renego_len ) ) != 0 )
Hanno Beckera7d25422019-04-09 17:28:10 +01003757 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003758 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
3759 ret );
Hanno Beckera7d25422019-04-09 17:28:10 +01003760 goto exit;
3761 }
Hanno Beckera7d25422019-04-09 17:28:10 +01003762 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003763#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +01003764
Piotr Nowicki0937ed22019-11-26 16:32:40 +01003765#if defined(MBEDTLS_MEMORY_DEBUG)
3766 mbedtls_memory_buffer_alloc_cur_get( &current_heap_memory, &heap_blocks );
3767 mbedtls_memory_buffer_alloc_max_get( &peak_heap_memory, &heap_blocks );
3768 mbedtls_printf( "Heap memory usage after handshake: %lu bytes. Peak memory usage was %lu\n",
3769 (unsigned long) current_heap_memory, (unsigned long) peak_heap_memory );
3770#endif /* MBEDTLS_MEMORY_DEBUG */
3771
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02003772 if( opt.exchanges == 0 )
3773 goto close_notify;
3774
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003775 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003776data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003777 /*
3778 * 6. Read the HTTP Request
3779 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 mbedtls_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003781 fflush( stdout );
3782
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003783 /*
3784 * TLS and DTLS need different reading styles (stream vs datagram)
3785 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003786 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003787 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003788 do
3789 {
3790 int terminated = 0;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003791 len = opt.buffer_size - 1;
3792 memset( buf, 0, opt.buffer_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003793 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003794
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003795 if( mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003796 {
3797 if( opt.event == 1 /* level triggered IO */ )
3798 {
3799#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003800 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003801#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003802 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003803#endif
3804 }
3805
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003806 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01003807 }
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003808
3809 if( ret <= 0 )
3810 {
3811 switch( ret )
3812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3814 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003815 goto close_notify;
3816
3817 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818 case MBEDTLS_ERR_NET_CONN_RESET:
3819 mbedtls_printf( " connection was reset by peer\n" );
3820 ret = MBEDTLS_ERR_NET_CONN_RESET;
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003821 goto reset;
3822
3823 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003825 goto reset;
3826 }
3827 }
3828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003829 if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003830 {
3831 len = ret;
3832 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003833 mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003834
3835 /* End of message should be detected according to the syntax of the
3836 * application protocol (eg HTTP), just use a dummy test here. */
3837 if( buf[len - 1] == '\n' )
3838 terminated = 1;
3839 }
3840 else
3841 {
3842 int extra_len, ori_len;
3843 unsigned char *larger_buf;
3844
3845 ori_len = ret;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02003846 extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003847
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003848 larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003849 if( larger_buf == NULL )
3850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851 mbedtls_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003852 ret = 1;
3853 goto reset;
3854 }
3855
3856 memset( larger_buf, 0, ori_len + extra_len );
3857 memcpy( larger_buf, buf, ori_len );
3858
3859 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003861 if( ret != extra_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862 mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003864 mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003865 ret = 1;
3866 goto reset;
3867 }
3868
3869 larger_buf[ori_len + extra_len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003870 mbedtls_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003871 ori_len + extra_len, ori_len, extra_len,
3872 (char *) larger_buf );
3873
3874 /* End of message should be detected according to the syntax of the
3875 * application protocol (eg HTTP), just use a dummy test here. */
3876 if( larger_buf[ori_len + extra_len - 1] == '\n' )
3877 terminated = 1;
3878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879 mbedtls_free( larger_buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003880 }
3881
3882 if( terminated )
3883 {
3884 ret = 0;
3885 break;
3886 }
3887 }
3888 while( 1 );
3889 }
3890 else /* Not stream, so datagram */
3891 {
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003892 len = opt.buffer_size - 1;
3893 memset( buf, 0, opt.buffer_size );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003894
Gilles Peskineb44692f2018-04-24 12:18:19 +02003895 do
Hanno Becker16970d22017-10-10 15:56:37 +01003896 {
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003897 /* Without the call to `mbedtls_ssl_check_pending`, it might
3898 * happen that the client sends application data in the same
3899 * datagram as the Finished message concluding the handshake.
3900 * In this case, the application data would be ready to be
3901 * processed while the underlying transport wouldn't signal
3902 * any further incoming data.
3903 *
3904 * See the test 'Event-driven I/O: session-id resume, UDP packing'
3905 * in tests/ssl-opt.sh.
3906 */
3907
3908 /* For event-driven IO, wait for socket to become available */
3909 if( mbedtls_ssl_check_pending( &ssl ) == 0 &&
3910 opt.event == 1 /* level triggered IO */ )
3911 {
3912#if defined(MBEDTLS_TIMING_C)
3913 idle( &client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ );
3914#else
3915 idle( &client_fd, MBEDTLS_ERR_SSL_WANT_READ );
3916#endif
3917 }
3918
Hanno Becker16970d22017-10-10 15:56:37 +01003919 ret = mbedtls_ssl_read( &ssl, buf, len );
3920
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003921 /* Note that even if `mbedtls_ssl_check_pending` returns true,
3922 * it can happen that the subsequent call to `mbedtls_ssl_read`
3923 * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
3924 * might be discarded (e.g. because they are retransmissions). */
Hanno Becker16970d22017-10-10 15:56:37 +01003925 }
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003926 while( mbedtls_status_is_ssl_in_progress( ret ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003927
3928 if( ret <= 0 )
3929 {
3930 switch( ret )
3931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003932 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3933 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003934 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003935 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003936
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003937 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003939 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003940 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003941 }
3942
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003943 len = ret;
3944 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003945 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003946 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003947 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003948
3949 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003950 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02003951 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003953#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00003954 if( opt.renegotiate && exchanges_left == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956 mbedtls_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003957 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003960 {
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003961 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003963 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003964 goto reset;
3965 }
Hanno Becker16970d22017-10-10 15:56:37 +01003966
3967 /* For event-driven IO, wait for socket to become available */
3968 if( opt.event == 1 /* level triggered IO */ )
3969 {
3970#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003971 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003972#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003973 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003974#endif
3975 }
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003976 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003979 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003980#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003981
Hanno Beckera0e20d02019-05-15 14:03:01 +01003982#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003983 ret = report_cid_usage( &ssl, "after renegotiation" );
3984 if( ret != 0 )
3985 goto exit;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003986#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003987
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003988 /*
3989 * 7. Write the 200 Response
3990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991 mbedtls_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003992 fflush( stdout );
3993
3994 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003996
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003997 /* Add padding to the response to reach opt.response_size in length */
3998 if( opt.response_size != DFL_RESPONSE_SIZE &&
3999 len < opt.response_size )
4000 {
4001 memset( buf + len, 'B', opt.response_size - len );
4002 len += opt.response_size - len;
4003 }
4004
4005 /* Truncate if response size is smaller than the "natural" size */
4006 if( opt.response_size != DFL_RESPONSE_SIZE &&
4007 len > opt.response_size )
4008 {
4009 len = opt.response_size;
4010
4011 /* Still end with \r\n unless that's really not possible */
4012 if( len >= 2 ) buf[len - 2] = '\r';
4013 if( len >= 1 ) buf[len - 1] = '\n';
4014 }
4015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004017 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004018 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004021 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02004022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004025 mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004026 goto reset;
4027 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004028
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02004029 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004031 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004032 goto reset;
4033 }
Hanno Becker16970d22017-10-10 15:56:37 +01004034
4035 /* For event-driven IO, wait for socket to become available */
4036 if( opt.event == 1 /* level triggered IO */ )
4037 {
4038#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00004039 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01004040#else
Hanno Becker197a91c2017-10-31 10:58:53 +00004041 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01004042#endif
4043 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02004044 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004045 }
4046 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004047 else /* Not stream, so datagram */
4048 {
Hanno Becker16970d22017-10-10 15:56:37 +01004049 while( 1 )
4050 {
4051 ret = mbedtls_ssl_write( &ssl, buf, len );
4052
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02004053 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01004054 break;
4055
4056 /* For event-driven IO, wait for socket to become available */
4057 if( opt.event == 1 /* level triggered IO */ )
4058 {
4059#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00004060 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01004061#else
Hanno Becker197a91c2017-10-31 10:58:53 +00004062 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01004063#endif
4064 }
4065 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004066
4067 if( ret < 0 )
4068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004069 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004070 goto reset;
4071 }
4072
4073 frags = 1;
4074 written = ret;
4075 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004076
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02004077 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078 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 +01004079 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01004080
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02004081 /*
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004082 * 7b. Simulate serialize/deserialize and go back to data exchange
4083 */
Jarno Lamsabbc7b412019-06-04 11:06:31 +03004084#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004085 if( opt.serialize != 0 )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004086 {
Jarno Lamsa15b3a7a2019-06-06 15:10:07 +03004087 size_t buf_len;
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004088
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004089 mbedtls_printf( " . Serializing live connection..." );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004090
Jarno Lamsa15b3a7a2019-06-06 15:10:07 +03004091 ret = mbedtls_ssl_context_save( &ssl, NULL, 0, &buf_len );
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004092 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004093 {
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004094 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
4095 "-0x%x\n\n", -ret );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004096
4097 goto exit;
4098 }
4099
Jarno Lamsa15b3a7a2019-06-06 15:10:07 +03004100 if( ( context_buf = mbedtls_calloc( 1, buf_len ) ) == NULL )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004101 {
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004102 mbedtls_printf( " failed\n ! Couldn't allocate buffer for "
4103 "serialized context" );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004104
4105 goto exit;
4106 }
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02004107 context_buf_len = buf_len;
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004108
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004109 if( ( ret = mbedtls_ssl_context_save( &ssl, context_buf,
4110 buf_len, &buf_len ) ) != 0 )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004111 {
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004112 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004113 "-0x%x\n\n", -ret );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004114
4115 goto exit;
4116 }
4117
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004118 mbedtls_printf( " ok\n" );
4119
Piotr Nowicki3de298f2020-04-16 14:35:19 +02004120 /* Save serialized context to the 'opt.context_file' as a base64 code */
4121 if( 0 < strlen( opt.context_file ) )
4122 {
4123 FILE *b64_file;
4124 uint8_t *b64_buf;
4125 size_t b64_len;
4126
4127 mbedtls_printf( " . Save serialized context to a file... " );
4128
4129 mbedtls_base64_encode( NULL, 0, &b64_len, context_buf, buf_len );
4130
4131 if( ( b64_buf = mbedtls_calloc( 1, b64_len ) ) == NULL )
4132 {
4133 mbedtls_printf( "failed\n ! Couldn't allocate buffer for "
4134 "the base64 code\n" );
4135 goto exit;
4136 }
4137
4138 if( ( ret = mbedtls_base64_encode( b64_buf, b64_len, &b64_len,
4139 context_buf, buf_len ) ) != 0 )
4140 {
4141 mbedtls_printf( "failed\n ! mbedtls_base64_encode returned "
4142 "-0x%x\n", -ret );
4143 mbedtls_free( b64_buf );
4144 goto exit;
4145 }
4146
4147 if( ( b64_file = fopen( opt.context_file, "w" ) ) == NULL )
4148 {
4149 mbedtls_printf( "failed\n ! Cannot open '%s' for writing.\n",
4150 opt.context_file );
4151 mbedtls_free( b64_buf );
4152 goto exit;
4153 }
4154
4155 if( b64_len != fwrite( b64_buf, 1, b64_len, b64_file ) )
4156 {
4157 mbedtls_printf( "failed\n ! fwrite(%ld bytes) failed\n",
4158 (long) b64_len );
4159 mbedtls_free( b64_buf );
4160 fclose( b64_file );
4161 goto exit;
4162 }
4163
4164 mbedtls_free( b64_buf );
4165 fclose( b64_file );
4166
4167 mbedtls_printf( "ok\n" );
4168 }
4169
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004170 /*
4171 * This simulates a workflow where you have a long-lived server
4172 * instance, potentially with a pool of ssl_context objects, and you
4173 * just want to re-use one while the connection is inactive: in that
4174 * case you can just reset() it, and then it's ready to receive
4175 * serialized data from another connection (or the same here).
4176 */
4177 if( opt.serialize == 1 )
4178 {
Manuel Pégourié-Gonnard9df5a822019-07-23 14:51:09 +02004179 /* nothing to do here, done by context_save() already */
Piotr Nowicki136bebf2020-04-17 14:37:00 +02004180 mbedtls_printf( " . Context has been reset... ok\n" );
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004181 }
4182
4183 /*
4184 * This simulates a workflow where you have one server instance per
4185 * connection, and want to release it entire when the connection is
4186 * inactive, and spawn it again when needed again - this would happen
4187 * between ssl_free() and ssl_init() below, together with any other
4188 * teardown/startup code needed - for example, preparing the
4189 * ssl_config again (see section 3 "setup stuff" in this file).
4190 */
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004191 if( opt.serialize == 2 )
4192 {
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004193 mbedtls_printf( " . Freeing and reinitializing context..." );
4194
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004195 mbedtls_ssl_free( &ssl );
4196
4197 mbedtls_ssl_init( &ssl );
4198
4199 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
4200 {
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004201 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned "
4202 "-0x%x\n\n", -ret );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004203 goto exit;
4204 }
4205
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004206 /*
4207 * This illustrates the minimum amount of things you need to set
4208 * up, however you could set up much more if desired, for example
4209 * if you want to share your set up code between the case of
4210 * establishing a new connection and this case.
4211 */
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004212 if( opt.nbio == 2 )
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004213 mbedtls_ssl_set_bio( &ssl, &client_fd, delayed_send,
4214 delayed_recv, NULL );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004215 else
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004216 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send,
4217 mbedtls_net_recv,
4218 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004219
Jarno Lamsa8e253212019-06-13 11:45:06 +03004220#if defined(MBEDTLS_TIMING_C)
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004221 mbedtls_ssl_set_timer_cb( &ssl, &timer,
4222 mbedtls_timing_set_delay,
4223 mbedtls_timing_get_delay );
Jarno Lamsa8e253212019-06-13 11:45:06 +03004224#endif /* MBEDTLS_TIMING_C */
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004225
4226 mbedtls_printf( " ok\n" );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004227 }
4228
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004229 mbedtls_printf( " . Deserializing connection..." );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004230
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004231 if( ( ret = mbedtls_ssl_context_load( &ssl, context_buf,
4232 buf_len ) ) != 0 )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004233 {
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004234 mbedtls_printf( "failed\n ! mbedtls_ssl_context_load returned "
4235 "-0x%x\n\n", -ret );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004236
4237 goto exit;
4238 }
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004239
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02004240 mbedtls_free( context_buf );
4241 context_buf = NULL;
4242 context_buf_len = 0;
4243
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004244 mbedtls_printf( " ok\n" );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004245 }
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004246#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004247
4248 /*
4249 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02004250 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00004251 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02004252 goto data_exchange;
4253
4254 /*
4255 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02004256 */
4257close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004258 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01004259
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02004260 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01004262 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02004263 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02004264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265 mbedtls_printf( " done\n" );
Rich Evansf90016a2015-01-19 14:26:37 +00004266
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004267 goto reset;
4268
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02004269 /*
4270 * Cleanup and exit
4271 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004272exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004273#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004274 if( ret != 0 )
4275 {
4276 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277 mbedtls_strerror( ret, error_buf, 100 );
4278 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004279 }
4280#endif
4281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282 mbedtls_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01004283 fflush( stdout );
4284
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02004285 mbedtls_net_free( &client_fd );
4286 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +02004287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004288#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
4289 mbedtls_dhm_free( &dhm );
Paul Bakkera317a982014-06-18 16:44:11 +02004290#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004291#if defined(MBEDTLS_X509_CRT_PARSE_C)
4292 mbedtls_x509_crt_free( &cacert );
4293 mbedtls_x509_crt_free( &srvcert );
4294 mbedtls_pk_free( &pkey );
4295 mbedtls_x509_crt_free( &srvcert2 );
4296 mbedtls_pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02004297#endif
Gilles Peskine44817442018-06-13 18:09:28 +02004298#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
4299 for( i = 0; (size_t) i < ssl_async_keys.slots_used; i++ )
4300 {
4301 if( ssl_async_keys.slots[i].pk_owned )
4302 {
4303 mbedtls_pk_free( ssl_async_keys.slots[i].pk );
4304 mbedtls_free( ssl_async_keys.slots[i].pk );
4305 ssl_async_keys.slots[i].pk = NULL;
4306 }
4307 }
4308#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02004309#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01004310 sni_free( sni_info );
4311#endif
Gilles Peskineeccd8882020-03-10 12:19:08 +01004312#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004313 if( ( ret = psk_free( psk_info ) ) != 0 )
4314 mbedtls_printf( "Failed to list of opaque PSKs - error was %d\n", ret );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02004315#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004316#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
4317 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02004318#endif
Paul Bakkered27a042013-04-18 22:46:23 +02004319
Gilles Peskineeccd8882020-03-10 12:19:08 +01004320#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) && \
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004321 defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00004322 if( opt.psk_opaque != 0 )
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004323 {
4324 /* This is ok even if the slot hasn't been
4325 * initialized (we might have jumed here
4326 * immediately because of bad cmd line params,
4327 * for example). */
Hanno Becker1d911cd2018-11-15 13:06:09 +00004328 status = psa_destroy_key( psk_slot );
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004329 if( status != PSA_SUCCESS )
4330 {
4331 mbedtls_printf( "Failed to destroy key slot %u - error was %d",
Hanno Becker1d911cd2018-11-15 13:06:09 +00004332 (unsigned) psk_slot, (int) status );
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004333 }
4334 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01004335#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED &&
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004336 MBEDTLS_USE_PSA_CRYPTO */
4337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02004339 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340 mbedtls_ctr_drbg_free( &ctr_drbg );
4341 mbedtls_entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004343#if defined(MBEDTLS_SSL_CACHE_C)
4344 mbedtls_ssl_cache_free( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00004345#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004346#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4347 mbedtls_ssl_ticket_free( &ticket_ctx );
4348#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004349#if defined(MBEDTLS_SSL_COOKIE_C)
4350 mbedtls_ssl_cookie_free( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02004351#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004352
Hanno Becker095d9cf2018-10-09 12:39:13 +01004353 mbedtls_free( buf );
4354
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02004355#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
4356 if( context_buf != NULL )
4357 mbedtls_platform_zeroize( context_buf, context_buf_len );
4358 mbedtls_free( context_buf );
4359#endif
4360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004361#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
4362#if defined(MBEDTLS_MEMORY_DEBUG)
4363 mbedtls_memory_buffer_alloc_status();
Paul Bakker82024bf2013-07-04 11:52:32 +02004364#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004365 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +02004366#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02004367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004368 mbedtls_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01004369
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004370#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004371 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004372 fflush( stdout ); getchar();
4373#endif
4374
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02004375 // Shell can not handle large exit numbers -> 1 for errors
4376 if( ret < 0 )
4377 ret = 1;
4378
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004379 return( ret );
4380}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
4382 MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02004383 MBEDTLS_CTR_DRBG_C */