blob: 27284fc5ab77f11dcd296788f5a1616a19a63f32 [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>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_printf printf
33#define mbedtls_fprintf fprintf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#define mbedtls_snprintf snprintf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020037#if !defined(MBEDTLS_ENTROPY_C) || \
38 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020039 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020040int main( void )
41{
42 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
43 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020044 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020045 return( 0 );
46}
47#else
48
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/net.h"
50#include "mbedtls/ssl.h"
51#include "mbedtls/entropy.h"
52#include "mbedtls/ctr_drbg.h"
53#include "mbedtls/certs.h"
54#include "mbedtls/x509.h"
55#include "mbedtls/error.h"
56#include "mbedtls/debug.h"
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020057#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Rich Evans18b78c72015-02-11 14:06:19 +000059#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010062
Paul Bakker9caf2d22010-02-18 19:37:19 +000063#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010064#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020065#define DFL_SERVER_PORT "4433"
Paul Bakker9caf2d22010-02-18 19:37:19 +000066#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +020067#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +000068#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010069#define DFL_NBIO 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020070#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +020071#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +000072#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000073#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000074#define DFL_CRT_FILE ""
75#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020076#define DFL_PSK ""
77#define DFL_PSK_IDENTITY "Client_identity"
Paul Bakker51936882011-02-20 16:05:58 +000078#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010080#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010081#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +020082#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +020083#define DFL_MIN_VERSION -1
Paul Bakker1d29fb52012-09-28 13:28:45 +000084#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +000085#define DFL_ARC4 -1
Gilles Peskineae765992017-05-09 15:59:24 +020086#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +010087#define DFL_AUTH_MODE -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +010089#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +010090#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +020091#define DFL_DHMLEN -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +020092#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010093#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +020094#define DFL_RECONNECT_HARD 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +020096#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +020098#define DFL_HS_TO_MIN 0
99#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200100#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200101#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100102#define DFL_ETM -1
Paul Bakker0e6975b2009-02-10 22:19:10 +0000103
Paul Bakker93c32b22014-04-25 13:40:05 +0200104#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
105#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#if defined(MBEDTLS_X509_CRT_PARSE_C)
108#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000109#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100110 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
111 " default: \"\" (pre-loaded)\n" \
112 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
113 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
114 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
115 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000116 " key_file=%%s default: \"\" (pre-loaded)\n"
117#else
118#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 " No file operations available (MBEDTLS_FS_IO not defined)\n"
120#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200121#else
122#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200126#define USAGE_PSK \
127 " psk=%%s default: \"\" (in hex, without 0x)\n" \
128 " psk_identity=%%s default: \"Client_identity\"\n"
129#else
130#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200134#define USAGE_TICKETS \
135 " tickets=%%d default: 1 (enabled)\n"
136#else
137#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200141#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100142 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200143#else
144#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200148#define USAGE_MAX_FRAG_LEN \
149 " max_frag_len=%%d default: 16384 (tls default)\n" \
150 " options: 512, 1024, 2048, 4096\n"
151#else
152#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100156#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200157 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100158#else
159#define USAGE_RECSPLIT
160#endif
161
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200162#if defined(MBEDTLS_DHM_C)
163#define USAGE_DHMLEN \
164 " dhmlen=%%d default: (library default: 1024 bits)\n"
165#else
166#define USAGE_DHMLEN
167#endif
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200170#define USAGE_ALPN \
171 " alpn=%%s default: \"\" (disabled)\n" \
172 " example: spdy/1,http/1.1\n"
173#else
174#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200178#define USAGE_DTLS \
179 " dtls=%%d default: 0 (TLS)\n" \
180 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
181 " range of DTLS handshake timeouts in millisecs\n"
182#else
183#define USAGE_DTLS ""
184#endif
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200187#define USAGE_FALLBACK \
188 " fallback=0/1 default: (library default: off)\n"
189#else
190#define USAGE_FALLBACK ""
191#endif
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200194#define USAGE_EMS \
195 " extended_ms=0/1 default: (library default: on)\n"
196#else
197#define USAGE_EMS ""
198#endif
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100201#define USAGE_ETM \
202 " etm=0/1 default: (library default: on)\n"
203#else
204#define USAGE_ETM ""
205#endif
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100208#define USAGE_RENEGO \
209 " renegotiation=%%d default: 0 (disabled)\n" \
210 " renegotiate=%%d default: 0 (disabled)\n"
211#else
212#define USAGE_RENEGO ""
213#endif
214
Paul Bakker9caf2d22010-02-18 19:37:19 +0000215#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000216 "\n usage: ssl_client2 param=<>...\n" \
217 "\n acceptable parameters:\n" \
218 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100219 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000220 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000221 " request_page=%%s default: \".\"\n" \
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +0200222 " request_size=%%d default: about 34 (basic request)\n" \
223 " (minimum: 0, max: 16384)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100224 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100225 " nbio=%%d default: 0 (blocking I/O)\n" \
226 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200227 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200228 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100229 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200230 USAGE_DTLS \
231 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100232 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100233 " options: none, optional, required\n" \
234 USAGE_IO \
235 "\n" \
236 USAGE_PSK \
237 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100238 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100239 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200240 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200241 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200242 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200243 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200244 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100245 USAGE_MAX_FRAG_LEN \
246 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200247 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200248 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200249 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100250 USAGE_ETM \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100251 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200252 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000253 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100254 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskineae765992017-05-09 15:59:24 +0200255 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200256 " min_version=%%s default: (library default: tls1)\n" \
257 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000258 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100259 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000260 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000261 " force_ciphersuite=<name> default: all enabled\n"\
262 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000263
Rich Evans85b05ec2015-02-12 11:37:29 +0000264/*
265 * global options
266 */
267struct options
268{
269 const char *server_name; /* hostname of the server (client only) */
270 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200271 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000272 int debug_level; /* level of debugging */
273 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000275 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000276 const char *request_page; /* page on server to request */
277 int request_size; /* pad request with header to requested size */
278 const char *ca_file; /* the file with the CA certificate(s) */
279 const char *ca_path; /* the path with the CA certificate(s) reside */
280 const char *crt_file; /* the file with the client certificate */
281 const char *key_file; /* the file with the client key */
282 const char *psk; /* the pre-shared key */
283 const char *psk_identity; /* the pre-shared key identity */
284 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
285 int renegotiation; /* enable / disable renegotiation */
286 int allow_legacy; /* allow legacy renegotiation */
287 int renegotiate; /* attempt renegotiation? */
288 int renego_delay; /* delay before enforcing renegotiation */
289 int exchanges; /* number of data exchanges */
290 int min_version; /* minimum protocol version accepted */
291 int max_version; /* maximum protocol version accepted */
292 int arc4; /* flag for arc4 suites support */
Gilles Peskineae765992017-05-09 15:59:24 +0200293 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000294 int auth_mode; /* verify mode for connection */
295 unsigned char mfl_code; /* code for maximum fragment length */
296 int trunc_hmac; /* negotiate truncated hmac or not */
297 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200298 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000299 int reconnect; /* attempt to resume session */
300 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200301 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000302 int tickets; /* enable / disable session tickets */
303 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000304 int transport; /* TLS or DTLS? */
305 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
306 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Rich Evans85b05ec2015-02-12 11:37:29 +0000307 int fallback; /* is this a fallback connection? */
308 int extended_ms; /* negotiate extended master secret? */
309 int etm; /* negotiate encrypt then mac? */
310} opt;
311
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200312static void my_debug( void *ctx, int level,
313 const char *file, int line,
314 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000315{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200316 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000317
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200318 /* Extract basename from file */
319 for( p = basename = file; *p != '\0'; p++ )
320 if( *p == '/' || *p == '\\' )
321 basename = p + 1;
322
323 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000324 fflush( (FILE *) ctx );
325}
326
327/*
328 * Test recv/send functions that make sure each try returns
329 * WANT_READ/WANT_WRITE at least once before sucesseding
330 */
331static int my_recv( void *ctx, unsigned char *buf, size_t len )
332{
333 static int first_try = 1;
334 int ret;
335
336 if( first_try )
337 {
338 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100339 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000340 }
341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100343 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000344 first_try = 1; /* Next call will be a new operation */
345 return( ret );
346}
347
348static int my_send( void *ctx, const unsigned char *buf, size_t len )
349{
350 static int first_try = 1;
351 int ret;
352
353 if( first_try )
354 {
355 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100356 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000357 }
358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100360 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000361 first_try = 1; /* Next call will be a new operation */
362 return( ret );
363}
364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#if defined(MBEDTLS_X509_CRT_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000366/*
367 * Enabled if debug_level > 1 in code below
368 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200369static int my_verify( void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000370{
371 char buf[1024];
372 ((void) data);
373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
375 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
376 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000377
Rich Evans85b05ec2015-02-12 11:37:29 +0000378 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100380 else
381 {
382 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
383 mbedtls_printf( "%s\n", buf );
384 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000385
386 return( 0 );
387}
Gilles Peskine12c19542017-05-09 14:57:45 +0200388
389static int ssl_sig_hashes_for_test[] = {
390#if defined(MBEDTLS_SHA512_C)
391 MBEDTLS_MD_SHA512,
392 MBEDTLS_MD_SHA384,
393#endif
394#if defined(MBEDTLS_SHA256_C)
395 MBEDTLS_MD_SHA256,
396 MBEDTLS_MD_SHA224,
397#endif
398#if defined(MBEDTLS_SHA1_C)
399 /* Allow SHA-1 as we use it extensively in tests. */
400 MBEDTLS_MD_SHA1,
401#endif
402 MBEDTLS_MD_NONE
403};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000405
Paul Bakker9caf2d22010-02-18 19:37:19 +0000406int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000407{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200408 int ret = 0, len, tail_len, i, written, frags, retry_left;
409 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 unsigned char buf[MBEDTLS_SSL_MAX_CONTENT_LEN + 1];
411#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
412 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200413 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200414#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200416 const char *alpn_list[10];
417#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200418 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000419
Gilles Peskinedd57d752017-05-05 18:59:02 +0200420#if defined(MBEDTLS_X509_CRT_PARSE_C)
421 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
422#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_entropy_context entropy;
424 mbedtls_ctr_drbg_context ctr_drbg;
425 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200426 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200428#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200429 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200430#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200432 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_x509_crt cacert;
434 mbedtls_x509_crt clicert;
435 mbedtls_pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200436#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000437 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000438 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000439
Paul Bakker1a207ec2011-02-06 13:22:40 +0000440 /*
441 * Make sure memory references are valid.
442 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200443 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200444 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200445 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200447 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448#if defined(MBEDTLS_X509_CRT_PARSE_C)
449 mbedtls_x509_crt_init( &cacert );
450 mbedtls_x509_crt_init( &clicert );
451 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200452#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200454 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200455#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000456
Paul Bakker9caf2d22010-02-18 19:37:19 +0000457 if( argc == 0 )
458 {
459 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000460 if( ret == 0 )
461 ret = 1;
462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000466 while( *list )
467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200469 list++;
470 if( !*list )
471 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000473 list++;
474 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000476 goto exit;
477 }
478
479 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100480 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000481 opt.server_port = DFL_SERVER_PORT;
482 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100483 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200484 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200485 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000486 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200487 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000488 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000489 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000490 opt.crt_file = DFL_CRT_FILE;
491 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200492 opt.psk = DFL_PSK;
493 opt.psk_identity = DFL_PSK_IDENTITY;
Paul Bakker51936882011-02-20 16:05:58 +0000494 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000495 opt.renegotiation = DFL_RENEGOTIATION;
496 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100497 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200498 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000499 opt.min_version = DFL_MIN_VERSION;
500 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100501 opt.arc4 = DFL_ARC4;
Gilles Peskineae765992017-05-09 15:59:24 +0200502 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100503 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200504 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200505 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100506 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200507 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200508 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100509 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200510 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200511 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200512 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200513 opt.transport = DFL_TRANSPORT;
514 opt.hs_to_min = DFL_HS_TO_MIN;
515 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200516 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200517 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100518 opt.etm = DFL_ETM;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000519
520 for( i = 1; i < argc; i++ )
521 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000522 p = argv[i];
523 if( ( q = strchr( p, '=' ) ) == NULL )
524 goto usage;
525 *q++ = '\0';
526
527 if( strcmp( p, "server_name" ) == 0 )
528 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100529 else if( strcmp( p, "server_addr" ) == 0 )
530 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000531 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200532 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100533 else if( strcmp( p, "dtls" ) == 0 )
534 {
535 int t = atoi( q );
536 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100538 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100540 else
541 goto usage;
542 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000543 else if( strcmp( p, "debug_level" ) == 0 )
544 {
545 opt.debug_level = atoi( q );
546 if( opt.debug_level < 0 || opt.debug_level > 65535 )
547 goto usage;
548 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100549 else if( strcmp( p, "nbio" ) == 0 )
550 {
551 opt.nbio = atoi( q );
552 if( opt.nbio < 0 || opt.nbio > 2 )
553 goto usage;
554 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200555 else if( strcmp( p, "read_timeout" ) == 0 )
556 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200557 else if( strcmp( p, "max_resend" ) == 0 )
558 {
559 opt.max_resend = atoi( q );
560 if( opt.max_resend < 0 )
561 goto usage;
562 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000563 else if( strcmp( p, "request_page" ) == 0 )
564 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200565 else if( strcmp( p, "request_size" ) == 0 )
566 {
567 opt.request_size = atoi( q );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 if( opt.request_size < 0 || opt.request_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker93c32b22014-04-25 13:40:05 +0200569 goto usage;
570 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000571 else if( strcmp( p, "ca_file" ) == 0 )
572 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000573 else if( strcmp( p, "ca_path" ) == 0 )
574 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000575 else if( strcmp( p, "crt_file" ) == 0 )
576 opt.crt_file = q;
577 else if( strcmp( p, "key_file" ) == 0 )
578 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200579 else if( strcmp( p, "psk" ) == 0 )
580 opt.psk = q;
581 else if( strcmp( p, "psk_identity" ) == 0 )
582 opt.psk_identity = q;
Paul Bakker51936882011-02-20 16:05:58 +0000583 else if( strcmp( p, "force_ciphersuite" ) == 0 )
584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +0000586
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200587 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000588 {
589 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000590 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000591 }
Paul Bakker51936882011-02-20 16:05:58 +0000592 opt.force_ciphersuite[1] = 0;
593 }
Paul Bakker48916f92012-09-16 19:57:18 +0000594 else if( strcmp( p, "renegotiation" ) == 0 )
595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 opt.renegotiation = (atoi( q )) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED :
597 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +0000598 }
599 else if( strcmp( p, "allow_legacy" ) == 0 )
600 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100601 switch( atoi( q ) )
602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 case -1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE; break;
604 case 0: opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; break;
605 case 1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION; break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100606 default: goto usage;
607 }
Paul Bakker48916f92012-09-16 19:57:18 +0000608 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100609 else if( strcmp( p, "renegotiate" ) == 0 )
610 {
611 opt.renegotiate = atoi( q );
612 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
613 goto usage;
614 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200615 else if( strcmp( p, "exchanges" ) == 0 )
616 {
617 opt.exchanges = atoi( q );
618 if( opt.exchanges < 1 )
619 goto usage;
620 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200621 else if( strcmp( p, "reconnect" ) == 0 )
622 {
623 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200624 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200625 goto usage;
626 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100627 else if( strcmp( p, "reco_delay" ) == 0 )
628 {
629 opt.reco_delay = atoi( q );
630 if( opt.reco_delay < 0 )
631 goto usage;
632 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200633 else if( strcmp( p, "reconnect_hard" ) == 0 )
634 {
635 opt.reconnect_hard = atoi( q );
636 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
637 goto usage;
638 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200639 else if( strcmp( p, "tickets" ) == 0 )
640 {
641 opt.tickets = atoi( q );
642 if( opt.tickets < 0 || opt.tickets > 2 )
643 goto usage;
644 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200645 else if( strcmp( p, "alpn" ) == 0 )
646 {
647 opt.alpn_string = q;
648 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200649 else if( strcmp( p, "fallback" ) == 0 )
650 {
651 switch( atoi( q ) )
652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
654 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200655 default: goto usage;
656 }
657 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200658 else if( strcmp( p, "extended_ms" ) == 0 )
659 {
660 switch( atoi( q ) )
661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 case 0: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED; break;
663 case 1: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200664 default: goto usage;
665 }
666 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100667 else if( strcmp( p, "etm" ) == 0 )
668 {
669 switch( atoi( q ) )
670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
672 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100673 default: goto usage;
674 }
675 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000676 else if( strcmp( p, "min_version" ) == 0 )
677 {
678 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000680 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100682 else if( strcmp( q, "tls1_1" ) == 0 ||
683 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100685 else if( strcmp( q, "tls1_2" ) == 0 ||
686 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000688 else
689 goto usage;
690 }
691 else if( strcmp( p, "max_version" ) == 0 )
692 {
693 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000695 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100697 else if( strcmp( q, "tls1_1" ) == 0 ||
698 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100700 else if( strcmp( q, "tls1_2" ) == 0 ||
701 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000703 else
704 goto usage;
705 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100706 else if( strcmp( p, "arc4" ) == 0 )
707 {
708 switch( atoi( q ) )
709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
711 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100712 default: goto usage;
713 }
714 }
Gilles Peskineae765992017-05-09 15:59:24 +0200715 else if( strcmp( p, "allow_sha1" ) == 0 )
716 {
717 switch( atoi( q ) )
718 {
719 case 0: opt.allow_sha1 = 0; break;
720 case 1: opt.allow_sha1 = 1; break;
721 default: goto usage;
722 }
723 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000724 else if( strcmp( p, "force_version" ) == 0 )
725 {
726 if( strcmp( q, "ssl3" ) == 0 )
727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
729 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000730 }
731 else if( strcmp( q, "tls1" ) == 0 )
732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
734 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000735 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100736 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
739 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000740 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100741 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
744 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000745 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100746 else if( strcmp( q, "dtls1" ) == 0 )
747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
749 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
750 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100751 }
752 else if( strcmp( q, "dtls1_2" ) == 0 )
753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
755 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
756 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100757 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000758 else
759 goto usage;
760 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100761 else if( strcmp( p, "auth_mode" ) == 0 )
762 {
763 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100765 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100767 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100769 else
770 goto usage;
771 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200772 else if( strcmp( p, "max_frag_len" ) == 0 )
773 {
774 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200776 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200778 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200780 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200782 else
783 goto usage;
784 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200785 else if( strcmp( p, "trunc_hmac" ) == 0 )
786 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100787 switch( atoi( q ) )
788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
790 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100791 default: goto usage;
792 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200793 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200794 else if( strcmp( p, "hs_timeout" ) == 0 )
795 {
796 if( ( p = strchr( q, '-' ) ) == NULL )
797 goto usage;
798 *p++ = '\0';
799 opt.hs_to_min = atoi( q );
800 opt.hs_to_max = atoi( p );
801 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
802 goto usage;
803 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100804 else if( strcmp( p, "recsplit" ) == 0 )
805 {
806 opt.recsplit = atoi( q );
807 if( opt.recsplit < 0 || opt.recsplit > 1 )
808 goto usage;
809 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200810 else if( strcmp( p, "dhmlen" ) == 0 )
811 {
812 opt.dhmlen = atoi( q );
813 if( opt.dhmlen < 0 )
814 goto usage;
815 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000816 else
817 goto usage;
818 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820#if defined(MBEDTLS_DEBUG_C)
821 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200822#endif
823
Paul Bakkerc1516be2013-06-29 16:01:32 +0200824 if( opt.force_ciphersuite[0] > 0 )
825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
827 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +0200828
Paul Bakker66c48102013-07-26 14:05:32 +0200829 if( opt.max_version != -1 &&
830 ciphersuite_info->min_minor_ver > opt.max_version )
831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakker66c48102013-07-26 14:05:32 +0200833 ret = 2;
834 goto usage;
835 }
836 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200837 ciphersuite_info->max_minor_ver < opt.min_version )
838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +0200840 ret = 2;
841 goto usage;
842 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100843
844 /* If the server selects a version that's not supported by
845 * this suite, then there will be no common ciphersuite... */
846 if( opt.max_version == -1 ||
847 opt.max_version > ciphersuite_info->max_minor_ver )
848 {
Paul Bakker66c48102013-07-26 14:05:32 +0200849 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100850 }
Paul Bakker66c48102013-07-26 14:05:32 +0200851 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100852 {
Paul Bakker66c48102013-07-26 14:05:32 +0200853 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100854 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
856 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
857 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100858 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000859
860 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000866 ret = 2;
867 goto usage;
868 }
869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000871 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200872 }
873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000875 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200876 * Unhexify the pre-shared key if any is given
877 */
878 if( strlen( opt.psk ) )
879 {
880 unsigned char c;
881 size_t j;
882
883 if( strlen( opt.psk ) % 2 != 0 )
884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200886 goto exit;
887 }
888
889 psk_len = strlen( opt.psk ) / 2;
890
891 for( j = 0; j < strlen( opt.psk ); j += 2 )
892 {
893 c = opt.psk[j];
894 if( c >= '0' && c <= '9' )
895 c -= '0';
896 else if( c >= 'a' && c <= 'f' )
897 c -= 'a' - 10;
898 else if( c >= 'A' && c <= 'F' )
899 c -= 'A' - 10;
900 else
901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200903 goto exit;
904 }
905 psk[ j / 2 ] = c << 4;
906
907 c = opt.psk[j + 1];
908 if( c >= '0' && c <= '9' )
909 c -= '0';
910 else if( c >= 'a' && c <= 'f' )
911 c -= 'a' - 10;
912 else if( c >= 'A' && c <= 'F' )
913 c -= 'A' - 10;
914 else
915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200917 goto exit;
918 }
919 psk[ j / 2 ] |= c;
920 }
921 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200925 if( opt.alpn_string != NULL )
926 {
927 p = (char *) opt.alpn_string;
928 i = 0;
929
930 /* Leave room for a final NULL in alpn_list */
931 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
932 {
933 alpn_list[i++] = p;
934
935 /* Terminate the current string and move on to next one */
936 while( *p != ',' && *p != '\0' )
937 p++;
938 if( *p == ',' )
939 *p++ = '\0';
940 }
941 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200943
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200944 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000945 * 0. Initialize the RNG and the session data
946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000948 fflush( stdout );
949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200951 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200952 (const unsigned char *) pers,
953 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000954 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200955 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n", -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000956 goto exit;
957 }
958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000962 /*
963 * 1.1. Load the trusted CA
964 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000966 fflush( stdout );
967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +0000969 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100970 if( strcmp( opt.ca_path, "none" ) == 0 )
971 ret = 0;
972 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +0000974 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100975 if( strcmp( opt.ca_file, "none" ) == 0 )
976 ret = 0;
977 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +0200979 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000980#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981#if defined(MBEDTLS_CERTS_C)
982 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +0100983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 ret = mbedtls_x509_crt_parse( &cacert,
985 (const unsigned char *) mbedtls_test_cas[i],
986 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +0100987 if( ret != 0 )
988 break;
989 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000990#else
991 {
992 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000994 }
995#endif
Paul Bakker42488232012-05-16 08:21:05 +0000996 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 goto exit;
1000 }
1001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001003
1004 /*
1005 * 1.2. Load own certificate and private key
1006 *
1007 * (can be skipped if client authentication is not required)
1008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 fflush( stdout );
1011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001013 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001014 if( strcmp( opt.crt_file, "none" ) == 0 )
1015 ret = 0;
1016 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001018 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001019#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020#if defined(MBEDTLS_CERTS_C)
1021 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
1022 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001023#else
1024 {
1025 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001027 }
1028#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 if( ret != 0 )
1030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001032 goto exit;
1033 }
1034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001036 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001037 if( strcmp( opt.key_file, "none" ) == 0 )
1038 ret = 0;
1039 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001041 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043#if defined(MBEDTLS_CERTS_C)
1044 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_cli_key,
1045 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001046#else
1047 {
1048 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001050 }
1051#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001052 if( ret != 0 )
1053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 goto exit;
1056 }
1057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 mbedtls_printf( " ok\n" );
1059#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001060
1061 /*
1062 * 2. Start the connection
1063 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001064 if( opt.server_addr == NULL)
1065 opt.server_addr = opt.server_name;
1066
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001067 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001069 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001070 fflush( stdout );
1071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
1073 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1074 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001077 goto exit;
1078 }
1079
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001080 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001081 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001082 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001083 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001084 if( ret != 0 )
1085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001087 goto exit;
1088 }
1089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001091
1092 /*
1093 * 3. Setup stuff
1094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001096 fflush( stdout );
1097
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001098 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1099 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001100 opt.transport,
1101 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001102 {
1103 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
1104 goto exit;
1105 }
1106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskinedd57d752017-05-05 18:59:02 +02001108 /* The default algorithms profile disables SHA-1, but our tests still
1109 rely on it heavily. */
Gilles Peskineae765992017-05-09 15:59:24 +02001110 if( opt.allow_sha1 > 0 )
1111 {
1112 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1113 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1114 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1115 }
Gilles Peskinedd57d752017-05-05 18:59:02 +02001116
Paul Bakker915275b2012-09-28 07:10:55 +00001117 if( opt.debug_level > 0 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001118 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
Gilles Peskinedd57d752017-05-05 18:59:02 +02001119#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001120
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001121 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001122 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001125 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 +02001126 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001130 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001131 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001132 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001133 goto exit;
1134 }
Paul Bakker05decb22013-08-15 13:33:48 +02001135#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001138 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001139 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001140#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001143 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001144 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001145#endif
1146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001148 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001149 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001150#endif
1151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001153 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001154 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1156 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001157#endif
1158
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001159#if defined(MBEDTLS_DHM_C)
1160 if( opt.dhmlen != DFL_DHMLEN )
1161 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1162#endif
1163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001165 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001166 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001167 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001168 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001169 goto exit;
1170 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001171#endif
1172
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001173 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1174 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001175
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001176 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02001179 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02001180#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001181
Paul Bakker645ce3a2012-10-31 12:32:41 +00001182 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001183 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001184
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001185#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001186 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001187 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001188#endif
Paul Bakker51936882011-02-20 16:05:58 +00001189
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001190 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001191 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001193 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001194#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001197 if( strcmp( opt.ca_path, "none" ) != 0 &&
1198 strcmp( opt.ca_file, "none" ) != 0 )
1199 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001200 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001201 }
1202 if( strcmp( opt.crt_file, "none" ) != 0 &&
1203 strcmp( opt.key_file, "none" ) != 0 )
1204 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001205 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001206 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001207 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001208 goto exit;
1209 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001210 }
Paul Bakkered27a042013-04-18 22:46:23 +02001211#endif
1212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001214 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001215 (const unsigned char *) opt.psk_identity,
1216 strlen( opt.psk_identity ) ) ) != 0 )
1217 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001218 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001219 goto exit;
1220 }
Paul Bakkered27a042013-04-18 22:46:23 +02001221#endif
1222
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001223 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001224 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001225
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001226 if( opt.max_version != DFL_MAX_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001227 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001230 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001231 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001232#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001233
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001234 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
1235 {
1236 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
1237 goto exit;
1238 }
1239
1240#if defined(MBEDTLS_X509_CRT_PARSE_C)
1241 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1242 {
1243 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
1244 goto exit;
1245 }
1246#endif
1247
1248 if( opt.nbio == 2 )
1249 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
1250 else
1251 mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02001252 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001253
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001254#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001255 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
1256 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001257#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001260
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 /*
1262 * 4. Handshake
1263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001265 fflush( stdout );
1266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001269 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker40e46942009-01-03 21:51:57 +00001270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n", -ret );
1272 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
1273 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001274 " Unable to verify the server's certificate. "
1275 "Either it is invalid,\n"
1276 " or you didn't set ca_file or ca_path "
1277 "to an appropriate value.\n"
1278 " Alternatively, you may want to use "
1279 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00001282 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 }
1284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1286 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
1289 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001290 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001292
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02001293#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1294 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
1295 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
1296#endif
1297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001299 if( opt.alpn_string != NULL )
1300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
1302 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001303 alp ? alp : "(none)" );
1304 }
1305#endif
1306
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001307 if( opt.reconnect != 0 )
1308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001310 fflush( stdout );
1311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001315 goto exit;
1316 }
1317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001319 }
1320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 /*
1323 * 5. Verify the server certificate
1324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001327 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001329 char vrfy_buf[512];
1330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001333 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001335 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 }
1337 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 mbedtls_printf( " . Peer certificate information ...\n" );
1343 mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1344 mbedtls_ssl_get_peer_cert( &ssl ) );
1345 mbedtls_printf( "%s\n", buf );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001346 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001350 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001351 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001352 /*
1353 * Perform renegotiation (this must be done when the server is waiting
1354 * for input from our side).
1355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001357 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001359 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001360 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1361 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001364 goto exit;
1365 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001366 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001368 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001370
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 /*
1372 * 6. Write the GET request
1373 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001374 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001375send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001377 fflush( stdout );
1378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 len = mbedtls_snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST,
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001380 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001381 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001382
1383 /* Add padding to GET request to reach opt.request_size in length */
1384 if( opt.request_size != DFL_REQUEST_SIZE &&
1385 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001386 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001387 memset( buf + len, 'A', opt.request_size - len - tail_len );
1388 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001389 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001391 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof(buf) - len - 1 );
1392 len += tail_len;
1393
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001394 /* Truncate if request size is smaller than the "natural" size */
1395 if( opt.request_size != DFL_REQUEST_SIZE &&
1396 len > opt.request_size )
1397 {
1398 len = opt.request_size;
1399
1400 /* Still end with \r\n unless that's really not possible */
1401 if( len >= 2 ) buf[len - 2] = '\r';
1402 if( len >= 1 ) buf[len - 1] = '\n';
1403 }
1404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001406 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001407 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001410 <= 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001411 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001412 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1413 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001416 goto exit;
1417 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001418 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 }
1420 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001421 else /* Not stream, so datagram */
1422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 do ret = mbedtls_ssl_write( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001424 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1425 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001426
1427 if( ret < 0 )
1428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001430 goto exit;
1431 }
1432
1433 frags = 1;
1434 written = ret;
1435 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001436
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001437 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001439
1440 /*
1441 * 7. Read the HTTP response
1442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 fflush( stdout );
1445
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001446 /*
1447 * TLS and DTLS need different reading styles (stream vs datagram)
1448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001450 {
1451 do
1452 {
1453 len = sizeof( buf ) - 1;
1454 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001456
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001457 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1458 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001459 continue;
1460
1461 if( ret <= 0 )
1462 {
1463 switch( ret )
1464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1466 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001467 ret = 0;
1468 goto close_notify;
1469
1470 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 case MBEDTLS_ERR_NET_CONN_RESET:
1472 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001473 ret = 0;
1474 goto reconnect;
1475
1476 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001478 goto exit;
1479 }
1480 }
1481
1482 len = ret;
1483 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001485
1486 /* End of message should be detected according to the syntax of the
1487 * application protocol (eg HTTP), just use a dummy test here. */
1488 if( ret > 0 && buf[len-1] == '\n' )
1489 {
1490 ret = 0;
1491 break;
1492 }
1493 }
1494 while( 1 );
1495 }
1496 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001497 {
1498 len = sizeof( buf ) - 1;
1499 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 do ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001502 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1503 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001504
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001505 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001507 switch( ret )
1508 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001509 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001511 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001512 goto send_request;
1513 goto exit;
1514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1516 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001517 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001518 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00001519
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001520 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001522 goto exit;
1523 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001524 }
1525
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02001527 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001529 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001530 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001531
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001532 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001533 * 7b. Simulate hard reset and reconnect from same port?
1534 */
1535 if( opt.reconnect_hard != 0 )
1536 {
1537 opt.reconnect_hard = 0;
1538
1539 mbedtls_printf( " . Restarting connection from same port..." );
1540 fflush( stdout );
1541
1542 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
1543 {
1544 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", -ret );
1545 goto exit;
1546 }
1547
1548 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
1549 {
1550 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1551 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1552 {
1553 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
1554 goto exit;
1555 }
1556 }
1557
1558 mbedtls_printf( " ok\n" );
1559
1560 goto send_request;
1561 }
1562
1563 /*
1564 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001565 */
1566 if( --opt.exchanges > 0 )
1567 goto send_request;
1568
1569 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001570 * 8. Done, cleanly close the connection
1571 */
1572close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001574 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001575
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001576 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001578 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001579 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001582
1583 /*
1584 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001585 */
1586reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001587 if( opt.reconnect != 0 )
1588 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001589 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001590
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001591 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001592
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001593#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001594 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02001595 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001596#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02001597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001603 goto exit;
1604 }
1605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001607 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001608 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001609 goto exit;
1610 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
1613 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1614 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001617 goto exit;
1618 }
1619
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001620 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001621 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001622 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001623 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001624 if( ret != 0 )
1625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001627 -ret );
1628 goto exit;
1629 }
1630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001632 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001633 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1634 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001637 goto exit;
1638 }
1639 }
1640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001642
1643 goto send_request;
1644 }
1645
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001646 /*
1647 * Cleanup and exit
1648 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001649exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00001651 if( ret != 0 )
1652 {
1653 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 mbedtls_strerror( ret, error_buf, 100 );
1655 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001656 }
1657#endif
1658
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001659 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#if defined(MBEDTLS_X509_CRT_PARSE_C)
1662 mbedtls_x509_crt_free( &clicert );
1663 mbedtls_x509_crt_free( &cacert );
1664 mbedtls_pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001665#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 mbedtls_ssl_session_free( &saved_session );
1667 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001668 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 mbedtls_ctr_drbg_free( &ctr_drbg );
1670 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00001671
Paul Bakkercce9d772011-11-18 14:26:47 +00001672#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001674 fflush( stdout ); getchar();
1675#endif
1676
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001677 // Shell can not handle large exit numbers -> 1 for errors
1678 if( ret < 0 )
1679 ret = 1;
1680
Paul Bakker5121ce52009-01-03 21:22:43 +00001681 return( ret );
1682}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
1684 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001685 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */