blob: 29a9c83487ff850469a5eceb815999124856cebc [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
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000057#else
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
Simon Butcherd3138c32016-04-27 01:26:50 +010059#include <stdlib.h>
60#define mbedtls_time time
61#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define mbedtls_printf printf
63#define mbedtls_fprintf fprintf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_snprintf snprintf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010065#define mbedtls_exit exit
66#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
67#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evansf90016a2015-01-19 14:26:37 +000068#endif
69
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020070#if !defined(MBEDTLS_ENTROPY_C) || \
71 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020072 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020073int main( void )
74{
75 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
76 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020077 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020078 mbedtls_exit( 0 );
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020079}
80#else
81
Andres AG788aa4a2016-09-14 14:32:09 +010082#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000083#include "mbedtls/ssl.h"
84#include "mbedtls/entropy.h"
85#include "mbedtls/ctr_drbg.h"
86#include "mbedtls/certs.h"
87#include "mbedtls/x509.h"
88#include "mbedtls/error.h"
89#include "mbedtls/debug.h"
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020090#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000091
Rich Evans18b78c72015-02-11 14:06:19 +000092#include <stdio.h>
93#include <stdlib.h>
94#include <string.h>
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010095
Hanno Beckere4ad3e82017-09-18 15:05:46 +010096#define MAX_REQUEST_SIZE 20000
97#define MAX_REQUEST_SIZE_STR "20000"
98
Paul Bakker9caf2d22010-02-18 19:37:19 +000099#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100100#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200101#define DFL_SERVER_PORT "4433"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000102#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +0200103#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +0000104#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100105#define DFL_NBIO 0
Hanno Becker16970d22017-10-10 15:56:37 +0100106#define DFL_EVENT 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200107#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200108#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +0000109#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +0000110#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +0000111#define DFL_CRT_FILE ""
112#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200113#define DFL_PSK ""
114#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200115#define DFL_ECJPAKE_PW NULL
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200116#define DFL_EC_MAX_OPS -1
Paul Bakker51936882011-02-20 16:05:58 +0000117#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100119#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100120#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200121#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200122#define DFL_MIN_VERSION -1
Paul Bakker1d29fb52012-09-28 13:28:45 +0000123#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000124#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +0200125#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100126#define DFL_AUTH_MODE -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100128#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100129#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200130#define DFL_DHMLEN -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200131#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100132#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200133#define DFL_RECONNECT_HARD 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200135#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100136#define DFL_CURVES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200138#define DFL_HS_TO_MIN 0
139#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200140#define DFL_DTLS_MTU -1
Hanno Becker4d615912018-08-14 13:33:30 +0100141#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200142#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200143#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100144#define DFL_ETM -1
Manuel Pégourié-Gonnard498e6322020-02-17 11:04:33 +0100145#define DFL_SKIP_CLOSE_NOTIFY 0
Paul Bakker0e6975b2009-02-10 22:19:10 +0000146
Paul Bakker93c32b22014-04-25 13:40:05 +0200147#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
148#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#if defined(MBEDTLS_X509_CRT_PARSE_C)
151#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000152#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100153 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
154 " default: \"\" (pre-loaded)\n" \
Hanno Becker1ce1a512019-05-01 11:16:28 +0100155 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100156 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
157 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
Hanno Becker1ce1a512019-05-01 11:16:28 +0100158 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100159 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
160 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000161 " key_file=%%s default: \"\" (pre-loaded)\n"
162#else
163#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 " No file operations available (MBEDTLS_FS_IO not defined)\n"
165#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200166#else
167#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200171#define USAGE_PSK \
172 " psk=%%s default: \"\" (in hex, without 0x)\n" \
173 " psk_identity=%%s default: \"Client_identity\"\n"
174#else
175#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200179#define USAGE_TICKETS \
180 " tickets=%%d default: 1 (enabled)\n"
181#else
182#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200186#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100187 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200188#else
189#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200193#define USAGE_MAX_FRAG_LEN \
194 " max_frag_len=%%d default: 16384 (tls default)\n" \
195 " options: 512, 1024, 2048, 4096\n"
196#else
197#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100201#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200202 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100203#else
204#define USAGE_RECSPLIT
205#endif
206
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200207#if defined(MBEDTLS_DHM_C)
208#define USAGE_DHMLEN \
209 " dhmlen=%%d default: (library default: 1024 bits)\n"
210#else
211#define USAGE_DHMLEN
212#endif
213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200215#define USAGE_ALPN \
216 " alpn=%%s default: \"\" (disabled)\n" \
217 " example: spdy/1,http/1.1\n"
218#else
219#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200221
Hanno Beckere6706e62017-05-15 16:05:15 +0100222#if defined(MBEDTLS_ECP_C)
223#define USAGE_CURVES \
224 " curves=a,b,c,d default: \"default\" (library default)\n" \
225 " example: \"secp521r1,brainpoolP512r1\"\n" \
226 " - use \"none\" for empty list\n" \
227 " - see mbedtls_ecp_curve_list()\n" \
228 " for acceptable curve names\n"
229#else
230#define USAGE_CURVES ""
231#endif
232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200234#define USAGE_DTLS \
235 " dtls=%%d default: 0 (TLS)\n" \
236 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200237 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Becker4d615912018-08-14 13:33:30 +0100238 " mtu=%%d default: (library default: unlimited)\n" \
239 " dgram_packing=%%d default: 1 (allowed)\n" \
240 " allow or forbid packing of multiple\n" \
241 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200242#else
243#define USAGE_DTLS ""
244#endif
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200247#define USAGE_FALLBACK \
248 " fallback=0/1 default: (library default: off)\n"
249#else
250#define USAGE_FALLBACK ""
251#endif
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200254#define USAGE_EMS \
255 " extended_ms=0/1 default: (library default: on)\n"
256#else
257#define USAGE_EMS ""
258#endif
259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100261#define USAGE_ETM \
262 " etm=0/1 default: (library default: on)\n"
263#else
264#define USAGE_ETM ""
265#endif
266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100268#define USAGE_RENEGO \
269 " renegotiation=%%d default: 0 (disabled)\n" \
270 " renegotiate=%%d default: 0 (disabled)\n"
271#else
272#define USAGE_RENEGO ""
273#endif
274
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200275#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
276#define USAGE_ECJPAKE \
277 " ecjpake_pw=%%s default: none (disabled)\n"
278#else
279#define USAGE_ECJPAKE ""
280#endif
281
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200282#if defined(MBEDTLS_ECP_RESTARTABLE)
283#define USAGE_ECRESTART \
284 " ec_max_ops=%%s default: library default (restart disabled)\n"
285#else
286#define USAGE_ECRESTART ""
287#endif
288
Gilles Peskined135bbd2020-04-14 19:41:01 +0200289/* USAGE is arbitrarily split to stay under the portable string literal
290 * length limit: 4095 bytes in C99. */
291#define USAGE1 \
Paul Bakker67968392010-07-18 08:28:20 +0000292 "\n usage: ssl_client2 param=<>...\n" \
293 "\n acceptable parameters:\n" \
294 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100295 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000296 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000297 " request_page=%%s default: \".\"\n" \
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +0100298 " request_size=%%d default: about 34 (basic request)\n" \
299 " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
300 " If 0, in the first exchange only an empty\n" \
301 " application data message is sent followed by\n" \
302 " a second non-empty message before attempting\n" \
303 " to read a response from the server\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100304 " debug_level=%%d default: 0 (disabled)\n" \
305 " nbio=%%d default: 0 (blocking I/O)\n" \
306 " options: 1 (non-blocking), 2 (added delays)\n" \
307 " event=%%d default: 0 (loop)\n" \
308 " options: 1 (level-triggered, implies nbio=1),\n" \
309 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200310 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard498e6322020-02-17 11:04:33 +0100311 " skip_close_notify=%%d default: 0 (send close_notify)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100312 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200313 USAGE_DTLS \
Gilles Peskined135bbd2020-04-14 19:41:01 +0200314 "\n"
315#define USAGE2 \
Hanno Becker16970d22017-10-10 15:56:37 +0100316 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100317 " options: none, optional, required\n" \
318 USAGE_IO \
319 "\n" \
320 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200321 USAGE_ECJPAKE \
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200322 USAGE_ECRESTART \
Gilles Peskined135bbd2020-04-14 19:41:01 +0200323 "\n"
324#define USAGE3 \
Hanno Becker16970d22017-10-10 15:56:37 +0100325 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100326 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200327 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200328 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200329 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200330 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200331 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100332 USAGE_MAX_FRAG_LEN \
333 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200334 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200335 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200336 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100337 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100338 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100339 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200340 USAGE_DHMLEN \
Gilles Peskined135bbd2020-04-14 19:41:01 +0200341 "\n"
342#define USAGE4 \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100343 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200344 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200345 " min_version=%%s default: (library default: tls1)\n" \
346 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000347 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100348 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000349 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000350 " force_ciphersuite=<name> default: all enabled\n"\
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100351 " query_config=<name> return 0 if the specified\n" \
352 " configuration macro is defined and 1\n" \
353 " otherwise. The expansion of the macro\n" \
354 " is printed if it is defined\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000355 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000356
Hanno Becker8651a432017-06-09 16:13:22 +0100357#define ALPN_LIST_SIZE 10
358#define CURVE_LIST_SIZE 20
359
Simon Butcher63cb97e2018-12-06 17:43:31 +0000360
Rich Evans85b05ec2015-02-12 11:37:29 +0000361/*
362 * global options
363 */
364struct options
365{
366 const char *server_name; /* hostname of the server (client only) */
367 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200368 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000369 int debug_level; /* level of debugging */
370 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100371 int event; /* loop or event-driven IO? level or edge triggered? */
372 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000373 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000374 const char *request_page; /* page on server to request */
375 int request_size; /* pad request with header to requested size */
376 const char *ca_file; /* the file with the CA certificate(s) */
377 const char *ca_path; /* the path with the CA certificate(s) reside */
378 const char *crt_file; /* the file with the client certificate */
379 const char *key_file; /* the file with the client key */
380 const char *psk; /* the pre-shared key */
381 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200382 const char *ecjpake_pw; /* the EC J-PAKE password */
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200383 int ec_max_ops; /* EC consecutive operations limit */
Rich Evans85b05ec2015-02-12 11:37:29 +0000384 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
385 int renegotiation; /* enable / disable renegotiation */
386 int allow_legacy; /* allow legacy renegotiation */
387 int renegotiate; /* attempt renegotiation? */
388 int renego_delay; /* delay before enforcing renegotiation */
389 int exchanges; /* number of data exchanges */
390 int min_version; /* minimum protocol version accepted */
391 int max_version; /* maximum protocol version accepted */
392 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200393 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000394 int auth_mode; /* verify mode for connection */
395 unsigned char mfl_code; /* code for maximum fragment length */
396 int trunc_hmac; /* negotiate truncated hmac or not */
397 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200398 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000399 int reconnect; /* attempt to resume session */
400 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200401 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000402 int tickets; /* enable / disable session tickets */
Hanno Beckere6706e62017-05-15 16:05:15 +0100403 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000404 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000405 int transport; /* TLS or DTLS? */
406 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
407 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200408 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Rich Evans85b05ec2015-02-12 11:37:29 +0000409 int fallback; /* is this a fallback connection? */
Hanno Becker4d615912018-08-14 13:33:30 +0100410 int dgram_packing; /* allow/forbid datagram packing */
Rich Evans85b05ec2015-02-12 11:37:29 +0000411 int extended_ms; /* negotiate extended master secret? */
412 int etm; /* negotiate encrypt then mac? */
Manuel Pégourié-Gonnard498e6322020-02-17 11:04:33 +0100413 int skip_close_notify; /* skip sending the close_notify alert */
Rich Evans85b05ec2015-02-12 11:37:29 +0000414} opt;
415
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100416int query_config( const char *config );
417
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200418static void my_debug( void *ctx, int level,
419 const char *file, int line,
420 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000421{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200422 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000423
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200424 /* Extract basename from file */
425 for( p = basename = file; *p != '\0'; p++ )
426 if( *p == '/' || *p == '\\' )
427 basename = p + 1;
428
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100429 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s",
430 basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000431 fflush( (FILE *) ctx );
432}
433
434/*
435 * Test recv/send functions that make sure each try returns
436 * WANT_READ/WANT_WRITE at least once before sucesseding
437 */
438static int my_recv( void *ctx, unsigned char *buf, size_t len )
439{
440 static int first_try = 1;
441 int ret;
442
443 if( first_try )
444 {
445 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100446 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000447 }
448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100450 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000451 first_try = 1; /* Next call will be a new operation */
452 return( ret );
453}
454
455static int my_send( void *ctx, const unsigned char *buf, size_t len )
456{
457 static int first_try = 1;
458 int ret;
459
460 if( first_try )
461 {
462 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100463 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000464 }
465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100467 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000468 first_try = 1; /* Next call will be a new operation */
469 return( ret );
470}
471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#if defined(MBEDTLS_X509_CRT_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000473/*
474 * Enabled if debug_level > 1 in code below
475 */
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100476static int my_verify( void *data, mbedtls_x509_crt *crt,
477 int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000478{
479 char buf[1024];
480 ((void) data);
481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
483 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
484 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000485
Rich Evans85b05ec2015-02-12 11:37:29 +0000486 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100488 else
489 {
490 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
491 mbedtls_printf( "%s\n", buf );
492 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000493
494 return( 0 );
495}
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200496
497static int ssl_sig_hashes_for_test[] = {
498#if defined(MBEDTLS_SHA512_C)
499 MBEDTLS_MD_SHA512,
500 MBEDTLS_MD_SHA384,
501#endif
502#if defined(MBEDTLS_SHA256_C)
503 MBEDTLS_MD_SHA256,
504 MBEDTLS_MD_SHA224,
505#endif
506#if defined(MBEDTLS_SHA1_C)
507 /* Allow SHA-1 as we use it extensively in tests. */
508 MBEDTLS_MD_SHA1,
509#endif
510 MBEDTLS_MD_NONE
511};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000513
Hanno Becker16970d22017-10-10 15:56:37 +0100514/*
515 * Wait for an event from the underlying transport or the timer
516 * (Used in event-driven IO mode).
517 */
518#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000519int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000520 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100521#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000522int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000523 mbedtls_timing_delay_context *timer,
524 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100525#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000526{
Hanno Becker16970d22017-10-10 15:56:37 +0100527
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000528 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +0100529 int poll_type = 0;
530
531 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
532 poll_type = MBEDTLS_NET_POLL_WRITE;
533 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
534 poll_type = MBEDTLS_NET_POLL_READ;
535#if !defined(MBEDTLS_TIMING_C)
536 else
Hanno Beckeref527962018-03-15 15:49:24 +0000537 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100538#endif
539
540 while( 1 )
541 {
Hanno Becker197a91c2017-10-31 10:58:53 +0000542 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +0100543#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +0000544 if( timer != NULL &&
545 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +0100546 {
Hanno Becker16970d22017-10-10 15:56:37 +0100547 break;
548 }
Hanno Becker197a91c2017-10-31 10:58:53 +0000549#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +0100550
Hanno Becker197a91c2017-10-31 10:58:53 +0000551 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000552 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +0100553 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000554 ret = mbedtls_net_poll( fd, poll_type, 0 );
555 if( ret < 0 )
556 return( ret );
557 if( ret == poll_type )
558 break;
Hanno Becker16970d22017-10-10 15:56:37 +0100559 }
560 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000561
562 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100563}
564
Paul Bakker9caf2d22010-02-18 19:37:19 +0000565int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000566{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200567 int ret = 0, len, tail_len, i, written, frags, retry_left;
568 mbedtls_net_context server_fd;
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100569
570 unsigned char buf[MAX_REQUEST_SIZE + 1];
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
573 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200574 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200575#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100577 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200578#endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100579#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +0100580 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100581 const mbedtls_ecp_curve_info *curve_cur;
582#endif
583
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200584 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000585
Gilles Peskineef86ab22017-05-05 18:59:02 +0200586#if defined(MBEDTLS_X509_CRT_PARSE_C)
587 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
588#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_entropy_context entropy;
590 mbedtls_ctr_drbg_context ctr_drbg;
591 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200592 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200594#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200595 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200596#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200598 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_x509_crt cacert;
600 mbedtls_x509_crt clicert;
601 mbedtls_pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200602#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000603 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000604 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000605
Paul Bakker1a207ec2011-02-06 13:22:40 +0000606 /*
607 * Make sure memory references are valid.
608 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200609 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200610 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200611 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200613 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614#if defined(MBEDTLS_X509_CRT_PARSE_C)
615 mbedtls_x509_crt_init( &cacert );
616 mbedtls_x509_crt_init( &clicert );
617 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200618#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200620 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200621#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000622
Paul Bakker9caf2d22010-02-18 19:37:19 +0000623 if( argc == 0 )
624 {
625 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000626 if( ret == 0 )
627 ret = 1;
628
Gilles Peskined135bbd2020-04-14 19:41:01 +0200629 mbedtls_printf( USAGE1 );
630 mbedtls_printf( USAGE2 );
631 mbedtls_printf( USAGE3 );
632 mbedtls_printf( USAGE4 );
Paul Bakker51936882011-02-20 16:05:58 +0000633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000635 while( *list )
636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200638 list++;
639 if( !*list )
640 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000642 list++;
643 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000645 goto exit;
646 }
647
648 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100649 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000650 opt.server_port = DFL_SERVER_PORT;
651 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100652 opt.nbio = DFL_NBIO;
Hanno Becker16970d22017-10-10 15:56:37 +0100653 opt.event = DFL_EVENT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200654 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200655 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000656 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200657 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000658 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000659 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000660 opt.crt_file = DFL_CRT_FILE;
661 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200662 opt.psk = DFL_PSK;
663 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200664 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200665 opt.ec_max_ops = DFL_EC_MAX_OPS;
Paul Bakker51936882011-02-20 16:05:58 +0000666 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000667 opt.renegotiation = DFL_RENEGOTIATION;
668 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100669 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200670 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000671 opt.min_version = DFL_MIN_VERSION;
672 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100673 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +0200674 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100675 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200676 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200677 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100678 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200679 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200680 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100681 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200682 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200683 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200684 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +0100685 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200686 opt.transport = DFL_TRANSPORT;
687 opt.hs_to_min = DFL_HS_TO_MIN;
688 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200689 opt.dtls_mtu = DFL_DTLS_MTU;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200690 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200691 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100692 opt.etm = DFL_ETM;
Hanno Becker4d615912018-08-14 13:33:30 +0100693 opt.dgram_packing = DFL_DGRAM_PACKING;
Manuel Pégourié-Gonnard498e6322020-02-17 11:04:33 +0100694 opt.skip_close_notify = DFL_SKIP_CLOSE_NOTIFY;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000695
696 for( i = 1; i < argc; i++ )
697 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000698 p = argv[i];
699 if( ( q = strchr( p, '=' ) ) == NULL )
700 goto usage;
701 *q++ = '\0';
702
703 if( strcmp( p, "server_name" ) == 0 )
704 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100705 else if( strcmp( p, "server_addr" ) == 0 )
706 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000707 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200708 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100709 else if( strcmp( p, "dtls" ) == 0 )
710 {
711 int t = atoi( q );
712 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100714 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100716 else
717 goto usage;
718 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000719 else if( strcmp( p, "debug_level" ) == 0 )
720 {
721 opt.debug_level = atoi( q );
722 if( opt.debug_level < 0 || opt.debug_level > 65535 )
723 goto usage;
724 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100725 else if( strcmp( p, "nbio" ) == 0 )
726 {
727 opt.nbio = atoi( q );
728 if( opt.nbio < 0 || opt.nbio > 2 )
729 goto usage;
730 }
Hanno Becker16970d22017-10-10 15:56:37 +0100731 else if( strcmp( p, "event" ) == 0 )
732 {
733 opt.event = atoi( q );
734 if( opt.event < 0 || opt.event > 2 )
735 goto usage;
736 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200737 else if( strcmp( p, "read_timeout" ) == 0 )
738 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200739 else if( strcmp( p, "max_resend" ) == 0 )
740 {
741 opt.max_resend = atoi( q );
742 if( opt.max_resend < 0 )
743 goto usage;
744 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000745 else if( strcmp( p, "request_page" ) == 0 )
746 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200747 else if( strcmp( p, "request_size" ) == 0 )
748 {
749 opt.request_size = atoi( q );
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100750 if( opt.request_size < 0 ||
751 opt.request_size > MAX_REQUEST_SIZE )
Paul Bakker93c32b22014-04-25 13:40:05 +0200752 goto usage;
753 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000754 else if( strcmp( p, "ca_file" ) == 0 )
755 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000756 else if( strcmp( p, "ca_path" ) == 0 )
757 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000758 else if( strcmp( p, "crt_file" ) == 0 )
759 opt.crt_file = q;
760 else if( strcmp( p, "key_file" ) == 0 )
761 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200762 else if( strcmp( p, "psk" ) == 0 )
763 opt.psk = q;
764 else if( strcmp( p, "psk_identity" ) == 0 )
765 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200766 else if( strcmp( p, "ecjpake_pw" ) == 0 )
767 opt.ecjpake_pw = q;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200768 else if( strcmp( p, "ec_max_ops" ) == 0 )
769 opt.ec_max_ops = atoi( q );
Paul Bakker51936882011-02-20 16:05:58 +0000770 else if( strcmp( p, "force_ciphersuite" ) == 0 )
771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +0000773
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200774 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000775 {
776 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000777 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000778 }
Paul Bakker51936882011-02-20 16:05:58 +0000779 opt.force_ciphersuite[1] = 0;
780 }
Paul Bakker48916f92012-09-16 19:57:18 +0000781 else if( strcmp( p, "renegotiation" ) == 0 )
782 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100783 opt.renegotiation = (atoi( q )) ?
784 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
785 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +0000786 }
787 else if( strcmp( p, "allow_legacy" ) == 0 )
788 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100789 switch( atoi( q ) )
790 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100791 case -1:
792 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
793 break;
794 case 0:
795 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
796 break;
797 case 1:
798 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
799 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100800 default: goto usage;
801 }
Paul Bakker48916f92012-09-16 19:57:18 +0000802 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100803 else if( strcmp( p, "renegotiate" ) == 0 )
804 {
805 opt.renegotiate = atoi( q );
806 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
807 goto usage;
808 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200809 else if( strcmp( p, "exchanges" ) == 0 )
810 {
811 opt.exchanges = atoi( q );
812 if( opt.exchanges < 1 )
813 goto usage;
814 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200815 else if( strcmp( p, "reconnect" ) == 0 )
816 {
817 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200818 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200819 goto usage;
820 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100821 else if( strcmp( p, "reco_delay" ) == 0 )
822 {
823 opt.reco_delay = atoi( q );
824 if( opt.reco_delay < 0 )
825 goto usage;
826 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200827 else if( strcmp( p, "reconnect_hard" ) == 0 )
828 {
829 opt.reconnect_hard = atoi( q );
830 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
831 goto usage;
832 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200833 else if( strcmp( p, "tickets" ) == 0 )
834 {
835 opt.tickets = atoi( q );
836 if( opt.tickets < 0 || opt.tickets > 2 )
837 goto usage;
838 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200839 else if( strcmp( p, "alpn" ) == 0 )
840 {
841 opt.alpn_string = q;
842 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200843 else if( strcmp( p, "fallback" ) == 0 )
844 {
845 switch( atoi( q ) )
846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
848 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200849 default: goto usage;
850 }
851 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200852 else if( strcmp( p, "extended_ms" ) == 0 )
853 {
854 switch( atoi( q ) )
855 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100856 case 0:
857 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
858 break;
859 case 1:
860 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
861 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200862 default: goto usage;
863 }
864 }
Hanno Beckere6706e62017-05-15 16:05:15 +0100865 else if( strcmp( p, "curves" ) == 0 )
866 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100867 else if( strcmp( p, "etm" ) == 0 )
868 {
869 switch( atoi( q ) )
870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
872 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100873 default: goto usage;
874 }
875 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000876 else if( strcmp( p, "min_version" ) == 0 )
877 {
878 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000880 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100882 else if( strcmp( q, "tls1_1" ) == 0 ||
883 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100885 else if( strcmp( q, "tls1_2" ) == 0 ||
886 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000888 else
889 goto usage;
890 }
891 else if( strcmp( p, "max_version" ) == 0 )
892 {
893 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000895 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100897 else if( strcmp( q, "tls1_1" ) == 0 ||
898 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100900 else if( strcmp( q, "tls1_2" ) == 0 ||
901 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000903 else
904 goto usage;
905 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100906 else if( strcmp( p, "arc4" ) == 0 )
907 {
908 switch( atoi( q ) )
909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
911 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100912 default: goto usage;
913 }
914 }
Gilles Peskinebc70a182017-05-09 15:59:24 +0200915 else if( strcmp( p, "allow_sha1" ) == 0 )
916 {
917 switch( atoi( q ) )
918 {
919 case 0: opt.allow_sha1 = 0; break;
920 case 1: opt.allow_sha1 = 1; break;
921 default: goto usage;
922 }
923 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000924 else if( strcmp( p, "force_version" ) == 0 )
925 {
926 if( strcmp( q, "ssl3" ) == 0 )
927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
929 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000930 }
931 else if( strcmp( q, "tls1" ) == 0 )
932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
934 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000935 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100936 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
939 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000940 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100941 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
944 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000945 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100946 else if( strcmp( q, "dtls1" ) == 0 )
947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
949 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
950 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100951 }
952 else if( strcmp( q, "dtls1_2" ) == 0 )
953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
955 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
956 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100957 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000958 else
959 goto usage;
960 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100961 else if( strcmp( p, "auth_mode" ) == 0 )
962 {
963 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100965 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100967 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100969 else
970 goto usage;
971 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200972 else if( strcmp( p, "max_frag_len" ) == 0 )
973 {
974 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200976 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200978 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200980 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200982 else
983 goto usage;
984 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200985 else if( strcmp( p, "trunc_hmac" ) == 0 )
986 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100987 switch( atoi( q ) )
988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
990 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100991 default: goto usage;
992 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200993 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200994 else if( strcmp( p, "hs_timeout" ) == 0 )
995 {
996 if( ( p = strchr( q, '-' ) ) == NULL )
997 goto usage;
998 *p++ = '\0';
999 opt.hs_to_min = atoi( q );
1000 opt.hs_to_max = atoi( p );
1001 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1002 goto usage;
1003 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001004 else if( strcmp( p, "mtu" ) == 0 )
1005 {
1006 opt.dtls_mtu = atoi( q );
1007 if( opt.dtls_mtu < 0 )
1008 goto usage;
1009 }
Hanno Becker4d615912018-08-14 13:33:30 +01001010 else if( strcmp( p, "dgram_packing" ) == 0 )
1011 {
1012 opt.dgram_packing = atoi( q );
1013 if( opt.dgram_packing != 0 &&
1014 opt.dgram_packing != 1 )
1015 {
1016 goto usage;
1017 }
1018 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001019 else if( strcmp( p, "recsplit" ) == 0 )
1020 {
1021 opt.recsplit = atoi( q );
1022 if( opt.recsplit < 0 || opt.recsplit > 1 )
1023 goto usage;
1024 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001025 else if( strcmp( p, "dhmlen" ) == 0 )
1026 {
1027 opt.dhmlen = atoi( q );
1028 if( opt.dhmlen < 0 )
1029 goto usage;
1030 }
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +01001031 else if( strcmp( p, "query_config" ) == 0 )
1032 {
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +02001033 mbedtls_exit( query_config( q ) );
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +01001034 }
Manuel Pégourié-Gonnard498e6322020-02-17 11:04:33 +01001035 else if( strcmp( p, "skip_close_notify" ) == 0 )
1036 {
1037 opt.skip_close_notify = atoi( q );
1038 if( opt.skip_close_notify < 0 || opt.skip_close_notify > 1 )
1039 goto usage;
1040 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001041 else
1042 goto usage;
1043 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001044
Hanno Becker16970d22017-10-10 15:56:37 +01001045 /* Event-driven IO is incompatible with the above custom
1046 * receive and send functions, as the polling builds on
1047 * refers to the underlying net_context. */
1048 if( opt.event == 1 && opt.nbio != 1 )
1049 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001050 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001051 opt.nbio = 1;
1052 }
1053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054#if defined(MBEDTLS_DEBUG_C)
1055 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001056#endif
1057
Paul Bakkerc1516be2013-06-29 16:01:32 +02001058 if( opt.force_ciphersuite[0] > 0 )
1059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001061 ciphersuite_info =
1062 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001063
Paul Bakker66c48102013-07-26 14:05:32 +02001064 if( opt.max_version != -1 &&
1065 ciphersuite_info->min_minor_ver > opt.max_version )
1066 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001067 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker66c48102013-07-26 14:05:32 +02001068 ret = 2;
1069 goto usage;
1070 }
1071 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001072 ciphersuite_info->max_minor_ver < opt.min_version )
1073 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001074 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001075 ret = 2;
1076 goto usage;
1077 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001078
1079 /* If the server selects a version that's not supported by
1080 * this suite, then there will be no common ciphersuite... */
1081 if( opt.max_version == -1 ||
1082 opt.max_version > ciphersuite_info->max_minor_ver )
1083 {
Paul Bakker66c48102013-07-26 14:05:32 +02001084 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001085 }
Paul Bakker66c48102013-07-26 14:05:32 +02001086 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001087 {
Paul Bakker66c48102013-07-26 14:05:32 +02001088 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001089 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1091 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1092 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001093 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001094
1095 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001099 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001100 mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001101 ret = 2;
1102 goto usage;
1103 }
1104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001106 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001107 }
1108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001111 * Unhexify the pre-shared key if any is given
1112 */
1113 if( strlen( opt.psk ) )
1114 {
1115 unsigned char c;
1116 size_t j;
1117
1118 if( strlen( opt.psk ) % 2 != 0 )
1119 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001120 mbedtls_printf( "pre-shared key not valid hex\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001121 goto exit;
1122 }
1123
1124 psk_len = strlen( opt.psk ) / 2;
1125
1126 for( j = 0; j < strlen( opt.psk ); j += 2 )
1127 {
1128 c = opt.psk[j];
1129 if( c >= '0' && c <= '9' )
1130 c -= '0';
1131 else if( c >= 'a' && c <= 'f' )
1132 c -= 'a' - 10;
1133 else if( c >= 'A' && c <= 'F' )
1134 c -= 'A' - 10;
1135 else
1136 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001137 mbedtls_printf( "pre-shared key not valid hex\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001138 goto exit;
1139 }
1140 psk[ j / 2 ] = c << 4;
1141
1142 c = opt.psk[j + 1];
1143 if( c >= '0' && c <= '9' )
1144 c -= '0';
1145 else if( c >= 'a' && c <= 'f' )
1146 c -= 'a' - 10;
1147 else if( c >= 'A' && c <= 'F' )
1148 c -= 'A' - 10;
1149 else
1150 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001151 mbedtls_printf( "pre-shared key not valid hex\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001152 goto exit;
1153 }
1154 psk[ j / 2 ] |= c;
1155 }
1156 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001158
Hanno Beckere6706e62017-05-15 16:05:15 +01001159#if defined(MBEDTLS_ECP_C)
1160 if( opt.curves != NULL )
1161 {
1162 p = (char *) opt.curves;
1163 i = 0;
1164
1165 if( strcmp( p, "none" ) == 0 )
1166 {
1167 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1168 }
1169 else if( strcmp( p, "default" ) != 0 )
1170 {
1171 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001172 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001173 {
1174 q = p;
1175
1176 /* Terminate the current string */
1177 while( *p != ',' && *p != '\0' )
1178 p++;
1179 if( *p == ',' )
1180 *p++ = '\0';
1181
1182 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1183 {
1184 curve_list[i++] = curve_cur->grp_id;
1185 }
1186 else
1187 {
1188 mbedtls_printf( "unknown curve %s\n", q );
1189 mbedtls_printf( "supported curves: " );
1190 for( curve_cur = mbedtls_ecp_curve_list();
1191 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1192 curve_cur++ )
1193 {
1194 mbedtls_printf( "%s ", curve_cur->name );
1195 }
1196 mbedtls_printf( "\n" );
1197 goto exit;
1198 }
1199 }
1200
1201 mbedtls_printf("Number of curves: %d\n", i );
1202
Hanno Becker8651a432017-06-09 16:13:22 +01001203 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001204 {
Hanno Becker8651a432017-06-09 16:13:22 +01001205 mbedtls_printf( "curves list too long, maximum %d",
1206 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001207 goto exit;
1208 }
1209
1210 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1211 }
1212 }
1213#endif /* MBEDTLS_ECP_C */
1214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001216 if( opt.alpn_string != NULL )
1217 {
1218 p = (char *) opt.alpn_string;
1219 i = 0;
1220
1221 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001222 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001223 {
1224 alpn_list[i++] = p;
1225
1226 /* Terminate the current string and move on to next one */
1227 while( *p != ',' && *p != '\0' )
1228 p++;
1229 if( *p == ',' )
1230 *p++ = '\0';
1231 }
1232 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001234
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001235 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 * 0. Initialize the RNG and the session data
1237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001239 fflush( stdout );
1240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_entropy_init( &entropy );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001242 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1243 &entropy, (const unsigned char *) pers,
1244 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +00001245 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001246 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
1247 -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001248 goto exit;
1249 }
1250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001254 /*
1255 * 1.1. Load the trusted CA
1256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001258 fflush( stdout );
1259
Hanno Beckera7242062019-03-05 16:02:15 +00001260 if( strcmp( opt.ca_path, "none" ) == 0 ||
1261 strcmp( opt.ca_file, "none" ) == 0 )
1262 {
1263 ret = 0;
1264 }
1265 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001267 if( strlen( opt.ca_path ) )
Hanno Beckera7242062019-03-05 16:02:15 +00001268 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001269 else if( strlen( opt.ca_file ) )
Hanno Beckera7242062019-03-05 16:02:15 +00001270 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001271 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001272#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273#if defined(MBEDTLS_CERTS_C)
Hanno Beckerbb676f72019-02-01 08:15:06 +00001274 {
1275#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 ret = mbedtls_x509_crt_parse( &cacert,
1279 (const unsigned char *) mbedtls_test_cas[i],
1280 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001281 if( ret != 0 )
1282 break;
1283 }
Hanno Beckerbb676f72019-02-01 08:15:06 +00001284 if( ret == 0 )
1285#endif /* MBEDTLS_PEM_PARSE_C */
1286 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
1287 {
1288 ret = mbedtls_x509_crt_parse_der( &cacert,
1289 (const unsigned char *) mbedtls_test_cas_der[i],
1290 mbedtls_test_cas_der_len[i] );
1291 if( ret != 0 )
1292 break;
1293 }
1294 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001295#else
1296 {
1297 ret = 1;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001298 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001299 }
Hanno Beckerbb676f72019-02-01 08:15:06 +00001300#endif /* MBEDTLS_CERTS_C */
Paul Bakker42488232012-05-16 08:21:05 +00001301 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001302 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001303 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1304 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001305 goto exit;
1306 }
1307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001309
1310 /*
1311 * 1.2. Load own certificate and private key
1312 *
1313 * (can be skipped if client authentication is not required)
1314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 fflush( stdout );
1317
Hanno Beckera7242062019-03-05 16:02:15 +00001318 if( strcmp( opt.crt_file, "none" ) == 0 )
1319 ret = 0;
1320 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001322 if( strlen( opt.crt_file ) )
Hanno Beckera7242062019-03-05 16:02:15 +00001323 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001324 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001325#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001327 ret = mbedtls_x509_crt_parse( &clicert,
1328 (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001330#else
1331 {
1332 ret = 1;
Hanno Beckerc258c442018-12-05 16:49:42 +00001333 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001334 }
1335#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 if( ret != 0 )
1337 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001338 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1339 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 goto exit;
1341 }
1342
Hanno Beckera7242062019-03-05 16:02:15 +00001343 if( strcmp( opt.key_file, "none" ) == 0 )
1344 ret = 0;
1345 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001347 if( strlen( opt.key_file ) )
Hanno Beckera7242062019-03-05 16:02:15 +00001348 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001349 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001350#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001352 ret = mbedtls_pk_parse_key( &pkey,
1353 (const unsigned char *) mbedtls_test_cli_key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001355#else
1356 {
1357 ret = 1;
Hanno Beckerc258c442018-12-05 16:49:42 +00001358 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001359 }
1360#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 if( ret != 0 )
1362 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001363 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
1364 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 goto exit;
1366 }
1367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 mbedtls_printf( " ok\n" );
1369#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
1371 /*
1372 * 2. Start the connection
1373 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001374 if( opt.server_addr == NULL)
1375 opt.server_addr = opt.server_name;
1376
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001377 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001379 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001380 fflush( stdout );
1381
Hanno Becker16970d22017-10-10 15:56:37 +01001382 if( ( ret = mbedtls_net_connect( &server_fd,
1383 opt.server_addr, opt.server_port,
1384 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1385 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001387 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
1388 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001389 goto exit;
1390 }
1391
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001392 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001393 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001394 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001395 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001396 if( ret != 0 )
1397 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001398 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
1399 -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001400 goto exit;
1401 }
1402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
1405 /*
1406 * 3. Setup stuff
1407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 fflush( stdout );
1410
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001411 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1412 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001413 opt.transport,
1414 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001415 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001416 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
1417 -ret );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001418 goto exit;
1419 }
1420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001422 /* The default algorithms profile disables SHA-1, but our tests still
1423 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001424 if( opt.allow_sha1 > 0 )
1425 {
1426 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1427 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1428 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1429 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001430
Paul Bakker915275b2012-09-28 07:10:55 +00001431 if( opt.debug_level > 0 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001432 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
Gilles Peskineef86ab22017-05-05 18:59:02 +02001433#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001434
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001435 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001436 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001439 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Hanno Becker16970d22017-10-10 15:56:37 +01001440 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min,
1441 opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001442
Hanno Becker4d615912018-08-14 13:33:30 +01001443 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01001444 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001448 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001449 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001450 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n",
1451 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001452 goto exit;
1453 }
Paul Bakker05decb22013-08-15 13:33:48 +02001454#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001457 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001458 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001459#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001462 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001463 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001464#endif
1465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001467 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001468 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001469#endif
1470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001472 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001473 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Hanno Becker16970d22017-10-10 15:56:37 +01001474 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1475 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001476#endif
1477
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001478#if defined(MBEDTLS_DHM_C)
1479 if( opt.dhmlen != DFL_DHMLEN )
1480 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1481#endif
1482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001484 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001485 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001486 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001487 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
1488 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001489 goto exit;
1490 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001491#endif
1492
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001493 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1494 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001495
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001496 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02001499 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02001500#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001501
Paul Bakker645ce3a2012-10-31 12:32:41 +00001502 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001503 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001504
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001505#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001506 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001507 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001508#endif
Paul Bakker51936882011-02-20 16:05:58 +00001509
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001510 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001511 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001513 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001514#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001517 if( strcmp( opt.ca_path, "none" ) != 0 &&
1518 strcmp( opt.ca_file, "none" ) != 0 )
1519 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001520 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001521 }
1522 if( strcmp( opt.crt_file, "none" ) != 0 &&
1523 strcmp( opt.key_file, "none" ) != 0 )
1524 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001525 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001526 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001527 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
1528 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001529 goto exit;
1530 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001531 }
Paul Bakkered27a042013-04-18 22:46:23 +02001532#endif
1533
Hanno Beckere6706e62017-05-15 16:05:15 +01001534#if defined(MBEDTLS_ECP_C)
1535 if( opt.curves != NULL &&
1536 strcmp( opt.curves, "default" ) != 0 )
1537 {
1538 mbedtls_ssl_conf_curves( &conf, curve_list );
1539 }
1540#endif
1541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001543 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001544 (const unsigned char *) opt.psk_identity,
1545 strlen( opt.psk_identity ) ) ) != 0 )
1546 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001547 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n",
1548 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001549 goto exit;
1550 }
Paul Bakkered27a042013-04-18 22:46:23 +02001551#endif
1552
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001553 if( opt.min_version != DFL_MIN_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01001554 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1555 opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001556
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001557 if( opt.max_version != DFL_MAX_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01001558 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1559 opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001562 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001563 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001564#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001565
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001566 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
1567 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001568 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
1569 -ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001570 goto exit;
1571 }
1572
1573#if defined(MBEDTLS_X509_CRT_PARSE_C)
1574 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1575 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001576 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
1577 ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001578 goto exit;
1579 }
1580#endif
1581
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001582#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1583 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
1584 {
1585 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
1586 (const unsigned char *) opt.ecjpake_pw,
1587 strlen( opt.ecjpake_pw ) ) ) != 0 )
1588 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001589 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
1590 ret );
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001591 goto exit;
1592 }
1593 }
1594#endif
1595
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001596 if( opt.nbio == 2 )
1597 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
1598 else
Hanno Becker16970d22017-10-10 15:56:37 +01001599 mbedtls_ssl_set_bio( &ssl, &server_fd,
1600 mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02001601 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001602
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001603#if defined(MBEDTLS_SSL_PROTO_DTLS)
1604 if( opt.dtls_mtu != DFL_DTLS_MTU )
1605 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
1606#endif
1607
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001608#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001609 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
1610 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001611#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001612
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001613#if defined(MBEDTLS_ECP_RESTARTABLE)
1614 if( opt.ec_max_ops != DFL_EC_MAX_OPS )
1615 mbedtls_ecp_set_max_ops( opt.ec_max_ops );
1616#endif
1617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001619
Paul Bakker5121ce52009-01-03 21:22:43 +00001620 /*
1621 * 4. Handshake
1622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001624 fflush( stdout );
1625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001627 {
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001628 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1629 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001630 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Paul Bakker40e46942009-01-03 21:51:57 +00001631 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001632 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
1633 -ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
1635 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001636 " Unable to verify the server's certificate. "
1637 "Either it is invalid,\n"
1638 " or you didn't set ca_file or ca_path "
1639 "to an appropriate value.\n"
1640 " Alternatively, you may want to use "
1641 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001643 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00001644 }
Hanno Becker16970d22017-10-10 15:56:37 +01001645
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001646#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02001647 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
1648 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001649#endif
1650
Hanno Becker16970d22017-10-10 15:56:37 +01001651 /* For event-driven IO, wait for socket to become available */
1652 if( opt.event == 1 /* level triggered IO */ )
1653 {
1654#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001655 ret = idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001656#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001657 ret = idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001658#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001659 if( ret != 0 )
1660 goto exit;
Hanno Becker16970d22017-10-10 15:56:37 +01001661 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001662 }
1663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Hanno Becker16970d22017-10-10 15:56:37 +01001665 mbedtls_ssl_get_version( &ssl ),
1666 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
1669 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001670 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001672
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02001673#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1674 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
1675 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
1676#endif
1677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001679 if( opt.alpn_string != NULL )
1680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
1682 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001683 alp ? alp : "(none)" );
1684 }
1685#endif
1686
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001687 if( opt.reconnect != 0 )
1688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001690 fflush( stdout );
1691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001693 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001694 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
1695 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001696 goto exit;
1697 }
1698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001700 }
1701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001703 /*
1704 * 5. Verify the server certificate
1705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001707
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001708 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001709 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001710 char vrfy_buf[512];
1711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001713
Hanno Becker16970d22017-10-10 15:56:37 +01001714 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ),
1715 " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00001716
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001717 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001718 }
1719 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 mbedtls_printf( " . Peer certificate information ...\n" );
1725 mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1726 mbedtls_ssl_get_peer_cert( &ssl ) );
1727 mbedtls_printf( "%s\n", buf );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001728 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001732 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001733 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001734 /*
1735 * Perform renegotiation (this must be done when the server is waiting
1736 * for input from our side).
1737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001739 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001741 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001742 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001743 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001744 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001745 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001746 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
1747 ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001748 goto exit;
1749 }
Hanno Becker16970d22017-10-10 15:56:37 +01001750
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02001751#if defined(MBEDTLS_ECP_RESTARTABLE)
1752 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
1753 continue;
1754#endif
1755
Hanno Becker16970d22017-10-10 15:56:37 +01001756 /* For event-driven IO, wait for socket to become available */
1757 if( opt.event == 1 /* level triggered IO */ )
1758 {
1759#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001760 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001761#else
Hanno Becker197a91c2017-10-31 10:58:53 +00001762 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001763#endif
1764 }
1765
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001766 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001768 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001770
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 /*
1772 * 6. Write the GET request
1773 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001774 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001775send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001777 fflush( stdout );
1778
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001779 len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST,
1780 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001781 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001782
1783 /* Add padding to GET request to reach opt.request_size in length */
1784 if( opt.request_size != DFL_REQUEST_SIZE &&
1785 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001786 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001787 memset( buf + len, 'A', opt.request_size - len - tail_len );
1788 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001789 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001790
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001791 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001792 len += tail_len;
1793
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001794 /* Truncate if request size is smaller than the "natural" size */
1795 if( opt.request_size != DFL_REQUEST_SIZE &&
1796 len > opt.request_size )
1797 {
1798 len = opt.request_size;
1799
1800 /* Still end with \r\n unless that's really not possible */
1801 if( len >= 2 ) buf[len - 2] = '\r';
1802 if( len >= 1 ) buf[len - 1] = '\n';
1803 }
1804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001806 {
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001807 written = 0;
1808 frags = 0;
1809
1810 do
Paul Bakker5121ce52009-01-03 21:22:43 +00001811 {
Hanno Becker16970d22017-10-10 15:56:37 +01001812 while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001813 len - written ) ) < 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001814 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001815 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001816 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001817 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001818 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001819 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
1820 -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001821 goto exit;
1822 }
Hanno Becker16970d22017-10-10 15:56:37 +01001823
1824 /* For event-driven IO, wait for socket to become available */
1825 if( opt.event == 1 /* level triggered IO */ )
1826 {
1827#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001828 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001829#else
Hanno Becker197a91c2017-10-31 10:58:53 +00001830 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001831#endif
1832 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001833 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001834
1835 frags++;
1836 written += ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001838 while( written < len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001839 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001840 else /* Not stream, so datagram */
1841 {
Hanno Becker16970d22017-10-10 15:56:37 +01001842 while( 1 )
1843 {
1844 ret = mbedtls_ssl_write( &ssl, buf, len );
1845
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001846#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001847 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001848 continue;
1849#endif
1850
Hanno Becker16970d22017-10-10 15:56:37 +01001851 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1852 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1853 break;
1854
1855 /* For event-driven IO, wait for socket to become available */
1856 if( opt.event == 1 /* level triggered IO */ )
1857 {
1858#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001859 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001860#else
Hanno Becker197a91c2017-10-31 10:58:53 +00001861 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001862#endif
1863 }
1864 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001865
1866 if( ret < 0 )
1867 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001868 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n",
1869 ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001870 goto exit;
1871 }
1872
1873 frags = 1;
1874 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001875
1876 if( written < len )
1877 {
1878 mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
1879 "was truncated to size %u", (unsigned) written );
1880 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001881 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001882
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001883 buf[written] = '\0';
Hanno Becker16970d22017-10-10 15:56:37 +01001884 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
1885 written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001886
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001887 /* Send a non-empty request if request_size == 0 */
1888 if ( len == 0 )
1889 {
1890 opt.request_size = DFL_REQUEST_SIZE;
1891 goto send_request;
1892 }
1893
Paul Bakker5121ce52009-01-03 21:22:43 +00001894 /*
1895 * 7. Read the HTTP response
1896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 fflush( stdout );
1899
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001900 /*
1901 * TLS and DTLS need different reading styles (stream vs datagram)
1902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001904 {
1905 do
1906 {
1907 len = sizeof( buf ) - 1;
1908 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001910
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001911#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001912 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001913 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001914#endif
1915
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001916 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1917 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Hanno Becker16970d22017-10-10 15:56:37 +01001918 {
1919 /* For event-driven IO, wait for socket to become available */
1920 if( opt.event == 1 /* level triggered IO */ )
1921 {
1922#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001923 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001924#else
Hanno Becker197a91c2017-10-31 10:58:53 +00001925 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001926#endif
1927 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001928 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01001929 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001930
1931 if( ret <= 0 )
1932 {
1933 switch( ret )
1934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1936 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001937 ret = 0;
1938 goto close_notify;
1939
1940 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 case MBEDTLS_ERR_NET_CONN_RESET:
1942 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001943 ret = 0;
1944 goto reconnect;
1945
1946 default:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001947 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n",
1948 -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001949 goto exit;
1950 }
1951 }
1952
1953 len = ret;
1954 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001956
1957 /* End of message should be detected according to the syntax of the
1958 * application protocol (eg HTTP), just use a dummy test here. */
1959 if( ret > 0 && buf[len-1] == '\n' )
1960 {
1961 ret = 0;
1962 break;
1963 }
1964 }
1965 while( 1 );
1966 }
1967 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001968 {
1969 len = sizeof( buf ) - 1;
1970 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001971
Hanno Becker16970d22017-10-10 15:56:37 +01001972 while( 1 )
1973 {
1974 ret = mbedtls_ssl_read( &ssl, buf, len );
1975
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001976#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001977 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001978 continue;
1979#endif
1980
Hanno Becker16970d22017-10-10 15:56:37 +01001981 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1982 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1983 break;
1984
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 }
1994 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001995
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001996 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001997 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001998 switch( ret )
1999 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002000 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002002 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02002003 goto send_request;
2004 goto exit;
2005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2007 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002008 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002009 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00002010
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002011 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002013 goto exit;
2014 }
Paul Bakker5690efc2011-05-26 13:16:06 +00002015 }
2016
Paul Bakker5121ce52009-01-03 21:22:43 +00002017 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02002018 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002020 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002021 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002022
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002023 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002024 * 7b. Simulate hard reset and reconnect from same port?
2025 */
2026 if( opt.reconnect_hard != 0 )
2027 {
2028 opt.reconnect_hard = 0;
2029
2030 mbedtls_printf( " . Restarting connection from same port..." );
2031 fflush( stdout );
2032
2033 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
2034 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002035 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2036 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002037 goto exit;
2038 }
2039
2040 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
2041 {
2042 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002043 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002044 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002045 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002046 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2047 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002048 goto exit;
2049 }
Hanno Becker16970d22017-10-10 15:56:37 +01002050
2051 /* For event-driven IO, wait for socket to become available */
2052 if( opt.event == 1 /* level triggered IO */ )
2053 {
2054#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002055 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002056#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002057 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002058#endif
2059 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002060 }
2061
2062 mbedtls_printf( " ok\n" );
2063
2064 goto send_request;
2065 }
2066
2067 /*
2068 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002069 */
2070 if( --opt.exchanges > 0 )
2071 goto send_request;
2072
2073 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002074 * 8. Done, cleanly close the connection
2075 */
2076close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002078 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002079
Manuel Pégourié-Gonnard498e6322020-02-17 11:04:33 +01002080 /*
2081 * Most of the time sending a close_notify before closing is the right
2082 * thing to do. However, when the server already knows how many messages
2083 * are expected and closes the connection by itself, this alert becomes
2084 * redundant. Sometimes with DTLS this redundancy becomes a problem by
2085 * leading to a race condition where the server might close the connection
2086 * before seeing the alert, and since UDP is connection-less when the
2087 * alert arrives it will be seen as a new connection, which will fail as
2088 * the alert is clearly not a valid ClientHello. This may cause spurious
2089 * failures in tests that use DTLS and resumption with ssl_server2 in
2090 * ssl-opt.sh, avoided by enabling skip_close_notify client-side.
2091 */
2092 if( opt.skip_close_notify == 0 )
2093 {
2094 /* No error checking, the connection might be closed already */
2095 do ret = mbedtls_ssl_close_notify( &ssl );
2096 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2097 ret = 0;
2098 }
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002101
2102 /*
2103 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002104 */
2105reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002106 if( opt.reconnect != 0 )
2107 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02002108 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002109
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002110 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002111
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002112#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002113 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02002114 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002115#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002120 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002121 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2122 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002123 goto exit;
2124 }
2125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002127 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002128 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n",
2129 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002130 goto exit;
2131 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002132
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002133 if( ( ret = mbedtls_net_connect( &server_fd,
2134 opt.server_addr, opt.server_port,
2135 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2136 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002137 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002138 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
2139 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002140 goto exit;
2141 }
2142
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002143 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002144 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002145 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002146 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002147 if( ret != 0 )
2148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002150 -ret );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002151 goto exit;
2152 }
2153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002155 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002156 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002157 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002158 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002159 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002160 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2161 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002162 goto exit;
2163 }
2164 }
2165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002167
2168 goto send_request;
2169 }
2170
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002171 /*
2172 * Cleanup and exit
2173 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002174exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00002176 if( ret != 0 )
2177 {
2178 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 mbedtls_strerror( ret, error_buf, 100 );
2180 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002181 }
2182#endif
2183
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002184 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186#if defined(MBEDTLS_X509_CRT_PARSE_C)
2187 mbedtls_x509_crt_free( &clicert );
2188 mbedtls_x509_crt_free( &cacert );
2189 mbedtls_pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02002190#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 mbedtls_ssl_session_free( &saved_session );
2192 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002193 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 mbedtls_ctr_drbg_free( &ctr_drbg );
2195 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002196
Paul Bakkercce9d772011-11-18 14:26:47 +00002197#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 fflush( stdout ); getchar();
2200#endif
2201
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002202 // Shell can not handle large exit numbers -> 1 for errors
2203 if( ret < 0 )
2204 ret = 1;
2205
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +02002206 mbedtls_exit( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002207}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
2209 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002210 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */