blob: 212f47a6cf234415ee3d7e99ab869c2c7b49e802 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client with certificate authentication
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Simon Butcherd3138c32016-04-27 01:26:50 +010032#include <stdlib.h>
33#define mbedtls_time time
34#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#define mbedtls_printf printf
36#define mbedtls_fprintf fprintf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define mbedtls_snprintf snprintf
Rich Evansf90016a2015-01-19 14:26:37 +000038#endif
39
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020040#if !defined(MBEDTLS_ENTROPY_C) || \
41 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020042 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020043int main( void )
44{
45 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
46 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020047 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020048 return( 0 );
49}
50#else
51
Andres AG788aa4a2016-09-14 14:32:09 +010052#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/ssl.h"
54#include "mbedtls/entropy.h"
55#include "mbedtls/ctr_drbg.h"
56#include "mbedtls/certs.h"
57#include "mbedtls/x509.h"
58#include "mbedtls/error.h"
59#include "mbedtls/debug.h"
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020060#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Rich Evans18b78c72015-02-11 14:06:19 +000062#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010065
Paul Bakker9caf2d22010-02-18 19:37:19 +000066#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010067#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020068#define DFL_SERVER_PORT "4433"
Paul Bakker9caf2d22010-02-18 19:37:19 +000069#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +020070#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +000071#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010072#define DFL_NBIO 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020073#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +020074#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +000075#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000076#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000077#define DFL_CRT_FILE ""
78#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020079#define DFL_PSK ""
80#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +020081#define DFL_ECJPAKE_PW NULL
Paul Bakker51936882011-02-20 16:05:58 +000082#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010084#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010085#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +020086#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +020087#define DFL_MIN_VERSION -1
Paul Bakker1d29fb52012-09-28 13:28:45 +000088#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +000089#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +020090#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +010091#define DFL_AUTH_MODE -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +010093#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +010094#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +020095#define DFL_DHMLEN -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +020096#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010097#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +020098#define DFL_RECONNECT_HARD 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200100#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200102#define DFL_HS_TO_MIN 0
103#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200104#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200105#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100106#define DFL_ETM -1
Paul Bakker0e6975b2009-02-10 22:19:10 +0000107
Paul Bakker93c32b22014-04-25 13:40:05 +0200108#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
109#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#if defined(MBEDTLS_X509_CRT_PARSE_C)
112#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000113#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100114 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
115 " default: \"\" (pre-loaded)\n" \
116 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
117 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
118 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
119 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000120 " key_file=%%s default: \"\" (pre-loaded)\n"
121#else
122#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 " No file operations available (MBEDTLS_FS_IO not defined)\n"
124#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200125#else
126#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200130#define USAGE_PSK \
131 " psk=%%s default: \"\" (in hex, without 0x)\n" \
132 " psk_identity=%%s default: \"Client_identity\"\n"
133#else
134#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200138#define USAGE_TICKETS \
139 " tickets=%%d default: 1 (enabled)\n"
140#else
141#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200145#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100146 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200147#else
148#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200152#define USAGE_MAX_FRAG_LEN \
153 " max_frag_len=%%d default: 16384 (tls default)\n" \
154 " options: 512, 1024, 2048, 4096\n"
155#else
156#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100160#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200161 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100162#else
163#define USAGE_RECSPLIT
164#endif
165
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200166#if defined(MBEDTLS_DHM_C)
167#define USAGE_DHMLEN \
168 " dhmlen=%%d default: (library default: 1024 bits)\n"
169#else
170#define USAGE_DHMLEN
171#endif
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200174#define USAGE_ALPN \
175 " alpn=%%s default: \"\" (disabled)\n" \
176 " example: spdy/1,http/1.1\n"
177#else
178#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200182#define USAGE_DTLS \
183 " dtls=%%d default: 0 (TLS)\n" \
184 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
185 " range of DTLS handshake timeouts in millisecs\n"
186#else
187#define USAGE_DTLS ""
188#endif
189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200191#define USAGE_FALLBACK \
192 " fallback=0/1 default: (library default: off)\n"
193#else
194#define USAGE_FALLBACK ""
195#endif
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200198#define USAGE_EMS \
199 " extended_ms=0/1 default: (library default: on)\n"
200#else
201#define USAGE_EMS ""
202#endif
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100205#define USAGE_ETM \
206 " etm=0/1 default: (library default: on)\n"
207#else
208#define USAGE_ETM ""
209#endif
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100212#define USAGE_RENEGO \
213 " renegotiation=%%d default: 0 (disabled)\n" \
214 " renegotiate=%%d default: 0 (disabled)\n"
215#else
216#define USAGE_RENEGO ""
217#endif
218
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200219#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
220#define USAGE_ECJPAKE \
221 " ecjpake_pw=%%s default: none (disabled)\n"
222#else
223#define USAGE_ECJPAKE ""
224#endif
225
Paul Bakker9caf2d22010-02-18 19:37:19 +0000226#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000227 "\n usage: ssl_client2 param=<>...\n" \
228 "\n acceptable parameters:\n" \
229 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100230 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000231 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000232 " request_page=%%s default: \".\"\n" \
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +0200233 " request_size=%%d default: about 34 (basic request)\n" \
234 " (minimum: 0, max: 16384)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100235 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100236 " nbio=%%d default: 0 (blocking I/O)\n" \
237 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200238 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200239 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100240 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200241 USAGE_DTLS \
242 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100243 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100244 " options: none, optional, required\n" \
245 USAGE_IO \
246 "\n" \
247 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200248 USAGE_ECJPAKE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100249 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100250 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100251 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200252 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200253 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200254 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200255 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200256 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100257 USAGE_MAX_FRAG_LEN \
258 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200259 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200260 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200261 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100262 USAGE_ETM \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100263 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200264 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000265 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100266 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200267 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200268 " min_version=%%s default: (library default: tls1)\n" \
269 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000270 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100271 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000272 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000273 " force_ciphersuite=<name> default: all enabled\n"\
274 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000275
Rich Evans85b05ec2015-02-12 11:37:29 +0000276/*
277 * global options
278 */
279struct options
280{
281 const char *server_name; /* hostname of the server (client only) */
282 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200283 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000284 int debug_level; /* level of debugging */
285 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000287 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000288 const char *request_page; /* page on server to request */
289 int request_size; /* pad request with header to requested size */
290 const char *ca_file; /* the file with the CA certificate(s) */
291 const char *ca_path; /* the path with the CA certificate(s) reside */
292 const char *crt_file; /* the file with the client certificate */
293 const char *key_file; /* the file with the client key */
294 const char *psk; /* the pre-shared key */
295 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200296 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000297 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
298 int renegotiation; /* enable / disable renegotiation */
299 int allow_legacy; /* allow legacy renegotiation */
300 int renegotiate; /* attempt renegotiation? */
301 int renego_delay; /* delay before enforcing renegotiation */
302 int exchanges; /* number of data exchanges */
303 int min_version; /* minimum protocol version accepted */
304 int max_version; /* maximum protocol version accepted */
305 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200306 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000307 int auth_mode; /* verify mode for connection */
308 unsigned char mfl_code; /* code for maximum fragment length */
309 int trunc_hmac; /* negotiate truncated hmac or not */
310 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200311 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000312 int reconnect; /* attempt to resume session */
313 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200314 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000315 int tickets; /* enable / disable session tickets */
316 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000317 int transport; /* TLS or DTLS? */
318 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
319 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Rich Evans85b05ec2015-02-12 11:37:29 +0000320 int fallback; /* is this a fallback connection? */
321 int extended_ms; /* negotiate extended master secret? */
322 int etm; /* negotiate encrypt then mac? */
323} opt;
324
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200325static void my_debug( void *ctx, int level,
326 const char *file, int line,
327 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000328{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200329 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000330
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200331 /* Extract basename from file */
332 for( p = basename = file; *p != '\0'; p++ )
333 if( *p == '/' || *p == '\\' )
334 basename = p + 1;
335
336 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000337 fflush( (FILE *) ctx );
338}
339
340/*
341 * Test recv/send functions that make sure each try returns
342 * WANT_READ/WANT_WRITE at least once before sucesseding
343 */
344static int my_recv( void *ctx, unsigned char *buf, size_t len )
345{
346 static int first_try = 1;
347 int ret;
348
349 if( first_try )
350 {
351 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100352 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000353 }
354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100356 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000357 first_try = 1; /* Next call will be a new operation */
358 return( ret );
359}
360
361static int my_send( void *ctx, const unsigned char *buf, size_t len )
362{
363 static int first_try = 1;
364 int ret;
365
366 if( first_try )
367 {
368 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100369 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000370 }
371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100373 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000374 first_try = 1; /* Next call will be a new operation */
375 return( ret );
376}
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378#if defined(MBEDTLS_X509_CRT_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000379/*
380 * Enabled if debug_level > 1 in code below
381 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200382static int my_verify( void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000383{
384 char buf[1024];
385 ((void) data);
386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
388 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
389 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000390
Rich Evans85b05ec2015-02-12 11:37:29 +0000391 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100393 else
394 {
395 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
396 mbedtls_printf( "%s\n", buf );
397 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000398
399 return( 0 );
400}
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200401
402static int ssl_sig_hashes_for_test[] = {
403#if defined(MBEDTLS_SHA512_C)
404 MBEDTLS_MD_SHA512,
405 MBEDTLS_MD_SHA384,
406#endif
407#if defined(MBEDTLS_SHA256_C)
408 MBEDTLS_MD_SHA256,
409 MBEDTLS_MD_SHA224,
410#endif
411#if defined(MBEDTLS_SHA1_C)
412 /* Allow SHA-1 as we use it extensively in tests. */
413 MBEDTLS_MD_SHA1,
414#endif
415 MBEDTLS_MD_NONE
416};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000418
Paul Bakker9caf2d22010-02-18 19:37:19 +0000419int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000420{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200421 int ret = 0, len, tail_len, i, written, frags, retry_left;
422 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 unsigned char buf[MBEDTLS_SSL_MAX_CONTENT_LEN + 1];
424#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
425 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200426 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200427#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200429 const char *alpn_list[10];
430#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200431 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000432
Gilles Peskineef86ab22017-05-05 18:59:02 +0200433#if defined(MBEDTLS_X509_CRT_PARSE_C)
434 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
435#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_entropy_context entropy;
437 mbedtls_ctr_drbg_context ctr_drbg;
438 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200439 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200441#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200442 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200443#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200445 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_x509_crt cacert;
447 mbedtls_x509_crt clicert;
448 mbedtls_pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200449#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000450 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000451 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000452
Paul Bakker1a207ec2011-02-06 13:22:40 +0000453 /*
454 * Make sure memory references are valid.
455 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200456 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200457 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200458 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200460 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#if defined(MBEDTLS_X509_CRT_PARSE_C)
462 mbedtls_x509_crt_init( &cacert );
463 mbedtls_x509_crt_init( &clicert );
464 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200465#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200467 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200468#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000469
Paul Bakker9caf2d22010-02-18 19:37:19 +0000470 if( argc == 0 )
471 {
472 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000473 if( ret == 0 )
474 ret = 1;
475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000479 while( *list )
480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200482 list++;
483 if( !*list )
484 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000486 list++;
487 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000489 goto exit;
490 }
491
492 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100493 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000494 opt.server_port = DFL_SERVER_PORT;
495 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100496 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200497 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200498 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000499 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200500 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000501 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000502 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000503 opt.crt_file = DFL_CRT_FILE;
504 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200505 opt.psk = DFL_PSK;
506 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200507 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakker51936882011-02-20 16:05:58 +0000508 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000509 opt.renegotiation = DFL_RENEGOTIATION;
510 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100511 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200512 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000513 opt.min_version = DFL_MIN_VERSION;
514 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100515 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +0200516 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100517 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200518 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200519 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100520 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200521 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200522 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100523 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200524 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200525 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200526 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200527 opt.transport = DFL_TRANSPORT;
528 opt.hs_to_min = DFL_HS_TO_MIN;
529 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200530 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200531 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100532 opt.etm = DFL_ETM;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000533
534 for( i = 1; i < argc; i++ )
535 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000536 p = argv[i];
537 if( ( q = strchr( p, '=' ) ) == NULL )
538 goto usage;
539 *q++ = '\0';
540
541 if( strcmp( p, "server_name" ) == 0 )
542 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100543 else if( strcmp( p, "server_addr" ) == 0 )
544 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000545 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200546 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100547 else if( strcmp( p, "dtls" ) == 0 )
548 {
549 int t = atoi( q );
550 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100552 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100554 else
555 goto usage;
556 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000557 else if( strcmp( p, "debug_level" ) == 0 )
558 {
559 opt.debug_level = atoi( q );
560 if( opt.debug_level < 0 || opt.debug_level > 65535 )
561 goto usage;
562 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100563 else if( strcmp( p, "nbio" ) == 0 )
564 {
565 opt.nbio = atoi( q );
566 if( opt.nbio < 0 || opt.nbio > 2 )
567 goto usage;
568 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200569 else if( strcmp( p, "read_timeout" ) == 0 )
570 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200571 else if( strcmp( p, "max_resend" ) == 0 )
572 {
573 opt.max_resend = atoi( q );
574 if( opt.max_resend < 0 )
575 goto usage;
576 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000577 else if( strcmp( p, "request_page" ) == 0 )
578 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200579 else if( strcmp( p, "request_size" ) == 0 )
580 {
581 opt.request_size = atoi( q );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 if( opt.request_size < 0 || opt.request_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker93c32b22014-04-25 13:40:05 +0200583 goto usage;
584 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000585 else if( strcmp( p, "ca_file" ) == 0 )
586 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000587 else if( strcmp( p, "ca_path" ) == 0 )
588 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000589 else if( strcmp( p, "crt_file" ) == 0 )
590 opt.crt_file = q;
591 else if( strcmp( p, "key_file" ) == 0 )
592 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200593 else if( strcmp( p, "psk" ) == 0 )
594 opt.psk = q;
595 else if( strcmp( p, "psk_identity" ) == 0 )
596 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200597 else if( strcmp( p, "ecjpake_pw" ) == 0 )
598 opt.ecjpake_pw = q;
Paul Bakker51936882011-02-20 16:05:58 +0000599 else if( strcmp( p, "force_ciphersuite" ) == 0 )
600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +0000602
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200603 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000604 {
605 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000606 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000607 }
Paul Bakker51936882011-02-20 16:05:58 +0000608 opt.force_ciphersuite[1] = 0;
609 }
Paul Bakker48916f92012-09-16 19:57:18 +0000610 else if( strcmp( p, "renegotiation" ) == 0 )
611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 opt.renegotiation = (atoi( q )) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED :
613 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +0000614 }
615 else if( strcmp( p, "allow_legacy" ) == 0 )
616 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100617 switch( atoi( q ) )
618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 case -1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE; break;
620 case 0: opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; break;
621 case 1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION; break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100622 default: goto usage;
623 }
Paul Bakker48916f92012-09-16 19:57:18 +0000624 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100625 else if( strcmp( p, "renegotiate" ) == 0 )
626 {
627 opt.renegotiate = atoi( q );
628 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
629 goto usage;
630 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200631 else if( strcmp( p, "exchanges" ) == 0 )
632 {
633 opt.exchanges = atoi( q );
634 if( opt.exchanges < 1 )
635 goto usage;
636 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200637 else if( strcmp( p, "reconnect" ) == 0 )
638 {
639 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200640 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200641 goto usage;
642 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100643 else if( strcmp( p, "reco_delay" ) == 0 )
644 {
645 opt.reco_delay = atoi( q );
646 if( opt.reco_delay < 0 )
647 goto usage;
648 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200649 else if( strcmp( p, "reconnect_hard" ) == 0 )
650 {
651 opt.reconnect_hard = atoi( q );
652 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
653 goto usage;
654 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200655 else if( strcmp( p, "tickets" ) == 0 )
656 {
657 opt.tickets = atoi( q );
658 if( opt.tickets < 0 || opt.tickets > 2 )
659 goto usage;
660 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200661 else if( strcmp( p, "alpn" ) == 0 )
662 {
663 opt.alpn_string = q;
664 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200665 else if( strcmp( p, "fallback" ) == 0 )
666 {
667 switch( atoi( q ) )
668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
670 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200671 default: goto usage;
672 }
673 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200674 else if( strcmp( p, "extended_ms" ) == 0 )
675 {
676 switch( atoi( q ) )
677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 case 0: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED; break;
679 case 1: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200680 default: goto usage;
681 }
682 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100683 else if( strcmp( p, "etm" ) == 0 )
684 {
685 switch( atoi( q ) )
686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
688 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100689 default: goto usage;
690 }
691 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000692 else if( strcmp( p, "min_version" ) == 0 )
693 {
694 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000696 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100698 else if( strcmp( q, "tls1_1" ) == 0 ||
699 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100701 else if( strcmp( q, "tls1_2" ) == 0 ||
702 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000704 else
705 goto usage;
706 }
707 else if( strcmp( p, "max_version" ) == 0 )
708 {
709 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000711 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100713 else if( strcmp( q, "tls1_1" ) == 0 ||
714 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100716 else if( strcmp( q, "tls1_2" ) == 0 ||
717 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000719 else
720 goto usage;
721 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100722 else if( strcmp( p, "arc4" ) == 0 )
723 {
724 switch( atoi( q ) )
725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
727 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100728 default: goto usage;
729 }
730 }
Gilles Peskinebc70a182017-05-09 15:59:24 +0200731 else if( strcmp( p, "allow_sha1" ) == 0 )
732 {
733 switch( atoi( q ) )
734 {
735 case 0: opt.allow_sha1 = 0; break;
736 case 1: opt.allow_sha1 = 1; break;
737 default: goto usage;
738 }
739 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000740 else if( strcmp( p, "force_version" ) == 0 )
741 {
742 if( strcmp( q, "ssl3" ) == 0 )
743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
745 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000746 }
747 else if( strcmp( q, "tls1" ) == 0 )
748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
750 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000751 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100752 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
755 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000756 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100757 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
760 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000761 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100762 else if( strcmp( q, "dtls1" ) == 0 )
763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
765 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
766 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100767 }
768 else if( strcmp( q, "dtls1_2" ) == 0 )
769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
771 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
772 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100773 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000774 else
775 goto usage;
776 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100777 else if( strcmp( p, "auth_mode" ) == 0 )
778 {
779 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100781 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100783 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100785 else
786 goto usage;
787 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200788 else if( strcmp( p, "max_frag_len" ) == 0 )
789 {
790 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200792 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200794 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200796 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200798 else
799 goto usage;
800 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200801 else if( strcmp( p, "trunc_hmac" ) == 0 )
802 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100803 switch( atoi( q ) )
804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
806 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100807 default: goto usage;
808 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200809 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200810 else if( strcmp( p, "hs_timeout" ) == 0 )
811 {
812 if( ( p = strchr( q, '-' ) ) == NULL )
813 goto usage;
814 *p++ = '\0';
815 opt.hs_to_min = atoi( q );
816 opt.hs_to_max = atoi( p );
817 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
818 goto usage;
819 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100820 else if( strcmp( p, "recsplit" ) == 0 )
821 {
822 opt.recsplit = atoi( q );
823 if( opt.recsplit < 0 || opt.recsplit > 1 )
824 goto usage;
825 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200826 else if( strcmp( p, "dhmlen" ) == 0 )
827 {
828 opt.dhmlen = atoi( q );
829 if( opt.dhmlen < 0 )
830 goto usage;
831 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000832 else
833 goto usage;
834 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#if defined(MBEDTLS_DEBUG_C)
837 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200838#endif
839
Paul Bakkerc1516be2013-06-29 16:01:32 +0200840 if( opt.force_ciphersuite[0] > 0 )
841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
843 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +0200844
Paul Bakker66c48102013-07-26 14:05:32 +0200845 if( opt.max_version != -1 &&
846 ciphersuite_info->min_minor_ver > opt.max_version )
847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakker66c48102013-07-26 14:05:32 +0200849 ret = 2;
850 goto usage;
851 }
852 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200853 ciphersuite_info->max_minor_ver < opt.min_version )
854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +0200856 ret = 2;
857 goto usage;
858 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100859
860 /* If the server selects a version that's not supported by
861 * this suite, then there will be no common ciphersuite... */
862 if( opt.max_version == -1 ||
863 opt.max_version > ciphersuite_info->max_minor_ver )
864 {
Paul Bakker66c48102013-07-26 14:05:32 +0200865 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100866 }
Paul Bakker66c48102013-07-26 14:05:32 +0200867 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100868 {
Paul Bakker66c48102013-07-26 14:05:32 +0200869 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100870 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
872 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
873 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100874 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000875
876 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000882 ret = 2;
883 goto usage;
884 }
885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000887 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200888 }
889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000891 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200892 * Unhexify the pre-shared key if any is given
893 */
894 if( strlen( opt.psk ) )
895 {
896 unsigned char c;
897 size_t j;
898
899 if( strlen( opt.psk ) % 2 != 0 )
900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200902 goto exit;
903 }
904
905 psk_len = strlen( opt.psk ) / 2;
906
907 for( j = 0; j < strlen( opt.psk ); j += 2 )
908 {
909 c = opt.psk[j];
910 if( c >= '0' && c <= '9' )
911 c -= '0';
912 else if( c >= 'a' && c <= 'f' )
913 c -= 'a' - 10;
914 else if( c >= 'A' && c <= 'F' )
915 c -= 'A' - 10;
916 else
917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200919 goto exit;
920 }
921 psk[ j / 2 ] = c << 4;
922
923 c = opt.psk[j + 1];
924 if( c >= '0' && c <= '9' )
925 c -= '0';
926 else if( c >= 'a' && c <= 'f' )
927 c -= 'a' - 10;
928 else if( c >= 'A' && c <= 'F' )
929 c -= 'A' - 10;
930 else
931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200933 goto exit;
934 }
935 psk[ j / 2 ] |= c;
936 }
937 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200941 if( opt.alpn_string != NULL )
942 {
943 p = (char *) opt.alpn_string;
944 i = 0;
945
946 /* Leave room for a final NULL in alpn_list */
947 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
948 {
949 alpn_list[i++] = p;
950
951 /* Terminate the current string and move on to next one */
952 while( *p != ',' && *p != '\0' )
953 p++;
954 if( *p == ',' )
955 *p++ = '\0';
956 }
957 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200959
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200960 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000961 * 0. Initialize the RNG and the session data
962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000964 fflush( stdout );
965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200967 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200968 (const unsigned char *) pers,
969 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000970 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200971 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n", -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000972 goto exit;
973 }
974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000978 /*
979 * 1.1. Load the trusted CA
980 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000982 fflush( stdout );
983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +0000985 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100986 if( strcmp( opt.ca_path, "none" ) == 0 )
987 ret = 0;
988 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +0000990 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100991 if( strcmp( opt.ca_file, "none" ) == 0 )
992 ret = 0;
993 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +0200995 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000996#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997#if defined(MBEDTLS_CERTS_C)
998 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +0100999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 ret = mbedtls_x509_crt_parse( &cacert,
1001 (const unsigned char *) mbedtls_test_cas[i],
1002 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001003 if( ret != 0 )
1004 break;
1005 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001006#else
1007 {
1008 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001010 }
1011#endif
Paul Bakker42488232012-05-16 08:21:05 +00001012 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001015 goto exit;
1016 }
1017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001019
1020 /*
1021 * 1.2. Load own certificate and private key
1022 *
1023 * (can be skipped if client authentication is not required)
1024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001026 fflush( stdout );
1027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001029 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001030 if( strcmp( opt.crt_file, "none" ) == 0 )
1031 ret = 0;
1032 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001034 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001035#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036#if defined(MBEDTLS_CERTS_C)
1037 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
1038 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001039#else
1040 {
1041 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001043 }
1044#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001045 if( ret != 0 )
1046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001048 goto exit;
1049 }
1050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001052 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001053 if( strcmp( opt.key_file, "none" ) == 0 )
1054 ret = 0;
1055 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001057 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001058#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#if defined(MBEDTLS_CERTS_C)
1060 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_cli_key,
1061 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001062#else
1063 {
1064 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001066 }
1067#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001068 if( ret != 0 )
1069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001071 goto exit;
1072 }
1073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 mbedtls_printf( " ok\n" );
1075#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001076
1077 /*
1078 * 2. Start the connection
1079 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001080 if( opt.server_addr == NULL)
1081 opt.server_addr = opt.server_name;
1082
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001083 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001085 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001086 fflush( stdout );
1087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
1089 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1090 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001093 goto exit;
1094 }
1095
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001096 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001097 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001098 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001099 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001100 if( ret != 0 )
1101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001103 goto exit;
1104 }
1105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001107
1108 /*
1109 * 3. Setup stuff
1110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 fflush( stdout );
1113
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001114 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1115 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001116 opt.transport,
1117 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001118 {
1119 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
1120 goto exit;
1121 }
1122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001124 /* The default algorithms profile disables SHA-1, but our tests still
1125 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001126 if( opt.allow_sha1 > 0 )
1127 {
1128 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1129 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1130 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1131 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001132
Paul Bakker915275b2012-09-28 07:10:55 +00001133 if( opt.debug_level > 0 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001134 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
Gilles Peskineef86ab22017-05-05 18:59:02 +02001135#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001136
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001137 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001138 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001141 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 +02001142 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001146 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001147 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001148 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001149 goto exit;
1150 }
Paul Bakker05decb22013-08-15 13:33:48 +02001151#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001154 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001155 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001156#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001159 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001160 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001161#endif
1162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001164 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001165 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001166#endif
1167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001169 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001170 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1172 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001173#endif
1174
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001175#if defined(MBEDTLS_DHM_C)
1176 if( opt.dhmlen != DFL_DHMLEN )
1177 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1178#endif
1179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001181 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001182 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001183 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001184 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001185 goto exit;
1186 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001187#endif
1188
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001189 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1190 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001191
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001192 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02001195 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02001196#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001197
Paul Bakker645ce3a2012-10-31 12:32:41 +00001198 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001199 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001200
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001201#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001202 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001203 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001204#endif
Paul Bakker51936882011-02-20 16:05:58 +00001205
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001206 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001207 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001209 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001210#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001213 if( strcmp( opt.ca_path, "none" ) != 0 &&
1214 strcmp( opt.ca_file, "none" ) != 0 )
1215 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001216 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001217 }
1218 if( strcmp( opt.crt_file, "none" ) != 0 &&
1219 strcmp( opt.key_file, "none" ) != 0 )
1220 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001221 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001222 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001223 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001224 goto exit;
1225 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001226 }
Paul Bakkered27a042013-04-18 22:46:23 +02001227#endif
1228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001230 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001231 (const unsigned char *) opt.psk_identity,
1232 strlen( opt.psk_identity ) ) ) != 0 )
1233 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001234 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001235 goto exit;
1236 }
Paul Bakkered27a042013-04-18 22:46:23 +02001237#endif
1238
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001239 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001240 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001241
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001242 if( opt.max_version != DFL_MAX_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001243 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001246 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001247 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001248#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001250 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
1251 {
1252 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
1253 goto exit;
1254 }
1255
1256#if defined(MBEDTLS_X509_CRT_PARSE_C)
1257 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1258 {
1259 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
1260 goto exit;
1261 }
1262#endif
1263
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001264#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1265 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
1266 {
1267 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
1268 (const unsigned char *) opt.ecjpake_pw,
1269 strlen( opt.ecjpake_pw ) ) ) != 0 )
1270 {
1271 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
1272 goto exit;
1273 }
1274 }
1275#endif
1276
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001277 if( opt.nbio == 2 )
1278 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
1279 else
1280 mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02001281 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001282
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001283#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001284 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
1285 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001286#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001289
Paul Bakker5121ce52009-01-03 21:22:43 +00001290 /*
1291 * 4. Handshake
1292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001294 fflush( stdout );
1295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001298 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker40e46942009-01-03 21:51:57 +00001299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n", -ret );
1301 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
1302 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001303 " Unable to verify the server's certificate. "
1304 "Either it is invalid,\n"
1305 " or you didn't set ca_file or ca_path "
1306 "to an appropriate value.\n"
1307 " Alternatively, you may want to use "
1308 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001310 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00001311 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 }
1313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1315 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
1318 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001319 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001321
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02001322#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1323 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
1324 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
1325#endif
1326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001328 if( opt.alpn_string != NULL )
1329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
1331 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001332 alp ? alp : "(none)" );
1333 }
1334#endif
1335
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001336 if( opt.reconnect != 0 )
1337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001339 fflush( stdout );
1340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001344 goto exit;
1345 }
1346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001348 }
1349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001351 /*
1352 * 5. Verify the server certificate
1353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001355
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001356 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001358 char vrfy_buf[512];
1359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001362 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00001363
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001364 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 }
1366 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 mbedtls_printf( " . Peer certificate information ...\n" );
1372 mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1373 mbedtls_ssl_get_peer_cert( &ssl ) );
1374 mbedtls_printf( "%s\n", buf );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001375 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001379 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001380 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001381 /*
1382 * Perform renegotiation (this must be done when the server is waiting
1383 * for input from our side).
1384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001386 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001388 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001389 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1390 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001393 goto exit;
1394 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001395 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001397 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001399
Paul Bakker5121ce52009-01-03 21:22:43 +00001400 /*
1401 * 6. Write the GET request
1402 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001403 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001404send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406 fflush( stdout );
1407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 len = mbedtls_snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST,
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001409 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001410 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001411
1412 /* Add padding to GET request to reach opt.request_size in length */
1413 if( opt.request_size != DFL_REQUEST_SIZE &&
1414 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001415 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001416 memset( buf + len, 'A', opt.request_size - len - tail_len );
1417 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001418 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001419
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001420 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof(buf) - len - 1 );
1421 len += tail_len;
1422
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001423 /* Truncate if request size is smaller than the "natural" size */
1424 if( opt.request_size != DFL_REQUEST_SIZE &&
1425 len > opt.request_size )
1426 {
1427 len = opt.request_size;
1428
1429 /* Still end with \r\n unless that's really not possible */
1430 if( len >= 2 ) buf[len - 2] = '\r';
1431 if( len >= 1 ) buf[len - 1] = '\n';
1432 }
1433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001435 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001436 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001439 <= 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001440 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001441 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1442 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001445 goto exit;
1446 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001447 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001448 }
1449 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001450 else /* Not stream, so datagram */
1451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 do ret = mbedtls_ssl_write( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001453 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1454 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001455
1456 if( ret < 0 )
1457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001459 goto exit;
1460 }
1461
1462 frags = 1;
1463 written = ret;
1464 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001465
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001466 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001468
1469 /*
1470 * 7. Read the HTTP response
1471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001473 fflush( stdout );
1474
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001475 /*
1476 * TLS and DTLS need different reading styles (stream vs datagram)
1477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001479 {
1480 do
1481 {
1482 len = sizeof( buf ) - 1;
1483 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001485
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001486 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1487 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001488 continue;
1489
1490 if( ret <= 0 )
1491 {
1492 switch( ret )
1493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1495 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001496 ret = 0;
1497 goto close_notify;
1498
1499 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 case MBEDTLS_ERR_NET_CONN_RESET:
1501 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001502 ret = 0;
1503 goto reconnect;
1504
1505 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001507 goto exit;
1508 }
1509 }
1510
1511 len = ret;
1512 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
1515 /* End of message should be detected according to the syntax of the
1516 * application protocol (eg HTTP), just use a dummy test here. */
1517 if( ret > 0 && buf[len-1] == '\n' )
1518 {
1519 ret = 0;
1520 break;
1521 }
1522 }
1523 while( 1 );
1524 }
1525 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 {
1527 len = sizeof( buf ) - 1;
1528 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 do ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001531 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1532 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001533
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001534 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001536 switch( ret )
1537 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001538 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001540 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001541 goto send_request;
1542 goto exit;
1543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1545 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001546 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001547 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00001548
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001549 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001551 goto exit;
1552 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001553 }
1554
Paul Bakker5121ce52009-01-03 21:22:43 +00001555 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02001556 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001558 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001559 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001560
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001561 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001562 * 7b. Simulate hard reset and reconnect from same port?
1563 */
1564 if( opt.reconnect_hard != 0 )
1565 {
1566 opt.reconnect_hard = 0;
1567
1568 mbedtls_printf( " . Restarting connection from same port..." );
1569 fflush( stdout );
1570
1571 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
1572 {
1573 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", -ret );
1574 goto exit;
1575 }
1576
1577 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
1578 {
1579 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1580 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1581 {
1582 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
1583 goto exit;
1584 }
1585 }
1586
1587 mbedtls_printf( " ok\n" );
1588
1589 goto send_request;
1590 }
1591
1592 /*
1593 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001594 */
1595 if( --opt.exchanges > 0 )
1596 goto send_request;
1597
1598 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001599 * 8. Done, cleanly close the connection
1600 */
1601close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001603 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001604
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001605 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001607 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001608 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001611
1612 /*
1613 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001614 */
1615reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001616 if( opt.reconnect != 0 )
1617 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001618 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001619
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001620 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001621
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001622#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001623 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02001624 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001625#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02001626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001632 goto exit;
1633 }
1634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001636 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001637 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001638 goto exit;
1639 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
1642 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1643 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001646 goto exit;
1647 }
1648
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001649 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001650 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001651 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001652 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001653 if( ret != 0 )
1654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001656 -ret );
1657 goto exit;
1658 }
1659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001661 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001662 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1663 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001666 goto exit;
1667 }
1668 }
1669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001671
1672 goto send_request;
1673 }
1674
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001675 /*
1676 * Cleanup and exit
1677 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001678exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00001680 if( ret != 0 )
1681 {
1682 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 mbedtls_strerror( ret, error_buf, 100 );
1684 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001685 }
1686#endif
1687
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001688 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690#if defined(MBEDTLS_X509_CRT_PARSE_C)
1691 mbedtls_x509_crt_free( &clicert );
1692 mbedtls_x509_crt_free( &cacert );
1693 mbedtls_pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001694#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 mbedtls_ssl_session_free( &saved_session );
1696 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001697 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 mbedtls_ctr_drbg_free( &ctr_drbg );
1699 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00001700
Paul Bakkercce9d772011-11-18 14:26:47 +00001701#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001703 fflush( stdout ); getchar();
1704#endif
1705
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001706 // Shell can not handle large exit numbers -> 1 for errors
1707 if( ret < 0 )
1708 ret = 1;
1709
Paul Bakker5121ce52009-01-03 21:22:43 +00001710 return( ret );
1711}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
1713 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001714 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */