blob: 5a4baa3daef26bae0f9ef86c4f516481ddc16849 [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
Hanno Becker90cb3592019-04-09 17:24:19 +0100116#define DFL_CID_ENABLED 0
117#define DFL_CID_VALUE ""
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100118#define DFL_CID_ENABLED_RENEGO -1
119#define DFL_CID_VALUE_RENEGO NULL
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200120#define DFL_RECONNECT_HARD 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200122#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100123#define DFL_CURVES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200125#define DFL_HS_TO_MIN 0
126#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200127#define DFL_DTLS_MTU -1
Hanno Becker4d615912018-08-14 13:33:30 +0100128#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200129#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200130#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100131#define DFL_ETM -1
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200132#define DFL_CA_CALLBACK 0
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300133#define DFL_EAP_TLS 0
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200134#define DFL_REPRODUCIBLE 0
Paul Bakker0e6975b2009-02-10 22:19:10 +0000135
Paul Bakker93c32b22014-04-25 13:40:05 +0200136#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
137#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if defined(MBEDTLS_X509_CRT_PARSE_C)
Janos Follathae13beb2019-04-05 14:06:58 +0100140#define USAGE_CONTEXT_CRT_CB \
Hanno Beckerbb425db2019-04-03 12:59:58 +0100141 " context_crt_cb=%%d This determines whether the CRT verification callback is bound\n" \
142 " to the SSL configuration of the SSL context.\n" \
143 " Possible values:\n"\
144 " - 0 (default): Use CRT callback bound to configuration\n" \
145 " - 1: Use CRT callback bound to SSL context\n"
146#else
Janos Follathae13beb2019-04-05 14:06:58 +0100147#define USAGE_CONTEXT_CRT_CB ""
Hanno Beckerbb425db2019-04-03 12:59:58 +0100148#endif /* MBEDTLS_X509_CRT_PARSE_C */
149#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000151#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100152 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
153 " default: \"\" (pre-loaded)\n" \
154 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
155 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
156 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
157 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000158 " key_file=%%s default: \"\" (pre-loaded)\n"
159#else
160#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 " No file operations available (MBEDTLS_FS_IO not defined)\n"
162#endif /* MBEDTLS_FS_IO */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100163#else /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200164#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100166#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
167#define USAGE_KEY_OPAQUE \
168 " key_opaque=%%d Handle your private key as if it were opaque\n" \
169 " default: 0 (disabled)\n"
170#else
171#define USAGE_KEY_OPAQUE ""
172#endif
Paul Bakkered27a042013-04-18 22:46:23 +0200173
Hanno Beckera0e20d02019-05-15 14:03:01 +0100174#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +0100175#define USAGE_CID \
176 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
177 " default: 0 (disabled)\n" \
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100178 " 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 +0100179 " default: same as 'cid' parameter\n" \
Hanno Becker90cb3592019-04-09 17:24:19 +0100180 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100181 " default: \"\"\n" \
182 " 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 +0100183 " default: same as 'cid_val' parameter\n"
Hanno Beckera0e20d02019-05-15 14:03:01 +0100184#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +0100185#define USAGE_CID ""
Hanno Beckera0e20d02019-05-15 14:03:01 +0100186#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +0100187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckere86964c2018-10-23 11:37:50 +0100189#define USAGE_PSK_RAW \
Paul Bakkered27a042013-04-18 22:46:23 +0200190 " psk=%%s default: \"\" (in hex, without 0x)\n" \
191 " psk_identity=%%s default: \"Client_identity\"\n"
Hanno Beckere86964c2018-10-23 11:37:50 +0100192#if defined(MBEDTLS_USE_PSA_CRYPTO)
193#define USAGE_PSK_SLOT \
Hanno Becker1d911cd2018-11-15 13:06:09 +0000194 " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
195 " Enable this to store the PSK configured through command line\n" \
196 " parameter `psk` in a PSA-based key slot.\n" \
Hanno Beckere86964c2018-10-23 11:37:50 +0100197 " Note: Currently only supported in conjunction with\n" \
198 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
199 " to force a particular PSK-only ciphersuite.\n" \
200 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
201 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
202 " with prepopulated key slots instead of importing raw key material.\n"
203#else
204#define USAGE_PSK_SLOT ""
205#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker1bac87c2019-03-29 12:02:26 +0000206#define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
207#else
208#define USAGE_PSK ""
209#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
210
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200211#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
212#define USAGE_CA_CALLBACK \
213 " ca_callback=%%d default: 0 (disabled)\n" \
214 " Enable this to use the trusted certificate callback function\n"
215#else
216#define USAGE_CA_CALLBACK ""
217#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5690efc2011-05-26 13:16:06 +0000218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200220#define USAGE_TICKETS \
221 " tickets=%%d default: 1 (enabled)\n"
222#else
223#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200225
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300226#if defined(MBEDTLS_SSL_EXPORT_KEYS)
227#define USAGE_EAP_TLS \
228 " eap_tls=%%d default: 0 (disabled)\n"
229#else
230#define USAGE_EAP_TLS ""
231#endif /* MBEDTLS_SSL_EXPORT_KEYS */
232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200234#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100235 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200236#else
237#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200241#define USAGE_MAX_FRAG_LEN \
242 " max_frag_len=%%d default: 16384 (tls default)\n" \
243 " options: 512, 1024, 2048, 4096\n"
244#else
245#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100249#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200250 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100251#else
252#define USAGE_RECSPLIT
253#endif
254
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200255#if defined(MBEDTLS_DHM_C)
256#define USAGE_DHMLEN \
257 " dhmlen=%%d default: (library default: 1024 bits)\n"
258#else
259#define USAGE_DHMLEN
260#endif
261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200263#define USAGE_ALPN \
264 " alpn=%%s default: \"\" (disabled)\n" \
265 " example: spdy/1,http/1.1\n"
266#else
267#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200269
Hanno Beckere6706e62017-05-15 16:05:15 +0100270#if defined(MBEDTLS_ECP_C)
271#define USAGE_CURVES \
272 " curves=a,b,c,d default: \"default\" (library default)\n" \
273 " example: \"secp521r1,brainpoolP512r1\"\n" \
274 " - use \"none\" for empty list\n" \
275 " - see mbedtls_ecp_curve_list()\n" \
276 " for acceptable curve names\n"
277#else
278#define USAGE_CURVES ""
279#endif
280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200282#define USAGE_DTLS \
283 " dtls=%%d default: 0 (TLS)\n" \
284 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200285 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Becker4d615912018-08-14 13:33:30 +0100286 " mtu=%%d default: (library default: unlimited)\n" \
287 " dgram_packing=%%d default: 1 (allowed)\n" \
288 " allow or forbid packing of multiple\n" \
289 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200290#else
291#define USAGE_DTLS ""
292#endif
293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200295#define USAGE_FALLBACK \
296 " fallback=0/1 default: (library default: off)\n"
297#else
298#define USAGE_FALLBACK ""
299#endif
300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200302#define USAGE_EMS \
303 " extended_ms=0/1 default: (library default: on)\n"
304#else
305#define USAGE_EMS ""
306#endif
307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100309#define USAGE_ETM \
310 " etm=0/1 default: (library default: on)\n"
311#else
312#define USAGE_ETM ""
313#endif
314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100316#define USAGE_RENEGO \
317 " renegotiation=%%d default: 0 (disabled)\n" \
318 " renegotiate=%%d default: 0 (disabled)\n"
319#else
320#define USAGE_RENEGO ""
321#endif
322
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200323#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
324#define USAGE_ECJPAKE \
325 " ecjpake_pw=%%s default: none (disabled)\n"
326#else
327#define USAGE_ECJPAKE ""
328#endif
329
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200330#if defined(MBEDTLS_ECP_RESTARTABLE)
331#define USAGE_ECRESTART \
332 " ec_max_ops=%%s default: library default (restart disabled)\n"
333#else
334#define USAGE_ECRESTART ""
335#endif
336
Paul Bakker9caf2d22010-02-18 19:37:19 +0000337#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000338 "\n usage: ssl_client2 param=<>...\n" \
339 "\n acceptable parameters:\n" \
340 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100341 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000342 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000343 " request_page=%%s default: \".\"\n" \
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +0100344 " request_size=%%d default: about 34 (basic request)\n" \
345 " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
346 " If 0, in the first exchange only an empty\n" \
347 " application data message is sent followed by\n" \
348 " a second non-empty message before attempting\n" \
349 " to read a response from the server\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100350 " debug_level=%%d default: 0 (disabled)\n" \
351 " nbio=%%d default: 0 (blocking I/O)\n" \
352 " options: 1 (non-blocking), 2 (added delays)\n" \
353 " event=%%d default: 0 (loop)\n" \
354 " options: 1 (level-triggered, implies nbio=1),\n" \
355 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200356 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100357 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200358 USAGE_DTLS \
Hanno Becker90cb3592019-04-09 17:24:19 +0100359 USAGE_CID \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200360 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100361 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100362 " options: none, optional, required\n" \
363 USAGE_IO \
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100364 USAGE_KEY_OPAQUE \
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200365 USAGE_CA_CALLBACK \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100366 "\n" \
367 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200368 USAGE_ECJPAKE \
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200369 USAGE_ECRESTART \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100370 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100371 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100372 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200373 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200374 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200375 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200376 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200377 USAGE_TICKETS \
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300378 USAGE_EAP_TLS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100379 USAGE_MAX_FRAG_LEN \
380 USAGE_TRUNC_HMAC \
Janos Follathae13beb2019-04-05 14:06:58 +0100381 USAGE_CONTEXT_CRT_CB \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200382 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200383 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200384 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100385 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100386 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100387 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200388 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000389 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100390 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200391 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200392 " min_version=%%s default: (library default: tls1)\n" \
393 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000394 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100395 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000396 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000397 " force_ciphersuite=<name> default: all enabled\n"\
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100398 " query_config=<name> return 0 if the specified\n" \
399 " configuration macro is defined and 1\n" \
400 " otherwise. The expansion of the macro\n" \
401 " is printed if it is defined\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000402 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000403
Hanno Becker8651a432017-06-09 16:13:22 +0100404#define ALPN_LIST_SIZE 10
405#define CURVE_LIST_SIZE 20
406
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500407#if defined(MBEDTLS_CHECK_PARAMS)
408#include "mbedtls/platform_util.h"
409void mbedtls_param_failed( const char *failure_condition,
410 const char *file,
411 int line )
412{
413 mbedtls_printf( "%s:%i: Input param failed - %s\n",
414 file, line, failure_condition );
415 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
416}
417#endif
418
Rich Evans85b05ec2015-02-12 11:37:29 +0000419/*
420 * global options
421 */
422struct options
423{
424 const char *server_name; /* hostname of the server (client only) */
425 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200426 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000427 int debug_level; /* level of debugging */
428 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100429 int event; /* loop or event-driven IO? level or edge triggered? */
430 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000431 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000432 const char *request_page; /* page on server to request */
433 int request_size; /* pad request with header to requested size */
434 const char *ca_file; /* the file with the CA certificate(s) */
435 const char *ca_path; /* the path with the CA certificate(s) reside */
436 const char *crt_file; /* the file with the client certificate */
437 const char *key_file; /* the file with the client key */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100438 int key_opaque; /* handle private key as if it were opaque */
Hanno Beckere86964c2018-10-23 11:37:50 +0100439#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000440 int psk_opaque;
Hanno Beckere86964c2018-10-23 11:37:50 +0100441#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200442#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000443 int ca_callback; /* Use callback for trusted certificate list */
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200444#endif
Rich Evans85b05ec2015-02-12 11:37:29 +0000445 const char *psk; /* the pre-shared key */
446 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200447 const char *ecjpake_pw; /* the EC J-PAKE password */
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200448 int ec_max_ops; /* EC consecutive operations limit */
Rich Evans85b05ec2015-02-12 11:37:29 +0000449 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
450 int renegotiation; /* enable / disable renegotiation */
451 int allow_legacy; /* allow legacy renegotiation */
452 int renegotiate; /* attempt renegotiation? */
453 int renego_delay; /* delay before enforcing renegotiation */
454 int exchanges; /* number of data exchanges */
455 int min_version; /* minimum protocol version accepted */
456 int max_version; /* maximum protocol version accepted */
457 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200458 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000459 int auth_mode; /* verify mode for connection */
460 unsigned char mfl_code; /* code for maximum fragment length */
461 int trunc_hmac; /* negotiate truncated hmac or not */
462 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200463 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000464 int reconnect; /* attempt to resume session */
465 int reco_delay; /* delay in seconds before resuming session */
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 Antoineaa4d1522019-06-06 21:24:31 +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
550int dummy_random( void *p_rng, unsigned char *output, size_t output_len )
551{
552 int ret;
553 size_t i;
554
555 //use mbedtls_ctr_drbg_random to find bugs in it
Philippe Antoine986b6f22019-06-07 15:04:32 +0200556 ret = mbedtls_ctr_drbg_random( p_rng, output, output_len );
557 for ( i = 0; i < output_len; i++ )
558 {
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200559 //replace result with pseudo random
560 output[i] = (unsigned char) rand();
561 }
562 return( ret );
563}
564
565int dummy_entropy( void *data, unsigned char *output, size_t len )
566{
567 size_t i;
568 (void) data;
569
Philippe Antoine986b6f22019-06-07 15:04:32 +0200570 //ret = mbedtls_entropy_func( data, output, len );
571 for ( i = 0; i < len; i++ )
572 {
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200573 //replace result with pseudo random
574 output[i] = (unsigned char) rand();
575 }
576 return( 0 );
577}
578
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200579#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000580int ca_callback( void *data, mbedtls_x509_crt const *child,
Janos Follathd7ecbd62019-04-05 14:52:17 +0100581 mbedtls_x509_crt **candidates )
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200582{
Hanno Beckercbb59032019-03-28 14:14:22 +0000583 int ret = 0;
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200584 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +0000585 mbedtls_x509_crt *first;
586
587 /* This is a test-only implementation of the CA callback
588 * which always returns the entire list of trusted certificates.
589 * Production implementations managing a large number of CAs
590 * should use an efficient presentation and lookup for the
591 * set of trusted certificates (such as a hashtable) and only
592 * return those trusted certificates which satisfy basic
593 * parental checks, such as the matching of child `Issuer`
Jarno Lamsaf7a7f9e2019-04-01 15:11:54 +0300594 * and parent `Subject` field or matching key identifiers. */
Hanno Beckercbb59032019-03-28 14:14:22 +0000595 ((void) child);
596
597 first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
598 if( first == NULL )
599 {
600 ret = -1;
601 goto exit;
602 }
603 mbedtls_x509_crt_init( first );
604
605 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
606 {
607 ret = -1;
608 goto exit;
609 }
610
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200611 while( ca->next != NULL )
612 {
613 ca = ca->next;
Hanno Beckercbb59032019-03-28 14:14:22 +0000614 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
615 {
616 ret = -1;
617 goto exit;
618 }
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200619 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000620
621exit:
622
623 if( ret != 0 )
624 {
625 mbedtls_x509_crt_free( first );
626 mbedtls_free( first );
627 first = NULL;
628 }
629
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200630 *candidates = first;
Hanno Beckercbb59032019-03-28 14:14:22 +0000631 return( ret );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200632}
633#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
634
Rich Evans85b05ec2015-02-12 11:37:29 +0000635/*
636 * Test recv/send functions that make sure each try returns
637 * WANT_READ/WANT_WRITE at least once before sucesseding
638 */
639static int my_recv( void *ctx, unsigned char *buf, size_t len )
640{
641 static int first_try = 1;
642 int ret;
643
644 if( first_try )
645 {
646 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100647 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000648 }
649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100651 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000652 first_try = 1; /* Next call will be a new operation */
653 return( ret );
654}
655
656static int my_send( void *ctx, const unsigned char *buf, size_t len )
657{
658 static int first_try = 1;
659 int ret;
660
661 if( first_try )
662 {
663 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100664 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000665 }
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100668 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000669 first_try = 1; /* Next call will be a new operation */
670 return( ret );
671}
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckera1051b42019-02-26 11:38:29 +0000674static unsigned char peer_crt_info[1024];
Hanno Beckera9766c2c2019-02-25 17:43:18 +0000675
Rich Evans85b05ec2015-02-12 11:37:29 +0000676/*
677 * Enabled if debug_level > 1 in code below
678 */
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100679static int my_verify( void *data, mbedtls_x509_crt *crt,
680 int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000681{
682 char buf[1024];
683 ((void) data);
684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Hanno Beckera9766c2c2019-02-25 17:43:18 +0000686 if( depth == 0 )
687 memcpy( peer_crt_info, buf, sizeof( buf ) );
688
689 if( opt.debug_level == 0 )
690 return( 0 );
691
692 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000694
Rich Evans85b05ec2015-02-12 11:37:29 +0000695 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100697 else
698 {
699 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
700 mbedtls_printf( "%s\n", buf );
701 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000702
703 return( 0 );
704}
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200705
706static int ssl_sig_hashes_for_test[] = {
707#if defined(MBEDTLS_SHA512_C)
708 MBEDTLS_MD_SHA512,
709 MBEDTLS_MD_SHA384,
710#endif
711#if defined(MBEDTLS_SHA256_C)
712 MBEDTLS_MD_SHA256,
713 MBEDTLS_MD_SHA224,
714#endif
715#if defined(MBEDTLS_SHA1_C)
716 /* Allow SHA-1 as we use it extensively in tests. */
717 MBEDTLS_MD_SHA1,
718#endif
719 MBEDTLS_MD_NONE
720};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000722
Hanno Becker16970d22017-10-10 15:56:37 +0100723/*
724 * Wait for an event from the underlying transport or the timer
725 * (Used in event-driven IO mode).
726 */
727#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000728int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000729 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100730#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000731int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000732 mbedtls_timing_delay_context *timer,
733 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100734#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000735{
Hanno Becker16970d22017-10-10 15:56:37 +0100736
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000737 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +0100738 int poll_type = 0;
739
740 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
741 poll_type = MBEDTLS_NET_POLL_WRITE;
742 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
743 poll_type = MBEDTLS_NET_POLL_READ;
744#if !defined(MBEDTLS_TIMING_C)
745 else
Hanno Beckeref527962018-03-15 15:49:24 +0000746 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100747#endif
748
749 while( 1 )
750 {
Hanno Becker197a91c2017-10-31 10:58:53 +0000751 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +0100752#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +0000753 if( timer != NULL &&
754 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +0100755 {
Hanno Becker16970d22017-10-10 15:56:37 +0100756 break;
757 }
Hanno Becker197a91c2017-10-31 10:58:53 +0000758#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +0100759
Hanno Becker197a91c2017-10-31 10:58:53 +0000760 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000761 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +0100762 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000763 ret = mbedtls_net_poll( fd, poll_type, 0 );
764 if( ret < 0 )
765 return( ret );
766 if( ret == poll_type )
767 break;
Hanno Becker16970d22017-10-10 15:56:37 +0100768 }
769 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000770
771 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100772}
773
Hanno Becker1f583ee2019-04-09 17:12:56 +0100774/* Unhexify `hex` into `dst`. `dst` must have
775 * size at least `strlen( hex ) / 2`. */
Hanno Becker90cb3592019-04-09 17:24:19 +0100776int unhexify( char const *hex, unsigned char *dst )
Hanno Becker1f583ee2019-04-09 17:12:56 +0100777{
778 unsigned char c;
779 size_t j;
780 size_t len = strlen( hex );
781
782 if( len % 2 != 0 )
783 return( -1 );
784
785 for( j = 0; j < len; j += 2 )
786 {
787 c = hex[j];
788 if( c >= '0' && c <= '9' )
789 c -= '0';
790 else if( c >= 'a' && c <= 'f' )
791 c -= 'a' - 10;
792 else if( c >= 'A' && c <= 'F' )
793 c -= 'A' - 10;
794 else
795 return( -1 );
796 dst[ j / 2 ] = c << 4;
797
798 c = hex[j + 1];
799 if( c >= '0' && c <= '9' )
800 c -= '0';
801 else if( c >= 'a' && c <= 'f' )
802 c -= 'a' - 10;
803 else if( c >= 'A' && c <= 'F' )
804 c -= 'A' - 10;
805 else
806 return( -1 );
807 dst[ j / 2 ] |= c;
808 }
809
810 return( 0 );
811}
812
Hanno Beckera0e20d02019-05-15 14:03:01 +0100813#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100814int report_cid_usage( mbedtls_ssl_context *ssl,
815 const char *additional_description )
816{
817 int ret;
818 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
819 size_t peer_cid_len;
820 int cid_negotiated;
821
822 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
823 return( 0 );
824
Hanno Becker6ae14c02019-05-22 16:59:25 +0100825 /* Check if the use of a CID has been negotiated,
826 * but don't ask for the CID value and length.
827 *
828 * Note: Here and below, we're demonstrating the various ways
829 * in which mbedtls_ssl_get_peer_cid() can be called,
830 * depending on whether or not the length/value of the
831 * peer's CID is needed.
832 *
833 * An actual application, however, should use
834 * just one call to mbedtls_ssl_get_peer_cid(). */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100835 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
Hanno Becker6ae14c02019-05-22 16:59:25 +0100836 NULL, NULL );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100837 if( ret != 0 )
838 {
839 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
840 -ret );
841 return( ret );
842 }
843
844 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
845 {
846 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
847 {
848 mbedtls_printf( "(%s) Use of Connection ID was rejected by the server.\n",
849 additional_description );
850 }
851 }
852 else
853 {
854 size_t idx=0;
855 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
856 additional_description );
Hanno Becker6ae14c02019-05-22 16:59:25 +0100857
858 /* Ask for just the length of the peer's CID. */
859 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
860 NULL, &peer_cid_len );
861 if( ret != 0 )
862 {
863 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
864 -ret );
865 return( ret );
866 }
867
868 /* Ask for just length + value of the peer's CID. */
869 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
870 peer_cid, &peer_cid_len );
871 if( ret != 0 )
872 {
873 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
874 -ret );
875 return( ret );
876 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100877 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
878 additional_description,
879 (unsigned) peer_cid_len );
880 while( idx < peer_cid_len )
881 {
882 mbedtls_printf( "%02x ", peer_cid[ idx ] );
883 idx++;
884 }
885 mbedtls_printf( "\n" );
886 }
887
888 return( 0 );
889}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100890#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100891
Paul Bakker9caf2d22010-02-18 19:37:19 +0000892int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000893{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200894 int ret = 0, len, tail_len, i, written, frags, retry_left;
895 mbedtls_net_context server_fd;
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100896
897 unsigned char buf[MAX_REQUEST_SIZE + 1];
898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
900 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200901 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200902#endif
Hanno Becker90cb3592019-04-09 17:24:19 +0100903
Hanno Beckera0e20d02019-05-15 14:03:01 +0100904#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +0100905 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100906 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker90cb3592019-04-09 17:24:19 +0100907 size_t cid_len = 0;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100908 size_t cid_renego_len = 0;
Hanno Becker90cb3592019-04-09 17:24:19 +0100909#endif
910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100912 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200913#endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100914#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +0100915 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100916 const mbedtls_ecp_curve_info *curve_cur;
917#endif
918
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200919 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000920
Hanno Beckere86964c2018-10-23 11:37:50 +0100921#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500922 psa_key_handle_t slot = 0;
Hanno Beckere86964c2018-10-23 11:37:50 +0100923 psa_algorithm_t alg = 0;
924 psa_key_policy_t policy;
925 psa_status_t status;
926#endif
927
Gilles Peskineef86ab22017-05-05 18:59:02 +0200928#if defined(MBEDTLS_X509_CRT_PARSE_C)
929 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
930#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 mbedtls_entropy_context entropy;
932 mbedtls_ctr_drbg_context ctr_drbg;
933 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200934 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200936#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200937 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200938#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200940 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 mbedtls_x509_crt cacert;
942 mbedtls_x509_crt clicert;
943 mbedtls_pk_context pkey;
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +0100944#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500945 psa_key_handle_t key_slot = 0; /* invalid key slot */
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +0100946#endif
Paul Bakkered27a042013-04-18 22:46:23 +0200947#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000948 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000949 const int *list;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300950#if defined(MBEDTLS_SSL_EXPORT_KEYS)
951 unsigned char eap_tls_keymaterial[16];
952 unsigned char eap_tls_iv[8];
953 const char* eap_tls_label = "client EAP encryption";
954 eap_tls_keys eap_tls_keying;
955#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000956
Paul Bakker1a207ec2011-02-06 13:22:40 +0000957 /*
958 * Make sure memory references are valid.
959 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200960 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200961 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200962 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200964 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965#if defined(MBEDTLS_X509_CRT_PARSE_C)
966 mbedtls_x509_crt_init( &cacert );
967 mbedtls_x509_crt_init( &clicert );
968 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200969#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200971 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200972#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000973
Hanno Beckerb2b468b2018-11-12 17:46:59 +0000974#if defined(MBEDTLS_USE_PSA_CRYPTO)
975 status = psa_crypto_init();
976 if( status != PSA_SUCCESS )
977 {
978 mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
979 (int) status );
980 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
981 goto exit;
982 }
983#endif
984
Paul Bakker9caf2d22010-02-18 19:37:19 +0000985 if( argc == 0 )
986 {
987 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000988 if( ret == 0 )
989 ret = 1;
990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000994 while( *list )
995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200997 list++;
998 if( !*list )
999 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +00001001 list++;
1002 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +00001004 goto exit;
1005 }
1006
1007 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001008 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001009 opt.server_port = DFL_SERVER_PORT;
1010 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker90cb3592019-04-09 17:24:19 +01001011 opt.cid_enabled = DFL_CID_ENABLED;
1012 opt.cid_val = DFL_CID_VALUE;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001013 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
1014 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001015 opt.nbio = DFL_NBIO;
Hanno Becker16970d22017-10-10 15:56:37 +01001016 opt.event = DFL_EVENT;
Hanno Beckerbb425db2019-04-03 12:59:58 +01001017 opt.context_crt_cb = DFL_CONTEXT_CRT_CB;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001018 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001019 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001020 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +02001021 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +00001022 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +00001023 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +00001024 opt.crt_file = DFL_CRT_FILE;
1025 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001026 opt.key_opaque = DFL_KEY_OPAQUE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001027 opt.psk = DFL_PSK;
Hanno Beckere86964c2018-10-23 11:37:50 +01001028#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001029 opt.psk_opaque = DFL_PSK_OPAQUE;
Hanno Beckere86964c2018-10-23 11:37:50 +01001030#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001031#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1032 opt.ca_callback = DFL_CA_CALLBACK;
1033#endif
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001034 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001035 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001036 opt.ec_max_ops = DFL_EC_MAX_OPS;
Paul Bakker51936882011-02-20 16:05:58 +00001037 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +00001038 opt.renegotiation = DFL_RENEGOTIATION;
1039 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001040 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001041 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001042 opt.min_version = DFL_MIN_VERSION;
1043 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001044 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001045 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001046 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001047 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001048 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001049 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001050 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001051 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001052 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001053 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001054 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001055 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001056 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001057 opt.transport = DFL_TRANSPORT;
1058 opt.hs_to_min = DFL_HS_TO_MIN;
1059 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001060 opt.dtls_mtu = DFL_DTLS_MTU;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001061 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001062 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001063 opt.etm = DFL_ETM;
Hanno Becker4d615912018-08-14 13:33:30 +01001064 opt.dgram_packing = DFL_DGRAM_PACKING;
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001065 opt.eap_tls = DFL_EAP_TLS;
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001066 opt.reproducible = DFL_REPRODUCIBLE;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001067
1068 for( i = 1; i < argc; i++ )
1069 {
Paul Bakker9caf2d22010-02-18 19:37:19 +00001070 p = argv[i];
1071 if( ( q = strchr( p, '=' ) ) == NULL )
1072 goto usage;
1073 *q++ = '\0';
1074
1075 if( strcmp( p, "server_name" ) == 0 )
1076 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001077 else if( strcmp( p, "server_addr" ) == 0 )
1078 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001079 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001080 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001081 else if( strcmp( p, "dtls" ) == 0 )
1082 {
1083 int t = atoi( q );
1084 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001086 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001088 else
1089 goto usage;
1090 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001091 else if( strcmp( p, "debug_level" ) == 0 )
1092 {
1093 opt.debug_level = atoi( q );
1094 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1095 goto usage;
1096 }
Hanno Beckerbb425db2019-04-03 12:59:58 +01001097 else if( strcmp( p, "context_crt_cb" ) == 0 )
1098 {
1099 opt.context_crt_cb = atoi( q );
1100 if( opt.context_crt_cb != 0 && opt.context_crt_cb != 1 )
1101 goto usage;
1102 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001103 else if( strcmp( p, "nbio" ) == 0 )
1104 {
1105 opt.nbio = atoi( q );
1106 if( opt.nbio < 0 || opt.nbio > 2 )
1107 goto usage;
1108 }
Hanno Becker16970d22017-10-10 15:56:37 +01001109 else if( strcmp( p, "event" ) == 0 )
1110 {
1111 opt.event = atoi( q );
1112 if( opt.event < 0 || opt.event > 2 )
1113 goto usage;
1114 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001115 else if( strcmp( p, "read_timeout" ) == 0 )
1116 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001117 else if( strcmp( p, "max_resend" ) == 0 )
1118 {
1119 opt.max_resend = atoi( q );
1120 if( opt.max_resend < 0 )
1121 goto usage;
1122 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001123 else if( strcmp( p, "request_page" ) == 0 )
1124 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +02001125 else if( strcmp( p, "request_size" ) == 0 )
1126 {
1127 opt.request_size = atoi( q );
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001128 if( opt.request_size < 0 ||
1129 opt.request_size > MAX_REQUEST_SIZE )
Paul Bakker93c32b22014-04-25 13:40:05 +02001130 goto usage;
1131 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001132 else if( strcmp( p, "ca_file" ) == 0 )
1133 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +00001134 else if( strcmp( p, "ca_path" ) == 0 )
1135 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +00001136 else if( strcmp( p, "crt_file" ) == 0 )
1137 opt.crt_file = q;
1138 else if( strcmp( p, "key_file" ) == 0 )
1139 opt.key_file = q;
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001140#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
1141 else if( strcmp( p, "key_opaque" ) == 0 )
1142 opt.key_opaque = atoi( q );
1143#endif
Hanno Beckera0e20d02019-05-15 14:03:01 +01001144#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +01001145 else if( strcmp( p, "cid" ) == 0 )
1146 {
1147 opt.cid_enabled = atoi( q );
1148 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
1149 goto usage;
1150 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001151 else if( strcmp( p, "cid_renego" ) == 0 )
1152 {
1153 opt.cid_enabled_renego = atoi( q );
1154 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
1155 goto usage;
1156 }
Hanno Becker90cb3592019-04-09 17:24:19 +01001157 else if( strcmp( p, "cid_val" ) == 0 )
1158 {
1159 opt.cid_val = q;
1160 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001161 else if( strcmp( p, "cid_val_renego" ) == 0 )
1162 {
1163 opt.cid_val_renego = q;
1164 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001165#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001166 else if( strcmp( p, "psk" ) == 0 )
1167 opt.psk = q;
Hanno Beckere86964c2018-10-23 11:37:50 +01001168#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001169 else if( strcmp( p, "psk_opaque" ) == 0 )
1170 opt.psk_opaque = atoi( q );
Hanno Beckere86964c2018-10-23 11:37:50 +01001171#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001172#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1173 else if( strcmp( p, "ca_callback" ) == 0)
1174 opt.ca_callback = atoi( q );
1175#endif
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001176 else if( strcmp( p, "psk_identity" ) == 0 )
1177 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001178 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1179 opt.ecjpake_pw = q;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001180 else if( strcmp( p, "ec_max_ops" ) == 0 )
1181 opt.ec_max_ops = atoi( q );
Paul Bakker51936882011-02-20 16:05:58 +00001182 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +00001185
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001186 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +00001187 {
1188 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +00001189 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +00001190 }
Paul Bakker51936882011-02-20 16:05:58 +00001191 opt.force_ciphersuite[1] = 0;
1192 }
Paul Bakker48916f92012-09-16 19:57:18 +00001193 else if( strcmp( p, "renegotiation" ) == 0 )
1194 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001195 opt.renegotiation = (atoi( q )) ?
1196 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1197 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +00001198 }
1199 else if( strcmp( p, "allow_legacy" ) == 0 )
1200 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001201 switch( atoi( q ) )
1202 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001203 case -1:
1204 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1205 break;
1206 case 0:
1207 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1208 break;
1209 case 1:
1210 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1211 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001212 default: goto usage;
1213 }
Paul Bakker48916f92012-09-16 19:57:18 +00001214 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001215 else if( strcmp( p, "renegotiate" ) == 0 )
1216 {
1217 opt.renegotiate = atoi( q );
1218 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1219 goto usage;
1220 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001221 else if( strcmp( p, "exchanges" ) == 0 )
1222 {
1223 opt.exchanges = atoi( q );
1224 if( opt.exchanges < 1 )
1225 goto usage;
1226 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001227 else if( strcmp( p, "reconnect" ) == 0 )
1228 {
1229 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001230 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001231 goto usage;
1232 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001233 else if( strcmp( p, "reco_delay" ) == 0 )
1234 {
1235 opt.reco_delay = atoi( q );
1236 if( opt.reco_delay < 0 )
1237 goto usage;
1238 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001239 else if( strcmp( p, "reconnect_hard" ) == 0 )
1240 {
1241 opt.reconnect_hard = atoi( q );
1242 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
1243 goto usage;
1244 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001245 else if( strcmp( p, "tickets" ) == 0 )
1246 {
1247 opt.tickets = atoi( q );
1248 if( opt.tickets < 0 || opt.tickets > 2 )
1249 goto usage;
1250 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001251 else if( strcmp( p, "alpn" ) == 0 )
1252 {
1253 opt.alpn_string = q;
1254 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001255 else if( strcmp( p, "fallback" ) == 0 )
1256 {
1257 switch( atoi( q ) )
1258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
1260 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001261 default: goto usage;
1262 }
1263 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001264 else if( strcmp( p, "extended_ms" ) == 0 )
1265 {
1266 switch( atoi( q ) )
1267 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001268 case 0:
1269 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1270 break;
1271 case 1:
1272 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1273 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001274 default: goto usage;
1275 }
1276 }
Hanno Beckere6706e62017-05-15 16:05:15 +01001277 else if( strcmp( p, "curves" ) == 0 )
1278 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001279 else if( strcmp( p, "etm" ) == 0 )
1280 {
1281 switch( atoi( q ) )
1282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1284 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001285 default: goto usage;
1286 }
1287 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001288 else if( strcmp( p, "min_version" ) == 0 )
1289 {
1290 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001292 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001294 else if( strcmp( q, "tls1_1" ) == 0 ||
1295 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001297 else if( strcmp( q, "tls1_2" ) == 0 ||
1298 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001300 else
1301 goto usage;
1302 }
1303 else if( strcmp( p, "max_version" ) == 0 )
1304 {
1305 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001307 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001309 else if( strcmp( q, "tls1_1" ) == 0 ||
1310 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001312 else if( strcmp( q, "tls1_2" ) == 0 ||
1313 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001315 else
1316 goto usage;
1317 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001318 else if( strcmp( p, "arc4" ) == 0 )
1319 {
1320 switch( atoi( q ) )
1321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1323 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001324 default: goto usage;
1325 }
1326 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02001327 else if( strcmp( p, "allow_sha1" ) == 0 )
1328 {
1329 switch( atoi( q ) )
1330 {
1331 case 0: opt.allow_sha1 = 0; break;
1332 case 1: opt.allow_sha1 = 1; break;
1333 default: goto usage;
1334 }
1335 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001336 else if( strcmp( p, "force_version" ) == 0 )
1337 {
1338 if( strcmp( q, "ssl3" ) == 0 )
1339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1341 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001342 }
1343 else if( strcmp( q, "tls1" ) == 0 )
1344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1346 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001347 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001348 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1351 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001352 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001353 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1356 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001357 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001358 else if( strcmp( q, "dtls1" ) == 0 )
1359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1361 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1362 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001363 }
1364 else if( strcmp( q, "dtls1_2" ) == 0 )
1365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1367 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1368 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001369 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001370 else
1371 goto usage;
1372 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001373 else if( strcmp( p, "auth_mode" ) == 0 )
1374 {
1375 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001377 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001379 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001381 else
1382 goto usage;
1383 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001384 else if( strcmp( p, "max_frag_len" ) == 0 )
1385 {
1386 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001388 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001390 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001392 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001394 else
1395 goto usage;
1396 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001397 else if( strcmp( p, "trunc_hmac" ) == 0 )
1398 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001399 switch( atoi( q ) )
1400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1402 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001403 default: goto usage;
1404 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001405 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001406 else if( strcmp( p, "hs_timeout" ) == 0 )
1407 {
1408 if( ( p = strchr( q, '-' ) ) == NULL )
1409 goto usage;
1410 *p++ = '\0';
1411 opt.hs_to_min = atoi( q );
1412 opt.hs_to_max = atoi( p );
1413 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1414 goto usage;
1415 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001416 else if( strcmp( p, "mtu" ) == 0 )
1417 {
1418 opt.dtls_mtu = atoi( q );
1419 if( opt.dtls_mtu < 0 )
1420 goto usage;
1421 }
Hanno Becker4d615912018-08-14 13:33:30 +01001422 else if( strcmp( p, "dgram_packing" ) == 0 )
1423 {
1424 opt.dgram_packing = atoi( q );
1425 if( opt.dgram_packing != 0 &&
1426 opt.dgram_packing != 1 )
1427 {
1428 goto usage;
1429 }
1430 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001431 else if( strcmp( p, "recsplit" ) == 0 )
1432 {
1433 opt.recsplit = atoi( q );
1434 if( opt.recsplit < 0 || opt.recsplit > 1 )
1435 goto usage;
1436 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001437 else if( strcmp( p, "dhmlen" ) == 0 )
1438 {
1439 opt.dhmlen = atoi( q );
1440 if( opt.dhmlen < 0 )
1441 goto usage;
1442 }
Andres Amaya Garciabc818842018-10-16 21:08:38 +01001443 else if( strcmp( p, "query_config" ) == 0 )
1444 {
1445 return query_config( q );
1446 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001447 else if( strcmp( p, "eap_tls" ) == 0 )
1448 {
1449 opt.eap_tls = atoi( q );
1450 if( opt.eap_tls < 0 || opt.eap_tls > 1 )
1451 goto usage;
1452 }
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001453 else if( strcmp( p, "reproducible" ) == 0 )
1454 {
1455 opt.reproducible = 1;
1456 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001457 else
1458 goto usage;
1459 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001460
Hanno Becker16970d22017-10-10 15:56:37 +01001461 /* Event-driven IO is incompatible with the above custom
1462 * receive and send functions, as the polling builds on
1463 * refers to the underlying net_context. */
1464 if( opt.event == 1 && opt.nbio != 1 )
1465 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001466 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001467 opt.nbio = 1;
1468 }
1469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#if defined(MBEDTLS_DEBUG_C)
1471 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001472#endif
1473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001475 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001476 * Unhexify the pre-shared key if any is given
1477 */
1478 if( strlen( opt.psk ) )
1479 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001480 psk_len = strlen( opt.psk ) / 2;
1481 if( psk_len > sizeof( psk ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001482 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001483 mbedtls_printf( "pre-shared key too long\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001484 goto exit;
1485 }
1486
Hanno Becker1f583ee2019-04-09 17:12:56 +01001487 if( unhexify( opt.psk, psk ) != 0 )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001488 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001489 mbedtls_printf( "pre-shared key not valid hex\n" );
1490 goto exit;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001491 }
1492 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001494
Hanno Beckere86964c2018-10-23 11:37:50 +01001495
1496#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001497 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001498 {
1499 if( opt.psk == NULL )
1500 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00001501 mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
Hanno Beckere86964c2018-10-23 11:37:50 +01001502 ret = 2;
1503 goto usage;
1504 }
1505
1506 if( opt.force_ciphersuite[0] <= 0 )
1507 {
1508 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" );
1509 ret = 2;
1510 goto usage;
1511 }
1512 }
1513#endif /* MBEDTLS_USE_PSA_CRYPTO */
1514
1515 if( opt.force_ciphersuite[0] > 0 )
1516 {
1517 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1518 ciphersuite_info =
1519 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1520
1521 if( opt.max_version != -1 &&
1522 ciphersuite_info->min_minor_ver > opt.max_version )
1523 {
1524 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
1525 ret = 2;
1526 goto usage;
1527 }
1528 if( opt.min_version != -1 &&
1529 ciphersuite_info->max_minor_ver < opt.min_version )
1530 {
1531 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
1532 ret = 2;
1533 goto usage;
1534 }
1535
1536 /* If the server selects a version that's not supported by
1537 * this suite, then there will be no common ciphersuite... */
1538 if( opt.max_version == -1 ||
1539 opt.max_version > ciphersuite_info->max_minor_ver )
1540 {
1541 opt.max_version = ciphersuite_info->max_minor_ver;
1542 }
1543 if( opt.min_version < ciphersuite_info->min_minor_ver )
1544 {
1545 opt.min_version = ciphersuite_info->min_minor_ver;
1546 /* DTLS starts with TLS 1.1 */
1547 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1548 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1549 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1550 }
1551
1552 /* Enable RC4 if needed and not explicitly disabled */
1553 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
1554 {
1555 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
1556 {
1557 mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" );
1558 ret = 2;
1559 goto usage;
1560 }
1561
1562 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
1563 }
1564
Hanno Becker90cb3592019-04-09 17:24:19 +01001565
Hanno Beckere86964c2018-10-23 11:37:50 +01001566#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001567 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001568 {
1569 /* Ensure that the chosen ciphersuite is PSK-only; we must know
1570 * the ciphersuite in advance to set the correct policy for the
1571 * PSK key slot. This limitation might go away in the future. */
1572 if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
1573 opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 )
1574 {
1575 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" );
1576 ret = 2;
1577 goto usage;
1578 }
1579
1580 /* Determine KDF algorithm the opaque PSK will be used in. */
1581#if defined(MBEDTLS_SHA512_C)
1582 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1583 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1584 else
1585#endif /* MBEDTLS_SHA512_C */
1586 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1587 }
1588#endif /* MBEDTLS_USE_PSA_CRYPTO */
1589 }
1590
Hanno Beckera0e20d02019-05-15 14:03:01 +01001591#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001592 cid_len = strlen( opt.cid_val ) / 2;
1593 if( cid_len > sizeof( cid ) )
1594 {
1595 mbedtls_printf( "CID too long\n" );
1596 goto exit;
1597 }
Hanno Becker90cb3592019-04-09 17:24:19 +01001598
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001599 if( unhexify( opt.cid_val, cid ) != 0 )
1600 {
1601 mbedtls_printf( "CID not valid hex\n" );
1602 goto exit;
1603 }
1604
1605 /* Keep CID settings for renegotiation unless
1606 * specified otherwise. */
1607 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
1608 opt.cid_enabled_renego = opt.cid_enabled;
1609 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
1610 opt.cid_val_renego = opt.cid_val;
1611
1612 cid_renego_len = strlen( opt.cid_val_renego ) / 2;
1613 if( cid_renego_len > sizeof( cid_renego ) )
1614 {
1615 mbedtls_printf( "CID too long\n" );
1616 goto exit;
1617 }
1618
1619 if( unhexify( opt.cid_val_renego, cid_renego ) != 0 )
1620 {
1621 mbedtls_printf( "CID not valid hex\n" );
1622 goto exit;
1623 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001624#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01001625
Hanno Beckere6706e62017-05-15 16:05:15 +01001626#if defined(MBEDTLS_ECP_C)
1627 if( opt.curves != NULL )
1628 {
1629 p = (char *) opt.curves;
1630 i = 0;
1631
1632 if( strcmp( p, "none" ) == 0 )
1633 {
1634 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1635 }
1636 else if( strcmp( p, "default" ) != 0 )
1637 {
1638 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001639 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001640 {
1641 q = p;
1642
1643 /* Terminate the current string */
1644 while( *p != ',' && *p != '\0' )
1645 p++;
1646 if( *p == ',' )
1647 *p++ = '\0';
1648
1649 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1650 {
1651 curve_list[i++] = curve_cur->grp_id;
1652 }
1653 else
1654 {
1655 mbedtls_printf( "unknown curve %s\n", q );
1656 mbedtls_printf( "supported curves: " );
1657 for( curve_cur = mbedtls_ecp_curve_list();
1658 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1659 curve_cur++ )
1660 {
1661 mbedtls_printf( "%s ", curve_cur->name );
1662 }
1663 mbedtls_printf( "\n" );
1664 goto exit;
1665 }
1666 }
1667
1668 mbedtls_printf("Number of curves: %d\n", i );
1669
Hanno Becker8651a432017-06-09 16:13:22 +01001670 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001671 {
Hanno Becker8651a432017-06-09 16:13:22 +01001672 mbedtls_printf( "curves list too long, maximum %d",
1673 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001674 goto exit;
1675 }
1676
1677 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1678 }
1679 }
1680#endif /* MBEDTLS_ECP_C */
1681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001683 if( opt.alpn_string != NULL )
1684 {
1685 p = (char *) opt.alpn_string;
1686 i = 0;
1687
1688 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001689 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001690 {
1691 alpn_list[i++] = p;
1692
1693 /* Terminate the current string and move on to next one */
1694 while( *p != ',' && *p != '\0' )
1695 p++;
1696 if( *p == ',' )
1697 *p++ = '\0';
1698 }
1699 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001701
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001702 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001703 * 0. Initialize the RNG and the session data
1704 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001706 fflush( stdout );
1707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 mbedtls_entropy_init( &entropy );
Philippe Antoine986b6f22019-06-07 15:04:32 +02001709 if (opt.reproducible)
1710 {
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001711 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy,
Philippe Antoine986b6f22019-06-07 15:04:32 +02001712 &entropy, (const unsigned char *) pers,
1713 strlen( pers ) ) ) != 0 )
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001714 {
1715 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
Philippe Antoine986b6f22019-06-07 15:04:32 +02001716 -ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001717 goto exit;
1718 }
Philippe Antoine986b6f22019-06-07 15:04:32 +02001719 }
1720 else
1721 {
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001722 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
Philippe Antoine986b6f22019-06-07 15:04:32 +02001723 &entropy, (const unsigned char *) pers,
1724 strlen( pers ) ) ) != 0 )
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001725 {
1726 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
Philippe Antoine986b6f22019-06-07 15:04:32 +02001727 -ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001728 goto exit;
1729 }
Paul Bakker508ad5a2011-12-04 17:09:26 +00001730 }
1731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001735 /*
1736 * 1.1. Load the trusted CA
1737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001739 fflush( stdout );
1740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001742 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001743 if( strcmp( opt.ca_path, "none" ) == 0 )
1744 ret = 0;
1745 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001747 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001748 if( strcmp( opt.ca_file, "none" ) == 0 )
1749 ret = 0;
1750 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001752 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001753#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754#if defined(MBEDTLS_CERTS_C)
1755 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 ret = mbedtls_x509_crt_parse( &cacert,
1758 (const unsigned char *) mbedtls_test_cas[i],
1759 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001760 if( ret != 0 )
1761 break;
1762 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001763#else
1764 {
1765 ret = 1;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001766 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001767 }
1768#endif
Paul Bakker42488232012-05-16 08:21:05 +00001769 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001770 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001771 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1772 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001773 goto exit;
1774 }
1775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001777
1778 /*
1779 * 1.2. Load own certificate and private key
1780 *
1781 * (can be skipped if client authentication is not required)
1782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001784 fflush( stdout );
1785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001787 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001788 if( strcmp( opt.crt_file, "none" ) == 0 )
1789 ret = 0;
1790 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001792 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001793#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001795 ret = mbedtls_x509_crt_parse( &clicert,
1796 (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001798#else
1799 {
1800 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001802 }
1803#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001804 if( ret != 0 )
1805 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001806 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1807 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001808 goto exit;
1809 }
1810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001812 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001813 if( strcmp( opt.key_file, "none" ) == 0 )
1814 ret = 0;
1815 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001817 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001818#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001820 ret = mbedtls_pk_parse_key( &pkey,
1821 (const unsigned char *) mbedtls_test_cli_key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001823#else
1824 {
1825 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001827 }
1828#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001829 if( ret != 0 )
1830 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001831 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
1832 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001833 goto exit;
1834 }
1835
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001836#if defined(MBEDTLS_USE_PSA_CRYPTO)
1837 if( opt.key_opaque != 0 )
1838 {
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001839 if( ( ret = mbedtls_pk_wrap_as_opaque( &pkey, &key_slot,
1840 PSA_ALG_SHA_256 ) ) != 0 )
1841 {
1842 mbedtls_printf( " failed\n ! "
1843 "mbedtls_pk_wrap_as_opaque returned -0x%x\n\n", -ret );
1844 goto exit;
1845 }
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001846 }
1847#endif /* MBEDTLS_USE_PSA_CRYPTO */
1848
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001849 mbedtls_printf( " ok (key type: %s)\n", mbedtls_pk_get_name( &pkey ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001851
1852 /*
1853 * 2. Start the connection
1854 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001855 if( opt.server_addr == NULL)
1856 opt.server_addr = opt.server_name;
1857
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001858 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001860 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001861 fflush( stdout );
1862
Hanno Becker16970d22017-10-10 15:56:37 +01001863 if( ( ret = mbedtls_net_connect( &server_fd,
1864 opt.server_addr, opt.server_port,
1865 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1866 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001867 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001868 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
1869 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001870 goto exit;
1871 }
1872
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001873 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001874 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001875 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001876 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001877 if( ret != 0 )
1878 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001879 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
1880 -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001881 goto exit;
1882 }
1883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001885
1886 /*
1887 * 3. Setup stuff
1888 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001890 fflush( stdout );
1891
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001892 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1893 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001894 opt.transport,
1895 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001896 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001897 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
1898 -ret );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001899 goto exit;
1900 }
1901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001903 /* The default algorithms profile disables SHA-1, but our tests still
1904 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001905 if( opt.allow_sha1 > 0 )
1906 {
1907 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1908 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1909 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1910 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001911
Hanno Beckerbb425db2019-04-03 12:59:58 +01001912 if( opt.context_crt_cb == 0 )
1913 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
1914
Hanno Beckera1051b42019-02-26 11:38:29 +00001915 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Gilles Peskineef86ab22017-05-05 18:59:02 +02001916#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001917
Hanno Beckera0e20d02019-05-15 14:03:01 +01001918#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001919 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckerad4a1372019-05-03 13:06:44 +01001920 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001921 if( opt.cid_enabled == 1 &&
1922 opt.cid_enabled_renego == 1 &&
1923 cid_len != cid_renego_len )
1924 {
1925 mbedtls_printf( "CID length must not change during renegotiation\n" );
1926 goto usage;
1927 }
1928
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001929 if( opt.cid_enabled == 1 )
Hanno Becker8367ccc2019-05-14 11:30:10 +01001930 ret = mbedtls_ssl_conf_cid( &conf, cid_len,
1931 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001932 else
Hanno Becker8367ccc2019-05-14 11:30:10 +01001933 ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
1934 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001935
Hanno Beckerad4a1372019-05-03 13:06:44 +01001936 if( ret != 0 )
1937 {
Hanno Beckerd5eed422019-05-23 16:58:22 +01001938 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
1939 -ret );
Hanno Beckerad4a1372019-05-03 13:06:44 +01001940 goto exit;
1941 }
1942 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001943#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerad4a1372019-05-03 13:06:44 +01001944
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001945 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001946 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001949 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Hanno Becker16970d22017-10-10 15:56:37 +01001950 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min,
1951 opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001952
Hanno Becker4d615912018-08-14 13:33:30 +01001953 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01001954 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001958 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001959 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001960 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n",
1961 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001962 goto exit;
1963 }
Paul Bakker05decb22013-08-15 13:33:48 +02001964#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001967 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001968 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001969#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001972 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001973 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001974#endif
1975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001977 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001978 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001979#endif
1980
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001981#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1982 if( opt.eap_tls != 0 )
1983 mbedtls_ssl_conf_export_keys_ext_cb( &conf, eap_tls_key_derivation,
1984 &eap_tls_keying );
1985#endif
1986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001988 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001989 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Hanno Becker16970d22017-10-10 15:56:37 +01001990 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1991 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001992#endif
1993
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001994#if defined(MBEDTLS_DHM_C)
1995 if( opt.dhmlen != DFL_DHMLEN )
1996 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1997#endif
1998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002000 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002001 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002002 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002003 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
2004 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002005 goto exit;
2006 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002007#endif
2008
Philippe Antoine986b6f22019-06-07 15:04:32 +02002009 if (opt.reproducible)
2010 {
2011 srand( 1 );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002012 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
2013#if defined(MBEDTLS_PLATFORM_TIME_ALT)
2014 mbedtls_platform_set_time( dummy_constant_time );
2015#else
Philippe Antoine986b6f22019-06-07 15:04:32 +02002016 fprintf( stderr, "Warning: reproduce without constant time\n" );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002017#endif
Philippe Antoine986b6f22019-06-07 15:04:32 +02002018 }
2019 else
2020 {
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002021 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
2022 }
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002023 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002024
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002025 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00002026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002028 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02002029#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002030
Paul Bakker645ce3a2012-10-31 12:32:41 +00002031 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002032 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002033
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002034#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002035 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002036 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002037#endif
Paul Bakker51936882011-02-20 16:05:58 +00002038
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002039 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002040 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002042 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002043#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002046 if( strcmp( opt.ca_path, "none" ) != 0 &&
2047 strcmp( opt.ca_file, "none" ) != 0 )
2048 {
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02002049#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2050 if( opt.ca_callback != 0 )
Hanno Beckercbb59032019-03-28 14:14:22 +00002051 mbedtls_ssl_conf_ca_cb( &conf, ca_callback, &cacert );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02002052 else
2053#endif
2054 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002055 }
2056 if( strcmp( opt.crt_file, "none" ) != 0 &&
2057 strcmp( opt.key_file, "none" ) != 0 )
2058 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002059 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002060 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002061 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
2062 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002063 goto exit;
2064 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002065 }
Paul Bakkered27a042013-04-18 22:46:23 +02002066#endif
2067
Hanno Beckere6706e62017-05-15 16:05:15 +01002068#if defined(MBEDTLS_ECP_C)
2069 if( opt.curves != NULL &&
2070 strcmp( opt.curves, "default" ) != 0 )
2071 {
2072 mbedtls_ssl_conf_curves( &conf, curve_list );
2073 }
2074#endif
2075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckere86964c2018-10-23 11:37:50 +01002077#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002078 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01002079 {
2080 /* The algorithm has already been determined earlier. */
Hanno Becker37519ea2019-01-25 14:26:01 +00002081 status = psa_allocate_key( &slot );
Hanno Becker1d911cd2018-11-15 13:06:09 +00002082 if( status != PSA_SUCCESS )
2083 {
2084 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2085 goto exit;
2086 }
Hanno Beckere86964c2018-10-23 11:37:50 +01002087
Hanno Becker13871242019-01-25 14:26:26 +00002088 policy = psa_key_policy_init();
Hanno Beckere86964c2018-10-23 11:37:50 +01002089 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2090
2091 status = psa_set_key_policy( slot, &policy );
2092 if( status != PSA_SUCCESS )
2093 {
2094 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2095 goto exit;
2096 }
2097
2098 status = psa_import_key( slot, PSA_KEY_TYPE_DERIVE, psk, psk_len );
2099 if( status != PSA_SUCCESS )
2100 {
2101 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2102 goto exit;
2103 }
2104
2105 if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, slot,
2106 (const unsigned char *) opt.psk_identity,
Hanno Becker5cd607b2018-11-05 12:52:42 +00002107 strlen( opt.psk_identity ) ) ) != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01002108 {
2109 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
2110 ret );
2111 goto exit;
2112 }
2113 }
2114 else
2115#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002116 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002117 (const unsigned char *) opt.psk_identity,
2118 strlen( opt.psk_identity ) ) ) != 0 )
2119 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002120 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n",
2121 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002122 goto exit;
2123 }
Hanno Beckere86964c2018-10-23 11:37:50 +01002124#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002125
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002126 if( opt.min_version != DFL_MIN_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01002127 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
2128 opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01002129
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002130 if( opt.max_version != DFL_MAX_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01002131 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
2132 opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00002133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02002135 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002136 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02002137#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002138
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002139 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
2140 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002141 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
2142 -ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002143 goto exit;
2144 }
2145
2146#if defined(MBEDTLS_X509_CRT_PARSE_C)
2147 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
2148 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002149 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
2150 ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002151 goto exit;
2152 }
2153#endif
2154
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002155#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2156 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
2157 {
2158 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
2159 (const unsigned char *) opt.ecjpake_pw,
2160 strlen( opt.ecjpake_pw ) ) ) != 0 )
2161 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002162 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
2163 ret );
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002164 goto exit;
2165 }
2166 }
2167#endif
2168
Hanno Beckerbb425db2019-04-03 12:59:58 +01002169#if defined(MBEDTLS_X509_CRT_PARSE_C)
2170 if( opt.context_crt_cb == 1 )
2171 mbedtls_ssl_set_verify( &ssl, my_verify, NULL );
2172#endif /* MBEDTLS_X509_CRT_PARSE_C */
2173
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002174 if( opt.nbio == 2 )
2175 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
2176 else
Hanno Becker16970d22017-10-10 15:56:37 +01002177 mbedtls_ssl_set_bio( &ssl, &server_fd,
2178 mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02002179 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002180
Hanno Beckera0e20d02019-05-15 14:03:01 +01002181#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +01002182 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2183 {
2184 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
2185 cid, cid_len ) ) != 0 )
2186 {
2187 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2188 ret );
2189 goto exit;
2190 }
2191 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002192#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01002193
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002194#if defined(MBEDTLS_SSL_PROTO_DTLS)
2195 if( opt.dtls_mtu != DFL_DTLS_MTU )
2196 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
2197#endif
2198
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002199#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002200 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
2201 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002202#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002203
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002204#if defined(MBEDTLS_ECP_RESTARTABLE)
2205 if( opt.ec_max_ops != DFL_EC_MAX_OPS )
2206 mbedtls_ecp_set_max_ops( opt.ec_max_ops );
2207#endif
2208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002210
Paul Bakker5121ce52009-01-03 21:22:43 +00002211 /*
2212 * 4. Handshake
2213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 fflush( stdout );
2216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 {
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002219 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2220 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002221 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Paul Bakker40e46942009-01-03 21:51:57 +00002222 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002223 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
2224 -ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2226 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01002227 " Unable to verify the server's certificate. "
2228 "Either it is invalid,\n"
2229 " or you didn't set ca_file or ca_path "
2230 "to an appropriate value.\n"
2231 " Alternatively, you may want to use "
2232 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002234 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00002235 }
Hanno Becker16970d22017-10-10 15:56:37 +01002236
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002237#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002238 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2239 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002240#endif
2241
Hanno Becker16970d22017-10-10 15:56:37 +01002242 /* For event-driven IO, wait for socket to become available */
2243 if( opt.event == 1 /* level triggered IO */ )
2244 {
2245#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002246 ret = idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002247#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002248 ret = idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002249#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002250 if( ret != 0 )
2251 goto exit;
Hanno Becker16970d22017-10-10 15:56:37 +01002252 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 }
2254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Hanno Becker16970d22017-10-10 15:56:37 +01002256 mbedtls_ssl_get_version( &ssl ),
2257 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
2260 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002261 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002263
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002264#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2265 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
2266 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
2267#endif
2268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002270 if( opt.alpn_string != NULL )
2271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
2273 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002274 alp ? alp : "(none)" );
2275 }
2276#endif
2277
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002278#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Ron Eldor51d3ab52019-05-12 14:54:30 +03002279 if( opt.eap_tls != 0 )
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002280 {
2281 size_t j = 0;
Ron Eldor51d3ab52019-05-12 14:54:30 +03002282
2283 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type,
2284 eap_tls_keying.master_secret,
2285 sizeof( eap_tls_keying.master_secret ),
2286 eap_tls_label,
2287 eap_tls_keying.randbytes,
2288 sizeof( eap_tls_keying.randbytes ),
2289 eap_tls_keymaterial,
2290 sizeof( eap_tls_keymaterial ) ) )
2291 != 0 )
2292 {
2293 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2294 -ret );
2295 goto exit;
2296 }
2297
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002298 mbedtls_printf( " EAP-TLS key material is:" );
2299 for( j = 0; j < sizeof( eap_tls_keymaterial ); j++ )
2300 {
2301 if( j % 8 == 0 )
2302 mbedtls_printf("\n ");
2303 mbedtls_printf("%02x ", eap_tls_keymaterial[j] );
2304 }
2305 mbedtls_printf("\n");
2306
Ron Eldor51d3ab52019-05-12 14:54:30 +03002307 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type, NULL, 0,
Ron Eldor51c45072019-05-15 17:49:54 +03002308 eap_tls_label,
2309 eap_tls_keying.randbytes,
2310 sizeof( eap_tls_keying.randbytes ),
2311 eap_tls_iv,
2312 sizeof( eap_tls_iv ) ) ) != 0 )
Ron Eldor51d3ab52019-05-12 14:54:30 +03002313 {
2314 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2315 -ret );
2316 goto exit;
2317 }
2318
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002319 mbedtls_printf( " EAP-TLS IV is:" );
2320 for( j = 0; j < sizeof( eap_tls_iv ); j++ )
2321 {
2322 if( j % 8 == 0 )
2323 mbedtls_printf("\n ");
2324 mbedtls_printf("%02x ", eap_tls_iv[j] );
2325 }
2326 mbedtls_printf("\n");
2327 }
2328#endif
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002329 if( opt.reconnect != 0 )
2330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002332 fflush( stdout );
2333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002335 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002336 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
2337 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002338 goto exit;
2339 }
2340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002342 }
2343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002345 /*
2346 * 5. Verify the server certificate
2347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002349
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002350 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002351 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002352 char vrfy_buf[512];
2353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002355
Hanno Becker16970d22017-10-10 15:56:37 +01002356 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ),
2357 " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00002358
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002359 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002360 }
2361 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002363
Hanno Beckera9766c2c2019-02-25 17:43:18 +00002364 mbedtls_printf( " . Peer certificate information ...\n" );
2365 mbedtls_printf( "%s\n", peer_crt_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002367
Hanno Beckera0e20d02019-05-15 14:03:01 +01002368#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002369 ret = report_cid_usage( &ssl, "initial handshake" );
2370 if( ret != 0 )
2371 goto exit;
2372
Hanno Becker90cb3592019-04-09 17:24:19 +01002373 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2374 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002375 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
2376 cid_renego,
2377 cid_renego_len ) ) != 0 )
Hanno Becker90cb3592019-04-09 17:24:19 +01002378 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002379 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2380 ret );
2381 return( ret );
Hanno Becker90cb3592019-04-09 17:24:19 +01002382 }
2383 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002384#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01002385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002387 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002388 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002389 /*
2390 * Perform renegotiation (this must be done when the server is waiting
2391 * for input from our side).
2392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002394 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002396 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002397 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002398 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002399 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002400 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002401 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
2402 ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002403 goto exit;
2404 }
Hanno Becker16970d22017-10-10 15:56:37 +01002405
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002406#if defined(MBEDTLS_ECP_RESTARTABLE)
2407 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2408 continue;
2409#endif
2410
Hanno Becker16970d22017-10-10 15:56:37 +01002411 /* For event-driven IO, wait for socket to become available */
2412 if( opt.event == 1 /* level triggered IO */ )
2413 {
2414#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002415 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002416#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002417 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002418#endif
2419 }
2420
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002421 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002423 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002425
Hanno Beckera0e20d02019-05-15 14:03:01 +01002426#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002427 ret = report_cid_usage( &ssl, "after renegotiation" );
2428 if( ret != 0 )
2429 goto exit;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002430#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002431
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 /*
2433 * 6. Write the GET request
2434 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002435 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002436send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002438 fflush( stdout );
2439
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002440 len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST,
2441 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002442 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002443
2444 /* Add padding to GET request to reach opt.request_size in length */
2445 if( opt.request_size != DFL_REQUEST_SIZE &&
2446 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02002447 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002448 memset( buf + len, 'A', opt.request_size - len - tail_len );
2449 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02002450 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002451
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002452 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002453 len += tail_len;
2454
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02002455 /* Truncate if request size is smaller than the "natural" size */
2456 if( opt.request_size != DFL_REQUEST_SIZE &&
2457 len > opt.request_size )
2458 {
2459 len = opt.request_size;
2460
2461 /* Still end with \r\n unless that's really not possible */
2462 if( len >= 2 ) buf[len - 2] = '\r';
2463 if( len >= 1 ) buf[len - 1] = '\n';
2464 }
2465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 {
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002468 written = 0;
2469 frags = 0;
2470
2471 do
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 {
Hanno Becker16970d22017-10-10 15:56:37 +01002473 while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002474 len - written ) ) < 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002475 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002476 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002477 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002478 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002479 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002480 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
2481 -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002482 goto exit;
2483 }
Hanno Becker16970d22017-10-10 15:56:37 +01002484
2485 /* For event-driven IO, wait for socket to become available */
2486 if( opt.event == 1 /* level triggered IO */ )
2487 {
2488#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002489 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002490#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002491 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002492#endif
2493 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002494 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002495
2496 frags++;
2497 written += ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002499 while( written < len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002501 else /* Not stream, so datagram */
2502 {
Hanno Becker16970d22017-10-10 15:56:37 +01002503 while( 1 )
2504 {
2505 ret = mbedtls_ssl_write( &ssl, buf, len );
2506
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002507#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002508 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002509 continue;
2510#endif
2511
Hanno Becker16970d22017-10-10 15:56:37 +01002512 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2513 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2514 break;
2515
2516 /* For event-driven IO, wait for socket to become available */
2517 if( opt.event == 1 /* level triggered IO */ )
2518 {
2519#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002520 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002521#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002522 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002523#endif
2524 }
2525 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002526
2527 if( ret < 0 )
2528 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002529 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n",
2530 ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002531 goto exit;
2532 }
2533
2534 frags = 1;
2535 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002536
2537 if( written < len )
2538 {
2539 mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
2540 "was truncated to size %u", (unsigned) written );
2541 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002543
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002544 buf[written] = '\0';
Hanno Becker16970d22017-10-10 15:56:37 +01002545 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
2546 written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002548 /* Send a non-empty request if request_size == 0 */
2549 if ( len == 0 )
2550 {
2551 opt.request_size = DFL_REQUEST_SIZE;
2552 goto send_request;
2553 }
2554
Paul Bakker5121ce52009-01-03 21:22:43 +00002555 /*
2556 * 7. Read the HTTP response
2557 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 fflush( stdout );
2560
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002561 /*
2562 * TLS and DTLS need different reading styles (stream vs datagram)
2563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002565 {
2566 do
2567 {
2568 len = sizeof( buf ) - 1;
2569 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002571
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002572#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002573 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002574 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002575#endif
2576
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002577 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
2578 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Hanno Becker16970d22017-10-10 15:56:37 +01002579 {
2580 /* For event-driven IO, wait for socket to become available */
2581 if( opt.event == 1 /* level triggered IO */ )
2582 {
2583#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002584 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002585#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002586 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002587#endif
2588 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002589 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01002590 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002591
2592 if( ret <= 0 )
2593 {
2594 switch( ret )
2595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2597 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002598 ret = 0;
2599 goto close_notify;
2600
2601 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602 case MBEDTLS_ERR_NET_CONN_RESET:
2603 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002604 ret = 0;
2605 goto reconnect;
2606
2607 default:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002608 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n",
2609 -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002610 goto exit;
2611 }
2612 }
2613
2614 len = ret;
2615 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002617
2618 /* End of message should be detected according to the syntax of the
2619 * application protocol (eg HTTP), just use a dummy test here. */
2620 if( ret > 0 && buf[len-1] == '\n' )
2621 {
2622 ret = 0;
2623 break;
2624 }
2625 }
2626 while( 1 );
2627 }
2628 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00002629 {
2630 len = sizeof( buf ) - 1;
2631 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002632
Hanno Becker16970d22017-10-10 15:56:37 +01002633 while( 1 )
2634 {
2635 ret = mbedtls_ssl_read( &ssl, buf, len );
2636
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002637#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002638 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002639 continue;
2640#endif
2641
Hanno Becker16970d22017-10-10 15:56:37 +01002642 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2643 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2644 break;
2645
2646 /* For event-driven IO, wait for socket to become available */
2647 if( opt.event == 1 /* level triggered IO */ )
2648 {
2649#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002650 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002651#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002652 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002653#endif
2654 }
2655 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002656
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002657 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002658 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002659 switch( ret )
2660 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002661 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002663 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02002664 goto send_request;
2665 goto exit;
2666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2668 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002669 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002670 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00002671
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002672 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002674 goto exit;
2675 }
Paul Bakker5690efc2011-05-26 13:16:06 +00002676 }
2677
Paul Bakker5121ce52009-01-03 21:22:43 +00002678 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02002679 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002681 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002682 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002684 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002685 * 7b. Simulate hard reset and reconnect from same port?
2686 */
2687 if( opt.reconnect_hard != 0 )
2688 {
2689 opt.reconnect_hard = 0;
2690
2691 mbedtls_printf( " . Restarting connection from same port..." );
2692 fflush( stdout );
2693
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002694#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker23699ef2019-02-26 12:36:53 +00002695 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002696#endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Becker23699ef2019-02-26 12:36:53 +00002697
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002698 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
2699 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002700 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2701 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002702 goto exit;
2703 }
2704
2705 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
2706 {
2707 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002708 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002709 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002710 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002711 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2712 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002713 goto exit;
2714 }
Hanno Becker16970d22017-10-10 15:56:37 +01002715
2716 /* For event-driven IO, wait for socket to become available */
2717 if( opt.event == 1 /* level triggered IO */ )
2718 {
2719#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002720 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002721#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002722 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002723#endif
2724 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002725 }
2726
2727 mbedtls_printf( " ok\n" );
2728
2729 goto send_request;
2730 }
2731
2732 /*
2733 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002734 */
2735 if( --opt.exchanges > 0 )
2736 goto send_request;
2737
2738 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002739 * 8. Done, cleanly close the connection
2740 */
2741close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002743 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002744
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002745 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002747 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002748 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002751
2752 /*
2753 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002754 */
2755reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002756 if( opt.reconnect != 0 )
2757 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02002758 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002759
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002760 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002761
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002762#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002763 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02002764 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002765#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002768
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002769#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckera1051b42019-02-26 11:38:29 +00002770 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002771#endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Beckera1051b42019-02-26 11:38:29 +00002772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002774 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002775 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2776 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002777 goto exit;
2778 }
2779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002781 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002782 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n",
2783 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002784 goto exit;
2785 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002786
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002787 if( ( ret = mbedtls_net_connect( &server_fd,
2788 opt.server_addr, opt.server_port,
2789 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2790 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002791 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002792 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
2793 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002794 goto exit;
2795 }
2796
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002797 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002798 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002799 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002800 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002801 if( ret != 0 )
2802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002804 -ret );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002805 goto exit;
2806 }
2807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002809 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002810 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002811 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002812 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002813 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002814 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2815 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002816 goto exit;
2817 }
2818 }
2819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002821
2822 goto send_request;
2823 }
2824
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002825 /*
2826 * Cleanup and exit
2827 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002828exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00002830 if( ret != 0 )
2831 {
2832 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 mbedtls_strerror( ret, error_buf, 100 );
2834 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002835 }
2836#endif
2837
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002838 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840#if defined(MBEDTLS_X509_CRT_PARSE_C)
2841 mbedtls_x509_crt_free( &clicert );
2842 mbedtls_x509_crt_free( &cacert );
2843 mbedtls_pk_free( &pkey );
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01002844#if defined(MBEDTLS_USE_PSA_CRYPTO)
2845 psa_destroy_key( key_slot );
2846#endif
Paul Bakkered27a042013-04-18 22:46:23 +02002847#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848 mbedtls_ssl_session_free( &saved_session );
2849 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002850 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 mbedtls_ctr_drbg_free( &ctr_drbg );
2852 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002853
Hanno Becker3f24ea92018-11-05 13:25:17 +00002854#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
2855 defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002856 if( opt.psk_opaque != 0 )
Hanno Becker3f24ea92018-11-05 13:25:17 +00002857 {
2858 /* This is ok even if the slot hasn't been
2859 * initialized (we might have jumed here
2860 * immediately because of bad cmd line params,
2861 * for example). */
Hanno Becker1d911cd2018-11-15 13:06:09 +00002862 status = psa_destroy_key( slot );
Hanno Becker3f24ea92018-11-05 13:25:17 +00002863 if( status != PSA_SUCCESS )
2864 {
2865 mbedtls_printf( "Failed to destroy key slot %u - error was %d",
Hanno Becker1d911cd2018-11-15 13:06:09 +00002866 (unsigned) slot, (int) status );
Hanno Becker3f24ea92018-11-05 13:25:17 +00002867 if( ret == 0 )
2868 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2869 }
2870 }
2871#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED &&
2872 MBEDTLS_USE_PSA_CRYPTO */
2873
Paul Bakkercce9d772011-11-18 14:26:47 +00002874#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002876 fflush( stdout ); getchar();
2877#endif
2878
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002879 // Shell can not handle large exit numbers -> 1 for errors
2880 if( ret < 0 )
2881 ret = 1;
2882
Paul Bakker5121ce52009-01-03 21:22:43 +00002883 return( ret );
2884}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
2886 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002887 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */