blob: 7cdaeacf23170193330bad57415cd2d6f6bb8168 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client with certificate authentication
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Simon Butcherd3138c32016-04-27 01:26:50 +010032#include <stdlib.h>
33#define mbedtls_time time
34#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#define mbedtls_printf printf
36#define mbedtls_fprintf fprintf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define mbedtls_snprintf snprintf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010038#define mbedtls_exit exit
39#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
40#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evansf90016a2015-01-19 14:26:37 +000041#endif
42
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020043#if !defined(MBEDTLS_ENTROPY_C) || \
44 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020045 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020046int main( void )
47{
48 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
49 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020050 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020051 return( 0 );
52}
53#else
54
Andres AG788aa4a2016-09-14 14:32:09 +010055#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/ssl.h"
57#include "mbedtls/entropy.h"
58#include "mbedtls/ctr_drbg.h"
59#include "mbedtls/certs.h"
60#include "mbedtls/x509.h"
61#include "mbedtls/error.h"
62#include "mbedtls/debug.h"
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020063#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Rich Evans18b78c72015-02-11 14:06:19 +000065#include <stdio.h>
66#include <stdlib.h>
67#include <string.h>
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010068
Hanno Beckere4ad3e82017-09-18 15:05:46 +010069#define MAX_REQUEST_SIZE 20000
70#define MAX_REQUEST_SIZE_STR "20000"
71
Paul Bakker9caf2d22010-02-18 19:37:19 +000072#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010073#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020074#define DFL_SERVER_PORT "4433"
Paul Bakker9caf2d22010-02-18 19:37:19 +000075#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +020076#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +000077#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010078#define DFL_NBIO 0
Hanno Becker16970d22017-10-10 15:56:37 +010079#define DFL_EVENT 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020080#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +020081#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +000082#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000083#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000084#define DFL_CRT_FILE ""
85#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020086#define DFL_PSK ""
87#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +020088#define DFL_ECJPAKE_PW NULL
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +020089#define DFL_EC_MAX_OPS -1
Paul Bakker51936882011-02-20 16:05:58 +000090#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010092#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010093#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +020094#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +020095#define DFL_MIN_VERSION -1
Paul Bakker1d29fb52012-09-28 13:28:45 +000096#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +000097#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +020098#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +010099#define DFL_AUTH_MODE -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100101#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100102#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200103#define DFL_DHMLEN -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200104#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100105#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200106#define DFL_RECONNECT_HARD 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200108#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100109#define DFL_CURVES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200111#define DFL_HS_TO_MIN 0
112#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200113#define DFL_DTLS_MTU -1
Hanno Becker4d615912018-08-14 13:33:30 +0100114#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200115#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200116#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100117#define DFL_ETM -1
Paul Bakker0e6975b2009-02-10 22:19:10 +0000118
Paul Bakker93c32b22014-04-25 13:40:05 +0200119#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
120#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if defined(MBEDTLS_X509_CRT_PARSE_C)
123#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000124#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100125 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
126 " default: \"\" (pre-loaded)\n" \
127 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
128 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
129 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
130 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000131 " key_file=%%s default: \"\" (pre-loaded)\n"
132#else
133#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 " No file operations available (MBEDTLS_FS_IO not defined)\n"
135#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200136#else
137#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200141#define USAGE_PSK \
142 " psk=%%s default: \"\" (in hex, without 0x)\n" \
143 " psk_identity=%%s default: \"Client_identity\"\n"
144#else
145#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200149#define USAGE_TICKETS \
150 " tickets=%%d default: 1 (enabled)\n"
151#else
152#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200156#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100157 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200158#else
159#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200163#define USAGE_MAX_FRAG_LEN \
164 " max_frag_len=%%d default: 16384 (tls default)\n" \
165 " options: 512, 1024, 2048, 4096\n"
166#else
167#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100171#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200172 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100173#else
174#define USAGE_RECSPLIT
175#endif
176
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200177#if defined(MBEDTLS_DHM_C)
178#define USAGE_DHMLEN \
179 " dhmlen=%%d default: (library default: 1024 bits)\n"
180#else
181#define USAGE_DHMLEN
182#endif
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200185#define USAGE_ALPN \
186 " alpn=%%s default: \"\" (disabled)\n" \
187 " example: spdy/1,http/1.1\n"
188#else
189#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200191
Hanno Beckere6706e62017-05-15 16:05:15 +0100192#if defined(MBEDTLS_ECP_C)
193#define USAGE_CURVES \
194 " curves=a,b,c,d default: \"default\" (library default)\n" \
195 " example: \"secp521r1,brainpoolP512r1\"\n" \
196 " - use \"none\" for empty list\n" \
197 " - see mbedtls_ecp_curve_list()\n" \
198 " for acceptable curve names\n"
199#else
200#define USAGE_CURVES ""
201#endif
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200204#define USAGE_DTLS \
205 " dtls=%%d default: 0 (TLS)\n" \
206 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200207 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Becker4d615912018-08-14 13:33:30 +0100208 " mtu=%%d default: (library default: unlimited)\n" \
209 " dgram_packing=%%d default: 1 (allowed)\n" \
210 " allow or forbid packing of multiple\n" \
211 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200212#else
213#define USAGE_DTLS ""
214#endif
215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200217#define USAGE_FALLBACK \
218 " fallback=0/1 default: (library default: off)\n"
219#else
220#define USAGE_FALLBACK ""
221#endif
222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200224#define USAGE_EMS \
225 " extended_ms=0/1 default: (library default: on)\n"
226#else
227#define USAGE_EMS ""
228#endif
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100231#define USAGE_ETM \
232 " etm=0/1 default: (library default: on)\n"
233#else
234#define USAGE_ETM ""
235#endif
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100238#define USAGE_RENEGO \
239 " renegotiation=%%d default: 0 (disabled)\n" \
240 " renegotiate=%%d default: 0 (disabled)\n"
241#else
242#define USAGE_RENEGO ""
243#endif
244
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200245#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
246#define USAGE_ECJPAKE \
247 " ecjpake_pw=%%s default: none (disabled)\n"
248#else
249#define USAGE_ECJPAKE ""
250#endif
251
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200252#if defined(MBEDTLS_ECP_RESTARTABLE)
253#define USAGE_ECRESTART \
254 " ec_max_ops=%%s default: library default (restart disabled)\n"
255#else
256#define USAGE_ECRESTART ""
257#endif
258
Paul Bakker9caf2d22010-02-18 19:37:19 +0000259#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000260 "\n usage: ssl_client2 param=<>...\n" \
261 "\n acceptable parameters:\n" \
262 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100263 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000264 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000265 " request_page=%%s default: \".\"\n" \
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +0100266 " request_size=%%d default: about 34 (basic request)\n" \
267 " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
268 " If 0, in the first exchange only an empty\n" \
269 " application data message is sent followed by\n" \
270 " a second non-empty message before attempting\n" \
271 " to read a response from the server\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100272 " debug_level=%%d default: 0 (disabled)\n" \
273 " nbio=%%d default: 0 (blocking I/O)\n" \
274 " options: 1 (non-blocking), 2 (added delays)\n" \
275 " event=%%d default: 0 (loop)\n" \
276 " options: 1 (level-triggered, implies nbio=1),\n" \
277 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200278 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100279 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200280 USAGE_DTLS \
281 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100282 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100283 " options: none, optional, required\n" \
284 USAGE_IO \
285 "\n" \
286 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200287 USAGE_ECJPAKE \
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200288 USAGE_ECRESTART \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100289 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100290 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100291 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200292 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200293 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200294 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200295 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200296 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100297 USAGE_MAX_FRAG_LEN \
298 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200299 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200300 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200301 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100302 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100303 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100304 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200305 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000306 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100307 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200308 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200309 " min_version=%%s default: (library default: tls1)\n" \
310 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000311 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100312 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000313 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000314 " force_ciphersuite=<name> default: all enabled\n"\
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100315 " query_config=<name> return 0 if the specified\n" \
316 " configuration macro is defined and 1\n" \
317 " otherwise. The expansion of the macro\n" \
318 " is printed if it is defined\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000319 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000320
Hanno Becker8651a432017-06-09 16:13:22 +0100321#define ALPN_LIST_SIZE 10
322#define CURVE_LIST_SIZE 20
323
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100324#if defined(MBEDTLS_CHECK_PARAMS)
325#include "mbedtls/platform_util.h"
326void mbedtls_param_failed( const char *failure_condition,
327 const char *file,
328 int line )
Simon Butcher63cb97e2018-12-06 17:43:31 +0000329{
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100330 mbedtls_printf( "%s:%i: Input param failed - %s\n",
331 file, line, failure_condition );
Simon Butcher63cb97e2018-12-06 17:43:31 +0000332 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
333}
334#endif
335
Rich Evans85b05ec2015-02-12 11:37:29 +0000336/*
337 * global options
338 */
339struct options
340{
341 const char *server_name; /* hostname of the server (client only) */
342 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200343 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000344 int debug_level; /* level of debugging */
345 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100346 int event; /* loop or event-driven IO? level or edge triggered? */
347 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000348 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000349 const char *request_page; /* page on server to request */
350 int request_size; /* pad request with header to requested size */
351 const char *ca_file; /* the file with the CA certificate(s) */
352 const char *ca_path; /* the path with the CA certificate(s) reside */
353 const char *crt_file; /* the file with the client certificate */
354 const char *key_file; /* the file with the client key */
355 const char *psk; /* the pre-shared key */
356 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200357 const char *ecjpake_pw; /* the EC J-PAKE password */
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200358 int ec_max_ops; /* EC consecutive operations limit */
Rich Evans85b05ec2015-02-12 11:37:29 +0000359 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
360 int renegotiation; /* enable / disable renegotiation */
361 int allow_legacy; /* allow legacy renegotiation */
362 int renegotiate; /* attempt renegotiation? */
363 int renego_delay; /* delay before enforcing renegotiation */
364 int exchanges; /* number of data exchanges */
365 int min_version; /* minimum protocol version accepted */
366 int max_version; /* maximum protocol version accepted */
367 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200368 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000369 int auth_mode; /* verify mode for connection */
370 unsigned char mfl_code; /* code for maximum fragment length */
371 int trunc_hmac; /* negotiate truncated hmac or not */
372 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200373 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000374 int reconnect; /* attempt to resume session */
375 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200376 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000377 int tickets; /* enable / disable session tickets */
Hanno Beckere6706e62017-05-15 16:05:15 +0100378 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000379 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000380 int transport; /* TLS or DTLS? */
381 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
382 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200383 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Rich Evans85b05ec2015-02-12 11:37:29 +0000384 int fallback; /* is this a fallback connection? */
Hanno Becker4d615912018-08-14 13:33:30 +0100385 int dgram_packing; /* allow/forbid datagram packing */
Rich Evans85b05ec2015-02-12 11:37:29 +0000386 int extended_ms; /* negotiate extended master secret? */
387 int etm; /* negotiate encrypt then mac? */
388} opt;
389
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100390int query_config( const char *config );
391
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200392static void my_debug( void *ctx, int level,
393 const char *file, int line,
394 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000395{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200396 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000397
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200398 /* Extract basename from file */
399 for( p = basename = file; *p != '\0'; p++ )
400 if( *p == '/' || *p == '\\' )
401 basename = p + 1;
402
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100403 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s",
404 basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000405 fflush( (FILE *) ctx );
406}
407
408/*
409 * Test recv/send functions that make sure each try returns
410 * WANT_READ/WANT_WRITE at least once before sucesseding
411 */
412static int my_recv( void *ctx, unsigned char *buf, size_t len )
413{
414 static int first_try = 1;
415 int ret;
416
417 if( first_try )
418 {
419 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100420 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000421 }
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100424 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000425 first_try = 1; /* Next call will be a new operation */
426 return( ret );
427}
428
429static int my_send( void *ctx, const unsigned char *buf, size_t len )
430{
431 static int first_try = 1;
432 int ret;
433
434 if( first_try )
435 {
436 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100437 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000438 }
439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100441 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000442 first_try = 1; /* Next call will be a new operation */
443 return( ret );
444}
445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#if defined(MBEDTLS_X509_CRT_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000447/*
448 * Enabled if debug_level > 1 in code below
449 */
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100450static int my_verify( void *data, mbedtls_x509_crt *crt,
451 int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000452{
453 char buf[1024];
454 ((void) data);
455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
457 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
458 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000459
Rich Evans85b05ec2015-02-12 11:37:29 +0000460 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100462 else
463 {
464 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
465 mbedtls_printf( "%s\n", buf );
466 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000467
468 return( 0 );
469}
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200470
471static int ssl_sig_hashes_for_test[] = {
472#if defined(MBEDTLS_SHA512_C)
473 MBEDTLS_MD_SHA512,
474 MBEDTLS_MD_SHA384,
475#endif
476#if defined(MBEDTLS_SHA256_C)
477 MBEDTLS_MD_SHA256,
478 MBEDTLS_MD_SHA224,
479#endif
480#if defined(MBEDTLS_SHA1_C)
481 /* Allow SHA-1 as we use it extensively in tests. */
482 MBEDTLS_MD_SHA1,
483#endif
484 MBEDTLS_MD_NONE
485};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000487
Hanno Becker16970d22017-10-10 15:56:37 +0100488/*
489 * Wait for an event from the underlying transport or the timer
490 * (Used in event-driven IO mode).
491 */
492#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000493int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000494 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100495#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000496int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000497 mbedtls_timing_delay_context *timer,
498 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100499#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000500{
Hanno Becker16970d22017-10-10 15:56:37 +0100501
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000502 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +0100503 int poll_type = 0;
504
505 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
506 poll_type = MBEDTLS_NET_POLL_WRITE;
507 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
508 poll_type = MBEDTLS_NET_POLL_READ;
509#if !defined(MBEDTLS_TIMING_C)
510 else
Hanno Beckeref527962018-03-15 15:49:24 +0000511 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100512#endif
513
514 while( 1 )
515 {
Hanno Becker197a91c2017-10-31 10:58:53 +0000516 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +0100517#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +0000518 if( timer != NULL &&
519 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +0100520 {
Hanno Becker16970d22017-10-10 15:56:37 +0100521 break;
522 }
Hanno Becker197a91c2017-10-31 10:58:53 +0000523#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +0100524
Hanno Becker197a91c2017-10-31 10:58:53 +0000525 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000526 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +0100527 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000528 ret = mbedtls_net_poll( fd, poll_type, 0 );
529 if( ret < 0 )
530 return( ret );
531 if( ret == poll_type )
532 break;
Hanno Becker16970d22017-10-10 15:56:37 +0100533 }
534 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000535
536 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100537}
538
Hanno Beckerec370302019-04-09 17:12:56 +0100539/* Unhexify `hex` into `dst`. `dst` must have
540 * size at least `strlen( hex ) / 2`. */
541int unhexify( unsigned char const *hex, unsigned char *dst )
542{
543 unsigned char c;
544 size_t j;
545 size_t len = strlen( hex );
546
547 if( len % 2 != 0 )
548 return( -1 );
549
550 for( j = 0; j < len; j += 2 )
551 {
552 c = hex[j];
553 if( c >= '0' && c <= '9' )
554 c -= '0';
555 else if( c >= 'a' && c <= 'f' )
556 c -= 'a' - 10;
557 else if( c >= 'A' && c <= 'F' )
558 c -= 'A' - 10;
559 else
560 return( -1 );
561 dst[ j / 2 ] = c << 4;
562
563 c = hex[j + 1];
564 if( c >= '0' && c <= '9' )
565 c -= '0';
566 else if( c >= 'a' && c <= 'f' )
567 c -= 'a' - 10;
568 else if( c >= 'A' && c <= 'F' )
569 c -= 'A' - 10;
570 else
571 return( -1 );
572 dst[ j / 2 ] |= c;
573 }
574
575 return( 0 );
576}
577
Paul Bakker9caf2d22010-02-18 19:37:19 +0000578int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000579{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200580 int ret = 0, len, tail_len, i, written, frags, retry_left;
581 mbedtls_net_context server_fd;
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100582
583 unsigned char buf[MAX_REQUEST_SIZE + 1];
584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
586 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200587 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200588#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100590 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200591#endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100592#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +0100593 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100594 const mbedtls_ecp_curve_info *curve_cur;
595#endif
596
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200597 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000598
Gilles Peskineef86ab22017-05-05 18:59:02 +0200599#if defined(MBEDTLS_X509_CRT_PARSE_C)
600 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
601#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_entropy_context entropy;
603 mbedtls_ctr_drbg_context ctr_drbg;
604 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200605 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200607#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200608 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200609#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200611 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_x509_crt cacert;
613 mbedtls_x509_crt clicert;
614 mbedtls_pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200615#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000616 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000617 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000618
Paul Bakker1a207ec2011-02-06 13:22:40 +0000619 /*
620 * Make sure memory references are valid.
621 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200622 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200623 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200624 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200626 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627#if defined(MBEDTLS_X509_CRT_PARSE_C)
628 mbedtls_x509_crt_init( &cacert );
629 mbedtls_x509_crt_init( &clicert );
630 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200631#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200633 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200634#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000635
Paul Bakker9caf2d22010-02-18 19:37:19 +0000636 if( argc == 0 )
637 {
638 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000639 if( ret == 0 )
640 ret = 1;
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000645 while( *list )
646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200648 list++;
649 if( !*list )
650 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000652 list++;
653 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000655 goto exit;
656 }
657
658 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100659 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000660 opt.server_port = DFL_SERVER_PORT;
661 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100662 opt.nbio = DFL_NBIO;
Hanno Becker16970d22017-10-10 15:56:37 +0100663 opt.event = DFL_EVENT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200664 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200665 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000666 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200667 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000668 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000669 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000670 opt.crt_file = DFL_CRT_FILE;
671 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200672 opt.psk = DFL_PSK;
673 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200674 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200675 opt.ec_max_ops = DFL_EC_MAX_OPS;
Paul Bakker51936882011-02-20 16:05:58 +0000676 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000677 opt.renegotiation = DFL_RENEGOTIATION;
678 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100679 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200680 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000681 opt.min_version = DFL_MIN_VERSION;
682 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100683 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +0200684 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100685 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200686 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200687 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100688 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200689 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200690 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100691 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200692 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200693 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200694 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +0100695 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200696 opt.transport = DFL_TRANSPORT;
697 opt.hs_to_min = DFL_HS_TO_MIN;
698 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200699 opt.dtls_mtu = DFL_DTLS_MTU;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200700 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200701 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100702 opt.etm = DFL_ETM;
Hanno Becker4d615912018-08-14 13:33:30 +0100703 opt.dgram_packing = DFL_DGRAM_PACKING;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000704
705 for( i = 1; i < argc; i++ )
706 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000707 p = argv[i];
708 if( ( q = strchr( p, '=' ) ) == NULL )
709 goto usage;
710 *q++ = '\0';
711
712 if( strcmp( p, "server_name" ) == 0 )
713 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100714 else if( strcmp( p, "server_addr" ) == 0 )
715 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000716 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200717 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100718 else if( strcmp( p, "dtls" ) == 0 )
719 {
720 int t = atoi( q );
721 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100723 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100725 else
726 goto usage;
727 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000728 else if( strcmp( p, "debug_level" ) == 0 )
729 {
730 opt.debug_level = atoi( q );
731 if( opt.debug_level < 0 || opt.debug_level > 65535 )
732 goto usage;
733 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100734 else if( strcmp( p, "nbio" ) == 0 )
735 {
736 opt.nbio = atoi( q );
737 if( opt.nbio < 0 || opt.nbio > 2 )
738 goto usage;
739 }
Hanno Becker16970d22017-10-10 15:56:37 +0100740 else if( strcmp( p, "event" ) == 0 )
741 {
742 opt.event = atoi( q );
743 if( opt.event < 0 || opt.event > 2 )
744 goto usage;
745 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200746 else if( strcmp( p, "read_timeout" ) == 0 )
747 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200748 else if( strcmp( p, "max_resend" ) == 0 )
749 {
750 opt.max_resend = atoi( q );
751 if( opt.max_resend < 0 )
752 goto usage;
753 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000754 else if( strcmp( p, "request_page" ) == 0 )
755 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200756 else if( strcmp( p, "request_size" ) == 0 )
757 {
758 opt.request_size = atoi( q );
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100759 if( opt.request_size < 0 ||
760 opt.request_size > MAX_REQUEST_SIZE )
Paul Bakker93c32b22014-04-25 13:40:05 +0200761 goto usage;
762 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000763 else if( strcmp( p, "ca_file" ) == 0 )
764 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000765 else if( strcmp( p, "ca_path" ) == 0 )
766 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000767 else if( strcmp( p, "crt_file" ) == 0 )
768 opt.crt_file = q;
769 else if( strcmp( p, "key_file" ) == 0 )
770 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200771 else if( strcmp( p, "psk" ) == 0 )
772 opt.psk = q;
773 else if( strcmp( p, "psk_identity" ) == 0 )
774 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200775 else if( strcmp( p, "ecjpake_pw" ) == 0 )
776 opt.ecjpake_pw = q;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200777 else if( strcmp( p, "ec_max_ops" ) == 0 )
778 opt.ec_max_ops = atoi( q );
Paul Bakker51936882011-02-20 16:05:58 +0000779 else if( strcmp( p, "force_ciphersuite" ) == 0 )
780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +0000782
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200783 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000784 {
785 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000786 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000787 }
Paul Bakker51936882011-02-20 16:05:58 +0000788 opt.force_ciphersuite[1] = 0;
789 }
Paul Bakker48916f92012-09-16 19:57:18 +0000790 else if( strcmp( p, "renegotiation" ) == 0 )
791 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100792 opt.renegotiation = (atoi( q )) ?
793 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
794 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +0000795 }
796 else if( strcmp( p, "allow_legacy" ) == 0 )
797 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100798 switch( atoi( q ) )
799 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100800 case -1:
801 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
802 break;
803 case 0:
804 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
805 break;
806 case 1:
807 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
808 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100809 default: goto usage;
810 }
Paul Bakker48916f92012-09-16 19:57:18 +0000811 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100812 else if( strcmp( p, "renegotiate" ) == 0 )
813 {
814 opt.renegotiate = atoi( q );
815 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
816 goto usage;
817 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200818 else if( strcmp( p, "exchanges" ) == 0 )
819 {
820 opt.exchanges = atoi( q );
821 if( opt.exchanges < 1 )
822 goto usage;
823 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200824 else if( strcmp( p, "reconnect" ) == 0 )
825 {
826 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200827 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200828 goto usage;
829 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100830 else if( strcmp( p, "reco_delay" ) == 0 )
831 {
832 opt.reco_delay = atoi( q );
833 if( opt.reco_delay < 0 )
834 goto usage;
835 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200836 else if( strcmp( p, "reconnect_hard" ) == 0 )
837 {
838 opt.reconnect_hard = atoi( q );
839 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
840 goto usage;
841 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200842 else if( strcmp( p, "tickets" ) == 0 )
843 {
844 opt.tickets = atoi( q );
845 if( opt.tickets < 0 || opt.tickets > 2 )
846 goto usage;
847 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200848 else if( strcmp( p, "alpn" ) == 0 )
849 {
850 opt.alpn_string = q;
851 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200852 else if( strcmp( p, "fallback" ) == 0 )
853 {
854 switch( atoi( q ) )
855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
857 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200858 default: goto usage;
859 }
860 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200861 else if( strcmp( p, "extended_ms" ) == 0 )
862 {
863 switch( atoi( q ) )
864 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100865 case 0:
866 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
867 break;
868 case 1:
869 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
870 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200871 default: goto usage;
872 }
873 }
Hanno Beckere6706e62017-05-15 16:05:15 +0100874 else if( strcmp( p, "curves" ) == 0 )
875 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100876 else if( strcmp( p, "etm" ) == 0 )
877 {
878 switch( atoi( q ) )
879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
881 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100882 default: goto usage;
883 }
884 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000885 else if( strcmp( p, "min_version" ) == 0 )
886 {
887 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000889 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100891 else if( strcmp( q, "tls1_1" ) == 0 ||
892 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100894 else if( strcmp( q, "tls1_2" ) == 0 ||
895 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000897 else
898 goto usage;
899 }
900 else if( strcmp( p, "max_version" ) == 0 )
901 {
902 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000904 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100906 else if( strcmp( q, "tls1_1" ) == 0 ||
907 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100909 else if( strcmp( q, "tls1_2" ) == 0 ||
910 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000912 else
913 goto usage;
914 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100915 else if( strcmp( p, "arc4" ) == 0 )
916 {
917 switch( atoi( q ) )
918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
920 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100921 default: goto usage;
922 }
923 }
Gilles Peskinebc70a182017-05-09 15:59:24 +0200924 else if( strcmp( p, "allow_sha1" ) == 0 )
925 {
926 switch( atoi( q ) )
927 {
928 case 0: opt.allow_sha1 = 0; break;
929 case 1: opt.allow_sha1 = 1; break;
930 default: goto usage;
931 }
932 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000933 else if( strcmp( p, "force_version" ) == 0 )
934 {
935 if( strcmp( q, "ssl3" ) == 0 )
936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
938 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000939 }
940 else if( strcmp( q, "tls1" ) == 0 )
941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
943 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000944 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100945 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
948 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000949 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100950 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
953 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000954 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100955 else if( strcmp( q, "dtls1" ) == 0 )
956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
958 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
959 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100960 }
961 else if( strcmp( q, "dtls1_2" ) == 0 )
962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
964 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
965 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100966 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000967 else
968 goto usage;
969 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100970 else if( strcmp( p, "auth_mode" ) == 0 )
971 {
972 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100974 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100976 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100978 else
979 goto usage;
980 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200981 else if( strcmp( p, "max_frag_len" ) == 0 )
982 {
983 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200985 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200987 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200989 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200991 else
992 goto usage;
993 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200994 else if( strcmp( p, "trunc_hmac" ) == 0 )
995 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100996 switch( atoi( q ) )
997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
999 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001000 default: goto usage;
1001 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001002 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001003 else if( strcmp( p, "hs_timeout" ) == 0 )
1004 {
1005 if( ( p = strchr( q, '-' ) ) == NULL )
1006 goto usage;
1007 *p++ = '\0';
1008 opt.hs_to_min = atoi( q );
1009 opt.hs_to_max = atoi( p );
1010 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1011 goto usage;
1012 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001013 else if( strcmp( p, "mtu" ) == 0 )
1014 {
1015 opt.dtls_mtu = atoi( q );
1016 if( opt.dtls_mtu < 0 )
1017 goto usage;
1018 }
Hanno Becker4d615912018-08-14 13:33:30 +01001019 else if( strcmp( p, "dgram_packing" ) == 0 )
1020 {
1021 opt.dgram_packing = atoi( q );
1022 if( opt.dgram_packing != 0 &&
1023 opt.dgram_packing != 1 )
1024 {
1025 goto usage;
1026 }
1027 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001028 else if( strcmp( p, "recsplit" ) == 0 )
1029 {
1030 opt.recsplit = atoi( q );
1031 if( opt.recsplit < 0 || opt.recsplit > 1 )
1032 goto usage;
1033 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001034 else if( strcmp( p, "dhmlen" ) == 0 )
1035 {
1036 opt.dhmlen = atoi( q );
1037 if( opt.dhmlen < 0 )
1038 goto usage;
1039 }
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +01001040 else if( strcmp( p, "query_config" ) == 0 )
1041 {
1042 return query_config( q );
1043 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001044 else
1045 goto usage;
1046 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001047
Hanno Becker16970d22017-10-10 15:56:37 +01001048 /* Event-driven IO is incompatible with the above custom
1049 * receive and send functions, as the polling builds on
1050 * refers to the underlying net_context. */
1051 if( opt.event == 1 && opt.nbio != 1 )
1052 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001053 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001054 opt.nbio = 1;
1055 }
1056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057#if defined(MBEDTLS_DEBUG_C)
1058 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001059#endif
1060
Paul Bakkerc1516be2013-06-29 16:01:32 +02001061 if( opt.force_ciphersuite[0] > 0 )
1062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001064 ciphersuite_info =
1065 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001066
Paul Bakker66c48102013-07-26 14:05:32 +02001067 if( opt.max_version != -1 &&
1068 ciphersuite_info->min_minor_ver > opt.max_version )
1069 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001070 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker66c48102013-07-26 14:05:32 +02001071 ret = 2;
1072 goto usage;
1073 }
1074 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001075 ciphersuite_info->max_minor_ver < opt.min_version )
1076 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001077 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001078 ret = 2;
1079 goto usage;
1080 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001081
1082 /* If the server selects a version that's not supported by
1083 * this suite, then there will be no common ciphersuite... */
1084 if( opt.max_version == -1 ||
1085 opt.max_version > ciphersuite_info->max_minor_ver )
1086 {
Paul Bakker66c48102013-07-26 14:05:32 +02001087 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001088 }
Paul Bakker66c48102013-07-26 14:05:32 +02001089 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001090 {
Paul Bakker66c48102013-07-26 14:05:32 +02001091 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001092 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1094 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1095 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001096 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001097
1098 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001102 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001103 mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001104 ret = 2;
1105 goto usage;
1106 }
1107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001109 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001110 }
1111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001113 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001114 * Unhexify the pre-shared key if any is given
1115 */
1116 if( strlen( opt.psk ) )
1117 {
Hanno Beckerec370302019-04-09 17:12:56 +01001118 psk_len = strlen( opt.psk ) / 2;
1119 if( psk_len > sizeof( psk ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001120 {
Hanno Beckerec370302019-04-09 17:12:56 +01001121 mbedtls_printf( "pre-shared key too long\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001122 goto exit;
1123 }
1124
Hanno Beckerec370302019-04-09 17:12:56 +01001125 if( unhexify( opt.psk, psk ) != 0 )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001126 {
Hanno Beckerec370302019-04-09 17:12:56 +01001127 mbedtls_printf( "pre-shared key not valid hex\n" );
1128 goto exit;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001129 }
1130 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001132
Hanno Beckere6706e62017-05-15 16:05:15 +01001133#if defined(MBEDTLS_ECP_C)
1134 if( opt.curves != NULL )
1135 {
1136 p = (char *) opt.curves;
1137 i = 0;
1138
1139 if( strcmp( p, "none" ) == 0 )
1140 {
1141 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1142 }
1143 else if( strcmp( p, "default" ) != 0 )
1144 {
1145 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001146 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001147 {
1148 q = p;
1149
1150 /* Terminate the current string */
1151 while( *p != ',' && *p != '\0' )
1152 p++;
1153 if( *p == ',' )
1154 *p++ = '\0';
1155
1156 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1157 {
1158 curve_list[i++] = curve_cur->grp_id;
1159 }
1160 else
1161 {
1162 mbedtls_printf( "unknown curve %s\n", q );
1163 mbedtls_printf( "supported curves: " );
1164 for( curve_cur = mbedtls_ecp_curve_list();
1165 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1166 curve_cur++ )
1167 {
1168 mbedtls_printf( "%s ", curve_cur->name );
1169 }
1170 mbedtls_printf( "\n" );
1171 goto exit;
1172 }
1173 }
1174
1175 mbedtls_printf("Number of curves: %d\n", i );
1176
Hanno Becker8651a432017-06-09 16:13:22 +01001177 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001178 {
Hanno Becker8651a432017-06-09 16:13:22 +01001179 mbedtls_printf( "curves list too long, maximum %d",
1180 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001181 goto exit;
1182 }
1183
1184 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1185 }
1186 }
1187#endif /* MBEDTLS_ECP_C */
1188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001190 if( opt.alpn_string != NULL )
1191 {
1192 p = (char *) opt.alpn_string;
1193 i = 0;
1194
1195 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001196 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001197 {
1198 alpn_list[i++] = p;
1199
1200 /* Terminate the current string and move on to next one */
1201 while( *p != ',' && *p != '\0' )
1202 p++;
1203 if( *p == ',' )
1204 *p++ = '\0';
1205 }
1206 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001208
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001209 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 * 0. Initialize the RNG and the session data
1211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001213 fflush( stdout );
1214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 mbedtls_entropy_init( &entropy );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001216 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1217 &entropy, (const unsigned char *) pers,
1218 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +00001219 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001220 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
1221 -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001222 goto exit;
1223 }
1224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001228 /*
1229 * 1.1. Load the trusted CA
1230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 fflush( stdout );
1233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001235 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001236 if( strcmp( opt.ca_path, "none" ) == 0 )
1237 ret = 0;
1238 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001240 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001241 if( strcmp( opt.ca_file, "none" ) == 0 )
1242 ret = 0;
1243 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001245 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001246#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247#if defined(MBEDTLS_CERTS_C)
1248 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 ret = mbedtls_x509_crt_parse( &cacert,
1251 (const unsigned char *) mbedtls_test_cas[i],
1252 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001253 if( ret != 0 )
1254 break;
1255 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001256#else
1257 {
1258 ret = 1;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001259 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001260 }
1261#endif
Paul Bakker42488232012-05-16 08:21:05 +00001262 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001263 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001264 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1265 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 goto exit;
1267 }
1268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001270
1271 /*
1272 * 1.2. Load own certificate and private key
1273 *
1274 * (can be skipped if client authentication is not required)
1275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 fflush( stdout );
1278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001280 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001281 if( strcmp( opt.crt_file, "none" ) == 0 )
1282 ret = 0;
1283 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001285 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001286#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001288 ret = mbedtls_x509_crt_parse( &clicert,
1289 (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001291#else
1292 {
1293 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001295 }
1296#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 if( ret != 0 )
1298 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001299 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1300 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001301 goto exit;
1302 }
1303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001305 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001306 if( strcmp( opt.key_file, "none" ) == 0 )
1307 ret = 0;
1308 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001310 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001311#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001313 ret = mbedtls_pk_parse_key( &pkey,
1314 (const unsigned char *) mbedtls_test_cli_key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001316#else
1317 {
1318 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001320 }
1321#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 if( ret != 0 )
1323 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001324 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
1325 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 goto exit;
1327 }
1328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 mbedtls_printf( " ok\n" );
1330#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001331
1332 /*
1333 * 2. Start the connection
1334 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001335 if( opt.server_addr == NULL)
1336 opt.server_addr = opt.server_name;
1337
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001338 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001340 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001341 fflush( stdout );
1342
Hanno Becker16970d22017-10-10 15:56:37 +01001343 if( ( ret = mbedtls_net_connect( &server_fd,
1344 opt.server_addr, opt.server_port,
1345 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1346 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001348 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
1349 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 goto exit;
1351 }
1352
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001353 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001354 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001355 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001356 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001357 if( ret != 0 )
1358 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001359 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
1360 -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001361 goto exit;
1362 }
1363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001365
1366 /*
1367 * 3. Setup stuff
1368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 fflush( stdout );
1371
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001372 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1373 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001374 opt.transport,
1375 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001376 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001377 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
1378 -ret );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001379 goto exit;
1380 }
1381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001383 /* The default algorithms profile disables SHA-1, but our tests still
1384 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001385 if( opt.allow_sha1 > 0 )
1386 {
1387 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1388 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1389 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1390 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001391
Paul Bakker915275b2012-09-28 07:10:55 +00001392 if( opt.debug_level > 0 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001393 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
Gilles Peskineef86ab22017-05-05 18:59:02 +02001394#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001395
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001396 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001397 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001400 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Hanno Becker16970d22017-10-10 15:56:37 +01001401 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min,
1402 opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001403
Hanno Becker4d615912018-08-14 13:33:30 +01001404 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01001405 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001409 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001410 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001411 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n",
1412 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001413 goto exit;
1414 }
Paul Bakker05decb22013-08-15 13:33:48 +02001415#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001418 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001419 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001420#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001423 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001424 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001425#endif
1426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001428 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001429 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001430#endif
1431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001433 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001434 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Hanno Becker16970d22017-10-10 15:56:37 +01001435 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1436 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001437#endif
1438
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001439#if defined(MBEDTLS_DHM_C)
1440 if( opt.dhmlen != DFL_DHMLEN )
1441 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1442#endif
1443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001445 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001446 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001447 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001448 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
1449 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001450 goto exit;
1451 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001452#endif
1453
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001454 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1455 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001456
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001457 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02001460 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02001461#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001462
Paul Bakker645ce3a2012-10-31 12:32:41 +00001463 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001464 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001465
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001466#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001467 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001468 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001469#endif
Paul Bakker51936882011-02-20 16:05:58 +00001470
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001471 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001472 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001474 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001475#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001478 if( strcmp( opt.ca_path, "none" ) != 0 &&
1479 strcmp( opt.ca_file, "none" ) != 0 )
1480 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001481 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001482 }
1483 if( strcmp( opt.crt_file, "none" ) != 0 &&
1484 strcmp( opt.key_file, "none" ) != 0 )
1485 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001486 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001487 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001488 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
1489 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001490 goto exit;
1491 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001492 }
Paul Bakkered27a042013-04-18 22:46:23 +02001493#endif
1494
Hanno Beckere6706e62017-05-15 16:05:15 +01001495#if defined(MBEDTLS_ECP_C)
1496 if( opt.curves != NULL &&
1497 strcmp( opt.curves, "default" ) != 0 )
1498 {
1499 mbedtls_ssl_conf_curves( &conf, curve_list );
1500 }
1501#endif
1502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001504 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001505 (const unsigned char *) opt.psk_identity,
1506 strlen( opt.psk_identity ) ) ) != 0 )
1507 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001508 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n",
1509 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001510 goto exit;
1511 }
Paul Bakkered27a042013-04-18 22:46:23 +02001512#endif
1513
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001514 if( opt.min_version != DFL_MIN_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01001515 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1516 opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001517
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001518 if( opt.max_version != DFL_MAX_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01001519 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1520 opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001523 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001524 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001525#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001526
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001527 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
1528 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001529 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
1530 -ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001531 goto exit;
1532 }
1533
1534#if defined(MBEDTLS_X509_CRT_PARSE_C)
1535 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1536 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001537 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
1538 ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001539 goto exit;
1540 }
1541#endif
1542
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001543#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1544 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
1545 {
1546 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
1547 (const unsigned char *) opt.ecjpake_pw,
1548 strlen( opt.ecjpake_pw ) ) ) != 0 )
1549 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001550 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
1551 ret );
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001552 goto exit;
1553 }
1554 }
1555#endif
1556
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001557 if( opt.nbio == 2 )
1558 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
1559 else
Hanno Becker16970d22017-10-10 15:56:37 +01001560 mbedtls_ssl_set_bio( &ssl, &server_fd,
1561 mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02001562 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001563
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001564#if defined(MBEDTLS_SSL_PROTO_DTLS)
1565 if( opt.dtls_mtu != DFL_DTLS_MTU )
1566 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
1567#endif
1568
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001569#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001570 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
1571 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001572#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001573
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001574#if defined(MBEDTLS_ECP_RESTARTABLE)
1575 if( opt.ec_max_ops != DFL_EC_MAX_OPS )
1576 mbedtls_ecp_set_max_ops( opt.ec_max_ops );
1577#endif
1578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001580
Paul Bakker5121ce52009-01-03 21:22:43 +00001581 /*
1582 * 4. Handshake
1583 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001585 fflush( stdout );
1586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 {
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001589 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1590 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001591 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Paul Bakker40e46942009-01-03 21:51:57 +00001592 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001593 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
1594 -ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
1596 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001597 " Unable to verify the server's certificate. "
1598 "Either it is invalid,\n"
1599 " or you didn't set ca_file or ca_path "
1600 "to an appropriate value.\n"
1601 " Alternatively, you may want to use "
1602 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001604 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00001605 }
Hanno Becker16970d22017-10-10 15:56:37 +01001606
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001607#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02001608 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
1609 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001610#endif
1611
Hanno Becker16970d22017-10-10 15:56:37 +01001612 /* For event-driven IO, wait for socket to become available */
1613 if( opt.event == 1 /* level triggered IO */ )
1614 {
1615#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001616 ret = idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001617#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001618 ret = idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001619#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001620 if( ret != 0 )
1621 goto exit;
Hanno Becker16970d22017-10-10 15:56:37 +01001622 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001623 }
1624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Hanno Becker16970d22017-10-10 15:56:37 +01001626 mbedtls_ssl_get_version( &ssl ),
1627 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
1630 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001631 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001633
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02001634#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1635 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
1636 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
1637#endif
1638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001640 if( opt.alpn_string != NULL )
1641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
1643 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001644 alp ? alp : "(none)" );
1645 }
1646#endif
1647
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001648 if( opt.reconnect != 0 )
1649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001651 fflush( stdout );
1652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001654 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001655 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
1656 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001657 goto exit;
1658 }
1659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001661 }
1662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001664 /*
1665 * 5. Verify the server certificate
1666 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001668
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001669 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001670 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001671 char vrfy_buf[512];
1672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001674
Hanno Becker16970d22017-10-10 15:56:37 +01001675 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ),
1676 " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001678 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001679 }
1680 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 mbedtls_printf( " . Peer certificate information ...\n" );
1686 mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1687 mbedtls_ssl_get_peer_cert( &ssl ) );
1688 mbedtls_printf( "%s\n", buf );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001689 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001693 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001694 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001695 /*
1696 * Perform renegotiation (this must be done when the server is waiting
1697 * for input from our side).
1698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001700 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001702 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001703 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001704 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001705 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001706 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001707 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
1708 ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001709 goto exit;
1710 }
Hanno Becker16970d22017-10-10 15:56:37 +01001711
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02001712#if defined(MBEDTLS_ECP_RESTARTABLE)
1713 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
1714 continue;
1715#endif
1716
Hanno Becker16970d22017-10-10 15:56:37 +01001717 /* For event-driven IO, wait for socket to become available */
1718 if( opt.event == 1 /* level triggered IO */ )
1719 {
1720#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001721 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001722#else
Hanno Becker197a91c2017-10-31 10:58:53 +00001723 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001724#endif
1725 }
1726
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001727 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001729 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001731
Paul Bakker5121ce52009-01-03 21:22:43 +00001732 /*
1733 * 6. Write the GET request
1734 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001735 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001736send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001738 fflush( stdout );
1739
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001740 len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST,
1741 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001742 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001743
1744 /* Add padding to GET request to reach opt.request_size in length */
1745 if( opt.request_size != DFL_REQUEST_SIZE &&
1746 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001747 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001748 memset( buf + len, 'A', opt.request_size - len - tail_len );
1749 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001750 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001751
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001752 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001753 len += tail_len;
1754
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001755 /* Truncate if request size is smaller than the "natural" size */
1756 if( opt.request_size != DFL_REQUEST_SIZE &&
1757 len > opt.request_size )
1758 {
1759 len = opt.request_size;
1760
1761 /* Still end with \r\n unless that's really not possible */
1762 if( len >= 2 ) buf[len - 2] = '\r';
1763 if( len >= 1 ) buf[len - 1] = '\n';
1764 }
1765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001767 {
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001768 written = 0;
1769 frags = 0;
1770
1771 do
Paul Bakker5121ce52009-01-03 21:22:43 +00001772 {
Hanno Becker16970d22017-10-10 15:56:37 +01001773 while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001774 len - written ) ) < 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001775 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001776 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001777 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001778 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001779 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001780 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
1781 -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001782 goto exit;
1783 }
Hanno Becker16970d22017-10-10 15:56:37 +01001784
1785 /* For event-driven IO, wait for socket to become available */
1786 if( opt.event == 1 /* level triggered IO */ )
1787 {
1788#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001789 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001790#else
Hanno Becker197a91c2017-10-31 10:58:53 +00001791 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001792#endif
1793 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001794 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001795
1796 frags++;
1797 written += ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001798 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001799 while( written < len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001800 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001801 else /* Not stream, so datagram */
1802 {
Hanno Becker16970d22017-10-10 15:56:37 +01001803 while( 1 )
1804 {
1805 ret = mbedtls_ssl_write( &ssl, buf, len );
1806
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001807#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001808 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001809 continue;
1810#endif
1811
Hanno Becker16970d22017-10-10 15:56:37 +01001812 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1813 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1814 break;
1815
1816 /* For event-driven IO, wait for socket to become available */
1817 if( opt.event == 1 /* level triggered IO */ )
1818 {
1819#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001820 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001821#else
Hanno Becker197a91c2017-10-31 10:58:53 +00001822 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001823#endif
1824 }
1825 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001826
1827 if( ret < 0 )
1828 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001829 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n",
1830 ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001831 goto exit;
1832 }
1833
1834 frags = 1;
1835 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001836
1837 if( written < len )
1838 {
1839 mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
1840 "was truncated to size %u", (unsigned) written );
1841 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001842 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001843
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001844 buf[written] = '\0';
Hanno Becker16970d22017-10-10 15:56:37 +01001845 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
1846 written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001847
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001848 /* Send a non-empty request if request_size == 0 */
1849 if ( len == 0 )
1850 {
1851 opt.request_size = DFL_REQUEST_SIZE;
1852 goto send_request;
1853 }
1854
Paul Bakker5121ce52009-01-03 21:22:43 +00001855 /*
1856 * 7. Read the HTTP response
1857 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001859 fflush( stdout );
1860
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001861 /*
1862 * TLS and DTLS need different reading styles (stream vs datagram)
1863 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001865 {
1866 do
1867 {
1868 len = sizeof( buf ) - 1;
1869 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001871
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001872#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001873 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001874 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001875#endif
1876
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001877 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1878 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Hanno Becker16970d22017-10-10 15:56:37 +01001879 {
1880 /* For event-driven IO, wait for socket to become available */
1881 if( opt.event == 1 /* level triggered IO */ )
1882 {
1883#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001884 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001885#else
Hanno Becker197a91c2017-10-31 10:58:53 +00001886 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001887#endif
1888 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001889 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01001890 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001891
1892 if( ret <= 0 )
1893 {
1894 switch( ret )
1895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1897 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001898 ret = 0;
1899 goto close_notify;
1900
1901 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 case MBEDTLS_ERR_NET_CONN_RESET:
1903 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001904 ret = 0;
1905 goto reconnect;
1906
1907 default:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001908 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n",
1909 -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001910 goto exit;
1911 }
1912 }
1913
1914 len = ret;
1915 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001917
1918 /* End of message should be detected according to the syntax of the
1919 * application protocol (eg HTTP), just use a dummy test here. */
1920 if( ret > 0 && buf[len-1] == '\n' )
1921 {
1922 ret = 0;
1923 break;
1924 }
1925 }
1926 while( 1 );
1927 }
1928 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 {
1930 len = sizeof( buf ) - 1;
1931 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001932
Hanno Becker16970d22017-10-10 15:56:37 +01001933 while( 1 )
1934 {
1935 ret = mbedtls_ssl_read( &ssl, buf, len );
1936
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001937#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001938 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001939 continue;
1940#endif
1941
Hanno Becker16970d22017-10-10 15:56:37 +01001942 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1943 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1944 break;
1945
1946 /* For event-driven IO, wait for socket to become available */
1947 if( opt.event == 1 /* level triggered IO */ )
1948 {
1949#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001950 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001951#else
Hanno Becker197a91c2017-10-31 10:58:53 +00001952 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001953#endif
1954 }
1955 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001956
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001957 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001959 switch( ret )
1960 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001961 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001963 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001964 goto send_request;
1965 goto exit;
1966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1968 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001969 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001970 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00001971
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001972 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001974 goto exit;
1975 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001976 }
1977
Paul Bakker5121ce52009-01-03 21:22:43 +00001978 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02001979 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001981 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001982 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001983
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001984 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001985 * 7b. Simulate hard reset and reconnect from same port?
1986 */
1987 if( opt.reconnect_hard != 0 )
1988 {
1989 opt.reconnect_hard = 0;
1990
1991 mbedtls_printf( " . Restarting connection from same port..." );
1992 fflush( stdout );
1993
1994 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
1995 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001996 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
1997 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001998 goto exit;
1999 }
2000
2001 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
2002 {
2003 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002004 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002005 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002006 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002007 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2008 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002009 goto exit;
2010 }
Hanno Becker16970d22017-10-10 15:56:37 +01002011
2012 /* For event-driven IO, wait for socket to become available */
2013 if( opt.event == 1 /* level triggered IO */ )
2014 {
2015#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002016 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002017#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002018 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002019#endif
2020 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002021 }
2022
2023 mbedtls_printf( " ok\n" );
2024
2025 goto send_request;
2026 }
2027
2028 /*
2029 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002030 */
2031 if( --opt.exchanges > 0 )
2032 goto send_request;
2033
2034 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002035 * 8. Done, cleanly close the connection
2036 */
2037close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002039 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002040
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002041 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002043 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002044 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002047
2048 /*
2049 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002050 */
2051reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002052 if( opt.reconnect != 0 )
2053 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02002054 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002055
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002056 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002057
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002058#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002059 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02002060 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002061#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002066 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002067 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2068 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002069 goto exit;
2070 }
2071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002073 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002074 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n",
2075 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002076 goto exit;
2077 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002078
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002079 if( ( ret = mbedtls_net_connect( &server_fd,
2080 opt.server_addr, opt.server_port,
2081 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2082 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002083 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002084 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
2085 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002086 goto exit;
2087 }
2088
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002089 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002090 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002091 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002092 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002093 if( ret != 0 )
2094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002096 -ret );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002097 goto exit;
2098 }
2099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002101 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002102 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002103 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002104 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002105 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002106 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2107 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002108 goto exit;
2109 }
2110 }
2111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002113
2114 goto send_request;
2115 }
2116
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002117 /*
2118 * Cleanup and exit
2119 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002120exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00002122 if( ret != 0 )
2123 {
2124 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 mbedtls_strerror( ret, error_buf, 100 );
2126 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002127 }
2128#endif
2129
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002130 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132#if defined(MBEDTLS_X509_CRT_PARSE_C)
2133 mbedtls_x509_crt_free( &clicert );
2134 mbedtls_x509_crt_free( &cacert );
2135 mbedtls_pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02002136#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 mbedtls_ssl_session_free( &saved_session );
2138 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002139 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 mbedtls_ctr_drbg_free( &ctr_drbg );
2141 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002142
Paul Bakkercce9d772011-11-18 14:26:47 +00002143#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 fflush( stdout ); getchar();
2146#endif
2147
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002148 // Shell can not handle large exit numbers -> 1 for errors
2149 if( ret < 0 )
2150 ret = 1;
2151
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 return( ret );
2153}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
2155 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002156 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */