blob: c63c4f75a1f40db6f1024e6ece0b68c59c40543d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client with certificate authentication
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Simon Butcherd3138c32016-04-27 01:26:50 +010032#include <stdlib.h>
33#define mbedtls_time time
34#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#define mbedtls_printf printf
36#define mbedtls_fprintf fprintf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define mbedtls_snprintf snprintf
Rich Evansf90016a2015-01-19 14:26:37 +000038#endif
39
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020040#if !defined(MBEDTLS_ENTROPY_C) || \
41 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020042 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020043int main( void )
44{
45 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
46 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020047 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020048 return( 0 );
49}
50#else
51
Andres AG788aa4a2016-09-14 14:32:09 +010052#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/ssl.h"
54#include "mbedtls/entropy.h"
55#include "mbedtls/ctr_drbg.h"
56#include "mbedtls/certs.h"
57#include "mbedtls/x509.h"
58#include "mbedtls/error.h"
59#include "mbedtls/debug.h"
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020060#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Rich Evans18b78c72015-02-11 14:06:19 +000062#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010065
Hanno Beckere4ad3e82017-09-18 15:05:46 +010066#define MAX_REQUEST_SIZE 20000
67#define MAX_REQUEST_SIZE_STR "20000"
68
Paul Bakker9caf2d22010-02-18 19:37:19 +000069#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010070#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020071#define DFL_SERVER_PORT "4433"
Paul Bakker9caf2d22010-02-18 19:37:19 +000072#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +020073#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +000074#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010075#define DFL_NBIO 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020076#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +020077#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +000078#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000079#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000080#define DFL_CRT_FILE ""
81#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020082#define DFL_PSK ""
83#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +020084#define DFL_ECJPAKE_PW NULL
Paul Bakker51936882011-02-20 16:05:58 +000085#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010087#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010088#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +020089#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +020090#define DFL_MIN_VERSION -1
Paul Bakker1d29fb52012-09-28 13:28:45 +000091#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +000092#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +020093#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +010094#define DFL_AUTH_MODE -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +010096#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +010097#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +020098#define DFL_DHMLEN -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +020099#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100100#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200101#define DFL_RECONNECT_HARD 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200103#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100104#define DFL_CURVES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200106#define DFL_HS_TO_MIN 0
107#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200108#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200109#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100110#define DFL_ETM -1
Paul Bakker0e6975b2009-02-10 22:19:10 +0000111
Paul Bakker93c32b22014-04-25 13:40:05 +0200112#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
113#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#if defined(MBEDTLS_X509_CRT_PARSE_C)
116#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000117#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100118 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
119 " default: \"\" (pre-loaded)\n" \
120 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
121 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
122 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
123 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000124 " key_file=%%s default: \"\" (pre-loaded)\n"
125#else
126#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 " No file operations available (MBEDTLS_FS_IO not defined)\n"
128#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200129#else
130#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200134#define USAGE_PSK \
135 " psk=%%s default: \"\" (in hex, without 0x)\n" \
136 " psk_identity=%%s default: \"Client_identity\"\n"
137#else
138#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200142#define USAGE_TICKETS \
143 " tickets=%%d default: 1 (enabled)\n"
144#else
145#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200149#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100150 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200151#else
152#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200156#define USAGE_MAX_FRAG_LEN \
157 " max_frag_len=%%d default: 16384 (tls default)\n" \
158 " options: 512, 1024, 2048, 4096\n"
159#else
160#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100164#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200165 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100166#else
167#define USAGE_RECSPLIT
168#endif
169
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200170#if defined(MBEDTLS_DHM_C)
171#define USAGE_DHMLEN \
172 " dhmlen=%%d default: (library default: 1024 bits)\n"
173#else
174#define USAGE_DHMLEN
175#endif
176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200178#define USAGE_ALPN \
179 " alpn=%%s default: \"\" (disabled)\n" \
180 " example: spdy/1,http/1.1\n"
181#else
182#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200184
Hanno Beckere6706e62017-05-15 16:05:15 +0100185#if defined(MBEDTLS_ECP_C)
186#define USAGE_CURVES \
187 " curves=a,b,c,d default: \"default\" (library default)\n" \
188 " example: \"secp521r1,brainpoolP512r1\"\n" \
189 " - use \"none\" for empty list\n" \
190 " - see mbedtls_ecp_curve_list()\n" \
191 " for acceptable curve names\n"
192#else
193#define USAGE_CURVES ""
194#endif
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200197#define USAGE_DTLS \
198 " dtls=%%d default: 0 (TLS)\n" \
199 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
200 " range of DTLS handshake timeouts in millisecs\n"
201#else
202#define USAGE_DTLS ""
203#endif
204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200206#define USAGE_FALLBACK \
207 " fallback=0/1 default: (library default: off)\n"
208#else
209#define USAGE_FALLBACK ""
210#endif
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200213#define USAGE_EMS \
214 " extended_ms=0/1 default: (library default: on)\n"
215#else
216#define USAGE_EMS ""
217#endif
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100220#define USAGE_ETM \
221 " etm=0/1 default: (library default: on)\n"
222#else
223#define USAGE_ETM ""
224#endif
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100227#define USAGE_RENEGO \
228 " renegotiation=%%d default: 0 (disabled)\n" \
229 " renegotiate=%%d default: 0 (disabled)\n"
230#else
231#define USAGE_RENEGO ""
232#endif
233
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200234#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
235#define USAGE_ECJPAKE \
236 " ecjpake_pw=%%s default: none (disabled)\n"
237#else
238#define USAGE_ECJPAKE ""
239#endif
240
Paul Bakker9caf2d22010-02-18 19:37:19 +0000241#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000242 "\n usage: ssl_client2 param=<>...\n" \
243 "\n acceptable parameters:\n" \
244 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100245 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000246 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000247 " request_page=%%s default: \".\"\n" \
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100248 " request_size=%%d default: about 34 (basic request)\n" \
Andres Amaya Garcia46a6d5c2018-07-04 09:29:34 +0100249 " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
250 " If 0, in the first exchange only an empty\n" \
251 " application data message is sent followed by\n" \
252 " a second non-empty message before attempting\n" \
253 " to read a response from the server\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100254 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100255 " nbio=%%d default: 0 (blocking I/O)\n" \
256 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200257 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200258 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100259 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200260 USAGE_DTLS \
261 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100262 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100263 " options: none, optional, required\n" \
264 USAGE_IO \
265 "\n" \
266 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200267 USAGE_ECJPAKE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100268 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100269 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100270 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200271 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200272 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200273 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200274 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200275 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100276 USAGE_MAX_FRAG_LEN \
277 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200278 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200279 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200280 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100281 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100282 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100283 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200284 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000285 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100286 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200287 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200288 " min_version=%%s default: (library default: tls1)\n" \
289 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000290 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100291 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000292 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000293 " force_ciphersuite=<name> default: all enabled\n"\
294 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000295
Hanno Becker8651a432017-06-09 16:13:22 +0100296#define ALPN_LIST_SIZE 10
297#define CURVE_LIST_SIZE 20
298
Rich Evans85b05ec2015-02-12 11:37:29 +0000299/*
300 * global options
301 */
302struct options
303{
304 const char *server_name; /* hostname of the server (client only) */
305 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200306 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000307 int debug_level; /* level of debugging */
308 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000310 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000311 const char *request_page; /* page on server to request */
312 int request_size; /* pad request with header to requested size */
313 const char *ca_file; /* the file with the CA certificate(s) */
314 const char *ca_path; /* the path with the CA certificate(s) reside */
315 const char *crt_file; /* the file with the client certificate */
316 const char *key_file; /* the file with the client key */
317 const char *psk; /* the pre-shared key */
318 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200319 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000320 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
321 int renegotiation; /* enable / disable renegotiation */
322 int allow_legacy; /* allow legacy renegotiation */
323 int renegotiate; /* attempt renegotiation? */
324 int renego_delay; /* delay before enforcing renegotiation */
325 int exchanges; /* number of data exchanges */
326 int min_version; /* minimum protocol version accepted */
327 int max_version; /* maximum protocol version accepted */
328 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200329 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000330 int auth_mode; /* verify mode for connection */
331 unsigned char mfl_code; /* code for maximum fragment length */
332 int trunc_hmac; /* negotiate truncated hmac or not */
333 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200334 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000335 int reconnect; /* attempt to resume session */
336 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200337 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000338 int tickets; /* enable / disable session tickets */
Hanno Beckere6706e62017-05-15 16:05:15 +0100339 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000340 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000341 int transport; /* TLS or DTLS? */
342 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
343 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Rich Evans85b05ec2015-02-12 11:37:29 +0000344 int fallback; /* is this a fallback connection? */
345 int extended_ms; /* negotiate extended master secret? */
346 int etm; /* negotiate encrypt then mac? */
347} opt;
348
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200349static void my_debug( void *ctx, int level,
350 const char *file, int line,
351 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000352{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200353 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000354
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200355 /* Extract basename from file */
356 for( p = basename = file; *p != '\0'; p++ )
357 if( *p == '/' || *p == '\\' )
358 basename = p + 1;
359
360 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000361 fflush( (FILE *) ctx );
362}
363
364/*
365 * Test recv/send functions that make sure each try returns
366 * WANT_READ/WANT_WRITE at least once before sucesseding
367 */
368static int my_recv( void *ctx, unsigned char *buf, size_t len )
369{
370 static int first_try = 1;
371 int ret;
372
373 if( first_try )
374 {
375 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100376 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000377 }
378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100380 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000381 first_try = 1; /* Next call will be a new operation */
382 return( ret );
383}
384
385static int my_send( void *ctx, const unsigned char *buf, size_t len )
386{
387 static int first_try = 1;
388 int ret;
389
390 if( first_try )
391 {
392 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100393 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000394 }
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100397 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000398 first_try = 1; /* Next call will be a new operation */
399 return( ret );
400}
401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#if defined(MBEDTLS_X509_CRT_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000403/*
404 * Enabled if debug_level > 1 in code below
405 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200406static int my_verify( void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000407{
408 char buf[1024];
409 ((void) data);
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
412 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
413 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000414
Rich Evans85b05ec2015-02-12 11:37:29 +0000415 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100417 else
418 {
419 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
420 mbedtls_printf( "%s\n", buf );
421 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000422
423 return( 0 );
424}
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200425
426static int ssl_sig_hashes_for_test[] = {
427#if defined(MBEDTLS_SHA512_C)
428 MBEDTLS_MD_SHA512,
429 MBEDTLS_MD_SHA384,
430#endif
431#if defined(MBEDTLS_SHA256_C)
432 MBEDTLS_MD_SHA256,
433 MBEDTLS_MD_SHA224,
434#endif
435#if defined(MBEDTLS_SHA1_C)
436 /* Allow SHA-1 as we use it extensively in tests. */
437 MBEDTLS_MD_SHA1,
438#endif
439 MBEDTLS_MD_NONE
440};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000442
Paul Bakker9caf2d22010-02-18 19:37:19 +0000443int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000444{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200445 int ret = 0, len, tail_len, i, written, frags, retry_left;
446 mbedtls_net_context server_fd;
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100447
448 unsigned char buf[MAX_REQUEST_SIZE + 1];
449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
451 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200452 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200453#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100455 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200456#endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100457#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +0100458 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100459 const mbedtls_ecp_curve_info *curve_cur;
460#endif
461
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200462 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000463
Gilles Peskineef86ab22017-05-05 18:59:02 +0200464#if defined(MBEDTLS_X509_CRT_PARSE_C)
465 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
466#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_entropy_context entropy;
468 mbedtls_ctr_drbg_context ctr_drbg;
469 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200470 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200472#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200473 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200474#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200476 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_x509_crt cacert;
478 mbedtls_x509_crt clicert;
479 mbedtls_pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200480#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000481 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000482 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000483
Paul Bakker1a207ec2011-02-06 13:22:40 +0000484 /*
485 * Make sure memory references are valid.
486 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200487 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200488 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200489 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200491 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492#if defined(MBEDTLS_X509_CRT_PARSE_C)
493 mbedtls_x509_crt_init( &cacert );
494 mbedtls_x509_crt_init( &clicert );
495 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200496#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200498 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200499#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000500
Paul Bakker9caf2d22010-02-18 19:37:19 +0000501 if( argc == 0 )
502 {
503 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000504 if( ret == 0 )
505 ret = 1;
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000510 while( *list )
511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200513 list++;
514 if( !*list )
515 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000517 list++;
518 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000520 goto exit;
521 }
522
523 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100524 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000525 opt.server_port = DFL_SERVER_PORT;
526 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100527 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200528 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200529 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000530 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200531 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000532 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000533 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000534 opt.crt_file = DFL_CRT_FILE;
535 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200536 opt.psk = DFL_PSK;
537 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200538 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakker51936882011-02-20 16:05:58 +0000539 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000540 opt.renegotiation = DFL_RENEGOTIATION;
541 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100542 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200543 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000544 opt.min_version = DFL_MIN_VERSION;
545 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100546 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +0200547 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100548 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200549 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200550 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100551 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200552 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200553 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100554 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200555 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200556 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200557 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +0100558 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200559 opt.transport = DFL_TRANSPORT;
560 opt.hs_to_min = DFL_HS_TO_MIN;
561 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200562 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200563 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100564 opt.etm = DFL_ETM;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000565
566 for( i = 1; i < argc; i++ )
567 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000568 p = argv[i];
569 if( ( q = strchr( p, '=' ) ) == NULL )
570 goto usage;
571 *q++ = '\0';
572
573 if( strcmp( p, "server_name" ) == 0 )
574 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100575 else if( strcmp( p, "server_addr" ) == 0 )
576 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000577 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200578 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100579 else if( strcmp( p, "dtls" ) == 0 )
580 {
581 int t = atoi( q );
582 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100584 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100586 else
587 goto usage;
588 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000589 else if( strcmp( p, "debug_level" ) == 0 )
590 {
591 opt.debug_level = atoi( q );
592 if( opt.debug_level < 0 || opt.debug_level > 65535 )
593 goto usage;
594 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100595 else if( strcmp( p, "nbio" ) == 0 )
596 {
597 opt.nbio = atoi( q );
598 if( opt.nbio < 0 || opt.nbio > 2 )
599 goto usage;
600 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200601 else if( strcmp( p, "read_timeout" ) == 0 )
602 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200603 else if( strcmp( p, "max_resend" ) == 0 )
604 {
605 opt.max_resend = atoi( q );
606 if( opt.max_resend < 0 )
607 goto usage;
608 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000609 else if( strcmp( p, "request_page" ) == 0 )
610 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200611 else if( strcmp( p, "request_size" ) == 0 )
612 {
613 opt.request_size = atoi( q );
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100614 if( opt.request_size < 0 ||
615 opt.request_size > MAX_REQUEST_SIZE )
Paul Bakker93c32b22014-04-25 13:40:05 +0200616 goto usage;
617 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000618 else if( strcmp( p, "ca_file" ) == 0 )
619 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000620 else if( strcmp( p, "ca_path" ) == 0 )
621 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000622 else if( strcmp( p, "crt_file" ) == 0 )
623 opt.crt_file = q;
624 else if( strcmp( p, "key_file" ) == 0 )
625 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200626 else if( strcmp( p, "psk" ) == 0 )
627 opt.psk = q;
628 else if( strcmp( p, "psk_identity" ) == 0 )
629 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200630 else if( strcmp( p, "ecjpake_pw" ) == 0 )
631 opt.ecjpake_pw = q;
Paul Bakker51936882011-02-20 16:05:58 +0000632 else if( strcmp( p, "force_ciphersuite" ) == 0 )
633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +0000635
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200636 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000637 {
638 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000639 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000640 }
Paul Bakker51936882011-02-20 16:05:58 +0000641 opt.force_ciphersuite[1] = 0;
642 }
Paul Bakker48916f92012-09-16 19:57:18 +0000643 else if( strcmp( p, "renegotiation" ) == 0 )
644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 opt.renegotiation = (atoi( q )) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED :
646 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +0000647 }
648 else if( strcmp( p, "allow_legacy" ) == 0 )
649 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100650 switch( atoi( q ) )
651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 case -1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE; break;
653 case 0: opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; break;
654 case 1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION; break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100655 default: goto usage;
656 }
Paul Bakker48916f92012-09-16 19:57:18 +0000657 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100658 else if( strcmp( p, "renegotiate" ) == 0 )
659 {
660 opt.renegotiate = atoi( q );
661 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
662 goto usage;
663 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200664 else if( strcmp( p, "exchanges" ) == 0 )
665 {
666 opt.exchanges = atoi( q );
667 if( opt.exchanges < 1 )
668 goto usage;
669 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200670 else if( strcmp( p, "reconnect" ) == 0 )
671 {
672 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200673 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200674 goto usage;
675 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100676 else if( strcmp( p, "reco_delay" ) == 0 )
677 {
678 opt.reco_delay = atoi( q );
679 if( opt.reco_delay < 0 )
680 goto usage;
681 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200682 else if( strcmp( p, "reconnect_hard" ) == 0 )
683 {
684 opt.reconnect_hard = atoi( q );
685 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
686 goto usage;
687 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200688 else if( strcmp( p, "tickets" ) == 0 )
689 {
690 opt.tickets = atoi( q );
691 if( opt.tickets < 0 || opt.tickets > 2 )
692 goto usage;
693 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200694 else if( strcmp( p, "alpn" ) == 0 )
695 {
696 opt.alpn_string = q;
697 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200698 else if( strcmp( p, "fallback" ) == 0 )
699 {
700 switch( atoi( q ) )
701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
703 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200704 default: goto usage;
705 }
706 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200707 else if( strcmp( p, "extended_ms" ) == 0 )
708 {
709 switch( atoi( q ) )
710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 case 0: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED; break;
712 case 1: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200713 default: goto usage;
714 }
715 }
Hanno Beckere6706e62017-05-15 16:05:15 +0100716 else if( strcmp( p, "curves" ) == 0 )
717 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100718 else if( strcmp( p, "etm" ) == 0 )
719 {
720 switch( atoi( q ) )
721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
723 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100724 default: goto usage;
725 }
726 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000727 else if( strcmp( p, "min_version" ) == 0 )
728 {
729 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000731 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100733 else if( strcmp( q, "tls1_1" ) == 0 ||
734 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100736 else if( strcmp( q, "tls1_2" ) == 0 ||
737 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000739 else
740 goto usage;
741 }
742 else if( strcmp( p, "max_version" ) == 0 )
743 {
744 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000746 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100748 else if( strcmp( q, "tls1_1" ) == 0 ||
749 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100751 else if( strcmp( q, "tls1_2" ) == 0 ||
752 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000754 else
755 goto usage;
756 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100757 else if( strcmp( p, "arc4" ) == 0 )
758 {
759 switch( atoi( q ) )
760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
762 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100763 default: goto usage;
764 }
765 }
Gilles Peskinebc70a182017-05-09 15:59:24 +0200766 else if( strcmp( p, "allow_sha1" ) == 0 )
767 {
768 switch( atoi( q ) )
769 {
770 case 0: opt.allow_sha1 = 0; break;
771 case 1: opt.allow_sha1 = 1; break;
772 default: goto usage;
773 }
774 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000775 else if( strcmp( p, "force_version" ) == 0 )
776 {
777 if( strcmp( q, "ssl3" ) == 0 )
778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
780 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000781 }
782 else if( strcmp( q, "tls1" ) == 0 )
783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
785 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000786 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100787 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
790 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000791 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100792 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
795 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000796 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100797 else if( strcmp( q, "dtls1" ) == 0 )
798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
800 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
801 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100802 }
803 else if( strcmp( q, "dtls1_2" ) == 0 )
804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
806 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
807 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100808 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000809 else
810 goto usage;
811 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100812 else if( strcmp( p, "auth_mode" ) == 0 )
813 {
814 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100816 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100818 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100820 else
821 goto usage;
822 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200823 else if( strcmp( p, "max_frag_len" ) == 0 )
824 {
825 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200827 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200829 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200831 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200833 else
834 goto usage;
835 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200836 else if( strcmp( p, "trunc_hmac" ) == 0 )
837 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100838 switch( atoi( q ) )
839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
841 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100842 default: goto usage;
843 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200844 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200845 else if( strcmp( p, "hs_timeout" ) == 0 )
846 {
847 if( ( p = strchr( q, '-' ) ) == NULL )
848 goto usage;
849 *p++ = '\0';
850 opt.hs_to_min = atoi( q );
851 opt.hs_to_max = atoi( p );
852 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
853 goto usage;
854 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100855 else if( strcmp( p, "recsplit" ) == 0 )
856 {
857 opt.recsplit = atoi( q );
858 if( opt.recsplit < 0 || opt.recsplit > 1 )
859 goto usage;
860 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200861 else if( strcmp( p, "dhmlen" ) == 0 )
862 {
863 opt.dhmlen = atoi( q );
864 if( opt.dhmlen < 0 )
865 goto usage;
866 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000867 else
868 goto usage;
869 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#if defined(MBEDTLS_DEBUG_C)
872 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200873#endif
874
Paul Bakkerc1516be2013-06-29 16:01:32 +0200875 if( opt.force_ciphersuite[0] > 0 )
876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
878 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +0200879
Paul Bakker66c48102013-07-26 14:05:32 +0200880 if( opt.max_version != -1 &&
881 ciphersuite_info->min_minor_ver > opt.max_version )
882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakker66c48102013-07-26 14:05:32 +0200884 ret = 2;
885 goto usage;
886 }
887 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200888 ciphersuite_info->max_minor_ver < opt.min_version )
889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +0200891 ret = 2;
892 goto usage;
893 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100894
895 /* If the server selects a version that's not supported by
896 * this suite, then there will be no common ciphersuite... */
897 if( opt.max_version == -1 ||
898 opt.max_version > ciphersuite_info->max_minor_ver )
899 {
Paul Bakker66c48102013-07-26 14:05:32 +0200900 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100901 }
Paul Bakker66c48102013-07-26 14:05:32 +0200902 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100903 {
Paul Bakker66c48102013-07-26 14:05:32 +0200904 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100905 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
907 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
908 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100909 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000910
911 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000917 ret = 2;
918 goto usage;
919 }
920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000922 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200923 }
924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000926 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200927 * Unhexify the pre-shared key if any is given
928 */
929 if( strlen( opt.psk ) )
930 {
931 unsigned char c;
932 size_t j;
933
934 if( strlen( opt.psk ) % 2 != 0 )
935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200937 goto exit;
938 }
939
940 psk_len = strlen( opt.psk ) / 2;
941
942 for( j = 0; j < strlen( opt.psk ); j += 2 )
943 {
944 c = opt.psk[j];
945 if( c >= '0' && c <= '9' )
946 c -= '0';
947 else if( c >= 'a' && c <= 'f' )
948 c -= 'a' - 10;
949 else if( c >= 'A' && c <= 'F' )
950 c -= 'A' - 10;
951 else
952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200954 goto exit;
955 }
956 psk[ j / 2 ] = c << 4;
957
958 c = opt.psk[j + 1];
959 if( c >= '0' && c <= '9' )
960 c -= '0';
961 else if( c >= 'a' && c <= 'f' )
962 c -= 'a' - 10;
963 else if( c >= 'A' && c <= 'F' )
964 c -= 'A' - 10;
965 else
966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 mbedtls_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200968 goto exit;
969 }
970 psk[ j / 2 ] |= c;
971 }
972 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200974
Hanno Beckere6706e62017-05-15 16:05:15 +0100975#if defined(MBEDTLS_ECP_C)
976 if( opt.curves != NULL )
977 {
978 p = (char *) opt.curves;
979 i = 0;
980
981 if( strcmp( p, "none" ) == 0 )
982 {
983 curve_list[0] = MBEDTLS_ECP_DP_NONE;
984 }
985 else if( strcmp( p, "default" ) != 0 )
986 {
987 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +0100988 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +0100989 {
990 q = p;
991
992 /* Terminate the current string */
993 while( *p != ',' && *p != '\0' )
994 p++;
995 if( *p == ',' )
996 *p++ = '\0';
997
998 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
999 {
1000 curve_list[i++] = curve_cur->grp_id;
1001 }
1002 else
1003 {
1004 mbedtls_printf( "unknown curve %s\n", q );
1005 mbedtls_printf( "supported curves: " );
1006 for( curve_cur = mbedtls_ecp_curve_list();
1007 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1008 curve_cur++ )
1009 {
1010 mbedtls_printf( "%s ", curve_cur->name );
1011 }
1012 mbedtls_printf( "\n" );
1013 goto exit;
1014 }
1015 }
1016
1017 mbedtls_printf("Number of curves: %d\n", i );
1018
Hanno Becker8651a432017-06-09 16:13:22 +01001019 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001020 {
Hanno Becker8651a432017-06-09 16:13:22 +01001021 mbedtls_printf( "curves list too long, maximum %d",
1022 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001023 goto exit;
1024 }
1025
1026 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1027 }
1028 }
1029#endif /* MBEDTLS_ECP_C */
1030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001032 if( opt.alpn_string != NULL )
1033 {
1034 p = (char *) opt.alpn_string;
1035 i = 0;
1036
1037 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001038 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001039 {
1040 alpn_list[i++] = p;
1041
1042 /* Terminate the current string and move on to next one */
1043 while( *p != ',' && *p != '\0' )
1044 p++;
1045 if( *p == ',' )
1046 *p++ = '\0';
1047 }
1048 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001050
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001051 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001052 * 0. Initialize the RNG and the session data
1053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001055 fflush( stdout );
1056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001058 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001059 (const unsigned char *) pers,
1060 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +00001061 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001062 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n", -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001063 goto exit;
1064 }
1065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001069 /*
1070 * 1.1. Load the trusted CA
1071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001073 fflush( stdout );
1074
Hanno Beckerf74f5ce2019-03-05 16:02:15 +00001075 if( strcmp( opt.ca_path, "none" ) == 0 ||
1076 strcmp( opt.ca_file, "none" ) == 0 )
1077 {
1078 ret = 0;
1079 }
1080 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001082 if( strlen( opt.ca_path ) )
Hanno Beckerf74f5ce2019-03-05 16:02:15 +00001083 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001084 else if( strlen( opt.ca_file ) )
Hanno Beckerf74f5ce2019-03-05 16:02:15 +00001085 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001086 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001087#endif
Hanno Becker37e7db22019-03-05 16:10:27 +00001088#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 ret = mbedtls_x509_crt_parse( &cacert,
1092 (const unsigned char *) mbedtls_test_cas[i],
1093 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001094 if( ret != 0 )
1095 break;
1096 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001097#else
1098 {
1099 ret = 1;
Hanno Becker37e7db22019-03-05 16:10:27 +00001100#if !defined(MBEDTLS_CERTS_C)
1101 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
1102#else
1103 mbedtls_printf( "All test CRTs loaded via MBEDTLS_CERTS_C are PEM-encoded, but MBEDTLS_PEM_PARSE_C is disabled." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001104 }
Hanno Becker37e7db22019-03-05 16:10:27 +00001105#endif /* MBEDTLS_CERTS_C */
1106#endif /* MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C */
Paul Bakker42488232012-05-16 08:21:05 +00001107 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 goto exit;
1111 }
1112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001114
1115 /*
1116 * 1.2. Load own certificate and private key
1117 *
1118 * (can be skipped if client authentication is not required)
1119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 fflush( stdout );
1122
Hanno Beckerf74f5ce2019-03-05 16:02:15 +00001123 if( strcmp( opt.crt_file, "none" ) == 0 )
1124 ret = 0;
1125 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001127 if( strlen( opt.crt_file ) )
Hanno Beckerf74f5ce2019-03-05 16:02:15 +00001128 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001129 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001130#endif
Hanno Becker37e7db22019-03-05 16:10:27 +00001131#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
1133 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001134#else
1135 {
1136 ret = 1;
Hanno Becker37e7db22019-03-05 16:10:27 +00001137#if !defined(MBEDTLS_CERTS_C)
1138 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
1139#else
1140 mbedtls_printf( "All test CRTs loaded via MBEDTLS_CERTS_C are PEM-encoded, but MBEDTLS_PEM_PARSE_C is disabled." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001141 }
Hanno Becker37e7db22019-03-05 16:10:27 +00001142#endif /* MBEDTLS_CERTS_C */
1143#endif /* MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001144 if( ret != 0 )
1145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001147 goto exit;
1148 }
1149
Hanno Beckerf74f5ce2019-03-05 16:02:15 +00001150 if( strcmp( opt.key_file, "none" ) == 0 )
1151 ret = 0;
1152 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001154 if( strlen( opt.key_file ) )
Hanno Beckerf74f5ce2019-03-05 16:02:15 +00001155 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001156 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001157#endif
Hanno Becker37e7db22019-03-05 16:10:27 +00001158#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_cli_key,
1160 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001161#else
1162 {
1163 ret = 1;
Hanno Becker37e7db22019-03-05 16:10:27 +00001164#if !defined(MBEDTLS_CERTS_C)
1165 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
1166#else
1167 mbedtls_printf( "All test keys loaded via MBEDTLS_CERTS_C are PEM-encoded, but MBEDTLS_PEM_PARSE_C is disabled." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001168 }
Hanno Becker37e7db22019-03-05 16:10:27 +00001169#endif /* MBEDTLS_CERTS_C */
1170#endif /* MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001171 if( ret != 0 )
1172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 goto exit;
1175 }
1176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 mbedtls_printf( " ok\n" );
1178#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001179
1180 /*
1181 * 2. Start the connection
1182 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001183 if( opt.server_addr == NULL)
1184 opt.server_addr = opt.server_name;
1185
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001186 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001188 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 fflush( stdout );
1190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
1192 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1193 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 goto exit;
1197 }
1198
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001199 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001200 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001201 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001202 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001203 if( ret != 0 )
1204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001206 goto exit;
1207 }
1208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001210
1211 /*
1212 * 3. Setup stuff
1213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 fflush( stdout );
1216
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001217 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1218 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001219 opt.transport,
1220 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001221 {
1222 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
1223 goto exit;
1224 }
1225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001227 /* The default algorithms profile disables SHA-1, but our tests still
1228 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001229 if( opt.allow_sha1 > 0 )
1230 {
1231 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1232 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1233 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1234 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001235
Paul Bakker915275b2012-09-28 07:10:55 +00001236 if( opt.debug_level > 0 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001237 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
Gilles Peskineef86ab22017-05-05 18:59:02 +02001238#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001239
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001240 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001241 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001244 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 +02001245 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001249 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001250 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001251 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001252 goto exit;
1253 }
Paul Bakker05decb22013-08-15 13:33:48 +02001254#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001257 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001258 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001259#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001262 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001263 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001264#endif
1265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001267 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001268 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001269#endif
1270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001272 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001273 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1275 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001276#endif
1277
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001278#if defined(MBEDTLS_DHM_C)
1279 if( opt.dhmlen != DFL_DHMLEN )
1280 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1281#endif
1282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001284 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001285 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001286 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001287 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001288 goto exit;
1289 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001290#endif
1291
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001292 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1293 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001294
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001295 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02001298 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02001299#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001300
Paul Bakker645ce3a2012-10-31 12:32:41 +00001301 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001302 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001303
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001304#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001305 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001306 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001307#endif
Paul Bakker51936882011-02-20 16:05:58 +00001308
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001309 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001310 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001312 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001313#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001316 if( strcmp( opt.ca_path, "none" ) != 0 &&
1317 strcmp( opt.ca_file, "none" ) != 0 )
1318 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001319 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001320 }
1321 if( strcmp( opt.crt_file, "none" ) != 0 &&
1322 strcmp( opt.key_file, "none" ) != 0 )
1323 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001324 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001325 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001326 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001327 goto exit;
1328 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001329 }
Paul Bakkered27a042013-04-18 22:46:23 +02001330#endif
1331
Hanno Beckere6706e62017-05-15 16:05:15 +01001332#if defined(MBEDTLS_ECP_C)
1333 if( opt.curves != NULL &&
1334 strcmp( opt.curves, "default" ) != 0 )
1335 {
1336 mbedtls_ssl_conf_curves( &conf, curve_list );
1337 }
1338#endif
1339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001341 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001342 (const unsigned char *) opt.psk_identity,
1343 strlen( opt.psk_identity ) ) ) != 0 )
1344 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001345 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001346 goto exit;
1347 }
Paul Bakkered27a042013-04-18 22:46:23 +02001348#endif
1349
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001350 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001351 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001352
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001353 if( opt.max_version != DFL_MAX_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001354 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001357 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001358 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001359#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001360
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001361 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
1362 {
1363 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
1364 goto exit;
1365 }
1366
1367#if defined(MBEDTLS_X509_CRT_PARSE_C)
1368 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1369 {
1370 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
1371 goto exit;
1372 }
1373#endif
1374
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001375#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1376 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
1377 {
1378 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
1379 (const unsigned char *) opt.ecjpake_pw,
1380 strlen( opt.ecjpake_pw ) ) ) != 0 )
1381 {
1382 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
1383 goto exit;
1384 }
1385 }
1386#endif
1387
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001388 if( opt.nbio == 2 )
1389 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
1390 else
1391 mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02001392 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001393
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001394#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001395 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
1396 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001397#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001400
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 /*
1402 * 4. Handshake
1403 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 fflush( stdout );
1406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001409 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker40e46942009-01-03 21:51:57 +00001410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n", -ret );
1412 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
1413 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001414 " Unable to verify the server's certificate. "
1415 "Either it is invalid,\n"
1416 " or you didn't set ca_file or ca_path "
1417 "to an appropriate value.\n"
1418 " Alternatively, you may want to use "
1419 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00001422 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001423 }
1424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1426 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
1429 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001430 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001432
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02001433#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1434 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
1435 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
1436#endif
1437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001439 if( opt.alpn_string != NULL )
1440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
1442 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001443 alp ? alp : "(none)" );
1444 }
1445#endif
1446
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001447 if( opt.reconnect != 0 )
1448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001450 fflush( stdout );
1451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001455 goto exit;
1456 }
1457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001459 }
1460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 /*
1463 * 5. Verify the server certificate
1464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001466
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001467 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001468 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001469 char vrfy_buf[512];
1470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001472
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001473 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00001474
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001475 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001476 }
1477 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 mbedtls_printf( " . Peer certificate information ...\n" );
1483 mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1484 mbedtls_ssl_get_peer_cert( &ssl ) );
1485 mbedtls_printf( "%s\n", buf );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001486 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001490 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001491 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001492 /*
1493 * Perform renegotiation (this must be done when the server is waiting
1494 * for input from our side).
1495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001497 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001499 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001500 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1501 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001504 goto exit;
1505 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001506 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001508 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001510
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 /*
1512 * 6. Write the GET request
1513 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001514 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001515send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001517 fflush( stdout );
1518
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001519 len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST,
1520 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001521 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001522
1523 /* Add padding to GET request to reach opt.request_size in length */
1524 if( opt.request_size != DFL_REQUEST_SIZE &&
1525 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001526 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001527 memset( buf + len, 'A', opt.request_size - len - tail_len );
1528 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001529 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001530
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001531 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001532 len += tail_len;
1533
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001534 /* Truncate if request size is smaller than the "natural" size */
1535 if( opt.request_size != DFL_REQUEST_SIZE &&
1536 len > opt.request_size )
1537 {
1538 len = opt.request_size;
1539
1540 /* Still end with \r\n unless that's really not possible */
1541 if( len >= 2 ) buf[len - 2] = '\r';
1542 if( len >= 1 ) buf[len - 1] = '\n';
1543 }
1544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001546 {
Andres Amaya Garcia46a6d5c2018-07-04 09:29:34 +01001547 written = 0;
1548 frags = 0;
1549
1550 do
Paul Bakker5121ce52009-01-03 21:22:43 +00001551 {
Andres Amaya Garcia46a6d5c2018-07-04 09:29:34 +01001552 while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
1553 len - written ) ) < 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001554 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001555 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1556 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001559 goto exit;
1560 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001561 }
Andres Amaya Garcia46a6d5c2018-07-04 09:29:34 +01001562
1563 frags++;
1564 written += ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001565 }
Andres Amaya Garcia46a6d5c2018-07-04 09:29:34 +01001566 while( written < len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001567 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001568 else /* Not stream, so datagram */
1569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 do ret = mbedtls_ssl_write( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001571 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1572 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001573
1574 if( ret < 0 )
1575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001577 goto exit;
1578 }
1579
1580 frags = 1;
1581 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001582
1583 if( written < len )
1584 {
1585 mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
1586 "was truncated to size %u", (unsigned) written );
1587 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001588 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001589
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001590 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001592
Andres Amaya Garcia46a6d5c2018-07-04 09:29:34 +01001593 /* Send a non-empty request if request_size == 0 */
1594 if ( len == 0 )
1595 {
1596 opt.request_size = DFL_REQUEST_SIZE;
1597 goto send_request;
1598 }
1599
Paul Bakker5121ce52009-01-03 21:22:43 +00001600 /*
1601 * 7. Read the HTTP response
1602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001604 fflush( stdout );
1605
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001606 /*
1607 * TLS and DTLS need different reading styles (stream vs datagram)
1608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001610 {
1611 do
1612 {
1613 len = sizeof( buf ) - 1;
1614 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001616
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001617 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1618 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001619 continue;
1620
1621 if( ret <= 0 )
1622 {
1623 switch( ret )
1624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1626 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001627 ret = 0;
1628 goto close_notify;
1629
1630 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 case MBEDTLS_ERR_NET_CONN_RESET:
1632 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001633 ret = 0;
1634 goto reconnect;
1635
1636 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001638 goto exit;
1639 }
1640 }
1641
1642 len = ret;
1643 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001645
1646 /* End of message should be detected according to the syntax of the
1647 * application protocol (eg HTTP), just use a dummy test here. */
1648 if( ret > 0 && buf[len-1] == '\n' )
1649 {
1650 ret = 0;
1651 break;
1652 }
1653 }
1654 while( 1 );
1655 }
1656 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001657 {
1658 len = sizeof( buf ) - 1;
1659 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 do ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001662 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1663 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001664
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001665 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001666 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001667 switch( ret )
1668 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001669 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001671 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001672 goto send_request;
1673 goto exit;
1674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1676 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001677 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001678 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00001679
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001680 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001682 goto exit;
1683 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001684 }
1685
Paul Bakker5121ce52009-01-03 21:22:43 +00001686 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02001687 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001689 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001690 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001691
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001692 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001693 * 7b. Simulate hard reset and reconnect from same port?
1694 */
1695 if( opt.reconnect_hard != 0 )
1696 {
1697 opt.reconnect_hard = 0;
1698
1699 mbedtls_printf( " . Restarting connection from same port..." );
1700 fflush( stdout );
1701
1702 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
1703 {
1704 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", -ret );
1705 goto exit;
1706 }
1707
1708 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
1709 {
1710 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1711 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1712 {
1713 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
1714 goto exit;
1715 }
1716 }
1717
1718 mbedtls_printf( " ok\n" );
1719
1720 goto send_request;
1721 }
1722
1723 /*
1724 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001725 */
1726 if( --opt.exchanges > 0 )
1727 goto send_request;
1728
1729 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001730 * 8. Done, cleanly close the connection
1731 */
1732close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001734 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001735
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001736 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001738 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001739 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001742
1743 /*
1744 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001745 */
1746reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001747 if( opt.reconnect != 0 )
1748 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001749 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001750
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001751 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001752
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001753#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001754 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02001755 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001756#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02001757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001763 goto exit;
1764 }
1765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001767 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001768 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001769 goto exit;
1770 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
1773 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1774 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001777 goto exit;
1778 }
1779
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001780 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001781 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001782 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001783 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001784 if( ret != 0 )
1785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001787 -ret );
1788 goto exit;
1789 }
1790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001792 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001793 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1794 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001797 goto exit;
1798 }
1799 }
1800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001802
1803 goto send_request;
1804 }
1805
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001806 /*
1807 * Cleanup and exit
1808 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001809exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00001811 if( ret != 0 )
1812 {
1813 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 mbedtls_strerror( ret, error_buf, 100 );
1815 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001816 }
1817#endif
1818
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001819 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821#if defined(MBEDTLS_X509_CRT_PARSE_C)
1822 mbedtls_x509_crt_free( &clicert );
1823 mbedtls_x509_crt_free( &cacert );
1824 mbedtls_pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001825#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 mbedtls_ssl_session_free( &saved_session );
1827 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001828 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 mbedtls_ctr_drbg_free( &ctr_drbg );
1830 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00001831
Paul Bakkercce9d772011-11-18 14:26:47 +00001832#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001834 fflush( stdout ); getchar();
1835#endif
1836
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001837 // Shell can not handle large exit numbers -> 1 for errors
1838 if( ret < 0 )
1839 ret = 1;
1840
Paul Bakker5121ce52009-01-03 21:22:43 +00001841 return( ret );
1842}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
1844 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001845 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */