blob: 2e297e36219934f08ee43a6ce100c8d93d73e06d [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
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050038#define mbedtls_exit exit
39#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
40#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evansf90016a2015-01-19 14:26:37 +000041#endif
42
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020043#if !defined(MBEDTLS_ENTROPY_C) || \
44 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020045 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020046int main( void )
47{
48 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
49 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020050 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020051 return( 0 );
52}
53#else
54
Andres AG788aa4a2016-09-14 14:32:09 +010055#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/ssl.h"
57#include "mbedtls/entropy.h"
58#include "mbedtls/ctr_drbg.h"
59#include "mbedtls/certs.h"
60#include "mbedtls/x509.h"
61#include "mbedtls/error.h"
62#include "mbedtls/debug.h"
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020063#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Hanno Beckerb2b468b2018-11-12 17:46:59 +000065#if defined(MBEDTLS_USE_PSA_CRYPTO)
66#include "psa/crypto.h"
Hanno Becker1d911cd2018-11-15 13:06:09 +000067#include "mbedtls/psa_util.h"
Hanno Beckerb2b468b2018-11-12 17:46:59 +000068#endif
69
Rich Evans18b78c72015-02-11 14:06:19 +000070#include <stdio.h>
71#include <stdlib.h>
72#include <string.h>
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010073
Hanno Beckere4ad3e82017-09-18 15:05:46 +010074#define MAX_REQUEST_SIZE 20000
75#define MAX_REQUEST_SIZE_STR "20000"
76
Paul Bakker9caf2d22010-02-18 19:37:19 +000077#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010078#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020079#define DFL_SERVER_PORT "4433"
Paul Bakker9caf2d22010-02-18 19:37:19 +000080#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +020081#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +000082#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010083#define DFL_NBIO 0
Hanno Becker16970d22017-10-10 15:56:37 +010084#define DFL_EVENT 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020085#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +020086#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +000087#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000088#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000089#define DFL_CRT_FILE ""
90#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +010091#define DFL_KEY_OPAQUE 0
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020092#define DFL_PSK ""
Hanno Becker1d911cd2018-11-15 13:06:09 +000093#define DFL_PSK_OPAQUE 0
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020094#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +020095#define DFL_ECJPAKE_PW NULL
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +020096#define DFL_EC_MAX_OPS -1
Paul Bakker51936882011-02-20 16:05:58 +000097#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010099#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100100#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200101#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200102#define DFL_MIN_VERSION -1
Paul Bakker1d29fb52012-09-28 13:28:45 +0000103#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000104#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +0200105#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100106#define DFL_AUTH_MODE -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100108#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100109#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200110#define DFL_DHMLEN -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200111#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100112#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200113#define DFL_RECONNECT_HARD 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200115#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100116#define DFL_CURVES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200118#define DFL_HS_TO_MIN 0
119#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200120#define DFL_DTLS_MTU -1
Hanno Becker4d615912018-08-14 13:33:30 +0100121#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200122#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200123#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100124#define DFL_ETM -1
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200125#define DFL_CA_CALLBACK 0
126
Paul Bakker0e6975b2009-02-10 22:19:10 +0000127
Paul Bakker93c32b22014-04-25 13:40:05 +0200128#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
129#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_X509_CRT_PARSE_C)
132#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000133#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100134 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
135 " default: \"\" (pre-loaded)\n" \
136 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
137 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
138 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
139 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000140 " key_file=%%s default: \"\" (pre-loaded)\n"
141#else
142#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 " No file operations available (MBEDTLS_FS_IO not defined)\n"
144#endif /* MBEDTLS_FS_IO */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100145#else /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200146#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100148#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
149#define USAGE_KEY_OPAQUE \
150 " key_opaque=%%d Handle your private key as if it were opaque\n" \
151 " default: 0 (disabled)\n"
152#else
153#define USAGE_KEY_OPAQUE ""
154#endif
Paul Bakkered27a042013-04-18 22:46:23 +0200155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckere86964c2018-10-23 11:37:50 +0100157#define USAGE_PSK_RAW \
Paul Bakkered27a042013-04-18 22:46:23 +0200158 " psk=%%s default: \"\" (in hex, without 0x)\n" \
159 " psk_identity=%%s default: \"Client_identity\"\n"
Hanno Beckere86964c2018-10-23 11:37:50 +0100160#if defined(MBEDTLS_USE_PSA_CRYPTO)
161#define USAGE_PSK_SLOT \
Hanno Becker1d911cd2018-11-15 13:06:09 +0000162 " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
163 " Enable this to store the PSK configured through command line\n" \
164 " parameter `psk` in a PSA-based key slot.\n" \
Hanno Beckere86964c2018-10-23 11:37:50 +0100165 " Note: Currently only supported in conjunction with\n" \
166 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
167 " to force a particular PSK-only ciphersuite.\n" \
168 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
169 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
170 " with prepopulated key slots instead of importing raw key material.\n"
171#else
172#define USAGE_PSK_SLOT ""
173#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200174#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
175#define USAGE_CA_CALLBACK \
176 " ca_callback=%%d default: 0 (disabled)\n" \
177 " Enable this to use the trusted certificate callback function\n"
178#else
179#define USAGE_CA_CALLBACK ""
180#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Hanno Beckere86964c2018-10-23 11:37:50 +0100181#define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
Paul Bakkered27a042013-04-18 22:46:23 +0200182#else
183#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200187#define USAGE_TICKETS \
188 " tickets=%%d default: 1 (enabled)\n"
189#else
190#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200194#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100195 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200196#else
197#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200201#define USAGE_MAX_FRAG_LEN \
202 " max_frag_len=%%d default: 16384 (tls default)\n" \
203 " options: 512, 1024, 2048, 4096\n"
204#else
205#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100209#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200210 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100211#else
212#define USAGE_RECSPLIT
213#endif
214
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200215#if defined(MBEDTLS_DHM_C)
216#define USAGE_DHMLEN \
217 " dhmlen=%%d default: (library default: 1024 bits)\n"
218#else
219#define USAGE_DHMLEN
220#endif
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200223#define USAGE_ALPN \
224 " alpn=%%s default: \"\" (disabled)\n" \
225 " example: spdy/1,http/1.1\n"
226#else
227#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200229
Hanno Beckere6706e62017-05-15 16:05:15 +0100230#if defined(MBEDTLS_ECP_C)
231#define USAGE_CURVES \
232 " curves=a,b,c,d default: \"default\" (library default)\n" \
233 " example: \"secp521r1,brainpoolP512r1\"\n" \
234 " - use \"none\" for empty list\n" \
235 " - see mbedtls_ecp_curve_list()\n" \
236 " for acceptable curve names\n"
237#else
238#define USAGE_CURVES ""
239#endif
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200242#define USAGE_DTLS \
243 " dtls=%%d default: 0 (TLS)\n" \
244 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200245 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Becker4d615912018-08-14 13:33:30 +0100246 " mtu=%%d default: (library default: unlimited)\n" \
247 " dgram_packing=%%d default: 1 (allowed)\n" \
248 " allow or forbid packing of multiple\n" \
249 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200250#else
251#define USAGE_DTLS ""
252#endif
253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200255#define USAGE_FALLBACK \
256 " fallback=0/1 default: (library default: off)\n"
257#else
258#define USAGE_FALLBACK ""
259#endif
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200262#define USAGE_EMS \
263 " extended_ms=0/1 default: (library default: on)\n"
264#else
265#define USAGE_EMS ""
266#endif
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100269#define USAGE_ETM \
270 " etm=0/1 default: (library default: on)\n"
271#else
272#define USAGE_ETM ""
273#endif
274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100276#define USAGE_RENEGO \
277 " renegotiation=%%d default: 0 (disabled)\n" \
278 " renegotiate=%%d default: 0 (disabled)\n"
279#else
280#define USAGE_RENEGO ""
281#endif
282
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200283#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
284#define USAGE_ECJPAKE \
285 " ecjpake_pw=%%s default: none (disabled)\n"
286#else
287#define USAGE_ECJPAKE ""
288#endif
289
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200290#if defined(MBEDTLS_ECP_RESTARTABLE)
291#define USAGE_ECRESTART \
292 " ec_max_ops=%%s default: library default (restart disabled)\n"
293#else
294#define USAGE_ECRESTART ""
295#endif
296
Paul Bakker9caf2d22010-02-18 19:37:19 +0000297#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000298 "\n usage: ssl_client2 param=<>...\n" \
299 "\n acceptable parameters:\n" \
300 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100301 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000302 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000303 " request_page=%%s default: \".\"\n" \
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +0100304 " request_size=%%d default: about 34 (basic request)\n" \
305 " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
306 " If 0, in the first exchange only an empty\n" \
307 " application data message is sent followed by\n" \
308 " a second non-empty message before attempting\n" \
309 " to read a response from the server\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100310 " debug_level=%%d default: 0 (disabled)\n" \
311 " nbio=%%d default: 0 (blocking I/O)\n" \
312 " options: 1 (non-blocking), 2 (added delays)\n" \
313 " event=%%d default: 0 (loop)\n" \
314 " options: 1 (level-triggered, implies nbio=1),\n" \
315 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200316 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100317 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200318 USAGE_DTLS \
319 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100320 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100321 " options: none, optional, required\n" \
322 USAGE_IO \
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100323 USAGE_KEY_OPAQUE \
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200324 USAGE_CA_CALLBACK \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100325 "\n" \
326 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200327 USAGE_ECJPAKE \
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200328 USAGE_ECRESTART \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100329 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100330 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100331 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200332 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200333 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200334 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200335 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200336 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100337 USAGE_MAX_FRAG_LEN \
338 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200339 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200340 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200341 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100342 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100343 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100344 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200345 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000346 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100347 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200348 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200349 " min_version=%%s default: (library default: tls1)\n" \
350 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000351 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100352 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000353 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000354 " force_ciphersuite=<name> default: all enabled\n"\
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100355 " query_config=<name> return 0 if the specified\n" \
356 " configuration macro is defined and 1\n" \
357 " otherwise. The expansion of the macro\n" \
358 " is printed if it is defined\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000359 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000360
Hanno Becker8651a432017-06-09 16:13:22 +0100361#define ALPN_LIST_SIZE 10
362#define CURVE_LIST_SIZE 20
363
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500364#if defined(MBEDTLS_CHECK_PARAMS)
365#include "mbedtls/platform_util.h"
366void mbedtls_param_failed( const char *failure_condition,
367 const char *file,
368 int line )
369{
370 mbedtls_printf( "%s:%i: Input param failed - %s\n",
371 file, line, failure_condition );
372 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
373}
374#endif
375
Rich Evans85b05ec2015-02-12 11:37:29 +0000376/*
377 * global options
378 */
379struct options
380{
381 const char *server_name; /* hostname of the server (client only) */
382 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200383 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000384 int debug_level; /* level of debugging */
385 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100386 int event; /* loop or event-driven IO? level or edge triggered? */
387 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000388 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000389 const char *request_page; /* page on server to request */
390 int request_size; /* pad request with header to requested size */
391 const char *ca_file; /* the file with the CA certificate(s) */
392 const char *ca_path; /* the path with the CA certificate(s) reside */
393 const char *crt_file; /* the file with the client certificate */
394 const char *key_file; /* the file with the client key */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100395 int key_opaque; /* handle private key as if it were opaque */
Hanno Beckere86964c2018-10-23 11:37:50 +0100396#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000397 int psk_opaque;
Hanno Beckere86964c2018-10-23 11:37:50 +0100398#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200399#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
400 int use_ca_callback /* Use a callback for a trusted certificate list */
401#endif
Rich Evans85b05ec2015-02-12 11:37:29 +0000402 const char *psk; /* the pre-shared key */
403 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200404 const char *ecjpake_pw; /* the EC J-PAKE password */
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200405 int ec_max_ops; /* EC consecutive operations limit */
Rich Evans85b05ec2015-02-12 11:37:29 +0000406 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
407 int renegotiation; /* enable / disable renegotiation */
408 int allow_legacy; /* allow legacy renegotiation */
409 int renegotiate; /* attempt renegotiation? */
410 int renego_delay; /* delay before enforcing renegotiation */
411 int exchanges; /* number of data exchanges */
412 int min_version; /* minimum protocol version accepted */
413 int max_version; /* maximum protocol version accepted */
414 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200415 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000416 int auth_mode; /* verify mode for connection */
417 unsigned char mfl_code; /* code for maximum fragment length */
418 int trunc_hmac; /* negotiate truncated hmac or not */
419 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200420 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000421 int reconnect; /* attempt to resume session */
422 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200423 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000424 int tickets; /* enable / disable session tickets */
Hanno Beckere6706e62017-05-15 16:05:15 +0100425 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000426 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000427 int transport; /* TLS or DTLS? */
428 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
429 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200430 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Rich Evans85b05ec2015-02-12 11:37:29 +0000431 int fallback; /* is this a fallback connection? */
Hanno Becker4d615912018-08-14 13:33:30 +0100432 int dgram_packing; /* allow/forbid datagram packing */
Rich Evans85b05ec2015-02-12 11:37:29 +0000433 int extended_ms; /* negotiate extended master secret? */
434 int etm; /* negotiate encrypt then mac? */
435} opt;
436
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100437int query_config( const char *config );
438
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200439static void my_debug( void *ctx, int level,
440 const char *file, int line,
441 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000442{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200443 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000444
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200445 /* Extract basename from file */
446 for( p = basename = file; *p != '\0'; p++ )
447 if( *p == '/' || *p == '\\' )
448 basename = p + 1;
449
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100450 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s",
451 basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000452 fflush( (FILE *) ctx );
453}
454
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200455#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
456int ca_callback( void *data, mbedtls_x509_crt *child, mbedtls_x509_crt **candidates)
457{
458 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
459
460 mbedtls_x509_crt *first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
461 TEST_ASSERT( first != NULL);
462 TEST_ASSERT( mbedtls_x509_crt_init( first ) == 0 );
463 TEST_ASSERT( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) == 0);
464 while( ca->next != NULL )
465 {
466 ca = ca->next;
467 TEST_ASSERT( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) == 0);
468 }
469 *candidates = first;
470 return 0;
471}
472#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
473
Rich Evans85b05ec2015-02-12 11:37:29 +0000474/*
475 * Test recv/send functions that make sure each try returns
476 * WANT_READ/WANT_WRITE at least once before sucesseding
477 */
478static int my_recv( void *ctx, unsigned char *buf, size_t len )
479{
480 static int first_try = 1;
481 int ret;
482
483 if( first_try )
484 {
485 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100486 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000487 }
488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100490 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000491 first_try = 1; /* Next call will be a new operation */
492 return( ret );
493}
494
495static int my_send( void *ctx, const unsigned char *buf, size_t len )
496{
497 static int first_try = 1;
498 int ret;
499
500 if( first_try )
501 {
502 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100503 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000504 }
505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100507 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000508 first_try = 1; /* Next call will be a new operation */
509 return( ret );
510}
511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckera1051b42019-02-26 11:38:29 +0000513static unsigned char peer_crt_info[1024];
Hanno Beckera9766c2c2019-02-25 17:43:18 +0000514
Rich Evans85b05ec2015-02-12 11:37:29 +0000515/*
516 * Enabled if debug_level > 1 in code below
517 */
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100518static int my_verify( void *data, mbedtls_x509_crt *crt,
519 int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000520{
521 char buf[1024];
522 ((void) data);
523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Hanno Beckera9766c2c2019-02-25 17:43:18 +0000525 if( depth == 0 )
526 memcpy( peer_crt_info, buf, sizeof( buf ) );
527
528 if( opt.debug_level == 0 )
529 return( 0 );
530
531 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000533
Rich Evans85b05ec2015-02-12 11:37:29 +0000534 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100536 else
537 {
538 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
539 mbedtls_printf( "%s\n", buf );
540 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000541
542 return( 0 );
543}
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200544
545static int ssl_sig_hashes_for_test[] = {
546#if defined(MBEDTLS_SHA512_C)
547 MBEDTLS_MD_SHA512,
548 MBEDTLS_MD_SHA384,
549#endif
550#if defined(MBEDTLS_SHA256_C)
551 MBEDTLS_MD_SHA256,
552 MBEDTLS_MD_SHA224,
553#endif
554#if defined(MBEDTLS_SHA1_C)
555 /* Allow SHA-1 as we use it extensively in tests. */
556 MBEDTLS_MD_SHA1,
557#endif
558 MBEDTLS_MD_NONE
559};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000561
Hanno Becker16970d22017-10-10 15:56:37 +0100562/*
563 * Wait for an event from the underlying transport or the timer
564 * (Used in event-driven IO mode).
565 */
566#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000567int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000568 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100569#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000570int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000571 mbedtls_timing_delay_context *timer,
572 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100573#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000574{
Hanno Becker16970d22017-10-10 15:56:37 +0100575
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000576 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +0100577 int poll_type = 0;
578
579 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
580 poll_type = MBEDTLS_NET_POLL_WRITE;
581 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
582 poll_type = MBEDTLS_NET_POLL_READ;
583#if !defined(MBEDTLS_TIMING_C)
584 else
Hanno Beckeref527962018-03-15 15:49:24 +0000585 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100586#endif
587
588 while( 1 )
589 {
Hanno Becker197a91c2017-10-31 10:58:53 +0000590 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +0100591#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +0000592 if( timer != NULL &&
593 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +0100594 {
Hanno Becker16970d22017-10-10 15:56:37 +0100595 break;
596 }
Hanno Becker197a91c2017-10-31 10:58:53 +0000597#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +0100598
Hanno Becker197a91c2017-10-31 10:58:53 +0000599 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000600 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +0100601 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000602 ret = mbedtls_net_poll( fd, poll_type, 0 );
603 if( ret < 0 )
604 return( ret );
605 if( ret == poll_type )
606 break;
Hanno Becker16970d22017-10-10 15:56:37 +0100607 }
608 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000609
610 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100611}
612
Paul Bakker9caf2d22010-02-18 19:37:19 +0000613int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000614{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200615 int ret = 0, len, tail_len, i, written, frags, retry_left;
616 mbedtls_net_context server_fd;
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100617
618 unsigned char buf[MAX_REQUEST_SIZE + 1];
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
621 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200622 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200623#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100625 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200626#endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100627#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +0100628 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100629 const mbedtls_ecp_curve_info *curve_cur;
630#endif
631
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200632 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000633
Hanno Beckere86964c2018-10-23 11:37:50 +0100634#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500635 psa_key_handle_t slot = 0;
Hanno Beckere86964c2018-10-23 11:37:50 +0100636 psa_algorithm_t alg = 0;
637 psa_key_policy_t policy;
638 psa_status_t status;
639#endif
640
Gilles Peskineef86ab22017-05-05 18:59:02 +0200641#if defined(MBEDTLS_X509_CRT_PARSE_C)
642 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
643#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_entropy_context entropy;
645 mbedtls_ctr_drbg_context ctr_drbg;
646 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200647 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200649#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200650 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200651#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200653 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_x509_crt cacert;
655 mbedtls_x509_crt clicert;
656 mbedtls_pk_context pkey;
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +0100657#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500658 psa_key_handle_t key_slot = 0; /* invalid key slot */
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +0100659#endif
Paul Bakkered27a042013-04-18 22:46:23 +0200660#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000661 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000662 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000663
Paul Bakker1a207ec2011-02-06 13:22:40 +0000664 /*
665 * Make sure memory references are valid.
666 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200667 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200668 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200669 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200671 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672#if defined(MBEDTLS_X509_CRT_PARSE_C)
673 mbedtls_x509_crt_init( &cacert );
674 mbedtls_x509_crt_init( &clicert );
675 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200676#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200678 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200679#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000680
Hanno Beckerb2b468b2018-11-12 17:46:59 +0000681#if defined(MBEDTLS_USE_PSA_CRYPTO)
682 status = psa_crypto_init();
683 if( status != PSA_SUCCESS )
684 {
685 mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
686 (int) status );
687 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
688 goto exit;
689 }
690#endif
691
Paul Bakker9caf2d22010-02-18 19:37:19 +0000692 if( argc == 0 )
693 {
694 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000695 if( ret == 0 )
696 ret = 1;
697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000701 while( *list )
702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200704 list++;
705 if( !*list )
706 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000708 list++;
709 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000711 goto exit;
712 }
713
714 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100715 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000716 opt.server_port = DFL_SERVER_PORT;
717 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100718 opt.nbio = DFL_NBIO;
Hanno Becker16970d22017-10-10 15:56:37 +0100719 opt.event = DFL_EVENT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200720 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200721 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000722 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200723 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000724 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000725 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000726 opt.crt_file = DFL_CRT_FILE;
727 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100728 opt.key_opaque = DFL_KEY_OPAQUE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200729 opt.psk = DFL_PSK;
Hanno Beckere86964c2018-10-23 11:37:50 +0100730#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000731 opt.psk_opaque = DFL_PSK_OPAQUE;
Hanno Beckere86964c2018-10-23 11:37:50 +0100732#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200733#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
734 opt.ca_callback = DFL_CA_CALLBACK;
735#endif
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200736 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200737 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200738 opt.ec_max_ops = DFL_EC_MAX_OPS;
Paul Bakker51936882011-02-20 16:05:58 +0000739 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000740 opt.renegotiation = DFL_RENEGOTIATION;
741 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100742 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200743 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000744 opt.min_version = DFL_MIN_VERSION;
745 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100746 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +0200747 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100748 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200749 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200750 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100751 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200752 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200753 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100754 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200755 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200756 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200757 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +0100758 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200759 opt.transport = DFL_TRANSPORT;
760 opt.hs_to_min = DFL_HS_TO_MIN;
761 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200762 opt.dtls_mtu = DFL_DTLS_MTU;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200763 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200764 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100765 opt.etm = DFL_ETM;
Hanno Becker4d615912018-08-14 13:33:30 +0100766 opt.dgram_packing = DFL_DGRAM_PACKING;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000767
768 for( i = 1; i < argc; i++ )
769 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000770 p = argv[i];
771 if( ( q = strchr( p, '=' ) ) == NULL )
772 goto usage;
773 *q++ = '\0';
774
775 if( strcmp( p, "server_name" ) == 0 )
776 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100777 else if( strcmp( p, "server_addr" ) == 0 )
778 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000779 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200780 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100781 else if( strcmp( p, "dtls" ) == 0 )
782 {
783 int t = atoi( q );
784 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100786 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100788 else
789 goto usage;
790 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000791 else if( strcmp( p, "debug_level" ) == 0 )
792 {
793 opt.debug_level = atoi( q );
794 if( opt.debug_level < 0 || opt.debug_level > 65535 )
795 goto usage;
796 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100797 else if( strcmp( p, "nbio" ) == 0 )
798 {
799 opt.nbio = atoi( q );
800 if( opt.nbio < 0 || opt.nbio > 2 )
801 goto usage;
802 }
Hanno Becker16970d22017-10-10 15:56:37 +0100803 else if( strcmp( p, "event" ) == 0 )
804 {
805 opt.event = atoi( q );
806 if( opt.event < 0 || opt.event > 2 )
807 goto usage;
808 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200809 else if( strcmp( p, "read_timeout" ) == 0 )
810 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200811 else if( strcmp( p, "max_resend" ) == 0 )
812 {
813 opt.max_resend = atoi( q );
814 if( opt.max_resend < 0 )
815 goto usage;
816 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000817 else if( strcmp( p, "request_page" ) == 0 )
818 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200819 else if( strcmp( p, "request_size" ) == 0 )
820 {
821 opt.request_size = atoi( q );
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100822 if( opt.request_size < 0 ||
823 opt.request_size > MAX_REQUEST_SIZE )
Paul Bakker93c32b22014-04-25 13:40:05 +0200824 goto usage;
825 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000826 else if( strcmp( p, "ca_file" ) == 0 )
827 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000828 else if( strcmp( p, "ca_path" ) == 0 )
829 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000830 else if( strcmp( p, "crt_file" ) == 0 )
831 opt.crt_file = q;
832 else if( strcmp( p, "key_file" ) == 0 )
833 opt.key_file = q;
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100834#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
835 else if( strcmp( p, "key_opaque" ) == 0 )
836 opt.key_opaque = atoi( q );
837#endif
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200838 else if( strcmp( p, "psk" ) == 0 )
839 opt.psk = q;
Hanno Beckere86964c2018-10-23 11:37:50 +0100840#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000841 else if( strcmp( p, "psk_opaque" ) == 0 )
842 opt.psk_opaque = atoi( q );
Hanno Beckere86964c2018-10-23 11:37:50 +0100843#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200844#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
845 else if( strcmp( p, "ca_callback" ) == 0)
846 opt.ca_callback = atoi( q );
847#endif
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200848 else if( strcmp( p, "psk_identity" ) == 0 )
849 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200850 else if( strcmp( p, "ecjpake_pw" ) == 0 )
851 opt.ecjpake_pw = q;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200852 else if( strcmp( p, "ec_max_ops" ) == 0 )
853 opt.ec_max_ops = atoi( q );
Paul Bakker51936882011-02-20 16:05:58 +0000854 else if( strcmp( p, "force_ciphersuite" ) == 0 )
855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +0000857
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200858 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000859 {
860 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000861 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000862 }
Paul Bakker51936882011-02-20 16:05:58 +0000863 opt.force_ciphersuite[1] = 0;
864 }
Paul Bakker48916f92012-09-16 19:57:18 +0000865 else if( strcmp( p, "renegotiation" ) == 0 )
866 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100867 opt.renegotiation = (atoi( q )) ?
868 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
869 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +0000870 }
871 else if( strcmp( p, "allow_legacy" ) == 0 )
872 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100873 switch( atoi( q ) )
874 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100875 case -1:
876 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
877 break;
878 case 0:
879 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
880 break;
881 case 1:
882 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
883 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100884 default: goto usage;
885 }
Paul Bakker48916f92012-09-16 19:57:18 +0000886 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100887 else if( strcmp( p, "renegotiate" ) == 0 )
888 {
889 opt.renegotiate = atoi( q );
890 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
891 goto usage;
892 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200893 else if( strcmp( p, "exchanges" ) == 0 )
894 {
895 opt.exchanges = atoi( q );
896 if( opt.exchanges < 1 )
897 goto usage;
898 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200899 else if( strcmp( p, "reconnect" ) == 0 )
900 {
901 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200902 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200903 goto usage;
904 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100905 else if( strcmp( p, "reco_delay" ) == 0 )
906 {
907 opt.reco_delay = atoi( q );
908 if( opt.reco_delay < 0 )
909 goto usage;
910 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200911 else if( strcmp( p, "reconnect_hard" ) == 0 )
912 {
913 opt.reconnect_hard = atoi( q );
914 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
915 goto usage;
916 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200917 else if( strcmp( p, "tickets" ) == 0 )
918 {
919 opt.tickets = atoi( q );
920 if( opt.tickets < 0 || opt.tickets > 2 )
921 goto usage;
922 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200923 else if( strcmp( p, "alpn" ) == 0 )
924 {
925 opt.alpn_string = q;
926 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200927 else if( strcmp( p, "fallback" ) == 0 )
928 {
929 switch( atoi( q ) )
930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
932 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200933 default: goto usage;
934 }
935 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200936 else if( strcmp( p, "extended_ms" ) == 0 )
937 {
938 switch( atoi( q ) )
939 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100940 case 0:
941 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
942 break;
943 case 1:
944 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
945 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200946 default: goto usage;
947 }
948 }
Hanno Beckere6706e62017-05-15 16:05:15 +0100949 else if( strcmp( p, "curves" ) == 0 )
950 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100951 else if( strcmp( p, "etm" ) == 0 )
952 {
953 switch( atoi( q ) )
954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
956 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100957 default: goto usage;
958 }
959 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000960 else if( strcmp( p, "min_version" ) == 0 )
961 {
962 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000964 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100966 else if( strcmp( q, "tls1_1" ) == 0 ||
967 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100969 else if( strcmp( q, "tls1_2" ) == 0 ||
970 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000972 else
973 goto usage;
974 }
975 else if( strcmp( p, "max_version" ) == 0 )
976 {
977 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000979 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100981 else if( strcmp( q, "tls1_1" ) == 0 ||
982 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100984 else if( strcmp( q, "tls1_2" ) == 0 ||
985 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000987 else
988 goto usage;
989 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100990 else if( strcmp( p, "arc4" ) == 0 )
991 {
992 switch( atoi( q ) )
993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
995 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100996 default: goto usage;
997 }
998 }
Gilles Peskinebc70a182017-05-09 15:59:24 +0200999 else if( strcmp( p, "allow_sha1" ) == 0 )
1000 {
1001 switch( atoi( q ) )
1002 {
1003 case 0: opt.allow_sha1 = 0; break;
1004 case 1: opt.allow_sha1 = 1; break;
1005 default: goto usage;
1006 }
1007 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001008 else if( strcmp( p, "force_version" ) == 0 )
1009 {
1010 if( strcmp( q, "ssl3" ) == 0 )
1011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1013 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001014 }
1015 else if( strcmp( q, "tls1" ) == 0 )
1016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1018 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001019 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001020 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1023 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001024 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001025 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1028 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001029 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001030 else if( strcmp( q, "dtls1" ) == 0 )
1031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1033 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1034 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001035 }
1036 else if( strcmp( q, "dtls1_2" ) == 0 )
1037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1039 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1040 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001041 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001042 else
1043 goto usage;
1044 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001045 else if( strcmp( p, "auth_mode" ) == 0 )
1046 {
1047 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001049 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001051 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001053 else
1054 goto usage;
1055 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001056 else if( strcmp( p, "max_frag_len" ) == 0 )
1057 {
1058 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001060 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001062 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001064 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001066 else
1067 goto usage;
1068 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001069 else if( strcmp( p, "trunc_hmac" ) == 0 )
1070 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001071 switch( atoi( q ) )
1072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1074 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001075 default: goto usage;
1076 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001077 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001078 else if( strcmp( p, "hs_timeout" ) == 0 )
1079 {
1080 if( ( p = strchr( q, '-' ) ) == NULL )
1081 goto usage;
1082 *p++ = '\0';
1083 opt.hs_to_min = atoi( q );
1084 opt.hs_to_max = atoi( p );
1085 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1086 goto usage;
1087 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001088 else if( strcmp( p, "mtu" ) == 0 )
1089 {
1090 opt.dtls_mtu = atoi( q );
1091 if( opt.dtls_mtu < 0 )
1092 goto usage;
1093 }
Hanno Becker4d615912018-08-14 13:33:30 +01001094 else if( strcmp( p, "dgram_packing" ) == 0 )
1095 {
1096 opt.dgram_packing = atoi( q );
1097 if( opt.dgram_packing != 0 &&
1098 opt.dgram_packing != 1 )
1099 {
1100 goto usage;
1101 }
1102 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001103 else if( strcmp( p, "recsplit" ) == 0 )
1104 {
1105 opt.recsplit = atoi( q );
1106 if( opt.recsplit < 0 || opt.recsplit > 1 )
1107 goto usage;
1108 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001109 else if( strcmp( p, "dhmlen" ) == 0 )
1110 {
1111 opt.dhmlen = atoi( q );
1112 if( opt.dhmlen < 0 )
1113 goto usage;
1114 }
Andres Amaya Garciabc818842018-10-16 21:08:38 +01001115 else if( strcmp( p, "query_config" ) == 0 )
1116 {
1117 return query_config( q );
1118 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001119 else
1120 goto usage;
1121 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001122
Hanno Becker16970d22017-10-10 15:56:37 +01001123 /* Event-driven IO is incompatible with the above custom
1124 * receive and send functions, as the polling builds on
1125 * refers to the underlying net_context. */
1126 if( opt.event == 1 && opt.nbio != 1 )
1127 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001128 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001129 opt.nbio = 1;
1130 }
1131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132#if defined(MBEDTLS_DEBUG_C)
1133 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001134#endif
1135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001137 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001138 * Unhexify the pre-shared key if any is given
1139 */
1140 if( strlen( opt.psk ) )
1141 {
1142 unsigned char c;
1143 size_t j;
1144
1145 if( strlen( opt.psk ) % 2 != 0 )
1146 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001147 mbedtls_printf( "pre-shared key not valid hex\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001148 goto exit;
1149 }
1150
1151 psk_len = strlen( opt.psk ) / 2;
1152
1153 for( j = 0; j < strlen( opt.psk ); j += 2 )
1154 {
1155 c = opt.psk[j];
1156 if( c >= '0' && c <= '9' )
1157 c -= '0';
1158 else if( c >= 'a' && c <= 'f' )
1159 c -= 'a' - 10;
1160 else if( c >= 'A' && c <= 'F' )
1161 c -= 'A' - 10;
1162 else
1163 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001164 mbedtls_printf( "pre-shared key not valid hex\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001165 goto exit;
1166 }
1167 psk[ j / 2 ] = c << 4;
1168
1169 c = opt.psk[j + 1];
1170 if( c >= '0' && c <= '9' )
1171 c -= '0';
1172 else if( c >= 'a' && c <= 'f' )
1173 c -= 'a' - 10;
1174 else if( c >= 'A' && c <= 'F' )
1175 c -= 'A' - 10;
1176 else
1177 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001178 mbedtls_printf( "pre-shared key not valid hex\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001179 goto exit;
1180 }
1181 psk[ j / 2 ] |= c;
1182 }
1183 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001185
Hanno Beckere86964c2018-10-23 11:37:50 +01001186
1187#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001188 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001189 {
1190 if( opt.psk == NULL )
1191 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00001192 mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
Hanno Beckere86964c2018-10-23 11:37:50 +01001193 ret = 2;
1194 goto usage;
1195 }
1196
1197 if( opt.force_ciphersuite[0] <= 0 )
1198 {
1199 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" );
1200 ret = 2;
1201 goto usage;
1202 }
1203 }
1204#endif /* MBEDTLS_USE_PSA_CRYPTO */
1205
1206 if( opt.force_ciphersuite[0] > 0 )
1207 {
1208 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1209 ciphersuite_info =
1210 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1211
1212 if( opt.max_version != -1 &&
1213 ciphersuite_info->min_minor_ver > opt.max_version )
1214 {
1215 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
1216 ret = 2;
1217 goto usage;
1218 }
1219 if( opt.min_version != -1 &&
1220 ciphersuite_info->max_minor_ver < opt.min_version )
1221 {
1222 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
1223 ret = 2;
1224 goto usage;
1225 }
1226
1227 /* If the server selects a version that's not supported by
1228 * this suite, then there will be no common ciphersuite... */
1229 if( opt.max_version == -1 ||
1230 opt.max_version > ciphersuite_info->max_minor_ver )
1231 {
1232 opt.max_version = ciphersuite_info->max_minor_ver;
1233 }
1234 if( opt.min_version < ciphersuite_info->min_minor_ver )
1235 {
1236 opt.min_version = ciphersuite_info->min_minor_ver;
1237 /* DTLS starts with TLS 1.1 */
1238 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1239 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1240 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1241 }
1242
1243 /* Enable RC4 if needed and not explicitly disabled */
1244 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
1245 {
1246 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
1247 {
1248 mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" );
1249 ret = 2;
1250 goto usage;
1251 }
1252
1253 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
1254 }
1255
1256#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001257 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001258 {
1259 /* Ensure that the chosen ciphersuite is PSK-only; we must know
1260 * the ciphersuite in advance to set the correct policy for the
1261 * PSK key slot. This limitation might go away in the future. */
1262 if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
1263 opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 )
1264 {
1265 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" );
1266 ret = 2;
1267 goto usage;
1268 }
1269
1270 /* Determine KDF algorithm the opaque PSK will be used in. */
1271#if defined(MBEDTLS_SHA512_C)
1272 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1273 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1274 else
1275#endif /* MBEDTLS_SHA512_C */
1276 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1277 }
1278#endif /* MBEDTLS_USE_PSA_CRYPTO */
1279 }
1280
Hanno Beckere6706e62017-05-15 16:05:15 +01001281#if defined(MBEDTLS_ECP_C)
1282 if( opt.curves != NULL )
1283 {
1284 p = (char *) opt.curves;
1285 i = 0;
1286
1287 if( strcmp( p, "none" ) == 0 )
1288 {
1289 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1290 }
1291 else if( strcmp( p, "default" ) != 0 )
1292 {
1293 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001294 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001295 {
1296 q = p;
1297
1298 /* Terminate the current string */
1299 while( *p != ',' && *p != '\0' )
1300 p++;
1301 if( *p == ',' )
1302 *p++ = '\0';
1303
1304 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1305 {
1306 curve_list[i++] = curve_cur->grp_id;
1307 }
1308 else
1309 {
1310 mbedtls_printf( "unknown curve %s\n", q );
1311 mbedtls_printf( "supported curves: " );
1312 for( curve_cur = mbedtls_ecp_curve_list();
1313 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1314 curve_cur++ )
1315 {
1316 mbedtls_printf( "%s ", curve_cur->name );
1317 }
1318 mbedtls_printf( "\n" );
1319 goto exit;
1320 }
1321 }
1322
1323 mbedtls_printf("Number of curves: %d\n", i );
1324
Hanno Becker8651a432017-06-09 16:13:22 +01001325 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001326 {
Hanno Becker8651a432017-06-09 16:13:22 +01001327 mbedtls_printf( "curves list too long, maximum %d",
1328 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001329 goto exit;
1330 }
1331
1332 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1333 }
1334 }
1335#endif /* MBEDTLS_ECP_C */
1336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001338 if( opt.alpn_string != NULL )
1339 {
1340 p = (char *) opt.alpn_string;
1341 i = 0;
1342
1343 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001344 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001345 {
1346 alpn_list[i++] = p;
1347
1348 /* Terminate the current string and move on to next one */
1349 while( *p != ',' && *p != '\0' )
1350 p++;
1351 if( *p == ',' )
1352 *p++ = '\0';
1353 }
1354 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001356
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001357 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 * 0. Initialize the RNG and the session data
1359 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001361 fflush( stdout );
1362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 mbedtls_entropy_init( &entropy );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001364 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1365 &entropy, (const unsigned char *) pers,
1366 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +00001367 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001368 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
1369 -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001370 goto exit;
1371 }
1372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 /*
1377 * 1.1. Load the trusted CA
1378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001380 fflush( stdout );
1381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001383 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001384 if( strcmp( opt.ca_path, "none" ) == 0 )
1385 ret = 0;
1386 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001388 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001389 if( strcmp( opt.ca_file, "none" ) == 0 )
1390 ret = 0;
1391 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001393 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001394#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#if defined(MBEDTLS_CERTS_C)
1396 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 ret = mbedtls_x509_crt_parse( &cacert,
1399 (const unsigned char *) mbedtls_test_cas[i],
1400 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001401 if( ret != 0 )
1402 break;
1403 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001404#else
1405 {
1406 ret = 1;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001407 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001408 }
1409#endif
Paul Bakker42488232012-05-16 08:21:05 +00001410 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001411 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001412 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1413 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 goto exit;
1415 }
1416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001418
1419 /*
1420 * 1.2. Load own certificate and private key
1421 *
1422 * (can be skipped if client authentication is not required)
1423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425 fflush( stdout );
1426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001428 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001429 if( strcmp( opt.crt_file, "none" ) == 0 )
1430 ret = 0;
1431 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001433 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001434#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001436 ret = mbedtls_x509_crt_parse( &clicert,
1437 (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001439#else
1440 {
1441 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001443 }
1444#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001445 if( ret != 0 )
1446 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001447 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1448 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001449 goto exit;
1450 }
1451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001453 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001454 if( strcmp( opt.key_file, "none" ) == 0 )
1455 ret = 0;
1456 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001458 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001459#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001461 ret = mbedtls_pk_parse_key( &pkey,
1462 (const unsigned char *) mbedtls_test_cli_key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001464#else
1465 {
1466 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001468 }
1469#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001470 if( ret != 0 )
1471 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001472 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
1473 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001474 goto exit;
1475 }
1476
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001477#if defined(MBEDTLS_USE_PSA_CRYPTO)
1478 if( opt.key_opaque != 0 )
1479 {
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001480 if( ( ret = mbedtls_pk_wrap_as_opaque( &pkey, &key_slot,
1481 PSA_ALG_SHA_256 ) ) != 0 )
1482 {
1483 mbedtls_printf( " failed\n ! "
1484 "mbedtls_pk_wrap_as_opaque returned -0x%x\n\n", -ret );
1485 goto exit;
1486 }
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001487 }
1488#endif /* MBEDTLS_USE_PSA_CRYPTO */
1489
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001490 mbedtls_printf( " ok (key type: %s)\n", mbedtls_pk_get_name( &pkey ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001492
1493 /*
1494 * 2. Start the connection
1495 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001496 if( opt.server_addr == NULL)
1497 opt.server_addr = opt.server_name;
1498
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001499 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001501 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001502 fflush( stdout );
1503
Hanno Becker16970d22017-10-10 15:56:37 +01001504 if( ( ret = mbedtls_net_connect( &server_fd,
1505 opt.server_addr, opt.server_port,
1506 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1507 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001508 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001509 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
1510 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 goto exit;
1512 }
1513
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001514 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001515 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001516 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001517 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001518 if( ret != 0 )
1519 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001520 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
1521 -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001522 goto exit;
1523 }
1524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001526
1527 /*
1528 * 3. Setup stuff
1529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001531 fflush( stdout );
1532
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001533 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1534 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001535 opt.transport,
1536 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001537 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001538 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
1539 -ret );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001540 goto exit;
1541 }
1542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001544 /* The default algorithms profile disables SHA-1, but our tests still
1545 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001546 if( opt.allow_sha1 > 0 )
1547 {
1548 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1549 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1550 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1551 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001552
Hanno Beckera9766c2c2019-02-25 17:43:18 +00001553 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
Hanno Beckera1051b42019-02-26 11:38:29 +00001554 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Gilles Peskineef86ab22017-05-05 18:59:02 +02001555#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001556
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001557 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001558 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001561 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Hanno Becker16970d22017-10-10 15:56:37 +01001562 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min,
1563 opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001564
Hanno Becker4d615912018-08-14 13:33:30 +01001565 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01001566 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001570 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001571 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001572 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n",
1573 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001574 goto exit;
1575 }
Paul Bakker05decb22013-08-15 13:33:48 +02001576#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001579 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001580 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001581#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001584 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001585 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001586#endif
1587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001589 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001590 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001591#endif
1592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001594 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001595 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Hanno Becker16970d22017-10-10 15:56:37 +01001596 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1597 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001598#endif
1599
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001600#if defined(MBEDTLS_DHM_C)
1601 if( opt.dhmlen != DFL_DHMLEN )
1602 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1603#endif
1604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001606 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001607 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001608 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001609 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
1610 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001611 goto exit;
1612 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001613#endif
1614
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001615 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1616 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001617
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001618 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02001621 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02001622#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001623
Paul Bakker645ce3a2012-10-31 12:32:41 +00001624 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001625 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001626
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001627#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001628 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001629 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001630#endif
Paul Bakker51936882011-02-20 16:05:58 +00001631
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001632 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001633 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001635 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001636#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001639 if( strcmp( opt.ca_path, "none" ) != 0 &&
1640 strcmp( opt.ca_file, "none" ) != 0 )
1641 {
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001642#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1643 if( opt.ca_callback != 0 )
1644 mbedtls_ssl_conf_ca_cb( &conf, ca_callback, &cacert);
1645 else
1646#endif
1647 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001648 }
1649 if( strcmp( opt.crt_file, "none" ) != 0 &&
1650 strcmp( opt.key_file, "none" ) != 0 )
1651 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001652 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001653 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001654 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
1655 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001656 goto exit;
1657 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001658 }
Paul Bakkered27a042013-04-18 22:46:23 +02001659#endif
1660
Hanno Beckere6706e62017-05-15 16:05:15 +01001661#if defined(MBEDTLS_ECP_C)
1662 if( opt.curves != NULL &&
1663 strcmp( opt.curves, "default" ) != 0 )
1664 {
1665 mbedtls_ssl_conf_curves( &conf, curve_list );
1666 }
1667#endif
1668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckere86964c2018-10-23 11:37:50 +01001670#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001671 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001672 {
1673 /* The algorithm has already been determined earlier. */
Hanno Becker37519ea2019-01-25 14:26:01 +00001674 status = psa_allocate_key( &slot );
Hanno Becker1d911cd2018-11-15 13:06:09 +00001675 if( status != PSA_SUCCESS )
1676 {
1677 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1678 goto exit;
1679 }
Hanno Beckere86964c2018-10-23 11:37:50 +01001680
Hanno Becker13871242019-01-25 14:26:26 +00001681 policy = psa_key_policy_init();
Hanno Beckere86964c2018-10-23 11:37:50 +01001682 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
1683
1684 status = psa_set_key_policy( slot, &policy );
1685 if( status != PSA_SUCCESS )
1686 {
1687 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1688 goto exit;
1689 }
1690
1691 status = psa_import_key( slot, PSA_KEY_TYPE_DERIVE, psk, psk_len );
1692 if( status != PSA_SUCCESS )
1693 {
1694 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1695 goto exit;
1696 }
1697
1698 if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, slot,
1699 (const unsigned char *) opt.psk_identity,
Hanno Becker5cd607b2018-11-05 12:52:42 +00001700 strlen( opt.psk_identity ) ) ) != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001701 {
1702 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
1703 ret );
1704 goto exit;
1705 }
1706 }
1707 else
1708#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001709 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001710 (const unsigned char *) opt.psk_identity,
1711 strlen( opt.psk_identity ) ) ) != 0 )
1712 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001713 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n",
1714 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001715 goto exit;
1716 }
Hanno Beckere86964c2018-10-23 11:37:50 +01001717#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02001718
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001719 if( opt.min_version != DFL_MIN_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01001720 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1721 opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001722
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001723 if( opt.max_version != DFL_MAX_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01001724 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1725 opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001728 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001729 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001730#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001731
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001732 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
1733 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001734 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
1735 -ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001736 goto exit;
1737 }
1738
1739#if defined(MBEDTLS_X509_CRT_PARSE_C)
1740 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1741 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001742 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
1743 ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001744 goto exit;
1745 }
1746#endif
1747
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001748#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1749 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
1750 {
1751 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
1752 (const unsigned char *) opt.ecjpake_pw,
1753 strlen( opt.ecjpake_pw ) ) ) != 0 )
1754 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001755 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
1756 ret );
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001757 goto exit;
1758 }
1759 }
1760#endif
1761
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001762 if( opt.nbio == 2 )
1763 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
1764 else
Hanno Becker16970d22017-10-10 15:56:37 +01001765 mbedtls_ssl_set_bio( &ssl, &server_fd,
1766 mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02001767 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001768
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001769#if defined(MBEDTLS_SSL_PROTO_DTLS)
1770 if( opt.dtls_mtu != DFL_DTLS_MTU )
1771 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
1772#endif
1773
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001774#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001775 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
1776 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001777#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001778
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001779#if defined(MBEDTLS_ECP_RESTARTABLE)
1780 if( opt.ec_max_ops != DFL_EC_MAX_OPS )
1781 mbedtls_ecp_set_max_ops( opt.ec_max_ops );
1782#endif
1783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001785
Paul Bakker5121ce52009-01-03 21:22:43 +00001786 /*
1787 * 4. Handshake
1788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001790 fflush( stdout );
1791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001793 {
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001794 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1795 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001796 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Paul Bakker40e46942009-01-03 21:51:57 +00001797 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001798 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
1799 -ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
1801 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001802 " Unable to verify the server's certificate. "
1803 "Either it is invalid,\n"
1804 " or you didn't set ca_file or ca_path "
1805 "to an appropriate value.\n"
1806 " Alternatively, you may want to use "
1807 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001809 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00001810 }
Hanno Becker16970d22017-10-10 15:56:37 +01001811
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001812#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02001813 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
1814 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001815#endif
1816
Hanno Becker16970d22017-10-10 15:56:37 +01001817 /* For event-driven IO, wait for socket to become available */
1818 if( opt.event == 1 /* level triggered IO */ )
1819 {
1820#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001821 ret = idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001822#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001823 ret = idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001824#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001825 if( ret != 0 )
1826 goto exit;
Hanno Becker16970d22017-10-10 15:56:37 +01001827 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001828 }
1829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Hanno Becker16970d22017-10-10 15:56:37 +01001831 mbedtls_ssl_get_version( &ssl ),
1832 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
1835 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001836 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001838
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02001839#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1840 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
1841 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
1842#endif
1843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001845 if( opt.alpn_string != NULL )
1846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
1848 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001849 alp ? alp : "(none)" );
1850 }
1851#endif
1852
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001853 if( opt.reconnect != 0 )
1854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001856 fflush( stdout );
1857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001859 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001860 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
1861 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001862 goto exit;
1863 }
1864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001866 }
1867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001869 /*
1870 * 5. Verify the server certificate
1871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001873
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001874 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001876 char vrfy_buf[512];
1877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001879
Hanno Becker16970d22017-10-10 15:56:37 +01001880 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ),
1881 " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00001882
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001883 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001884 }
1885 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001887
Hanno Beckera9766c2c2019-02-25 17:43:18 +00001888 mbedtls_printf( " . Peer certificate information ...\n" );
1889 mbedtls_printf( "%s\n", peer_crt_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001893 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001894 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001895 /*
1896 * Perform renegotiation (this must be done when the server is waiting
1897 * for input from our side).
1898 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001900 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001902 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001903 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001904 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001905 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001906 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001907 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
1908 ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001909 goto exit;
1910 }
Hanno Becker16970d22017-10-10 15:56:37 +01001911
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02001912#if defined(MBEDTLS_ECP_RESTARTABLE)
1913 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
1914 continue;
1915#endif
1916
Hanno Becker16970d22017-10-10 15:56:37 +01001917 /* For event-driven IO, wait for socket to become available */
1918 if( opt.event == 1 /* level triggered IO */ )
1919 {
1920#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001921 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001922#else
Hanno Becker197a91c2017-10-31 10:58:53 +00001923 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001924#endif
1925 }
1926
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001927 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001929 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001931
Paul Bakker5121ce52009-01-03 21:22:43 +00001932 /*
1933 * 6. Write the GET request
1934 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001935 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001936send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001938 fflush( stdout );
1939
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001940 len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST,
1941 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001942 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001943
1944 /* Add padding to GET request to reach opt.request_size in length */
1945 if( opt.request_size != DFL_REQUEST_SIZE &&
1946 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001947 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001948 memset( buf + len, 'A', opt.request_size - len - tail_len );
1949 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001950 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001951
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001952 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001953 len += tail_len;
1954
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001955 /* Truncate if request size is smaller than the "natural" size */
1956 if( opt.request_size != DFL_REQUEST_SIZE &&
1957 len > opt.request_size )
1958 {
1959 len = opt.request_size;
1960
1961 /* Still end with \r\n unless that's really not possible */
1962 if( len >= 2 ) buf[len - 2] = '\r';
1963 if( len >= 1 ) buf[len - 1] = '\n';
1964 }
1965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 {
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001968 written = 0;
1969 frags = 0;
1970
1971 do
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 {
Hanno Becker16970d22017-10-10 15:56:37 +01001973 while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001974 len - written ) ) < 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001975 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001976 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001977 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001978 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001979 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001980 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
1981 -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001982 goto exit;
1983 }
Hanno Becker16970d22017-10-10 15:56:37 +01001984
1985 /* For event-driven IO, wait for socket to become available */
1986 if( opt.event == 1 /* level triggered IO */ )
1987 {
1988#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001989 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001990#else
Hanno Becker197a91c2017-10-31 10:58:53 +00001991 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001992#endif
1993 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001994 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001995
1996 frags++;
1997 written += ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001998 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001999 while( written < len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002000 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002001 else /* Not stream, so datagram */
2002 {
Hanno Becker16970d22017-10-10 15:56:37 +01002003 while( 1 )
2004 {
2005 ret = mbedtls_ssl_write( &ssl, buf, len );
2006
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002007#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002008 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002009 continue;
2010#endif
2011
Hanno Becker16970d22017-10-10 15:56:37 +01002012 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2013 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2014 break;
2015
2016 /* For event-driven IO, wait for socket to become available */
2017 if( opt.event == 1 /* level triggered IO */ )
2018 {
2019#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002020 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002021#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002022 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002023#endif
2024 }
2025 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002026
2027 if( ret < 0 )
2028 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002029 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n",
2030 ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002031 goto exit;
2032 }
2033
2034 frags = 1;
2035 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002036
2037 if( written < len )
2038 {
2039 mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
2040 "was truncated to size %u", (unsigned) written );
2041 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002042 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002043
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002044 buf[written] = '\0';
Hanno Becker16970d22017-10-10 15:56:37 +01002045 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
2046 written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002047
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002048 /* Send a non-empty request if request_size == 0 */
2049 if ( len == 0 )
2050 {
2051 opt.request_size = DFL_REQUEST_SIZE;
2052 goto send_request;
2053 }
2054
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 /*
2056 * 7. Read the HTTP response
2057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 fflush( stdout );
2060
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002061 /*
2062 * TLS and DTLS need different reading styles (stream vs datagram)
2063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002065 {
2066 do
2067 {
2068 len = sizeof( buf ) - 1;
2069 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002071
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002072#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002073 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002074 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002075#endif
2076
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002077 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
2078 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Hanno Becker16970d22017-10-10 15:56:37 +01002079 {
2080 /* For event-driven IO, wait for socket to become available */
2081 if( opt.event == 1 /* level triggered IO */ )
2082 {
2083#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002084 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002085#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002086 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002087#endif
2088 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002089 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01002090 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002091
2092 if( ret <= 0 )
2093 {
2094 switch( ret )
2095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2097 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002098 ret = 0;
2099 goto close_notify;
2100
2101 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 case MBEDTLS_ERR_NET_CONN_RESET:
2103 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002104 ret = 0;
2105 goto reconnect;
2106
2107 default:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002108 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n",
2109 -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002110 goto exit;
2111 }
2112 }
2113
2114 len = ret;
2115 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002117
2118 /* End of message should be detected according to the syntax of the
2119 * application protocol (eg HTTP), just use a dummy test here. */
2120 if( ret > 0 && buf[len-1] == '\n' )
2121 {
2122 ret = 0;
2123 break;
2124 }
2125 }
2126 while( 1 );
2127 }
2128 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 {
2130 len = sizeof( buf ) - 1;
2131 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002132
Hanno Becker16970d22017-10-10 15:56:37 +01002133 while( 1 )
2134 {
2135 ret = mbedtls_ssl_read( &ssl, buf, len );
2136
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002137#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002138 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002139 continue;
2140#endif
2141
Hanno Becker16970d22017-10-10 15:56:37 +01002142 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2143 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2144 break;
2145
2146 /* For event-driven IO, wait for socket to become available */
2147 if( opt.event == 1 /* level triggered IO */ )
2148 {
2149#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002150 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002151#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002152 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002153#endif
2154 }
2155 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002156
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002157 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002158 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002159 switch( ret )
2160 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002161 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002163 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02002164 goto send_request;
2165 goto exit;
2166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2168 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002169 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002170 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00002171
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002172 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002174 goto exit;
2175 }
Paul Bakker5690efc2011-05-26 13:16:06 +00002176 }
2177
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02002179 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002181 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002183
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002184 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002185 * 7b. Simulate hard reset and reconnect from same port?
2186 */
2187 if( opt.reconnect_hard != 0 )
2188 {
2189 opt.reconnect_hard = 0;
2190
2191 mbedtls_printf( " . Restarting connection from same port..." );
2192 fflush( stdout );
2193
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002194#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker23699ef2019-02-26 12:36:53 +00002195 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002196#endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Becker23699ef2019-02-26 12:36:53 +00002197
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002198 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
2199 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002200 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2201 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002202 goto exit;
2203 }
2204
2205 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
2206 {
2207 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002208 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002209 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002210 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002211 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2212 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002213 goto exit;
2214 }
Hanno Becker16970d22017-10-10 15:56:37 +01002215
2216 /* For event-driven IO, wait for socket to become available */
2217 if( opt.event == 1 /* level triggered IO */ )
2218 {
2219#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002220 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002221#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002222 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002223#endif
2224 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002225 }
2226
2227 mbedtls_printf( " ok\n" );
2228
2229 goto send_request;
2230 }
2231
2232 /*
2233 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002234 */
2235 if( --opt.exchanges > 0 )
2236 goto send_request;
2237
2238 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002239 * 8. Done, cleanly close the connection
2240 */
2241close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002243 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002244
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002245 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002247 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002248 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002251
2252 /*
2253 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002254 */
2255reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002256 if( opt.reconnect != 0 )
2257 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02002258 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002259
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002260 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002261
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002262#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002263 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02002264 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002265#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002268
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002269#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckera1051b42019-02-26 11:38:29 +00002270 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002271#endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Beckera1051b42019-02-26 11:38:29 +00002272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002274 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002275 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2276 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002277 goto exit;
2278 }
2279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002281 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002282 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n",
2283 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002284 goto exit;
2285 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002286
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002287 if( ( ret = mbedtls_net_connect( &server_fd,
2288 opt.server_addr, opt.server_port,
2289 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2290 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002291 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002292 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
2293 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002294 goto exit;
2295 }
2296
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002297 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002298 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002299 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002300 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002301 if( ret != 0 )
2302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002304 -ret );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002305 goto exit;
2306 }
2307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002309 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002310 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002311 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002312 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002313 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002314 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2315 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002316 goto exit;
2317 }
2318 }
2319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002321
2322 goto send_request;
2323 }
2324
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002325 /*
2326 * Cleanup and exit
2327 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002328exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00002330 if( ret != 0 )
2331 {
2332 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 mbedtls_strerror( ret, error_buf, 100 );
2334 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002335 }
2336#endif
2337
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002338 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340#if defined(MBEDTLS_X509_CRT_PARSE_C)
2341 mbedtls_x509_crt_free( &clicert );
2342 mbedtls_x509_crt_free( &cacert );
2343 mbedtls_pk_free( &pkey );
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01002344#if defined(MBEDTLS_USE_PSA_CRYPTO)
2345 psa_destroy_key( key_slot );
2346#endif
Paul Bakkered27a042013-04-18 22:46:23 +02002347#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 mbedtls_ssl_session_free( &saved_session );
2349 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002350 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 mbedtls_ctr_drbg_free( &ctr_drbg );
2352 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002353
Hanno Becker3f24ea92018-11-05 13:25:17 +00002354#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
2355 defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002356 if( opt.psk_opaque != 0 )
Hanno Becker3f24ea92018-11-05 13:25:17 +00002357 {
2358 /* This is ok even if the slot hasn't been
2359 * initialized (we might have jumed here
2360 * immediately because of bad cmd line params,
2361 * for example). */
Hanno Becker1d911cd2018-11-15 13:06:09 +00002362 status = psa_destroy_key( slot );
Hanno Becker3f24ea92018-11-05 13:25:17 +00002363 if( status != PSA_SUCCESS )
2364 {
2365 mbedtls_printf( "Failed to destroy key slot %u - error was %d",
Hanno Becker1d911cd2018-11-15 13:06:09 +00002366 (unsigned) slot, (int) status );
Hanno Becker3f24ea92018-11-05 13:25:17 +00002367 if( ret == 0 )
2368 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2369 }
2370 }
2371#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED &&
2372 MBEDTLS_USE_PSA_CRYPTO */
2373
Paul Bakkercce9d772011-11-18 14:26:47 +00002374#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 fflush( stdout ); getchar();
2377#endif
2378
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002379 // Shell can not handle large exit numbers -> 1 for errors
2380 if( ret < 0 )
2381 ret = 1;
2382
Paul Bakker5121ce52009-01-03 21:22:43 +00002383 return( ret );
2384}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
2386 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002387 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */