blob: 559e5028d8c4285aac8d8e8c9b768c3e02399e7c [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"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +020078#define DFL_ECJPAKE_PW NULL
Paul Bakker51936882011-02-20 16:05:58 +000079#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010081#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010082#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +020083#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +020084#define DFL_MIN_VERSION -1
Paul Bakker1d29fb52012-09-28 13:28:45 +000085#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +000086#define DFL_ARC4 -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
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200215#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
216#define USAGE_ECJPAKE \
217 " ecjpake_pw=%%s default: none (disabled)\n"
218#else
219#define USAGE_ECJPAKE ""
220#endif
221
Paul Bakker9caf2d22010-02-18 19:37:19 +0000222#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000223 "\n usage: ssl_client2 param=<>...\n" \
224 "\n acceptable parameters:\n" \
225 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100226 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000227 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000228 " request_page=%%s default: \".\"\n" \
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +0200229 " request_size=%%d default: about 34 (basic request)\n" \
230 " (minimum: 0, max: 16384)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100231 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100232 " nbio=%%d default: 0 (blocking I/O)\n" \
233 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200234 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200235 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100236 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200237 USAGE_DTLS \
238 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100239 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100240 " options: none, optional, required\n" \
241 USAGE_IO \
242 "\n" \
243 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200244 USAGE_ECJPAKE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100245 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100246 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100247 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200248 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200249 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200250 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200251 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200252 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100253 USAGE_MAX_FRAG_LEN \
254 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200255 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200256 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200257 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100258 USAGE_ETM \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100259 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200260 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000261 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100262 " arc4=%%d default: (library default: 0)\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200263 " min_version=%%s default: (library default: tls1)\n" \
264 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000265 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100266 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000267 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000268 " force_ciphersuite=<name> default: all enabled\n"\
269 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000270
Rich Evans85b05ec2015-02-12 11:37:29 +0000271/*
272 * global options
273 */
274struct options
275{
276 const char *server_name; /* hostname of the server (client only) */
277 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200278 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000279 int debug_level; /* level of debugging */
280 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000282 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000283 const char *request_page; /* page on server to request */
284 int request_size; /* pad request with header to requested size */
285 const char *ca_file; /* the file with the CA certificate(s) */
286 const char *ca_path; /* the path with the CA certificate(s) reside */
287 const char *crt_file; /* the file with the client certificate */
288 const char *key_file; /* the file with the client key */
289 const char *psk; /* the pre-shared key */
290 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200291 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000292 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
293 int renegotiation; /* enable / disable renegotiation */
294 int allow_legacy; /* allow legacy renegotiation */
295 int renegotiate; /* attempt renegotiation? */
296 int renego_delay; /* delay before enforcing renegotiation */
297 int exchanges; /* number of data exchanges */
298 int min_version; /* minimum protocol version accepted */
299 int max_version; /* maximum protocol version accepted */
300 int arc4; /* flag for arc4 suites support */
301 int auth_mode; /* verify mode for connection */
302 unsigned char mfl_code; /* code for maximum fragment length */
303 int trunc_hmac; /* negotiate truncated hmac or not */
304 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200305 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000306 int reconnect; /* attempt to resume session */
307 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200308 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000309 int tickets; /* enable / disable session tickets */
310 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000311 int transport; /* TLS or DTLS? */
312 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
313 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Rich Evans85b05ec2015-02-12 11:37:29 +0000314 int fallback; /* is this a fallback connection? */
315 int extended_ms; /* negotiate extended master secret? */
316 int etm; /* negotiate encrypt then mac? */
317} opt;
318
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200319static void my_debug( void *ctx, int level,
320 const char *file, int line,
321 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000322{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200323 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000324
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200325 /* Extract basename from file */
326 for( p = basename = file; *p != '\0'; p++ )
327 if( *p == '/' || *p == '\\' )
328 basename = p + 1;
329
330 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000331 fflush( (FILE *) ctx );
332}
333
334/*
335 * Test recv/send functions that make sure each try returns
336 * WANT_READ/WANT_WRITE at least once before sucesseding
337 */
338static int my_recv( void *ctx, unsigned char *buf, size_t len )
339{
340 static int first_try = 1;
341 int ret;
342
343 if( first_try )
344 {
345 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100346 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000347 }
348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100350 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000351 first_try = 1; /* Next call will be a new operation */
352 return( ret );
353}
354
355static int my_send( void *ctx, const unsigned char *buf, size_t len )
356{
357 static int first_try = 1;
358 int ret;
359
360 if( first_try )
361 {
362 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100363 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000364 }
365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100367 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000368 first_try = 1; /* Next call will be a new operation */
369 return( ret );
370}
371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#if defined(MBEDTLS_X509_CRT_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000373/*
374 * Enabled if debug_level > 1 in code below
375 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200376static int my_verify( void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000377{
378 char buf[1024];
379 ((void) data);
380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
382 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
383 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000384
Rich Evans85b05ec2015-02-12 11:37:29 +0000385 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100387 else
388 {
389 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
390 mbedtls_printf( "%s\n", buf );
391 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000392
393 return( 0 );
394}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000396
Paul Bakker9caf2d22010-02-18 19:37:19 +0000397int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000398{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200399 int ret = 0, len, tail_len, i, written, frags, retry_left;
400 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 unsigned char buf[MBEDTLS_SSL_MAX_CONTENT_LEN + 1];
402#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
403 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200404 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200405#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200407 const char *alpn_list[10];
408#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200409 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_entropy_context entropy;
412 mbedtls_ctr_drbg_context ctr_drbg;
413 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200414 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200416#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200417 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200418#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200420 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_x509_crt cacert;
422 mbedtls_x509_crt clicert;
423 mbedtls_pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200424#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000425 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000426 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000427
Paul Bakker1a207ec2011-02-06 13:22:40 +0000428 /*
429 * Make sure memory references are valid.
430 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200431 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200432 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200433 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200435 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436#if defined(MBEDTLS_X509_CRT_PARSE_C)
437 mbedtls_x509_crt_init( &cacert );
438 mbedtls_x509_crt_init( &clicert );
439 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200440#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200442 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200443#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000444
Paul Bakker9caf2d22010-02-18 19:37:19 +0000445 if( argc == 0 )
446 {
447 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000448 if( ret == 0 )
449 ret = 1;
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000454 while( *list )
455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200457 list++;
458 if( !*list )
459 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000461 list++;
462 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000464 goto exit;
465 }
466
467 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100468 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000469 opt.server_port = DFL_SERVER_PORT;
470 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100471 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200472 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200473 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000474 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200475 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000476 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000477 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000478 opt.crt_file = DFL_CRT_FILE;
479 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200480 opt.psk = DFL_PSK;
481 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200482 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakker51936882011-02-20 16:05:58 +0000483 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000484 opt.renegotiation = DFL_RENEGOTIATION;
485 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100486 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200487 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000488 opt.min_version = DFL_MIN_VERSION;
489 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100490 opt.arc4 = DFL_ARC4;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100491 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200492 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200493 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100494 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200495 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200496 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100497 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200498 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200499 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200500 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200501 opt.transport = DFL_TRANSPORT;
502 opt.hs_to_min = DFL_HS_TO_MIN;
503 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200504 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200505 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100506 opt.etm = DFL_ETM;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000507
508 for( i = 1; i < argc; i++ )
509 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000510 p = argv[i];
511 if( ( q = strchr( p, '=' ) ) == NULL )
512 goto usage;
513 *q++ = '\0';
514
515 if( strcmp( p, "server_name" ) == 0 )
516 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100517 else if( strcmp( p, "server_addr" ) == 0 )
518 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000519 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200520 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100521 else if( strcmp( p, "dtls" ) == 0 )
522 {
523 int t = atoi( q );
524 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100526 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100528 else
529 goto usage;
530 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000531 else if( strcmp( p, "debug_level" ) == 0 )
532 {
533 opt.debug_level = atoi( q );
534 if( opt.debug_level < 0 || opt.debug_level > 65535 )
535 goto usage;
536 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100537 else if( strcmp( p, "nbio" ) == 0 )
538 {
539 opt.nbio = atoi( q );
540 if( opt.nbio < 0 || opt.nbio > 2 )
541 goto usage;
542 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200543 else if( strcmp( p, "read_timeout" ) == 0 )
544 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200545 else if( strcmp( p, "max_resend" ) == 0 )
546 {
547 opt.max_resend = atoi( q );
548 if( opt.max_resend < 0 )
549 goto usage;
550 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000551 else if( strcmp( p, "request_page" ) == 0 )
552 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200553 else if( strcmp( p, "request_size" ) == 0 )
554 {
555 opt.request_size = atoi( q );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 if( opt.request_size < 0 || opt.request_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker93c32b22014-04-25 13:40:05 +0200557 goto usage;
558 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000559 else if( strcmp( p, "ca_file" ) == 0 )
560 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000561 else if( strcmp( p, "ca_path" ) == 0 )
562 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000563 else if( strcmp( p, "crt_file" ) == 0 )
564 opt.crt_file = q;
565 else if( strcmp( p, "key_file" ) == 0 )
566 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200567 else if( strcmp( p, "psk" ) == 0 )
568 opt.psk = q;
569 else if( strcmp( p, "psk_identity" ) == 0 )
570 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200571 else if( strcmp( p, "ecjpake_pw" ) == 0 )
572 opt.ecjpake_pw = q;
Paul Bakker51936882011-02-20 16:05:58 +0000573 else if( strcmp( p, "force_ciphersuite" ) == 0 )
574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +0000576
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200577 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000578 {
579 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000580 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000581 }
Paul Bakker51936882011-02-20 16:05:58 +0000582 opt.force_ciphersuite[1] = 0;
583 }
Paul Bakker48916f92012-09-16 19:57:18 +0000584 else if( strcmp( p, "renegotiation" ) == 0 )
585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 opt.renegotiation = (atoi( q )) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED :
587 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +0000588 }
589 else if( strcmp( p, "allow_legacy" ) == 0 )
590 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100591 switch( atoi( q ) )
592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 case -1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE; break;
594 case 0: opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; break;
595 case 1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION; break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100596 default: goto usage;
597 }
Paul Bakker48916f92012-09-16 19:57:18 +0000598 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100599 else if( strcmp( p, "renegotiate" ) == 0 )
600 {
601 opt.renegotiate = atoi( q );
602 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
603 goto usage;
604 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200605 else if( strcmp( p, "exchanges" ) == 0 )
606 {
607 opt.exchanges = atoi( q );
608 if( opt.exchanges < 1 )
609 goto usage;
610 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200611 else if( strcmp( p, "reconnect" ) == 0 )
612 {
613 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200614 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200615 goto usage;
616 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100617 else if( strcmp( p, "reco_delay" ) == 0 )
618 {
619 opt.reco_delay = atoi( q );
620 if( opt.reco_delay < 0 )
621 goto usage;
622 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200623 else if( strcmp( p, "reconnect_hard" ) == 0 )
624 {
625 opt.reconnect_hard = atoi( q );
626 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
627 goto usage;
628 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200629 else if( strcmp( p, "tickets" ) == 0 )
630 {
631 opt.tickets = atoi( q );
632 if( opt.tickets < 0 || opt.tickets > 2 )
633 goto usage;
634 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200635 else if( strcmp( p, "alpn" ) == 0 )
636 {
637 opt.alpn_string = q;
638 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200639 else if( strcmp( p, "fallback" ) == 0 )
640 {
641 switch( atoi( q ) )
642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
644 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200645 default: goto usage;
646 }
647 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200648 else if( strcmp( p, "extended_ms" ) == 0 )
649 {
650 switch( atoi( q ) )
651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 case 0: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED; break;
653 case 1: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200654 default: goto usage;
655 }
656 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100657 else if( strcmp( p, "etm" ) == 0 )
658 {
659 switch( atoi( q ) )
660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
662 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100663 default: goto usage;
664 }
665 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000666 else if( strcmp( p, "min_version" ) == 0 )
667 {
668 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000670 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100672 else if( strcmp( q, "tls1_1" ) == 0 ||
673 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100675 else if( strcmp( q, "tls1_2" ) == 0 ||
676 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000678 else
679 goto usage;
680 }
681 else if( strcmp( p, "max_version" ) == 0 )
682 {
683 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000685 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100687 else if( strcmp( q, "tls1_1" ) == 0 ||
688 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100690 else if( strcmp( q, "tls1_2" ) == 0 ||
691 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000693 else
694 goto usage;
695 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100696 else if( strcmp( p, "arc4" ) == 0 )
697 {
698 switch( atoi( q ) )
699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
701 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100702 default: goto usage;
703 }
704 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000705 else if( strcmp( p, "force_version" ) == 0 )
706 {
707 if( strcmp( q, "ssl3" ) == 0 )
708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
710 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000711 }
712 else if( strcmp( q, "tls1" ) == 0 )
713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
715 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000716 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100717 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
720 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000721 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100722 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
725 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000726 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100727 else if( strcmp( q, "dtls1" ) == 0 )
728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
730 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
731 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100732 }
733 else if( strcmp( q, "dtls1_2" ) == 0 )
734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
736 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
737 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100738 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000739 else
740 goto usage;
741 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100742 else if( strcmp( p, "auth_mode" ) == 0 )
743 {
744 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100746 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100748 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100750 else
751 goto usage;
752 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200753 else if( strcmp( p, "max_frag_len" ) == 0 )
754 {
755 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200757 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200759 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200761 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200763 else
764 goto usage;
765 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200766 else if( strcmp( p, "trunc_hmac" ) == 0 )
767 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100768 switch( atoi( q ) )
769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
771 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100772 default: goto usage;
773 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200774 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200775 else if( strcmp( p, "hs_timeout" ) == 0 )
776 {
777 if( ( p = strchr( q, '-' ) ) == NULL )
778 goto usage;
779 *p++ = '\0';
780 opt.hs_to_min = atoi( q );
781 opt.hs_to_max = atoi( p );
782 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
783 goto usage;
784 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100785 else if( strcmp( p, "recsplit" ) == 0 )
786 {
787 opt.recsplit = atoi( q );
788 if( opt.recsplit < 0 || opt.recsplit > 1 )
789 goto usage;
790 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200791 else if( strcmp( p, "dhmlen" ) == 0 )
792 {
793 opt.dhmlen = atoi( q );
794 if( opt.dhmlen < 0 )
795 goto usage;
796 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000797 else
798 goto usage;
799 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801#if defined(MBEDTLS_DEBUG_C)
802 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200803#endif
804
Paul Bakkerc1516be2013-06-29 16:01:32 +0200805 if( opt.force_ciphersuite[0] > 0 )
806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
808 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +0200809
Paul Bakker66c48102013-07-26 14:05:32 +0200810 if( opt.max_version != -1 &&
811 ciphersuite_info->min_minor_ver > opt.max_version )
812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakker66c48102013-07-26 14:05:32 +0200814 ret = 2;
815 goto usage;
816 }
817 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200818 ciphersuite_info->max_minor_ver < opt.min_version )
819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +0200821 ret = 2;
822 goto usage;
823 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100824
825 /* If the server selects a version that's not supported by
826 * this suite, then there will be no common ciphersuite... */
827 if( opt.max_version == -1 ||
828 opt.max_version > ciphersuite_info->max_minor_ver )
829 {
Paul Bakker66c48102013-07-26 14:05:32 +0200830 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100831 }
Paul Bakker66c48102013-07-26 14:05:32 +0200832 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100833 {
Paul Bakker66c48102013-07-26 14:05:32 +0200834 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100835 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
837 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
838 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100839 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000840
841 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000847 ret = 2;
848 goto usage;
849 }
850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000852 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200853 }
854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000856 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200857 * Unhexify the pre-shared key if any is given
858 */
859 if( strlen( opt.psk ) )
860 {
861 unsigned char c;
862 size_t j;
863
864 if( strlen( opt.psk ) % 2 != 0 )
865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200867 goto exit;
868 }
869
870 psk_len = strlen( opt.psk ) / 2;
871
872 for( j = 0; j < strlen( opt.psk ); j += 2 )
873 {
874 c = opt.psk[j];
875 if( c >= '0' && c <= '9' )
876 c -= '0';
877 else if( c >= 'a' && c <= 'f' )
878 c -= 'a' - 10;
879 else if( c >= 'A' && c <= 'F' )
880 c -= 'A' - 10;
881 else
882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200884 goto exit;
885 }
886 psk[ j / 2 ] = c << 4;
887
888 c = opt.psk[j + 1];
889 if( c >= '0' && c <= '9' )
890 c -= '0';
891 else if( c >= 'a' && c <= 'f' )
892 c -= 'a' - 10;
893 else if( c >= 'A' && c <= 'F' )
894 c -= 'A' - 10;
895 else
896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200898 goto exit;
899 }
900 psk[ j / 2 ] |= c;
901 }
902 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200906 if( opt.alpn_string != NULL )
907 {
908 p = (char *) opt.alpn_string;
909 i = 0;
910
911 /* Leave room for a final NULL in alpn_list */
912 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
913 {
914 alpn_list[i++] = p;
915
916 /* Terminate the current string and move on to next one */
917 while( *p != ',' && *p != '\0' )
918 p++;
919 if( *p == ',' )
920 *p++ = '\0';
921 }
922 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200924
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200925 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000926 * 0. Initialize the RNG and the session data
927 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000929 fflush( stdout );
930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200932 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200933 (const unsigned char *) pers,
934 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000935 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200936 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n", -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000937 goto exit;
938 }
939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000943 /*
944 * 1.1. Load the trusted CA
945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000947 fflush( stdout );
948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +0000950 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100951 if( strcmp( opt.ca_path, "none" ) == 0 )
952 ret = 0;
953 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +0000955 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100956 if( strcmp( opt.ca_file, "none" ) == 0 )
957 ret = 0;
958 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +0200960 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000961#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962#if defined(MBEDTLS_CERTS_C)
963 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +0100964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 ret = mbedtls_x509_crt_parse( &cacert,
966 (const unsigned char *) mbedtls_test_cas[i],
967 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +0100968 if( ret != 0 )
969 break;
970 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000971#else
972 {
973 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000975 }
976#endif
Paul Bakker42488232012-05-16 08:21:05 +0000977 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000980 goto exit;
981 }
982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000984
985 /*
986 * 1.2. Load own certificate and private key
987 *
988 * (can be skipped if client authentication is not required)
989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000991 fflush( stdout );
992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +0000994 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100995 if( strcmp( opt.crt_file, "none" ) == 0 )
996 ret = 0;
997 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +0200999 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001000#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001#if defined(MBEDTLS_CERTS_C)
1002 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
1003 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001004#else
1005 {
1006 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001008 }
1009#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 if( ret != 0 )
1011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001013 goto exit;
1014 }
1015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001017 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001018 if( strcmp( opt.key_file, "none" ) == 0 )
1019 ret = 0;
1020 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001022 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001023#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024#if defined(MBEDTLS_CERTS_C)
1025 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_cli_key,
1026 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001027#else
1028 {
1029 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001031 }
1032#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 if( ret != 0 )
1034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001036 goto exit;
1037 }
1038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 mbedtls_printf( " ok\n" );
1040#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
1042 /*
1043 * 2. Start the connection
1044 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001045 if( opt.server_addr == NULL)
1046 opt.server_addr = opt.server_name;
1047
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001048 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001050 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001051 fflush( stdout );
1052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
1054 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1055 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001058 goto exit;
1059 }
1060
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001061 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001062 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001063 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001064 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001065 if( ret != 0 )
1066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001068 goto exit;
1069 }
1070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001072
1073 /*
1074 * 3. Setup stuff
1075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001077 fflush( stdout );
1078
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001079 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1080 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001081 opt.transport,
1082 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001083 {
1084 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
1085 goto exit;
1086 }
1087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker915275b2012-09-28 07:10:55 +00001089 if( opt.debug_level > 0 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001090 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
Paul Bakkered27a042013-04-18 22:46:23 +02001091#endif
Paul Bakker915275b2012-09-28 07:10:55 +00001092
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001093 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001094 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001097 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 +02001098 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001102 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001103 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001104 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001105 goto exit;
1106 }
Paul Bakker05decb22013-08-15 13:33:48 +02001107#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001110 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001111 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001112#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001115 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001116 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001117#endif
1118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001120 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001121 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001122#endif
1123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001125 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001126 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1128 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001129#endif
1130
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001131#if defined(MBEDTLS_DHM_C)
1132 if( opt.dhmlen != DFL_DHMLEN )
1133 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1134#endif
1135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001137 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001138 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001139 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001140 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001141 goto exit;
1142 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001143#endif
1144
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001145 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1146 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001147
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001148 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02001151 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02001152#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001153
Paul Bakker645ce3a2012-10-31 12:32:41 +00001154 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001155 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001156
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001157#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001158 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001159 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001160#endif
Paul Bakker51936882011-02-20 16:05:58 +00001161
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001162 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001163 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001165 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001166#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001169 if( strcmp( opt.ca_path, "none" ) != 0 &&
1170 strcmp( opt.ca_file, "none" ) != 0 )
1171 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001172 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001173 }
1174 if( strcmp( opt.crt_file, "none" ) != 0 &&
1175 strcmp( opt.key_file, "none" ) != 0 )
1176 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001177 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001178 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001179 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001180 goto exit;
1181 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001182 }
Paul Bakkered27a042013-04-18 22:46:23 +02001183#endif
1184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001186 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001187 (const unsigned char *) opt.psk_identity,
1188 strlen( opt.psk_identity ) ) ) != 0 )
1189 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001190 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001191 goto exit;
1192 }
Paul Bakkered27a042013-04-18 22:46:23 +02001193#endif
1194
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001195 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001196 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001197
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001198 if( opt.max_version != DFL_MAX_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001199 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001202 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001203 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001204#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001205
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001206 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
1207 {
1208 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
1209 goto exit;
1210 }
1211
1212#if defined(MBEDTLS_X509_CRT_PARSE_C)
1213 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1214 {
1215 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
1216 goto exit;
1217 }
1218#endif
1219
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001220#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1221 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
1222 {
1223 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
1224 (const unsigned char *) opt.ecjpake_pw,
1225 strlen( opt.ecjpake_pw ) ) ) != 0 )
1226 {
1227 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
1228 goto exit;
1229 }
1230 }
1231#endif
1232
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001233 if( opt.nbio == 2 )
1234 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
1235 else
1236 mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02001237 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001238
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001239#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001240 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
1241 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001242#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001245
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 /*
1247 * 4. Handshake
1248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 fflush( stdout );
1251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001254 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker40e46942009-01-03 21:51:57 +00001255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n", -ret );
1257 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
1258 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001259 " Unable to verify the server's certificate. "
1260 "Either it is invalid,\n"
1261 " or you didn't set ca_file or ca_path "
1262 "to an appropriate value.\n"
1263 " Alternatively, you may want to use "
1264 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00001267 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 }
1269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1271 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
1274 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001275 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001277
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02001278#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1279 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
1280 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
1281#endif
1282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001284 if( opt.alpn_string != NULL )
1285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
1287 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001288 alp ? alp : "(none)" );
1289 }
1290#endif
1291
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001292 if( opt.reconnect != 0 )
1293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001295 fflush( stdout );
1296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001300 goto exit;
1301 }
1302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001304 }
1305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001307 /*
1308 * 5. Verify the server certificate
1309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001311
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001312 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001314 char vrfy_buf[512];
1315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001317
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001318 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00001319
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001320 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 }
1322 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 mbedtls_printf( " . Peer certificate information ...\n" );
1328 mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1329 mbedtls_ssl_get_peer_cert( &ssl ) );
1330 mbedtls_printf( "%s\n", buf );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001331 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001335 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001336 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001337 /*
1338 * Perform renegotiation (this must be done when the server is waiting
1339 * for input from our side).
1340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001342 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001344 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001345 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1346 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001349 goto exit;
1350 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001351 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001353 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001355
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 /*
1357 * 6. Write the GET request
1358 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001359 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001360send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 fflush( stdout );
1363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 len = mbedtls_snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST,
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001365 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001366 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001367
1368 /* Add padding to GET request to reach opt.request_size in length */
1369 if( opt.request_size != DFL_REQUEST_SIZE &&
1370 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001371 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001372 memset( buf + len, 'A', opt.request_size - len - tail_len );
1373 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001374 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001375
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001376 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof(buf) - len - 1 );
1377 len += tail_len;
1378
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001379 /* Truncate if request size is smaller than the "natural" size */
1380 if( opt.request_size != DFL_REQUEST_SIZE &&
1381 len > opt.request_size )
1382 {
1383 len = opt.request_size;
1384
1385 /* Still end with \r\n unless that's really not possible */
1386 if( len >= 2 ) buf[len - 2] = '\r';
1387 if( len >= 1 ) buf[len - 1] = '\n';
1388 }
1389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001391 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001392 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001395 <= 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001396 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001397 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1398 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001401 goto exit;
1402 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001403 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 }
1405 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001406 else /* Not stream, so datagram */
1407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 do ret = mbedtls_ssl_write( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001409 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1410 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001411
1412 if( ret < 0 )
1413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001415 goto exit;
1416 }
1417
1418 frags = 1;
1419 written = ret;
1420 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001422 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001424
1425 /*
1426 * 7. Read the HTTP response
1427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 fflush( stdout );
1430
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001431 /*
1432 * TLS and DTLS need different reading styles (stream vs datagram)
1433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001435 {
1436 do
1437 {
1438 len = sizeof( buf ) - 1;
1439 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001441
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001442 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1443 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001444 continue;
1445
1446 if( ret <= 0 )
1447 {
1448 switch( ret )
1449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1451 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001452 ret = 0;
1453 goto close_notify;
1454
1455 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 case MBEDTLS_ERR_NET_CONN_RESET:
1457 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001458 ret = 0;
1459 goto reconnect;
1460
1461 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001463 goto exit;
1464 }
1465 }
1466
1467 len = ret;
1468 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001470
1471 /* End of message should be detected according to the syntax of the
1472 * application protocol (eg HTTP), just use a dummy test here. */
1473 if( ret > 0 && buf[len-1] == '\n' )
1474 {
1475 ret = 0;
1476 break;
1477 }
1478 }
1479 while( 1 );
1480 }
1481 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 {
1483 len = sizeof( buf ) - 1;
1484 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 do ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001487 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1488 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001489
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001490 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001492 switch( ret )
1493 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001494 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001496 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001497 goto send_request;
1498 goto exit;
1499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1501 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001502 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001503 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00001504
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001505 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001507 goto exit;
1508 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001509 }
1510
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02001512 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001514 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001515 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001516
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001517 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001518 * 7b. Simulate hard reset and reconnect from same port?
1519 */
1520 if( opt.reconnect_hard != 0 )
1521 {
1522 opt.reconnect_hard = 0;
1523
1524 mbedtls_printf( " . Restarting connection from same port..." );
1525 fflush( stdout );
1526
1527 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
1528 {
1529 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", -ret );
1530 goto exit;
1531 }
1532
1533 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
1534 {
1535 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1536 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1537 {
1538 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
1539 goto exit;
1540 }
1541 }
1542
1543 mbedtls_printf( " ok\n" );
1544
1545 goto send_request;
1546 }
1547
1548 /*
1549 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001550 */
1551 if( --opt.exchanges > 0 )
1552 goto send_request;
1553
1554 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001555 * 8. Done, cleanly close the connection
1556 */
1557close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001559 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001560
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001561 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001563 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001564 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001567
1568 /*
1569 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001570 */
1571reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001572 if( opt.reconnect != 0 )
1573 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001574 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001575
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001576 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001577
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001578#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001579 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02001580 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001581#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001588 goto exit;
1589 }
1590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001592 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001593 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001594 goto exit;
1595 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
1598 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1599 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001602 goto exit;
1603 }
1604
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001605 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001606 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001607 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001608 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001609 if( ret != 0 )
1610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001612 -ret );
1613 goto exit;
1614 }
1615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001617 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001618 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1619 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001622 goto exit;
1623 }
1624 }
1625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001627
1628 goto send_request;
1629 }
1630
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001631 /*
1632 * Cleanup and exit
1633 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001634exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00001636 if( ret != 0 )
1637 {
1638 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 mbedtls_strerror( ret, error_buf, 100 );
1640 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001641 }
1642#endif
1643
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001644 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646#if defined(MBEDTLS_X509_CRT_PARSE_C)
1647 mbedtls_x509_crt_free( &clicert );
1648 mbedtls_x509_crt_free( &cacert );
1649 mbedtls_pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001650#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 mbedtls_ssl_session_free( &saved_session );
1652 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001653 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 mbedtls_ctr_drbg_free( &ctr_drbg );
1655 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00001656
Paul Bakkercce9d772011-11-18 14:26:47 +00001657#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001659 fflush( stdout ); getchar();
1660#endif
1661
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001662 // Shell can not handle large exit numbers -> 1 for errors
1663 if( ret < 0 )
1664 ret = 1;
1665
Paul Bakker5121ce52009-01-03 21:22:43 +00001666 return( ret );
1667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
1669 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001670 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */