blob: a1f30732e2cffd58fc3c05b5fea7e0e4129bee3b [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úti4e9f7122020-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úti4e9f7122020-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
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020065#define mbedtls_exit exit
Rich Evansf90016a2015-01-19 14:26:37 +000066#endif
67
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020068#if !defined(MBEDTLS_ENTROPY_C) || \
69 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020070 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020071int main( void )
72{
73 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
74 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020075 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020076 mbedtls_exit( 0 );
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020077}
78#else
79
Andres AG788aa4a2016-09-14 14:32:09 +010080#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000081#include "mbedtls/ssl.h"
82#include "mbedtls/entropy.h"
83#include "mbedtls/ctr_drbg.h"
84#include "mbedtls/certs.h"
85#include "mbedtls/x509.h"
86#include "mbedtls/error.h"
87#include "mbedtls/debug.h"
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020088#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000089
Rich Evans18b78c72015-02-11 14:06:19 +000090#include <stdio.h>
91#include <stdlib.h>
92#include <string.h>
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010093
Hanno Beckere4ad3e82017-09-18 15:05:46 +010094#define MAX_REQUEST_SIZE 20000
95#define MAX_REQUEST_SIZE_STR "20000"
96
Paul Bakker9caf2d22010-02-18 19:37:19 +000097#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010098#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020099#define DFL_SERVER_PORT "4433"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000100#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +0200101#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +0000102#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100103#define DFL_NBIO 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200104#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200105#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +0000106#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +0000107#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +0000108#define DFL_CRT_FILE ""
109#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200110#define DFL_PSK ""
111#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200112#define DFL_ECJPAKE_PW NULL
Paul Bakker51936882011-02-20 16:05:58 +0000113#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100115#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100116#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200117#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200118#define DFL_MIN_VERSION -1
Paul Bakker1d29fb52012-09-28 13:28:45 +0000119#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000120#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +0200121#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100122#define DFL_AUTH_MODE -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100124#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100125#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200126#define DFL_DHMLEN -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200127#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100128#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200129#define DFL_RECONNECT_HARD 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200131#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100132#define DFL_CURVES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200134#define DFL_HS_TO_MIN 0
135#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200136#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200137#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100138#define DFL_ETM -1
Manuel Pégourié-Gonnard5e261e92020-02-17 11:04:33 +0100139#define DFL_SKIP_CLOSE_NOTIFY 0
Paul Bakker0e6975b2009-02-10 22:19:10 +0000140
Paul Bakker93c32b22014-04-25 13:40:05 +0200141#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
142#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_X509_CRT_PARSE_C)
145#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000146#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100147 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
148 " default: \"\" (pre-loaded)\n" \
149 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
150 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
151 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
152 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000153 " key_file=%%s default: \"\" (pre-loaded)\n"
154#else
155#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 " No file operations available (MBEDTLS_FS_IO not defined)\n"
157#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200158#else
159#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200163#define USAGE_PSK \
164 " psk=%%s default: \"\" (in hex, without 0x)\n" \
165 " psk_identity=%%s default: \"Client_identity\"\n"
166#else
167#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200171#define USAGE_TICKETS \
172 " tickets=%%d default: 1 (enabled)\n"
173#else
174#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200178#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100179 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200180#else
181#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200185#define USAGE_MAX_FRAG_LEN \
186 " max_frag_len=%%d default: 16384 (tls default)\n" \
187 " options: 512, 1024, 2048, 4096\n"
188#else
189#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100193#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200194 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100195#else
196#define USAGE_RECSPLIT
197#endif
198
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200199#if defined(MBEDTLS_DHM_C)
200#define USAGE_DHMLEN \
201 " dhmlen=%%d default: (library default: 1024 bits)\n"
202#else
203#define USAGE_DHMLEN
204#endif
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200207#define USAGE_ALPN \
208 " alpn=%%s default: \"\" (disabled)\n" \
209 " example: spdy/1,http/1.1\n"
210#else
211#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200213
Hanno Beckere6706e62017-05-15 16:05:15 +0100214#if defined(MBEDTLS_ECP_C)
215#define USAGE_CURVES \
216 " curves=a,b,c,d default: \"default\" (library default)\n" \
217 " example: \"secp521r1,brainpoolP512r1\"\n" \
218 " - use \"none\" for empty list\n" \
219 " - see mbedtls_ecp_curve_list()\n" \
220 " for acceptable curve names\n"
221#else
222#define USAGE_CURVES ""
223#endif
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200226#define USAGE_DTLS \
227 " dtls=%%d default: 0 (TLS)\n" \
228 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
229 " range of DTLS handshake timeouts in millisecs\n"
230#else
231#define USAGE_DTLS ""
232#endif
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200235#define USAGE_FALLBACK \
236 " fallback=0/1 default: (library default: off)\n"
237#else
238#define USAGE_FALLBACK ""
239#endif
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200242#define USAGE_EMS \
243 " extended_ms=0/1 default: (library default: on)\n"
244#else
245#define USAGE_EMS ""
246#endif
247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100249#define USAGE_ETM \
250 " etm=0/1 default: (library default: on)\n"
251#else
252#define USAGE_ETM ""
253#endif
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100256#define USAGE_RENEGO \
257 " renegotiation=%%d default: 0 (disabled)\n" \
258 " renegotiate=%%d default: 0 (disabled)\n"
259#else
260#define USAGE_RENEGO ""
261#endif
262
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200263#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
264#define USAGE_ECJPAKE \
265 " ecjpake_pw=%%s default: none (disabled)\n"
266#else
267#define USAGE_ECJPAKE ""
268#endif
269
Gilles Peskine63d92c62020-04-14 19:41:01 +0200270/* USAGE is arbitrarily split to stay under the portable string literal
271 * length limit: 4095 bytes in C99. */
272#define USAGE1 \
Paul Bakker67968392010-07-18 08:28:20 +0000273 "\n usage: ssl_client2 param=<>...\n" \
274 "\n acceptable parameters:\n" \
275 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100276 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000277 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000278 " request_page=%%s default: \".\"\n" \
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100279 " request_size=%%d default: about 34 (basic request)\n" \
Andres Amaya Garcia46a6d5c2018-07-04 09:29:34 +0100280 " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
281 " If 0, in the first exchange only an empty\n" \
282 " application data message is sent followed by\n" \
283 " a second non-empty message before attempting\n" \
284 " to read a response from the server\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100285 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100286 " nbio=%%d default: 0 (blocking I/O)\n" \
287 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200288 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200289 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard5e261e92020-02-17 11:04:33 +0100290 " skip_close_notify=%%d default: 0 (send close_notify)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100291 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200292 USAGE_DTLS \
Gilles Peskine63d92c62020-04-14 19:41:01 +0200293 "\n"
294#define USAGE2 \
295 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100296 " options: none, optional, required\n" \
297 USAGE_IO \
298 "\n" \
299 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200300 USAGE_ECJPAKE \
Gilles Peskine63d92c62020-04-14 19:41:01 +0200301 "\n"
302#define USAGE3 \
303 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100304 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200305 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200306 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200307 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200308 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200309 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100310 USAGE_MAX_FRAG_LEN \
311 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200312 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200313 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200314 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100315 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100316 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100317 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200318 USAGE_DHMLEN \
Gilles Peskine63d92c62020-04-14 19:41:01 +0200319 "\n"
320#define USAGE4 \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100321 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200322 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200323 " min_version=%%s default: (library default: tls1)\n" \
324 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000325 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100326 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000327 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000328 " force_ciphersuite=<name> default: all enabled\n"\
329 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000330
Hanno Becker8651a432017-06-09 16:13:22 +0100331#define ALPN_LIST_SIZE 10
332#define CURVE_LIST_SIZE 20
333
Rich Evans85b05ec2015-02-12 11:37:29 +0000334/*
335 * global options
336 */
337struct options
338{
339 const char *server_name; /* hostname of the server (client only) */
340 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200341 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000342 int debug_level; /* level of debugging */
343 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000345 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000346 const char *request_page; /* page on server to request */
347 int request_size; /* pad request with header to requested size */
348 const char *ca_file; /* the file with the CA certificate(s) */
349 const char *ca_path; /* the path with the CA certificate(s) reside */
350 const char *crt_file; /* the file with the client certificate */
351 const char *key_file; /* the file with the client key */
352 const char *psk; /* the pre-shared key */
353 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200354 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000355 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
356 int renegotiation; /* enable / disable renegotiation */
357 int allow_legacy; /* allow legacy renegotiation */
358 int renegotiate; /* attempt renegotiation? */
359 int renego_delay; /* delay before enforcing renegotiation */
360 int exchanges; /* number of data exchanges */
361 int min_version; /* minimum protocol version accepted */
362 int max_version; /* maximum protocol version accepted */
363 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200364 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000365 int auth_mode; /* verify mode for connection */
366 unsigned char mfl_code; /* code for maximum fragment length */
367 int trunc_hmac; /* negotiate truncated hmac or not */
368 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200369 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000370 int reconnect; /* attempt to resume session */
371 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200372 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000373 int tickets; /* enable / disable session tickets */
Hanno Beckere6706e62017-05-15 16:05:15 +0100374 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000375 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000376 int transport; /* TLS or DTLS? */
377 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
378 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Rich Evans85b05ec2015-02-12 11:37:29 +0000379 int fallback; /* is this a fallback connection? */
380 int extended_ms; /* negotiate extended master secret? */
381 int etm; /* negotiate encrypt then mac? */
Manuel Pégourié-Gonnard5e261e92020-02-17 11:04:33 +0100382 int skip_close_notify; /* skip sending the close_notify alert */
Rich Evans85b05ec2015-02-12 11:37:29 +0000383} opt;
384
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200385static void my_debug( void *ctx, int level,
386 const char *file, int line,
387 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000388{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200389 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000390
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200391 /* Extract basename from file */
392 for( p = basename = file; *p != '\0'; p++ )
393 if( *p == '/' || *p == '\\' )
394 basename = p + 1;
395
396 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000397 fflush( (FILE *) ctx );
398}
399
400/*
401 * Test recv/send functions that make sure each try returns
402 * WANT_READ/WANT_WRITE at least once before sucesseding
403 */
404static int my_recv( void *ctx, unsigned char *buf, size_t len )
405{
406 static int first_try = 1;
407 int ret;
408
409 if( first_try )
410 {
411 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100412 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000413 }
414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100416 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000417 first_try = 1; /* Next call will be a new operation */
418 return( ret );
419}
420
421static int my_send( void *ctx, const unsigned char *buf, size_t len )
422{
423 static int first_try = 1;
424 int ret;
425
426 if( first_try )
427 {
428 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100429 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000430 }
431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100433 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000434 first_try = 1; /* Next call will be a new operation */
435 return( ret );
436}
437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#if defined(MBEDTLS_X509_CRT_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000439/*
440 * Enabled if debug_level > 1 in code below
441 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200442static int my_verify( void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000443{
444 char buf[1024];
445 ((void) data);
446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
448 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
449 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000450
Rich Evans85b05ec2015-02-12 11:37:29 +0000451 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100453 else
454 {
455 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
456 mbedtls_printf( "%s\n", buf );
457 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000458
459 return( 0 );
460}
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200461
462static int ssl_sig_hashes_for_test[] = {
463#if defined(MBEDTLS_SHA512_C)
464 MBEDTLS_MD_SHA512,
465 MBEDTLS_MD_SHA384,
466#endif
467#if defined(MBEDTLS_SHA256_C)
468 MBEDTLS_MD_SHA256,
469 MBEDTLS_MD_SHA224,
470#endif
471#if defined(MBEDTLS_SHA1_C)
472 /* Allow SHA-1 as we use it extensively in tests. */
473 MBEDTLS_MD_SHA1,
474#endif
475 MBEDTLS_MD_NONE
476};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000478
Paul Bakker9caf2d22010-02-18 19:37:19 +0000479int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000480{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200481 int ret = 0, len, tail_len, i, written, frags, retry_left;
482 mbedtls_net_context server_fd;
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100483
484 unsigned char buf[MAX_REQUEST_SIZE + 1];
485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
487 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200488 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200489#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100491 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200492#endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100493#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +0100494 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100495 const mbedtls_ecp_curve_info *curve_cur;
496#endif
497
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200498 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000499
Gilles Peskineef86ab22017-05-05 18:59:02 +0200500#if defined(MBEDTLS_X509_CRT_PARSE_C)
501 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
502#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 mbedtls_entropy_context entropy;
504 mbedtls_ctr_drbg_context ctr_drbg;
505 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200506 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200508#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200509 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200510#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200512 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_x509_crt cacert;
514 mbedtls_x509_crt clicert;
515 mbedtls_pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200516#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000517 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000518 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000519
Paul Bakker1a207ec2011-02-06 13:22:40 +0000520 /*
521 * Make sure memory references are valid.
522 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200523 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200524 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200525 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200527 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#if defined(MBEDTLS_X509_CRT_PARSE_C)
529 mbedtls_x509_crt_init( &cacert );
530 mbedtls_x509_crt_init( &clicert );
531 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200532#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200534 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200535#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000536
Paul Bakker9caf2d22010-02-18 19:37:19 +0000537 if( argc == 0 )
538 {
539 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000540 if( ret == 0 )
541 ret = 1;
542
Gilles Peskine63d92c62020-04-14 19:41:01 +0200543 mbedtls_printf( USAGE1 );
544 mbedtls_printf( USAGE2 );
545 mbedtls_printf( USAGE3 );
546 mbedtls_printf( USAGE4 );
Paul Bakker51936882011-02-20 16:05:58 +0000547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000549 while( *list )
550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200552 list++;
553 if( !*list )
554 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000556 list++;
557 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000559 goto exit;
560 }
561
562 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100563 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000564 opt.server_port = DFL_SERVER_PORT;
565 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100566 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200567 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200568 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000569 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200570 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000571 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000572 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000573 opt.crt_file = DFL_CRT_FILE;
574 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200575 opt.psk = DFL_PSK;
576 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200577 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakker51936882011-02-20 16:05:58 +0000578 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000579 opt.renegotiation = DFL_RENEGOTIATION;
580 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100581 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200582 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000583 opt.min_version = DFL_MIN_VERSION;
584 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100585 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +0200586 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100587 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200588 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200589 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100590 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200591 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200592 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100593 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200594 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200595 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200596 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +0100597 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200598 opt.transport = DFL_TRANSPORT;
599 opt.hs_to_min = DFL_HS_TO_MIN;
600 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200601 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200602 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100603 opt.etm = DFL_ETM;
Manuel Pégourié-Gonnard5e261e92020-02-17 11:04:33 +0100604 opt.skip_close_notify = DFL_SKIP_CLOSE_NOTIFY;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000605
606 for( i = 1; i < argc; i++ )
607 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000608 p = argv[i];
609 if( ( q = strchr( p, '=' ) ) == NULL )
610 goto usage;
611 *q++ = '\0';
612
613 if( strcmp( p, "server_name" ) == 0 )
614 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100615 else if( strcmp( p, "server_addr" ) == 0 )
616 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000617 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200618 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100619 else if( strcmp( p, "dtls" ) == 0 )
620 {
621 int t = atoi( q );
622 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100624 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100626 else
627 goto usage;
628 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000629 else if( strcmp( p, "debug_level" ) == 0 )
630 {
631 opt.debug_level = atoi( q );
632 if( opt.debug_level < 0 || opt.debug_level > 65535 )
633 goto usage;
634 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100635 else if( strcmp( p, "nbio" ) == 0 )
636 {
637 opt.nbio = atoi( q );
638 if( opt.nbio < 0 || opt.nbio > 2 )
639 goto usage;
640 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200641 else if( strcmp( p, "read_timeout" ) == 0 )
642 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200643 else if( strcmp( p, "max_resend" ) == 0 )
644 {
645 opt.max_resend = atoi( q );
646 if( opt.max_resend < 0 )
647 goto usage;
648 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000649 else if( strcmp( p, "request_page" ) == 0 )
650 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200651 else if( strcmp( p, "request_size" ) == 0 )
652 {
653 opt.request_size = atoi( q );
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100654 if( opt.request_size < 0 ||
655 opt.request_size > MAX_REQUEST_SIZE )
Paul Bakker93c32b22014-04-25 13:40:05 +0200656 goto usage;
657 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000658 else if( strcmp( p, "ca_file" ) == 0 )
659 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000660 else if( strcmp( p, "ca_path" ) == 0 )
661 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000662 else if( strcmp( p, "crt_file" ) == 0 )
663 opt.crt_file = q;
664 else if( strcmp( p, "key_file" ) == 0 )
665 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200666 else if( strcmp( p, "psk" ) == 0 )
667 opt.psk = q;
668 else if( strcmp( p, "psk_identity" ) == 0 )
669 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200670 else if( strcmp( p, "ecjpake_pw" ) == 0 )
671 opt.ecjpake_pw = q;
Paul Bakker51936882011-02-20 16:05:58 +0000672 else if( strcmp( p, "force_ciphersuite" ) == 0 )
673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +0000675
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200676 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000677 {
678 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000679 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000680 }
Paul Bakker51936882011-02-20 16:05:58 +0000681 opt.force_ciphersuite[1] = 0;
682 }
Paul Bakker48916f92012-09-16 19:57:18 +0000683 else if( strcmp( p, "renegotiation" ) == 0 )
684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 opt.renegotiation = (atoi( q )) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED :
686 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +0000687 }
688 else if( strcmp( p, "allow_legacy" ) == 0 )
689 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100690 switch( atoi( q ) )
691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 case -1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE; break;
693 case 0: opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; break;
694 case 1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION; break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100695 default: goto usage;
696 }
Paul Bakker48916f92012-09-16 19:57:18 +0000697 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100698 else if( strcmp( p, "renegotiate" ) == 0 )
699 {
700 opt.renegotiate = atoi( q );
701 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
702 goto usage;
703 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200704 else if( strcmp( p, "exchanges" ) == 0 )
705 {
706 opt.exchanges = atoi( q );
707 if( opt.exchanges < 1 )
708 goto usage;
709 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200710 else if( strcmp( p, "reconnect" ) == 0 )
711 {
712 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200713 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200714 goto usage;
715 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100716 else if( strcmp( p, "reco_delay" ) == 0 )
717 {
718 opt.reco_delay = atoi( q );
719 if( opt.reco_delay < 0 )
720 goto usage;
721 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200722 else if( strcmp( p, "reconnect_hard" ) == 0 )
723 {
724 opt.reconnect_hard = atoi( q );
725 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
726 goto usage;
727 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200728 else if( strcmp( p, "tickets" ) == 0 )
729 {
730 opt.tickets = atoi( q );
731 if( opt.tickets < 0 || opt.tickets > 2 )
732 goto usage;
733 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200734 else if( strcmp( p, "alpn" ) == 0 )
735 {
736 opt.alpn_string = q;
737 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200738 else if( strcmp( p, "fallback" ) == 0 )
739 {
740 switch( atoi( q ) )
741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
743 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200744 default: goto usage;
745 }
746 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200747 else if( strcmp( p, "extended_ms" ) == 0 )
748 {
749 switch( atoi( q ) )
750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 case 0: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED; break;
752 case 1: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200753 default: goto usage;
754 }
755 }
Hanno Beckere6706e62017-05-15 16:05:15 +0100756 else if( strcmp( p, "curves" ) == 0 )
757 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100758 else if( strcmp( p, "etm" ) == 0 )
759 {
760 switch( atoi( q ) )
761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
763 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100764 default: goto usage;
765 }
766 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000767 else if( strcmp( p, "min_version" ) == 0 )
768 {
769 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000771 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100773 else if( strcmp( q, "tls1_1" ) == 0 ||
774 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100776 else if( strcmp( q, "tls1_2" ) == 0 ||
777 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000779 else
780 goto usage;
781 }
782 else if( strcmp( p, "max_version" ) == 0 )
783 {
784 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000786 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100788 else if( strcmp( q, "tls1_1" ) == 0 ||
789 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100791 else if( strcmp( q, "tls1_2" ) == 0 ||
792 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000794 else
795 goto usage;
796 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100797 else if( strcmp( p, "arc4" ) == 0 )
798 {
799 switch( atoi( q ) )
800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
802 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100803 default: goto usage;
804 }
805 }
Gilles Peskinebc70a182017-05-09 15:59:24 +0200806 else if( strcmp( p, "allow_sha1" ) == 0 )
807 {
808 switch( atoi( q ) )
809 {
810 case 0: opt.allow_sha1 = 0; break;
811 case 1: opt.allow_sha1 = 1; break;
812 default: goto usage;
813 }
814 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000815 else if( strcmp( p, "force_version" ) == 0 )
816 {
817 if( strcmp( q, "ssl3" ) == 0 )
818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
820 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000821 }
822 else if( strcmp( q, "tls1" ) == 0 )
823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
825 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000826 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100827 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
830 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000831 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100832 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
835 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000836 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100837 else if( strcmp( q, "dtls1" ) == 0 )
838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
840 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
841 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100842 }
843 else if( strcmp( q, "dtls1_2" ) == 0 )
844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
846 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
847 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100848 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000849 else
850 goto usage;
851 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100852 else if( strcmp( p, "auth_mode" ) == 0 )
853 {
854 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100856 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100858 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100860 else
861 goto usage;
862 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200863 else if( strcmp( p, "max_frag_len" ) == 0 )
864 {
865 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200867 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200869 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200871 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200873 else
874 goto usage;
875 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200876 else if( strcmp( p, "trunc_hmac" ) == 0 )
877 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100878 switch( atoi( q ) )
879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
881 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100882 default: goto usage;
883 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200884 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200885 else if( strcmp( p, "hs_timeout" ) == 0 )
886 {
887 if( ( p = strchr( q, '-' ) ) == NULL )
888 goto usage;
889 *p++ = '\0';
890 opt.hs_to_min = atoi( q );
891 opt.hs_to_max = atoi( p );
892 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
893 goto usage;
894 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100895 else if( strcmp( p, "recsplit" ) == 0 )
896 {
897 opt.recsplit = atoi( q );
898 if( opt.recsplit < 0 || opt.recsplit > 1 )
899 goto usage;
900 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200901 else if( strcmp( p, "dhmlen" ) == 0 )
902 {
903 opt.dhmlen = atoi( q );
904 if( opt.dhmlen < 0 )
905 goto usage;
906 }
Manuel Pégourié-Gonnard5e261e92020-02-17 11:04:33 +0100907 else if( strcmp( p, "skip_close_notify" ) == 0 )
908 {
909 opt.skip_close_notify = atoi( q );
910 if( opt.skip_close_notify < 0 || opt.skip_close_notify > 1 )
911 goto usage;
912 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000913 else
914 goto usage;
915 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917#if defined(MBEDTLS_DEBUG_C)
918 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200919#endif
920
Paul Bakkerc1516be2013-06-29 16:01:32 +0200921 if( opt.force_ciphersuite[0] > 0 )
922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
924 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +0200925
Paul Bakker66c48102013-07-26 14:05:32 +0200926 if( opt.max_version != -1 &&
927 ciphersuite_info->min_minor_ver > opt.max_version )
928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakker66c48102013-07-26 14:05:32 +0200930 ret = 2;
931 goto usage;
932 }
933 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200934 ciphersuite_info->max_minor_ver < opt.min_version )
935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +0200937 ret = 2;
938 goto usage;
939 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100940
941 /* If the server selects a version that's not supported by
942 * this suite, then there will be no common ciphersuite... */
943 if( opt.max_version == -1 ||
944 opt.max_version > ciphersuite_info->max_minor_ver )
945 {
Paul Bakker66c48102013-07-26 14:05:32 +0200946 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100947 }
Paul Bakker66c48102013-07-26 14:05:32 +0200948 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100949 {
Paul Bakker66c48102013-07-26 14:05:32 +0200950 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100951 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
953 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
954 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100955 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000956
957 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000963 ret = 2;
964 goto usage;
965 }
966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000968 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200969 }
970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000972 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200973 * Unhexify the pre-shared key if any is given
974 */
975 if( strlen( opt.psk ) )
976 {
977 unsigned char c;
978 size_t j;
979
980 if( strlen( opt.psk ) % 2 != 0 )
981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200983 goto exit;
984 }
985
986 psk_len = strlen( opt.psk ) / 2;
987
988 for( j = 0; j < strlen( opt.psk ); j += 2 )
989 {
990 c = opt.psk[j];
991 if( c >= '0' && c <= '9' )
992 c -= '0';
993 else if( c >= 'a' && c <= 'f' )
994 c -= 'a' - 10;
995 else if( c >= 'A' && c <= 'F' )
996 c -= 'A' - 10;
997 else
998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001000 goto exit;
1001 }
1002 psk[ j / 2 ] = c << 4;
1003
1004 c = opt.psk[j + 1];
1005 if( c >= '0' && c <= '9' )
1006 c -= '0';
1007 else if( c >= 'a' && c <= 'f' )
1008 c -= 'a' - 10;
1009 else if( c >= 'A' && c <= 'F' )
1010 c -= 'A' - 10;
1011 else
1012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001014 goto exit;
1015 }
1016 psk[ j / 2 ] |= c;
1017 }
1018 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001020
Hanno Beckere6706e62017-05-15 16:05:15 +01001021#if defined(MBEDTLS_ECP_C)
1022 if( opt.curves != NULL )
1023 {
1024 p = (char *) opt.curves;
1025 i = 0;
1026
1027 if( strcmp( p, "none" ) == 0 )
1028 {
1029 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1030 }
1031 else if( strcmp( p, "default" ) != 0 )
1032 {
1033 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001034 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001035 {
1036 q = p;
1037
1038 /* Terminate the current string */
1039 while( *p != ',' && *p != '\0' )
1040 p++;
1041 if( *p == ',' )
1042 *p++ = '\0';
1043
1044 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1045 {
1046 curve_list[i++] = curve_cur->grp_id;
1047 }
1048 else
1049 {
1050 mbedtls_printf( "unknown curve %s\n", q );
1051 mbedtls_printf( "supported curves: " );
1052 for( curve_cur = mbedtls_ecp_curve_list();
1053 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1054 curve_cur++ )
1055 {
1056 mbedtls_printf( "%s ", curve_cur->name );
1057 }
1058 mbedtls_printf( "\n" );
1059 goto exit;
1060 }
1061 }
1062
1063 mbedtls_printf("Number of curves: %d\n", i );
1064
Hanno Becker8651a432017-06-09 16:13:22 +01001065 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001066 {
Hanno Becker8651a432017-06-09 16:13:22 +01001067 mbedtls_printf( "curves list too long, maximum %d",
1068 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001069 goto exit;
1070 }
1071
1072 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1073 }
1074 }
1075#endif /* MBEDTLS_ECP_C */
1076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001078 if( opt.alpn_string != NULL )
1079 {
1080 p = (char *) opt.alpn_string;
1081 i = 0;
1082
1083 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001084 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001085 {
1086 alpn_list[i++] = p;
1087
1088 /* Terminate the current string and move on to next one */
1089 while( *p != ',' && *p != '\0' )
1090 p++;
1091 if( *p == ',' )
1092 *p++ = '\0';
1093 }
1094 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001096
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001097 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001098 * 0. Initialize the RNG and the session data
1099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001101 fflush( stdout );
1102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001104 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001105 (const unsigned char *) pers,
1106 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +00001107 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001108 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n", -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001109 goto exit;
1110 }
1111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001115 /*
1116 * 1.1. Load the trusted CA
1117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 fflush( stdout );
1120
Hanno Beckerf74f5ce2019-03-05 16:02:15 +00001121 if( strcmp( opt.ca_path, "none" ) == 0 ||
1122 strcmp( opt.ca_file, "none" ) == 0 )
1123 {
1124 ret = 0;
1125 }
1126 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001128 if( strlen( opt.ca_path ) )
Hanno Beckerf74f5ce2019-03-05 16:02:15 +00001129 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001130 else if( strlen( opt.ca_file ) )
Hanno Beckerf74f5ce2019-03-05 16:02:15 +00001131 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001132 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001133#endif
Hanno Becker37e7db22019-03-05 16:10:27 +00001134#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 ret = mbedtls_x509_crt_parse( &cacert,
1138 (const unsigned char *) mbedtls_test_cas[i],
1139 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001140 if( ret != 0 )
1141 break;
1142 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001143#else
1144 {
1145 ret = 1;
Hanno Becker37e7db22019-03-05 16:10:27 +00001146#if !defined(MBEDTLS_CERTS_C)
1147 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
1148#else
1149 mbedtls_printf( "All test CRTs loaded via MBEDTLS_CERTS_C are PEM-encoded, but MBEDTLS_PEM_PARSE_C is disabled." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001150 }
Hanno Becker37e7db22019-03-05 16:10:27 +00001151#endif /* MBEDTLS_CERTS_C */
1152#endif /* MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C */
Paul Bakker42488232012-05-16 08:21:05 +00001153 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 goto exit;
1157 }
1158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001160
1161 /*
1162 * 1.2. Load own certificate and private key
1163 *
1164 * (can be skipped if client authentication is not required)
1165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 fflush( stdout );
1168
Hanno Beckerf74f5ce2019-03-05 16:02:15 +00001169 if( strcmp( opt.crt_file, "none" ) == 0 )
1170 ret = 0;
1171 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001173 if( strlen( opt.crt_file ) )
Hanno Beckerf74f5ce2019-03-05 16:02:15 +00001174 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001175 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001176#endif
Hanno Becker37e7db22019-03-05 16:10:27 +00001177#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
1179 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001180#else
1181 {
1182 ret = 1;
Hanno Becker37e7db22019-03-05 16:10:27 +00001183#if !defined(MBEDTLS_CERTS_C)
1184 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
1185#else
1186 mbedtls_printf( "All test CRTs loaded via MBEDTLS_CERTS_C are PEM-encoded, but MBEDTLS_PEM_PARSE_C is disabled." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001187 }
Hanno Becker37e7db22019-03-05 16:10:27 +00001188#endif /* MBEDTLS_CERTS_C */
1189#endif /* MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001190 if( ret != 0 )
1191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 goto exit;
1194 }
1195
Hanno Beckerf74f5ce2019-03-05 16:02:15 +00001196 if( strcmp( opt.key_file, "none" ) == 0 )
1197 ret = 0;
1198 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001200 if( strlen( opt.key_file ) )
Hanno Beckerf74f5ce2019-03-05 16:02:15 +00001201 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001202 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001203#endif
Hanno Becker37e7db22019-03-05 16:10:27 +00001204#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_cli_key,
1206 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001207#else
1208 {
1209 ret = 1;
Hanno Becker37e7db22019-03-05 16:10:27 +00001210#if !defined(MBEDTLS_CERTS_C)
1211 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
1212#else
1213 mbedtls_printf( "All test keys loaded via MBEDTLS_CERTS_C are PEM-encoded, but MBEDTLS_PEM_PARSE_C is disabled." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001214 }
Hanno Becker37e7db22019-03-05 16:10:27 +00001215#endif /* MBEDTLS_CERTS_C */
1216#endif /* MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001217 if( ret != 0 )
1218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 goto exit;
1221 }
1222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 mbedtls_printf( " ok\n" );
1224#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001225
1226 /*
1227 * 2. Start the connection
1228 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001229 if( opt.server_addr == NULL)
1230 opt.server_addr = opt.server_name;
1231
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001232 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001234 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001235 fflush( stdout );
1236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
1238 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1239 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 goto exit;
1243 }
1244
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001245 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001246 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001247 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001248 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001249 if( ret != 0 )
1250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001252 goto exit;
1253 }
1254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001256
1257 /*
1258 * 3. Setup stuff
1259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 fflush( stdout );
1262
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001263 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1264 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001265 opt.transport,
1266 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001267 {
1268 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
1269 goto exit;
1270 }
1271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001273 /* The default algorithms profile disables SHA-1, but our tests still
1274 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001275 if( opt.allow_sha1 > 0 )
1276 {
1277 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1278 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1279 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1280 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001281
Paul Bakker915275b2012-09-28 07:10:55 +00001282 if( opt.debug_level > 0 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001283 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
Gilles Peskineef86ab22017-05-05 18:59:02 +02001284#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001285
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001286 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001287 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001290 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001291 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001295 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001296 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001297 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001298 goto exit;
1299 }
Paul Bakker05decb22013-08-15 13:33:48 +02001300#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001303 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001304 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001305#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001308 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001309 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001310#endif
1311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001313 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001314 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001315#endif
1316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001318 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001319 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1321 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001322#endif
1323
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001324#if defined(MBEDTLS_DHM_C)
1325 if( opt.dhmlen != DFL_DHMLEN )
1326 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1327#endif
1328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001330 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001331 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001332 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001333 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001334 goto exit;
1335 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001336#endif
1337
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001338 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1339 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001340
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001341 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02001344 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02001345#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001346
Paul Bakker645ce3a2012-10-31 12:32:41 +00001347 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001348 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001349
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001350#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001351 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001352 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001353#endif
Paul Bakker51936882011-02-20 16:05:58 +00001354
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001355 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001356 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001358 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001359#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001362 if( strcmp( opt.ca_path, "none" ) != 0 &&
1363 strcmp( opt.ca_file, "none" ) != 0 )
1364 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001365 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001366 }
1367 if( strcmp( opt.crt_file, "none" ) != 0 &&
1368 strcmp( opt.key_file, "none" ) != 0 )
1369 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001370 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001371 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001372 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001373 goto exit;
1374 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001375 }
Paul Bakkered27a042013-04-18 22:46:23 +02001376#endif
1377
Hanno Beckere6706e62017-05-15 16:05:15 +01001378#if defined(MBEDTLS_ECP_C)
1379 if( opt.curves != NULL &&
1380 strcmp( opt.curves, "default" ) != 0 )
1381 {
1382 mbedtls_ssl_conf_curves( &conf, curve_list );
1383 }
1384#endif
1385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001387 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001388 (const unsigned char *) opt.psk_identity,
1389 strlen( opt.psk_identity ) ) ) != 0 )
1390 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001391 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001392 goto exit;
1393 }
Paul Bakkered27a042013-04-18 22:46:23 +02001394#endif
1395
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001396 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001397 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001398
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001399 if( opt.max_version != DFL_MAX_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001400 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001403 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001404 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001405#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001407 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
1408 {
1409 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
1410 goto exit;
1411 }
1412
1413#if defined(MBEDTLS_X509_CRT_PARSE_C)
1414 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1415 {
1416 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
1417 goto exit;
1418 }
1419#endif
1420
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001421#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1422 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
1423 {
1424 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
1425 (const unsigned char *) opt.ecjpake_pw,
1426 strlen( opt.ecjpake_pw ) ) ) != 0 )
1427 {
1428 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
1429 goto exit;
1430 }
1431 }
1432#endif
1433
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001434 if( opt.nbio == 2 )
1435 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
1436 else
1437 mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02001438 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001439
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001440#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001441 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
1442 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001443#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001446
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 /*
1448 * 4. Handshake
1449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001451 fflush( stdout );
1452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001455 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker40e46942009-01-03 21:51:57 +00001456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n", -ret );
1458 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
1459 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001460 " Unable to verify the server's certificate. "
1461 "Either it is invalid,\n"
1462 " or you didn't set ca_file or ca_path "
1463 "to an appropriate value.\n"
1464 " Alternatively, you may want to use "
1465 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001467 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00001468 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001469 }
1470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1472 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
1475 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001476 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001478
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02001479#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1480 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
1481 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
1482#endif
1483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001485 if( opt.alpn_string != NULL )
1486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
1488 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001489 alp ? alp : "(none)" );
1490 }
1491#endif
1492
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001493 if( opt.reconnect != 0 )
1494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001496 fflush( stdout );
1497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001501 goto exit;
1502 }
1503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001505 }
1506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001508 /*
1509 * 5. Verify the server certificate
1510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001512
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001513 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001514 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001515 char vrfy_buf[512];
1516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001518
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001519 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00001520
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001521 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001522 }
1523 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 mbedtls_printf( " . Peer certificate information ...\n" );
1529 mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1530 mbedtls_ssl_get_peer_cert( &ssl ) );
1531 mbedtls_printf( "%s\n", buf );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001532 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001536 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001537 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001538 /*
1539 * Perform renegotiation (this must be done when the server is waiting
1540 * for input from our side).
1541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001543 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001545 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001546 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1547 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001550 goto exit;
1551 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001552 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001554 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001556
Paul Bakker5121ce52009-01-03 21:22:43 +00001557 /*
1558 * 6. Write the GET request
1559 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001560 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001561send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001563 fflush( stdout );
1564
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001565 len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST,
1566 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001567 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001568
1569 /* Add padding to GET request to reach opt.request_size in length */
1570 if( opt.request_size != DFL_REQUEST_SIZE &&
1571 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001572 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001573 memset( buf + len, 'A', opt.request_size - len - tail_len );
1574 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001575 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001576
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001577 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001578 len += tail_len;
1579
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001580 /* Truncate if request size is smaller than the "natural" size */
1581 if( opt.request_size != DFL_REQUEST_SIZE &&
1582 len > opt.request_size )
1583 {
1584 len = opt.request_size;
1585
1586 /* Still end with \r\n unless that's really not possible */
1587 if( len >= 2 ) buf[len - 2] = '\r';
1588 if( len >= 1 ) buf[len - 1] = '\n';
1589 }
1590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001592 {
Andres Amaya Garcia46a6d5c2018-07-04 09:29:34 +01001593 written = 0;
1594 frags = 0;
1595
1596 do
Paul Bakker5121ce52009-01-03 21:22:43 +00001597 {
Andres Amaya Garcia46a6d5c2018-07-04 09:29:34 +01001598 while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
1599 len - written ) ) < 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001600 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001601 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1602 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001605 goto exit;
1606 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001607 }
Andres Amaya Garcia46a6d5c2018-07-04 09:29:34 +01001608
1609 frags++;
1610 written += ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001611 }
Andres Amaya Garcia46a6d5c2018-07-04 09:29:34 +01001612 while( written < len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001613 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001614 else /* Not stream, so datagram */
1615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 do ret = mbedtls_ssl_write( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001617 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1618 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001619
1620 if( ret < 0 )
1621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001623 goto exit;
1624 }
1625
1626 frags = 1;
1627 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001628
1629 if( written < len )
1630 {
1631 mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
1632 "was truncated to size %u", (unsigned) written );
1633 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001634 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001635
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001636 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001638
Andres Amaya Garcia46a6d5c2018-07-04 09:29:34 +01001639 /* Send a non-empty request if request_size == 0 */
1640 if ( len == 0 )
1641 {
1642 opt.request_size = DFL_REQUEST_SIZE;
1643 goto send_request;
1644 }
1645
Paul Bakker5121ce52009-01-03 21:22:43 +00001646 /*
1647 * 7. Read the HTTP response
1648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001650 fflush( stdout );
1651
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001652 /*
1653 * TLS and DTLS need different reading styles (stream vs datagram)
1654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001656 {
1657 do
1658 {
1659 len = sizeof( buf ) - 1;
1660 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001662
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001663 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1664 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001665 continue;
1666
1667 if( ret <= 0 )
1668 {
1669 switch( ret )
1670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1672 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001673 ret = 0;
1674 goto close_notify;
1675
1676 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 case MBEDTLS_ERR_NET_CONN_RESET:
1678 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001679 ret = 0;
1680 goto reconnect;
1681
1682 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001684 goto exit;
1685 }
1686 }
1687
1688 len = ret;
1689 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001691
1692 /* End of message should be detected according to the syntax of the
1693 * application protocol (eg HTTP), just use a dummy test here. */
1694 if( ret > 0 && buf[len-1] == '\n' )
1695 {
1696 ret = 0;
1697 break;
1698 }
1699 }
1700 while( 1 );
1701 }
1702 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001703 {
1704 len = sizeof( buf ) - 1;
1705 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 do ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001708 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1709 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001710
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001711 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001712 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001713 switch( ret )
1714 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001715 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001717 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001718 goto send_request;
1719 goto exit;
1720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1722 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001723 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001724 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00001725
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001726 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001728 goto exit;
1729 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001730 }
1731
Paul Bakker5121ce52009-01-03 21:22:43 +00001732 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02001733 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001735 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001736 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001737
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001738 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001739 * 7b. Simulate hard reset and reconnect from same port?
1740 */
1741 if( opt.reconnect_hard != 0 )
1742 {
1743 opt.reconnect_hard = 0;
1744
1745 mbedtls_printf( " . Restarting connection from same port..." );
1746 fflush( stdout );
1747
1748 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
1749 {
1750 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", -ret );
1751 goto exit;
1752 }
1753
1754 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
1755 {
1756 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1757 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1758 {
1759 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
1760 goto exit;
1761 }
1762 }
1763
1764 mbedtls_printf( " ok\n" );
1765
1766 goto send_request;
1767 }
1768
1769 /*
1770 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001771 */
1772 if( --opt.exchanges > 0 )
1773 goto send_request;
1774
1775 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001776 * 8. Done, cleanly close the connection
1777 */
1778close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001780 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001781
Manuel Pégourié-Gonnard5e261e92020-02-17 11:04:33 +01001782 /*
1783 * Most of the time sending a close_notify before closing is the right
1784 * thing to do. However, when the server already knows how many messages
1785 * are expected and closes the connection by itself, this alert becomes
1786 * redundant. Sometimes with DTLS this redundancy becomes a problem by
1787 * leading to a race condition where the server might close the connection
1788 * before seeing the alert, and since UDP is connection-less when the
1789 * alert arrives it will be seen as a new connection, which will fail as
1790 * the alert is clearly not a valid ClientHello. This may cause spurious
1791 * failures in tests that use DTLS and resumption with ssl_server2 in
1792 * ssl-opt.sh, avoided by enabling skip_close_notify client-side.
1793 */
1794 if( opt.skip_close_notify == 0 )
1795 {
1796 /* No error checking, the connection might be closed already */
1797 do ret = mbedtls_ssl_close_notify( &ssl );
1798 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1799 ret = 0;
1800 }
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001803
1804 /*
1805 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001806 */
1807reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001808 if( opt.reconnect != 0 )
1809 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001810 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001811
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001812 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001813
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001814#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001815 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02001816 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001817#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02001818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001824 goto exit;
1825 }
1826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001828 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001829 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001830 goto exit;
1831 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
1834 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1835 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001838 goto exit;
1839 }
1840
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001841 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001842 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001843 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001844 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001845 if( ret != 0 )
1846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001848 -ret );
1849 goto exit;
1850 }
1851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001853 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001854 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1855 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001858 goto exit;
1859 }
1860 }
1861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001863
1864 goto send_request;
1865 }
1866
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001867 /*
1868 * Cleanup and exit
1869 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001870exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00001872 if( ret != 0 )
1873 {
1874 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 mbedtls_strerror( ret, error_buf, 100 );
1876 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001877 }
1878#endif
1879
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001880 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882#if defined(MBEDTLS_X509_CRT_PARSE_C)
1883 mbedtls_x509_crt_free( &clicert );
1884 mbedtls_x509_crt_free( &cacert );
1885 mbedtls_pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001886#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 mbedtls_ssl_session_free( &saved_session );
1888 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001889 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 mbedtls_ctr_drbg_free( &ctr_drbg );
1891 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00001892
Paul Bakkercce9d772011-11-18 14:26:47 +00001893#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001895 fflush( stdout ); getchar();
1896#endif
1897
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001898 // Shell can not handle large exit numbers -> 1 for errors
1899 if( ret < 0 )
1900 ret = 1;
1901
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +02001902 mbedtls_exit( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001903}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
1905 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001906 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */