blob: 186e2a07ff7cd488a798c755bc81f3fb44e4a4c7 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client with certificate authentication
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_printf printf
33#define mbedtls_fprintf fprintf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#define mbedtls_snprintf snprintf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020037#if !defined(MBEDTLS_ENTROPY_C) || \
38 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020039 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020040int main( void )
41{
42 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
43 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020044 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020045 return( 0 );
46}
47#else
48
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/net.h"
50#include "mbedtls/ssl.h"
51#include "mbedtls/entropy.h"
52#include "mbedtls/ctr_drbg.h"
53#include "mbedtls/certs.h"
54#include "mbedtls/x509.h"
55#include "mbedtls/error.h"
56#include "mbedtls/debug.h"
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020057#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Rich Evans18b78c72015-02-11 14:06:19 +000059#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010062
Paul Bakker9caf2d22010-02-18 19:37:19 +000063#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010064#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020065#define DFL_SERVER_PORT "4433"
Paul Bakker9caf2d22010-02-18 19:37:19 +000066#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +020067#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +000068#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010069#define DFL_NBIO 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020070#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +020071#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +000072#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000073#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000074#define DFL_CRT_FILE ""
75#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020076#define DFL_PSK ""
77#define DFL_PSK_IDENTITY "Client_identity"
Paul Bakker51936882011-02-20 16:05:58 +000078#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010080#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010081#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +020082#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +020083#define DFL_MIN_VERSION -1
Paul Bakker1d29fb52012-09-28 13:28:45 +000084#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +000085#define DFL_ARC4 -1
Gilles Peskineae765992017-05-09 15:59:24 +020086#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +010087#define DFL_AUTH_MODE -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +010089#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +010090#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +020091#define DFL_DHMLEN -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +020092#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010093#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +020094#define DFL_RECONNECT_HARD 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +020096#define DFL_ALPN_STRING NULL
Hanno Becker61c0c702017-05-15 16:05:15 +010097#define DFL_CURVES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +020099#define DFL_HS_TO_MIN 0
100#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200101#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200102#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100103#define DFL_ETM -1
Paul Bakker0e6975b2009-02-10 22:19:10 +0000104
Paul Bakker93c32b22014-04-25 13:40:05 +0200105#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
106#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#if defined(MBEDTLS_X509_CRT_PARSE_C)
109#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000110#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100111 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
112 " default: \"\" (pre-loaded)\n" \
113 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
114 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
115 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
116 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000117 " key_file=%%s default: \"\" (pre-loaded)\n"
118#else
119#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 " No file operations available (MBEDTLS_FS_IO not defined)\n"
121#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200122#else
123#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200127#define USAGE_PSK \
128 " psk=%%s default: \"\" (in hex, without 0x)\n" \
129 " psk_identity=%%s default: \"Client_identity\"\n"
130#else
131#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200135#define USAGE_TICKETS \
136 " tickets=%%d default: 1 (enabled)\n"
137#else
138#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200142#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100143 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200144#else
145#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200149#define USAGE_MAX_FRAG_LEN \
150 " max_frag_len=%%d default: 16384 (tls default)\n" \
151 " options: 512, 1024, 2048, 4096\n"
152#else
153#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100157#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200158 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100159#else
160#define USAGE_RECSPLIT
161#endif
162
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200163#if defined(MBEDTLS_DHM_C)
164#define USAGE_DHMLEN \
165 " dhmlen=%%d default: (library default: 1024 bits)\n"
166#else
167#define USAGE_DHMLEN
168#endif
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200171#define USAGE_ALPN \
172 " alpn=%%s default: \"\" (disabled)\n" \
173 " example: spdy/1,http/1.1\n"
174#else
175#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200177
Hanno Becker61c0c702017-05-15 16:05:15 +0100178#if defined(MBEDTLS_ECP_C)
179#define USAGE_CURVES \
180 " curves=a,b,c,d default: \"default\" (library default)\n" \
181 " example: \"secp521r1,brainpoolP512r1\"\n" \
182 " - use \"none\" for empty list\n" \
183 " - see mbedtls_ecp_curve_list()\n" \
184 " for acceptable curve names\n"
185#else
186#define USAGE_CURVES ""
187#endif
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200190#define USAGE_DTLS \
191 " dtls=%%d default: 0 (TLS)\n" \
192 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
193 " range of DTLS handshake timeouts in millisecs\n"
194#else
195#define USAGE_DTLS ""
196#endif
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200199#define USAGE_FALLBACK \
200 " fallback=0/1 default: (library default: off)\n"
201#else
202#define USAGE_FALLBACK ""
203#endif
204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200206#define USAGE_EMS \
207 " extended_ms=0/1 default: (library default: on)\n"
208#else
209#define USAGE_EMS ""
210#endif
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100213#define USAGE_ETM \
214 " etm=0/1 default: (library default: on)\n"
215#else
216#define USAGE_ETM ""
217#endif
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100220#define USAGE_RENEGO \
221 " renegotiation=%%d default: 0 (disabled)\n" \
222 " renegotiate=%%d default: 0 (disabled)\n"
223#else
224#define USAGE_RENEGO ""
225#endif
226
Paul Bakker9caf2d22010-02-18 19:37:19 +0000227#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000228 "\n usage: ssl_client2 param=<>...\n" \
229 "\n acceptable parameters:\n" \
230 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100231 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000232 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000233 " request_page=%%s default: \".\"\n" \
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +0200234 " request_size=%%d default: about 34 (basic request)\n" \
235 " (minimum: 0, max: 16384)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100236 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100237 " nbio=%%d default: 0 (blocking I/O)\n" \
238 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200239 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200240 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100241 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200242 USAGE_DTLS \
243 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100244 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100245 " options: none, optional, required\n" \
246 USAGE_IO \
247 "\n" \
248 USAGE_PSK \
249 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100250 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100251 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200252 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200253 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200254 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200255 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200256 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100257 USAGE_MAX_FRAG_LEN \
258 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200259 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200260 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200261 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100262 USAGE_ETM \
Hanno Becker61c0c702017-05-15 16:05:15 +0100263 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100264 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200265 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000266 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100267 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskineae765992017-05-09 15:59:24 +0200268 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200269 " min_version=%%s default: (library default: tls1)\n" \
270 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000271 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100272 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000273 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000274 " force_ciphersuite=<name> default: all enabled\n"\
275 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000276
Hanno Becker46a16292017-06-09 16:13:22 +0100277#define ALPN_LIST_SIZE 10
278#define CURVE_LIST_SIZE 20
279
Rich Evans85b05ec2015-02-12 11:37:29 +0000280/*
281 * global options
282 */
283struct options
284{
285 const char *server_name; /* hostname of the server (client only) */
286 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200287 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000288 int debug_level; /* level of debugging */
289 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000291 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000292 const char *request_page; /* page on server to request */
293 int request_size; /* pad request with header to requested size */
294 const char *ca_file; /* the file with the CA certificate(s) */
295 const char *ca_path; /* the path with the CA certificate(s) reside */
296 const char *crt_file; /* the file with the client certificate */
297 const char *key_file; /* the file with the client key */
298 const char *psk; /* the pre-shared key */
299 const char *psk_identity; /* the pre-shared key identity */
300 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
301 int renegotiation; /* enable / disable renegotiation */
302 int allow_legacy; /* allow legacy renegotiation */
303 int renegotiate; /* attempt renegotiation? */
304 int renego_delay; /* delay before enforcing renegotiation */
305 int exchanges; /* number of data exchanges */
306 int min_version; /* minimum protocol version accepted */
307 int max_version; /* maximum protocol version accepted */
308 int arc4; /* flag for arc4 suites support */
Gilles Peskineae765992017-05-09 15:59:24 +0200309 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000310 int auth_mode; /* verify mode for connection */
311 unsigned char mfl_code; /* code for maximum fragment length */
312 int trunc_hmac; /* negotiate truncated hmac or not */
313 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200314 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000315 int reconnect; /* attempt to resume session */
316 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200317 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000318 int tickets; /* enable / disable session tickets */
Hanno Becker61c0c702017-05-15 16:05:15 +0100319 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000320 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000321 int transport; /* TLS or DTLS? */
322 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
323 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Rich Evans85b05ec2015-02-12 11:37:29 +0000324 int fallback; /* is this a fallback connection? */
325 int extended_ms; /* negotiate extended master secret? */
326 int etm; /* negotiate encrypt then mac? */
327} opt;
328
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200329static void my_debug( void *ctx, int level,
330 const char *file, int line,
331 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000332{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200333 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000334
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200335 /* Extract basename from file */
336 for( p = basename = file; *p != '\0'; p++ )
337 if( *p == '/' || *p == '\\' )
338 basename = p + 1;
339
340 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000341 fflush( (FILE *) ctx );
342}
343
344/*
345 * Test recv/send functions that make sure each try returns
346 * WANT_READ/WANT_WRITE at least once before sucesseding
347 */
348static int my_recv( void *ctx, unsigned char *buf, size_t len )
349{
350 static int first_try = 1;
351 int ret;
352
353 if( first_try )
354 {
355 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100356 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000357 }
358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100360 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000361 first_try = 1; /* Next call will be a new operation */
362 return( ret );
363}
364
365static int my_send( void *ctx, const 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_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000374 }
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100377 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000378 first_try = 1; /* Next call will be a new operation */
379 return( ret );
380}
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382#if defined(MBEDTLS_X509_CRT_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000383/*
384 * Enabled if debug_level > 1 in code below
385 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200386static int my_verify( void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000387{
388 char buf[1024];
389 ((void) data);
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
392 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
393 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000394
Rich Evans85b05ec2015-02-12 11:37:29 +0000395 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100397 else
398 {
399 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
400 mbedtls_printf( "%s\n", buf );
401 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000402
403 return( 0 );
404}
Gilles Peskine12c19542017-05-09 14:57:45 +0200405
406static int ssl_sig_hashes_for_test[] = {
407#if defined(MBEDTLS_SHA512_C)
408 MBEDTLS_MD_SHA512,
409 MBEDTLS_MD_SHA384,
410#endif
411#if defined(MBEDTLS_SHA256_C)
412 MBEDTLS_MD_SHA256,
413 MBEDTLS_MD_SHA224,
414#endif
415#if defined(MBEDTLS_SHA1_C)
416 /* Allow SHA-1 as we use it extensively in tests. */
417 MBEDTLS_MD_SHA1,
418#endif
419 MBEDTLS_MD_NONE
420};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000422
Paul Bakker9caf2d22010-02-18 19:37:19 +0000423int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000424{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200425 int ret = 0, len, tail_len, i, written, frags, retry_left;
426 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 unsigned char buf[MBEDTLS_SSL_MAX_CONTENT_LEN + 1];
428#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
429 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200430 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200431#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker46a16292017-06-09 16:13:22 +0100433 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200434#endif
Hanno Becker61c0c702017-05-15 16:05:15 +0100435#if defined(MBEDTLS_ECP_C)
Hanno Becker46a16292017-06-09 16:13:22 +0100436 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Becker61c0c702017-05-15 16:05:15 +0100437 const mbedtls_ecp_curve_info *curve_cur;
438#endif
439
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200440 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000441
Gilles Peskinedd57d752017-05-05 18:59:02 +0200442#if defined(MBEDTLS_X509_CRT_PARSE_C)
443 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
444#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_entropy_context entropy;
446 mbedtls_ctr_drbg_context ctr_drbg;
447 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200448 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200450#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200451 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200452#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200454 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_x509_crt cacert;
456 mbedtls_x509_crt clicert;
457 mbedtls_pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200458#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000459 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000460 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000461
Paul Bakker1a207ec2011-02-06 13:22:40 +0000462 /*
463 * Make sure memory references are valid.
464 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200465 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200466 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200467 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200469 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470#if defined(MBEDTLS_X509_CRT_PARSE_C)
471 mbedtls_x509_crt_init( &cacert );
472 mbedtls_x509_crt_init( &clicert );
473 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200474#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200476 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200477#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000478
Paul Bakker9caf2d22010-02-18 19:37:19 +0000479 if( argc == 0 )
480 {
481 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000482 if( ret == 0 )
483 ret = 1;
484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000488 while( *list )
489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200491 list++;
492 if( !*list )
493 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000495 list++;
496 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000498 goto exit;
499 }
500
501 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100502 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000503 opt.server_port = DFL_SERVER_PORT;
504 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100505 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200506 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200507 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000508 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200509 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000510 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000511 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000512 opt.crt_file = DFL_CRT_FILE;
513 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200514 opt.psk = DFL_PSK;
515 opt.psk_identity = DFL_PSK_IDENTITY;
Paul Bakker51936882011-02-20 16:05:58 +0000516 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000517 opt.renegotiation = DFL_RENEGOTIATION;
518 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100519 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200520 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000521 opt.min_version = DFL_MIN_VERSION;
522 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100523 opt.arc4 = DFL_ARC4;
Gilles Peskineae765992017-05-09 15:59:24 +0200524 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100525 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200526 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200527 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100528 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200529 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200530 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100531 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200532 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200533 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200534 opt.alpn_string = DFL_ALPN_STRING;
Hanno Becker61c0c702017-05-15 16:05:15 +0100535 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200536 opt.transport = DFL_TRANSPORT;
537 opt.hs_to_min = DFL_HS_TO_MIN;
538 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200539 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200540 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100541 opt.etm = DFL_ETM;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000542
543 for( i = 1; i < argc; i++ )
544 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000545 p = argv[i];
546 if( ( q = strchr( p, '=' ) ) == NULL )
547 goto usage;
548 *q++ = '\0';
549
550 if( strcmp( p, "server_name" ) == 0 )
551 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100552 else if( strcmp( p, "server_addr" ) == 0 )
553 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000554 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200555 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100556 else if( strcmp( p, "dtls" ) == 0 )
557 {
558 int t = atoi( q );
559 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100561 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100563 else
564 goto usage;
565 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000566 else if( strcmp( p, "debug_level" ) == 0 )
567 {
568 opt.debug_level = atoi( q );
569 if( opt.debug_level < 0 || opt.debug_level > 65535 )
570 goto usage;
571 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100572 else if( strcmp( p, "nbio" ) == 0 )
573 {
574 opt.nbio = atoi( q );
575 if( opt.nbio < 0 || opt.nbio > 2 )
576 goto usage;
577 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200578 else if( strcmp( p, "read_timeout" ) == 0 )
579 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200580 else if( strcmp( p, "max_resend" ) == 0 )
581 {
582 opt.max_resend = atoi( q );
583 if( opt.max_resend < 0 )
584 goto usage;
585 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000586 else if( strcmp( p, "request_page" ) == 0 )
587 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200588 else if( strcmp( p, "request_size" ) == 0 )
589 {
590 opt.request_size = atoi( q );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 if( opt.request_size < 0 || opt.request_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker93c32b22014-04-25 13:40:05 +0200592 goto usage;
593 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000594 else if( strcmp( p, "ca_file" ) == 0 )
595 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000596 else if( strcmp( p, "ca_path" ) == 0 )
597 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000598 else if( strcmp( p, "crt_file" ) == 0 )
599 opt.crt_file = q;
600 else if( strcmp( p, "key_file" ) == 0 )
601 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200602 else if( strcmp( p, "psk" ) == 0 )
603 opt.psk = q;
604 else if( strcmp( p, "psk_identity" ) == 0 )
605 opt.psk_identity = q;
Paul Bakker51936882011-02-20 16:05:58 +0000606 else if( strcmp( p, "force_ciphersuite" ) == 0 )
607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +0000609
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200610 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000611 {
612 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000613 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000614 }
Paul Bakker51936882011-02-20 16:05:58 +0000615 opt.force_ciphersuite[1] = 0;
616 }
Paul Bakker48916f92012-09-16 19:57:18 +0000617 else if( strcmp( p, "renegotiation" ) == 0 )
618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 opt.renegotiation = (atoi( q )) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED :
620 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +0000621 }
622 else if( strcmp( p, "allow_legacy" ) == 0 )
623 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100624 switch( atoi( q ) )
625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 case -1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE; break;
627 case 0: opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; break;
628 case 1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION; break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100629 default: goto usage;
630 }
Paul Bakker48916f92012-09-16 19:57:18 +0000631 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100632 else if( strcmp( p, "renegotiate" ) == 0 )
633 {
634 opt.renegotiate = atoi( q );
635 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
636 goto usage;
637 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200638 else if( strcmp( p, "exchanges" ) == 0 )
639 {
640 opt.exchanges = atoi( q );
641 if( opt.exchanges < 1 )
642 goto usage;
643 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200644 else if( strcmp( p, "reconnect" ) == 0 )
645 {
646 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200647 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200648 goto usage;
649 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100650 else if( strcmp( p, "reco_delay" ) == 0 )
651 {
652 opt.reco_delay = atoi( q );
653 if( opt.reco_delay < 0 )
654 goto usage;
655 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200656 else if( strcmp( p, "reconnect_hard" ) == 0 )
657 {
658 opt.reconnect_hard = atoi( q );
659 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
660 goto usage;
661 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200662 else if( strcmp( p, "tickets" ) == 0 )
663 {
664 opt.tickets = atoi( q );
665 if( opt.tickets < 0 || opt.tickets > 2 )
666 goto usage;
667 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200668 else if( strcmp( p, "alpn" ) == 0 )
669 {
670 opt.alpn_string = q;
671 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200672 else if( strcmp( p, "fallback" ) == 0 )
673 {
674 switch( atoi( q ) )
675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
677 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200678 default: goto usage;
679 }
680 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200681 else if( strcmp( p, "extended_ms" ) == 0 )
682 {
683 switch( atoi( q ) )
684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 case 0: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED; break;
686 case 1: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200687 default: goto usage;
688 }
689 }
Hanno Becker61c0c702017-05-15 16:05:15 +0100690 else if( strcmp( p, "curves" ) == 0 )
691 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100692 else if( strcmp( p, "etm" ) == 0 )
693 {
694 switch( atoi( q ) )
695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
697 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100698 default: goto usage;
699 }
700 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000701 else if( strcmp( p, "min_version" ) == 0 )
702 {
703 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000705 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100707 else if( strcmp( q, "tls1_1" ) == 0 ||
708 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100710 else if( strcmp( q, "tls1_2" ) == 0 ||
711 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000713 else
714 goto usage;
715 }
716 else if( strcmp( p, "max_version" ) == 0 )
717 {
718 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000720 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100722 else if( strcmp( q, "tls1_1" ) == 0 ||
723 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100725 else if( strcmp( q, "tls1_2" ) == 0 ||
726 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000728 else
729 goto usage;
730 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100731 else if( strcmp( p, "arc4" ) == 0 )
732 {
733 switch( atoi( q ) )
734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
736 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100737 default: goto usage;
738 }
739 }
Gilles Peskineae765992017-05-09 15:59:24 +0200740 else if( strcmp( p, "allow_sha1" ) == 0 )
741 {
742 switch( atoi( q ) )
743 {
744 case 0: opt.allow_sha1 = 0; break;
745 case 1: opt.allow_sha1 = 1; break;
746 default: goto usage;
747 }
748 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000749 else if( strcmp( p, "force_version" ) == 0 )
750 {
751 if( strcmp( q, "ssl3" ) == 0 )
752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
754 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000755 }
756 else if( strcmp( q, "tls1" ) == 0 )
757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
759 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000760 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100761 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
764 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000765 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100766 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
769 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000770 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100771 else if( strcmp( q, "dtls1" ) == 0 )
772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
774 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
775 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100776 }
777 else if( strcmp( q, "dtls1_2" ) == 0 )
778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
780 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
781 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100782 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000783 else
784 goto usage;
785 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100786 else if( strcmp( p, "auth_mode" ) == 0 )
787 {
788 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100790 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100792 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100794 else
795 goto usage;
796 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200797 else if( strcmp( p, "max_frag_len" ) == 0 )
798 {
799 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200801 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200803 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200805 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200807 else
808 goto usage;
809 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200810 else if( strcmp( p, "trunc_hmac" ) == 0 )
811 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100812 switch( atoi( q ) )
813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
815 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100816 default: goto usage;
817 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200818 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200819 else if( strcmp( p, "hs_timeout" ) == 0 )
820 {
821 if( ( p = strchr( q, '-' ) ) == NULL )
822 goto usage;
823 *p++ = '\0';
824 opt.hs_to_min = atoi( q );
825 opt.hs_to_max = atoi( p );
826 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
827 goto usage;
828 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100829 else if( strcmp( p, "recsplit" ) == 0 )
830 {
831 opt.recsplit = atoi( q );
832 if( opt.recsplit < 0 || opt.recsplit > 1 )
833 goto usage;
834 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200835 else if( strcmp( p, "dhmlen" ) == 0 )
836 {
837 opt.dhmlen = atoi( q );
838 if( opt.dhmlen < 0 )
839 goto usage;
840 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000841 else
842 goto usage;
843 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#if defined(MBEDTLS_DEBUG_C)
846 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200847#endif
848
Paul Bakkerc1516be2013-06-29 16:01:32 +0200849 if( opt.force_ciphersuite[0] > 0 )
850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
852 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +0200853
Paul Bakker66c48102013-07-26 14:05:32 +0200854 if( opt.max_version != -1 &&
855 ciphersuite_info->min_minor_ver > opt.max_version )
856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakker66c48102013-07-26 14:05:32 +0200858 ret = 2;
859 goto usage;
860 }
861 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200862 ciphersuite_info->max_minor_ver < opt.min_version )
863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +0200865 ret = 2;
866 goto usage;
867 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100868
869 /* If the server selects a version that's not supported by
870 * this suite, then there will be no common ciphersuite... */
871 if( opt.max_version == -1 ||
872 opt.max_version > ciphersuite_info->max_minor_ver )
873 {
Paul Bakker66c48102013-07-26 14:05:32 +0200874 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100875 }
Paul Bakker66c48102013-07-26 14:05:32 +0200876 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100877 {
Paul Bakker66c48102013-07-26 14:05:32 +0200878 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100879 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
881 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
882 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100883 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000884
885 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000891 ret = 2;
892 goto usage;
893 }
894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000896 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200897 }
898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000900 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200901 * Unhexify the pre-shared key if any is given
902 */
903 if( strlen( opt.psk ) )
904 {
905 unsigned char c;
906 size_t j;
907
908 if( strlen( opt.psk ) % 2 != 0 )
909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200911 goto exit;
912 }
913
914 psk_len = strlen( opt.psk ) / 2;
915
916 for( j = 0; j < strlen( opt.psk ); j += 2 )
917 {
918 c = opt.psk[j];
919 if( c >= '0' && c <= '9' )
920 c -= '0';
921 else if( c >= 'a' && c <= 'f' )
922 c -= 'a' - 10;
923 else if( c >= 'A' && c <= 'F' )
924 c -= 'A' - 10;
925 else
926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200928 goto exit;
929 }
930 psk[ j / 2 ] = c << 4;
931
932 c = opt.psk[j + 1];
933 if( c >= '0' && c <= '9' )
934 c -= '0';
935 else if( c >= 'a' && c <= 'f' )
936 c -= 'a' - 10;
937 else if( c >= 'A' && c <= 'F' )
938 c -= 'A' - 10;
939 else
940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200942 goto exit;
943 }
944 psk[ j / 2 ] |= c;
945 }
946 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200948
Hanno Becker61c0c702017-05-15 16:05:15 +0100949#if defined(MBEDTLS_ECP_C)
950 if( opt.curves != NULL )
951 {
952 p = (char *) opt.curves;
953 i = 0;
954
955 if( strcmp( p, "none" ) == 0 )
956 {
957 curve_list[0] = MBEDTLS_ECP_DP_NONE;
958 }
959 else if( strcmp( p, "default" ) != 0 )
960 {
961 /* Leave room for a final NULL in curve list */
Hanno Becker46a16292017-06-09 16:13:22 +0100962 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Becker61c0c702017-05-15 16:05:15 +0100963 {
964 q = p;
965
966 /* Terminate the current string */
967 while( *p != ',' && *p != '\0' )
968 p++;
969 if( *p == ',' )
970 *p++ = '\0';
971
972 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
973 {
974 curve_list[i++] = curve_cur->grp_id;
975 }
976 else
977 {
978 mbedtls_printf( "unknown curve %s\n", q );
979 mbedtls_printf( "supported curves: " );
980 for( curve_cur = mbedtls_ecp_curve_list();
981 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
982 curve_cur++ )
983 {
984 mbedtls_printf( "%s ", curve_cur->name );
985 }
986 mbedtls_printf( "\n" );
987 goto exit;
988 }
989 }
990
991 mbedtls_printf("Number of curves: %d\n", i );
992
Hanno Becker46a16292017-06-09 16:13:22 +0100993 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Becker61c0c702017-05-15 16:05:15 +0100994 {
Hanno Becker46a16292017-06-09 16:13:22 +0100995 mbedtls_printf( "curves list too long, maximum %d",
996 CURVE_LIST_SIZE - 1 );
Hanno Becker61c0c702017-05-15 16:05:15 +0100997 goto exit;
998 }
999
1000 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1001 }
1002 }
1003#endif /* MBEDTLS_ECP_C */
1004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001006 if( opt.alpn_string != NULL )
1007 {
1008 p = (char *) opt.alpn_string;
1009 i = 0;
1010
1011 /* Leave room for a final NULL in alpn_list */
Hanno Becker46a16292017-06-09 16:13:22 +01001012 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001013 {
1014 alpn_list[i++] = p;
1015
1016 /* Terminate the current string and move on to next one */
1017 while( *p != ',' && *p != '\0' )
1018 p++;
1019 if( *p == ',' )
1020 *p++ = '\0';
1021 }
1022 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001024
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001025 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001026 * 0. Initialize the RNG and the session data
1027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001029 fflush( stdout );
1030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001032 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001033 (const unsigned char *) pers,
1034 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +00001035 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001036 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n", -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001037 goto exit;
1038 }
1039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 /*
1044 * 1.1. Load the trusted CA
1045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001047 fflush( stdout );
1048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001050 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001051 if( strcmp( opt.ca_path, "none" ) == 0 )
1052 ret = 0;
1053 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001055 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001056 if( strcmp( opt.ca_file, "none" ) == 0 )
1057 ret = 0;
1058 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001060 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001061#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#if defined(MBEDTLS_CERTS_C)
1063 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 ret = mbedtls_x509_crt_parse( &cacert,
1066 (const unsigned char *) mbedtls_test_cas[i],
1067 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001068 if( ret != 0 )
1069 break;
1070 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001071#else
1072 {
1073 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001075 }
1076#endif
Paul Bakker42488232012-05-16 08:21:05 +00001077 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 goto exit;
1081 }
1082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
1085 /*
1086 * 1.2. Load own certificate and private key
1087 *
1088 * (can be skipped if client authentication is not required)
1089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 fflush( stdout );
1092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001094 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001095 if( strcmp( opt.crt_file, "none" ) == 0 )
1096 ret = 0;
1097 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001099 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001100#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101#if defined(MBEDTLS_CERTS_C)
1102 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
1103 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001104#else
1105 {
1106 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001108 }
1109#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 if( ret != 0 )
1111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001113 goto exit;
1114 }
1115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001117 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001118 if( strcmp( opt.key_file, "none" ) == 0 )
1119 ret = 0;
1120 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001122 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001123#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124#if defined(MBEDTLS_CERTS_C)
1125 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_cli_key,
1126 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001127#else
1128 {
1129 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001131 }
1132#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001133 if( ret != 0 )
1134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 goto exit;
1137 }
1138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 mbedtls_printf( " ok\n" );
1140#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001141
1142 /*
1143 * 2. Start the connection
1144 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001145 if( opt.server_addr == NULL)
1146 opt.server_addr = opt.server_name;
1147
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001148 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001150 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001151 fflush( stdout );
1152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
1154 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1155 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001158 goto exit;
1159 }
1160
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001161 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001162 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001163 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001164 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001165 if( ret != 0 )
1166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001168 goto exit;
1169 }
1170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001172
1173 /*
1174 * 3. Setup stuff
1175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001177 fflush( stdout );
1178
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001179 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1180 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001181 opt.transport,
1182 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001183 {
1184 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
1185 goto exit;
1186 }
1187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskinedd57d752017-05-05 18:59:02 +02001189 /* The default algorithms profile disables SHA-1, but our tests still
1190 rely on it heavily. */
Gilles Peskineae765992017-05-09 15:59:24 +02001191 if( opt.allow_sha1 > 0 )
1192 {
1193 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1194 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1195 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1196 }
Gilles Peskinedd57d752017-05-05 18:59:02 +02001197
Paul Bakker915275b2012-09-28 07:10:55 +00001198 if( opt.debug_level > 0 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001199 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
Gilles Peskinedd57d752017-05-05 18:59:02 +02001200#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001201
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001202 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001203 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001206 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001207 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001211 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001212 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001213 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001214 goto exit;
1215 }
Paul Bakker05decb22013-08-15 13:33:48 +02001216#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001219 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001220 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001221#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001224 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001225 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001226#endif
1227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001229 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001230 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001231#endif
1232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001234 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001235 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1237 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001238#endif
1239
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001240#if defined(MBEDTLS_DHM_C)
1241 if( opt.dhmlen != DFL_DHMLEN )
1242 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1243#endif
1244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001246 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001247 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001248 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001249 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001250 goto exit;
1251 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001252#endif
1253
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001254 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1255 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001256
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001257 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02001260 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02001261#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001262
Paul Bakker645ce3a2012-10-31 12:32:41 +00001263 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001264 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001265
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001266#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001267 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001268 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001269#endif
Paul Bakker51936882011-02-20 16:05:58 +00001270
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001271 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001272 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001274 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001275#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001278 if( strcmp( opt.ca_path, "none" ) != 0 &&
1279 strcmp( opt.ca_file, "none" ) != 0 )
1280 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001281 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001282 }
1283 if( strcmp( opt.crt_file, "none" ) != 0 &&
1284 strcmp( opt.key_file, "none" ) != 0 )
1285 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001286 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001287 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001288 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001289 goto exit;
1290 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001291 }
Paul Bakkered27a042013-04-18 22:46:23 +02001292#endif
1293
Hanno Becker61c0c702017-05-15 16:05:15 +01001294#if defined(MBEDTLS_ECP_C)
1295 if( opt.curves != NULL &&
1296 strcmp( opt.curves, "default" ) != 0 )
1297 {
1298 mbedtls_ssl_conf_curves( &conf, curve_list );
1299 }
1300#endif
1301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001303 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001304 (const unsigned char *) opt.psk_identity,
1305 strlen( opt.psk_identity ) ) ) != 0 )
1306 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001307 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001308 goto exit;
1309 }
Paul Bakkered27a042013-04-18 22:46:23 +02001310#endif
1311
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001312 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001313 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001314
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001315 if( opt.max_version != DFL_MAX_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001316 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001319 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001320 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001321#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001323 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
1324 {
1325 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
1326 goto exit;
1327 }
1328
1329#if defined(MBEDTLS_X509_CRT_PARSE_C)
1330 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1331 {
1332 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
1333 goto exit;
1334 }
1335#endif
1336
1337 if( opt.nbio == 2 )
1338 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
1339 else
1340 mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02001341 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001342
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001343#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001344 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
1345 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001346#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001349
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 /*
1351 * 4. Handshake
1352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 fflush( stdout );
1355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001358 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker40e46942009-01-03 21:51:57 +00001359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n", -ret );
1361 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
1362 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001363 " Unable to verify the server's certificate. "
1364 "Either it is invalid,\n"
1365 " or you didn't set ca_file or ca_path "
1366 "to an appropriate value.\n"
1367 " Alternatively, you may want to use "
1368 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00001371 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001372 }
1373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1375 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
1378 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001379 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001381
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02001382#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1383 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
1384 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
1385#endif
1386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001388 if( opt.alpn_string != NULL )
1389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
1391 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001392 alp ? alp : "(none)" );
1393 }
1394#endif
1395
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001396 if( opt.reconnect != 0 )
1397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001399 fflush( stdout );
1400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001404 goto exit;
1405 }
1406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001408 }
1409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001411 /*
1412 * 5. Verify the server certificate
1413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001415
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001416 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001418 char vrfy_buf[512];
1419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001422 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00001423
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001424 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425 }
1426 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 mbedtls_printf( " . Peer certificate information ...\n" );
1432 mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1433 mbedtls_ssl_get_peer_cert( &ssl ) );
1434 mbedtls_printf( "%s\n", buf );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001435 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001439 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001440 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001441 /*
1442 * Perform renegotiation (this must be done when the server is waiting
1443 * for input from our side).
1444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001446 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001448 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001449 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1450 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001453 goto exit;
1454 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001455 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001457 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001459
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 /*
1461 * 6. Write the GET request
1462 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001463 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001464send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001466 fflush( stdout );
1467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 len = mbedtls_snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST,
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001469 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001470 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001471
1472 /* Add padding to GET request to reach opt.request_size in length */
1473 if( opt.request_size != DFL_REQUEST_SIZE &&
1474 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001475 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001476 memset( buf + len, 'A', opt.request_size - len - tail_len );
1477 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001478 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001479
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001480 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof(buf) - len - 1 );
1481 len += tail_len;
1482
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001483 /* Truncate if request size is smaller than the "natural" size */
1484 if( opt.request_size != DFL_REQUEST_SIZE &&
1485 len > opt.request_size )
1486 {
1487 len = opt.request_size;
1488
1489 /* Still end with \r\n unless that's really not possible */
1490 if( len >= 2 ) buf[len - 2] = '\r';
1491 if( len >= 1 ) buf[len - 1] = '\n';
1492 }
1493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001495 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001496 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001499 <= 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001500 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001501 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1502 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001505 goto exit;
1506 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001507 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001508 }
1509 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001510 else /* Not stream, so datagram */
1511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 do ret = mbedtls_ssl_write( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001513 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1514 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001515
1516 if( ret < 0 )
1517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001519 goto exit;
1520 }
1521
1522 frags = 1;
1523 written = ret;
1524 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001525
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001526 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001528
1529 /*
1530 * 7. Read the HTTP response
1531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 fflush( stdout );
1534
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001535 /*
1536 * TLS and DTLS need different reading styles (stream vs datagram)
1537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001539 {
1540 do
1541 {
1542 len = sizeof( buf ) - 1;
1543 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001545
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001546 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1547 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001548 continue;
1549
1550 if( ret <= 0 )
1551 {
1552 switch( ret )
1553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1555 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001556 ret = 0;
1557 goto close_notify;
1558
1559 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 case MBEDTLS_ERR_NET_CONN_RESET:
1561 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001562 ret = 0;
1563 goto reconnect;
1564
1565 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001567 goto exit;
1568 }
1569 }
1570
1571 len = ret;
1572 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001574
1575 /* End of message should be detected according to the syntax of the
1576 * application protocol (eg HTTP), just use a dummy test here. */
1577 if( ret > 0 && buf[len-1] == '\n' )
1578 {
1579 ret = 0;
1580 break;
1581 }
1582 }
1583 while( 1 );
1584 }
1585 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001586 {
1587 len = sizeof( buf ) - 1;
1588 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 do ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001591 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1592 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001593
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001594 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001595 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001596 switch( ret )
1597 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001598 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001600 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001601 goto send_request;
1602 goto exit;
1603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1605 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001606 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001607 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00001608
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001609 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001611 goto exit;
1612 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001613 }
1614
Paul Bakker5121ce52009-01-03 21:22:43 +00001615 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02001616 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001618 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001619 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001620
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001621 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001622 * 7b. Simulate hard reset and reconnect from same port?
1623 */
1624 if( opt.reconnect_hard != 0 )
1625 {
1626 opt.reconnect_hard = 0;
1627
1628 mbedtls_printf( " . Restarting connection from same port..." );
1629 fflush( stdout );
1630
1631 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
1632 {
1633 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", -ret );
1634 goto exit;
1635 }
1636
1637 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
1638 {
1639 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1640 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1641 {
1642 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
1643 goto exit;
1644 }
1645 }
1646
1647 mbedtls_printf( " ok\n" );
1648
1649 goto send_request;
1650 }
1651
1652 /*
1653 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001654 */
1655 if( --opt.exchanges > 0 )
1656 goto send_request;
1657
1658 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001659 * 8. Done, cleanly close the connection
1660 */
1661close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001663 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001664
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001665 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001667 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001668 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001671
1672 /*
1673 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001674 */
1675reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001676 if( opt.reconnect != 0 )
1677 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001678 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001679
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001680 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001681
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001682#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001683 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02001684 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001685#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02001686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001692 goto exit;
1693 }
1694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001696 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001697 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001698 goto exit;
1699 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
1702 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1703 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001706 goto exit;
1707 }
1708
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001709 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001710 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001711 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001712 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001713 if( ret != 0 )
1714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001716 -ret );
1717 goto exit;
1718 }
1719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001721 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001722 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1723 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001726 goto exit;
1727 }
1728 }
1729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001731
1732 goto send_request;
1733 }
1734
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001735 /*
1736 * Cleanup and exit
1737 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001738exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00001740 if( ret != 0 )
1741 {
1742 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 mbedtls_strerror( ret, error_buf, 100 );
1744 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001745 }
1746#endif
1747
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001748 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750#if defined(MBEDTLS_X509_CRT_PARSE_C)
1751 mbedtls_x509_crt_free( &clicert );
1752 mbedtls_x509_crt_free( &cacert );
1753 mbedtls_pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001754#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 mbedtls_ssl_session_free( &saved_session );
1756 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001757 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 mbedtls_ctr_drbg_free( &ctr_drbg );
1759 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00001760
Paul Bakkercce9d772011-11-18 14:26:47 +00001761#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001763 fflush( stdout ); getchar();
1764#endif
1765
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001766 // Shell can not handle large exit numbers -> 1 for errors
1767 if( ret < 0 )
1768 ret = 1;
1769
Paul Bakker5121ce52009-01-03 21:22:43 +00001770 return( ret );
1771}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
1773 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001774 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */