blob: 81514321fff7269f0aa3494af0e4682021732466 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client with certificate authentication
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Simon Butcherd3138c32016-04-27 01:26:50 +010032#include <stdlib.h>
33#define mbedtls_time time
34#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#define mbedtls_printf printf
36#define mbedtls_fprintf fprintf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define mbedtls_snprintf snprintf
Rich Evansf90016a2015-01-19 14:26:37 +000038#endif
39
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020040#if !defined(MBEDTLS_ENTROPY_C) || \
41 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020042 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020043int main( void )
44{
45 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
46 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020047 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020048 return( 0 );
49}
50#else
51
Andres AG788aa4a2016-09-14 14:32:09 +010052#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/ssl.h"
54#include "mbedtls/entropy.h"
55#include "mbedtls/ctr_drbg.h"
56#include "mbedtls/certs.h"
57#include "mbedtls/x509.h"
58#include "mbedtls/error.h"
59#include "mbedtls/debug.h"
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020060#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Rich Evans18b78c72015-02-11 14:06:19 +000062#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010065
Hanno Beckere4ad3e82017-09-18 15:05:46 +010066#define MAX_REQUEST_SIZE 20000
67#define MAX_REQUEST_SIZE_STR "20000"
68
Paul Bakker9caf2d22010-02-18 19:37:19 +000069#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010070#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020071#define DFL_SERVER_PORT "4433"
Paul Bakker9caf2d22010-02-18 19:37:19 +000072#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +020073#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +000074#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010075#define DFL_NBIO 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020076#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +020077#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +000078#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000079#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000080#define DFL_CRT_FILE ""
81#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020082#define DFL_PSK ""
83#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +020084#define DFL_ECJPAKE_PW NULL
Paul Bakker51936882011-02-20 16:05:58 +000085#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010087#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010088#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +020089#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +020090#define DFL_MIN_VERSION -1
Paul Bakker1d29fb52012-09-28 13:28:45 +000091#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +000092#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +020093#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +010094#define DFL_AUTH_MODE -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +010096#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +010097#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +020098#define DFL_DHMLEN -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +020099#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100100#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200101#define DFL_RECONNECT_HARD 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200103#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100104#define DFL_CURVES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200106#define DFL_HS_TO_MIN 0
107#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200108#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200109#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100110#define DFL_ETM -1
Paul Bakker0e6975b2009-02-10 22:19:10 +0000111
Paul Bakker93c32b22014-04-25 13:40:05 +0200112#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
113#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#if defined(MBEDTLS_X509_CRT_PARSE_C)
116#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000117#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100118 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
119 " default: \"\" (pre-loaded)\n" \
120 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
121 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
122 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
123 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000124 " key_file=%%s default: \"\" (pre-loaded)\n"
125#else
126#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 " No file operations available (MBEDTLS_FS_IO not defined)\n"
128#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200129#else
130#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200134#define USAGE_PSK \
135 " psk=%%s default: \"\" (in hex, without 0x)\n" \
136 " psk_identity=%%s default: \"Client_identity\"\n"
137#else
138#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200142#define USAGE_TICKETS \
143 " tickets=%%d default: 1 (enabled)\n"
144#else
145#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200149#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100150 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200151#else
152#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200156#define USAGE_MAX_FRAG_LEN \
157 " max_frag_len=%%d default: 16384 (tls default)\n" \
158 " options: 512, 1024, 2048, 4096\n"
159#else
160#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100164#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200165 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100166#else
167#define USAGE_RECSPLIT
168#endif
169
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200170#if defined(MBEDTLS_DHM_C)
171#define USAGE_DHMLEN \
172 " dhmlen=%%d default: (library default: 1024 bits)\n"
173#else
174#define USAGE_DHMLEN
175#endif
176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200178#define USAGE_ALPN \
179 " alpn=%%s default: \"\" (disabled)\n" \
180 " example: spdy/1,http/1.1\n"
181#else
182#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200184
Hanno Beckere6706e62017-05-15 16:05:15 +0100185#if defined(MBEDTLS_ECP_C)
186#define USAGE_CURVES \
187 " curves=a,b,c,d default: \"default\" (library default)\n" \
188 " example: \"secp521r1,brainpoolP512r1\"\n" \
189 " - use \"none\" for empty list\n" \
190 " - see mbedtls_ecp_curve_list()\n" \
191 " for acceptable curve names\n"
192#else
193#define USAGE_CURVES ""
194#endif
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200197#define USAGE_DTLS \
198 " dtls=%%d default: 0 (TLS)\n" \
199 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
200 " range of DTLS handshake timeouts in millisecs\n"
201#else
202#define USAGE_DTLS ""
203#endif
204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200206#define USAGE_FALLBACK \
207 " fallback=0/1 default: (library default: off)\n"
208#else
209#define USAGE_FALLBACK ""
210#endif
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200213#define USAGE_EMS \
214 " extended_ms=0/1 default: (library default: on)\n"
215#else
216#define USAGE_EMS ""
217#endif
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100220#define USAGE_ETM \
221 " etm=0/1 default: (library default: on)\n"
222#else
223#define USAGE_ETM ""
224#endif
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100227#define USAGE_RENEGO \
228 " renegotiation=%%d default: 0 (disabled)\n" \
229 " renegotiate=%%d default: 0 (disabled)\n"
230#else
231#define USAGE_RENEGO ""
232#endif
233
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200234#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
235#define USAGE_ECJPAKE \
236 " ecjpake_pw=%%s default: none (disabled)\n"
237#else
238#define USAGE_ECJPAKE ""
239#endif
240
Paul Bakker9caf2d22010-02-18 19:37:19 +0000241#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000242 "\n usage: ssl_client2 param=<>...\n" \
243 "\n acceptable parameters:\n" \
244 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100245 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000246 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000247 " request_page=%%s default: \".\"\n" \
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100248 " request_size=%%d default: about 34 (basic request)\n" \
Andres Amaya Garcia46a6d5c2018-07-04 09:29:34 +0100249 " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
250 " If 0, in the first exchange only an empty\n" \
251 " application data message is sent followed by\n" \
252 " a second non-empty message before attempting\n" \
253 " to read a response from the server\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100254 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100255 " nbio=%%d default: 0 (blocking I/O)\n" \
256 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200257 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200258 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100259 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200260 USAGE_DTLS \
261 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100262 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100263 " options: none, optional, required\n" \
264 USAGE_IO \
265 "\n" \
266 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200267 USAGE_ECJPAKE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100268 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100269 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100270 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200271 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200272 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200273 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200274 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200275 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100276 USAGE_MAX_FRAG_LEN \
277 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200278 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200279 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200280 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100281 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100282 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100283 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200284 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000285 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100286 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200287 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200288 " min_version=%%s default: (library default: tls1)\n" \
289 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000290 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100291 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000292 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000293 " force_ciphersuite=<name> default: all enabled\n"\
294 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000295
Hanno Becker8651a432017-06-09 16:13:22 +0100296#define ALPN_LIST_SIZE 10
297#define CURVE_LIST_SIZE 20
298
Rich Evans85b05ec2015-02-12 11:37:29 +0000299/*
300 * global options
301 */
302struct options
303{
304 const char *server_name; /* hostname of the server (client only) */
305 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200306 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000307 int debug_level; /* level of debugging */
308 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000310 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000311 const char *request_page; /* page on server to request */
312 int request_size; /* pad request with header to requested size */
313 const char *ca_file; /* the file with the CA certificate(s) */
314 const char *ca_path; /* the path with the CA certificate(s) reside */
315 const char *crt_file; /* the file with the client certificate */
316 const char *key_file; /* the file with the client key */
317 const char *psk; /* the pre-shared key */
318 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200319 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000320 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
321 int renegotiation; /* enable / disable renegotiation */
322 int allow_legacy; /* allow legacy renegotiation */
323 int renegotiate; /* attempt renegotiation? */
324 int renego_delay; /* delay before enforcing renegotiation */
325 int exchanges; /* number of data exchanges */
326 int min_version; /* minimum protocol version accepted */
327 int max_version; /* maximum protocol version accepted */
328 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200329 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000330 int auth_mode; /* verify mode for connection */
331 unsigned char mfl_code; /* code for maximum fragment length */
332 int trunc_hmac; /* negotiate truncated hmac or not */
333 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200334 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000335 int reconnect; /* attempt to resume session */
336 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200337 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000338 int tickets; /* enable / disable session tickets */
Hanno Beckere6706e62017-05-15 16:05:15 +0100339 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000340 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000341 int transport; /* TLS or DTLS? */
342 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
343 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Rich Evans85b05ec2015-02-12 11:37:29 +0000344 int fallback; /* is this a fallback connection? */
345 int extended_ms; /* negotiate extended master secret? */
346 int etm; /* negotiate encrypt then mac? */
347} opt;
348
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200349static void my_debug( void *ctx, int level,
350 const char *file, int line,
351 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000352{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200353 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000354
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200355 /* Extract basename from file */
356 for( p = basename = file; *p != '\0'; p++ )
357 if( *p == '/' || *p == '\\' )
358 basename = p + 1;
359
360 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000361 fflush( (FILE *) ctx );
362}
363
364/*
365 * Test recv/send functions that make sure each try returns
366 * WANT_READ/WANT_WRITE at least once before sucesseding
367 */
368static int my_recv( void *ctx, unsigned char *buf, size_t len )
369{
370 static int first_try = 1;
371 int ret;
372
373 if( first_try )
374 {
375 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100376 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000377 }
378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100380 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000381 first_try = 1; /* Next call will be a new operation */
382 return( ret );
383}
384
385static int my_send( void *ctx, const unsigned char *buf, size_t len )
386{
387 static int first_try = 1;
388 int ret;
389
390 if( first_try )
391 {
392 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100393 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000394 }
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100397 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000398 first_try = 1; /* Next call will be a new operation */
399 return( ret );
400}
401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#if defined(MBEDTLS_X509_CRT_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000403/*
404 * Enabled if debug_level > 1 in code below
405 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200406static int my_verify( void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000407{
408 char buf[1024];
409 ((void) data);
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
412 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
413 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000414
Rich Evans85b05ec2015-02-12 11:37:29 +0000415 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100417 else
418 {
419 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
420 mbedtls_printf( "%s\n", buf );
421 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000422
423 return( 0 );
424}
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200425
426static int ssl_sig_hashes_for_test[] = {
427#if defined(MBEDTLS_SHA512_C)
428 MBEDTLS_MD_SHA512,
429 MBEDTLS_MD_SHA384,
430#endif
431#if defined(MBEDTLS_SHA256_C)
432 MBEDTLS_MD_SHA256,
433 MBEDTLS_MD_SHA224,
434#endif
435#if defined(MBEDTLS_SHA1_C)
436 /* Allow SHA-1 as we use it extensively in tests. */
437 MBEDTLS_MD_SHA1,
438#endif
439 MBEDTLS_MD_NONE
440};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000442
Paul Bakker9caf2d22010-02-18 19:37:19 +0000443int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000444{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200445 int ret = 0, len, tail_len, i, written, frags, retry_left;
446 mbedtls_net_context server_fd;
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100447
448 unsigned char buf[MAX_REQUEST_SIZE + 1];
449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
451 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200452 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200453#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100455 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200456#endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100457#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +0100458 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100459 const mbedtls_ecp_curve_info *curve_cur;
460#endif
461
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200462 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000463
Gilles Peskineef86ab22017-05-05 18:59:02 +0200464#if defined(MBEDTLS_X509_CRT_PARSE_C)
465 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
466#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_entropy_context entropy;
468 mbedtls_ctr_drbg_context ctr_drbg;
469 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200470 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200472#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200473 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200474#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200476 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_x509_crt cacert;
478 mbedtls_x509_crt clicert;
479 mbedtls_pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200480#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000481 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000482 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000483
Paul Bakker1a207ec2011-02-06 13:22:40 +0000484 /*
485 * Make sure memory references are valid.
486 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200487 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200488 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200489 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200491 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492#if defined(MBEDTLS_X509_CRT_PARSE_C)
493 mbedtls_x509_crt_init( &cacert );
494 mbedtls_x509_crt_init( &clicert );
495 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200496#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200498 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200499#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000500
Paul Bakker9caf2d22010-02-18 19:37:19 +0000501 if( argc == 0 )
502 {
503 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000504 if( ret == 0 )
505 ret = 1;
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000510 while( *list )
511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200513 list++;
514 if( !*list )
515 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000517 list++;
518 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000520 goto exit;
521 }
522
523 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100524 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000525 opt.server_port = DFL_SERVER_PORT;
526 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100527 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200528 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200529 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000530 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200531 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000532 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000533 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000534 opt.crt_file = DFL_CRT_FILE;
535 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200536 opt.psk = DFL_PSK;
537 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200538 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakker51936882011-02-20 16:05:58 +0000539 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000540 opt.renegotiation = DFL_RENEGOTIATION;
541 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100542 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200543 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000544 opt.min_version = DFL_MIN_VERSION;
545 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100546 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +0200547 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100548 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200549 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200550 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100551 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200552 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200553 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100554 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200555 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200556 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200557 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +0100558 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200559 opt.transport = DFL_TRANSPORT;
560 opt.hs_to_min = DFL_HS_TO_MIN;
561 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200562 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200563 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100564 opt.etm = DFL_ETM;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000565
566 for( i = 1; i < argc; i++ )
567 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000568 p = argv[i];
569 if( ( q = strchr( p, '=' ) ) == NULL )
570 goto usage;
571 *q++ = '\0';
572
573 if( strcmp( p, "server_name" ) == 0 )
574 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100575 else if( strcmp( p, "server_addr" ) == 0 )
576 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000577 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200578 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100579 else if( strcmp( p, "dtls" ) == 0 )
580 {
581 int t = atoi( q );
582 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100584 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100586 else
587 goto usage;
588 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000589 else if( strcmp( p, "debug_level" ) == 0 )
590 {
591 opt.debug_level = atoi( q );
592 if( opt.debug_level < 0 || opt.debug_level > 65535 )
593 goto usage;
594 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100595 else if( strcmp( p, "nbio" ) == 0 )
596 {
597 opt.nbio = atoi( q );
598 if( opt.nbio < 0 || opt.nbio > 2 )
599 goto usage;
600 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200601 else if( strcmp( p, "read_timeout" ) == 0 )
602 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200603 else if( strcmp( p, "max_resend" ) == 0 )
604 {
605 opt.max_resend = atoi( q );
606 if( opt.max_resend < 0 )
607 goto usage;
608 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000609 else if( strcmp( p, "request_page" ) == 0 )
610 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200611 else if( strcmp( p, "request_size" ) == 0 )
612 {
613 opt.request_size = atoi( q );
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100614 if( opt.request_size < 0 ||
615 opt.request_size > MAX_REQUEST_SIZE )
Paul Bakker93c32b22014-04-25 13:40:05 +0200616 goto usage;
617 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000618 else if( strcmp( p, "ca_file" ) == 0 )
619 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000620 else if( strcmp( p, "ca_path" ) == 0 )
621 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000622 else if( strcmp( p, "crt_file" ) == 0 )
623 opt.crt_file = q;
624 else if( strcmp( p, "key_file" ) == 0 )
625 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200626 else if( strcmp( p, "psk" ) == 0 )
627 opt.psk = q;
628 else if( strcmp( p, "psk_identity" ) == 0 )
629 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200630 else if( strcmp( p, "ecjpake_pw" ) == 0 )
631 opt.ecjpake_pw = q;
Paul Bakker51936882011-02-20 16:05:58 +0000632 else if( strcmp( p, "force_ciphersuite" ) == 0 )
633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +0000635
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200636 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000637 {
638 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000639 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000640 }
Paul Bakker51936882011-02-20 16:05:58 +0000641 opt.force_ciphersuite[1] = 0;
642 }
Paul Bakker48916f92012-09-16 19:57:18 +0000643 else if( strcmp( p, "renegotiation" ) == 0 )
644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 opt.renegotiation = (atoi( q )) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED :
646 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +0000647 }
648 else if( strcmp( p, "allow_legacy" ) == 0 )
649 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100650 switch( atoi( q ) )
651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 case -1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE; break;
653 case 0: opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; break;
654 case 1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION; break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100655 default: goto usage;
656 }
Paul Bakker48916f92012-09-16 19:57:18 +0000657 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100658 else if( strcmp( p, "renegotiate" ) == 0 )
659 {
660 opt.renegotiate = atoi( q );
661 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
662 goto usage;
663 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200664 else if( strcmp( p, "exchanges" ) == 0 )
665 {
666 opt.exchanges = atoi( q );
667 if( opt.exchanges < 1 )
668 goto usage;
669 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200670 else if( strcmp( p, "reconnect" ) == 0 )
671 {
672 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200673 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200674 goto usage;
675 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100676 else if( strcmp( p, "reco_delay" ) == 0 )
677 {
678 opt.reco_delay = atoi( q );
679 if( opt.reco_delay < 0 )
680 goto usage;
681 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200682 else if( strcmp( p, "reconnect_hard" ) == 0 )
683 {
684 opt.reconnect_hard = atoi( q );
685 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
686 goto usage;
687 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200688 else if( strcmp( p, "tickets" ) == 0 )
689 {
690 opt.tickets = atoi( q );
691 if( opt.tickets < 0 || opt.tickets > 2 )
692 goto usage;
693 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200694 else if( strcmp( p, "alpn" ) == 0 )
695 {
696 opt.alpn_string = q;
697 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200698 else if( strcmp( p, "fallback" ) == 0 )
699 {
700 switch( atoi( q ) )
701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
703 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200704 default: goto usage;
705 }
706 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200707 else if( strcmp( p, "extended_ms" ) == 0 )
708 {
709 switch( atoi( q ) )
710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 case 0: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED; break;
712 case 1: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200713 default: goto usage;
714 }
715 }
Hanno Beckere6706e62017-05-15 16:05:15 +0100716 else if( strcmp( p, "curves" ) == 0 )
717 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100718 else if( strcmp( p, "etm" ) == 0 )
719 {
720 switch( atoi( q ) )
721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
723 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100724 default: goto usage;
725 }
726 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000727 else if( strcmp( p, "min_version" ) == 0 )
728 {
729 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000731 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100733 else if( strcmp( q, "tls1_1" ) == 0 ||
734 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100736 else if( strcmp( q, "tls1_2" ) == 0 ||
737 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000739 else
740 goto usage;
741 }
742 else if( strcmp( p, "max_version" ) == 0 )
743 {
744 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000746 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100748 else if( strcmp( q, "tls1_1" ) == 0 ||
749 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100751 else if( strcmp( q, "tls1_2" ) == 0 ||
752 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000754 else
755 goto usage;
756 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100757 else if( strcmp( p, "arc4" ) == 0 )
758 {
759 switch( atoi( q ) )
760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
762 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100763 default: goto usage;
764 }
765 }
Gilles Peskinebc70a182017-05-09 15:59:24 +0200766 else if( strcmp( p, "allow_sha1" ) == 0 )
767 {
768 switch( atoi( q ) )
769 {
770 case 0: opt.allow_sha1 = 0; break;
771 case 1: opt.allow_sha1 = 1; break;
772 default: goto usage;
773 }
774 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000775 else if( strcmp( p, "force_version" ) == 0 )
776 {
777 if( strcmp( q, "ssl3" ) == 0 )
778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
780 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000781 }
782 else if( strcmp( q, "tls1" ) == 0 )
783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
785 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000786 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100787 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
790 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000791 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100792 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
795 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000796 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100797 else if( strcmp( q, "dtls1" ) == 0 )
798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
800 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
801 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100802 }
803 else if( strcmp( q, "dtls1_2" ) == 0 )
804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
806 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
807 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100808 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000809 else
810 goto usage;
811 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100812 else if( strcmp( p, "auth_mode" ) == 0 )
813 {
814 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100816 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100818 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100820 else
821 goto usage;
822 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200823 else if( strcmp( p, "max_frag_len" ) == 0 )
824 {
825 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200827 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200829 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200831 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200833 else
834 goto usage;
835 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200836 else if( strcmp( p, "trunc_hmac" ) == 0 )
837 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100838 switch( atoi( q ) )
839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
841 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100842 default: goto usage;
843 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200844 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200845 else if( strcmp( p, "hs_timeout" ) == 0 )
846 {
847 if( ( p = strchr( q, '-' ) ) == NULL )
848 goto usage;
849 *p++ = '\0';
850 opt.hs_to_min = atoi( q );
851 opt.hs_to_max = atoi( p );
852 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
853 goto usage;
854 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100855 else if( strcmp( p, "recsplit" ) == 0 )
856 {
857 opt.recsplit = atoi( q );
858 if( opt.recsplit < 0 || opt.recsplit > 1 )
859 goto usage;
860 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200861 else if( strcmp( p, "dhmlen" ) == 0 )
862 {
863 opt.dhmlen = atoi( q );
864 if( opt.dhmlen < 0 )
865 goto usage;
866 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000867 else
868 goto usage;
869 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#if defined(MBEDTLS_DEBUG_C)
872 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200873#endif
874
Paul Bakkerc1516be2013-06-29 16:01:32 +0200875 if( opt.force_ciphersuite[0] > 0 )
876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
878 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +0200879
Paul Bakker66c48102013-07-26 14:05:32 +0200880 if( opt.max_version != -1 &&
881 ciphersuite_info->min_minor_ver > opt.max_version )
882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakker66c48102013-07-26 14:05:32 +0200884 ret = 2;
885 goto usage;
886 }
887 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200888 ciphersuite_info->max_minor_ver < opt.min_version )
889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +0200891 ret = 2;
892 goto usage;
893 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100894
895 /* If the server selects a version that's not supported by
896 * this suite, then there will be no common ciphersuite... */
897 if( opt.max_version == -1 ||
898 opt.max_version > ciphersuite_info->max_minor_ver )
899 {
Paul Bakker66c48102013-07-26 14:05:32 +0200900 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100901 }
Paul Bakker66c48102013-07-26 14:05:32 +0200902 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100903 {
Paul Bakker66c48102013-07-26 14:05:32 +0200904 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100905 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
907 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
908 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100909 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000910
911 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000917 ret = 2;
918 goto usage;
919 }
920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000922 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200923 }
924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000926 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200927 * Unhexify the pre-shared key if any is given
928 */
929 if( strlen( opt.psk ) )
930 {
931 unsigned char c;
932 size_t j;
933
934 if( strlen( opt.psk ) % 2 != 0 )
935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200937 goto exit;
938 }
939
940 psk_len = strlen( opt.psk ) / 2;
941
942 for( j = 0; j < strlen( opt.psk ); j += 2 )
943 {
944 c = opt.psk[j];
945 if( c >= '0' && c <= '9' )
946 c -= '0';
947 else if( c >= 'a' && c <= 'f' )
948 c -= 'a' - 10;
949 else if( c >= 'A' && c <= 'F' )
950 c -= 'A' - 10;
951 else
952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200954 goto exit;
955 }
956 psk[ j / 2 ] = c << 4;
957
958 c = opt.psk[j + 1];
959 if( c >= '0' && c <= '9' )
960 c -= '0';
961 else if( c >= 'a' && c <= 'f' )
962 c -= 'a' - 10;
963 else if( c >= 'A' && c <= 'F' )
964 c -= 'A' - 10;
965 else
966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200968 goto exit;
969 }
970 psk[ j / 2 ] |= c;
971 }
972 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200974
Hanno Beckere6706e62017-05-15 16:05:15 +0100975#if defined(MBEDTLS_ECP_C)
976 if( opt.curves != NULL )
977 {
978 p = (char *) opt.curves;
979 i = 0;
980
981 if( strcmp( p, "none" ) == 0 )
982 {
983 curve_list[0] = MBEDTLS_ECP_DP_NONE;
984 }
985 else if( strcmp( p, "default" ) != 0 )
986 {
987 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +0100988 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +0100989 {
990 q = p;
991
992 /* Terminate the current string */
993 while( *p != ',' && *p != '\0' )
994 p++;
995 if( *p == ',' )
996 *p++ = '\0';
997
998 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
999 {
1000 curve_list[i++] = curve_cur->grp_id;
1001 }
1002 else
1003 {
1004 mbedtls_printf( "unknown curve %s\n", q );
1005 mbedtls_printf( "supported curves: " );
1006 for( curve_cur = mbedtls_ecp_curve_list();
1007 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1008 curve_cur++ )
1009 {
1010 mbedtls_printf( "%s ", curve_cur->name );
1011 }
1012 mbedtls_printf( "\n" );
1013 goto exit;
1014 }
1015 }
1016
1017 mbedtls_printf("Number of curves: %d\n", i );
1018
Hanno Becker8651a432017-06-09 16:13:22 +01001019 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001020 {
Hanno Becker8651a432017-06-09 16:13:22 +01001021 mbedtls_printf( "curves list too long, maximum %d",
1022 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001023 goto exit;
1024 }
1025
1026 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1027 }
1028 }
1029#endif /* MBEDTLS_ECP_C */
1030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001032 if( opt.alpn_string != NULL )
1033 {
1034 p = (char *) opt.alpn_string;
1035 i = 0;
1036
1037 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001038 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001039 {
1040 alpn_list[i++] = p;
1041
1042 /* Terminate the current string and move on to next one */
1043 while( *p != ',' && *p != '\0' )
1044 p++;
1045 if( *p == ',' )
1046 *p++ = '\0';
1047 }
1048 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001050
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001051 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001052 * 0. Initialize the RNG and the session data
1053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001055 fflush( stdout );
1056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001058 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001059 (const unsigned char *) pers,
1060 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +00001061 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001062 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n", -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001063 goto exit;
1064 }
1065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001069 /*
1070 * 1.1. Load the trusted CA
1071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001073 fflush( stdout );
1074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001076 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001077 if( strcmp( opt.ca_path, "none" ) == 0 )
1078 ret = 0;
1079 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001081 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001082 if( strcmp( opt.ca_file, "none" ) == 0 )
1083 ret = 0;
1084 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001086 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#if defined(MBEDTLS_CERTS_C)
1089 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 ret = mbedtls_x509_crt_parse( &cacert,
1092 (const unsigned char *) mbedtls_test_cas[i],
1093 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001094 if( ret != 0 )
1095 break;
1096 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001097#else
1098 {
1099 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001101 }
1102#endif
Paul Bakker42488232012-05-16 08:21:05 +00001103 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001106 goto exit;
1107 }
1108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001110
1111 /*
1112 * 1.2. Load own certificate and private key
1113 *
1114 * (can be skipped if client authentication is not required)
1115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 fflush( stdout );
1118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001120 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001121 if( strcmp( opt.crt_file, "none" ) == 0 )
1122 ret = 0;
1123 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001125 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001126#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#if defined(MBEDTLS_CERTS_C)
1128 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
1129 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001130#else
1131 {
1132 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001134 }
1135#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 if( ret != 0 )
1137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001139 goto exit;
1140 }
1141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001143 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001144 if( strcmp( opt.key_file, "none" ) == 0 )
1145 ret = 0;
1146 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001148 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001149#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150#if defined(MBEDTLS_CERTS_C)
1151 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_cli_key,
1152 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001153#else
1154 {
1155 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001157 }
1158#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001159 if( ret != 0 )
1160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 goto exit;
1163 }
1164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 mbedtls_printf( " ok\n" );
1166#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001167
1168 /*
1169 * 2. Start the connection
1170 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001171 if( opt.server_addr == NULL)
1172 opt.server_addr = opt.server_name;
1173
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001174 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001176 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001177 fflush( stdout );
1178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
1180 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1181 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 goto exit;
1185 }
1186
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001187 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001188 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001189 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001190 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001191 if( ret != 0 )
1192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001194 goto exit;
1195 }
1196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001198
1199 /*
1200 * 3. Setup stuff
1201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 fflush( stdout );
1204
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001205 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1206 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001207 opt.transport,
1208 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001209 {
1210 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
1211 goto exit;
1212 }
1213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001215 /* The default algorithms profile disables SHA-1, but our tests still
1216 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001217 if( opt.allow_sha1 > 0 )
1218 {
1219 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1220 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1221 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1222 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001223
Paul Bakker915275b2012-09-28 07:10:55 +00001224 if( opt.debug_level > 0 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001225 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
Gilles Peskineef86ab22017-05-05 18:59:02 +02001226#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001227
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001228 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001229 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001232 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 +02001233 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001237 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001238 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001239 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001240 goto exit;
1241 }
Paul Bakker05decb22013-08-15 13:33:48 +02001242#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001245 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001246 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001247#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001250 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001251 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001252#endif
1253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001255 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001256 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001257#endif
1258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001260 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001261 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1263 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001264#endif
1265
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001266#if defined(MBEDTLS_DHM_C)
1267 if( opt.dhmlen != DFL_DHMLEN )
1268 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1269#endif
1270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001272 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001273 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001274 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001275 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001276 goto exit;
1277 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001278#endif
1279
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001280 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1281 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001282
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001283 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02001286 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02001287#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001288
Paul Bakker645ce3a2012-10-31 12:32:41 +00001289 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001290 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001291
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001292#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001293 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001294 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001295#endif
Paul Bakker51936882011-02-20 16:05:58 +00001296
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001297 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001298 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001300 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001301#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001304 if( strcmp( opt.ca_path, "none" ) != 0 &&
1305 strcmp( opt.ca_file, "none" ) != 0 )
1306 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001307 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001308 }
1309 if( strcmp( opt.crt_file, "none" ) != 0 &&
1310 strcmp( opt.key_file, "none" ) != 0 )
1311 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001312 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001313 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001314 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001315 goto exit;
1316 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001317 }
Paul Bakkered27a042013-04-18 22:46:23 +02001318#endif
1319
Hanno Beckere6706e62017-05-15 16:05:15 +01001320#if defined(MBEDTLS_ECP_C)
1321 if( opt.curves != NULL &&
1322 strcmp( opt.curves, "default" ) != 0 )
1323 {
1324 mbedtls_ssl_conf_curves( &conf, curve_list );
1325 }
1326#endif
1327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001329 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001330 (const unsigned char *) opt.psk_identity,
1331 strlen( opt.psk_identity ) ) ) != 0 )
1332 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001333 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001334 goto exit;
1335 }
Paul Bakkered27a042013-04-18 22:46:23 +02001336#endif
1337
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001338 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001339 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001340
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001341 if( opt.max_version != DFL_MAX_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001342 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001345 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001346 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001347#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001349 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
1350 {
1351 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
1352 goto exit;
1353 }
1354
1355#if defined(MBEDTLS_X509_CRT_PARSE_C)
1356 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1357 {
1358 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
1359 goto exit;
1360 }
1361#endif
1362
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001363#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1364 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
1365 {
1366 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
1367 (const unsigned char *) opt.ecjpake_pw,
1368 strlen( opt.ecjpake_pw ) ) ) != 0 )
1369 {
1370 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
1371 goto exit;
1372 }
1373 }
1374#endif
1375
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001376 if( opt.nbio == 2 )
1377 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
1378 else
1379 mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02001380 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001381
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001382#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001383 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
1384 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001385#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001388
Paul Bakker5121ce52009-01-03 21:22:43 +00001389 /*
1390 * 4. Handshake
1391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001393 fflush( stdout );
1394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001397 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker40e46942009-01-03 21:51:57 +00001398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n", -ret );
1400 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
1401 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001402 " Unable to verify the server's certificate. "
1403 "Either it is invalid,\n"
1404 " or you didn't set ca_file or ca_path "
1405 "to an appropriate value.\n"
1406 " Alternatively, you may want to use "
1407 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00001410 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001411 }
1412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1414 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
1417 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001418 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001420
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02001421#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1422 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
1423 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
1424#endif
1425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001427 if( opt.alpn_string != NULL )
1428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
1430 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001431 alp ? alp : "(none)" );
1432 }
1433#endif
1434
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001435 if( opt.reconnect != 0 )
1436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001438 fflush( stdout );
1439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001443 goto exit;
1444 }
1445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001447 }
1448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001450 /*
1451 * 5. Verify the server certificate
1452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001454
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001455 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001457 char vrfy_buf[512];
1458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001460
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001461 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00001462
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001463 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001464 }
1465 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 mbedtls_printf( " . Peer certificate information ...\n" );
1471 mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1472 mbedtls_ssl_get_peer_cert( &ssl ) );
1473 mbedtls_printf( "%s\n", buf );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001474 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001478 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001479 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001480 /*
1481 * Perform renegotiation (this must be done when the server is waiting
1482 * for input from our side).
1483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001485 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001487 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001488 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1489 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001492 goto exit;
1493 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001494 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001496 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001498
Paul Bakker5121ce52009-01-03 21:22:43 +00001499 /*
1500 * 6. Write the GET request
1501 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001502 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001503send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 fflush( stdout );
1506
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001507 len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST,
1508 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001509 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001510
1511 /* Add padding to GET request to reach opt.request_size in length */
1512 if( opt.request_size != DFL_REQUEST_SIZE &&
1513 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001514 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001515 memset( buf + len, 'A', opt.request_size - len - tail_len );
1516 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001517 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001518
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001519 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001520 len += tail_len;
1521
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001522 /* Truncate if request size is smaller than the "natural" size */
1523 if( opt.request_size != DFL_REQUEST_SIZE &&
1524 len > opt.request_size )
1525 {
1526 len = opt.request_size;
1527
1528 /* Still end with \r\n unless that's really not possible */
1529 if( len >= 2 ) buf[len - 2] = '\r';
1530 if( len >= 1 ) buf[len - 1] = '\n';
1531 }
1532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001534 {
Andres Amaya Garcia46a6d5c2018-07-04 09:29:34 +01001535 written = 0;
1536 frags = 0;
1537
1538 do
Paul Bakker5121ce52009-01-03 21:22:43 +00001539 {
Andres Amaya Garcia46a6d5c2018-07-04 09:29:34 +01001540 while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
1541 len - written ) ) < 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001542 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001543 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1544 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001547 goto exit;
1548 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001549 }
Andres Amaya Garcia46a6d5c2018-07-04 09:29:34 +01001550
1551 frags++;
1552 written += ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001553 }
Andres Amaya Garcia46a6d5c2018-07-04 09:29:34 +01001554 while( written < len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001555 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001556 else /* Not stream, so datagram */
1557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 do ret = mbedtls_ssl_write( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001559 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1560 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001561
1562 if( ret < 0 )
1563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001565 goto exit;
1566 }
1567
1568 frags = 1;
1569 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001570
1571 if( written < len )
1572 {
1573 mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
1574 "was truncated to size %u", (unsigned) written );
1575 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001576 }
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
Andres Amaya Garcia46a6d5c2018-07-04 09:29:34 +01001581 /* Send a non-empty request if request_size == 0 */
1582 if ( len == 0 )
1583 {
1584 opt.request_size = DFL_REQUEST_SIZE;
1585 goto send_request;
1586 }
1587
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 /*
1589 * 7. Read the HTTP response
1590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001592 fflush( stdout );
1593
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001594 /*
1595 * TLS and DTLS need different reading styles (stream vs datagram)
1596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001598 {
1599 do
1600 {
1601 len = sizeof( buf ) - 1;
1602 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001604
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001605 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1606 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001607 continue;
1608
1609 if( ret <= 0 )
1610 {
1611 switch( ret )
1612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1614 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001615 ret = 0;
1616 goto close_notify;
1617
1618 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 case MBEDTLS_ERR_NET_CONN_RESET:
1620 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001621 ret = 0;
1622 goto reconnect;
1623
1624 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001626 goto exit;
1627 }
1628 }
1629
1630 len = ret;
1631 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001633
1634 /* End of message should be detected according to the syntax of the
1635 * application protocol (eg HTTP), just use a dummy test here. */
1636 if( ret > 0 && buf[len-1] == '\n' )
1637 {
1638 ret = 0;
1639 break;
1640 }
1641 }
1642 while( 1 );
1643 }
1644 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001645 {
1646 len = sizeof( buf ) - 1;
1647 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 do ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001650 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1651 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001652
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001653 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001654 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001655 switch( ret )
1656 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001657 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001659 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001660 goto send_request;
1661 goto exit;
1662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1664 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001665 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001666 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001668 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001670 goto exit;
1671 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001672 }
1673
Paul Bakker5121ce52009-01-03 21:22:43 +00001674 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02001675 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001677 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001678 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001679
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001680 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001681 * 7b. Simulate hard reset and reconnect from same port?
1682 */
1683 if( opt.reconnect_hard != 0 )
1684 {
1685 opt.reconnect_hard = 0;
1686
1687 mbedtls_printf( " . Restarting connection from same port..." );
1688 fflush( stdout );
1689
1690 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
1691 {
1692 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", -ret );
1693 goto exit;
1694 }
1695
1696 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
1697 {
1698 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1699 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1700 {
1701 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
1702 goto exit;
1703 }
1704 }
1705
1706 mbedtls_printf( " ok\n" );
1707
1708 goto send_request;
1709 }
1710
1711 /*
1712 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001713 */
1714 if( --opt.exchanges > 0 )
1715 goto send_request;
1716
1717 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001718 * 8. Done, cleanly close the connection
1719 */
1720close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001722 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001723
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001724 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001726 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001727 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001730
1731 /*
1732 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001733 */
1734reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001735 if( opt.reconnect != 0 )
1736 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001737 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001738
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001739 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001740
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001741#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001742 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02001743 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001744#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02001745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001751 goto exit;
1752 }
1753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001755 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001756 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001757 goto exit;
1758 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
1761 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1762 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001765 goto exit;
1766 }
1767
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001768 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001769 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001770 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001771 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001772 if( ret != 0 )
1773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001775 -ret );
1776 goto exit;
1777 }
1778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001780 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001781 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1782 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001785 goto exit;
1786 }
1787 }
1788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001790
1791 goto send_request;
1792 }
1793
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001794 /*
1795 * Cleanup and exit
1796 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001797exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00001799 if( ret != 0 )
1800 {
1801 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 mbedtls_strerror( ret, error_buf, 100 );
1803 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001804 }
1805#endif
1806
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001807 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809#if defined(MBEDTLS_X509_CRT_PARSE_C)
1810 mbedtls_x509_crt_free( &clicert );
1811 mbedtls_x509_crt_free( &cacert );
1812 mbedtls_pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001813#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 mbedtls_ssl_session_free( &saved_session );
1815 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001816 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 mbedtls_ctr_drbg_free( &ctr_drbg );
1818 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00001819
Paul Bakkercce9d772011-11-18 14:26:47 +00001820#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001822 fflush( stdout ); getchar();
1823#endif
1824
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001825 // Shell can not handle large exit numbers -> 1 for errors
1826 if( ret < 0 )
1827 ret = 1;
1828
Paul Bakker5121ce52009-01-03 21:22:43 +00001829 return( ret );
1830}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
1832 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001833 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */