blob: 36b78e59029e9a4e10493b0155da5a5e33a55149 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client with certificate authentication
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 Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +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 Bakker5121ce52009-01-03 21:22:43 +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
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Simon Butcherd3138c32016-04-27 01:26:50 +010032#include <stdlib.h>
33#define mbedtls_time time
34#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#define mbedtls_printf printf
36#define mbedtls_fprintf fprintf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define mbedtls_snprintf snprintf
Hanno Beckerd6d100b2019-03-30 06:27:43 +000038#define mbedtls_calloc calloc
39#define mbedtls_free free
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050040#define mbedtls_exit exit
41#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
42#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evansf90016a2015-01-19 14:26:37 +000043#endif
44
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020045#if !defined(MBEDTLS_ENTROPY_C) || \
46 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020047 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020048int main( void )
49{
50 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
51 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020052 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020053 return( 0 );
54}
55#else
56
Andres AG788aa4a2016-09-14 14:32:09 +010057#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/ssl.h"
59#include "mbedtls/entropy.h"
60#include "mbedtls/ctr_drbg.h"
61#include "mbedtls/certs.h"
62#include "mbedtls/x509.h"
63#include "mbedtls/error.h"
64#include "mbedtls/debug.h"
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020065#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Hanno Beckerb2b468b2018-11-12 17:46:59 +000067#if defined(MBEDTLS_USE_PSA_CRYPTO)
68#include "psa/crypto.h"
Hanno Becker1d911cd2018-11-15 13:06:09 +000069#include "mbedtls/psa_util.h"
Hanno Beckerb2b468b2018-11-12 17:46:59 +000070#endif
71
Rich Evans18b78c72015-02-11 14:06:19 +000072#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010075
Hanno Beckere4ad3e82017-09-18 15:05:46 +010076#define MAX_REQUEST_SIZE 20000
77#define MAX_REQUEST_SIZE_STR "20000"
78
Paul Bakker9caf2d22010-02-18 19:37:19 +000079#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010080#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020081#define DFL_SERVER_PORT "4433"
Paul Bakker9caf2d22010-02-18 19:37:19 +000082#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +020083#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +000084#define DFL_DEBUG_LEVEL 0
Hanno Beckerbb425db2019-04-03 12:59:58 +010085#define DFL_CONTEXT_CRT_CB 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010086#define DFL_NBIO 0
Hanno Becker16970d22017-10-10 15:56:37 +010087#define DFL_EVENT 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020088#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +020089#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +000090#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000091#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000092#define DFL_CRT_FILE ""
93#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +010094#define DFL_KEY_OPAQUE 0
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020095#define DFL_PSK ""
Hanno Becker1d911cd2018-11-15 13:06:09 +000096#define DFL_PSK_OPAQUE 0
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020097#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +020098#define DFL_ECJPAKE_PW NULL
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +020099#define DFL_EC_MAX_OPS -1
Paul Bakker51936882011-02-20 16:05:58 +0000100#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100102#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100103#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200104#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200105#define DFL_MIN_VERSION -1
Paul Bakker1d29fb52012-09-28 13:28:45 +0000106#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000107#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +0200108#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100109#define DFL_AUTH_MODE -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100111#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100112#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200113#define DFL_DHMLEN -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200114#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100115#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +0200116#define DFL_RECO_MODE 1
Hanno Becker90cb3592019-04-09 17:24:19 +0100117#define DFL_CID_ENABLED 0
118#define DFL_CID_VALUE ""
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100119#define DFL_CID_ENABLED_RENEGO -1
120#define DFL_CID_VALUE_RENEGO NULL
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200121#define DFL_RECONNECT_HARD 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200123#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100124#define DFL_CURVES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200126#define DFL_HS_TO_MIN 0
127#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200128#define DFL_DTLS_MTU -1
Hanno Becker4d615912018-08-14 13:33:30 +0100129#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200130#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200131#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100132#define DFL_ETM -1
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200133#define DFL_CA_CALLBACK 0
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300134#define DFL_EAP_TLS 0
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200135#define DFL_REPRODUCIBLE 0
Paul Bakker0e6975b2009-02-10 22:19:10 +0000136
Paul Bakker93c32b22014-04-25 13:40:05 +0200137#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
138#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#if defined(MBEDTLS_X509_CRT_PARSE_C)
Janos Follathae13beb2019-04-05 14:06:58 +0100141#define USAGE_CONTEXT_CRT_CB \
Hanno Beckerbb425db2019-04-03 12:59:58 +0100142 " context_crt_cb=%%d This determines whether the CRT verification callback is bound\n" \
143 " to the SSL configuration of the SSL context.\n" \
144 " Possible values:\n"\
145 " - 0 (default): Use CRT callback bound to configuration\n" \
146 " - 1: Use CRT callback bound to SSL context\n"
147#else
Janos Follathae13beb2019-04-05 14:06:58 +0100148#define USAGE_CONTEXT_CRT_CB ""
Hanno Beckerbb425db2019-04-03 12:59:58 +0100149#endif /* MBEDTLS_X509_CRT_PARSE_C */
150#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000152#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100153 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
154 " default: \"\" (pre-loaded)\n" \
Hanno Becker422d1992019-05-01 11:16:28 +0100155 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100156 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
157 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
Hanno Becker422d1992019-05-01 11:16:28 +0100158 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100159 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
160 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000161 " key_file=%%s default: \"\" (pre-loaded)\n"
162#else
163#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 " No file operations available (MBEDTLS_FS_IO not defined)\n"
165#endif /* MBEDTLS_FS_IO */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100166#else /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200167#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100169#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
170#define USAGE_KEY_OPAQUE \
171 " key_opaque=%%d Handle your private key as if it were opaque\n" \
172 " default: 0 (disabled)\n"
173#else
174#define USAGE_KEY_OPAQUE ""
175#endif
Paul Bakkered27a042013-04-18 22:46:23 +0200176
Hanno Beckera0e20d02019-05-15 14:03:01 +0100177#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +0100178#define USAGE_CID \
179 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
180 " default: 0 (disabled)\n" \
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100181 " cid_renego=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension during renegotiation.\n" \
Hanno Becker32798222019-05-23 17:01:06 +0100182 " default: same as 'cid' parameter\n" \
Hanno Becker90cb3592019-04-09 17:24:19 +0100183 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100184 " default: \"\"\n" \
185 " cid_val_renego=%%s The CID to use for incoming messages (in hex, without 0x) after renegotiation.\n" \
Hanno Becker32798222019-05-23 17:01:06 +0100186 " default: same as 'cid_val' parameter\n"
Hanno Beckera0e20d02019-05-15 14:03:01 +0100187#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +0100188#define USAGE_CID ""
Hanno Beckera0e20d02019-05-15 14:03:01 +0100189#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +0100190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckere86964c2018-10-23 11:37:50 +0100192#define USAGE_PSK_RAW \
Paul Bakkered27a042013-04-18 22:46:23 +0200193 " psk=%%s default: \"\" (in hex, without 0x)\n" \
194 " psk_identity=%%s default: \"Client_identity\"\n"
Hanno Beckere86964c2018-10-23 11:37:50 +0100195#if defined(MBEDTLS_USE_PSA_CRYPTO)
196#define USAGE_PSK_SLOT \
Hanno Becker1d911cd2018-11-15 13:06:09 +0000197 " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
198 " Enable this to store the PSK configured through command line\n" \
199 " parameter `psk` in a PSA-based key slot.\n" \
Hanno Beckere86964c2018-10-23 11:37:50 +0100200 " Note: Currently only supported in conjunction with\n" \
201 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
202 " to force a particular PSK-only ciphersuite.\n" \
203 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
204 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
205 " with prepopulated key slots instead of importing raw key material.\n"
206#else
207#define USAGE_PSK_SLOT ""
208#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker1bac87c2019-03-29 12:02:26 +0000209#define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
210#else
211#define USAGE_PSK ""
212#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
213
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200214#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
215#define USAGE_CA_CALLBACK \
216 " ca_callback=%%d default: 0 (disabled)\n" \
217 " Enable this to use the trusted certificate callback function\n"
218#else
219#define USAGE_CA_CALLBACK ""
220#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5690efc2011-05-26 13:16:06 +0000221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200223#define USAGE_TICKETS \
224 " tickets=%%d default: 1 (enabled)\n"
225#else
226#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200228
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300229#if defined(MBEDTLS_SSL_EXPORT_KEYS)
230#define USAGE_EAP_TLS \
231 " eap_tls=%%d default: 0 (disabled)\n"
232#else
233#define USAGE_EAP_TLS ""
234#endif /* MBEDTLS_SSL_EXPORT_KEYS */
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200237#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100238 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200239#else
240#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200244#define USAGE_MAX_FRAG_LEN \
245 " max_frag_len=%%d default: 16384 (tls default)\n" \
246 " options: 512, 1024, 2048, 4096\n"
247#else
248#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100252#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200253 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100254#else
255#define USAGE_RECSPLIT
256#endif
257
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200258#if defined(MBEDTLS_DHM_C)
259#define USAGE_DHMLEN \
260 " dhmlen=%%d default: (library default: 1024 bits)\n"
261#else
262#define USAGE_DHMLEN
263#endif
264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200266#define USAGE_ALPN \
267 " alpn=%%s default: \"\" (disabled)\n" \
268 " example: spdy/1,http/1.1\n"
269#else
270#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200272
Hanno Beckere6706e62017-05-15 16:05:15 +0100273#if defined(MBEDTLS_ECP_C)
274#define USAGE_CURVES \
275 " curves=a,b,c,d default: \"default\" (library default)\n" \
276 " example: \"secp521r1,brainpoolP512r1\"\n" \
277 " - use \"none\" for empty list\n" \
278 " - see mbedtls_ecp_curve_list()\n" \
279 " for acceptable curve names\n"
280#else
281#define USAGE_CURVES ""
282#endif
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200285#define USAGE_DTLS \
286 " dtls=%%d default: 0 (TLS)\n" \
287 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200288 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Becker4d615912018-08-14 13:33:30 +0100289 " mtu=%%d default: (library default: unlimited)\n" \
290 " dgram_packing=%%d default: 1 (allowed)\n" \
291 " allow or forbid packing of multiple\n" \
292 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200293#else
294#define USAGE_DTLS ""
295#endif
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200298#define USAGE_FALLBACK \
299 " fallback=0/1 default: (library default: off)\n"
300#else
301#define USAGE_FALLBACK ""
302#endif
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200305#define USAGE_EMS \
306 " extended_ms=0/1 default: (library default: on)\n"
307#else
308#define USAGE_EMS ""
309#endif
310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100312#define USAGE_ETM \
313 " etm=0/1 default: (library default: on)\n"
314#else
315#define USAGE_ETM ""
316#endif
317
Philippe Antoine738153a2019-06-18 20:16:43 +0200318#define USAGE_REPRODUCIBLE \
319 " reproducible=0/1 default: 0 (disabled)\n"
320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100322#define USAGE_RENEGO \
323 " renegotiation=%%d default: 0 (disabled)\n" \
324 " renegotiate=%%d default: 0 (disabled)\n"
325#else
326#define USAGE_RENEGO ""
327#endif
328
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200329#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
330#define USAGE_ECJPAKE \
331 " ecjpake_pw=%%s default: none (disabled)\n"
332#else
333#define USAGE_ECJPAKE ""
334#endif
335
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200336#if defined(MBEDTLS_ECP_RESTARTABLE)
337#define USAGE_ECRESTART \
338 " ec_max_ops=%%s default: library default (restart disabled)\n"
339#else
340#define USAGE_ECRESTART ""
341#endif
342
Paul Bakker9caf2d22010-02-18 19:37:19 +0000343#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000344 "\n usage: ssl_client2 param=<>...\n" \
345 "\n acceptable parameters:\n" \
346 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100347 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000348 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000349 " request_page=%%s default: \".\"\n" \
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +0100350 " request_size=%%d default: about 34 (basic request)\n" \
351 " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
352 " If 0, in the first exchange only an empty\n" \
353 " application data message is sent followed by\n" \
354 " a second non-empty message before attempting\n" \
355 " to read a response from the server\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100356 " debug_level=%%d default: 0 (disabled)\n" \
357 " nbio=%%d default: 0 (blocking I/O)\n" \
358 " options: 1 (non-blocking), 2 (added delays)\n" \
359 " event=%%d default: 0 (loop)\n" \
360 " options: 1 (level-triggered, implies nbio=1),\n" \
361 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200362 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100363 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200364 USAGE_DTLS \
Hanno Becker90cb3592019-04-09 17:24:19 +0100365 USAGE_CID \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200366 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100367 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100368 " options: none, optional, required\n" \
369 USAGE_IO \
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100370 USAGE_KEY_OPAQUE \
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200371 USAGE_CA_CALLBACK \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100372 "\n" \
373 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200374 USAGE_ECJPAKE \
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200375 USAGE_ECRESTART \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100376 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100377 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100378 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200379 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +0200380 " reconnect=%%d number of reconnections using session resumption\n" \
381 " default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200382 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +0200383 " reco_mode=%%d 0: copy session, 1: serialise session\n" \
384 " default: 1\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200385 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200386 USAGE_TICKETS \
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300387 USAGE_EAP_TLS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100388 USAGE_MAX_FRAG_LEN \
389 USAGE_TRUNC_HMAC \
Janos Follathae13beb2019-04-05 14:06:58 +0100390 USAGE_CONTEXT_CRT_CB \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200391 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200392 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200393 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100394 USAGE_ETM \
Philippe Antoine738153a2019-06-18 20:16:43 +0200395 USAGE_REPRODUCIBLE \
Hanno Beckere6706e62017-05-15 16:05:15 +0100396 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100397 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200398 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000399 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100400 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200401 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200402 " min_version=%%s default: (library default: tls1)\n" \
403 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000404 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100405 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000406 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000407 " force_ciphersuite=<name> default: all enabled\n"\
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100408 " query_config=<name> return 0 if the specified\n" \
409 " configuration macro is defined and 1\n" \
410 " otherwise. The expansion of the macro\n" \
411 " is printed if it is defined\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000412 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000413
Hanno Becker8651a432017-06-09 16:13:22 +0100414#define ALPN_LIST_SIZE 10
415#define CURVE_LIST_SIZE 20
416
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500417
Rich Evans85b05ec2015-02-12 11:37:29 +0000418/*
419 * global options
420 */
421struct options
422{
423 const char *server_name; /* hostname of the server (client only) */
424 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200425 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000426 int debug_level; /* level of debugging */
427 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100428 int event; /* loop or event-driven IO? level or edge triggered? */
429 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000430 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000431 const char *request_page; /* page on server to request */
432 int request_size; /* pad request with header to requested size */
433 const char *ca_file; /* the file with the CA certificate(s) */
434 const char *ca_path; /* the path with the CA certificate(s) reside */
435 const char *crt_file; /* the file with the client certificate */
436 const char *key_file; /* the file with the client key */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100437 int key_opaque; /* handle private key as if it were opaque */
Hanno Beckere86964c2018-10-23 11:37:50 +0100438#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000439 int psk_opaque;
Hanno Beckere86964c2018-10-23 11:37:50 +0100440#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200441#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000442 int ca_callback; /* Use callback for trusted certificate list */
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200443#endif
Rich Evans85b05ec2015-02-12 11:37:29 +0000444 const char *psk; /* the pre-shared key */
445 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200446 const char *ecjpake_pw; /* the EC J-PAKE password */
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200447 int ec_max_ops; /* EC consecutive operations limit */
Rich Evans85b05ec2015-02-12 11:37:29 +0000448 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
449 int renegotiation; /* enable / disable renegotiation */
450 int allow_legacy; /* allow legacy renegotiation */
451 int renegotiate; /* attempt renegotiation? */
452 int renego_delay; /* delay before enforcing renegotiation */
453 int exchanges; /* number of data exchanges */
454 int min_version; /* minimum protocol version accepted */
455 int max_version; /* maximum protocol version accepted */
456 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200457 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000458 int auth_mode; /* verify mode for connection */
459 unsigned char mfl_code; /* code for maximum fragment length */
460 int trunc_hmac; /* negotiate truncated hmac or not */
461 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200462 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000463 int reconnect; /* attempt to resume session */
464 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +0200465 int reco_mode; /* how to keep the session around */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200466 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000467 int tickets; /* enable / disable session tickets */
Hanno Beckere6706e62017-05-15 16:05:15 +0100468 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000469 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000470 int transport; /* TLS or DTLS? */
471 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
472 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200473 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Rich Evans85b05ec2015-02-12 11:37:29 +0000474 int fallback; /* is this a fallback connection? */
Hanno Becker4d615912018-08-14 13:33:30 +0100475 int dgram_packing; /* allow/forbid datagram packing */
Rich Evans85b05ec2015-02-12 11:37:29 +0000476 int extended_ms; /* negotiate extended master secret? */
477 int etm; /* negotiate encrypt then mac? */
Hanno Beckerbb425db2019-04-03 12:59:58 +0100478 int context_crt_cb; /* use context-specific CRT verify callback */
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300479 int eap_tls; /* derive EAP-TLS keying material? */
Hanno Becker90cb3592019-04-09 17:24:19 +0100480 int cid_enabled; /* whether to use the CID extension or not */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100481 int cid_enabled_renego; /* whether to use the CID extension or not
482 * during renegotiation */
Hanno Becker90cb3592019-04-09 17:24:19 +0100483 const char *cid_val; /* the CID to use for incoming messages */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100484 const char *cid_val_renego; /* the CID to use for incoming messages
485 * after renegotiation */
Philippe Antoine154feb22019-06-11 17:50:23 +0200486 int reproducible; /* make communication reproducible */
Rich Evans85b05ec2015-02-12 11:37:29 +0000487} opt;
488
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100489int query_config( const char *config );
490
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300491#if defined(MBEDTLS_SSL_EXPORT_KEYS)
492typedef struct eap_tls_keys
493{
494 unsigned char master_secret[48];
495 unsigned char randbytes[64];
Ron Eldor51d3ab52019-05-12 14:54:30 +0300496 mbedtls_tls_prf_types tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300497} eap_tls_keys;
498
499static int eap_tls_key_derivation ( void *p_expkey,
500 const unsigned char *ms,
501 const unsigned char *kb,
502 size_t maclen,
503 size_t keylen,
504 size_t ivlen,
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300505 unsigned char client_random[32],
Ron Eldor51d3ab52019-05-12 14:54:30 +0300506 unsigned char server_random[32],
507 mbedtls_tls_prf_types tls_prf_type )
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300508{
509 eap_tls_keys *keys = (eap_tls_keys *)p_expkey;
510
511 ( ( void ) kb );
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300512 memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) );
513 memcpy( keys->randbytes, client_random, 32 );
514 memcpy( keys->randbytes + 32, server_random, 32 );
Ron Eldor51d3ab52019-05-12 14:54:30 +0300515 keys->tls_prf_type = tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300516
Ron Eldorf75e2522019-05-14 20:38:49 +0300517 if( opt.debug_level > 2 )
518 {
Ron Eldor801faf02019-05-15 17:45:24 +0300519 mbedtls_printf("exported maclen is %u\n", (unsigned)maclen);
520 mbedtls_printf("exported keylen is %u\n", (unsigned)keylen);
521 mbedtls_printf("exported ivlen is %u\n", (unsigned)ivlen);
Ron Eldorf75e2522019-05-14 20:38:49 +0300522 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300523 return( 0 );
524}
525#endif
526
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200527static void my_debug( void *ctx, int level,
528 const char *file, int line,
529 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000530{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200531 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000532
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200533 /* Extract basename from file */
534 for( p = basename = file; *p != '\0'; p++ )
535 if( *p == '/' || *p == '\\' )
536 basename = p + 1;
537
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100538 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s",
539 basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000540 fflush( (FILE *) ctx );
541}
542
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200543
544mbedtls_time_t dummy_constant_time( mbedtls_time_t* time )
545{
546 (void) time;
547 return 0x5af2a056;
548}
549
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200550int dummy_entropy( void *data, unsigned char *output, size_t len )
551{
552 size_t i;
Philippe Antoine12e85de2019-06-11 16:07:53 +0200553 int ret;
Philippe Antoined2235f22019-06-11 16:29:28 +0200554 (void) data;
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200555
Philippe Antoine3ca50852019-06-07 22:31:59 +0200556 ret = mbedtls_entropy_func( data, output, len );
Philippe Antoine986b6f22019-06-07 15:04:32 +0200557 for ( i = 0; i < len; i++ )
558 {
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200559 //replace result with pseudo random
560 output[i] = (unsigned char) rand();
561 }
Philippe Antoine3ca50852019-06-07 22:31:59 +0200562 return( ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200563}
564
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200565#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000566int ca_callback( void *data, mbedtls_x509_crt const *child,
Janos Follathd7ecbd62019-04-05 14:52:17 +0100567 mbedtls_x509_crt **candidates )
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200568{
Hanno Beckercbb59032019-03-28 14:14:22 +0000569 int ret = 0;
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200570 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +0000571 mbedtls_x509_crt *first;
572
573 /* This is a test-only implementation of the CA callback
574 * which always returns the entire list of trusted certificates.
575 * Production implementations managing a large number of CAs
576 * should use an efficient presentation and lookup for the
577 * set of trusted certificates (such as a hashtable) and only
578 * return those trusted certificates which satisfy basic
579 * parental checks, such as the matching of child `Issuer`
Jarno Lamsaf7a7f9e2019-04-01 15:11:54 +0300580 * and parent `Subject` field or matching key identifiers. */
Hanno Beckercbb59032019-03-28 14:14:22 +0000581 ((void) child);
582
583 first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
584 if( first == NULL )
585 {
586 ret = -1;
587 goto exit;
588 }
589 mbedtls_x509_crt_init( first );
590
591 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
592 {
593 ret = -1;
594 goto exit;
595 }
596
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200597 while( ca->next != NULL )
598 {
599 ca = ca->next;
Hanno Beckercbb59032019-03-28 14:14:22 +0000600 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
601 {
602 ret = -1;
603 goto exit;
604 }
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200605 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000606
607exit:
608
609 if( ret != 0 )
610 {
611 mbedtls_x509_crt_free( first );
612 mbedtls_free( first );
613 first = NULL;
614 }
615
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200616 *candidates = first;
Hanno Beckercbb59032019-03-28 14:14:22 +0000617 return( ret );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200618}
619#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
620
Rich Evans85b05ec2015-02-12 11:37:29 +0000621/*
622 * Test recv/send functions that make sure each try returns
623 * WANT_READ/WANT_WRITE at least once before sucesseding
624 */
Hanno Becker8b1af2f2019-07-03 17:02:43 +0100625
626static int delayed_recv( void *ctx, unsigned char *buf, size_t len )
Rich Evans85b05ec2015-02-12 11:37:29 +0000627{
628 static int first_try = 1;
629 int ret;
630
631 if( first_try )
632 {
633 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100634 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000635 }
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100638 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000639 first_try = 1; /* Next call will be a new operation */
640 return( ret );
641}
642
Hanno Becker8b1af2f2019-07-03 17:02:43 +0100643static int delayed_send( void *ctx, const unsigned char *buf, size_t len )
Rich Evans85b05ec2015-02-12 11:37:29 +0000644{
645 static int first_try = 1;
646 int ret;
647
648 if( first_try )
649 {
650 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100651 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000652 }
653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100655 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000656 first_try = 1; /* Next call will be a new operation */
657 return( ret );
658}
659
Hanno Becker8b1af2f2019-07-03 17:02:43 +0100660typedef struct
661{
662 mbedtls_ssl_context *ssl;
663 mbedtls_net_context *net;
664} io_ctx_t;
665
Hanno Becker4b6649e2019-07-03 17:14:41 +0100666#if defined(MBEDTLS_SSL_RECORD_CHECKING)
667static int ssl_check_record( mbedtls_ssl_context const *ssl,
668 unsigned char const *buf, size_t len )
669{
670 int ret;
671 unsigned char *tmp_buf;
672
673 tmp_buf = mbedtls_calloc( 1, len );
674 if( tmp_buf == NULL )
675 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
676 memcpy( tmp_buf, buf, len );
677
678 ret = mbedtls_ssl_check_record( ssl, tmp_buf, len );
679 if( ret != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE )
680 {
681 int ret_repeated;
682
683 /* Test-only: Make sure that mbedtls_ssl_check_record()
684 * doesn't alter state. */
685 memcpy( tmp_buf, buf, len ); /* Restore buffer */
686 ret_repeated = mbedtls_ssl_check_record( ssl, tmp_buf, len );
687 if( ret != ret_repeated )
688 {
Hanno Becker91f83272019-07-24 13:59:07 +0100689 mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" );
690 return( -1 );
Hanno Becker4b6649e2019-07-03 17:14:41 +0100691 }
692
693 switch( ret )
694 {
695 case 0:
696 break;
697
698 case MBEDTLS_ERR_SSL_INVALID_RECORD:
699 if( opt.debug_level > 1 )
700 mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" );
701 break;
702
703 case MBEDTLS_ERR_SSL_INVALID_MAC:
704 if( opt.debug_level > 1 )
705 mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" );
706 break;
707
708 case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD:
709 if( opt.debug_level > 1 )
710 mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" );
711 break;
712
713 default:
714 mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", -ret );
715 return( -1 );
716 }
717
718 /* Regardless of the outcome, forward the record to the stack. */
719 }
720
Hanno Becker4b6649e2019-07-03 17:14:41 +0100721 mbedtls_free( tmp_buf );
722
723 return( 0 );
724}
725#endif /* MBEDTLS_SSL_RECORD_CHECKING */
726
Hanno Becker8b1af2f2019-07-03 17:02:43 +0100727static int recv_cb( void *ctx, unsigned char *buf, size_t len )
728{
729 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
730 size_t recv_len;
731 int ret;
732
733 if( opt.nbio == 2 )
734 ret = delayed_recv( io_ctx->net, buf, len );
735 else
736 ret = mbedtls_net_recv( io_ctx->net, buf, len );
737 if( ret < 0 )
738 return( ret );
739 recv_len = (size_t) ret;
740
741 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
742 {
743 /* Here's the place to do any datagram/record checking
744 * in between receiving the packet from the underlying
745 * transport and passing it on to the TLS stack. */
Hanno Becker4b6649e2019-07-03 17:14:41 +0100746#if defined(MBEDTLS_SSL_RECORD_CHECKING)
747 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
748 return( -1 );
749#endif /* MBEDTLS_SSL_RECORD_CHECKING */
Hanno Becker8b1af2f2019-07-03 17:02:43 +0100750 }
751
752 return( (int) recv_len );
753}
754
755static int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len,
756 uint32_t timeout )
757{
758 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
759 int ret;
760 size_t recv_len;
761
762 ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout );
763 if( ret < 0 )
764 return( ret );
765 recv_len = (size_t) ret;
766
767 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
768 {
769 /* Here's the place to do any datagram/record checking
770 * in between receiving the packet from the underlying
771 * transport and passing it on to the TLS stack. */
Hanno Becker4b6649e2019-07-03 17:14:41 +0100772#if defined(MBEDTLS_SSL_RECORD_CHECKING)
773 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
774 return( -1 );
775#endif /* MBEDTLS_SSL_RECORD_CHECKING */
Hanno Becker8b1af2f2019-07-03 17:02:43 +0100776 }
777
778 return( (int) recv_len );
779}
780
781static int send_cb( void *ctx, unsigned char const *buf, size_t len )
782{
783 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
784
785 if( opt.nbio == 2 )
786 return( delayed_send( io_ctx->net, buf, len ) );
787
788 return( mbedtls_net_send( io_ctx->net, buf, len ) );
789}
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckera1051b42019-02-26 11:38:29 +0000792static unsigned char peer_crt_info[1024];
Hanno Beckera9766c2c2019-02-25 17:43:18 +0000793
Rich Evans85b05ec2015-02-12 11:37:29 +0000794/*
795 * Enabled if debug_level > 1 in code below
796 */
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100797static int my_verify( void *data, mbedtls_x509_crt *crt,
798 int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000799{
800 char buf[1024];
801 ((void) data);
802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Hanno Beckera9766c2c2019-02-25 17:43:18 +0000804 if( depth == 0 )
805 memcpy( peer_crt_info, buf, sizeof( buf ) );
806
807 if( opt.debug_level == 0 )
808 return( 0 );
809
810 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000812
Rich Evans85b05ec2015-02-12 11:37:29 +0000813 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100815 else
816 {
817 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
818 mbedtls_printf( "%s\n", buf );
819 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000820
821 return( 0 );
822}
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200823
824static int ssl_sig_hashes_for_test[] = {
825#if defined(MBEDTLS_SHA512_C)
826 MBEDTLS_MD_SHA512,
827 MBEDTLS_MD_SHA384,
828#endif
829#if defined(MBEDTLS_SHA256_C)
830 MBEDTLS_MD_SHA256,
831 MBEDTLS_MD_SHA224,
832#endif
833#if defined(MBEDTLS_SHA1_C)
834 /* Allow SHA-1 as we use it extensively in tests. */
835 MBEDTLS_MD_SHA1,
836#endif
837 MBEDTLS_MD_NONE
838};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000840
Hanno Becker16970d22017-10-10 15:56:37 +0100841/*
842 * Wait for an event from the underlying transport or the timer
843 * (Used in event-driven IO mode).
844 */
845#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000846int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000847 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100848#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000849int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000850 mbedtls_timing_delay_context *timer,
851 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100852#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000853{
Hanno Becker16970d22017-10-10 15:56:37 +0100854
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000855 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +0100856 int poll_type = 0;
857
858 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
859 poll_type = MBEDTLS_NET_POLL_WRITE;
860 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
861 poll_type = MBEDTLS_NET_POLL_READ;
862#if !defined(MBEDTLS_TIMING_C)
863 else
Hanno Beckeref527962018-03-15 15:49:24 +0000864 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100865#endif
866
867 while( 1 )
868 {
Hanno Becker197a91c2017-10-31 10:58:53 +0000869 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +0100870#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +0000871 if( timer != NULL &&
872 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +0100873 {
Hanno Becker16970d22017-10-10 15:56:37 +0100874 break;
875 }
Hanno Becker197a91c2017-10-31 10:58:53 +0000876#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +0100877
Hanno Becker197a91c2017-10-31 10:58:53 +0000878 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000879 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +0100880 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000881 ret = mbedtls_net_poll( fd, poll_type, 0 );
882 if( ret < 0 )
883 return( ret );
884 if( ret == poll_type )
885 break;
Hanno Becker16970d22017-10-10 15:56:37 +0100886 }
887 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000888
889 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100890}
891
Hanno Becker1f583ee2019-04-09 17:12:56 +0100892/* Unhexify `hex` into `dst`. `dst` must have
893 * size at least `strlen( hex ) / 2`. */
Hanno Becker90cb3592019-04-09 17:24:19 +0100894int unhexify( char const *hex, unsigned char *dst )
Hanno Becker1f583ee2019-04-09 17:12:56 +0100895{
896 unsigned char c;
897 size_t j;
898 size_t len = strlen( hex );
899
900 if( len % 2 != 0 )
901 return( -1 );
902
903 for( j = 0; j < len; j += 2 )
904 {
905 c = hex[j];
906 if( c >= '0' && c <= '9' )
907 c -= '0';
908 else if( c >= 'a' && c <= 'f' )
909 c -= 'a' - 10;
910 else if( c >= 'A' && c <= 'F' )
911 c -= 'A' - 10;
912 else
913 return( -1 );
914 dst[ j / 2 ] = c << 4;
915
916 c = hex[j + 1];
917 if( c >= '0' && c <= '9' )
918 c -= '0';
919 else if( c >= 'a' && c <= 'f' )
920 c -= 'a' - 10;
921 else if( c >= 'A' && c <= 'F' )
922 c -= 'A' - 10;
923 else
924 return( -1 );
925 dst[ j / 2 ] |= c;
926 }
927
928 return( 0 );
929}
930
Hanno Beckera0e20d02019-05-15 14:03:01 +0100931#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100932int report_cid_usage( mbedtls_ssl_context *ssl,
933 const char *additional_description )
934{
935 int ret;
936 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
937 size_t peer_cid_len;
938 int cid_negotiated;
939
940 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
941 return( 0 );
942
Hanno Becker6ae14c02019-05-22 16:59:25 +0100943 /* Check if the use of a CID has been negotiated,
944 * but don't ask for the CID value and length.
945 *
946 * Note: Here and below, we're demonstrating the various ways
947 * in which mbedtls_ssl_get_peer_cid() can be called,
948 * depending on whether or not the length/value of the
949 * peer's CID is needed.
950 *
951 * An actual application, however, should use
952 * just one call to mbedtls_ssl_get_peer_cid(). */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100953 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
Hanno Becker6ae14c02019-05-22 16:59:25 +0100954 NULL, NULL );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100955 if( ret != 0 )
956 {
957 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
958 -ret );
959 return( ret );
960 }
961
962 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
963 {
964 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
965 {
966 mbedtls_printf( "(%s) Use of Connection ID was rejected by the server.\n",
967 additional_description );
968 }
969 }
970 else
971 {
972 size_t idx=0;
973 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
974 additional_description );
Hanno Becker6ae14c02019-05-22 16:59:25 +0100975
976 /* Ask for just the length of the peer's CID. */
977 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
978 NULL, &peer_cid_len );
979 if( ret != 0 )
980 {
981 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
982 -ret );
983 return( ret );
984 }
985
986 /* Ask for just length + value of the peer's CID. */
987 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
988 peer_cid, &peer_cid_len );
989 if( ret != 0 )
990 {
991 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
992 -ret );
993 return( ret );
994 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100995 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
996 additional_description,
997 (unsigned) peer_cid_len );
998 while( idx < peer_cid_len )
999 {
1000 mbedtls_printf( "%02x ", peer_cid[ idx ] );
1001 idx++;
1002 }
1003 mbedtls_printf( "\n" );
1004 }
1005
1006 return( 0 );
1007}
Hanno Beckera0e20d02019-05-15 14:03:01 +01001008#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001009
Paul Bakker9caf2d22010-02-18 19:37:19 +00001010int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001011{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001012 int ret = 0, len, tail_len, i, written, frags, retry_left;
1013 mbedtls_net_context server_fd;
Hanno Becker8b1af2f2019-07-03 17:02:43 +01001014 io_ctx_t io_ctx;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001015
1016 unsigned char buf[MAX_REQUEST_SIZE + 1];
1017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1019 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001020 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +02001021#endif
Hanno Becker90cb3592019-04-09 17:24:19 +01001022
Hanno Beckera0e20d02019-05-15 14:03:01 +01001023#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +01001024 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001025 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker90cb3592019-04-09 17:24:19 +01001026 size_t cid_len = 0;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001027 size_t cid_renego_len = 0;
Hanno Becker90cb3592019-04-09 17:24:19 +01001028#endif
1029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +01001031 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001032#endif
Hanno Beckere6706e62017-05-15 16:05:15 +01001033#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +01001034 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +01001035 const mbedtls_ecp_curve_info *curve_cur;
1036#endif
1037
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001038 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +00001039
Hanno Beckere86964c2018-10-23 11:37:50 +01001040#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001041 psa_key_handle_t slot = 0;
Hanno Beckere86964c2018-10-23 11:37:50 +01001042 psa_algorithm_t alg = 0;
Janos Follathbe4efc22019-08-08 11:38:18 +01001043 psa_key_attributes_t key_attributes;
Hanno Beckere86964c2018-10-23 11:37:50 +01001044 psa_status_t status;
1045#endif
1046
Gilles Peskineef86ab22017-05-05 18:59:02 +02001047#if defined(MBEDTLS_X509_CRT_PARSE_C)
1048 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
1049#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 mbedtls_entropy_context entropy;
1051 mbedtls_ctr_drbg_context ctr_drbg;
1052 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001053 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnard21548632019-05-16 11:39:42 +02001055 unsigned char session_data[MBEDTLS_SSL_MAX_CONTENT_LEN];
1056 size_t session_data_len;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001057#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001058 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001059#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +02001061 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 mbedtls_x509_crt cacert;
1063 mbedtls_x509_crt clicert;
1064 mbedtls_pk_context pkey;
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001065#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001066 psa_key_handle_t key_slot = 0; /* invalid key slot */
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001067#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001068#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +00001069 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +00001070 const int *list;
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001071#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1072 unsigned char eap_tls_keymaterial[16];
1073 unsigned char eap_tls_iv[8];
1074 const char* eap_tls_label = "client EAP encryption";
1075 eap_tls_keys eap_tls_keying;
1076#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +00001077
Paul Bakker1a207ec2011-02-06 13:22:40 +00001078 /*
1079 * Make sure memory references are valid.
1080 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001081 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001082 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001083 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001085 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#if defined(MBEDTLS_X509_CRT_PARSE_C)
1087 mbedtls_x509_crt_init( &cacert );
1088 mbedtls_x509_crt_init( &clicert );
1089 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001090#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +02001092 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001093#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +00001094
Hanno Beckerb2b468b2018-11-12 17:46:59 +00001095#if defined(MBEDTLS_USE_PSA_CRYPTO)
1096 status = psa_crypto_init();
1097 if( status != PSA_SUCCESS )
1098 {
1099 mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
1100 (int) status );
1101 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1102 goto exit;
1103 }
1104#endif
1105
Paul Bakker9caf2d22010-02-18 19:37:19 +00001106 if( argc == 0 )
1107 {
1108 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +00001109 if( ret == 0 )
1110 ret = 1;
1111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +00001113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +00001115 while( *list )
1116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001118 list++;
1119 if( !*list )
1120 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +00001122 list++;
1123 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +00001125 goto exit;
1126 }
1127
1128 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001129 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001130 opt.server_port = DFL_SERVER_PORT;
1131 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker90cb3592019-04-09 17:24:19 +01001132 opt.cid_enabled = DFL_CID_ENABLED;
1133 opt.cid_val = DFL_CID_VALUE;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001134 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
1135 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001136 opt.nbio = DFL_NBIO;
Hanno Becker16970d22017-10-10 15:56:37 +01001137 opt.event = DFL_EVENT;
Hanno Beckerbb425db2019-04-03 12:59:58 +01001138 opt.context_crt_cb = DFL_CONTEXT_CRT_CB;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001139 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001140 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001141 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +02001142 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +00001143 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +00001144 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +00001145 opt.crt_file = DFL_CRT_FILE;
1146 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001147 opt.key_opaque = DFL_KEY_OPAQUE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001148 opt.psk = DFL_PSK;
Hanno Beckere86964c2018-10-23 11:37:50 +01001149#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001150 opt.psk_opaque = DFL_PSK_OPAQUE;
Hanno Beckere86964c2018-10-23 11:37:50 +01001151#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001152#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1153 opt.ca_callback = DFL_CA_CALLBACK;
1154#endif
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001155 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001156 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001157 opt.ec_max_ops = DFL_EC_MAX_OPS;
Paul Bakker51936882011-02-20 16:05:58 +00001158 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +00001159 opt.renegotiation = DFL_RENEGOTIATION;
1160 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001161 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001162 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001163 opt.min_version = DFL_MIN_VERSION;
1164 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001165 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001166 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001167 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001168 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001169 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001170 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001171 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001172 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001173 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +02001174 opt.reco_mode = DFL_RECO_MODE;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001175 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001176 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001177 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001178 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001179 opt.transport = DFL_TRANSPORT;
1180 opt.hs_to_min = DFL_HS_TO_MIN;
1181 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001182 opt.dtls_mtu = DFL_DTLS_MTU;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001183 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001184 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001185 opt.etm = DFL_ETM;
Hanno Becker4d615912018-08-14 13:33:30 +01001186 opt.dgram_packing = DFL_DGRAM_PACKING;
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001187 opt.eap_tls = DFL_EAP_TLS;
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001188 opt.reproducible = DFL_REPRODUCIBLE;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001189
1190 for( i = 1; i < argc; i++ )
1191 {
Paul Bakker9caf2d22010-02-18 19:37:19 +00001192 p = argv[i];
1193 if( ( q = strchr( p, '=' ) ) == NULL )
1194 goto usage;
1195 *q++ = '\0';
1196
1197 if( strcmp( p, "server_name" ) == 0 )
1198 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001199 else if( strcmp( p, "server_addr" ) == 0 )
1200 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001201 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001202 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001203 else if( strcmp( p, "dtls" ) == 0 )
1204 {
1205 int t = atoi( q );
1206 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001208 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001210 else
1211 goto usage;
1212 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001213 else if( strcmp( p, "debug_level" ) == 0 )
1214 {
1215 opt.debug_level = atoi( q );
1216 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1217 goto usage;
1218 }
Hanno Beckerbb425db2019-04-03 12:59:58 +01001219 else if( strcmp( p, "context_crt_cb" ) == 0 )
1220 {
1221 opt.context_crt_cb = atoi( q );
1222 if( opt.context_crt_cb != 0 && opt.context_crt_cb != 1 )
1223 goto usage;
1224 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001225 else if( strcmp( p, "nbio" ) == 0 )
1226 {
1227 opt.nbio = atoi( q );
1228 if( opt.nbio < 0 || opt.nbio > 2 )
1229 goto usage;
1230 }
Hanno Becker16970d22017-10-10 15:56:37 +01001231 else if( strcmp( p, "event" ) == 0 )
1232 {
1233 opt.event = atoi( q );
1234 if( opt.event < 0 || opt.event > 2 )
1235 goto usage;
1236 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001237 else if( strcmp( p, "read_timeout" ) == 0 )
1238 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001239 else if( strcmp( p, "max_resend" ) == 0 )
1240 {
1241 opt.max_resend = atoi( q );
1242 if( opt.max_resend < 0 )
1243 goto usage;
1244 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001245 else if( strcmp( p, "request_page" ) == 0 )
1246 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +02001247 else if( strcmp( p, "request_size" ) == 0 )
1248 {
1249 opt.request_size = atoi( q );
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001250 if( opt.request_size < 0 ||
1251 opt.request_size > MAX_REQUEST_SIZE )
Paul Bakker93c32b22014-04-25 13:40:05 +02001252 goto usage;
1253 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001254 else if( strcmp( p, "ca_file" ) == 0 )
1255 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +00001256 else if( strcmp( p, "ca_path" ) == 0 )
1257 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +00001258 else if( strcmp( p, "crt_file" ) == 0 )
1259 opt.crt_file = q;
1260 else if( strcmp( p, "key_file" ) == 0 )
1261 opt.key_file = q;
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001262#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
1263 else if( strcmp( p, "key_opaque" ) == 0 )
1264 opt.key_opaque = atoi( q );
1265#endif
Hanno Beckera0e20d02019-05-15 14:03:01 +01001266#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +01001267 else if( strcmp( p, "cid" ) == 0 )
1268 {
1269 opt.cid_enabled = atoi( q );
1270 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
1271 goto usage;
1272 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001273 else if( strcmp( p, "cid_renego" ) == 0 )
1274 {
1275 opt.cid_enabled_renego = atoi( q );
1276 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
1277 goto usage;
1278 }
Hanno Becker90cb3592019-04-09 17:24:19 +01001279 else if( strcmp( p, "cid_val" ) == 0 )
1280 {
1281 opt.cid_val = q;
1282 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001283 else if( strcmp( p, "cid_val_renego" ) == 0 )
1284 {
1285 opt.cid_val_renego = q;
1286 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001287#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001288 else if( strcmp( p, "psk" ) == 0 )
1289 opt.psk = q;
Hanno Beckere86964c2018-10-23 11:37:50 +01001290#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001291 else if( strcmp( p, "psk_opaque" ) == 0 )
1292 opt.psk_opaque = atoi( q );
Hanno Beckere86964c2018-10-23 11:37:50 +01001293#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001294#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1295 else if( strcmp( p, "ca_callback" ) == 0)
1296 opt.ca_callback = atoi( q );
1297#endif
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001298 else if( strcmp( p, "psk_identity" ) == 0 )
1299 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001300 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1301 opt.ecjpake_pw = q;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001302 else if( strcmp( p, "ec_max_ops" ) == 0 )
1303 opt.ec_max_ops = atoi( q );
Paul Bakker51936882011-02-20 16:05:58 +00001304 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +00001307
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001308 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +00001309 {
1310 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +00001311 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +00001312 }
Paul Bakker51936882011-02-20 16:05:58 +00001313 opt.force_ciphersuite[1] = 0;
1314 }
Paul Bakker48916f92012-09-16 19:57:18 +00001315 else if( strcmp( p, "renegotiation" ) == 0 )
1316 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001317 opt.renegotiation = (atoi( q )) ?
1318 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1319 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +00001320 }
1321 else if( strcmp( p, "allow_legacy" ) == 0 )
1322 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001323 switch( atoi( q ) )
1324 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001325 case -1:
1326 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1327 break;
1328 case 0:
1329 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1330 break;
1331 case 1:
1332 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1333 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001334 default: goto usage;
1335 }
Paul Bakker48916f92012-09-16 19:57:18 +00001336 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001337 else if( strcmp( p, "renegotiate" ) == 0 )
1338 {
1339 opt.renegotiate = atoi( q );
1340 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1341 goto usage;
1342 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001343 else if( strcmp( p, "exchanges" ) == 0 )
1344 {
1345 opt.exchanges = atoi( q );
1346 if( opt.exchanges < 1 )
1347 goto usage;
1348 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001349 else if( strcmp( p, "reconnect" ) == 0 )
1350 {
1351 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001352 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001353 goto usage;
1354 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001355 else if( strcmp( p, "reco_delay" ) == 0 )
1356 {
1357 opt.reco_delay = atoi( q );
1358 if( opt.reco_delay < 0 )
1359 goto usage;
1360 }
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +02001361 else if( strcmp( p, "reco_mode" ) == 0 )
1362 {
1363 opt.reco_mode = atoi( q );
1364 if( opt.reco_mode < 0 )
1365 goto usage;
1366 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001367 else if( strcmp( p, "reconnect_hard" ) == 0 )
1368 {
1369 opt.reconnect_hard = atoi( q );
1370 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
1371 goto usage;
1372 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001373 else if( strcmp( p, "tickets" ) == 0 )
1374 {
1375 opt.tickets = atoi( q );
1376 if( opt.tickets < 0 || opt.tickets > 2 )
1377 goto usage;
1378 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001379 else if( strcmp( p, "alpn" ) == 0 )
1380 {
1381 opt.alpn_string = q;
1382 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001383 else if( strcmp( p, "fallback" ) == 0 )
1384 {
1385 switch( atoi( q ) )
1386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
1388 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001389 default: goto usage;
1390 }
1391 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001392 else if( strcmp( p, "extended_ms" ) == 0 )
1393 {
1394 switch( atoi( q ) )
1395 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001396 case 0:
1397 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1398 break;
1399 case 1:
1400 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1401 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001402 default: goto usage;
1403 }
1404 }
Hanno Beckere6706e62017-05-15 16:05:15 +01001405 else if( strcmp( p, "curves" ) == 0 )
1406 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001407 else if( strcmp( p, "etm" ) == 0 )
1408 {
1409 switch( atoi( q ) )
1410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1412 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001413 default: goto usage;
1414 }
1415 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001416 else if( strcmp( p, "min_version" ) == 0 )
1417 {
1418 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001420 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001422 else if( strcmp( q, "tls1_1" ) == 0 ||
1423 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001425 else if( strcmp( q, "tls1_2" ) == 0 ||
1426 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001428 else
1429 goto usage;
1430 }
1431 else if( strcmp( p, "max_version" ) == 0 )
1432 {
1433 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001435 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001437 else if( strcmp( q, "tls1_1" ) == 0 ||
1438 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001440 else if( strcmp( q, "tls1_2" ) == 0 ||
1441 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001443 else
1444 goto usage;
1445 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001446 else if( strcmp( p, "arc4" ) == 0 )
1447 {
1448 switch( atoi( q ) )
1449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1451 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001452 default: goto usage;
1453 }
1454 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02001455 else if( strcmp( p, "allow_sha1" ) == 0 )
1456 {
1457 switch( atoi( q ) )
1458 {
1459 case 0: opt.allow_sha1 = 0; break;
1460 case 1: opt.allow_sha1 = 1; break;
1461 default: goto usage;
1462 }
1463 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001464 else if( strcmp( p, "force_version" ) == 0 )
1465 {
1466 if( strcmp( q, "ssl3" ) == 0 )
1467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1469 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001470 }
1471 else if( strcmp( q, "tls1" ) == 0 )
1472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1474 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001475 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001476 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1479 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001480 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001481 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1484 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001485 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001486 else if( strcmp( q, "dtls1" ) == 0 )
1487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1489 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1490 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001491 }
1492 else if( strcmp( q, "dtls1_2" ) == 0 )
1493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1495 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1496 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001497 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001498 else
1499 goto usage;
1500 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001501 else if( strcmp( p, "auth_mode" ) == 0 )
1502 {
1503 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001505 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001507 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001509 else
1510 goto usage;
1511 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001512 else if( strcmp( p, "max_frag_len" ) == 0 )
1513 {
1514 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001516 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001518 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001520 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001522 else
1523 goto usage;
1524 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001525 else if( strcmp( p, "trunc_hmac" ) == 0 )
1526 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001527 switch( atoi( q ) )
1528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1530 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001531 default: goto usage;
1532 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001533 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001534 else if( strcmp( p, "hs_timeout" ) == 0 )
1535 {
1536 if( ( p = strchr( q, '-' ) ) == NULL )
1537 goto usage;
1538 *p++ = '\0';
1539 opt.hs_to_min = atoi( q );
1540 opt.hs_to_max = atoi( p );
1541 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1542 goto usage;
1543 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001544 else if( strcmp( p, "mtu" ) == 0 )
1545 {
1546 opt.dtls_mtu = atoi( q );
1547 if( opt.dtls_mtu < 0 )
1548 goto usage;
1549 }
Hanno Becker4d615912018-08-14 13:33:30 +01001550 else if( strcmp( p, "dgram_packing" ) == 0 )
1551 {
1552 opt.dgram_packing = atoi( q );
1553 if( opt.dgram_packing != 0 &&
1554 opt.dgram_packing != 1 )
1555 {
1556 goto usage;
1557 }
1558 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001559 else if( strcmp( p, "recsplit" ) == 0 )
1560 {
1561 opt.recsplit = atoi( q );
1562 if( opt.recsplit < 0 || opt.recsplit > 1 )
1563 goto usage;
1564 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001565 else if( strcmp( p, "dhmlen" ) == 0 )
1566 {
1567 opt.dhmlen = atoi( q );
1568 if( opt.dhmlen < 0 )
1569 goto usage;
1570 }
Andres Amaya Garciabc818842018-10-16 21:08:38 +01001571 else if( strcmp( p, "query_config" ) == 0 )
1572 {
1573 return query_config( q );
1574 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001575 else if( strcmp( p, "eap_tls" ) == 0 )
1576 {
1577 opt.eap_tls = atoi( q );
1578 if( opt.eap_tls < 0 || opt.eap_tls > 1 )
1579 goto usage;
1580 }
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001581 else if( strcmp( p, "reproducible" ) == 0 )
1582 {
1583 opt.reproducible = 1;
1584 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001585 else
1586 goto usage;
1587 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001588
Hanno Becker16970d22017-10-10 15:56:37 +01001589 /* Event-driven IO is incompatible with the above custom
1590 * receive and send functions, as the polling builds on
1591 * refers to the underlying net_context. */
1592 if( opt.event == 1 && opt.nbio != 1 )
1593 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001594 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001595 opt.nbio = 1;
1596 }
1597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598#if defined(MBEDTLS_DEBUG_C)
1599 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001600#endif
1601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001603 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001604 * Unhexify the pre-shared key if any is given
1605 */
1606 if( strlen( opt.psk ) )
1607 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001608 psk_len = strlen( opt.psk ) / 2;
1609 if( psk_len > sizeof( psk ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001610 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001611 mbedtls_printf( "pre-shared key too long\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001612 goto exit;
1613 }
1614
Hanno Becker1f583ee2019-04-09 17:12:56 +01001615 if( unhexify( opt.psk, psk ) != 0 )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001616 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001617 mbedtls_printf( "pre-shared key not valid hex\n" );
1618 goto exit;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001619 }
1620 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001622
Hanno Beckere86964c2018-10-23 11:37:50 +01001623#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001624 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001625 {
1626 if( opt.psk == NULL )
1627 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00001628 mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
Hanno Beckere86964c2018-10-23 11:37:50 +01001629 ret = 2;
1630 goto usage;
1631 }
1632
1633 if( opt.force_ciphersuite[0] <= 0 )
1634 {
1635 mbedtls_printf( "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n" );
1636 ret = 2;
1637 goto usage;
1638 }
1639 }
1640#endif /* MBEDTLS_USE_PSA_CRYPTO */
1641
1642 if( opt.force_ciphersuite[0] > 0 )
1643 {
1644 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1645 ciphersuite_info =
1646 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1647
1648 if( opt.max_version != -1 &&
1649 ciphersuite_info->min_minor_ver > opt.max_version )
1650 {
1651 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
1652 ret = 2;
1653 goto usage;
1654 }
1655 if( opt.min_version != -1 &&
1656 ciphersuite_info->max_minor_ver < opt.min_version )
1657 {
1658 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
1659 ret = 2;
1660 goto usage;
1661 }
1662
1663 /* If the server selects a version that's not supported by
1664 * this suite, then there will be no common ciphersuite... */
1665 if( opt.max_version == -1 ||
1666 opt.max_version > ciphersuite_info->max_minor_ver )
1667 {
1668 opt.max_version = ciphersuite_info->max_minor_ver;
1669 }
1670 if( opt.min_version < ciphersuite_info->min_minor_ver )
1671 {
1672 opt.min_version = ciphersuite_info->min_minor_ver;
1673 /* DTLS starts with TLS 1.1 */
1674 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1675 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1676 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1677 }
1678
1679 /* Enable RC4 if needed and not explicitly disabled */
1680 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
1681 {
1682 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
1683 {
1684 mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" );
1685 ret = 2;
1686 goto usage;
1687 }
1688
1689 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
1690 }
1691
Hanno Becker90cb3592019-04-09 17:24:19 +01001692
Hanno Beckere86964c2018-10-23 11:37:50 +01001693#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001694 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001695 {
1696 /* Ensure that the chosen ciphersuite is PSK-only; we must know
1697 * the ciphersuite in advance to set the correct policy for the
1698 * PSK key slot. This limitation might go away in the future. */
1699 if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
1700 opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 )
1701 {
1702 mbedtls_printf( "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n" );
1703 ret = 2;
1704 goto usage;
1705 }
1706
1707 /* Determine KDF algorithm the opaque PSK will be used in. */
1708#if defined(MBEDTLS_SHA512_C)
1709 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1710 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1711 else
1712#endif /* MBEDTLS_SHA512_C */
1713 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1714 }
1715#endif /* MBEDTLS_USE_PSA_CRYPTO */
1716 }
1717
Hanno Beckera0e20d02019-05-15 14:03:01 +01001718#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001719 cid_len = strlen( opt.cid_val ) / 2;
1720 if( cid_len > sizeof( cid ) )
1721 {
1722 mbedtls_printf( "CID too long\n" );
1723 goto exit;
1724 }
Hanno Becker90cb3592019-04-09 17:24:19 +01001725
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001726 if( unhexify( opt.cid_val, cid ) != 0 )
1727 {
1728 mbedtls_printf( "CID not valid hex\n" );
1729 goto exit;
1730 }
1731
1732 /* Keep CID settings for renegotiation unless
1733 * specified otherwise. */
1734 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
1735 opt.cid_enabled_renego = opt.cid_enabled;
1736 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
1737 opt.cid_val_renego = opt.cid_val;
1738
1739 cid_renego_len = strlen( opt.cid_val_renego ) / 2;
1740 if( cid_renego_len > sizeof( cid_renego ) )
1741 {
1742 mbedtls_printf( "CID too long\n" );
1743 goto exit;
1744 }
1745
1746 if( unhexify( opt.cid_val_renego, cid_renego ) != 0 )
1747 {
1748 mbedtls_printf( "CID not valid hex\n" );
1749 goto exit;
1750 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001751#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01001752
Hanno Beckere6706e62017-05-15 16:05:15 +01001753#if defined(MBEDTLS_ECP_C)
1754 if( opt.curves != NULL )
1755 {
1756 p = (char *) opt.curves;
1757 i = 0;
1758
1759 if( strcmp( p, "none" ) == 0 )
1760 {
1761 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1762 }
1763 else if( strcmp( p, "default" ) != 0 )
1764 {
1765 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001766 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001767 {
1768 q = p;
1769
1770 /* Terminate the current string */
1771 while( *p != ',' && *p != '\0' )
1772 p++;
1773 if( *p == ',' )
1774 *p++ = '\0';
1775
1776 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1777 {
1778 curve_list[i++] = curve_cur->grp_id;
1779 }
1780 else
1781 {
1782 mbedtls_printf( "unknown curve %s\n", q );
1783 mbedtls_printf( "supported curves: " );
1784 for( curve_cur = mbedtls_ecp_curve_list();
1785 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1786 curve_cur++ )
1787 {
1788 mbedtls_printf( "%s ", curve_cur->name );
1789 }
1790 mbedtls_printf( "\n" );
1791 goto exit;
1792 }
1793 }
1794
1795 mbedtls_printf("Number of curves: %d\n", i );
1796
Hanno Becker8651a432017-06-09 16:13:22 +01001797 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001798 {
Hanno Becker8651a432017-06-09 16:13:22 +01001799 mbedtls_printf( "curves list too long, maximum %d",
1800 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001801 goto exit;
1802 }
1803
1804 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1805 }
1806 }
1807#endif /* MBEDTLS_ECP_C */
1808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001810 if( opt.alpn_string != NULL )
1811 {
1812 p = (char *) opt.alpn_string;
1813 i = 0;
1814
1815 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001816 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001817 {
1818 alpn_list[i++] = p;
1819
1820 /* Terminate the current string and move on to next one */
1821 while( *p != ',' && *p != '\0' )
1822 p++;
1823 if( *p == ',' )
1824 *p++ = '\0';
1825 }
1826 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001828
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001829 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001830 * 0. Initialize the RNG and the session data
1831 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001833 fflush( stdout );
1834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 mbedtls_entropy_init( &entropy );
Philippe Antoine986b6f22019-06-07 15:04:32 +02001836 if (opt.reproducible)
Paul Bakker508ad5a2011-12-04 17:09:26 +00001837 {
Philippe Antoine738153a2019-06-18 20:16:43 +02001838 srand( 1 );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001839 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy,
Philippe Antoine986b6f22019-06-07 15:04:32 +02001840 &entropy, (const unsigned char *) pers,
1841 strlen( pers ) ) ) != 0 )
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001842 {
1843 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
Philippe Antoine986b6f22019-06-07 15:04:32 +02001844 -ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001845 goto exit;
1846 }
Philippe Antoine986b6f22019-06-07 15:04:32 +02001847 }
1848 else
1849 {
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001850 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
Philippe Antoine986b6f22019-06-07 15:04:32 +02001851 &entropy, (const unsigned char *) pers,
1852 strlen( pers ) ) ) != 0 )
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001853 {
1854 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
Philippe Antoine986b6f22019-06-07 15:04:32 +02001855 -ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001856 goto exit;
1857 }
Paul Bakker508ad5a2011-12-04 17:09:26 +00001858 }
1859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001863 /*
1864 * 1.1. Load the trusted CA
1865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001867 fflush( stdout );
1868
Hanno Becker623e7b42019-03-05 16:02:15 +00001869 if( strcmp( opt.ca_path, "none" ) == 0 ||
1870 strcmp( opt.ca_file, "none" ) == 0 )
1871 {
1872 ret = 0;
1873 }
1874 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001876 if( strlen( opt.ca_path ) )
Hanno Becker623e7b42019-03-05 16:02:15 +00001877 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001878 else if( strlen( opt.ca_file ) )
Hanno Becker623e7b42019-03-05 16:02:15 +00001879 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001880 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001881#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882#if defined(MBEDTLS_CERTS_C)
Hanno Becker2900b142019-02-01 08:15:06 +00001883 {
1884#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 ret = mbedtls_x509_crt_parse( &cacert,
1888 (const unsigned char *) mbedtls_test_cas[i],
1889 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001890 if( ret != 0 )
1891 break;
1892 }
Hanno Becker2900b142019-02-01 08:15:06 +00001893 if( ret == 0 )
1894#endif /* MBEDTLS_PEM_PARSE_C */
1895 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
1896 {
1897 ret = mbedtls_x509_crt_parse_der( &cacert,
1898 (const unsigned char *) mbedtls_test_cas_der[i],
1899 mbedtls_test_cas_der_len[i] );
1900 if( ret != 0 )
1901 break;
1902 }
1903 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001904#else
1905 {
1906 ret = 1;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001907 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001908 }
Hanno Becker2900b142019-02-01 08:15:06 +00001909#endif /* MBEDTLS_CERTS_C */
Paul Bakker42488232012-05-16 08:21:05 +00001910 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001911 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001912 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1913 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 goto exit;
1915 }
1916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001918
1919 /*
1920 * 1.2. Load own certificate and private key
1921 *
1922 * (can be skipped if client authentication is not required)
1923 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001925 fflush( stdout );
1926
Hanno Becker623e7b42019-03-05 16:02:15 +00001927 if( strcmp( opt.crt_file, "none" ) == 0 )
1928 ret = 0;
1929 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001931 if( strlen( opt.crt_file ) )
Hanno Becker623e7b42019-03-05 16:02:15 +00001932 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001933 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001934#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001936 ret = mbedtls_x509_crt_parse( &clicert,
1937 (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001939#else
1940 {
1941 ret = 1;
Hanno Beckera0c5ceb2018-12-05 16:49:42 +00001942 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001943 }
1944#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001945 if( ret != 0 )
1946 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001947 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1948 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 goto exit;
1950 }
1951
Hanno Becker623e7b42019-03-05 16:02:15 +00001952 if( strcmp( opt.key_file, "none" ) == 0 )
1953 ret = 0;
1954 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001956 if( strlen( opt.key_file ) )
Hanno Becker623e7b42019-03-05 16:02:15 +00001957 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001958 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001959#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001961 ret = mbedtls_pk_parse_key( &pkey,
1962 (const unsigned char *) mbedtls_test_cli_key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001964#else
1965 {
1966 ret = 1;
Hanno Beckera0c5ceb2018-12-05 16:49:42 +00001967 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001968 }
1969#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001970 if( ret != 0 )
1971 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001972 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
1973 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 goto exit;
1975 }
1976
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001977#if defined(MBEDTLS_USE_PSA_CRYPTO)
1978 if( opt.key_opaque != 0 )
1979 {
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001980 if( ( ret = mbedtls_pk_wrap_as_opaque( &pkey, &key_slot,
1981 PSA_ALG_SHA_256 ) ) != 0 )
1982 {
1983 mbedtls_printf( " failed\n ! "
1984 "mbedtls_pk_wrap_as_opaque returned -0x%x\n\n", -ret );
1985 goto exit;
1986 }
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001987 }
1988#endif /* MBEDTLS_USE_PSA_CRYPTO */
1989
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001990 mbedtls_printf( " ok (key type: %s)\n", mbedtls_pk_get_name( &pkey ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001992
1993 /*
1994 * 2. Start the connection
1995 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001996 if( opt.server_addr == NULL)
1997 opt.server_addr = opt.server_name;
1998
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001999 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002001 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00002002 fflush( stdout );
2003
Hanno Becker16970d22017-10-10 15:56:37 +01002004 if( ( ret = mbedtls_net_connect( &server_fd,
2005 opt.server_addr, opt.server_port,
2006 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2007 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002008 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002009 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
2010 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002011 goto exit;
2012 }
2013
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002014 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002015 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002016 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002017 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002018 if( ret != 0 )
2019 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002020 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
2021 -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002022 goto exit;
2023 }
2024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002026
2027 /*
2028 * 3. Setup stuff
2029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 fflush( stdout );
2032
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02002033 if( ( ret = mbedtls_ssl_config_defaults( &conf,
2034 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02002035 opt.transport,
2036 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002037 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002038 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
2039 -ret );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002040 goto exit;
2041 }
2042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02002044 /* The default algorithms profile disables SHA-1, but our tests still
2045 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02002046 if( opt.allow_sha1 > 0 )
2047 {
2048 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
2049 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
2050 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
2051 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02002052
Hanno Beckerbb425db2019-04-03 12:59:58 +01002053 if( opt.context_crt_cb == 0 )
2054 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
2055
Hanno Beckera1051b42019-02-26 11:38:29 +00002056 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Gilles Peskineef86ab22017-05-05 18:59:02 +02002057#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00002058
Hanno Beckera0e20d02019-05-15 14:03:01 +01002059#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002060 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckerad4a1372019-05-03 13:06:44 +01002061 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002062 if( opt.cid_enabled == 1 &&
2063 opt.cid_enabled_renego == 1 &&
2064 cid_len != cid_renego_len )
2065 {
2066 mbedtls_printf( "CID length must not change during renegotiation\n" );
2067 goto usage;
2068 }
2069
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002070 if( opt.cid_enabled == 1 )
Hanno Becker8367ccc2019-05-14 11:30:10 +01002071 ret = mbedtls_ssl_conf_cid( &conf, cid_len,
2072 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002073 else
Hanno Becker8367ccc2019-05-14 11:30:10 +01002074 ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
2075 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002076
Hanno Beckerad4a1372019-05-03 13:06:44 +01002077 if( ret != 0 )
2078 {
Hanno Beckerd5eed422019-05-23 16:58:22 +01002079 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
2080 -ret );
Hanno Beckerad4a1372019-05-03 13:06:44 +01002081 goto exit;
2082 }
2083 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002084#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerad4a1372019-05-03 13:06:44 +01002085
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01002086 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002087 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00002088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002090 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Hanno Becker16970d22017-10-10 15:56:37 +01002091 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min,
2092 opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002093
Hanno Becker4d615912018-08-14 13:33:30 +01002094 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01002095 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002099 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002100 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002101 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n",
2102 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002103 goto exit;
2104 }
Paul Bakker05decb22013-08-15 13:33:48 +02002105#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02002106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01002108 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002109 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02002110#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02002111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002113 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002114 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002115#endif
2116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002118 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002119 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002120#endif
2121
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002122#if defined(MBEDTLS_SSL_EXPORT_KEYS)
2123 if( opt.eap_tls != 0 )
2124 mbedtls_ssl_conf_export_keys_ext_cb( &conf, eap_tls_key_derivation,
2125 &eap_tls_keying );
2126#endif
2127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01002129 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002130 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Hanno Becker16970d22017-10-10 15:56:37 +01002131 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
2132 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01002133#endif
2134
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02002135#if defined(MBEDTLS_DHM_C)
2136 if( opt.dhmlen != DFL_DHMLEN )
2137 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
2138#endif
2139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002141 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002142 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002143 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002144 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
2145 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002146 goto exit;
2147 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002148#endif
2149
Philippe Antoine986b6f22019-06-07 15:04:32 +02002150 if (opt.reproducible)
2151 {
Philippe Antoinef91b3722019-06-11 16:02:43 +02002152#if defined(MBEDTLS_HAVE_TIME)
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002153#if defined(MBEDTLS_PLATFORM_TIME_ALT)
2154 mbedtls_platform_set_time( dummy_constant_time );
2155#else
Philippe Antoine7c9d7242019-06-11 12:11:36 +02002156 fprintf( stderr, "Warning: reproducible option used without constant time\n" );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002157#endif
Philippe Antoine0ff84fb2019-06-11 12:15:17 +02002158#endif
Philippe Antoine986b6f22019-06-07 15:04:32 +02002159 }
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002160 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
2161 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002162
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002163 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00002164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002166 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02002167#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002168
Paul Bakker645ce3a2012-10-31 12:32:41 +00002169 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002170 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002171
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002172#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002173 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002174 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002175#endif
Paul Bakker51936882011-02-20 16:05:58 +00002176
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002177 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002178 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002180 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002181#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002184 if( strcmp( opt.ca_path, "none" ) != 0 &&
2185 strcmp( opt.ca_file, "none" ) != 0 )
2186 {
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02002187#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2188 if( opt.ca_callback != 0 )
Hanno Beckercbb59032019-03-28 14:14:22 +00002189 mbedtls_ssl_conf_ca_cb( &conf, ca_callback, &cacert );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02002190 else
2191#endif
2192 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002193 }
2194 if( strcmp( opt.crt_file, "none" ) != 0 &&
2195 strcmp( opt.key_file, "none" ) != 0 )
2196 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002197 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002198 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002199 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
2200 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002201 goto exit;
2202 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002203 }
Paul Bakkered27a042013-04-18 22:46:23 +02002204#endif
2205
Hanno Beckere6706e62017-05-15 16:05:15 +01002206#if defined(MBEDTLS_ECP_C)
2207 if( opt.curves != NULL &&
2208 strcmp( opt.curves, "default" ) != 0 )
2209 {
2210 mbedtls_ssl_conf_curves( &conf, curve_list );
2211 }
2212#endif
2213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckere86964c2018-10-23 11:37:50 +01002215#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002216 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01002217 {
Janos Follathbe4efc22019-08-08 11:38:18 +01002218 key_attributes = psa_key_attributes_init();
2219 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
2220 psa_set_key_algorithm( &key_attributes, alg );
2221 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
Hanno Beckere86964c2018-10-23 11:37:50 +01002222
Janos Follathbe4efc22019-08-08 11:38:18 +01002223 status = psa_import_key( &key_attributes, psk, psk_len, &slot );
Hanno Beckere86964c2018-10-23 11:37:50 +01002224 if( status != PSA_SUCCESS )
2225 {
2226 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2227 goto exit;
2228 }
2229
2230 if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, slot,
2231 (const unsigned char *) opt.psk_identity,
Hanno Becker5cd607b2018-11-05 12:52:42 +00002232 strlen( opt.psk_identity ) ) ) != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01002233 {
2234 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
2235 ret );
2236 goto exit;
2237 }
2238 }
2239 else
2240#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002241 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002242 (const unsigned char *) opt.psk_identity,
2243 strlen( opt.psk_identity ) ) ) != 0 )
2244 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002245 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n",
2246 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002247 goto exit;
2248 }
Hanno Beckere86964c2018-10-23 11:37:50 +01002249#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002250
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002251 if( opt.min_version != DFL_MIN_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01002252 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
2253 opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01002254
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002255 if( opt.max_version != DFL_MAX_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01002256 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
2257 opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00002258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02002260 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002261 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02002262#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002263
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002264 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
2265 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002266 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
2267 -ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002268 goto exit;
2269 }
2270
2271#if defined(MBEDTLS_X509_CRT_PARSE_C)
2272 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
2273 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002274 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
2275 ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002276 goto exit;
2277 }
2278#endif
2279
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002280#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2281 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
2282 {
2283 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
2284 (const unsigned char *) opt.ecjpake_pw,
2285 strlen( opt.ecjpake_pw ) ) ) != 0 )
2286 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002287 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
2288 ret );
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002289 goto exit;
2290 }
2291 }
2292#endif
2293
Hanno Beckerbb425db2019-04-03 12:59:58 +01002294#if defined(MBEDTLS_X509_CRT_PARSE_C)
2295 if( opt.context_crt_cb == 1 )
2296 mbedtls_ssl_set_verify( &ssl, my_verify, NULL );
2297#endif /* MBEDTLS_X509_CRT_PARSE_C */
2298
Hanno Becker8b1af2f2019-07-03 17:02:43 +01002299 io_ctx.ssl = &ssl;
2300 io_ctx.net = &server_fd;
2301 mbedtls_ssl_set_bio( &ssl, &io_ctx, send_cb, recv_cb,
2302 opt.nbio == 0 ? recv_timeout_cb : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002303
Hanno Beckera0e20d02019-05-15 14:03:01 +01002304#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +01002305 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2306 {
2307 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
2308 cid, cid_len ) ) != 0 )
2309 {
2310 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2311 ret );
2312 goto exit;
2313 }
2314 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002315#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01002316
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002317#if defined(MBEDTLS_SSL_PROTO_DTLS)
2318 if( opt.dtls_mtu != DFL_DTLS_MTU )
2319 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
2320#endif
2321
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002322#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002323 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
2324 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002325#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002326
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002327#if defined(MBEDTLS_ECP_RESTARTABLE)
2328 if( opt.ec_max_ops != DFL_EC_MAX_OPS )
2329 mbedtls_ecp_set_max_ops( opt.ec_max_ops );
2330#endif
2331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002333
Paul Bakker5121ce52009-01-03 21:22:43 +00002334 /*
2335 * 4. Handshake
2336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002338 fflush( stdout );
2339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002341 {
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002342 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2343 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002344 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Paul Bakker40e46942009-01-03 21:51:57 +00002345 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002346 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
2347 -ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2349 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01002350 " Unable to verify the server's certificate. "
2351 "Either it is invalid,\n"
2352 " or you didn't set ca_file or ca_path "
2353 "to an appropriate value.\n"
2354 " Alternatively, you may want to use "
2355 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00002358 }
Hanno Becker16970d22017-10-10 15:56:37 +01002359
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002360#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002361 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2362 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002363#endif
2364
Hanno Becker16970d22017-10-10 15:56:37 +01002365 /* For event-driven IO, wait for socket to become available */
2366 if( opt.event == 1 /* level triggered IO */ )
2367 {
2368#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002369 ret = idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002370#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002371 ret = idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002372#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002373 if( ret != 0 )
2374 goto exit;
Hanno Becker16970d22017-10-10 15:56:37 +01002375 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 }
2377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Hanno Becker16970d22017-10-10 15:56:37 +01002379 mbedtls_ssl_get_version( &ssl ),
2380 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
2383 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002384 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002386
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002387#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2388 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
2389 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
2390#endif
2391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002393 if( opt.alpn_string != NULL )
2394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
2396 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002397 alp ? alp : "(none)" );
2398 }
2399#endif
2400
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002401#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Ron Eldor51d3ab52019-05-12 14:54:30 +03002402 if( opt.eap_tls != 0 )
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002403 {
2404 size_t j = 0;
Ron Eldor51d3ab52019-05-12 14:54:30 +03002405
2406 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type,
2407 eap_tls_keying.master_secret,
2408 sizeof( eap_tls_keying.master_secret ),
2409 eap_tls_label,
2410 eap_tls_keying.randbytes,
2411 sizeof( eap_tls_keying.randbytes ),
2412 eap_tls_keymaterial,
2413 sizeof( eap_tls_keymaterial ) ) )
2414 != 0 )
2415 {
2416 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2417 -ret );
2418 goto exit;
2419 }
2420
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002421 mbedtls_printf( " EAP-TLS key material is:" );
2422 for( j = 0; j < sizeof( eap_tls_keymaterial ); j++ )
2423 {
2424 if( j % 8 == 0 )
2425 mbedtls_printf("\n ");
2426 mbedtls_printf("%02x ", eap_tls_keymaterial[j] );
2427 }
2428 mbedtls_printf("\n");
2429
Ron Eldor51d3ab52019-05-12 14:54:30 +03002430 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type, NULL, 0,
Ron Eldor51c45072019-05-15 17:49:54 +03002431 eap_tls_label,
2432 eap_tls_keying.randbytes,
2433 sizeof( eap_tls_keying.randbytes ),
2434 eap_tls_iv,
2435 sizeof( eap_tls_iv ) ) ) != 0 )
Ron Eldor51d3ab52019-05-12 14:54:30 +03002436 {
2437 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2438 -ret );
2439 goto exit;
2440 }
2441
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002442 mbedtls_printf( " EAP-TLS IV is:" );
2443 for( j = 0; j < sizeof( eap_tls_iv ); j++ )
2444 {
2445 if( j % 8 == 0 )
2446 mbedtls_printf("\n ");
2447 mbedtls_printf("%02x ", eap_tls_iv[j] );
2448 }
2449 mbedtls_printf("\n");
2450 }
2451#endif
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002452 if( opt.reconnect != 0 )
2453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002455 fflush( stdout );
2456
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +02002457 if( opt.reco_mode == 1 )
Manuel Pégourié-Gonnard21548632019-05-16 11:39:42 +02002458 {
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +02002459 if( ( ret = mbedtls_ssl_session_save( mbedtls_ssl_get_session_pointer( &ssl ),
2460 session_data, sizeof( session_data ),
2461 &session_data_len ) ) != 0 )
2462 {
2463 mbedtls_printf( " failed\n ! mbedtls_ssl_session_saved returned -0x%04x\n\n",
2464 -ret );
2465 goto exit;
2466 }
2467 }
2468 else
2469 {
2470 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
2471 {
2472 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
2473 -ret );
2474 goto exit;
2475 }
Manuel Pégourié-Gonnard21548632019-05-16 11:39:42 +02002476 }
2477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002479 }
2480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002482 /*
2483 * 5. Verify the server certificate
2484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002487 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002489 char vrfy_buf[512];
2490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
Hanno Becker16970d22017-10-10 15:56:37 +01002493 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ),
2494 " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00002495
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002496 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 }
2498 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
Hanno Beckera9766c2c2019-02-25 17:43:18 +00002501 mbedtls_printf( " . Peer certificate information ...\n" );
2502 mbedtls_printf( "%s\n", peer_crt_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002504
Hanno Beckera0e20d02019-05-15 14:03:01 +01002505#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002506 ret = report_cid_usage( &ssl, "initial handshake" );
2507 if( ret != 0 )
2508 goto exit;
2509
Hanno Becker90cb3592019-04-09 17:24:19 +01002510 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2511 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002512 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
2513 cid_renego,
2514 cid_renego_len ) ) != 0 )
Hanno Becker90cb3592019-04-09 17:24:19 +01002515 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002516 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2517 ret );
2518 return( ret );
Hanno Becker90cb3592019-04-09 17:24:19 +01002519 }
2520 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002521#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01002522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002524 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002525 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002526 /*
2527 * Perform renegotiation (this must be done when the server is waiting
2528 * for input from our side).
2529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002531 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002533 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002534 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002535 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002536 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002537 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002538 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
2539 ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002540 goto exit;
2541 }
Hanno Becker16970d22017-10-10 15:56:37 +01002542
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002543#if defined(MBEDTLS_ECP_RESTARTABLE)
2544 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2545 continue;
2546#endif
2547
Hanno Becker16970d22017-10-10 15:56:37 +01002548 /* For event-driven IO, wait for socket to become available */
2549 if( opt.event == 1 /* level triggered IO */ )
2550 {
2551#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002552 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002553#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002554 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002555#endif
2556 }
2557
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002558 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002560 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002562
Hanno Beckera0e20d02019-05-15 14:03:01 +01002563#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002564 ret = report_cid_usage( &ssl, "after renegotiation" );
2565 if( ret != 0 )
2566 goto exit;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002567#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002568
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 /*
2570 * 6. Write the GET request
2571 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002572 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002573send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 fflush( stdout );
2576
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002577 len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST,
2578 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002579 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002580
2581 /* Add padding to GET request to reach opt.request_size in length */
2582 if( opt.request_size != DFL_REQUEST_SIZE &&
2583 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02002584 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002585 memset( buf + len, 'A', opt.request_size - len - tail_len );
2586 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02002587 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002588
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002589 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002590 len += tail_len;
2591
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02002592 /* Truncate if request size is smaller than the "natural" size */
2593 if( opt.request_size != DFL_REQUEST_SIZE &&
2594 len > opt.request_size )
2595 {
2596 len = opt.request_size;
2597
2598 /* Still end with \r\n unless that's really not possible */
2599 if( len >= 2 ) buf[len - 2] = '\r';
2600 if( len >= 1 ) buf[len - 1] = '\n';
2601 }
2602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002604 {
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002605 written = 0;
2606 frags = 0;
2607
2608 do
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 {
Hanno Becker16970d22017-10-10 15:56:37 +01002610 while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002611 len - written ) ) < 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002612 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002613 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002614 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002615 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002616 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002617 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
2618 -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002619 goto exit;
2620 }
Hanno Becker16970d22017-10-10 15:56:37 +01002621
2622 /* For event-driven IO, wait for socket to become available */
2623 if( opt.event == 1 /* level triggered IO */ )
2624 {
2625#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002626 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002627#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002628 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002629#endif
2630 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002631 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002632
2633 frags++;
2634 written += ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002636 while( written < len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002637 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002638 else /* Not stream, so datagram */
2639 {
Hanno Becker16970d22017-10-10 15:56:37 +01002640 while( 1 )
2641 {
2642 ret = mbedtls_ssl_write( &ssl, buf, len );
2643
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002644#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002645 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002646 continue;
2647#endif
2648
Hanno Becker16970d22017-10-10 15:56:37 +01002649 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2650 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2651 break;
2652
2653 /* For event-driven IO, wait for socket to become available */
2654 if( opt.event == 1 /* level triggered IO */ )
2655 {
2656#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002657 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002658#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002659 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002660#endif
2661 }
2662 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002663
2664 if( ret < 0 )
2665 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002666 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n",
2667 ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002668 goto exit;
2669 }
2670
2671 frags = 1;
2672 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002673
2674 if( written < len )
2675 {
2676 mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
2677 "was truncated to size %u", (unsigned) written );
2678 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002679 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002681 buf[written] = '\0';
Hanno Becker16970d22017-10-10 15:56:37 +01002682 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
2683 written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002685 /* Send a non-empty request if request_size == 0 */
2686 if ( len == 0 )
2687 {
2688 opt.request_size = DFL_REQUEST_SIZE;
2689 goto send_request;
2690 }
2691
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 /*
2693 * 7. Read the HTTP response
2694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 fflush( stdout );
2697
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002698 /*
2699 * TLS and DTLS need different reading styles (stream vs datagram)
2700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002702 {
2703 do
2704 {
2705 len = sizeof( buf ) - 1;
2706 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002708
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002709#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002710 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002711 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002712#endif
2713
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002714 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
2715 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Hanno Becker16970d22017-10-10 15:56:37 +01002716 {
2717 /* For event-driven IO, wait for socket to become available */
2718 if( opt.event == 1 /* level triggered IO */ )
2719 {
2720#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002721 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002722#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002723 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002724#endif
2725 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002726 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01002727 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002728
2729 if( ret <= 0 )
2730 {
2731 switch( ret )
2732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2734 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002735 ret = 0;
2736 goto close_notify;
2737
2738 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 case MBEDTLS_ERR_NET_CONN_RESET:
2740 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002741 ret = 0;
2742 goto reconnect;
2743
2744 default:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002745 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n",
2746 -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002747 goto exit;
2748 }
2749 }
2750
2751 len = ret;
2752 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002754
2755 /* End of message should be detected according to the syntax of the
2756 * application protocol (eg HTTP), just use a dummy test here. */
2757 if( ret > 0 && buf[len-1] == '\n' )
2758 {
2759 ret = 0;
2760 break;
2761 }
2762 }
2763 while( 1 );
2764 }
2765 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00002766 {
2767 len = sizeof( buf ) - 1;
2768 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002769
Hanno Becker16970d22017-10-10 15:56:37 +01002770 while( 1 )
2771 {
2772 ret = mbedtls_ssl_read( &ssl, buf, len );
2773
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002774#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002775 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002776 continue;
2777#endif
2778
Hanno Becker16970d22017-10-10 15:56:37 +01002779 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2780 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2781 break;
2782
2783 /* For event-driven IO, wait for socket to become available */
2784 if( opt.event == 1 /* level triggered IO */ )
2785 {
2786#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002787 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002788#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002789 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002790#endif
2791 }
2792 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002793
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002794 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002795 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002796 switch( ret )
2797 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002798 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002800 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02002801 goto send_request;
2802 goto exit;
2803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2805 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002806 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002807 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00002808
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002809 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002811 goto exit;
2812 }
Paul Bakker5690efc2011-05-26 13:16:06 +00002813 }
2814
Paul Bakker5121ce52009-01-03 21:22:43 +00002815 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02002816 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002818 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002820
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002821 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002822 * 7b. Simulate hard reset and reconnect from same port?
2823 */
2824 if( opt.reconnect_hard != 0 )
2825 {
2826 opt.reconnect_hard = 0;
2827
2828 mbedtls_printf( " . Restarting connection from same port..." );
2829 fflush( stdout );
2830
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002831#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker23699ef2019-02-26 12:36:53 +00002832 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002833#endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Becker23699ef2019-02-26 12:36:53 +00002834
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002835 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
2836 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002837 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2838 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002839 goto exit;
2840 }
2841
2842 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
2843 {
2844 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002845 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002846 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002847 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002848 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2849 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002850 goto exit;
2851 }
Hanno Becker16970d22017-10-10 15:56:37 +01002852
2853 /* For event-driven IO, wait for socket to become available */
2854 if( opt.event == 1 /* level triggered IO */ )
2855 {
2856#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002857 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002858#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002859 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002860#endif
2861 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002862 }
2863
2864 mbedtls_printf( " ok\n" );
2865
2866 goto send_request;
2867 }
2868
2869 /*
2870 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002871 */
2872 if( --opt.exchanges > 0 )
2873 goto send_request;
2874
2875 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002876 * 8. Done, cleanly close the connection
2877 */
2878close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002880 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002881
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002882 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002884 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002885 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002888
2889 /*
2890 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002891 */
2892reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002893 if( opt.reconnect != 0 )
2894 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02002895 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002896
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002897 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002898
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002899#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002900 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02002901 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002902#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002905
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002906#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckera1051b42019-02-26 11:38:29 +00002907 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002908#endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Beckera1051b42019-02-26 11:38:29 +00002909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002911 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002912 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2913 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002914 goto exit;
2915 }
2916
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +02002917 if( opt.reco_mode == 1 )
Manuel Pégourié-Gonnard21548632019-05-16 11:39:42 +02002918 {
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +02002919 if( ( ret = mbedtls_ssl_session_load( &saved_session,
2920 session_data,
2921 session_data_len ) ) != 0 )
2922 {
2923 mbedtls_printf( " failed\n ! mbedtls_ssl_session_load returned -0x%x\n\n",
2924 -ret );
2925 goto exit;
2926 }
Manuel Pégourié-Gonnard21548632019-05-16 11:39:42 +02002927 }
2928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002930 {
Manuel Pégourié-Gonnard21548632019-05-16 11:39:42 +02002931 mbedtls_printf( " failed\n ! mbedtls_ssl_set_session returned -0x%x\n\n",
2932 -ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002933 goto exit;
2934 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002935
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002936 if( ( ret = mbedtls_net_connect( &server_fd,
2937 opt.server_addr, opt.server_port,
2938 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2939 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002940 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002941 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
2942 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002943 goto exit;
2944 }
2945
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002946 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002947 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002948 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002949 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002950 if( ret != 0 )
2951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002953 -ret );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002954 goto exit;
2955 }
2956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002958 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002959 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002960 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002961 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002962 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002963 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2964 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002965 goto exit;
2966 }
2967 }
2968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002970
2971 goto send_request;
2972 }
2973
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002974 /*
2975 * Cleanup and exit
2976 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002977exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00002979 if( ret != 0 )
2980 {
2981 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982 mbedtls_strerror( ret, error_buf, 100 );
2983 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002984 }
2985#endif
2986
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002987 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989#if defined(MBEDTLS_X509_CRT_PARSE_C)
2990 mbedtls_x509_crt_free( &clicert );
2991 mbedtls_x509_crt_free( &cacert );
2992 mbedtls_pk_free( &pkey );
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01002993#if defined(MBEDTLS_USE_PSA_CRYPTO)
2994 psa_destroy_key( key_slot );
2995#endif
Paul Bakkered27a042013-04-18 22:46:23 +02002996#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997 mbedtls_ssl_session_free( &saved_session );
2998 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002999 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 mbedtls_ctr_drbg_free( &ctr_drbg );
3001 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003002
Hanno Becker3f24ea92018-11-05 13:25:17 +00003003#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
3004 defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00003005 if( opt.psk_opaque != 0 )
Hanno Becker3f24ea92018-11-05 13:25:17 +00003006 {
3007 /* This is ok even if the slot hasn't been
3008 * initialized (we might have jumed here
3009 * immediately because of bad cmd line params,
3010 * for example). */
Hanno Becker1d911cd2018-11-15 13:06:09 +00003011 status = psa_destroy_key( slot );
Hanno Becker3f24ea92018-11-05 13:25:17 +00003012 if( status != PSA_SUCCESS )
3013 {
3014 mbedtls_printf( "Failed to destroy key slot %u - error was %d",
Hanno Becker1d911cd2018-11-15 13:06:09 +00003015 (unsigned) slot, (int) status );
Hanno Becker3f24ea92018-11-05 13:25:17 +00003016 if( ret == 0 )
3017 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
3018 }
3019 }
3020#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED &&
3021 MBEDTLS_USE_PSA_CRYPTO */
3022
Paul Bakkercce9d772011-11-18 14:26:47 +00003023#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003025 fflush( stdout ); getchar();
3026#endif
3027
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02003028 // Shell can not handle large exit numbers -> 1 for errors
3029 if( ret < 0 )
3030 ret = 1;
3031
Paul Bakker5121ce52009-01-03 21:22:43 +00003032 return( ret );
3033}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
3035 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003036 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */