blob: e82adaa7b77664d764d2e8c82f6f982e7f6da97c [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
Hanno Becker16970d22017-10-10 15:56:37 +010073#define DFL_EVENT 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020074#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +020075#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +000076#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000077#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000078#define DFL_CRT_FILE ""
79#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020080#define DFL_PSK ""
81#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +020082#define DFL_ECJPAKE_PW NULL
Paul Bakker51936882011-02-20 16:05:58 +000083#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010085#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010086#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +020087#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +020088#define DFL_MIN_VERSION -1
Paul Bakker1d29fb52012-09-28 13:28:45 +000089#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +000090#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +020091#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +010092#define DFL_AUTH_MODE -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +010094#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +010095#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +020096#define DFL_DHMLEN -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +020097#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010098#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +020099#define DFL_RECONNECT_HARD 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200101#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100102#define DFL_CURVES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200104#define DFL_HS_TO_MIN 0
105#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200106#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200107#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100108#define DFL_ETM -1
Paul Bakker0e6975b2009-02-10 22:19:10 +0000109
Paul Bakker93c32b22014-04-25 13:40:05 +0200110#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
111#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_X509_CRT_PARSE_C)
114#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000115#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100116 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
117 " default: \"\" (pre-loaded)\n" \
118 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
119 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
120 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
121 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000122 " key_file=%%s default: \"\" (pre-loaded)\n"
123#else
124#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 " No file operations available (MBEDTLS_FS_IO not defined)\n"
126#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200127#else
128#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200132#define USAGE_PSK \
133 " psk=%%s default: \"\" (in hex, without 0x)\n" \
134 " psk_identity=%%s default: \"Client_identity\"\n"
135#else
136#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200140#define USAGE_TICKETS \
141 " tickets=%%d default: 1 (enabled)\n"
142#else
143#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200147#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100148 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200149#else
150#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200154#define USAGE_MAX_FRAG_LEN \
155 " max_frag_len=%%d default: 16384 (tls default)\n" \
156 " options: 512, 1024, 2048, 4096\n"
157#else
158#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100162#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200163 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100164#else
165#define USAGE_RECSPLIT
166#endif
167
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200168#if defined(MBEDTLS_DHM_C)
169#define USAGE_DHMLEN \
170 " dhmlen=%%d default: (library default: 1024 bits)\n"
171#else
172#define USAGE_DHMLEN
173#endif
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200176#define USAGE_ALPN \
177 " alpn=%%s default: \"\" (disabled)\n" \
178 " example: spdy/1,http/1.1\n"
179#else
180#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200182
Hanno Beckere6706e62017-05-15 16:05:15 +0100183#if defined(MBEDTLS_ECP_C)
184#define USAGE_CURVES \
185 " curves=a,b,c,d default: \"default\" (library default)\n" \
186 " example: \"secp521r1,brainpoolP512r1\"\n" \
187 " - use \"none\" for empty list\n" \
188 " - see mbedtls_ecp_curve_list()\n" \
189 " for acceptable curve names\n"
190#else
191#define USAGE_CURVES ""
192#endif
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200195#define USAGE_DTLS \
196 " dtls=%%d default: 0 (TLS)\n" \
197 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
198 " range of DTLS handshake timeouts in millisecs\n"
199#else
200#define USAGE_DTLS ""
201#endif
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200204#define USAGE_FALLBACK \
205 " fallback=0/1 default: (library default: off)\n"
206#else
207#define USAGE_FALLBACK ""
208#endif
209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200211#define USAGE_EMS \
212 " extended_ms=0/1 default: (library default: on)\n"
213#else
214#define USAGE_EMS ""
215#endif
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100218#define USAGE_ETM \
219 " etm=0/1 default: (library default: on)\n"
220#else
221#define USAGE_ETM ""
222#endif
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100225#define USAGE_RENEGO \
226 " renegotiation=%%d default: 0 (disabled)\n" \
227 " renegotiate=%%d default: 0 (disabled)\n"
228#else
229#define USAGE_RENEGO ""
230#endif
231
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200232#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
233#define USAGE_ECJPAKE \
234 " ecjpake_pw=%%s default: none (disabled)\n"
235#else
236#define USAGE_ECJPAKE ""
237#endif
238
Paul Bakker9caf2d22010-02-18 19:37:19 +0000239#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000240 "\n usage: ssl_client2 param=<>...\n" \
241 "\n acceptable parameters:\n" \
242 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100243 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000244 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000245 " request_page=%%s default: \".\"\n" \
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +0200246 " request_size=%%d default: about 34 (basic request)\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100247 " (minimum: 0, max: 16384)\n" \
248 " debug_level=%%d default: 0 (disabled)\n" \
249 " nbio=%%d default: 0 (blocking I/O)\n" \
250 " options: 1 (non-blocking), 2 (added delays)\n" \
251 " event=%%d default: 0 (loop)\n" \
252 " options: 1 (level-triggered, implies nbio=1),\n" \
253 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200254 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100255 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200256 USAGE_DTLS \
257 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100258 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100259 " options: none, optional, required\n" \
260 USAGE_IO \
261 "\n" \
262 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200263 USAGE_ECJPAKE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100264 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100265 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100266 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200267 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200268 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200269 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200270 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200271 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100272 USAGE_MAX_FRAG_LEN \
273 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200274 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200275 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200276 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100277 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100278 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100279 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200280 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000281 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100282 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200283 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200284 " min_version=%%s default: (library default: tls1)\n" \
285 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000286 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100287 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000288 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000289 " force_ciphersuite=<name> default: all enabled\n"\
290 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000291
Hanno Becker8651a432017-06-09 16:13:22 +0100292#define ALPN_LIST_SIZE 10
293#define CURVE_LIST_SIZE 20
294
Rich Evans85b05ec2015-02-12 11:37:29 +0000295/*
296 * global options
297 */
298struct options
299{
300 const char *server_name; /* hostname of the server (client only) */
301 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200302 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000303 int debug_level; /* level of debugging */
304 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100305 int event; /* loop or event-driven IO? level or edge triggered? */
306 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000307 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000308 const char *request_page; /* page on server to request */
309 int request_size; /* pad request with header to requested size */
310 const char *ca_file; /* the file with the CA certificate(s) */
311 const char *ca_path; /* the path with the CA certificate(s) reside */
312 const char *crt_file; /* the file with the client certificate */
313 const char *key_file; /* the file with the client key */
314 const char *psk; /* the pre-shared key */
315 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200316 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000317 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
318 int renegotiation; /* enable / disable renegotiation */
319 int allow_legacy; /* allow legacy renegotiation */
320 int renegotiate; /* attempt renegotiation? */
321 int renego_delay; /* delay before enforcing renegotiation */
322 int exchanges; /* number of data exchanges */
323 int min_version; /* minimum protocol version accepted */
324 int max_version; /* maximum protocol version accepted */
325 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200326 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000327 int auth_mode; /* verify mode for connection */
328 unsigned char mfl_code; /* code for maximum fragment length */
329 int trunc_hmac; /* negotiate truncated hmac or not */
330 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200331 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000332 int reconnect; /* attempt to resume session */
333 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200334 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000335 int tickets; /* enable / disable session tickets */
Hanno Beckere6706e62017-05-15 16:05:15 +0100336 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000337 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000338 int transport; /* TLS or DTLS? */
339 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
340 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Rich Evans85b05ec2015-02-12 11:37:29 +0000341 int fallback; /* is this a fallback connection? */
342 int extended_ms; /* negotiate extended master secret? */
343 int etm; /* negotiate encrypt then mac? */
344} opt;
345
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200346static void my_debug( void *ctx, int level,
347 const char *file, int line,
348 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000349{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200350 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000351
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200352 /* Extract basename from file */
353 for( p = basename = file; *p != '\0'; p++ )
354 if( *p == '/' || *p == '\\' )
355 basename = p + 1;
356
357 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000358 fflush( (FILE *) ctx );
359}
360
361/*
362 * Test recv/send functions that make sure each try returns
363 * WANT_READ/WANT_WRITE at least once before sucesseding
364 */
365static int my_recv( void *ctx, unsigned char *buf, size_t len )
366{
367 static int first_try = 1;
368 int ret;
369
370 if( first_try )
371 {
372 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100373 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000374 }
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100377 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000378 first_try = 1; /* Next call will be a new operation */
379 return( ret );
380}
381
382static int my_send( void *ctx, const unsigned char *buf, size_t len )
383{
384 static int first_try = 1;
385 int ret;
386
387 if( first_try )
388 {
389 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100390 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000391 }
392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100394 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000395 first_try = 1; /* Next call will be a new operation */
396 return( ret );
397}
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#if defined(MBEDTLS_X509_CRT_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000400/*
401 * Enabled if debug_level > 1 in code below
402 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200403static int my_verify( void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000404{
405 char buf[1024];
406 ((void) data);
407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
409 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
410 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000411
Rich Evans85b05ec2015-02-12 11:37:29 +0000412 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100414 else
415 {
416 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
417 mbedtls_printf( "%s\n", buf );
418 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000419
420 return( 0 );
421}
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200422
423static int ssl_sig_hashes_for_test[] = {
424#if defined(MBEDTLS_SHA512_C)
425 MBEDTLS_MD_SHA512,
426 MBEDTLS_MD_SHA384,
427#endif
428#if defined(MBEDTLS_SHA256_C)
429 MBEDTLS_MD_SHA256,
430 MBEDTLS_MD_SHA224,
431#endif
432#if defined(MBEDTLS_SHA1_C)
433 /* Allow SHA-1 as we use it extensively in tests. */
434 MBEDTLS_MD_SHA1,
435#endif
436 MBEDTLS_MD_NONE
437};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000439
Hanno Becker16970d22017-10-10 15:56:37 +0100440/*
441 * Wait for an event from the underlying transport or the timer
442 * (Used in event-driven IO mode).
443 */
444#if !defined(MBEDTLS_TIMING_C)
445void idle( mbedtls_ssl_context *ssl,
446 mbedtls_net_context *fd,
447 int idle_reason )
448{
449#else
450void idle( mbedtls_ssl_context *ssl,
451 mbedtls_net_context *fd,
452 mbedtls_timing_delay_context *timer,
453 int idle_reason )
454{
455#if defined(MBEDTLS_DEBUG_C)
456 struct mbedtls_timing_hr_time tm;
457 unsigned long time_elapsed;
458#endif
459#endif
460
461 int poll_type = 0;
462
463 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
464 poll_type = MBEDTLS_NET_POLL_WRITE;
465 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
466 poll_type = MBEDTLS_NET_POLL_READ;
467#if !defined(MBEDTLS_TIMING_C)
468 else
469 {
470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "WARNING: No reason for idling given" ) );
471 return;
472 }
473#endif
474
475 /* One should not idle on the underlying transport
476 * if data is still pending to be processed. */
477 if( mbedtls_ssl_check_pending( ssl ) != 0 )
478 {
479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "WARNING: Data still pending, "
480 "but idling requested!" ) );
481 }
482 MBEDTLS_SSL_DEBUG_MSG( 3, ( "idle, waiting for event... " ) );
483
484#if defined(MBEDTLS_TIMING_C) && defined(MBEDTLS_DEBUG_C)
485 mbedtls_timing_get_timer( &tm, 1 /* restart */ );
486#endif
487
488 while( 1 )
489 {
490#if defined(MBEDTLS_TIMING_C)
491#if defined(MBEDTLS_DEBUG_C)
492 time_elapsed = mbedtls_timing_get_timer( &tm, 0 );
493#endif
494 if( mbedtls_timing_get_delay( timer ) == 2 )
495 {
496 MBEDTLS_SSL_DEBUG_MSG( 3, ( "[%lu ms] timer expired - continue",
497 time_elapsed ) );
498 break;
499 }
500#endif
501
502 if( poll_type != 0 &&
503 mbedtls_net_poll( fd, poll_type, 0 ) == poll_type )
504 {
505 MBEDTLS_SSL_DEBUG_MSG( 3, ( "[%lu ms] net_context signals data - "
506 "continue", time_elapsed ) );
507 break;
508 }
509 }
510}
511
Paul Bakker9caf2d22010-02-18 19:37:19 +0000512int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000513{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200514 int ret = 0, len, tail_len, i, written, frags, retry_left;
515 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 unsigned char buf[MBEDTLS_SSL_MAX_CONTENT_LEN + 1];
517#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
518 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200519 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200520#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100522 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200523#endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100524#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +0100525 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100526 const mbedtls_ecp_curve_info *curve_cur;
527#endif
528
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200529 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000530
Gilles Peskineef86ab22017-05-05 18:59:02 +0200531#if defined(MBEDTLS_X509_CRT_PARSE_C)
532 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
533#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_entropy_context entropy;
535 mbedtls_ctr_drbg_context ctr_drbg;
536 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200537 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200539#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200540 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200541#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200543 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_x509_crt cacert;
545 mbedtls_x509_crt clicert;
546 mbedtls_pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200547#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000548 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000549 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000550
Paul Bakker1a207ec2011-02-06 13:22:40 +0000551 /*
552 * Make sure memory references are valid.
553 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200554 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200555 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200556 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200558 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559#if defined(MBEDTLS_X509_CRT_PARSE_C)
560 mbedtls_x509_crt_init( &cacert );
561 mbedtls_x509_crt_init( &clicert );
562 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200563#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200565 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200566#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000567
Paul Bakker9caf2d22010-02-18 19:37:19 +0000568 if( argc == 0 )
569 {
570 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000571 if( ret == 0 )
572 ret = 1;
573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000577 while( *list )
578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200580 list++;
581 if( !*list )
582 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000584 list++;
585 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000587 goto exit;
588 }
589
590 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100591 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000592 opt.server_port = DFL_SERVER_PORT;
593 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100594 opt.nbio = DFL_NBIO;
Hanno Becker16970d22017-10-10 15:56:37 +0100595 opt.event = DFL_EVENT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200596 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200597 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000598 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200599 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000600 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000601 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000602 opt.crt_file = DFL_CRT_FILE;
603 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200604 opt.psk = DFL_PSK;
605 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200606 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakker51936882011-02-20 16:05:58 +0000607 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000608 opt.renegotiation = DFL_RENEGOTIATION;
609 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100610 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200611 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000612 opt.min_version = DFL_MIN_VERSION;
613 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100614 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +0200615 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100616 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200617 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200618 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100619 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200620 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200621 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100622 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200623 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200624 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200625 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +0100626 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200627 opt.transport = DFL_TRANSPORT;
628 opt.hs_to_min = DFL_HS_TO_MIN;
629 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200630 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200631 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100632 opt.etm = DFL_ETM;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000633
634 for( i = 1; i < argc; i++ )
635 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000636 p = argv[i];
637 if( ( q = strchr( p, '=' ) ) == NULL )
638 goto usage;
639 *q++ = '\0';
640
641 if( strcmp( p, "server_name" ) == 0 )
642 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100643 else if( strcmp( p, "server_addr" ) == 0 )
644 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000645 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200646 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100647 else if( strcmp( p, "dtls" ) == 0 )
648 {
649 int t = atoi( q );
650 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100652 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100654 else
655 goto usage;
656 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000657 else if( strcmp( p, "debug_level" ) == 0 )
658 {
659 opt.debug_level = atoi( q );
660 if( opt.debug_level < 0 || opt.debug_level > 65535 )
661 goto usage;
662 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100663 else if( strcmp( p, "nbio" ) == 0 )
664 {
665 opt.nbio = atoi( q );
666 if( opt.nbio < 0 || opt.nbio > 2 )
667 goto usage;
668 }
Hanno Becker16970d22017-10-10 15:56:37 +0100669 else if( strcmp( p, "event" ) == 0 )
670 {
671 opt.event = atoi( q );
672 if( opt.event < 0 || opt.event > 2 )
673 goto usage;
674 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200675 else if( strcmp( p, "read_timeout" ) == 0 )
676 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200677 else if( strcmp( p, "max_resend" ) == 0 )
678 {
679 opt.max_resend = atoi( q );
680 if( opt.max_resend < 0 )
681 goto usage;
682 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000683 else if( strcmp( p, "request_page" ) == 0 )
684 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200685 else if( strcmp( p, "request_size" ) == 0 )
686 {
687 opt.request_size = atoi( q );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 if( opt.request_size < 0 || opt.request_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker93c32b22014-04-25 13:40:05 +0200689 goto usage;
690 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000691 else if( strcmp( p, "ca_file" ) == 0 )
692 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000693 else if( strcmp( p, "ca_path" ) == 0 )
694 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000695 else if( strcmp( p, "crt_file" ) == 0 )
696 opt.crt_file = q;
697 else if( strcmp( p, "key_file" ) == 0 )
698 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200699 else if( strcmp( p, "psk" ) == 0 )
700 opt.psk = q;
701 else if( strcmp( p, "psk_identity" ) == 0 )
702 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200703 else if( strcmp( p, "ecjpake_pw" ) == 0 )
704 opt.ecjpake_pw = q;
Paul Bakker51936882011-02-20 16:05:58 +0000705 else if( strcmp( p, "force_ciphersuite" ) == 0 )
706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +0000708
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200709 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000710 {
711 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000712 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000713 }
Paul Bakker51936882011-02-20 16:05:58 +0000714 opt.force_ciphersuite[1] = 0;
715 }
Paul Bakker48916f92012-09-16 19:57:18 +0000716 else if( strcmp( p, "renegotiation" ) == 0 )
717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 opt.renegotiation = (atoi( q )) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED :
719 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +0000720 }
721 else if( strcmp( p, "allow_legacy" ) == 0 )
722 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100723 switch( atoi( q ) )
724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 case -1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE; break;
726 case 0: opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; break;
727 case 1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION; break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100728 default: goto usage;
729 }
Paul Bakker48916f92012-09-16 19:57:18 +0000730 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100731 else if( strcmp( p, "renegotiate" ) == 0 )
732 {
733 opt.renegotiate = atoi( q );
734 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
735 goto usage;
736 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200737 else if( strcmp( p, "exchanges" ) == 0 )
738 {
739 opt.exchanges = atoi( q );
740 if( opt.exchanges < 1 )
741 goto usage;
742 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200743 else if( strcmp( p, "reconnect" ) == 0 )
744 {
745 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200746 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200747 goto usage;
748 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100749 else if( strcmp( p, "reco_delay" ) == 0 )
750 {
751 opt.reco_delay = atoi( q );
752 if( opt.reco_delay < 0 )
753 goto usage;
754 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200755 else if( strcmp( p, "reconnect_hard" ) == 0 )
756 {
757 opt.reconnect_hard = atoi( q );
758 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
759 goto usage;
760 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200761 else if( strcmp( p, "tickets" ) == 0 )
762 {
763 opt.tickets = atoi( q );
764 if( opt.tickets < 0 || opt.tickets > 2 )
765 goto usage;
766 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200767 else if( strcmp( p, "alpn" ) == 0 )
768 {
769 opt.alpn_string = q;
770 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200771 else if( strcmp( p, "fallback" ) == 0 )
772 {
773 switch( atoi( q ) )
774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
776 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200777 default: goto usage;
778 }
779 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200780 else if( strcmp( p, "extended_ms" ) == 0 )
781 {
782 switch( atoi( q ) )
783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 case 0: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED; break;
785 case 1: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200786 default: goto usage;
787 }
788 }
Hanno Beckere6706e62017-05-15 16:05:15 +0100789 else if( strcmp( p, "curves" ) == 0 )
790 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100791 else if( strcmp( p, "etm" ) == 0 )
792 {
793 switch( atoi( q ) )
794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
796 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100797 default: goto usage;
798 }
799 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000800 else if( strcmp( p, "min_version" ) == 0 )
801 {
802 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000804 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100806 else if( strcmp( q, "tls1_1" ) == 0 ||
807 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100809 else if( strcmp( q, "tls1_2" ) == 0 ||
810 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000812 else
813 goto usage;
814 }
815 else if( strcmp( p, "max_version" ) == 0 )
816 {
817 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000819 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100821 else if( strcmp( q, "tls1_1" ) == 0 ||
822 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100824 else if( strcmp( q, "tls1_2" ) == 0 ||
825 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000827 else
828 goto usage;
829 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100830 else if( strcmp( p, "arc4" ) == 0 )
831 {
832 switch( atoi( q ) )
833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
835 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100836 default: goto usage;
837 }
838 }
Gilles Peskinebc70a182017-05-09 15:59:24 +0200839 else if( strcmp( p, "allow_sha1" ) == 0 )
840 {
841 switch( atoi( q ) )
842 {
843 case 0: opt.allow_sha1 = 0; break;
844 case 1: opt.allow_sha1 = 1; break;
845 default: goto usage;
846 }
847 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000848 else if( strcmp( p, "force_version" ) == 0 )
849 {
850 if( strcmp( q, "ssl3" ) == 0 )
851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
853 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000854 }
855 else if( strcmp( q, "tls1" ) == 0 )
856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
858 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000859 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100860 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
863 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000864 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100865 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
868 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000869 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100870 else if( strcmp( q, "dtls1" ) == 0 )
871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
873 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
874 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100875 }
876 else if( strcmp( q, "dtls1_2" ) == 0 )
877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
879 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
880 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100881 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000882 else
883 goto usage;
884 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100885 else if( strcmp( p, "auth_mode" ) == 0 )
886 {
887 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100889 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100891 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100893 else
894 goto usage;
895 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200896 else if( strcmp( p, "max_frag_len" ) == 0 )
897 {
898 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200900 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200902 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200904 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200906 else
907 goto usage;
908 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200909 else if( strcmp( p, "trunc_hmac" ) == 0 )
910 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100911 switch( atoi( q ) )
912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
914 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100915 default: goto usage;
916 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200917 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200918 else if( strcmp( p, "hs_timeout" ) == 0 )
919 {
920 if( ( p = strchr( q, '-' ) ) == NULL )
921 goto usage;
922 *p++ = '\0';
923 opt.hs_to_min = atoi( q );
924 opt.hs_to_max = atoi( p );
925 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
926 goto usage;
927 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100928 else if( strcmp( p, "recsplit" ) == 0 )
929 {
930 opt.recsplit = atoi( q );
931 if( opt.recsplit < 0 || opt.recsplit > 1 )
932 goto usage;
933 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200934 else if( strcmp( p, "dhmlen" ) == 0 )
935 {
936 opt.dhmlen = atoi( q );
937 if( opt.dhmlen < 0 )
938 goto usage;
939 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000940 else
941 goto usage;
942 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000943
Hanno Becker16970d22017-10-10 15:56:37 +0100944 /* Event-driven IO is incompatible with the above custom
945 * receive and send functions, as the polling builds on
946 * refers to the underlying net_context. */
947 if( opt.event == 1 && opt.nbio != 1 )
948 {
949 mbedtls_printf( "Warning: event-driven IO mandates nbio=1"
950 " - overwrite\n" );
951 opt.nbio = 1;
952 }
953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954#if defined(MBEDTLS_DEBUG_C)
955 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200956#endif
957
Paul Bakkerc1516be2013-06-29 16:01:32 +0200958 if( opt.force_ciphersuite[0] > 0 )
959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
961 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +0200962
Paul Bakker66c48102013-07-26 14:05:32 +0200963 if( opt.max_version != -1 &&
964 ciphersuite_info->min_minor_ver > opt.max_version )
965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakker66c48102013-07-26 14:05:32 +0200967 ret = 2;
968 goto usage;
969 }
970 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200971 ciphersuite_info->max_minor_ver < opt.min_version )
972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +0200974 ret = 2;
975 goto usage;
976 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100977
978 /* If the server selects a version that's not supported by
979 * this suite, then there will be no common ciphersuite... */
980 if( opt.max_version == -1 ||
981 opt.max_version > ciphersuite_info->max_minor_ver )
982 {
Paul Bakker66c48102013-07-26 14:05:32 +0200983 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100984 }
Paul Bakker66c48102013-07-26 14:05:32 +0200985 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100986 {
Paul Bakker66c48102013-07-26 14:05:32 +0200987 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100988 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
990 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
991 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100992 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000993
994 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001000 ret = 2;
1001 goto usage;
1002 }
1003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001005 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001006 }
1007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001010 * Unhexify the pre-shared key if any is given
1011 */
1012 if( strlen( opt.psk ) )
1013 {
1014 unsigned char c;
1015 size_t j;
1016
1017 if( strlen( opt.psk ) % 2 != 0 )
1018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001020 goto exit;
1021 }
1022
1023 psk_len = strlen( opt.psk ) / 2;
1024
1025 for( j = 0; j < strlen( opt.psk ); j += 2 )
1026 {
1027 c = opt.psk[j];
1028 if( c >= '0' && c <= '9' )
1029 c -= '0';
1030 else if( c >= 'a' && c <= 'f' )
1031 c -= 'a' - 10;
1032 else if( c >= 'A' && c <= 'F' )
1033 c -= 'A' - 10;
1034 else
1035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001037 goto exit;
1038 }
1039 psk[ j / 2 ] = c << 4;
1040
1041 c = opt.psk[j + 1];
1042 if( c >= '0' && c <= '9' )
1043 c -= '0';
1044 else if( c >= 'a' && c <= 'f' )
1045 c -= 'a' - 10;
1046 else if( c >= 'A' && c <= 'F' )
1047 c -= 'A' - 10;
1048 else
1049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001051 goto exit;
1052 }
1053 psk[ j / 2 ] |= c;
1054 }
1055 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001057
Hanno Beckere6706e62017-05-15 16:05:15 +01001058#if defined(MBEDTLS_ECP_C)
1059 if( opt.curves != NULL )
1060 {
1061 p = (char *) opt.curves;
1062 i = 0;
1063
1064 if( strcmp( p, "none" ) == 0 )
1065 {
1066 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1067 }
1068 else if( strcmp( p, "default" ) != 0 )
1069 {
1070 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001071 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001072 {
1073 q = p;
1074
1075 /* Terminate the current string */
1076 while( *p != ',' && *p != '\0' )
1077 p++;
1078 if( *p == ',' )
1079 *p++ = '\0';
1080
1081 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1082 {
1083 curve_list[i++] = curve_cur->grp_id;
1084 }
1085 else
1086 {
1087 mbedtls_printf( "unknown curve %s\n", q );
1088 mbedtls_printf( "supported curves: " );
1089 for( curve_cur = mbedtls_ecp_curve_list();
1090 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1091 curve_cur++ )
1092 {
1093 mbedtls_printf( "%s ", curve_cur->name );
1094 }
1095 mbedtls_printf( "\n" );
1096 goto exit;
1097 }
1098 }
1099
1100 mbedtls_printf("Number of curves: %d\n", i );
1101
Hanno Becker8651a432017-06-09 16:13:22 +01001102 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001103 {
Hanno Becker8651a432017-06-09 16:13:22 +01001104 mbedtls_printf( "curves list too long, maximum %d",
1105 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001106 goto exit;
1107 }
1108
1109 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1110 }
1111 }
1112#endif /* MBEDTLS_ECP_C */
1113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001115 if( opt.alpn_string != NULL )
1116 {
1117 p = (char *) opt.alpn_string;
1118 i = 0;
1119
1120 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001121 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001122 {
1123 alpn_list[i++] = p;
1124
1125 /* Terminate the current string and move on to next one */
1126 while( *p != ',' && *p != '\0' )
1127 p++;
1128 if( *p == ',' )
1129 *p++ = '\0';
1130 }
1131 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001133
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001134 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 * 0. Initialize the RNG and the session data
1136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001138 fflush( stdout );
1139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001141 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001142 (const unsigned char *) pers,
1143 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +00001144 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001145 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n", -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001146 goto exit;
1147 }
1148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001152 /*
1153 * 1.1. Load the trusted CA
1154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 fflush( stdout );
1157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001159 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001160 if( strcmp( opt.ca_path, "none" ) == 0 )
1161 ret = 0;
1162 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001164 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001165 if( strcmp( opt.ca_file, "none" ) == 0 )
1166 ret = 0;
1167 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001169 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001170#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171#if defined(MBEDTLS_CERTS_C)
1172 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 ret = mbedtls_x509_crt_parse( &cacert,
1175 (const unsigned char *) mbedtls_test_cas[i],
1176 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001177 if( ret != 0 )
1178 break;
1179 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001180#else
1181 {
1182 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001184 }
1185#endif
Paul Bakker42488232012-05-16 08:21:05 +00001186 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 {
Hanno Becker16970d22017-10-10 15:56:37 +01001188 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse "
1189 "returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001190 goto exit;
1191 }
1192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001194
1195 /*
1196 * 1.2. Load own certificate and private key
1197 *
1198 * (can be skipped if client authentication is not required)
1199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 fflush( stdout );
1202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001204 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001205 if( strcmp( opt.crt_file, "none" ) == 0 )
1206 ret = 0;
1207 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001209 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001210#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001212 ret = mbedtls_x509_crt_parse( &clicert,
1213 (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001215#else
1216 {
1217 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001219 }
1220#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001221 if( ret != 0 )
1222 {
Hanno Becker16970d22017-10-10 15:56:37 +01001223 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse "
1224 "returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001225 goto exit;
1226 }
1227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001229 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001230 if( strcmp( opt.key_file, "none" ) == 0 )
1231 ret = 0;
1232 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001234 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001235#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001237 ret = mbedtls_pk_parse_key( &pkey,
1238 (const unsigned char *) mbedtls_test_cli_key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001240#else
1241 {
1242 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001244 }
1245#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 if( ret != 0 )
1247 {
Hanno Becker16970d22017-10-10 15:56:37 +01001248 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key "
1249 "returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 goto exit;
1251 }
1252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 mbedtls_printf( " ok\n" );
1254#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001255
1256 /*
1257 * 2. Start the connection
1258 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001259 if( opt.server_addr == NULL)
1260 opt.server_addr = opt.server_name;
1261
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001262 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001264 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001265 fflush( stdout );
1266
Hanno Becker16970d22017-10-10 15:56:37 +01001267 if( ( ret = mbedtls_net_connect( &server_fd,
1268 opt.server_addr, opt.server_port,
1269 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1270 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 {
Hanno Becker16970d22017-10-10 15:56:37 +01001272 mbedtls_printf( " failed\n ! mbedtls_net_connect "
1273 "returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001274 goto exit;
1275 }
1276
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001277 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001278 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001279 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001280 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001281 if( ret != 0 )
1282 {
Hanno Becker16970d22017-10-10 15:56:37 +01001283 mbedtls_printf( " failed\n ! net_set_(non)block() "
1284 "returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001285 goto exit;
1286 }
1287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001289
1290 /*
1291 * 3. Setup stuff
1292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001294 fflush( stdout );
1295
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001296 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1297 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001298 opt.transport,
1299 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001300 {
Hanno Becker16970d22017-10-10 15:56:37 +01001301 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults "
1302 "returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001303 goto exit;
1304 }
1305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001307 /* The default algorithms profile disables SHA-1, but our tests still
1308 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001309 if( opt.allow_sha1 > 0 )
1310 {
1311 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1312 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1313 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1314 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001315
Paul Bakker915275b2012-09-28 07:10:55 +00001316 if( opt.debug_level > 0 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001317 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
Gilles Peskineef86ab22017-05-05 18:59:02 +02001318#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001319
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001320 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001321 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001324 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Hanno Becker16970d22017-10-10 15:56:37 +01001325 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min,
1326 opt.hs_to_max );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001330 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001331 {
Hanno Becker16970d22017-10-10 15:56:37 +01001332 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len "
1333 "returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001334 goto exit;
1335 }
Paul Bakker05decb22013-08-15 13:33:48 +02001336#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001339 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001340 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001341#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001344 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001345 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001346#endif
1347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001349 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001350 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001351#endif
1352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001354 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001355 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Hanno Becker16970d22017-10-10 15:56:37 +01001356 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1357 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001358#endif
1359
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001360#if defined(MBEDTLS_DHM_C)
1361 if( opt.dhmlen != DFL_DHMLEN )
1362 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1363#endif
1364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001366 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001367 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001368 {
Hanno Becker16970d22017-10-10 15:56:37 +01001369 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols "
1370 "returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001371 goto exit;
1372 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001373#endif
1374
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001375 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1376 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001377
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001378 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02001381 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02001382#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001383
Paul Bakker645ce3a2012-10-31 12:32:41 +00001384 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001385 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001386
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001387#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001388 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001389 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001390#endif
Paul Bakker51936882011-02-20 16:05:58 +00001391
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001392 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001393 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001395 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001396#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001399 if( strcmp( opt.ca_path, "none" ) != 0 &&
1400 strcmp( opt.ca_file, "none" ) != 0 )
1401 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001402 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001403 }
1404 if( strcmp( opt.crt_file, "none" ) != 0 &&
1405 strcmp( opt.key_file, "none" ) != 0 )
1406 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001407 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001408 {
Hanno Becker16970d22017-10-10 15:56:37 +01001409 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert "
1410 "returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001411 goto exit;
1412 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001413 }
Paul Bakkered27a042013-04-18 22:46:23 +02001414#endif
1415
Hanno Beckere6706e62017-05-15 16:05:15 +01001416#if defined(MBEDTLS_ECP_C)
1417 if( opt.curves != NULL &&
1418 strcmp( opt.curves, "default" ) != 0 )
1419 {
1420 mbedtls_ssl_conf_curves( &conf, curve_list );
1421 }
1422#endif
1423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001425 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001426 (const unsigned char *) opt.psk_identity,
1427 strlen( opt.psk_identity ) ) ) != 0 )
1428 {
Hanno Becker16970d22017-10-10 15:56:37 +01001429 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk "
1430 "returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001431 goto exit;
1432 }
Paul Bakkered27a042013-04-18 22:46:23 +02001433#endif
1434
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001435 if( opt.min_version != DFL_MIN_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01001436 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1437 opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001438
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001439 if( opt.max_version != DFL_MAX_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01001440 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1441 opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001444 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001445 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001446#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001447
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001448 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
1449 {
Hanno Becker16970d22017-10-10 15:56:37 +01001450 mbedtls_printf( " failed\n ! mbedtls_ssl_setup "
1451 "returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001452 goto exit;
1453 }
1454
1455#if defined(MBEDTLS_X509_CRT_PARSE_C)
1456 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1457 {
Hanno Becker16970d22017-10-10 15:56:37 +01001458 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname "
1459 "returned %d\n\n", ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001460 goto exit;
1461 }
1462#endif
1463
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001464#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1465 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
1466 {
1467 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
1468 (const unsigned char *) opt.ecjpake_pw,
1469 strlen( opt.ecjpake_pw ) ) ) != 0 )
1470 {
Hanno Becker16970d22017-10-10 15:56:37 +01001471 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password "
1472 "returned %d\n\n", ret );
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001473 goto exit;
1474 }
1475 }
1476#endif
1477
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001478 if( opt.nbio == 2 )
1479 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
1480 else
Hanno Becker16970d22017-10-10 15:56:37 +01001481 mbedtls_ssl_set_bio( &ssl, &server_fd,
1482 mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02001483 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001484
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001485#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001486 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
1487 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001488#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001491
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 /*
1493 * 4. Handshake
1494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 fflush( stdout );
1497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001499 {
Hanno Becker16970d22017-10-10 15:56:37 +01001500 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1501 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker40e46942009-01-03 21:51:57 +00001502 {
Hanno Becker16970d22017-10-10 15:56:37 +01001503 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake "
1504 "returned -0x%x\n", -ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
1506 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001507 " Unable to verify the server's certificate. "
1508 "Either it is invalid,\n"
1509 " or you didn't set ca_file or ca_path "
1510 "to an appropriate value.\n"
1511 " Alternatively, you may want to use "
1512 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001514 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00001515 }
Hanno Becker16970d22017-10-10 15:56:37 +01001516
1517 /* For event-driven IO, wait for socket to become available */
1518 if( opt.event == 1 /* level triggered IO */ )
1519 {
1520#if defined(MBEDTLS_TIMING_C)
1521 idle( &ssl, &server_fd, &timer, ret );
1522#else
1523 idle( &ssl, &server_fd, ret );
1524#endif
1525 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 }
1527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Hanno Becker16970d22017-10-10 15:56:37 +01001529 mbedtls_ssl_get_version( &ssl ),
1530 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
1533 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001534 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001536
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02001537#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1538 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
1539 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
1540#endif
1541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001543 if( opt.alpn_string != NULL )
1544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
1546 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001547 alp ? alp : "(none)" );
1548 }
1549#endif
1550
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001551 if( opt.reconnect != 0 )
1552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001554 fflush( stdout );
1555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001557 {
Hanno Becker16970d22017-10-10 15:56:37 +01001558 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session "
1559 "returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001560 goto exit;
1561 }
1562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001564 }
1565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001567 /*
1568 * 5. Verify the server certificate
1569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001571
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001572 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001573 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001574 char vrfy_buf[512];
1575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001577
Hanno Becker16970d22017-10-10 15:56:37 +01001578 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ),
1579 " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00001580
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001581 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001582 }
1583 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 mbedtls_printf( " . Peer certificate information ...\n" );
1589 mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1590 mbedtls_ssl_get_peer_cert( &ssl ) );
1591 mbedtls_printf( "%s\n", buf );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001592 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001596 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001597 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001598 /*
1599 * Perform renegotiation (this must be done when the server is waiting
1600 * for input from our side).
1601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001603 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001605 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001606 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1607 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001608 {
Hanno Becker16970d22017-10-10 15:56:37 +01001609 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate "
1610 "returned %d\n\n", ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001611 goto exit;
1612 }
Hanno Becker16970d22017-10-10 15:56:37 +01001613
1614 /* For event-driven IO, wait for socket to become available */
1615 if( opt.event == 1 /* level triggered IO */ )
1616 {
1617#if defined(MBEDTLS_TIMING_C)
1618 idle( &ssl, &server_fd, &timer, ret );
1619#else
1620 idle( &ssl, &server_fd, ret );
1621#endif
1622 }
1623
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001624 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001626 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001628
Paul Bakker5121ce52009-01-03 21:22:43 +00001629 /*
1630 * 6. Write the GET request
1631 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001632 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001633send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001635 fflush( stdout );
1636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 len = mbedtls_snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST,
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001638 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001639 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001640
1641 /* Add padding to GET request to reach opt.request_size in length */
1642 if( opt.request_size != DFL_REQUEST_SIZE &&
1643 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001644 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001645 memset( buf + len, 'A', opt.request_size - len - tail_len );
1646 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001647 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001648
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001649 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof(buf) - len - 1 );
1650 len += tail_len;
1651
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001652 /* Truncate if request size is smaller than the "natural" size */
1653 if( opt.request_size != DFL_REQUEST_SIZE &&
1654 len > opt.request_size )
1655 {
1656 len = opt.request_size;
1657
1658 /* Still end with \r\n unless that's really not possible */
1659 if( len >= 2 ) buf[len - 2] = '\r';
1660 if( len >= 1 ) buf[len - 1] = '\n';
1661 }
1662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001664 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001665 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001666 {
Hanno Becker16970d22017-10-10 15:56:37 +01001667 while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
1668 len - written ) ) <= 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001669 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001670 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1671 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001672 {
Hanno Becker16970d22017-10-10 15:56:37 +01001673 mbedtls_printf( " failed\n ! mbedtls_ssl_write "
1674 "returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001675 goto exit;
1676 }
Hanno Becker16970d22017-10-10 15:56:37 +01001677
1678 /* For event-driven IO, wait for socket to become available */
1679 if( opt.event == 1 /* level triggered IO */ )
1680 {
1681#if defined(MBEDTLS_TIMING_C)
1682 idle( &ssl, &server_fd, &timer, ret );
1683#else
1684 idle( &ssl, &server_fd, ret );
1685#endif
1686 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001687 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001688 }
1689 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001690 else /* Not stream, so datagram */
1691 {
Hanno Becker16970d22017-10-10 15:56:37 +01001692 while( 1 )
1693 {
1694 ret = mbedtls_ssl_write( &ssl, buf, len );
1695
1696 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1697 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1698 break;
1699
1700 /* For event-driven IO, wait for socket to become available */
1701 if( opt.event == 1 /* level triggered IO */ )
1702 {
1703#if defined(MBEDTLS_TIMING_C)
1704 idle( &ssl, &server_fd, &timer, ret );
1705#else
1706 idle( &ssl, &server_fd, ret );
1707#endif
1708 }
1709 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001710
1711 if( ret < 0 )
1712 {
Hanno Becker16970d22017-10-10 15:56:37 +01001713 mbedtls_printf( " failed\n ! mbedtls_ssl_write "
1714 "returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001715 goto exit;
1716 }
1717
1718 frags = 1;
1719 written = ret;
1720 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001721
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001722 buf[written] = '\0';
Hanno Becker16970d22017-10-10 15:56:37 +01001723 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
1724 written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001725
1726 /*
1727 * 7. Read the HTTP response
1728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001730 fflush( stdout );
1731
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001732 /*
1733 * TLS and DTLS need different reading styles (stream vs datagram)
1734 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001736 {
1737 do
1738 {
1739 len = sizeof( buf ) - 1;
1740 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001742
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001743 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1744 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Hanno Becker16970d22017-10-10 15:56:37 +01001745 {
1746 /* For event-driven IO, wait for socket to become available */
1747 if( opt.event == 1 /* level triggered IO */ )
1748 {
1749#if defined(MBEDTLS_TIMING_C)
1750 idle( &ssl, &server_fd, &timer, ret );
1751#else
1752 idle( &ssl, &server_fd, ret );
1753#endif
1754 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001755 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01001756 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001757
1758 if( ret <= 0 )
1759 {
1760 switch( ret )
1761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1763 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001764 ret = 0;
1765 goto close_notify;
1766
1767 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 case MBEDTLS_ERR_NET_CONN_RESET:
1769 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001770 ret = 0;
1771 goto reconnect;
1772
1773 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001775 goto exit;
1776 }
1777 }
1778
1779 len = ret;
1780 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001782
1783 /* End of message should be detected according to the syntax of the
1784 * application protocol (eg HTTP), just use a dummy test here. */
1785 if( ret > 0 && buf[len-1] == '\n' )
1786 {
1787 ret = 0;
1788 break;
1789 }
1790 }
1791 while( 1 );
1792 }
1793 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001794 {
1795 len = sizeof( buf ) - 1;
1796 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001797
Hanno Becker16970d22017-10-10 15:56:37 +01001798 while( 1 )
1799 {
1800 ret = mbedtls_ssl_read( &ssl, buf, len );
1801
1802 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1803 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1804 break;
1805
1806 /* For event-driven IO, wait for socket to become available */
1807 if( opt.event == 1 /* level triggered IO */ )
1808 {
1809#if defined(MBEDTLS_TIMING_C)
1810 idle( &ssl, &server_fd, &timer, ret );
1811#else
1812 idle( &ssl, &server_fd, ret );
1813#endif
1814 }
1815 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001816
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001817 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001818 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001819 switch( ret )
1820 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001821 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001823 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001824 goto send_request;
1825 goto exit;
1826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1828 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001829 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001830 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00001831
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001832 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001834 goto exit;
1835 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001836 }
1837
Paul Bakker5121ce52009-01-03 21:22:43 +00001838 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02001839 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001841 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001842 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001843
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001844 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001845 * 7b. Simulate hard reset and reconnect from same port?
1846 */
1847 if( opt.reconnect_hard != 0 )
1848 {
1849 opt.reconnect_hard = 0;
1850
1851 mbedtls_printf( " . Restarting connection from same port..." );
1852 fflush( stdout );
1853
1854 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
1855 {
1856 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", -ret );
1857 goto exit;
1858 }
1859
1860 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
1861 {
1862 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1863 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1864 {
1865 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
1866 goto exit;
1867 }
Hanno Becker16970d22017-10-10 15:56:37 +01001868
1869 /* For event-driven IO, wait for socket to become available */
1870 if( opt.event == 1 /* level triggered IO */ )
1871 {
1872#if defined(MBEDTLS_TIMING_C)
1873 idle( &ssl, &server_fd, &timer, ret );
1874#else
1875 idle( &ssl, &server_fd, ret );
1876#endif
1877 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001878 }
1879
1880 mbedtls_printf( " ok\n" );
1881
1882 goto send_request;
1883 }
1884
1885 /*
1886 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001887 */
1888 if( --opt.exchanges > 0 )
1889 goto send_request;
1890
1891 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001892 * 8. Done, cleanly close the connection
1893 */
1894close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001896 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001897
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001898 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001900 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001901 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001904
1905 /*
1906 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001907 */
1908reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001909 if( opt.reconnect != 0 )
1910 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001911 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001912
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001913 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001914
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001915#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001916 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02001917 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001918#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02001919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001925 goto exit;
1926 }
1927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001929 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001930 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001931 goto exit;
1932 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
1935 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1936 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001939 goto exit;
1940 }
1941
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001942 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001943 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001944 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001945 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001946 if( ret != 0 )
1947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001949 -ret );
1950 goto exit;
1951 }
1952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001954 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001955 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1956 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001959 goto exit;
1960 }
1961 }
1962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001964
1965 goto send_request;
1966 }
1967
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001968 /*
1969 * Cleanup and exit
1970 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001971exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00001973 if( ret != 0 )
1974 {
1975 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 mbedtls_strerror( ret, error_buf, 100 );
1977 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001978 }
1979#endif
1980
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001981 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983#if defined(MBEDTLS_X509_CRT_PARSE_C)
1984 mbedtls_x509_crt_free( &clicert );
1985 mbedtls_x509_crt_free( &cacert );
1986 mbedtls_pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001987#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 mbedtls_ssl_session_free( &saved_session );
1989 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001990 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 mbedtls_ctr_drbg_free( &ctr_drbg );
1992 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00001993
Paul Bakkercce9d772011-11-18 14:26:47 +00001994#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001996 fflush( stdout ); getchar();
1997#endif
1998
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001999 // Shell can not handle large exit numbers -> 1 for errors
2000 if( ret < 0 )
2001 ret = 1;
2002
Paul Bakker5121ce52009-01-03 21:22:43 +00002003 return( ret );
2004}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
2006 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002007 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */