blob: 81f3f750dc7c6681fbe191256ede2297ba95569b [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 Lamsa0ea3cfe2019-05-29 13:33:32 +0300125#define DFL_SERIALIZE 0
Jarno Lamsa41b35912019-06-10 15:51:11 +0300126#define DFL_EXTENDED_MS_ENFORCE -1
Paul Bakker0e6975b2009-02-10 22:19:10 +0000127
Paul Bakker93c32b22014-04-25 13:40:05 +0200128#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
129#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_X509_CRT_PARSE_C)
132#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000133#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100134 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
135 " default: \"\" (pre-loaded)\n" \
136 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
137 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
138 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
139 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000140 " key_file=%%s default: \"\" (pre-loaded)\n"
141#else
142#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 " No file operations available (MBEDTLS_FS_IO not defined)\n"
144#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200145#else
146#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200148
Hanno Beckera5a2b082019-05-15 14:03:01 +0100149#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7a7aa192019-04-09 17:24:19 +0100150#define USAGE_CID \
151 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
152 " default: 0 (disabled)\n" \
Hanno Becker96870292019-05-03 17:30:59 +0100153 " 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 +0100154 " default: same as 'cid' parameter\n" \
Hanno Becker7a7aa192019-04-09 17:24:19 +0100155 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
Hanno Becker96870292019-05-03 17:30:59 +0100156 " default: \"\"\n" \
157 " 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 +0100158 " default: same as 'cid_val' parameter\n"
Hanno Beckera5a2b082019-05-15 14:03:01 +0100159#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100160#define USAGE_CID ""
Hanno Beckera5a2b082019-05-15 14:03:01 +0100161#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200164#define USAGE_PSK \
165 " psk=%%s default: \"\" (in hex, without 0x)\n" \
166 " psk_identity=%%s default: \"Client_identity\"\n"
167#else
168#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200172#define USAGE_TICKETS \
173 " tickets=%%d default: 1 (enabled)\n"
174#else
175#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200179#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100180 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200181#else
182#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200186#define USAGE_MAX_FRAG_LEN \
187 " max_frag_len=%%d default: 16384 (tls default)\n" \
188 " options: 512, 1024, 2048, 4096\n"
189#else
190#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100194#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200195 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100196#else
197#define USAGE_RECSPLIT
198#endif
199
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200200#if defined(MBEDTLS_DHM_C)
201#define USAGE_DHMLEN \
202 " dhmlen=%%d default: (library default: 1024 bits)\n"
203#else
204#define USAGE_DHMLEN
205#endif
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200208#define USAGE_ALPN \
209 " alpn=%%s default: \"\" (disabled)\n" \
210 " example: spdy/1,http/1.1\n"
211#else
212#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200214
Hanno Beckere6706e62017-05-15 16:05:15 +0100215#if defined(MBEDTLS_ECP_C)
216#define USAGE_CURVES \
217 " curves=a,b,c,d default: \"default\" (library default)\n" \
218 " example: \"secp521r1,brainpoolP512r1\"\n" \
219 " - use \"none\" for empty list\n" \
220 " - see mbedtls_ecp_curve_list()\n" \
221 " for acceptable curve names\n"
222#else
223#define USAGE_CURVES ""
224#endif
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200227#define USAGE_DTLS \
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +0100228 " dtls=%%d default: 0 (TLS) (if both enabled)\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200229 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200230 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Becker4d615912018-08-14 13:33:30 +0100231 " mtu=%%d default: (library default: unlimited)\n" \
232 " dgram_packing=%%d default: 1 (allowed)\n" \
233 " allow or forbid packing of multiple\n" \
234 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200235#else
236#define USAGE_DTLS ""
237#endif
238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200240#define USAGE_FALLBACK \
241 " fallback=0/1 default: (library default: off)\n"
242#else
243#define USAGE_FALLBACK ""
244#endif
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200247#define USAGE_EMS \
Jarno Lamsa41b35912019-06-10 15:51:11 +0300248 " extended_ms=0/1 default: (library default: on)\n" \
249 " enforce_extended_master_secret=0/1 default: (library default: off)\n"
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200250#else
251#define USAGE_EMS ""
252#endif
253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100255#define USAGE_ETM \
256 " etm=0/1 default: (library default: on)\n"
257#else
258#define USAGE_ETM ""
259#endif
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100262#define USAGE_RENEGO \
263 " renegotiation=%%d default: 0 (disabled)\n" \
264 " renegotiate=%%d default: 0 (disabled)\n"
265#else
266#define USAGE_RENEGO ""
267#endif
268
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200269#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
270#define USAGE_ECJPAKE \
271 " ecjpake_pw=%%s default: none (disabled)\n"
272#else
273#define USAGE_ECJPAKE ""
274#endif
275
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200276#if defined(MBEDTLS_ECP_RESTARTABLE)
277#define USAGE_ECRESTART \
278 " ec_max_ops=%%s default: library default (restart disabled)\n"
279#else
280#define USAGE_ECRESTART ""
281#endif
282
Paul Bakker9caf2d22010-02-18 19:37:19 +0000283#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000284 "\n usage: ssl_client2 param=<>...\n" \
285 "\n acceptable parameters:\n" \
286 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100287 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000288 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000289 " request_page=%%s default: \".\"\n" \
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +0100290 " request_size=%%d default: about 34 (basic request)\n" \
291 " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
292 " If 0, in the first exchange only an empty\n" \
293 " application data message is sent followed by\n" \
294 " a second non-empty message before attempting\n" \
295 " to read a response from the server\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100296 " debug_level=%%d default: 0 (disabled)\n" \
297 " nbio=%%d default: 0 (blocking I/O)\n" \
298 " options: 1 (non-blocking), 2 (added delays)\n" \
299 " event=%%d default: 0 (loop)\n" \
300 " options: 1 (level-triggered, implies nbio=1),\n" \
301 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200302 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100303 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200304 USAGE_DTLS \
Hanno Becker7a7aa192019-04-09 17:24:19 +0100305 USAGE_CID \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200306 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100307 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100308 " options: none, optional, required\n" \
309 USAGE_IO \
310 "\n" \
311 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200312 USAGE_ECJPAKE \
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200313 USAGE_ECRESTART \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100314 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100315 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100316 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200317 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +0200318 " reconnect=%%d number of reconnections using session resumption\n" \
319 " default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200320 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +0200321 " reco_mode=%%d 0: copy session, 1: serialize session\n" \
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +0200322 " default: 1\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200323 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200324 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100325 USAGE_MAX_FRAG_LEN \
326 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200327 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200328 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200329 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100330 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100331 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100332 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200333 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000334 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100335 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200336 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200337 " min_version=%%s default: (library default: tls1)\n" \
338 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000339 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100340 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000341 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000342 " force_ciphersuite=<name> default: all enabled\n"\
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100343 " query_config=<name> return 0 if the specified\n" \
344 " configuration macro is defined and 1\n" \
345 " otherwise. The expansion of the macro\n" \
346 " is printed if it is defined\n" \
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +0300347 " serialize=%%d default: 0 (do not serialize/deserialize)\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000348 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000349
Hanno Becker8651a432017-06-09 16:13:22 +0100350#define ALPN_LIST_SIZE 10
351#define CURVE_LIST_SIZE 20
352
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100353#if defined(MBEDTLS_CHECK_PARAMS)
354#include "mbedtls/platform_util.h"
355void mbedtls_param_failed( const char *failure_condition,
356 const char *file,
357 int line )
Simon Butcher63cb97e2018-12-06 17:43:31 +0000358{
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100359 mbedtls_printf( "%s:%i: Input param failed - %s\n",
360 file, line, failure_condition );
Simon Butcher63cb97e2018-12-06 17:43:31 +0000361 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
362}
363#endif
364
Rich Evans85b05ec2015-02-12 11:37:29 +0000365/*
366 * global options
367 */
368struct options
369{
370 const char *server_name; /* hostname of the server (client only) */
371 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200372 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000373 int debug_level; /* level of debugging */
374 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100375 int event; /* loop or event-driven IO? level or edge triggered? */
376 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000377 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000378 const char *request_page; /* page on server to request */
379 int request_size; /* pad request with header to requested size */
380 const char *ca_file; /* the file with the CA certificate(s) */
381 const char *ca_path; /* the path with the CA certificate(s) reside */
382 const char *crt_file; /* the file with the client certificate */
383 const char *key_file; /* the file with the client key */
384 const char *psk; /* the pre-shared key */
385 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200386 const char *ecjpake_pw; /* the EC J-PAKE password */
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200387 int ec_max_ops; /* EC consecutive operations limit */
Rich Evans85b05ec2015-02-12 11:37:29 +0000388 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
389 int renegotiation; /* enable / disable renegotiation */
390 int allow_legacy; /* allow legacy renegotiation */
391 int renegotiate; /* attempt renegotiation? */
392 int renego_delay; /* delay before enforcing renegotiation */
393 int exchanges; /* number of data exchanges */
394 int min_version; /* minimum protocol version accepted */
395 int max_version; /* maximum protocol version accepted */
396 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200397 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000398 int auth_mode; /* verify mode for connection */
399 unsigned char mfl_code; /* code for maximum fragment length */
400 int trunc_hmac; /* negotiate truncated hmac or not */
401 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200402 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000403 int reconnect; /* attempt to resume session */
404 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +0200405 int reco_mode; /* how to keep the session around */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200406 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000407 int tickets; /* enable / disable session tickets */
Hanno Beckere6706e62017-05-15 16:05:15 +0100408 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000409 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000410 int transport; /* TLS or DTLS? */
411 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
412 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200413 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Rich Evans85b05ec2015-02-12 11:37:29 +0000414 int fallback; /* is this a fallback connection? */
Hanno Becker4d615912018-08-14 13:33:30 +0100415 int dgram_packing; /* allow/forbid datagram packing */
Rich Evans85b05ec2015-02-12 11:37:29 +0000416 int extended_ms; /* negotiate extended master secret? */
Jarno Lamsa41b35912019-06-10 15:51:11 +0300417 int enforce_extended_master_secret; /* Enforce the usage of extended
418 * master secret */
Rich Evans85b05ec2015-02-12 11:37:29 +0000419 int etm; /* negotiate encrypt then mac? */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100420 int cid_enabled; /* whether to use the CID extension or not */
Hanno Becker96870292019-05-03 17:30:59 +0100421 int cid_enabled_renego; /* whether to use the CID extension or not
422 * during renegotiation */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100423 const char *cid_val; /* the CID to use for incoming messages */
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +0300424 int serialize; /* serialize/deserialize connection */
Hanno Becker96870292019-05-03 17:30:59 +0100425 const char *cid_val_renego; /* the CID to use for incoming messages
426 * after renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000427} opt;
428
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100429int query_config( const char *config );
430
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200431static void my_debug( void *ctx, int level,
432 const char *file, int line,
433 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000434{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200435 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000436
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200437 /* Extract basename from file */
438 for( p = basename = file; *p != '\0'; p++ )
439 if( *p == '/' || *p == '\\' )
440 basename = p + 1;
441
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100442 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s",
443 basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000444 fflush( (FILE *) ctx );
445}
446
447/*
448 * Test recv/send functions that make sure each try returns
449 * WANT_READ/WANT_WRITE at least once before sucesseding
450 */
451static int my_recv( void *ctx, unsigned char *buf, size_t len )
452{
453 static int first_try = 1;
454 int ret;
455
456 if( first_try )
457 {
458 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100459 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000460 }
461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100463 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000464 first_try = 1; /* Next call will be a new operation */
465 return( ret );
466}
467
468static int my_send( void *ctx, const unsigned char *buf, size_t len )
469{
470 static int first_try = 1;
471 int ret;
472
473 if( first_try )
474 {
475 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100476 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000477 }
478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100480 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000481 first_try = 1; /* Next call will be a new operation */
482 return( ret );
483}
484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485#if defined(MBEDTLS_X509_CRT_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000486/*
487 * Enabled if debug_level > 1 in code below
488 */
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100489static int my_verify( void *data, mbedtls_x509_crt *crt,
490 int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000491{
492 char buf[1024];
493 ((void) data);
494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
496 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
497 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000498
Rich Evans85b05ec2015-02-12 11:37:29 +0000499 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100501 else
502 {
503 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
504 mbedtls_printf( "%s\n", buf );
505 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000506
507 return( 0 );
508}
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200509
510static int ssl_sig_hashes_for_test[] = {
511#if defined(MBEDTLS_SHA512_C)
512 MBEDTLS_MD_SHA512,
513 MBEDTLS_MD_SHA384,
514#endif
515#if defined(MBEDTLS_SHA256_C)
516 MBEDTLS_MD_SHA256,
517 MBEDTLS_MD_SHA224,
518#endif
519#if defined(MBEDTLS_SHA1_C)
520 /* Allow SHA-1 as we use it extensively in tests. */
521 MBEDTLS_MD_SHA1,
522#endif
523 MBEDTLS_MD_NONE
524};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000526
Hanno Becker16970d22017-10-10 15:56:37 +0100527/*
528 * Wait for an event from the underlying transport or the timer
529 * (Used in event-driven IO mode).
530 */
531#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000532int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000533 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100534#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000535int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000536 mbedtls_timing_delay_context *timer,
537 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100538#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000539{
Hanno Becker16970d22017-10-10 15:56:37 +0100540
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000541 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +0100542 int poll_type = 0;
543
544 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
545 poll_type = MBEDTLS_NET_POLL_WRITE;
546 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
547 poll_type = MBEDTLS_NET_POLL_READ;
548#if !defined(MBEDTLS_TIMING_C)
549 else
Hanno Beckeref527962018-03-15 15:49:24 +0000550 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100551#endif
552
553 while( 1 )
554 {
Hanno Becker197a91c2017-10-31 10:58:53 +0000555 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +0100556#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +0000557 if( timer != NULL &&
558 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +0100559 {
Hanno Becker16970d22017-10-10 15:56:37 +0100560 break;
561 }
Hanno Becker197a91c2017-10-31 10:58:53 +0000562#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +0100563
Hanno Becker197a91c2017-10-31 10:58:53 +0000564 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000565 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +0100566 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000567 ret = mbedtls_net_poll( fd, poll_type, 0 );
568 if( ret < 0 )
569 return( ret );
570 if( ret == poll_type )
571 break;
Hanno Becker16970d22017-10-10 15:56:37 +0100572 }
573 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000574
575 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100576}
577
Hanno Beckerec370302019-04-09 17:12:56 +0100578/* Unhexify `hex` into `dst`. `dst` must have
579 * size at least `strlen( hex ) / 2`. */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100580int unhexify( char const *hex, unsigned char *dst )
Hanno Beckerec370302019-04-09 17:12:56 +0100581{
582 unsigned char c;
583 size_t j;
584 size_t len = strlen( hex );
585
586 if( len % 2 != 0 )
587 return( -1 );
588
589 for( j = 0; j < len; j += 2 )
590 {
591 c = hex[j];
592 if( c >= '0' && c <= '9' )
593 c -= '0';
594 else if( c >= 'a' && c <= 'f' )
595 c -= 'a' - 10;
596 else if( c >= 'A' && c <= 'F' )
597 c -= 'A' - 10;
598 else
599 return( -1 );
600 dst[ j / 2 ] = c << 4;
601
602 c = hex[j + 1];
603 if( c >= '0' && c <= '9' )
604 c -= '0';
605 else if( c >= 'a' && c <= 'f' )
606 c -= 'a' - 10;
607 else if( c >= 'A' && c <= 'F' )
608 c -= 'A' - 10;
609 else
610 return( -1 );
611 dst[ j / 2 ] |= c;
612 }
613
614 return( 0 );
615}
616
Hanno Beckera5a2b082019-05-15 14:03:01 +0100617#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +0100618int report_cid_usage( mbedtls_ssl_context *ssl,
619 const char *additional_description )
620{
621 int ret;
622 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
623 size_t peer_cid_len;
624 int cid_negotiated;
625
626 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
627 return( 0 );
628
Hanno Beckerac363882019-05-22 16:59:25 +0100629 /* Check if the use of a CID has been negotiated,
630 * but don't ask for the CID value and length.
631 *
632 * Note: Here and below, we're demonstrating the various ways
633 * in which mbedtls_ssl_get_peer_cid() can be called,
634 * depending on whether or not the length/value of the
635 * peer's CID is needed.
636 *
637 * An actual application, however, should use
638 * just one call to mbedtls_ssl_get_peer_cid(). */
Hanno Becker96870292019-05-03 17:30:59 +0100639 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
Hanno Beckerac363882019-05-22 16:59:25 +0100640 NULL, NULL );
Hanno Becker96870292019-05-03 17:30:59 +0100641 if( ret != 0 )
642 {
643 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
644 -ret );
645 return( ret );
646 }
647
648 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
649 {
650 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
651 {
652 mbedtls_printf( "(%s) Use of Connection ID was rejected by the server.\n",
653 additional_description );
654 }
655 }
656 else
657 {
658 size_t idx=0;
659 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
660 additional_description );
Hanno Beckerac363882019-05-22 16:59:25 +0100661
662 /* Ask for just the length of the peer's CID. */
663 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
664 NULL, &peer_cid_len );
665 if( ret != 0 )
666 {
667 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
668 -ret );
669 return( ret );
670 }
671
672 /* Ask for just length + value of the peer's CID. */
673 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
674 peer_cid, &peer_cid_len );
675 if( ret != 0 )
676 {
677 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
678 -ret );
679 return( ret );
680 }
Hanno Becker96870292019-05-03 17:30:59 +0100681 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
682 additional_description,
683 (unsigned) peer_cid_len );
684 while( idx < peer_cid_len )
685 {
686 mbedtls_printf( "%02x ", peer_cid[ idx ] );
687 idx++;
688 }
689 mbedtls_printf( "\n" );
690 }
691
692 return( 0 );
693}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100694#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker96870292019-05-03 17:30:59 +0100695
Paul Bakker9caf2d22010-02-18 19:37:19 +0000696int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000697{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200698 int ret = 0, len, tail_len, i, written, frags, retry_left;
699 mbedtls_net_context server_fd;
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100700
701 unsigned char buf[MAX_REQUEST_SIZE + 1];
702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
704 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200705 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200706#endif
Hanno Becker7a7aa192019-04-09 17:24:19 +0100707
Hanno Beckera5a2b082019-05-15 14:03:01 +0100708#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7a7aa192019-04-09 17:24:19 +0100709 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker96870292019-05-03 17:30:59 +0100710 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker7a7aa192019-04-09 17:24:19 +0100711 size_t cid_len = 0;
Hanno Becker96870292019-05-03 17:30:59 +0100712 size_t cid_renego_len = 0;
Hanno Becker7a7aa192019-04-09 17:24:19 +0100713#endif
714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100716 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200717#endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100718#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +0100719 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100720 const mbedtls_ecp_curve_info *curve_cur;
721#endif
722
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200723 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000724
Gilles Peskineef86ab22017-05-05 18:59:02 +0200725#if defined(MBEDTLS_X509_CRT_PARSE_C)
726 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
727#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 mbedtls_entropy_context entropy;
729 mbedtls_ctr_drbg_context ctr_drbg;
730 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200731 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +0200733 unsigned char *session_data = NULL;
734 size_t session_data_len = 0;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200735#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200736 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200737#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200739 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_x509_crt cacert;
741 mbedtls_x509_crt clicert;
742 mbedtls_pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200743#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000744 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000745 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000746
Paul Bakker1a207ec2011-02-06 13:22:40 +0000747 /*
748 * Make sure memory references are valid.
749 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200750 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200751 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200752 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200754 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755#if defined(MBEDTLS_X509_CRT_PARSE_C)
756 mbedtls_x509_crt_init( &cacert );
757 mbedtls_x509_crt_init( &clicert );
758 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200759#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200761 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200762#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000763
Paul Bakker9caf2d22010-02-18 19:37:19 +0000764 if( argc == 0 )
765 {
766 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000767 if( ret == 0 )
768 ret = 1;
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000773 while( *list )
774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200776 list++;
777 if( !*list )
778 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000780 list++;
781 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000783 goto exit;
784 }
785
786 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100787 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000788 opt.server_port = DFL_SERVER_PORT;
789 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker7a7aa192019-04-09 17:24:19 +0100790 opt.cid_enabled = DFL_CID_ENABLED;
791 opt.cid_val = DFL_CID_VALUE;
Hanno Becker96870292019-05-03 17:30:59 +0100792 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
793 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100794 opt.nbio = DFL_NBIO;
Hanno Becker16970d22017-10-10 15:56:37 +0100795 opt.event = DFL_EVENT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200796 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200797 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000798 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200799 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000800 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000801 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000802 opt.crt_file = DFL_CRT_FILE;
803 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200804 opt.psk = DFL_PSK;
805 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200806 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200807 opt.ec_max_ops = DFL_EC_MAX_OPS;
Paul Bakker51936882011-02-20 16:05:58 +0000808 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000809 opt.renegotiation = DFL_RENEGOTIATION;
810 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100811 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200812 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000813 opt.min_version = DFL_MIN_VERSION;
814 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100815 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +0200816 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100817 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200818 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200819 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100820 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200821 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200822 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100823 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +0200824 opt.reco_mode = DFL_RECO_MODE;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200825 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200826 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200827 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +0100828 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200829 opt.transport = DFL_TRANSPORT;
830 opt.hs_to_min = DFL_HS_TO_MIN;
831 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200832 opt.dtls_mtu = DFL_DTLS_MTU;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200833 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200834 opt.extended_ms = DFL_EXTENDED_MS;
Jarno Lamsa41b35912019-06-10 15:51:11 +0300835 opt.enforce_extended_master_secret = DFL_EXTENDED_MS_ENFORCE;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100836 opt.etm = DFL_ETM;
Hanno Becker4d615912018-08-14 13:33:30 +0100837 opt.dgram_packing = DFL_DGRAM_PACKING;
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +0300838 opt.serialize = DFL_SERIALIZE;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000839
840 for( i = 1; i < argc; i++ )
841 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000842 p = argv[i];
843 if( ( q = strchr( p, '=' ) ) == NULL )
844 goto usage;
845 *q++ = '\0';
846
847 if( strcmp( p, "server_name" ) == 0 )
848 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100849 else if( strcmp( p, "server_addr" ) == 0 )
850 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000851 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200852 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100853 else if( strcmp( p, "dtls" ) == 0 )
854 {
855 int t = atoi( q );
856 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100858 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100860 else
861 goto usage;
862 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000863 else if( strcmp( p, "debug_level" ) == 0 )
864 {
865 opt.debug_level = atoi( q );
866 if( opt.debug_level < 0 || opt.debug_level > 65535 )
867 goto usage;
868 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100869 else if( strcmp( p, "nbio" ) == 0 )
870 {
871 opt.nbio = atoi( q );
872 if( opt.nbio < 0 || opt.nbio > 2 )
873 goto usage;
874 }
Hanno Becker16970d22017-10-10 15:56:37 +0100875 else if( strcmp( p, "event" ) == 0 )
876 {
877 opt.event = atoi( q );
878 if( opt.event < 0 || opt.event > 2 )
879 goto usage;
880 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200881 else if( strcmp( p, "read_timeout" ) == 0 )
882 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200883 else if( strcmp( p, "max_resend" ) == 0 )
884 {
885 opt.max_resend = atoi( q );
886 if( opt.max_resend < 0 )
887 goto usage;
888 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000889 else if( strcmp( p, "request_page" ) == 0 )
890 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200891 else if( strcmp( p, "request_size" ) == 0 )
892 {
893 opt.request_size = atoi( q );
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100894 if( opt.request_size < 0 ||
895 opt.request_size > MAX_REQUEST_SIZE )
Paul Bakker93c32b22014-04-25 13:40:05 +0200896 goto usage;
897 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000898 else if( strcmp( p, "ca_file" ) == 0 )
899 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000900 else if( strcmp( p, "ca_path" ) == 0 )
901 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000902 else if( strcmp( p, "crt_file" ) == 0 )
903 opt.crt_file = q;
904 else if( strcmp( p, "key_file" ) == 0 )
905 opt.key_file = q;
Hanno Beckera5a2b082019-05-15 14:03:01 +0100906#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7a7aa192019-04-09 17:24:19 +0100907 else if( strcmp( p, "cid" ) == 0 )
908 {
909 opt.cid_enabled = atoi( q );
910 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
911 goto usage;
912 }
Hanno Becker96870292019-05-03 17:30:59 +0100913 else if( strcmp( p, "cid_renego" ) == 0 )
914 {
915 opt.cid_enabled_renego = atoi( q );
916 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
917 goto usage;
918 }
Hanno Becker7a7aa192019-04-09 17:24:19 +0100919 else if( strcmp( p, "cid_val" ) == 0 )
920 {
921 opt.cid_val = q;
922 }
Hanno Becker96870292019-05-03 17:30:59 +0100923 else if( strcmp( p, "cid_val_renego" ) == 0 )
924 {
925 opt.cid_val_renego = q;
926 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100927#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200928 else if( strcmp( p, "psk" ) == 0 )
929 opt.psk = q;
930 else if( strcmp( p, "psk_identity" ) == 0 )
931 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200932 else if( strcmp( p, "ecjpake_pw" ) == 0 )
933 opt.ecjpake_pw = q;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200934 else if( strcmp( p, "ec_max_ops" ) == 0 )
935 opt.ec_max_ops = atoi( q );
Paul Bakker51936882011-02-20 16:05:58 +0000936 else if( strcmp( p, "force_ciphersuite" ) == 0 )
937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +0000939
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200940 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000941 {
942 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000943 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000944 }
Paul Bakker51936882011-02-20 16:05:58 +0000945 opt.force_ciphersuite[1] = 0;
946 }
Paul Bakker48916f92012-09-16 19:57:18 +0000947 else if( strcmp( p, "renegotiation" ) == 0 )
948 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100949 opt.renegotiation = (atoi( q )) ?
950 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
951 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +0000952 }
953 else if( strcmp( p, "allow_legacy" ) == 0 )
954 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100955 switch( atoi( q ) )
956 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100957 case -1:
958 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
959 break;
960 case 0:
961 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
962 break;
963 case 1:
964 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
965 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100966 default: goto usage;
967 }
Paul Bakker48916f92012-09-16 19:57:18 +0000968 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100969 else if( strcmp( p, "renegotiate" ) == 0 )
970 {
971 opt.renegotiate = atoi( q );
972 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
973 goto usage;
974 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200975 else if( strcmp( p, "exchanges" ) == 0 )
976 {
977 opt.exchanges = atoi( q );
978 if( opt.exchanges < 1 )
979 goto usage;
980 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200981 else if( strcmp( p, "reconnect" ) == 0 )
982 {
983 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200984 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200985 goto usage;
986 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100987 else if( strcmp( p, "reco_delay" ) == 0 )
988 {
989 opt.reco_delay = atoi( q );
990 if( opt.reco_delay < 0 )
991 goto usage;
992 }
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +0200993 else if( strcmp( p, "reco_mode" ) == 0 )
994 {
995 opt.reco_mode = atoi( q );
996 if( opt.reco_mode < 0 )
997 goto usage;
998 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200999 else if( strcmp( p, "reconnect_hard" ) == 0 )
1000 {
1001 opt.reconnect_hard = atoi( q );
1002 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
1003 goto usage;
1004 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001005 else if( strcmp( p, "tickets" ) == 0 )
1006 {
1007 opt.tickets = atoi( q );
1008 if( opt.tickets < 0 || opt.tickets > 2 )
1009 goto usage;
1010 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001011 else if( strcmp( p, "alpn" ) == 0 )
1012 {
1013 opt.alpn_string = q;
1014 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001015 else if( strcmp( p, "fallback" ) == 0 )
1016 {
1017 switch( atoi( q ) )
1018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
1020 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001021 default: goto usage;
1022 }
1023 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001024 else if( strcmp( p, "extended_ms" ) == 0 )
1025 {
1026 switch( atoi( q ) )
1027 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001028 case 0:
1029 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1030 break;
1031 case 1:
1032 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1033 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001034 default: goto usage;
1035 }
1036 }
Jarno Lamsa41b35912019-06-10 15:51:11 +03001037 else if( strcmp( p, "enforce_extended_master_secret" ) == 0 )
1038 {
1039 switch( atoi( q ) )
1040 {
1041 case 0:
1042 opt.enforce_extended_master_secret =
1043 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
1044 break;
1045 case 1:
1046 opt.enforce_extended_master_secret =
1047 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_ENABLED;
1048 break;
1049 default: goto usage;
1050 }
1051 }
Hanno Beckere6706e62017-05-15 16:05:15 +01001052 else if( strcmp( p, "curves" ) == 0 )
1053 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001054 else if( strcmp( p, "etm" ) == 0 )
1055 {
1056 switch( atoi( q ) )
1057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1059 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001060 default: goto usage;
1061 }
1062 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001063 else if( strcmp( p, "min_version" ) == 0 )
1064 {
1065 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001067 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001069 else if( strcmp( q, "tls1_1" ) == 0 ||
1070 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001072 else if( strcmp( q, "tls1_2" ) == 0 ||
1073 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001075 else
1076 goto usage;
1077 }
1078 else if( strcmp( p, "max_version" ) == 0 )
1079 {
1080 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001082 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001084 else if( strcmp( q, "tls1_1" ) == 0 ||
1085 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001087 else if( strcmp( q, "tls1_2" ) == 0 ||
1088 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001090 else
1091 goto usage;
1092 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001093 else if( strcmp( p, "arc4" ) == 0 )
1094 {
1095 switch( atoi( q ) )
1096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1098 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001099 default: goto usage;
1100 }
1101 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02001102 else if( strcmp( p, "allow_sha1" ) == 0 )
1103 {
1104 switch( atoi( q ) )
1105 {
1106 case 0: opt.allow_sha1 = 0; break;
1107 case 1: opt.allow_sha1 = 1; break;
1108 default: goto usage;
1109 }
1110 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001111 else if( strcmp( p, "force_version" ) == 0 )
1112 {
1113 if( strcmp( q, "ssl3" ) == 0 )
1114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1116 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001117 }
1118 else if( strcmp( q, "tls1" ) == 0 )
1119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1121 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001122 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001123 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1126 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001127 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001128 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1131 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001132 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001133 else if( strcmp( q, "dtls1" ) == 0 )
1134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1136 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1137 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001138 }
1139 else if( strcmp( q, "dtls1_2" ) == 0 )
1140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1142 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1143 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001144 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001145 else
1146 goto usage;
1147 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001148 else if( strcmp( p, "auth_mode" ) == 0 )
1149 {
1150 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001152 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001154 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001156 else
1157 goto usage;
1158 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001159 else if( strcmp( p, "max_frag_len" ) == 0 )
1160 {
1161 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001163 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001165 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001167 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001169 else
1170 goto usage;
1171 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001172 else if( strcmp( p, "trunc_hmac" ) == 0 )
1173 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001174 switch( atoi( q ) )
1175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1177 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001178 default: goto usage;
1179 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001180 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001181 else if( strcmp( p, "hs_timeout" ) == 0 )
1182 {
1183 if( ( p = strchr( q, '-' ) ) == NULL )
1184 goto usage;
1185 *p++ = '\0';
1186 opt.hs_to_min = atoi( q );
1187 opt.hs_to_max = atoi( p );
1188 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1189 goto usage;
1190 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001191 else if( strcmp( p, "mtu" ) == 0 )
1192 {
1193 opt.dtls_mtu = atoi( q );
1194 if( opt.dtls_mtu < 0 )
1195 goto usage;
1196 }
Hanno Becker4d615912018-08-14 13:33:30 +01001197 else if( strcmp( p, "dgram_packing" ) == 0 )
1198 {
1199 opt.dgram_packing = atoi( q );
1200 if( opt.dgram_packing != 0 &&
1201 opt.dgram_packing != 1 )
1202 {
1203 goto usage;
1204 }
1205 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001206 else if( strcmp( p, "recsplit" ) == 0 )
1207 {
1208 opt.recsplit = atoi( q );
1209 if( opt.recsplit < 0 || opt.recsplit > 1 )
1210 goto usage;
1211 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001212 else if( strcmp( p, "dhmlen" ) == 0 )
1213 {
1214 opt.dhmlen = atoi( q );
1215 if( opt.dhmlen < 0 )
1216 goto usage;
1217 }
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +01001218 else if( strcmp( p, "query_config" ) == 0 )
1219 {
1220 return query_config( q );
1221 }
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03001222 else if( strcmp( p, "serialize") == 0 )
1223 {
1224 opt.serialize = atoi( q );
1225 if( opt.serialize < 0 || opt.serialize > 1)
1226 goto usage;
1227 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001228 else
1229 goto usage;
1230 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001231
Hanno Becker16970d22017-10-10 15:56:37 +01001232 /* Event-driven IO is incompatible with the above custom
1233 * receive and send functions, as the polling builds on
1234 * refers to the underlying net_context. */
1235 if( opt.event == 1 && opt.nbio != 1 )
1236 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001237 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001238 opt.nbio = 1;
1239 }
1240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241#if defined(MBEDTLS_DEBUG_C)
1242 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001243#endif
1244
Paul Bakkerc1516be2013-06-29 16:01:32 +02001245 if( opt.force_ciphersuite[0] > 0 )
1246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001248 ciphersuite_info =
1249 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001250
Paul Bakker66c48102013-07-26 14:05:32 +02001251 if( opt.max_version != -1 &&
1252 ciphersuite_info->min_minor_ver > opt.max_version )
1253 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001254 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker66c48102013-07-26 14:05:32 +02001255 ret = 2;
1256 goto usage;
1257 }
1258 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001259 ciphersuite_info->max_minor_ver < opt.min_version )
1260 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001261 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001262 ret = 2;
1263 goto usage;
1264 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001265
1266 /* If the server selects a version that's not supported by
1267 * this suite, then there will be no common ciphersuite... */
1268 if( opt.max_version == -1 ||
1269 opt.max_version > ciphersuite_info->max_minor_ver )
1270 {
Paul Bakker66c48102013-07-26 14:05:32 +02001271 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001272 }
Paul Bakker66c48102013-07-26 14:05:32 +02001273 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001274 {
Paul Bakker66c48102013-07-26 14:05:32 +02001275 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001276 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1278 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1279 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001280 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001281
1282 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001286 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001287 mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001288 ret = 2;
1289 goto usage;
1290 }
1291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001293 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001294 }
1295
Hanno Beckera5a2b082019-05-15 14:03:01 +01001296#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01001297 cid_len = strlen( opt.cid_val ) / 2;
1298 if( cid_len > sizeof( cid ) )
1299 {
1300 mbedtls_printf( "CID too long\n" );
1301 goto exit;
1302 }
Hanno Becker7a7aa192019-04-09 17:24:19 +01001303
Hanno Becker96870292019-05-03 17:30:59 +01001304 if( unhexify( opt.cid_val, cid ) != 0 )
1305 {
1306 mbedtls_printf( "CID not valid hex\n" );
1307 goto exit;
1308 }
1309
1310 /* Keep CID settings for renegotiation unless
1311 * specified otherwise. */
1312 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
1313 opt.cid_enabled_renego = opt.cid_enabled;
1314 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
1315 opt.cid_val_renego = opt.cid_val;
1316
1317 cid_renego_len = strlen( opt.cid_val_renego ) / 2;
1318 if( cid_renego_len > sizeof( cid_renego ) )
1319 {
1320 mbedtls_printf( "CID too long\n" );
1321 goto exit;
1322 }
1323
1324 if( unhexify( opt.cid_val_renego, cid_renego ) != 0 )
1325 {
1326 mbedtls_printf( "CID not valid hex\n" );
1327 goto exit;
1328 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001329#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +01001330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001333 * Unhexify the pre-shared key if any is given
1334 */
1335 if( strlen( opt.psk ) )
1336 {
Hanno Beckerec370302019-04-09 17:12:56 +01001337 psk_len = strlen( opt.psk ) / 2;
1338 if( psk_len > sizeof( psk ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001339 {
Hanno Beckerec370302019-04-09 17:12:56 +01001340 mbedtls_printf( "pre-shared key too long\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001341 goto exit;
1342 }
1343
Hanno Beckerec370302019-04-09 17:12:56 +01001344 if( unhexify( opt.psk, psk ) != 0 )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001345 {
Hanno Beckerec370302019-04-09 17:12:56 +01001346 mbedtls_printf( "pre-shared key not valid hex\n" );
1347 goto exit;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001348 }
1349 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001351
Hanno Beckere6706e62017-05-15 16:05:15 +01001352#if defined(MBEDTLS_ECP_C)
1353 if( opt.curves != NULL )
1354 {
1355 p = (char *) opt.curves;
1356 i = 0;
1357
1358 if( strcmp( p, "none" ) == 0 )
1359 {
1360 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1361 }
1362 else if( strcmp( p, "default" ) != 0 )
1363 {
1364 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001365 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001366 {
1367 q = p;
1368
1369 /* Terminate the current string */
1370 while( *p != ',' && *p != '\0' )
1371 p++;
1372 if( *p == ',' )
1373 *p++ = '\0';
1374
1375 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1376 {
1377 curve_list[i++] = curve_cur->grp_id;
1378 }
1379 else
1380 {
1381 mbedtls_printf( "unknown curve %s\n", q );
1382 mbedtls_printf( "supported curves: " );
1383 for( curve_cur = mbedtls_ecp_curve_list();
1384 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1385 curve_cur++ )
1386 {
1387 mbedtls_printf( "%s ", curve_cur->name );
1388 }
1389 mbedtls_printf( "\n" );
1390 goto exit;
1391 }
1392 }
1393
1394 mbedtls_printf("Number of curves: %d\n", i );
1395
Hanno Becker8651a432017-06-09 16:13:22 +01001396 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001397 {
Hanno Becker8651a432017-06-09 16:13:22 +01001398 mbedtls_printf( "curves list too long, maximum %d",
1399 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001400 goto exit;
1401 }
1402
1403 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1404 }
1405 }
1406#endif /* MBEDTLS_ECP_C */
1407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001409 if( opt.alpn_string != NULL )
1410 {
1411 p = (char *) opt.alpn_string;
1412 i = 0;
1413
1414 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001415 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001416 {
1417 alpn_list[i++] = p;
1418
1419 /* Terminate the current string and move on to next one */
1420 while( *p != ',' && *p != '\0' )
1421 p++;
1422 if( *p == ',' )
1423 *p++ = '\0';
1424 }
1425 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001427
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001428 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 * 0. Initialize the RNG and the session data
1430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001432 fflush( stdout );
1433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 mbedtls_entropy_init( &entropy );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001435 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1436 &entropy, (const unsigned char *) pers,
1437 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +00001438 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001439 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
1440 -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001441 goto exit;
1442 }
1443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 /*
1448 * 1.1. Load the trusted CA
1449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001451 fflush( stdout );
1452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001454 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001455 if( strcmp( opt.ca_path, "none" ) == 0 )
1456 ret = 0;
1457 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001459 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001460 if( strcmp( opt.ca_file, "none" ) == 0 )
1461 ret = 0;
1462 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001464 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001465#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#if defined(MBEDTLS_CERTS_C)
1467 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 ret = mbedtls_x509_crt_parse( &cacert,
1470 (const unsigned char *) mbedtls_test_cas[i],
1471 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001472 if( ret != 0 )
1473 break;
1474 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001475#else
1476 {
1477 ret = 1;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001478 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001479 }
1480#endif
Paul Bakker42488232012-05-16 08:21:05 +00001481 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001483 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1484 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001485 goto exit;
1486 }
1487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001489
1490 /*
1491 * 1.2. Load own certificate and private key
1492 *
1493 * (can be skipped if client authentication is not required)
1494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 fflush( stdout );
1497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001499 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001500 if( strcmp( opt.crt_file, "none" ) == 0 )
1501 ret = 0;
1502 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001504 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001505#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001507 ret = mbedtls_x509_crt_parse( &clicert,
1508 (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001510#else
1511 {
1512 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001514 }
1515#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001516 if( ret != 0 )
1517 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001518 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1519 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001520 goto exit;
1521 }
1522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001524 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001525 if( strcmp( opt.key_file, "none" ) == 0 )
1526 ret = 0;
1527 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001529 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001530#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001532 ret = mbedtls_pk_parse_key( &pkey,
1533 (const unsigned char *) mbedtls_test_cli_key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001535#else
1536 {
1537 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +00001539 }
1540#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001541 if( ret != 0 )
1542 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001543 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
1544 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001545 goto exit;
1546 }
1547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 mbedtls_printf( " ok\n" );
1549#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001550
1551 /*
1552 * 2. Start the connection
1553 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001554 if( opt.server_addr == NULL)
1555 opt.server_addr = opt.server_name;
1556
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001557 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001559 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001560 fflush( stdout );
1561
Hanno Becker16970d22017-10-10 15:56:37 +01001562 if( ( ret = mbedtls_net_connect( &server_fd,
1563 opt.server_addr, opt.server_port,
1564 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1565 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001567 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
1568 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001569 goto exit;
1570 }
1571
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001572 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001573 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001574 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001575 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001576 if( ret != 0 )
1577 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001578 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
1579 -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001580 goto exit;
1581 }
1582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001584
1585 /*
1586 * 3. Setup stuff
1587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001589 fflush( stdout );
1590
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001591 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1592 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001593 opt.transport,
1594 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001595 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001596 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
1597 -ret );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001598 goto exit;
1599 }
1600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001602 /* The default algorithms profile disables SHA-1, but our tests still
1603 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001604 if( opt.allow_sha1 > 0 )
1605 {
1606 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1607 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1608 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1609 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001610
Paul Bakker915275b2012-09-28 07:10:55 +00001611 if( opt.debug_level > 0 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001612 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
Gilles Peskineef86ab22017-05-05 18:59:02 +02001613#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001614
Hanno Beckera5a2b082019-05-15 14:03:01 +01001615#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01001616 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckereec2be92019-05-03 13:06:44 +01001617 {
Hanno Becker96870292019-05-03 17:30:59 +01001618 if( opt.cid_enabled == 1 &&
1619 opt.cid_enabled_renego == 1 &&
1620 cid_len != cid_renego_len )
1621 {
1622 mbedtls_printf( "CID length must not change during renegotiation\n" );
1623 goto usage;
1624 }
1625
Hanno Becker96870292019-05-03 17:30:59 +01001626 if( opt.cid_enabled == 1 )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01001627 ret = mbedtls_ssl_conf_cid( &conf, cid_len,
1628 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Becker96870292019-05-03 17:30:59 +01001629 else
Hanno Beckere8eff9a2019-05-14 11:30:10 +01001630 ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
1631 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Becker96870292019-05-03 17:30:59 +01001632
Hanno Beckereec2be92019-05-03 13:06:44 +01001633 if( ret != 0 )
1634 {
Hanno Becker76581052019-05-23 16:58:22 +01001635 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
1636 -ret );
Hanno Beckereec2be92019-05-03 13:06:44 +01001637 goto exit;
1638 }
1639 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001640#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckereec2be92019-05-03 13:06:44 +01001641
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001642 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001643 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001646 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Hanno Becker16970d22017-10-10 15:56:37 +01001647 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min,
1648 opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001649
Hanno Becker4d615912018-08-14 13:33:30 +01001650 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01001651 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001655 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001656 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001657 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n",
1658 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001659 goto exit;
1660 }
Paul Bakker05decb22013-08-15 13:33:48 +02001661#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001664 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001665 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001666#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001669 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001670 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Jarno Lamsa41b35912019-06-10 15:51:11 +03001671 if( opt.enforce_extended_master_secret != DFL_EXTENDED_MS_ENFORCE )
1672 mbedtls_ssl_conf_extended_master_secret_enforce( &conf,
1673 opt.enforce_extended_master_secret );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001674#endif
1675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001677 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001678 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001679#endif
1680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001682 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001683 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Hanno Becker16970d22017-10-10 15:56:37 +01001684 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1685 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001686#endif
1687
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001688#if defined(MBEDTLS_DHM_C)
1689 if( opt.dhmlen != DFL_DHMLEN )
1690 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1691#endif
1692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001694 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001695 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001696 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001697 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
1698 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001699 goto exit;
1700 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001701#endif
1702
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001703 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1704 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001705
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001706 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02001709 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02001710#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001711
Paul Bakker645ce3a2012-10-31 12:32:41 +00001712 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001713 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001714
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001715#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001716 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001717 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001718#endif
Paul Bakker51936882011-02-20 16:05:58 +00001719
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001720 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001721 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001723 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001724#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001727 if( strcmp( opt.ca_path, "none" ) != 0 &&
1728 strcmp( opt.ca_file, "none" ) != 0 )
1729 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001730 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001731 }
1732 if( strcmp( opt.crt_file, "none" ) != 0 &&
1733 strcmp( opt.key_file, "none" ) != 0 )
1734 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001735 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001736 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001737 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
1738 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001739 goto exit;
1740 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001741 }
Paul Bakkered27a042013-04-18 22:46:23 +02001742#endif
1743
Hanno Beckere6706e62017-05-15 16:05:15 +01001744#if defined(MBEDTLS_ECP_C)
1745 if( opt.curves != NULL &&
1746 strcmp( opt.curves, "default" ) != 0 )
1747 {
1748 mbedtls_ssl_conf_curves( &conf, curve_list );
1749 }
1750#endif
1751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001753 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001754 (const unsigned char *) opt.psk_identity,
1755 strlen( opt.psk_identity ) ) ) != 0 )
1756 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001757 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n",
1758 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001759 goto exit;
1760 }
Paul Bakkered27a042013-04-18 22:46:23 +02001761#endif
1762
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001763 if( opt.min_version != DFL_MIN_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01001764 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1765 opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001766
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001767 if( opt.max_version != DFL_MAX_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01001768 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1769 opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001772 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001773 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001774#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001775
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001776 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
1777 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001778 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
1779 -ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001780 goto exit;
1781 }
1782
1783#if defined(MBEDTLS_X509_CRT_PARSE_C)
1784 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1785 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001786 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
1787 ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001788 goto exit;
1789 }
1790#endif
1791
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001792#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1793 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
1794 {
1795 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
1796 (const unsigned char *) opt.ecjpake_pw,
1797 strlen( opt.ecjpake_pw ) ) ) != 0 )
1798 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001799 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
1800 ret );
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001801 goto exit;
1802 }
1803 }
1804#endif
1805
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001806 if( opt.nbio == 2 )
1807 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
1808 else
Hanno Becker16970d22017-10-10 15:56:37 +01001809 mbedtls_ssl_set_bio( &ssl, &server_fd,
1810 mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02001811 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001812
Hanno Beckera5a2b082019-05-15 14:03:01 +01001813#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7a7aa192019-04-09 17:24:19 +01001814 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1815 {
1816 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
1817 cid, cid_len ) ) != 0 )
1818 {
1819 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
1820 ret );
1821 goto exit;
1822 }
1823 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001824#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +01001825
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001826#if defined(MBEDTLS_SSL_PROTO_DTLS)
1827 if( opt.dtls_mtu != DFL_DTLS_MTU )
1828 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
1829#endif
1830
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001831#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001832 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
1833 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001834#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001835
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001836#if defined(MBEDTLS_ECP_RESTARTABLE)
1837 if( opt.ec_max_ops != DFL_EC_MAX_OPS )
1838 mbedtls_ecp_set_max_ops( opt.ec_max_ops );
1839#endif
1840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001842
Paul Bakker5121ce52009-01-03 21:22:43 +00001843 /*
1844 * 4. Handshake
1845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001847 fflush( stdout );
1848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 {
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001851 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1852 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001853 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Paul Bakker40e46942009-01-03 21:51:57 +00001854 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001855 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
1856 -ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
1858 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001859 " Unable to verify the server's certificate. "
1860 "Either it is invalid,\n"
1861 " or you didn't set ca_file or ca_path "
1862 "to an appropriate value.\n"
1863 " Alternatively, you may want to use "
1864 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001866 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00001867 }
Hanno Becker16970d22017-10-10 15:56:37 +01001868
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001869#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02001870 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
1871 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001872#endif
1873
Hanno Becker16970d22017-10-10 15:56:37 +01001874 /* For event-driven IO, wait for socket to become available */
1875 if( opt.event == 1 /* level triggered IO */ )
1876 {
1877#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001878 ret = idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001879#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001880 ret = idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01001881#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001882 if( ret != 0 )
1883 goto exit;
Hanno Becker16970d22017-10-10 15:56:37 +01001884 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001885 }
1886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Hanno Becker16970d22017-10-10 15:56:37 +01001888 mbedtls_ssl_get_version( &ssl ),
1889 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
1892 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001893 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001895
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02001896#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1897 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
1898 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
1899#endif
1900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001902 if( opt.alpn_string != NULL )
1903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
1905 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001906 alp ? alp : "(none)" );
1907 }
1908#endif
1909
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001910 if( opt.reconnect != 0 )
1911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001913 fflush( stdout );
1914
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02001915 if( opt.reco_mode == 1 )
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02001916 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02001917 /* free any previously saved data */
Manuel Pégourié-Gonnard4bb1b992019-05-24 10:26:41 +02001918 if( session_data != NULL )
1919 {
1920 mbedtls_platform_zeroize( session_data, session_data_len );
1921 mbedtls_free( session_data );
1922 session_data = NULL;
1923 }
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02001924
1925 /* get size of the buffer needed */
1926 mbedtls_ssl_session_save( mbedtls_ssl_get_session_pointer( &ssl ),
1927 NULL, 0, &session_data_len );
1928 session_data = mbedtls_calloc( 1, session_data_len );
1929 if( session_data == NULL )
1930 {
1931 mbedtls_printf( " failed\n ! alloc %u bytes for session data\n",
1932 (unsigned) session_data_len );
1933 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1934 goto exit;
1935 }
1936
1937 /* actually save session data */
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02001938 if( ( ret = mbedtls_ssl_session_save( mbedtls_ssl_get_session_pointer( &ssl ),
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02001939 session_data, session_data_len,
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02001940 &session_data_len ) ) != 0 )
1941 {
1942 mbedtls_printf( " failed\n ! mbedtls_ssl_session_saved returned -0x%04x\n\n",
1943 -ret );
1944 goto exit;
1945 }
1946 }
1947 else
1948 {
1949 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
1950 {
1951 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
1952 -ret );
1953 goto exit;
1954 }
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02001955 }
1956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02001958
1959 if( opt.reco_mode == 1 )
1960 {
1961 mbedtls_printf( " [ Saved %u bytes of session data]\n",
1962 (unsigned) session_data_len );
1963 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001964 }
1965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 /*
1968 * 5. Verify the server certificate
1969 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001971
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001972 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001973 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001974 char vrfy_buf[512];
1975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001977
Hanno Becker16970d22017-10-10 15:56:37 +01001978 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ),
1979 " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00001980
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01001981 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001982 }
1983 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 mbedtls_printf( " . Peer certificate information ...\n" );
1989 mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1990 mbedtls_ssl_get_peer_cert( &ssl ) );
1991 mbedtls_printf( "%s\n", buf );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001992 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001994
Hanno Beckera5a2b082019-05-15 14:03:01 +01001995#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01001996 ret = report_cid_usage( &ssl, "initial handshake" );
1997 if( ret != 0 )
1998 goto exit;
1999
Hanno Becker7a7aa192019-04-09 17:24:19 +01002000 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2001 {
Hanno Becker96870292019-05-03 17:30:59 +01002002 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
2003 cid_renego,
2004 cid_renego_len ) ) != 0 )
Hanno Becker7a7aa192019-04-09 17:24:19 +01002005 {
Hanno Becker96870292019-05-03 17:30:59 +01002006 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2007 ret );
2008 return( ret );
Hanno Becker7a7aa192019-04-09 17:24:19 +01002009 }
2010 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002011#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +01002012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002014 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002015 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002016 /*
2017 * Perform renegotiation (this must be done when the server is waiting
2018 * for input from our side).
2019 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002021 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002023 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002024 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002025 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002026 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002027 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002028 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
2029 ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002030 goto exit;
2031 }
Hanno Becker16970d22017-10-10 15:56:37 +01002032
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002033#if defined(MBEDTLS_ECP_RESTARTABLE)
2034 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2035 continue;
2036#endif
2037
Hanno Becker16970d22017-10-10 15:56:37 +01002038 /* For event-driven IO, wait for socket to become available */
2039 if( opt.event == 1 /* level triggered IO */ )
2040 {
2041#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002042 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002043#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002044 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002045#endif
2046 }
2047
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002048 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002050 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002052
Hanno Beckera5a2b082019-05-15 14:03:01 +01002053#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01002054 ret = report_cid_usage( &ssl, "after renegotiation" );
2055 if( ret != 0 )
2056 goto exit;
Hanno Beckera5a2b082019-05-15 14:03:01 +01002057#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker96870292019-05-03 17:30:59 +01002058
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 /*
2060 * 6. Write the GET request
2061 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002062 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002063send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 fflush( stdout );
2066
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002067 len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST,
2068 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002069 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002070
2071 /* Add padding to GET request to reach opt.request_size in length */
2072 if( opt.request_size != DFL_REQUEST_SIZE &&
2073 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02002074 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002075 memset( buf + len, 'A', opt.request_size - len - tail_len );
2076 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02002077 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002078
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002079 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002080 len += tail_len;
2081
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02002082 /* Truncate if request size is smaller than the "natural" size */
2083 if( opt.request_size != DFL_REQUEST_SIZE &&
2084 len > opt.request_size )
2085 {
2086 len = opt.request_size;
2087
2088 /* Still end with \r\n unless that's really not possible */
2089 if( len >= 2 ) buf[len - 2] = '\r';
2090 if( len >= 1 ) buf[len - 1] = '\n';
2091 }
2092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 {
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002095 written = 0;
2096 frags = 0;
2097
2098 do
Paul Bakker5121ce52009-01-03 21:22:43 +00002099 {
Hanno Becker16970d22017-10-10 15:56:37 +01002100 while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002101 len - written ) ) < 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002102 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002103 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002104 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002105 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002106 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002107 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
2108 -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002109 goto exit;
2110 }
Hanno Becker16970d22017-10-10 15:56:37 +01002111
2112 /* For event-driven IO, wait for socket to become available */
2113 if( opt.event == 1 /* level triggered IO */ )
2114 {
2115#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002116 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002117#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002118 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002119#endif
2120 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002121 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002122
2123 frags++;
2124 written += ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002126 while( written < len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002128 else /* Not stream, so datagram */
2129 {
Hanno Becker16970d22017-10-10 15:56:37 +01002130 while( 1 )
2131 {
2132 ret = mbedtls_ssl_write( &ssl, buf, len );
2133
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002134#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002135 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002136 continue;
2137#endif
2138
Hanno Becker16970d22017-10-10 15:56:37 +01002139 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2140 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2141 break;
2142
2143 /* For event-driven IO, wait for socket to become available */
2144 if( opt.event == 1 /* level triggered IO */ )
2145 {
2146#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002147 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002148#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002149 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002150#endif
2151 }
2152 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002153
2154 if( ret < 0 )
2155 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002156 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n",
2157 ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002158 goto exit;
2159 }
2160
2161 frags = 1;
2162 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002163
2164 if( written < len )
2165 {
2166 mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
2167 "was truncated to size %u", (unsigned) written );
2168 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002169 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002170
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002171 buf[written] = '\0';
Hanno Becker16970d22017-10-10 15:56:37 +01002172 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
2173 written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002174
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002175 /* Send a non-empty request if request_size == 0 */
2176 if ( len == 0 )
2177 {
2178 opt.request_size = DFL_REQUEST_SIZE;
2179 goto send_request;
2180 }
2181
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 /*
2183 * 7. Read the HTTP response
2184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002186 fflush( stdout );
2187
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002188 /*
2189 * TLS and DTLS need different reading styles (stream vs datagram)
2190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002192 {
2193 do
2194 {
2195 len = sizeof( buf ) - 1;
2196 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002198
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002199#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002200 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002201 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002202#endif
2203
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002204 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
2205 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Hanno Becker16970d22017-10-10 15:56:37 +01002206 {
2207 /* For event-driven IO, wait for socket to become available */
2208 if( opt.event == 1 /* level triggered IO */ )
2209 {
2210#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002211 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002212#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002213 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002214#endif
2215 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002216 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01002217 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002218
2219 if( ret <= 0 )
2220 {
2221 switch( ret )
2222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2224 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002225 ret = 0;
2226 goto close_notify;
2227
2228 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 case MBEDTLS_ERR_NET_CONN_RESET:
2230 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002231 ret = 0;
2232 goto reconnect;
2233
2234 default:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002235 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n",
2236 -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002237 goto exit;
2238 }
2239 }
2240
2241 len = ret;
2242 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002244
2245 /* End of message should be detected according to the syntax of the
2246 * application protocol (eg HTTP), just use a dummy test here. */
2247 if( ret > 0 && buf[len-1] == '\n' )
2248 {
2249 ret = 0;
2250 break;
2251 }
2252 }
2253 while( 1 );
2254 }
2255 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 {
2257 len = sizeof( buf ) - 1;
2258 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002259
Hanno Becker16970d22017-10-10 15:56:37 +01002260 while( 1 )
2261 {
2262 ret = mbedtls_ssl_read( &ssl, buf, len );
2263
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002264#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002265 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002266 continue;
2267#endif
2268
Hanno Becker16970d22017-10-10 15:56:37 +01002269 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2270 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2271 break;
2272
2273 /* For event-driven IO, wait for socket to become available */
2274 if( opt.event == 1 /* level triggered IO */ )
2275 {
2276#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002277 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002278#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002279 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002280#endif
2281 }
2282 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002283
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002284 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002285 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002286 switch( ret )
2287 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002288 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002290 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02002291 goto send_request;
2292 goto exit;
2293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2295 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002296 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002297 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00002298
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002299 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002301 goto exit;
2302 }
Paul Bakker5690efc2011-05-26 13:16:06 +00002303 }
2304
Paul Bakker5121ce52009-01-03 21:22:43 +00002305 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02002306 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002308 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002309 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002310
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002311 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002312 * 7b. Simulate hard reset and reconnect from same port?
2313 */
2314 if( opt.reconnect_hard != 0 )
2315 {
2316 opt.reconnect_hard = 0;
2317
2318 mbedtls_printf( " . Restarting connection from same port..." );
2319 fflush( stdout );
2320
2321 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
2322 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002323 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2324 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002325 goto exit;
2326 }
2327
2328 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
2329 {
2330 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002331 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002332 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002333 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002334 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2335 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002336 goto exit;
2337 }
Hanno Becker16970d22017-10-10 15:56:37 +01002338
2339 /* For event-driven IO, wait for socket to become available */
2340 if( opt.event == 1 /* level triggered IO */ )
2341 {
2342#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002343 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002344#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002345 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002346#endif
2347 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002348 }
2349
2350 mbedtls_printf( " ok\n" );
2351
2352 goto send_request;
2353 }
2354
2355 /*
Jarno Lamsad736d082019-05-29 15:15:08 +03002356 * 7c. Simulate serialize/deserialize and go back to data exchange
2357 */
2358 if( opt.serialize != 0)
2359 {
2360 size_t len;
2361 unsigned char *buf = NULL;
2362
2363 opt.serialize = 0;
2364 mbedtls_printf( " Serializing live connection..." );
2365
2366 if( ( ret = mbedtls_ssl_context_save( &ssl, NULL, 0, &len) ) != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
2367 {
2368 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned -0x%x\n\n", -ret );
2369
2370 goto exit;
2371 }
2372
2373 if( ( buf = mbedtls_calloc(1, len) ) == NULL )
2374 {
2375 mbedtls_printf( " failed\n ! Couldn't allocate buffer for serialized context" );
2376
2377 goto exit;
2378 }
2379
2380 if( ( ret = mbedtls_ssl_context_save( &ssl, buf, len, &len ) ) != 0 )
2381 {
2382 mbedtls_printf( "failed\n ! mbedtls_ssl_context_save returned -0x%x\n\n", -ret );
2383
2384 goto exit;
2385 }
2386
2387 mbedtls_ssl_free( &ssl );
2388
2389 mbedtls_printf( " Deserializing connection..." );
2390
2391 mbedtls_ssl_init( &ssl );
2392
2393 if( ( ret = mbedtls_ssl_context_load( &ssl, buf, len ) ) != 0 )
2394 {
2395 mbedtls_printf( "failed\n ! mbedtls_ssl_context_load returned -0x%x\n\n", -ret );
2396
2397 goto exit;
2398 }
2399
2400 goto send_request;
2401 }
2402
2403
2404 /*
2405 * 7d. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002406 */
2407 if( --opt.exchanges > 0 )
2408 goto send_request;
2409
2410 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002411 * 8. Done, cleanly close the connection
2412 */
2413close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002415 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002416
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002417 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002419 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002420 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002423
2424 /*
2425 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002426 */
2427reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002428 if( opt.reconnect != 0 )
2429 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02002430 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002431
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002432 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002433
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002434#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002435 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02002436 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002437#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002442 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002443 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2444 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002445 goto exit;
2446 }
2447
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02002448 if( opt.reco_mode == 1 )
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02002449 {
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02002450 if( ( ret = mbedtls_ssl_session_load( &saved_session,
2451 session_data,
2452 session_data_len ) ) != 0 )
2453 {
2454 mbedtls_printf( " failed\n ! mbedtls_ssl_session_load returned -0x%x\n\n",
2455 -ret );
2456 goto exit;
2457 }
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02002458 }
2459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002461 {
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02002462 mbedtls_printf( " failed\n ! mbedtls_ssl_set_session returned -0x%x\n\n",
2463 -ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002464 goto exit;
2465 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002466
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002467 if( ( ret = mbedtls_net_connect( &server_fd,
2468 opt.server_addr, opt.server_port,
2469 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2470 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002471 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002472 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
2473 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002474 goto exit;
2475 }
2476
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002477 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002478 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002479 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002480 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002481 if( ret != 0 )
2482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002484 -ret );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002485 goto exit;
2486 }
2487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002489 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002490 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002491 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002492 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002493 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002494 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2495 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002496 goto exit;
2497 }
2498 }
2499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002501
2502 goto send_request;
2503 }
2504
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002505 /*
2506 * Cleanup and exit
2507 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002508exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00002510 if( ret != 0 )
2511 {
2512 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 mbedtls_strerror( ret, error_buf, 100 );
2514 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002515 }
2516#endif
2517
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002518 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520#if defined(MBEDTLS_X509_CRT_PARSE_C)
2521 mbedtls_x509_crt_free( &clicert );
2522 mbedtls_x509_crt_free( &cacert );
2523 mbedtls_pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02002524#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525 mbedtls_ssl_session_free( &saved_session );
2526 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002527 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 mbedtls_ctr_drbg_free( &ctr_drbg );
2529 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnard4bb1b992019-05-24 10:26:41 +02002530 if( session_data != NULL )
2531 mbedtls_platform_zeroize( session_data, session_data_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02002532 mbedtls_free( session_data );
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
Paul Bakkercce9d772011-11-18 14:26:47 +00002534#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 fflush( stdout ); getchar();
2537#endif
2538
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002539 // Shell can not handle large exit numbers -> 1 for errors
2540 if( ret < 0 )
2541 ret = 1;
2542
Paul Bakker5121ce52009-01-03 21:22:43 +00002543 return( ret );
2544}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
2546 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002547 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */