blob: ef6484810dff42ebf0f586431d3352393ff6c076 [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;
Philippe Antoine12e85de2019-06-11 16:07:53 +0200569 int ret;
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200570
Philippe Antoine3ca50852019-06-07 22:31:59 +0200571 ret = mbedtls_entropy_func( data, output, len );
Philippe Antoine986b6f22019-06-07 15:04:32 +0200572 for ( i = 0; i < len; i++ )
573 {
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200574 //replace result with pseudo random
575 output[i] = (unsigned char) rand();
576 }
Philippe Antoine3ca50852019-06-07 22:31:59 +0200577 return( ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200578}
579
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200580#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000581int ca_callback( void *data, mbedtls_x509_crt const *child,
Janos Follathd7ecbd62019-04-05 14:52:17 +0100582 mbedtls_x509_crt **candidates )
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200583{
Hanno Beckercbb59032019-03-28 14:14:22 +0000584 int ret = 0;
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200585 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +0000586 mbedtls_x509_crt *first;
587
588 /* This is a test-only implementation of the CA callback
589 * which always returns the entire list of trusted certificates.
590 * Production implementations managing a large number of CAs
591 * should use an efficient presentation and lookup for the
592 * set of trusted certificates (such as a hashtable) and only
593 * return those trusted certificates which satisfy basic
594 * parental checks, such as the matching of child `Issuer`
Jarno Lamsaf7a7f9e2019-04-01 15:11:54 +0300595 * and parent `Subject` field or matching key identifiers. */
Hanno Beckercbb59032019-03-28 14:14:22 +0000596 ((void) child);
597
598 first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
599 if( first == NULL )
600 {
601 ret = -1;
602 goto exit;
603 }
604 mbedtls_x509_crt_init( first );
605
606 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
607 {
608 ret = -1;
609 goto exit;
610 }
611
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200612 while( ca->next != NULL )
613 {
614 ca = ca->next;
Hanno Beckercbb59032019-03-28 14:14:22 +0000615 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
616 {
617 ret = -1;
618 goto exit;
619 }
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200620 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000621
622exit:
623
624 if( ret != 0 )
625 {
626 mbedtls_x509_crt_free( first );
627 mbedtls_free( first );
628 first = NULL;
629 }
630
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200631 *candidates = first;
Hanno Beckercbb59032019-03-28 14:14:22 +0000632 return( ret );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200633}
634#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
635
Rich Evans85b05ec2015-02-12 11:37:29 +0000636/*
637 * Test recv/send functions that make sure each try returns
638 * WANT_READ/WANT_WRITE at least once before sucesseding
639 */
640static int my_recv( void *ctx, unsigned char *buf, size_t len )
641{
642 static int first_try = 1;
643 int ret;
644
645 if( first_try )
646 {
647 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100648 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000649 }
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100652 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000653 first_try = 1; /* Next call will be a new operation */
654 return( ret );
655}
656
657static int my_send( void *ctx, const unsigned char *buf, size_t len )
658{
659 static int first_try = 1;
660 int ret;
661
662 if( first_try )
663 {
664 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100665 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000666 }
667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100669 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000670 first_try = 1; /* Next call will be a new operation */
671 return( ret );
672}
673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckera1051b42019-02-26 11:38:29 +0000675static unsigned char peer_crt_info[1024];
Hanno Beckera9766c2c2019-02-25 17:43:18 +0000676
Rich Evans85b05ec2015-02-12 11:37:29 +0000677/*
678 * Enabled if debug_level > 1 in code below
679 */
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100680static int my_verify( void *data, mbedtls_x509_crt *crt,
681 int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000682{
683 char buf[1024];
684 ((void) data);
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Hanno Beckera9766c2c2019-02-25 17:43:18 +0000687 if( depth == 0 )
688 memcpy( peer_crt_info, buf, sizeof( buf ) );
689
690 if( opt.debug_level == 0 )
691 return( 0 );
692
693 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000695
Rich Evans85b05ec2015-02-12 11:37:29 +0000696 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100698 else
699 {
700 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
701 mbedtls_printf( "%s\n", buf );
702 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000703
704 return( 0 );
705}
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200706
707static int ssl_sig_hashes_for_test[] = {
708#if defined(MBEDTLS_SHA512_C)
709 MBEDTLS_MD_SHA512,
710 MBEDTLS_MD_SHA384,
711#endif
712#if defined(MBEDTLS_SHA256_C)
713 MBEDTLS_MD_SHA256,
714 MBEDTLS_MD_SHA224,
715#endif
716#if defined(MBEDTLS_SHA1_C)
717 /* Allow SHA-1 as we use it extensively in tests. */
718 MBEDTLS_MD_SHA1,
719#endif
720 MBEDTLS_MD_NONE
721};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000723
Hanno Becker16970d22017-10-10 15:56:37 +0100724/*
725 * Wait for an event from the underlying transport or the timer
726 * (Used in event-driven IO mode).
727 */
728#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000729int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000730 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100731#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000732int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000733 mbedtls_timing_delay_context *timer,
734 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100735#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000736{
Hanno Becker16970d22017-10-10 15:56:37 +0100737
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000738 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +0100739 int poll_type = 0;
740
741 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
742 poll_type = MBEDTLS_NET_POLL_WRITE;
743 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
744 poll_type = MBEDTLS_NET_POLL_READ;
745#if !defined(MBEDTLS_TIMING_C)
746 else
Hanno Beckeref527962018-03-15 15:49:24 +0000747 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100748#endif
749
750 while( 1 )
751 {
Hanno Becker197a91c2017-10-31 10:58:53 +0000752 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +0100753#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +0000754 if( timer != NULL &&
755 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +0100756 {
Hanno Becker16970d22017-10-10 15:56:37 +0100757 break;
758 }
Hanno Becker197a91c2017-10-31 10:58:53 +0000759#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +0100760
Hanno Becker197a91c2017-10-31 10:58:53 +0000761 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000762 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +0100763 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000764 ret = mbedtls_net_poll( fd, poll_type, 0 );
765 if( ret < 0 )
766 return( ret );
767 if( ret == poll_type )
768 break;
Hanno Becker16970d22017-10-10 15:56:37 +0100769 }
770 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000771
772 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100773}
774
Hanno Becker1f583ee2019-04-09 17:12:56 +0100775/* Unhexify `hex` into `dst`. `dst` must have
776 * size at least `strlen( hex ) / 2`. */
Hanno Becker90cb3592019-04-09 17:24:19 +0100777int unhexify( char const *hex, unsigned char *dst )
Hanno Becker1f583ee2019-04-09 17:12:56 +0100778{
779 unsigned char c;
780 size_t j;
781 size_t len = strlen( hex );
782
783 if( len % 2 != 0 )
784 return( -1 );
785
786 for( j = 0; j < len; j += 2 )
787 {
788 c = hex[j];
789 if( c >= '0' && c <= '9' )
790 c -= '0';
791 else if( c >= 'a' && c <= 'f' )
792 c -= 'a' - 10;
793 else if( c >= 'A' && c <= 'F' )
794 c -= 'A' - 10;
795 else
796 return( -1 );
797 dst[ j / 2 ] = c << 4;
798
799 c = hex[j + 1];
800 if( c >= '0' && c <= '9' )
801 c -= '0';
802 else if( c >= 'a' && c <= 'f' )
803 c -= 'a' - 10;
804 else if( c >= 'A' && c <= 'F' )
805 c -= 'A' - 10;
806 else
807 return( -1 );
808 dst[ j / 2 ] |= c;
809 }
810
811 return( 0 );
812}
813
Hanno Beckera0e20d02019-05-15 14:03:01 +0100814#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100815int report_cid_usage( mbedtls_ssl_context *ssl,
816 const char *additional_description )
817{
818 int ret;
819 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
820 size_t peer_cid_len;
821 int cid_negotiated;
822
823 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
824 return( 0 );
825
Hanno Becker6ae14c02019-05-22 16:59:25 +0100826 /* Check if the use of a CID has been negotiated,
827 * but don't ask for the CID value and length.
828 *
829 * Note: Here and below, we're demonstrating the various ways
830 * in which mbedtls_ssl_get_peer_cid() can be called,
831 * depending on whether or not the length/value of the
832 * peer's CID is needed.
833 *
834 * An actual application, however, should use
835 * just one call to mbedtls_ssl_get_peer_cid(). */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100836 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
Hanno Becker6ae14c02019-05-22 16:59:25 +0100837 NULL, NULL );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100838 if( ret != 0 )
839 {
840 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
841 -ret );
842 return( ret );
843 }
844
845 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
846 {
847 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
848 {
849 mbedtls_printf( "(%s) Use of Connection ID was rejected by the server.\n",
850 additional_description );
851 }
852 }
853 else
854 {
855 size_t idx=0;
856 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
857 additional_description );
Hanno Becker6ae14c02019-05-22 16:59:25 +0100858
859 /* Ask for just the length of the peer's CID. */
860 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
861 NULL, &peer_cid_len );
862 if( ret != 0 )
863 {
864 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
865 -ret );
866 return( ret );
867 }
868
869 /* Ask for just length + value of the peer's CID. */
870 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
871 peer_cid, &peer_cid_len );
872 if( ret != 0 )
873 {
874 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
875 -ret );
876 return( ret );
877 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100878 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
879 additional_description,
880 (unsigned) peer_cid_len );
881 while( idx < peer_cid_len )
882 {
883 mbedtls_printf( "%02x ", peer_cid[ idx ] );
884 idx++;
885 }
886 mbedtls_printf( "\n" );
887 }
888
889 return( 0 );
890}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100891#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100892
Paul Bakker9caf2d22010-02-18 19:37:19 +0000893int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000894{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200895 int ret = 0, len, tail_len, i, written, frags, retry_left;
896 mbedtls_net_context server_fd;
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100897
898 unsigned char buf[MAX_REQUEST_SIZE + 1];
899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
901 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200902 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200903#endif
Hanno Becker90cb3592019-04-09 17:24:19 +0100904
Hanno Beckera0e20d02019-05-15 14:03:01 +0100905#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +0100906 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100907 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker90cb3592019-04-09 17:24:19 +0100908 size_t cid_len = 0;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100909 size_t cid_renego_len = 0;
Hanno Becker90cb3592019-04-09 17:24:19 +0100910#endif
911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100913 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200914#endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100915#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +0100916 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100917 const mbedtls_ecp_curve_info *curve_cur;
918#endif
919
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200920 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000921
Hanno Beckere86964c2018-10-23 11:37:50 +0100922#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500923 psa_key_handle_t slot = 0;
Hanno Beckere86964c2018-10-23 11:37:50 +0100924 psa_algorithm_t alg = 0;
925 psa_key_policy_t policy;
926 psa_status_t status;
927#endif
928
Gilles Peskineef86ab22017-05-05 18:59:02 +0200929#if defined(MBEDTLS_X509_CRT_PARSE_C)
930 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
931#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_entropy_context entropy;
933 mbedtls_ctr_drbg_context ctr_drbg;
934 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200935 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200937#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200938 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200939#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200941 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 mbedtls_x509_crt cacert;
943 mbedtls_x509_crt clicert;
944 mbedtls_pk_context pkey;
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +0100945#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500946 psa_key_handle_t key_slot = 0; /* invalid key slot */
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +0100947#endif
Paul Bakkered27a042013-04-18 22:46:23 +0200948#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000949 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000950 const int *list;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300951#if defined(MBEDTLS_SSL_EXPORT_KEYS)
952 unsigned char eap_tls_keymaterial[16];
953 unsigned char eap_tls_iv[8];
954 const char* eap_tls_label = "client EAP encryption";
955 eap_tls_keys eap_tls_keying;
956#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000957
Paul Bakker1a207ec2011-02-06 13:22:40 +0000958 /*
959 * Make sure memory references are valid.
960 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200961 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200962 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200963 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200965 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966#if defined(MBEDTLS_X509_CRT_PARSE_C)
967 mbedtls_x509_crt_init( &cacert );
968 mbedtls_x509_crt_init( &clicert );
969 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200970#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200972 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200973#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000974
Hanno Beckerb2b468b2018-11-12 17:46:59 +0000975#if defined(MBEDTLS_USE_PSA_CRYPTO)
976 status = psa_crypto_init();
977 if( status != PSA_SUCCESS )
978 {
979 mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
980 (int) status );
981 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
982 goto exit;
983 }
984#endif
985
Paul Bakker9caf2d22010-02-18 19:37:19 +0000986 if( argc == 0 )
987 {
988 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000989 if( ret == 0 )
990 ret = 1;
991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000995 while( *list )
996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200998 list++;
999 if( !*list )
1000 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +00001002 list++;
1003 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +00001005 goto exit;
1006 }
1007
1008 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001009 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001010 opt.server_port = DFL_SERVER_PORT;
1011 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker90cb3592019-04-09 17:24:19 +01001012 opt.cid_enabled = DFL_CID_ENABLED;
1013 opt.cid_val = DFL_CID_VALUE;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001014 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
1015 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001016 opt.nbio = DFL_NBIO;
Hanno Becker16970d22017-10-10 15:56:37 +01001017 opt.event = DFL_EVENT;
Hanno Beckerbb425db2019-04-03 12:59:58 +01001018 opt.context_crt_cb = DFL_CONTEXT_CRT_CB;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001019 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001020 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001021 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +02001022 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +00001023 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +00001024 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +00001025 opt.crt_file = DFL_CRT_FILE;
1026 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001027 opt.key_opaque = DFL_KEY_OPAQUE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001028 opt.psk = DFL_PSK;
Hanno Beckere86964c2018-10-23 11:37:50 +01001029#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001030 opt.psk_opaque = DFL_PSK_OPAQUE;
Hanno Beckere86964c2018-10-23 11:37:50 +01001031#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001032#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1033 opt.ca_callback = DFL_CA_CALLBACK;
1034#endif
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001035 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001036 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001037 opt.ec_max_ops = DFL_EC_MAX_OPS;
Paul Bakker51936882011-02-20 16:05:58 +00001038 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +00001039 opt.renegotiation = DFL_RENEGOTIATION;
1040 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001041 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001042 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001043 opt.min_version = DFL_MIN_VERSION;
1044 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001045 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001046 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001047 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001048 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001049 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001050 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001051 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001052 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001053 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001054 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001055 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001056 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001057 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001058 opt.transport = DFL_TRANSPORT;
1059 opt.hs_to_min = DFL_HS_TO_MIN;
1060 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001061 opt.dtls_mtu = DFL_DTLS_MTU;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001062 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001063 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001064 opt.etm = DFL_ETM;
Hanno Becker4d615912018-08-14 13:33:30 +01001065 opt.dgram_packing = DFL_DGRAM_PACKING;
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001066 opt.eap_tls = DFL_EAP_TLS;
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001067 opt.reproducible = DFL_REPRODUCIBLE;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001068
1069 for( i = 1; i < argc; i++ )
1070 {
Paul Bakker9caf2d22010-02-18 19:37:19 +00001071 p = argv[i];
1072 if( ( q = strchr( p, '=' ) ) == NULL )
1073 goto usage;
1074 *q++ = '\0';
1075
1076 if( strcmp( p, "server_name" ) == 0 )
1077 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001078 else if( strcmp( p, "server_addr" ) == 0 )
1079 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001080 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001081 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001082 else if( strcmp( p, "dtls" ) == 0 )
1083 {
1084 int t = atoi( q );
1085 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001087 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001089 else
1090 goto usage;
1091 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001092 else if( strcmp( p, "debug_level" ) == 0 )
1093 {
1094 opt.debug_level = atoi( q );
1095 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1096 goto usage;
1097 }
Hanno Beckerbb425db2019-04-03 12:59:58 +01001098 else if( strcmp( p, "context_crt_cb" ) == 0 )
1099 {
1100 opt.context_crt_cb = atoi( q );
1101 if( opt.context_crt_cb != 0 && opt.context_crt_cb != 1 )
1102 goto usage;
1103 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001104 else if( strcmp( p, "nbio" ) == 0 )
1105 {
1106 opt.nbio = atoi( q );
1107 if( opt.nbio < 0 || opt.nbio > 2 )
1108 goto usage;
1109 }
Hanno Becker16970d22017-10-10 15:56:37 +01001110 else if( strcmp( p, "event" ) == 0 )
1111 {
1112 opt.event = atoi( q );
1113 if( opt.event < 0 || opt.event > 2 )
1114 goto usage;
1115 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001116 else if( strcmp( p, "read_timeout" ) == 0 )
1117 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001118 else if( strcmp( p, "max_resend" ) == 0 )
1119 {
1120 opt.max_resend = atoi( q );
1121 if( opt.max_resend < 0 )
1122 goto usage;
1123 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001124 else if( strcmp( p, "request_page" ) == 0 )
1125 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +02001126 else if( strcmp( p, "request_size" ) == 0 )
1127 {
1128 opt.request_size = atoi( q );
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001129 if( opt.request_size < 0 ||
1130 opt.request_size > MAX_REQUEST_SIZE )
Paul Bakker93c32b22014-04-25 13:40:05 +02001131 goto usage;
1132 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001133 else if( strcmp( p, "ca_file" ) == 0 )
1134 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +00001135 else if( strcmp( p, "ca_path" ) == 0 )
1136 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +00001137 else if( strcmp( p, "crt_file" ) == 0 )
1138 opt.crt_file = q;
1139 else if( strcmp( p, "key_file" ) == 0 )
1140 opt.key_file = q;
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001141#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
1142 else if( strcmp( p, "key_opaque" ) == 0 )
1143 opt.key_opaque = atoi( q );
1144#endif
Hanno Beckera0e20d02019-05-15 14:03:01 +01001145#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +01001146 else if( strcmp( p, "cid" ) == 0 )
1147 {
1148 opt.cid_enabled = atoi( q );
1149 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
1150 goto usage;
1151 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001152 else if( strcmp( p, "cid_renego" ) == 0 )
1153 {
1154 opt.cid_enabled_renego = atoi( q );
1155 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
1156 goto usage;
1157 }
Hanno Becker90cb3592019-04-09 17:24:19 +01001158 else if( strcmp( p, "cid_val" ) == 0 )
1159 {
1160 opt.cid_val = q;
1161 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001162 else if( strcmp( p, "cid_val_renego" ) == 0 )
1163 {
1164 opt.cid_val_renego = q;
1165 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001166#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001167 else if( strcmp( p, "psk" ) == 0 )
1168 opt.psk = q;
Hanno Beckere86964c2018-10-23 11:37:50 +01001169#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001170 else if( strcmp( p, "psk_opaque" ) == 0 )
1171 opt.psk_opaque = atoi( q );
Hanno Beckere86964c2018-10-23 11:37:50 +01001172#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001173#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1174 else if( strcmp( p, "ca_callback" ) == 0)
1175 opt.ca_callback = atoi( q );
1176#endif
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001177 else if( strcmp( p, "psk_identity" ) == 0 )
1178 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001179 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1180 opt.ecjpake_pw = q;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001181 else if( strcmp( p, "ec_max_ops" ) == 0 )
1182 opt.ec_max_ops = atoi( q );
Paul Bakker51936882011-02-20 16:05:58 +00001183 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +00001186
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001187 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +00001188 {
1189 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +00001190 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +00001191 }
Paul Bakker51936882011-02-20 16:05:58 +00001192 opt.force_ciphersuite[1] = 0;
1193 }
Paul Bakker48916f92012-09-16 19:57:18 +00001194 else if( strcmp( p, "renegotiation" ) == 0 )
1195 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001196 opt.renegotiation = (atoi( q )) ?
1197 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1198 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +00001199 }
1200 else if( strcmp( p, "allow_legacy" ) == 0 )
1201 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001202 switch( atoi( q ) )
1203 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001204 case -1:
1205 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1206 break;
1207 case 0:
1208 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1209 break;
1210 case 1:
1211 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1212 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001213 default: goto usage;
1214 }
Paul Bakker48916f92012-09-16 19:57:18 +00001215 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001216 else if( strcmp( p, "renegotiate" ) == 0 )
1217 {
1218 opt.renegotiate = atoi( q );
1219 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1220 goto usage;
1221 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001222 else if( strcmp( p, "exchanges" ) == 0 )
1223 {
1224 opt.exchanges = atoi( q );
1225 if( opt.exchanges < 1 )
1226 goto usage;
1227 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001228 else if( strcmp( p, "reconnect" ) == 0 )
1229 {
1230 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001231 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001232 goto usage;
1233 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001234 else if( strcmp( p, "reco_delay" ) == 0 )
1235 {
1236 opt.reco_delay = atoi( q );
1237 if( opt.reco_delay < 0 )
1238 goto usage;
1239 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001240 else if( strcmp( p, "reconnect_hard" ) == 0 )
1241 {
1242 opt.reconnect_hard = atoi( q );
1243 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
1244 goto usage;
1245 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001246 else if( strcmp( p, "tickets" ) == 0 )
1247 {
1248 opt.tickets = atoi( q );
1249 if( opt.tickets < 0 || opt.tickets > 2 )
1250 goto usage;
1251 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001252 else if( strcmp( p, "alpn" ) == 0 )
1253 {
1254 opt.alpn_string = q;
1255 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001256 else if( strcmp( p, "fallback" ) == 0 )
1257 {
1258 switch( atoi( q ) )
1259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
1261 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001262 default: goto usage;
1263 }
1264 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001265 else if( strcmp( p, "extended_ms" ) == 0 )
1266 {
1267 switch( atoi( q ) )
1268 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001269 case 0:
1270 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1271 break;
1272 case 1:
1273 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1274 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001275 default: goto usage;
1276 }
1277 }
Hanno Beckere6706e62017-05-15 16:05:15 +01001278 else if( strcmp( p, "curves" ) == 0 )
1279 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001280 else if( strcmp( p, "etm" ) == 0 )
1281 {
1282 switch( atoi( q ) )
1283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1285 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001286 default: goto usage;
1287 }
1288 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001289 else if( strcmp( p, "min_version" ) == 0 )
1290 {
1291 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001293 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001295 else if( strcmp( q, "tls1_1" ) == 0 ||
1296 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001298 else if( strcmp( q, "tls1_2" ) == 0 ||
1299 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001301 else
1302 goto usage;
1303 }
1304 else if( strcmp( p, "max_version" ) == 0 )
1305 {
1306 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001308 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001310 else if( strcmp( q, "tls1_1" ) == 0 ||
1311 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001313 else if( strcmp( q, "tls1_2" ) == 0 ||
1314 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001316 else
1317 goto usage;
1318 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001319 else if( strcmp( p, "arc4" ) == 0 )
1320 {
1321 switch( atoi( q ) )
1322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1324 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001325 default: goto usage;
1326 }
1327 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02001328 else if( strcmp( p, "allow_sha1" ) == 0 )
1329 {
1330 switch( atoi( q ) )
1331 {
1332 case 0: opt.allow_sha1 = 0; break;
1333 case 1: opt.allow_sha1 = 1; break;
1334 default: goto usage;
1335 }
1336 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001337 else if( strcmp( p, "force_version" ) == 0 )
1338 {
1339 if( strcmp( q, "ssl3" ) == 0 )
1340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1342 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001343 }
1344 else if( strcmp( q, "tls1" ) == 0 )
1345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1347 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001348 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001349 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1352 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001353 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001354 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1357 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001358 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001359 else if( strcmp( q, "dtls1" ) == 0 )
1360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1362 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1363 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001364 }
1365 else if( strcmp( q, "dtls1_2" ) == 0 )
1366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1368 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1369 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001370 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001371 else
1372 goto usage;
1373 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001374 else if( strcmp( p, "auth_mode" ) == 0 )
1375 {
1376 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001378 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001380 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001382 else
1383 goto usage;
1384 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001385 else if( strcmp( p, "max_frag_len" ) == 0 )
1386 {
1387 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001389 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001391 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001393 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001395 else
1396 goto usage;
1397 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001398 else if( strcmp( p, "trunc_hmac" ) == 0 )
1399 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001400 switch( atoi( q ) )
1401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1403 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001404 default: goto usage;
1405 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001406 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001407 else if( strcmp( p, "hs_timeout" ) == 0 )
1408 {
1409 if( ( p = strchr( q, '-' ) ) == NULL )
1410 goto usage;
1411 *p++ = '\0';
1412 opt.hs_to_min = atoi( q );
1413 opt.hs_to_max = atoi( p );
1414 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1415 goto usage;
1416 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001417 else if( strcmp( p, "mtu" ) == 0 )
1418 {
1419 opt.dtls_mtu = atoi( q );
1420 if( opt.dtls_mtu < 0 )
1421 goto usage;
1422 }
Hanno Becker4d615912018-08-14 13:33:30 +01001423 else if( strcmp( p, "dgram_packing" ) == 0 )
1424 {
1425 opt.dgram_packing = atoi( q );
1426 if( opt.dgram_packing != 0 &&
1427 opt.dgram_packing != 1 )
1428 {
1429 goto usage;
1430 }
1431 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001432 else if( strcmp( p, "recsplit" ) == 0 )
1433 {
1434 opt.recsplit = atoi( q );
1435 if( opt.recsplit < 0 || opt.recsplit > 1 )
1436 goto usage;
1437 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001438 else if( strcmp( p, "dhmlen" ) == 0 )
1439 {
1440 opt.dhmlen = atoi( q );
1441 if( opt.dhmlen < 0 )
1442 goto usage;
1443 }
Andres Amaya Garciabc818842018-10-16 21:08:38 +01001444 else if( strcmp( p, "query_config" ) == 0 )
1445 {
1446 return query_config( q );
1447 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001448 else if( strcmp( p, "eap_tls" ) == 0 )
1449 {
1450 opt.eap_tls = atoi( q );
1451 if( opt.eap_tls < 0 || opt.eap_tls > 1 )
1452 goto usage;
1453 }
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001454 else if( strcmp( p, "reproducible" ) == 0 )
1455 {
1456 opt.reproducible = 1;
1457 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001458 else
1459 goto usage;
1460 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001461
Hanno Becker16970d22017-10-10 15:56:37 +01001462 /* Event-driven IO is incompatible with the above custom
1463 * receive and send functions, as the polling builds on
1464 * refers to the underlying net_context. */
1465 if( opt.event == 1 && opt.nbio != 1 )
1466 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001467 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001468 opt.nbio = 1;
1469 }
1470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#if defined(MBEDTLS_DEBUG_C)
1472 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001473#endif
1474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001476 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001477 * Unhexify the pre-shared key if any is given
1478 */
1479 if( strlen( opt.psk ) )
1480 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001481 psk_len = strlen( opt.psk ) / 2;
1482 if( psk_len > sizeof( psk ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001483 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001484 mbedtls_printf( "pre-shared key too long\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001485 goto exit;
1486 }
1487
Hanno Becker1f583ee2019-04-09 17:12:56 +01001488 if( unhexify( opt.psk, psk ) != 0 )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001489 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001490 mbedtls_printf( "pre-shared key not valid hex\n" );
1491 goto exit;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001492 }
1493 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001495
Hanno Beckere86964c2018-10-23 11:37:50 +01001496
1497#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001498 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001499 {
1500 if( opt.psk == NULL )
1501 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00001502 mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
Hanno Beckere86964c2018-10-23 11:37:50 +01001503 ret = 2;
1504 goto usage;
1505 }
1506
1507 if( opt.force_ciphersuite[0] <= 0 )
1508 {
1509 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" );
1510 ret = 2;
1511 goto usage;
1512 }
1513 }
1514#endif /* MBEDTLS_USE_PSA_CRYPTO */
1515
1516 if( opt.force_ciphersuite[0] > 0 )
1517 {
1518 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1519 ciphersuite_info =
1520 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1521
1522 if( opt.max_version != -1 &&
1523 ciphersuite_info->min_minor_ver > opt.max_version )
1524 {
1525 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
1526 ret = 2;
1527 goto usage;
1528 }
1529 if( opt.min_version != -1 &&
1530 ciphersuite_info->max_minor_ver < opt.min_version )
1531 {
1532 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
1533 ret = 2;
1534 goto usage;
1535 }
1536
1537 /* If the server selects a version that's not supported by
1538 * this suite, then there will be no common ciphersuite... */
1539 if( opt.max_version == -1 ||
1540 opt.max_version > ciphersuite_info->max_minor_ver )
1541 {
1542 opt.max_version = ciphersuite_info->max_minor_ver;
1543 }
1544 if( opt.min_version < ciphersuite_info->min_minor_ver )
1545 {
1546 opt.min_version = ciphersuite_info->min_minor_ver;
1547 /* DTLS starts with TLS 1.1 */
1548 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1549 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1550 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1551 }
1552
1553 /* Enable RC4 if needed and not explicitly disabled */
1554 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
1555 {
1556 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
1557 {
1558 mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" );
1559 ret = 2;
1560 goto usage;
1561 }
1562
1563 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
1564 }
1565
Hanno Becker90cb3592019-04-09 17:24:19 +01001566
Hanno Beckere86964c2018-10-23 11:37:50 +01001567#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001568 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001569 {
1570 /* Ensure that the chosen ciphersuite is PSK-only; we must know
1571 * the ciphersuite in advance to set the correct policy for the
1572 * PSK key slot. This limitation might go away in the future. */
1573 if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
1574 opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 )
1575 {
1576 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" );
1577 ret = 2;
1578 goto usage;
1579 }
1580
1581 /* Determine KDF algorithm the opaque PSK will be used in. */
1582#if defined(MBEDTLS_SHA512_C)
1583 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1584 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1585 else
1586#endif /* MBEDTLS_SHA512_C */
1587 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1588 }
1589#endif /* MBEDTLS_USE_PSA_CRYPTO */
1590 }
1591
Hanno Beckera0e20d02019-05-15 14:03:01 +01001592#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001593 cid_len = strlen( opt.cid_val ) / 2;
1594 if( cid_len > sizeof( cid ) )
1595 {
1596 mbedtls_printf( "CID too long\n" );
1597 goto exit;
1598 }
Hanno Becker90cb3592019-04-09 17:24:19 +01001599
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001600 if( unhexify( opt.cid_val, cid ) != 0 )
1601 {
1602 mbedtls_printf( "CID not valid hex\n" );
1603 goto exit;
1604 }
1605
1606 /* Keep CID settings for renegotiation unless
1607 * specified otherwise. */
1608 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
1609 opt.cid_enabled_renego = opt.cid_enabled;
1610 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
1611 opt.cid_val_renego = opt.cid_val;
1612
1613 cid_renego_len = strlen( opt.cid_val_renego ) / 2;
1614 if( cid_renego_len > sizeof( cid_renego ) )
1615 {
1616 mbedtls_printf( "CID too long\n" );
1617 goto exit;
1618 }
1619
1620 if( unhexify( opt.cid_val_renego, cid_renego ) != 0 )
1621 {
1622 mbedtls_printf( "CID not valid hex\n" );
1623 goto exit;
1624 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001625#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01001626
Hanno Beckere6706e62017-05-15 16:05:15 +01001627#if defined(MBEDTLS_ECP_C)
1628 if( opt.curves != NULL )
1629 {
1630 p = (char *) opt.curves;
1631 i = 0;
1632
1633 if( strcmp( p, "none" ) == 0 )
1634 {
1635 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1636 }
1637 else if( strcmp( p, "default" ) != 0 )
1638 {
1639 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001640 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001641 {
1642 q = p;
1643
1644 /* Terminate the current string */
1645 while( *p != ',' && *p != '\0' )
1646 p++;
1647 if( *p == ',' )
1648 *p++ = '\0';
1649
1650 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1651 {
1652 curve_list[i++] = curve_cur->grp_id;
1653 }
1654 else
1655 {
1656 mbedtls_printf( "unknown curve %s\n", q );
1657 mbedtls_printf( "supported curves: " );
1658 for( curve_cur = mbedtls_ecp_curve_list();
1659 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1660 curve_cur++ )
1661 {
1662 mbedtls_printf( "%s ", curve_cur->name );
1663 }
1664 mbedtls_printf( "\n" );
1665 goto exit;
1666 }
1667 }
1668
1669 mbedtls_printf("Number of curves: %d\n", i );
1670
Hanno Becker8651a432017-06-09 16:13:22 +01001671 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001672 {
Hanno Becker8651a432017-06-09 16:13:22 +01001673 mbedtls_printf( "curves list too long, maximum %d",
1674 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001675 goto exit;
1676 }
1677
1678 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1679 }
1680 }
1681#endif /* MBEDTLS_ECP_C */
1682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001684 if( opt.alpn_string != NULL )
1685 {
1686 p = (char *) opt.alpn_string;
1687 i = 0;
1688
1689 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001690 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001691 {
1692 alpn_list[i++] = p;
1693
1694 /* Terminate the current string and move on to next one */
1695 while( *p != ',' && *p != '\0' )
1696 p++;
1697 if( *p == ',' )
1698 *p++ = '\0';
1699 }
1700 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001702
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001703 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001704 * 0. Initialize the RNG and the session data
1705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001707 fflush( stdout );
1708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 mbedtls_entropy_init( &entropy );
Philippe Antoine986b6f22019-06-07 15:04:32 +02001710 if (opt.reproducible)
1711 {
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001712 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy,
Philippe Antoine986b6f22019-06-07 15:04:32 +02001713 &entropy, (const unsigned char *) pers,
1714 strlen( pers ) ) ) != 0 )
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001715 {
1716 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
Philippe Antoine986b6f22019-06-07 15:04:32 +02001717 -ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001718 goto exit;
1719 }
Philippe Antoine986b6f22019-06-07 15:04:32 +02001720 }
1721 else
1722 {
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001723 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
Philippe Antoine986b6f22019-06-07 15:04:32 +02001724 &entropy, (const unsigned char *) pers,
1725 strlen( pers ) ) ) != 0 )
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001726 {
1727 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
Philippe Antoine986b6f22019-06-07 15:04:32 +02001728 -ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001729 goto exit;
1730 }
Paul Bakker508ad5a2011-12-04 17:09:26 +00001731 }
1732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001736 /*
1737 * 1.1. Load the trusted CA
1738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001740 fflush( stdout );
1741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001743 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001744 if( strcmp( opt.ca_path, "none" ) == 0 )
1745 ret = 0;
1746 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001748 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001749 if( strcmp( opt.ca_file, "none" ) == 0 )
1750 ret = 0;
1751 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001753 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001754#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755#if defined(MBEDTLS_CERTS_C)
1756 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 ret = mbedtls_x509_crt_parse( &cacert,
1759 (const unsigned char *) mbedtls_test_cas[i],
1760 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001761 if( ret != 0 )
1762 break;
1763 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001764#else
1765 {
1766 ret = 1;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001767 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001768 }
1769#endif
Paul Bakker42488232012-05-16 08:21:05 +00001770 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001772 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1773 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001774 goto exit;
1775 }
1776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001778
1779 /*
1780 * 1.2. Load own certificate and private key
1781 *
1782 * (can be skipped if client authentication is not required)
1783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001785 fflush( stdout );
1786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001788 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001789 if( strcmp( opt.crt_file, "none" ) == 0 )
1790 ret = 0;
1791 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001793 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001794#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001796 ret = mbedtls_x509_crt_parse( &clicert,
1797 (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001799#else
1800 {
1801 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001803 }
1804#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001805 if( ret != 0 )
1806 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001807 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1808 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001809 goto exit;
1810 }
1811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001813 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001814 if( strcmp( opt.key_file, "none" ) == 0 )
1815 ret = 0;
1816 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001818 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001819#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001821 ret = mbedtls_pk_parse_key( &pkey,
1822 (const unsigned char *) mbedtls_test_cli_key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001824#else
1825 {
1826 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001828 }
1829#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001830 if( ret != 0 )
1831 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001832 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
1833 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001834 goto exit;
1835 }
1836
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001837#if defined(MBEDTLS_USE_PSA_CRYPTO)
1838 if( opt.key_opaque != 0 )
1839 {
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001840 if( ( ret = mbedtls_pk_wrap_as_opaque( &pkey, &key_slot,
1841 PSA_ALG_SHA_256 ) ) != 0 )
1842 {
1843 mbedtls_printf( " failed\n ! "
1844 "mbedtls_pk_wrap_as_opaque returned -0x%x\n\n", -ret );
1845 goto exit;
1846 }
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001847 }
1848#endif /* MBEDTLS_USE_PSA_CRYPTO */
1849
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001850 mbedtls_printf( " ok (key type: %s)\n", mbedtls_pk_get_name( &pkey ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001852
1853 /*
1854 * 2. Start the connection
1855 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001856 if( opt.server_addr == NULL)
1857 opt.server_addr = opt.server_name;
1858
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001859 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001861 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001862 fflush( stdout );
1863
Hanno Becker16970d22017-10-10 15:56:37 +01001864 if( ( ret = mbedtls_net_connect( &server_fd,
1865 opt.server_addr, opt.server_port,
1866 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1867 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001868 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001869 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
1870 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001871 goto exit;
1872 }
1873
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001874 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001875 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001876 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001877 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001878 if( ret != 0 )
1879 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001880 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
1881 -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001882 goto exit;
1883 }
1884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001886
1887 /*
1888 * 3. Setup stuff
1889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001891 fflush( stdout );
1892
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001893 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1894 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001895 opt.transport,
1896 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001897 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001898 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
1899 -ret );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001900 goto exit;
1901 }
1902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001904 /* The default algorithms profile disables SHA-1, but our tests still
1905 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001906 if( opt.allow_sha1 > 0 )
1907 {
1908 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1909 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1910 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1911 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001912
Hanno Beckerbb425db2019-04-03 12:59:58 +01001913 if( opt.context_crt_cb == 0 )
1914 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
1915
Hanno Beckera1051b42019-02-26 11:38:29 +00001916 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Gilles Peskineef86ab22017-05-05 18:59:02 +02001917#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001918
Hanno Beckera0e20d02019-05-15 14:03:01 +01001919#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001920 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckerad4a1372019-05-03 13:06:44 +01001921 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001922 if( opt.cid_enabled == 1 &&
1923 opt.cid_enabled_renego == 1 &&
1924 cid_len != cid_renego_len )
1925 {
1926 mbedtls_printf( "CID length must not change during renegotiation\n" );
1927 goto usage;
1928 }
1929
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001930 if( opt.cid_enabled == 1 )
Hanno Becker8367ccc2019-05-14 11:30:10 +01001931 ret = mbedtls_ssl_conf_cid( &conf, cid_len,
1932 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001933 else
Hanno Becker8367ccc2019-05-14 11:30:10 +01001934 ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
1935 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001936
Hanno Beckerad4a1372019-05-03 13:06:44 +01001937 if( ret != 0 )
1938 {
Hanno Beckerd5eed422019-05-23 16:58:22 +01001939 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
1940 -ret );
Hanno Beckerad4a1372019-05-03 13:06:44 +01001941 goto exit;
1942 }
1943 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001944#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerad4a1372019-05-03 13:06:44 +01001945
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001946 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001947 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001950 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Hanno Becker16970d22017-10-10 15:56:37 +01001951 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min,
1952 opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001953
Hanno Becker4d615912018-08-14 13:33:30 +01001954 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01001955 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001959 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001960 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001961 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n",
1962 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001963 goto exit;
1964 }
Paul Bakker05decb22013-08-15 13:33:48 +02001965#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001968 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001969 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001970#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001973 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001974 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001975#endif
1976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001978 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001979 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001980#endif
1981
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001982#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1983 if( opt.eap_tls != 0 )
1984 mbedtls_ssl_conf_export_keys_ext_cb( &conf, eap_tls_key_derivation,
1985 &eap_tls_keying );
1986#endif
1987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001989 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001990 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Hanno Becker16970d22017-10-10 15:56:37 +01001991 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1992 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001993#endif
1994
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001995#if defined(MBEDTLS_DHM_C)
1996 if( opt.dhmlen != DFL_DHMLEN )
1997 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1998#endif
1999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002001 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002002 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002003 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002004 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
2005 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002006 goto exit;
2007 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002008#endif
2009
Philippe Antoine986b6f22019-06-07 15:04:32 +02002010 if (opt.reproducible)
2011 {
2012 srand( 1 );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002013 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
Philippe Antoinef91b3722019-06-11 16:02:43 +02002014#if defined(MBEDTLS_HAVE_TIME)
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002015#if defined(MBEDTLS_PLATFORM_TIME_ALT)
2016 mbedtls_platform_set_time( dummy_constant_time );
2017#else
Philippe Antoine7c9d7242019-06-11 12:11:36 +02002018 fprintf( stderr, "Warning: reproducible option used without constant time\n" );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002019#endif
Philippe Antoine0ff84fb2019-06-11 12:15:17 +02002020#endif
Philippe Antoine986b6f22019-06-07 15:04:32 +02002021 }
2022 else
2023 {
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002024 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
2025 }
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002026 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002027
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002028 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00002029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002031 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02002032#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002033
Paul Bakker645ce3a2012-10-31 12:32:41 +00002034 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002035 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002036
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002037#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002038 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002039 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002040#endif
Paul Bakker51936882011-02-20 16:05:58 +00002041
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002042 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002043 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002045 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002046#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002049 if( strcmp( opt.ca_path, "none" ) != 0 &&
2050 strcmp( opt.ca_file, "none" ) != 0 )
2051 {
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02002052#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2053 if( opt.ca_callback != 0 )
Hanno Beckercbb59032019-03-28 14:14:22 +00002054 mbedtls_ssl_conf_ca_cb( &conf, ca_callback, &cacert );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02002055 else
2056#endif
2057 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002058 }
2059 if( strcmp( opt.crt_file, "none" ) != 0 &&
2060 strcmp( opt.key_file, "none" ) != 0 )
2061 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002062 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002063 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002064 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
2065 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002066 goto exit;
2067 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002068 }
Paul Bakkered27a042013-04-18 22:46:23 +02002069#endif
2070
Hanno Beckere6706e62017-05-15 16:05:15 +01002071#if defined(MBEDTLS_ECP_C)
2072 if( opt.curves != NULL &&
2073 strcmp( opt.curves, "default" ) != 0 )
2074 {
2075 mbedtls_ssl_conf_curves( &conf, curve_list );
2076 }
2077#endif
2078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckere86964c2018-10-23 11:37:50 +01002080#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002081 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01002082 {
2083 /* The algorithm has already been determined earlier. */
Hanno Becker37519ea2019-01-25 14:26:01 +00002084 status = psa_allocate_key( &slot );
Hanno Becker1d911cd2018-11-15 13:06:09 +00002085 if( status != PSA_SUCCESS )
2086 {
2087 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2088 goto exit;
2089 }
Hanno Beckere86964c2018-10-23 11:37:50 +01002090
Hanno Becker13871242019-01-25 14:26:26 +00002091 policy = psa_key_policy_init();
Hanno Beckere86964c2018-10-23 11:37:50 +01002092 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2093
2094 status = psa_set_key_policy( slot, &policy );
2095 if( status != PSA_SUCCESS )
2096 {
2097 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2098 goto exit;
2099 }
2100
2101 status = psa_import_key( slot, PSA_KEY_TYPE_DERIVE, psk, psk_len );
2102 if( status != PSA_SUCCESS )
2103 {
2104 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2105 goto exit;
2106 }
2107
2108 if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, slot,
2109 (const unsigned char *) opt.psk_identity,
Hanno Becker5cd607b2018-11-05 12:52:42 +00002110 strlen( opt.psk_identity ) ) ) != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01002111 {
2112 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
2113 ret );
2114 goto exit;
2115 }
2116 }
2117 else
2118#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002119 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002120 (const unsigned char *) opt.psk_identity,
2121 strlen( opt.psk_identity ) ) ) != 0 )
2122 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002123 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n",
2124 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002125 goto exit;
2126 }
Hanno Beckere86964c2018-10-23 11:37:50 +01002127#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002128
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002129 if( opt.min_version != DFL_MIN_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01002130 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
2131 opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01002132
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002133 if( opt.max_version != DFL_MAX_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01002134 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
2135 opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00002136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02002138 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002139 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02002140#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002142 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
2143 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002144 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
2145 -ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002146 goto exit;
2147 }
2148
2149#if defined(MBEDTLS_X509_CRT_PARSE_C)
2150 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
2151 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002152 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
2153 ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002154 goto exit;
2155 }
2156#endif
2157
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002158#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2159 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
2160 {
2161 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
2162 (const unsigned char *) opt.ecjpake_pw,
2163 strlen( opt.ecjpake_pw ) ) ) != 0 )
2164 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002165 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
2166 ret );
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002167 goto exit;
2168 }
2169 }
2170#endif
2171
Hanno Beckerbb425db2019-04-03 12:59:58 +01002172#if defined(MBEDTLS_X509_CRT_PARSE_C)
2173 if( opt.context_crt_cb == 1 )
2174 mbedtls_ssl_set_verify( &ssl, my_verify, NULL );
2175#endif /* MBEDTLS_X509_CRT_PARSE_C */
2176
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002177 if( opt.nbio == 2 )
2178 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
2179 else
Hanno Becker16970d22017-10-10 15:56:37 +01002180 mbedtls_ssl_set_bio( &ssl, &server_fd,
2181 mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02002182 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002183
Hanno Beckera0e20d02019-05-15 14:03:01 +01002184#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +01002185 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2186 {
2187 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
2188 cid, cid_len ) ) != 0 )
2189 {
2190 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2191 ret );
2192 goto exit;
2193 }
2194 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002195#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01002196
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002197#if defined(MBEDTLS_SSL_PROTO_DTLS)
2198 if( opt.dtls_mtu != DFL_DTLS_MTU )
2199 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
2200#endif
2201
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002202#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002203 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
2204 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002205#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002206
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002207#if defined(MBEDTLS_ECP_RESTARTABLE)
2208 if( opt.ec_max_ops != DFL_EC_MAX_OPS )
2209 mbedtls_ecp_set_max_ops( opt.ec_max_ops );
2210#endif
2211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002213
Paul Bakker5121ce52009-01-03 21:22:43 +00002214 /*
2215 * 4. Handshake
2216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 fflush( stdout );
2219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 {
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002222 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2223 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002224 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Paul Bakker40e46942009-01-03 21:51:57 +00002225 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002226 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
2227 -ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2229 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01002230 " Unable to verify the server's certificate. "
2231 "Either it is invalid,\n"
2232 " or you didn't set ca_file or ca_path "
2233 "to an appropriate value.\n"
2234 " Alternatively, you may want to use "
2235 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002237 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00002238 }
Hanno Becker16970d22017-10-10 15:56:37 +01002239
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002240#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002241 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2242 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002243#endif
2244
Hanno Becker16970d22017-10-10 15:56:37 +01002245 /* For event-driven IO, wait for socket to become available */
2246 if( opt.event == 1 /* level triggered IO */ )
2247 {
2248#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002249 ret = idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002250#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002251 ret = idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002252#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002253 if( ret != 0 )
2254 goto exit;
Hanno Becker16970d22017-10-10 15:56:37 +01002255 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 }
2257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Hanno Becker16970d22017-10-10 15:56:37 +01002259 mbedtls_ssl_get_version( &ssl ),
2260 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
2263 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002264 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002266
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002267#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2268 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
2269 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
2270#endif
2271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002273 if( opt.alpn_string != NULL )
2274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
2276 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002277 alp ? alp : "(none)" );
2278 }
2279#endif
2280
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002281#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Ron Eldor51d3ab52019-05-12 14:54:30 +03002282 if( opt.eap_tls != 0 )
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002283 {
2284 size_t j = 0;
Ron Eldor51d3ab52019-05-12 14:54:30 +03002285
2286 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type,
2287 eap_tls_keying.master_secret,
2288 sizeof( eap_tls_keying.master_secret ),
2289 eap_tls_label,
2290 eap_tls_keying.randbytes,
2291 sizeof( eap_tls_keying.randbytes ),
2292 eap_tls_keymaterial,
2293 sizeof( eap_tls_keymaterial ) ) )
2294 != 0 )
2295 {
2296 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2297 -ret );
2298 goto exit;
2299 }
2300
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002301 mbedtls_printf( " EAP-TLS key material is:" );
2302 for( j = 0; j < sizeof( eap_tls_keymaterial ); j++ )
2303 {
2304 if( j % 8 == 0 )
2305 mbedtls_printf("\n ");
2306 mbedtls_printf("%02x ", eap_tls_keymaterial[j] );
2307 }
2308 mbedtls_printf("\n");
2309
Ron Eldor51d3ab52019-05-12 14:54:30 +03002310 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type, NULL, 0,
Ron Eldor51c45072019-05-15 17:49:54 +03002311 eap_tls_label,
2312 eap_tls_keying.randbytes,
2313 sizeof( eap_tls_keying.randbytes ),
2314 eap_tls_iv,
2315 sizeof( eap_tls_iv ) ) ) != 0 )
Ron Eldor51d3ab52019-05-12 14:54:30 +03002316 {
2317 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2318 -ret );
2319 goto exit;
2320 }
2321
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002322 mbedtls_printf( " EAP-TLS IV is:" );
2323 for( j = 0; j < sizeof( eap_tls_iv ); j++ )
2324 {
2325 if( j % 8 == 0 )
2326 mbedtls_printf("\n ");
2327 mbedtls_printf("%02x ", eap_tls_iv[j] );
2328 }
2329 mbedtls_printf("\n");
2330 }
2331#endif
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002332 if( opt.reconnect != 0 )
2333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002335 fflush( stdout );
2336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002338 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002339 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
2340 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002341 goto exit;
2342 }
2343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002345 }
2346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002348 /*
2349 * 5. Verify the server certificate
2350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002352
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002353 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002354 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002355 char vrfy_buf[512];
2356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002358
Hanno Becker16970d22017-10-10 15:56:37 +01002359 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ),
2360 " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00002361
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002362 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002363 }
2364 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002366
Hanno Beckera9766c2c2019-02-25 17:43:18 +00002367 mbedtls_printf( " . Peer certificate information ...\n" );
2368 mbedtls_printf( "%s\n", peer_crt_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002370
Hanno Beckera0e20d02019-05-15 14:03:01 +01002371#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002372 ret = report_cid_usage( &ssl, "initial handshake" );
2373 if( ret != 0 )
2374 goto exit;
2375
Hanno Becker90cb3592019-04-09 17:24:19 +01002376 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2377 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002378 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
2379 cid_renego,
2380 cid_renego_len ) ) != 0 )
Hanno Becker90cb3592019-04-09 17:24:19 +01002381 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002382 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2383 ret );
2384 return( ret );
Hanno Becker90cb3592019-04-09 17:24:19 +01002385 }
2386 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002387#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01002388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002390 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002391 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002392 /*
2393 * Perform renegotiation (this must be done when the server is waiting
2394 * for input from our side).
2395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002397 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002399 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002400 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002401 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002402 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002403 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002404 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
2405 ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002406 goto exit;
2407 }
Hanno Becker16970d22017-10-10 15:56:37 +01002408
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002409#if defined(MBEDTLS_ECP_RESTARTABLE)
2410 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2411 continue;
2412#endif
2413
Hanno Becker16970d22017-10-10 15:56:37 +01002414 /* For event-driven IO, wait for socket to become available */
2415 if( opt.event == 1 /* level triggered IO */ )
2416 {
2417#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002418 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002419#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002420 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002421#endif
2422 }
2423
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002424 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002426 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002428
Hanno Beckera0e20d02019-05-15 14:03:01 +01002429#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002430 ret = report_cid_usage( &ssl, "after renegotiation" );
2431 if( ret != 0 )
2432 goto exit;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002433#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002434
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 /*
2436 * 6. Write the GET request
2437 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002438 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002439send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002441 fflush( stdout );
2442
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002443 len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST,
2444 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002445 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002446
2447 /* Add padding to GET request to reach opt.request_size in length */
2448 if( opt.request_size != DFL_REQUEST_SIZE &&
2449 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02002450 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002451 memset( buf + len, 'A', opt.request_size - len - tail_len );
2452 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02002453 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002454
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002455 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002456 len += tail_len;
2457
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02002458 /* Truncate if request size is smaller than the "natural" size */
2459 if( opt.request_size != DFL_REQUEST_SIZE &&
2460 len > opt.request_size )
2461 {
2462 len = opt.request_size;
2463
2464 /* Still end with \r\n unless that's really not possible */
2465 if( len >= 2 ) buf[len - 2] = '\r';
2466 if( len >= 1 ) buf[len - 1] = '\n';
2467 }
2468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 {
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002471 written = 0;
2472 frags = 0;
2473
2474 do
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 {
Hanno Becker16970d22017-10-10 15:56:37 +01002476 while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002477 len - written ) ) < 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002478 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002479 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002480 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002481 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002482 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002483 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
2484 -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002485 goto exit;
2486 }
Hanno Becker16970d22017-10-10 15:56:37 +01002487
2488 /* For event-driven IO, wait for socket to become available */
2489 if( opt.event == 1 /* level triggered IO */ )
2490 {
2491#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002492 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002493#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002494 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002495#endif
2496 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002497 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002498
2499 frags++;
2500 written += ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002501 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002502 while( written < len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002503 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002504 else /* Not stream, so datagram */
2505 {
Hanno Becker16970d22017-10-10 15:56:37 +01002506 while( 1 )
2507 {
2508 ret = mbedtls_ssl_write( &ssl, buf, len );
2509
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002510#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002511 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002512 continue;
2513#endif
2514
Hanno Becker16970d22017-10-10 15:56:37 +01002515 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2516 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2517 break;
2518
2519 /* For event-driven IO, wait for socket to become available */
2520 if( opt.event == 1 /* level triggered IO */ )
2521 {
2522#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002523 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002524#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002525 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002526#endif
2527 }
2528 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002529
2530 if( ret < 0 )
2531 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002532 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n",
2533 ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002534 goto exit;
2535 }
2536
2537 frags = 1;
2538 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002539
2540 if( written < len )
2541 {
2542 mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
2543 "was truncated to size %u", (unsigned) written );
2544 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002545 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002546
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002547 buf[written] = '\0';
Hanno Becker16970d22017-10-10 15:56:37 +01002548 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
2549 written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002550
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002551 /* Send a non-empty request if request_size == 0 */
2552 if ( len == 0 )
2553 {
2554 opt.request_size = DFL_REQUEST_SIZE;
2555 goto send_request;
2556 }
2557
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 /*
2559 * 7. Read the HTTP response
2560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002562 fflush( stdout );
2563
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002564 /*
2565 * TLS and DTLS need different reading styles (stream vs datagram)
2566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002568 {
2569 do
2570 {
2571 len = sizeof( buf ) - 1;
2572 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002574
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002575#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002576 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002577 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002578#endif
2579
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002580 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
2581 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Hanno Becker16970d22017-10-10 15:56:37 +01002582 {
2583 /* For event-driven IO, wait for socket to become available */
2584 if( opt.event == 1 /* level triggered IO */ )
2585 {
2586#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002587 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002588#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002589 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002590#endif
2591 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002592 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01002593 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002594
2595 if( ret <= 0 )
2596 {
2597 switch( ret )
2598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2600 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002601 ret = 0;
2602 goto close_notify;
2603
2604 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 case MBEDTLS_ERR_NET_CONN_RESET:
2606 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002607 ret = 0;
2608 goto reconnect;
2609
2610 default:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002611 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n",
2612 -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002613 goto exit;
2614 }
2615 }
2616
2617 len = ret;
2618 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002620
2621 /* End of message should be detected according to the syntax of the
2622 * application protocol (eg HTTP), just use a dummy test here. */
2623 if( ret > 0 && buf[len-1] == '\n' )
2624 {
2625 ret = 0;
2626 break;
2627 }
2628 }
2629 while( 1 );
2630 }
2631 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00002632 {
2633 len = sizeof( buf ) - 1;
2634 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002635
Hanno Becker16970d22017-10-10 15:56:37 +01002636 while( 1 )
2637 {
2638 ret = mbedtls_ssl_read( &ssl, buf, len );
2639
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002640#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002641 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002642 continue;
2643#endif
2644
Hanno Becker16970d22017-10-10 15:56:37 +01002645 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2646 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2647 break;
2648
2649 /* For event-driven IO, wait for socket to become available */
2650 if( opt.event == 1 /* level triggered IO */ )
2651 {
2652#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002653 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002654#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002655 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002656#endif
2657 }
2658 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002660 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002661 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002662 switch( ret )
2663 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002664 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002666 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02002667 goto send_request;
2668 goto exit;
2669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2671 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002672 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002673 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00002674
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002675 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002677 goto exit;
2678 }
Paul Bakker5690efc2011-05-26 13:16:06 +00002679 }
2680
Paul Bakker5121ce52009-01-03 21:22:43 +00002681 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02002682 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002684 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002687 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002688 * 7b. Simulate hard reset and reconnect from same port?
2689 */
2690 if( opt.reconnect_hard != 0 )
2691 {
2692 opt.reconnect_hard = 0;
2693
2694 mbedtls_printf( " . Restarting connection from same port..." );
2695 fflush( stdout );
2696
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002697#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker23699ef2019-02-26 12:36:53 +00002698 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002699#endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Becker23699ef2019-02-26 12:36:53 +00002700
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002701 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
2702 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002703 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2704 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002705 goto exit;
2706 }
2707
2708 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
2709 {
2710 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002711 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002712 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002713 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002714 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2715 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002716 goto exit;
2717 }
Hanno Becker16970d22017-10-10 15:56:37 +01002718
2719 /* For event-driven IO, wait for socket to become available */
2720 if( opt.event == 1 /* level triggered IO */ )
2721 {
2722#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002723 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002724#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002725 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002726#endif
2727 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002728 }
2729
2730 mbedtls_printf( " ok\n" );
2731
2732 goto send_request;
2733 }
2734
2735 /*
2736 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002737 */
2738 if( --opt.exchanges > 0 )
2739 goto send_request;
2740
2741 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002742 * 8. Done, cleanly close the connection
2743 */
2744close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002746 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002747
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002748 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002750 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002751 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002754
2755 /*
2756 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002757 */
2758reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002759 if( opt.reconnect != 0 )
2760 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02002761 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002762
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002763 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002764
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002765#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002766 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02002767 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002768#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002771
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002772#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckera1051b42019-02-26 11:38:29 +00002773 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002774#endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Beckera1051b42019-02-26 11:38:29 +00002775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002777 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002778 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2779 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002780 goto exit;
2781 }
2782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002784 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002785 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n",
2786 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002787 goto exit;
2788 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002789
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002790 if( ( ret = mbedtls_net_connect( &server_fd,
2791 opt.server_addr, opt.server_port,
2792 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2793 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002794 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002795 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
2796 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002797 goto exit;
2798 }
2799
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002800 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002801 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002802 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002803 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002804 if( ret != 0 )
2805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002807 -ret );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002808 goto exit;
2809 }
2810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002812 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002813 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002814 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002815 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002816 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002817 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2818 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002819 goto exit;
2820 }
2821 }
2822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002824
2825 goto send_request;
2826 }
2827
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002828 /*
2829 * Cleanup and exit
2830 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002831exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00002833 if( ret != 0 )
2834 {
2835 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 mbedtls_strerror( ret, error_buf, 100 );
2837 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002838 }
2839#endif
2840
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002841 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843#if defined(MBEDTLS_X509_CRT_PARSE_C)
2844 mbedtls_x509_crt_free( &clicert );
2845 mbedtls_x509_crt_free( &cacert );
2846 mbedtls_pk_free( &pkey );
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01002847#if defined(MBEDTLS_USE_PSA_CRYPTO)
2848 psa_destroy_key( key_slot );
2849#endif
Paul Bakkered27a042013-04-18 22:46:23 +02002850#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 mbedtls_ssl_session_free( &saved_session );
2852 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002853 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 mbedtls_ctr_drbg_free( &ctr_drbg );
2855 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
Hanno Becker3f24ea92018-11-05 13:25:17 +00002857#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
2858 defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002859 if( opt.psk_opaque != 0 )
Hanno Becker3f24ea92018-11-05 13:25:17 +00002860 {
2861 /* This is ok even if the slot hasn't been
2862 * initialized (we might have jumed here
2863 * immediately because of bad cmd line params,
2864 * for example). */
Hanno Becker1d911cd2018-11-15 13:06:09 +00002865 status = psa_destroy_key( slot );
Hanno Becker3f24ea92018-11-05 13:25:17 +00002866 if( status != PSA_SUCCESS )
2867 {
2868 mbedtls_printf( "Failed to destroy key slot %u - error was %d",
Hanno Becker1d911cd2018-11-15 13:06:09 +00002869 (unsigned) slot, (int) status );
Hanno Becker3f24ea92018-11-05 13:25:17 +00002870 if( ret == 0 )
2871 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2872 }
2873 }
2874#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED &&
2875 MBEDTLS_USE_PSA_CRYPTO */
2876
Paul Bakkercce9d772011-11-18 14:26:47 +00002877#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002879 fflush( stdout ); getchar();
2880#endif
2881
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002882 // Shell can not handle large exit numbers -> 1 for errors
2883 if( ret < 0 )
2884 ret = 1;
2885
Paul Bakker5121ce52009-01-03 21:22:43 +00002886 return( ret );
2887}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
2889 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002890 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */