blob: a21de794a1371aba2803647868768b71407278b8 [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
Paul Bakker0e6975b2009-02-10 22:19:10 +0000134
Paul Bakker93c32b22014-04-25 13:40:05 +0200135#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
136#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_X509_CRT_PARSE_C)
Janos Follathae13beb2019-04-05 14:06:58 +0100139#define USAGE_CONTEXT_CRT_CB \
Hanno Beckerbb425db2019-04-03 12:59:58 +0100140 " context_crt_cb=%%d This determines whether the CRT verification callback is bound\n" \
141 " to the SSL configuration of the SSL context.\n" \
142 " Possible values:\n"\
143 " - 0 (default): Use CRT callback bound to configuration\n" \
144 " - 1: Use CRT callback bound to SSL context\n"
145#else
Janos Follathae13beb2019-04-05 14:06:58 +0100146#define USAGE_CONTEXT_CRT_CB ""
Hanno Beckerbb425db2019-04-03 12:59:58 +0100147#endif /* MBEDTLS_X509_CRT_PARSE_C */
148#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000150#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100151 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
152 " default: \"\" (pre-loaded)\n" \
153 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
154 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
155 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
156 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000157 " key_file=%%s default: \"\" (pre-loaded)\n"
158#else
159#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 " No file operations available (MBEDTLS_FS_IO not defined)\n"
161#endif /* MBEDTLS_FS_IO */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100162#else /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200163#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100165#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
166#define USAGE_KEY_OPAQUE \
167 " key_opaque=%%d Handle your private key as if it were opaque\n" \
168 " default: 0 (disabled)\n"
169#else
170#define USAGE_KEY_OPAQUE ""
171#endif
Paul Bakkered27a042013-04-18 22:46:23 +0200172
Hanno Beckera0e20d02019-05-15 14:03:01 +0100173#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +0100174#define USAGE_CID \
175 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
176 " default: 0 (disabled)\n" \
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100177 " cid_renego=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension during renegotiation.\n" \
178 " default: same as 'cid'\n" \
Hanno Becker90cb3592019-04-09 17:24:19 +0100179 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100180 " default: \"\"\n" \
181 " cid_val_renego=%%s The CID to use for incoming messages (in hex, without 0x) after renegotiation.\n" \
182 " default: same as cid_val\n"
Hanno Beckera0e20d02019-05-15 14:03:01 +0100183#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +0100184#define USAGE_CID ""
Hanno Beckera0e20d02019-05-15 14:03:01 +0100185#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +0100186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckere86964c2018-10-23 11:37:50 +0100188#define USAGE_PSK_RAW \
Paul Bakkered27a042013-04-18 22:46:23 +0200189 " psk=%%s default: \"\" (in hex, without 0x)\n" \
190 " psk_identity=%%s default: \"Client_identity\"\n"
Hanno Beckere86964c2018-10-23 11:37:50 +0100191#if defined(MBEDTLS_USE_PSA_CRYPTO)
192#define USAGE_PSK_SLOT \
Hanno Becker1d911cd2018-11-15 13:06:09 +0000193 " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
194 " Enable this to store the PSK configured through command line\n" \
195 " parameter `psk` in a PSA-based key slot.\n" \
Hanno Beckere86964c2018-10-23 11:37:50 +0100196 " Note: Currently only supported in conjunction with\n" \
197 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
198 " to force a particular PSK-only ciphersuite.\n" \
199 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
200 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
201 " with prepopulated key slots instead of importing raw key material.\n"
202#else
203#define USAGE_PSK_SLOT ""
204#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker1bac87c2019-03-29 12:02:26 +0000205#define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
206#else
207#define USAGE_PSK ""
208#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
209
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200210#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
211#define USAGE_CA_CALLBACK \
212 " ca_callback=%%d default: 0 (disabled)\n" \
213 " Enable this to use the trusted certificate callback function\n"
214#else
215#define USAGE_CA_CALLBACK ""
216#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5690efc2011-05-26 13:16:06 +0000217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200219#define USAGE_TICKETS \
220 " tickets=%%d default: 1 (enabled)\n"
221#else
222#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200224
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300225#if defined(MBEDTLS_SSL_EXPORT_KEYS)
226#define USAGE_EAP_TLS \
227 " eap_tls=%%d default: 0 (disabled)\n"
228#else
229#define USAGE_EAP_TLS ""
230#endif /* MBEDTLS_SSL_EXPORT_KEYS */
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200233#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100234 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200235#else
236#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200240#define USAGE_MAX_FRAG_LEN \
241 " max_frag_len=%%d default: 16384 (tls default)\n" \
242 " options: 512, 1024, 2048, 4096\n"
243#else
244#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100248#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200249 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100250#else
251#define USAGE_RECSPLIT
252#endif
253
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200254#if defined(MBEDTLS_DHM_C)
255#define USAGE_DHMLEN \
256 " dhmlen=%%d default: (library default: 1024 bits)\n"
257#else
258#define USAGE_DHMLEN
259#endif
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200262#define USAGE_ALPN \
263 " alpn=%%s default: \"\" (disabled)\n" \
264 " example: spdy/1,http/1.1\n"
265#else
266#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200268
Hanno Beckere6706e62017-05-15 16:05:15 +0100269#if defined(MBEDTLS_ECP_C)
270#define USAGE_CURVES \
271 " curves=a,b,c,d default: \"default\" (library default)\n" \
272 " example: \"secp521r1,brainpoolP512r1\"\n" \
273 " - use \"none\" for empty list\n" \
274 " - see mbedtls_ecp_curve_list()\n" \
275 " for acceptable curve names\n"
276#else
277#define USAGE_CURVES ""
278#endif
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200281#define USAGE_DTLS \
282 " dtls=%%d default: 0 (TLS)\n" \
283 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200284 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Becker4d615912018-08-14 13:33:30 +0100285 " mtu=%%d default: (library default: unlimited)\n" \
286 " dgram_packing=%%d default: 1 (allowed)\n" \
287 " allow or forbid packing of multiple\n" \
288 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200289#else
290#define USAGE_DTLS ""
291#endif
292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200294#define USAGE_FALLBACK \
295 " fallback=0/1 default: (library default: off)\n"
296#else
297#define USAGE_FALLBACK ""
298#endif
299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200301#define USAGE_EMS \
302 " extended_ms=0/1 default: (library default: on)\n"
303#else
304#define USAGE_EMS ""
305#endif
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100308#define USAGE_ETM \
309 " etm=0/1 default: (library default: on)\n"
310#else
311#define USAGE_ETM ""
312#endif
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100315#define USAGE_RENEGO \
316 " renegotiation=%%d default: 0 (disabled)\n" \
317 " renegotiate=%%d default: 0 (disabled)\n"
318#else
319#define USAGE_RENEGO ""
320#endif
321
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200322#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
323#define USAGE_ECJPAKE \
324 " ecjpake_pw=%%s default: none (disabled)\n"
325#else
326#define USAGE_ECJPAKE ""
327#endif
328
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200329#if defined(MBEDTLS_ECP_RESTARTABLE)
330#define USAGE_ECRESTART \
331 " ec_max_ops=%%s default: library default (restart disabled)\n"
332#else
333#define USAGE_ECRESTART ""
334#endif
335
Paul Bakker9caf2d22010-02-18 19:37:19 +0000336#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000337 "\n usage: ssl_client2 param=<>...\n" \
338 "\n acceptable parameters:\n" \
339 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100340 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000341 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000342 " request_page=%%s default: \".\"\n" \
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +0100343 " request_size=%%d default: about 34 (basic request)\n" \
344 " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
345 " If 0, in the first exchange only an empty\n" \
346 " application data message is sent followed by\n" \
347 " a second non-empty message before attempting\n" \
348 " to read a response from the server\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100349 " debug_level=%%d default: 0 (disabled)\n" \
350 " nbio=%%d default: 0 (blocking I/O)\n" \
351 " options: 1 (non-blocking), 2 (added delays)\n" \
352 " event=%%d default: 0 (loop)\n" \
353 " options: 1 (level-triggered, implies nbio=1),\n" \
354 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200355 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100356 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200357 USAGE_DTLS \
Hanno Becker90cb3592019-04-09 17:24:19 +0100358 USAGE_CID \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200359 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100360 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100361 " options: none, optional, required\n" \
362 USAGE_IO \
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100363 USAGE_KEY_OPAQUE \
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200364 USAGE_CA_CALLBACK \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100365 "\n" \
366 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200367 USAGE_ECJPAKE \
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200368 USAGE_ECRESTART \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100369 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100370 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100371 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200372 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200373 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200374 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200375 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200376 USAGE_TICKETS \
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300377 USAGE_EAP_TLS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100378 USAGE_MAX_FRAG_LEN \
379 USAGE_TRUNC_HMAC \
Janos Follathae13beb2019-04-05 14:06:58 +0100380 USAGE_CONTEXT_CRT_CB \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200381 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200382 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200383 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100384 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100385 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100386 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200387 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000388 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100389 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200390 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200391 " min_version=%%s default: (library default: tls1)\n" \
392 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000393 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100394 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000395 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000396 " force_ciphersuite=<name> default: all enabled\n"\
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100397 " query_config=<name> return 0 if the specified\n" \
398 " configuration macro is defined and 1\n" \
399 " otherwise. The expansion of the macro\n" \
400 " is printed if it is defined\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000401 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000402
Hanno Becker8651a432017-06-09 16:13:22 +0100403#define ALPN_LIST_SIZE 10
404#define CURVE_LIST_SIZE 20
405
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500406#if defined(MBEDTLS_CHECK_PARAMS)
407#include "mbedtls/platform_util.h"
408void mbedtls_param_failed( const char *failure_condition,
409 const char *file,
410 int line )
411{
412 mbedtls_printf( "%s:%i: Input param failed - %s\n",
413 file, line, failure_condition );
414 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
415}
416#endif
417
Rich Evans85b05ec2015-02-12 11:37:29 +0000418/*
419 * global options
420 */
421struct options
422{
423 const char *server_name; /* hostname of the server (client only) */
424 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200425 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000426 int debug_level; /* level of debugging */
427 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100428 int event; /* loop or event-driven IO? level or edge triggered? */
429 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000430 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000431 const char *request_page; /* page on server to request */
432 int request_size; /* pad request with header to requested size */
433 const char *ca_file; /* the file with the CA certificate(s) */
434 const char *ca_path; /* the path with the CA certificate(s) reside */
435 const char *crt_file; /* the file with the client certificate */
436 const char *key_file; /* the file with the client key */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100437 int key_opaque; /* handle private key as if it were opaque */
Hanno Beckere86964c2018-10-23 11:37:50 +0100438#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000439 int psk_opaque;
Hanno Beckere86964c2018-10-23 11:37:50 +0100440#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200441#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000442 int ca_callback; /* Use callback for trusted certificate list */
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200443#endif
Rich Evans85b05ec2015-02-12 11:37:29 +0000444 const char *psk; /* the pre-shared key */
445 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200446 const char *ecjpake_pw; /* the EC J-PAKE password */
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200447 int ec_max_ops; /* EC consecutive operations limit */
Rich Evans85b05ec2015-02-12 11:37:29 +0000448 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
449 int renegotiation; /* enable / disable renegotiation */
450 int allow_legacy; /* allow legacy renegotiation */
451 int renegotiate; /* attempt renegotiation? */
452 int renego_delay; /* delay before enforcing renegotiation */
453 int exchanges; /* number of data exchanges */
454 int min_version; /* minimum protocol version accepted */
455 int max_version; /* maximum protocol version accepted */
456 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200457 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000458 int auth_mode; /* verify mode for connection */
459 unsigned char mfl_code; /* code for maximum fragment length */
460 int trunc_hmac; /* negotiate truncated hmac or not */
461 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200462 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000463 int reconnect; /* attempt to resume session */
464 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200465 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000466 int tickets; /* enable / disable session tickets */
Hanno Beckere6706e62017-05-15 16:05:15 +0100467 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000468 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000469 int transport; /* TLS or DTLS? */
470 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
471 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200472 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Rich Evans85b05ec2015-02-12 11:37:29 +0000473 int fallback; /* is this a fallback connection? */
Hanno Becker4d615912018-08-14 13:33:30 +0100474 int dgram_packing; /* allow/forbid datagram packing */
Rich Evans85b05ec2015-02-12 11:37:29 +0000475 int extended_ms; /* negotiate extended master secret? */
476 int etm; /* negotiate encrypt then mac? */
Hanno Beckerbb425db2019-04-03 12:59:58 +0100477 int context_crt_cb; /* use context-specific CRT verify callback */
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300478 int eap_tls; /* derive EAP-TLS keying material? */
Hanno Becker90cb3592019-04-09 17:24:19 +0100479 int cid_enabled; /* whether to use the CID extension or not */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100480 int cid_enabled_renego; /* whether to use the CID extension or not
481 * during renegotiation */
Hanno Becker90cb3592019-04-09 17:24:19 +0100482 const char *cid_val; /* the CID to use for incoming messages */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100483 const char *cid_val_renego; /* the CID to use for incoming messages
484 * after renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000485} opt;
486
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100487int query_config( const char *config );
488
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300489#if defined(MBEDTLS_SSL_EXPORT_KEYS)
490typedef struct eap_tls_keys
491{
492 unsigned char master_secret[48];
493 unsigned char randbytes[64];
Ron Eldor51d3ab52019-05-12 14:54:30 +0300494 mbedtls_tls_prf_types tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300495} eap_tls_keys;
496
497static int eap_tls_key_derivation ( void *p_expkey,
498 const unsigned char *ms,
499 const unsigned char *kb,
500 size_t maclen,
501 size_t keylen,
502 size_t ivlen,
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300503 unsigned char client_random[32],
Ron Eldor51d3ab52019-05-12 14:54:30 +0300504 unsigned char server_random[32],
505 mbedtls_tls_prf_types tls_prf_type )
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300506{
507 eap_tls_keys *keys = (eap_tls_keys *)p_expkey;
508
509 ( ( void ) kb );
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300510 memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) );
511 memcpy( keys->randbytes, client_random, 32 );
512 memcpy( keys->randbytes + 32, server_random, 32 );
Ron Eldor51d3ab52019-05-12 14:54:30 +0300513 keys->tls_prf_type = tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300514
Ron Eldorf75e2522019-05-14 20:38:49 +0300515 if( opt.debug_level > 2 )
516 {
Ron Eldor801faf02019-05-15 17:45:24 +0300517 mbedtls_printf("exported maclen is %u\n", (unsigned)maclen);
518 mbedtls_printf("exported keylen is %u\n", (unsigned)keylen);
519 mbedtls_printf("exported ivlen is %u\n", (unsigned)ivlen);
Ron Eldorf75e2522019-05-14 20:38:49 +0300520 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300521 return( 0 );
522}
523#endif
524
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200525static void my_debug( void *ctx, int level,
526 const char *file, int line,
527 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000528{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200529 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000530
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200531 /* Extract basename from file */
532 for( p = basename = file; *p != '\0'; p++ )
533 if( *p == '/' || *p == '\\' )
534 basename = p + 1;
535
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100536 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s",
537 basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000538 fflush( (FILE *) ctx );
539}
540
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200541#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000542int ca_callback( void *data, mbedtls_x509_crt const *child,
Janos Follathd7ecbd62019-04-05 14:52:17 +0100543 mbedtls_x509_crt **candidates )
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200544{
Hanno Beckercbb59032019-03-28 14:14:22 +0000545 int ret = 0;
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200546 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +0000547 mbedtls_x509_crt *first;
548
549 /* This is a test-only implementation of the CA callback
550 * which always returns the entire list of trusted certificates.
551 * Production implementations managing a large number of CAs
552 * should use an efficient presentation and lookup for the
553 * set of trusted certificates (such as a hashtable) and only
554 * return those trusted certificates which satisfy basic
555 * parental checks, such as the matching of child `Issuer`
Jarno Lamsaf7a7f9e2019-04-01 15:11:54 +0300556 * and parent `Subject` field or matching key identifiers. */
Hanno Beckercbb59032019-03-28 14:14:22 +0000557 ((void) child);
558
559 first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
560 if( first == NULL )
561 {
562 ret = -1;
563 goto exit;
564 }
565 mbedtls_x509_crt_init( first );
566
567 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
568 {
569 ret = -1;
570 goto exit;
571 }
572
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200573 while( ca->next != NULL )
574 {
575 ca = ca->next;
Hanno Beckercbb59032019-03-28 14:14:22 +0000576 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
577 {
578 ret = -1;
579 goto exit;
580 }
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200581 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000582
583exit:
584
585 if( ret != 0 )
586 {
587 mbedtls_x509_crt_free( first );
588 mbedtls_free( first );
589 first = NULL;
590 }
591
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200592 *candidates = first;
Hanno Beckercbb59032019-03-28 14:14:22 +0000593 return( ret );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200594}
595#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
596
Rich Evans85b05ec2015-02-12 11:37:29 +0000597/*
598 * Test recv/send functions that make sure each try returns
599 * WANT_READ/WANT_WRITE at least once before sucesseding
600 */
601static int my_recv( void *ctx, unsigned char *buf, size_t len )
602{
603 static int first_try = 1;
604 int ret;
605
606 if( first_try )
607 {
608 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100609 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000610 }
611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100613 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000614 first_try = 1; /* Next call will be a new operation */
615 return( ret );
616}
617
618static int my_send( void *ctx, const unsigned char *buf, size_t len )
619{
620 static int first_try = 1;
621 int ret;
622
623 if( first_try )
624 {
625 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100626 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000627 }
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100630 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000631 first_try = 1; /* Next call will be a new operation */
632 return( ret );
633}
634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckera1051b42019-02-26 11:38:29 +0000636static unsigned char peer_crt_info[1024];
Hanno Beckera9766c2c2019-02-25 17:43:18 +0000637
Rich Evans85b05ec2015-02-12 11:37:29 +0000638/*
639 * Enabled if debug_level > 1 in code below
640 */
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100641static int my_verify( void *data, mbedtls_x509_crt *crt,
642 int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000643{
644 char buf[1024];
645 ((void) data);
646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Hanno Beckera9766c2c2019-02-25 17:43:18 +0000648 if( depth == 0 )
649 memcpy( peer_crt_info, buf, sizeof( buf ) );
650
651 if( opt.debug_level == 0 )
652 return( 0 );
653
654 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000656
Rich Evans85b05ec2015-02-12 11:37:29 +0000657 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100659 else
660 {
661 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
662 mbedtls_printf( "%s\n", buf );
663 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000664
665 return( 0 );
666}
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200667
668static int ssl_sig_hashes_for_test[] = {
669#if defined(MBEDTLS_SHA512_C)
670 MBEDTLS_MD_SHA512,
671 MBEDTLS_MD_SHA384,
672#endif
673#if defined(MBEDTLS_SHA256_C)
674 MBEDTLS_MD_SHA256,
675 MBEDTLS_MD_SHA224,
676#endif
677#if defined(MBEDTLS_SHA1_C)
678 /* Allow SHA-1 as we use it extensively in tests. */
679 MBEDTLS_MD_SHA1,
680#endif
681 MBEDTLS_MD_NONE
682};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000684
Hanno Becker16970d22017-10-10 15:56:37 +0100685/*
686 * Wait for an event from the underlying transport or the timer
687 * (Used in event-driven IO mode).
688 */
689#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000690int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000691 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100692#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000693int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000694 mbedtls_timing_delay_context *timer,
695 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100696#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000697{
Hanno Becker16970d22017-10-10 15:56:37 +0100698
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000699 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +0100700 int poll_type = 0;
701
702 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
703 poll_type = MBEDTLS_NET_POLL_WRITE;
704 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
705 poll_type = MBEDTLS_NET_POLL_READ;
706#if !defined(MBEDTLS_TIMING_C)
707 else
Hanno Beckeref527962018-03-15 15:49:24 +0000708 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100709#endif
710
711 while( 1 )
712 {
Hanno Becker197a91c2017-10-31 10:58:53 +0000713 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +0100714#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +0000715 if( timer != NULL &&
716 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +0100717 {
Hanno Becker16970d22017-10-10 15:56:37 +0100718 break;
719 }
Hanno Becker197a91c2017-10-31 10:58:53 +0000720#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +0100721
Hanno Becker197a91c2017-10-31 10:58:53 +0000722 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000723 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +0100724 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000725 ret = mbedtls_net_poll( fd, poll_type, 0 );
726 if( ret < 0 )
727 return( ret );
728 if( ret == poll_type )
729 break;
Hanno Becker16970d22017-10-10 15:56:37 +0100730 }
731 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000732
733 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100734}
735
Hanno Becker1f583ee2019-04-09 17:12:56 +0100736/* Unhexify `hex` into `dst`. `dst` must have
737 * size at least `strlen( hex ) / 2`. */
Hanno Becker90cb3592019-04-09 17:24:19 +0100738int unhexify( char const *hex, unsigned char *dst )
Hanno Becker1f583ee2019-04-09 17:12:56 +0100739{
740 unsigned char c;
741 size_t j;
742 size_t len = strlen( hex );
743
744 if( len % 2 != 0 )
745 return( -1 );
746
747 for( j = 0; j < len; j += 2 )
748 {
749 c = hex[j];
750 if( c >= '0' && c <= '9' )
751 c -= '0';
752 else if( c >= 'a' && c <= 'f' )
753 c -= 'a' - 10;
754 else if( c >= 'A' && c <= 'F' )
755 c -= 'A' - 10;
756 else
757 return( -1 );
758 dst[ j / 2 ] = c << 4;
759
760 c = hex[j + 1];
761 if( c >= '0' && c <= '9' )
762 c -= '0';
763 else if( c >= 'a' && c <= 'f' )
764 c -= 'a' - 10;
765 else if( c >= 'A' && c <= 'F' )
766 c -= 'A' - 10;
767 else
768 return( -1 );
769 dst[ j / 2 ] |= c;
770 }
771
772 return( 0 );
773}
774
Hanno Beckera0e20d02019-05-15 14:03:01 +0100775#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100776int report_cid_usage( mbedtls_ssl_context *ssl,
777 const char *additional_description )
778{
779 int ret;
780 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
781 size_t peer_cid_len;
782 int cid_negotiated;
783
784 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
785 return( 0 );
786
787 /* Check if the use of a CID has been negotiated */
788 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
789 peer_cid, &peer_cid_len );
790 if( ret != 0 )
791 {
792 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
793 -ret );
794 return( ret );
795 }
796
797 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
798 {
799 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
800 {
801 mbedtls_printf( "(%s) Use of Connection ID was rejected by the server.\n",
802 additional_description );
803 }
804 }
805 else
806 {
807 size_t idx=0;
808 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
809 additional_description );
810 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
811 additional_description,
812 (unsigned) peer_cid_len );
813 while( idx < peer_cid_len )
814 {
815 mbedtls_printf( "%02x ", peer_cid[ idx ] );
816 idx++;
817 }
818 mbedtls_printf( "\n" );
819 }
820
821 return( 0 );
822}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100823#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100824
Paul Bakker9caf2d22010-02-18 19:37:19 +0000825int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000826{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200827 int ret = 0, len, tail_len, i, written, frags, retry_left;
828 mbedtls_net_context server_fd;
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100829
830 unsigned char buf[MAX_REQUEST_SIZE + 1];
831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
833 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200834 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200835#endif
Hanno Becker90cb3592019-04-09 17:24:19 +0100836
Hanno Beckera0e20d02019-05-15 14:03:01 +0100837#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +0100838 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100839 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker90cb3592019-04-09 17:24:19 +0100840 size_t cid_len = 0;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100841 size_t cid_renego_len = 0;
Hanno Becker90cb3592019-04-09 17:24:19 +0100842#endif
843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100845 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200846#endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100847#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +0100848 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100849 const mbedtls_ecp_curve_info *curve_cur;
850#endif
851
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200852 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000853
Hanno Beckere86964c2018-10-23 11:37:50 +0100854#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500855 psa_key_handle_t slot = 0;
Hanno Beckere86964c2018-10-23 11:37:50 +0100856 psa_algorithm_t alg = 0;
857 psa_key_policy_t policy;
858 psa_status_t status;
859#endif
860
Gilles Peskineef86ab22017-05-05 18:59:02 +0200861#if defined(MBEDTLS_X509_CRT_PARSE_C)
862 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
863#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 mbedtls_entropy_context entropy;
865 mbedtls_ctr_drbg_context ctr_drbg;
866 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200867 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200869#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200870 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200871#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200873 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 mbedtls_x509_crt cacert;
875 mbedtls_x509_crt clicert;
876 mbedtls_pk_context pkey;
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +0100877#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500878 psa_key_handle_t key_slot = 0; /* invalid key slot */
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +0100879#endif
Paul Bakkered27a042013-04-18 22:46:23 +0200880#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000881 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000882 const int *list;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300883#if defined(MBEDTLS_SSL_EXPORT_KEYS)
884 unsigned char eap_tls_keymaterial[16];
885 unsigned char eap_tls_iv[8];
886 const char* eap_tls_label = "client EAP encryption";
887 eap_tls_keys eap_tls_keying;
888#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000889
Paul Bakker1a207ec2011-02-06 13:22:40 +0000890 /*
891 * Make sure memory references are valid.
892 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200893 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200894 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200895 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200897 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898#if defined(MBEDTLS_X509_CRT_PARSE_C)
899 mbedtls_x509_crt_init( &cacert );
900 mbedtls_x509_crt_init( &clicert );
901 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200902#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200904 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200905#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000906
Hanno Beckerb2b468b2018-11-12 17:46:59 +0000907#if defined(MBEDTLS_USE_PSA_CRYPTO)
908 status = psa_crypto_init();
909 if( status != PSA_SUCCESS )
910 {
911 mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
912 (int) status );
913 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
914 goto exit;
915 }
916#endif
917
Paul Bakker9caf2d22010-02-18 19:37:19 +0000918 if( argc == 0 )
919 {
920 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000921 if( ret == 0 )
922 ret = 1;
923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000927 while( *list )
928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200930 list++;
931 if( !*list )
932 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000934 list++;
935 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000937 goto exit;
938 }
939
940 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100941 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000942 opt.server_port = DFL_SERVER_PORT;
943 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker90cb3592019-04-09 17:24:19 +0100944 opt.cid_enabled = DFL_CID_ENABLED;
945 opt.cid_val = DFL_CID_VALUE;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100946 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
947 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100948 opt.nbio = DFL_NBIO;
Hanno Becker16970d22017-10-10 15:56:37 +0100949 opt.event = DFL_EVENT;
Hanno Beckerbb425db2019-04-03 12:59:58 +0100950 opt.context_crt_cb = DFL_CONTEXT_CRT_CB;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200951 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200952 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000953 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200954 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000955 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000956 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000957 opt.crt_file = DFL_CRT_FILE;
958 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100959 opt.key_opaque = DFL_KEY_OPAQUE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200960 opt.psk = DFL_PSK;
Hanno Beckere86964c2018-10-23 11:37:50 +0100961#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000962 opt.psk_opaque = DFL_PSK_OPAQUE;
Hanno Beckere86964c2018-10-23 11:37:50 +0100963#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200964#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
965 opt.ca_callback = DFL_CA_CALLBACK;
966#endif
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200967 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200968 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200969 opt.ec_max_ops = DFL_EC_MAX_OPS;
Paul Bakker51936882011-02-20 16:05:58 +0000970 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000971 opt.renegotiation = DFL_RENEGOTIATION;
972 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100973 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200974 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000975 opt.min_version = DFL_MIN_VERSION;
976 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100977 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +0200978 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100979 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200980 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200981 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100982 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200983 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200984 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100985 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200986 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200987 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200988 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +0100989 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200990 opt.transport = DFL_TRANSPORT;
991 opt.hs_to_min = DFL_HS_TO_MIN;
992 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200993 opt.dtls_mtu = DFL_DTLS_MTU;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200994 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200995 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100996 opt.etm = DFL_ETM;
Hanno Becker4d615912018-08-14 13:33:30 +0100997 opt.dgram_packing = DFL_DGRAM_PACKING;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300998 opt.eap_tls = DFL_EAP_TLS;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000999
1000 for( i = 1; i < argc; i++ )
1001 {
Paul Bakker9caf2d22010-02-18 19:37:19 +00001002 p = argv[i];
1003 if( ( q = strchr( p, '=' ) ) == NULL )
1004 goto usage;
1005 *q++ = '\0';
1006
1007 if( strcmp( p, "server_name" ) == 0 )
1008 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001009 else if( strcmp( p, "server_addr" ) == 0 )
1010 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001011 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001012 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001013 else if( strcmp( p, "dtls" ) == 0 )
1014 {
1015 int t = atoi( q );
1016 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001018 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001020 else
1021 goto usage;
1022 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001023 else if( strcmp( p, "debug_level" ) == 0 )
1024 {
1025 opt.debug_level = atoi( q );
1026 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1027 goto usage;
1028 }
Hanno Beckerbb425db2019-04-03 12:59:58 +01001029 else if( strcmp( p, "context_crt_cb" ) == 0 )
1030 {
1031 opt.context_crt_cb = atoi( q );
1032 if( opt.context_crt_cb != 0 && opt.context_crt_cb != 1 )
1033 goto usage;
1034 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001035 else if( strcmp( p, "nbio" ) == 0 )
1036 {
1037 opt.nbio = atoi( q );
1038 if( opt.nbio < 0 || opt.nbio > 2 )
1039 goto usage;
1040 }
Hanno Becker16970d22017-10-10 15:56:37 +01001041 else if( strcmp( p, "event" ) == 0 )
1042 {
1043 opt.event = atoi( q );
1044 if( opt.event < 0 || opt.event > 2 )
1045 goto usage;
1046 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001047 else if( strcmp( p, "read_timeout" ) == 0 )
1048 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001049 else if( strcmp( p, "max_resend" ) == 0 )
1050 {
1051 opt.max_resend = atoi( q );
1052 if( opt.max_resend < 0 )
1053 goto usage;
1054 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001055 else if( strcmp( p, "request_page" ) == 0 )
1056 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +02001057 else if( strcmp( p, "request_size" ) == 0 )
1058 {
1059 opt.request_size = atoi( q );
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001060 if( opt.request_size < 0 ||
1061 opt.request_size > MAX_REQUEST_SIZE )
Paul Bakker93c32b22014-04-25 13:40:05 +02001062 goto usage;
1063 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001064 else if( strcmp( p, "ca_file" ) == 0 )
1065 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +00001066 else if( strcmp( p, "ca_path" ) == 0 )
1067 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +00001068 else if( strcmp( p, "crt_file" ) == 0 )
1069 opt.crt_file = q;
1070 else if( strcmp( p, "key_file" ) == 0 )
1071 opt.key_file = q;
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001072#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
1073 else if( strcmp( p, "key_opaque" ) == 0 )
1074 opt.key_opaque = atoi( q );
1075#endif
Hanno Beckera0e20d02019-05-15 14:03:01 +01001076#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +01001077 else if( strcmp( p, "cid" ) == 0 )
1078 {
1079 opt.cid_enabled = atoi( q );
1080 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
1081 goto usage;
1082 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001083 else if( strcmp( p, "cid_renego" ) == 0 )
1084 {
1085 opt.cid_enabled_renego = atoi( q );
1086 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
1087 goto usage;
1088 }
Hanno Becker90cb3592019-04-09 17:24:19 +01001089 else if( strcmp( p, "cid_val" ) == 0 )
1090 {
1091 opt.cid_val = q;
1092 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001093 else if( strcmp( p, "cid_val_renego" ) == 0 )
1094 {
1095 opt.cid_val_renego = q;
1096 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001097#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001098 else if( strcmp( p, "psk" ) == 0 )
1099 opt.psk = q;
Hanno Beckere86964c2018-10-23 11:37:50 +01001100#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001101 else if( strcmp( p, "psk_opaque" ) == 0 )
1102 opt.psk_opaque = atoi( q );
Hanno Beckere86964c2018-10-23 11:37:50 +01001103#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001104#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1105 else if( strcmp( p, "ca_callback" ) == 0)
1106 opt.ca_callback = atoi( q );
1107#endif
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001108 else if( strcmp( p, "psk_identity" ) == 0 )
1109 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001110 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1111 opt.ecjpake_pw = q;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001112 else if( strcmp( p, "ec_max_ops" ) == 0 )
1113 opt.ec_max_ops = atoi( q );
Paul Bakker51936882011-02-20 16:05:58 +00001114 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +00001117
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001118 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +00001119 {
1120 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +00001121 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +00001122 }
Paul Bakker51936882011-02-20 16:05:58 +00001123 opt.force_ciphersuite[1] = 0;
1124 }
Paul Bakker48916f92012-09-16 19:57:18 +00001125 else if( strcmp( p, "renegotiation" ) == 0 )
1126 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001127 opt.renegotiation = (atoi( q )) ?
1128 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1129 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +00001130 }
1131 else if( strcmp( p, "allow_legacy" ) == 0 )
1132 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001133 switch( atoi( q ) )
1134 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001135 case -1:
1136 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1137 break;
1138 case 0:
1139 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1140 break;
1141 case 1:
1142 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1143 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001144 default: goto usage;
1145 }
Paul Bakker48916f92012-09-16 19:57:18 +00001146 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001147 else if( strcmp( p, "renegotiate" ) == 0 )
1148 {
1149 opt.renegotiate = atoi( q );
1150 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1151 goto usage;
1152 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001153 else if( strcmp( p, "exchanges" ) == 0 )
1154 {
1155 opt.exchanges = atoi( q );
1156 if( opt.exchanges < 1 )
1157 goto usage;
1158 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001159 else if( strcmp( p, "reconnect" ) == 0 )
1160 {
1161 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001162 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001163 goto usage;
1164 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001165 else if( strcmp( p, "reco_delay" ) == 0 )
1166 {
1167 opt.reco_delay = atoi( q );
1168 if( opt.reco_delay < 0 )
1169 goto usage;
1170 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001171 else if( strcmp( p, "reconnect_hard" ) == 0 )
1172 {
1173 opt.reconnect_hard = atoi( q );
1174 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
1175 goto usage;
1176 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001177 else if( strcmp( p, "tickets" ) == 0 )
1178 {
1179 opt.tickets = atoi( q );
1180 if( opt.tickets < 0 || opt.tickets > 2 )
1181 goto usage;
1182 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001183 else if( strcmp( p, "alpn" ) == 0 )
1184 {
1185 opt.alpn_string = q;
1186 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001187 else if( strcmp( p, "fallback" ) == 0 )
1188 {
1189 switch( atoi( q ) )
1190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
1192 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001193 default: goto usage;
1194 }
1195 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001196 else if( strcmp( p, "extended_ms" ) == 0 )
1197 {
1198 switch( atoi( q ) )
1199 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001200 case 0:
1201 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1202 break;
1203 case 1:
1204 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1205 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001206 default: goto usage;
1207 }
1208 }
Hanno Beckere6706e62017-05-15 16:05:15 +01001209 else if( strcmp( p, "curves" ) == 0 )
1210 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001211 else if( strcmp( p, "etm" ) == 0 )
1212 {
1213 switch( atoi( q ) )
1214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1216 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001217 default: goto usage;
1218 }
1219 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001220 else if( strcmp( p, "min_version" ) == 0 )
1221 {
1222 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001224 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001226 else if( strcmp( q, "tls1_1" ) == 0 ||
1227 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001229 else if( strcmp( q, "tls1_2" ) == 0 ||
1230 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001232 else
1233 goto usage;
1234 }
1235 else if( strcmp( p, "max_version" ) == 0 )
1236 {
1237 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001239 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001241 else if( strcmp( q, "tls1_1" ) == 0 ||
1242 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001244 else if( strcmp( q, "tls1_2" ) == 0 ||
1245 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001247 else
1248 goto usage;
1249 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001250 else if( strcmp( p, "arc4" ) == 0 )
1251 {
1252 switch( atoi( q ) )
1253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1255 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001256 default: goto usage;
1257 }
1258 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02001259 else if( strcmp( p, "allow_sha1" ) == 0 )
1260 {
1261 switch( atoi( q ) )
1262 {
1263 case 0: opt.allow_sha1 = 0; break;
1264 case 1: opt.allow_sha1 = 1; break;
1265 default: goto usage;
1266 }
1267 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001268 else if( strcmp( p, "force_version" ) == 0 )
1269 {
1270 if( strcmp( q, "ssl3" ) == 0 )
1271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1273 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001274 }
1275 else if( strcmp( q, "tls1" ) == 0 )
1276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1278 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001279 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001280 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1283 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001284 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001285 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1288 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001289 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001290 else if( strcmp( q, "dtls1" ) == 0 )
1291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1293 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1294 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001295 }
1296 else if( strcmp( q, "dtls1_2" ) == 0 )
1297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1299 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1300 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001301 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001302 else
1303 goto usage;
1304 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001305 else if( strcmp( p, "auth_mode" ) == 0 )
1306 {
1307 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001309 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001311 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001313 else
1314 goto usage;
1315 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001316 else if( strcmp( p, "max_frag_len" ) == 0 )
1317 {
1318 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001320 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001322 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001324 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001326 else
1327 goto usage;
1328 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001329 else if( strcmp( p, "trunc_hmac" ) == 0 )
1330 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001331 switch( atoi( q ) )
1332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1334 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001335 default: goto usage;
1336 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001337 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001338 else if( strcmp( p, "hs_timeout" ) == 0 )
1339 {
1340 if( ( p = strchr( q, '-' ) ) == NULL )
1341 goto usage;
1342 *p++ = '\0';
1343 opt.hs_to_min = atoi( q );
1344 opt.hs_to_max = atoi( p );
1345 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1346 goto usage;
1347 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001348 else if( strcmp( p, "mtu" ) == 0 )
1349 {
1350 opt.dtls_mtu = atoi( q );
1351 if( opt.dtls_mtu < 0 )
1352 goto usage;
1353 }
Hanno Becker4d615912018-08-14 13:33:30 +01001354 else if( strcmp( p, "dgram_packing" ) == 0 )
1355 {
1356 opt.dgram_packing = atoi( q );
1357 if( opt.dgram_packing != 0 &&
1358 opt.dgram_packing != 1 )
1359 {
1360 goto usage;
1361 }
1362 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001363 else if( strcmp( p, "recsplit" ) == 0 )
1364 {
1365 opt.recsplit = atoi( q );
1366 if( opt.recsplit < 0 || opt.recsplit > 1 )
1367 goto usage;
1368 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001369 else if( strcmp( p, "dhmlen" ) == 0 )
1370 {
1371 opt.dhmlen = atoi( q );
1372 if( opt.dhmlen < 0 )
1373 goto usage;
1374 }
Andres Amaya Garciabc818842018-10-16 21:08:38 +01001375 else if( strcmp( p, "query_config" ) == 0 )
1376 {
1377 return query_config( q );
1378 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001379 else if( strcmp( p, "eap_tls" ) == 0 )
1380 {
1381 opt.eap_tls = atoi( q );
1382 if( opt.eap_tls < 0 || opt.eap_tls > 1 )
1383 goto usage;
1384 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001385 else
1386 goto usage;
1387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001388
Hanno Becker16970d22017-10-10 15:56:37 +01001389 /* Event-driven IO is incompatible with the above custom
1390 * receive and send functions, as the polling builds on
1391 * refers to the underlying net_context. */
1392 if( opt.event == 1 && opt.nbio != 1 )
1393 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001394 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001395 opt.nbio = 1;
1396 }
1397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398#if defined(MBEDTLS_DEBUG_C)
1399 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001400#endif
1401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001404 * Unhexify the pre-shared key if any is given
1405 */
1406 if( strlen( opt.psk ) )
1407 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001408 psk_len = strlen( opt.psk ) / 2;
1409 if( psk_len > sizeof( psk ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001410 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001411 mbedtls_printf( "pre-shared key too long\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001412 goto exit;
1413 }
1414
Hanno Becker1f583ee2019-04-09 17:12:56 +01001415 if( unhexify( opt.psk, psk ) != 0 )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001416 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001417 mbedtls_printf( "pre-shared key not valid hex\n" );
1418 goto exit;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001419 }
1420 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001422
Hanno Beckere86964c2018-10-23 11:37:50 +01001423
1424#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001425 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001426 {
1427 if( opt.psk == NULL )
1428 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00001429 mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
Hanno Beckere86964c2018-10-23 11:37:50 +01001430 ret = 2;
1431 goto usage;
1432 }
1433
1434 if( opt.force_ciphersuite[0] <= 0 )
1435 {
1436 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" );
1437 ret = 2;
1438 goto usage;
1439 }
1440 }
1441#endif /* MBEDTLS_USE_PSA_CRYPTO */
1442
1443 if( opt.force_ciphersuite[0] > 0 )
1444 {
1445 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1446 ciphersuite_info =
1447 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1448
1449 if( opt.max_version != -1 &&
1450 ciphersuite_info->min_minor_ver > opt.max_version )
1451 {
1452 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
1453 ret = 2;
1454 goto usage;
1455 }
1456 if( opt.min_version != -1 &&
1457 ciphersuite_info->max_minor_ver < opt.min_version )
1458 {
1459 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
1460 ret = 2;
1461 goto usage;
1462 }
1463
1464 /* If the server selects a version that's not supported by
1465 * this suite, then there will be no common ciphersuite... */
1466 if( opt.max_version == -1 ||
1467 opt.max_version > ciphersuite_info->max_minor_ver )
1468 {
1469 opt.max_version = ciphersuite_info->max_minor_ver;
1470 }
1471 if( opt.min_version < ciphersuite_info->min_minor_ver )
1472 {
1473 opt.min_version = ciphersuite_info->min_minor_ver;
1474 /* DTLS starts with TLS 1.1 */
1475 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1476 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1477 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1478 }
1479
1480 /* Enable RC4 if needed and not explicitly disabled */
1481 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
1482 {
1483 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
1484 {
1485 mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" );
1486 ret = 2;
1487 goto usage;
1488 }
1489
1490 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
1491 }
1492
Hanno Becker90cb3592019-04-09 17:24:19 +01001493
Hanno Beckere86964c2018-10-23 11:37:50 +01001494#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001495 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001496 {
1497 /* Ensure that the chosen ciphersuite is PSK-only; we must know
1498 * the ciphersuite in advance to set the correct policy for the
1499 * PSK key slot. This limitation might go away in the future. */
1500 if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
1501 opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 )
1502 {
1503 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" );
1504 ret = 2;
1505 goto usage;
1506 }
1507
1508 /* Determine KDF algorithm the opaque PSK will be used in. */
1509#if defined(MBEDTLS_SHA512_C)
1510 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1511 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1512 else
1513#endif /* MBEDTLS_SHA512_C */
1514 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1515 }
1516#endif /* MBEDTLS_USE_PSA_CRYPTO */
1517 }
1518
Hanno Beckera0e20d02019-05-15 14:03:01 +01001519#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001520 cid_len = strlen( opt.cid_val ) / 2;
1521 if( cid_len > sizeof( cid ) )
1522 {
1523 mbedtls_printf( "CID too long\n" );
1524 goto exit;
1525 }
Hanno Becker90cb3592019-04-09 17:24:19 +01001526
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001527 if( unhexify( opt.cid_val, cid ) != 0 )
1528 {
1529 mbedtls_printf( "CID not valid hex\n" );
1530 goto exit;
1531 }
1532
1533 /* Keep CID settings for renegotiation unless
1534 * specified otherwise. */
1535 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
1536 opt.cid_enabled_renego = opt.cid_enabled;
1537 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
1538 opt.cid_val_renego = opt.cid_val;
1539
1540 cid_renego_len = strlen( opt.cid_val_renego ) / 2;
1541 if( cid_renego_len > sizeof( cid_renego ) )
1542 {
1543 mbedtls_printf( "CID too long\n" );
1544 goto exit;
1545 }
1546
1547 if( unhexify( opt.cid_val_renego, cid_renego ) != 0 )
1548 {
1549 mbedtls_printf( "CID not valid hex\n" );
1550 goto exit;
1551 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001552#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01001553
Hanno Beckere6706e62017-05-15 16:05:15 +01001554#if defined(MBEDTLS_ECP_C)
1555 if( opt.curves != NULL )
1556 {
1557 p = (char *) opt.curves;
1558 i = 0;
1559
1560 if( strcmp( p, "none" ) == 0 )
1561 {
1562 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1563 }
1564 else if( strcmp( p, "default" ) != 0 )
1565 {
1566 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001567 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001568 {
1569 q = p;
1570
1571 /* Terminate the current string */
1572 while( *p != ',' && *p != '\0' )
1573 p++;
1574 if( *p == ',' )
1575 *p++ = '\0';
1576
1577 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1578 {
1579 curve_list[i++] = curve_cur->grp_id;
1580 }
1581 else
1582 {
1583 mbedtls_printf( "unknown curve %s\n", q );
1584 mbedtls_printf( "supported curves: " );
1585 for( curve_cur = mbedtls_ecp_curve_list();
1586 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1587 curve_cur++ )
1588 {
1589 mbedtls_printf( "%s ", curve_cur->name );
1590 }
1591 mbedtls_printf( "\n" );
1592 goto exit;
1593 }
1594 }
1595
1596 mbedtls_printf("Number of curves: %d\n", i );
1597
Hanno Becker8651a432017-06-09 16:13:22 +01001598 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001599 {
Hanno Becker8651a432017-06-09 16:13:22 +01001600 mbedtls_printf( "curves list too long, maximum %d",
1601 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001602 goto exit;
1603 }
1604
1605 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1606 }
1607 }
1608#endif /* MBEDTLS_ECP_C */
1609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001611 if( opt.alpn_string != NULL )
1612 {
1613 p = (char *) opt.alpn_string;
1614 i = 0;
1615
1616 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001617 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001618 {
1619 alpn_list[i++] = p;
1620
1621 /* Terminate the current string and move on to next one */
1622 while( *p != ',' && *p != '\0' )
1623 p++;
1624 if( *p == ',' )
1625 *p++ = '\0';
1626 }
1627 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001629
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001630 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001631 * 0. Initialize the RNG and the session data
1632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001634 fflush( stdout );
1635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 mbedtls_entropy_init( &entropy );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001637 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1638 &entropy, (const unsigned char *) pers,
1639 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +00001640 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001641 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
1642 -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001643 goto exit;
1644 }
1645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001649 /*
1650 * 1.1. Load the trusted CA
1651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001653 fflush( stdout );
1654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001656 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001657 if( strcmp( opt.ca_path, "none" ) == 0 )
1658 ret = 0;
1659 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001661 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001662 if( strcmp( opt.ca_file, "none" ) == 0 )
1663 ret = 0;
1664 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001666 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001667#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#if defined(MBEDTLS_CERTS_C)
1669 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 ret = mbedtls_x509_crt_parse( &cacert,
1672 (const unsigned char *) mbedtls_test_cas[i],
1673 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001674 if( ret != 0 )
1675 break;
1676 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001677#else
1678 {
1679 ret = 1;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001680 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001681 }
1682#endif
Paul Bakker42488232012-05-16 08:21:05 +00001683 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001684 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001685 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1686 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001687 goto exit;
1688 }
1689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001691
1692 /*
1693 * 1.2. Load own certificate and private key
1694 *
1695 * (can be skipped if client authentication is not required)
1696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001698 fflush( stdout );
1699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001701 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001702 if( strcmp( opt.crt_file, "none" ) == 0 )
1703 ret = 0;
1704 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001706 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001707#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001709 ret = mbedtls_x509_crt_parse( &clicert,
1710 (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001712#else
1713 {
1714 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001716 }
1717#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001718 if( ret != 0 )
1719 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001720 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1721 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001722 goto exit;
1723 }
1724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001726 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001727 if( strcmp( opt.key_file, "none" ) == 0 )
1728 ret = 0;
1729 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001731 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001732#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001734 ret = mbedtls_pk_parse_key( &pkey,
1735 (const unsigned char *) mbedtls_test_cli_key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001737#else
1738 {
1739 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001741 }
1742#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001743 if( ret != 0 )
1744 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001745 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
1746 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001747 goto exit;
1748 }
1749
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001750#if defined(MBEDTLS_USE_PSA_CRYPTO)
1751 if( opt.key_opaque != 0 )
1752 {
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001753 if( ( ret = mbedtls_pk_wrap_as_opaque( &pkey, &key_slot,
1754 PSA_ALG_SHA_256 ) ) != 0 )
1755 {
1756 mbedtls_printf( " failed\n ! "
1757 "mbedtls_pk_wrap_as_opaque returned -0x%x\n\n", -ret );
1758 goto exit;
1759 }
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001760 }
1761#endif /* MBEDTLS_USE_PSA_CRYPTO */
1762
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001763 mbedtls_printf( " ok (key type: %s)\n", mbedtls_pk_get_name( &pkey ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
1766 /*
1767 * 2. Start the connection
1768 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001769 if( opt.server_addr == NULL)
1770 opt.server_addr = opt.server_name;
1771
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001772 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001774 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001775 fflush( stdout );
1776
Hanno Becker16970d22017-10-10 15:56:37 +01001777 if( ( ret = mbedtls_net_connect( &server_fd,
1778 opt.server_addr, opt.server_port,
1779 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1780 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001781 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001782 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
1783 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001784 goto exit;
1785 }
1786
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001787 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001788 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001789 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001790 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001791 if( ret != 0 )
1792 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001793 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
1794 -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001795 goto exit;
1796 }
1797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001799
1800 /*
1801 * 3. Setup stuff
1802 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001804 fflush( stdout );
1805
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001806 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1807 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001808 opt.transport,
1809 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001810 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001811 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
1812 -ret );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001813 goto exit;
1814 }
1815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001817 /* The default algorithms profile disables SHA-1, but our tests still
1818 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001819 if( opt.allow_sha1 > 0 )
1820 {
1821 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1822 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1823 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1824 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001825
Hanno Beckerbb425db2019-04-03 12:59:58 +01001826 if( opt.context_crt_cb == 0 )
1827 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
1828
Hanno Beckera1051b42019-02-26 11:38:29 +00001829 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Gilles Peskineef86ab22017-05-05 18:59:02 +02001830#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001831
Hanno Beckera0e20d02019-05-15 14:03:01 +01001832#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001833 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckerad4a1372019-05-03 13:06:44 +01001834 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001835 if( opt.cid_enabled == 1 &&
1836 opt.cid_enabled_renego == 1 &&
1837 cid_len != cid_renego_len )
1838 {
1839 mbedtls_printf( "CID length must not change during renegotiation\n" );
1840 goto usage;
1841 }
1842
1843
1844 if( opt.cid_enabled == 1 )
Hanno Becker8367ccc2019-05-14 11:30:10 +01001845 ret = mbedtls_ssl_conf_cid( &conf, cid_len,
1846 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001847 else
Hanno Becker8367ccc2019-05-14 11:30:10 +01001848 ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
1849 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001850
Hanno Beckerad4a1372019-05-03 13:06:44 +01001851 if( ret != 0 )
1852 {
1853 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned %d\n\n",
1854 ret );
1855 goto exit;
1856 }
1857 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001858#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerad4a1372019-05-03 13:06:44 +01001859
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001860 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001861 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001864 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Hanno Becker16970d22017-10-10 15:56:37 +01001865 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min,
1866 opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001867
Hanno Becker4d615912018-08-14 13:33:30 +01001868 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01001869 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001873 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001874 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001875 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n",
1876 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001877 goto exit;
1878 }
Paul Bakker05decb22013-08-15 13:33:48 +02001879#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001882 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001883 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001884#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001887 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001888 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001889#endif
1890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001892 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001893 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001894#endif
1895
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001896#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1897 if( opt.eap_tls != 0 )
1898 mbedtls_ssl_conf_export_keys_ext_cb( &conf, eap_tls_key_derivation,
1899 &eap_tls_keying );
1900#endif
1901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001903 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001904 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Hanno Becker16970d22017-10-10 15:56:37 +01001905 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1906 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001907#endif
1908
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001909#if defined(MBEDTLS_DHM_C)
1910 if( opt.dhmlen != DFL_DHMLEN )
1911 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1912#endif
1913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001915 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001916 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001917 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001918 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
1919 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001920 goto exit;
1921 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001922#endif
1923
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001924 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1925 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001926
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001927 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02001930 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02001931#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001932
Paul Bakker645ce3a2012-10-31 12:32:41 +00001933 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001934 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001935
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001936#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001937 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001938 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001939#endif
Paul Bakker51936882011-02-20 16:05:58 +00001940
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001941 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001942 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001944 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001945#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001948 if( strcmp( opt.ca_path, "none" ) != 0 &&
1949 strcmp( opt.ca_file, "none" ) != 0 )
1950 {
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001951#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1952 if( opt.ca_callback != 0 )
Hanno Beckercbb59032019-03-28 14:14:22 +00001953 mbedtls_ssl_conf_ca_cb( &conf, ca_callback, &cacert );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001954 else
1955#endif
1956 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001957 }
1958 if( strcmp( opt.crt_file, "none" ) != 0 &&
1959 strcmp( opt.key_file, "none" ) != 0 )
1960 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001961 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001962 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001963 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
1964 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001965 goto exit;
1966 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001967 }
Paul Bakkered27a042013-04-18 22:46:23 +02001968#endif
1969
Hanno Beckere6706e62017-05-15 16:05:15 +01001970#if defined(MBEDTLS_ECP_C)
1971 if( opt.curves != NULL &&
1972 strcmp( opt.curves, "default" ) != 0 )
1973 {
1974 mbedtls_ssl_conf_curves( &conf, curve_list );
1975 }
1976#endif
1977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckere86964c2018-10-23 11:37:50 +01001979#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001980 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001981 {
1982 /* The algorithm has already been determined earlier. */
Hanno Becker37519ea2019-01-25 14:26:01 +00001983 status = psa_allocate_key( &slot );
Hanno Becker1d911cd2018-11-15 13:06:09 +00001984 if( status != PSA_SUCCESS )
1985 {
1986 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1987 goto exit;
1988 }
Hanno Beckere86964c2018-10-23 11:37:50 +01001989
Hanno Becker13871242019-01-25 14:26:26 +00001990 policy = psa_key_policy_init();
Hanno Beckere86964c2018-10-23 11:37:50 +01001991 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
1992
1993 status = psa_set_key_policy( slot, &policy );
1994 if( status != PSA_SUCCESS )
1995 {
1996 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1997 goto exit;
1998 }
1999
2000 status = psa_import_key( slot, PSA_KEY_TYPE_DERIVE, psk, psk_len );
2001 if( status != PSA_SUCCESS )
2002 {
2003 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2004 goto exit;
2005 }
2006
2007 if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, slot,
2008 (const unsigned char *) opt.psk_identity,
Hanno Becker5cd607b2018-11-05 12:52:42 +00002009 strlen( opt.psk_identity ) ) ) != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01002010 {
2011 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
2012 ret );
2013 goto exit;
2014 }
2015 }
2016 else
2017#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002018 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002019 (const unsigned char *) opt.psk_identity,
2020 strlen( opt.psk_identity ) ) ) != 0 )
2021 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002022 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n",
2023 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002024 goto exit;
2025 }
Hanno Beckere86964c2018-10-23 11:37:50 +01002026#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002027
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002028 if( opt.min_version != DFL_MIN_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01002029 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
2030 opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01002031
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002032 if( opt.max_version != DFL_MAX_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01002033 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
2034 opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00002035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02002037 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002038 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02002039#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002040
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002041 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
2042 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002043 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
2044 -ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002045 goto exit;
2046 }
2047
2048#if defined(MBEDTLS_X509_CRT_PARSE_C)
2049 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
2050 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002051 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
2052 ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002053 goto exit;
2054 }
2055#endif
2056
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002057#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2058 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
2059 {
2060 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
2061 (const unsigned char *) opt.ecjpake_pw,
2062 strlen( opt.ecjpake_pw ) ) ) != 0 )
2063 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002064 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
2065 ret );
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002066 goto exit;
2067 }
2068 }
2069#endif
2070
Hanno Beckerbb425db2019-04-03 12:59:58 +01002071#if defined(MBEDTLS_X509_CRT_PARSE_C)
2072 if( opt.context_crt_cb == 1 )
2073 mbedtls_ssl_set_verify( &ssl, my_verify, NULL );
2074#endif /* MBEDTLS_X509_CRT_PARSE_C */
2075
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002076 if( opt.nbio == 2 )
2077 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
2078 else
Hanno Becker16970d22017-10-10 15:56:37 +01002079 mbedtls_ssl_set_bio( &ssl, &server_fd,
2080 mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02002081 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002082
Hanno Beckera0e20d02019-05-15 14:03:01 +01002083#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +01002084 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2085 {
2086 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
2087 cid, cid_len ) ) != 0 )
2088 {
2089 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2090 ret );
2091 goto exit;
2092 }
2093 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002094#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01002095
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002096#if defined(MBEDTLS_SSL_PROTO_DTLS)
2097 if( opt.dtls_mtu != DFL_DTLS_MTU )
2098 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
2099#endif
2100
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002101#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002102 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
2103 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002104#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002105
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002106#if defined(MBEDTLS_ECP_RESTARTABLE)
2107 if( opt.ec_max_ops != DFL_EC_MAX_OPS )
2108 mbedtls_ecp_set_max_ops( opt.ec_max_ops );
2109#endif
2110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002112
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 /*
2114 * 4. Handshake
2115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 fflush( stdout );
2118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 {
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002121 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2122 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002123 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Paul Bakker40e46942009-01-03 21:51:57 +00002124 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002125 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
2126 -ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2128 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01002129 " Unable to verify the server's certificate. "
2130 "Either it is invalid,\n"
2131 " or you didn't set ca_file or ca_path "
2132 "to an appropriate value.\n"
2133 " Alternatively, you may want to use "
2134 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00002137 }
Hanno Becker16970d22017-10-10 15:56:37 +01002138
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002139#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002140 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2141 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002142#endif
2143
Hanno Becker16970d22017-10-10 15:56:37 +01002144 /* For event-driven IO, wait for socket to become available */
2145 if( opt.event == 1 /* level triggered IO */ )
2146 {
2147#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002148 ret = idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002149#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002150 ret = idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002151#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002152 if( ret != 0 )
2153 goto exit;
Hanno Becker16970d22017-10-10 15:56:37 +01002154 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 }
2156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Hanno Becker16970d22017-10-10 15:56:37 +01002158 mbedtls_ssl_get_version( &ssl ),
2159 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
2162 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002163 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002165
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002166#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2167 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
2168 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
2169#endif
2170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002172 if( opt.alpn_string != NULL )
2173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
2175 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002176 alp ? alp : "(none)" );
2177 }
2178#endif
2179
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002180#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Ron Eldor51d3ab52019-05-12 14:54:30 +03002181 if( opt.eap_tls != 0 )
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002182 {
2183 size_t j = 0;
Ron Eldor51d3ab52019-05-12 14:54:30 +03002184
2185 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type,
2186 eap_tls_keying.master_secret,
2187 sizeof( eap_tls_keying.master_secret ),
2188 eap_tls_label,
2189 eap_tls_keying.randbytes,
2190 sizeof( eap_tls_keying.randbytes ),
2191 eap_tls_keymaterial,
2192 sizeof( eap_tls_keymaterial ) ) )
2193 != 0 )
2194 {
2195 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2196 -ret );
2197 goto exit;
2198 }
2199
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002200 mbedtls_printf( " EAP-TLS key material is:" );
2201 for( j = 0; j < sizeof( eap_tls_keymaterial ); j++ )
2202 {
2203 if( j % 8 == 0 )
2204 mbedtls_printf("\n ");
2205 mbedtls_printf("%02x ", eap_tls_keymaterial[j] );
2206 }
2207 mbedtls_printf("\n");
2208
Ron Eldor51d3ab52019-05-12 14:54:30 +03002209 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type, NULL, 0,
Ron Eldor51c45072019-05-15 17:49:54 +03002210 eap_tls_label,
2211 eap_tls_keying.randbytes,
2212 sizeof( eap_tls_keying.randbytes ),
2213 eap_tls_iv,
2214 sizeof( eap_tls_iv ) ) ) != 0 )
Ron Eldor51d3ab52019-05-12 14:54:30 +03002215 {
2216 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2217 -ret );
2218 goto exit;
2219 }
2220
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002221 mbedtls_printf( " EAP-TLS IV is:" );
2222 for( j = 0; j < sizeof( eap_tls_iv ); j++ )
2223 {
2224 if( j % 8 == 0 )
2225 mbedtls_printf("\n ");
2226 mbedtls_printf("%02x ", eap_tls_iv[j] );
2227 }
2228 mbedtls_printf("\n");
2229 }
2230#endif
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002231 if( opt.reconnect != 0 )
2232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002234 fflush( stdout );
2235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002237 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002238 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
2239 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002240 goto exit;
2241 }
2242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002244 }
2245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 /*
2248 * 5. Verify the server certificate
2249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002251
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002252 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002254 char vrfy_buf[512];
2255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
Hanno Becker16970d22017-10-10 15:56:37 +01002258 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ),
2259 " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00002260
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002261 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 }
2263 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002265
Hanno Beckera9766c2c2019-02-25 17:43:18 +00002266 mbedtls_printf( " . Peer certificate information ...\n" );
2267 mbedtls_printf( "%s\n", peer_crt_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002269
Hanno Beckera0e20d02019-05-15 14:03:01 +01002270#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002271 ret = report_cid_usage( &ssl, "initial handshake" );
2272 if( ret != 0 )
2273 goto exit;
2274
Hanno Becker90cb3592019-04-09 17:24:19 +01002275 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2276 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002277 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
2278 cid_renego,
2279 cid_renego_len ) ) != 0 )
Hanno Becker90cb3592019-04-09 17:24:19 +01002280 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002281 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2282 ret );
2283 return( ret );
Hanno Becker90cb3592019-04-09 17:24:19 +01002284 }
2285 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002286#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01002287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002289 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002290 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002291 /*
2292 * Perform renegotiation (this must be done when the server is waiting
2293 * for input from our side).
2294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002296 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002298 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002299 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002300 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002301 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002302 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002303 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
2304 ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002305 goto exit;
2306 }
Hanno Becker16970d22017-10-10 15:56:37 +01002307
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002308#if defined(MBEDTLS_ECP_RESTARTABLE)
2309 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2310 continue;
2311#endif
2312
Hanno Becker16970d22017-10-10 15:56:37 +01002313 /* For event-driven IO, wait for socket to become available */
2314 if( opt.event == 1 /* level triggered IO */ )
2315 {
2316#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002317 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002318#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002319 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002320#endif
2321 }
2322
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002323 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002325 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002327
Hanno Beckera0e20d02019-05-15 14:03:01 +01002328#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002329 ret = report_cid_usage( &ssl, "after renegotiation" );
2330 if( ret != 0 )
2331 goto exit;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002332#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002333
Paul Bakker5121ce52009-01-03 21:22:43 +00002334 /*
2335 * 6. Write the GET request
2336 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002337 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002338send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002340 fflush( stdout );
2341
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002342 len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST,
2343 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002344 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002345
2346 /* Add padding to GET request to reach opt.request_size in length */
2347 if( opt.request_size != DFL_REQUEST_SIZE &&
2348 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02002349 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002350 memset( buf + len, 'A', opt.request_size - len - tail_len );
2351 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02002352 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002353
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002354 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002355 len += tail_len;
2356
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02002357 /* Truncate if request size is smaller than the "natural" size */
2358 if( opt.request_size != DFL_REQUEST_SIZE &&
2359 len > opt.request_size )
2360 {
2361 len = opt.request_size;
2362
2363 /* Still end with \r\n unless that's really not possible */
2364 if( len >= 2 ) buf[len - 2] = '\r';
2365 if( len >= 1 ) buf[len - 1] = '\n';
2366 }
2367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002369 {
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002370 written = 0;
2371 frags = 0;
2372
2373 do
Paul Bakker5121ce52009-01-03 21:22:43 +00002374 {
Hanno Becker16970d22017-10-10 15:56:37 +01002375 while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002376 len - written ) ) < 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002377 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002378 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002379 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002380 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002381 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002382 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
2383 -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002384 goto exit;
2385 }
Hanno Becker16970d22017-10-10 15:56:37 +01002386
2387 /* For event-driven IO, wait for socket to become available */
2388 if( opt.event == 1 /* level triggered IO */ )
2389 {
2390#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002391 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002392#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002393 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002394#endif
2395 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002396 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002397
2398 frags++;
2399 written += ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002401 while( written < len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002402 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002403 else /* Not stream, so datagram */
2404 {
Hanno Becker16970d22017-10-10 15:56:37 +01002405 while( 1 )
2406 {
2407 ret = mbedtls_ssl_write( &ssl, buf, len );
2408
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002409#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002410 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002411 continue;
2412#endif
2413
Hanno Becker16970d22017-10-10 15:56:37 +01002414 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2415 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2416 break;
2417
2418 /* For event-driven IO, wait for socket to become available */
2419 if( opt.event == 1 /* level triggered IO */ )
2420 {
2421#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002422 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002423#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002424 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002425#endif
2426 }
2427 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002428
2429 if( ret < 0 )
2430 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002431 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n",
2432 ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002433 goto exit;
2434 }
2435
2436 frags = 1;
2437 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002438
2439 if( written < len )
2440 {
2441 mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
2442 "was truncated to size %u", (unsigned) written );
2443 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002444 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002445
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002446 buf[written] = '\0';
Hanno Becker16970d22017-10-10 15:56:37 +01002447 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
2448 written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002449
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002450 /* Send a non-empty request if request_size == 0 */
2451 if ( len == 0 )
2452 {
2453 opt.request_size = DFL_REQUEST_SIZE;
2454 goto send_request;
2455 }
2456
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 /*
2458 * 7. Read the HTTP response
2459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 fflush( stdout );
2462
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002463 /*
2464 * TLS and DTLS need different reading styles (stream vs datagram)
2465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002467 {
2468 do
2469 {
2470 len = sizeof( buf ) - 1;
2471 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002473
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002474#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002475 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002476 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002477#endif
2478
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002479 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
2480 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Hanno Becker16970d22017-10-10 15:56:37 +01002481 {
2482 /* For event-driven IO, wait for socket to become available */
2483 if( opt.event == 1 /* level triggered IO */ )
2484 {
2485#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002486 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002487#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002488 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002489#endif
2490 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002491 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01002492 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002493
2494 if( ret <= 0 )
2495 {
2496 switch( ret )
2497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2499 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002500 ret = 0;
2501 goto close_notify;
2502
2503 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 case MBEDTLS_ERR_NET_CONN_RESET:
2505 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002506 ret = 0;
2507 goto reconnect;
2508
2509 default:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002510 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n",
2511 -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002512 goto exit;
2513 }
2514 }
2515
2516 len = ret;
2517 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002519
2520 /* End of message should be detected according to the syntax of the
2521 * application protocol (eg HTTP), just use a dummy test here. */
2522 if( ret > 0 && buf[len-1] == '\n' )
2523 {
2524 ret = 0;
2525 break;
2526 }
2527 }
2528 while( 1 );
2529 }
2530 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 {
2532 len = sizeof( buf ) - 1;
2533 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002534
Hanno Becker16970d22017-10-10 15:56:37 +01002535 while( 1 )
2536 {
2537 ret = mbedtls_ssl_read( &ssl, buf, len );
2538
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002539#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002540 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002541 continue;
2542#endif
2543
Hanno Becker16970d22017-10-10 15:56:37 +01002544 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2545 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2546 break;
2547
2548 /* For event-driven IO, wait for socket to become available */
2549 if( opt.event == 1 /* level triggered IO */ )
2550 {
2551#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002552 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002553#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002554 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002555#endif
2556 }
2557 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002559 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002561 switch( ret )
2562 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002563 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002565 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02002566 goto send_request;
2567 goto exit;
2568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2570 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002571 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002572 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002574 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002576 goto exit;
2577 }
Paul Bakker5690efc2011-05-26 13:16:06 +00002578 }
2579
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02002581 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002583 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002585
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002586 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002587 * 7b. Simulate hard reset and reconnect from same port?
2588 */
2589 if( opt.reconnect_hard != 0 )
2590 {
2591 opt.reconnect_hard = 0;
2592
2593 mbedtls_printf( " . Restarting connection from same port..." );
2594 fflush( stdout );
2595
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002596#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker23699ef2019-02-26 12:36:53 +00002597 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002598#endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Becker23699ef2019-02-26 12:36:53 +00002599
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002600 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
2601 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002602 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2603 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002604 goto exit;
2605 }
2606
2607 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
2608 {
2609 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002610 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002611 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002612 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002613 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2614 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002615 goto exit;
2616 }
Hanno Becker16970d22017-10-10 15:56:37 +01002617
2618 /* For event-driven IO, wait for socket to become available */
2619 if( opt.event == 1 /* level triggered IO */ )
2620 {
2621#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002622 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002623#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002624 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002625#endif
2626 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002627 }
2628
2629 mbedtls_printf( " ok\n" );
2630
2631 goto send_request;
2632 }
2633
2634 /*
2635 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002636 */
2637 if( --opt.exchanges > 0 )
2638 goto send_request;
2639
2640 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002641 * 8. Done, cleanly close the connection
2642 */
2643close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002645 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002646
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002647 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002649 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002650 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002653
2654 /*
2655 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002656 */
2657reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002658 if( opt.reconnect != 0 )
2659 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02002660 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002661
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002662 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002663
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002664#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002665 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02002666 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002667#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002670
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002671#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckera1051b42019-02-26 11:38:29 +00002672 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002673#endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Beckera1051b42019-02-26 11:38:29 +00002674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002676 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002677 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2678 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002679 goto exit;
2680 }
2681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002683 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002684 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n",
2685 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002686 goto exit;
2687 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002688
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002689 if( ( ret = mbedtls_net_connect( &server_fd,
2690 opt.server_addr, opt.server_port,
2691 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2692 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002693 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002694 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
2695 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002696 goto exit;
2697 }
2698
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002699 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002700 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002701 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002702 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002703 if( ret != 0 )
2704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002706 -ret );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002707 goto exit;
2708 }
2709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002711 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002712 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002713 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002714 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002715 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002716 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2717 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002718 goto exit;
2719 }
2720 }
2721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002723
2724 goto send_request;
2725 }
2726
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002727 /*
2728 * Cleanup and exit
2729 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002730exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00002732 if( ret != 0 )
2733 {
2734 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 mbedtls_strerror( ret, error_buf, 100 );
2736 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002737 }
2738#endif
2739
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002740 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742#if defined(MBEDTLS_X509_CRT_PARSE_C)
2743 mbedtls_x509_crt_free( &clicert );
2744 mbedtls_x509_crt_free( &cacert );
2745 mbedtls_pk_free( &pkey );
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01002746#if defined(MBEDTLS_USE_PSA_CRYPTO)
2747 psa_destroy_key( key_slot );
2748#endif
Paul Bakkered27a042013-04-18 22:46:23 +02002749#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 mbedtls_ssl_session_free( &saved_session );
2751 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002752 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 mbedtls_ctr_drbg_free( &ctr_drbg );
2754 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002755
Hanno Becker3f24ea92018-11-05 13:25:17 +00002756#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
2757 defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002758 if( opt.psk_opaque != 0 )
Hanno Becker3f24ea92018-11-05 13:25:17 +00002759 {
2760 /* This is ok even if the slot hasn't been
2761 * initialized (we might have jumed here
2762 * immediately because of bad cmd line params,
2763 * for example). */
Hanno Becker1d911cd2018-11-15 13:06:09 +00002764 status = psa_destroy_key( slot );
Hanno Becker3f24ea92018-11-05 13:25:17 +00002765 if( status != PSA_SUCCESS )
2766 {
2767 mbedtls_printf( "Failed to destroy key slot %u - error was %d",
Hanno Becker1d911cd2018-11-15 13:06:09 +00002768 (unsigned) slot, (int) status );
Hanno Becker3f24ea92018-11-05 13:25:17 +00002769 if( ret == 0 )
2770 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2771 }
2772 }
2773#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED &&
2774 MBEDTLS_USE_PSA_CRYPTO */
2775
Paul Bakkercce9d772011-11-18 14:26:47 +00002776#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 fflush( stdout ); getchar();
2779#endif
2780
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002781 // Shell can not handle large exit numbers -> 1 for errors
2782 if( ret < 0 )
2783 ret = 1;
2784
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 return( ret );
2786}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
2788 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002789 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */