blob: d32d3aaa6564dd5b4c332dff38ab0de8a669b2c8 [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>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_free free
Simon Butcherd3138c32016-04-27 01:26:50 +010034#define mbedtls_time time
35#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020036#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define mbedtls_fprintf fprintf
38#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010039#define mbedtls_exit exit
40#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
41#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evans18b78c72015-02-11 14:06:19 +000042#endif
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020043
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020044#if !defined(MBEDTLS_ENTROPY_C) || \
Hanno Becker7f1c8052019-08-26 13:30:13 +010045 !defined(MBEDTLS_SSL_TLS_C) || \
46 !defined(MBEDTLS_SSL_SRV_C) || \
47 !defined(MBEDTLS_NET_C) || \
48 !( defined(MBEDTLS_CTR_DRBG_C) || defined(MBEDTLS_HMAC_DRBG_C) )
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020049int main( void )
50{
51 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
52 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
Hanno Becker7f1c8052019-08-26 13:30:13 +010053 "MBEDTLS_NET_C not defined, or "
54 "neither MBEDTLS_CTR_DRBG_C nor MBEDTLS_HMAC_DRBG_C defined.\n");
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020055 return( 0 );
56}
57#else
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010058
Andres AG788aa4a2016-09-14 14:32:09 +010059#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/ssl.h"
Hanno Becker473f98f2019-06-26 10:27:32 +010061#include "mbedtls/ssl_ciphersuites.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/entropy.h"
63#include "mbedtls/ctr_drbg.h"
Hanno Becker7f1c8052019-08-26 13:30:13 +010064#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/certs.h"
66#include "mbedtls/x509.h"
67#include "mbedtls/error.h"
68#include "mbedtls/debug.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020069#include "mbedtls/timing.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000070
Rich Evans18b78c72015-02-11 14:06:19 +000071#include <stdio.h>
72#include <stdlib.h>
73#include <string.h>
Andres AG692ad842017-01-19 16:30:57 +000074#include <stdint.h>
Andres AG0b736db2017-02-08 14:05:57 +000075
76#if !defined(_MSC_VER)
Andres AG692ad842017-01-19 16:30:57 +000077#include <inttypes.h>
Andres AG0b736db2017-02-08 14:05:57 +000078#endif
Rich Evans18b78c72015-02-11 14:06:19 +000079
80#if !defined(_WIN32)
81#include <signal.h>
82#endif
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000085#include "mbedtls/ssl_cache.h"
Paul Bakker0a597072012-09-25 21:55:46 +000086#endif
87
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020088#if defined(MBEDTLS_SSL_TICKET_C)
89#include "mbedtls/ssl_ticket.h"
90#endif
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000093#include "mbedtls/ssl_cookie.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020094#endif
95
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000097#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker82024bf2013-07-04 11:52:32 +020098#endif
99
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200100#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO)
101#define SNI_OPTION
102#endif
103
104#if defined(_WIN32)
105#include <windows.h>
106#endif
107
Simon Butchercce68be2018-07-23 14:26:09 +0100108/* Size of memory to be allocated for the heap, when using the library's memory
109 * management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */
110#define MEMORY_HEAP_SIZE 120000
111
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100112#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200113#define DFL_SERVER_PORT "4433"
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200114#define DFL_RESPONSE_SIZE -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000115#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100116#define DFL_NBIO 0
Hanno Becker16970d22017-10-10 15:56:37 +0100117#define DFL_EVENT 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200118#define DFL_READ_TIMEOUT 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000119#define DFL_CA_FILE ""
120#define DFL_CA_PATH ""
121#define DFL_CRT_FILE ""
122#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200123#define DFL_CRT_FILE2 ""
124#define DFL_KEY_FILE2 ""
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100125#define DFL_ASYNC_OPERATIONS "-"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100126#define DFL_ASYNC_PRIVATE_DELAY1 ( -1 )
127#define DFL_ASYNC_PRIVATE_DELAY2 ( -1 )
Gilles Peskine3665f1d2018-01-05 21:22:12 +0100128#define DFL_ASYNC_PRIVATE_ERROR ( 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +0200129#define DFL_PSK ""
130#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200131#define DFL_ECJPAKE_PW NULL
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200132#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000133#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200134#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100136#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100137#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200138#define DFL_RENEGO_DELAY -2
Andres AG692ad842017-01-19 16:30:57 +0000139#define DFL_RENEGO_PERIOD ( (uint64_t)-1 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200140#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200141#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200142#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000143#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +0200144#define DFL_SHA1 -1
Hanno Becker1029ace2019-04-09 17:28:10 +0100145#define DFL_CID_ENABLED 0
146#define DFL_CID_VALUE ""
Hanno Becker96870292019-05-03 17:30:59 +0100147#define DFL_CID_ENABLED_RENEGO -1
148#define DFL_CID_VALUE_RENEGO NULL
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100149#define DFL_AUTH_MODE -1
Janos Follath4817e272017-04-10 13:44:33 +0100150#define DFL_CERT_REQ_CA_LIST MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100152#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200154#define DFL_TICKET_TIMEOUT 86400
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100155#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100156#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100157#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200158#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100159#define DFL_CURVES NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200160#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200162#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200163#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200164#define DFL_HS_TO_MIN 0
165#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200166#define DFL_DTLS_MTU -1
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200167#define DFL_BADMAC_LIMIT -1
Hanno Beckere7675d02018-08-14 13:28:56 +0100168#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200169#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100170#define DFL_ETM -1
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +0300171#define DFL_SERIALIZE 0
Jarno Lamsa41b35912019-06-10 15:51:11 +0300172#define DFL_EXTENDED_MS_ENFORCE -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000173
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200174#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
175 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
176 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
177 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
178 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
179 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
180 "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 +0200181
Paul Bakker8e714d72013-07-18 11:05:13 +0200182/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
183 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000184#define HTTP_RESPONSE \
185 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000186 "<h2>mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200187 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000188
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200189/*
190 * Size of the basic I/O buffer. Able to hold our default response.
191 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 * 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 +0200193 * if you change this value to something outside the range <= 100 or > 500
194 */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200195#define DFL_IO_BUF_LEN 200
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_X509_CRT_PARSE_C)
198#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000199#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100200 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
201 " default: \"\" (pre-loaded)\n" \
Hanno Becker1ce1a512019-05-01 11:16:28 +0100202 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100203 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
204 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
Hanno Becker1ce1a512019-05-01 11:16:28 +0100205 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100206 " 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 +0200207 " default: see note after key_file2\n" \
208 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200209 " 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 +0200210 " default: see note after key_file2\n" \
211 " key_file2=%%s default: see note below\n" \
212 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200213 " preloaded certificate(s) and key(s) are used if available\n" \
214 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
215 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000216#else
217#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200218 "\n" \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 " No file operations available (MBEDTLS_FS_IO not defined)\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200220 "\n"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200222#else
223#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200225
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200226#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100227#define USAGE_SSL_ASYNC \
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100228 " async_operations=%%c... d=decrypt, s=sign (default: -=off)\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100229 " async_private_delay1=%%d Asynchronous delay for key_file or preloaded key\n" \
Gilles Peskine166ce742018-04-30 10:30:49 +0200230 " async_private_delay2=%%d Asynchronous delay for key_file2 and sni\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100231 " default: -1 (not asynchronous)\n" \
Gilles Peskine60ee4ca2018-01-08 11:28:05 +0100232 " async_private_error=%%d Async callback error injection (default=0=none,\n" \
Gilles Peskinec9125722018-04-26 07:15:40 +0200233 " 1=start, 2=cancel, 3=resume, negative=first time only)"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100234#else
235#define USAGE_SSL_ASYNC ""
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200236#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100237
Hanno Beckera5a2b082019-05-15 14:03:01 +0100238#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1029ace2019-04-09 17:28:10 +0100239#define USAGE_CID \
240 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
241 " default: 0 (disabled)\n" \
Hanno Becker96870292019-05-03 17:30:59 +0100242 " cid_renego=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension during renegotiation.\n" \
Hanno Beckerc8f43d82019-05-23 17:01:06 +0100243 " default: same as 'cid' parameter\n" \
Hanno Becker1029ace2019-04-09 17:28:10 +0100244 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
Hanno Becker96870292019-05-03 17:30:59 +0100245 " default: \"\"\n" \
246 " cid_val_renego=%%s The CID to use for incoming messages (in hex, without 0x) after renegotiation.\n" \
Hanno Beckerc8f43d82019-05-23 17:01:06 +0100247 " default: same as 'cid_val' parameter\n"
Hanno Beckera5a2b082019-05-15 14:03:01 +0100248#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1029ace2019-04-09 17:28:10 +0100249#define USAGE_CID ""
Hanno Beckera5a2b082019-05-15 14:03:01 +0100250#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1029ace2019-04-09 17:28:10 +0100251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd0d01c52018-10-25 16:49:48 +0100253#define USAGE_PSK \
254 " psk=%%s default: \"\" (in hex, without 0x)\n" \
255 " psk_list=%%s default: \"\"\n" \
Hanno Becker5ddc0632018-10-26 10:04:13 +0100256 " A list of (PSK identity, PSK value) pairs.\n" \
257 " The PSK values are in hex, without 0x.\n" \
Hanno Beckerd0d01c52018-10-25 16:49:48 +0100258 " id1,psk1[,id2,psk2[,...]]\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200259 " psk_identity=%%s default: \"Client_identity\"\n"
260#else
261#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200265#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100266 " tickets=%%d default: 1 (enabled)\n" \
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200267 " ticket_timeout=%%d default: 86400 (one day)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200268#else
269#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100273#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100274 " cache_max=%%d default: cache default (50)\n" \
275 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100276#else
277#define USAGE_CACHE ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278#endif /* MBEDTLS_SSL_CACHE_C */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100279
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200280#if defined(SNI_OPTION)
Ron Eldor24eec792019-04-04 15:02:01 +0300281#if defined(MBEDTLS_X509_CRL_PARSE_C)
282#define SNI_CRL ",crl"
283#else
284#define SNI_CRL ""
285#endif
286
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100287#define USAGE_SNI \
Ron Eldor24eec792019-04-04 15:02:01 +0300288 " sni=%%s name1,cert1,key1,ca1"SNI_CRL",auth1[,...]\n" \
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200289 " default: disabled\n"
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100290#else
291#define USAGE_SNI ""
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200292#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200295#define USAGE_MAX_FRAG_LEN \
296 " max_frag_len=%%d default: 16384 (tls default)\n" \
297 " options: 512, 1024, 2048, 4096\n"
298#else
299#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100303#define USAGE_TRUNC_HMAC \
304 " trunc_hmac=%%d default: library default\n"
305#else
306#define USAGE_TRUNC_HMAC ""
307#endif
308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200310#define USAGE_ALPN \
311 " alpn=%%s default: \"\" (disabled)\n" \
312 " example: spdy/1,http/1.1\n"
313#else
314#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200318#define USAGE_COOKIES \
319 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200320 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200321#else
322#define USAGE_COOKIES ""
323#endif
324
Hanno Becker7f376f42019-06-12 16:20:48 +0100325#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
326 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200327#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200328 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200329#else
330#define USAGE_ANTI_REPLAY ""
331#endif
332
Hanno Beckerde671542019-06-12 16:30:46 +0100333#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
334 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200335#define USAGE_BADMAC_LIMIT \
336 " badmac_limit=%%d default: (library default: disabled)\n"
337#else
338#define USAGE_BADMAC_LIMIT ""
339#endif
340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200342#define USAGE_DTLS \
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +0100343 " dtls=%%d default: 0 (TLS) (if both enabled)\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200344 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200345 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Beckere7675d02018-08-14 13:28:56 +0100346 " mtu=%%d default: (library default: unlimited)\n" \
347 " dgram_packing=%%d default: 1 (allowed)\n" \
348 " allow or forbid packing of multiple\n" \
349 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200350#else
351#define USAGE_DTLS ""
352#endif
353
Hanno Beckerf765ce62019-06-21 13:17:14 +0100354#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
355 !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET) && \
356 !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200357#define USAGE_EMS \
Jarno Lamsa41b35912019-06-10 15:51:11 +0300358 " extended_ms=0/1 default: (library default: on)\n" \
359 " enforce_extended_master_secret=0/1 default: (library default: off)\n"
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200360#else
361#define USAGE_EMS ""
362#endif
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100365#define USAGE_ETM \
366 " etm=0/1 default: (library default: on)\n"
367#else
368#define USAGE_ETM ""
369#endif
370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100372#define USAGE_RENEGO \
373 " renegotiation=%%d default: 0 (disabled)\n" \
374 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100375 " renego_delay=%%d default: -2 (library default)\n" \
Andres AG692ad842017-01-19 16:30:57 +0000376 " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100377#else
378#define USAGE_RENEGO ""
379#endif
380
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200381#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
382#define USAGE_ECJPAKE \
383 " ecjpake_pw=%%s default: none (disabled)\n"
384#else
385#define USAGE_ECJPAKE ""
386#endif
387
Hanno Beckerc1096e72019-06-19 12:30:41 +0100388#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Hanno Beckere6706e62017-05-15 16:05:15 +0100389#define USAGE_CURVES \
390 " curves=a,b,c,d default: \"default\" (library default)\n" \
391 " example: \"secp521r1,brainpoolP512r1\"\n" \
392 " - use \"none\" for empty list\n" \
393 " - see mbedtls_ecp_curve_list()\n" \
394 " for acceptable curve names\n"
395#else
396#define USAGE_CURVES ""
397#endif
398
Jarno Lamsacf1b6722019-06-04 11:06:31 +0300399#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
400#define USAGE_SERIALIZATION \
Jarno Lamsab5ff6a42019-06-06 10:40:52 +0300401 " serialize=%%d default: 0 (do not serialize/deserialize)\n" \
Jarno Lamsa85c23802019-06-07 08:39:24 +0300402 " options: 1 (serialize)\n" \
403 " 2 (serialize with re-initialization)\n"
Jarno Lamsacf1b6722019-06-04 11:06:31 +0300404#else
405#define USAGE_SERIALIZATION ""
406#endif
407
Hanno Beckeracd4fc02019-06-12 16:40:50 +0100408#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
409#define USAGE_AUTH_MODE \
410 " auth_mode=%%s default: (library default: none)\n" \
411 " options: none, optional, required\n"
412#else
413#define USAGE_AUTH_MODE ""
414#endif
415
Hanno Beckerb0b2b672019-06-12 16:58:10 +0100416#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
417#define USAGE_ALLOW_LEGACY_RENEGO \
418 " allow_legacy=%%d default: (library default: no)\n"
419#else
420#define USAGE_ALLOW_LEGACY_RENEGO ""
421#endif
422
Hanno Becker1f835fa2019-06-13 10:14:59 +0100423#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
424#define USAGE_READ_TIMEOUT \
425 " read_timeout=%%d default: 0 ms (no timeout)\n"
426#else
427#define USAGE_READ_TIMEOUT ""
428#endif
429
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +0100430#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
431#define USAGE_CERT_REQ_CA_LIST \
432 " cert_req_ca_list=%%d default: 1 (send ca list)\n" \
433 " options: 1 (send ca list), 0 (don't send)\n"
434#else
435#define USAGE_CERT_REQ_CA_LIST ""
436#endif
437
Hanno Becker72e5ffc2019-07-05 11:35:08 +0100438#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
439 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) || \
440 !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
441 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
442#define USAGE_MAX_VERSION " max_version=%%s default: (library default: tls1_2)\n"
443#define USAGE_MIN_VERSION " min_version=%%s default: (library default: tls1)\n"
444#define USAGE_FORCE_VERSION " force_version=%%s default: \"\" (none)\n" \
445 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n"
446#else
447#define USAGE_MAX_VERSION ""
448#define USAGE_MIN_VERSION ""
449#define USAGE_FORCE_VERSION ""
450#endif
451
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000452#define USAGE \
453 "\n usage: ssl_server2 param=<>...\n" \
454 "\n acceptable parameters:\n" \
Ron Eldor71f68c42017-09-26 11:29:11 +0300455 " server_addr=%%s default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000456 " server_port=%%d default: 4433\n" \
457 " debug_level=%%d default: 0 (disabled)\n" \
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200458 " buffer_size=%%d default: 200 \n" \
459 " (minimum: 1, max: 16385)\n" \
460 " response_size=%%d default: about 152 (basic response)\n" \
461 " (minimum: 0, max: 16384)\n" \
462 " increases buffer_size if bigger\n"\
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100463 " nbio=%%d default: 0 (blocking I/O)\n" \
464 " options: 1 (non-blocking), 2 (added delays)\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100465 " event=%%d default: 0 (loop)\n" \
466 " options: 1 (level-triggered, implies nbio=1),\n" \
Hanno Becker1f835fa2019-06-13 10:14:59 +0100467 USAGE_READ_TIMEOUT \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100468 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200469 USAGE_DTLS \
470 USAGE_COOKIES \
471 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200472 USAGE_BADMAC_LIMIT \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200473 "\n" \
Hanno Beckeracd4fc02019-06-12 16:40:50 +0100474 USAGE_AUTH_MODE \
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +0100475 USAGE_CERT_REQ_CA_LIST \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000476 USAGE_IO \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100477 USAGE_SSL_ASYNC \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100478 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100479 "\n" \
480 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200481 USAGE_ECJPAKE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100482 "\n" \
Hanno Beckerb0b2b672019-06-12 16:58:10 +0100483 USAGE_ALLOW_LEGACY_RENEGO \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100484 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200485 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200486 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100487 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100488 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100489 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100490 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200491 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200492 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100493 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100494 USAGE_CURVES \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100495 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100496 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200497 " allow_sha1=%%d default: 0\n" \
Hanno Becker72e5ffc2019-07-05 11:35:08 +0100498 USAGE_MIN_VERSION \
499 USAGE_MAX_VERSION \
500 USAGE_FORCE_VERSION \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200501 "\n" \
502 " version_suites=a,b,c,d per-version ciphersuites\n" \
503 " in order from ssl3 to tls1_2\n" \
504 " default: all enabled\n" \
505 " force_ciphersuite=<name> default: all enabled\n" \
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100506 " query_config=<name> return 0 if the specified\n" \
507 " configuration macro is defined and 1\n" \
508 " otherwise. The expansion of the macro\n" \
509 " is printed if it is defined\n" \
Jarno Lamsacf1b6722019-06-04 11:06:31 +0300510 USAGE_SERIALIZATION \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000511 " acceptable ciphersuite names:\n"
512
Hanno Becker8651a432017-06-09 16:13:22 +0100513#define ALPN_LIST_SIZE 10
514#define CURVE_LIST_SIZE 20
515
Andres AG692ad842017-01-19 16:30:57 +0000516#define PUT_UINT64_BE(out_be,in_le,i) \
517{ \
518 (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \
519 (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \
520 (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \
521 (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \
522 (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \
523 (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \
524 (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \
525 (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \
526}
527
Simon Butcher63cb97e2018-12-06 17:43:31 +0000528
Rich Evans85b05ec2015-02-12 11:37:29 +0000529/*
530 * global options
531 */
532struct options
533{
534 const char *server_addr; /* address on which the ssl service runs */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200535 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000536 int debug_level; /* level of debugging */
537 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100538 int event; /* loop or event-driven IO? level or edge triggered? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200540 int response_size; /* pad response with header to requested size */
541 uint16_t buffer_size; /* IO buffer size */
Rich Evans85b05ec2015-02-12 11:37:29 +0000542 const char *ca_file; /* the file with the CA certificate(s) */
543 const char *ca_path; /* the path with the CA certificate(s) reside */
544 const char *crt_file; /* the file with the server certificate */
545 const char *key_file; /* the file with the server key */
546 const char *crt_file2; /* the file with the 2nd server certificate */
547 const char *key_file2; /* the file with the 2nd server key */
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100548 const char *async_operations; /* supported SSL asynchronous operations */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100549 int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
550 int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
551 int async_private_error; /* inject error in async private callback */
Rich Evans85b05ec2015-02-12 11:37:29 +0000552 const char *psk; /* the pre-shared key */
553 const char *psk_identity; /* the pre-shared key identity */
554 char *psk_list; /* list of PSK id/key pairs for callback */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200555 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000556 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
557 const char *version_suites; /* per-version ciphersuites */
558 int renegotiation; /* enable / disable renegotiation */
559 int allow_legacy; /* allow legacy renegotiation */
560 int renegotiate; /* attempt renegotiation? */
561 int renego_delay; /* delay before enforcing renegotiation */
Andres AG692ad842017-01-19 16:30:57 +0000562 uint64_t renego_period; /* period for automatic renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000563 int exchanges; /* number of data exchanges */
564 int min_version; /* minimum protocol version accepted */
565 int max_version; /* maximum protocol version accepted */
566 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200567 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000568 int auth_mode; /* verify mode for connection */
Janos Follath4817e272017-04-10 13:44:33 +0100569 int cert_req_ca_list; /* should we send the CA list? */
Rich Evans85b05ec2015-02-12 11:37:29 +0000570 unsigned char mfl_code; /* code for maximum fragment length */
571 int trunc_hmac; /* accept truncated hmac? */
572 int tickets; /* enable / disable session tickets */
573 int ticket_timeout; /* session ticket lifetime */
574 int cache_max; /* max number of session cache entries */
575 int cache_timeout; /* expiration delay of session cache entries */
576 char *sni; /* string describing sni information */
Hanno Beckere6706e62017-05-15 16:05:15 +0100577 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000578 const char *alpn_string; /* ALPN supported protocols */
579 const char *dhm_file; /* the file with the DH parameters */
580 int extended_ms; /* allow negotiation of extended MS? */
Jarno Lamsa41b35912019-06-10 15:51:11 +0300581 int enforce_extended_master_secret; /* Enforce the usage of extended
582 * master secret */
Rich Evans85b05ec2015-02-12 11:37:29 +0000583 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000584 int transport; /* TLS or DTLS? */
585 int cookies; /* Use cookies for DTLS? -1 to break them */
586 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
587 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
588 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200589 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Hanno Beckere7675d02018-08-14 13:28:56 +0100590 int dgram_packing; /* allow/forbid datagram packing */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000591 int badmac_limit; /* Limit of records with bad MAC */
Hanno Becker1029ace2019-04-09 17:28:10 +0100592 int cid_enabled; /* whether to use the CID extension or not */
Hanno Becker96870292019-05-03 17:30:59 +0100593 int cid_enabled_renego; /* whether to use the CID extension or not
594 * during renegotiation */
Hanno Becker1029ace2019-04-09 17:28:10 +0100595 const char *cid_val; /* the CID to use for incoming messages */
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +0300596 int serialize; /* serialize/deserialize connection */
Hanno Becker96870292019-05-03 17:30:59 +0100597 const char *cid_val_renego; /* the CID to use for incoming messages
598 * after renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000599} opt;
600
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100601int query_config( const char *config );
602
Hanno Becker14a4a442019-07-02 17:00:34 +0100603#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200604static void my_debug( void *ctx, int level,
605 const char *file, int line,
606 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000607{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200608 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000609
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200610 /* Extract basename from file */
611 for( p = basename = file; *p != '\0'; p++ )
612 if( *p == '/' || *p == '\\' )
613 basename = p + 1;
614
615 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000616 fflush( (FILE *) ctx );
617}
Hanno Becker14a4a442019-07-02 17:00:34 +0100618#endif /* MBEDTLS_DEBUG_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000619
Hanno Beckera58a8962019-06-13 16:11:15 +0100620
621#if !defined(MBEDTLS_SSL_CONF_RECV) && \
622 !defined(MBEDTLS_SSL_CONF_SEND) && \
623 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Rich Evans85b05ec2015-02-12 11:37:29 +0000624/*
625 * Test recv/send functions that make sure each try returns
626 * WANT_READ/WANT_WRITE at least once before sucesseding
627 */
Hanno Beckerfe24b3b2019-07-03 17:05:43 +0100628static int delayed_recv( void *ctx, unsigned char *buf, size_t len )
Rich Evans85b05ec2015-02-12 11:37:29 +0000629{
630 static int first_try = 1;
631 int ret;
632
633 if( first_try )
634 {
635 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100636 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000637 }
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100640 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000641 first_try = 1; /* Next call will be a new operation */
642 return( ret );
643}
644
Hanno Beckerfe24b3b2019-07-03 17:05:43 +0100645static int delayed_send( void *ctx, const unsigned char *buf, size_t len )
Rich Evans85b05ec2015-02-12 11:37:29 +0000646{
647 static int first_try = 1;
648 int ret;
649
650 if( first_try )
651 {
652 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100653 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000654 }
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100657 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000658 first_try = 1; /* Next call will be a new operation */
659 return( ret );
660}
661
Hanno Beckerfe24b3b2019-07-03 17:05:43 +0100662typedef struct
663{
664 mbedtls_ssl_context *ssl;
665 mbedtls_net_context *net;
666} io_ctx_t;
667
Hanno Beckerde9e36e2019-07-03 17:14:41 +0100668#if defined(MBEDTLS_SSL_RECORD_CHECKING)
669static int ssl_check_record( mbedtls_ssl_context const *ssl,
670 unsigned char const *buf, size_t len )
671{
672 int ret;
673 unsigned char *tmp_buf;
674
675 /* Record checking may modify the input buffer,
676 * so make a copy. */
677 tmp_buf = mbedtls_calloc( 1, len );
678 if( tmp_buf == NULL )
679 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
680 memcpy( tmp_buf, buf, len );
681
682 ret = mbedtls_ssl_check_record( ssl, tmp_buf, len );
683 if( ret != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE )
684 {
685 int ret_repeated;
686
687 /* Test-only: Make sure that mbedtls_ssl_check_record()
688 * doesn't alter state. */
689 memcpy( tmp_buf, buf, len ); /* Restore buffer */
690 ret_repeated = mbedtls_ssl_check_record( ssl, tmp_buf, len );
691 if( ret != ret_repeated )
692 {
Hanno Becker83b8c3b2019-07-24 13:59:07 +0100693 mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" );
694 return( -1 );
Hanno Beckerde9e36e2019-07-03 17:14:41 +0100695 }
696
697 switch( ret )
698 {
699 case 0:
700 break;
701
702 case MBEDTLS_ERR_SSL_INVALID_RECORD:
703 if( opt.debug_level > 1 )
704 mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" );
705 break;
706
707 case MBEDTLS_ERR_SSL_INVALID_MAC:
708 if( opt.debug_level > 1 )
709 mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" );
710 break;
711
712 case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD:
713 if( opt.debug_level > 1 )
714 mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" );
715 break;
716
717 default:
718 mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", -ret );
719 return( -1 );
720 }
721
722 /* Regardless of the outcome, forward the record to the stack. */
723 }
724
Hanno Beckerde9e36e2019-07-03 17:14:41 +0100725 mbedtls_free( tmp_buf );
726
727 return( 0 );
728}
729#endif /* MBEDTLS_SSL_RECORD_CHECKING */
730
Hanno Beckerfe24b3b2019-07-03 17:05:43 +0100731static int recv_cb( void *ctx, unsigned char *buf, size_t len )
732{
733 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
734 size_t recv_len;
735 int ret;
736
737 if( opt.nbio == 2 )
738 ret = delayed_recv( io_ctx->net, buf, len );
739 else
740 ret = mbedtls_net_recv( io_ctx->net, buf, len );
741 if( ret < 0 )
742 return( ret );
743 recv_len = (size_t) ret;
744
745 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
746 {
747 /* Here's the place to do any datagram/record checking
748 * in between receiving the packet from the underlying
749 * transport and passing it on to the TLS stack. */
Hanno Beckerde9e36e2019-07-03 17:14:41 +0100750#if defined(MBEDTLS_SSL_RECORD_CHECKING)
751 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
752 return( -1 );
753#endif /* MBEDTLS_SSL_RECORD_CHECKING */
Hanno Beckerfe24b3b2019-07-03 17:05:43 +0100754 }
755
756 return( (int) recv_len );
757}
758
759static int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len,
760 uint32_t timeout )
761{
762 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
763 int ret;
764 size_t recv_len;
765
766 ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout );
767 if( ret < 0 )
768 return( ret );
769 recv_len = (size_t) ret;
770
771 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
772 {
773 /* Here's the place to do any datagram/record checking
774 * in between receiving the packet from the underlying
775 * transport and passing it on to the TLS stack. */
Hanno Beckerde9e36e2019-07-03 17:14:41 +0100776#if defined(MBEDTLS_SSL_RECORD_CHECKING)
777 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
778 return( -1 );
779#endif /* MBEDTLS_SSL_RECORD_CHECKING */
Hanno Beckerfe24b3b2019-07-03 17:05:43 +0100780 }
781
782 return( (int) recv_len );
783}
784
785static int send_cb( void *ctx, unsigned char const *buf, size_t len )
786{
787 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
788
789 if( opt.nbio == 2 )
790 return( delayed_send( io_ctx->net, buf, len ) );
791
792 return( mbedtls_net_send( io_ctx->net, buf, len ) );
793}
Manuel Pégourié-Gonnard2b29a372019-07-30 17:07:38 +0200794#endif /* !MBEDTLS_SSL_CONF_RECV &&
795 !MBEDTLS_SSL_CONF_SEND &&
796 !MBEDTLS_SSL_CONF_RECV_TIMEOUT */
Hanno Beckerfe24b3b2019-07-03 17:05:43 +0100797
Manuel Pégourié-Gonnardd64a2f72019-07-30 14:54:50 +0200798#if defined(SNI_OPTION) || !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200799/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200800 * Return authmode from string, or -1 on error
801 */
802static int get_auth_mode( const char *s )
803{
804 if( strcmp( s, "none" ) == 0 )
805 return( MBEDTLS_SSL_VERIFY_NONE );
806 if( strcmp( s, "optional" ) == 0 )
807 return( MBEDTLS_SSL_VERIFY_OPTIONAL );
808 if( strcmp( s, "required" ) == 0 )
809 return( MBEDTLS_SSL_VERIFY_REQUIRED );
810
811 return( -1 );
812}
Manuel Pégourié-Gonnardd64a2f72019-07-30 14:54:50 +0200813#endif /* SNI_OPTION || !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200814
815/*
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200816 * Used by sni_parse and psk_parse to handle coma-separated lists
817 */
818#define GET_ITEM( dst ) \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100819 do \
820 { \
821 (dst) = p; \
822 while( *p != ',' ) \
823 if( ++p > end ) \
824 goto error; \
825 *p++ = '\0'; \
826 } while( 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200827
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200828#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100829typedef struct _sni_entry sni_entry;
830
831struct _sni_entry {
832 const char *name;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_x509_crt *cert;
834 mbedtls_pk_context *key;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200835 mbedtls_x509_crt* ca;
836 mbedtls_x509_crl* crl;
837 int authmode;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100838 sni_entry *next;
839};
840
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100841void sni_free( sni_entry *head )
842{
843 sni_entry *cur = head, *next;
844
845 while( cur != NULL )
846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 mbedtls_x509_crt_free( cur->cert );
848 mbedtls_free( cur->cert );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 mbedtls_pk_free( cur->key );
851 mbedtls_free( cur->key );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100852
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200853 mbedtls_x509_crt_free( cur->ca );
854 mbedtls_free( cur->ca );
Ron Eldor24eec792019-04-04 15:02:01 +0300855#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200856 mbedtls_x509_crl_free( cur->crl );
857 mbedtls_free( cur->crl );
Ron Eldor24eec792019-04-04 15:02:01 +0300858#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100859 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 mbedtls_free( cur );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100861 cur = next;
862 }
863}
864
865/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200866 * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
867 * into a usable sni_entry list. For ca1, crl1, auth1, the special value
868 * '-' means unset. If ca1 is unset, then crl1 is ignored too.
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000869 *
870 * Modifies the input string! This is not production quality!
871 */
872sni_entry *sni_parse( char *sni_string )
873{
874 sni_entry *cur = NULL, *new = NULL;
875 char *p = sni_string;
876 char *end = p;
Ron Eldor24eec792019-04-04 15:02:01 +0300877 char *crt_file, *key_file, *ca_file, *auth_str;
878#if defined(MBEDTLS_X509_CRL_PARSE_C)
879 char *crl_file;
880#endif
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000881
882 while( *end != '\0' )
883 ++end;
884 *end = ',';
885
886 while( p <= end )
887 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200888 if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000889 {
890 sni_free( cur );
891 return( NULL );
892 }
893
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200894 GET_ITEM( new->name );
895 GET_ITEM( crt_file );
896 GET_ITEM( key_file );
897 GET_ITEM( ca_file );
Ron Eldor24eec792019-04-04 15:02:01 +0300898#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200899 GET_ITEM( crl_file );
Ron Eldor24eec792019-04-04 15:02:01 +0300900#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200901 GET_ITEM( auth_str );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000902
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200903 if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
904 ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200905 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_x509_crt_init( new->cert );
908 mbedtls_pk_init( new->key );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
911 mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000912 goto error;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200913
914 if( strcmp( ca_file, "-" ) != 0 )
915 {
916 if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
917 goto error;
918
919 mbedtls_x509_crt_init( new->ca );
920
921 if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
922 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000923 }
924
Ron Eldor24eec792019-04-04 15:02:01 +0300925#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200926 if( strcmp( crl_file, "-" ) != 0 )
927 {
928 if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
929 goto error;
930
931 mbedtls_x509_crl_init( new->crl );
932
933 if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
934 goto error;
935 }
Ron Eldor24eec792019-04-04 15:02:01 +0300936#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200937
938 if( strcmp( auth_str, "-" ) != 0 )
939 {
940 if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
941 goto error;
942 }
943 else
944 new->authmode = DFL_AUTH_MODE;
945
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000946 new->next = cur;
947 cur = new;
948 }
949
950 return( cur );
951
952error:
953 sni_free( new );
954 sni_free( cur );
955 return( NULL );
956}
957
958/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100959 * SNI callback.
960 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100962 const unsigned char *name, size_t name_len )
963{
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200964 const sni_entry *cur = (const sni_entry *) p_info;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100965
966 while( cur != NULL )
967 {
968 if( name_len == strlen( cur->name ) &&
969 memcmp( name, cur->name, name_len ) == 0 )
970 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200971 if( cur->ca != NULL )
972 mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
973
974 if( cur->authmode != DFL_AUTH_MODE )
975 mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
976
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200977 return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100978 }
979
980 cur = cur->next;
981 }
982
983 return( -1 );
984}
985
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200986#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100987
Hanno Becker2e0bedc2019-04-30 14:18:06 +0100988#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) || \
Hanno Beckera5a2b082019-05-15 14:03:01 +0100989 defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200990
Hanno Beckerd6028a12018-10-15 12:01:35 +0100991#define HEX2NUM( c ) \
992 do \
993 { \
994 if( (c) >= '0' && (c) <= '9' ) \
995 (c) -= '0'; \
996 else if( (c) >= 'a' && (c) <= 'f' ) \
997 (c) -= 'a' - 10; \
998 else if( (c) >= 'A' && (c) <= 'F' ) \
999 (c) -= 'A' - 10; \
1000 else \
1001 return( -1 ); \
1002 } while( 0 )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001003
1004/*
1005 * Convert a hex string to bytes.
1006 * Return 0 on success, -1 on error.
1007 */
1008int unhexify( unsigned char *output, const char *input, size_t *olen )
1009{
1010 unsigned char c;
1011 size_t j;
1012
1013 *olen = strlen( input );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 if( *olen % 2 != 0 || *olen / 2 > MBEDTLS_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001015 return( -1 );
1016 *olen /= 2;
1017
1018 for( j = 0; j < *olen * 2; j += 2 )
1019 {
1020 c = input[j];
1021 HEX2NUM( c );
1022 output[ j / 2 ] = c << 4;
1023
1024 c = input[j + 1];
1025 HEX2NUM( c );
1026 output[ j / 2 ] |= c;
1027 }
1028
1029 return( 0 );
1030}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001031
Hanno Becker2e0bedc2019-04-30 14:18:06 +01001032#endif
1033
1034#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1035
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001036typedef struct _psk_entry psk_entry;
1037
1038struct _psk_entry
1039{
1040 const char *name;
1041 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 unsigned char key[MBEDTLS_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001043 psk_entry *next;
1044};
1045
1046/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001047 * Free a list of psk_entry's
1048 */
1049void psk_free( psk_entry *head )
1050{
1051 psk_entry *next;
1052
1053 while( head != NULL )
1054 {
1055 next = head->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_free( head );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001057 head = next;
1058 }
1059}
1060
1061/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001062 * Parse a string of pairs name1,key1[,name2,key2[,...]]
1063 * into a usable psk_entry list.
1064 *
1065 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001066 */
1067psk_entry *psk_parse( char *psk_string )
1068{
1069 psk_entry *cur = NULL, *new = NULL;
1070 char *p = psk_string;
1071 char *end = p;
1072 char *key_hex;
1073
1074 while( *end != '\0' )
1075 ++end;
1076 *end = ',';
1077
1078 while( p <= end )
1079 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001080 if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +00001081 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001082
1083 memset( new, 0, sizeof( psk_entry ) );
1084
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +02001085 GET_ITEM( new->name );
1086 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001087
1088 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001089 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001090
1091 new->next = cur;
1092 cur = new;
1093 }
1094
1095 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001096
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001097error:
1098 psk_free( new );
1099 psk_free( cur );
1100 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001101}
1102
1103/*
1104 * PSK callback
1105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001107 const unsigned char *name, size_t name_len )
1108{
1109 psk_entry *cur = (psk_entry *) p_info;
1110
1111 while( cur != NULL )
1112 {
1113 if( name_len == strlen( cur->name ) &&
1114 memcmp( name, cur->name, name_len ) == 0 )
1115 {
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001116 return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001117 }
1118
1119 cur = cur->next;
1120 }
1121
1122 return( -1 );
1123}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001125
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001126static mbedtls_net_context listen_fd, client_fd;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +02001127
1128/* Interruption handler to ensure clean exit (for valgrind testing) */
1129#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001130static int received_sigterm = 0;
1131void term_handler( int sig )
1132{
1133 ((void) sig);
1134 received_sigterm = 1;
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001135 mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
1136 mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001137}
Paul Bakkerc1283d32014-08-18 11:05:51 +02001138#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001139
Hanno Becker7f1c8052019-08-26 13:30:13 +01001140static int available_hashes[] = {
Gilles Peskine682df092017-05-11 19:01:11 +02001141#if defined(MBEDTLS_SHA512_C)
1142 MBEDTLS_MD_SHA512,
1143 MBEDTLS_MD_SHA384,
1144#endif
1145#if defined(MBEDTLS_SHA256_C)
1146 MBEDTLS_MD_SHA256,
1147 MBEDTLS_MD_SHA224,
1148#endif
1149#if defined(MBEDTLS_SHA1_C)
1150 /* Allow SHA-1 as we use it extensively in tests. */
1151 MBEDTLS_MD_SHA1,
1152#endif
1153 MBEDTLS_MD_NONE
1154};
Gilles Peskine682df092017-05-11 19:01:11 +02001155
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02001156/** Return true if \p ret is a status code indicating that there is an
1157 * operation in progress on an SSL connection, and false if it indicates
1158 * success or a fatal error.
1159 *
1160 * The possible operations in progress are:
1161 *
1162 * - A read, when the SSL input buffer does not contain a full message.
1163 * - A write, when the SSL output buffer contains some data that has not
1164 * been sent over the network yet.
1165 * - An asynchronous callback that has not completed yet. */
1166static int mbedtls_status_is_ssl_in_progress( int ret )
1167{
1168 return( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1169 ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
1170 ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1171}
1172
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001173#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001174typedef struct
1175{
Gilles Peskine44817442018-06-13 18:09:28 +02001176 mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
1177 mbedtls_pk_context *pk; /*!< Private key */
1178 unsigned delay; /*!< Number of resume steps to go through */
1179 unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001180} ssl_async_key_slot_t;
1181
1182typedef enum {
Gilles Peskinead28bf02018-04-26 00:19:16 +02001183 SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
1184 SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
1185 SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start */
1186 SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
Gilles Peskinec9125722018-04-26 07:15:40 +02001187#define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001188} ssl_async_inject_error_t;
1189
1190typedef struct
1191{
Gilles Peskinee2479892018-06-13 18:06:51 +02001192 ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001193 size_t slots_used;
1194 ssl_async_inject_error_t inject_error;
1195 int (*f_rng)(void *, unsigned char *, size_t);
1196 void *p_rng;
1197} ssl_async_key_context_t;
1198
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001199int ssl_async_set_key( ssl_async_key_context_t *ctx,
Gilles Peskine44817442018-06-13 18:09:28 +02001200 mbedtls_x509_crt *cert,
1201 mbedtls_pk_context *pk,
1202 int pk_take_ownership,
1203 unsigned delay )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001204{
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001205 if( ctx->slots_used >= sizeof( ctx->slots ) / sizeof( *ctx->slots ) )
1206 return( -1 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001207 ctx->slots[ctx->slots_used].cert = cert;
1208 ctx->slots[ctx->slots_used].pk = pk;
1209 ctx->slots[ctx->slots_used].delay = delay;
Gilles Peskine44817442018-06-13 18:09:28 +02001210 ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001211 ++ctx->slots_used;
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001212 return( 0 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001213}
1214
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001215#define SSL_ASYNC_INPUT_MAX_SIZE 512
Gilles Peskined3268832018-04-26 06:23:59 +02001216
1217typedef enum
1218{
1219 ASYNC_OP_SIGN,
1220 ASYNC_OP_DECRYPT,
1221} ssl_async_operation_type_t;
1222/* Note that the enum above and the array below need to be kept in sync!
1223 * `ssl_async_operation_names[op]` is the name of op for each value `op`
1224 * of type `ssl_async_operation_type_t`. */
1225static const char *const ssl_async_operation_names[] =
1226{
1227 "sign",
1228 "decrypt",
1229};
1230
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001231typedef struct
1232{
Gilles Peskine6331d782018-04-26 13:27:43 +02001233 unsigned slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001234 ssl_async_operation_type_t operation_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001235 mbedtls_md_type_t md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001236 unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
1237 size_t input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001238 unsigned remaining_delay;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001239} ssl_async_operation_context_t;
1240
Gilles Peskine8f97af72018-04-26 11:46:10 +02001241static int ssl_async_start( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001242 mbedtls_x509_crt *cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001243 ssl_async_operation_type_t op_type,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001244 mbedtls_md_type_t md_alg,
1245 const unsigned char *input,
1246 size_t input_len )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001247{
Hanno Becker5d9021e2019-02-28 14:32:37 +00001248 int ret;
Gilles Peskine8f97af72018-04-26 11:46:10 +02001249 ssl_async_key_context_t *config_data =
1250 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskine6331d782018-04-26 13:27:43 +02001251 unsigned slot;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001252 ssl_async_operation_context_t *ctx = NULL;
Gilles Peskined3268832018-04-26 06:23:59 +02001253 const char *op_name = ssl_async_operation_names[op_type];
Gilles Peskinef1127252018-04-24 13:05:39 +02001254
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001255 {
1256 char dn[100];
Hanno Becker5d9021e2019-02-28 14:32:37 +00001257 mbedtls_x509_name *subject;
1258
1259 ret = mbedtls_x509_crt_get_subject( cert, &subject );
1260 if( ret != 0 )
1261 return( ret );
1262
1263 if( mbedtls_x509_dn_gets( dn, sizeof( dn ), subject ) > 0 )
Gilles Peskined5d983e2018-06-15 14:05:10 +02001264 mbedtls_printf( "Async %s callback: looking for DN=%s\n",
1265 op_name, dn );
Hanno Becker5d9021e2019-02-28 14:32:37 +00001266
1267 mbedtls_x509_name_free( subject );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001268 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001269
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001270 /* Look for a private key that matches the public key in cert.
1271 * Since this test code has the private key inside Mbed TLS,
1272 * we call mbedtls_pk_check_pair to match a private key with the
1273 * public key. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001274 for( slot = 0; slot < config_data->slots_used; slot++ )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001275 {
Hanno Becker5d9021e2019-02-28 14:32:37 +00001276 mbedtls_pk_context *pk;
1277 int match;
1278 ret = mbedtls_x509_crt_pk_acquire( cert, &pk );
1279 if( ret != 0 )
1280 return( ret );
1281 match = mbedtls_pk_check_pair( pk, config_data->slots[slot].pk );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00001282 mbedtls_x509_crt_pk_release( cert );
Hanno Becker5d9021e2019-02-28 14:32:37 +00001283 if( match == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001284 break;
1285 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001286 if( slot == config_data->slots_used )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001287 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001288 mbedtls_printf( "Async %s callback: no key matches this certificate.\n",
1289 op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001290 return( MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH );
1291 }
Gilles Peskine6331d782018-04-26 13:27:43 +02001292 mbedtls_printf( "Async %s callback: using key slot %u, delay=%u.\n",
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001293 op_name, slot, config_data->slots[slot].delay );
Gilles Peskinef1127252018-04-24 13:05:39 +02001294
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001295 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001296 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001297 mbedtls_printf( "Async %s callback: injected error\n", op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001298 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1299 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001300
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001301 if( input_len > SSL_ASYNC_INPUT_MAX_SIZE )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Gilles Peskinef1127252018-04-24 13:05:39 +02001303
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001304 ctx = mbedtls_calloc( 1, sizeof( *ctx ) );
1305 if( ctx == NULL )
1306 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1307 ctx->slot = slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001308 ctx->operation_type = op_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001309 ctx->md_alg = md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001310 memcpy( ctx->input, input, input_len );
1311 ctx->input_len = input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001312 ctx->remaining_delay = config_data->slots[slot].delay;
Gilles Peskinea668c602018-04-30 11:54:39 +02001313 mbedtls_ssl_set_async_operation_data( ssl, ctx );
Gilles Peskinef1127252018-04-24 13:05:39 +02001314
Gilles Peskineceb541b2018-04-26 06:30:45 +02001315 if( ctx->remaining_delay == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001316 return( 0 );
1317 else
1318 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1319}
1320
Gilles Peskine8f97af72018-04-26 11:46:10 +02001321static int ssl_async_sign( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001322 mbedtls_x509_crt *cert,
1323 mbedtls_md_type_t md_alg,
1324 const unsigned char *hash,
1325 size_t hash_len )
1326{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001327 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001328 ASYNC_OP_SIGN, md_alg,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001329 hash, hash_len ) );
1330}
1331
Gilles Peskine8f97af72018-04-26 11:46:10 +02001332static int ssl_async_decrypt( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001333 mbedtls_x509_crt *cert,
1334 const unsigned char *input,
1335 size_t input_len )
1336{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001337 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001338 ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001339 input, input_len ) );
1340}
1341
Gilles Peskine8f97af72018-04-26 11:46:10 +02001342static int ssl_async_resume( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001343 unsigned char *output,
1344 size_t *output_len,
1345 size_t output_size )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001346{
Gilles Peskinea668c602018-04-30 11:54:39 +02001347 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine8f97af72018-04-26 11:46:10 +02001348 ssl_async_key_context_t *config_data =
1349 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001350 ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001351 int ret;
Gilles Peskinef5a99962018-04-30 16:37:23 +02001352 const char *op_name;
Gilles Peskinef1127252018-04-24 13:05:39 +02001353
Gilles Peskineceb541b2018-04-26 06:30:45 +02001354 if( ctx->remaining_delay > 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001355 {
Gilles Peskineceb541b2018-04-26 06:30:45 +02001356 --ctx->remaining_delay;
Gilles Peskine6331d782018-04-26 13:27:43 +02001357 mbedtls_printf( "Async resume (slot %u): call %u more times.\n",
Gilles Peskineceb541b2018-04-26 06:30:45 +02001358 ctx->slot, ctx->remaining_delay );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001359 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1360 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001361
Gilles Peskined3268832018-04-26 06:23:59 +02001362 switch( ctx->operation_type )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001363 {
Gilles Peskined3268832018-04-26 06:23:59 +02001364 case ASYNC_OP_DECRYPT:
Gilles Peskined3268832018-04-26 06:23:59 +02001365 ret = mbedtls_pk_decrypt( key_slot->pk,
1366 ctx->input, ctx->input_len,
1367 output, output_len, output_size,
1368 config_data->f_rng, config_data->p_rng );
1369 break;
1370 case ASYNC_OP_SIGN:
Gilles Peskined3268832018-04-26 06:23:59 +02001371 ret = mbedtls_pk_sign( key_slot->pk,
1372 ctx->md_alg,
1373 ctx->input, ctx->input_len,
1374 output, output_len,
1375 config_data->f_rng, config_data->p_rng );
1376 break;
1377 default:
Gilles Peskine6331d782018-04-26 13:27:43 +02001378 mbedtls_printf( "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
Gilles Peskined3268832018-04-26 06:23:59 +02001379 ctx->slot, (long) ctx->operation_type );
Gilles Peskine44817442018-06-13 18:09:28 +02001380 mbedtls_free( ctx );
Gilles Peskined3268832018-04-26 06:23:59 +02001381 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1382 break;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001383 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001384
Gilles Peskinef5a99962018-04-30 16:37:23 +02001385 op_name = ssl_async_operation_names[ctx->operation_type];
1386
Gilles Peskinec9125722018-04-26 07:15:40 +02001387 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001388 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001389 mbedtls_printf( "Async resume callback: %s done but injected error\n",
1390 op_name );
Gilles Peskine2636fad2018-06-12 14:17:39 +02001391 mbedtls_free( ctx );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001392 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1393 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001394
Gilles Peskine6331d782018-04-26 13:27:43 +02001395 mbedtls_printf( "Async resume (slot %u): %s done, status=%d.\n",
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001396 ctx->slot, op_name, ret );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001397 mbedtls_free( ctx );
1398 return( ret );
1399}
1400
Gilles Peskine8f97af72018-04-26 11:46:10 +02001401static void ssl_async_cancel( mbedtls_ssl_context *ssl )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001402{
Gilles Peskinea668c602018-04-30 11:54:39 +02001403 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001404 mbedtls_printf( "Async cancel callback.\n" );
1405 mbedtls_free( ctx );
1406}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001407#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001408
Hanno Becker16970d22017-10-10 15:56:37 +01001409/*
1410 * Wait for an event from the underlying transport or the timer
1411 * (Used in event-driven IO mode).
1412 */
1413#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001414int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001415 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001416#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001417int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001418 mbedtls_timing_delay_context *timer,
1419 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001420#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001421{
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001422 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +01001423 int poll_type = 0;
1424
1425 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
1426 poll_type = MBEDTLS_NET_POLL_WRITE;
1427 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
1428 poll_type = MBEDTLS_NET_POLL_READ;
1429#if !defined(MBEDTLS_TIMING_C)
1430 else
Hanno Beckeref527962018-03-15 15:49:24 +00001431 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001432#endif
1433
1434 while( 1 )
1435 {
Hanno Becker197a91c2017-10-31 10:58:53 +00001436 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +01001437#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001438 if( timer != NULL &&
1439 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +01001440 {
Hanno Becker16970d22017-10-10 15:56:37 +01001441 break;
1442 }
Hanno Becker197a91c2017-10-31 10:58:53 +00001443#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +01001444
Hanno Becker197a91c2017-10-31 10:58:53 +00001445 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001446 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +01001447 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001448 ret = mbedtls_net_poll( fd, poll_type, 0 );
1449 if( ret < 0 )
1450 return( ret );
1451 if( ret == poll_type )
1452 break;
Hanno Becker16970d22017-10-10 15:56:37 +01001453 }
1454 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001455
1456 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001457}
1458
Hanno Beckera5a2b082019-05-15 14:03:01 +01001459#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01001460int report_cid_usage( mbedtls_ssl_context *ssl,
1461 const char *additional_description )
1462{
1463 int ret;
1464 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
1465 size_t peer_cid_len;
1466 int cid_negotiated;
1467
1468 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1469 return( 0 );
1470
1471 /* Check if the use of a CID has been negotiated */
1472 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
1473 peer_cid, &peer_cid_len );
1474 if( ret != 0 )
1475 {
1476 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
1477 -ret );
1478 return( ret );
1479 }
1480
1481 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
1482 {
1483 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
1484 {
1485 mbedtls_printf( "(%s) Use of Connection ID was not offered by client.\n",
1486 additional_description );
1487 }
1488 }
1489 else
1490 {
1491 size_t idx=0;
1492 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
1493 additional_description );
1494 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
1495 additional_description,
1496 (unsigned) peer_cid_len );
1497 while( idx < peer_cid_len )
1498 {
1499 mbedtls_printf( "%02x ", peer_cid[ idx ] );
1500 idx++;
1501 }
1502 mbedtls_printf( "\n" );
1503 }
1504
1505 return( 0 );
1506}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001507#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker96870292019-05-03 17:30:59 +01001508
Hanno Becker572d4482019-07-23 13:47:53 +01001509#if defined(MBEDTLS_SSL_CONF_RNG)
1510int rng_wrap( void *ctx, unsigned char *dst, size_t len );
1511
Hanno Becker7f1c8052019-08-26 13:30:13 +01001512#if defined(MBEDTLS_CTR_DRBG_C)
Hanno Becker572d4482019-07-23 13:47:53 +01001513mbedtls_ctr_drbg_context *rng_ctx_global = NULL;
Hanno Becker7f1c8052019-08-26 13:30:13 +01001514#else
1515mbedtls_hmac_drbg_context *rng_ctx_global = NULL;
1516#endif /* MBEDTLS_CTR_DRBG_C */
1517
Hanno Becker572d4482019-07-23 13:47:53 +01001518int rng_wrap( void *ctx, unsigned char *dst, size_t len )
1519{
1520 /* We expect the NULL parameter here. */
1521 if( ctx != NULL )
1522 return( -1 );
1523
Hanno Becker7f1c8052019-08-26 13:30:13 +01001524#if defined(MBEDTLS_CTR_DRBG_C)
Hanno Becker572d4482019-07-23 13:47:53 +01001525 return( mbedtls_ctr_drbg_random( rng_ctx_global, dst, len ) );
Hanno Becker7f1c8052019-08-26 13:30:13 +01001526#else
1527 return( mbedtls_hmac_drbg_random( rng_ctx_global, dst, len ) );
1528#endif
Hanno Becker572d4482019-07-23 13:47:53 +01001529}
1530#endif /* MBEDTLS_SSL_CONF_RNG */
1531
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001532int main( int argc, char *argv[] )
1533{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001534 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001535 int version_suites[4][2];
Manuel Pégourié-Gonnard2b29a372019-07-30 17:07:38 +02001536#if !defined(MBEDTLS_SSL_CONF_RECV) && \
1537 !defined(MBEDTLS_SSL_CONF_SEND) && \
1538 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Hanno Beckerfe24b3b2019-07-03 17:05:43 +01001539 io_ctx_t io_ctx;
Manuel Pégourié-Gonnard2b29a372019-07-30 17:07:38 +02001540#endif
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001541 unsigned char* buf = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1543 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +02001544 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +02001545 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +02001546#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001547 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001548 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02001549 size_t cliip_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550#if defined(MBEDTLS_SSL_COOKIE_C)
1551 mbedtls_ssl_cookie_ctx cookie_ctx;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001552#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001553
Gilles Peskineef86ab22017-05-05 18:59:02 +02001554#if defined(MBEDTLS_X509_CRT_PARSE_C)
1555 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
1556#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 mbedtls_entropy_context entropy;
Hanno Becker7f1c8052019-08-26 13:30:13 +01001558#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker7f1c8052019-08-26 13:30:13 +01001560#else
1561 mbedtls_hmac_drbg_context hmac_drbg;
1562#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001564 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001565#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001566 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001567#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001569 unsigned char renego_period[8] = { 0 };
1570#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +02001572 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 mbedtls_x509_crt cacert;
1574 mbedtls_x509_crt srvcert;
1575 mbedtls_pk_context pkey;
1576 mbedtls_x509_crt srvcert2;
1577 mbedtls_pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001578 int key_cert_init = 0, key_cert_init2 = 0;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001579#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001580 ssl_async_key_context_t ssl_async_keys;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001581#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001582#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1584 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001585#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#if defined(MBEDTLS_SSL_CACHE_C)
1587 mbedtls_ssl_cache_context cache;
Paul Bakker0a597072012-09-25 21:55:46 +00001588#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001589#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1590 mbedtls_ssl_ticket_context ticket_ctx;
1591#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001592#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001593 sni_entry *sni_info = NULL;
1594#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +01001595#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Hanno Becker8651a432017-06-09 16:13:22 +01001596 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +01001597 const mbedtls_ecp_curve_info * curve_cur;
1598#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +01001600 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001601#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Simon Butchercce68be2018-07-23 14:26:09 +01001603 unsigned char alloc_buf[MEMORY_HEAP_SIZE];
Paul Bakker82024bf2013-07-04 11:52:32 +02001604#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001605
Hanno Beckera5a2b082019-05-15 14:03:01 +01001606#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1029ace2019-04-09 17:28:10 +01001607 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker96870292019-05-03 17:30:59 +01001608 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker1029ace2019-04-09 17:28:10 +01001609 size_t cid_len = 0;
Hanno Becker96870292019-05-03 17:30:59 +01001610 size_t cid_renego_len = 0;
Hanno Becker1029ace2019-04-09 17:28:10 +01001611#endif
Manuel Pégourié-Gonnardc9812292019-07-15 10:31:11 +02001612#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1613 unsigned char *context_buf = NULL;
1614 size_t context_buf_len;
1615#endif
Hanno Becker1029ace2019-04-09 17:28:10 +01001616
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001617 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001618 char *p, *q;
1619 const int *list;
1620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1622 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker82024bf2013-07-04 11:52:32 +02001623#endif
1624
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001625 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +02001626 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001627 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001628 mbedtls_net_init( &client_fd );
1629 mbedtls_net_init( &listen_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001630 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001631 mbedtls_ssl_config_init( &conf );
Hanno Becker7f1c8052019-08-26 13:30:13 +01001632#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001633 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Becker7f1c8052019-08-26 13:30:13 +01001634#else
1635 mbedtls_hmac_drbg_init( &hmac_drbg );
1636#endif /* MBEDTLS_CTR_DRBG_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637#if defined(MBEDTLS_X509_CRT_PARSE_C)
1638 mbedtls_x509_crt_init( &cacert );
1639 mbedtls_x509_crt_init( &srvcert );
1640 mbedtls_pk_init( &pkey );
1641 mbedtls_x509_crt_init( &srvcert2 );
1642 mbedtls_pk_init( &pkey2 );
Gilles Peskine4d9ec4d2018-04-26 14:33:43 +02001643#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
1644 memset( &ssl_async_keys, 0, sizeof( ssl_async_keys ) );
1645#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001646#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1648 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001649#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650#if defined(MBEDTLS_SSL_CACHE_C)
1651 mbedtls_ssl_cache_init( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00001652#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001653#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1654 mbedtls_ssl_ticket_init( &ticket_ctx );
1655#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +02001657 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001658#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#if defined(MBEDTLS_SSL_COOKIE_C)
1660 mbedtls_ssl_cookie_init( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001661#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001662
Paul Bakkerc1283d32014-08-18 11:05:51 +02001663#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001664 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001665 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001666 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +02001667#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001668
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001669 if( argc == 0 )
1670 {
1671 usage:
1672 if( ret == 0 )
1673 ret = 1;
1674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 mbedtls_printf( USAGE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 list = mbedtls_ssl_list_ciphersuites();
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001678 while( *list )
1679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001681 list++;
1682 if( !*list )
1683 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001685 list++;
1686 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 mbedtls_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001688 goto exit;
1689 }
1690
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001691 opt.buffer_size = DFL_IO_BUF_LEN;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001692 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001693 opt.server_port = DFL_SERVER_PORT;
1694 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker16970d22017-10-10 15:56:37 +01001695 opt.event = DFL_EVENT;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001696 opt.response_size = DFL_RESPONSE_SIZE;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001697 opt.nbio = DFL_NBIO;
Hanno Becker1029ace2019-04-09 17:28:10 +01001698 opt.cid_enabled = DFL_CID_ENABLED;
Hanno Becker96870292019-05-03 17:30:59 +01001699 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
Hanno Becker1029ace2019-04-09 17:28:10 +01001700 opt.cid_val = DFL_CID_VALUE;
Hanno Becker96870292019-05-03 17:30:59 +01001701 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001702 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001703 opt.ca_file = DFL_CA_FILE;
1704 opt.ca_path = DFL_CA_PATH;
1705 opt.crt_file = DFL_CRT_FILE;
1706 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001707 opt.crt_file2 = DFL_CRT_FILE2;
1708 opt.key_file2 = DFL_KEY_FILE2;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001709 opt.async_operations = DFL_ASYNC_OPERATIONS;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001710 opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
1711 opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
1712 opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
Paul Bakkerfbb17802013-04-17 19:10:21 +02001713 opt.psk = DFL_PSK;
1714 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001715 opt.psk_list = DFL_PSK_LIST;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001716 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001717 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001718 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001719 opt.renegotiation = DFL_RENEGOTIATION;
1720 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001721 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001722 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001723 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001724 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001725 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001726 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001727 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001728 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001729 opt.auth_mode = DFL_AUTH_MODE;
Janos Follath4817e272017-04-10 13:44:33 +01001730 opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001731 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001732 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001733 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001734 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001735 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001736 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001737 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001738 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001739 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001740 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001741 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001742 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001743 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001744 opt.hs_to_min = DFL_HS_TO_MIN;
1745 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001746 opt.dtls_mtu = DFL_DTLS_MTU;
Hanno Beckere7675d02018-08-14 13:28:56 +01001747 opt.dgram_packing = DFL_DGRAM_PACKING;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001748 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001749 opt.extended_ms = DFL_EXTENDED_MS;
Jarno Lamsa41b35912019-06-10 15:51:11 +03001750 opt.enforce_extended_master_secret = DFL_EXTENDED_MS_ENFORCE;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001751 opt.etm = DFL_ETM;
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03001752 opt.serialize = DFL_SERIALIZE;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001753
1754 for( i = 1; i < argc; i++ )
1755 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001756 p = argv[i];
1757 if( ( q = strchr( p, '=' ) ) == NULL )
1758 goto usage;
1759 *q++ = '\0';
1760
1761 if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001762 opt.server_port = q;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001763 else if( strcmp( p, "server_addr" ) == 0 )
1764 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001765 else if( strcmp( p, "dtls" ) == 0 )
1766 {
1767 int t = atoi( q );
1768 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001770 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001772 else
1773 goto usage;
1774 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001775 else if( strcmp( p, "debug_level" ) == 0 )
1776 {
1777 opt.debug_level = atoi( q );
1778 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1779 goto usage;
1780 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001781 else if( strcmp( p, "nbio" ) == 0 )
1782 {
1783 opt.nbio = atoi( q );
1784 if( opt.nbio < 0 || opt.nbio > 2 )
1785 goto usage;
1786 }
Hanno Becker16970d22017-10-10 15:56:37 +01001787 else if( strcmp( p, "event" ) == 0 )
1788 {
1789 opt.event = atoi( q );
1790 if( opt.event < 0 || opt.event > 2 )
1791 goto usage;
1792 }
Hanno Becker1f835fa2019-06-13 10:14:59 +01001793#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001794 else if( strcmp( p, "read_timeout" ) == 0 )
1795 opt.read_timeout = atoi( q );
Hanno Becker1f835fa2019-06-13 10:14:59 +01001796#endif
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001797 else if( strcmp( p, "buffer_size" ) == 0 )
1798 {
1799 opt.buffer_size = atoi( q );
1800 if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
1801 goto usage;
1802 }
1803 else if( strcmp( p, "response_size" ) == 0 )
1804 {
1805 opt.response_size = atoi( q );
1806 if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
1807 goto usage;
1808 if( opt.buffer_size < opt.response_size )
1809 opt.buffer_size = opt.response_size;
1810 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001811 else if( strcmp( p, "ca_file" ) == 0 )
1812 opt.ca_file = q;
1813 else if( strcmp( p, "ca_path" ) == 0 )
1814 opt.ca_path = q;
1815 else if( strcmp( p, "crt_file" ) == 0 )
1816 opt.crt_file = q;
1817 else if( strcmp( p, "key_file" ) == 0 )
1818 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001819 else if( strcmp( p, "crt_file2" ) == 0 )
1820 opt.crt_file2 = q;
1821 else if( strcmp( p, "key_file2" ) == 0 )
1822 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001823 else if( strcmp( p, "dhm_file" ) == 0 )
1824 opt.dhm_file = q;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001825#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001826 else if( strcmp( p, "async_operations" ) == 0 )
1827 opt.async_operations = q;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001828 else if( strcmp( p, "async_private_delay1" ) == 0 )
1829 opt.async_private_delay1 = atoi( q );
1830 else if( strcmp( p, "async_private_delay2" ) == 0 )
1831 opt.async_private_delay2 = atoi( q );
1832 else if( strcmp( p, "async_private_error" ) == 0 )
1833 {
1834 int n = atoi( q );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01001835 if( n < -SSL_ASYNC_INJECT_ERROR_MAX ||
1836 n > SSL_ASYNC_INJECT_ERROR_MAX )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001837 {
1838 ret = 2;
1839 goto usage;
1840 }
1841 opt.async_private_error = n;
1842 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001843#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Hanno Beckera5a2b082019-05-15 14:03:01 +01001844#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1029ace2019-04-09 17:28:10 +01001845 else if( strcmp( p, "cid" ) == 0 )
1846 {
1847 opt.cid_enabled = atoi( q );
1848 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
1849 goto usage;
1850 }
Hanno Becker96870292019-05-03 17:30:59 +01001851 else if( strcmp( p, "cid_renego" ) == 0 )
1852 {
1853 opt.cid_enabled_renego = atoi( q );
1854 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
1855 goto usage;
1856 }
Hanno Becker1029ace2019-04-09 17:28:10 +01001857 else if( strcmp( p, "cid_val" ) == 0 )
1858 {
1859 opt.cid_val = q;
1860 }
Hanno Becker96870292019-05-03 17:30:59 +01001861 else if( strcmp( p, "cid_val_renego" ) == 0 )
1862 {
1863 opt.cid_val_renego = q;
1864 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001865#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001866 else if( strcmp( p, "psk" ) == 0 )
1867 opt.psk = q;
1868 else if( strcmp( p, "psk_identity" ) == 0 )
1869 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001870 else if( strcmp( p, "psk_list" ) == 0 )
1871 opt.psk_list = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001872 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1873 opt.ecjpake_pw = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001874 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001877
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001878 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001879 {
1880 ret = 2;
1881 goto usage;
1882 }
1883 opt.force_ciphersuite[1] = 0;
1884 }
Hanno Beckerc1096e72019-06-19 12:30:41 +01001885#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Hanno Beckere6706e62017-05-15 16:05:15 +01001886 else if( strcmp( p, "curves" ) == 0 )
1887 opt.curves = q;
Hanno Beckerc1096e72019-06-19 12:30:41 +01001888#endif /* !MBEDTLS_SSL_CONF_SINGLE_EC */
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001889 else if( strcmp( p, "version_suites" ) == 0 )
1890 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001891 else if( strcmp( p, "renegotiation" ) == 0 )
1892 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001893 opt.renegotiation = (atoi( q )) ?
1894 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1895 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001896 }
Hanno Beckerb0b2b672019-06-12 16:58:10 +01001897#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001898 else if( strcmp( p, "allow_legacy" ) == 0 )
1899 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001900 switch( atoi( q ) )
1901 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001902 case -1:
1903 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1904 break;
1905 case 0:
1906 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1907 break;
1908 case 1:
1909 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1910 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001911 default: goto usage;
1912 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001913 }
Hanno Beckerb0b2b672019-06-12 16:58:10 +01001914#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001915 else if( strcmp( p, "renegotiate" ) == 0 )
1916 {
1917 opt.renegotiate = atoi( q );
1918 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1919 goto usage;
1920 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001921 else if( strcmp( p, "renego_delay" ) == 0 )
1922 {
1923 opt.renego_delay = atoi( q );
1924 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001925 else if( strcmp( p, "renego_period" ) == 0 )
1926 {
Andres AG0b736db2017-02-08 14:05:57 +00001927#if defined(_MSC_VER)
1928 opt.renego_period = _strtoui64( q, NULL, 10 );
1929#else
1930 if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
1931 goto usage;
1932#endif /* _MSC_VER */
1933 if( opt.renego_period < 2 )
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001934 goto usage;
1935 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001936 else if( strcmp( p, "exchanges" ) == 0 )
1937 {
1938 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001939 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001940 goto usage;
1941 }
Hanno Becker72e5ffc2019-07-05 11:35:08 +01001942#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
1943 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) || \
1944 !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
1945 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Paul Bakker1d29fb52012-09-28 13:28:45 +00001946 else if( strcmp( p, "min_version" ) == 0 )
1947 {
1948 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001950 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001952 else if( strcmp( q, "tls1_1" ) == 0 ||
1953 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001955 else if( strcmp( q, "tls1_2" ) == 0 ||
1956 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001958 else
1959 goto usage;
1960 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001961 else if( strcmp( p, "max_version" ) == 0 )
1962 {
1963 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001965 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001967 else if( strcmp( q, "tls1_1" ) == 0 ||
1968 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001970 else if( strcmp( q, "tls1_2" ) == 0 ||
1971 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001973 else
1974 goto usage;
1975 }
1976 else if( strcmp( p, "force_version" ) == 0 )
1977 {
1978 if( strcmp( q, "ssl3" ) == 0 )
1979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1981 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001982 }
1983 else if( strcmp( q, "tls1" ) == 0 )
1984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1986 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001987 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001988 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1991 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001992 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001993 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1996 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001997 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001998 else if( strcmp( q, "dtls1" ) == 0 )
1999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
2001 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
2002 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002003 }
2004 else if( strcmp( q, "dtls1_2" ) == 0 )
2005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
2007 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
2008 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002009 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002010 else
2011 goto usage;
2012 }
Hanno Becker72e5ffc2019-07-05 11:35:08 +01002013#endif
2014 else if( strcmp( p, "arc4" ) == 0 )
2015 {
2016 switch( atoi( q ) )
2017 {
2018 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
2019 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
2020 default: goto usage;
2021 }
2022 }
2023 else if( strcmp( p, "allow_sha1" ) == 0 )
2024 {
2025 switch( atoi( q ) )
2026 {
2027 case 0: opt.allow_sha1 = 0; break;
2028 case 1: opt.allow_sha1 = 1; break;
2029 default: goto usage;
2030 }
2031 }
2032#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
2033 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) || \
2034 !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
2035 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
2036
2037#endif
Hanno Beckeracd4fc02019-06-12 16:40:50 +01002038#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Paul Bakker91ebfb52012-11-23 14:04:08 +01002039 else if( strcmp( p, "auth_mode" ) == 0 )
2040 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02002041 if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
Paul Bakker91ebfb52012-11-23 14:04:08 +01002042 goto usage;
2043 }
Hanno Beckeracd4fc02019-06-12 16:40:50 +01002044#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Janos Follath4817e272017-04-10 13:44:33 +01002045 else if( strcmp( p, "cert_req_ca_list" ) == 0 )
2046 {
2047 opt.cert_req_ca_list = atoi( q );
2048 if( opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1 )
2049 goto usage;
2050 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002051 else if( strcmp( p, "max_frag_len" ) == 0 )
2052 {
2053 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002055 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002057 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002059 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002061 else
2062 goto usage;
2063 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002064 else if( strcmp( p, "alpn" ) == 0 )
2065 {
2066 opt.alpn_string = q;
2067 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002068 else if( strcmp( p, "trunc_hmac" ) == 0 )
2069 {
2070 switch( atoi( q ) )
2071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
2073 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002074 default: goto usage;
2075 }
2076 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002077 else if( strcmp( p, "extended_ms" ) == 0 )
2078 {
2079 switch( atoi( q ) )
2080 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002081 case 0:
2082 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
2083 break;
2084 case 1:
2085 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
2086 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002087 default: goto usage;
2088 }
2089 }
Jarno Lamsa41b35912019-06-10 15:51:11 +03002090 else if( strcmp( p, "enforce_extended_master_secret" ) == 0 )
2091 {
2092 switch( atoi( q ) )
2093 {
2094 case 0:
2095 opt.enforce_extended_master_secret =
2096 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
2097 break;
2098 case 1:
2099 opt.enforce_extended_master_secret =
2100 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_ENABLED;
2101 break;
2102 default: goto usage;
2103 }
2104 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002105 else if( strcmp( p, "etm" ) == 0 )
2106 {
2107 switch( atoi( q ) )
2108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
2110 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002111 default: goto usage;
2112 }
2113 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002114 else if( strcmp( p, "tickets" ) == 0 )
2115 {
2116 opt.tickets = atoi( q );
2117 if( opt.tickets < 0 || opt.tickets > 1 )
2118 goto usage;
2119 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01002120 else if( strcmp( p, "ticket_timeout" ) == 0 )
2121 {
2122 opt.ticket_timeout = atoi( q );
2123 if( opt.ticket_timeout < 0 )
2124 goto usage;
2125 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002126 else if( strcmp( p, "cache_max" ) == 0 )
2127 {
2128 opt.cache_max = atoi( q );
2129 if( opt.cache_max < 0 )
2130 goto usage;
2131 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002132 else if( strcmp( p, "cache_timeout" ) == 0 )
2133 {
2134 opt.cache_timeout = atoi( q );
2135 if( opt.cache_timeout < 0 )
2136 goto usage;
2137 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002138 else if( strcmp( p, "cookies" ) == 0 )
2139 {
2140 opt.cookies = atoi( q );
2141 if( opt.cookies < -1 || opt.cookies > 1)
2142 goto usage;
2143 }
Hanno Becker7f376f42019-06-12 16:20:48 +01002144#if !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002145 else if( strcmp( p, "anti_replay" ) == 0 )
2146 {
2147 opt.anti_replay = atoi( q );
2148 if( opt.anti_replay < 0 || opt.anti_replay > 1)
2149 goto usage;
2150 }
Hanno Becker7f376f42019-06-12 16:20:48 +01002151#endif /* !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Hanno Beckerde671542019-06-12 16:30:46 +01002152#if !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002153 else if( strcmp( p, "badmac_limit" ) == 0 )
2154 {
2155 opt.badmac_limit = atoi( q );
2156 if( opt.badmac_limit < 0 )
2157 goto usage;
2158 }
Hanno Beckerde671542019-06-12 16:30:46 +01002159#endif /* !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002160 else if( strcmp( p, "hs_timeout" ) == 0 )
2161 {
2162 if( ( p = strchr( q, '-' ) ) == NULL )
2163 goto usage;
2164 *p++ = '\0';
2165 opt.hs_to_min = atoi( q );
2166 opt.hs_to_max = atoi( p );
2167 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
2168 goto usage;
2169 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002170 else if( strcmp( p, "mtu" ) == 0 )
2171 {
2172 opt.dtls_mtu = atoi( q );
2173 if( opt.dtls_mtu < 0 )
2174 goto usage;
2175 }
Hanno Beckere7675d02018-08-14 13:28:56 +01002176 else if( strcmp( p, "dgram_packing" ) == 0 )
2177 {
2178 opt.dgram_packing = atoi( q );
2179 if( opt.dgram_packing != 0 &&
2180 opt.dgram_packing != 1 )
2181 {
2182 goto usage;
2183 }
2184 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002185 else if( strcmp( p, "sni" ) == 0 )
2186 {
2187 opt.sni = q;
2188 }
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +01002189 else if( strcmp( p, "query_config" ) == 0 )
2190 {
2191 return query_config( q );
2192 }
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03002193 else if( strcmp( p, "serialize") == 0 )
2194 {
2195 opt.serialize = atoi( q );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03002196 if( opt.serialize < 0 || opt.serialize > 2)
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03002197 goto usage;
2198 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002199 else
2200 goto usage;
2201 }
2202
Hanno Becker16970d22017-10-10 15:56:37 +01002203 /* Event-driven IO is incompatible with the above custom
2204 * receive and send functions, as the polling builds on
2205 * refers to the underlying net_context. */
2206 if( opt.event == 1 && opt.nbio != 1 )
2207 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002208 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01002209 opt.nbio = 1;
2210 }
2211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212#if defined(MBEDTLS_DEBUG_C)
2213 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02002214#endif
Andrzej Kurekda4029d2018-06-20 07:07:55 -04002215 buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002216 if( buf == NULL )
2217 {
Andrzej Kurek755890f2018-06-20 08:17:04 -04002218 mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002219 ret = 3;
2220 goto exit;
2221 }
Paul Bakkerc73079a2014-04-25 16:34:30 +02002222
Paul Bakkerc1516be2013-06-29 16:01:32 +02002223 if( opt.force_ciphersuite[0] > 0 )
2224 {
Hanno Becker473f98f2019-06-26 10:27:32 +01002225 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002226 ciphersuite_info =
2227 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002228
Paul Bakker5b55b792013-07-19 13:43:43 +02002229 if( opt.max_version != -1 &&
Hanno Becker473f98f2019-06-26 10:27:32 +01002230 mbedtls_ssl_suite_get_min_minor_ver( ciphersuite_info ) > opt.max_version )
Paul Bakker5b55b792013-07-19 13:43:43 +02002231 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002232 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker5b55b792013-07-19 13:43:43 +02002233 ret = 2;
2234 goto usage;
2235 }
2236 if( opt.min_version != -1 &&
Hanno Becker473f98f2019-06-26 10:27:32 +01002237 mbedtls_ssl_suite_get_max_minor_ver( ciphersuite_info ) < opt.min_version )
Paul Bakkerc1516be2013-06-29 16:01:32 +02002238 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002239 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002240 ret = 2;
2241 goto usage;
2242 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002243
2244 /* If we select a version that's not supported by
2245 * this suite, then there will be no common ciphersuite... */
2246 if( opt.max_version == -1 ||
Hanno Becker473f98f2019-06-26 10:27:32 +01002247 opt.max_version > mbedtls_ssl_suite_get_max_minor_ver( ciphersuite_info ) )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002248 {
Hanno Becker473f98f2019-06-26 10:27:32 +01002249 opt.max_version = mbedtls_ssl_suite_get_max_minor_ver( ciphersuite_info );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002250 }
Hanno Becker473f98f2019-06-26 10:27:32 +01002251 if( opt.min_version < mbedtls_ssl_suite_get_min_minor_ver( ciphersuite_info ) )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002252 {
Hanno Becker473f98f2019-06-26 10:27:32 +01002253 opt.min_version = mbedtls_ssl_suite_get_min_minor_ver( ciphersuite_info );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002254 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2256 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
2257 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002258 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002259
2260 /* Enable RC4 if needed and not explicitly disabled */
Hanno Becker473f98f2019-06-26 10:27:32 +01002261 if( mbedtls_ssl_suite_get_cipher( ciphersuite_info ) == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002266 ret = 2;
2267 goto usage;
2268 }
2269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002271 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002272 }
2273
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002274 if( opt.version_suites != NULL )
2275 {
2276 const char *name[4] = { 0 };
2277
2278 /* Parse 4-element coma-separated list */
2279 for( i = 0, p = (char *) opt.version_suites;
2280 i < 4 && *p != '\0';
2281 i++ )
2282 {
2283 name[i] = p;
2284
2285 /* Terminate the current string and move on to next one */
2286 while( *p != ',' && *p != '\0' )
2287 p++;
2288 if( *p == ',' )
2289 *p++ = '\0';
2290 }
2291
2292 if( i != 4 )
2293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 mbedtls_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002295 ret = 1;
2296 goto exit;
2297 }
2298
2299 memset( version_suites, 0, sizeof( version_suites ) );
2300
2301 /* Get the suites identifiers from their name */
2302 for( i = 0; i < 4; i++ )
2303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002305
2306 if( version_suites[i][0] == 0 )
2307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002309 ret = 2;
2310 goto usage;
2311 }
2312 }
2313 }
2314
Hanno Beckera5a2b082019-05-15 14:03:01 +01002315#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01002316 if( unhexify( cid, opt.cid_val, &cid_len ) != 0 )
2317 {
2318 mbedtls_printf( "CID not valid hex\n" );
2319 goto exit;
2320 }
Hanno Becker1029ace2019-04-09 17:28:10 +01002321
Hanno Becker96870292019-05-03 17:30:59 +01002322 /* Keep CID settings for renegotiation unless
2323 * specified otherwise. */
2324 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
2325 opt.cid_enabled_renego = opt.cid_enabled;
2326 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
2327 opt.cid_val_renego = opt.cid_val;
2328
2329 if( unhexify( cid_renego, opt.cid_val_renego, &cid_renego_len ) != 0 )
2330 {
2331 mbedtls_printf( "CID not valid hex\n" );
2332 goto exit;
2333 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002334#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1029ace2019-04-09 17:28:10 +01002335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002337 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002338 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02002339 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002340 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 mbedtls_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002343 goto exit;
2344 }
2345
2346 if( opt.psk_list != NULL )
2347 {
2348 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 mbedtls_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002351 goto exit;
2352 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002353 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002355
Hanno Beckerc1096e72019-06-19 12:30:41 +01002356#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Hanno Beckere6706e62017-05-15 16:05:15 +01002357 if( opt.curves != NULL )
2358 {
2359 p = (char *) opt.curves;
2360 i = 0;
2361
2362 if( strcmp( p, "none" ) == 0 )
2363 {
2364 curve_list[0] = MBEDTLS_ECP_DP_NONE;
2365 }
2366 else if( strcmp( p, "default" ) != 0 )
2367 {
2368 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01002369 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002370 {
2371 q = p;
2372
2373 /* Terminate the current string */
2374 while( *p != ',' && *p != '\0' )
2375 p++;
2376 if( *p == ',' )
2377 *p++ = '\0';
2378
2379 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
2380 {
2381 curve_list[i++] = curve_cur->grp_id;
2382 }
2383 else
2384 {
2385 mbedtls_printf( "unknown curve %s\n", q );
2386 mbedtls_printf( "supported curves: " );
2387 for( curve_cur = mbedtls_ecp_curve_list();
2388 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
2389 curve_cur++ )
2390 {
2391 mbedtls_printf( "%s ", curve_cur->name );
2392 }
2393 mbedtls_printf( "\n" );
2394 goto exit;
2395 }
2396 }
2397
2398 mbedtls_printf("Number of curves: %d\n", i );
2399
Hanno Becker8651a432017-06-09 16:13:22 +01002400 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002401 {
Hanno Becker8651a432017-06-09 16:13:22 +01002402 mbedtls_printf( "curves list too long, maximum %d",
2403 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01002404 goto exit;
2405 }
2406
2407 curve_list[i] = MBEDTLS_ECP_DP_NONE;
2408 }
2409 }
Hanno Beckerc1096e72019-06-19 12:30:41 +01002410#endif /* MBEDTLS_ECP_C && !MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Beckere6706e62017-05-15 16:05:15 +01002411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002413 if( opt.alpn_string != NULL )
2414 {
2415 p = (char *) opt.alpn_string;
2416 i = 0;
2417
2418 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01002419 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002420 {
2421 alpn_list[i++] = p;
2422
2423 /* Terminate the current string and move on to next one */
2424 while( *p != ',' && *p != '\0' )
2425 p++;
2426 if( *p == ',' )
2427 *p++ = '\0';
2428 }
2429 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002431
Paul Bakkerfbb17802013-04-17 19:10:21 +02002432 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002433 * 0. Initialize the RNG and the session data
2434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002436 fflush( stdout );
2437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 mbedtls_entropy_init( &entropy );
Hanno Becker7f1c8052019-08-26 13:30:13 +01002439#if defined(MBEDTLS_CTR_DRBG_C)
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002440 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
2441 &entropy, (const unsigned char *) pers,
2442 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002443 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002444 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
2445 -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002446 goto exit;
2447 }
Hanno Becker7f1c8052019-08-26 13:30:13 +01002448#else /* MBEDTLS_CTR_DRBG_C */
2449 if( ( ret = mbedtls_hmac_drbg_seed( &hmac_drbg,
2450 mbedtls_md_info_from_type(
2451 available_hashes[0] ),
2452 mbedtls_entropy_func,
2453 &entropy, (const unsigned char *) pers,
2454 strlen( pers ) ) ) != 0 )
2455 {
2456 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
2457 -ret );
2458 goto exit;
2459 }
2460#endif /* MBEDTLS_CTR_DRBG */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002465 /*
2466 * 1.1. Load the trusted CA
2467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002469 fflush( stdout );
2470
Hanno Becker7ae36e42019-03-05 16:22:07 +00002471 if( strcmp( opt.ca_path, "none" ) == 0 ||
2472 strcmp( opt.ca_file, "none" ) == 0 )
2473 {
2474 ret = 0;
2475 }
2476 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002478 if( strlen( opt.ca_path ) )
Hanno Becker7ae36e42019-03-05 16:22:07 +00002479 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002480 else if( strlen( opt.ca_file ) )
Hanno Becker7ae36e42019-03-05 16:22:07 +00002481 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02002482 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002483#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484#if defined(MBEDTLS_CERTS_C)
Hanno Becker38566cc2019-02-01 08:13:19 +00002485 {
2486#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 ret = mbedtls_x509_crt_parse( &cacert,
2490 (const unsigned char *) mbedtls_test_cas[i],
2491 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002492 if( ret != 0 )
2493 break;
2494 }
Hanno Becker38566cc2019-02-01 08:13:19 +00002495 if( ret == 0 )
2496#endif /* MBEDTLS_PEM_PARSE_C */
2497 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
2498 {
2499 ret = mbedtls_x509_crt_parse_der( &cacert,
2500 (const unsigned char *) mbedtls_test_cas_der[i],
2501 mbedtls_test_cas_der_len[i] );
2502 if( ret != 0 )
2503 break;
2504 }
2505 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002506#else
2507 {
2508 ret = 1;
Hanno Beckerc258c442018-12-05 16:49:42 +00002509 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002510 }
Hanno Becker38566cc2019-02-01 08:13:19 +00002511#endif /* MBEDTLS_CERTS_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002512 if( ret < 0 )
2513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002515 goto exit;
2516 }
2517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002519
2520 /*
2521 * 1.2. Load own certificate and private key
2522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002524 fflush( stdout );
2525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002527 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002528 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002529 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002533 -ret );
2534 goto exit;
2535 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002536 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002537 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002538 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002539 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002543 goto exit;
2544 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002545 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002546 if( key_cert_init == 1 )
2547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002549 goto exit;
2550 }
2551
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002552 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002553 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002554 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002558 -ret );
2559 goto exit;
2560 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002561 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002562 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002563 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002564 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002568 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002569 goto exit;
2570 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002571 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002572 if( key_cert_init2 == 1 )
2573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002575 goto exit;
2576 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002577#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002578 if( key_cert_init == 0 &&
2579 strcmp( opt.crt_file, "none" ) != 0 &&
2580 strcmp( opt.key_file, "none" ) != 0 &&
2581 key_cert_init2 == 0 &&
2582 strcmp( opt.crt_file2, "none" ) != 0 &&
2583 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585#if !defined(MBEDTLS_CERTS_C)
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002586 mbedtls_printf( "Not certificated or key provided, and \nMBEDTLS_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002587 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002588#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589#if defined(MBEDTLS_RSA_C)
2590 if( ( ret = mbedtls_x509_crt_parse( &srvcert,
2591 (const unsigned char *) mbedtls_test_srv_crt_rsa,
2592 mbedtls_test_srv_crt_rsa_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002593 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002594 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
2595 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002596 goto exit;
2597 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 if( ( ret = mbedtls_pk_parse_key( &pkey,
2599 (const unsigned char *) mbedtls_test_srv_key_rsa,
2600 mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002601 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002602 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
2603 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002604 goto exit;
2605 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002606 key_cert_init = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607#endif /* MBEDTLS_RSA_C */
2608#if defined(MBEDTLS_ECDSA_C)
2609 if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
2610 (const unsigned char *) mbedtls_test_srv_crt_ec,
2611 mbedtls_test_srv_crt_ec_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002612 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002613 mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n",
2614 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002615 goto exit;
2616 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 if( ( ret = mbedtls_pk_parse_key( &pkey2,
2618 (const unsigned char *) mbedtls_test_srv_key_ec,
2619 mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002620 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002621 mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n",
2622 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002623 goto exit;
2624 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002625 key_cert_init2 = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626#endif /* MBEDTLS_ECDSA_C */
2627#endif /* MBEDTLS_CERTS_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002628 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 mbedtls_printf( " ok\n" );
2631#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002634 if( opt.dhm_file != NULL )
2635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 mbedtls_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002637 fflush( stdout );
2638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002642 -ret );
2643 goto exit;
2644 }
2645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002647 }
2648#endif
2649
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002650#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002651 if( opt.sni != NULL )
2652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 mbedtls_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002654 fflush( stdout );
2655
2656 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
2657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002659 goto exit;
2660 }
2661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002663 }
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002664#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002665
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002666 /*
2667 * 2. Setup the listening TCP socket
2668 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02002669 mbedtls_printf( " . Bind on %s://%s:%s/ ...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002671 opt.server_addr ? opt.server_addr : "*",
2672 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002673 fflush( stdout );
2674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
2676 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2677 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002680 goto exit;
2681 }
2682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002684
2685 /*
2686 * 3. Setup stuff
2687 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002689 fflush( stdout );
2690
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02002691 if( ( ret = mbedtls_ssl_config_defaults( &conf,
2692 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02002693 opt.transport,
2694 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002695 {
2696 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
2697 goto exit;
2698 }
2699
Gilles Peskineef86ab22017-05-05 18:59:02 +02002700#if defined(MBEDTLS_X509_CRT_PARSE_C)
2701 /* The default algorithms profile disables SHA-1, but our tests still
2702 rely on it heavily. Hence we allow it here. A real-world server
2703 should use the default profile unless there is a good reason not to. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02002704 if( opt.allow_sha1 > 0 )
2705 {
2706 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
2707 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
Hanno Becker56595f42019-06-19 16:31:38 +01002708#if !defined(MBEDTLS_SSL_CONF_SINGLE_HASH)
Hanno Becker7f1c8052019-08-26 13:30:13 +01002709 mbedtls_ssl_conf_sig_hashes( &conf, available_hashes );
Hanno Becker56595f42019-06-19 16:31:38 +01002710#endif
Gilles Peskinebc70a182017-05-09 15:59:24 +02002711 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02002712#endif /* MBEDTLS_X509_CRT_PARSE_C */
2713
Hanno Beckeracd4fc02019-06-12 16:40:50 +01002714#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01002715 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002716 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Hanno Beckeracd4fc02019-06-12 16:40:50 +01002717#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002718
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01002719#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath4817e272017-04-10 13:44:33 +01002720 if( opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST )
2721 mbedtls_ssl_conf_cert_req_ca_list( &conf, opt.cert_req_ca_list );
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01002722#endif
Janos Follath4817e272017-04-10 13:44:33 +01002723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002725 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 +02002726 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002727
Hanno Beckere7675d02018-08-14 13:28:56 +01002728 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01002729 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002733 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002734 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002735 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002736 goto exit;
2737 };
Paul Bakker05decb22013-08-15 13:33:48 +02002738#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002739
Hanno Beckere0200da2019-06-13 09:23:43 +01002740#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
2741 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
2742 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Becker96870292019-05-03 17:30:59 +01002743 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckereec2be92019-05-03 13:06:44 +01002744 {
Hanno Becker96870292019-05-03 17:30:59 +01002745 if( opt.cid_enabled == 1 &&
2746 opt.cid_enabled_renego == 1 &&
2747 cid_len != cid_renego_len )
2748 {
2749 mbedtls_printf( "CID length must not change during renegotiation\n" );
2750 goto usage;
2751 }
2752
2753 if( opt.cid_enabled == 1 )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002754 ret = mbedtls_ssl_conf_cid( &conf, cid_len,
2755 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Becker96870292019-05-03 17:30:59 +01002756 else
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002757 ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
2758 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Becker96870292019-05-03 17:30:59 +01002759
Hanno Beckereec2be92019-05-03 13:06:44 +01002760 if( ret != 0 )
2761 {
Hanno Becker76581052019-05-23 16:58:22 +01002762 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
2763 -ret );
Hanno Beckereec2be92019-05-03 13:06:44 +01002764 goto exit;
2765 }
2766 }
Hanno Beckere0200da2019-06-13 09:23:43 +01002767#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
2768 !MBEDTLS_SSL_CONF_CID_LEN &&
2769 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +01002770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002772 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002773 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002774#endif
2775
Hanno Beckerf765ce62019-06-21 13:17:14 +01002776#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
2777 !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET) && \
2778 !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002779 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002780 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Jarno Lamsa41b35912019-06-10 15:51:11 +03002781 if( opt.enforce_extended_master_secret != DFL_EXTENDED_MS_ENFORCE )
2782 mbedtls_ssl_conf_extended_master_secret_enforce( &conf,
2783 opt.enforce_extended_master_secret );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002784#endif
2785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002787 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002788 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002789#endif
2790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002792 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002793 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002794 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002795 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002796 goto exit;
2797 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002798#endif
2799
Hanno Becker7f1c8052019-08-26 13:30:13 +01002800#if defined(MBEDTLS_CTR_DRBG_C)
Hanno Beckerece325c2019-06-13 15:39:27 +01002801#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002802 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
Hanno Beckerece325c2019-06-13 15:39:27 +01002803#else
Hanno Becker572d4482019-07-23 13:47:53 +01002804 rng_ctx_global = &ctr_drbg;
Hanno Beckerece325c2019-06-13 15:39:27 +01002805#endif
Hanno Becker7f1c8052019-08-26 13:30:13 +01002806#else /* MBEDTLS_CTR_DRBG_C */
2807#if !defined(MBEDTLS_SSL_CONF_RNG)
2808 mbedtls_ssl_conf_rng( &conf, mbedtls_hmac_drbg_random, &hmac_drbg );
2809#else
2810 rng_ctx_global = &hmac_drbg;
2811#endif
2812#endif /* MBEDTLS_CTR_DRBG_C */
Hanno Beckerece325c2019-06-13 15:39:27 +01002813
Hanno Becker14a4a442019-07-02 17:00:34 +01002814#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002815 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Hanno Becker14a4a442019-07-02 17:00:34 +01002816#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002819 if( opt.cache_max != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002821
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002822 if( opt.cache_timeout != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002824
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03002825#if !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002826 mbedtls_ssl_conf_session_cache( &conf, &cache,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01002827 mbedtls_ssl_cache_get,
2828 mbedtls_ssl_cache_set );
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03002829#endif /* !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00002830#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002833 if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002834 {
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002835 if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
Hanno Becker7f1c8052019-08-26 13:30:13 +01002836#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002837 mbedtls_ctr_drbg_random, &ctr_drbg,
Hanno Becker7f1c8052019-08-26 13:30:13 +01002838#else
2839 mbedtls_hmac_drbg_random, &hmac_drbg,
2840#endif
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +02002841 MBEDTLS_CIPHER_AES_256_GCM,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002842 opt.ticket_timeout ) ) != 0 )
2843 {
2844 mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
2845 goto exit;
2846 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01002847
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002848 mbedtls_ssl_conf_session_tickets_cb( &conf,
2849 mbedtls_ssl_ticket_write,
2850 mbedtls_ssl_ticket_parse,
2851 &ticket_ctx );
2852 }
Paul Bakkera503a632013-08-14 13:48:06 +02002853#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855#if defined(MBEDTLS_SSL_PROTO_DTLS)
2856 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002859 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
Hanno Becker7f1c8052019-08-26 13:30:13 +01002862#if defined(MBEDTLS_CTR_DRBG_C)
2863 mbedtls_ctr_drbg_random, &ctr_drbg
2864#else
2865 mbedtls_hmac_drbg_random, &hmac_drbg
2866#endif /* MBEDTLS_CTR_DRBG_C */
2867 ) ) != 0 )
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869 mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002870 goto exit;
2871 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002872
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002873 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002874 &cookie_ctx );
2875 }
2876 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877#endif /* MBEDTLS_SSL_COOKIE_C */
2878#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002879 if( opt.cookies == 0 )
2880 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002881 mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002882 }
2883 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002885 {
2886 ; /* Nothing to do */
2887 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002888
Hanno Becker7f376f42019-06-12 16:20:48 +01002889#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
2890 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002891 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002892 mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002893#endif
2894
Hanno Beckerde671542019-06-12 16:30:46 +01002895#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
2896 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002897 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002898 mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002899#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002900 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002902
Hanno Becker73f4cb12019-06-27 13:51:07 +01002903#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Paul Bakker41c83d32013-03-20 14:39:14 +01002904 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002905 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Hanno Becker73f4cb12019-06-27 13:51:07 +01002906#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002907
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002908#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002909 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002910 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002911#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002912
Hanno Becker73f4cb12019-06-27 13:51:07 +01002913#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002914 if( opt.version_suites != NULL )
2915 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002916 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 MBEDTLS_SSL_MAJOR_VERSION_3,
2918 MBEDTLS_SSL_MINOR_VERSION_0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002919 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_MAJOR_VERSION_3,
2921 MBEDTLS_SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002922 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_MAJOR_VERSION_3,
2924 MBEDTLS_SSL_MINOR_VERSION_2 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002925 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 MBEDTLS_SSL_MAJOR_VERSION_3,
2927 MBEDTLS_SSL_MINOR_VERSION_3 );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002928 }
Hanno Becker73f4cb12019-06-27 13:51:07 +01002929#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002930
Hanno Beckerb0b2b672019-06-12 16:58:10 +01002931#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002932 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002933 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Hanno Beckerb0b2b672019-06-12 16:58:10 +01002934#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002936 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002937
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02002938 if( opt.renego_delay != DFL_RENEGO_DELAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002939 mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002940
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002941 if( opt.renego_period != DFL_RENEGO_PERIOD )
2942 {
Andres AG692ad842017-01-19 16:30:57 +00002943 PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002944 mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002945 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002946#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002949 if( strcmp( opt.ca_path, "none" ) != 0 &&
2950 strcmp( opt.ca_file, "none" ) != 0 )
2951 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002952 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002953 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002954 if( key_cert_init )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002955 {
2956 mbedtls_pk_context *pk = &pkey;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002957#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002958 if( opt.async_private_delay1 >= 0 )
2959 {
Gilles Peskine44817442018-06-13 18:09:28 +02002960 ret = ssl_async_set_key( &ssl_async_keys, &srvcert, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002961 opt.async_private_delay1 );
2962 if( ret < 0 )
2963 {
2964 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2965 ret );
2966 goto exit;
2967 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002968 pk = NULL;
2969 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002970#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002971 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002972 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002973 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002974 goto exit;
2975 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002976 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002977 if( key_cert_init2 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002978 {
2979 mbedtls_pk_context *pk = &pkey2;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002980#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002981 if( opt.async_private_delay2 >= 0 )
2982 {
Gilles Peskine44817442018-06-13 18:09:28 +02002983 ret = ssl_async_set_key( &ssl_async_keys, &srvcert2, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002984 opt.async_private_delay2 );
2985 if( ret < 0 )
2986 {
2987 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2988 ret );
2989 goto exit;
2990 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002991 pk = NULL;
2992 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002993#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002994 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002995 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002996 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002997 goto exit;
2998 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002999 }
3000
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003001#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003002 if( opt.async_operations[0] != '-' )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003003 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003004 mbedtls_ssl_async_sign_t *sign = NULL;
3005 mbedtls_ssl_async_decrypt_t *decrypt = NULL;
Gilles Peskine12ab5d42018-04-24 12:32:04 +02003006 const char *r;
3007 for( r = opt.async_operations; *r; r++ )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003008 {
Gilles Peskine12ab5d42018-04-24 12:32:04 +02003009 switch( *r )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003010 {
3011 case 'd':
3012 decrypt = ssl_async_decrypt;
3013 break;
3014 case 's':
3015 sign = ssl_async_sign;
3016 break;
3017 }
3018 }
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003019 ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
3020 - opt.async_private_error :
3021 opt.async_private_error );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003022 ssl_async_keys.f_rng = mbedtls_ctr_drbg_random;
3023 ssl_async_keys.p_rng = &ctr_drbg;
3024 mbedtls_ssl_conf_async_private_cb( &conf,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003025 sign,
3026 decrypt,
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003027 ssl_async_resume,
3028 ssl_async_cancel,
3029 &ssl_async_keys );
3030 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003031#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003032#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +02003033
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02003034#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003035 if( opt.sni != NULL )
Gilles Peskine166ce742018-04-30 10:30:49 +02003036 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003037 mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
Gilles Peskine166ce742018-04-30 10:30:49 +02003038#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3039 if( opt.async_private_delay2 >= 0 )
3040 {
Gilles Peskinee2479892018-06-13 18:06:51 +02003041 sni_entry *cur;
3042 for( cur = sni_info; cur != NULL; cur = cur->next )
Gilles Peskine166ce742018-04-30 10:30:49 +02003043 {
Gilles Peskinee2479892018-06-13 18:06:51 +02003044 ret = ssl_async_set_key( &ssl_async_keys,
Gilles Peskine44817442018-06-13 18:09:28 +02003045 cur->cert, cur->key, 1,
Gilles Peskinee2479892018-06-13 18:06:51 +02003046 opt.async_private_delay2 );
3047 if( ret < 0 )
3048 {
3049 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
3050 ret );
3051 goto exit;
3052 }
3053 cur->key = NULL;
Gilles Peskine166ce742018-04-30 10:30:49 +02003054 }
Gilles Peskine166ce742018-04-30 10:30:49 +02003055 }
3056#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3057 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003058#endif
3059
Hanno Beckere6706e62017-05-15 16:05:15 +01003060#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01003061#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Hanno Beckere6706e62017-05-15 16:05:15 +01003062 if( opt.curves != NULL &&
3063 strcmp( opt.curves, "default" ) != 0 )
3064 {
3065 mbedtls_ssl_conf_curves( &conf, curve_list );
3066 }
Hanno Beckerc1096e72019-06-19 12:30:41 +01003067#endif /* !MBEDTLS_SSL_CONF_SINGLE_EC */
3068#endif /* MBEDTLS_ECP_C */
Hanno Beckere6706e62017-05-15 16:05:15 +01003069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02003071 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
3072 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003073 ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02003074 (const unsigned char *) opt.psk_identity,
3075 strlen( opt.psk_identity ) );
3076 if( ret != 0 )
3077 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003078 mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02003079 goto exit;
3080 }
3081 }
3082
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02003083 if( opt.psk_list != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003084 mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02003085#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087#if defined(MBEDTLS_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00003088 /*
3089 * Use different group than default DHM group
3090 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003092 if( opt.dhm_file != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003093 ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003094#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003095 if( ret != 0 )
3096 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003097 mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003098 goto exit;
3099 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003100#endif
3101
Hanno Becker72e5ffc2019-07-05 11:35:08 +01003102#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
3103 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) || \
3104 !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
3105 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02003106 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003107 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00003108
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02003109 if( opt.max_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003110 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Hanno Becker72e5ffc2019-07-05 11:35:08 +01003111#endif
Paul Bakkerc1516be2013-06-29 16:01:32 +02003112
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003113 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
3114 {
3115 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
3116 goto exit;
3117 }
3118
Hanno Beckera58a8962019-06-13 16:11:15 +01003119#if !defined(MBEDTLS_SSL_CONF_RECV) && \
3120 !defined(MBEDTLS_SSL_CONF_SEND) && \
3121 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Hanno Beckerfe24b3b2019-07-03 17:05:43 +01003122 io_ctx.ssl = &ssl;
3123 io_ctx.net = &client_fd;
3124 mbedtls_ssl_set_bio( &ssl, &io_ctx, send_cb, recv_cb,
3125 opt.nbio == 0 ? recv_timeout_cb : NULL );
Hanno Beckera58a8962019-06-13 16:11:15 +01003126#else
3127 mbedtls_ssl_set_bio_ctx( &ssl, &client_fd );
3128#endif
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003129
Hanno Beckera5a2b082019-05-15 14:03:01 +01003130#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1029ace2019-04-09 17:28:10 +01003131 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3132 {
3133 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
3134 cid, cid_len ) ) != 0 )
3135 {
3136 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
3137 ret );
3138 goto exit;
3139 }
3140 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003141#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1029ace2019-04-09 17:28:10 +01003142
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02003143#if defined(MBEDTLS_SSL_PROTO_DTLS)
3144 if( opt.dtls_mtu != DFL_DTLS_MTU )
3145 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
3146#endif
3147
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003148#if defined(MBEDTLS_TIMING_C)
Hanno Becker0ae6b242019-06-13 16:45:36 +01003149#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
3150 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02003151 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
3152 mbedtls_timing_get_delay );
Hanno Becker0ae6b242019-06-13 16:45:36 +01003153#else
3154 mbedtls_ssl_set_timer_cb_ctx( &ssl, &timer );
3155#endif
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003156#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02003157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003159
3160reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02003161#if !defined(_WIN32)
3162 if( received_sigterm )
3163 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01003164 mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" );
3165 if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT )
3166 ret = 0;
3167
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02003168 goto exit;
3169 }
3170#endif
3171
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003172 if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
3173 {
3174 mbedtls_printf( " ! Client initiated reconnection from same port\n" );
3175 goto handshake;
3176 }
3177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003179 if( ret != 0 )
3180 {
3181 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 mbedtls_strerror( ret, error_buf, 100 );
3183 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003184 }
3185#endif
3186
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003187 mbedtls_net_free( &client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01003188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 mbedtls_ssl_session_reset( &ssl );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003190
3191 /*
3192 * 3. Wait until a client connects
3193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 mbedtls_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003195 fflush( stdout );
3196
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003197 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02003198 client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003199 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02003200#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003201 if( received_sigterm )
3202 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01003203 mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" );
3204 if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED )
3205 ret = 0;
3206
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003207 goto exit;
3208 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02003209#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211 mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003212 goto exit;
3213 }
3214
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003215 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003216 ret = mbedtls_net_set_nonblock( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003217 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003218 ret = mbedtls_net_set_block( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003219 if( ret != 0 )
3220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003222 goto exit;
3223 }
3224
Hanno Becker1f835fa2019-06-13 10:14:59 +01003225#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003226 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Hanno Becker1f835fa2019-06-13 10:14:59 +01003227#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
3230 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003231 {
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02003232 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
3233 client_ip, cliip_len ) ) != 0 )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003234 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01003235 mbedtls_printf( " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n",
3236 -ret );
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003237 goto exit;
3238 }
3239 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003241
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02003242#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3243 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
3244 {
3245 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
3246 (const unsigned char *) opt.ecjpake_pw,
3247 strlen( opt.ecjpake_pw ) ) ) != 0 )
3248 {
3249 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
3250 goto exit;
3251 }
3252 }
3253#endif
3254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003256
3257 /*
3258 * 4. Handshake
3259 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003260handshake:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003262 fflush( stdout );
3263
Hanno Becker16970d22017-10-10 15:56:37 +01003264 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003265 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003266#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003267 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003268 ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003269 {
3270 mbedtls_printf( " cancelling on injected error\n" );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003271 break;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003272 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003273#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskineb44692f2018-04-24 12:18:19 +02003274
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003275 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003276 break;
3277
3278 /* For event-driven IO, wait for socket to become available */
3279 if( opt.event == 1 /* level triggered IO */ )
3280 {
3281#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003282 ret = idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003283#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003284 ret = idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003285#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003286 if( ret != 0 )
3287 goto reset;
Hanno Becker16970d22017-10-10 15:56:37 +01003288 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003289 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293 mbedtls_printf( " hello verification requested\n" );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003294 ret = 0;
3295 goto reset;
3296 }
3297 else if( ret != 0 )
3298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02003300
Hanno Becker02a21932019-06-10 15:08:43 +01003301#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02003302 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3303 {
3304 char vrfy_buf[512];
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02003305 flags = mbedtls_ssl_get_verify_result( &ssl );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02003306
3307 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
3308
3309 mbedtls_printf( "%s\n", vrfy_buf );
3310 }
3311#endif
3312
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003313#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003314 if( opt.async_private_error < 0 )
3315 /* Injected error only the first time round, to test reset */
3316 ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
3317#endif
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003318 goto reset;
3319 }
3320 else /* ret == 0 */
3321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
3323 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003324 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
3327 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02003328 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02003330
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003331#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3332 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
3333 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
3334#endif
3335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003337 if( opt.alpn_string != NULL )
3338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
3340 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003341 alp ? alp : "(none)" );
3342 }
3343#endif
3344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003346 /*
Ron Eldor2a47be52017-06-20 15:23:23 +03003347 * 5. Verify the client certificate
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003350
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003351 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003352 {
Hanno Becker02a21932019-06-10 15:08:43 +01003353#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003354 char vrfy_buf[512];
Peter Kolbusdc470ae2018-12-11 13:55:56 -06003355#endif
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 mbedtls_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003358
Hanno Becker02a21932019-06-10 15:08:43 +01003359#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003360 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003361
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003362 mbedtls_printf( "%s\n", vrfy_buf );
Peter Kolbusdc470ae2018-12-11 13:55:56 -06003363#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003364 }
3365 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003367
Hanno Becker02a21932019-06-10 15:08:43 +01003368#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003369 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003370 {
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003371 char crt_buf[512];
3372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 mbedtls_printf( " . Peer certificate information ...\n" );
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003374 mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 mbedtls_ssl_get_peer_cert( &ssl ) );
Manuel Pégourié-Gonnard67557172015-07-02 11:15:48 +02003376 mbedtls_printf( "%s\n", crt_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003377 }
Hanno Becker02a21932019-06-10 15:08:43 +01003378#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003380
Hanno Beckera5a2b082019-05-15 14:03:01 +01003381#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01003382 ret = report_cid_usage( &ssl, "initial handshake" );
3383 if( ret != 0 )
3384 goto exit;
3385
Hanno Becker1029ace2019-04-09 17:28:10 +01003386 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3387 {
Hanno Becker96870292019-05-03 17:30:59 +01003388 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
3389 cid_renego, cid_renego_len ) ) != 0 )
Hanno Becker1029ace2019-04-09 17:28:10 +01003390 {
Hanno Becker96870292019-05-03 17:30:59 +01003391 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
3392 ret );
Hanno Becker1029ace2019-04-09 17:28:10 +01003393 goto exit;
3394 }
Hanno Becker1029ace2019-04-09 17:28:10 +01003395 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003396#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1029ace2019-04-09 17:28:10 +01003397
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02003398 if( opt.exchanges == 0 )
3399 goto close_notify;
3400
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003401 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003402data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003403 /*
3404 * 6. Read the HTTP Request
3405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406 mbedtls_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003407 fflush( stdout );
3408
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003409 /*
3410 * TLS and DTLS need different reading styles (stream vs datagram)
3411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003413 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003414 do
3415 {
3416 int terminated = 0;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003417 len = opt.buffer_size - 1;
3418 memset( buf, 0, opt.buffer_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003420
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003421 if( mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003422 {
3423 if( opt.event == 1 /* level triggered IO */ )
3424 {
3425#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003426 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003427#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003428 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003429#endif
3430 }
3431
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003432 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01003433 }
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003434
3435 if( ret <= 0 )
3436 {
3437 switch( ret )
3438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3440 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003441 goto close_notify;
3442
3443 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003444 case MBEDTLS_ERR_NET_CONN_RESET:
3445 mbedtls_printf( " connection was reset by peer\n" );
3446 ret = MBEDTLS_ERR_NET_CONN_RESET;
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003447 goto reset;
3448
3449 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003451 goto reset;
3452 }
3453 }
3454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455 if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003456 {
3457 len = ret;
3458 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003459 mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003460
3461 /* End of message should be detected according to the syntax of the
3462 * application protocol (eg HTTP), just use a dummy test here. */
3463 if( buf[len - 1] == '\n' )
3464 terminated = 1;
3465 }
3466 else
3467 {
3468 int extra_len, ori_len;
3469 unsigned char *larger_buf;
3470
3471 ori_len = ret;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02003472 extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003473
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003474 larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003475 if( larger_buf == NULL )
3476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477 mbedtls_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003478 ret = 1;
3479 goto reset;
3480 }
3481
3482 memset( larger_buf, 0, ori_len + extra_len );
3483 memcpy( larger_buf, buf, ori_len );
3484
3485 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003487 if( ret != extra_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488 mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490 mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003491 ret = 1;
3492 goto reset;
3493 }
3494
3495 larger_buf[ori_len + extra_len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496 mbedtls_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003497 ori_len + extra_len, ori_len, extra_len,
3498 (char *) larger_buf );
3499
3500 /* End of message should be detected according to the syntax of the
3501 * application protocol (eg HTTP), just use a dummy test here. */
3502 if( larger_buf[ori_len + extra_len - 1] == '\n' )
3503 terminated = 1;
3504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505 mbedtls_free( larger_buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003506 }
3507
3508 if( terminated )
3509 {
3510 ret = 0;
3511 break;
3512 }
3513 }
3514 while( 1 );
3515 }
3516 else /* Not stream, so datagram */
3517 {
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003518 len = opt.buffer_size - 1;
3519 memset( buf, 0, opt.buffer_size );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003520
Gilles Peskineb44692f2018-04-24 12:18:19 +02003521 do
Hanno Becker16970d22017-10-10 15:56:37 +01003522 {
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003523 /* Without the call to `mbedtls_ssl_check_pending`, it might
3524 * happen that the client sends application data in the same
3525 * datagram as the Finished message concluding the handshake.
3526 * In this case, the application data would be ready to be
3527 * processed while the underlying transport wouldn't signal
3528 * any further incoming data.
3529 *
3530 * See the test 'Event-driven I/O: session-id resume, UDP packing'
3531 * in tests/ssl-opt.sh.
3532 */
3533
3534 /* For event-driven IO, wait for socket to become available */
3535 if( mbedtls_ssl_check_pending( &ssl ) == 0 &&
3536 opt.event == 1 /* level triggered IO */ )
3537 {
3538#if defined(MBEDTLS_TIMING_C)
3539 idle( &client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ );
3540#else
3541 idle( &client_fd, MBEDTLS_ERR_SSL_WANT_READ );
3542#endif
3543 }
3544
Hanno Becker16970d22017-10-10 15:56:37 +01003545 ret = mbedtls_ssl_read( &ssl, buf, len );
3546
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003547 /* Note that even if `mbedtls_ssl_check_pending` returns true,
3548 * it can happen that the subsequent call to `mbedtls_ssl_read`
3549 * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
3550 * might be discarded (e.g. because they are retransmissions). */
Hanno Becker16970d22017-10-10 15:56:37 +01003551 }
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003552 while( mbedtls_status_is_ssl_in_progress( ret ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003553
3554 if( ret <= 0 )
3555 {
3556 switch( ret )
3557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003558 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3559 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003560 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003561 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003562
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003563 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003565 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003566 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003567 }
3568
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003569 len = ret;
3570 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003572 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003573 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003574
3575 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003576 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02003577 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00003580 if( opt.renegotiate && exchanges_left == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 mbedtls_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003583 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003586 {
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003587 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003590 goto reset;
3591 }
Hanno Becker16970d22017-10-10 15:56:37 +01003592
3593 /* For event-driven IO, wait for socket to become available */
3594 if( opt.event == 1 /* level triggered IO */ )
3595 {
3596#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003597 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003598#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003599 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003600#endif
3601 }
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003602 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003605 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003606#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003607
Hanno Beckera5a2b082019-05-15 14:03:01 +01003608#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01003609 ret = report_cid_usage( &ssl, "after renegotiation" );
3610 if( ret != 0 )
3611 goto exit;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003612#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker96870292019-05-03 17:30:59 +01003613
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003614 /*
3615 * 7. Write the 200 Response
3616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617 mbedtls_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003618 fflush( stdout );
3619
3620 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003622
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003623 /* Add padding to the response to reach opt.response_size in length */
3624 if( opt.response_size != DFL_RESPONSE_SIZE &&
3625 len < opt.response_size )
3626 {
3627 memset( buf + len, 'B', opt.response_size - len );
3628 len += opt.response_size - len;
3629 }
3630
3631 /* Truncate if response size is smaller than the "natural" size */
3632 if( opt.response_size != DFL_RESPONSE_SIZE &&
3633 len > opt.response_size )
3634 {
3635 len = opt.response_size;
3636
3637 /* Still end with \r\n unless that's really not possible */
3638 if( len >= 2 ) buf[len - 2] = '\r';
3639 if( len >= 1 ) buf[len - 1] = '\n';
3640 }
3641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003643 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003644 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003647 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003652 goto reset;
3653 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003654
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003655 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003658 goto reset;
3659 }
Hanno Becker16970d22017-10-10 15:56:37 +01003660
3661 /* For event-driven IO, wait for socket to become available */
3662 if( opt.event == 1 /* level triggered IO */ )
3663 {
3664#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003665 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003666#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003667 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003668#endif
3669 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003670 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003671 }
3672 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003673 else /* Not stream, so datagram */
3674 {
Hanno Becker16970d22017-10-10 15:56:37 +01003675 while( 1 )
3676 {
3677 ret = mbedtls_ssl_write( &ssl, buf, len );
3678
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003679 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003680 break;
3681
3682 /* For event-driven IO, wait for socket to become available */
3683 if( opt.event == 1 /* level triggered IO */ )
3684 {
3685#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003686 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003687#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003688 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003689#endif
3690 }
3691 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003692
3693 if( ret < 0 )
3694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003696 goto reset;
3697 }
3698
3699 frags = 1;
3700 written = ret;
3701 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003702
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003703 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704 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 +01003705 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01003706
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003707 /*
Jarno Lamsaf4572932019-05-29 15:41:21 +03003708 * 7b. Simulate serialize/deserialize and go back to data exchange
3709 */
Jarno Lamsacf1b6722019-06-04 11:06:31 +03003710#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003711 if( opt.serialize != 0 )
Jarno Lamsaf4572932019-05-29 15:41:21 +03003712 {
Jarno Lamsa034ae842019-06-06 15:10:07 +03003713 size_t buf_len;
Jarno Lamsaf4572932019-05-29 15:41:21 +03003714
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02003715 mbedtls_printf( " . Serializing live connection..." );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003716
Jarno Lamsa034ae842019-06-06 15:10:07 +03003717 ret = mbedtls_ssl_context_save( &ssl, NULL, 0, &buf_len );
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02003718 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jarno Lamsaf4572932019-05-29 15:41:21 +03003719 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003720 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
3721 "-0x%x\n\n", -ret );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003722
3723 goto exit;
3724 }
3725
Jarno Lamsa034ae842019-06-06 15:10:07 +03003726 if( ( context_buf = mbedtls_calloc( 1, buf_len ) ) == NULL )
Jarno Lamsaf4572932019-05-29 15:41:21 +03003727 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003728 mbedtls_printf( " failed\n ! Couldn't allocate buffer for "
3729 "serialized context" );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003730
3731 goto exit;
3732 }
Manuel Pégourié-Gonnardc9812292019-07-15 10:31:11 +02003733 context_buf_len = buf_len;
Jarno Lamsaf4572932019-05-29 15:41:21 +03003734
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03003735 if( ( ret = mbedtls_ssl_context_save( &ssl, context_buf,
3736 buf_len, &buf_len ) ) != 0 )
Jarno Lamsaf4572932019-05-29 15:41:21 +03003737 {
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02003738 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003739 "-0x%x\n\n", -ret );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003740
3741 goto exit;
3742 }
3743
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02003744 mbedtls_printf( " ok\n" );
3745
3746 /*
3747 * This simulates a workflow where you have a long-lived server
3748 * instance, potentially with a pool of ssl_context objects, and you
3749 * just want to re-use one while the connection is inactive: in that
3750 * case you can just reset() it, and then it's ready to receive
3751 * serialized data from another connection (or the same here).
3752 */
3753 if( opt.serialize == 1 )
3754 {
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +02003755 /* nothing to do here, done by context_save() already */
3756 mbedtls_printf( " . Context has been reset... ok" );
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02003757 }
3758
3759 /*
3760 * This simulates a workflow where you have one server instance per
3761 * connection, and want to release it entire when the connection is
3762 * inactive, and spawn it again when needed again - this would happen
3763 * between ssl_free() and ssl_init() below, together with any other
3764 * teardown/startup code needed - for example, preparing the
3765 * ssl_config again (see section 3 "setup stuff" in this file).
3766 */
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003767 if( opt.serialize == 2 )
3768 {
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02003769 mbedtls_printf( " . Freeing and reinitializing context..." );
3770
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003771 mbedtls_ssl_free( &ssl );
3772
3773 mbedtls_ssl_init( &ssl );
3774
3775 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
3776 {
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03003777 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned "
3778 "-0x%x\n\n", -ret );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003779 goto exit;
3780 }
3781
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02003782 /*
3783 * This illustrates the minimum amount of things you need to set
Hanno Becker41e5a682019-07-19 17:07:30 +01003784 * up: I/O and timer callbacks/contexts; however you could set up
3785 * much more if desired, for example if you want to share your set
3786 * up code between the case of establishing a new connection and
3787 * this case.
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02003788 */
Hanno Becker41e5a682019-07-19 17:07:30 +01003789#if !defined(MBEDTLS_SSL_CONF_RECV) && \
3790 !defined(MBEDTLS_SSL_CONF_SEND) && \
3791 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Hanno Beckerfe24b3b2019-07-03 17:05:43 +01003792 mbedtls_ssl_set_bio( &ssl, &io_ctx, send_cb, recv_cb,
3793 opt.nbio == 0 ? recv_timeout_cb : NULL );
Hanno Becker41e5a682019-07-19 17:07:30 +01003794#else
3795 mbedtls_ssl_set_bio_ctx( &ssl, &client_fd );
3796#endif
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003797
Jarno Lamsa29a15c22019-06-13 11:45:06 +03003798#if defined(MBEDTLS_TIMING_C)
Hanno Becker0ae6b242019-06-13 16:45:36 +01003799#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
3800 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard86dfa0c2019-07-15 12:23:22 +02003801 mbedtls_ssl_set_timer_cb( &ssl, &timer,
3802 mbedtls_timing_set_delay,
3803 mbedtls_timing_get_delay );
Hanno Becker0ae6b242019-06-13 16:45:36 +01003804#else
Manuel Pégourié-Gonnard86dfa0c2019-07-15 12:23:22 +02003805 mbedtls_ssl_set_timer_cb_ctx( &ssl, &timer );
Hanno Becker0ae6b242019-06-13 16:45:36 +01003806#endif
Jarno Lamsa29a15c22019-06-13 11:45:06 +03003807#endif /* MBEDTLS_TIMING_C */
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02003808
3809 mbedtls_printf( " ok\n" );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003810 }
3811
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02003812 mbedtls_printf( " . Deserializing connection..." );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003813
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03003814 if( ( ret = mbedtls_ssl_context_load( &ssl, context_buf,
3815 buf_len ) ) != 0 )
Jarno Lamsaf4572932019-05-29 15:41:21 +03003816 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003817 mbedtls_printf( "failed\n ! mbedtls_ssl_context_load returned "
3818 "-0x%x\n\n", -ret );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003819
3820 goto exit;
3821 }
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02003822
Manuel Pégourié-Gonnardc9812292019-07-15 10:31:11 +02003823 mbedtls_free( context_buf );
3824 context_buf = NULL;
3825 context_buf_len = 0;
3826
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02003827 mbedtls_printf( " ok\n" );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003828 }
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003829#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jarno Lamsaf4572932019-05-29 15:41:21 +03003830
3831 /*
3832 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003833 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003834 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003835 goto data_exchange;
3836
3837 /*
3838 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003839 */
3840close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01003842
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003843 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003845 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003846 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848 mbedtls_printf( " done\n" );
Rich Evansf90016a2015-01-19 14:26:37 +00003849
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003850 goto reset;
3851
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003852 /*
3853 * Cleanup and exit
3854 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003855exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003857 if( ret != 0 )
3858 {
3859 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 mbedtls_strerror( ret, error_buf, 100 );
3861 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003862 }
3863#endif
3864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865 mbedtls_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003866 fflush( stdout );
3867
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003868 mbedtls_net_free( &client_fd );
3869 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +02003870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3872 mbedtls_dhm_free( &dhm );
Paul Bakkera317a982014-06-18 16:44:11 +02003873#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874#if defined(MBEDTLS_X509_CRT_PARSE_C)
3875 mbedtls_x509_crt_free( &cacert );
3876 mbedtls_x509_crt_free( &srvcert );
3877 mbedtls_pk_free( &pkey );
3878 mbedtls_x509_crt_free( &srvcert2 );
3879 mbedtls_pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02003880#endif
Gilles Peskine44817442018-06-13 18:09:28 +02003881#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3882 for( i = 0; (size_t) i < ssl_async_keys.slots_used; i++ )
3883 {
3884 if( ssl_async_keys.slots[i].pk_owned )
3885 {
3886 mbedtls_pk_free( ssl_async_keys.slots[i].pk );
3887 mbedtls_free( ssl_async_keys.slots[i].pk );
3888 ssl_async_keys.slots[i].pk = NULL;
3889 }
3890 }
3891#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02003892#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003893 sni_free( sni_info );
3894#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003895#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003896 psk_free( psk_info );
3897#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3899 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003900#endif
Paul Bakkered27a042013-04-18 22:46:23 +02003901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003903 mbedtls_ssl_config_free( &conf );
Hanno Becker7f1c8052019-08-26 13:30:13 +01003904#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905 mbedtls_ctr_drbg_free( &ctr_drbg );
Hanno Becker7f1c8052019-08-26 13:30:13 +01003906#else
3907 mbedtls_hmac_drbg_free( &hmac_drbg );
3908#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003909 mbedtls_entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003911#if defined(MBEDTLS_SSL_CACHE_C)
3912 mbedtls_ssl_cache_free( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00003913#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003914#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3915 mbedtls_ssl_ticket_free( &ticket_ctx );
3916#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003917#if defined(MBEDTLS_SSL_COOKIE_C)
3918 mbedtls_ssl_cookie_free( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02003919#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003920
Hanno Becker095d9cf2018-10-09 12:39:13 +01003921 mbedtls_free( buf );
3922
Manuel Pégourié-Gonnardc9812292019-07-15 10:31:11 +02003923#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
3924 if( context_buf != NULL )
3925 mbedtls_platform_zeroize( context_buf, context_buf_len );
3926 mbedtls_free( context_buf );
3927#endif
3928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003929#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
3930#if defined(MBEDTLS_MEMORY_DEBUG)
3931 mbedtls_memory_buffer_alloc_status();
Paul Bakker82024bf2013-07-04 11:52:32 +02003932#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003933 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +02003934#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02003935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003936 mbedtls_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003937
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003938#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003940 fflush( stdout ); getchar();
3941#endif
3942
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02003943 // Shell can not handle large exit numbers -> 1 for errors
3944 if( ret < 0 )
3945 ret = 1;
3946
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003947 return( ret );
3948}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
3950 MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Hanno Becker7f1c8052019-08-26 13:30:13 +01003951 ( MBEDTLS_CTR_DRBG_C || MBEDTLS_HMAC_DRBG_C ) */