blob: 76b1ba6cc09d1870bcf3c51dee590ac5b0214d6b [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"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000066
Hanno Becker5a9942e2018-11-12 17:47:48 +000067#if defined(MBEDTLS_USE_PSA_CRYPTO)
68#include "psa/crypto.h"
Hanno Becker1d911cd2018-11-15 13:06:09 +000069#include "mbedtls/psa_util.h"
Hanno Becker5a9942e2018-11-12 17:47:48 +000070#endif
71
Rich Evans18b78c72015-02-11 14:06:19 +000072#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
Andres AG692ad842017-01-19 16:30:57 +000075#include <stdint.h>
Andres AG0b736db2017-02-08 14:05:57 +000076
77#if !defined(_MSC_VER)
Andres AG692ad842017-01-19 16:30:57 +000078#include <inttypes.h>
Andres AG0b736db2017-02-08 14:05:57 +000079#endif
Rich Evans18b78c72015-02-11 14:06:19 +000080
81#if !defined(_WIN32)
82#include <signal.h>
83#endif
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000086#include "mbedtls/ssl_cache.h"
Paul Bakker0a597072012-09-25 21:55:46 +000087#endif
88
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020089#if defined(MBEDTLS_SSL_TICKET_C)
90#include "mbedtls/ssl_ticket.h"
91#endif
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000094#include "mbedtls/ssl_cookie.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020095#endif
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000098#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker82024bf2013-07-04 11:52:32 +020099#endif
100
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200101#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO)
102#define SNI_OPTION
103#endif
104
105#if defined(_WIN32)
106#include <windows.h>
107#endif
108
Simon Butchercce68be2018-07-23 14:26:09 +0100109/* Size of memory to be allocated for the heap, when using the library's memory
110 * management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */
111#define MEMORY_HEAP_SIZE 120000
112
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100113#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200114#define DFL_SERVER_PORT "4433"
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200115#define DFL_RESPONSE_SIZE -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000116#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100117#define DFL_NBIO 0
Hanno Becker16970d22017-10-10 15:56:37 +0100118#define DFL_EVENT 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200119#define DFL_READ_TIMEOUT 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000120#define DFL_CA_FILE ""
121#define DFL_CA_PATH ""
122#define DFL_CRT_FILE ""
123#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200124#define DFL_CRT_FILE2 ""
125#define DFL_KEY_FILE2 ""
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100126#define DFL_ASYNC_OPERATIONS "-"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100127#define DFL_ASYNC_PRIVATE_DELAY1 ( -1 )
128#define DFL_ASYNC_PRIVATE_DELAY2 ( -1 )
Gilles Peskine3665f1d2018-01-05 21:22:12 +0100129#define DFL_ASYNC_PRIVATE_ERROR ( 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +0200130#define DFL_PSK ""
Hanno Becker1d911cd2018-11-15 13:06:09 +0000131#define DFL_PSK_OPAQUE 0
132#define DFL_PSK_LIST_OPAQUE 0
Paul Bakkerfbb17802013-04-17 19:10:21 +0200133#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200134#define DFL_ECJPAKE_PW NULL
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200135#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000136#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200137#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100139#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100140#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200141#define DFL_RENEGO_DELAY -2
Andres AG692ad842017-01-19 16:30:57 +0000142#define DFL_RENEGO_PERIOD ( (uint64_t)-1 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200143#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200144#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200145#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000146#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +0200147#define DFL_SHA1 -1
Hanno Beckera7d25422019-04-09 17:28:10 +0100148#define DFL_CID_ENABLED 0
149#define DFL_CID_VALUE ""
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100150#define DFL_CID_ENABLED_RENEGO -1
151#define DFL_CID_VALUE_RENEGO NULL
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100152#define DFL_AUTH_MODE -1
Janos Follath4817e272017-04-10 13:44:33 +0100153#define DFL_CERT_REQ_CA_LIST MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100155#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200157#define DFL_TICKET_TIMEOUT 86400
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100158#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100159#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100160#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200161#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100162#define DFL_CURVES NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200163#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200165#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200166#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200167#define DFL_HS_TO_MIN 0
168#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200169#define DFL_DTLS_MTU -1
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200170#define DFL_BADMAC_LIMIT -1
Hanno Beckere7675d02018-08-14 13:28:56 +0100171#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200172#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100173#define DFL_ETM -1
Jarno Lamsa9831c8a2019-05-29 13:33:32 +0300174#define DFL_SERIALIZE 0
175#define DFL_EXTENDED_MS_ENFORCE -1
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200176#define DFL_CA_CALLBACK 0
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300177#define DFL_EAP_TLS 0
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200178#define DFL_REPRODUCIBLE 0
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100179#define DFL_NSS_KEYLOG 0
180#define DFL_NSS_KEYLOG_FILE NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000181
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200182#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
183 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
184 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
185 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
186 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
187 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
188 "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 +0200189
Paul Bakker8e714d72013-07-18 11:05:13 +0200190/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
191 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000192#define HTTP_RESPONSE \
193 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000194 "<h2>mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200195 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000196
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200197/*
198 * Size of the basic I/O buffer. Able to hold our default response.
199 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 * 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 +0200201 * if you change this value to something outside the range <= 100 or > 500
202 */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200203#define DFL_IO_BUF_LEN 200
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#if defined(MBEDTLS_X509_CRT_PARSE_C)
206#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000207#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100208 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
209 " default: \"\" (pre-loaded)\n" \
Hanno Becker422d1992019-05-01 11:16:28 +0100210 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100211 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
212 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
Hanno Becker422d1992019-05-01 11:16:28 +0100213 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100214 " 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 +0200215 " default: see note after key_file2\n" \
216 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200217 " 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 +0200218 " default: see note after key_file2\n" \
219 " key_file2=%%s default: see note below\n" \
220 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200221 " preloaded certificate(s) and key(s) are used if available\n" \
222 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
223 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000224#else
225#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200226 "\n" \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 " No file operations available (MBEDTLS_FS_IO not defined)\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200228 "\n"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200230#else
231#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200233
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200234#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100235#define USAGE_SSL_ASYNC \
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100236 " async_operations=%%c... d=decrypt, s=sign (default: -=off)\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100237 " async_private_delay1=%%d Asynchronous delay for key_file or preloaded key\n" \
Gilles Peskine166ce742018-04-30 10:30:49 +0200238 " async_private_delay2=%%d Asynchronous delay for key_file2 and sni\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100239 " default: -1 (not asynchronous)\n" \
Gilles Peskine60ee4ca2018-01-08 11:28:05 +0100240 " async_private_error=%%d Async callback error injection (default=0=none,\n" \
Gilles Peskinec9125722018-04-26 07:15:40 +0200241 " 1=start, 2=cancel, 3=resume, negative=first time only)"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100242#else
243#define USAGE_SSL_ASYNC ""
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200244#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100245
Hanno Beckera0e20d02019-05-15 14:03:01 +0100246#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +0100247#define USAGE_CID \
248 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
249 " default: 0 (disabled)\n" \
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100250 " 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 +0100251 " default: same as 'cid' parameter\n" \
Hanno Beckera7d25422019-04-09 17:28:10 +0100252 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100253 " default: \"\"\n" \
254 " 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 +0100255 " default: same as 'cid_val' parameter\n"
Hanno Beckera0e20d02019-05-15 14:03:01 +0100256#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +0100257#define USAGE_CID ""
Hanno Beckera0e20d02019-05-15 14:03:01 +0100258#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +0100259
Gilles Peskineeccd8882020-03-10 12:19:08 +0100260#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100261#define USAGE_PSK_RAW \
Piotr Nowicki9926eaf2019-11-20 14:54:36 +0100262 " psk=%%s default: \"\" (disabled)\n" \
263 " The PSK values are in hex, without 0x.\n" \
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100264 " psk_list=%%s default: \"\"\n" \
Andrzej Kurekb274f272019-02-05 05:06:35 -0500265 " A list of (PSK identity, PSK value) pairs.\n" \
266 " The PSK values are in hex, without 0x.\n" \
267 " id1,psk1[,id2,psk2[,...]]\n" \
268 " psk_identity=%%s default: \"Client_identity\"\n"
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100269#if defined(MBEDTLS_USE_PSA_CRYPTO)
270#define USAGE_PSK_SLOT \
Hanno Becker1d911cd2018-11-15 13:06:09 +0000271 " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
272 " Enable this to store the PSK configured through command line\n" \
273 " parameter `psk` in a PSA-based key slot.\n" \
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100274 " Note: Currently only supported in conjunction with\n" \
275 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
276 " to force a particular PSK-only ciphersuite.\n" \
277 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
278 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
279 " with prepopulated key slots instead of importing raw key material.\n" \
Hanno Becker1d911cd2018-11-15 13:06:09 +0000280 " psk_list_opaque=%%d default: 0 (don't use opaque dynamic PSKs)\n" \
281 " Enable this to store the list of dynamically chosen PSKs configured\n" \
282 " through the command line parameter `psk_list` in PSA-based key slots.\n" \
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100283 " Note: Currently only supported in conjunction with\n" \
284 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
285 " to force a particular PSK-only ciphersuite.\n" \
286 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
287 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
288 " with prepopulated key slots instead of importing raw key material.\n"
289#else
290#define USAGE_PSK_SLOT ""
291#endif /* MBEDTLS_USE_PSA_CRYPTO */
292#define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
Paul Bakkered27a042013-04-18 22:46:23 +0200293#else
294#define USAGE_PSK ""
Gilles Peskineeccd8882020-03-10 12:19:08 +0100295#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200296#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
297#define USAGE_CA_CALLBACK \
298 " ca_callback=%%d default: 0 (disabled)\n" \
299 " Enable this to use the trusted certificate callback function\n"
300#else
301#define USAGE_CA_CALLBACK ""
302#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200304#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100305 " tickets=%%d default: 1 (enabled)\n" \
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200306 " ticket_timeout=%%d default: 86400 (one day)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200307#else
308#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200310
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300311#if defined(MBEDTLS_SSL_EXPORT_KEYS)
312#define USAGE_EAP_TLS \
313 " eap_tls=%%d default: 0 (disabled)\n"
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100314#define USAGE_NSS_KEYLOG \
Hanno Beckerbc5308c2019-09-09 11:38:51 +0100315 " nss_keylog=%%d default: 0 (disabled)\n" \
316 " This cannot be used with eap_tls=1\n"
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100317#define USAGE_NSS_KEYLOG_FILE \
318 " nss_keylog_file=%%s\n"
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300319#else
320#define USAGE_EAP_TLS ""
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100321#define USAGE_NSS_KEYLOG ""
322#define USAGE_NSS_KEYLOG_FILE ""
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300323#endif /* MBEDTLS_SSL_EXPORT_KEYS */
324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100326#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100327 " cache_max=%%d default: cache default (50)\n" \
328 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100329#else
330#define USAGE_CACHE ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331#endif /* MBEDTLS_SSL_CACHE_C */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100332
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200333#if defined(SNI_OPTION)
Ron Eldor80d04192019-04-04 15:02:01 +0300334#if defined(MBEDTLS_X509_CRL_PARSE_C)
335#define SNI_CRL ",crl"
336#else
337#define SNI_CRL ""
338#endif
339
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100340#define USAGE_SNI \
Ron Eldor80d04192019-04-04 15:02:01 +0300341 " sni=%%s name1,cert1,key1,ca1"SNI_CRL",auth1[,...]\n" \
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200342 " default: disabled\n"
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100343#else
344#define USAGE_SNI ""
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200345#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200348#define USAGE_MAX_FRAG_LEN \
349 " max_frag_len=%%d default: 16384 (tls default)\n" \
350 " options: 512, 1024, 2048, 4096\n"
351#else
352#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100356#define USAGE_TRUNC_HMAC \
357 " trunc_hmac=%%d default: library default\n"
358#else
359#define USAGE_TRUNC_HMAC ""
360#endif
361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200363#define USAGE_ALPN \
364 " alpn=%%s default: \"\" (disabled)\n" \
365 " example: spdy/1,http/1.1\n"
366#else
367#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200371#define USAGE_COOKIES \
372 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200373 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200374#else
375#define USAGE_COOKIES ""
376#endif
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200379#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200380 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200381#else
382#define USAGE_ANTI_REPLAY ""
383#endif
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200386#define USAGE_BADMAC_LIMIT \
387 " badmac_limit=%%d default: (library default: disabled)\n"
388#else
389#define USAGE_BADMAC_LIMIT ""
390#endif
391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200393#define USAGE_DTLS \
394 " dtls=%%d default: 0 (TLS)\n" \
395 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200396 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Beckere7675d02018-08-14 13:28:56 +0100397 " mtu=%%d default: (library default: unlimited)\n" \
398 " dgram_packing=%%d default: 1 (allowed)\n" \
399 " allow or forbid packing of multiple\n" \
400 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200401#else
402#define USAGE_DTLS ""
403#endif
404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200406#define USAGE_EMS \
407 " extended_ms=0/1 default: (library default: on)\n"
408#else
409#define USAGE_EMS ""
410#endif
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100413#define USAGE_ETM \
414 " etm=0/1 default: (library default: on)\n"
415#else
416#define USAGE_ETM ""
417#endif
418
Philippe Antoine738153a2019-06-18 20:16:43 +0200419#define USAGE_REPRODUCIBLE \
420 " reproducible=0/1 default: 0 (disabled)\n"
421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100423#define USAGE_RENEGO \
424 " renegotiation=%%d default: 0 (disabled)\n" \
425 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100426 " renego_delay=%%d default: -2 (library default)\n" \
Andres AG692ad842017-01-19 16:30:57 +0000427 " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100428#else
429#define USAGE_RENEGO ""
430#endif
431
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200432#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
433#define USAGE_ECJPAKE \
434 " ecjpake_pw=%%s default: none (disabled)\n"
435#else
436#define USAGE_ECJPAKE ""
437#endif
438
Hanno Beckere6706e62017-05-15 16:05:15 +0100439#if defined(MBEDTLS_ECP_C)
440#define USAGE_CURVES \
441 " curves=a,b,c,d default: \"default\" (library default)\n" \
442 " example: \"secp521r1,brainpoolP512r1\"\n" \
443 " - use \"none\" for empty list\n" \
444 " - see mbedtls_ecp_curve_list()\n" \
445 " for acceptable curve names\n"
446#else
447#define USAGE_CURVES ""
448#endif
449
Jarno Lamsabbc7b412019-06-04 11:06:31 +0300450#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
451#define USAGE_SERIALIZATION \
Jarno Lamsa304d61c2019-06-06 10:40:52 +0300452 " serialize=%%d default: 0 (do not serialize/deserialize)\n" \
Jarno Lamsa1a7f7932019-06-07 08:39:24 +0300453 " options: 1 (serialize)\n" \
454 " 2 (serialize with re-initialization)\n"
Jarno Lamsabbc7b412019-06-04 11:06:31 +0300455#else
456#define USAGE_SERIALIZATION ""
457#endif
458
Gilles Peskineb2971ff2020-04-14 19:41:01 +0200459/* USAGE is arbitrarily split to stay under the portable string literal
460 * length limit: 4095 bytes in C99. */
461#define USAGE1 \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000462 "\n usage: ssl_server2 param=<>...\n" \
463 "\n acceptable parameters:\n" \
Ron Eldor71f68c42017-09-26 11:29:11 +0300464 " server_addr=%%s default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000465 " server_port=%%d default: 4433\n" \
466 " debug_level=%%d default: 0 (disabled)\n" \
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200467 " buffer_size=%%d default: 200 \n" \
468 " (minimum: 1, max: 16385)\n" \
469 " response_size=%%d default: about 152 (basic response)\n" \
470 " (minimum: 0, max: 16384)\n" \
471 " increases buffer_size if bigger\n"\
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100472 " nbio=%%d default: 0 (blocking I/O)\n" \
473 " options: 1 (non-blocking), 2 (added delays)\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100474 " event=%%d default: 0 (loop)\n" \
475 " options: 1 (level-triggered, implies nbio=1),\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200476 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100477 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200478 USAGE_DTLS \
479 USAGE_COOKIES \
480 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200481 USAGE_BADMAC_LIMIT \
Gilles Peskineb2971ff2020-04-14 19:41:01 +0200482 "\n"
483#define USAGE2 \
Manuel Pégourié-Gonnardee6139c2015-05-07 10:18:26 +0100484 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100485 " options: none, optional, required\n" \
Janos Follath4817e272017-04-10 13:44:33 +0100486 " cert_req_ca_list=%%d default: 1 (send ca list)\n" \
487 " options: 1 (send ca list), 0 (don't send)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000488 USAGE_IO \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100489 USAGE_SSL_ASYNC \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100490 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100491 "\n" \
492 USAGE_PSK \
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200493 USAGE_CA_CALLBACK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200494 USAGE_ECJPAKE \
Gilles Peskineb2971ff2020-04-14 19:41:01 +0200495 "\n"
496#define USAGE3 \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100497 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100498 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200499 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200500 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100501 USAGE_TICKETS \
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300502 USAGE_EAP_TLS \
Philippe Antoine738153a2019-06-18 20:16:43 +0200503 USAGE_REPRODUCIBLE \
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100504 USAGE_NSS_KEYLOG \
505 USAGE_NSS_KEYLOG_FILE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100506 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100507 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100508 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200509 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200510 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100511 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100512 USAGE_CURVES \
Gilles Peskineb2971ff2020-04-14 19:41:01 +0200513 "\n"
514#define USAGE4 \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100515 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200516 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200517 " min_version=%%s default: (library default: tls1)\n" \
518 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200519 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100520 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200521 "\n" \
522 " version_suites=a,b,c,d per-version ciphersuites\n" \
523 " in order from ssl3 to tls1_2\n" \
524 " default: all enabled\n" \
525 " force_ciphersuite=<name> default: all enabled\n" \
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100526 " query_config=<name> return 0 if the specified\n" \
527 " configuration macro is defined and 1\n" \
528 " otherwise. The expansion of the macro\n" \
529 " is printed if it is defined\n" \
Jarno Lamsabbc7b412019-06-04 11:06:31 +0300530 USAGE_SERIALIZATION \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000531 " acceptable ciphersuite names:\n"
532
Hanno Becker8651a432017-06-09 16:13:22 +0100533#define ALPN_LIST_SIZE 10
534#define CURVE_LIST_SIZE 20
535
Andres AG692ad842017-01-19 16:30:57 +0000536#define PUT_UINT64_BE(out_be,in_le,i) \
537{ \
538 (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \
539 (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \
540 (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \
541 (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \
542 (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \
543 (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \
544 (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \
545 (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \
546}
547
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500548
Rich Evans85b05ec2015-02-12 11:37:29 +0000549/*
550 * global options
551 */
552struct options
553{
554 const char *server_addr; /* address on which the ssl service runs */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200555 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000556 int debug_level; /* level of debugging */
557 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100558 int event; /* loop or event-driven IO? level or edge triggered? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200560 int response_size; /* pad response with header to requested size */
561 uint16_t buffer_size; /* IO buffer size */
Rich Evans85b05ec2015-02-12 11:37:29 +0000562 const char *ca_file; /* the file with the CA certificate(s) */
563 const char *ca_path; /* the path with the CA certificate(s) reside */
564 const char *crt_file; /* the file with the server certificate */
565 const char *key_file; /* the file with the server key */
566 const char *crt_file2; /* the file with the 2nd server certificate */
567 const char *key_file2; /* the file with the 2nd server key */
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100568 const char *async_operations; /* supported SSL asynchronous operations */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100569 int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
570 int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
571 int async_private_error; /* inject error in async private callback */
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100572#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000573 int psk_opaque;
574 int psk_list_opaque;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100575#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200576#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000577 int ca_callback; /* Use callback for trusted certificate list */
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200578#endif
Rich Evans85b05ec2015-02-12 11:37:29 +0000579 const char *psk; /* the pre-shared key */
580 const char *psk_identity; /* the pre-shared key identity */
581 char *psk_list; /* list of PSK id/key pairs for callback */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200582 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000583 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
584 const char *version_suites; /* per-version ciphersuites */
585 int renegotiation; /* enable / disable renegotiation */
586 int allow_legacy; /* allow legacy renegotiation */
587 int renegotiate; /* attempt renegotiation? */
588 int renego_delay; /* delay before enforcing renegotiation */
Andres AG692ad842017-01-19 16:30:57 +0000589 uint64_t renego_period; /* period for automatic renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000590 int exchanges; /* number of data exchanges */
591 int min_version; /* minimum protocol version accepted */
592 int max_version; /* maximum protocol version accepted */
593 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200594 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000595 int auth_mode; /* verify mode for connection */
Janos Follath4817e272017-04-10 13:44:33 +0100596 int cert_req_ca_list; /* should we send the CA list? */
Rich Evans85b05ec2015-02-12 11:37:29 +0000597 unsigned char mfl_code; /* code for maximum fragment length */
598 int trunc_hmac; /* accept truncated hmac? */
599 int tickets; /* enable / disable session tickets */
600 int ticket_timeout; /* session ticket lifetime */
601 int cache_max; /* max number of session cache entries */
602 int cache_timeout; /* expiration delay of session cache entries */
603 char *sni; /* string describing sni information */
Hanno Beckere6706e62017-05-15 16:05:15 +0100604 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000605 const char *alpn_string; /* ALPN supported protocols */
606 const char *dhm_file; /* the file with the DH parameters */
607 int extended_ms; /* allow negotiation of extended MS? */
608 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000609 int transport; /* TLS or DTLS? */
610 int cookies; /* Use cookies for DTLS? -1 to break them */
611 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
612 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
613 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200614 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Hanno Beckere7675d02018-08-14 13:28:56 +0100615 int dgram_packing; /* allow/forbid datagram packing */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000616 int badmac_limit; /* Limit of records with bad MAC */
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300617 int eap_tls; /* derive EAP-TLS keying material? */
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100618 int nss_keylog; /* export NSS key log material */
619 const char *nss_keylog_file; /* NSS key log file */
Hanno Beckera7d25422019-04-09 17:28:10 +0100620 int cid_enabled; /* whether to use the CID extension or not */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100621 int cid_enabled_renego; /* whether to use the CID extension or not
622 * during renegotiation */
Hanno Beckera7d25422019-04-09 17:28:10 +0100623 const char *cid_val; /* the CID to use for incoming messages */
Jarno Lamsa9831c8a2019-05-29 13:33:32 +0300624 int serialize; /* serialize/deserialize connection */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100625 const char *cid_val_renego; /* the CID to use for incoming messages
626 * after renegotiation */
Philippe Antoine154feb22019-06-11 17:50:23 +0200627 int reproducible; /* make communication reproducible */
Rich Evans85b05ec2015-02-12 11:37:29 +0000628} opt;
629
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100630int query_config( const char *config );
631
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300632#if defined(MBEDTLS_SSL_EXPORT_KEYS)
633typedef struct eap_tls_keys
634{
635 unsigned char master_secret[48];
636 unsigned char randbytes[64];
Ron Eldor51d3ab52019-05-12 14:54:30 +0300637 mbedtls_tls_prf_types tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300638} eap_tls_keys;
639
640static int eap_tls_key_derivation ( void *p_expkey,
641 const unsigned char *ms,
642 const unsigned char *kb,
643 size_t maclen,
644 size_t keylen,
645 size_t ivlen,
Jaeden Amero63d813d2019-09-12 10:09:57 +0100646 const unsigned char client_random[32],
647 const unsigned char server_random[32],
Ron Eldor51d3ab52019-05-12 14:54:30 +0300648 mbedtls_tls_prf_types tls_prf_type )
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300649{
650 eap_tls_keys *keys = (eap_tls_keys *)p_expkey;
651
652 ( ( void ) kb );
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300653 memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) );
654 memcpy( keys->randbytes, client_random, 32 );
655 memcpy( keys->randbytes + 32, server_random, 32 );
Ron Eldor51d3ab52019-05-12 14:54:30 +0300656 keys->tls_prf_type = tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300657
Ron Eldorf75e2522019-05-14 20:38:49 +0300658 if( opt.debug_level > 2 )
659 {
Ron Eldor801faf02019-05-15 17:45:24 +0300660 mbedtls_printf("exported maclen is %u\n", (unsigned)maclen);
661 mbedtls_printf("exported keylen is %u\n", (unsigned)keylen);
662 mbedtls_printf("exported ivlen is %u\n", (unsigned)ivlen);
Ron Eldorf75e2522019-05-14 20:38:49 +0300663 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300664 return( 0 );
665}
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100666
667static int nss_keylog_export( void *p_expkey,
668 const unsigned char *ms,
669 const unsigned char *kb,
670 size_t maclen,
671 size_t keylen,
672 size_t ivlen,
Jaeden Amero63d813d2019-09-12 10:09:57 +0100673 const unsigned char client_random[32],
674 const unsigned char server_random[32],
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100675 mbedtls_tls_prf_types tls_prf_type )
676{
677 char nss_keylog_line[ 200 ];
678 size_t const client_random_len = 32;
679 size_t const master_secret_len = 48;
680 size_t len = 0;
681 size_t j;
682 int ret = 0;
683
684 ((void) p_expkey);
685 ((void) kb);
686 ((void) maclen);
687 ((void) keylen);
688 ((void) ivlen);
689 ((void) server_random);
690 ((void) tls_prf_type);
691
692 len += sprintf( nss_keylog_line + len,
693 "%s", "CLIENT_RANDOM " );
694
695 for( j = 0; j < client_random_len; j++ )
696 {
697 len += sprintf( nss_keylog_line + len,
698 "%02x", client_random[j] );
699 }
700
701 len += sprintf( nss_keylog_line + len, " " );
702
703 for( j = 0; j < master_secret_len; j++ )
704 {
705 len += sprintf( nss_keylog_line + len,
706 "%02x", ms[j] );
707 }
708
709 len += sprintf( nss_keylog_line + len, "\n" );
710 nss_keylog_line[ len ] = '\0';
711
712 mbedtls_printf( "\n" );
713 mbedtls_printf( "---------------- NSS KEYLOG -----------------\n" );
714 mbedtls_printf( "%s", nss_keylog_line );
715 mbedtls_printf( "---------------------------------------------\n" );
716
717 if( opt.nss_keylog_file != NULL )
718 {
719 FILE *f;
720
721 if( ( f = fopen( opt.nss_keylog_file, "a" ) ) == NULL )
722 {
723 ret = -1;
724 goto exit;
725 }
726
727 if( fwrite( nss_keylog_line, 1, len, f ) != len )
728 {
729 ret = -1;
k-stachowiakbbc1c692019-09-26 13:36:54 +0200730 fclose( f );
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100731 goto exit;
732 }
733
734 fclose( f );
735 }
736
737exit:
738 mbedtls_platform_zeroize( nss_keylog_line,
739 sizeof( nss_keylog_line ) );
740 return( ret );
741}
742
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300743#endif
744
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200745static void my_debug( void *ctx, int level,
746 const char *file, int line,
747 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000748{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200749 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000750
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200751 /* Extract basename from file */
752 for( p = basename = file; *p != '\0'; p++ )
753 if( *p == '/' || *p == '\\' )
754 basename = p + 1;
755
756 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000757 fflush( (FILE *) ctx );
758}
759
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200760mbedtls_time_t dummy_constant_time( mbedtls_time_t* time )
761{
762 (void) time;
763 return 0x5af2a056;
764}
765
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200766int dummy_entropy( void *data, unsigned char *output, size_t len )
767{
768 size_t i;
Philippe Antoine12e85de2019-06-11 16:07:53 +0200769 int ret;
Philippe Antoined2235f22019-06-11 16:29:28 +0200770 (void) data;
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200771
Philippe Antoine3ca50852019-06-07 22:31:59 +0200772 ret = mbedtls_entropy_func( data, output, len );
Philippe Antoine986b6f22019-06-07 15:04:32 +0200773 for (i = 0; i < len; i++ ) {
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200774 //replace result with pseudo random
775 output[i] = (unsigned char) rand();
776 }
Philippe Antoine3ca50852019-06-07 22:31:59 +0200777 return( ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200778}
779
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200780#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000781int ca_callback( void *data, mbedtls_x509_crt const *child,
782 mbedtls_x509_crt **candidates)
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200783{
Hanno Beckercbb59032019-03-28 14:14:22 +0000784 int ret = 0;
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200785 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +0000786 mbedtls_x509_crt *first;
787
788 /* This is a test-only implementation of the CA callback
789 * which always returns the entire list of trusted certificates.
790 * Production implementations managing a large number of CAs
791 * should use an efficient presentation and lookup for the
792 * set of trusted certificates (such as a hashtable) and only
793 * return those trusted certificates which satisfy basic
794 * parental checks, such as the matching of child `Issuer`
795 * and parent `Subject` field. */
796 ((void) child);
797
798 first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
799 if( first == NULL )
800 {
801 ret = -1;
802 goto exit;
803 }
804 mbedtls_x509_crt_init( first );
805
806 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
807 {
808 ret = -1;
809 goto exit;
810 }
811
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200812 while( ca->next != NULL )
813 {
814 ca = ca->next;
Hanno Beckercbb59032019-03-28 14:14:22 +0000815 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
816 {
817 ret = -1;
818 goto exit;
819 }
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200820 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000821
822exit:
823
824 if( ret != 0 )
825 {
826 mbedtls_x509_crt_free( first );
827 mbedtls_free( first );
828 first = NULL;
829 }
830
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200831 *candidates = first;
Hanno Beckercbb59032019-03-28 14:14:22 +0000832 return( ret );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200833}
834#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
835
Rich Evans85b05ec2015-02-12 11:37:29 +0000836/*
837 * Test recv/send functions that make sure each try returns
838 * WANT_READ/WANT_WRITE at least once before sucesseding
839 */
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100840static int delayed_recv( void *ctx, unsigned char *buf, size_t len )
Rich Evans85b05ec2015-02-12 11:37:29 +0000841{
842 static int first_try = 1;
843 int ret;
844
845 if( first_try )
846 {
847 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100848 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000849 }
850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100852 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000853 first_try = 1; /* Next call will be a new operation */
854 return( ret );
855}
856
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100857static int delayed_send( void *ctx, const unsigned char *buf, size_t len )
Rich Evans85b05ec2015-02-12 11:37:29 +0000858{
859 static int first_try = 1;
860 int ret;
861
862 if( first_try )
863 {
864 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100865 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000866 }
867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100869 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000870 first_try = 1; /* Next call will be a new operation */
871 return( ret );
872}
873
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100874typedef struct
875{
876 mbedtls_ssl_context *ssl;
877 mbedtls_net_context *net;
878} io_ctx_t;
879
Hanno Becker4b6649e2019-07-03 17:14:41 +0100880#if defined(MBEDTLS_SSL_RECORD_CHECKING)
881static int ssl_check_record( mbedtls_ssl_context const *ssl,
882 unsigned char const *buf, size_t len )
883{
884 int ret;
885 unsigned char *tmp_buf;
886
887 /* Record checking may modify the input buffer,
888 * so make a copy. */
889 tmp_buf = mbedtls_calloc( 1, len );
890 if( tmp_buf == NULL )
891 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
892 memcpy( tmp_buf, buf, len );
893
894 ret = mbedtls_ssl_check_record( ssl, tmp_buf, len );
895 if( ret != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE )
896 {
897 int ret_repeated;
898
899 /* Test-only: Make sure that mbedtls_ssl_check_record()
900 * doesn't alter state. */
901 memcpy( tmp_buf, buf, len ); /* Restore buffer */
902 ret_repeated = mbedtls_ssl_check_record( ssl, tmp_buf, len );
903 if( ret != ret_repeated )
904 {
Hanno Becker91f83272019-07-24 13:59:07 +0100905 mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" );
906 return( -1 );
Hanno Becker4b6649e2019-07-03 17:14:41 +0100907 }
908
909 switch( ret )
910 {
911 case 0:
912 break;
913
914 case MBEDTLS_ERR_SSL_INVALID_RECORD:
915 if( opt.debug_level > 1 )
916 mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" );
917 break;
918
919 case MBEDTLS_ERR_SSL_INVALID_MAC:
920 if( opt.debug_level > 1 )
921 mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" );
922 break;
923
924 case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD:
925 if( opt.debug_level > 1 )
926 mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" );
927 break;
928
929 default:
930 mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", -ret );
931 return( -1 );
932 }
933
934 /* Regardless of the outcome, forward the record to the stack. */
935 }
936
Hanno Becker4b6649e2019-07-03 17:14:41 +0100937 mbedtls_free( tmp_buf );
938
939 return( 0 );
940}
941#endif /* MBEDTLS_SSL_RECORD_CHECKING */
942
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100943static int recv_cb( void *ctx, unsigned char *buf, size_t len )
944{
945 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
946 size_t recv_len;
947 int ret;
948
949 if( opt.nbio == 2 )
950 ret = delayed_recv( io_ctx->net, buf, len );
951 else
952 ret = mbedtls_net_recv( io_ctx->net, buf, len );
953 if( ret < 0 )
954 return( ret );
955 recv_len = (size_t) ret;
956
957 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
958 {
959 /* Here's the place to do any datagram/record checking
960 * in between receiving the packet from the underlying
961 * transport and passing it on to the TLS stack. */
Hanno Becker4b6649e2019-07-03 17:14:41 +0100962#if defined(MBEDTLS_SSL_RECORD_CHECKING)
963 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
964 return( -1 );
965#endif /* MBEDTLS_SSL_RECORD_CHECKING */
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100966 }
967
968 return( (int) recv_len );
969}
970
971static int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len,
972 uint32_t timeout )
973{
974 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
975 int ret;
976 size_t recv_len;
977
978 ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout );
979 if( ret < 0 )
980 return( ret );
981 recv_len = (size_t) ret;
982
983 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
984 {
985 /* Here's the place to do any datagram/record checking
986 * in between receiving the packet from the underlying
987 * transport and passing it on to the TLS stack. */
Hanno Becker4b6649e2019-07-03 17:14:41 +0100988#if defined(MBEDTLS_SSL_RECORD_CHECKING)
989 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
990 return( -1 );
991#endif /* MBEDTLS_SSL_RECORD_CHECKING */
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100992 }
993
994 return( (int) recv_len );
995}
996
997static int send_cb( void *ctx, unsigned char const *buf, size_t len )
998{
999 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
1000
1001 if( opt.nbio == 2 )
1002 return( delayed_send( io_ctx->net, buf, len ) );
1003
1004 return( mbedtls_net_send( io_ctx->net, buf, len ) );
1005}
1006
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +02001007/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001008 * Return authmode from string, or -1 on error
1009 */
1010static int get_auth_mode( const char *s )
1011{
1012 if( strcmp( s, "none" ) == 0 )
1013 return( MBEDTLS_SSL_VERIFY_NONE );
1014 if( strcmp( s, "optional" ) == 0 )
1015 return( MBEDTLS_SSL_VERIFY_OPTIONAL );
1016 if( strcmp( s, "required" ) == 0 )
1017 return( MBEDTLS_SSL_VERIFY_REQUIRED );
1018
1019 return( -1 );
1020}
1021
1022/*
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +02001023 * Used by sni_parse and psk_parse to handle coma-separated lists
1024 */
1025#define GET_ITEM( dst ) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001026 do \
1027 { \
1028 (dst) = p; \
1029 while( *p != ',' ) \
1030 if( ++p > end ) \
1031 goto error; \
1032 *p++ = '\0'; \
1033 } while( 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +02001034
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001035#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001036typedef struct _sni_entry sni_entry;
1037
1038struct _sni_entry {
1039 const char *name;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 mbedtls_x509_crt *cert;
1041 mbedtls_pk_context *key;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001042 mbedtls_x509_crt* ca;
1043 mbedtls_x509_crl* crl;
1044 int authmode;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001045 sni_entry *next;
1046};
1047
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001048void sni_free( sni_entry *head )
1049{
1050 sni_entry *cur = head, *next;
1051
1052 while( cur != NULL )
1053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 mbedtls_x509_crt_free( cur->cert );
1055 mbedtls_free( cur->cert );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_pk_free( cur->key );
1058 mbedtls_free( cur->key );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001059
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001060 mbedtls_x509_crt_free( cur->ca );
1061 mbedtls_free( cur->ca );
Ron Eldor80d04192019-04-04 15:02:01 +03001062#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001063 mbedtls_x509_crl_free( cur->crl );
1064 mbedtls_free( cur->crl );
Ron Eldor80d04192019-04-04 15:02:01 +03001065#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001066 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 mbedtls_free( cur );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001068 cur = next;
1069 }
1070}
1071
1072/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001073 * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
1074 * into a usable sni_entry list. For ca1, crl1, auth1, the special value
1075 * '-' means unset. If ca1 is unset, then crl1 is ignored too.
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001076 *
1077 * Modifies the input string! This is not production quality!
1078 */
1079sni_entry *sni_parse( char *sni_string )
1080{
1081 sni_entry *cur = NULL, *new = NULL;
1082 char *p = sni_string;
1083 char *end = p;
Ron Eldor80d04192019-04-04 15:02:01 +03001084 char *crt_file, *key_file, *ca_file, *auth_str;
1085#if defined(MBEDTLS_X509_CRL_PARSE_C)
1086 char *crl_file;
1087#endif
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001088
1089 while( *end != '\0' )
1090 ++end;
1091 *end = ',';
1092
1093 while( p <= end )
1094 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001095 if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001096 {
1097 sni_free( cur );
1098 return( NULL );
1099 }
1100
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001101 GET_ITEM( new->name );
1102 GET_ITEM( crt_file );
1103 GET_ITEM( key_file );
1104 GET_ITEM( ca_file );
Ron Eldor80d04192019-04-04 15:02:01 +03001105#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001106 GET_ITEM( crl_file );
Ron Eldor80d04192019-04-04 15:02:01 +03001107#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001108 GET_ITEM( auth_str );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001109
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001110 if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
1111 ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001112 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 mbedtls_x509_crt_init( new->cert );
1115 mbedtls_pk_init( new->key );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
1118 mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001119 goto error;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001120
1121 if( strcmp( ca_file, "-" ) != 0 )
1122 {
1123 if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
1124 goto error;
1125
1126 mbedtls_x509_crt_init( new->ca );
1127
1128 if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
1129 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001130 }
1131
Ron Eldor80d04192019-04-04 15:02:01 +03001132#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001133 if( strcmp( crl_file, "-" ) != 0 )
1134 {
1135 if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
1136 goto error;
1137
1138 mbedtls_x509_crl_init( new->crl );
1139
1140 if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
1141 goto error;
1142 }
Ron Eldor80d04192019-04-04 15:02:01 +03001143#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001144
1145 if( strcmp( auth_str, "-" ) != 0 )
1146 {
1147 if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
1148 goto error;
1149 }
1150 else
1151 new->authmode = DFL_AUTH_MODE;
1152
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001153 new->next = cur;
1154 cur = new;
1155 }
1156
1157 return( cur );
1158
1159error:
1160 sni_free( new );
1161 sni_free( cur );
1162 return( NULL );
1163}
1164
1165/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001166 * SNI callback.
1167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001169 const unsigned char *name, size_t name_len )
1170{
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001171 const sni_entry *cur = (const sni_entry *) p_info;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001172
1173 while( cur != NULL )
1174 {
1175 if( name_len == strlen( cur->name ) &&
1176 memcmp( name, cur->name, name_len ) == 0 )
1177 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001178 if( cur->ca != NULL )
1179 mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
1180
1181 if( cur->authmode != DFL_AUTH_MODE )
1182 mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
1183
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001184 return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001185 }
1186
1187 cur = cur->next;
1188 }
1189
1190 return( -1 );
1191}
1192
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001193#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001194
Gilles Peskineeccd8882020-03-10 12:19:08 +01001195#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) || \
Hanno Beckera0e20d02019-05-15 14:03:01 +01001196 defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001197
Hanno Becker1eeca412018-10-15 12:01:35 +01001198#define HEX2NUM( c ) \
1199 do \
1200 { \
1201 if( (c) >= '0' && (c) <= '9' ) \
1202 (c) -= '0'; \
1203 else if( (c) >= 'a' && (c) <= 'f' ) \
1204 (c) -= 'a' - 10; \
1205 else if( (c) >= 'A' && (c) <= 'F' ) \
1206 (c) -= 'A' - 10; \
1207 else \
1208 return( -1 ); \
1209 } while( 0 )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001210
1211/*
1212 * Convert a hex string to bytes.
1213 * Return 0 on success, -1 on error.
1214 */
1215int unhexify( unsigned char *output, const char *input, size_t *olen )
1216{
1217 unsigned char c;
1218 size_t j;
1219
1220 *olen = strlen( input );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 if( *olen % 2 != 0 || *olen / 2 > MBEDTLS_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001222 return( -1 );
1223 *olen /= 2;
1224
1225 for( j = 0; j < *olen * 2; j += 2 )
1226 {
1227 c = input[j];
1228 HEX2NUM( c );
1229 output[ j / 2 ] = c << 4;
1230
1231 c = input[j + 1];
1232 HEX2NUM( c );
1233 output[ j / 2 ] |= c;
1234 }
1235
1236 return( 0 );
1237}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001238
Hanno Becker554b6ea2019-04-30 14:18:06 +01001239#endif
1240
Gilles Peskineeccd8882020-03-10 12:19:08 +01001241#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker554b6ea2019-04-30 14:18:06 +01001242
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001243typedef struct _psk_entry psk_entry;
1244
1245struct _psk_entry
1246{
1247 const char *name;
1248 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 unsigned char key[MBEDTLS_PSK_MAX_LEN];
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001250#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001251 psa_key_handle_t slot;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001252#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001253 psk_entry *next;
1254};
1255
1256/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001257 * Free a list of psk_entry's
1258 */
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001259int psk_free( psk_entry *head )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001260{
1261 psk_entry *next;
1262
1263 while( head != NULL )
1264 {
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001265#if defined(MBEDTLS_USE_PSA_CRYPTO)
1266 psa_status_t status;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001267 psa_key_handle_t const slot = head->slot;
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001268
1269 if( slot != 0 )
1270 {
1271 status = psa_destroy_key( slot );
1272 if( status != PSA_SUCCESS )
1273 return( status );
1274 }
1275#endif /* MBEDTLS_USE_PSA_CRYPTO */
1276
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001277 next = head->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 mbedtls_free( head );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001279 head = next;
1280 }
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001281
1282 return( 0 );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001283}
1284
1285/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001286 * Parse a string of pairs name1,key1[,name2,key2[,...]]
1287 * into a usable psk_entry list.
1288 *
1289 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001290 */
1291psk_entry *psk_parse( char *psk_string )
1292{
1293 psk_entry *cur = NULL, *new = NULL;
1294 char *p = psk_string;
1295 char *end = p;
1296 char *key_hex;
1297
1298 while( *end != '\0' )
1299 ++end;
1300 *end = ',';
1301
1302 while( p <= end )
1303 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001304 if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +00001305 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001306
1307 memset( new, 0, sizeof( psk_entry ) );
1308
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +02001309 GET_ITEM( new->name );
1310 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001311
1312 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001313 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001314
1315 new->next = cur;
1316 cur = new;
1317 }
1318
1319 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001320
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001321error:
1322 psk_free( new );
1323 psk_free( cur );
1324 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001325}
1326
1327/*
1328 * PSK callback
1329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001331 const unsigned char *name, size_t name_len )
1332{
1333 psk_entry *cur = (psk_entry *) p_info;
1334
1335 while( cur != NULL )
1336 {
1337 if( name_len == strlen( cur->name ) &&
1338 memcmp( name, cur->name, name_len ) == 0 )
1339 {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001340#if defined(MBEDTLS_USE_PSA_CRYPTO)
1341 if( cur->slot != 0 )
1342 return( mbedtls_ssl_set_hs_psk_opaque( ssl, cur->slot ) );
1343 else
1344#endif
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001345 return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001346 }
1347
1348 cur = cur->next;
1349 }
1350
1351 return( -1 );
1352}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001353#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001354
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001355static mbedtls_net_context listen_fd, client_fd;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +02001356
1357/* Interruption handler to ensure clean exit (for valgrind testing) */
1358#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001359static int received_sigterm = 0;
1360void term_handler( int sig )
1361{
1362 ((void) sig);
1363 received_sigterm = 1;
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001364 mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
1365 mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001366}
Paul Bakkerc1283d32014-08-18 11:05:51 +02001367#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001368
Gilles Peskine682df092017-05-11 19:01:11 +02001369#if defined(MBEDTLS_X509_CRT_PARSE_C)
1370static int ssl_sig_hashes_for_test[] = {
1371#if defined(MBEDTLS_SHA512_C)
1372 MBEDTLS_MD_SHA512,
1373 MBEDTLS_MD_SHA384,
1374#endif
1375#if defined(MBEDTLS_SHA256_C)
1376 MBEDTLS_MD_SHA256,
1377 MBEDTLS_MD_SHA224,
1378#endif
1379#if defined(MBEDTLS_SHA1_C)
1380 /* Allow SHA-1 as we use it extensively in tests. */
1381 MBEDTLS_MD_SHA1,
1382#endif
1383 MBEDTLS_MD_NONE
1384};
1385#endif /* MBEDTLS_X509_CRT_PARSE_C */
1386
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02001387/** Return true if \p ret is a status code indicating that there is an
1388 * operation in progress on an SSL connection, and false if it indicates
1389 * success or a fatal error.
1390 *
1391 * The possible operations in progress are:
1392 *
1393 * - A read, when the SSL input buffer does not contain a full message.
1394 * - A write, when the SSL output buffer contains some data that has not
1395 * been sent over the network yet.
1396 * - An asynchronous callback that has not completed yet. */
1397static int mbedtls_status_is_ssl_in_progress( int ret )
1398{
1399 return( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1400 ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
1401 ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1402}
1403
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001404#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001405typedef struct
1406{
Gilles Peskine44817442018-06-13 18:09:28 +02001407 mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
1408 mbedtls_pk_context *pk; /*!< Private key */
1409 unsigned delay; /*!< Number of resume steps to go through */
1410 unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001411} ssl_async_key_slot_t;
1412
1413typedef enum {
Gilles Peskinead28bf02018-04-26 00:19:16 +02001414 SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
1415 SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
1416 SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start */
1417 SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
Gilles Peskinec9125722018-04-26 07:15:40 +02001418#define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001419} ssl_async_inject_error_t;
1420
1421typedef struct
1422{
Gilles Peskinee2479892018-06-13 18:06:51 +02001423 ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001424 size_t slots_used;
1425 ssl_async_inject_error_t inject_error;
1426 int (*f_rng)(void *, unsigned char *, size_t);
1427 void *p_rng;
1428} ssl_async_key_context_t;
1429
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001430int ssl_async_set_key( ssl_async_key_context_t *ctx,
Gilles Peskine44817442018-06-13 18:09:28 +02001431 mbedtls_x509_crt *cert,
1432 mbedtls_pk_context *pk,
1433 int pk_take_ownership,
1434 unsigned delay )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001435{
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001436 if( ctx->slots_used >= sizeof( ctx->slots ) / sizeof( *ctx->slots ) )
1437 return( -1 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001438 ctx->slots[ctx->slots_used].cert = cert;
1439 ctx->slots[ctx->slots_used].pk = pk;
1440 ctx->slots[ctx->slots_used].delay = delay;
Gilles Peskine44817442018-06-13 18:09:28 +02001441 ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001442 ++ctx->slots_used;
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001443 return( 0 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001444}
1445
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001446#define SSL_ASYNC_INPUT_MAX_SIZE 512
Gilles Peskined3268832018-04-26 06:23:59 +02001447
1448typedef enum
1449{
1450 ASYNC_OP_SIGN,
1451 ASYNC_OP_DECRYPT,
1452} ssl_async_operation_type_t;
1453/* Note that the enum above and the array below need to be kept in sync!
1454 * `ssl_async_operation_names[op]` is the name of op for each value `op`
1455 * of type `ssl_async_operation_type_t`. */
1456static const char *const ssl_async_operation_names[] =
1457{
1458 "sign",
1459 "decrypt",
1460};
1461
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001462typedef struct
1463{
Gilles Peskine6331d782018-04-26 13:27:43 +02001464 unsigned slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001465 ssl_async_operation_type_t operation_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001466 mbedtls_md_type_t md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001467 unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
1468 size_t input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001469 unsigned remaining_delay;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001470} ssl_async_operation_context_t;
1471
Gilles Peskine8f97af72018-04-26 11:46:10 +02001472static int ssl_async_start( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001473 mbedtls_x509_crt *cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001474 ssl_async_operation_type_t op_type,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001475 mbedtls_md_type_t md_alg,
1476 const unsigned char *input,
1477 size_t input_len )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001478{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001479 ssl_async_key_context_t *config_data =
1480 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskine6331d782018-04-26 13:27:43 +02001481 unsigned slot;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001482 ssl_async_operation_context_t *ctx = NULL;
Gilles Peskined3268832018-04-26 06:23:59 +02001483 const char *op_name = ssl_async_operation_names[op_type];
Gilles Peskinef1127252018-04-24 13:05:39 +02001484
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001485 {
1486 char dn[100];
Gilles Peskined5d983e2018-06-15 14:05:10 +02001487 if( mbedtls_x509_dn_gets( dn, sizeof( dn ), &cert->subject ) > 0 )
1488 mbedtls_printf( "Async %s callback: looking for DN=%s\n",
1489 op_name, dn );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001490 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001491
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001492 /* Look for a private key that matches the public key in cert.
1493 * Since this test code has the private key inside Mbed TLS,
1494 * we call mbedtls_pk_check_pair to match a private key with the
1495 * public key. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001496 for( slot = 0; slot < config_data->slots_used; slot++ )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001497 {
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001498 if( mbedtls_pk_check_pair( &cert->pk,
1499 config_data->slots[slot].pk ) == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001500 break;
1501 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001502 if( slot == config_data->slots_used )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001503 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001504 mbedtls_printf( "Async %s callback: no key matches this certificate.\n",
1505 op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001506 return( MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH );
1507 }
Gilles Peskine6331d782018-04-26 13:27:43 +02001508 mbedtls_printf( "Async %s callback: using key slot %u, delay=%u.\n",
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001509 op_name, slot, config_data->slots[slot].delay );
Gilles Peskinef1127252018-04-24 13:05:39 +02001510
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001511 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001512 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001513 mbedtls_printf( "Async %s callback: injected error\n", op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001514 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1515 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001516
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001517 if( input_len > SSL_ASYNC_INPUT_MAX_SIZE )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Gilles Peskinef1127252018-04-24 13:05:39 +02001519
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001520 ctx = mbedtls_calloc( 1, sizeof( *ctx ) );
1521 if( ctx == NULL )
1522 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1523 ctx->slot = slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001524 ctx->operation_type = op_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001525 ctx->md_alg = md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001526 memcpy( ctx->input, input, input_len );
1527 ctx->input_len = input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001528 ctx->remaining_delay = config_data->slots[slot].delay;
Gilles Peskinea668c602018-04-30 11:54:39 +02001529 mbedtls_ssl_set_async_operation_data( ssl, ctx );
Gilles Peskinef1127252018-04-24 13:05:39 +02001530
Gilles Peskineceb541b2018-04-26 06:30:45 +02001531 if( ctx->remaining_delay == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001532 return( 0 );
1533 else
1534 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1535}
1536
Gilles Peskine8f97af72018-04-26 11:46:10 +02001537static int ssl_async_sign( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001538 mbedtls_x509_crt *cert,
1539 mbedtls_md_type_t md_alg,
1540 const unsigned char *hash,
1541 size_t hash_len )
1542{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001543 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001544 ASYNC_OP_SIGN, md_alg,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001545 hash, hash_len ) );
1546}
1547
Gilles Peskine8f97af72018-04-26 11:46:10 +02001548static int ssl_async_decrypt( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001549 mbedtls_x509_crt *cert,
1550 const unsigned char *input,
1551 size_t input_len )
1552{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001553 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001554 ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001555 input, input_len ) );
1556}
1557
Gilles Peskine8f97af72018-04-26 11:46:10 +02001558static int ssl_async_resume( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001559 unsigned char *output,
1560 size_t *output_len,
1561 size_t output_size )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001562{
Gilles Peskinea668c602018-04-30 11:54:39 +02001563 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine8f97af72018-04-26 11:46:10 +02001564 ssl_async_key_context_t *config_data =
1565 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001566 ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001567 int ret;
Gilles Peskinef5a99962018-04-30 16:37:23 +02001568 const char *op_name;
Gilles Peskinef1127252018-04-24 13:05:39 +02001569
Gilles Peskineceb541b2018-04-26 06:30:45 +02001570 if( ctx->remaining_delay > 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001571 {
Gilles Peskineceb541b2018-04-26 06:30:45 +02001572 --ctx->remaining_delay;
Gilles Peskine6331d782018-04-26 13:27:43 +02001573 mbedtls_printf( "Async resume (slot %u): call %u more times.\n",
Gilles Peskineceb541b2018-04-26 06:30:45 +02001574 ctx->slot, ctx->remaining_delay );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001575 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1576 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001577
Gilles Peskined3268832018-04-26 06:23:59 +02001578 switch( ctx->operation_type )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001579 {
Gilles Peskined3268832018-04-26 06:23:59 +02001580 case ASYNC_OP_DECRYPT:
Gilles Peskined3268832018-04-26 06:23:59 +02001581 ret = mbedtls_pk_decrypt( key_slot->pk,
1582 ctx->input, ctx->input_len,
1583 output, output_len, output_size,
1584 config_data->f_rng, config_data->p_rng );
1585 break;
1586 case ASYNC_OP_SIGN:
Gilles Peskined3268832018-04-26 06:23:59 +02001587 ret = mbedtls_pk_sign( key_slot->pk,
1588 ctx->md_alg,
1589 ctx->input, ctx->input_len,
1590 output, output_len,
1591 config_data->f_rng, config_data->p_rng );
1592 break;
1593 default:
Gilles Peskine6331d782018-04-26 13:27:43 +02001594 mbedtls_printf( "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
Gilles Peskined3268832018-04-26 06:23:59 +02001595 ctx->slot, (long) ctx->operation_type );
Gilles Peskine44817442018-06-13 18:09:28 +02001596 mbedtls_free( ctx );
Gilles Peskined3268832018-04-26 06:23:59 +02001597 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1598 break;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001599 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001600
Gilles Peskinef5a99962018-04-30 16:37:23 +02001601 op_name = ssl_async_operation_names[ctx->operation_type];
1602
Gilles Peskinec9125722018-04-26 07:15:40 +02001603 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001604 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001605 mbedtls_printf( "Async resume callback: %s done but injected error\n",
1606 op_name );
Gilles Peskine2636fad2018-06-12 14:17:39 +02001607 mbedtls_free( ctx );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001608 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1609 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001610
Gilles Peskine6331d782018-04-26 13:27:43 +02001611 mbedtls_printf( "Async resume (slot %u): %s done, status=%d.\n",
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001612 ctx->slot, op_name, ret );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001613 mbedtls_free( ctx );
1614 return( ret );
1615}
1616
Gilles Peskine8f97af72018-04-26 11:46:10 +02001617static void ssl_async_cancel( mbedtls_ssl_context *ssl )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001618{
Gilles Peskinea668c602018-04-30 11:54:39 +02001619 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001620 mbedtls_printf( "Async cancel callback.\n" );
1621 mbedtls_free( ctx );
1622}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001623#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001624
Hanno Becker16970d22017-10-10 15:56:37 +01001625/*
1626 * Wait for an event from the underlying transport or the timer
1627 * (Used in event-driven IO mode).
1628 */
1629#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001630int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001631 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001632#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001633int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001634 mbedtls_timing_delay_context *timer,
1635 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001636#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001637{
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001638 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +01001639 int poll_type = 0;
1640
1641 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
1642 poll_type = MBEDTLS_NET_POLL_WRITE;
1643 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
1644 poll_type = MBEDTLS_NET_POLL_READ;
1645#if !defined(MBEDTLS_TIMING_C)
1646 else
Hanno Beckeref527962018-03-15 15:49:24 +00001647 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001648#endif
1649
1650 while( 1 )
1651 {
Hanno Becker197a91c2017-10-31 10:58:53 +00001652 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +01001653#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001654 if( timer != NULL &&
1655 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +01001656 {
Hanno Becker16970d22017-10-10 15:56:37 +01001657 break;
1658 }
Hanno Becker197a91c2017-10-31 10:58:53 +00001659#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +01001660
Hanno Becker197a91c2017-10-31 10:58:53 +00001661 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001662 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +01001663 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001664 ret = mbedtls_net_poll( fd, poll_type, 0 );
1665 if( ret < 0 )
1666 return( ret );
1667 if( ret == poll_type )
1668 break;
Hanno Becker16970d22017-10-10 15:56:37 +01001669 }
1670 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001671
1672 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001673}
1674
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001675#if defined(MBEDTLS_USE_PSA_CRYPTO)
Janos Follathbe4efc22019-08-08 11:38:18 +01001676static psa_status_t psa_setup_psk_key_slot( psa_key_handle_t *slot,
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001677 psa_algorithm_t alg,
1678 unsigned char *psk,
1679 size_t psk_len )
1680{
1681 psa_status_t status;
Janos Follathbe4efc22019-08-08 11:38:18 +01001682 psa_key_attributes_t key_attributes;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001683
Janos Follathbe4efc22019-08-08 11:38:18 +01001684 key_attributes = psa_key_attributes_init();
1685 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1686 psa_set_key_algorithm( &key_attributes, alg );
1687 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001688
Janos Follathbe4efc22019-08-08 11:38:18 +01001689 status = psa_import_key( &key_attributes, psk, psk_len, slot );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001690 if( status != PSA_SUCCESS )
Hanno Becker1d911cd2018-11-15 13:06:09 +00001691 {
1692 fprintf( stderr, "IMPORT\n" );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001693 return( status );
Hanno Becker1d911cd2018-11-15 13:06:09 +00001694 }
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001695
1696 return( PSA_SUCCESS );
1697}
1698#endif /* MBEDTLS_USE_PSA_CRYPTO */
1699
Hanno Beckera0e20d02019-05-15 14:03:01 +01001700#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001701int report_cid_usage( mbedtls_ssl_context *ssl,
1702 const char *additional_description )
1703{
1704 int ret;
1705 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
1706 size_t peer_cid_len;
1707 int cid_negotiated;
1708
1709 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1710 return( 0 );
1711
1712 /* Check if the use of a CID has been negotiated */
1713 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
1714 peer_cid, &peer_cid_len );
1715 if( ret != 0 )
1716 {
1717 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
1718 -ret );
1719 return( ret );
1720 }
1721
1722 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
1723 {
1724 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
1725 {
1726 mbedtls_printf( "(%s) Use of Connection ID was not offered by client.\n",
1727 additional_description );
1728 }
1729 }
1730 else
1731 {
1732 size_t idx=0;
1733 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
1734 additional_description );
1735 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
1736 additional_description,
1737 (unsigned) peer_cid_len );
1738 while( idx < peer_cid_len )
1739 {
1740 mbedtls_printf( "%02x ", peer_cid[ idx ] );
1741 idx++;
1742 }
1743 mbedtls_printf( "\n" );
1744 }
1745
1746 return( 0 );
1747}
Hanno Beckera0e20d02019-05-15 14:03:01 +01001748#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001749
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001750int main( int argc, char *argv[] )
1751{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001752 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001753 int version_suites[4][2];
Hanno Beckerdcc94e62019-07-03 17:05:43 +01001754 io_ctx_t io_ctx;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001755 unsigned char* buf = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01001756#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001757#if defined(MBEDTLS_USE_PSA_CRYPTO)
1758 psa_algorithm_t alg = 0;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001759 psa_key_handle_t psk_slot = 0;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001760#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +02001762 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +02001763 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +02001764#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001765 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001766 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02001767 size_t cliip_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768#if defined(MBEDTLS_SSL_COOKIE_C)
1769 mbedtls_ssl_cookie_ctx cookie_ctx;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001770#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001771
Gilles Peskineef86ab22017-05-05 18:59:02 +02001772#if defined(MBEDTLS_X509_CRT_PARSE_C)
1773 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
1774#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 mbedtls_entropy_context entropy;
1776 mbedtls_ctr_drbg_context ctr_drbg;
1777 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001778 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001779#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001780 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001781#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001783 unsigned char renego_period[8] = { 0 };
1784#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +02001786 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 mbedtls_x509_crt cacert;
1788 mbedtls_x509_crt srvcert;
1789 mbedtls_pk_context pkey;
1790 mbedtls_x509_crt srvcert2;
1791 mbedtls_pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001792 int key_cert_init = 0, key_cert_init2 = 0;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001793#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001794 ssl_async_key_context_t ssl_async_keys;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001795#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001796#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1798 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001799#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800#if defined(MBEDTLS_SSL_CACHE_C)
1801 mbedtls_ssl_cache_context cache;
Paul Bakker0a597072012-09-25 21:55:46 +00001802#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001803#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1804 mbedtls_ssl_ticket_context ticket_ctx;
1805#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001806#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001807 sni_entry *sni_info = NULL;
1808#endif
Hanno Beckere6706e62017-05-15 16:05:15 +01001809#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +01001810 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +01001811 const mbedtls_ecp_curve_info * curve_cur;
1812#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +01001814 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001815#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Simon Butchercce68be2018-07-23 14:26:09 +01001817 unsigned char alloc_buf[MEMORY_HEAP_SIZE];
Paul Bakker82024bf2013-07-04 11:52:32 +02001818#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001819
Hanno Beckera0e20d02019-05-15 14:03:01 +01001820#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +01001821 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001822 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckera7d25422019-04-09 17:28:10 +01001823 size_t cid_len = 0;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001824 size_t cid_renego_len = 0;
Hanno Beckera7d25422019-04-09 17:28:10 +01001825#endif
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02001826#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1827 unsigned char *context_buf = NULL;
Gilles Peskine4e8b5942019-09-20 19:12:31 +02001828 size_t context_buf_len = 0;
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02001829#endif
Hanno Beckera7d25422019-04-09 17:28:10 +01001830
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001831 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001832 char *p, *q;
1833 const int *list;
Hanno Becker5a9942e2018-11-12 17:47:48 +00001834#if defined(MBEDTLS_USE_PSA_CRYPTO)
1835 psa_status_t status;
1836#endif
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001837#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1838 unsigned char eap_tls_keymaterial[16];
1839 unsigned char eap_tls_iv[8];
1840 const char* eap_tls_label = "client EAP encryption";
1841 eap_tls_keys eap_tls_keying;
1842#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1845 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Piotr Nowicki0937ed22019-11-26 16:32:40 +01001846#if defined(MBEDTLS_MEMORY_DEBUG)
1847 size_t current_heap_memory, peak_heap_memory, heap_blocks;
1848#endif /* MBEDTLS_MEMORY_DEBUG */
1849#endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
Paul Bakker82024bf2013-07-04 11:52:32 +02001850
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001851 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +02001852 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001853 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001854 mbedtls_net_init( &client_fd );
1855 mbedtls_net_init( &listen_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001856 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001857 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001858 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859#if defined(MBEDTLS_X509_CRT_PARSE_C)
1860 mbedtls_x509_crt_init( &cacert );
1861 mbedtls_x509_crt_init( &srvcert );
1862 mbedtls_pk_init( &pkey );
1863 mbedtls_x509_crt_init( &srvcert2 );
1864 mbedtls_pk_init( &pkey2 );
Gilles Peskine4d9ec4d2018-04-26 14:33:43 +02001865#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
1866 memset( &ssl_async_keys, 0, sizeof( ssl_async_keys ) );
1867#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001868#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1870 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001871#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872#if defined(MBEDTLS_SSL_CACHE_C)
1873 mbedtls_ssl_cache_init( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00001874#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001875#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1876 mbedtls_ssl_ticket_init( &ticket_ctx );
1877#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +02001879 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001880#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881#if defined(MBEDTLS_SSL_COOKIE_C)
1882 mbedtls_ssl_cookie_init( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001883#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001884
Hanno Becker5a9942e2018-11-12 17:47:48 +00001885#if defined(MBEDTLS_USE_PSA_CRYPTO)
1886 status = psa_crypto_init();
1887 if( status != PSA_SUCCESS )
1888 {
1889 mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
1890 (int) status );
1891 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1892 goto exit;
1893 }
1894#endif
1895
Paul Bakkerc1283d32014-08-18 11:05:51 +02001896#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001897 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001898 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001899 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +02001900#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001901
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001902 if( argc == 0 )
1903 {
1904 usage:
1905 if( ret == 0 )
1906 ret = 1;
1907
Gilles Peskineb2971ff2020-04-14 19:41:01 +02001908 mbedtls_printf( USAGE1 );
1909 mbedtls_printf( USAGE2 );
1910 mbedtls_printf( USAGE3 );
1911 mbedtls_printf( USAGE4 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 list = mbedtls_ssl_list_ciphersuites();
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001914 while( *list )
1915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001917 list++;
1918 if( !*list )
1919 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001921 list++;
1922 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923 mbedtls_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001924 goto exit;
1925 }
1926
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001927 opt.buffer_size = DFL_IO_BUF_LEN;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001928 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001929 opt.server_port = DFL_SERVER_PORT;
1930 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker16970d22017-10-10 15:56:37 +01001931 opt.event = DFL_EVENT;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001932 opt.response_size = DFL_RESPONSE_SIZE;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001933 opt.nbio = DFL_NBIO;
Hanno Beckera7d25422019-04-09 17:28:10 +01001934 opt.cid_enabled = DFL_CID_ENABLED;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001935 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
Hanno Beckera7d25422019-04-09 17:28:10 +01001936 opt.cid_val = DFL_CID_VALUE;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001937 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001938 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001939 opt.ca_file = DFL_CA_FILE;
1940 opt.ca_path = DFL_CA_PATH;
1941 opt.crt_file = DFL_CRT_FILE;
1942 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001943 opt.crt_file2 = DFL_CRT_FILE2;
1944 opt.key_file2 = DFL_KEY_FILE2;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001945 opt.async_operations = DFL_ASYNC_OPERATIONS;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001946 opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
1947 opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
1948 opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
Paul Bakkerfbb17802013-04-17 19:10:21 +02001949 opt.psk = DFL_PSK;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001950#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001951 opt.psk_opaque = DFL_PSK_OPAQUE;
1952 opt.psk_list_opaque = DFL_PSK_LIST_OPAQUE;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001953#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001954#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1955 opt.ca_callback = DFL_CA_CALLBACK;
1956#endif
Paul Bakkerfbb17802013-04-17 19:10:21 +02001957 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001958 opt.psk_list = DFL_PSK_LIST;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001959 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001960 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001961 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001962 opt.renegotiation = DFL_RENEGOTIATION;
1963 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001964 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001965 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001966 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001967 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001968 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001969 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001970 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001971 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001972 opt.auth_mode = DFL_AUTH_MODE;
Janos Follath4817e272017-04-10 13:44:33 +01001973 opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001974 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001975 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001976 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001977 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001978 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001979 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001980 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001981 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001982 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001983 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001984 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001985 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001986 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001987 opt.hs_to_min = DFL_HS_TO_MIN;
1988 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001989 opt.dtls_mtu = DFL_DTLS_MTU;
Hanno Beckere7675d02018-08-14 13:28:56 +01001990 opt.dgram_packing = DFL_DGRAM_PACKING;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001991 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001992 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001993 opt.etm = DFL_ETM;
Jarno Lamsa9831c8a2019-05-29 13:33:32 +03001994 opt.serialize = DFL_SERIALIZE;
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001995 opt.eap_tls = DFL_EAP_TLS;
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001996 opt.reproducible = DFL_REPRODUCIBLE;
Hanno Becker48f3a3d2019-08-29 06:31:22 +01001997 opt.nss_keylog = DFL_NSS_KEYLOG;
1998 opt.nss_keylog_file = DFL_NSS_KEYLOG_FILE;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001999
2000 for( i = 1; i < argc; i++ )
2001 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002002 p = argv[i];
2003 if( ( q = strchr( p, '=' ) ) == NULL )
2004 goto usage;
2005 *q++ = '\0';
2006
2007 if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02002008 opt.server_port = q;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01002009 else if( strcmp( p, "server_addr" ) == 0 )
2010 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01002011 else if( strcmp( p, "dtls" ) == 0 )
2012 {
2013 int t = atoi( q );
2014 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01002016 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01002018 else
2019 goto usage;
2020 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002021 else if( strcmp( p, "debug_level" ) == 0 )
2022 {
2023 opt.debug_level = atoi( q );
2024 if( opt.debug_level < 0 || opt.debug_level > 65535 )
2025 goto usage;
2026 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002027 else if( strcmp( p, "nbio" ) == 0 )
2028 {
2029 opt.nbio = atoi( q );
2030 if( opt.nbio < 0 || opt.nbio > 2 )
2031 goto usage;
2032 }
Hanno Becker16970d22017-10-10 15:56:37 +01002033 else if( strcmp( p, "event" ) == 0 )
2034 {
2035 opt.event = atoi( q );
2036 if( opt.event < 0 || opt.event > 2 )
2037 goto usage;
2038 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002039 else if( strcmp( p, "read_timeout" ) == 0 )
2040 opt.read_timeout = atoi( q );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002041 else if( strcmp( p, "buffer_size" ) == 0 )
2042 {
2043 opt.buffer_size = atoi( q );
2044 if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
2045 goto usage;
2046 }
2047 else if( strcmp( p, "response_size" ) == 0 )
2048 {
2049 opt.response_size = atoi( q );
2050 if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
2051 goto usage;
2052 if( opt.buffer_size < opt.response_size )
2053 opt.buffer_size = opt.response_size;
2054 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002055 else if( strcmp( p, "ca_file" ) == 0 )
2056 opt.ca_file = q;
2057 else if( strcmp( p, "ca_path" ) == 0 )
2058 opt.ca_path = q;
2059 else if( strcmp( p, "crt_file" ) == 0 )
2060 opt.crt_file = q;
2061 else if( strcmp( p, "key_file" ) == 0 )
2062 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002063 else if( strcmp( p, "crt_file2" ) == 0 )
2064 opt.crt_file2 = q;
2065 else if( strcmp( p, "key_file2" ) == 0 )
2066 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002067 else if( strcmp( p, "dhm_file" ) == 0 )
2068 opt.dhm_file = q;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002069#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002070 else if( strcmp( p, "async_operations" ) == 0 )
2071 opt.async_operations = q;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002072 else if( strcmp( p, "async_private_delay1" ) == 0 )
2073 opt.async_private_delay1 = atoi( q );
2074 else if( strcmp( p, "async_private_delay2" ) == 0 )
2075 opt.async_private_delay2 = atoi( q );
2076 else if( strcmp( p, "async_private_error" ) == 0 )
2077 {
2078 int n = atoi( q );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002079 if( n < -SSL_ASYNC_INJECT_ERROR_MAX ||
2080 n > SSL_ASYNC_INJECT_ERROR_MAX )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002081 {
2082 ret = 2;
2083 goto usage;
2084 }
2085 opt.async_private_error = n;
2086 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002087#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Hanno Beckera0e20d02019-05-15 14:03:01 +01002088#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +01002089 else if( strcmp( p, "cid" ) == 0 )
2090 {
2091 opt.cid_enabled = atoi( q );
2092 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
2093 goto usage;
2094 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002095 else if( strcmp( p, "cid_renego" ) == 0 )
2096 {
2097 opt.cid_enabled_renego = atoi( q );
2098 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
2099 goto usage;
2100 }
Hanno Beckera7d25422019-04-09 17:28:10 +01002101 else if( strcmp( p, "cid_val" ) == 0 )
2102 {
2103 opt.cid_val = q;
2104 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002105 else if( strcmp( p, "cid_val_renego" ) == 0 )
2106 {
2107 opt.cid_val_renego = q;
2108 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002109#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002110 else if( strcmp( p, "psk" ) == 0 )
2111 opt.psk = q;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002112#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002113 else if( strcmp( p, "psk_opaque" ) == 0 )
2114 opt.psk_opaque = atoi( q );
2115 else if( strcmp( p, "psk_list_opaque" ) == 0 )
2116 opt.psk_list_opaque = atoi( q );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002117#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02002118#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2119 else if( strcmp( p, "ca_callback" ) == 0)
2120 opt.ca_callback = atoi( q );
2121#endif
Paul Bakkerfbb17802013-04-17 19:10:21 +02002122 else if( strcmp( p, "psk_identity" ) == 0 )
2123 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002124 else if( strcmp( p, "psk_list" ) == 0 )
2125 opt.psk_list = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002126 else if( strcmp( p, "ecjpake_pw" ) == 0 )
2127 opt.ecjpake_pw = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002128 else if( strcmp( p, "force_ciphersuite" ) == 0 )
2129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002131
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02002132 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002133 {
2134 ret = 2;
2135 goto usage;
2136 }
2137 opt.force_ciphersuite[1] = 0;
2138 }
Hanno Beckere6706e62017-05-15 16:05:15 +01002139 else if( strcmp( p, "curves" ) == 0 )
2140 opt.curves = q;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002141 else if( strcmp( p, "version_suites" ) == 0 )
2142 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002143 else if( strcmp( p, "renegotiation" ) == 0 )
2144 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002145 opt.renegotiation = (atoi( q )) ?
2146 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
2147 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002148 }
2149 else if( strcmp( p, "allow_legacy" ) == 0 )
2150 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002151 switch( atoi( q ) )
2152 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002153 case -1:
2154 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
2155 break;
2156 case 0:
2157 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
2158 break;
2159 case 1:
2160 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
2161 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002162 default: goto usage;
2163 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002164 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002165 else if( strcmp( p, "renegotiate" ) == 0 )
2166 {
2167 opt.renegotiate = atoi( q );
2168 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
2169 goto usage;
2170 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02002171 else if( strcmp( p, "renego_delay" ) == 0 )
2172 {
2173 opt.renego_delay = atoi( q );
2174 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002175 else if( strcmp( p, "renego_period" ) == 0 )
2176 {
Andres AG0b736db2017-02-08 14:05:57 +00002177#if defined(_MSC_VER)
2178 opt.renego_period = _strtoui64( q, NULL, 10 );
2179#else
2180 if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
2181 goto usage;
2182#endif /* _MSC_VER */
2183 if( opt.renego_period < 2 )
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002184 goto usage;
2185 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02002186 else if( strcmp( p, "exchanges" ) == 0 )
2187 {
2188 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02002189 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02002190 goto usage;
2191 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00002192 else if( strcmp( p, "min_version" ) == 0 )
2193 {
2194 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002196 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01002198 else if( strcmp( q, "tls1_1" ) == 0 ||
2199 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01002201 else if( strcmp( q, "tls1_2" ) == 0 ||
2202 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002204 else
2205 goto usage;
2206 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002207 else if( strcmp( p, "max_version" ) == 0 )
2208 {
2209 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002211 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01002213 else if( strcmp( q, "tls1_1" ) == 0 ||
2214 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01002216 else if( strcmp( q, "tls1_2" ) == 0 ||
2217 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002219 else
2220 goto usage;
2221 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01002222 else if( strcmp( p, "arc4" ) == 0 )
2223 {
2224 switch( atoi( q ) )
2225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
2227 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01002228 default: goto usage;
2229 }
2230 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02002231 else if( strcmp( p, "allow_sha1" ) == 0 )
2232 {
2233 switch( atoi( q ) )
2234 {
2235 case 0: opt.allow_sha1 = 0; break;
2236 case 1: opt.allow_sha1 = 1; break;
2237 default: goto usage;
2238 }
2239 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002240 else if( strcmp( p, "force_version" ) == 0 )
2241 {
2242 if( strcmp( q, "ssl3" ) == 0 )
2243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
2245 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002246 }
2247 else if( strcmp( q, "tls1" ) == 0 )
2248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
2250 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002251 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002252 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02002253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
2255 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002256 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002257 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02002258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
2260 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002261 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002262 else if( strcmp( q, "dtls1" ) == 0 )
2263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
2265 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
2266 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002267 }
2268 else if( strcmp( q, "dtls1_2" ) == 0 )
2269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
2271 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
2272 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002273 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002274 else
2275 goto usage;
2276 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01002277 else if( strcmp( p, "auth_mode" ) == 0 )
2278 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02002279 if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
Paul Bakker91ebfb52012-11-23 14:04:08 +01002280 goto usage;
2281 }
Janos Follath4817e272017-04-10 13:44:33 +01002282 else if( strcmp( p, "cert_req_ca_list" ) == 0 )
2283 {
2284 opt.cert_req_ca_list = atoi( q );
2285 if( opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1 )
2286 goto usage;
2287 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002288 else if( strcmp( p, "max_frag_len" ) == 0 )
2289 {
2290 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002292 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002294 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002296 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002298 else
2299 goto usage;
2300 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002301 else if( strcmp( p, "alpn" ) == 0 )
2302 {
2303 opt.alpn_string = q;
2304 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002305 else if( strcmp( p, "trunc_hmac" ) == 0 )
2306 {
2307 switch( atoi( q ) )
2308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
2310 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002311 default: goto usage;
2312 }
2313 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002314 else if( strcmp( p, "extended_ms" ) == 0 )
2315 {
2316 switch( atoi( q ) )
2317 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002318 case 0:
2319 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
2320 break;
2321 case 1:
2322 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
2323 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002324 default: goto usage;
2325 }
2326 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002327 else if( strcmp( p, "etm" ) == 0 )
2328 {
2329 switch( atoi( q ) )
2330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
2332 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002333 default: goto usage;
2334 }
2335 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002336 else if( strcmp( p, "tickets" ) == 0 )
2337 {
2338 opt.tickets = atoi( q );
2339 if( opt.tickets < 0 || opt.tickets > 1 )
2340 goto usage;
2341 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01002342 else if( strcmp( p, "ticket_timeout" ) == 0 )
2343 {
2344 opt.ticket_timeout = atoi( q );
2345 if( opt.ticket_timeout < 0 )
2346 goto usage;
2347 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002348 else if( strcmp( p, "cache_max" ) == 0 )
2349 {
2350 opt.cache_max = atoi( q );
2351 if( opt.cache_max < 0 )
2352 goto usage;
2353 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002354 else if( strcmp( p, "cache_timeout" ) == 0 )
2355 {
2356 opt.cache_timeout = atoi( q );
2357 if( opt.cache_timeout < 0 )
2358 goto usage;
2359 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002360 else if( strcmp( p, "cookies" ) == 0 )
2361 {
2362 opt.cookies = atoi( q );
2363 if( opt.cookies < -1 || opt.cookies > 1)
2364 goto usage;
2365 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002366 else if( strcmp( p, "anti_replay" ) == 0 )
2367 {
2368 opt.anti_replay = atoi( q );
2369 if( opt.anti_replay < 0 || opt.anti_replay > 1)
2370 goto usage;
2371 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002372 else if( strcmp( p, "badmac_limit" ) == 0 )
2373 {
2374 opt.badmac_limit = atoi( q );
2375 if( opt.badmac_limit < 0 )
2376 goto usage;
2377 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002378 else if( strcmp( p, "hs_timeout" ) == 0 )
2379 {
2380 if( ( p = strchr( q, '-' ) ) == NULL )
2381 goto usage;
2382 *p++ = '\0';
2383 opt.hs_to_min = atoi( q );
2384 opt.hs_to_max = atoi( p );
2385 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
2386 goto usage;
2387 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002388 else if( strcmp( p, "mtu" ) == 0 )
2389 {
2390 opt.dtls_mtu = atoi( q );
2391 if( opt.dtls_mtu < 0 )
2392 goto usage;
2393 }
Hanno Beckere7675d02018-08-14 13:28:56 +01002394 else if( strcmp( p, "dgram_packing" ) == 0 )
2395 {
2396 opt.dgram_packing = atoi( q );
2397 if( opt.dgram_packing != 0 &&
2398 opt.dgram_packing != 1 )
2399 {
2400 goto usage;
2401 }
2402 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002403 else if( strcmp( p, "sni" ) == 0 )
2404 {
2405 opt.sni = q;
2406 }
Andres Amaya Garciabc818842018-10-16 21:08:38 +01002407 else if( strcmp( p, "query_config" ) == 0 )
2408 {
2409 return query_config( q );
2410 }
Jarno Lamsa9831c8a2019-05-29 13:33:32 +03002411 else if( strcmp( p, "serialize") == 0 )
2412 {
2413 opt.serialize = atoi( q );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03002414 if( opt.serialize < 0 || opt.serialize > 2)
Jarno Lamsa9831c8a2019-05-29 13:33:32 +03002415 goto usage;
2416 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002417 else if( strcmp( p, "eap_tls" ) == 0 )
2418 {
2419 opt.eap_tls = atoi( q );
2420 if( opt.eap_tls < 0 || opt.eap_tls > 1 )
2421 goto usage;
2422 }
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002423 else if( strcmp( p, "reproducible" ) == 0 )
2424 {
2425 opt.reproducible = 1;
2426 }
Hanno Becker48f3a3d2019-08-29 06:31:22 +01002427 else if( strcmp( p, "nss_keylog" ) == 0 )
2428 {
2429 opt.nss_keylog = atoi( q );
2430 if( opt.nss_keylog < 0 || opt.nss_keylog > 1 )
2431 goto usage;
2432 }
2433 else if( strcmp( p, "nss_keylog_file" ) == 0 )
2434 {
2435 opt.nss_keylog_file = q;
2436 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002437 else
2438 goto usage;
2439 }
2440
Hanno Beckerbc5308c2019-09-09 11:38:51 +01002441 if( opt.nss_keylog != 0 && opt.eap_tls != 0 )
2442 {
2443 mbedtls_printf( "Error: eap_tls and nss_keylog options cannot be used together.\n" );
2444 goto usage;
2445 }
2446
Hanno Becker16970d22017-10-10 15:56:37 +01002447 /* Event-driven IO is incompatible with the above custom
2448 * receive and send functions, as the polling builds on
2449 * refers to the underlying net_context. */
2450 if( opt.event == 1 && opt.nbio != 1 )
2451 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002452 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01002453 opt.nbio = 1;
2454 }
2455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456#if defined(MBEDTLS_DEBUG_C)
2457 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02002458#endif
Andrzej Kurekda4029d2018-06-20 07:07:55 -04002459 buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002460 if( buf == NULL )
2461 {
Andrzej Kurek755890f2018-06-20 08:17:04 -04002462 mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002463 ret = 3;
2464 goto exit;
2465 }
Paul Bakkerc73079a2014-04-25 16:34:30 +02002466
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002467#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002468 if( opt.psk_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002469 {
2470 if( strlen( opt.psk ) == 0 )
2471 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00002472 mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002473 ret = 2;
2474 goto usage;
2475 }
2476
2477 if( opt.force_ciphersuite[0] <= 0 )
2478 {
2479 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" );
2480 ret = 2;
2481 goto usage;
2482 }
2483 }
2484
Hanno Becker1d911cd2018-11-15 13:06:09 +00002485 if( opt.psk_list_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002486 {
2487 if( opt.psk_list == NULL )
2488 {
2489 mbedtls_printf( "psk_slot set but no psk to be imported specified.\n" );
2490 ret = 2;
2491 goto usage;
2492 }
2493
2494 if( opt.force_ciphersuite[0] <= 0 )
2495 {
2496 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" );
2497 ret = 2;
2498 goto usage;
2499 }
2500 }
2501#endif /* MBEDTLS_USE_PSA_CRYPTO */
2502
Paul Bakkerc1516be2013-06-29 16:01:32 +02002503 if( opt.force_ciphersuite[0] > 0 )
2504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002506 ciphersuite_info =
2507 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002508
Paul Bakker5b55b792013-07-19 13:43:43 +02002509 if( opt.max_version != -1 &&
2510 ciphersuite_info->min_minor_ver > opt.max_version )
2511 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002512 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker5b55b792013-07-19 13:43:43 +02002513 ret = 2;
2514 goto usage;
2515 }
2516 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02002517 ciphersuite_info->max_minor_ver < opt.min_version )
2518 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002519 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002520 ret = 2;
2521 goto usage;
2522 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002523
2524 /* If we select a version that's not supported by
2525 * this suite, then there will be no common ciphersuite... */
2526 if( opt.max_version == -1 ||
2527 opt.max_version > ciphersuite_info->max_minor_ver )
2528 {
Paul Bakker5b55b792013-07-19 13:43:43 +02002529 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002530 }
Paul Bakker5b55b792013-07-19 13:43:43 +02002531 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002532 {
Paul Bakker5b55b792013-07-19 13:43:43 +02002533 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002534 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2536 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
2537 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002538 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002539
2540 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002546 ret = 2;
2547 goto usage;
2548 }
2549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002551 }
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002552
2553#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002554 if( opt.psk_opaque != 0 || opt.psk_list_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002555 {
2556 /* Ensure that the chosen ciphersuite is PSK-only; we must know
2557 * the ciphersuite in advance to set the correct policy for the
2558 * PSK key slot. This limitation might go away in the future. */
2559 if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
2560 opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 )
2561 {
2562 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" );
2563 ret = 2;
2564 goto usage;
2565 }
2566
2567 /* Determine KDF algorithm the opaque PSK will be used in. */
2568#if defined(MBEDTLS_SHA512_C)
2569 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
2570 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2571 else
2572#endif /* MBEDTLS_SHA512_C */
2573 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2574 }
2575#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerc1516be2013-06-29 16:01:32 +02002576 }
2577
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002578 if( opt.version_suites != NULL )
2579 {
2580 const char *name[4] = { 0 };
2581
2582 /* Parse 4-element coma-separated list */
2583 for( i = 0, p = (char *) opt.version_suites;
2584 i < 4 && *p != '\0';
2585 i++ )
2586 {
2587 name[i] = p;
2588
2589 /* Terminate the current string and move on to next one */
2590 while( *p != ',' && *p != '\0' )
2591 p++;
2592 if( *p == ',' )
2593 *p++ = '\0';
2594 }
2595
2596 if( i != 4 )
2597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 mbedtls_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002599 ret = 1;
2600 goto exit;
2601 }
2602
2603 memset( version_suites, 0, sizeof( version_suites ) );
2604
2605 /* Get the suites identifiers from their name */
2606 for( i = 0; i < 4; i++ )
2607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002609
2610 if( version_suites[i][0] == 0 )
2611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612 mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002613 ret = 2;
2614 goto usage;
2615 }
2616 }
2617 }
2618
Hanno Beckera0e20d02019-05-15 14:03:01 +01002619#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002620 if( unhexify( cid, opt.cid_val, &cid_len ) != 0 )
2621 {
2622 mbedtls_printf( "CID not valid hex\n" );
2623 goto exit;
2624 }
2625
2626 /* Keep CID settings for renegotiation unless
2627 * specified otherwise. */
2628 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
2629 opt.cid_enabled_renego = opt.cid_enabled;
2630 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
2631 opt.cid_val_renego = opt.cid_val;
2632
2633 if( unhexify( cid_renego, opt.cid_val_renego, &cid_renego_len ) != 0 )
2634 {
2635 mbedtls_printf( "CID not valid hex\n" );
2636 goto exit;
2637 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002638#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +01002639
Gilles Peskineeccd8882020-03-10 12:19:08 +01002640#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002641 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002642 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02002643 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002644 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 mbedtls_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002647 goto exit;
2648 }
2649
2650 if( opt.psk_list != NULL )
2651 {
2652 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 mbedtls_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002655 goto exit;
2656 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002657 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002658#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002659
Hanno Beckere6706e62017-05-15 16:05:15 +01002660#if defined(MBEDTLS_ECP_C)
2661 if( opt.curves != NULL )
2662 {
2663 p = (char *) opt.curves;
2664 i = 0;
2665
2666 if( strcmp( p, "none" ) == 0 )
2667 {
2668 curve_list[0] = MBEDTLS_ECP_DP_NONE;
2669 }
2670 else if( strcmp( p, "default" ) != 0 )
2671 {
2672 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01002673 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002674 {
2675 q = p;
2676
2677 /* Terminate the current string */
2678 while( *p != ',' && *p != '\0' )
2679 p++;
2680 if( *p == ',' )
2681 *p++ = '\0';
2682
2683 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
2684 {
2685 curve_list[i++] = curve_cur->grp_id;
2686 }
2687 else
2688 {
2689 mbedtls_printf( "unknown curve %s\n", q );
2690 mbedtls_printf( "supported curves: " );
2691 for( curve_cur = mbedtls_ecp_curve_list();
2692 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
2693 curve_cur++ )
2694 {
2695 mbedtls_printf( "%s ", curve_cur->name );
2696 }
2697 mbedtls_printf( "\n" );
2698 goto exit;
2699 }
2700 }
2701
2702 mbedtls_printf("Number of curves: %d\n", i );
2703
Hanno Becker8651a432017-06-09 16:13:22 +01002704 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002705 {
Hanno Becker8651a432017-06-09 16:13:22 +01002706 mbedtls_printf( "curves list too long, maximum %d",
2707 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01002708 goto exit;
2709 }
2710
2711 curve_list[i] = MBEDTLS_ECP_DP_NONE;
2712 }
2713 }
2714#endif /* MBEDTLS_ECP_C */
2715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002717 if( opt.alpn_string != NULL )
2718 {
2719 p = (char *) opt.alpn_string;
2720 i = 0;
2721
2722 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01002723 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002724 {
2725 alpn_list[i++] = p;
2726
2727 /* Terminate the current string and move on to next one */
2728 while( *p != ',' && *p != '\0' )
2729 p++;
2730 if( *p == ',' )
2731 *p++ = '\0';
2732 }
2733 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002735
Paul Bakkerfbb17802013-04-17 19:10:21 +02002736 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002737 * 0. Initialize the RNG and the session data
2738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002740 fflush( stdout );
2741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 mbedtls_entropy_init( &entropy );
Philippe Antoine986b6f22019-06-07 15:04:32 +02002743 if (opt.reproducible)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002744 {
Philippe Antoine738153a2019-06-18 20:16:43 +02002745 srand( 1 );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002746 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy,
Philippe Antoine986b6f22019-06-07 15:04:32 +02002747 &entropy, (const unsigned char *) pers,
2748 strlen( pers ) ) ) != 0 )
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002749 {
2750 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
Philippe Antoine986b6f22019-06-07 15:04:32 +02002751 -ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002752 goto exit;
2753 }
Philippe Antoine986b6f22019-06-07 15:04:32 +02002754 }
2755 else
2756 {
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002757 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
Philippe Antoine986b6f22019-06-07 15:04:32 +02002758 &entropy, (const unsigned char *) pers,
2759 strlen( pers ) ) ) != 0 )
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002760 {
2761 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
Philippe Antoine986b6f22019-06-07 15:04:32 +02002762 -ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002763 goto exit;
2764 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002765 }
2766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002770 /*
2771 * 1.1. Load the trusted CA
2772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002774 fflush( stdout );
2775
Hanno Becker8174bdf2019-03-05 16:22:07 +00002776 if( strcmp( opt.ca_path, "none" ) == 0 ||
2777 strcmp( opt.ca_file, "none" ) == 0 )
2778 {
2779 ret = 0;
2780 }
2781 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002783 if( strlen( opt.ca_path ) )
Hanno Becker8174bdf2019-03-05 16:22:07 +00002784 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002785 else if( strlen( opt.ca_file ) )
Hanno Becker8174bdf2019-03-05 16:22:07 +00002786 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02002787 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002788#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789#if defined(MBEDTLS_CERTS_C)
Hanno Becker09b8cae2019-02-01 08:13:19 +00002790 {
2791#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794 ret = mbedtls_x509_crt_parse( &cacert,
2795 (const unsigned char *) mbedtls_test_cas[i],
2796 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002797 if( ret != 0 )
2798 break;
2799 }
Hanno Becker09b8cae2019-02-01 08:13:19 +00002800 if( ret == 0 )
2801#endif /* MBEDTLS_PEM_PARSE_C */
2802 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
2803 {
2804 ret = mbedtls_x509_crt_parse_der( &cacert,
2805 (const unsigned char *) mbedtls_test_cas_der[i],
2806 mbedtls_test_cas_der_len[i] );
2807 if( ret != 0 )
2808 break;
2809 }
2810 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002811#else
2812 {
2813 ret = 1;
Hanno Beckera0c5ceb2018-12-05 16:49:42 +00002814 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002815 }
Hanno Becker09b8cae2019-02-01 08:13:19 +00002816#endif /* MBEDTLS_CERTS_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002817 if( ret < 0 )
2818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002820 goto exit;
2821 }
2822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002824
2825 /*
2826 * 1.2. Load own certificate and private key
2827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002829 fflush( stdout );
2830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002832 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002833 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002834 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002838 -ret );
2839 goto exit;
2840 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002841 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002842 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002843 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002844 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002848 goto exit;
2849 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002850 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002851 if( key_cert_init == 1 )
2852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853 mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002854 goto exit;
2855 }
2856
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002857 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002858 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002859 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002863 -ret );
2864 goto exit;
2865 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002866 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002867 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002868 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002869 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002873 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002874 goto exit;
2875 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002876 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002877 if( key_cert_init2 == 1 )
2878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879 mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002880 goto exit;
2881 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002882#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002883 if( key_cert_init == 0 &&
2884 strcmp( opt.crt_file, "none" ) != 0 &&
2885 strcmp( opt.key_file, "none" ) != 0 &&
2886 key_cert_init2 == 0 &&
2887 strcmp( opt.crt_file2, "none" ) != 0 &&
2888 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890#if !defined(MBEDTLS_CERTS_C)
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002891 mbedtls_printf( "Not certificated or key provided, and \nMBEDTLS_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002892 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002893#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894#if defined(MBEDTLS_RSA_C)
2895 if( ( ret = mbedtls_x509_crt_parse( &srvcert,
2896 (const unsigned char *) mbedtls_test_srv_crt_rsa,
2897 mbedtls_test_srv_crt_rsa_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002898 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002899 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
2900 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002901 goto exit;
2902 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 if( ( ret = mbedtls_pk_parse_key( &pkey,
2904 (const unsigned char *) mbedtls_test_srv_key_rsa,
2905 mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002906 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002907 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
2908 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002909 goto exit;
2910 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002911 key_cert_init = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912#endif /* MBEDTLS_RSA_C */
2913#if defined(MBEDTLS_ECDSA_C)
2914 if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
2915 (const unsigned char *) mbedtls_test_srv_crt_ec,
2916 mbedtls_test_srv_crt_ec_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002917 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002918 mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n",
2919 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002920 goto exit;
2921 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 if( ( ret = mbedtls_pk_parse_key( &pkey2,
2923 (const unsigned char *) mbedtls_test_srv_key_ec,
2924 mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002925 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002926 mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n",
2927 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002928 goto exit;
2929 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002930 key_cert_init2 = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931#endif /* MBEDTLS_ECDSA_C */
2932#endif /* MBEDTLS_CERTS_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002933 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 mbedtls_printf( " ok\n" );
2936#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002939 if( opt.dhm_file != NULL )
2940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941 mbedtls_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002942 fflush( stdout );
2943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944 if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002947 -ret );
2948 goto exit;
2949 }
2950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002952 }
2953#endif
2954
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002955#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002956 if( opt.sni != NULL )
2957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958 mbedtls_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002959 fflush( stdout );
2960
2961 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
2962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002964 goto exit;
2965 }
2966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002968 }
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002969#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002970
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002971 /*
2972 * 2. Setup the listening TCP socket
2973 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02002974 mbedtls_printf( " . Bind on %s://%s:%s/ ...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002976 opt.server_addr ? opt.server_addr : "*",
2977 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002978 fflush( stdout );
2979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980 if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
2981 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2982 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984 mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002985 goto exit;
2986 }
2987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002989
2990 /*
2991 * 3. Setup stuff
2992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002994 fflush( stdout );
2995
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02002996 if( ( ret = mbedtls_ssl_config_defaults( &conf,
2997 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02002998 opt.transport,
2999 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003000 {
3001 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
3002 goto exit;
3003 }
3004
Gilles Peskineef86ab22017-05-05 18:59:02 +02003005#if defined(MBEDTLS_X509_CRT_PARSE_C)
3006 /* The default algorithms profile disables SHA-1, but our tests still
3007 rely on it heavily. Hence we allow it here. A real-world server
3008 should use the default profile unless there is a good reason not to. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02003009 if( opt.allow_sha1 > 0 )
3010 {
3011 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
3012 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
Gilles Peskine682df092017-05-11 19:01:11 +02003013 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
Gilles Peskinebc70a182017-05-09 15:59:24 +02003014 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02003015#endif /* MBEDTLS_X509_CRT_PARSE_C */
3016
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01003017 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003018 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003019
Janos Follath4817e272017-04-10 13:44:33 +01003020 if( opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST )
3021 mbedtls_ssl_conf_cert_req_ca_list( &conf, opt.cert_req_ca_list );
3022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003023#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02003024 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 +02003025 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003026
Hanno Beckere7675d02018-08-14 13:28:56 +01003027 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01003028 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02003030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003032 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003033 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003034 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003035 goto exit;
3036 };
Paul Bakker05decb22013-08-15 13:33:48 +02003037#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02003038
Hanno Beckera0e20d02019-05-15 14:03:01 +01003039#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003040 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckerad4a1372019-05-03 13:06:44 +01003041 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003042 if( opt.cid_enabled == 1 &&
3043 opt.cid_enabled_renego == 1 &&
3044 cid_len != cid_renego_len )
3045 {
3046 mbedtls_printf( "CID length must not change during renegotiation\n" );
3047 goto usage;
3048 }
3049
3050 if( opt.cid_enabled == 1 )
Hanno Becker8367ccc2019-05-14 11:30:10 +01003051 ret = mbedtls_ssl_conf_cid( &conf, cid_len,
3052 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003053 else
Hanno Becker8367ccc2019-05-14 11:30:10 +01003054 ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
3055 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003056
Hanno Beckerad4a1372019-05-03 13:06:44 +01003057 if( ret != 0 )
3058 {
Hanno Beckerd5eed422019-05-23 16:58:22 +01003059 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
3060 -ret );
Hanno Beckerad4a1372019-05-03 13:06:44 +01003061 goto exit;
3062 }
3063 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003064#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerad4a1372019-05-03 13:06:44 +01003065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003067 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003068 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003069#endif
3070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003072 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003073 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003074#endif
3075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003077 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003078 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003079#endif
3080
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003081#if defined(MBEDTLS_SSL_EXPORT_KEYS)
3082 if( opt.eap_tls != 0 )
Hanno Becker48f3a3d2019-08-29 06:31:22 +01003083 {
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003084 mbedtls_ssl_conf_export_keys_ext_cb( &conf, eap_tls_key_derivation,
3085 &eap_tls_keying );
Hanno Becker48f3a3d2019-08-29 06:31:22 +01003086 }
3087 else if( opt.nss_keylog != 0 )
3088 {
3089 mbedtls_ssl_conf_export_keys_ext_cb( &conf,
3090 nss_keylog_export,
3091 NULL );
3092 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003093#endif
3094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003096 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003097 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003098 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003099 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003100 goto exit;
3101 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003102#endif
3103
Philippe Antoine986b6f22019-06-07 15:04:32 +02003104 if (opt.reproducible)
3105 {
Philippe Antoinef91b3722019-06-11 16:02:43 +02003106#if defined(MBEDTLS_HAVE_TIME)
Philippe Antoineaa4d1522019-06-06 21:24:31 +02003107#if defined(MBEDTLS_PLATFORM_TIME_ALT)
3108 mbedtls_platform_set_time( dummy_constant_time );
3109#else
Philippe Antoine7c9d7242019-06-11 12:11:36 +02003110 fprintf( stderr, "Warning: reproducible option used without constant time\n" );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02003111#endif
Philippe Antoine0ff84fb2019-06-11 12:15:17 +02003112#endif
Philippe Antoine986b6f22019-06-07 15:04:32 +02003113 }
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003114 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
3115 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01003118 if( opt.cache_max != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119 mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01003120
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01003121 if( opt.cache_timeout != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122 mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01003123
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003124 mbedtls_ssl_conf_session_cache( &conf, &cache,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01003125 mbedtls_ssl_cache_get,
3126 mbedtls_ssl_cache_set );
Paul Bakker0a597072012-09-25 21:55:46 +00003127#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003130 if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003131 {
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003132 if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
3133 mbedtls_ctr_drbg_random, &ctr_drbg,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +02003134 MBEDTLS_CIPHER_AES_256_GCM,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003135 opt.ticket_timeout ) ) != 0 )
3136 {
3137 mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
3138 goto exit;
3139 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01003140
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003141 mbedtls_ssl_conf_session_tickets_cb( &conf,
3142 mbedtls_ssl_ticket_write,
3143 mbedtls_ssl_ticket_parse,
3144 &ticket_ctx );
3145 }
Paul Bakkera503a632013-08-14 13:48:06 +02003146#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148#if defined(MBEDTLS_SSL_PROTO_DTLS)
3149 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02003150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003152 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02003153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
3155 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003158 goto exit;
3159 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02003160
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003161 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003162 &cookie_ctx );
3163 }
3164 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165#endif /* MBEDTLS_SSL_COOKIE_C */
3166#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003167 if( opt.cookies == 0 )
3168 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003169 mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003170 }
3171 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003173 {
3174 ; /* Nothing to do */
3175 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003178 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003179 mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02003180#endif
3181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02003183 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003184 mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003185#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02003186 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02003188
Paul Bakker41c83d32013-03-20 14:39:14 +01003189 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003190 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00003191
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02003192#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00003193 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003194 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02003195#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003196
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02003197 if( opt.version_suites != NULL )
3198 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003199 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200 MBEDTLS_SSL_MAJOR_VERSION_3,
3201 MBEDTLS_SSL_MINOR_VERSION_0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003202 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203 MBEDTLS_SSL_MAJOR_VERSION_3,
3204 MBEDTLS_SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003205 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 MBEDTLS_SSL_MAJOR_VERSION_3,
3207 MBEDTLS_SSL_MINOR_VERSION_2 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003208 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209 MBEDTLS_SSL_MAJOR_VERSION_3,
3210 MBEDTLS_SSL_MINOR_VERSION_3 );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02003211 }
3212
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01003213 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003214 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003216 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01003217
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02003218 if( opt.renego_delay != DFL_RENEGO_DELAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003219 mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003220
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01003221 if( opt.renego_period != DFL_RENEGO_PERIOD )
3222 {
Andres AG692ad842017-01-19 16:30:57 +00003223 PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003224 mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01003225 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003226#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01003229 if( strcmp( opt.ca_path, "none" ) != 0 &&
3230 strcmp( opt.ca_file, "none" ) != 0 )
3231 {
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02003232#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3233 if( opt.ca_callback != 0 )
3234 mbedtls_ssl_conf_ca_cb( &conf, ca_callback, &cacert);
3235 else
3236#endif
3237 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01003238 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02003239 if( key_cert_init )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003240 {
3241 mbedtls_pk_context *pk = &pkey;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003242#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003243 if( opt.async_private_delay1 >= 0 )
3244 {
Gilles Peskine44817442018-06-13 18:09:28 +02003245 ret = ssl_async_set_key( &ssl_async_keys, &srvcert, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02003246 opt.async_private_delay1 );
3247 if( ret < 0 )
3248 {
3249 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
3250 ret );
3251 goto exit;
3252 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003253 pk = NULL;
3254 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003255#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003256 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003257 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003258 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003259 goto exit;
3260 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003261 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02003262 if( key_cert_init2 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003263 {
3264 mbedtls_pk_context *pk = &pkey2;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003265#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003266 if( opt.async_private_delay2 >= 0 )
3267 {
Gilles Peskine44817442018-06-13 18:09:28 +02003268 ret = ssl_async_set_key( &ssl_async_keys, &srvcert2, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02003269 opt.async_private_delay2 );
3270 if( ret < 0 )
3271 {
3272 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
3273 ret );
3274 goto exit;
3275 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003276 pk = NULL;
3277 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003278#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003279 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003280 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003281 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003282 goto exit;
3283 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003284 }
3285
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003286#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003287 if( opt.async_operations[0] != '-' )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003288 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003289 mbedtls_ssl_async_sign_t *sign = NULL;
3290 mbedtls_ssl_async_decrypt_t *decrypt = NULL;
Gilles Peskine12ab5d42018-04-24 12:32:04 +02003291 const char *r;
3292 for( r = opt.async_operations; *r; r++ )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003293 {
Gilles Peskine12ab5d42018-04-24 12:32:04 +02003294 switch( *r )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003295 {
3296 case 'd':
3297 decrypt = ssl_async_decrypt;
3298 break;
3299 case 's':
3300 sign = ssl_async_sign;
3301 break;
3302 }
3303 }
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003304 ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
3305 - opt.async_private_error :
3306 opt.async_private_error );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003307 ssl_async_keys.f_rng = mbedtls_ctr_drbg_random;
3308 ssl_async_keys.p_rng = &ctr_drbg;
3309 mbedtls_ssl_conf_async_private_cb( &conf,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003310 sign,
3311 decrypt,
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003312 ssl_async_resume,
3313 ssl_async_cancel,
3314 &ssl_async_keys );
3315 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003316#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003317#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +02003318
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02003319#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003320 if( opt.sni != NULL )
Gilles Peskine166ce742018-04-30 10:30:49 +02003321 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003322 mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
Gilles Peskine166ce742018-04-30 10:30:49 +02003323#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3324 if( opt.async_private_delay2 >= 0 )
3325 {
Gilles Peskinee2479892018-06-13 18:06:51 +02003326 sni_entry *cur;
3327 for( cur = sni_info; cur != NULL; cur = cur->next )
Gilles Peskine166ce742018-04-30 10:30:49 +02003328 {
Gilles Peskinee2479892018-06-13 18:06:51 +02003329 ret = ssl_async_set_key( &ssl_async_keys,
Gilles Peskine44817442018-06-13 18:09:28 +02003330 cur->cert, cur->key, 1,
Gilles Peskinee2479892018-06-13 18:06:51 +02003331 opt.async_private_delay2 );
3332 if( ret < 0 )
3333 {
3334 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
3335 ret );
3336 goto exit;
3337 }
3338 cur->key = NULL;
Gilles Peskine166ce742018-04-30 10:30:49 +02003339 }
Gilles Peskine166ce742018-04-30 10:30:49 +02003340 }
3341#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3342 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003343#endif
3344
Hanno Beckere6706e62017-05-15 16:05:15 +01003345#if defined(MBEDTLS_ECP_C)
3346 if( opt.curves != NULL &&
3347 strcmp( opt.curves, "default" ) != 0 )
3348 {
3349 mbedtls_ssl_conf_curves( &conf, curve_list );
3350 }
3351#endif
3352
Gilles Peskineeccd8882020-03-10 12:19:08 +01003353#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003354
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02003355 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
3356 {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003357#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00003358 if( opt.psk_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003359 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00003360 /* The algorithm has already been determined earlier. */
Janos Follathbe4efc22019-08-08 11:38:18 +01003361 status = psa_setup_psk_key_slot( &psk_slot, alg, psk, psk_len );
Hanno Becker1d911cd2018-11-15 13:06:09 +00003362 if( status != PSA_SUCCESS )
3363 {
3364 fprintf( stderr, "SETUP FAIL\n" );
3365 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
3366 goto exit;
3367 }
3368 if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, psk_slot,
3369 (const unsigned char *) opt.psk_identity,
3370 strlen( opt.psk_identity ) ) ) != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003371 {
3372 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
3373 ret );
3374 goto exit;
3375 }
3376 }
3377 else
3378#endif /* MBEDTLS_USE_PSA_CRYPTO */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01003379 if( psk_len > 0 )
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02003380 {
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01003381 ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
3382 (const unsigned char *) opt.psk_identity,
3383 strlen( opt.psk_identity ) );
3384 if( ret != 0 )
3385 {
3386 mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", - ret );
3387 goto exit;
3388 }
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02003389 }
3390 }
3391
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02003392 if( opt.psk_list != NULL )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003393 {
3394#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00003395 if( opt.psk_list_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003396 {
3397 psk_entry *cur_psk;
3398 for( cur_psk = psk_info; cur_psk != NULL; cur_psk = cur_psk->next )
3399 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00003400
Janos Follathbe4efc22019-08-08 11:38:18 +01003401 status = psa_setup_psk_key_slot( &cur_psk->slot, alg,
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003402 cur_psk->key,
3403 cur_psk->key_len );
3404 if( status != PSA_SUCCESS )
3405 {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003406 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
3407 goto exit;
3408 }
3409 }
3410 }
3411#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker1d911cd2018-11-15 13:06:09 +00003412
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003413 mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003414 }
Paul Bakkered27a042013-04-18 22:46:23 +02003415#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417#if defined(MBEDTLS_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00003418 /*
3419 * Use different group than default DHM group
3420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003422 if( opt.dhm_file != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003423 ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003424#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003425 if( ret != 0 )
3426 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003427 mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003428 goto exit;
3429 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003430#endif
3431
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02003432 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003433 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00003434
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02003435 if( opt.max_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003436 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakkerc1516be2013-06-29 16:01:32 +02003437
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003438 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
3439 {
3440 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
3441 goto exit;
3442 }
3443
Hanno Beckerdcc94e62019-07-03 17:05:43 +01003444 io_ctx.ssl = &ssl;
3445 io_ctx.net = &client_fd;
3446 mbedtls_ssl_set_bio( &ssl, &io_ctx, send_cb, recv_cb,
3447 opt.nbio == 0 ? recv_timeout_cb : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003448
Hanno Beckera0e20d02019-05-15 14:03:01 +01003449#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +01003450 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3451 {
3452 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
3453 cid, cid_len ) ) != 0 )
3454 {
3455 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
3456 ret );
3457 goto exit;
3458 }
3459 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003460#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +01003461
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02003462#if defined(MBEDTLS_SSL_PROTO_DTLS)
3463 if( opt.dtls_mtu != DFL_DTLS_MTU )
3464 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
3465#endif
3466
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003467#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02003468 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
3469 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003470#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02003471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003473
3474reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02003475#if !defined(_WIN32)
3476 if( received_sigterm )
3477 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01003478 mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" );
3479 if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT )
3480 ret = 0;
3481
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02003482 goto exit;
3483 }
3484#endif
3485
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003486 if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
3487 {
3488 mbedtls_printf( " ! Client initiated reconnection from same port\n" );
3489 goto handshake;
3490 }
3491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003493 if( ret != 0 )
3494 {
3495 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496 mbedtls_strerror( ret, error_buf, 100 );
3497 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003498 }
3499#endif
3500
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003501 mbedtls_net_free( &client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01003502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 mbedtls_ssl_session_reset( &ssl );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003504
3505 /*
3506 * 3. Wait until a client connects
3507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 mbedtls_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003509 fflush( stdout );
3510
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003511 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02003512 client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003513 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02003514#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003515 if( received_sigterm )
3516 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01003517 mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" );
3518 if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED )
3519 ret = 0;
3520
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003521 goto exit;
3522 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02003523#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003525 mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003526 goto exit;
3527 }
3528
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003529 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003530 ret = mbedtls_net_set_nonblock( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003531 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003532 ret = mbedtls_net_set_block( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003533 if( ret != 0 )
3534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003535 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003536 goto exit;
3537 }
3538
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003539 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
3542 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003543 {
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02003544 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
3545 client_ip, cliip_len ) ) != 0 )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003546 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01003547 mbedtls_printf( " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n",
3548 -ret );
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003549 goto exit;
3550 }
3551 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003553
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02003554#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3555 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
3556 {
3557 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
3558 (const unsigned char *) opt.ecjpake_pw,
3559 strlen( opt.ecjpake_pw ) ) ) != 0 )
3560 {
3561 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
3562 goto exit;
3563 }
3564 }
3565#endif
3566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003568
3569 /*
3570 * 4. Handshake
3571 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003572handshake:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003574 fflush( stdout );
3575
Hanno Becker16970d22017-10-10 15:56:37 +01003576 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003577 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003578#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003579 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003580 ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003581 {
3582 mbedtls_printf( " cancelling on injected error\n" );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003583 break;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003584 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003585#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskineb44692f2018-04-24 12:18:19 +02003586
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003587 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003588 break;
3589
3590 /* For event-driven IO, wait for socket to become available */
3591 if( opt.event == 1 /* level triggered IO */ )
3592 {
3593#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003594 ret = idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003595#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003596 ret = idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003597#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003598 if( ret != 0 )
3599 goto reset;
Hanno Becker16970d22017-10-10 15:56:37 +01003600 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003601 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 mbedtls_printf( " hello verification requested\n" );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003606 ret = 0;
3607 goto reset;
3608 }
3609 else if( ret != 0 )
3610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02003612
3613#if defined(MBEDTLS_X509_CRT_PARSE_C)
3614 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3615 {
3616 char vrfy_buf[512];
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02003617 flags = mbedtls_ssl_get_verify_result( &ssl );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02003618
3619 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
3620
3621 mbedtls_printf( "%s\n", vrfy_buf );
3622 }
3623#endif
3624
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003625#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003626 if( opt.async_private_error < 0 )
3627 /* Injected error only the first time round, to test reset */
3628 ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
3629#endif
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003630 goto reset;
3631 }
3632 else /* ret == 0 */
3633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003634 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
3635 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003636 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
3639 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02003640 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02003642
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003643#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003644 mbedtls_printf( " [ Maximum input fragment length is %u ]\n",
3645 (unsigned int) mbedtls_ssl_get_input_max_frag_len( &ssl ) );
3646 mbedtls_printf( " [ Maximum output fragment length is %u ]\n",
3647 (unsigned int) mbedtls_ssl_get_output_max_frag_len( &ssl ) );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003648#endif
3649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003651 if( opt.alpn_string != NULL )
3652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
3654 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003655 alp ? alp : "(none)" );
3656 }
3657#endif
3658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003660 /*
Ron Eldor2a47be52017-06-20 15:23:23 +03003661 * 5. Verify the client certificate
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003662 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003664
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003665 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003666 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003667 char vrfy_buf[512];
3668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 mbedtls_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003670
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003671 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003672
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003673 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003674 }
3675 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003676 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003677
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003678 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003679 {
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003680 char crt_buf[512];
3681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003682 mbedtls_printf( " . Peer certificate information ...\n" );
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003683 mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684 mbedtls_ssl_get_peer_cert( &ssl ) );
Manuel Pégourié-Gonnard67557172015-07-02 11:15:48 +02003685 mbedtls_printf( "%s\n", crt_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003686 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003688
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003689#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Ron Eldor51d3ab52019-05-12 14:54:30 +03003690 if( opt.eap_tls != 0 )
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003691 {
3692 size_t j = 0;
Ron Eldor51d3ab52019-05-12 14:54:30 +03003693
3694 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type,
3695 eap_tls_keying.master_secret,
3696 sizeof( eap_tls_keying.master_secret ),
3697 eap_tls_label,
3698 eap_tls_keying.randbytes,
3699 sizeof( eap_tls_keying.randbytes ),
3700 eap_tls_keymaterial,
3701 sizeof( eap_tls_keymaterial ) ) )
3702 != 0 )
3703 {
3704 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
3705 -ret );
3706 goto exit;
3707 }
3708
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003709 mbedtls_printf( " EAP-TLS key material is:" );
3710 for( j = 0; j < sizeof( eap_tls_keymaterial ); j++ )
3711 {
3712 if( j % 8 == 0 )
3713 mbedtls_printf("\n ");
3714 mbedtls_printf("%02x ", eap_tls_keymaterial[j] );
3715 }
3716 mbedtls_printf("\n");
3717
Ron Eldor51d3ab52019-05-12 14:54:30 +03003718 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type, NULL, 0,
Ron Eldor51c45072019-05-15 17:49:54 +03003719 eap_tls_label,
3720 eap_tls_keying.randbytes,
3721 sizeof( eap_tls_keying.randbytes ),
3722 eap_tls_iv,
3723 sizeof( eap_tls_iv ) ) ) != 0 )
Ron Eldor51d3ab52019-05-12 14:54:30 +03003724 {
3725 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
3726 -ret );
3727 goto exit;
3728 }
3729
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003730 mbedtls_printf( " EAP-TLS IV is:" );
3731 for( j = 0; j < sizeof( eap_tls_iv ); j++ )
3732 {
3733 if( j % 8 == 0 )
3734 mbedtls_printf("\n ");
3735 mbedtls_printf("%02x ", eap_tls_iv[j] );
3736 }
3737 mbedtls_printf("\n");
3738 }
3739#endif
Hanno Beckera7d25422019-04-09 17:28:10 +01003740
Hanno Beckera0e20d02019-05-15 14:03:01 +01003741#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003742 ret = report_cid_usage( &ssl, "initial handshake" );
3743 if( ret != 0 )
3744 goto exit;
3745
Hanno Beckera7d25422019-04-09 17:28:10 +01003746 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3747 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003748 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
3749 cid_renego, cid_renego_len ) ) != 0 )
Hanno Beckera7d25422019-04-09 17:28:10 +01003750 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003751 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
3752 ret );
Hanno Beckera7d25422019-04-09 17:28:10 +01003753 goto exit;
3754 }
Hanno Beckera7d25422019-04-09 17:28:10 +01003755 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003756#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +01003757
Piotr Nowicki0937ed22019-11-26 16:32:40 +01003758#if defined(MBEDTLS_MEMORY_DEBUG)
3759 mbedtls_memory_buffer_alloc_cur_get( &current_heap_memory, &heap_blocks );
3760 mbedtls_memory_buffer_alloc_max_get( &peak_heap_memory, &heap_blocks );
3761 mbedtls_printf( "Heap memory usage after handshake: %lu bytes. Peak memory usage was %lu\n",
3762 (unsigned long) current_heap_memory, (unsigned long) peak_heap_memory );
3763#endif /* MBEDTLS_MEMORY_DEBUG */
3764
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02003765 if( opt.exchanges == 0 )
3766 goto close_notify;
3767
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003768 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003769data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003770 /*
3771 * 6. Read the HTTP Request
3772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773 mbedtls_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003774 fflush( stdout );
3775
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003776 /*
3777 * TLS and DTLS need different reading styles (stream vs datagram)
3778 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003779 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003780 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003781 do
3782 {
3783 int terminated = 0;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003784 len = opt.buffer_size - 1;
3785 memset( buf, 0, opt.buffer_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003786 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003787
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003788 if( mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003789 {
3790 if( opt.event == 1 /* level triggered IO */ )
3791 {
3792#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003793 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003794#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003795 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003796#endif
3797 }
3798
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003799 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01003800 }
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003801
3802 if( ret <= 0 )
3803 {
3804 switch( ret )
3805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3807 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003808 goto close_notify;
3809
3810 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811 case MBEDTLS_ERR_NET_CONN_RESET:
3812 mbedtls_printf( " connection was reset by peer\n" );
3813 ret = MBEDTLS_ERR_NET_CONN_RESET;
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003814 goto reset;
3815
3816 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003817 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003818 goto reset;
3819 }
3820 }
3821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822 if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003823 {
3824 len = ret;
3825 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826 mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003827
3828 /* End of message should be detected according to the syntax of the
3829 * application protocol (eg HTTP), just use a dummy test here. */
3830 if( buf[len - 1] == '\n' )
3831 terminated = 1;
3832 }
3833 else
3834 {
3835 int extra_len, ori_len;
3836 unsigned char *larger_buf;
3837
3838 ori_len = ret;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02003839 extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003840
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003841 larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003842 if( larger_buf == NULL )
3843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844 mbedtls_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003845 ret = 1;
3846 goto reset;
3847 }
3848
3849 memset( larger_buf, 0, ori_len + extra_len );
3850 memcpy( larger_buf, buf, ori_len );
3851
3852 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853 ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003854 if( ret != extra_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857 mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003858 ret = 1;
3859 goto reset;
3860 }
3861
3862 larger_buf[ori_len + extra_len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863 mbedtls_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003864 ori_len + extra_len, ori_len, extra_len,
3865 (char *) larger_buf );
3866
3867 /* End of message should be detected according to the syntax of the
3868 * application protocol (eg HTTP), just use a dummy test here. */
3869 if( larger_buf[ori_len + extra_len - 1] == '\n' )
3870 terminated = 1;
3871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 mbedtls_free( larger_buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003873 }
3874
3875 if( terminated )
3876 {
3877 ret = 0;
3878 break;
3879 }
3880 }
3881 while( 1 );
3882 }
3883 else /* Not stream, so datagram */
3884 {
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003885 len = opt.buffer_size - 1;
3886 memset( buf, 0, opt.buffer_size );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003887
Gilles Peskineb44692f2018-04-24 12:18:19 +02003888 do
Hanno Becker16970d22017-10-10 15:56:37 +01003889 {
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003890 /* Without the call to `mbedtls_ssl_check_pending`, it might
3891 * happen that the client sends application data in the same
3892 * datagram as the Finished message concluding the handshake.
3893 * In this case, the application data would be ready to be
3894 * processed while the underlying transport wouldn't signal
3895 * any further incoming data.
3896 *
3897 * See the test 'Event-driven I/O: session-id resume, UDP packing'
3898 * in tests/ssl-opt.sh.
3899 */
3900
3901 /* For event-driven IO, wait for socket to become available */
3902 if( mbedtls_ssl_check_pending( &ssl ) == 0 &&
3903 opt.event == 1 /* level triggered IO */ )
3904 {
3905#if defined(MBEDTLS_TIMING_C)
3906 idle( &client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ );
3907#else
3908 idle( &client_fd, MBEDTLS_ERR_SSL_WANT_READ );
3909#endif
3910 }
3911
Hanno Becker16970d22017-10-10 15:56:37 +01003912 ret = mbedtls_ssl_read( &ssl, buf, len );
3913
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003914 /* Note that even if `mbedtls_ssl_check_pending` returns true,
3915 * it can happen that the subsequent call to `mbedtls_ssl_read`
3916 * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
3917 * might be discarded (e.g. because they are retransmissions). */
Hanno Becker16970d22017-10-10 15:56:37 +01003918 }
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003919 while( mbedtls_status_is_ssl_in_progress( ret ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003920
3921 if( ret <= 0 )
3922 {
3923 switch( ret )
3924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003925 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3926 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003927 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003928 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003929
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003930 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003931 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003932 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003933 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003934 }
3935
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003936 len = ret;
3937 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003939 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003940 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003941
3942 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003943 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02003944 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003946#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00003947 if( opt.renegotiate && exchanges_left == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949 mbedtls_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003950 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003952 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003953 {
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003954 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003957 goto reset;
3958 }
Hanno Becker16970d22017-10-10 15:56:37 +01003959
3960 /* For event-driven IO, wait for socket to become available */
3961 if( opt.event == 1 /* level triggered IO */ )
3962 {
3963#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003964 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003965#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003966 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003967#endif
3968 }
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003969 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003972 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003973#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003974
Hanno Beckera0e20d02019-05-15 14:03:01 +01003975#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003976 ret = report_cid_usage( &ssl, "after renegotiation" );
3977 if( ret != 0 )
3978 goto exit;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003979#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003980
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003981 /*
3982 * 7. Write the 200 Response
3983 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984 mbedtls_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003985 fflush( stdout );
3986
3987 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003988 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003989
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003990 /* Add padding to the response to reach opt.response_size in length */
3991 if( opt.response_size != DFL_RESPONSE_SIZE &&
3992 len < opt.response_size )
3993 {
3994 memset( buf + len, 'B', opt.response_size - len );
3995 len += opt.response_size - len;
3996 }
3997
3998 /* Truncate if response size is smaller than the "natural" size */
3999 if( opt.response_size != DFL_RESPONSE_SIZE &&
4000 len > opt.response_size )
4001 {
4002 len = opt.response_size;
4003
4004 /* Still end with \r\n unless that's really not possible */
4005 if( len >= 2 ) buf[len - 2] = '\r';
4006 if( len >= 1 ) buf[len - 1] = '\n';
4007 }
4008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004010 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004011 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004013 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004014 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02004015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018 mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004019 goto reset;
4020 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004021
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02004022 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004024 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004025 goto reset;
4026 }
Hanno Becker16970d22017-10-10 15:56:37 +01004027
4028 /* For event-driven IO, wait for socket to become available */
4029 if( opt.event == 1 /* level triggered IO */ )
4030 {
4031#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00004032 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01004033#else
Hanno Becker197a91c2017-10-31 10:58:53 +00004034 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01004035#endif
4036 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02004037 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004038 }
4039 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004040 else /* Not stream, so datagram */
4041 {
Hanno Becker16970d22017-10-10 15:56:37 +01004042 while( 1 )
4043 {
4044 ret = mbedtls_ssl_write( &ssl, buf, len );
4045
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02004046 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01004047 break;
4048
4049 /* For event-driven IO, wait for socket to become available */
4050 if( opt.event == 1 /* level triggered IO */ )
4051 {
4052#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00004053 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01004054#else
Hanno Becker197a91c2017-10-31 10:58:53 +00004055 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01004056#endif
4057 }
4058 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004059
4060 if( ret < 0 )
4061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004062 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004063 goto reset;
4064 }
4065
4066 frags = 1;
4067 written = ret;
4068 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004069
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02004070 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071 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 +01004072 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01004073
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02004074 /*
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004075 * 7b. Simulate serialize/deserialize and go back to data exchange
4076 */
Jarno Lamsabbc7b412019-06-04 11:06:31 +03004077#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004078 if( opt.serialize != 0 )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004079 {
Jarno Lamsa15b3a7a2019-06-06 15:10:07 +03004080 size_t buf_len;
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004081
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004082 mbedtls_printf( " . Serializing live connection..." );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004083
Jarno Lamsa15b3a7a2019-06-06 15:10:07 +03004084 ret = mbedtls_ssl_context_save( &ssl, NULL, 0, &buf_len );
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004085 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004086 {
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004087 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
4088 "-0x%x\n\n", -ret );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004089
4090 goto exit;
4091 }
4092
Jarno Lamsa15b3a7a2019-06-06 15:10:07 +03004093 if( ( context_buf = mbedtls_calloc( 1, buf_len ) ) == NULL )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004094 {
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004095 mbedtls_printf( " failed\n ! Couldn't allocate buffer for "
4096 "serialized context" );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004097
4098 goto exit;
4099 }
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02004100 context_buf_len = buf_len;
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004101
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004102 if( ( ret = mbedtls_ssl_context_save( &ssl, context_buf,
4103 buf_len, &buf_len ) ) != 0 )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004104 {
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004105 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004106 "-0x%x\n\n", -ret );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004107
4108 goto exit;
4109 }
4110
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004111 mbedtls_printf( " ok\n" );
4112
4113 /*
4114 * This simulates a workflow where you have a long-lived server
4115 * instance, potentially with a pool of ssl_context objects, and you
4116 * just want to re-use one while the connection is inactive: in that
4117 * case you can just reset() it, and then it's ready to receive
4118 * serialized data from another connection (or the same here).
4119 */
4120 if( opt.serialize == 1 )
4121 {
Manuel Pégourié-Gonnard9df5a822019-07-23 14:51:09 +02004122 /* nothing to do here, done by context_save() already */
4123 mbedtls_printf( " . Context has been reset... ok" );
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004124 }
4125
4126 /*
4127 * This simulates a workflow where you have one server instance per
4128 * connection, and want to release it entire when the connection is
4129 * inactive, and spawn it again when needed again - this would happen
4130 * between ssl_free() and ssl_init() below, together with any other
4131 * teardown/startup code needed - for example, preparing the
4132 * ssl_config again (see section 3 "setup stuff" in this file).
4133 */
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004134 if( opt.serialize == 2 )
4135 {
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004136 mbedtls_printf( " . Freeing and reinitializing context..." );
4137
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004138 mbedtls_ssl_free( &ssl );
4139
4140 mbedtls_ssl_init( &ssl );
4141
4142 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
4143 {
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004144 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned "
4145 "-0x%x\n\n", -ret );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004146 goto exit;
4147 }
4148
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004149 /*
4150 * This illustrates the minimum amount of things you need to set
4151 * up, however you could set up much more if desired, for example
4152 * if you want to share your set up code between the case of
4153 * establishing a new connection and this case.
4154 */
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004155 if( opt.nbio == 2 )
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004156 mbedtls_ssl_set_bio( &ssl, &client_fd, delayed_send,
4157 delayed_recv, NULL );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004158 else
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004159 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send,
4160 mbedtls_net_recv,
4161 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004162
Jarno Lamsa8e253212019-06-13 11:45:06 +03004163#if defined(MBEDTLS_TIMING_C)
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004164 mbedtls_ssl_set_timer_cb( &ssl, &timer,
4165 mbedtls_timing_set_delay,
4166 mbedtls_timing_get_delay );
Jarno Lamsa8e253212019-06-13 11:45:06 +03004167#endif /* MBEDTLS_TIMING_C */
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004168
4169 mbedtls_printf( " ok\n" );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004170 }
4171
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004172 mbedtls_printf( " . Deserializing connection..." );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004173
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004174 if( ( ret = mbedtls_ssl_context_load( &ssl, context_buf,
4175 buf_len ) ) != 0 )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004176 {
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004177 mbedtls_printf( "failed\n ! mbedtls_ssl_context_load returned "
4178 "-0x%x\n\n", -ret );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004179
4180 goto exit;
4181 }
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004182
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02004183 mbedtls_free( context_buf );
4184 context_buf = NULL;
4185 context_buf_len = 0;
4186
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004187 mbedtls_printf( " ok\n" );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004188 }
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004189#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004190
4191 /*
4192 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02004193 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00004194 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02004195 goto data_exchange;
4196
4197 /*
4198 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02004199 */
4200close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004201 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01004202
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02004203 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01004205 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02004206 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02004207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004208 mbedtls_printf( " done\n" );
Rich Evansf90016a2015-01-19 14:26:37 +00004209
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004210 goto reset;
4211
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02004212 /*
4213 * Cleanup and exit
4214 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004215exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004216#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004217 if( ret != 0 )
4218 {
4219 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 mbedtls_strerror( ret, error_buf, 100 );
4221 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004222 }
4223#endif
4224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004225 mbedtls_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01004226 fflush( stdout );
4227
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02004228 mbedtls_net_free( &client_fd );
4229 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +02004230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004231#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
4232 mbedtls_dhm_free( &dhm );
Paul Bakkera317a982014-06-18 16:44:11 +02004233#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234#if defined(MBEDTLS_X509_CRT_PARSE_C)
4235 mbedtls_x509_crt_free( &cacert );
4236 mbedtls_x509_crt_free( &srvcert );
4237 mbedtls_pk_free( &pkey );
4238 mbedtls_x509_crt_free( &srvcert2 );
4239 mbedtls_pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02004240#endif
Gilles Peskine44817442018-06-13 18:09:28 +02004241#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
4242 for( i = 0; (size_t) i < ssl_async_keys.slots_used; i++ )
4243 {
4244 if( ssl_async_keys.slots[i].pk_owned )
4245 {
4246 mbedtls_pk_free( ssl_async_keys.slots[i].pk );
4247 mbedtls_free( ssl_async_keys.slots[i].pk );
4248 ssl_async_keys.slots[i].pk = NULL;
4249 }
4250 }
4251#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02004252#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01004253 sni_free( sni_info );
4254#endif
Gilles Peskineeccd8882020-03-10 12:19:08 +01004255#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004256 if( ( ret = psk_free( psk_info ) ) != 0 )
4257 mbedtls_printf( "Failed to list of opaque PSKs - error was %d\n", ret );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02004258#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
4260 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02004261#endif
Paul Bakkered27a042013-04-18 22:46:23 +02004262
Gilles Peskineeccd8882020-03-10 12:19:08 +01004263#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) && \
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004264 defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00004265 if( opt.psk_opaque != 0 )
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004266 {
4267 /* This is ok even if the slot hasn't been
4268 * initialized (we might have jumed here
4269 * immediately because of bad cmd line params,
4270 * for example). */
Hanno Becker1d911cd2018-11-15 13:06:09 +00004271 status = psa_destroy_key( psk_slot );
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004272 if( status != PSA_SUCCESS )
4273 {
4274 mbedtls_printf( "Failed to destroy key slot %u - error was %d",
Hanno Becker1d911cd2018-11-15 13:06:09 +00004275 (unsigned) psk_slot, (int) status );
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004276 }
4277 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01004278#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED &&
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004279 MBEDTLS_USE_PSA_CRYPTO */
4280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02004282 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004283 mbedtls_ctr_drbg_free( &ctr_drbg );
4284 mbedtls_entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004286#if defined(MBEDTLS_SSL_CACHE_C)
4287 mbedtls_ssl_cache_free( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00004288#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004289#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4290 mbedtls_ssl_ticket_free( &ticket_ctx );
4291#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004292#if defined(MBEDTLS_SSL_COOKIE_C)
4293 mbedtls_ssl_cookie_free( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02004294#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004295
Hanno Becker095d9cf2018-10-09 12:39:13 +01004296 mbedtls_free( buf );
4297
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02004298#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
4299 if( context_buf != NULL )
4300 mbedtls_platform_zeroize( context_buf, context_buf_len );
4301 mbedtls_free( context_buf );
4302#endif
4303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
4305#if defined(MBEDTLS_MEMORY_DEBUG)
4306 mbedtls_memory_buffer_alloc_status();
Paul Bakker82024bf2013-07-04 11:52:32 +02004307#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004308 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +02004309#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02004310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311 mbedtls_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01004312
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004313#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004315 fflush( stdout ); getchar();
4316#endif
4317
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02004318 // Shell can not handle large exit numbers -> 1 for errors
4319 if( ret < 0 )
4320 ret = 1;
4321
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004322 return( ret );
4323}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004324#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
4325 MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02004326 MBEDTLS_CTR_DRBG_C */