blob: c9e3864e28caab3ab5fa497a9e00aeda32725e3b [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
Hanno Becker6ae14c02019-05-22 16:59:25 +0100787 /* Check if the use of a CID has been negotiated,
788 * but don't ask for the CID value and length.
789 *
790 * Note: Here and below, we're demonstrating the various ways
791 * in which mbedtls_ssl_get_peer_cid() can be called,
792 * depending on whether or not the length/value of the
793 * peer's CID is needed.
794 *
795 * An actual application, however, should use
796 * just one call to mbedtls_ssl_get_peer_cid(). */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100797 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
Hanno Becker6ae14c02019-05-22 16:59:25 +0100798 NULL, NULL );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100799 if( ret != 0 )
800 {
801 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
802 -ret );
803 return( ret );
804 }
805
806 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
807 {
808 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
809 {
810 mbedtls_printf( "(%s) Use of Connection ID was rejected by the server.\n",
811 additional_description );
812 }
813 }
814 else
815 {
816 size_t idx=0;
817 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
818 additional_description );
Hanno Becker6ae14c02019-05-22 16:59:25 +0100819
820 /* Ask for just the length of the peer's CID. */
821 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
822 NULL, &peer_cid_len );
823 if( ret != 0 )
824 {
825 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
826 -ret );
827 return( ret );
828 }
829
830 /* Ask for just length + value of the peer's CID. */
831 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
832 peer_cid, &peer_cid_len );
833 if( ret != 0 )
834 {
835 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
836 -ret );
837 return( ret );
838 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100839 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
840 additional_description,
841 (unsigned) peer_cid_len );
842 while( idx < peer_cid_len )
843 {
844 mbedtls_printf( "%02x ", peer_cid[ idx ] );
845 idx++;
846 }
847 mbedtls_printf( "\n" );
848 }
849
850 return( 0 );
851}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100852#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100853
Paul Bakker9caf2d22010-02-18 19:37:19 +0000854int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000855{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200856 int ret = 0, len, tail_len, i, written, frags, retry_left;
857 mbedtls_net_context server_fd;
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100858
859 unsigned char buf[MAX_REQUEST_SIZE + 1];
860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
862 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200863 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200864#endif
Hanno Becker90cb3592019-04-09 17:24:19 +0100865
Hanno Beckera0e20d02019-05-15 14:03:01 +0100866#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +0100867 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100868 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker90cb3592019-04-09 17:24:19 +0100869 size_t cid_len = 0;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100870 size_t cid_renego_len = 0;
Hanno Becker90cb3592019-04-09 17:24:19 +0100871#endif
872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100874 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200875#endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100876#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +0100877 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100878 const mbedtls_ecp_curve_info *curve_cur;
879#endif
880
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200881 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000882
Hanno Beckere86964c2018-10-23 11:37:50 +0100883#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500884 psa_key_handle_t slot = 0;
Hanno Beckere86964c2018-10-23 11:37:50 +0100885 psa_algorithm_t alg = 0;
886 psa_key_policy_t policy;
887 psa_status_t status;
888#endif
889
Gilles Peskineef86ab22017-05-05 18:59:02 +0200890#if defined(MBEDTLS_X509_CRT_PARSE_C)
891 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
892#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 mbedtls_entropy_context entropy;
894 mbedtls_ctr_drbg_context ctr_drbg;
895 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200896 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200898#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200899 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200900#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200902 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 mbedtls_x509_crt cacert;
904 mbedtls_x509_crt clicert;
905 mbedtls_pk_context pkey;
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +0100906#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500907 psa_key_handle_t key_slot = 0; /* invalid key slot */
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +0100908#endif
Paul Bakkered27a042013-04-18 22:46:23 +0200909#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000910 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000911 const int *list;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300912#if defined(MBEDTLS_SSL_EXPORT_KEYS)
913 unsigned char eap_tls_keymaterial[16];
914 unsigned char eap_tls_iv[8];
915 const char* eap_tls_label = "client EAP encryption";
916 eap_tls_keys eap_tls_keying;
917#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000918
Paul Bakker1a207ec2011-02-06 13:22:40 +0000919 /*
920 * Make sure memory references are valid.
921 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200922 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200923 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200924 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200926 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927#if defined(MBEDTLS_X509_CRT_PARSE_C)
928 mbedtls_x509_crt_init( &cacert );
929 mbedtls_x509_crt_init( &clicert );
930 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200931#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200933 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200934#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000935
Hanno Beckerb2b468b2018-11-12 17:46:59 +0000936#if defined(MBEDTLS_USE_PSA_CRYPTO)
937 status = psa_crypto_init();
938 if( status != PSA_SUCCESS )
939 {
940 mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
941 (int) status );
942 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
943 goto exit;
944 }
945#endif
946
Paul Bakker9caf2d22010-02-18 19:37:19 +0000947 if( argc == 0 )
948 {
949 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000950 if( ret == 0 )
951 ret = 1;
952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000956 while( *list )
957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200959 list++;
960 if( !*list )
961 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000963 list++;
964 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000966 goto exit;
967 }
968
969 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100970 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000971 opt.server_port = DFL_SERVER_PORT;
972 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker90cb3592019-04-09 17:24:19 +0100973 opt.cid_enabled = DFL_CID_ENABLED;
974 opt.cid_val = DFL_CID_VALUE;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100975 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
976 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100977 opt.nbio = DFL_NBIO;
Hanno Becker16970d22017-10-10 15:56:37 +0100978 opt.event = DFL_EVENT;
Hanno Beckerbb425db2019-04-03 12:59:58 +0100979 opt.context_crt_cb = DFL_CONTEXT_CRT_CB;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200980 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200981 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000982 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200983 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000984 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000985 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000986 opt.crt_file = DFL_CRT_FILE;
987 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100988 opt.key_opaque = DFL_KEY_OPAQUE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200989 opt.psk = DFL_PSK;
Hanno Beckere86964c2018-10-23 11:37:50 +0100990#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000991 opt.psk_opaque = DFL_PSK_OPAQUE;
Hanno Beckere86964c2018-10-23 11:37:50 +0100992#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200993#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
994 opt.ca_callback = DFL_CA_CALLBACK;
995#endif
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200996 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200997 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200998 opt.ec_max_ops = DFL_EC_MAX_OPS;
Paul Bakker51936882011-02-20 16:05:58 +0000999 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +00001000 opt.renegotiation = DFL_RENEGOTIATION;
1001 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001002 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001003 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001004 opt.min_version = DFL_MIN_VERSION;
1005 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001006 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001007 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001008 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001009 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001010 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001011 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001012 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001013 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001014 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001015 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001016 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001017 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001018 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001019 opt.transport = DFL_TRANSPORT;
1020 opt.hs_to_min = DFL_HS_TO_MIN;
1021 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001022 opt.dtls_mtu = DFL_DTLS_MTU;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001023 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001024 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001025 opt.etm = DFL_ETM;
Hanno Becker4d615912018-08-14 13:33:30 +01001026 opt.dgram_packing = DFL_DGRAM_PACKING;
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001027 opt.eap_tls = DFL_EAP_TLS;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001028
1029 for( i = 1; i < argc; i++ )
1030 {
Paul Bakker9caf2d22010-02-18 19:37:19 +00001031 p = argv[i];
1032 if( ( q = strchr( p, '=' ) ) == NULL )
1033 goto usage;
1034 *q++ = '\0';
1035
1036 if( strcmp( p, "server_name" ) == 0 )
1037 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001038 else if( strcmp( p, "server_addr" ) == 0 )
1039 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001040 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001041 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001042 else if( strcmp( p, "dtls" ) == 0 )
1043 {
1044 int t = atoi( q );
1045 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001047 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001049 else
1050 goto usage;
1051 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001052 else if( strcmp( p, "debug_level" ) == 0 )
1053 {
1054 opt.debug_level = atoi( q );
1055 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1056 goto usage;
1057 }
Hanno Beckerbb425db2019-04-03 12:59:58 +01001058 else if( strcmp( p, "context_crt_cb" ) == 0 )
1059 {
1060 opt.context_crt_cb = atoi( q );
1061 if( opt.context_crt_cb != 0 && opt.context_crt_cb != 1 )
1062 goto usage;
1063 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001064 else if( strcmp( p, "nbio" ) == 0 )
1065 {
1066 opt.nbio = atoi( q );
1067 if( opt.nbio < 0 || opt.nbio > 2 )
1068 goto usage;
1069 }
Hanno Becker16970d22017-10-10 15:56:37 +01001070 else if( strcmp( p, "event" ) == 0 )
1071 {
1072 opt.event = atoi( q );
1073 if( opt.event < 0 || opt.event > 2 )
1074 goto usage;
1075 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001076 else if( strcmp( p, "read_timeout" ) == 0 )
1077 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001078 else if( strcmp( p, "max_resend" ) == 0 )
1079 {
1080 opt.max_resend = atoi( q );
1081 if( opt.max_resend < 0 )
1082 goto usage;
1083 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001084 else if( strcmp( p, "request_page" ) == 0 )
1085 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +02001086 else if( strcmp( p, "request_size" ) == 0 )
1087 {
1088 opt.request_size = atoi( q );
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001089 if( opt.request_size < 0 ||
1090 opt.request_size > MAX_REQUEST_SIZE )
Paul Bakker93c32b22014-04-25 13:40:05 +02001091 goto usage;
1092 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001093 else if( strcmp( p, "ca_file" ) == 0 )
1094 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +00001095 else if( strcmp( p, "ca_path" ) == 0 )
1096 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +00001097 else if( strcmp( p, "crt_file" ) == 0 )
1098 opt.crt_file = q;
1099 else if( strcmp( p, "key_file" ) == 0 )
1100 opt.key_file = q;
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001101#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
1102 else if( strcmp( p, "key_opaque" ) == 0 )
1103 opt.key_opaque = atoi( q );
1104#endif
Hanno Beckera0e20d02019-05-15 14:03:01 +01001105#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +01001106 else if( strcmp( p, "cid" ) == 0 )
1107 {
1108 opt.cid_enabled = atoi( q );
1109 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
1110 goto usage;
1111 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001112 else if( strcmp( p, "cid_renego" ) == 0 )
1113 {
1114 opt.cid_enabled_renego = atoi( q );
1115 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
1116 goto usage;
1117 }
Hanno Becker90cb3592019-04-09 17:24:19 +01001118 else if( strcmp( p, "cid_val" ) == 0 )
1119 {
1120 opt.cid_val = q;
1121 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001122 else if( strcmp( p, "cid_val_renego" ) == 0 )
1123 {
1124 opt.cid_val_renego = q;
1125 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001126#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001127 else if( strcmp( p, "psk" ) == 0 )
1128 opt.psk = q;
Hanno Beckere86964c2018-10-23 11:37:50 +01001129#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001130 else if( strcmp( p, "psk_opaque" ) == 0 )
1131 opt.psk_opaque = atoi( q );
Hanno Beckere86964c2018-10-23 11:37:50 +01001132#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001133#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1134 else if( strcmp( p, "ca_callback" ) == 0)
1135 opt.ca_callback = atoi( q );
1136#endif
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001137 else if( strcmp( p, "psk_identity" ) == 0 )
1138 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001139 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1140 opt.ecjpake_pw = q;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001141 else if( strcmp( p, "ec_max_ops" ) == 0 )
1142 opt.ec_max_ops = atoi( q );
Paul Bakker51936882011-02-20 16:05:58 +00001143 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +00001146
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001147 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +00001148 {
1149 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +00001150 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +00001151 }
Paul Bakker51936882011-02-20 16:05:58 +00001152 opt.force_ciphersuite[1] = 0;
1153 }
Paul Bakker48916f92012-09-16 19:57:18 +00001154 else if( strcmp( p, "renegotiation" ) == 0 )
1155 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001156 opt.renegotiation = (atoi( q )) ?
1157 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1158 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +00001159 }
1160 else if( strcmp( p, "allow_legacy" ) == 0 )
1161 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001162 switch( atoi( q ) )
1163 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001164 case -1:
1165 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1166 break;
1167 case 0:
1168 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1169 break;
1170 case 1:
1171 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1172 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001173 default: goto usage;
1174 }
Paul Bakker48916f92012-09-16 19:57:18 +00001175 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001176 else if( strcmp( p, "renegotiate" ) == 0 )
1177 {
1178 opt.renegotiate = atoi( q );
1179 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1180 goto usage;
1181 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001182 else if( strcmp( p, "exchanges" ) == 0 )
1183 {
1184 opt.exchanges = atoi( q );
1185 if( opt.exchanges < 1 )
1186 goto usage;
1187 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001188 else if( strcmp( p, "reconnect" ) == 0 )
1189 {
1190 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001191 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001192 goto usage;
1193 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001194 else if( strcmp( p, "reco_delay" ) == 0 )
1195 {
1196 opt.reco_delay = atoi( q );
1197 if( opt.reco_delay < 0 )
1198 goto usage;
1199 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001200 else if( strcmp( p, "reconnect_hard" ) == 0 )
1201 {
1202 opt.reconnect_hard = atoi( q );
1203 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
1204 goto usage;
1205 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001206 else if( strcmp( p, "tickets" ) == 0 )
1207 {
1208 opt.tickets = atoi( q );
1209 if( opt.tickets < 0 || opt.tickets > 2 )
1210 goto usage;
1211 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001212 else if( strcmp( p, "alpn" ) == 0 )
1213 {
1214 opt.alpn_string = q;
1215 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001216 else if( strcmp( p, "fallback" ) == 0 )
1217 {
1218 switch( atoi( q ) )
1219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
1221 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001222 default: goto usage;
1223 }
1224 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001225 else if( strcmp( p, "extended_ms" ) == 0 )
1226 {
1227 switch( atoi( q ) )
1228 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001229 case 0:
1230 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1231 break;
1232 case 1:
1233 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1234 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001235 default: goto usage;
1236 }
1237 }
Hanno Beckere6706e62017-05-15 16:05:15 +01001238 else if( strcmp( p, "curves" ) == 0 )
1239 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001240 else if( strcmp( p, "etm" ) == 0 )
1241 {
1242 switch( atoi( q ) )
1243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1245 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001246 default: goto usage;
1247 }
1248 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001249 else if( strcmp( p, "min_version" ) == 0 )
1250 {
1251 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001253 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001255 else if( strcmp( q, "tls1_1" ) == 0 ||
1256 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001258 else if( strcmp( q, "tls1_2" ) == 0 ||
1259 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001261 else
1262 goto usage;
1263 }
1264 else if( strcmp( p, "max_version" ) == 0 )
1265 {
1266 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001268 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001270 else if( strcmp( q, "tls1_1" ) == 0 ||
1271 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001273 else if( strcmp( q, "tls1_2" ) == 0 ||
1274 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001276 else
1277 goto usage;
1278 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001279 else if( strcmp( p, "arc4" ) == 0 )
1280 {
1281 switch( atoi( q ) )
1282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1284 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001285 default: goto usage;
1286 }
1287 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02001288 else if( strcmp( p, "allow_sha1" ) == 0 )
1289 {
1290 switch( atoi( q ) )
1291 {
1292 case 0: opt.allow_sha1 = 0; break;
1293 case 1: opt.allow_sha1 = 1; break;
1294 default: goto usage;
1295 }
1296 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001297 else if( strcmp( p, "force_version" ) == 0 )
1298 {
1299 if( strcmp( q, "ssl3" ) == 0 )
1300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1302 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001303 }
1304 else if( strcmp( q, "tls1" ) == 0 )
1305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1307 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001308 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001309 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1312 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001313 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001314 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1317 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001318 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001319 else if( strcmp( q, "dtls1" ) == 0 )
1320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1322 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1323 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001324 }
1325 else if( strcmp( q, "dtls1_2" ) == 0 )
1326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1328 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1329 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001330 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001331 else
1332 goto usage;
1333 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001334 else if( strcmp( p, "auth_mode" ) == 0 )
1335 {
1336 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001338 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001340 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001342 else
1343 goto usage;
1344 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001345 else if( strcmp( p, "max_frag_len" ) == 0 )
1346 {
1347 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001349 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001351 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001353 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001355 else
1356 goto usage;
1357 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001358 else if( strcmp( p, "trunc_hmac" ) == 0 )
1359 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001360 switch( atoi( q ) )
1361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1363 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001364 default: goto usage;
1365 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001366 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001367 else if( strcmp( p, "hs_timeout" ) == 0 )
1368 {
1369 if( ( p = strchr( q, '-' ) ) == NULL )
1370 goto usage;
1371 *p++ = '\0';
1372 opt.hs_to_min = atoi( q );
1373 opt.hs_to_max = atoi( p );
1374 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1375 goto usage;
1376 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001377 else if( strcmp( p, "mtu" ) == 0 )
1378 {
1379 opt.dtls_mtu = atoi( q );
1380 if( opt.dtls_mtu < 0 )
1381 goto usage;
1382 }
Hanno Becker4d615912018-08-14 13:33:30 +01001383 else if( strcmp( p, "dgram_packing" ) == 0 )
1384 {
1385 opt.dgram_packing = atoi( q );
1386 if( opt.dgram_packing != 0 &&
1387 opt.dgram_packing != 1 )
1388 {
1389 goto usage;
1390 }
1391 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001392 else if( strcmp( p, "recsplit" ) == 0 )
1393 {
1394 opt.recsplit = atoi( q );
1395 if( opt.recsplit < 0 || opt.recsplit > 1 )
1396 goto usage;
1397 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001398 else if( strcmp( p, "dhmlen" ) == 0 )
1399 {
1400 opt.dhmlen = atoi( q );
1401 if( opt.dhmlen < 0 )
1402 goto usage;
1403 }
Andres Amaya Garciabc818842018-10-16 21:08:38 +01001404 else if( strcmp( p, "query_config" ) == 0 )
1405 {
1406 return query_config( q );
1407 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001408 else if( strcmp( p, "eap_tls" ) == 0 )
1409 {
1410 opt.eap_tls = atoi( q );
1411 if( opt.eap_tls < 0 || opt.eap_tls > 1 )
1412 goto usage;
1413 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001414 else
1415 goto usage;
1416 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001417
Hanno Becker16970d22017-10-10 15:56:37 +01001418 /* Event-driven IO is incompatible with the above custom
1419 * receive and send functions, as the polling builds on
1420 * refers to the underlying net_context. */
1421 if( opt.event == 1 && opt.nbio != 1 )
1422 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001423 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001424 opt.nbio = 1;
1425 }
1426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#if defined(MBEDTLS_DEBUG_C)
1428 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001429#endif
1430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001432 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001433 * Unhexify the pre-shared key if any is given
1434 */
1435 if( strlen( opt.psk ) )
1436 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001437 psk_len = strlen( opt.psk ) / 2;
1438 if( psk_len > sizeof( psk ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001439 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001440 mbedtls_printf( "pre-shared key too long\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001441 goto exit;
1442 }
1443
Hanno Becker1f583ee2019-04-09 17:12:56 +01001444 if( unhexify( opt.psk, psk ) != 0 )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001445 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001446 mbedtls_printf( "pre-shared key not valid hex\n" );
1447 goto exit;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001448 }
1449 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001451
Hanno Beckere86964c2018-10-23 11:37:50 +01001452
1453#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001454 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001455 {
1456 if( opt.psk == NULL )
1457 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00001458 mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
Hanno Beckere86964c2018-10-23 11:37:50 +01001459 ret = 2;
1460 goto usage;
1461 }
1462
1463 if( opt.force_ciphersuite[0] <= 0 )
1464 {
1465 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" );
1466 ret = 2;
1467 goto usage;
1468 }
1469 }
1470#endif /* MBEDTLS_USE_PSA_CRYPTO */
1471
1472 if( opt.force_ciphersuite[0] > 0 )
1473 {
1474 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1475 ciphersuite_info =
1476 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1477
1478 if( opt.max_version != -1 &&
1479 ciphersuite_info->min_minor_ver > opt.max_version )
1480 {
1481 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
1482 ret = 2;
1483 goto usage;
1484 }
1485 if( opt.min_version != -1 &&
1486 ciphersuite_info->max_minor_ver < opt.min_version )
1487 {
1488 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
1489 ret = 2;
1490 goto usage;
1491 }
1492
1493 /* If the server selects a version that's not supported by
1494 * this suite, then there will be no common ciphersuite... */
1495 if( opt.max_version == -1 ||
1496 opt.max_version > ciphersuite_info->max_minor_ver )
1497 {
1498 opt.max_version = ciphersuite_info->max_minor_ver;
1499 }
1500 if( opt.min_version < ciphersuite_info->min_minor_ver )
1501 {
1502 opt.min_version = ciphersuite_info->min_minor_ver;
1503 /* DTLS starts with TLS 1.1 */
1504 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1505 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1506 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1507 }
1508
1509 /* Enable RC4 if needed and not explicitly disabled */
1510 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
1511 {
1512 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
1513 {
1514 mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" );
1515 ret = 2;
1516 goto usage;
1517 }
1518
1519 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
1520 }
1521
Hanno Becker90cb3592019-04-09 17:24:19 +01001522
Hanno Beckere86964c2018-10-23 11:37:50 +01001523#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001524 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001525 {
1526 /* Ensure that the chosen ciphersuite is PSK-only; we must know
1527 * the ciphersuite in advance to set the correct policy for the
1528 * PSK key slot. This limitation might go away in the future. */
1529 if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
1530 opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 )
1531 {
1532 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" );
1533 ret = 2;
1534 goto usage;
1535 }
1536
1537 /* Determine KDF algorithm the opaque PSK will be used in. */
1538#if defined(MBEDTLS_SHA512_C)
1539 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1540 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1541 else
1542#endif /* MBEDTLS_SHA512_C */
1543 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1544 }
1545#endif /* MBEDTLS_USE_PSA_CRYPTO */
1546 }
1547
Hanno Beckera0e20d02019-05-15 14:03:01 +01001548#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001549 cid_len = strlen( opt.cid_val ) / 2;
1550 if( cid_len > sizeof( cid ) )
1551 {
1552 mbedtls_printf( "CID too long\n" );
1553 goto exit;
1554 }
Hanno Becker90cb3592019-04-09 17:24:19 +01001555
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001556 if( unhexify( opt.cid_val, cid ) != 0 )
1557 {
1558 mbedtls_printf( "CID not valid hex\n" );
1559 goto exit;
1560 }
1561
1562 /* Keep CID settings for renegotiation unless
1563 * specified otherwise. */
1564 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
1565 opt.cid_enabled_renego = opt.cid_enabled;
1566 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
1567 opt.cid_val_renego = opt.cid_val;
1568
1569 cid_renego_len = strlen( opt.cid_val_renego ) / 2;
1570 if( cid_renego_len > sizeof( cid_renego ) )
1571 {
1572 mbedtls_printf( "CID too long\n" );
1573 goto exit;
1574 }
1575
1576 if( unhexify( opt.cid_val_renego, cid_renego ) != 0 )
1577 {
1578 mbedtls_printf( "CID not valid hex\n" );
1579 goto exit;
1580 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001581#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01001582
Hanno Beckere6706e62017-05-15 16:05:15 +01001583#if defined(MBEDTLS_ECP_C)
1584 if( opt.curves != NULL )
1585 {
1586 p = (char *) opt.curves;
1587 i = 0;
1588
1589 if( strcmp( p, "none" ) == 0 )
1590 {
1591 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1592 }
1593 else if( strcmp( p, "default" ) != 0 )
1594 {
1595 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001596 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001597 {
1598 q = p;
1599
1600 /* Terminate the current string */
1601 while( *p != ',' && *p != '\0' )
1602 p++;
1603 if( *p == ',' )
1604 *p++ = '\0';
1605
1606 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1607 {
1608 curve_list[i++] = curve_cur->grp_id;
1609 }
1610 else
1611 {
1612 mbedtls_printf( "unknown curve %s\n", q );
1613 mbedtls_printf( "supported curves: " );
1614 for( curve_cur = mbedtls_ecp_curve_list();
1615 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1616 curve_cur++ )
1617 {
1618 mbedtls_printf( "%s ", curve_cur->name );
1619 }
1620 mbedtls_printf( "\n" );
1621 goto exit;
1622 }
1623 }
1624
1625 mbedtls_printf("Number of curves: %d\n", i );
1626
Hanno Becker8651a432017-06-09 16:13:22 +01001627 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001628 {
Hanno Becker8651a432017-06-09 16:13:22 +01001629 mbedtls_printf( "curves list too long, maximum %d",
1630 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001631 goto exit;
1632 }
1633
1634 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1635 }
1636 }
1637#endif /* MBEDTLS_ECP_C */
1638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001640 if( opt.alpn_string != NULL )
1641 {
1642 p = (char *) opt.alpn_string;
1643 i = 0;
1644
1645 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001646 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001647 {
1648 alpn_list[i++] = p;
1649
1650 /* Terminate the current string and move on to next one */
1651 while( *p != ',' && *p != '\0' )
1652 p++;
1653 if( *p == ',' )
1654 *p++ = '\0';
1655 }
1656 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001658
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001659 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001660 * 0. Initialize the RNG and the session data
1661 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001663 fflush( stdout );
1664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 mbedtls_entropy_init( &entropy );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001666 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1667 &entropy, (const unsigned char *) pers,
1668 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +00001669 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001670 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
1671 -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001672 goto exit;
1673 }
1674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001678 /*
1679 * 1.1. Load the trusted CA
1680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001682 fflush( stdout );
1683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001685 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001686 if( strcmp( opt.ca_path, "none" ) == 0 )
1687 ret = 0;
1688 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001690 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001691 if( strcmp( opt.ca_file, "none" ) == 0 )
1692 ret = 0;
1693 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001695 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001696#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697#if defined(MBEDTLS_CERTS_C)
1698 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700 ret = mbedtls_x509_crt_parse( &cacert,
1701 (const unsigned char *) mbedtls_test_cas[i],
1702 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001703 if( ret != 0 )
1704 break;
1705 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001706#else
1707 {
1708 ret = 1;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001709 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001710 }
1711#endif
Paul Bakker42488232012-05-16 08:21:05 +00001712 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001713 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001714 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1715 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001716 goto exit;
1717 }
1718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001720
1721 /*
1722 * 1.2. Load own certificate and private key
1723 *
1724 * (can be skipped if client authentication is not required)
1725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001727 fflush( stdout );
1728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001730 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001731 if( strcmp( opt.crt_file, "none" ) == 0 )
1732 ret = 0;
1733 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001735 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001736#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001738 ret = mbedtls_x509_crt_parse( &clicert,
1739 (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001741#else
1742 {
1743 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001745 }
1746#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001747 if( ret != 0 )
1748 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001749 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1750 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001751 goto exit;
1752 }
1753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001755 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001756 if( strcmp( opt.key_file, "none" ) == 0 )
1757 ret = 0;
1758 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001760 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001761#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001763 ret = mbedtls_pk_parse_key( &pkey,
1764 (const unsigned char *) mbedtls_test_cli_key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001766#else
1767 {
1768 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001770 }
1771#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001772 if( ret != 0 )
1773 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001774 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
1775 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001776 goto exit;
1777 }
1778
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001779#if defined(MBEDTLS_USE_PSA_CRYPTO)
1780 if( opt.key_opaque != 0 )
1781 {
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001782 if( ( ret = mbedtls_pk_wrap_as_opaque( &pkey, &key_slot,
1783 PSA_ALG_SHA_256 ) ) != 0 )
1784 {
1785 mbedtls_printf( " failed\n ! "
1786 "mbedtls_pk_wrap_as_opaque returned -0x%x\n\n", -ret );
1787 goto exit;
1788 }
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001789 }
1790#endif /* MBEDTLS_USE_PSA_CRYPTO */
1791
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001792 mbedtls_printf( " ok (key type: %s)\n", mbedtls_pk_get_name( &pkey ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001794
1795 /*
1796 * 2. Start the connection
1797 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001798 if( opt.server_addr == NULL)
1799 opt.server_addr = opt.server_name;
1800
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001801 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001803 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001804 fflush( stdout );
1805
Hanno Becker16970d22017-10-10 15:56:37 +01001806 if( ( ret = mbedtls_net_connect( &server_fd,
1807 opt.server_addr, opt.server_port,
1808 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1809 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001810 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001811 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
1812 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001813 goto exit;
1814 }
1815
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001816 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001817 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001818 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001819 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001820 if( ret != 0 )
1821 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001822 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
1823 -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001824 goto exit;
1825 }
1826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001828
1829 /*
1830 * 3. Setup stuff
1831 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001833 fflush( stdout );
1834
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001835 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1836 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001837 opt.transport,
1838 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001839 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001840 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
1841 -ret );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001842 goto exit;
1843 }
1844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001846 /* The default algorithms profile disables SHA-1, but our tests still
1847 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001848 if( opt.allow_sha1 > 0 )
1849 {
1850 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1851 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1852 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1853 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001854
Hanno Beckerbb425db2019-04-03 12:59:58 +01001855 if( opt.context_crt_cb == 0 )
1856 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
1857
Hanno Beckera1051b42019-02-26 11:38:29 +00001858 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Gilles Peskineef86ab22017-05-05 18:59:02 +02001859#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001860
Hanno Beckera0e20d02019-05-15 14:03:01 +01001861#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001862 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckerad4a1372019-05-03 13:06:44 +01001863 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001864 if( opt.cid_enabled == 1 &&
1865 opt.cid_enabled_renego == 1 &&
1866 cid_len != cid_renego_len )
1867 {
1868 mbedtls_printf( "CID length must not change during renegotiation\n" );
1869 goto usage;
1870 }
1871
1872
1873 if( opt.cid_enabled == 1 )
Hanno Becker8367ccc2019-05-14 11:30:10 +01001874 ret = mbedtls_ssl_conf_cid( &conf, cid_len,
1875 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001876 else
Hanno Becker8367ccc2019-05-14 11:30:10 +01001877 ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
1878 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001879
Hanno Beckerad4a1372019-05-03 13:06:44 +01001880 if( ret != 0 )
1881 {
1882 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned %d\n\n",
1883 ret );
1884 goto exit;
1885 }
1886 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001887#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerad4a1372019-05-03 13:06:44 +01001888
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001889 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001890 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001893 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Hanno Becker16970d22017-10-10 15:56:37 +01001894 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min,
1895 opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001896
Hanno Becker4d615912018-08-14 13:33:30 +01001897 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01001898 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001902 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001903 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001904 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n",
1905 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001906 goto exit;
1907 }
Paul Bakker05decb22013-08-15 13:33:48 +02001908#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001911 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001912 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001913#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001916 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001917 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001918#endif
1919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001921 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001922 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001923#endif
1924
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001925#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1926 if( opt.eap_tls != 0 )
1927 mbedtls_ssl_conf_export_keys_ext_cb( &conf, eap_tls_key_derivation,
1928 &eap_tls_keying );
1929#endif
1930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001932 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001933 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Hanno Becker16970d22017-10-10 15:56:37 +01001934 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1935 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001936#endif
1937
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001938#if defined(MBEDTLS_DHM_C)
1939 if( opt.dhmlen != DFL_DHMLEN )
1940 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1941#endif
1942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001944 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001945 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001946 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001947 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
1948 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001949 goto exit;
1950 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001951#endif
1952
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001953 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1954 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001955
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001956 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02001959 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02001960#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001961
Paul Bakker645ce3a2012-10-31 12:32:41 +00001962 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001963 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001964
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001965#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001966 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001967 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001968#endif
Paul Bakker51936882011-02-20 16:05:58 +00001969
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001970 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001971 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001973 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001974#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001977 if( strcmp( opt.ca_path, "none" ) != 0 &&
1978 strcmp( opt.ca_file, "none" ) != 0 )
1979 {
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001980#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1981 if( opt.ca_callback != 0 )
Hanno Beckercbb59032019-03-28 14:14:22 +00001982 mbedtls_ssl_conf_ca_cb( &conf, ca_callback, &cacert );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001983 else
1984#endif
1985 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001986 }
1987 if( strcmp( opt.crt_file, "none" ) != 0 &&
1988 strcmp( opt.key_file, "none" ) != 0 )
1989 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001990 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001991 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001992 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
1993 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001994 goto exit;
1995 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001996 }
Paul Bakkered27a042013-04-18 22:46:23 +02001997#endif
1998
Hanno Beckere6706e62017-05-15 16:05:15 +01001999#if defined(MBEDTLS_ECP_C)
2000 if( opt.curves != NULL &&
2001 strcmp( opt.curves, "default" ) != 0 )
2002 {
2003 mbedtls_ssl_conf_curves( &conf, curve_list );
2004 }
2005#endif
2006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckere86964c2018-10-23 11:37:50 +01002008#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002009 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01002010 {
2011 /* The algorithm has already been determined earlier. */
Hanno Becker37519ea2019-01-25 14:26:01 +00002012 status = psa_allocate_key( &slot );
Hanno Becker1d911cd2018-11-15 13:06:09 +00002013 if( status != PSA_SUCCESS )
2014 {
2015 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2016 goto exit;
2017 }
Hanno Beckere86964c2018-10-23 11:37:50 +01002018
Hanno Becker13871242019-01-25 14:26:26 +00002019 policy = psa_key_policy_init();
Hanno Beckere86964c2018-10-23 11:37:50 +01002020 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2021
2022 status = psa_set_key_policy( slot, &policy );
2023 if( status != PSA_SUCCESS )
2024 {
2025 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2026 goto exit;
2027 }
2028
2029 status = psa_import_key( slot, PSA_KEY_TYPE_DERIVE, psk, psk_len );
2030 if( status != PSA_SUCCESS )
2031 {
2032 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2033 goto exit;
2034 }
2035
2036 if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, slot,
2037 (const unsigned char *) opt.psk_identity,
Hanno Becker5cd607b2018-11-05 12:52:42 +00002038 strlen( opt.psk_identity ) ) ) != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01002039 {
2040 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
2041 ret );
2042 goto exit;
2043 }
2044 }
2045 else
2046#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002047 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002048 (const unsigned char *) opt.psk_identity,
2049 strlen( opt.psk_identity ) ) ) != 0 )
2050 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002051 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n",
2052 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002053 goto exit;
2054 }
Hanno Beckere86964c2018-10-23 11:37:50 +01002055#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002056
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002057 if( opt.min_version != DFL_MIN_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01002058 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
2059 opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01002060
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002061 if( opt.max_version != DFL_MAX_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01002062 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
2063 opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00002064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02002066 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002067 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02002068#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002069
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002070 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
2071 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002072 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
2073 -ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002074 goto exit;
2075 }
2076
2077#if defined(MBEDTLS_X509_CRT_PARSE_C)
2078 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
2079 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002080 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
2081 ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002082 goto exit;
2083 }
2084#endif
2085
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002086#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2087 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
2088 {
2089 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
2090 (const unsigned char *) opt.ecjpake_pw,
2091 strlen( opt.ecjpake_pw ) ) ) != 0 )
2092 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002093 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
2094 ret );
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002095 goto exit;
2096 }
2097 }
2098#endif
2099
Hanno Beckerbb425db2019-04-03 12:59:58 +01002100#if defined(MBEDTLS_X509_CRT_PARSE_C)
2101 if( opt.context_crt_cb == 1 )
2102 mbedtls_ssl_set_verify( &ssl, my_verify, NULL );
2103#endif /* MBEDTLS_X509_CRT_PARSE_C */
2104
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002105 if( opt.nbio == 2 )
2106 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
2107 else
Hanno Becker16970d22017-10-10 15:56:37 +01002108 mbedtls_ssl_set_bio( &ssl, &server_fd,
2109 mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02002110 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002111
Hanno Beckera0e20d02019-05-15 14:03:01 +01002112#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +01002113 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2114 {
2115 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
2116 cid, cid_len ) ) != 0 )
2117 {
2118 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2119 ret );
2120 goto exit;
2121 }
2122 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002123#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01002124
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002125#if defined(MBEDTLS_SSL_PROTO_DTLS)
2126 if( opt.dtls_mtu != DFL_DTLS_MTU )
2127 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
2128#endif
2129
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002130#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002131 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
2132 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002133#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002134
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002135#if defined(MBEDTLS_ECP_RESTARTABLE)
2136 if( opt.ec_max_ops != DFL_EC_MAX_OPS )
2137 mbedtls_ecp_set_max_ops( opt.ec_max_ops );
2138#endif
2139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002141
Paul Bakker5121ce52009-01-03 21:22:43 +00002142 /*
2143 * 4. Handshake
2144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 fflush( stdout );
2147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 {
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002150 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2151 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002152 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Paul Bakker40e46942009-01-03 21:51:57 +00002153 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002154 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
2155 -ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2157 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01002158 " Unable to verify the server's certificate. "
2159 "Either it is invalid,\n"
2160 " or you didn't set ca_file or ca_path "
2161 "to an appropriate value.\n"
2162 " Alternatively, you may want to use "
2163 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00002166 }
Hanno Becker16970d22017-10-10 15:56:37 +01002167
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002168#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002169 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2170 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002171#endif
2172
Hanno Becker16970d22017-10-10 15:56:37 +01002173 /* For event-driven IO, wait for socket to become available */
2174 if( opt.event == 1 /* level triggered IO */ )
2175 {
2176#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002177 ret = idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002178#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002179 ret = idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002180#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002181 if( ret != 0 )
2182 goto exit;
Hanno Becker16970d22017-10-10 15:56:37 +01002183 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002184 }
2185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Hanno Becker16970d22017-10-10 15:56:37 +01002187 mbedtls_ssl_get_version( &ssl ),
2188 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
2191 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002192 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002194
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002195#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2196 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
2197 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
2198#endif
2199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002201 if( opt.alpn_string != NULL )
2202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
2204 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002205 alp ? alp : "(none)" );
2206 }
2207#endif
2208
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002209#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Ron Eldor51d3ab52019-05-12 14:54:30 +03002210 if( opt.eap_tls != 0 )
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002211 {
2212 size_t j = 0;
Ron Eldor51d3ab52019-05-12 14:54:30 +03002213
2214 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type,
2215 eap_tls_keying.master_secret,
2216 sizeof( eap_tls_keying.master_secret ),
2217 eap_tls_label,
2218 eap_tls_keying.randbytes,
2219 sizeof( eap_tls_keying.randbytes ),
2220 eap_tls_keymaterial,
2221 sizeof( eap_tls_keymaterial ) ) )
2222 != 0 )
2223 {
2224 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2225 -ret );
2226 goto exit;
2227 }
2228
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002229 mbedtls_printf( " EAP-TLS key material is:" );
2230 for( j = 0; j < sizeof( eap_tls_keymaterial ); j++ )
2231 {
2232 if( j % 8 == 0 )
2233 mbedtls_printf("\n ");
2234 mbedtls_printf("%02x ", eap_tls_keymaterial[j] );
2235 }
2236 mbedtls_printf("\n");
2237
Ron Eldor51d3ab52019-05-12 14:54:30 +03002238 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type, NULL, 0,
Ron Eldor51c45072019-05-15 17:49:54 +03002239 eap_tls_label,
2240 eap_tls_keying.randbytes,
2241 sizeof( eap_tls_keying.randbytes ),
2242 eap_tls_iv,
2243 sizeof( eap_tls_iv ) ) ) != 0 )
Ron Eldor51d3ab52019-05-12 14:54:30 +03002244 {
2245 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2246 -ret );
2247 goto exit;
2248 }
2249
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002250 mbedtls_printf( " EAP-TLS IV is:" );
2251 for( j = 0; j < sizeof( eap_tls_iv ); j++ )
2252 {
2253 if( j % 8 == 0 )
2254 mbedtls_printf("\n ");
2255 mbedtls_printf("%02x ", eap_tls_iv[j] );
2256 }
2257 mbedtls_printf("\n");
2258 }
2259#endif
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002260 if( opt.reconnect != 0 )
2261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002263 fflush( stdout );
2264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002266 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002267 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
2268 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002269 goto exit;
2270 }
2271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002273 }
2274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 /*
2277 * 5. Verify the server certificate
2278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002280
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002281 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002282 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002283 char vrfy_buf[512];
2284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002286
Hanno Becker16970d22017-10-10 15:56:37 +01002287 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ),
2288 " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00002289
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002290 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002291 }
2292 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Hanno Beckera9766c2c2019-02-25 17:43:18 +00002295 mbedtls_printf( " . Peer certificate information ...\n" );
2296 mbedtls_printf( "%s\n", peer_crt_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002298
Hanno Beckera0e20d02019-05-15 14:03:01 +01002299#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002300 ret = report_cid_usage( &ssl, "initial handshake" );
2301 if( ret != 0 )
2302 goto exit;
2303
Hanno Becker90cb3592019-04-09 17:24:19 +01002304 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2305 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002306 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
2307 cid_renego,
2308 cid_renego_len ) ) != 0 )
Hanno Becker90cb3592019-04-09 17:24:19 +01002309 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002310 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2311 ret );
2312 return( ret );
Hanno Becker90cb3592019-04-09 17:24:19 +01002313 }
2314 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002315#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01002316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002318 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002319 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002320 /*
2321 * Perform renegotiation (this must be done when the server is waiting
2322 * for input from our side).
2323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002325 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002327 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002328 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002329 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002330 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002331 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002332 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
2333 ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002334 goto exit;
2335 }
Hanno Becker16970d22017-10-10 15:56:37 +01002336
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002337#if defined(MBEDTLS_ECP_RESTARTABLE)
2338 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2339 continue;
2340#endif
2341
Hanno Becker16970d22017-10-10 15:56:37 +01002342 /* For event-driven IO, wait for socket to become available */
2343 if( opt.event == 1 /* level triggered IO */ )
2344 {
2345#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002346 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002347#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002348 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002349#endif
2350 }
2351
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002352 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002354 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002356
Hanno Beckera0e20d02019-05-15 14:03:01 +01002357#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002358 ret = report_cid_usage( &ssl, "after renegotiation" );
2359 if( ret != 0 )
2360 goto exit;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002361#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002362
Paul Bakker5121ce52009-01-03 21:22:43 +00002363 /*
2364 * 6. Write the GET request
2365 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002366 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002367send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002369 fflush( stdout );
2370
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002371 len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST,
2372 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002373 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002374
2375 /* Add padding to GET request to reach opt.request_size in length */
2376 if( opt.request_size != DFL_REQUEST_SIZE &&
2377 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02002378 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002379 memset( buf + len, 'A', opt.request_size - len - tail_len );
2380 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02002381 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002382
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002383 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002384 len += tail_len;
2385
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02002386 /* Truncate if request size is smaller than the "natural" size */
2387 if( opt.request_size != DFL_REQUEST_SIZE &&
2388 len > opt.request_size )
2389 {
2390 len = opt.request_size;
2391
2392 /* Still end with \r\n unless that's really not possible */
2393 if( len >= 2 ) buf[len - 2] = '\r';
2394 if( len >= 1 ) buf[len - 1] = '\n';
2395 }
2396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002398 {
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002399 written = 0;
2400 frags = 0;
2401
2402 do
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 {
Hanno Becker16970d22017-10-10 15:56:37 +01002404 while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002405 len - written ) ) < 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002406 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002407 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002408 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002409 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002410 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002411 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
2412 -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002413 goto exit;
2414 }
Hanno Becker16970d22017-10-10 15:56:37 +01002415
2416 /* For event-driven IO, wait for socket to become available */
2417 if( opt.event == 1 /* level triggered IO */ )
2418 {
2419#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002420 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002421#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002422 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002423#endif
2424 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002425 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002426
2427 frags++;
2428 written += ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002430 while( written < len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002432 else /* Not stream, so datagram */
2433 {
Hanno Becker16970d22017-10-10 15:56:37 +01002434 while( 1 )
2435 {
2436 ret = mbedtls_ssl_write( &ssl, buf, len );
2437
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002438#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002439 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002440 continue;
2441#endif
2442
Hanno Becker16970d22017-10-10 15:56:37 +01002443 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2444 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2445 break;
2446
2447 /* For event-driven IO, wait for socket to become available */
2448 if( opt.event == 1 /* level triggered IO */ )
2449 {
2450#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002451 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002452#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002453 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002454#endif
2455 }
2456 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002457
2458 if( ret < 0 )
2459 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002460 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n",
2461 ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002462 goto exit;
2463 }
2464
2465 frags = 1;
2466 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002467
2468 if( written < len )
2469 {
2470 mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
2471 "was truncated to size %u", (unsigned) written );
2472 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002473 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002474
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002475 buf[written] = '\0';
Hanno Becker16970d22017-10-10 15:56:37 +01002476 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
2477 written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002478
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002479 /* Send a non-empty request if request_size == 0 */
2480 if ( len == 0 )
2481 {
2482 opt.request_size = DFL_REQUEST_SIZE;
2483 goto send_request;
2484 }
2485
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 /*
2487 * 7. Read the HTTP response
2488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002490 fflush( stdout );
2491
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002492 /*
2493 * TLS and DTLS need different reading styles (stream vs datagram)
2494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002496 {
2497 do
2498 {
2499 len = sizeof( buf ) - 1;
2500 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002502
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002503#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002504 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002505 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002506#endif
2507
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002508 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
2509 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Hanno Becker16970d22017-10-10 15:56:37 +01002510 {
2511 /* For event-driven IO, wait for socket to become available */
2512 if( opt.event == 1 /* level triggered IO */ )
2513 {
2514#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002515 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002516#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002517 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002518#endif
2519 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002520 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01002521 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002522
2523 if( ret <= 0 )
2524 {
2525 switch( ret )
2526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2528 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002529 ret = 0;
2530 goto close_notify;
2531
2532 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 case MBEDTLS_ERR_NET_CONN_RESET:
2534 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002535 ret = 0;
2536 goto reconnect;
2537
2538 default:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002539 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n",
2540 -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002541 goto exit;
2542 }
2543 }
2544
2545 len = ret;
2546 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002548
2549 /* End of message should be detected according to the syntax of the
2550 * application protocol (eg HTTP), just use a dummy test here. */
2551 if( ret > 0 && buf[len-1] == '\n' )
2552 {
2553 ret = 0;
2554 break;
2555 }
2556 }
2557 while( 1 );
2558 }
2559 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 {
2561 len = sizeof( buf ) - 1;
2562 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
Hanno Becker16970d22017-10-10 15:56:37 +01002564 while( 1 )
2565 {
2566 ret = mbedtls_ssl_read( &ssl, buf, len );
2567
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002568#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002569 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002570 continue;
2571#endif
2572
Hanno Becker16970d22017-10-10 15:56:37 +01002573 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2574 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2575 break;
2576
2577 /* For event-driven IO, wait for socket to become available */
2578 if( opt.event == 1 /* level triggered IO */ )
2579 {
2580#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002581 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002582#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002583 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002584#endif
2585 }
2586 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002588 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002590 switch( ret )
2591 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002592 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002594 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02002595 goto send_request;
2596 goto exit;
2597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2599 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002600 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002601 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002603 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002605 goto exit;
2606 }
Paul Bakker5690efc2011-05-26 13:16:06 +00002607 }
2608
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02002610 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002612 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002613 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002614
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002615 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002616 * 7b. Simulate hard reset and reconnect from same port?
2617 */
2618 if( opt.reconnect_hard != 0 )
2619 {
2620 opt.reconnect_hard = 0;
2621
2622 mbedtls_printf( " . Restarting connection from same port..." );
2623 fflush( stdout );
2624
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002625#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker23699ef2019-02-26 12:36:53 +00002626 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002627#endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Becker23699ef2019-02-26 12:36:53 +00002628
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002629 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
2630 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002631 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2632 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002633 goto exit;
2634 }
2635
2636 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
2637 {
2638 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002639 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002640 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002641 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002642 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2643 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002644 goto exit;
2645 }
Hanno Becker16970d22017-10-10 15:56:37 +01002646
2647 /* For event-driven IO, wait for socket to become available */
2648 if( opt.event == 1 /* level triggered IO */ )
2649 {
2650#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002651 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002652#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002653 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002654#endif
2655 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002656 }
2657
2658 mbedtls_printf( " ok\n" );
2659
2660 goto send_request;
2661 }
2662
2663 /*
2664 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002665 */
2666 if( --opt.exchanges > 0 )
2667 goto send_request;
2668
2669 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002670 * 8. Done, cleanly close the connection
2671 */
2672close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002674 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002675
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002676 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002678 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002679 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002682
2683 /*
2684 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002685 */
2686reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002687 if( opt.reconnect != 0 )
2688 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02002689 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002690
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002691 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002692
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002693#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002694 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02002695 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002696#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002699
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002700#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckera1051b42019-02-26 11:38:29 +00002701 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002702#endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Beckera1051b42019-02-26 11:38:29 +00002703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002705 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002706 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2707 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002708 goto exit;
2709 }
2710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002712 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002713 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n",
2714 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002715 goto exit;
2716 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002717
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002718 if( ( ret = mbedtls_net_connect( &server_fd,
2719 opt.server_addr, opt.server_port,
2720 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2721 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002722 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002723 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
2724 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002725 goto exit;
2726 }
2727
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002728 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002729 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002730 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002731 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002732 if( ret != 0 )
2733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002735 -ret );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002736 goto exit;
2737 }
2738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002740 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002741 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002742 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002743 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002744 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002745 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2746 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002747 goto exit;
2748 }
2749 }
2750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002752
2753 goto send_request;
2754 }
2755
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002756 /*
2757 * Cleanup and exit
2758 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002759exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00002761 if( ret != 0 )
2762 {
2763 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 mbedtls_strerror( ret, error_buf, 100 );
2765 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002766 }
2767#endif
2768
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002769 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771#if defined(MBEDTLS_X509_CRT_PARSE_C)
2772 mbedtls_x509_crt_free( &clicert );
2773 mbedtls_x509_crt_free( &cacert );
2774 mbedtls_pk_free( &pkey );
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01002775#if defined(MBEDTLS_USE_PSA_CRYPTO)
2776 psa_destroy_key( key_slot );
2777#endif
Paul Bakkered27a042013-04-18 22:46:23 +02002778#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 mbedtls_ssl_session_free( &saved_session );
2780 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002781 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782 mbedtls_ctr_drbg_free( &ctr_drbg );
2783 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002784
Hanno Becker3f24ea92018-11-05 13:25:17 +00002785#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
2786 defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002787 if( opt.psk_opaque != 0 )
Hanno Becker3f24ea92018-11-05 13:25:17 +00002788 {
2789 /* This is ok even if the slot hasn't been
2790 * initialized (we might have jumed here
2791 * immediately because of bad cmd line params,
2792 * for example). */
Hanno Becker1d911cd2018-11-15 13:06:09 +00002793 status = psa_destroy_key( slot );
Hanno Becker3f24ea92018-11-05 13:25:17 +00002794 if( status != PSA_SUCCESS )
2795 {
2796 mbedtls_printf( "Failed to destroy key slot %u - error was %d",
Hanno Becker1d911cd2018-11-15 13:06:09 +00002797 (unsigned) slot, (int) status );
Hanno Becker3f24ea92018-11-05 13:25:17 +00002798 if( ret == 0 )
2799 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2800 }
2801 }
2802#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED &&
2803 MBEDTLS_USE_PSA_CRYPTO */
2804
Paul Bakkercce9d772011-11-18 14:26:47 +00002805#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002807 fflush( stdout ); getchar();
2808#endif
2809
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002810 // Shell can not handle large exit numbers -> 1 for errors
2811 if( ret < 0 )
2812 ret = 1;
2813
Paul Bakker5121ce52009-01-03 21:22:43 +00002814 return( ret );
2815}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
2817 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002818 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */