blob: bbd4d255559bf2e75c840f692c9a877d6ac5dd77 [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
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010038#define mbedtls_exit exit
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020039#define mbedtls_calloc calloc
40#define mbedtls_free free
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010041#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
42#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evansf90016a2015-01-19 14:26:37 +000043#endif
44
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020045#if !defined(MBEDTLS_ENTROPY_C) || \
46 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020047 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020048int main( void )
49{
50 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
51 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020052 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020053 return( 0 );
54}
55#else
56
Andres AG788aa4a2016-09-14 14:32:09 +010057#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/ssl.h"
59#include "mbedtls/entropy.h"
60#include "mbedtls/ctr_drbg.h"
61#include "mbedtls/certs.h"
62#include "mbedtls/x509.h"
63#include "mbedtls/error.h"
64#include "mbedtls/debug.h"
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020065#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Rich Evans18b78c72015-02-11 14:06:19 +000067#include <stdio.h>
68#include <stdlib.h>
69#include <string.h>
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010070
Hanno Beckere4ad3e82017-09-18 15:05:46 +010071#define MAX_REQUEST_SIZE 20000
72#define MAX_REQUEST_SIZE_STR "20000"
73
Paul Bakker9caf2d22010-02-18 19:37:19 +000074#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010075#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020076#define DFL_SERVER_PORT "4433"
Paul Bakker9caf2d22010-02-18 19:37:19 +000077#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +020078#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +000079#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010080#define DFL_NBIO 0
Hanno Becker16970d22017-10-10 15:56:37 +010081#define DFL_EVENT 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020082#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +020083#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +000084#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000085#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000086#define DFL_CRT_FILE ""
87#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020088#define DFL_PSK ""
89#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +020090#define DFL_ECJPAKE_PW NULL
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +020091#define DFL_EC_MAX_OPS -1
Paul Bakker51936882011-02-20 16:05:58 +000092#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010094#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010095#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +020096#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +020097#define DFL_MIN_VERSION -1
Paul Bakker1d29fb52012-09-28 13:28:45 +000098#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +000099#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +0200100#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100101#define DFL_AUTH_MODE -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100103#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100104#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200105#define DFL_DHMLEN -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200106#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100107#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +0200108#define DFL_RECO_MODE 1
Hanno Becker7a7aa192019-04-09 17:24:19 +0100109#define DFL_CID_ENABLED 0
110#define DFL_CID_VALUE ""
Hanno Becker96870292019-05-03 17:30:59 +0100111#define DFL_CID_ENABLED_RENEGO -1
112#define DFL_CID_VALUE_RENEGO NULL
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200113#define DFL_RECONNECT_HARD 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200115#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100116#define DFL_CURVES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200118#define DFL_HS_TO_MIN 0
119#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200120#define DFL_DTLS_MTU -1
Hanno Becker4d615912018-08-14 13:33:30 +0100121#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200122#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200123#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100124#define DFL_ETM -1
Jarno Lamsa41b35912019-06-10 15:51:11 +0300125#define DFL_EXTENDED_MS_ENFORCE -1
Paul Bakker0e6975b2009-02-10 22:19:10 +0000126
Paul Bakker93c32b22014-04-25 13:40:05 +0200127#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
128#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#if defined(MBEDTLS_X509_CRT_PARSE_C)
131#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000132#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100133 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
134 " default: \"\" (pre-loaded)\n" \
135 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
136 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
137 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
138 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000139 " key_file=%%s default: \"\" (pre-loaded)\n"
140#else
141#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 " No file operations available (MBEDTLS_FS_IO not defined)\n"
143#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200144#else
145#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200147
Hanno Beckera5a2b082019-05-15 14:03:01 +0100148#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7a7aa192019-04-09 17:24:19 +0100149#define USAGE_CID \
150 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
151 " default: 0 (disabled)\n" \
Hanno Becker96870292019-05-03 17:30:59 +0100152 " cid_renego=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension during renegotiation.\n" \
Hanno Beckerc8f43d82019-05-23 17:01:06 +0100153 " default: same as 'cid' parameter\n" \
Hanno Becker7a7aa192019-04-09 17:24:19 +0100154 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
Hanno Becker96870292019-05-03 17:30:59 +0100155 " default: \"\"\n" \
156 " cid_val_renego=%%s The CID to use for incoming messages (in hex, without 0x) after renegotiation.\n" \
Hanno Beckerc8f43d82019-05-23 17:01:06 +0100157 " default: same as 'cid_val' parameter\n"
Hanno Beckera5a2b082019-05-15 14:03:01 +0100158#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100159#define USAGE_CID ""
Hanno Beckera5a2b082019-05-15 14:03:01 +0100160#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200163#define USAGE_PSK \
164 " psk=%%s default: \"\" (in hex, without 0x)\n" \
165 " psk_identity=%%s default: \"Client_identity\"\n"
166#else
167#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200171#define USAGE_TICKETS \
172 " tickets=%%d default: 1 (enabled)\n"
173#else
174#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200178#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100179 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200180#else
181#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200185#define USAGE_MAX_FRAG_LEN \
186 " max_frag_len=%%d default: 16384 (tls default)\n" \
187 " options: 512, 1024, 2048, 4096\n"
188#else
189#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100193#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200194 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100195#else
196#define USAGE_RECSPLIT
197#endif
198
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200199#if defined(MBEDTLS_DHM_C)
200#define USAGE_DHMLEN \
201 " dhmlen=%%d default: (library default: 1024 bits)\n"
202#else
203#define USAGE_DHMLEN
204#endif
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200207#define USAGE_ALPN \
208 " alpn=%%s default: \"\" (disabled)\n" \
209 " example: spdy/1,http/1.1\n"
210#else
211#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200213
Hanno Beckere6706e62017-05-15 16:05:15 +0100214#if defined(MBEDTLS_ECP_C)
215#define USAGE_CURVES \
216 " curves=a,b,c,d default: \"default\" (library default)\n" \
217 " example: \"secp521r1,brainpoolP512r1\"\n" \
218 " - use \"none\" for empty list\n" \
219 " - see mbedtls_ecp_curve_list()\n" \
220 " for acceptable curve names\n"
221#else
222#define USAGE_CURVES ""
223#endif
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200226#define USAGE_DTLS \
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +0100227 " dtls=%%d default: 0 (TLS) (if both enabled)\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200228 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200229 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Becker4d615912018-08-14 13:33:30 +0100230 " mtu=%%d default: (library default: unlimited)\n" \
231 " dgram_packing=%%d default: 1 (allowed)\n" \
232 " allow or forbid packing of multiple\n" \
233 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200234#else
235#define USAGE_DTLS ""
236#endif
237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200239#define USAGE_FALLBACK \
240 " fallback=0/1 default: (library default: off)\n"
241#else
242#define USAGE_FALLBACK ""
243#endif
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200246#define USAGE_EMS \
Jarno Lamsa41b35912019-06-10 15:51:11 +0300247 " extended_ms=0/1 default: (library default: on)\n" \
248 " enforce_extended_master_secret=0/1 default: (library default: off)\n"
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200249#else
250#define USAGE_EMS ""
251#endif
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100254#define USAGE_ETM \
255 " etm=0/1 default: (library default: on)\n"
256#else
257#define USAGE_ETM ""
258#endif
259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100261#define USAGE_RENEGO \
262 " renegotiation=%%d default: 0 (disabled)\n" \
263 " renegotiate=%%d default: 0 (disabled)\n"
264#else
265#define USAGE_RENEGO ""
266#endif
267
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200268#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
269#define USAGE_ECJPAKE \
270 " ecjpake_pw=%%s default: none (disabled)\n"
271#else
272#define USAGE_ECJPAKE ""
273#endif
274
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200275#if defined(MBEDTLS_ECP_RESTARTABLE)
276#define USAGE_ECRESTART \
277 " ec_max_ops=%%s default: library default (restart disabled)\n"
278#else
279#define USAGE_ECRESTART ""
280#endif
281
Paul Bakker9caf2d22010-02-18 19:37:19 +0000282#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000283 "\n usage: ssl_client2 param=<>...\n" \
284 "\n acceptable parameters:\n" \
285 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100286 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000287 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000288 " request_page=%%s default: \".\"\n" \
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +0100289 " request_size=%%d default: about 34 (basic request)\n" \
290 " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
291 " If 0, in the first exchange only an empty\n" \
292 " application data message is sent followed by\n" \
293 " a second non-empty message before attempting\n" \
294 " to read a response from the server\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100295 " debug_level=%%d default: 0 (disabled)\n" \
296 " nbio=%%d default: 0 (blocking I/O)\n" \
297 " options: 1 (non-blocking), 2 (added delays)\n" \
298 " event=%%d default: 0 (loop)\n" \
299 " options: 1 (level-triggered, implies nbio=1),\n" \
300 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200301 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100302 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200303 USAGE_DTLS \
Hanno Becker7a7aa192019-04-09 17:24:19 +0100304 USAGE_CID \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200305 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100306 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100307 " options: none, optional, required\n" \
308 USAGE_IO \
309 "\n" \
310 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200311 USAGE_ECJPAKE \
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200312 USAGE_ECRESTART \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100313 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100314 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100315 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200316 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +0200317 " reconnect=%%d number of reconnections using session resumption\n" \
318 " default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200319 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +0200320 " reco_mode=%%d 0: copy session, 1: serialize session\n" \
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +0200321 " default: 1\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200322 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200323 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100324 USAGE_MAX_FRAG_LEN \
325 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200326 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200327 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200328 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100329 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100330 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100331 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200332 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000333 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100334 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200335 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200336 " min_version=%%s default: (library default: tls1)\n" \
337 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000338 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100339 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000340 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000341 " force_ciphersuite=<name> default: all enabled\n"\
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100342 " query_config=<name> return 0 if the specified\n" \
343 " configuration macro is defined and 1\n" \
344 " otherwise. The expansion of the macro\n" \
345 " is printed if it is defined\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000346 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000347
Hanno Becker8651a432017-06-09 16:13:22 +0100348#define ALPN_LIST_SIZE 10
349#define CURVE_LIST_SIZE 20
350
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100351#if defined(MBEDTLS_CHECK_PARAMS)
352#include "mbedtls/platform_util.h"
353void mbedtls_param_failed( const char *failure_condition,
354 const char *file,
355 int line )
Simon Butcher63cb97e2018-12-06 17:43:31 +0000356{
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100357 mbedtls_printf( "%s:%i: Input param failed - %s\n",
358 file, line, failure_condition );
Simon Butcher63cb97e2018-12-06 17:43:31 +0000359 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
360}
361#endif
362
Rich Evans85b05ec2015-02-12 11:37:29 +0000363/*
364 * global options
365 */
366struct options
367{
368 const char *server_name; /* hostname of the server (client only) */
369 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200370 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000371 int debug_level; /* level of debugging */
372 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100373 int event; /* loop or event-driven IO? level or edge triggered? */
374 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000375 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000376 const char *request_page; /* page on server to request */
377 int request_size; /* pad request with header to requested size */
378 const char *ca_file; /* the file with the CA certificate(s) */
379 const char *ca_path; /* the path with the CA certificate(s) reside */
380 const char *crt_file; /* the file with the client certificate */
381 const char *key_file; /* the file with the client key */
382 const char *psk; /* the pre-shared key */
383 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200384 const char *ecjpake_pw; /* the EC J-PAKE password */
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200385 int ec_max_ops; /* EC consecutive operations limit */
Rich Evans85b05ec2015-02-12 11:37:29 +0000386 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
387 int renegotiation; /* enable / disable renegotiation */
388 int allow_legacy; /* allow legacy renegotiation */
389 int renegotiate; /* attempt renegotiation? */
390 int renego_delay; /* delay before enforcing renegotiation */
391 int exchanges; /* number of data exchanges */
392 int min_version; /* minimum protocol version accepted */
393 int max_version; /* maximum protocol version accepted */
394 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200395 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000396 int auth_mode; /* verify mode for connection */
397 unsigned char mfl_code; /* code for maximum fragment length */
398 int trunc_hmac; /* negotiate truncated hmac or not */
399 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200400 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000401 int reconnect; /* attempt to resume session */
402 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +0200403 int reco_mode; /* how to keep the session around */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200404 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000405 int tickets; /* enable / disable session tickets */
Hanno Beckere6706e62017-05-15 16:05:15 +0100406 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000407 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000408 int transport; /* TLS or DTLS? */
409 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
410 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200411 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Rich Evans85b05ec2015-02-12 11:37:29 +0000412 int fallback; /* is this a fallback connection? */
Hanno Becker4d615912018-08-14 13:33:30 +0100413 int dgram_packing; /* allow/forbid datagram packing */
Rich Evans85b05ec2015-02-12 11:37:29 +0000414 int extended_ms; /* negotiate extended master secret? */
Jarno Lamsa41b35912019-06-10 15:51:11 +0300415 int enforce_extended_master_secret; /* Enforce the usage of extended
416 * master secret */
Rich Evans85b05ec2015-02-12 11:37:29 +0000417 int etm; /* negotiate encrypt then mac? */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100418 int cid_enabled; /* whether to use the CID extension or not */
Hanno Becker96870292019-05-03 17:30:59 +0100419 int cid_enabled_renego; /* whether to use the CID extension or not
420 * during renegotiation */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100421 const char *cid_val; /* the CID to use for incoming messages */
Hanno Becker96870292019-05-03 17:30:59 +0100422 const char *cid_val_renego; /* the CID to use for incoming messages
423 * after renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000424} opt;
425
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100426int query_config( const char *config );
427
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200428static void my_debug( void *ctx, int level,
429 const char *file, int line,
430 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000431{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200432 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000433
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200434 /* Extract basename from file */
435 for( p = basename = file; *p != '\0'; p++ )
436 if( *p == '/' || *p == '\\' )
437 basename = p + 1;
438
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100439 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s",
440 basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000441 fflush( (FILE *) ctx );
442}
443
444/*
445 * Test recv/send functions that make sure each try returns
446 * WANT_READ/WANT_WRITE at least once before sucesseding
447 */
448static int my_recv( void *ctx, unsigned char *buf, size_t len )
449{
450 static int first_try = 1;
451 int ret;
452
453 if( first_try )
454 {
455 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100456 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000457 }
458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100460 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000461 first_try = 1; /* Next call will be a new operation */
462 return( ret );
463}
464
465static int my_send( void *ctx, const unsigned char *buf, size_t len )
466{
467 static int first_try = 1;
468 int ret;
469
470 if( first_try )
471 {
472 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100473 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000474 }
475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100477 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000478 first_try = 1; /* Next call will be a new operation */
479 return( ret );
480}
481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482#if defined(MBEDTLS_X509_CRT_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000483/*
484 * Enabled if debug_level > 1 in code below
485 */
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100486static int my_verify( void *data, mbedtls_x509_crt *crt,
487 int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000488{
489 char buf[1024];
490 ((void) data);
491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
493 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
494 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000495
Rich Evans85b05ec2015-02-12 11:37:29 +0000496 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100498 else
499 {
500 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
501 mbedtls_printf( "%s\n", buf );
502 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000503
504 return( 0 );
505}
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200506
507static int ssl_sig_hashes_for_test[] = {
508#if defined(MBEDTLS_SHA512_C)
509 MBEDTLS_MD_SHA512,
510 MBEDTLS_MD_SHA384,
511#endif
512#if defined(MBEDTLS_SHA256_C)
513 MBEDTLS_MD_SHA256,
514 MBEDTLS_MD_SHA224,
515#endif
516#if defined(MBEDTLS_SHA1_C)
517 /* Allow SHA-1 as we use it extensively in tests. */
518 MBEDTLS_MD_SHA1,
519#endif
520 MBEDTLS_MD_NONE
521};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000523
Hanno Becker16970d22017-10-10 15:56:37 +0100524/*
525 * Wait for an event from the underlying transport or the timer
526 * (Used in event-driven IO mode).
527 */
528#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000529int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000530 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100531#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000532int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000533 mbedtls_timing_delay_context *timer,
534 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100535#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000536{
Hanno Becker16970d22017-10-10 15:56:37 +0100537
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000538 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +0100539 int poll_type = 0;
540
541 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
542 poll_type = MBEDTLS_NET_POLL_WRITE;
543 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
544 poll_type = MBEDTLS_NET_POLL_READ;
545#if !defined(MBEDTLS_TIMING_C)
546 else
Hanno Beckeref527962018-03-15 15:49:24 +0000547 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100548#endif
549
550 while( 1 )
551 {
Hanno Becker197a91c2017-10-31 10:58:53 +0000552 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +0100553#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +0000554 if( timer != NULL &&
555 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +0100556 {
Hanno Becker16970d22017-10-10 15:56:37 +0100557 break;
558 }
Hanno Becker197a91c2017-10-31 10:58:53 +0000559#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +0100560
Hanno Becker197a91c2017-10-31 10:58:53 +0000561 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000562 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +0100563 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000564 ret = mbedtls_net_poll( fd, poll_type, 0 );
565 if( ret < 0 )
566 return( ret );
567 if( ret == poll_type )
568 break;
Hanno Becker16970d22017-10-10 15:56:37 +0100569 }
570 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000571
572 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100573}
574
Hanno Beckerec370302019-04-09 17:12:56 +0100575/* Unhexify `hex` into `dst`. `dst` must have
576 * size at least `strlen( hex ) / 2`. */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100577int unhexify( char const *hex, unsigned char *dst )
Hanno Beckerec370302019-04-09 17:12:56 +0100578{
579 unsigned char c;
580 size_t j;
581 size_t len = strlen( hex );
582
583 if( len % 2 != 0 )
584 return( -1 );
585
586 for( j = 0; j < len; j += 2 )
587 {
588 c = hex[j];
589 if( c >= '0' && c <= '9' )
590 c -= '0';
591 else if( c >= 'a' && c <= 'f' )
592 c -= 'a' - 10;
593 else if( c >= 'A' && c <= 'F' )
594 c -= 'A' - 10;
595 else
596 return( -1 );
597 dst[ j / 2 ] = c << 4;
598
599 c = hex[j + 1];
600 if( c >= '0' && c <= '9' )
601 c -= '0';
602 else if( c >= 'a' && c <= 'f' )
603 c -= 'a' - 10;
604 else if( c >= 'A' && c <= 'F' )
605 c -= 'A' - 10;
606 else
607 return( -1 );
608 dst[ j / 2 ] |= c;
609 }
610
611 return( 0 );
612}
613
Hanno Beckera5a2b082019-05-15 14:03:01 +0100614#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +0100615int report_cid_usage( mbedtls_ssl_context *ssl,
616 const char *additional_description )
617{
618 int ret;
619 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
620 size_t peer_cid_len;
621 int cid_negotiated;
622
623 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
624 return( 0 );
625
Hanno Beckerac363882019-05-22 16:59:25 +0100626 /* Check if the use of a CID has been negotiated,
627 * but don't ask for the CID value and length.
628 *
629 * Note: Here and below, we're demonstrating the various ways
630 * in which mbedtls_ssl_get_peer_cid() can be called,
631 * depending on whether or not the length/value of the
632 * peer's CID is needed.
633 *
634 * An actual application, however, should use
635 * just one call to mbedtls_ssl_get_peer_cid(). */
Hanno Becker96870292019-05-03 17:30:59 +0100636 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
Hanno Beckerac363882019-05-22 16:59:25 +0100637 NULL, NULL );
Hanno Becker96870292019-05-03 17:30:59 +0100638 if( ret != 0 )
639 {
640 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
641 -ret );
642 return( ret );
643 }
644
645 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
646 {
647 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
648 {
649 mbedtls_printf( "(%s) Use of Connection ID was rejected by the server.\n",
650 additional_description );
651 }
652 }
653 else
654 {
655 size_t idx=0;
656 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
657 additional_description );
Hanno Beckerac363882019-05-22 16:59:25 +0100658
659 /* Ask for just the length of the peer's CID. */
660 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
661 NULL, &peer_cid_len );
662 if( ret != 0 )
663 {
664 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
665 -ret );
666 return( ret );
667 }
668
669 /* Ask for just length + value of the peer's CID. */
670 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
671 peer_cid, &peer_cid_len );
672 if( ret != 0 )
673 {
674 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
675 -ret );
676 return( ret );
677 }
Hanno Becker96870292019-05-03 17:30:59 +0100678 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
679 additional_description,
680 (unsigned) peer_cid_len );
681 while( idx < peer_cid_len )
682 {
683 mbedtls_printf( "%02x ", peer_cid[ idx ] );
684 idx++;
685 }
686 mbedtls_printf( "\n" );
687 }
688
689 return( 0 );
690}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100691#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker96870292019-05-03 17:30:59 +0100692
Paul Bakker9caf2d22010-02-18 19:37:19 +0000693int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000694{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200695 int ret = 0, len, tail_len, i, written, frags, retry_left;
696 mbedtls_net_context server_fd;
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100697
698 unsigned char buf[MAX_REQUEST_SIZE + 1];
699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
701 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200702 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200703#endif
Hanno Becker7a7aa192019-04-09 17:24:19 +0100704
Hanno Beckera5a2b082019-05-15 14:03:01 +0100705#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7a7aa192019-04-09 17:24:19 +0100706 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker96870292019-05-03 17:30:59 +0100707 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker7a7aa192019-04-09 17:24:19 +0100708 size_t cid_len = 0;
Hanno Becker96870292019-05-03 17:30:59 +0100709 size_t cid_renego_len = 0;
Hanno Becker7a7aa192019-04-09 17:24:19 +0100710#endif
711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100713 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200714#endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100715#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +0100716 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100717 const mbedtls_ecp_curve_info *curve_cur;
718#endif
719
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200720 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000721
Gilles Peskineef86ab22017-05-05 18:59:02 +0200722#if defined(MBEDTLS_X509_CRT_PARSE_C)
723 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
724#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_entropy_context entropy;
726 mbedtls_ctr_drbg_context ctr_drbg;
727 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200728 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +0200730 unsigned char *session_data = NULL;
731 size_t session_data_len = 0;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200732#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200733 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200734#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200736 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_x509_crt cacert;
738 mbedtls_x509_crt clicert;
739 mbedtls_pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200740#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000741 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000742 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000743
Paul Bakker1a207ec2011-02-06 13:22:40 +0000744 /*
745 * Make sure memory references are valid.
746 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200747 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200748 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200749 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200751 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752#if defined(MBEDTLS_X509_CRT_PARSE_C)
753 mbedtls_x509_crt_init( &cacert );
754 mbedtls_x509_crt_init( &clicert );
755 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200756#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200758 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200759#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000760
Paul Bakker9caf2d22010-02-18 19:37:19 +0000761 if( argc == 0 )
762 {
763 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000764 if( ret == 0 )
765 ret = 1;
766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000770 while( *list )
771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200773 list++;
774 if( !*list )
775 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000777 list++;
778 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000780 goto exit;
781 }
782
783 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100784 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000785 opt.server_port = DFL_SERVER_PORT;
786 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker7a7aa192019-04-09 17:24:19 +0100787 opt.cid_enabled = DFL_CID_ENABLED;
788 opt.cid_val = DFL_CID_VALUE;
Hanno Becker96870292019-05-03 17:30:59 +0100789 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
790 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100791 opt.nbio = DFL_NBIO;
Hanno Becker16970d22017-10-10 15:56:37 +0100792 opt.event = DFL_EVENT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200793 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200794 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000795 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200796 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000797 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000798 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000799 opt.crt_file = DFL_CRT_FILE;
800 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200801 opt.psk = DFL_PSK;
802 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200803 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200804 opt.ec_max_ops = DFL_EC_MAX_OPS;
Paul Bakker51936882011-02-20 16:05:58 +0000805 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000806 opt.renegotiation = DFL_RENEGOTIATION;
807 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100808 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200809 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000810 opt.min_version = DFL_MIN_VERSION;
811 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100812 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +0200813 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100814 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200815 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200816 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100817 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200818 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200819 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100820 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +0200821 opt.reco_mode = DFL_RECO_MODE;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200822 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200823 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200824 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +0100825 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200826 opt.transport = DFL_TRANSPORT;
827 opt.hs_to_min = DFL_HS_TO_MIN;
828 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200829 opt.dtls_mtu = DFL_DTLS_MTU;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200830 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200831 opt.extended_ms = DFL_EXTENDED_MS;
Jarno Lamsa41b35912019-06-10 15:51:11 +0300832 opt.enforce_extended_master_secret = DFL_EXTENDED_MS_ENFORCE;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100833 opt.etm = DFL_ETM;
Hanno Becker4d615912018-08-14 13:33:30 +0100834 opt.dgram_packing = DFL_DGRAM_PACKING;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000835
836 for( i = 1; i < argc; i++ )
837 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000838 p = argv[i];
839 if( ( q = strchr( p, '=' ) ) == NULL )
840 goto usage;
841 *q++ = '\0';
842
843 if( strcmp( p, "server_name" ) == 0 )
844 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100845 else if( strcmp( p, "server_addr" ) == 0 )
846 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000847 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200848 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100849 else if( strcmp( p, "dtls" ) == 0 )
850 {
851 int t = atoi( q );
852 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100854 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100856 else
857 goto usage;
858 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000859 else if( strcmp( p, "debug_level" ) == 0 )
860 {
861 opt.debug_level = atoi( q );
862 if( opt.debug_level < 0 || opt.debug_level > 65535 )
863 goto usage;
864 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100865 else if( strcmp( p, "nbio" ) == 0 )
866 {
867 opt.nbio = atoi( q );
868 if( opt.nbio < 0 || opt.nbio > 2 )
869 goto usage;
870 }
Hanno Becker16970d22017-10-10 15:56:37 +0100871 else if( strcmp( p, "event" ) == 0 )
872 {
873 opt.event = atoi( q );
874 if( opt.event < 0 || opt.event > 2 )
875 goto usage;
876 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200877 else if( strcmp( p, "read_timeout" ) == 0 )
878 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200879 else if( strcmp( p, "max_resend" ) == 0 )
880 {
881 opt.max_resend = atoi( q );
882 if( opt.max_resend < 0 )
883 goto usage;
884 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000885 else if( strcmp( p, "request_page" ) == 0 )
886 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200887 else if( strcmp( p, "request_size" ) == 0 )
888 {
889 opt.request_size = atoi( q );
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100890 if( opt.request_size < 0 ||
891 opt.request_size > MAX_REQUEST_SIZE )
Paul Bakker93c32b22014-04-25 13:40:05 +0200892 goto usage;
893 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000894 else if( strcmp( p, "ca_file" ) == 0 )
895 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000896 else if( strcmp( p, "ca_path" ) == 0 )
897 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000898 else if( strcmp( p, "crt_file" ) == 0 )
899 opt.crt_file = q;
900 else if( strcmp( p, "key_file" ) == 0 )
901 opt.key_file = q;
Hanno Beckera5a2b082019-05-15 14:03:01 +0100902#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7a7aa192019-04-09 17:24:19 +0100903 else if( strcmp( p, "cid" ) == 0 )
904 {
905 opt.cid_enabled = atoi( q );
906 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
907 goto usage;
908 }
Hanno Becker96870292019-05-03 17:30:59 +0100909 else if( strcmp( p, "cid_renego" ) == 0 )
910 {
911 opt.cid_enabled_renego = atoi( q );
912 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
913 goto usage;
914 }
Hanno Becker7a7aa192019-04-09 17:24:19 +0100915 else if( strcmp( p, "cid_val" ) == 0 )
916 {
917 opt.cid_val = q;
918 }
Hanno Becker96870292019-05-03 17:30:59 +0100919 else if( strcmp( p, "cid_val_renego" ) == 0 )
920 {
921 opt.cid_val_renego = q;
922 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100923#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200924 else if( strcmp( p, "psk" ) == 0 )
925 opt.psk = q;
926 else if( strcmp( p, "psk_identity" ) == 0 )
927 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200928 else if( strcmp( p, "ecjpake_pw" ) == 0 )
929 opt.ecjpake_pw = q;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200930 else if( strcmp( p, "ec_max_ops" ) == 0 )
931 opt.ec_max_ops = atoi( q );
Paul Bakker51936882011-02-20 16:05:58 +0000932 else if( strcmp( p, "force_ciphersuite" ) == 0 )
933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +0000935
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200936 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000937 {
938 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000939 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000940 }
Paul Bakker51936882011-02-20 16:05:58 +0000941 opt.force_ciphersuite[1] = 0;
942 }
Paul Bakker48916f92012-09-16 19:57:18 +0000943 else if( strcmp( p, "renegotiation" ) == 0 )
944 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100945 opt.renegotiation = (atoi( q )) ?
946 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
947 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +0000948 }
949 else if( strcmp( p, "allow_legacy" ) == 0 )
950 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100951 switch( atoi( q ) )
952 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100953 case -1:
954 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
955 break;
956 case 0:
957 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
958 break;
959 case 1:
960 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
961 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100962 default: goto usage;
963 }
Paul Bakker48916f92012-09-16 19:57:18 +0000964 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100965 else if( strcmp( p, "renegotiate" ) == 0 )
966 {
967 opt.renegotiate = atoi( q );
968 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
969 goto usage;
970 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200971 else if( strcmp( p, "exchanges" ) == 0 )
972 {
973 opt.exchanges = atoi( q );
974 if( opt.exchanges < 1 )
975 goto usage;
976 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200977 else if( strcmp( p, "reconnect" ) == 0 )
978 {
979 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200980 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200981 goto usage;
982 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100983 else if( strcmp( p, "reco_delay" ) == 0 )
984 {
985 opt.reco_delay = atoi( q );
986 if( opt.reco_delay < 0 )
987 goto usage;
988 }
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +0200989 else if( strcmp( p, "reco_mode" ) == 0 )
990 {
991 opt.reco_mode = atoi( q );
992 if( opt.reco_mode < 0 )
993 goto usage;
994 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200995 else if( strcmp( p, "reconnect_hard" ) == 0 )
996 {
997 opt.reconnect_hard = atoi( q );
998 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
999 goto usage;
1000 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001001 else if( strcmp( p, "tickets" ) == 0 )
1002 {
1003 opt.tickets = atoi( q );
1004 if( opt.tickets < 0 || opt.tickets > 2 )
1005 goto usage;
1006 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001007 else if( strcmp( p, "alpn" ) == 0 )
1008 {
1009 opt.alpn_string = q;
1010 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001011 else if( strcmp( p, "fallback" ) == 0 )
1012 {
1013 switch( atoi( q ) )
1014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
1016 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001017 default: goto usage;
1018 }
1019 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001020 else if( strcmp( p, "extended_ms" ) == 0 )
1021 {
1022 switch( atoi( q ) )
1023 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001024 case 0:
1025 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1026 break;
1027 case 1:
1028 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1029 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001030 default: goto usage;
1031 }
1032 }
Jarno Lamsa41b35912019-06-10 15:51:11 +03001033 else if( strcmp( p, "enforce_extended_master_secret" ) == 0 )
1034 {
1035 switch( atoi( q ) )
1036 {
1037 case 0:
1038 opt.enforce_extended_master_secret =
1039 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
1040 break;
1041 case 1:
1042 opt.enforce_extended_master_secret =
1043 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_ENABLED;
1044 break;
1045 default: goto usage;
1046 }
1047 }
Hanno Beckere6706e62017-05-15 16:05:15 +01001048 else if( strcmp( p, "curves" ) == 0 )
1049 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001050 else if( strcmp( p, "etm" ) == 0 )
1051 {
1052 switch( atoi( q ) )
1053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1055 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001056 default: goto usage;
1057 }
1058 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001059 else if( strcmp( p, "min_version" ) == 0 )
1060 {
1061 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001063 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001065 else if( strcmp( q, "tls1_1" ) == 0 ||
1066 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001068 else if( strcmp( q, "tls1_2" ) == 0 ||
1069 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001071 else
1072 goto usage;
1073 }
1074 else if( strcmp( p, "max_version" ) == 0 )
1075 {
1076 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001078 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001080 else if( strcmp( q, "tls1_1" ) == 0 ||
1081 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001083 else if( strcmp( q, "tls1_2" ) == 0 ||
1084 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001086 else
1087 goto usage;
1088 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001089 else if( strcmp( p, "arc4" ) == 0 )
1090 {
1091 switch( atoi( q ) )
1092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1094 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001095 default: goto usage;
1096 }
1097 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02001098 else if( strcmp( p, "allow_sha1" ) == 0 )
1099 {
1100 switch( atoi( q ) )
1101 {
1102 case 0: opt.allow_sha1 = 0; break;
1103 case 1: opt.allow_sha1 = 1; break;
1104 default: goto usage;
1105 }
1106 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001107 else if( strcmp( p, "force_version" ) == 0 )
1108 {
1109 if( strcmp( q, "ssl3" ) == 0 )
1110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1112 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001113 }
1114 else if( strcmp( q, "tls1" ) == 0 )
1115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1117 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001118 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001119 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1122 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001123 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001124 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1127 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001128 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001129 else if( strcmp( q, "dtls1" ) == 0 )
1130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1132 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1133 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001134 }
1135 else if( strcmp( q, "dtls1_2" ) == 0 )
1136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1138 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1139 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001140 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001141 else
1142 goto usage;
1143 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001144 else if( strcmp( p, "auth_mode" ) == 0 )
1145 {
1146 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001148 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001150 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001152 else
1153 goto usage;
1154 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001155 else if( strcmp( p, "max_frag_len" ) == 0 )
1156 {
1157 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001159 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001161 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001163 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001165 else
1166 goto usage;
1167 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001168 else if( strcmp( p, "trunc_hmac" ) == 0 )
1169 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001170 switch( atoi( q ) )
1171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1173 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001174 default: goto usage;
1175 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001176 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001177 else if( strcmp( p, "hs_timeout" ) == 0 )
1178 {
1179 if( ( p = strchr( q, '-' ) ) == NULL )
1180 goto usage;
1181 *p++ = '\0';
1182 opt.hs_to_min = atoi( q );
1183 opt.hs_to_max = atoi( p );
1184 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1185 goto usage;
1186 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001187 else if( strcmp( p, "mtu" ) == 0 )
1188 {
1189 opt.dtls_mtu = atoi( q );
1190 if( opt.dtls_mtu < 0 )
1191 goto usage;
1192 }
Hanno Becker4d615912018-08-14 13:33:30 +01001193 else if( strcmp( p, "dgram_packing" ) == 0 )
1194 {
1195 opt.dgram_packing = atoi( q );
1196 if( opt.dgram_packing != 0 &&
1197 opt.dgram_packing != 1 )
1198 {
1199 goto usage;
1200 }
1201 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001202 else if( strcmp( p, "recsplit" ) == 0 )
1203 {
1204 opt.recsplit = atoi( q );
1205 if( opt.recsplit < 0 || opt.recsplit > 1 )
1206 goto usage;
1207 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001208 else if( strcmp( p, "dhmlen" ) == 0 )
1209 {
1210 opt.dhmlen = atoi( q );
1211 if( opt.dhmlen < 0 )
1212 goto usage;
1213 }
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +01001214 else if( strcmp( p, "query_config" ) == 0 )
1215 {
1216 return query_config( q );
1217 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001218 else
1219 goto usage;
1220 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001221
Hanno Becker16970d22017-10-10 15:56:37 +01001222 /* Event-driven IO is incompatible with the above custom
1223 * receive and send functions, as the polling builds on
1224 * refers to the underlying net_context. */
1225 if( opt.event == 1 && opt.nbio != 1 )
1226 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001227 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001228 opt.nbio = 1;
1229 }
1230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231#if defined(MBEDTLS_DEBUG_C)
1232 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001233#endif
1234
Paul Bakkerc1516be2013-06-29 16:01:32 +02001235 if( opt.force_ciphersuite[0] > 0 )
1236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001238 ciphersuite_info =
1239 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001240
Paul Bakker66c48102013-07-26 14:05:32 +02001241 if( opt.max_version != -1 &&
1242 ciphersuite_info->min_minor_ver > opt.max_version )
1243 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001244 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker66c48102013-07-26 14:05:32 +02001245 ret = 2;
1246 goto usage;
1247 }
1248 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001249 ciphersuite_info->max_minor_ver < opt.min_version )
1250 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001251 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001252 ret = 2;
1253 goto usage;
1254 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001255
1256 /* If the server selects a version that's not supported by
1257 * this suite, then there will be no common ciphersuite... */
1258 if( opt.max_version == -1 ||
1259 opt.max_version > ciphersuite_info->max_minor_ver )
1260 {
Paul Bakker66c48102013-07-26 14:05:32 +02001261 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001262 }
Paul Bakker66c48102013-07-26 14:05:32 +02001263 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001264 {
Paul Bakker66c48102013-07-26 14:05:32 +02001265 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001266 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1268 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1269 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001270 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001271
1272 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001276 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001277 mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001278 ret = 2;
1279 goto usage;
1280 }
1281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001283 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001284 }
1285
Hanno Beckera5a2b082019-05-15 14:03:01 +01001286#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01001287 cid_len = strlen( opt.cid_val ) / 2;
1288 if( cid_len > sizeof( cid ) )
1289 {
1290 mbedtls_printf( "CID too long\n" );
1291 goto exit;
1292 }
Hanno Becker7a7aa192019-04-09 17:24:19 +01001293
Hanno Becker96870292019-05-03 17:30:59 +01001294 if( unhexify( opt.cid_val, cid ) != 0 )
1295 {
1296 mbedtls_printf( "CID not valid hex\n" );
1297 goto exit;
1298 }
1299
1300 /* Keep CID settings for renegotiation unless
1301 * specified otherwise. */
1302 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
1303 opt.cid_enabled_renego = opt.cid_enabled;
1304 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
1305 opt.cid_val_renego = opt.cid_val;
1306
1307 cid_renego_len = strlen( opt.cid_val_renego ) / 2;
1308 if( cid_renego_len > sizeof( cid_renego ) )
1309 {
1310 mbedtls_printf( "CID too long\n" );
1311 goto exit;
1312 }
1313
1314 if( unhexify( opt.cid_val_renego, cid_renego ) != 0 )
1315 {
1316 mbedtls_printf( "CID not valid hex\n" );
1317 goto exit;
1318 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001319#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +01001320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001323 * Unhexify the pre-shared key if any is given
1324 */
1325 if( strlen( opt.psk ) )
1326 {
Hanno Beckerec370302019-04-09 17:12:56 +01001327 psk_len = strlen( opt.psk ) / 2;
1328 if( psk_len > sizeof( psk ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001329 {
Hanno Beckerec370302019-04-09 17:12:56 +01001330 mbedtls_printf( "pre-shared key too long\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001331 goto exit;
1332 }
1333
Hanno Beckerec370302019-04-09 17:12:56 +01001334 if( unhexify( opt.psk, psk ) != 0 )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001335 {
Hanno Beckerec370302019-04-09 17:12:56 +01001336 mbedtls_printf( "pre-shared key not valid hex\n" );
1337 goto exit;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001338 }
1339 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001341
Hanno Beckere6706e62017-05-15 16:05:15 +01001342#if defined(MBEDTLS_ECP_C)
1343 if( opt.curves != NULL )
1344 {
1345 p = (char *) opt.curves;
1346 i = 0;
1347
1348 if( strcmp( p, "none" ) == 0 )
1349 {
1350 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1351 }
1352 else if( strcmp( p, "default" ) != 0 )
1353 {
1354 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001355 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001356 {
1357 q = p;
1358
1359 /* Terminate the current string */
1360 while( *p != ',' && *p != '\0' )
1361 p++;
1362 if( *p == ',' )
1363 *p++ = '\0';
1364
1365 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1366 {
1367 curve_list[i++] = curve_cur->grp_id;
1368 }
1369 else
1370 {
1371 mbedtls_printf( "unknown curve %s\n", q );
1372 mbedtls_printf( "supported curves: " );
1373 for( curve_cur = mbedtls_ecp_curve_list();
1374 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1375 curve_cur++ )
1376 {
1377 mbedtls_printf( "%s ", curve_cur->name );
1378 }
1379 mbedtls_printf( "\n" );
1380 goto exit;
1381 }
1382 }
1383
1384 mbedtls_printf("Number of curves: %d\n", i );
1385
Hanno Becker8651a432017-06-09 16:13:22 +01001386 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001387 {
Hanno Becker8651a432017-06-09 16:13:22 +01001388 mbedtls_printf( "curves list too long, maximum %d",
1389 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001390 goto exit;
1391 }
1392
1393 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1394 }
1395 }
1396#endif /* MBEDTLS_ECP_C */
1397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001399 if( opt.alpn_string != NULL )
1400 {
1401 p = (char *) opt.alpn_string;
1402 i = 0;
1403
1404 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001405 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001406 {
1407 alpn_list[i++] = p;
1408
1409 /* Terminate the current string and move on to next one */
1410 while( *p != ',' && *p != '\0' )
1411 p++;
1412 if( *p == ',' )
1413 *p++ = '\0';
1414 }
1415 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001417
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001418 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 * 0. Initialize the RNG and the session data
1420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001422 fflush( stdout );
1423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 mbedtls_entropy_init( &entropy );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001425 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1426 &entropy, (const unsigned char *) pers,
1427 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +00001428 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001429 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
1430 -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001431 goto exit;
1432 }
1433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 /*
1438 * 1.1. Load the trusted CA
1439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 fflush( stdout );
1442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001444 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001445 if( strcmp( opt.ca_path, "none" ) == 0 )
1446 ret = 0;
1447 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001449 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001450 if( strcmp( opt.ca_file, "none" ) == 0 )
1451 ret = 0;
1452 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001454 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001455#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456#if defined(MBEDTLS_CERTS_C)
1457 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 ret = mbedtls_x509_crt_parse( &cacert,
1460 (const unsigned char *) mbedtls_test_cas[i],
1461 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001462 if( ret != 0 )
1463 break;
1464 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001465#else
1466 {
1467 ret = 1;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001468 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001469 }
1470#endif
Paul Bakker42488232012-05-16 08:21:05 +00001471 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001472 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001473 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1474 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001475 goto exit;
1476 }
1477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001479
1480 /*
1481 * 1.2. Load own certificate and private key
1482 *
1483 * (can be skipped if client authentication is not required)
1484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001486 fflush( stdout );
1487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001489 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001490 if( strcmp( opt.crt_file, "none" ) == 0 )
1491 ret = 0;
1492 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001494 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001495#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001497 ret = mbedtls_x509_crt_parse( &clicert,
1498 (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001500#else
1501 {
1502 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001504 }
1505#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 if( ret != 0 )
1507 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001508 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1509 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001510 goto exit;
1511 }
1512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001514 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001515 if( strcmp( opt.key_file, "none" ) == 0 )
1516 ret = 0;
1517 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001519 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001520#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001522 ret = mbedtls_pk_parse_key( &pkey,
1523 (const unsigned char *) mbedtls_test_cli_key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001525#else
1526 {
1527 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001529 }
1530#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001531 if( ret != 0 )
1532 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001533 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
1534 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 goto exit;
1536 }
1537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 mbedtls_printf( " ok\n" );
1539#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001540
1541 /*
1542 * 2. Start the connection
1543 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001544 if( opt.server_addr == NULL)
1545 opt.server_addr = opt.server_name;
1546
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001547 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001549 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001550 fflush( stdout );
1551
Hanno Becker16970d22017-10-10 15:56:37 +01001552 if( ( ret = mbedtls_net_connect( &server_fd,
1553 opt.server_addr, opt.server_port,
1554 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1555 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001556 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001557 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
1558 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001559 goto exit;
1560 }
1561
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001562 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001563 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001564 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001565 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001566 if( ret != 0 )
1567 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001568 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
1569 -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001570 goto exit;
1571 }
1572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001574
1575 /*
1576 * 3. Setup stuff
1577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001579 fflush( stdout );
1580
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001581 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1582 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001583 opt.transport,
1584 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001585 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001586 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
1587 -ret );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001588 goto exit;
1589 }
1590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001592 /* The default algorithms profile disables SHA-1, but our tests still
1593 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001594 if( opt.allow_sha1 > 0 )
1595 {
1596 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1597 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1598 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1599 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001600
Paul Bakker915275b2012-09-28 07:10:55 +00001601 if( opt.debug_level > 0 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001602 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
Gilles Peskineef86ab22017-05-05 18:59:02 +02001603#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001604
Hanno Beckera5a2b082019-05-15 14:03:01 +01001605#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01001606 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckereec2be92019-05-03 13:06:44 +01001607 {
Hanno Becker96870292019-05-03 17:30:59 +01001608 if( opt.cid_enabled == 1 &&
1609 opt.cid_enabled_renego == 1 &&
1610 cid_len != cid_renego_len )
1611 {
1612 mbedtls_printf( "CID length must not change during renegotiation\n" );
1613 goto usage;
1614 }
1615
Hanno Becker96870292019-05-03 17:30:59 +01001616 if( opt.cid_enabled == 1 )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01001617 ret = mbedtls_ssl_conf_cid( &conf, cid_len,
1618 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Becker96870292019-05-03 17:30:59 +01001619 else
Hanno Beckere8eff9a2019-05-14 11:30:10 +01001620 ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
1621 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Becker96870292019-05-03 17:30:59 +01001622
Hanno Beckereec2be92019-05-03 13:06:44 +01001623 if( ret != 0 )
1624 {
Hanno Becker76581052019-05-23 16:58:22 +01001625 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
1626 -ret );
Hanno Beckereec2be92019-05-03 13:06:44 +01001627 goto exit;
1628 }
1629 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001630#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckereec2be92019-05-03 13:06:44 +01001631
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001632 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001633 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001636 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Hanno Becker16970d22017-10-10 15:56:37 +01001637 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min,
1638 opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001639
Hanno Becker4d615912018-08-14 13:33:30 +01001640 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01001641 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001645 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001646 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001647 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n",
1648 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001649 goto exit;
1650 }
Paul Bakker05decb22013-08-15 13:33:48 +02001651#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001654 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001655 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001656#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001659 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001660 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Jarno Lamsa41b35912019-06-10 15:51:11 +03001661 if( opt.enforce_extended_master_secret != DFL_EXTENDED_MS_ENFORCE )
1662 mbedtls_ssl_conf_extended_master_secret_enforce( &conf,
1663 opt.enforce_extended_master_secret );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001664#endif
1665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001667 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001668 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001669#endif
1670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001672 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001673 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Hanno Becker16970d22017-10-10 15:56:37 +01001674 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1675 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001676#endif
1677
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001678#if defined(MBEDTLS_DHM_C)
1679 if( opt.dhmlen != DFL_DHMLEN )
1680 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1681#endif
1682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001684 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001685 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001686 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001687 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
1688 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001689 goto exit;
1690 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001691#endif
1692
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001693 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1694 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001695
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001696 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02001699 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02001700#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001701
Paul Bakker645ce3a2012-10-31 12:32:41 +00001702 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001703 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001704
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001705#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001706 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001707 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001708#endif
Paul Bakker51936882011-02-20 16:05:58 +00001709
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001710 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001711 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001713 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001714#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001717 if( strcmp( opt.ca_path, "none" ) != 0 &&
1718 strcmp( opt.ca_file, "none" ) != 0 )
1719 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001720 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001721 }
1722 if( strcmp( opt.crt_file, "none" ) != 0 &&
1723 strcmp( opt.key_file, "none" ) != 0 )
1724 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001725 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001726 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001727 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
1728 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001729 goto exit;
1730 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001731 }
Paul Bakkered27a042013-04-18 22:46:23 +02001732#endif
1733
Hanno Beckere6706e62017-05-15 16:05:15 +01001734#if defined(MBEDTLS_ECP_C)
1735 if( opt.curves != NULL &&
1736 strcmp( opt.curves, "default" ) != 0 )
1737 {
1738 mbedtls_ssl_conf_curves( &conf, curve_list );
1739 }
1740#endif
1741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001743 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001744 (const unsigned char *) opt.psk_identity,
1745 strlen( opt.psk_identity ) ) ) != 0 )
1746 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001747 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n",
1748 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001749 goto exit;
1750 }
Paul Bakkered27a042013-04-18 22:46:23 +02001751#endif
1752
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001753 if( opt.min_version != DFL_MIN_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01001754 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1755 opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001756
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001757 if( opt.max_version != DFL_MAX_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01001758 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1759 opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001762 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001763 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001764#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001766 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
1767 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001768 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
1769 -ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001770 goto exit;
1771 }
1772
1773#if defined(MBEDTLS_X509_CRT_PARSE_C)
1774 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1775 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001776 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
1777 ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001778 goto exit;
1779 }
1780#endif
1781
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001782#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1783 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
1784 {
1785 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
1786 (const unsigned char *) opt.ecjpake_pw,
1787 strlen( opt.ecjpake_pw ) ) ) != 0 )
1788 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001789 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
1790 ret );
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001791 goto exit;
1792 }
1793 }
1794#endif
1795
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001796 if( opt.nbio == 2 )
1797 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
1798 else
Hanno Becker16970d22017-10-10 15:56:37 +01001799 mbedtls_ssl_set_bio( &ssl, &server_fd,
1800 mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02001801 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001802
Hanno Beckera5a2b082019-05-15 14:03:01 +01001803#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7a7aa192019-04-09 17:24:19 +01001804 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1805 {
1806 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
1807 cid, cid_len ) ) != 0 )
1808 {
1809 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
1810 ret );
1811 goto exit;
1812 }
1813 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001814#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +01001815
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001816#if defined(MBEDTLS_SSL_PROTO_DTLS)
1817 if( opt.dtls_mtu != DFL_DTLS_MTU )
1818 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
1819#endif
1820
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001821#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001822 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
1823 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001824#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001825
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001826#if defined(MBEDTLS_ECP_RESTARTABLE)
1827 if( opt.ec_max_ops != DFL_EC_MAX_OPS )
1828 mbedtls_ecp_set_max_ops( opt.ec_max_ops );
1829#endif
1830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001832
Paul Bakker5121ce52009-01-03 21:22:43 +00001833 /*
1834 * 4. Handshake
1835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 fflush( stdout );
1838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001840 {
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001841 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1842 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001843 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Paul Bakker40e46942009-01-03 21:51:57 +00001844 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001845 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
1846 -ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
1848 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001849 " Unable to verify the server's certificate. "
1850 "Either it is invalid,\n"
1851 " or you didn't set ca_file or ca_path "
1852 "to an appropriate value.\n"
1853 " Alternatively, you may want to use "
1854 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001856 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00001857 }
Hanno Becker16970d22017-10-10 15:56:37 +01001858
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001859#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02001860 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
1861 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001862#endif
1863
Hanno Becker16970d22017-10-10 15:56:37 +01001864 /* For event-driven IO, wait for socket to become available */
1865 if( opt.event == 1 /* level triggered IO */ )
1866 {
1867#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001868 ret = idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001869#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001870 ret = idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001871#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001872 if( ret != 0 )
1873 goto exit;
Hanno Becker16970d22017-10-10 15:56:37 +01001874 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 }
1876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Hanno Becker16970d22017-10-10 15:56:37 +01001878 mbedtls_ssl_get_version( &ssl ),
1879 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
1882 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001883 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001885
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02001886#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1887 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
1888 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
1889#endif
1890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001892 if( opt.alpn_string != NULL )
1893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
1895 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001896 alp ? alp : "(none)" );
1897 }
1898#endif
1899
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001900 if( opt.reconnect != 0 )
1901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001903 fflush( stdout );
1904
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02001905 if( opt.reco_mode == 1 )
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02001906 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02001907 /* free any previously saved data */
Manuel Pégourié-Gonnard4bb1b992019-05-24 10:26:41 +02001908 if( session_data != NULL )
1909 {
1910 mbedtls_platform_zeroize( session_data, session_data_len );
1911 mbedtls_free( session_data );
1912 session_data = NULL;
1913 }
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02001914
1915 /* get size of the buffer needed */
1916 mbedtls_ssl_session_save( mbedtls_ssl_get_session_pointer( &ssl ),
1917 NULL, 0, &session_data_len );
1918 session_data = mbedtls_calloc( 1, session_data_len );
1919 if( session_data == NULL )
1920 {
1921 mbedtls_printf( " failed\n ! alloc %u bytes for session data\n",
1922 (unsigned) session_data_len );
1923 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1924 goto exit;
1925 }
1926
1927 /* actually save session data */
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02001928 if( ( ret = mbedtls_ssl_session_save( mbedtls_ssl_get_session_pointer( &ssl ),
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02001929 session_data, session_data_len,
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02001930 &session_data_len ) ) != 0 )
1931 {
1932 mbedtls_printf( " failed\n ! mbedtls_ssl_session_saved returned -0x%04x\n\n",
1933 -ret );
1934 goto exit;
1935 }
1936 }
1937 else
1938 {
1939 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
1940 {
1941 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
1942 -ret );
1943 goto exit;
1944 }
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02001945 }
1946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02001948
1949 if( opt.reco_mode == 1 )
1950 {
1951 mbedtls_printf( " [ Saved %u bytes of session data]\n",
1952 (unsigned) session_data_len );
1953 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001954 }
1955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 /*
1958 * 5. Verify the server certificate
1959 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001961
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001962 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001963 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001964 char vrfy_buf[512];
1965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001967
Hanno Becker16970d22017-10-10 15:56:37 +01001968 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ),
1969 " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00001970
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001971 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 }
1973 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 mbedtls_printf( " . Peer certificate information ...\n" );
1979 mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1980 mbedtls_ssl_get_peer_cert( &ssl ) );
1981 mbedtls_printf( "%s\n", buf );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001982 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001984
Hanno Beckera5a2b082019-05-15 14:03:01 +01001985#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01001986 ret = report_cid_usage( &ssl, "initial handshake" );
1987 if( ret != 0 )
1988 goto exit;
1989
Hanno Becker7a7aa192019-04-09 17:24:19 +01001990 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1991 {
Hanno Becker96870292019-05-03 17:30:59 +01001992 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
1993 cid_renego,
1994 cid_renego_len ) ) != 0 )
Hanno Becker7a7aa192019-04-09 17:24:19 +01001995 {
Hanno Becker96870292019-05-03 17:30:59 +01001996 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
1997 ret );
1998 return( ret );
Hanno Becker7a7aa192019-04-09 17:24:19 +01001999 }
2000 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002001#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +01002002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002004 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002005 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002006 /*
2007 * Perform renegotiation (this must be done when the server is waiting
2008 * for input from our side).
2009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002011 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002013 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002014 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002015 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002016 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002017 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002018 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
2019 ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002020 goto exit;
2021 }
Hanno Becker16970d22017-10-10 15:56:37 +01002022
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002023#if defined(MBEDTLS_ECP_RESTARTABLE)
2024 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2025 continue;
2026#endif
2027
Hanno Becker16970d22017-10-10 15:56:37 +01002028 /* For event-driven IO, wait for socket to become available */
2029 if( opt.event == 1 /* level triggered IO */ )
2030 {
2031#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002032 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002033#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002034 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002035#endif
2036 }
2037
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002038 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002040 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002042
Hanno Beckera5a2b082019-05-15 14:03:01 +01002043#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01002044 ret = report_cid_usage( &ssl, "after renegotiation" );
2045 if( ret != 0 )
2046 goto exit;
Hanno Beckera5a2b082019-05-15 14:03:01 +01002047#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker96870292019-05-03 17:30:59 +01002048
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 /*
2050 * 6. Write the GET request
2051 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002052 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002053send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 fflush( stdout );
2056
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002057 len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST,
2058 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002059 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002060
2061 /* Add padding to GET request to reach opt.request_size in length */
2062 if( opt.request_size != DFL_REQUEST_SIZE &&
2063 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02002064 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002065 memset( buf + len, 'A', opt.request_size - len - tail_len );
2066 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02002067 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002068
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002069 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002070 len += tail_len;
2071
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02002072 /* Truncate if request size is smaller than the "natural" size */
2073 if( opt.request_size != DFL_REQUEST_SIZE &&
2074 len > opt.request_size )
2075 {
2076 len = opt.request_size;
2077
2078 /* Still end with \r\n unless that's really not possible */
2079 if( len >= 2 ) buf[len - 2] = '\r';
2080 if( len >= 1 ) buf[len - 1] = '\n';
2081 }
2082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 {
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002085 written = 0;
2086 frags = 0;
2087
2088 do
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 {
Hanno Becker16970d22017-10-10 15:56:37 +01002090 while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002091 len - written ) ) < 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002092 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002093 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002094 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002095 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002096 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002097 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
2098 -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002099 goto exit;
2100 }
Hanno Becker16970d22017-10-10 15:56:37 +01002101
2102 /* For event-driven IO, wait for socket to become available */
2103 if( opt.event == 1 /* level triggered IO */ )
2104 {
2105#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002106 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002107#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002108 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002109#endif
2110 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002111 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002112
2113 frags++;
2114 written += ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002116 while( written < len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002118 else /* Not stream, so datagram */
2119 {
Hanno Becker16970d22017-10-10 15:56:37 +01002120 while( 1 )
2121 {
2122 ret = mbedtls_ssl_write( &ssl, buf, len );
2123
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002124#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002125 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002126 continue;
2127#endif
2128
Hanno Becker16970d22017-10-10 15:56:37 +01002129 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2130 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2131 break;
2132
2133 /* For event-driven IO, wait for socket to become available */
2134 if( opt.event == 1 /* level triggered IO */ )
2135 {
2136#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002137 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002138#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002139 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002140#endif
2141 }
2142 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002143
2144 if( ret < 0 )
2145 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002146 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n",
2147 ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002148 goto exit;
2149 }
2150
2151 frags = 1;
2152 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002153
2154 if( written < len )
2155 {
2156 mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
2157 "was truncated to size %u", (unsigned) written );
2158 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002159 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002160
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002161 buf[written] = '\0';
Hanno Becker16970d22017-10-10 15:56:37 +01002162 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
2163 written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002164
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002165 /* Send a non-empty request if request_size == 0 */
2166 if ( len == 0 )
2167 {
2168 opt.request_size = DFL_REQUEST_SIZE;
2169 goto send_request;
2170 }
2171
Paul Bakker5121ce52009-01-03 21:22:43 +00002172 /*
2173 * 7. Read the HTTP response
2174 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002176 fflush( stdout );
2177
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002178 /*
2179 * TLS and DTLS need different reading styles (stream vs datagram)
2180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002182 {
2183 do
2184 {
2185 len = sizeof( buf ) - 1;
2186 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002188
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002189#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002190 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002191 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002192#endif
2193
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002194 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
2195 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Hanno Becker16970d22017-10-10 15:56:37 +01002196 {
2197 /* For event-driven IO, wait for socket to become available */
2198 if( opt.event == 1 /* level triggered IO */ )
2199 {
2200#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002201 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002202#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002203 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002204#endif
2205 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002206 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01002207 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002208
2209 if( ret <= 0 )
2210 {
2211 switch( ret )
2212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2214 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002215 ret = 0;
2216 goto close_notify;
2217
2218 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 case MBEDTLS_ERR_NET_CONN_RESET:
2220 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002221 ret = 0;
2222 goto reconnect;
2223
2224 default:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002225 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n",
2226 -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002227 goto exit;
2228 }
2229 }
2230
2231 len = ret;
2232 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002234
2235 /* End of message should be detected according to the syntax of the
2236 * application protocol (eg HTTP), just use a dummy test here. */
2237 if( ret > 0 && buf[len-1] == '\n' )
2238 {
2239 ret = 0;
2240 break;
2241 }
2242 }
2243 while( 1 );
2244 }
2245 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00002246 {
2247 len = sizeof( buf ) - 1;
2248 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002249
Hanno Becker16970d22017-10-10 15:56:37 +01002250 while( 1 )
2251 {
2252 ret = mbedtls_ssl_read( &ssl, buf, len );
2253
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002254#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002255 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002256 continue;
2257#endif
2258
Hanno Becker16970d22017-10-10 15:56:37 +01002259 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2260 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2261 break;
2262
2263 /* For event-driven IO, wait for socket to become available */
2264 if( opt.event == 1 /* level triggered IO */ )
2265 {
2266#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002267 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002268#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002269 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002270#endif
2271 }
2272 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002273
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002274 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002275 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002276 switch( ret )
2277 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002278 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002280 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02002281 goto send_request;
2282 goto exit;
2283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2285 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002286 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002287 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00002288
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002289 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002291 goto exit;
2292 }
Paul Bakker5690efc2011-05-26 13:16:06 +00002293 }
2294
Paul Bakker5121ce52009-01-03 21:22:43 +00002295 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02002296 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002298 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002299 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002300
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002301 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002302 * 7b. Simulate hard reset and reconnect from same port?
2303 */
2304 if( opt.reconnect_hard != 0 )
2305 {
2306 opt.reconnect_hard = 0;
2307
2308 mbedtls_printf( " . Restarting connection from same port..." );
2309 fflush( stdout );
2310
2311 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
2312 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002313 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2314 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002315 goto exit;
2316 }
2317
2318 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
2319 {
2320 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002321 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002322 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002323 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002324 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2325 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002326 goto exit;
2327 }
Hanno Becker16970d22017-10-10 15:56:37 +01002328
2329 /* For event-driven IO, wait for socket to become available */
2330 if( opt.event == 1 /* level triggered IO */ )
2331 {
2332#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002333 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002334#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002335 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002336#endif
2337 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002338 }
2339
2340 mbedtls_printf( " ok\n" );
2341
2342 goto send_request;
2343 }
2344
2345 /*
2346 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002347 */
2348 if( --opt.exchanges > 0 )
2349 goto send_request;
2350
2351 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002352 * 8. Done, cleanly close the connection
2353 */
2354close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002356 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002357
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002358 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002360 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002361 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002364
2365 /*
2366 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002367 */
2368reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002369 if( opt.reconnect != 0 )
2370 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02002371 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002372
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002373 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002374
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002375#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002376 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02002377 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002378#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002383 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002384 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2385 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002386 goto exit;
2387 }
2388
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02002389 if( opt.reco_mode == 1 )
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02002390 {
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02002391 if( ( ret = mbedtls_ssl_session_load( &saved_session,
2392 session_data,
2393 session_data_len ) ) != 0 )
2394 {
2395 mbedtls_printf( " failed\n ! mbedtls_ssl_session_load returned -0x%x\n\n",
2396 -ret );
2397 goto exit;
2398 }
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02002399 }
2400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002402 {
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02002403 mbedtls_printf( " failed\n ! mbedtls_ssl_set_session returned -0x%x\n\n",
2404 -ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002405 goto exit;
2406 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002407
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002408 if( ( ret = mbedtls_net_connect( &server_fd,
2409 opt.server_addr, opt.server_port,
2410 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2411 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002412 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002413 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
2414 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002415 goto exit;
2416 }
2417
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002418 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002419 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002420 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002421 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002422 if( ret != 0 )
2423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002425 -ret );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002426 goto exit;
2427 }
2428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002430 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002431 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002432 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002433 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002434 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002435 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2436 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002437 goto exit;
2438 }
2439 }
2440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002442
2443 goto send_request;
2444 }
2445
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002446 /*
2447 * Cleanup and exit
2448 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002449exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00002451 if( ret != 0 )
2452 {
2453 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 mbedtls_strerror( ret, error_buf, 100 );
2455 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002456 }
2457#endif
2458
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002459 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461#if defined(MBEDTLS_X509_CRT_PARSE_C)
2462 mbedtls_x509_crt_free( &clicert );
2463 mbedtls_x509_crt_free( &cacert );
2464 mbedtls_pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02002465#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 mbedtls_ssl_session_free( &saved_session );
2467 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002468 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 mbedtls_ctr_drbg_free( &ctr_drbg );
2470 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnard4bb1b992019-05-24 10:26:41 +02002471 if( session_data != NULL )
2472 mbedtls_platform_zeroize( session_data, session_data_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02002473 mbedtls_free( session_data );
Paul Bakker5121ce52009-01-03 21:22:43 +00002474
Paul Bakkercce9d772011-11-18 14:26:47 +00002475#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002477 fflush( stdout ); getchar();
2478#endif
2479
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002480 // Shell can not handle large exit numbers -> 1 for errors
2481 if( ret < 0 )
2482 ret = 1;
2483
Paul Bakker5121ce52009-01-03 21:22:43 +00002484 return( ret );
2485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
2487 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002488 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */