blob: f77026297023dab7f82832f1623511eb7f82f135 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100261#define USAGE_PSK_RAW \
Andrzej Kurekb274f272019-02-05 05:06:35 -0500262 " psk=%%s default: \"\" (in hex, without 0x)\n" \
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100263 " psk_list=%%s default: \"\"\n" \
Andrzej Kurekb274f272019-02-05 05:06:35 -0500264 " A list of (PSK identity, PSK value) pairs.\n" \
265 " The PSK values are in hex, without 0x.\n" \
266 " id1,psk1[,id2,psk2[,...]]\n" \
267 " psk_identity=%%s default: \"Client_identity\"\n"
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100268#if defined(MBEDTLS_USE_PSA_CRYPTO)
269#define USAGE_PSK_SLOT \
Hanno Becker1d911cd2018-11-15 13:06:09 +0000270 " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
271 " Enable this to store the PSK configured through command line\n" \
272 " parameter `psk` in a PSA-based key slot.\n" \
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100273 " Note: Currently only supported in conjunction with\n" \
274 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
275 " to force a particular PSK-only ciphersuite.\n" \
276 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
277 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
278 " with prepopulated key slots instead of importing raw key material.\n" \
Hanno Becker1d911cd2018-11-15 13:06:09 +0000279 " psk_list_opaque=%%d default: 0 (don't use opaque dynamic PSKs)\n" \
280 " Enable this to store the list of dynamically chosen PSKs configured\n" \
281 " through the command line parameter `psk_list` in PSA-based key slots.\n" \
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100282 " Note: Currently only supported in conjunction with\n" \
283 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
284 " to force a particular PSK-only ciphersuite.\n" \
285 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
286 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
287 " with prepopulated key slots instead of importing raw key material.\n"
288#else
289#define USAGE_PSK_SLOT ""
290#endif /* MBEDTLS_USE_PSA_CRYPTO */
291#define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
Paul Bakkered27a042013-04-18 22:46:23 +0200292#else
293#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200295#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
296#define USAGE_CA_CALLBACK \
297 " ca_callback=%%d default: 0 (disabled)\n" \
298 " Enable this to use the trusted certificate callback function\n"
299#else
300#define USAGE_CA_CALLBACK ""
301#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200303#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100304 " tickets=%%d default: 1 (enabled)\n" \
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200305 " ticket_timeout=%%d default: 86400 (one day)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200306#else
307#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200309
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300310#if defined(MBEDTLS_SSL_EXPORT_KEYS)
311#define USAGE_EAP_TLS \
312 " eap_tls=%%d default: 0 (disabled)\n"
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100313#define USAGE_NSS_KEYLOG \
314 " nss_keylog=%%d default: 0 (disabled)\n"
315#define USAGE_NSS_KEYLOG_FILE \
316 " nss_keylog_file=%%s\n"
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300317#else
318#define USAGE_EAP_TLS ""
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100319#define USAGE_NSS_KEYLOG ""
320#define USAGE_NSS_KEYLOG_FILE ""
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300321#endif /* MBEDTLS_SSL_EXPORT_KEYS */
322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100324#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100325 " cache_max=%%d default: cache default (50)\n" \
326 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100327#else
328#define USAGE_CACHE ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329#endif /* MBEDTLS_SSL_CACHE_C */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100330
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200331#if defined(SNI_OPTION)
Ron Eldor80d04192019-04-04 15:02:01 +0300332#if defined(MBEDTLS_X509_CRL_PARSE_C)
333#define SNI_CRL ",crl"
334#else
335#define SNI_CRL ""
336#endif
337
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100338#define USAGE_SNI \
Ron Eldor80d04192019-04-04 15:02:01 +0300339 " sni=%%s name1,cert1,key1,ca1"SNI_CRL",auth1[,...]\n" \
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200340 " default: disabled\n"
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100341#else
342#define USAGE_SNI ""
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200343#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200346#define USAGE_MAX_FRAG_LEN \
347 " max_frag_len=%%d default: 16384 (tls default)\n" \
348 " options: 512, 1024, 2048, 4096\n"
349#else
350#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100354#define USAGE_TRUNC_HMAC \
355 " trunc_hmac=%%d default: library default\n"
356#else
357#define USAGE_TRUNC_HMAC ""
358#endif
359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200361#define USAGE_ALPN \
362 " alpn=%%s default: \"\" (disabled)\n" \
363 " example: spdy/1,http/1.1\n"
364#else
365#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200369#define USAGE_COOKIES \
370 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200371 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200372#else
373#define USAGE_COOKIES ""
374#endif
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200377#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200378 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200379#else
380#define USAGE_ANTI_REPLAY ""
381#endif
382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200384#define USAGE_BADMAC_LIMIT \
385 " badmac_limit=%%d default: (library default: disabled)\n"
386#else
387#define USAGE_BADMAC_LIMIT ""
388#endif
389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200391#define USAGE_DTLS \
392 " dtls=%%d default: 0 (TLS)\n" \
393 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200394 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Beckere7675d02018-08-14 13:28:56 +0100395 " mtu=%%d default: (library default: unlimited)\n" \
396 " dgram_packing=%%d default: 1 (allowed)\n" \
397 " allow or forbid packing of multiple\n" \
398 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200399#else
400#define USAGE_DTLS ""
401#endif
402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200404#define USAGE_EMS \
405 " extended_ms=0/1 default: (library default: on)\n"
406#else
407#define USAGE_EMS ""
408#endif
409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100411#define USAGE_ETM \
412 " etm=0/1 default: (library default: on)\n"
413#else
414#define USAGE_ETM ""
415#endif
416
Philippe Antoine738153a2019-06-18 20:16:43 +0200417#define USAGE_REPRODUCIBLE \
418 " reproducible=0/1 default: 0 (disabled)\n"
419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100421#define USAGE_RENEGO \
422 " renegotiation=%%d default: 0 (disabled)\n" \
423 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100424 " renego_delay=%%d default: -2 (library default)\n" \
Andres AG692ad842017-01-19 16:30:57 +0000425 " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100426#else
427#define USAGE_RENEGO ""
428#endif
429
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200430#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
431#define USAGE_ECJPAKE \
432 " ecjpake_pw=%%s default: none (disabled)\n"
433#else
434#define USAGE_ECJPAKE ""
435#endif
436
Hanno Beckere6706e62017-05-15 16:05:15 +0100437#if defined(MBEDTLS_ECP_C)
438#define USAGE_CURVES \
439 " curves=a,b,c,d default: \"default\" (library default)\n" \
440 " example: \"secp521r1,brainpoolP512r1\"\n" \
441 " - use \"none\" for empty list\n" \
442 " - see mbedtls_ecp_curve_list()\n" \
443 " for acceptable curve names\n"
444#else
445#define USAGE_CURVES ""
446#endif
447
Jarno Lamsabbc7b412019-06-04 11:06:31 +0300448#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
449#define USAGE_SERIALIZATION \
Jarno Lamsa304d61c2019-06-06 10:40:52 +0300450 " serialize=%%d default: 0 (do not serialize/deserialize)\n" \
Jarno Lamsa1a7f7932019-06-07 08:39:24 +0300451 " options: 1 (serialize)\n" \
452 " 2 (serialize with re-initialization)\n"
Jarno Lamsabbc7b412019-06-04 11:06:31 +0300453#else
454#define USAGE_SERIALIZATION ""
455#endif
456
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000457#define USAGE \
458 "\n usage: ssl_server2 param=<>...\n" \
459 "\n acceptable parameters:\n" \
Ron Eldor71f68c42017-09-26 11:29:11 +0300460 " server_addr=%%s default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000461 " server_port=%%d default: 4433\n" \
462 " debug_level=%%d default: 0 (disabled)\n" \
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200463 " buffer_size=%%d default: 200 \n" \
464 " (minimum: 1, max: 16385)\n" \
465 " response_size=%%d default: about 152 (basic response)\n" \
466 " (minimum: 0, max: 16384)\n" \
467 " increases buffer_size if bigger\n"\
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100468 " nbio=%%d default: 0 (blocking I/O)\n" \
469 " options: 1 (non-blocking), 2 (added delays)\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100470 " event=%%d default: 0 (loop)\n" \
471 " options: 1 (level-triggered, implies nbio=1),\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200472 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100473 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200474 USAGE_DTLS \
475 USAGE_COOKIES \
476 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200477 USAGE_BADMAC_LIMIT \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200478 "\n" \
Manuel Pégourié-Gonnardee6139c2015-05-07 10:18:26 +0100479 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100480 " options: none, optional, required\n" \
Janos Follath4817e272017-04-10 13:44:33 +0100481 " cert_req_ca_list=%%d default: 1 (send ca list)\n" \
482 " options: 1 (send ca list), 0 (don't send)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000483 USAGE_IO \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100484 USAGE_SSL_ASYNC \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100485 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100486 "\n" \
487 USAGE_PSK \
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200488 USAGE_CA_CALLBACK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200489 USAGE_ECJPAKE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100490 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100491 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100492 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200493 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200494 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100495 USAGE_TICKETS \
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300496 USAGE_EAP_TLS \
Philippe Antoine738153a2019-06-18 20:16:43 +0200497 USAGE_REPRODUCIBLE \
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100498 USAGE_NSS_KEYLOG \
499 USAGE_NSS_KEYLOG_FILE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100500 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100501 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100502 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200503 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200504 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100505 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100506 USAGE_CURVES \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100507 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100508 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200509 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200510 " min_version=%%s default: (library default: tls1)\n" \
511 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200512 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100513 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200514 "\n" \
515 " version_suites=a,b,c,d per-version ciphersuites\n" \
516 " in order from ssl3 to tls1_2\n" \
517 " default: all enabled\n" \
518 " force_ciphersuite=<name> default: all enabled\n" \
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100519 " query_config=<name> return 0 if the specified\n" \
520 " configuration macro is defined and 1\n" \
521 " otherwise. The expansion of the macro\n" \
522 " is printed if it is defined\n" \
Jarno Lamsabbc7b412019-06-04 11:06:31 +0300523 USAGE_SERIALIZATION \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000524 " acceptable ciphersuite names:\n"
525
Hanno Becker8651a432017-06-09 16:13:22 +0100526#define ALPN_LIST_SIZE 10
527#define CURVE_LIST_SIZE 20
528
Andres AG692ad842017-01-19 16:30:57 +0000529#define PUT_UINT64_BE(out_be,in_le,i) \
530{ \
531 (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \
532 (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \
533 (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \
534 (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \
535 (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \
536 (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \
537 (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \
538 (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \
539}
540
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500541
Rich Evans85b05ec2015-02-12 11:37:29 +0000542/*
543 * global options
544 */
545struct options
546{
547 const char *server_addr; /* address on which the ssl service runs */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200548 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000549 int debug_level; /* level of debugging */
550 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100551 int event; /* loop or event-driven IO? level or edge triggered? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200553 int response_size; /* pad response with header to requested size */
554 uint16_t buffer_size; /* IO buffer size */
Rich Evans85b05ec2015-02-12 11:37:29 +0000555 const char *ca_file; /* the file with the CA certificate(s) */
556 const char *ca_path; /* the path with the CA certificate(s) reside */
557 const char *crt_file; /* the file with the server certificate */
558 const char *key_file; /* the file with the server key */
559 const char *crt_file2; /* the file with the 2nd server certificate */
560 const char *key_file2; /* the file with the 2nd server key */
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100561 const char *async_operations; /* supported SSL asynchronous operations */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100562 int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
563 int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
564 int async_private_error; /* inject error in async private callback */
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100565#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000566 int psk_opaque;
567 int psk_list_opaque;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100568#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200569#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000570 int ca_callback; /* Use callback for trusted certificate list */
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200571#endif
Rich Evans85b05ec2015-02-12 11:37:29 +0000572 const char *psk; /* the pre-shared key */
573 const char *psk_identity; /* the pre-shared key identity */
574 char *psk_list; /* list of PSK id/key pairs for callback */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200575 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000576 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
577 const char *version_suites; /* per-version ciphersuites */
578 int renegotiation; /* enable / disable renegotiation */
579 int allow_legacy; /* allow legacy renegotiation */
580 int renegotiate; /* attempt renegotiation? */
581 int renego_delay; /* delay before enforcing renegotiation */
Andres AG692ad842017-01-19 16:30:57 +0000582 uint64_t renego_period; /* period for automatic renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000583 int exchanges; /* number of data exchanges */
584 int min_version; /* minimum protocol version accepted */
585 int max_version; /* maximum protocol version accepted */
586 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200587 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000588 int auth_mode; /* verify mode for connection */
Janos Follath4817e272017-04-10 13:44:33 +0100589 int cert_req_ca_list; /* should we send the CA list? */
Rich Evans85b05ec2015-02-12 11:37:29 +0000590 unsigned char mfl_code; /* code for maximum fragment length */
591 int trunc_hmac; /* accept truncated hmac? */
592 int tickets; /* enable / disable session tickets */
593 int ticket_timeout; /* session ticket lifetime */
594 int cache_max; /* max number of session cache entries */
595 int cache_timeout; /* expiration delay of session cache entries */
596 char *sni; /* string describing sni information */
Hanno Beckere6706e62017-05-15 16:05:15 +0100597 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000598 const char *alpn_string; /* ALPN supported protocols */
599 const char *dhm_file; /* the file with the DH parameters */
600 int extended_ms; /* allow negotiation of extended MS? */
601 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000602 int transport; /* TLS or DTLS? */
603 int cookies; /* Use cookies for DTLS? -1 to break them */
604 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
605 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
606 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200607 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Hanno Beckere7675d02018-08-14 13:28:56 +0100608 int dgram_packing; /* allow/forbid datagram packing */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000609 int badmac_limit; /* Limit of records with bad MAC */
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300610 int eap_tls; /* derive EAP-TLS keying material? */
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100611 int nss_keylog; /* export NSS key log material */
612 const char *nss_keylog_file; /* NSS key log file */
Hanno Beckera7d25422019-04-09 17:28:10 +0100613 int cid_enabled; /* whether to use the CID extension or not */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100614 int cid_enabled_renego; /* whether to use the CID extension or not
615 * during renegotiation */
Hanno Beckera7d25422019-04-09 17:28:10 +0100616 const char *cid_val; /* the CID to use for incoming messages */
Jarno Lamsa9831c8a2019-05-29 13:33:32 +0300617 int serialize; /* serialize/deserialize connection */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100618 const char *cid_val_renego; /* the CID to use for incoming messages
619 * after renegotiation */
Philippe Antoine154feb22019-06-11 17:50:23 +0200620 int reproducible; /* make communication reproducible */
Rich Evans85b05ec2015-02-12 11:37:29 +0000621} opt;
622
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100623int query_config( const char *config );
624
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300625#if defined(MBEDTLS_SSL_EXPORT_KEYS)
626typedef struct eap_tls_keys
627{
628 unsigned char master_secret[48];
629 unsigned char randbytes[64];
Ron Eldor51d3ab52019-05-12 14:54:30 +0300630 mbedtls_tls_prf_types tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300631} eap_tls_keys;
632
633static int eap_tls_key_derivation ( void *p_expkey,
634 const unsigned char *ms,
635 const unsigned char *kb,
636 size_t maclen,
637 size_t keylen,
638 size_t ivlen,
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300639 unsigned char client_random[32],
Ron Eldor51d3ab52019-05-12 14:54:30 +0300640 unsigned char server_random[32],
641 mbedtls_tls_prf_types tls_prf_type )
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300642{
643 eap_tls_keys *keys = (eap_tls_keys *)p_expkey;
644
645 ( ( void ) kb );
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300646 memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) );
647 memcpy( keys->randbytes, client_random, 32 );
648 memcpy( keys->randbytes + 32, server_random, 32 );
Ron Eldor51d3ab52019-05-12 14:54:30 +0300649 keys->tls_prf_type = tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300650
Ron Eldorf75e2522019-05-14 20:38:49 +0300651 if( opt.debug_level > 2 )
652 {
Ron Eldor801faf02019-05-15 17:45:24 +0300653 mbedtls_printf("exported maclen is %u\n", (unsigned)maclen);
654 mbedtls_printf("exported keylen is %u\n", (unsigned)keylen);
655 mbedtls_printf("exported ivlen is %u\n", (unsigned)ivlen);
Ron Eldorf75e2522019-05-14 20:38:49 +0300656 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300657 return( 0 );
658}
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100659
660static int nss_keylog_export( void *p_expkey,
661 const unsigned char *ms,
662 const unsigned char *kb,
663 size_t maclen,
664 size_t keylen,
665 size_t ivlen,
666 unsigned char client_random[32],
667 unsigned char server_random[32],
668 mbedtls_tls_prf_types tls_prf_type )
669{
670 char nss_keylog_line[ 200 ];
671 size_t const client_random_len = 32;
672 size_t const master_secret_len = 48;
673 size_t len = 0;
674 size_t j;
675 int ret = 0;
676
677 ((void) p_expkey);
678 ((void) kb);
679 ((void) maclen);
680 ((void) keylen);
681 ((void) ivlen);
682 ((void) server_random);
683 ((void) tls_prf_type);
684
685 len += sprintf( nss_keylog_line + len,
686 "%s", "CLIENT_RANDOM " );
687
688 for( j = 0; j < client_random_len; j++ )
689 {
690 len += sprintf( nss_keylog_line + len,
691 "%02x", client_random[j] );
692 }
693
694 len += sprintf( nss_keylog_line + len, " " );
695
696 for( j = 0; j < master_secret_len; j++ )
697 {
698 len += sprintf( nss_keylog_line + len,
699 "%02x", ms[j] );
700 }
701
702 len += sprintf( nss_keylog_line + len, "\n" );
703 nss_keylog_line[ len ] = '\0';
704
705 mbedtls_printf( "\n" );
706 mbedtls_printf( "---------------- NSS KEYLOG -----------------\n" );
707 mbedtls_printf( "%s", nss_keylog_line );
708 mbedtls_printf( "---------------------------------------------\n" );
709
710 if( opt.nss_keylog_file != NULL )
711 {
712 FILE *f;
713
714 if( ( f = fopen( opt.nss_keylog_file, "a" ) ) == NULL )
715 {
716 ret = -1;
717 goto exit;
718 }
719
720 if( fwrite( nss_keylog_line, 1, len, f ) != len )
721 {
722 ret = -1;
723 goto exit;
724 }
725
726 fclose( f );
727 }
728
729exit:
730 mbedtls_platform_zeroize( nss_keylog_line,
731 sizeof( nss_keylog_line ) );
732 return( ret );
733}
734
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300735#endif
736
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200737static void my_debug( void *ctx, int level,
738 const char *file, int line,
739 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000740{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200741 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000742
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200743 /* Extract basename from file */
744 for( p = basename = file; *p != '\0'; p++ )
745 if( *p == '/' || *p == '\\' )
746 basename = p + 1;
747
748 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000749 fflush( (FILE *) ctx );
750}
751
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200752mbedtls_time_t dummy_constant_time( mbedtls_time_t* time )
753{
754 (void) time;
755 return 0x5af2a056;
756}
757
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200758int dummy_entropy( void *data, unsigned char *output, size_t len )
759{
760 size_t i;
Philippe Antoine12e85de2019-06-11 16:07:53 +0200761 int ret;
Philippe Antoined2235f22019-06-11 16:29:28 +0200762 (void) data;
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200763
Philippe Antoine3ca50852019-06-07 22:31:59 +0200764 ret = mbedtls_entropy_func( data, output, len );
Philippe Antoine986b6f22019-06-07 15:04:32 +0200765 for (i = 0; i < len; i++ ) {
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200766 //replace result with pseudo random
767 output[i] = (unsigned char) rand();
768 }
Philippe Antoine3ca50852019-06-07 22:31:59 +0200769 return( ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200770}
771
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200772#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000773int ca_callback( void *data, mbedtls_x509_crt const *child,
774 mbedtls_x509_crt **candidates)
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200775{
Hanno Beckercbb59032019-03-28 14:14:22 +0000776 int ret = 0;
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200777 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +0000778 mbedtls_x509_crt *first;
779
780 /* This is a test-only implementation of the CA callback
781 * which always returns the entire list of trusted certificates.
782 * Production implementations managing a large number of CAs
783 * should use an efficient presentation and lookup for the
784 * set of trusted certificates (such as a hashtable) and only
785 * return those trusted certificates which satisfy basic
786 * parental checks, such as the matching of child `Issuer`
787 * and parent `Subject` field. */
788 ((void) child);
789
790 first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
791 if( first == NULL )
792 {
793 ret = -1;
794 goto exit;
795 }
796 mbedtls_x509_crt_init( first );
797
798 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
799 {
800 ret = -1;
801 goto exit;
802 }
803
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200804 while( ca->next != NULL )
805 {
806 ca = ca->next;
Hanno Beckercbb59032019-03-28 14:14:22 +0000807 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
808 {
809 ret = -1;
810 goto exit;
811 }
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200812 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000813
814exit:
815
816 if( ret != 0 )
817 {
818 mbedtls_x509_crt_free( first );
819 mbedtls_free( first );
820 first = NULL;
821 }
822
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200823 *candidates = first;
Hanno Beckercbb59032019-03-28 14:14:22 +0000824 return( ret );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200825}
826#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
827
Rich Evans85b05ec2015-02-12 11:37:29 +0000828/*
829 * Test recv/send functions that make sure each try returns
830 * WANT_READ/WANT_WRITE at least once before sucesseding
831 */
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100832static int delayed_recv( void *ctx, unsigned char *buf, size_t len )
Rich Evans85b05ec2015-02-12 11:37:29 +0000833{
834 static int first_try = 1;
835 int ret;
836
837 if( first_try )
838 {
839 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100840 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000841 }
842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100844 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000845 first_try = 1; /* Next call will be a new operation */
846 return( ret );
847}
848
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100849static int delayed_send( void *ctx, const unsigned char *buf, size_t len )
Rich Evans85b05ec2015-02-12 11:37:29 +0000850{
851 static int first_try = 1;
852 int ret;
853
854 if( first_try )
855 {
856 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100857 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000858 }
859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100861 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000862 first_try = 1; /* Next call will be a new operation */
863 return( ret );
864}
865
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100866typedef struct
867{
868 mbedtls_ssl_context *ssl;
869 mbedtls_net_context *net;
870} io_ctx_t;
871
Hanno Becker4b6649e2019-07-03 17:14:41 +0100872#if defined(MBEDTLS_SSL_RECORD_CHECKING)
873static int ssl_check_record( mbedtls_ssl_context const *ssl,
874 unsigned char const *buf, size_t len )
875{
876 int ret;
877 unsigned char *tmp_buf;
878
879 /* Record checking may modify the input buffer,
880 * so make a copy. */
881 tmp_buf = mbedtls_calloc( 1, len );
882 if( tmp_buf == NULL )
883 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
884 memcpy( tmp_buf, buf, len );
885
886 ret = mbedtls_ssl_check_record( ssl, tmp_buf, len );
887 if( ret != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE )
888 {
889 int ret_repeated;
890
891 /* Test-only: Make sure that mbedtls_ssl_check_record()
892 * doesn't alter state. */
893 memcpy( tmp_buf, buf, len ); /* Restore buffer */
894 ret_repeated = mbedtls_ssl_check_record( ssl, tmp_buf, len );
895 if( ret != ret_repeated )
896 {
Hanno Becker91f83272019-07-24 13:59:07 +0100897 mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" );
898 return( -1 );
Hanno Becker4b6649e2019-07-03 17:14:41 +0100899 }
900
901 switch( ret )
902 {
903 case 0:
904 break;
905
906 case MBEDTLS_ERR_SSL_INVALID_RECORD:
907 if( opt.debug_level > 1 )
908 mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" );
909 break;
910
911 case MBEDTLS_ERR_SSL_INVALID_MAC:
912 if( opt.debug_level > 1 )
913 mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" );
914 break;
915
916 case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD:
917 if( opt.debug_level > 1 )
918 mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" );
919 break;
920
921 default:
922 mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", -ret );
923 return( -1 );
924 }
925
926 /* Regardless of the outcome, forward the record to the stack. */
927 }
928
Hanno Becker4b6649e2019-07-03 17:14:41 +0100929 mbedtls_free( tmp_buf );
930
931 return( 0 );
932}
933#endif /* MBEDTLS_SSL_RECORD_CHECKING */
934
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100935static int recv_cb( void *ctx, unsigned char *buf, size_t len )
936{
937 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
938 size_t recv_len;
939 int ret;
940
941 if( opt.nbio == 2 )
942 ret = delayed_recv( io_ctx->net, buf, len );
943 else
944 ret = mbedtls_net_recv( io_ctx->net, buf, len );
945 if( ret < 0 )
946 return( ret );
947 recv_len = (size_t) ret;
948
949 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
950 {
951 /* Here's the place to do any datagram/record checking
952 * in between receiving the packet from the underlying
953 * transport and passing it on to the TLS stack. */
Hanno Becker4b6649e2019-07-03 17:14:41 +0100954#if defined(MBEDTLS_SSL_RECORD_CHECKING)
955 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
956 return( -1 );
957#endif /* MBEDTLS_SSL_RECORD_CHECKING */
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100958 }
959
960 return( (int) recv_len );
961}
962
963static int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len,
964 uint32_t timeout )
965{
966 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
967 int ret;
968 size_t recv_len;
969
970 ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout );
971 if( ret < 0 )
972 return( ret );
973 recv_len = (size_t) ret;
974
975 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
976 {
977 /* Here's the place to do any datagram/record checking
978 * in between receiving the packet from the underlying
979 * transport and passing it on to the TLS stack. */
Hanno Becker4b6649e2019-07-03 17:14:41 +0100980#if defined(MBEDTLS_SSL_RECORD_CHECKING)
981 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
982 return( -1 );
983#endif /* MBEDTLS_SSL_RECORD_CHECKING */
Hanno Beckerdcc94e62019-07-03 17:05:43 +0100984 }
985
986 return( (int) recv_len );
987}
988
989static int send_cb( void *ctx, unsigned char const *buf, size_t len )
990{
991 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
992
993 if( opt.nbio == 2 )
994 return( delayed_send( io_ctx->net, buf, len ) );
995
996 return( mbedtls_net_send( io_ctx->net, buf, len ) );
997}
998
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200999/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001000 * Return authmode from string, or -1 on error
1001 */
1002static int get_auth_mode( const char *s )
1003{
1004 if( strcmp( s, "none" ) == 0 )
1005 return( MBEDTLS_SSL_VERIFY_NONE );
1006 if( strcmp( s, "optional" ) == 0 )
1007 return( MBEDTLS_SSL_VERIFY_OPTIONAL );
1008 if( strcmp( s, "required" ) == 0 )
1009 return( MBEDTLS_SSL_VERIFY_REQUIRED );
1010
1011 return( -1 );
1012}
1013
1014/*
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +02001015 * Used by sni_parse and psk_parse to handle coma-separated lists
1016 */
1017#define GET_ITEM( dst ) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001018 do \
1019 { \
1020 (dst) = p; \
1021 while( *p != ',' ) \
1022 if( ++p > end ) \
1023 goto error; \
1024 *p++ = '\0'; \
1025 } while( 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +02001026
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001027#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001028typedef struct _sni_entry sni_entry;
1029
1030struct _sni_entry {
1031 const char *name;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 mbedtls_x509_crt *cert;
1033 mbedtls_pk_context *key;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001034 mbedtls_x509_crt* ca;
1035 mbedtls_x509_crl* crl;
1036 int authmode;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001037 sni_entry *next;
1038};
1039
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001040void sni_free( sni_entry *head )
1041{
1042 sni_entry *cur = head, *next;
1043
1044 while( cur != NULL )
1045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_x509_crt_free( cur->cert );
1047 mbedtls_free( cur->cert );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 mbedtls_pk_free( cur->key );
1050 mbedtls_free( cur->key );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001051
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001052 mbedtls_x509_crt_free( cur->ca );
1053 mbedtls_free( cur->ca );
Ron Eldor80d04192019-04-04 15:02:01 +03001054#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001055 mbedtls_x509_crl_free( cur->crl );
1056 mbedtls_free( cur->crl );
Ron Eldor80d04192019-04-04 15:02:01 +03001057#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001058 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 mbedtls_free( cur );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001060 cur = next;
1061 }
1062}
1063
1064/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001065 * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
1066 * into a usable sni_entry list. For ca1, crl1, auth1, the special value
1067 * '-' means unset. If ca1 is unset, then crl1 is ignored too.
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001068 *
1069 * Modifies the input string! This is not production quality!
1070 */
1071sni_entry *sni_parse( char *sni_string )
1072{
1073 sni_entry *cur = NULL, *new = NULL;
1074 char *p = sni_string;
1075 char *end = p;
Ron Eldor80d04192019-04-04 15:02:01 +03001076 char *crt_file, *key_file, *ca_file, *auth_str;
1077#if defined(MBEDTLS_X509_CRL_PARSE_C)
1078 char *crl_file;
1079#endif
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001080
1081 while( *end != '\0' )
1082 ++end;
1083 *end = ',';
1084
1085 while( p <= end )
1086 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001087 if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001088 {
1089 sni_free( cur );
1090 return( NULL );
1091 }
1092
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001093 GET_ITEM( new->name );
1094 GET_ITEM( crt_file );
1095 GET_ITEM( key_file );
1096 GET_ITEM( ca_file );
Ron Eldor80d04192019-04-04 15:02:01 +03001097#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001098 GET_ITEM( crl_file );
Ron Eldor80d04192019-04-04 15:02:01 +03001099#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001100 GET_ITEM( auth_str );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001101
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001102 if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
1103 ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001104 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 mbedtls_x509_crt_init( new->cert );
1107 mbedtls_pk_init( new->key );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
1110 mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001111 goto error;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001112
1113 if( strcmp( ca_file, "-" ) != 0 )
1114 {
1115 if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
1116 goto error;
1117
1118 mbedtls_x509_crt_init( new->ca );
1119
1120 if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
1121 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001122 }
1123
Ron Eldor80d04192019-04-04 15:02:01 +03001124#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001125 if( strcmp( crl_file, "-" ) != 0 )
1126 {
1127 if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
1128 goto error;
1129
1130 mbedtls_x509_crl_init( new->crl );
1131
1132 if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
1133 goto error;
1134 }
Ron Eldor80d04192019-04-04 15:02:01 +03001135#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001136
1137 if( strcmp( auth_str, "-" ) != 0 )
1138 {
1139 if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
1140 goto error;
1141 }
1142 else
1143 new->authmode = DFL_AUTH_MODE;
1144
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001145 new->next = cur;
1146 cur = new;
1147 }
1148
1149 return( cur );
1150
1151error:
1152 sni_free( new );
1153 sni_free( cur );
1154 return( NULL );
1155}
1156
1157/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001158 * SNI callback.
1159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001161 const unsigned char *name, size_t name_len )
1162{
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001163 const sni_entry *cur = (const sni_entry *) p_info;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001164
1165 while( cur != NULL )
1166 {
1167 if( name_len == strlen( cur->name ) &&
1168 memcmp( name, cur->name, name_len ) == 0 )
1169 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001170 if( cur->ca != NULL )
1171 mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
1172
1173 if( cur->authmode != DFL_AUTH_MODE )
1174 mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
1175
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001176 return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001177 }
1178
1179 cur = cur->next;
1180 }
1181
1182 return( -1 );
1183}
1184
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001185#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001186
Hanno Becker554b6ea2019-04-30 14:18:06 +01001187#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) || \
Hanno Beckera0e20d02019-05-15 14:03:01 +01001188 defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001189
Hanno Becker1eeca412018-10-15 12:01:35 +01001190#define HEX2NUM( c ) \
1191 do \
1192 { \
1193 if( (c) >= '0' && (c) <= '9' ) \
1194 (c) -= '0'; \
1195 else if( (c) >= 'a' && (c) <= 'f' ) \
1196 (c) -= 'a' - 10; \
1197 else if( (c) >= 'A' && (c) <= 'F' ) \
1198 (c) -= 'A' - 10; \
1199 else \
1200 return( -1 ); \
1201 } while( 0 )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001202
1203/*
1204 * Convert a hex string to bytes.
1205 * Return 0 on success, -1 on error.
1206 */
1207int unhexify( unsigned char *output, const char *input, size_t *olen )
1208{
1209 unsigned char c;
1210 size_t j;
1211
1212 *olen = strlen( input );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 if( *olen % 2 != 0 || *olen / 2 > MBEDTLS_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001214 return( -1 );
1215 *olen /= 2;
1216
1217 for( j = 0; j < *olen * 2; j += 2 )
1218 {
1219 c = input[j];
1220 HEX2NUM( c );
1221 output[ j / 2 ] = c << 4;
1222
1223 c = input[j + 1];
1224 HEX2NUM( c );
1225 output[ j / 2 ] |= c;
1226 }
1227
1228 return( 0 );
1229}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001230
Hanno Becker554b6ea2019-04-30 14:18:06 +01001231#endif
1232
1233#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1234
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001235typedef struct _psk_entry psk_entry;
1236
1237struct _psk_entry
1238{
1239 const char *name;
1240 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 unsigned char key[MBEDTLS_PSK_MAX_LEN];
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001242#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001243 psa_key_handle_t slot;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001244#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001245 psk_entry *next;
1246};
1247
1248/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001249 * Free a list of psk_entry's
1250 */
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001251int psk_free( psk_entry *head )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001252{
1253 psk_entry *next;
1254
1255 while( head != NULL )
1256 {
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001257#if defined(MBEDTLS_USE_PSA_CRYPTO)
1258 psa_status_t status;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001259 psa_key_handle_t const slot = head->slot;
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001260
1261 if( slot != 0 )
1262 {
1263 status = psa_destroy_key( slot );
1264 if( status != PSA_SUCCESS )
1265 return( status );
1266 }
1267#endif /* MBEDTLS_USE_PSA_CRYPTO */
1268
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001269 next = head->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 mbedtls_free( head );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001271 head = next;
1272 }
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001273
1274 return( 0 );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001275}
1276
1277/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001278 * Parse a string of pairs name1,key1[,name2,key2[,...]]
1279 * into a usable psk_entry list.
1280 *
1281 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001282 */
1283psk_entry *psk_parse( char *psk_string )
1284{
1285 psk_entry *cur = NULL, *new = NULL;
1286 char *p = psk_string;
1287 char *end = p;
1288 char *key_hex;
1289
1290 while( *end != '\0' )
1291 ++end;
1292 *end = ',';
1293
1294 while( p <= end )
1295 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001296 if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +00001297 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001298
1299 memset( new, 0, sizeof( psk_entry ) );
1300
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +02001301 GET_ITEM( new->name );
1302 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001303
1304 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001305 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001306
1307 new->next = cur;
1308 cur = new;
1309 }
1310
1311 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001312
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001313error:
1314 psk_free( new );
1315 psk_free( cur );
1316 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001317}
1318
1319/*
1320 * PSK callback
1321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001323 const unsigned char *name, size_t name_len )
1324{
1325 psk_entry *cur = (psk_entry *) p_info;
1326
1327 while( cur != NULL )
1328 {
1329 if( name_len == strlen( cur->name ) &&
1330 memcmp( name, cur->name, name_len ) == 0 )
1331 {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001332#if defined(MBEDTLS_USE_PSA_CRYPTO)
1333 if( cur->slot != 0 )
1334 return( mbedtls_ssl_set_hs_psk_opaque( ssl, cur->slot ) );
1335 else
1336#endif
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001337 return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001338 }
1339
1340 cur = cur->next;
1341 }
1342
1343 return( -1 );
1344}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001346
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001347static mbedtls_net_context listen_fd, client_fd;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +02001348
1349/* Interruption handler to ensure clean exit (for valgrind testing) */
1350#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001351static int received_sigterm = 0;
1352void term_handler( int sig )
1353{
1354 ((void) sig);
1355 received_sigterm = 1;
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001356 mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
1357 mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001358}
Paul Bakkerc1283d32014-08-18 11:05:51 +02001359#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001360
Gilles Peskine682df092017-05-11 19:01:11 +02001361#if defined(MBEDTLS_X509_CRT_PARSE_C)
1362static int ssl_sig_hashes_for_test[] = {
1363#if defined(MBEDTLS_SHA512_C)
1364 MBEDTLS_MD_SHA512,
1365 MBEDTLS_MD_SHA384,
1366#endif
1367#if defined(MBEDTLS_SHA256_C)
1368 MBEDTLS_MD_SHA256,
1369 MBEDTLS_MD_SHA224,
1370#endif
1371#if defined(MBEDTLS_SHA1_C)
1372 /* Allow SHA-1 as we use it extensively in tests. */
1373 MBEDTLS_MD_SHA1,
1374#endif
1375 MBEDTLS_MD_NONE
1376};
1377#endif /* MBEDTLS_X509_CRT_PARSE_C */
1378
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02001379/** Return true if \p ret is a status code indicating that there is an
1380 * operation in progress on an SSL connection, and false if it indicates
1381 * success or a fatal error.
1382 *
1383 * The possible operations in progress are:
1384 *
1385 * - A read, when the SSL input buffer does not contain a full message.
1386 * - A write, when the SSL output buffer contains some data that has not
1387 * been sent over the network yet.
1388 * - An asynchronous callback that has not completed yet. */
1389static int mbedtls_status_is_ssl_in_progress( int ret )
1390{
1391 return( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1392 ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
1393 ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1394}
1395
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001396#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001397typedef struct
1398{
Gilles Peskine44817442018-06-13 18:09:28 +02001399 mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
1400 mbedtls_pk_context *pk; /*!< Private key */
1401 unsigned delay; /*!< Number of resume steps to go through */
1402 unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001403} ssl_async_key_slot_t;
1404
1405typedef enum {
Gilles Peskinead28bf02018-04-26 00:19:16 +02001406 SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
1407 SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
1408 SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start */
1409 SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
Gilles Peskinec9125722018-04-26 07:15:40 +02001410#define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001411} ssl_async_inject_error_t;
1412
1413typedef struct
1414{
Gilles Peskinee2479892018-06-13 18:06:51 +02001415 ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001416 size_t slots_used;
1417 ssl_async_inject_error_t inject_error;
1418 int (*f_rng)(void *, unsigned char *, size_t);
1419 void *p_rng;
1420} ssl_async_key_context_t;
1421
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001422int ssl_async_set_key( ssl_async_key_context_t *ctx,
Gilles Peskine44817442018-06-13 18:09:28 +02001423 mbedtls_x509_crt *cert,
1424 mbedtls_pk_context *pk,
1425 int pk_take_ownership,
1426 unsigned delay )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001427{
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001428 if( ctx->slots_used >= sizeof( ctx->slots ) / sizeof( *ctx->slots ) )
1429 return( -1 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001430 ctx->slots[ctx->slots_used].cert = cert;
1431 ctx->slots[ctx->slots_used].pk = pk;
1432 ctx->slots[ctx->slots_used].delay = delay;
Gilles Peskine44817442018-06-13 18:09:28 +02001433 ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001434 ++ctx->slots_used;
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001435 return( 0 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001436}
1437
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001438#define SSL_ASYNC_INPUT_MAX_SIZE 512
Gilles Peskined3268832018-04-26 06:23:59 +02001439
1440typedef enum
1441{
1442 ASYNC_OP_SIGN,
1443 ASYNC_OP_DECRYPT,
1444} ssl_async_operation_type_t;
1445/* Note that the enum above and the array below need to be kept in sync!
1446 * `ssl_async_operation_names[op]` is the name of op for each value `op`
1447 * of type `ssl_async_operation_type_t`. */
1448static const char *const ssl_async_operation_names[] =
1449{
1450 "sign",
1451 "decrypt",
1452};
1453
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001454typedef struct
1455{
Gilles Peskine6331d782018-04-26 13:27:43 +02001456 unsigned slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001457 ssl_async_operation_type_t operation_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001458 mbedtls_md_type_t md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001459 unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
1460 size_t input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001461 unsigned remaining_delay;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001462} ssl_async_operation_context_t;
1463
Gilles Peskine8f97af72018-04-26 11:46:10 +02001464static int ssl_async_start( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001465 mbedtls_x509_crt *cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001466 ssl_async_operation_type_t op_type,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001467 mbedtls_md_type_t md_alg,
1468 const unsigned char *input,
1469 size_t input_len )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001470{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001471 ssl_async_key_context_t *config_data =
1472 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskine6331d782018-04-26 13:27:43 +02001473 unsigned slot;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001474 ssl_async_operation_context_t *ctx = NULL;
Gilles Peskined3268832018-04-26 06:23:59 +02001475 const char *op_name = ssl_async_operation_names[op_type];
Gilles Peskinef1127252018-04-24 13:05:39 +02001476
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001477 {
1478 char dn[100];
Gilles Peskined5d983e2018-06-15 14:05:10 +02001479 if( mbedtls_x509_dn_gets( dn, sizeof( dn ), &cert->subject ) > 0 )
1480 mbedtls_printf( "Async %s callback: looking for DN=%s\n",
1481 op_name, dn );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001482 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001483
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001484 /* Look for a private key that matches the public key in cert.
1485 * Since this test code has the private key inside Mbed TLS,
1486 * we call mbedtls_pk_check_pair to match a private key with the
1487 * public key. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001488 for( slot = 0; slot < config_data->slots_used; slot++ )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001489 {
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001490 if( mbedtls_pk_check_pair( &cert->pk,
1491 config_data->slots[slot].pk ) == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001492 break;
1493 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001494 if( slot == config_data->slots_used )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001495 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001496 mbedtls_printf( "Async %s callback: no key matches this certificate.\n",
1497 op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001498 return( MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH );
1499 }
Gilles Peskine6331d782018-04-26 13:27:43 +02001500 mbedtls_printf( "Async %s callback: using key slot %u, delay=%u.\n",
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001501 op_name, slot, config_data->slots[slot].delay );
Gilles Peskinef1127252018-04-24 13:05:39 +02001502
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001503 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001504 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001505 mbedtls_printf( "Async %s callback: injected error\n", op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001506 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1507 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001508
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001509 if( input_len > SSL_ASYNC_INPUT_MAX_SIZE )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Gilles Peskinef1127252018-04-24 13:05:39 +02001511
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001512 ctx = mbedtls_calloc( 1, sizeof( *ctx ) );
1513 if( ctx == NULL )
1514 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1515 ctx->slot = slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001516 ctx->operation_type = op_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001517 ctx->md_alg = md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001518 memcpy( ctx->input, input, input_len );
1519 ctx->input_len = input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001520 ctx->remaining_delay = config_data->slots[slot].delay;
Gilles Peskinea668c602018-04-30 11:54:39 +02001521 mbedtls_ssl_set_async_operation_data( ssl, ctx );
Gilles Peskinef1127252018-04-24 13:05:39 +02001522
Gilles Peskineceb541b2018-04-26 06:30:45 +02001523 if( ctx->remaining_delay == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001524 return( 0 );
1525 else
1526 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1527}
1528
Gilles Peskine8f97af72018-04-26 11:46:10 +02001529static int ssl_async_sign( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001530 mbedtls_x509_crt *cert,
1531 mbedtls_md_type_t md_alg,
1532 const unsigned char *hash,
1533 size_t hash_len )
1534{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001535 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001536 ASYNC_OP_SIGN, md_alg,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001537 hash, hash_len ) );
1538}
1539
Gilles Peskine8f97af72018-04-26 11:46:10 +02001540static int ssl_async_decrypt( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001541 mbedtls_x509_crt *cert,
1542 const unsigned char *input,
1543 size_t input_len )
1544{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001545 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001546 ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001547 input, input_len ) );
1548}
1549
Gilles Peskine8f97af72018-04-26 11:46:10 +02001550static int ssl_async_resume( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001551 unsigned char *output,
1552 size_t *output_len,
1553 size_t output_size )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001554{
Gilles Peskinea668c602018-04-30 11:54:39 +02001555 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine8f97af72018-04-26 11:46:10 +02001556 ssl_async_key_context_t *config_data =
1557 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001558 ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001559 int ret;
Gilles Peskinef5a99962018-04-30 16:37:23 +02001560 const char *op_name;
Gilles Peskinef1127252018-04-24 13:05:39 +02001561
Gilles Peskineceb541b2018-04-26 06:30:45 +02001562 if( ctx->remaining_delay > 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001563 {
Gilles Peskineceb541b2018-04-26 06:30:45 +02001564 --ctx->remaining_delay;
Gilles Peskine6331d782018-04-26 13:27:43 +02001565 mbedtls_printf( "Async resume (slot %u): call %u more times.\n",
Gilles Peskineceb541b2018-04-26 06:30:45 +02001566 ctx->slot, ctx->remaining_delay );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001567 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1568 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001569
Gilles Peskined3268832018-04-26 06:23:59 +02001570 switch( ctx->operation_type )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001571 {
Gilles Peskined3268832018-04-26 06:23:59 +02001572 case ASYNC_OP_DECRYPT:
Gilles Peskined3268832018-04-26 06:23:59 +02001573 ret = mbedtls_pk_decrypt( key_slot->pk,
1574 ctx->input, ctx->input_len,
1575 output, output_len, output_size,
1576 config_data->f_rng, config_data->p_rng );
1577 break;
1578 case ASYNC_OP_SIGN:
Gilles Peskined3268832018-04-26 06:23:59 +02001579 ret = mbedtls_pk_sign( key_slot->pk,
1580 ctx->md_alg,
1581 ctx->input, ctx->input_len,
1582 output, output_len,
1583 config_data->f_rng, config_data->p_rng );
1584 break;
1585 default:
Gilles Peskine6331d782018-04-26 13:27:43 +02001586 mbedtls_printf( "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
Gilles Peskined3268832018-04-26 06:23:59 +02001587 ctx->slot, (long) ctx->operation_type );
Gilles Peskine44817442018-06-13 18:09:28 +02001588 mbedtls_free( ctx );
Gilles Peskined3268832018-04-26 06:23:59 +02001589 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1590 break;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001591 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001592
Gilles Peskinef5a99962018-04-30 16:37:23 +02001593 op_name = ssl_async_operation_names[ctx->operation_type];
1594
Gilles Peskinec9125722018-04-26 07:15:40 +02001595 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001596 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001597 mbedtls_printf( "Async resume callback: %s done but injected error\n",
1598 op_name );
Gilles Peskine2636fad2018-06-12 14:17:39 +02001599 mbedtls_free( ctx );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001600 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1601 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001602
Gilles Peskine6331d782018-04-26 13:27:43 +02001603 mbedtls_printf( "Async resume (slot %u): %s done, status=%d.\n",
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001604 ctx->slot, op_name, ret );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001605 mbedtls_free( ctx );
1606 return( ret );
1607}
1608
Gilles Peskine8f97af72018-04-26 11:46:10 +02001609static void ssl_async_cancel( mbedtls_ssl_context *ssl )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001610{
Gilles Peskinea668c602018-04-30 11:54:39 +02001611 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001612 mbedtls_printf( "Async cancel callback.\n" );
1613 mbedtls_free( ctx );
1614}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001615#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001616
Hanno Becker16970d22017-10-10 15:56:37 +01001617/*
1618 * Wait for an event from the underlying transport or the timer
1619 * (Used in event-driven IO mode).
1620 */
1621#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001622int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001623 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001624#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001625int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001626 mbedtls_timing_delay_context *timer,
1627 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001628#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001629{
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001630 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +01001631 int poll_type = 0;
1632
1633 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
1634 poll_type = MBEDTLS_NET_POLL_WRITE;
1635 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
1636 poll_type = MBEDTLS_NET_POLL_READ;
1637#if !defined(MBEDTLS_TIMING_C)
1638 else
Hanno Beckeref527962018-03-15 15:49:24 +00001639 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001640#endif
1641
1642 while( 1 )
1643 {
Hanno Becker197a91c2017-10-31 10:58:53 +00001644 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +01001645#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001646 if( timer != NULL &&
1647 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +01001648 {
Hanno Becker16970d22017-10-10 15:56:37 +01001649 break;
1650 }
Hanno Becker197a91c2017-10-31 10:58:53 +00001651#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +01001652
Hanno Becker197a91c2017-10-31 10:58:53 +00001653 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001654 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +01001655 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001656 ret = mbedtls_net_poll( fd, poll_type, 0 );
1657 if( ret < 0 )
1658 return( ret );
1659 if( ret == poll_type )
1660 break;
Hanno Becker16970d22017-10-10 15:56:37 +01001661 }
1662 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001663
1664 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001665}
1666
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001667#if defined(MBEDTLS_USE_PSA_CRYPTO)
Janos Follathbe4efc22019-08-08 11:38:18 +01001668static psa_status_t psa_setup_psk_key_slot( psa_key_handle_t *slot,
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001669 psa_algorithm_t alg,
1670 unsigned char *psk,
1671 size_t psk_len )
1672{
1673 psa_status_t status;
Janos Follathbe4efc22019-08-08 11:38:18 +01001674 psa_key_attributes_t key_attributes;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001675
Janos Follathbe4efc22019-08-08 11:38:18 +01001676 key_attributes = psa_key_attributes_init();
1677 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1678 psa_set_key_algorithm( &key_attributes, alg );
1679 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001680
Janos Follathbe4efc22019-08-08 11:38:18 +01001681 status = psa_import_key( &key_attributes, psk, psk_len, slot );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001682 if( status != PSA_SUCCESS )
Hanno Becker1d911cd2018-11-15 13:06:09 +00001683 {
1684 fprintf( stderr, "IMPORT\n" );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001685 return( status );
Hanno Becker1d911cd2018-11-15 13:06:09 +00001686 }
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001687
1688 return( PSA_SUCCESS );
1689}
1690#endif /* MBEDTLS_USE_PSA_CRYPTO */
1691
Hanno Beckera0e20d02019-05-15 14:03:01 +01001692#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001693int report_cid_usage( mbedtls_ssl_context *ssl,
1694 const char *additional_description )
1695{
1696 int ret;
1697 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
1698 size_t peer_cid_len;
1699 int cid_negotiated;
1700
1701 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1702 return( 0 );
1703
1704 /* Check if the use of a CID has been negotiated */
1705 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
1706 peer_cid, &peer_cid_len );
1707 if( ret != 0 )
1708 {
1709 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
1710 -ret );
1711 return( ret );
1712 }
1713
1714 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
1715 {
1716 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
1717 {
1718 mbedtls_printf( "(%s) Use of Connection ID was not offered by client.\n",
1719 additional_description );
1720 }
1721 }
1722 else
1723 {
1724 size_t idx=0;
1725 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
1726 additional_description );
1727 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
1728 additional_description,
1729 (unsigned) peer_cid_len );
1730 while( idx < peer_cid_len )
1731 {
1732 mbedtls_printf( "%02x ", peer_cid[ idx ] );
1733 idx++;
1734 }
1735 mbedtls_printf( "\n" );
1736 }
1737
1738 return( 0 );
1739}
Hanno Beckera0e20d02019-05-15 14:03:01 +01001740#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001741
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001742int main( int argc, char *argv[] )
1743{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001744 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001745 int version_suites[4][2];
Hanno Beckerdcc94e62019-07-03 17:05:43 +01001746 io_ctx_t io_ctx;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001747 unsigned char* buf = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001749#if defined(MBEDTLS_USE_PSA_CRYPTO)
1750 psa_algorithm_t alg = 0;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001751 psa_key_handle_t psk_slot = 0;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001752#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +02001754 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +02001755 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +02001756#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001757 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001758 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02001759 size_t cliip_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760#if defined(MBEDTLS_SSL_COOKIE_C)
1761 mbedtls_ssl_cookie_ctx cookie_ctx;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001762#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001763
Gilles Peskineef86ab22017-05-05 18:59:02 +02001764#if defined(MBEDTLS_X509_CRT_PARSE_C)
1765 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
1766#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 mbedtls_entropy_context entropy;
1768 mbedtls_ctr_drbg_context ctr_drbg;
1769 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001770 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001771#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001772 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001773#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001775 unsigned char renego_period[8] = { 0 };
1776#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +02001778 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 mbedtls_x509_crt cacert;
1780 mbedtls_x509_crt srvcert;
1781 mbedtls_pk_context pkey;
1782 mbedtls_x509_crt srvcert2;
1783 mbedtls_pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001784 int key_cert_init = 0, key_cert_init2 = 0;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001785#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001786 ssl_async_key_context_t ssl_async_keys;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001787#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001788#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1790 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001791#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792#if defined(MBEDTLS_SSL_CACHE_C)
1793 mbedtls_ssl_cache_context cache;
Paul Bakker0a597072012-09-25 21:55:46 +00001794#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001795#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1796 mbedtls_ssl_ticket_context ticket_ctx;
1797#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001798#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001799 sni_entry *sni_info = NULL;
1800#endif
Hanno Beckere6706e62017-05-15 16:05:15 +01001801#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +01001802 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +01001803 const mbedtls_ecp_curve_info * curve_cur;
1804#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +01001806 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001807#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Simon Butchercce68be2018-07-23 14:26:09 +01001809 unsigned char alloc_buf[MEMORY_HEAP_SIZE];
Paul Bakker82024bf2013-07-04 11:52:32 +02001810#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001811
Hanno Beckera0e20d02019-05-15 14:03:01 +01001812#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +01001813 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001814 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckera7d25422019-04-09 17:28:10 +01001815 size_t cid_len = 0;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001816 size_t cid_renego_len = 0;
Hanno Beckera7d25422019-04-09 17:28:10 +01001817#endif
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02001818#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1819 unsigned char *context_buf = NULL;
1820 size_t context_buf_len;
1821#endif
Hanno Beckera7d25422019-04-09 17:28:10 +01001822
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001823 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001824 char *p, *q;
1825 const int *list;
Hanno Becker5a9942e2018-11-12 17:47:48 +00001826#if defined(MBEDTLS_USE_PSA_CRYPTO)
1827 psa_status_t status;
1828#endif
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001829#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1830 unsigned char eap_tls_keymaterial[16];
1831 unsigned char eap_tls_iv[8];
1832 const char* eap_tls_label = "client EAP encryption";
1833 eap_tls_keys eap_tls_keying;
1834#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1837 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker82024bf2013-07-04 11:52:32 +02001838#endif
1839
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001840 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +02001841 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001842 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001843 mbedtls_net_init( &client_fd );
1844 mbedtls_net_init( &listen_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001845 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001846 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001847 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848#if defined(MBEDTLS_X509_CRT_PARSE_C)
1849 mbedtls_x509_crt_init( &cacert );
1850 mbedtls_x509_crt_init( &srvcert );
1851 mbedtls_pk_init( &pkey );
1852 mbedtls_x509_crt_init( &srvcert2 );
1853 mbedtls_pk_init( &pkey2 );
Gilles Peskine4d9ec4d2018-04-26 14:33:43 +02001854#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
1855 memset( &ssl_async_keys, 0, sizeof( ssl_async_keys ) );
1856#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001857#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1859 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001860#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861#if defined(MBEDTLS_SSL_CACHE_C)
1862 mbedtls_ssl_cache_init( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00001863#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001864#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1865 mbedtls_ssl_ticket_init( &ticket_ctx );
1866#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +02001868 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001869#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870#if defined(MBEDTLS_SSL_COOKIE_C)
1871 mbedtls_ssl_cookie_init( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001872#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001873
Hanno Becker5a9942e2018-11-12 17:47:48 +00001874#if defined(MBEDTLS_USE_PSA_CRYPTO)
1875 status = psa_crypto_init();
1876 if( status != PSA_SUCCESS )
1877 {
1878 mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
1879 (int) status );
1880 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1881 goto exit;
1882 }
1883#endif
1884
Paul Bakkerc1283d32014-08-18 11:05:51 +02001885#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001886 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001887 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001888 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +02001889#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001890
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001891 if( argc == 0 )
1892 {
1893 usage:
1894 if( ret == 0 )
1895 ret = 1;
1896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 mbedtls_printf( USAGE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 list = mbedtls_ssl_list_ciphersuites();
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001900 while( *list )
1901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001903 list++;
1904 if( !*list )
1905 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001907 list++;
1908 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 mbedtls_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001910 goto exit;
1911 }
1912
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001913 opt.buffer_size = DFL_IO_BUF_LEN;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001914 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001915 opt.server_port = DFL_SERVER_PORT;
1916 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker16970d22017-10-10 15:56:37 +01001917 opt.event = DFL_EVENT;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001918 opt.response_size = DFL_RESPONSE_SIZE;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001919 opt.nbio = DFL_NBIO;
Hanno Beckera7d25422019-04-09 17:28:10 +01001920 opt.cid_enabled = DFL_CID_ENABLED;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001921 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
Hanno Beckera7d25422019-04-09 17:28:10 +01001922 opt.cid_val = DFL_CID_VALUE;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001923 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001924 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001925 opt.ca_file = DFL_CA_FILE;
1926 opt.ca_path = DFL_CA_PATH;
1927 opt.crt_file = DFL_CRT_FILE;
1928 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001929 opt.crt_file2 = DFL_CRT_FILE2;
1930 opt.key_file2 = DFL_KEY_FILE2;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001931 opt.async_operations = DFL_ASYNC_OPERATIONS;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001932 opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
1933 opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
1934 opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
Paul Bakkerfbb17802013-04-17 19:10:21 +02001935 opt.psk = DFL_PSK;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001936#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001937 opt.psk_opaque = DFL_PSK_OPAQUE;
1938 opt.psk_list_opaque = DFL_PSK_LIST_OPAQUE;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001939#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001940#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1941 opt.ca_callback = DFL_CA_CALLBACK;
1942#endif
Paul Bakkerfbb17802013-04-17 19:10:21 +02001943 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001944 opt.psk_list = DFL_PSK_LIST;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001945 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001946 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001947 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001948 opt.renegotiation = DFL_RENEGOTIATION;
1949 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001950 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001951 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001952 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001953 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001954 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001955 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001956 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001957 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001958 opt.auth_mode = DFL_AUTH_MODE;
Janos Follath4817e272017-04-10 13:44:33 +01001959 opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001960 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001961 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001962 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001963 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001964 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001965 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001966 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001967 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001968 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001969 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001970 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001971 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001972 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001973 opt.hs_to_min = DFL_HS_TO_MIN;
1974 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001975 opt.dtls_mtu = DFL_DTLS_MTU;
Hanno Beckere7675d02018-08-14 13:28:56 +01001976 opt.dgram_packing = DFL_DGRAM_PACKING;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001977 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001978 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001979 opt.etm = DFL_ETM;
Jarno Lamsa9831c8a2019-05-29 13:33:32 +03001980 opt.serialize = DFL_SERIALIZE;
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001981 opt.eap_tls = DFL_EAP_TLS;
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001982 opt.reproducible = DFL_REPRODUCIBLE;
Hanno Becker48f3a3d2019-08-29 06:31:22 +01001983 opt.nss_keylog = DFL_NSS_KEYLOG;
1984 opt.nss_keylog_file = DFL_NSS_KEYLOG_FILE;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001985
1986 for( i = 1; i < argc; i++ )
1987 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001988 p = argv[i];
1989 if( ( q = strchr( p, '=' ) ) == NULL )
1990 goto usage;
1991 *q++ = '\0';
1992
1993 if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001994 opt.server_port = q;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001995 else if( strcmp( p, "server_addr" ) == 0 )
1996 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001997 else if( strcmp( p, "dtls" ) == 0 )
1998 {
1999 int t = atoi( q );
2000 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01002002 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01002004 else
2005 goto usage;
2006 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002007 else if( strcmp( p, "debug_level" ) == 0 )
2008 {
2009 opt.debug_level = atoi( q );
2010 if( opt.debug_level < 0 || opt.debug_level > 65535 )
2011 goto usage;
2012 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002013 else if( strcmp( p, "nbio" ) == 0 )
2014 {
2015 opt.nbio = atoi( q );
2016 if( opt.nbio < 0 || opt.nbio > 2 )
2017 goto usage;
2018 }
Hanno Becker16970d22017-10-10 15:56:37 +01002019 else if( strcmp( p, "event" ) == 0 )
2020 {
2021 opt.event = atoi( q );
2022 if( opt.event < 0 || opt.event > 2 )
2023 goto usage;
2024 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002025 else if( strcmp( p, "read_timeout" ) == 0 )
2026 opt.read_timeout = atoi( q );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002027 else if( strcmp( p, "buffer_size" ) == 0 )
2028 {
2029 opt.buffer_size = atoi( q );
2030 if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
2031 goto usage;
2032 }
2033 else if( strcmp( p, "response_size" ) == 0 )
2034 {
2035 opt.response_size = atoi( q );
2036 if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
2037 goto usage;
2038 if( opt.buffer_size < opt.response_size )
2039 opt.buffer_size = opt.response_size;
2040 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002041 else if( strcmp( p, "ca_file" ) == 0 )
2042 opt.ca_file = q;
2043 else if( strcmp( p, "ca_path" ) == 0 )
2044 opt.ca_path = q;
2045 else if( strcmp( p, "crt_file" ) == 0 )
2046 opt.crt_file = q;
2047 else if( strcmp( p, "key_file" ) == 0 )
2048 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002049 else if( strcmp( p, "crt_file2" ) == 0 )
2050 opt.crt_file2 = q;
2051 else if( strcmp( p, "key_file2" ) == 0 )
2052 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002053 else if( strcmp( p, "dhm_file" ) == 0 )
2054 opt.dhm_file = q;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002055#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002056 else if( strcmp( p, "async_operations" ) == 0 )
2057 opt.async_operations = q;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002058 else if( strcmp( p, "async_private_delay1" ) == 0 )
2059 opt.async_private_delay1 = atoi( q );
2060 else if( strcmp( p, "async_private_delay2" ) == 0 )
2061 opt.async_private_delay2 = atoi( q );
2062 else if( strcmp( p, "async_private_error" ) == 0 )
2063 {
2064 int n = atoi( q );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002065 if( n < -SSL_ASYNC_INJECT_ERROR_MAX ||
2066 n > SSL_ASYNC_INJECT_ERROR_MAX )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002067 {
2068 ret = 2;
2069 goto usage;
2070 }
2071 opt.async_private_error = n;
2072 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002073#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Hanno Beckera0e20d02019-05-15 14:03:01 +01002074#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +01002075 else if( strcmp( p, "cid" ) == 0 )
2076 {
2077 opt.cid_enabled = atoi( q );
2078 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
2079 goto usage;
2080 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002081 else if( strcmp( p, "cid_renego" ) == 0 )
2082 {
2083 opt.cid_enabled_renego = atoi( q );
2084 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
2085 goto usage;
2086 }
Hanno Beckera7d25422019-04-09 17:28:10 +01002087 else if( strcmp( p, "cid_val" ) == 0 )
2088 {
2089 opt.cid_val = q;
2090 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002091 else if( strcmp( p, "cid_val_renego" ) == 0 )
2092 {
2093 opt.cid_val_renego = q;
2094 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002095#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002096 else if( strcmp( p, "psk" ) == 0 )
2097 opt.psk = q;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002098#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002099 else if( strcmp( p, "psk_opaque" ) == 0 )
2100 opt.psk_opaque = atoi( q );
2101 else if( strcmp( p, "psk_list_opaque" ) == 0 )
2102 opt.psk_list_opaque = atoi( q );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002103#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02002104#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2105 else if( strcmp( p, "ca_callback" ) == 0)
2106 opt.ca_callback = atoi( q );
2107#endif
Paul Bakkerfbb17802013-04-17 19:10:21 +02002108 else if( strcmp( p, "psk_identity" ) == 0 )
2109 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002110 else if( strcmp( p, "psk_list" ) == 0 )
2111 opt.psk_list = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002112 else if( strcmp( p, "ecjpake_pw" ) == 0 )
2113 opt.ecjpake_pw = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002114 else if( strcmp( p, "force_ciphersuite" ) == 0 )
2115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002117
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02002118 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002119 {
2120 ret = 2;
2121 goto usage;
2122 }
2123 opt.force_ciphersuite[1] = 0;
2124 }
Hanno Beckere6706e62017-05-15 16:05:15 +01002125 else if( strcmp( p, "curves" ) == 0 )
2126 opt.curves = q;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002127 else if( strcmp( p, "version_suites" ) == 0 )
2128 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002129 else if( strcmp( p, "renegotiation" ) == 0 )
2130 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002131 opt.renegotiation = (atoi( q )) ?
2132 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
2133 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002134 }
2135 else if( strcmp( p, "allow_legacy" ) == 0 )
2136 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002137 switch( atoi( q ) )
2138 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002139 case -1:
2140 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
2141 break;
2142 case 0:
2143 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
2144 break;
2145 case 1:
2146 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
2147 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002148 default: goto usage;
2149 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002150 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002151 else if( strcmp( p, "renegotiate" ) == 0 )
2152 {
2153 opt.renegotiate = atoi( q );
2154 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
2155 goto usage;
2156 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02002157 else if( strcmp( p, "renego_delay" ) == 0 )
2158 {
2159 opt.renego_delay = atoi( q );
2160 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002161 else if( strcmp( p, "renego_period" ) == 0 )
2162 {
Andres AG0b736db2017-02-08 14:05:57 +00002163#if defined(_MSC_VER)
2164 opt.renego_period = _strtoui64( q, NULL, 10 );
2165#else
2166 if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
2167 goto usage;
2168#endif /* _MSC_VER */
2169 if( opt.renego_period < 2 )
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002170 goto usage;
2171 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02002172 else if( strcmp( p, "exchanges" ) == 0 )
2173 {
2174 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02002175 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02002176 goto usage;
2177 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00002178 else if( strcmp( p, "min_version" ) == 0 )
2179 {
2180 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002182 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01002184 else if( strcmp( q, "tls1_1" ) == 0 ||
2185 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01002187 else if( strcmp( q, "tls1_2" ) == 0 ||
2188 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002190 else
2191 goto usage;
2192 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002193 else if( strcmp( p, "max_version" ) == 0 )
2194 {
2195 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002197 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01002199 else if( strcmp( q, "tls1_1" ) == 0 ||
2200 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01002202 else if( strcmp( q, "tls1_2" ) == 0 ||
2203 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002205 else
2206 goto usage;
2207 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01002208 else if( strcmp( p, "arc4" ) == 0 )
2209 {
2210 switch( atoi( q ) )
2211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
2213 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01002214 default: goto usage;
2215 }
2216 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02002217 else if( strcmp( p, "allow_sha1" ) == 0 )
2218 {
2219 switch( atoi( q ) )
2220 {
2221 case 0: opt.allow_sha1 = 0; break;
2222 case 1: opt.allow_sha1 = 1; break;
2223 default: goto usage;
2224 }
2225 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002226 else if( strcmp( p, "force_version" ) == 0 )
2227 {
2228 if( strcmp( q, "ssl3" ) == 0 )
2229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
2231 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002232 }
2233 else if( strcmp( q, "tls1" ) == 0 )
2234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
2236 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002237 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002238 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02002239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
2241 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002242 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002243 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02002244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
2246 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002247 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002248 else if( strcmp( q, "dtls1" ) == 0 )
2249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
2251 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
2252 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002253 }
2254 else if( strcmp( q, "dtls1_2" ) == 0 )
2255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
2257 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
2258 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002259 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002260 else
2261 goto usage;
2262 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01002263 else if( strcmp( p, "auth_mode" ) == 0 )
2264 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02002265 if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
Paul Bakker91ebfb52012-11-23 14:04:08 +01002266 goto usage;
2267 }
Janos Follath4817e272017-04-10 13:44:33 +01002268 else if( strcmp( p, "cert_req_ca_list" ) == 0 )
2269 {
2270 opt.cert_req_ca_list = atoi( q );
2271 if( opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1 )
2272 goto usage;
2273 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002274 else if( strcmp( p, "max_frag_len" ) == 0 )
2275 {
2276 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002278 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002280 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002282 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002284 else
2285 goto usage;
2286 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002287 else if( strcmp( p, "alpn" ) == 0 )
2288 {
2289 opt.alpn_string = q;
2290 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002291 else if( strcmp( p, "trunc_hmac" ) == 0 )
2292 {
2293 switch( atoi( q ) )
2294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
2296 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002297 default: goto usage;
2298 }
2299 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002300 else if( strcmp( p, "extended_ms" ) == 0 )
2301 {
2302 switch( atoi( q ) )
2303 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002304 case 0:
2305 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
2306 break;
2307 case 1:
2308 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
2309 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002310 default: goto usage;
2311 }
2312 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002313 else if( strcmp( p, "etm" ) == 0 )
2314 {
2315 switch( atoi( q ) )
2316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
2318 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002319 default: goto usage;
2320 }
2321 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002322 else if( strcmp( p, "tickets" ) == 0 )
2323 {
2324 opt.tickets = atoi( q );
2325 if( opt.tickets < 0 || opt.tickets > 1 )
2326 goto usage;
2327 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01002328 else if( strcmp( p, "ticket_timeout" ) == 0 )
2329 {
2330 opt.ticket_timeout = atoi( q );
2331 if( opt.ticket_timeout < 0 )
2332 goto usage;
2333 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002334 else if( strcmp( p, "cache_max" ) == 0 )
2335 {
2336 opt.cache_max = atoi( q );
2337 if( opt.cache_max < 0 )
2338 goto usage;
2339 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002340 else if( strcmp( p, "cache_timeout" ) == 0 )
2341 {
2342 opt.cache_timeout = atoi( q );
2343 if( opt.cache_timeout < 0 )
2344 goto usage;
2345 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002346 else if( strcmp( p, "cookies" ) == 0 )
2347 {
2348 opt.cookies = atoi( q );
2349 if( opt.cookies < -1 || opt.cookies > 1)
2350 goto usage;
2351 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002352 else if( strcmp( p, "anti_replay" ) == 0 )
2353 {
2354 opt.anti_replay = atoi( q );
2355 if( opt.anti_replay < 0 || opt.anti_replay > 1)
2356 goto usage;
2357 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002358 else if( strcmp( p, "badmac_limit" ) == 0 )
2359 {
2360 opt.badmac_limit = atoi( q );
2361 if( opt.badmac_limit < 0 )
2362 goto usage;
2363 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002364 else if( strcmp( p, "hs_timeout" ) == 0 )
2365 {
2366 if( ( p = strchr( q, '-' ) ) == NULL )
2367 goto usage;
2368 *p++ = '\0';
2369 opt.hs_to_min = atoi( q );
2370 opt.hs_to_max = atoi( p );
2371 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
2372 goto usage;
2373 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002374 else if( strcmp( p, "mtu" ) == 0 )
2375 {
2376 opt.dtls_mtu = atoi( q );
2377 if( opt.dtls_mtu < 0 )
2378 goto usage;
2379 }
Hanno Beckere7675d02018-08-14 13:28:56 +01002380 else if( strcmp( p, "dgram_packing" ) == 0 )
2381 {
2382 opt.dgram_packing = atoi( q );
2383 if( opt.dgram_packing != 0 &&
2384 opt.dgram_packing != 1 )
2385 {
2386 goto usage;
2387 }
2388 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002389 else if( strcmp( p, "sni" ) == 0 )
2390 {
2391 opt.sni = q;
2392 }
Andres Amaya Garciabc818842018-10-16 21:08:38 +01002393 else if( strcmp( p, "query_config" ) == 0 )
2394 {
2395 return query_config( q );
2396 }
Jarno Lamsa9831c8a2019-05-29 13:33:32 +03002397 else if( strcmp( p, "serialize") == 0 )
2398 {
2399 opt.serialize = atoi( q );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03002400 if( opt.serialize < 0 || opt.serialize > 2)
Jarno Lamsa9831c8a2019-05-29 13:33:32 +03002401 goto usage;
2402 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002403 else if( strcmp( p, "eap_tls" ) == 0 )
2404 {
2405 opt.eap_tls = atoi( q );
2406 if( opt.eap_tls < 0 || opt.eap_tls > 1 )
2407 goto usage;
2408 }
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002409 else if( strcmp( p, "reproducible" ) == 0 )
2410 {
2411 opt.reproducible = 1;
2412 }
Hanno Becker48f3a3d2019-08-29 06:31:22 +01002413 else if( strcmp( p, "nss_keylog" ) == 0 )
2414 {
2415 opt.nss_keylog = atoi( q );
2416 if( opt.nss_keylog < 0 || opt.nss_keylog > 1 )
2417 goto usage;
2418 }
2419 else if( strcmp( p, "nss_keylog_file" ) == 0 )
2420 {
2421 opt.nss_keylog_file = q;
2422 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002423 else
2424 goto usage;
2425 }
2426
Hanno Becker16970d22017-10-10 15:56:37 +01002427 /* Event-driven IO is incompatible with the above custom
2428 * receive and send functions, as the polling builds on
2429 * refers to the underlying net_context. */
2430 if( opt.event == 1 && opt.nbio != 1 )
2431 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002432 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01002433 opt.nbio = 1;
2434 }
2435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436#if defined(MBEDTLS_DEBUG_C)
2437 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02002438#endif
Andrzej Kurekda4029d2018-06-20 07:07:55 -04002439 buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002440 if( buf == NULL )
2441 {
Andrzej Kurek755890f2018-06-20 08:17:04 -04002442 mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002443 ret = 3;
2444 goto exit;
2445 }
Paul Bakkerc73079a2014-04-25 16:34:30 +02002446
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002447#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002448 if( opt.psk_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002449 {
2450 if( strlen( opt.psk ) == 0 )
2451 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00002452 mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002453 ret = 2;
2454 goto usage;
2455 }
2456
2457 if( opt.force_ciphersuite[0] <= 0 )
2458 {
2459 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" );
2460 ret = 2;
2461 goto usage;
2462 }
2463 }
2464
Hanno Becker1d911cd2018-11-15 13:06:09 +00002465 if( opt.psk_list_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002466 {
2467 if( opt.psk_list == NULL )
2468 {
2469 mbedtls_printf( "psk_slot set but no psk to be imported specified.\n" );
2470 ret = 2;
2471 goto usage;
2472 }
2473
2474 if( opt.force_ciphersuite[0] <= 0 )
2475 {
2476 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" );
2477 ret = 2;
2478 goto usage;
2479 }
2480 }
2481#endif /* MBEDTLS_USE_PSA_CRYPTO */
2482
Paul Bakkerc1516be2013-06-29 16:01:32 +02002483 if( opt.force_ciphersuite[0] > 0 )
2484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002486 ciphersuite_info =
2487 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002488
Paul Bakker5b55b792013-07-19 13:43:43 +02002489 if( opt.max_version != -1 &&
2490 ciphersuite_info->min_minor_ver > opt.max_version )
2491 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002492 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker5b55b792013-07-19 13:43:43 +02002493 ret = 2;
2494 goto usage;
2495 }
2496 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02002497 ciphersuite_info->max_minor_ver < opt.min_version )
2498 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002499 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002500 ret = 2;
2501 goto usage;
2502 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002503
2504 /* If we select a version that's not supported by
2505 * this suite, then there will be no common ciphersuite... */
2506 if( opt.max_version == -1 ||
2507 opt.max_version > ciphersuite_info->max_minor_ver )
2508 {
Paul Bakker5b55b792013-07-19 13:43:43 +02002509 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002510 }
Paul Bakker5b55b792013-07-19 13:43:43 +02002511 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002512 {
Paul Bakker5b55b792013-07-19 13:43:43 +02002513 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002514 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2516 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
2517 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002518 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002519
2520 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002526 ret = 2;
2527 goto usage;
2528 }
2529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002531 }
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002532
2533#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002534 if( opt.psk_opaque != 0 || opt.psk_list_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002535 {
2536 /* Ensure that the chosen ciphersuite is PSK-only; we must know
2537 * the ciphersuite in advance to set the correct policy for the
2538 * PSK key slot. This limitation might go away in the future. */
2539 if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
2540 opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 )
2541 {
2542 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" );
2543 ret = 2;
2544 goto usage;
2545 }
2546
2547 /* Determine KDF algorithm the opaque PSK will be used in. */
2548#if defined(MBEDTLS_SHA512_C)
2549 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
2550 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2551 else
2552#endif /* MBEDTLS_SHA512_C */
2553 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2554 }
2555#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerc1516be2013-06-29 16:01:32 +02002556 }
2557
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002558 if( opt.version_suites != NULL )
2559 {
2560 const char *name[4] = { 0 };
2561
2562 /* Parse 4-element coma-separated list */
2563 for( i = 0, p = (char *) opt.version_suites;
2564 i < 4 && *p != '\0';
2565 i++ )
2566 {
2567 name[i] = p;
2568
2569 /* Terminate the current string and move on to next one */
2570 while( *p != ',' && *p != '\0' )
2571 p++;
2572 if( *p == ',' )
2573 *p++ = '\0';
2574 }
2575
2576 if( i != 4 )
2577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578 mbedtls_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002579 ret = 1;
2580 goto exit;
2581 }
2582
2583 memset( version_suites, 0, sizeof( version_suites ) );
2584
2585 /* Get the suites identifiers from their name */
2586 for( i = 0; i < 4; i++ )
2587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002589
2590 if( version_suites[i][0] == 0 )
2591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592 mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002593 ret = 2;
2594 goto usage;
2595 }
2596 }
2597 }
2598
Hanno Beckera0e20d02019-05-15 14:03:01 +01002599#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002600 if( unhexify( cid, opt.cid_val, &cid_len ) != 0 )
2601 {
2602 mbedtls_printf( "CID not valid hex\n" );
2603 goto exit;
2604 }
2605
2606 /* Keep CID settings for renegotiation unless
2607 * specified otherwise. */
2608 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
2609 opt.cid_enabled_renego = opt.cid_enabled;
2610 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
2611 opt.cid_val_renego = opt.cid_val;
2612
2613 if( unhexify( cid_renego, opt.cid_val_renego, &cid_renego_len ) != 0 )
2614 {
2615 mbedtls_printf( "CID not valid hex\n" );
2616 goto exit;
2617 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002618#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +01002619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002621 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002622 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02002623 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002624 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 mbedtls_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002627 goto exit;
2628 }
2629
2630 if( opt.psk_list != NULL )
2631 {
2632 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 mbedtls_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002635 goto exit;
2636 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002637 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002639
Hanno Beckere6706e62017-05-15 16:05:15 +01002640#if defined(MBEDTLS_ECP_C)
2641 if( opt.curves != NULL )
2642 {
2643 p = (char *) opt.curves;
2644 i = 0;
2645
2646 if( strcmp( p, "none" ) == 0 )
2647 {
2648 curve_list[0] = MBEDTLS_ECP_DP_NONE;
2649 }
2650 else if( strcmp( p, "default" ) != 0 )
2651 {
2652 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01002653 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002654 {
2655 q = p;
2656
2657 /* Terminate the current string */
2658 while( *p != ',' && *p != '\0' )
2659 p++;
2660 if( *p == ',' )
2661 *p++ = '\0';
2662
2663 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
2664 {
2665 curve_list[i++] = curve_cur->grp_id;
2666 }
2667 else
2668 {
2669 mbedtls_printf( "unknown curve %s\n", q );
2670 mbedtls_printf( "supported curves: " );
2671 for( curve_cur = mbedtls_ecp_curve_list();
2672 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
2673 curve_cur++ )
2674 {
2675 mbedtls_printf( "%s ", curve_cur->name );
2676 }
2677 mbedtls_printf( "\n" );
2678 goto exit;
2679 }
2680 }
2681
2682 mbedtls_printf("Number of curves: %d\n", i );
2683
Hanno Becker8651a432017-06-09 16:13:22 +01002684 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002685 {
Hanno Becker8651a432017-06-09 16:13:22 +01002686 mbedtls_printf( "curves list too long, maximum %d",
2687 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01002688 goto exit;
2689 }
2690
2691 curve_list[i] = MBEDTLS_ECP_DP_NONE;
2692 }
2693 }
2694#endif /* MBEDTLS_ECP_C */
2695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002697 if( opt.alpn_string != NULL )
2698 {
2699 p = (char *) opt.alpn_string;
2700 i = 0;
2701
2702 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01002703 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002704 {
2705 alpn_list[i++] = p;
2706
2707 /* Terminate the current string and move on to next one */
2708 while( *p != ',' && *p != '\0' )
2709 p++;
2710 if( *p == ',' )
2711 *p++ = '\0';
2712 }
2713 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002715
Paul Bakkerfbb17802013-04-17 19:10:21 +02002716 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002717 * 0. Initialize the RNG and the session data
2718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002720 fflush( stdout );
2721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722 mbedtls_entropy_init( &entropy );
Philippe Antoine986b6f22019-06-07 15:04:32 +02002723 if (opt.reproducible)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002724 {
Philippe Antoine738153a2019-06-18 20:16:43 +02002725 srand( 1 );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002726 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy,
Philippe Antoine986b6f22019-06-07 15:04:32 +02002727 &entropy, (const unsigned char *) pers,
2728 strlen( pers ) ) ) != 0 )
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002729 {
2730 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
Philippe Antoine986b6f22019-06-07 15:04:32 +02002731 -ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002732 goto exit;
2733 }
Philippe Antoine986b6f22019-06-07 15:04:32 +02002734 }
2735 else
2736 {
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002737 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
Philippe Antoine986b6f22019-06-07 15:04:32 +02002738 &entropy, (const unsigned char *) pers,
2739 strlen( pers ) ) ) != 0 )
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002740 {
2741 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
Philippe Antoine986b6f22019-06-07 15:04:32 +02002742 -ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002743 goto exit;
2744 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002745 }
2746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002750 /*
2751 * 1.1. Load the trusted CA
2752 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002754 fflush( stdout );
2755
Hanno Becker8174bdf2019-03-05 16:22:07 +00002756 if( strcmp( opt.ca_path, "none" ) == 0 ||
2757 strcmp( opt.ca_file, "none" ) == 0 )
2758 {
2759 ret = 0;
2760 }
2761 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002763 if( strlen( opt.ca_path ) )
Hanno Becker8174bdf2019-03-05 16:22:07 +00002764 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002765 else if( strlen( opt.ca_file ) )
Hanno Becker8174bdf2019-03-05 16:22:07 +00002766 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02002767 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002768#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769#if defined(MBEDTLS_CERTS_C)
Hanno Becker09b8cae2019-02-01 08:13:19 +00002770 {
2771#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 ret = mbedtls_x509_crt_parse( &cacert,
2775 (const unsigned char *) mbedtls_test_cas[i],
2776 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002777 if( ret != 0 )
2778 break;
2779 }
Hanno Becker09b8cae2019-02-01 08:13:19 +00002780 if( ret == 0 )
2781#endif /* MBEDTLS_PEM_PARSE_C */
2782 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
2783 {
2784 ret = mbedtls_x509_crt_parse_der( &cacert,
2785 (const unsigned char *) mbedtls_test_cas_der[i],
2786 mbedtls_test_cas_der_len[i] );
2787 if( ret != 0 )
2788 break;
2789 }
2790 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002791#else
2792 {
2793 ret = 1;
Hanno Beckera0c5ceb2018-12-05 16:49:42 +00002794 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002795 }
Hanno Becker09b8cae2019-02-01 08:13:19 +00002796#endif /* MBEDTLS_CERTS_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002797 if( ret < 0 )
2798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002800 goto exit;
2801 }
2802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002804
2805 /*
2806 * 1.2. Load own certificate and private key
2807 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002809 fflush( stdout );
2810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002812 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002813 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002814 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002818 -ret );
2819 goto exit;
2820 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002821 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002822 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002823 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002824 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825 if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002828 goto exit;
2829 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002830 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002831 if( key_cert_init == 1 )
2832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002834 goto exit;
2835 }
2836
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002837 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002838 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002839 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002843 -ret );
2844 goto exit;
2845 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002846 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002847 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002848 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002849 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850 if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002853 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002854 goto exit;
2855 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002856 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002857 if( key_cert_init2 == 1 )
2858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859 mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002860 goto exit;
2861 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002862#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002863 if( key_cert_init == 0 &&
2864 strcmp( opt.crt_file, "none" ) != 0 &&
2865 strcmp( opt.key_file, "none" ) != 0 &&
2866 key_cert_init2 == 0 &&
2867 strcmp( opt.crt_file2, "none" ) != 0 &&
2868 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870#if !defined(MBEDTLS_CERTS_C)
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002871 mbedtls_printf( "Not certificated or key provided, and \nMBEDTLS_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002872 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002873#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874#if defined(MBEDTLS_RSA_C)
2875 if( ( ret = mbedtls_x509_crt_parse( &srvcert,
2876 (const unsigned char *) mbedtls_test_srv_crt_rsa,
2877 mbedtls_test_srv_crt_rsa_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002878 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002879 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
2880 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002881 goto exit;
2882 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 if( ( ret = mbedtls_pk_parse_key( &pkey,
2884 (const unsigned char *) mbedtls_test_srv_key_rsa,
2885 mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002886 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002887 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
2888 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002889 goto exit;
2890 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002891 key_cert_init = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892#endif /* MBEDTLS_RSA_C */
2893#if defined(MBEDTLS_ECDSA_C)
2894 if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
2895 (const unsigned char *) mbedtls_test_srv_crt_ec,
2896 mbedtls_test_srv_crt_ec_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002897 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002898 mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n",
2899 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002900 goto exit;
2901 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 if( ( ret = mbedtls_pk_parse_key( &pkey2,
2903 (const unsigned char *) mbedtls_test_srv_key_ec,
2904 mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002905 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002906 mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n",
2907 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002908 goto exit;
2909 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002910 key_cert_init2 = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911#endif /* MBEDTLS_ECDSA_C */
2912#endif /* MBEDTLS_CERTS_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002913 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 mbedtls_printf( " ok\n" );
2916#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002919 if( opt.dhm_file != NULL )
2920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 mbedtls_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002922 fflush( stdout );
2923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002927 -ret );
2928 goto exit;
2929 }
2930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002932 }
2933#endif
2934
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002935#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002936 if( opt.sni != NULL )
2937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 mbedtls_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002939 fflush( stdout );
2940
2941 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
2942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002944 goto exit;
2945 }
2946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002948 }
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002949#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002950
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002951 /*
2952 * 2. Setup the listening TCP socket
2953 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02002954 mbedtls_printf( " . Bind on %s://%s:%s/ ...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002956 opt.server_addr ? opt.server_addr : "*",
2957 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002958 fflush( stdout );
2959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
2961 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2962 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002965 goto exit;
2966 }
2967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002969
2970 /*
2971 * 3. Setup stuff
2972 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002974 fflush( stdout );
2975
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02002976 if( ( ret = mbedtls_ssl_config_defaults( &conf,
2977 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02002978 opt.transport,
2979 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002980 {
2981 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
2982 goto exit;
2983 }
2984
Gilles Peskineef86ab22017-05-05 18:59:02 +02002985#if defined(MBEDTLS_X509_CRT_PARSE_C)
2986 /* The default algorithms profile disables SHA-1, but our tests still
2987 rely on it heavily. Hence we allow it here. A real-world server
2988 should use the default profile unless there is a good reason not to. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02002989 if( opt.allow_sha1 > 0 )
2990 {
2991 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
2992 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
Gilles Peskine682df092017-05-11 19:01:11 +02002993 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
Gilles Peskinebc70a182017-05-09 15:59:24 +02002994 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02002995#endif /* MBEDTLS_X509_CRT_PARSE_C */
2996
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01002997 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002998 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002999
Janos Follath4817e272017-04-10 13:44:33 +01003000 if( opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST )
3001 mbedtls_ssl_conf_cert_req_ca_list( &conf, opt.cert_req_ca_list );
3002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02003004 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 +02003005 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003006
Hanno Beckere7675d02018-08-14 13:28:56 +01003007 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01003008 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02003010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003012 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003013 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003014 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003015 goto exit;
3016 };
Paul Bakker05decb22013-08-15 13:33:48 +02003017#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02003018
Hanno Beckera0e20d02019-05-15 14:03:01 +01003019#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003020 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckerad4a1372019-05-03 13:06:44 +01003021 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003022 if( opt.cid_enabled == 1 &&
3023 opt.cid_enabled_renego == 1 &&
3024 cid_len != cid_renego_len )
3025 {
3026 mbedtls_printf( "CID length must not change during renegotiation\n" );
3027 goto usage;
3028 }
3029
3030 if( opt.cid_enabled == 1 )
Hanno Becker8367ccc2019-05-14 11:30:10 +01003031 ret = mbedtls_ssl_conf_cid( &conf, cid_len,
3032 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003033 else
Hanno Becker8367ccc2019-05-14 11:30:10 +01003034 ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
3035 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003036
Hanno Beckerad4a1372019-05-03 13:06:44 +01003037 if( ret != 0 )
3038 {
Hanno Beckerd5eed422019-05-23 16:58:22 +01003039 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
3040 -ret );
Hanno Beckerad4a1372019-05-03 13:06:44 +01003041 goto exit;
3042 }
3043 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003044#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerad4a1372019-05-03 13:06:44 +01003045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003047 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003048 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003049#endif
3050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003052 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003053 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003054#endif
3055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003057 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003058 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003059#endif
3060
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003061#if defined(MBEDTLS_SSL_EXPORT_KEYS)
3062 if( opt.eap_tls != 0 )
Hanno Becker48f3a3d2019-08-29 06:31:22 +01003063 {
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003064 mbedtls_ssl_conf_export_keys_ext_cb( &conf, eap_tls_key_derivation,
3065 &eap_tls_keying );
Hanno Becker48f3a3d2019-08-29 06:31:22 +01003066 }
3067 else if( opt.nss_keylog != 0 )
3068 {
3069 mbedtls_ssl_conf_export_keys_ext_cb( &conf,
3070 nss_keylog_export,
3071 NULL );
3072 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003073#endif
3074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003076 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003077 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003078 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003079 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003080 goto exit;
3081 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003082#endif
3083
Philippe Antoine986b6f22019-06-07 15:04:32 +02003084 if (opt.reproducible)
3085 {
Philippe Antoinef91b3722019-06-11 16:02:43 +02003086#if defined(MBEDTLS_HAVE_TIME)
Philippe Antoineaa4d1522019-06-06 21:24:31 +02003087#if defined(MBEDTLS_PLATFORM_TIME_ALT)
3088 mbedtls_platform_set_time( dummy_constant_time );
3089#else
Philippe Antoine7c9d7242019-06-11 12:11:36 +02003090 fprintf( stderr, "Warning: reproducible option used without constant time\n" );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02003091#endif
Philippe Antoine0ff84fb2019-06-11 12:15:17 +02003092#endif
Philippe Antoine986b6f22019-06-07 15:04:32 +02003093 }
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003094 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
3095 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01003098 if( opt.cache_max != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01003100
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01003101 if( opt.cache_timeout != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102 mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01003103
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003104 mbedtls_ssl_conf_session_cache( &conf, &cache,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01003105 mbedtls_ssl_cache_get,
3106 mbedtls_ssl_cache_set );
Paul Bakker0a597072012-09-25 21:55:46 +00003107#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003110 if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003111 {
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003112 if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
3113 mbedtls_ctr_drbg_random, &ctr_drbg,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +02003114 MBEDTLS_CIPHER_AES_256_GCM,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003115 opt.ticket_timeout ) ) != 0 )
3116 {
3117 mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
3118 goto exit;
3119 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01003120
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003121 mbedtls_ssl_conf_session_tickets_cb( &conf,
3122 mbedtls_ssl_ticket_write,
3123 mbedtls_ssl_ticket_parse,
3124 &ticket_ctx );
3125 }
Paul Bakkera503a632013-08-14 13:48:06 +02003126#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128#if defined(MBEDTLS_SSL_PROTO_DTLS)
3129 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02003130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003132 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02003133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
3135 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137 mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003138 goto exit;
3139 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02003140
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003141 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003142 &cookie_ctx );
3143 }
3144 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145#endif /* MBEDTLS_SSL_COOKIE_C */
3146#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003147 if( opt.cookies == 0 )
3148 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003149 mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003150 }
3151 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02003153 {
3154 ; /* Nothing to do */
3155 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003158 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003159 mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02003160#endif
3161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02003163 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003164 mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003165#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02003166 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02003168
Paul Bakker41c83d32013-03-20 14:39:14 +01003169 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003170 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00003171
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02003172#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00003173 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003174 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02003175#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003176
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02003177 if( opt.version_suites != NULL )
3178 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003179 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180 MBEDTLS_SSL_MAJOR_VERSION_3,
3181 MBEDTLS_SSL_MINOR_VERSION_0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003182 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183 MBEDTLS_SSL_MAJOR_VERSION_3,
3184 MBEDTLS_SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003185 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186 MBEDTLS_SSL_MAJOR_VERSION_3,
3187 MBEDTLS_SSL_MINOR_VERSION_2 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003188 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 MBEDTLS_SSL_MAJOR_VERSION_3,
3190 MBEDTLS_SSL_MINOR_VERSION_3 );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02003191 }
3192
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01003193 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003194 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003196 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01003197
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02003198 if( opt.renego_delay != DFL_RENEGO_DELAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003199 mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003200
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01003201 if( opt.renego_period != DFL_RENEGO_PERIOD )
3202 {
Andres AG692ad842017-01-19 16:30:57 +00003203 PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003204 mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01003205 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003206#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01003209 if( strcmp( opt.ca_path, "none" ) != 0 &&
3210 strcmp( opt.ca_file, "none" ) != 0 )
3211 {
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02003212#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3213 if( opt.ca_callback != 0 )
3214 mbedtls_ssl_conf_ca_cb( &conf, ca_callback, &cacert);
3215 else
3216#endif
3217 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01003218 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02003219 if( key_cert_init )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003220 {
3221 mbedtls_pk_context *pk = &pkey;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003222#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003223 if( opt.async_private_delay1 >= 0 )
3224 {
Gilles Peskine44817442018-06-13 18:09:28 +02003225 ret = ssl_async_set_key( &ssl_async_keys, &srvcert, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02003226 opt.async_private_delay1 );
3227 if( ret < 0 )
3228 {
3229 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
3230 ret );
3231 goto exit;
3232 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003233 pk = NULL;
3234 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003235#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003236 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003237 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003238 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003239 goto exit;
3240 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003241 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02003242 if( key_cert_init2 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003243 {
3244 mbedtls_pk_context *pk = &pkey2;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003245#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003246 if( opt.async_private_delay2 >= 0 )
3247 {
Gilles Peskine44817442018-06-13 18:09:28 +02003248 ret = ssl_async_set_key( &ssl_async_keys, &srvcert2, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02003249 opt.async_private_delay2 );
3250 if( ret < 0 )
3251 {
3252 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
3253 ret );
3254 goto exit;
3255 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003256 pk = NULL;
3257 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003258#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003259 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003260 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003261 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003262 goto exit;
3263 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003264 }
3265
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003266#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003267 if( opt.async_operations[0] != '-' )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003268 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003269 mbedtls_ssl_async_sign_t *sign = NULL;
3270 mbedtls_ssl_async_decrypt_t *decrypt = NULL;
Gilles Peskine12ab5d42018-04-24 12:32:04 +02003271 const char *r;
3272 for( r = opt.async_operations; *r; r++ )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003273 {
Gilles Peskine12ab5d42018-04-24 12:32:04 +02003274 switch( *r )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003275 {
3276 case 'd':
3277 decrypt = ssl_async_decrypt;
3278 break;
3279 case 's':
3280 sign = ssl_async_sign;
3281 break;
3282 }
3283 }
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003284 ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
3285 - opt.async_private_error :
3286 opt.async_private_error );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003287 ssl_async_keys.f_rng = mbedtls_ctr_drbg_random;
3288 ssl_async_keys.p_rng = &ctr_drbg;
3289 mbedtls_ssl_conf_async_private_cb( &conf,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003290 sign,
3291 decrypt,
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003292 ssl_async_resume,
3293 ssl_async_cancel,
3294 &ssl_async_keys );
3295 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003296#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003297#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +02003298
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02003299#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003300 if( opt.sni != NULL )
Gilles Peskine166ce742018-04-30 10:30:49 +02003301 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003302 mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
Gilles Peskine166ce742018-04-30 10:30:49 +02003303#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3304 if( opt.async_private_delay2 >= 0 )
3305 {
Gilles Peskinee2479892018-06-13 18:06:51 +02003306 sni_entry *cur;
3307 for( cur = sni_info; cur != NULL; cur = cur->next )
Gilles Peskine166ce742018-04-30 10:30:49 +02003308 {
Gilles Peskinee2479892018-06-13 18:06:51 +02003309 ret = ssl_async_set_key( &ssl_async_keys,
Gilles Peskine44817442018-06-13 18:09:28 +02003310 cur->cert, cur->key, 1,
Gilles Peskinee2479892018-06-13 18:06:51 +02003311 opt.async_private_delay2 );
3312 if( ret < 0 )
3313 {
3314 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
3315 ret );
3316 goto exit;
3317 }
3318 cur->key = NULL;
Gilles Peskine166ce742018-04-30 10:30:49 +02003319 }
Gilles Peskine166ce742018-04-30 10:30:49 +02003320 }
3321#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3322 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003323#endif
3324
Hanno Beckere6706e62017-05-15 16:05:15 +01003325#if defined(MBEDTLS_ECP_C)
3326 if( opt.curves != NULL &&
3327 strcmp( opt.curves, "default" ) != 0 )
3328 {
3329 mbedtls_ssl_conf_curves( &conf, curve_list );
3330 }
3331#endif
3332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003333#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003334
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02003335 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
3336 {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003337#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00003338 if( opt.psk_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003339 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00003340 /* The algorithm has already been determined earlier. */
Janos Follathbe4efc22019-08-08 11:38:18 +01003341 status = psa_setup_psk_key_slot( &psk_slot, alg, psk, psk_len );
Hanno Becker1d911cd2018-11-15 13:06:09 +00003342 if( status != PSA_SUCCESS )
3343 {
3344 fprintf( stderr, "SETUP FAIL\n" );
3345 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
3346 goto exit;
3347 }
3348 if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, psk_slot,
3349 (const unsigned char *) opt.psk_identity,
3350 strlen( opt.psk_identity ) ) ) != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003351 {
3352 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
3353 ret );
3354 goto exit;
3355 }
3356 }
3357 else
3358#endif /* MBEDTLS_USE_PSA_CRYPTO */
3359 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
3360 (const unsigned char *) opt.psk_identity,
3361 strlen( opt.psk_identity ) ) ) != 0 )
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02003362 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003363 mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02003364 goto exit;
3365 }
3366 }
3367
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02003368 if( opt.psk_list != NULL )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003369 {
3370#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00003371 if( opt.psk_list_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003372 {
3373 psk_entry *cur_psk;
3374 for( cur_psk = psk_info; cur_psk != NULL; cur_psk = cur_psk->next )
3375 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00003376
Janos Follathbe4efc22019-08-08 11:38:18 +01003377 status = psa_setup_psk_key_slot( &cur_psk->slot, alg,
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003378 cur_psk->key,
3379 cur_psk->key_len );
3380 if( status != PSA_SUCCESS )
3381 {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003382 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
3383 goto exit;
3384 }
3385 }
3386 }
3387#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker1d911cd2018-11-15 13:06:09 +00003388
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003389 mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003390 }
Paul Bakkered27a042013-04-18 22:46:23 +02003391#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393#if defined(MBEDTLS_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00003394 /*
3395 * Use different group than default DHM group
3396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003398 if( opt.dhm_file != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003399 ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003400#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003401 if( ret != 0 )
3402 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003403 mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003404 goto exit;
3405 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003406#endif
3407
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02003408 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003409 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00003410
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02003411 if( opt.max_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003412 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakkerc1516be2013-06-29 16:01:32 +02003413
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003414 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
3415 {
3416 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
3417 goto exit;
3418 }
3419
Hanno Beckerdcc94e62019-07-03 17:05:43 +01003420 io_ctx.ssl = &ssl;
3421 io_ctx.net = &client_fd;
3422 mbedtls_ssl_set_bio( &ssl, &io_ctx, send_cb, recv_cb,
3423 opt.nbio == 0 ? recv_timeout_cb : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003424
Hanno Beckera0e20d02019-05-15 14:03:01 +01003425#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +01003426 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3427 {
3428 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
3429 cid, cid_len ) ) != 0 )
3430 {
3431 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
3432 ret );
3433 goto exit;
3434 }
3435 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003436#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +01003437
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02003438#if defined(MBEDTLS_SSL_PROTO_DTLS)
3439 if( opt.dtls_mtu != DFL_DTLS_MTU )
3440 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
3441#endif
3442
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003443#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02003444 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
3445 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003446#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02003447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003449
3450reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02003451#if !defined(_WIN32)
3452 if( received_sigterm )
3453 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01003454 mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" );
3455 if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT )
3456 ret = 0;
3457
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02003458 goto exit;
3459 }
3460#endif
3461
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003462 if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
3463 {
3464 mbedtls_printf( " ! Client initiated reconnection from same port\n" );
3465 goto handshake;
3466 }
3467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003469 if( ret != 0 )
3470 {
3471 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 mbedtls_strerror( ret, error_buf, 100 );
3473 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003474 }
3475#endif
3476
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003477 mbedtls_net_free( &client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01003478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 mbedtls_ssl_session_reset( &ssl );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003480
3481 /*
3482 * 3. Wait until a client connects
3483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 mbedtls_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003485 fflush( stdout );
3486
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003487 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02003488 client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003489 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02003490#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003491 if( received_sigterm )
3492 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01003493 mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" );
3494 if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED )
3495 ret = 0;
3496
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003497 goto exit;
3498 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02003499#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501 mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003502 goto exit;
3503 }
3504
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003505 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003506 ret = mbedtls_net_set_nonblock( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003507 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003508 ret = mbedtls_net_set_block( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003509 if( ret != 0 )
3510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003512 goto exit;
3513 }
3514
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003515 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003517#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
3518 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003519 {
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02003520 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
3521 client_ip, cliip_len ) ) != 0 )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003522 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01003523 mbedtls_printf( " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n",
3524 -ret );
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003525 goto exit;
3526 }
3527 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003529
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02003530#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3531 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
3532 {
3533 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
3534 (const unsigned char *) opt.ecjpake_pw,
3535 strlen( opt.ecjpake_pw ) ) ) != 0 )
3536 {
3537 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
3538 goto exit;
3539 }
3540 }
3541#endif
3542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003543 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003544
3545 /*
3546 * 4. Handshake
3547 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003548handshake:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003550 fflush( stdout );
3551
Hanno Becker16970d22017-10-10 15:56:37 +01003552 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003553 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003554#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003555 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003556 ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003557 {
3558 mbedtls_printf( " cancelling on injected error\n" );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003559 break;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003560 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003561#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskineb44692f2018-04-24 12:18:19 +02003562
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003563 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003564 break;
3565
3566 /* For event-driven IO, wait for socket to become available */
3567 if( opt.event == 1 /* level triggered IO */ )
3568 {
3569#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003570 ret = idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003571#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003572 ret = idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003573#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003574 if( ret != 0 )
3575 goto reset;
Hanno Becker16970d22017-10-10 15:56:37 +01003576 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003577 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581 mbedtls_printf( " hello verification requested\n" );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003582 ret = 0;
3583 goto reset;
3584 }
3585 else if( ret != 0 )
3586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02003588
3589#if defined(MBEDTLS_X509_CRT_PARSE_C)
3590 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3591 {
3592 char vrfy_buf[512];
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02003593 flags = mbedtls_ssl_get_verify_result( &ssl );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02003594
3595 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
3596
3597 mbedtls_printf( "%s\n", vrfy_buf );
3598 }
3599#endif
3600
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003601#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003602 if( opt.async_private_error < 0 )
3603 /* Injected error only the first time round, to test reset */
3604 ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
3605#endif
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003606 goto reset;
3607 }
3608 else /* ret == 0 */
3609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
3611 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003612 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
3615 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02003616 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02003618
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003619#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3620 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
3621 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
3622#endif
3623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003625 if( opt.alpn_string != NULL )
3626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
3628 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003629 alp ? alp : "(none)" );
3630 }
3631#endif
3632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003634 /*
Ron Eldor2a47be52017-06-20 15:23:23 +03003635 * 5. Verify the client certificate
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003638
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003639 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003640 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003641 char vrfy_buf[512];
3642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 mbedtls_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003644
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003645 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003646
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003647 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003648 }
3649 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003651
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003652 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003653 {
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003654 char crt_buf[512];
3655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656 mbedtls_printf( " . Peer certificate information ...\n" );
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003657 mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658 mbedtls_ssl_get_peer_cert( &ssl ) );
Manuel Pégourié-Gonnard67557172015-07-02 11:15:48 +02003659 mbedtls_printf( "%s\n", crt_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003660 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003662
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003663#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Ron Eldor51d3ab52019-05-12 14:54:30 +03003664 if( opt.eap_tls != 0 )
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003665 {
3666 size_t j = 0;
Ron Eldor51d3ab52019-05-12 14:54:30 +03003667
3668 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type,
3669 eap_tls_keying.master_secret,
3670 sizeof( eap_tls_keying.master_secret ),
3671 eap_tls_label,
3672 eap_tls_keying.randbytes,
3673 sizeof( eap_tls_keying.randbytes ),
3674 eap_tls_keymaterial,
3675 sizeof( eap_tls_keymaterial ) ) )
3676 != 0 )
3677 {
3678 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
3679 -ret );
3680 goto exit;
3681 }
3682
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003683 mbedtls_printf( " EAP-TLS key material is:" );
3684 for( j = 0; j < sizeof( eap_tls_keymaterial ); j++ )
3685 {
3686 if( j % 8 == 0 )
3687 mbedtls_printf("\n ");
3688 mbedtls_printf("%02x ", eap_tls_keymaterial[j] );
3689 }
3690 mbedtls_printf("\n");
3691
Ron Eldor51d3ab52019-05-12 14:54:30 +03003692 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type, NULL, 0,
Ron Eldor51c45072019-05-15 17:49:54 +03003693 eap_tls_label,
3694 eap_tls_keying.randbytes,
3695 sizeof( eap_tls_keying.randbytes ),
3696 eap_tls_iv,
3697 sizeof( eap_tls_iv ) ) ) != 0 )
Ron Eldor51d3ab52019-05-12 14:54:30 +03003698 {
3699 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
3700 -ret );
3701 goto exit;
3702 }
3703
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003704 mbedtls_printf( " EAP-TLS IV is:" );
3705 for( j = 0; j < sizeof( eap_tls_iv ); j++ )
3706 {
3707 if( j % 8 == 0 )
3708 mbedtls_printf("\n ");
3709 mbedtls_printf("%02x ", eap_tls_iv[j] );
3710 }
3711 mbedtls_printf("\n");
3712 }
3713#endif
Hanno Beckera7d25422019-04-09 17:28:10 +01003714
Hanno Beckera0e20d02019-05-15 14:03:01 +01003715#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003716 ret = report_cid_usage( &ssl, "initial handshake" );
3717 if( ret != 0 )
3718 goto exit;
3719
Hanno Beckera7d25422019-04-09 17:28:10 +01003720 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3721 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003722 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
3723 cid_renego, cid_renego_len ) ) != 0 )
Hanno Beckera7d25422019-04-09 17:28:10 +01003724 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003725 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
3726 ret );
Hanno Beckera7d25422019-04-09 17:28:10 +01003727 goto exit;
3728 }
Hanno Beckera7d25422019-04-09 17:28:10 +01003729 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003730#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +01003731
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02003732 if( opt.exchanges == 0 )
3733 goto close_notify;
3734
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003735 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003736data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003737 /*
3738 * 6. Read the HTTP Request
3739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740 mbedtls_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003741 fflush( stdout );
3742
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003743 /*
3744 * TLS and DTLS need different reading styles (stream vs datagram)
3745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003746 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003747 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003748 do
3749 {
3750 int terminated = 0;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003751 len = opt.buffer_size - 1;
3752 memset( buf, 0, opt.buffer_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003753 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003754
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003755 if( mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003756 {
3757 if( opt.event == 1 /* level triggered IO */ )
3758 {
3759#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003760 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003761#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003762 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003763#endif
3764 }
3765
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003766 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01003767 }
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003768
3769 if( ret <= 0 )
3770 {
3771 switch( ret )
3772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3774 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003775 goto close_notify;
3776
3777 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003778 case MBEDTLS_ERR_NET_CONN_RESET:
3779 mbedtls_printf( " connection was reset by peer\n" );
3780 ret = MBEDTLS_ERR_NET_CONN_RESET;
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003781 goto reset;
3782
3783 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003784 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003785 goto reset;
3786 }
3787 }
3788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003789 if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003790 {
3791 len = ret;
3792 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003793 mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003794
3795 /* End of message should be detected according to the syntax of the
3796 * application protocol (eg HTTP), just use a dummy test here. */
3797 if( buf[len - 1] == '\n' )
3798 terminated = 1;
3799 }
3800 else
3801 {
3802 int extra_len, ori_len;
3803 unsigned char *larger_buf;
3804
3805 ori_len = ret;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02003806 extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003807
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003808 larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003809 if( larger_buf == NULL )
3810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811 mbedtls_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003812 ret = 1;
3813 goto reset;
3814 }
3815
3816 memset( larger_buf, 0, ori_len + extra_len );
3817 memcpy( larger_buf, buf, ori_len );
3818
3819 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820 ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003821 if( ret != extra_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822 mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824 mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003825 ret = 1;
3826 goto reset;
3827 }
3828
3829 larger_buf[ori_len + extra_len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830 mbedtls_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003831 ori_len + extra_len, ori_len, extra_len,
3832 (char *) larger_buf );
3833
3834 /* End of message should be detected according to the syntax of the
3835 * application protocol (eg HTTP), just use a dummy test here. */
3836 if( larger_buf[ori_len + extra_len - 1] == '\n' )
3837 terminated = 1;
3838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003839 mbedtls_free( larger_buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003840 }
3841
3842 if( terminated )
3843 {
3844 ret = 0;
3845 break;
3846 }
3847 }
3848 while( 1 );
3849 }
3850 else /* Not stream, so datagram */
3851 {
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003852 len = opt.buffer_size - 1;
3853 memset( buf, 0, opt.buffer_size );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003854
Gilles Peskineb44692f2018-04-24 12:18:19 +02003855 do
Hanno Becker16970d22017-10-10 15:56:37 +01003856 {
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003857 /* Without the call to `mbedtls_ssl_check_pending`, it might
3858 * happen that the client sends application data in the same
3859 * datagram as the Finished message concluding the handshake.
3860 * In this case, the application data would be ready to be
3861 * processed while the underlying transport wouldn't signal
3862 * any further incoming data.
3863 *
3864 * See the test 'Event-driven I/O: session-id resume, UDP packing'
3865 * in tests/ssl-opt.sh.
3866 */
3867
3868 /* For event-driven IO, wait for socket to become available */
3869 if( mbedtls_ssl_check_pending( &ssl ) == 0 &&
3870 opt.event == 1 /* level triggered IO */ )
3871 {
3872#if defined(MBEDTLS_TIMING_C)
3873 idle( &client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ );
3874#else
3875 idle( &client_fd, MBEDTLS_ERR_SSL_WANT_READ );
3876#endif
3877 }
3878
Hanno Becker16970d22017-10-10 15:56:37 +01003879 ret = mbedtls_ssl_read( &ssl, buf, len );
3880
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003881 /* Note that even if `mbedtls_ssl_check_pending` returns true,
3882 * it can happen that the subsequent call to `mbedtls_ssl_read`
3883 * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
3884 * might be discarded (e.g. because they are retransmissions). */
Hanno Becker16970d22017-10-10 15:56:37 +01003885 }
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003886 while( mbedtls_status_is_ssl_in_progress( ret ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003887
3888 if( ret <= 0 )
3889 {
3890 switch( ret )
3891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3893 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003894 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003895 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003896
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003897 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003899 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003900 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003901 }
3902
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003903 len = ret;
3904 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003906 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003907 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003908
3909 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003910 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02003911 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003912 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003913#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00003914 if( opt.renegotiate && exchanges_left == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916 mbedtls_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003917 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003920 {
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003921 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003924 goto reset;
3925 }
Hanno Becker16970d22017-10-10 15:56:37 +01003926
3927 /* For event-driven IO, wait for socket to become available */
3928 if( opt.event == 1 /* level triggered IO */ )
3929 {
3930#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003931 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003932#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003933 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003934#endif
3935 }
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003936 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003939 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003940#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003941
Hanno Beckera0e20d02019-05-15 14:03:01 +01003942#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003943 ret = report_cid_usage( &ssl, "after renegotiation" );
3944 if( ret != 0 )
3945 goto exit;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003946#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003947
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003948 /*
3949 * 7. Write the 200 Response
3950 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003951 mbedtls_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003952 fflush( stdout );
3953
3954 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003956
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003957 /* Add padding to the response to reach opt.response_size in length */
3958 if( opt.response_size != DFL_RESPONSE_SIZE &&
3959 len < opt.response_size )
3960 {
3961 memset( buf + len, 'B', opt.response_size - len );
3962 len += opt.response_size - len;
3963 }
3964
3965 /* Truncate if response size is smaller than the "natural" size */
3966 if( opt.response_size != DFL_RESPONSE_SIZE &&
3967 len > opt.response_size )
3968 {
3969 len = opt.response_size;
3970
3971 /* Still end with \r\n unless that's really not possible */
3972 if( len >= 2 ) buf[len - 2] = '\r';
3973 if( len >= 1 ) buf[len - 1] = '\n';
3974 }
3975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003977 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003978 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003980 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003981 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985 mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003986 goto reset;
3987 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003988
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003989 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003992 goto reset;
3993 }
Hanno Becker16970d22017-10-10 15:56:37 +01003994
3995 /* For event-driven IO, wait for socket to become available */
3996 if( opt.event == 1 /* level triggered IO */ )
3997 {
3998#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003999 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01004000#else
Hanno Becker197a91c2017-10-31 10:58:53 +00004001 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01004002#endif
4003 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02004004 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004005 }
4006 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004007 else /* Not stream, so datagram */
4008 {
Hanno Becker16970d22017-10-10 15:56:37 +01004009 while( 1 )
4010 {
4011 ret = mbedtls_ssl_write( &ssl, buf, len );
4012
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02004013 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01004014 break;
4015
4016 /* For event-driven IO, wait for socket to become available */
4017 if( opt.event == 1 /* level triggered IO */ )
4018 {
4019#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00004020 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01004021#else
Hanno Becker197a91c2017-10-31 10:58:53 +00004022 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01004023#endif
4024 }
4025 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004026
4027 if( ret < 0 )
4028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004029 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02004030 goto reset;
4031 }
4032
4033 frags = 1;
4034 written = ret;
4035 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004036
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02004037 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038 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 +01004039 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01004040
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02004041 /*
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004042 * 7b. Simulate serialize/deserialize and go back to data exchange
4043 */
Jarno Lamsabbc7b412019-06-04 11:06:31 +03004044#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004045 if( opt.serialize != 0 )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004046 {
Jarno Lamsa15b3a7a2019-06-06 15:10:07 +03004047 size_t buf_len;
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004048
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004049 mbedtls_printf( " . Serializing live connection..." );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004050
Jarno Lamsa15b3a7a2019-06-06 15:10:07 +03004051 ret = mbedtls_ssl_context_save( &ssl, NULL, 0, &buf_len );
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004052 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004053 {
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004054 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
4055 "-0x%x\n\n", -ret );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004056
4057 goto exit;
4058 }
4059
Jarno Lamsa15b3a7a2019-06-06 15:10:07 +03004060 if( ( context_buf = mbedtls_calloc( 1, buf_len ) ) == NULL )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004061 {
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004062 mbedtls_printf( " failed\n ! Couldn't allocate buffer for "
4063 "serialized context" );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004064
4065 goto exit;
4066 }
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02004067 context_buf_len = buf_len;
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004068
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004069 if( ( ret = mbedtls_ssl_context_save( &ssl, context_buf,
4070 buf_len, &buf_len ) ) != 0 )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004071 {
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004072 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004073 "-0x%x\n\n", -ret );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004074
4075 goto exit;
4076 }
4077
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004078 mbedtls_printf( " ok\n" );
4079
4080 /*
4081 * This simulates a workflow where you have a long-lived server
4082 * instance, potentially with a pool of ssl_context objects, and you
4083 * just want to re-use one while the connection is inactive: in that
4084 * case you can just reset() it, and then it's ready to receive
4085 * serialized data from another connection (or the same here).
4086 */
4087 if( opt.serialize == 1 )
4088 {
Manuel Pégourié-Gonnard9df5a822019-07-23 14:51:09 +02004089 /* nothing to do here, done by context_save() already */
4090 mbedtls_printf( " . Context has been reset... ok" );
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004091 }
4092
4093 /*
4094 * This simulates a workflow where you have one server instance per
4095 * connection, and want to release it entire when the connection is
4096 * inactive, and spawn it again when needed again - this would happen
4097 * between ssl_free() and ssl_init() below, together with any other
4098 * teardown/startup code needed - for example, preparing the
4099 * ssl_config again (see section 3 "setup stuff" in this file).
4100 */
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004101 if( opt.serialize == 2 )
4102 {
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004103 mbedtls_printf( " . Freeing and reinitializing context..." );
4104
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004105 mbedtls_ssl_free( &ssl );
4106
4107 mbedtls_ssl_init( &ssl );
4108
4109 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
4110 {
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004111 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned "
4112 "-0x%x\n\n", -ret );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004113 goto exit;
4114 }
4115
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004116 /*
4117 * This illustrates the minimum amount of things you need to set
4118 * up, however you could set up much more if desired, for example
4119 * if you want to share your set up code between the case of
4120 * establishing a new connection and this case.
4121 */
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004122 if( opt.nbio == 2 )
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004123 mbedtls_ssl_set_bio( &ssl, &client_fd, delayed_send,
4124 delayed_recv, NULL );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004125 else
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004126 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send,
4127 mbedtls_net_recv,
4128 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004129
Jarno Lamsa8e253212019-06-13 11:45:06 +03004130#if defined(MBEDTLS_TIMING_C)
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004131 mbedtls_ssl_set_timer_cb( &ssl, &timer,
4132 mbedtls_timing_set_delay,
4133 mbedtls_timing_get_delay );
Jarno Lamsa8e253212019-06-13 11:45:06 +03004134#endif /* MBEDTLS_TIMING_C */
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004135
4136 mbedtls_printf( " ok\n" );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03004137 }
4138
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004139 mbedtls_printf( " . Deserializing connection..." );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004140
Jarno Lamsaddf72a12019-06-13 12:22:50 +03004141 if( ( ret = mbedtls_ssl_context_load( &ssl, context_buf,
4142 buf_len ) ) != 0 )
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004143 {
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004144 mbedtls_printf( "failed\n ! mbedtls_ssl_context_load returned "
4145 "-0x%x\n\n", -ret );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004146
4147 goto exit;
4148 }
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004149
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02004150 mbedtls_free( context_buf );
4151 context_buf = NULL;
4152 context_buf_len = 0;
4153
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02004154 mbedtls_printf( " ok\n" );
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004155 }
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03004156#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jarno Lamsa5a3a16c2019-05-29 15:41:21 +03004157
4158 /*
4159 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02004160 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00004161 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02004162 goto data_exchange;
4163
4164 /*
4165 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02004166 */
4167close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004168 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01004169
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02004170 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004171 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01004172 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02004173 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02004174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004175 mbedtls_printf( " done\n" );
Rich Evansf90016a2015-01-19 14:26:37 +00004176
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004177 goto reset;
4178
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02004179 /*
4180 * Cleanup and exit
4181 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004182exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004184 if( ret != 0 )
4185 {
4186 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187 mbedtls_strerror( ret, error_buf, 100 );
4188 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004189 }
4190#endif
4191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192 mbedtls_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01004193 fflush( stdout );
4194
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02004195 mbedtls_net_free( &client_fd );
4196 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +02004197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
4199 mbedtls_dhm_free( &dhm );
Paul Bakkera317a982014-06-18 16:44:11 +02004200#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004201#if defined(MBEDTLS_X509_CRT_PARSE_C)
4202 mbedtls_x509_crt_free( &cacert );
4203 mbedtls_x509_crt_free( &srvcert );
4204 mbedtls_pk_free( &pkey );
4205 mbedtls_x509_crt_free( &srvcert2 );
4206 mbedtls_pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02004207#endif
Gilles Peskine44817442018-06-13 18:09:28 +02004208#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
4209 for( i = 0; (size_t) i < ssl_async_keys.slots_used; i++ )
4210 {
4211 if( ssl_async_keys.slots[i].pk_owned )
4212 {
4213 mbedtls_pk_free( ssl_async_keys.slots[i].pk );
4214 mbedtls_free( ssl_async_keys.slots[i].pk );
4215 ssl_async_keys.slots[i].pk = NULL;
4216 }
4217 }
4218#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02004219#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01004220 sni_free( sni_info );
4221#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004223 if( ( ret = psk_free( psk_info ) ) != 0 )
4224 mbedtls_printf( "Failed to list of opaque PSKs - error was %d\n", ret );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02004225#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004226#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
4227 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02004228#endif
Paul Bakkered27a042013-04-18 22:46:23 +02004229
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004230#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
4231 defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00004232 if( opt.psk_opaque != 0 )
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004233 {
4234 /* This is ok even if the slot hasn't been
4235 * initialized (we might have jumed here
4236 * immediately because of bad cmd line params,
4237 * for example). */
Hanno Becker1d911cd2018-11-15 13:06:09 +00004238 status = psa_destroy_key( psk_slot );
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004239 if( status != PSA_SUCCESS )
4240 {
4241 mbedtls_printf( "Failed to destroy key slot %u - error was %d",
Hanno Becker1d911cd2018-11-15 13:06:09 +00004242 (unsigned) psk_slot, (int) status );
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00004243 }
4244 }
4245#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED &&
4246 MBEDTLS_USE_PSA_CRYPTO */
4247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004248 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02004249 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004250 mbedtls_ctr_drbg_free( &ctr_drbg );
4251 mbedtls_entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253#if defined(MBEDTLS_SSL_CACHE_C)
4254 mbedtls_ssl_cache_free( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00004255#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004256#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4257 mbedtls_ssl_ticket_free( &ticket_ctx );
4258#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259#if defined(MBEDTLS_SSL_COOKIE_C)
4260 mbedtls_ssl_cookie_free( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02004261#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004262
Hanno Becker095d9cf2018-10-09 12:39:13 +01004263 mbedtls_free( buf );
4264
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02004265#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
4266 if( context_buf != NULL )
4267 mbedtls_platform_zeroize( context_buf, context_buf_len );
4268 mbedtls_free( context_buf );
4269#endif
4270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
4272#if defined(MBEDTLS_MEMORY_DEBUG)
4273 mbedtls_memory_buffer_alloc_status();
Paul Bakker82024bf2013-07-04 11:52:32 +02004274#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004275 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +02004276#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02004277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278 mbedtls_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01004279
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004280#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004282 fflush( stdout ); getchar();
4283#endif
4284
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02004285 // Shell can not handle large exit numbers -> 1 for errors
4286 if( ret < 0 )
4287 ret = 1;
4288
Paul Bakkerb60b95f2012-09-25 09:05:17 +00004289 return( ret );
4290}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004291#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
4292 MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02004293 MBEDTLS_CTR_DRBG_C */