blob: 84ce115cddaed1148694bf7e2ff0d8531faae244 [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
Paul Bakker9caf2d22010-02-18 19:37:19 +000066#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010067#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020068#define DFL_SERVER_PORT "4433"
Paul Bakker9caf2d22010-02-18 19:37:19 +000069#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +020070#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +000071#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010072#define DFL_NBIO 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020073#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +020074#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +000075#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000076#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000077#define DFL_CRT_FILE ""
78#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020079#define DFL_PSK ""
80#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +020081#define DFL_ECJPAKE_PW NULL
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +020082#define DFL_EC_MAX_OPS -1
Paul Bakker51936882011-02-20 16:05:58 +000083#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010085#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010086#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +020087#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +020088#define DFL_MIN_VERSION -1
Paul Bakker1d29fb52012-09-28 13:28:45 +000089#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +000090#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +020091#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +010092#define DFL_AUTH_MODE -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +010094#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +010095#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +020096#define DFL_DHMLEN -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +020097#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010098#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +020099#define DFL_RECONNECT_HARD 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200101#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100102#define DFL_CURVES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200104#define DFL_HS_TO_MIN 0
105#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200106#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200107#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100108#define DFL_ETM -1
Paul Bakker0e6975b2009-02-10 22:19:10 +0000109
Paul Bakker93c32b22014-04-25 13:40:05 +0200110#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
111#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_X509_CRT_PARSE_C)
114#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000115#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100116 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
117 " default: \"\" (pre-loaded)\n" \
118 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
119 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
120 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
121 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000122 " key_file=%%s default: \"\" (pre-loaded)\n"
123#else
124#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 " No file operations available (MBEDTLS_FS_IO not defined)\n"
126#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200127#else
128#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200132#define USAGE_PSK \
133 " psk=%%s default: \"\" (in hex, without 0x)\n" \
134 " psk_identity=%%s default: \"Client_identity\"\n"
135#else
136#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200140#define USAGE_TICKETS \
141 " tickets=%%d default: 1 (enabled)\n"
142#else
143#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200147#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100148 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200149#else
150#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200154#define USAGE_MAX_FRAG_LEN \
155 " max_frag_len=%%d default: 16384 (tls default)\n" \
156 " options: 512, 1024, 2048, 4096\n"
157#else
158#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100162#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200163 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100164#else
165#define USAGE_RECSPLIT
166#endif
167
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200168#if defined(MBEDTLS_DHM_C)
169#define USAGE_DHMLEN \
170 " dhmlen=%%d default: (library default: 1024 bits)\n"
171#else
172#define USAGE_DHMLEN
173#endif
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200176#define USAGE_ALPN \
177 " alpn=%%s default: \"\" (disabled)\n" \
178 " example: spdy/1,http/1.1\n"
179#else
180#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200182
Hanno Beckere6706e62017-05-15 16:05:15 +0100183#if defined(MBEDTLS_ECP_C)
184#define USAGE_CURVES \
185 " curves=a,b,c,d default: \"default\" (library default)\n" \
186 " example: \"secp521r1,brainpoolP512r1\"\n" \
187 " - use \"none\" for empty list\n" \
188 " - see mbedtls_ecp_curve_list()\n" \
189 " for acceptable curve names\n"
190#else
191#define USAGE_CURVES ""
192#endif
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200195#define USAGE_DTLS \
196 " dtls=%%d default: 0 (TLS)\n" \
197 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
198 " range of DTLS handshake timeouts in millisecs\n"
199#else
200#define USAGE_DTLS ""
201#endif
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200204#define USAGE_FALLBACK \
205 " fallback=0/1 default: (library default: off)\n"
206#else
207#define USAGE_FALLBACK ""
208#endif
209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200211#define USAGE_EMS \
212 " extended_ms=0/1 default: (library default: on)\n"
213#else
214#define USAGE_EMS ""
215#endif
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100218#define USAGE_ETM \
219 " etm=0/1 default: (library default: on)\n"
220#else
221#define USAGE_ETM ""
222#endif
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100225#define USAGE_RENEGO \
226 " renegotiation=%%d default: 0 (disabled)\n" \
227 " renegotiate=%%d default: 0 (disabled)\n"
228#else
229#define USAGE_RENEGO ""
230#endif
231
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200232#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
233#define USAGE_ECJPAKE \
234 " ecjpake_pw=%%s default: none (disabled)\n"
235#else
236#define USAGE_ECJPAKE ""
237#endif
238
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200239#if defined(MBEDTLS_ECP_RESTARTABLE)
240#define USAGE_ECRESTART \
241 " ec_max_ops=%%s default: library default (restart disabled)\n"
242#else
243#define USAGE_ECRESTART ""
244#endif
245
Paul Bakker9caf2d22010-02-18 19:37:19 +0000246#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000247 "\n usage: ssl_client2 param=<>...\n" \
248 "\n acceptable parameters:\n" \
249 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100250 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000251 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000252 " request_page=%%s default: \".\"\n" \
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +0200253 " request_size=%%d default: about 34 (basic request)\n" \
254 " (minimum: 0, max: 16384)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100255 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100256 " nbio=%%d default: 0 (blocking I/O)\n" \
257 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200258 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200259 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100260 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200261 USAGE_DTLS \
262 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100263 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100264 " options: none, optional, required\n" \
265 USAGE_IO \
266 "\n" \
267 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200268 USAGE_ECJPAKE \
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200269 USAGE_ECRESTART \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100270 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100271 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100272 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200273 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200274 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200275 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200276 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200277 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100278 USAGE_MAX_FRAG_LEN \
279 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200280 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200281 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200282 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100283 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100284 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100285 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200286 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000287 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100288 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200289 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200290 " min_version=%%s default: (library default: tls1)\n" \
291 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000292 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100293 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000294 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000295 " force_ciphersuite=<name> default: all enabled\n"\
296 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000297
Hanno Becker8651a432017-06-09 16:13:22 +0100298#define ALPN_LIST_SIZE 10
299#define CURVE_LIST_SIZE 20
300
Rich Evans85b05ec2015-02-12 11:37:29 +0000301/*
302 * global options
303 */
304struct options
305{
306 const char *server_name; /* hostname of the server (client only) */
307 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200308 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000309 int debug_level; /* level of debugging */
310 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000312 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000313 const char *request_page; /* page on server to request */
314 int request_size; /* pad request with header to requested size */
315 const char *ca_file; /* the file with the CA certificate(s) */
316 const char *ca_path; /* the path with the CA certificate(s) reside */
317 const char *crt_file; /* the file with the client certificate */
318 const char *key_file; /* the file with the client key */
319 const char *psk; /* the pre-shared key */
320 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200321 const char *ecjpake_pw; /* the EC J-PAKE password */
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200322 int ec_max_ops; /* EC consecutive operations limit */
Rich Evans85b05ec2015-02-12 11:37:29 +0000323 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
324 int renegotiation; /* enable / disable renegotiation */
325 int allow_legacy; /* allow legacy renegotiation */
326 int renegotiate; /* attempt renegotiation? */
327 int renego_delay; /* delay before enforcing renegotiation */
328 int exchanges; /* number of data exchanges */
329 int min_version; /* minimum protocol version accepted */
330 int max_version; /* maximum protocol version accepted */
331 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200332 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000333 int auth_mode; /* verify mode for connection */
334 unsigned char mfl_code; /* code for maximum fragment length */
335 int trunc_hmac; /* negotiate truncated hmac or not */
336 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200337 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000338 int reconnect; /* attempt to resume session */
339 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200340 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000341 int tickets; /* enable / disable session tickets */
Hanno Beckere6706e62017-05-15 16:05:15 +0100342 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000343 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000344 int transport; /* TLS or DTLS? */
345 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
346 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Rich Evans85b05ec2015-02-12 11:37:29 +0000347 int fallback; /* is this a fallback connection? */
348 int extended_ms; /* negotiate extended master secret? */
349 int etm; /* negotiate encrypt then mac? */
350} opt;
351
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200352static void my_debug( void *ctx, int level,
353 const char *file, int line,
354 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000355{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200356 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000357
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200358 /* Extract basename from file */
359 for( p = basename = file; *p != '\0'; p++ )
360 if( *p == '/' || *p == '\\' )
361 basename = p + 1;
362
363 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000364 fflush( (FILE *) ctx );
365}
366
367/*
368 * Test recv/send functions that make sure each try returns
369 * WANT_READ/WANT_WRITE at least once before sucesseding
370 */
371static int my_recv( void *ctx, unsigned char *buf, size_t len )
372{
373 static int first_try = 1;
374 int ret;
375
376 if( first_try )
377 {
378 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100379 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000380 }
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100383 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000384 first_try = 1; /* Next call will be a new operation */
385 return( ret );
386}
387
388static int my_send( void *ctx, const unsigned char *buf, size_t len )
389{
390 static int first_try = 1;
391 int ret;
392
393 if( first_try )
394 {
395 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100396 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000397 }
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100400 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000401 first_try = 1; /* Next call will be a new operation */
402 return( ret );
403}
404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#if defined(MBEDTLS_X509_CRT_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000406/*
407 * Enabled if debug_level > 1 in code below
408 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200409static int my_verify( void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000410{
411 char buf[1024];
412 ((void) data);
413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
415 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
416 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000417
Rich Evans85b05ec2015-02-12 11:37:29 +0000418 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100420 else
421 {
422 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
423 mbedtls_printf( "%s\n", buf );
424 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000425
426 return( 0 );
427}
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200428
429static int ssl_sig_hashes_for_test[] = {
430#if defined(MBEDTLS_SHA512_C)
431 MBEDTLS_MD_SHA512,
432 MBEDTLS_MD_SHA384,
433#endif
434#if defined(MBEDTLS_SHA256_C)
435 MBEDTLS_MD_SHA256,
436 MBEDTLS_MD_SHA224,
437#endif
438#if defined(MBEDTLS_SHA1_C)
439 /* Allow SHA-1 as we use it extensively in tests. */
440 MBEDTLS_MD_SHA1,
441#endif
442 MBEDTLS_MD_NONE
443};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000445
Paul Bakker9caf2d22010-02-18 19:37:19 +0000446int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000447{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200448 int ret = 0, len, tail_len, i, written, frags, retry_left;
449 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 unsigned char buf[MBEDTLS_SSL_MAX_CONTENT_LEN + 1];
451#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
452 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200453 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200454#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100456 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200457#endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100458#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +0100459 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100460 const mbedtls_ecp_curve_info *curve_cur;
461#endif
462
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200463 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000464
Gilles Peskineef86ab22017-05-05 18:59:02 +0200465#if defined(MBEDTLS_X509_CRT_PARSE_C)
466 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
467#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_entropy_context entropy;
469 mbedtls_ctr_drbg_context ctr_drbg;
470 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200471 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200473#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200474 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200475#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200477 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_x509_crt cacert;
479 mbedtls_x509_crt clicert;
480 mbedtls_pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200481#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000482 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000483 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000484
Paul Bakker1a207ec2011-02-06 13:22:40 +0000485 /*
486 * Make sure memory references are valid.
487 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200488 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200489 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200490 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200492 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493#if defined(MBEDTLS_X509_CRT_PARSE_C)
494 mbedtls_x509_crt_init( &cacert );
495 mbedtls_x509_crt_init( &clicert );
496 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200497#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200499 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200500#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000501
Paul Bakker9caf2d22010-02-18 19:37:19 +0000502 if( argc == 0 )
503 {
504 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000505 if( ret == 0 )
506 ret = 1;
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000511 while( *list )
512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200514 list++;
515 if( !*list )
516 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000518 list++;
519 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000521 goto exit;
522 }
523
524 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100525 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000526 opt.server_port = DFL_SERVER_PORT;
527 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100528 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200529 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200530 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000531 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200532 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000533 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000534 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000535 opt.crt_file = DFL_CRT_FILE;
536 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200537 opt.psk = DFL_PSK;
538 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200539 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200540 opt.ec_max_ops = DFL_EC_MAX_OPS;
Paul Bakker51936882011-02-20 16:05:58 +0000541 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000542 opt.renegotiation = DFL_RENEGOTIATION;
543 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100544 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200545 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000546 opt.min_version = DFL_MIN_VERSION;
547 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100548 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +0200549 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100550 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200551 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200552 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100553 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200554 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200555 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100556 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200557 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200558 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200559 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +0100560 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200561 opt.transport = DFL_TRANSPORT;
562 opt.hs_to_min = DFL_HS_TO_MIN;
563 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200564 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200565 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100566 opt.etm = DFL_ETM;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000567
568 for( i = 1; i < argc; i++ )
569 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000570 p = argv[i];
571 if( ( q = strchr( p, '=' ) ) == NULL )
572 goto usage;
573 *q++ = '\0';
574
575 if( strcmp( p, "server_name" ) == 0 )
576 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100577 else if( strcmp( p, "server_addr" ) == 0 )
578 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000579 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200580 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100581 else if( strcmp( p, "dtls" ) == 0 )
582 {
583 int t = atoi( q );
584 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100586 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100588 else
589 goto usage;
590 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000591 else if( strcmp( p, "debug_level" ) == 0 )
592 {
593 opt.debug_level = atoi( q );
594 if( opt.debug_level < 0 || opt.debug_level > 65535 )
595 goto usage;
596 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100597 else if( strcmp( p, "nbio" ) == 0 )
598 {
599 opt.nbio = atoi( q );
600 if( opt.nbio < 0 || opt.nbio > 2 )
601 goto usage;
602 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200603 else if( strcmp( p, "read_timeout" ) == 0 )
604 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200605 else if( strcmp( p, "max_resend" ) == 0 )
606 {
607 opt.max_resend = atoi( q );
608 if( opt.max_resend < 0 )
609 goto usage;
610 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000611 else if( strcmp( p, "request_page" ) == 0 )
612 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200613 else if( strcmp( p, "request_size" ) == 0 )
614 {
615 opt.request_size = atoi( q );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 if( opt.request_size < 0 || opt.request_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker93c32b22014-04-25 13:40:05 +0200617 goto usage;
618 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000619 else if( strcmp( p, "ca_file" ) == 0 )
620 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000621 else if( strcmp( p, "ca_path" ) == 0 )
622 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000623 else if( strcmp( p, "crt_file" ) == 0 )
624 opt.crt_file = q;
625 else if( strcmp( p, "key_file" ) == 0 )
626 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200627 else if( strcmp( p, "psk" ) == 0 )
628 opt.psk = q;
629 else if( strcmp( p, "psk_identity" ) == 0 )
630 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200631 else if( strcmp( p, "ecjpake_pw" ) == 0 )
632 opt.ecjpake_pw = q;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200633 else if( strcmp( p, "ec_max_ops" ) == 0 )
634 opt.ec_max_ops = atoi( q );
Paul Bakker51936882011-02-20 16:05:58 +0000635 else if( strcmp( p, "force_ciphersuite" ) == 0 )
636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +0000638
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200639 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000640 {
641 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000642 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000643 }
Paul Bakker51936882011-02-20 16:05:58 +0000644 opt.force_ciphersuite[1] = 0;
645 }
Paul Bakker48916f92012-09-16 19:57:18 +0000646 else if( strcmp( p, "renegotiation" ) == 0 )
647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 opt.renegotiation = (atoi( q )) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED :
649 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +0000650 }
651 else if( strcmp( p, "allow_legacy" ) == 0 )
652 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100653 switch( atoi( q ) )
654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 case -1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE; break;
656 case 0: opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; break;
657 case 1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION; break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100658 default: goto usage;
659 }
Paul Bakker48916f92012-09-16 19:57:18 +0000660 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100661 else if( strcmp( p, "renegotiate" ) == 0 )
662 {
663 opt.renegotiate = atoi( q );
664 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
665 goto usage;
666 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200667 else if( strcmp( p, "exchanges" ) == 0 )
668 {
669 opt.exchanges = atoi( q );
670 if( opt.exchanges < 1 )
671 goto usage;
672 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200673 else if( strcmp( p, "reconnect" ) == 0 )
674 {
675 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200676 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200677 goto usage;
678 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100679 else if( strcmp( p, "reco_delay" ) == 0 )
680 {
681 opt.reco_delay = atoi( q );
682 if( opt.reco_delay < 0 )
683 goto usage;
684 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200685 else if( strcmp( p, "reconnect_hard" ) == 0 )
686 {
687 opt.reconnect_hard = atoi( q );
688 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
689 goto usage;
690 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200691 else if( strcmp( p, "tickets" ) == 0 )
692 {
693 opt.tickets = atoi( q );
694 if( opt.tickets < 0 || opt.tickets > 2 )
695 goto usage;
696 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200697 else if( strcmp( p, "alpn" ) == 0 )
698 {
699 opt.alpn_string = q;
700 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200701 else if( strcmp( p, "fallback" ) == 0 )
702 {
703 switch( atoi( q ) )
704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
706 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200707 default: goto usage;
708 }
709 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200710 else if( strcmp( p, "extended_ms" ) == 0 )
711 {
712 switch( atoi( q ) )
713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 case 0: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED; break;
715 case 1: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200716 default: goto usage;
717 }
718 }
Hanno Beckere6706e62017-05-15 16:05:15 +0100719 else if( strcmp( p, "curves" ) == 0 )
720 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100721 else if( strcmp( p, "etm" ) == 0 )
722 {
723 switch( atoi( q ) )
724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
726 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100727 default: goto usage;
728 }
729 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000730 else if( strcmp( p, "min_version" ) == 0 )
731 {
732 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000734 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100736 else if( strcmp( q, "tls1_1" ) == 0 ||
737 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100739 else if( strcmp( q, "tls1_2" ) == 0 ||
740 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000742 else
743 goto usage;
744 }
745 else if( strcmp( p, "max_version" ) == 0 )
746 {
747 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000749 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100751 else if( strcmp( q, "tls1_1" ) == 0 ||
752 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100754 else if( strcmp( q, "tls1_2" ) == 0 ||
755 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000757 else
758 goto usage;
759 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100760 else if( strcmp( p, "arc4" ) == 0 )
761 {
762 switch( atoi( q ) )
763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
765 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100766 default: goto usage;
767 }
768 }
Gilles Peskinebc70a182017-05-09 15:59:24 +0200769 else if( strcmp( p, "allow_sha1" ) == 0 )
770 {
771 switch( atoi( q ) )
772 {
773 case 0: opt.allow_sha1 = 0; break;
774 case 1: opt.allow_sha1 = 1; break;
775 default: goto usage;
776 }
777 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000778 else if( strcmp( p, "force_version" ) == 0 )
779 {
780 if( strcmp( q, "ssl3" ) == 0 )
781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
783 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000784 }
785 else if( strcmp( q, "tls1" ) == 0 )
786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
788 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000789 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100790 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
793 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000794 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100795 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
798 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000799 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100800 else if( strcmp( q, "dtls1" ) == 0 )
801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
803 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
804 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100805 }
806 else if( strcmp( q, "dtls1_2" ) == 0 )
807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
809 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
810 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100811 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000812 else
813 goto usage;
814 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100815 else if( strcmp( p, "auth_mode" ) == 0 )
816 {
817 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100819 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100821 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100823 else
824 goto usage;
825 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200826 else if( strcmp( p, "max_frag_len" ) == 0 )
827 {
828 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200830 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200832 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200834 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200836 else
837 goto usage;
838 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200839 else if( strcmp( p, "trunc_hmac" ) == 0 )
840 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100841 switch( atoi( q ) )
842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
844 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100845 default: goto usage;
846 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200847 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200848 else if( strcmp( p, "hs_timeout" ) == 0 )
849 {
850 if( ( p = strchr( q, '-' ) ) == NULL )
851 goto usage;
852 *p++ = '\0';
853 opt.hs_to_min = atoi( q );
854 opt.hs_to_max = atoi( p );
855 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
856 goto usage;
857 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100858 else if( strcmp( p, "recsplit" ) == 0 )
859 {
860 opt.recsplit = atoi( q );
861 if( opt.recsplit < 0 || opt.recsplit > 1 )
862 goto usage;
863 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200864 else if( strcmp( p, "dhmlen" ) == 0 )
865 {
866 opt.dhmlen = atoi( q );
867 if( opt.dhmlen < 0 )
868 goto usage;
869 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000870 else
871 goto usage;
872 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874#if defined(MBEDTLS_DEBUG_C)
875 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200876#endif
877
Paul Bakkerc1516be2013-06-29 16:01:32 +0200878 if( opt.force_ciphersuite[0] > 0 )
879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
881 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +0200882
Paul Bakker66c48102013-07-26 14:05:32 +0200883 if( opt.max_version != -1 &&
884 ciphersuite_info->min_minor_ver > opt.max_version )
885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakker66c48102013-07-26 14:05:32 +0200887 ret = 2;
888 goto usage;
889 }
890 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200891 ciphersuite_info->max_minor_ver < opt.min_version )
892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +0200894 ret = 2;
895 goto usage;
896 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100897
898 /* If the server selects a version that's not supported by
899 * this suite, then there will be no common ciphersuite... */
900 if( opt.max_version == -1 ||
901 opt.max_version > ciphersuite_info->max_minor_ver )
902 {
Paul Bakker66c48102013-07-26 14:05:32 +0200903 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100904 }
Paul Bakker66c48102013-07-26 14:05:32 +0200905 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100906 {
Paul Bakker66c48102013-07-26 14:05:32 +0200907 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100908 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
910 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
911 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100912 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000913
914 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000920 ret = 2;
921 goto usage;
922 }
923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000925 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200926 }
927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200930 * Unhexify the pre-shared key if any is given
931 */
932 if( strlen( opt.psk ) )
933 {
934 unsigned char c;
935 size_t j;
936
937 if( strlen( opt.psk ) % 2 != 0 )
938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200940 goto exit;
941 }
942
943 psk_len = strlen( opt.psk ) / 2;
944
945 for( j = 0; j < strlen( opt.psk ); j += 2 )
946 {
947 c = opt.psk[j];
948 if( c >= '0' && c <= '9' )
949 c -= '0';
950 else if( c >= 'a' && c <= 'f' )
951 c -= 'a' - 10;
952 else if( c >= 'A' && c <= 'F' )
953 c -= 'A' - 10;
954 else
955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200957 goto exit;
958 }
959 psk[ j / 2 ] = c << 4;
960
961 c = opt.psk[j + 1];
962 if( c >= '0' && c <= '9' )
963 c -= '0';
964 else if( c >= 'a' && c <= 'f' )
965 c -= 'a' - 10;
966 else if( c >= 'A' && c <= 'F' )
967 c -= 'A' - 10;
968 else
969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200971 goto exit;
972 }
973 psk[ j / 2 ] |= c;
974 }
975 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200977
Hanno Beckere6706e62017-05-15 16:05:15 +0100978#if defined(MBEDTLS_ECP_C)
979 if( opt.curves != NULL )
980 {
981 p = (char *) opt.curves;
982 i = 0;
983
984 if( strcmp( p, "none" ) == 0 )
985 {
986 curve_list[0] = MBEDTLS_ECP_DP_NONE;
987 }
988 else if( strcmp( p, "default" ) != 0 )
989 {
990 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +0100991 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +0100992 {
993 q = p;
994
995 /* Terminate the current string */
996 while( *p != ',' && *p != '\0' )
997 p++;
998 if( *p == ',' )
999 *p++ = '\0';
1000
1001 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1002 {
1003 curve_list[i++] = curve_cur->grp_id;
1004 }
1005 else
1006 {
1007 mbedtls_printf( "unknown curve %s\n", q );
1008 mbedtls_printf( "supported curves: " );
1009 for( curve_cur = mbedtls_ecp_curve_list();
1010 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1011 curve_cur++ )
1012 {
1013 mbedtls_printf( "%s ", curve_cur->name );
1014 }
1015 mbedtls_printf( "\n" );
1016 goto exit;
1017 }
1018 }
1019
1020 mbedtls_printf("Number of curves: %d\n", i );
1021
Hanno Becker8651a432017-06-09 16:13:22 +01001022 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001023 {
Hanno Becker8651a432017-06-09 16:13:22 +01001024 mbedtls_printf( "curves list too long, maximum %d",
1025 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001026 goto exit;
1027 }
1028
1029 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1030 }
1031 }
1032#endif /* MBEDTLS_ECP_C */
1033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001035 if( opt.alpn_string != NULL )
1036 {
1037 p = (char *) opt.alpn_string;
1038 i = 0;
1039
1040 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001041 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001042 {
1043 alpn_list[i++] = p;
1044
1045 /* Terminate the current string and move on to next one */
1046 while( *p != ',' && *p != '\0' )
1047 p++;
1048 if( *p == ',' )
1049 *p++ = '\0';
1050 }
1051 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001053
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001054 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 * 0. Initialize the RNG and the session data
1056 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001058 fflush( stdout );
1059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001061 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001062 (const unsigned char *) pers,
1063 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +00001064 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001065 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n", -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001066 goto exit;
1067 }
1068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 /*
1073 * 1.1. Load the trusted CA
1074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001076 fflush( stdout );
1077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001079 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001080 if( strcmp( opt.ca_path, "none" ) == 0 )
1081 ret = 0;
1082 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001084 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001085 if( strcmp( opt.ca_file, "none" ) == 0 )
1086 ret = 0;
1087 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001089 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001090#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091#if defined(MBEDTLS_CERTS_C)
1092 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 ret = mbedtls_x509_crt_parse( &cacert,
1095 (const unsigned char *) mbedtls_test_cas[i],
1096 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001097 if( ret != 0 )
1098 break;
1099 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001100#else
1101 {
1102 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001104 }
1105#endif
Paul Bakker42488232012-05-16 08:21:05 +00001106 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001109 goto exit;
1110 }
1111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001113
1114 /*
1115 * 1.2. Load own certificate and private key
1116 *
1117 * (can be skipped if client authentication is not required)
1118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001120 fflush( stdout );
1121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001123 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001124 if( strcmp( opt.crt_file, "none" ) == 0 )
1125 ret = 0;
1126 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001128 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001129#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130#if defined(MBEDTLS_CERTS_C)
1131 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
1132 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001133#else
1134 {
1135 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001137 }
1138#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001139 if( ret != 0 )
1140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001142 goto exit;
1143 }
1144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001146 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001147 if( strcmp( opt.key_file, "none" ) == 0 )
1148 ret = 0;
1149 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001151 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001152#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153#if defined(MBEDTLS_CERTS_C)
1154 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_cli_key,
1155 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001156#else
1157 {
1158 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001160 }
1161#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 if( ret != 0 )
1163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 goto exit;
1166 }
1167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 mbedtls_printf( " ok\n" );
1169#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001170
1171 /*
1172 * 2. Start the connection
1173 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001174 if( opt.server_addr == NULL)
1175 opt.server_addr = opt.server_name;
1176
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001177 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001179 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001180 fflush( stdout );
1181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
1183 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1184 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 goto exit;
1188 }
1189
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001190 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001191 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001192 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001193 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001194 if( ret != 0 )
1195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001197 goto exit;
1198 }
1199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001201
1202 /*
1203 * 3. Setup stuff
1204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 fflush( stdout );
1207
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001208 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1209 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001210 opt.transport,
1211 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001212 {
1213 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
1214 goto exit;
1215 }
1216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001218 /* The default algorithms profile disables SHA-1, but our tests still
1219 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001220 if( opt.allow_sha1 > 0 )
1221 {
1222 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1223 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1224 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1225 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001226
Paul Bakker915275b2012-09-28 07:10:55 +00001227 if( opt.debug_level > 0 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001228 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
Gilles Peskineef86ab22017-05-05 18:59:02 +02001229#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001230
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001231 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001232 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001235 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001236 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001240 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001241 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001242 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001243 goto exit;
1244 }
Paul Bakker05decb22013-08-15 13:33:48 +02001245#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001248 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001249 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001250#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001253 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001254 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001255#endif
1256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001258 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001259 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001260#endif
1261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001263 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001264 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1266 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001267#endif
1268
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001269#if defined(MBEDTLS_DHM_C)
1270 if( opt.dhmlen != DFL_DHMLEN )
1271 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1272#endif
1273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001275 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001276 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001277 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001278 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001279 goto exit;
1280 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001281#endif
1282
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001283 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1284 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001285
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001286 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02001289 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02001290#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001291
Paul Bakker645ce3a2012-10-31 12:32:41 +00001292 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001293 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001294
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001295#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001296 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001297 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001298#endif
Paul Bakker51936882011-02-20 16:05:58 +00001299
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001300 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001301 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001303 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001304#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001307 if( strcmp( opt.ca_path, "none" ) != 0 &&
1308 strcmp( opt.ca_file, "none" ) != 0 )
1309 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001310 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001311 }
1312 if( strcmp( opt.crt_file, "none" ) != 0 &&
1313 strcmp( opt.key_file, "none" ) != 0 )
1314 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001315 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001316 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001317 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001318 goto exit;
1319 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001320 }
Paul Bakkered27a042013-04-18 22:46:23 +02001321#endif
1322
Hanno Beckere6706e62017-05-15 16:05:15 +01001323#if defined(MBEDTLS_ECP_C)
1324 if( opt.curves != NULL &&
1325 strcmp( opt.curves, "default" ) != 0 )
1326 {
1327 mbedtls_ssl_conf_curves( &conf, curve_list );
1328 }
1329#endif
1330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001332 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001333 (const unsigned char *) opt.psk_identity,
1334 strlen( opt.psk_identity ) ) ) != 0 )
1335 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001336 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001337 goto exit;
1338 }
Paul Bakkered27a042013-04-18 22:46:23 +02001339#endif
1340
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001341 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001342 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001343
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001344 if( opt.max_version != DFL_MAX_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001345 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001348 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001349 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001350#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001351
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001352 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
1353 {
1354 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
1355 goto exit;
1356 }
1357
1358#if defined(MBEDTLS_X509_CRT_PARSE_C)
1359 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1360 {
1361 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
1362 goto exit;
1363 }
1364#endif
1365
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001366#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1367 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
1368 {
1369 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
1370 (const unsigned char *) opt.ecjpake_pw,
1371 strlen( opt.ecjpake_pw ) ) ) != 0 )
1372 {
1373 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
1374 goto exit;
1375 }
1376 }
1377#endif
1378
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001379 if( opt.nbio == 2 )
1380 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
1381 else
1382 mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02001383 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001384
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001385#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001386 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
1387 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001388#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001389
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001390#if defined(MBEDTLS_ECP_RESTARTABLE)
1391 if( opt.ec_max_ops != DFL_EC_MAX_OPS )
1392 mbedtls_ecp_set_max_ops( opt.ec_max_ops );
1393#endif
1394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001396
Paul Bakker5121ce52009-01-03 21:22:43 +00001397 /*
1398 * 4. Handshake
1399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 fflush( stdout );
1402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 {
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001405 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1406 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
1407 ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
Paul Bakker40e46942009-01-03 21:51:57 +00001408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n", -ret );
1410 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
1411 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001412 " Unable to verify the server's certificate. "
1413 "Either it is invalid,\n"
1414 " or you didn't set ca_file or ca_path "
1415 "to an appropriate value.\n"
1416 " Alternatively, you may want to use "
1417 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00001420 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 }
1422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1424 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
1427 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001428 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001430
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02001431#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1432 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
1433 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
1434#endif
1435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001437 if( opt.alpn_string != NULL )
1438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
1440 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001441 alp ? alp : "(none)" );
1442 }
1443#endif
1444
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001445 if( opt.reconnect != 0 )
1446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001448 fflush( stdout );
1449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001453 goto exit;
1454 }
1455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001457 }
1458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 /*
1461 * 5. Verify the server certificate
1462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001464
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001465 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001466 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001467 char vrfy_buf[512];
1468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001470
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001471 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00001472
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001473 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001474 }
1475 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 mbedtls_printf( " . Peer certificate information ...\n" );
1481 mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1482 mbedtls_ssl_get_peer_cert( &ssl ) );
1483 mbedtls_printf( "%s\n", buf );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001484 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001488 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001489 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001490 /*
1491 * Perform renegotiation (this must be done when the server is waiting
1492 * for input from our side).
1493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001495 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001497 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001498 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001499 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
1500 ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001503 goto exit;
1504 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001505 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001507 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001509
Paul Bakker5121ce52009-01-03 21:22:43 +00001510 /*
1511 * 6. Write the GET request
1512 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001513 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001514send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001516 fflush( stdout );
1517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 len = mbedtls_snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST,
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001519 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001520 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001521
1522 /* Add padding to GET request to reach opt.request_size in length */
1523 if( opt.request_size != DFL_REQUEST_SIZE &&
1524 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001525 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001526 memset( buf + len, 'A', opt.request_size - len - tail_len );
1527 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001528 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001529
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001530 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof(buf) - len - 1 );
1531 len += tail_len;
1532
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001533 /* Truncate if request size is smaller than the "natural" size */
1534 if( opt.request_size != DFL_REQUEST_SIZE &&
1535 len > opt.request_size )
1536 {
1537 len = opt.request_size;
1538
1539 /* Still end with \r\n unless that's really not possible */
1540 if( len >= 2 ) buf[len - 2] = '\r';
1541 if( len >= 1 ) buf[len - 1] = '\n';
1542 }
1543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001545 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001546 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001549 <= 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001550 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001551 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001552 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
1553 ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001556 goto exit;
1557 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001558 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001559 }
1560 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001561 else /* Not stream, so datagram */
1562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 do ret = mbedtls_ssl_write( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001564 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001565 ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
1566 ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001567
1568 if( ret < 0 )
1569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001571 goto exit;
1572 }
1573
1574 frags = 1;
1575 written = ret;
1576 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001577
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001578 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001580
1581 /*
1582 * 7. Read the HTTP response
1583 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001585 fflush( stdout );
1586
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001587 /*
1588 * TLS and DTLS need different reading styles (stream vs datagram)
1589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001591 {
1592 do
1593 {
1594 len = sizeof( buf ) - 1;
1595 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001597
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001598 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001599 ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
1600 ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001601 continue;
1602
1603 if( ret <= 0 )
1604 {
1605 switch( ret )
1606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1608 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001609 ret = 0;
1610 goto close_notify;
1611
1612 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 case MBEDTLS_ERR_NET_CONN_RESET:
1614 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001615 ret = 0;
1616 goto reconnect;
1617
1618 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001620 goto exit;
1621 }
1622 }
1623
1624 len = ret;
1625 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001627
1628 /* End of message should be detected according to the syntax of the
1629 * application protocol (eg HTTP), just use a dummy test here. */
1630 if( ret > 0 && buf[len-1] == '\n' )
1631 {
1632 ret = 0;
1633 break;
1634 }
1635 }
1636 while( 1 );
1637 }
1638 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001639 {
1640 len = sizeof( buf ) - 1;
1641 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 do ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001644 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001645 ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
1646 ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
Paul Bakker5121ce52009-01-03 21:22:43 +00001647
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001648 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001649 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001650 switch( ret )
1651 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001652 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001654 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001655 goto send_request;
1656 goto exit;
1657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1659 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001660 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001661 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001663 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001665 goto exit;
1666 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001667 }
1668
Paul Bakker5121ce52009-01-03 21:22:43 +00001669 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02001670 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001672 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001673 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001674
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001675 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001676 * 7b. Simulate hard reset and reconnect from same port?
1677 */
1678 if( opt.reconnect_hard != 0 )
1679 {
1680 opt.reconnect_hard = 0;
1681
1682 mbedtls_printf( " . Restarting connection from same port..." );
1683 fflush( stdout );
1684
1685 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
1686 {
1687 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", -ret );
1688 goto exit;
1689 }
1690
1691 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
1692 {
1693 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001694 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
1695 ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001696 {
1697 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
1698 goto exit;
1699 }
1700 }
1701
1702 mbedtls_printf( " ok\n" );
1703
1704 goto send_request;
1705 }
1706
1707 /*
1708 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001709 */
1710 if( --opt.exchanges > 0 )
1711 goto send_request;
1712
1713 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001714 * 8. Done, cleanly close the connection
1715 */
1716close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001718 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001719
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001720 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001722 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001723 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001726
1727 /*
1728 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001729 */
1730reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001731 if( opt.reconnect != 0 )
1732 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001733 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001734
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001735 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001736
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001737#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001738 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02001739 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001740#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02001741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001747 goto exit;
1748 }
1749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001751 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001752 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001753 goto exit;
1754 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
1757 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1758 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001761 goto exit;
1762 }
1763
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001764 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001765 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001766 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001767 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001768 if( ret != 0 )
1769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001771 -ret );
1772 goto exit;
1773 }
1774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001776 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001777 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001778 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
1779 ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001782 goto exit;
1783 }
1784 }
1785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001787
1788 goto send_request;
1789 }
1790
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001791 /*
1792 * Cleanup and exit
1793 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001794exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00001796 if( ret != 0 )
1797 {
1798 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 mbedtls_strerror( ret, error_buf, 100 );
1800 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001801 }
1802#endif
1803
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001804 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806#if defined(MBEDTLS_X509_CRT_PARSE_C)
1807 mbedtls_x509_crt_free( &clicert );
1808 mbedtls_x509_crt_free( &cacert );
1809 mbedtls_pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001810#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 mbedtls_ssl_session_free( &saved_session );
1812 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001813 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 mbedtls_ctr_drbg_free( &ctr_drbg );
1815 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00001816
Paul Bakkercce9d772011-11-18 14:26:47 +00001817#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001819 fflush( stdout ); getchar();
1820#endif
1821
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001822 // Shell can not handle large exit numbers -> 1 for errors
1823 if( ret < 0 )
1824 ret = 1;
1825
Paul Bakker5121ce52009-01-03 21:22:43 +00001826 return( ret );
1827}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
1829 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001830 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */