blob: e723273152627a8c127337d0e16fc4b53f08abf4 [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
Rich Evansf90016a2015-01-19 14:26:37 +000038#endif
39
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020040#if !defined(MBEDTLS_ENTROPY_C) || \
41 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020042 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020043int main( void )
44{
45 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
46 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020047 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020048 return( 0 );
49}
50#else
51
Andres AG788aa4a2016-09-14 14:32:09 +010052#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/ssl.h"
54#include "mbedtls/entropy.h"
55#include "mbedtls/ctr_drbg.h"
56#include "mbedtls/certs.h"
57#include "mbedtls/x509.h"
58#include "mbedtls/error.h"
59#include "mbedtls/debug.h"
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020060#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Rich Evans18b78c72015-02-11 14:06:19 +000062#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010065
Hanno Beckere4ad3e82017-09-18 15:05:46 +010066#define MAX_REQUEST_SIZE 20000
67#define MAX_REQUEST_SIZE_STR "20000"
68
Paul Bakker9caf2d22010-02-18 19:37:19 +000069#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010070#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020071#define DFL_SERVER_PORT "4433"
Paul Bakker9caf2d22010-02-18 19:37:19 +000072#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +020073#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +000074#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010075#define DFL_NBIO 0
Hanno Becker16970d22017-10-10 15:56:37 +010076#define DFL_EVENT 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020077#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +020078#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +000079#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000080#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000081#define DFL_CRT_FILE ""
82#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020083#define DFL_PSK ""
84#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +020085#define DFL_ECJPAKE_PW NULL
Paul Bakker51936882011-02-20 16:05:58 +000086#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010088#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010089#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +020090#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +020091#define DFL_MIN_VERSION -1
Paul Bakker1d29fb52012-09-28 13:28:45 +000092#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +000093#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +020094#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +010095#define DFL_AUTH_MODE -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +010097#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +010098#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +020099#define DFL_DHMLEN -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200100#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100101#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200102#define DFL_RECONNECT_HARD 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200104#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100105#define DFL_CURVES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200107#define DFL_HS_TO_MIN 0
108#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200109#define DFL_DTLS_MTU -1
Hanno Becker4d615912018-08-14 13:33:30 +0100110#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200111#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200112#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100113#define DFL_ETM -1
Paul Bakker0e6975b2009-02-10 22:19:10 +0000114
Paul Bakker93c32b22014-04-25 13:40:05 +0200115#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
116#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_X509_CRT_PARSE_C)
119#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000120#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100121 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
122 " default: \"\" (pre-loaded)\n" \
123 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
124 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
125 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
126 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000127 " key_file=%%s default: \"\" (pre-loaded)\n"
128#else
129#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 " No file operations available (MBEDTLS_FS_IO not defined)\n"
131#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200132#else
133#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200137#define USAGE_PSK \
138 " psk=%%s default: \"\" (in hex, without 0x)\n" \
139 " psk_identity=%%s default: \"Client_identity\"\n"
140#else
141#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200145#define USAGE_TICKETS \
146 " tickets=%%d default: 1 (enabled)\n"
147#else
148#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200152#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100153 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200154#else
155#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200159#define USAGE_MAX_FRAG_LEN \
160 " max_frag_len=%%d default: 16384 (tls default)\n" \
161 " options: 512, 1024, 2048, 4096\n"
162#else
163#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100167#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200168 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100169#else
170#define USAGE_RECSPLIT
171#endif
172
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200173#if defined(MBEDTLS_DHM_C)
174#define USAGE_DHMLEN \
175 " dhmlen=%%d default: (library default: 1024 bits)\n"
176#else
177#define USAGE_DHMLEN
178#endif
179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200181#define USAGE_ALPN \
182 " alpn=%%s default: \"\" (disabled)\n" \
183 " example: spdy/1,http/1.1\n"
184#else
185#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200187
Hanno Beckere6706e62017-05-15 16:05:15 +0100188#if defined(MBEDTLS_ECP_C)
189#define USAGE_CURVES \
190 " curves=a,b,c,d default: \"default\" (library default)\n" \
191 " example: \"secp521r1,brainpoolP512r1\"\n" \
192 " - use \"none\" for empty list\n" \
193 " - see mbedtls_ecp_curve_list()\n" \
194 " for acceptable curve names\n"
195#else
196#define USAGE_CURVES ""
197#endif
198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200200#define USAGE_DTLS \
201 " dtls=%%d default: 0 (TLS)\n" \
202 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200203 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Becker4d615912018-08-14 13:33:30 +0100204 " mtu=%%d default: (library default: unlimited)\n" \
205 " dgram_packing=%%d default: 1 (allowed)\n" \
206 " allow or forbid packing of multiple\n" \
207 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200208#else
209#define USAGE_DTLS ""
210#endif
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200213#define USAGE_FALLBACK \
214 " fallback=0/1 default: (library default: off)\n"
215#else
216#define USAGE_FALLBACK ""
217#endif
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200220#define USAGE_EMS \
221 " extended_ms=0/1 default: (library default: on)\n"
222#else
223#define USAGE_EMS ""
224#endif
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100227#define USAGE_ETM \
228 " etm=0/1 default: (library default: on)\n"
229#else
230#define USAGE_ETM ""
231#endif
232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100234#define USAGE_RENEGO \
235 " renegotiation=%%d default: 0 (disabled)\n" \
236 " renegotiate=%%d default: 0 (disabled)\n"
237#else
238#define USAGE_RENEGO ""
239#endif
240
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200241#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
242#define USAGE_ECJPAKE \
243 " ecjpake_pw=%%s default: none (disabled)\n"
244#else
245#define USAGE_ECJPAKE ""
246#endif
247
Paul Bakker9caf2d22010-02-18 19:37:19 +0000248#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000249 "\n usage: ssl_client2 param=<>...\n" \
250 "\n acceptable parameters:\n" \
251 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100252 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000253 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000254 " request_page=%%s default: \".\"\n" \
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +0100255 " request_size=%%d default: about 34 (basic request)\n" \
256 " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
257 " If 0, in the first exchange only an empty\n" \
258 " application data message is sent followed by\n" \
259 " a second non-empty message before attempting\n" \
260 " to read a response from the server\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100261 " debug_level=%%d default: 0 (disabled)\n" \
262 " nbio=%%d default: 0 (blocking I/O)\n" \
263 " options: 1 (non-blocking), 2 (added delays)\n" \
264 " event=%%d default: 0 (loop)\n" \
265 " options: 1 (level-triggered, implies nbio=1),\n" \
266 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200267 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100268 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200269 USAGE_DTLS \
270 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100271 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100272 " options: none, optional, required\n" \
273 USAGE_IO \
274 "\n" \
275 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200276 USAGE_ECJPAKE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100277 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100278 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100279 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200280 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200281 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200282 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200283 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200284 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100285 USAGE_MAX_FRAG_LEN \
286 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200287 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200288 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200289 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100290 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100291 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100292 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200293 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000294 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100295 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200296 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200297 " min_version=%%s default: (library default: tls1)\n" \
298 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000299 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100300 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000301 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000302 " force_ciphersuite=<name> default: all enabled\n"\
303 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000304
Hanno Becker8651a432017-06-09 16:13:22 +0100305#define ALPN_LIST_SIZE 10
306#define CURVE_LIST_SIZE 20
307
Rich Evans85b05ec2015-02-12 11:37:29 +0000308/*
309 * global options
310 */
311struct options
312{
313 const char *server_name; /* hostname of the server (client only) */
314 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200315 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000316 int debug_level; /* level of debugging */
317 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100318 int event; /* loop or event-driven IO? level or edge triggered? */
319 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000320 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000321 const char *request_page; /* page on server to request */
322 int request_size; /* pad request with header to requested size */
323 const char *ca_file; /* the file with the CA certificate(s) */
324 const char *ca_path; /* the path with the CA certificate(s) reside */
325 const char *crt_file; /* the file with the client certificate */
326 const char *key_file; /* the file with the client key */
327 const char *psk; /* the pre-shared key */
328 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200329 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000330 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
331 int renegotiation; /* enable / disable renegotiation */
332 int allow_legacy; /* allow legacy renegotiation */
333 int renegotiate; /* attempt renegotiation? */
334 int renego_delay; /* delay before enforcing renegotiation */
335 int exchanges; /* number of data exchanges */
336 int min_version; /* minimum protocol version accepted */
337 int max_version; /* maximum protocol version accepted */
338 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200339 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000340 int auth_mode; /* verify mode for connection */
341 unsigned char mfl_code; /* code for maximum fragment length */
342 int trunc_hmac; /* negotiate truncated hmac or not */
343 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200344 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000345 int reconnect; /* attempt to resume session */
346 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200347 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000348 int tickets; /* enable / disable session tickets */
Hanno Beckere6706e62017-05-15 16:05:15 +0100349 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000350 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000351 int transport; /* TLS or DTLS? */
352 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
353 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200354 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Rich Evans85b05ec2015-02-12 11:37:29 +0000355 int fallback; /* is this a fallback connection? */
Hanno Becker4d615912018-08-14 13:33:30 +0100356 int dgram_packing; /* allow/forbid datagram packing */
Rich Evans85b05ec2015-02-12 11:37:29 +0000357 int extended_ms; /* negotiate extended master secret? */
358 int etm; /* negotiate encrypt then mac? */
359} opt;
360
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200361static void my_debug( void *ctx, int level,
362 const char *file, int line,
363 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000364{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200365 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000366
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200367 /* Extract basename from file */
368 for( p = basename = file; *p != '\0'; p++ )
369 if( *p == '/' || *p == '\\' )
370 basename = p + 1;
371
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100372 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s",
373 basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000374 fflush( (FILE *) ctx );
375}
376
377/*
378 * Test recv/send functions that make sure each try returns
379 * WANT_READ/WANT_WRITE at least once before sucesseding
380 */
381static int my_recv( void *ctx, unsigned char *buf, size_t len )
382{
383 static int first_try = 1;
384 int ret;
385
386 if( first_try )
387 {
388 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100389 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000390 }
391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100393 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000394 first_try = 1; /* Next call will be a new operation */
395 return( ret );
396}
397
398static int my_send( void *ctx, const unsigned char *buf, size_t len )
399{
400 static int first_try = 1;
401 int ret;
402
403 if( first_try )
404 {
405 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100406 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000407 }
408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100410 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000411 first_try = 1; /* Next call will be a new operation */
412 return( ret );
413}
414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415#if defined(MBEDTLS_X509_CRT_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000416/*
417 * Enabled if debug_level > 1 in code below
418 */
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100419static int my_verify( void *data, mbedtls_x509_crt *crt,
420 int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000421{
422 char buf[1024];
423 ((void) data);
424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
426 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
427 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000428
Rich Evans85b05ec2015-02-12 11:37:29 +0000429 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100431 else
432 {
433 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
434 mbedtls_printf( "%s\n", buf );
435 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000436
437 return( 0 );
438}
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200439
440static int ssl_sig_hashes_for_test[] = {
441#if defined(MBEDTLS_SHA512_C)
442 MBEDTLS_MD_SHA512,
443 MBEDTLS_MD_SHA384,
444#endif
445#if defined(MBEDTLS_SHA256_C)
446 MBEDTLS_MD_SHA256,
447 MBEDTLS_MD_SHA224,
448#endif
449#if defined(MBEDTLS_SHA1_C)
450 /* Allow SHA-1 as we use it extensively in tests. */
451 MBEDTLS_MD_SHA1,
452#endif
453 MBEDTLS_MD_NONE
454};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000456
Hanno Becker16970d22017-10-10 15:56:37 +0100457/*
458 * Wait for an event from the underlying transport or the timer
459 * (Used in event-driven IO mode).
460 */
461#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000462int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000463 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100464#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000465int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000466 mbedtls_timing_delay_context *timer,
467 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100468#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000469{
Hanno Becker16970d22017-10-10 15:56:37 +0100470
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000471 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +0100472 int poll_type = 0;
473
474 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
475 poll_type = MBEDTLS_NET_POLL_WRITE;
476 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
477 poll_type = MBEDTLS_NET_POLL_READ;
478#if !defined(MBEDTLS_TIMING_C)
479 else
Hanno Beckeref527962018-03-15 15:49:24 +0000480 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100481#endif
482
483 while( 1 )
484 {
Hanno Becker197a91c2017-10-31 10:58:53 +0000485 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +0100486#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +0000487 if( timer != NULL &&
488 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +0100489 {
Hanno Becker16970d22017-10-10 15:56:37 +0100490 break;
491 }
Hanno Becker197a91c2017-10-31 10:58:53 +0000492#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +0100493
Hanno Becker197a91c2017-10-31 10:58:53 +0000494 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000495 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +0100496 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000497 ret = mbedtls_net_poll( fd, poll_type, 0 );
498 if( ret < 0 )
499 return( ret );
500 if( ret == poll_type )
501 break;
Hanno Becker16970d22017-10-10 15:56:37 +0100502 }
503 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000504
505 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100506}
507
Paul Bakker9caf2d22010-02-18 19:37:19 +0000508int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000509{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200510 int ret = 0, len, tail_len, i, written, frags, retry_left;
511 mbedtls_net_context server_fd;
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100512
513 unsigned char buf[MAX_REQUEST_SIZE + 1];
514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
516 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200517 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200518#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100520 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200521#endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100522#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +0100523 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100524 const mbedtls_ecp_curve_info *curve_cur;
525#endif
526
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200527 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000528
Gilles Peskineef86ab22017-05-05 18:59:02 +0200529#if defined(MBEDTLS_X509_CRT_PARSE_C)
530 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
531#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_entropy_context entropy;
533 mbedtls_ctr_drbg_context ctr_drbg;
534 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200535 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200537#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200538 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200539#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200541 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_x509_crt cacert;
543 mbedtls_x509_crt clicert;
544 mbedtls_pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200545#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000546 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000547 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000548
Paul Bakker1a207ec2011-02-06 13:22:40 +0000549 /*
550 * Make sure memory references are valid.
551 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200552 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200553 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200554 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200556 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557#if defined(MBEDTLS_X509_CRT_PARSE_C)
558 mbedtls_x509_crt_init( &cacert );
559 mbedtls_x509_crt_init( &clicert );
560 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200561#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200563 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200564#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000565
Paul Bakker9caf2d22010-02-18 19:37:19 +0000566 if( argc == 0 )
567 {
568 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000569 if( ret == 0 )
570 ret = 1;
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000575 while( *list )
576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200578 list++;
579 if( !*list )
580 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000582 list++;
583 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000585 goto exit;
586 }
587
588 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100589 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000590 opt.server_port = DFL_SERVER_PORT;
591 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100592 opt.nbio = DFL_NBIO;
Hanno Becker16970d22017-10-10 15:56:37 +0100593 opt.event = DFL_EVENT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200594 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200595 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000596 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200597 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000598 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000599 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000600 opt.crt_file = DFL_CRT_FILE;
601 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200602 opt.psk = DFL_PSK;
603 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200604 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakker51936882011-02-20 16:05:58 +0000605 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000606 opt.renegotiation = DFL_RENEGOTIATION;
607 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100608 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200609 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000610 opt.min_version = DFL_MIN_VERSION;
611 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100612 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +0200613 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100614 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200615 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200616 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100617 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200618 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200619 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100620 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200621 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200622 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200623 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +0100624 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200625 opt.transport = DFL_TRANSPORT;
626 opt.hs_to_min = DFL_HS_TO_MIN;
627 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200628 opt.dtls_mtu = DFL_DTLS_MTU;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200629 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200630 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100631 opt.etm = DFL_ETM;
Hanno Becker4d615912018-08-14 13:33:30 +0100632 opt.dgram_packing = DFL_DGRAM_PACKING;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000633
634 for( i = 1; i < argc; i++ )
635 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000636 p = argv[i];
637 if( ( q = strchr( p, '=' ) ) == NULL )
638 goto usage;
639 *q++ = '\0';
640
641 if( strcmp( p, "server_name" ) == 0 )
642 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100643 else if( strcmp( p, "server_addr" ) == 0 )
644 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000645 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200646 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100647 else if( strcmp( p, "dtls" ) == 0 )
648 {
649 int t = atoi( q );
650 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100652 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100654 else
655 goto usage;
656 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000657 else if( strcmp( p, "debug_level" ) == 0 )
658 {
659 opt.debug_level = atoi( q );
660 if( opt.debug_level < 0 || opt.debug_level > 65535 )
661 goto usage;
662 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100663 else if( strcmp( p, "nbio" ) == 0 )
664 {
665 opt.nbio = atoi( q );
666 if( opt.nbio < 0 || opt.nbio > 2 )
667 goto usage;
668 }
Hanno Becker16970d22017-10-10 15:56:37 +0100669 else if( strcmp( p, "event" ) == 0 )
670 {
671 opt.event = atoi( q );
672 if( opt.event < 0 || opt.event > 2 )
673 goto usage;
674 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200675 else if( strcmp( p, "read_timeout" ) == 0 )
676 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200677 else if( strcmp( p, "max_resend" ) == 0 )
678 {
679 opt.max_resend = atoi( q );
680 if( opt.max_resend < 0 )
681 goto usage;
682 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000683 else if( strcmp( p, "request_page" ) == 0 )
684 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200685 else if( strcmp( p, "request_size" ) == 0 )
686 {
687 opt.request_size = atoi( q );
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100688 if( opt.request_size < 0 ||
689 opt.request_size > MAX_REQUEST_SIZE )
Paul Bakker93c32b22014-04-25 13:40:05 +0200690 goto usage;
691 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000692 else if( strcmp( p, "ca_file" ) == 0 )
693 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000694 else if( strcmp( p, "ca_path" ) == 0 )
695 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000696 else if( strcmp( p, "crt_file" ) == 0 )
697 opt.crt_file = q;
698 else if( strcmp( p, "key_file" ) == 0 )
699 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200700 else if( strcmp( p, "psk" ) == 0 )
701 opt.psk = q;
702 else if( strcmp( p, "psk_identity" ) == 0 )
703 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200704 else if( strcmp( p, "ecjpake_pw" ) == 0 )
705 opt.ecjpake_pw = q;
Paul Bakker51936882011-02-20 16:05:58 +0000706 else if( strcmp( p, "force_ciphersuite" ) == 0 )
707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +0000709
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200710 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000711 {
712 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000713 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000714 }
Paul Bakker51936882011-02-20 16:05:58 +0000715 opt.force_ciphersuite[1] = 0;
716 }
Paul Bakker48916f92012-09-16 19:57:18 +0000717 else if( strcmp( p, "renegotiation" ) == 0 )
718 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100719 opt.renegotiation = (atoi( q )) ?
720 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
721 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +0000722 }
723 else if( strcmp( p, "allow_legacy" ) == 0 )
724 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100725 switch( atoi( q ) )
726 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100727 case -1:
728 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
729 break;
730 case 0:
731 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
732 break;
733 case 1:
734 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
735 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100736 default: goto usage;
737 }
Paul Bakker48916f92012-09-16 19:57:18 +0000738 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100739 else if( strcmp( p, "renegotiate" ) == 0 )
740 {
741 opt.renegotiate = atoi( q );
742 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
743 goto usage;
744 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200745 else if( strcmp( p, "exchanges" ) == 0 )
746 {
747 opt.exchanges = atoi( q );
748 if( opt.exchanges < 1 )
749 goto usage;
750 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200751 else if( strcmp( p, "reconnect" ) == 0 )
752 {
753 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200754 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200755 goto usage;
756 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100757 else if( strcmp( p, "reco_delay" ) == 0 )
758 {
759 opt.reco_delay = atoi( q );
760 if( opt.reco_delay < 0 )
761 goto usage;
762 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200763 else if( strcmp( p, "reconnect_hard" ) == 0 )
764 {
765 opt.reconnect_hard = atoi( q );
766 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
767 goto usage;
768 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200769 else if( strcmp( p, "tickets" ) == 0 )
770 {
771 opt.tickets = atoi( q );
772 if( opt.tickets < 0 || opt.tickets > 2 )
773 goto usage;
774 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200775 else if( strcmp( p, "alpn" ) == 0 )
776 {
777 opt.alpn_string = q;
778 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200779 else if( strcmp( p, "fallback" ) == 0 )
780 {
781 switch( atoi( q ) )
782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
784 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200785 default: goto usage;
786 }
787 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200788 else if( strcmp( p, "extended_ms" ) == 0 )
789 {
790 switch( atoi( q ) )
791 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100792 case 0:
793 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
794 break;
795 case 1:
796 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
797 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200798 default: goto usage;
799 }
800 }
Hanno Beckere6706e62017-05-15 16:05:15 +0100801 else if( strcmp( p, "curves" ) == 0 )
802 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100803 else if( strcmp( p, "etm" ) == 0 )
804 {
805 switch( atoi( q ) )
806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
808 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100809 default: goto usage;
810 }
811 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000812 else if( strcmp( p, "min_version" ) == 0 )
813 {
814 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000816 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100818 else if( strcmp( q, "tls1_1" ) == 0 ||
819 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100821 else if( strcmp( q, "tls1_2" ) == 0 ||
822 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000824 else
825 goto usage;
826 }
827 else if( strcmp( p, "max_version" ) == 0 )
828 {
829 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000831 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100833 else if( strcmp( q, "tls1_1" ) == 0 ||
834 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100836 else if( strcmp( q, "tls1_2" ) == 0 ||
837 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000839 else
840 goto usage;
841 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100842 else if( strcmp( p, "arc4" ) == 0 )
843 {
844 switch( atoi( q ) )
845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
847 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100848 default: goto usage;
849 }
850 }
Gilles Peskinebc70a182017-05-09 15:59:24 +0200851 else if( strcmp( p, "allow_sha1" ) == 0 )
852 {
853 switch( atoi( q ) )
854 {
855 case 0: opt.allow_sha1 = 0; break;
856 case 1: opt.allow_sha1 = 1; break;
857 default: goto usage;
858 }
859 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000860 else if( strcmp( p, "force_version" ) == 0 )
861 {
862 if( strcmp( q, "ssl3" ) == 0 )
863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
865 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000866 }
867 else if( strcmp( q, "tls1" ) == 0 )
868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
870 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000871 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100872 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
875 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000876 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100877 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
880 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000881 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100882 else if( strcmp( q, "dtls1" ) == 0 )
883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
885 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
886 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100887 }
888 else if( strcmp( q, "dtls1_2" ) == 0 )
889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
891 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
892 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100893 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000894 else
895 goto usage;
896 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100897 else if( strcmp( p, "auth_mode" ) == 0 )
898 {
899 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100901 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100903 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100905 else
906 goto usage;
907 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200908 else if( strcmp( p, "max_frag_len" ) == 0 )
909 {
910 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200912 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200914 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200916 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200918 else
919 goto usage;
920 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200921 else if( strcmp( p, "trunc_hmac" ) == 0 )
922 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100923 switch( atoi( q ) )
924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
926 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100927 default: goto usage;
928 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200929 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200930 else if( strcmp( p, "hs_timeout" ) == 0 )
931 {
932 if( ( p = strchr( q, '-' ) ) == NULL )
933 goto usage;
934 *p++ = '\0';
935 opt.hs_to_min = atoi( q );
936 opt.hs_to_max = atoi( p );
937 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
938 goto usage;
939 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200940 else if( strcmp( p, "mtu" ) == 0 )
941 {
942 opt.dtls_mtu = atoi( q );
943 if( opt.dtls_mtu < 0 )
944 goto usage;
945 }
Hanno Becker4d615912018-08-14 13:33:30 +0100946 else if( strcmp( p, "dgram_packing" ) == 0 )
947 {
948 opt.dgram_packing = atoi( q );
949 if( opt.dgram_packing != 0 &&
950 opt.dgram_packing != 1 )
951 {
952 goto usage;
953 }
954 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100955 else if( strcmp( p, "recsplit" ) == 0 )
956 {
957 opt.recsplit = atoi( q );
958 if( opt.recsplit < 0 || opt.recsplit > 1 )
959 goto usage;
960 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200961 else if( strcmp( p, "dhmlen" ) == 0 )
962 {
963 opt.dhmlen = atoi( q );
964 if( opt.dhmlen < 0 )
965 goto usage;
966 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000967 else
968 goto usage;
969 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000970
Hanno Becker16970d22017-10-10 15:56:37 +0100971 /* Event-driven IO is incompatible with the above custom
972 * receive and send functions, as the polling builds on
973 * refers to the underlying net_context. */
974 if( opt.event == 1 && opt.nbio != 1 )
975 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +0100976 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +0100977 opt.nbio = 1;
978 }
979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980#if defined(MBEDTLS_DEBUG_C)
981 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200982#endif
983
Paul Bakkerc1516be2013-06-29 16:01:32 +0200984 if( opt.force_ciphersuite[0] > 0 )
985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100987 ciphersuite_info =
988 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +0200989
Paul Bakker66c48102013-07-26 14:05:32 +0200990 if( opt.max_version != -1 &&
991 ciphersuite_info->min_minor_ver > opt.max_version )
992 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100993 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker66c48102013-07-26 14:05:32 +0200994 ret = 2;
995 goto usage;
996 }
997 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200998 ciphersuite_info->max_minor_ver < opt.min_version )
999 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001000 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001001 ret = 2;
1002 goto usage;
1003 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001004
1005 /* If the server selects a version that's not supported by
1006 * this suite, then there will be no common ciphersuite... */
1007 if( opt.max_version == -1 ||
1008 opt.max_version > ciphersuite_info->max_minor_ver )
1009 {
Paul Bakker66c48102013-07-26 14:05:32 +02001010 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001011 }
Paul Bakker66c48102013-07-26 14:05:32 +02001012 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001013 {
Paul Bakker66c48102013-07-26 14:05:32 +02001014 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001015 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1017 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1018 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001019 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001020
1021 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001025 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001026 mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001027 ret = 2;
1028 goto usage;
1029 }
1030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001032 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001033 }
1034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001036 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001037 * Unhexify the pre-shared key if any is given
1038 */
1039 if( strlen( opt.psk ) )
1040 {
1041 unsigned char c;
1042 size_t j;
1043
1044 if( strlen( opt.psk ) % 2 != 0 )
1045 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001046 mbedtls_printf( "pre-shared key not valid hex\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001047 goto exit;
1048 }
1049
1050 psk_len = strlen( opt.psk ) / 2;
1051
1052 for( j = 0; j < strlen( opt.psk ); j += 2 )
1053 {
1054 c = opt.psk[j];
1055 if( c >= '0' && c <= '9' )
1056 c -= '0';
1057 else if( c >= 'a' && c <= 'f' )
1058 c -= 'a' - 10;
1059 else if( c >= 'A' && c <= 'F' )
1060 c -= 'A' - 10;
1061 else
1062 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001063 mbedtls_printf( "pre-shared key not valid hex\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001064 goto exit;
1065 }
1066 psk[ j / 2 ] = c << 4;
1067
1068 c = opt.psk[j + 1];
1069 if( c >= '0' && c <= '9' )
1070 c -= '0';
1071 else if( c >= 'a' && c <= 'f' )
1072 c -= 'a' - 10;
1073 else if( c >= 'A' && c <= 'F' )
1074 c -= 'A' - 10;
1075 else
1076 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001077 mbedtls_printf( "pre-shared key not valid hex\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001078 goto exit;
1079 }
1080 psk[ j / 2 ] |= c;
1081 }
1082 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001084
Hanno Beckere6706e62017-05-15 16:05:15 +01001085#if defined(MBEDTLS_ECP_C)
1086 if( opt.curves != NULL )
1087 {
1088 p = (char *) opt.curves;
1089 i = 0;
1090
1091 if( strcmp( p, "none" ) == 0 )
1092 {
1093 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1094 }
1095 else if( strcmp( p, "default" ) != 0 )
1096 {
1097 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001098 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001099 {
1100 q = p;
1101
1102 /* Terminate the current string */
1103 while( *p != ',' && *p != '\0' )
1104 p++;
1105 if( *p == ',' )
1106 *p++ = '\0';
1107
1108 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1109 {
1110 curve_list[i++] = curve_cur->grp_id;
1111 }
1112 else
1113 {
1114 mbedtls_printf( "unknown curve %s\n", q );
1115 mbedtls_printf( "supported curves: " );
1116 for( curve_cur = mbedtls_ecp_curve_list();
1117 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1118 curve_cur++ )
1119 {
1120 mbedtls_printf( "%s ", curve_cur->name );
1121 }
1122 mbedtls_printf( "\n" );
1123 goto exit;
1124 }
1125 }
1126
1127 mbedtls_printf("Number of curves: %d\n", i );
1128
Hanno Becker8651a432017-06-09 16:13:22 +01001129 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001130 {
Hanno Becker8651a432017-06-09 16:13:22 +01001131 mbedtls_printf( "curves list too long, maximum %d",
1132 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001133 goto exit;
1134 }
1135
1136 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1137 }
1138 }
1139#endif /* MBEDTLS_ECP_C */
1140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001142 if( opt.alpn_string != NULL )
1143 {
1144 p = (char *) opt.alpn_string;
1145 i = 0;
1146
1147 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001148 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001149 {
1150 alpn_list[i++] = p;
1151
1152 /* Terminate the current string and move on to next one */
1153 while( *p != ',' && *p != '\0' )
1154 p++;
1155 if( *p == ',' )
1156 *p++ = '\0';
1157 }
1158 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001160
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001161 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 * 0. Initialize the RNG and the session data
1163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001165 fflush( stdout );
1166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 mbedtls_entropy_init( &entropy );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001168 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1169 &entropy, (const unsigned char *) pers,
1170 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +00001171 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001172 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
1173 -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001174 goto exit;
1175 }
1176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001180 /*
1181 * 1.1. Load the trusted CA
1182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 fflush( stdout );
1185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001187 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001188 if( strcmp( opt.ca_path, "none" ) == 0 )
1189 ret = 0;
1190 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001192 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001193 if( strcmp( opt.ca_file, "none" ) == 0 )
1194 ret = 0;
1195 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001197 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001198#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199#if defined(MBEDTLS_CERTS_C)
1200 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 ret = mbedtls_x509_crt_parse( &cacert,
1203 (const unsigned char *) mbedtls_test_cas[i],
1204 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001205 if( ret != 0 )
1206 break;
1207 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001208#else
1209 {
1210 ret = 1;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001211 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001212 }
1213#endif
Paul Bakker42488232012-05-16 08:21:05 +00001214 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001216 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1217 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 goto exit;
1219 }
1220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001222
1223 /*
1224 * 1.2. Load own certificate and private key
1225 *
1226 * (can be skipped if client authentication is not required)
1227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 fflush( stdout );
1230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001232 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001233 if( strcmp( opt.crt_file, "none" ) == 0 )
1234 ret = 0;
1235 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001237 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001238#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001240 ret = mbedtls_x509_crt_parse( &clicert,
1241 (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001243#else
1244 {
1245 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001247 }
1248#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 if( ret != 0 )
1250 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001251 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1252 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 goto exit;
1254 }
1255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001257 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001258 if( strcmp( opt.key_file, "none" ) == 0 )
1259 ret = 0;
1260 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001262 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001263#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001265 ret = mbedtls_pk_parse_key( &pkey,
1266 (const unsigned char *) mbedtls_test_cli_key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001268#else
1269 {
1270 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001272 }
1273#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001274 if( ret != 0 )
1275 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001276 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
1277 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 goto exit;
1279 }
1280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 mbedtls_printf( " ok\n" );
1282#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001283
1284 /*
1285 * 2. Start the connection
1286 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001287 if( opt.server_addr == NULL)
1288 opt.server_addr = opt.server_name;
1289
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001290 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001292 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 fflush( stdout );
1294
Hanno Becker16970d22017-10-10 15:56:37 +01001295 if( ( ret = mbedtls_net_connect( &server_fd,
1296 opt.server_addr, opt.server_port,
1297 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1298 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001299 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001300 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
1301 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001302 goto exit;
1303 }
1304
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001305 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001306 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001307 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001308 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001309 if( ret != 0 )
1310 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001311 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
1312 -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001313 goto exit;
1314 }
1315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001317
1318 /*
1319 * 3. Setup stuff
1320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 fflush( stdout );
1323
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001324 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1325 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001326 opt.transport,
1327 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001328 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001329 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
1330 -ret );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001331 goto exit;
1332 }
1333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001335 /* The default algorithms profile disables SHA-1, but our tests still
1336 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001337 if( opt.allow_sha1 > 0 )
1338 {
1339 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1340 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1341 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1342 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001343
Paul Bakker915275b2012-09-28 07:10:55 +00001344 if( opt.debug_level > 0 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001345 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
Gilles Peskineef86ab22017-05-05 18:59:02 +02001346#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001347
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001348 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001349 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001352 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Hanno Becker16970d22017-10-10 15:56:37 +01001353 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min,
1354 opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001355
1356 if( opt.dtls_mtu != DFL_DTLS_MTU )
1357 mbedtls_ssl_conf_mtu( &conf, opt.dtls_mtu );
Hanno Becker4d615912018-08-14 13:33:30 +01001358
1359 if( opt.dgram_packing != DFL_DGRAM_PACKING )
1360 mbedtls_ssl_conf_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001364 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001365 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001366 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n",
1367 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001368 goto exit;
1369 }
Paul Bakker05decb22013-08-15 13:33:48 +02001370#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001373 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001374 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001375#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001378 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001379 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001380#endif
1381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001383 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001384 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001385#endif
1386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001388 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001389 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Hanno Becker16970d22017-10-10 15:56:37 +01001390 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1391 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001392#endif
1393
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001394#if defined(MBEDTLS_DHM_C)
1395 if( opt.dhmlen != DFL_DHMLEN )
1396 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1397#endif
1398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001400 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001401 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001402 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001403 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
1404 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001405 goto exit;
1406 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001407#endif
1408
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001409 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1410 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001411
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001412 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02001415 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02001416#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001417
Paul Bakker645ce3a2012-10-31 12:32:41 +00001418 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001419 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001420
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001421#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001422 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001423 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001424#endif
Paul Bakker51936882011-02-20 16:05:58 +00001425
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001426 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001427 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001429 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001430#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001433 if( strcmp( opt.ca_path, "none" ) != 0 &&
1434 strcmp( opt.ca_file, "none" ) != 0 )
1435 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001436 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001437 }
1438 if( strcmp( opt.crt_file, "none" ) != 0 &&
1439 strcmp( opt.key_file, "none" ) != 0 )
1440 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001441 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001442 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001443 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
1444 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001445 goto exit;
1446 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001447 }
Paul Bakkered27a042013-04-18 22:46:23 +02001448#endif
1449
Hanno Beckere6706e62017-05-15 16:05:15 +01001450#if defined(MBEDTLS_ECP_C)
1451 if( opt.curves != NULL &&
1452 strcmp( opt.curves, "default" ) != 0 )
1453 {
1454 mbedtls_ssl_conf_curves( &conf, curve_list );
1455 }
1456#endif
1457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001459 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001460 (const unsigned char *) opt.psk_identity,
1461 strlen( opt.psk_identity ) ) ) != 0 )
1462 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001463 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n",
1464 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001465 goto exit;
1466 }
Paul Bakkered27a042013-04-18 22:46:23 +02001467#endif
1468
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001469 if( opt.min_version != DFL_MIN_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01001470 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1471 opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001472
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001473 if( opt.max_version != DFL_MAX_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01001474 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1475 opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001478 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001479 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001480#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001481
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001482 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
1483 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001484 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
1485 -ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001486 goto exit;
1487 }
1488
1489#if defined(MBEDTLS_X509_CRT_PARSE_C)
1490 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1491 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001492 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
1493 ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001494 goto exit;
1495 }
1496#endif
1497
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001498#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1499 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
1500 {
1501 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
1502 (const unsigned char *) opt.ecjpake_pw,
1503 strlen( opt.ecjpake_pw ) ) ) != 0 )
1504 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001505 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
1506 ret );
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001507 goto exit;
1508 }
1509 }
1510#endif
1511
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001512 if( opt.nbio == 2 )
1513 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
1514 else
Hanno Becker16970d22017-10-10 15:56:37 +01001515 mbedtls_ssl_set_bio( &ssl, &server_fd,
1516 mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02001517 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001518
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001519#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001520 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
1521 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001522#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001525
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 /*
1527 * 4. Handshake
1528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001530 fflush( stdout );
1531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 {
Hanno Becker16970d22017-10-10 15:56:37 +01001534 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1535 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker40e46942009-01-03 21:51:57 +00001536 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001537 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
1538 -ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
1540 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001541 " Unable to verify the server's certificate. "
1542 "Either it is invalid,\n"
1543 " or you didn't set ca_file or ca_path "
1544 "to an appropriate value.\n"
1545 " Alternatively, you may want to use "
1546 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001548 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00001549 }
Hanno Becker16970d22017-10-10 15:56:37 +01001550
1551 /* For event-driven IO, wait for socket to become available */
1552 if( opt.event == 1 /* level triggered IO */ )
1553 {
1554#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001555 ret = idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001556#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001557 ret = idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001558#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001559 if( ret != 0 )
1560 goto exit;
Hanno Becker16970d22017-10-10 15:56:37 +01001561 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001562 }
1563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Hanno Becker16970d22017-10-10 15:56:37 +01001565 mbedtls_ssl_get_version( &ssl ),
1566 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
1569 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001570 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001572
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02001573#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1574 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
1575 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
1576#endif
1577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001579 if( opt.alpn_string != NULL )
1580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
1582 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001583 alp ? alp : "(none)" );
1584 }
1585#endif
1586
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001587 if( opt.reconnect != 0 )
1588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001590 fflush( stdout );
1591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001593 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001594 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
1595 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001596 goto exit;
1597 }
1598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001600 }
1601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001603 /*
1604 * 5. Verify the server certificate
1605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001607
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001608 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001609 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001610 char vrfy_buf[512];
1611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
Hanno Becker16970d22017-10-10 15:56:37 +01001614 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ),
1615 " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00001616
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001617 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001618 }
1619 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 mbedtls_printf( " . Peer certificate information ...\n" );
1625 mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1626 mbedtls_ssl_get_peer_cert( &ssl ) );
1627 mbedtls_printf( "%s\n", buf );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001628 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001632 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001633 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001634 /*
1635 * Perform renegotiation (this must be done when the server is waiting
1636 * for input from our side).
1637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001639 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001641 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001642 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1643 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001644 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001645 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
1646 ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001647 goto exit;
1648 }
Hanno Becker16970d22017-10-10 15:56:37 +01001649
1650 /* For event-driven IO, wait for socket to become available */
1651 if( opt.event == 1 /* level triggered IO */ )
1652 {
1653#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001654 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001655#else
Hanno Becker197a91c2017-10-31 10:58:53 +00001656 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001657#endif
1658 }
1659
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001660 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001662 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001664
Paul Bakker5121ce52009-01-03 21:22:43 +00001665 /*
1666 * 6. Write the GET request
1667 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001668 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001669send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001671 fflush( stdout );
1672
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001673 len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST,
1674 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001675 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001676
1677 /* Add padding to GET request to reach opt.request_size in length */
1678 if( opt.request_size != DFL_REQUEST_SIZE &&
1679 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001680 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001681 memset( buf + len, 'A', opt.request_size - len - tail_len );
1682 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001683 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001684
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001685 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001686 len += tail_len;
1687
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001688 /* Truncate if request size is smaller than the "natural" size */
1689 if( opt.request_size != DFL_REQUEST_SIZE &&
1690 len > opt.request_size )
1691 {
1692 len = opt.request_size;
1693
1694 /* Still end with \r\n unless that's really not possible */
1695 if( len >= 2 ) buf[len - 2] = '\r';
1696 if( len >= 1 ) buf[len - 1] = '\n';
1697 }
1698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001700 {
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001701 written = 0;
1702 frags = 0;
1703
1704 do
Paul Bakker5121ce52009-01-03 21:22:43 +00001705 {
Hanno Becker16970d22017-10-10 15:56:37 +01001706 while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001707 len - written ) ) < 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001708 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001709 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1710 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001711 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001712 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
1713 -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001714 goto exit;
1715 }
Hanno Becker16970d22017-10-10 15:56:37 +01001716
1717 /* 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 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001726 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001727
1728 frags++;
1729 written += ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001730 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001731 while( written < len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001732 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001733 else /* Not stream, so datagram */
1734 {
Hanno Becker16970d22017-10-10 15:56:37 +01001735 while( 1 )
1736 {
1737 ret = mbedtls_ssl_write( &ssl, buf, len );
1738
1739 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1740 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1741 break;
1742
1743 /* For event-driven IO, wait for socket to become available */
1744 if( opt.event == 1 /* level triggered IO */ )
1745 {
1746#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001747 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001748#else
Hanno Becker197a91c2017-10-31 10:58:53 +00001749 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001750#endif
1751 }
1752 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001753
1754 if( ret < 0 )
1755 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001756 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n",
1757 ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001758 goto exit;
1759 }
1760
1761 frags = 1;
1762 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001763
1764 if( written < len )
1765 {
1766 mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
1767 "was truncated to size %u", (unsigned) written );
1768 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001769 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001770
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001771 buf[written] = '\0';
Hanno Becker16970d22017-10-10 15:56:37 +01001772 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
1773 written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001774
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01001775 /* Send a non-empty request if request_size == 0 */
1776 if ( len == 0 )
1777 {
1778 opt.request_size = DFL_REQUEST_SIZE;
1779 goto send_request;
1780 }
1781
Paul Bakker5121ce52009-01-03 21:22:43 +00001782 /*
1783 * 7. Read the HTTP response
1784 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001786 fflush( stdout );
1787
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001788 /*
1789 * TLS and DTLS need different reading styles (stream vs datagram)
1790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001792 {
1793 do
1794 {
1795 len = sizeof( buf ) - 1;
1796 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001798
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001799 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1800 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Hanno Becker16970d22017-10-10 15:56:37 +01001801 {
1802 /* For event-driven IO, wait for socket to become available */
1803 if( opt.event == 1 /* level triggered IO */ )
1804 {
1805#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001806 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001807#else
Hanno Becker197a91c2017-10-31 10:58:53 +00001808 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001809#endif
1810 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001811 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01001812 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001813
1814 if( ret <= 0 )
1815 {
1816 switch( ret )
1817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1819 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001820 ret = 0;
1821 goto close_notify;
1822
1823 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 case MBEDTLS_ERR_NET_CONN_RESET:
1825 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001826 ret = 0;
1827 goto reconnect;
1828
1829 default:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001830 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n",
1831 -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001832 goto exit;
1833 }
1834 }
1835
1836 len = ret;
1837 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001839
1840 /* End of message should be detected according to the syntax of the
1841 * application protocol (eg HTTP), just use a dummy test here. */
1842 if( ret > 0 && buf[len-1] == '\n' )
1843 {
1844 ret = 0;
1845 break;
1846 }
1847 }
1848 while( 1 );
1849 }
1850 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001851 {
1852 len = sizeof( buf ) - 1;
1853 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001854
Hanno Becker16970d22017-10-10 15:56:37 +01001855 while( 1 )
1856 {
1857 ret = mbedtls_ssl_read( &ssl, buf, len );
1858
1859 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1860 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1861 break;
1862
1863 /* For event-driven IO, wait for socket to become available */
1864 if( opt.event == 1 /* level triggered IO */ )
1865 {
1866#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001867 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001868#else
Hanno Becker197a91c2017-10-31 10:58:53 +00001869 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001870#endif
1871 }
1872 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001873
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001874 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001876 switch( ret )
1877 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001878 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001880 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001881 goto send_request;
1882 goto exit;
1883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1885 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001886 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001887 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00001888
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001889 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001891 goto exit;
1892 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001893 }
1894
Paul Bakker5121ce52009-01-03 21:22:43 +00001895 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02001896 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001898 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001900
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001901 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001902 * 7b. Simulate hard reset and reconnect from same port?
1903 */
1904 if( opt.reconnect_hard != 0 )
1905 {
1906 opt.reconnect_hard = 0;
1907
1908 mbedtls_printf( " . Restarting connection from same port..." );
1909 fflush( stdout );
1910
1911 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
1912 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001913 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
1914 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001915 goto exit;
1916 }
1917
1918 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
1919 {
1920 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1921 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1922 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001923 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
1924 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001925 goto exit;
1926 }
Hanno Becker16970d22017-10-10 15:56:37 +01001927
1928 /* For event-driven IO, wait for socket to become available */
1929 if( opt.event == 1 /* level triggered IO */ )
1930 {
1931#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001932 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001933#else
Hanno Becker197a91c2017-10-31 10:58:53 +00001934 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001935#endif
1936 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001937 }
1938
1939 mbedtls_printf( " ok\n" );
1940
1941 goto send_request;
1942 }
1943
1944 /*
1945 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001946 */
1947 if( --opt.exchanges > 0 )
1948 goto send_request;
1949
1950 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001951 * 8. Done, cleanly close the connection
1952 */
1953close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001955 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001956
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001957 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001959 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001960 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001963
1964 /*
1965 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001966 */
1967reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001968 if( opt.reconnect != 0 )
1969 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001970 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001971
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001972 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001973
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001974#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001975 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02001976 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001977#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02001978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001982 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001983 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
1984 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001985 goto exit;
1986 }
1987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001989 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001990 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n",
1991 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001992 goto exit;
1993 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001994
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001995 if( ( ret = mbedtls_net_connect( &server_fd,
1996 opt.server_addr, opt.server_port,
1997 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1998 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001999 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002000 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
2001 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002002 goto exit;
2003 }
2004
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002005 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002006 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002007 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002008 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002009 if( ret != 0 )
2010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002012 -ret );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002013 goto exit;
2014 }
2015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002017 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002018 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2019 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002020 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002021 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2022 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002023 goto exit;
2024 }
2025 }
2026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002028
2029 goto send_request;
2030 }
2031
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002032 /*
2033 * Cleanup and exit
2034 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002035exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00002037 if( ret != 0 )
2038 {
2039 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 mbedtls_strerror( ret, error_buf, 100 );
2041 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002042 }
2043#endif
2044
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002045 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047#if defined(MBEDTLS_X509_CRT_PARSE_C)
2048 mbedtls_x509_crt_free( &clicert );
2049 mbedtls_x509_crt_free( &cacert );
2050 mbedtls_pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02002051#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 mbedtls_ssl_session_free( &saved_session );
2053 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002054 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 mbedtls_ctr_drbg_free( &ctr_drbg );
2056 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002057
Paul Bakkercce9d772011-11-18 14:26:47 +00002058#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 fflush( stdout ); getchar();
2061#endif
2062
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002063 // Shell can not handle large exit numbers -> 1 for errors
2064 if( ret < 0 )
2065 ret = 1;
2066
Paul Bakker5121ce52009-01-03 21:22:43 +00002067 return( ret );
2068}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
2070 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002071 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */