blob: 207b0a2260772f17751c7afebfb6462d92ac4425 [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) || \
Hanno Becker7f1c8052019-08-26 13:30:13 +010046 !defined(MBEDTLS_SSL_TLS_C) || \
47 !defined(MBEDTLS_SSL_CLI_C) || \
48 !defined(MBEDTLS_NET_C) || \
49 !( defined(MBEDTLS_CTR_DRBG_C) || defined(MBEDTLS_HMAC_DRBG_C) )
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020050int main( void )
51{
52 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
53 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
Hanno Becker7f1c8052019-08-26 13:30:13 +010054 "MBEDTLS_NET_C not defined, or "
55 "neither MBEDTLS_CTR_DRBG_C nor MBEDTLS_HMAC_DRBG_C defined.\n");
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020056 return( 0 );
57}
58#else
59
Andres AG788aa4a2016-09-14 14:32:09 +010060#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/ssl.h"
Hanno Becker473f98f2019-06-26 10:27:32 +010062#include "mbedtls/ssl_ciphersuites.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/entropy.h"
64#include "mbedtls/ctr_drbg.h"
Hanno Becker7f1c8052019-08-26 13:30:13 +010065#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/certs.h"
67#include "mbedtls/x509.h"
68#include "mbedtls/error.h"
69#include "mbedtls/debug.h"
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020070#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Hanno Becker7bcf2b52019-07-26 09:02:40 +010072#include "mbedtls/ssl_internal.h"
73
Rich Evans18b78c72015-02-11 14:06:19 +000074#include <stdio.h>
75#include <stdlib.h>
76#include <string.h>
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010077
Hanno Beckere4ad3e82017-09-18 15:05:46 +010078#define MAX_REQUEST_SIZE 20000
79#define MAX_REQUEST_SIZE_STR "20000"
80
Paul Bakker9caf2d22010-02-18 19:37:19 +000081#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010082#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020083#define DFL_SERVER_PORT "4433"
Paul Bakker9caf2d22010-02-18 19:37:19 +000084#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +020085#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +000086#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010087#define DFL_NBIO 0
Hanno Becker16970d22017-10-10 15:56:37 +010088#define DFL_EVENT 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020089#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +020090#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +000091#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000092#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000093#define DFL_CRT_FILE ""
94#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020095#define DFL_PSK ""
96#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +020097#define DFL_ECJPAKE_PW NULL
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +020098#define DFL_EC_MAX_OPS -1
Paul Bakker51936882011-02-20 16:05:58 +000099#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100101#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100102#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200103#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200104#define DFL_MIN_VERSION -1
Paul Bakker1d29fb52012-09-28 13:28:45 +0000105#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000106#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +0200107#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100108#define DFL_AUTH_MODE -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100110#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100111#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200112#define DFL_DHMLEN -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200113#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100114#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +0200115#define DFL_RECO_MODE 1
Hanno Becker7a7aa192019-04-09 17:24:19 +0100116#define DFL_CID_ENABLED 0
117#define DFL_CID_VALUE ""
Hanno Becker96870292019-05-03 17:30:59 +0100118#define DFL_CID_ENABLED_RENEGO -1
119#define DFL_CID_VALUE_RENEGO NULL
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200120#define DFL_RECONNECT_HARD 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200122#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100123#define DFL_CURVES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200125#define DFL_HS_TO_MIN 0
126#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200127#define DFL_DTLS_MTU -1
Hanno Becker4d615912018-08-14 13:33:30 +0100128#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200129#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200130#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100131#define DFL_ETM -1
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +0300132#define DFL_SERIALIZE 0
Jarno Lamsa41b35912019-06-10 15:51:11 +0300133#define DFL_EXTENDED_MS_ENFORCE -1
Paul Bakker0e6975b2009-02-10 22:19:10 +0000134
Paul Bakker93c32b22014-04-25 13:40:05 +0200135#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
136#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_X509_CRT_PARSE_C)
139#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000140#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100141 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
142 " default: \"\" (pre-loaded)\n" \
Hanno Becker1ce1a512019-05-01 11:16:28 +0100143 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100144 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
145 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
Hanno Becker1ce1a512019-05-01 11:16:28 +0100146 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100147 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
148 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000149 " key_file=%%s default: \"\" (pre-loaded)\n"
150#else
151#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 " No file operations available (MBEDTLS_FS_IO not defined)\n"
153#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200154#else
155#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200157
Hanno Beckera5a2b082019-05-15 14:03:01 +0100158#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7a7aa192019-04-09 17:24:19 +0100159#define USAGE_CID \
160 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
161 " default: 0 (disabled)\n" \
Hanno Becker96870292019-05-03 17:30:59 +0100162 " 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 +0100163 " default: same as 'cid' parameter\n" \
Hanno Becker7a7aa192019-04-09 17:24:19 +0100164 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
Hanno Becker96870292019-05-03 17:30:59 +0100165 " default: \"\"\n" \
166 " 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 +0100167 " default: same as 'cid_val' parameter\n"
Hanno Beckera5a2b082019-05-15 14:03:01 +0100168#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100169#define USAGE_CID ""
Hanno Beckera5a2b082019-05-15 14:03:01 +0100170#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200173#define USAGE_PSK \
174 " psk=%%s default: \"\" (in hex, without 0x)\n" \
175 " psk_identity=%%s default: \"Client_identity\"\n"
176#else
177#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200181#define USAGE_TICKETS \
182 " tickets=%%d default: 1 (enabled)\n"
183#else
184#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200188#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100189 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200190#else
191#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200195#define USAGE_MAX_FRAG_LEN \
196 " max_frag_len=%%d default: 16384 (tls default)\n" \
197 " options: 512, 1024, 2048, 4096\n"
198#else
199#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100203#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200204 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100205#else
206#define USAGE_RECSPLIT
207#endif
208
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200209#if defined(MBEDTLS_DHM_C)
210#define USAGE_DHMLEN \
211 " dhmlen=%%d default: (library default: 1024 bits)\n"
212#else
213#define USAGE_DHMLEN
214#endif
215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200217#define USAGE_ALPN \
218 " alpn=%%s default: \"\" (disabled)\n" \
219 " example: spdy/1,http/1.1\n"
220#else
221#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200223
Hanno Beckerc1096e72019-06-19 12:30:41 +0100224#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Hanno Beckere6706e62017-05-15 16:05:15 +0100225#define USAGE_CURVES \
226 " curves=a,b,c,d default: \"default\" (library default)\n" \
227 " example: \"secp521r1,brainpoolP512r1\"\n" \
228 " - use \"none\" for empty list\n" \
229 " - see mbedtls_ecp_curve_list()\n" \
230 " for acceptable curve names\n"
231#else
232#define USAGE_CURVES ""
233#endif
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200236#define USAGE_DTLS \
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +0100237 " dtls=%%d default: 0 (TLS) (if both enabled)\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200238 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200239 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Becker4d615912018-08-14 13:33:30 +0100240 " mtu=%%d default: (library default: unlimited)\n" \
241 " dgram_packing=%%d default: 1 (allowed)\n" \
242 " allow or forbid packing of multiple\n" \
243 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200244#else
245#define USAGE_DTLS ""
246#endif
247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200249#define USAGE_FALLBACK \
250 " fallback=0/1 default: (library default: off)\n"
251#else
252#define USAGE_FALLBACK ""
253#endif
254
Hanno Beckerf765ce62019-06-21 13:17:14 +0100255#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
256 !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET) && \
257 !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200258#define USAGE_EMS \
Jarno Lamsa41b35912019-06-10 15:51:11 +0300259 " extended_ms=0/1 default: (library default: on)\n" \
260 " enforce_extended_master_secret=0/1 default: (library default: off)\n"
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200261#else
262#define USAGE_EMS ""
263#endif
264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100266#define USAGE_ETM \
267 " etm=0/1 default: (library default: on)\n"
268#else
269#define USAGE_ETM ""
270#endif
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100273#define USAGE_RENEGO \
274 " renegotiation=%%d default: 0 (disabled)\n" \
275 " renegotiate=%%d default: 0 (disabled)\n"
276#else
277#define USAGE_RENEGO ""
278#endif
279
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200280#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
281#define USAGE_ECJPAKE \
282 " ecjpake_pw=%%s default: none (disabled)\n"
283#else
284#define USAGE_ECJPAKE ""
285#endif
286
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200287#if defined(MBEDTLS_ECP_RESTARTABLE)
288#define USAGE_ECRESTART \
289 " ec_max_ops=%%s default: library default (restart disabled)\n"
290#else
291#define USAGE_ECRESTART ""
292#endif
293
Jarno Lamsacf1b6722019-06-04 11:06:31 +0300294#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
295#define USAGE_SERIALIZATION \
Jarno Lamsab5ff6a42019-06-06 10:40:52 +0300296 " serialize=%%d default: 0 (do not serialize/deserialize)\n" \
Jarno Lamsa85c23802019-06-07 08:39:24 +0300297 " options: 1 (serialize)\n" \
298 " 2 (serialize with re-initialization)\n"
Jarno Lamsacf1b6722019-06-04 11:06:31 +0300299#else
300#define USAGE_SERIALIZATION ""
301#endif
302
Hanno Beckeracd4fc02019-06-12 16:40:50 +0100303#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
304#define USAGE_AUTH_MODE \
305 " auth_mode=%%s default: (library default: none)\n" \
306 " options: none, optional, required\n"
307#else
308#define USAGE_AUTH_MODE ""
309#endif
310
Hanno Beckerb0b2b672019-06-12 16:58:10 +0100311#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
312#define USAGE_ALLOW_LEGACY_RENEGO " allow_legacy=%%d default: (library default: no)\n"
313#else
314#define USAGE_ALLOW_LEGACY_RENEGO ""
315#endif
316
Hanno Becker1f835fa2019-06-13 10:14:59 +0100317#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
318#define USAGE_READ_TIMEOUT \
319 " read_timeout=%%d default: 0 ms (no timeout)\n"
320#else
321#define USAGE_READ_TIMEOUT ""
322#endif
323
Hanno Becker72e5ffc2019-07-05 11:35:08 +0100324#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
325 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) || \
326 !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
327 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
328#define USAGE_MAX_VERSION " max_version=%%s default: (library default: tls1_2)\n"
329#define USAGE_MIN_VERSION " min_version=%%s default: (library default: tls1)\n"
330#define USAGE_FORCE_VERSION " force_version=%%s default: \"\" (none)\n" \
331 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n"
332#else
333#define USAGE_MAX_VERSION ""
334#define USAGE_MIN_VERSION ""
335#define USAGE_FORCE_VERSION ""
336#endif
337
Paul Bakker9caf2d22010-02-18 19:37:19 +0000338#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000339 "\n usage: ssl_client2 param=<>...\n" \
340 "\n acceptable parameters:\n" \
341 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100342 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000343 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000344 " request_page=%%s default: \".\"\n" \
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +0100345 " request_size=%%d default: about 34 (basic request)\n" \
346 " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
347 " If 0, in the first exchange only an empty\n" \
348 " application data message is sent followed by\n" \
349 " a second non-empty message before attempting\n" \
350 " to read a response from the server\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100351 " debug_level=%%d default: 0 (disabled)\n" \
352 " nbio=%%d default: 0 (blocking I/O)\n" \
353 " options: 1 (non-blocking), 2 (added delays)\n" \
354 " event=%%d default: 0 (loop)\n" \
355 " options: 1 (level-triggered, implies nbio=1),\n" \
Hanno Becker1f835fa2019-06-13 10:14:59 +0100356 USAGE_READ_TIMEOUT \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200357 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100358 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200359 USAGE_DTLS \
Hanno Becker7a7aa192019-04-09 17:24:19 +0100360 USAGE_CID \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200361 "\n" \
Hanno Beckeracd4fc02019-06-12 16:40:50 +0100362 USAGE_AUTH_MODE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100363 USAGE_IO \
364 "\n" \
365 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200366 USAGE_ECJPAKE \
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200367 USAGE_ECRESTART \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100368 "\n" \
Hanno Beckerb0b2b672019-06-12 16:58:10 +0100369 USAGE_ALLOW_LEGACY_RENEGO \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100370 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200371 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +0200372 " reconnect=%%d number of reconnections using session resumption\n" \
373 " default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200374 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +0200375 " reco_mode=%%d 0: copy session, 1: serialize session\n" \
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +0200376 " default: 1\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200377 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200378 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100379 USAGE_MAX_FRAG_LEN \
380 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200381 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200382 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200383 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100384 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100385 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100386 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200387 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000388 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100389 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200390 " allow_sha1=%%d default: 0\n" \
Hanno Becker72e5ffc2019-07-05 11:35:08 +0100391 USAGE_MIN_VERSION \
392 USAGE_MAX_VERSION \
393 USAGE_FORCE_VERSION \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000394 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000395 " force_ciphersuite=<name> default: all enabled\n"\
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100396 " query_config=<name> return 0 if the specified\n" \
397 " configuration macro is defined and 1\n" \
398 " otherwise. The expansion of the macro\n" \
399 " is printed if it is defined\n" \
Jarno Lamsacf1b6722019-06-04 11:06:31 +0300400 USAGE_SERIALIZATION \
Paul Bakker51936882011-02-20 16:05:58 +0000401 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000402
Hanno Becker8651a432017-06-09 16:13:22 +0100403#define ALPN_LIST_SIZE 10
404#define CURVE_LIST_SIZE 20
405
Simon Butcher63cb97e2018-12-06 17:43:31 +0000406
Rich Evans85b05ec2015-02-12 11:37:29 +0000407/*
408 * global options
409 */
410struct options
411{
412 const char *server_name; /* hostname of the server (client only) */
413 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200414 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000415 int debug_level; /* level of debugging */
416 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100417 int event; /* loop or event-driven IO? level or edge triggered? */
418 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000419 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000420 const char *request_page; /* page on server to request */
421 int request_size; /* pad request with header to requested size */
422 const char *ca_file; /* the file with the CA certificate(s) */
423 const char *ca_path; /* the path with the CA certificate(s) reside */
424 const char *crt_file; /* the file with the client certificate */
425 const char *key_file; /* the file with the client key */
426 const char *psk; /* the pre-shared key */
427 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200428 const char *ecjpake_pw; /* the EC J-PAKE password */
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200429 int ec_max_ops; /* EC consecutive operations limit */
Rich Evans85b05ec2015-02-12 11:37:29 +0000430 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
431 int renegotiation; /* enable / disable renegotiation */
432 int allow_legacy; /* allow legacy renegotiation */
433 int renegotiate; /* attempt renegotiation? */
434 int renego_delay; /* delay before enforcing renegotiation */
435 int exchanges; /* number of data exchanges */
436 int min_version; /* minimum protocol version accepted */
437 int max_version; /* maximum protocol version accepted */
438 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200439 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000440 int auth_mode; /* verify mode for connection */
441 unsigned char mfl_code; /* code for maximum fragment length */
442 int trunc_hmac; /* negotiate truncated hmac or not */
443 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200444 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000445 int reconnect; /* attempt to resume session */
446 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +0200447 int reco_mode; /* how to keep the session around */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200448 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000449 int tickets; /* enable / disable session tickets */
Hanno Beckere6706e62017-05-15 16:05:15 +0100450 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000451 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000452 int transport; /* TLS or DTLS? */
453 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
454 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200455 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Rich Evans85b05ec2015-02-12 11:37:29 +0000456 int fallback; /* is this a fallback connection? */
Hanno Becker4d615912018-08-14 13:33:30 +0100457 int dgram_packing; /* allow/forbid datagram packing */
Rich Evans85b05ec2015-02-12 11:37:29 +0000458 int extended_ms; /* negotiate extended master secret? */
Jarno Lamsa41b35912019-06-10 15:51:11 +0300459 int enforce_extended_master_secret; /* Enforce the usage of extended
460 * master secret */
Rich Evans85b05ec2015-02-12 11:37:29 +0000461 int etm; /* negotiate encrypt then mac? */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100462 int cid_enabled; /* whether to use the CID extension or not */
Hanno Becker96870292019-05-03 17:30:59 +0100463 int cid_enabled_renego; /* whether to use the CID extension or not
464 * during renegotiation */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100465 const char *cid_val; /* the CID to use for incoming messages */
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +0300466 int serialize; /* serialize/deserialize connection */
Hanno Becker96870292019-05-03 17:30:59 +0100467 const char *cid_val_renego; /* the CID to use for incoming messages
468 * after renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000469} opt;
470
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100471int query_config( const char *config );
472
Hanno Becker14a4a442019-07-02 17:00:34 +0100473#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200474static void my_debug( void *ctx, int level,
475 const char *file, int line,
476 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000477{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200478 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000479
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200480 /* Extract basename from file */
481 for( p = basename = file; *p != '\0'; p++ )
482 if( *p == '/' || *p == '\\' )
483 basename = p + 1;
484
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100485 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s",
486 basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000487 fflush( (FILE *) ctx );
488}
Hanno Becker14a4a442019-07-02 17:00:34 +0100489#endif /* MBEDTLS_DEBUG_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000490
Hanno Beckera58a8962019-06-13 16:11:15 +0100491
492#if !defined(MBEDTLS_SSL_CONF_RECV) && \
493 !defined(MBEDTLS_SSL_CONF_SEND) && \
494 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Rich Evans85b05ec2015-02-12 11:37:29 +0000495/*
496 * Test recv/send functions that make sure each try returns
497 * WANT_READ/WANT_WRITE at least once before sucesseding
498 */
Hanno Becker14219fe2019-07-03 17:02:43 +0100499
500static int delayed_recv( void *ctx, unsigned char *buf, size_t len )
Rich Evans85b05ec2015-02-12 11:37:29 +0000501{
502 static int first_try = 1;
503 int ret;
504
505 if( first_try )
506 {
507 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100508 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000509 }
510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100512 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000513 first_try = 1; /* Next call will be a new operation */
514 return( ret );
515}
516
Hanno Becker14219fe2019-07-03 17:02:43 +0100517static int delayed_send( void *ctx, const unsigned char *buf, size_t len )
Rich Evans85b05ec2015-02-12 11:37:29 +0000518{
519 static int first_try = 1;
520 int ret;
521
522 if( first_try )
523 {
524 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100525 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000526 }
527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100529 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000530 first_try = 1; /* Next call will be a new operation */
531 return( ret );
532}
533
Hanno Becker14219fe2019-07-03 17:02:43 +0100534typedef struct
535{
536 mbedtls_ssl_context *ssl;
537 mbedtls_net_context *net;
538} io_ctx_t;
539
Hanno Beckerde9e36e2019-07-03 17:14:41 +0100540#if defined(MBEDTLS_SSL_RECORD_CHECKING)
541static int ssl_check_record( mbedtls_ssl_context const *ssl,
542 unsigned char const *buf, size_t len )
543{
544 int ret;
545 unsigned char *tmp_buf;
546
547 tmp_buf = mbedtls_calloc( 1, len );
548 if( tmp_buf == NULL )
549 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
550 memcpy( tmp_buf, buf, len );
551
552 ret = mbedtls_ssl_check_record( ssl, tmp_buf, len );
553 if( ret != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE )
554 {
555 int ret_repeated;
556
557 /* Test-only: Make sure that mbedtls_ssl_check_record()
558 * doesn't alter state. */
559 memcpy( tmp_buf, buf, len ); /* Restore buffer */
560 ret_repeated = mbedtls_ssl_check_record( ssl, tmp_buf, len );
561 if( ret != ret_repeated )
562 {
Hanno Becker83b8c3b2019-07-24 13:59:07 +0100563 mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" );
564 return( -1 );
Hanno Beckerde9e36e2019-07-03 17:14:41 +0100565 }
566
567 switch( ret )
568 {
569 case 0:
570 break;
571
572 case MBEDTLS_ERR_SSL_INVALID_RECORD:
573 if( opt.debug_level > 1 )
574 mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" );
575 break;
576
577 case MBEDTLS_ERR_SSL_INVALID_MAC:
578 if( opt.debug_level > 1 )
579 mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" );
580 break;
581
582 case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD:
583 if( opt.debug_level > 1 )
584 mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" );
585 break;
586
587 default:
588 mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", -ret );
589 return( -1 );
590 }
591
592 /* Regardless of the outcome, forward the record to the stack. */
593 }
594
Hanno Beckerde9e36e2019-07-03 17:14:41 +0100595 mbedtls_free( tmp_buf );
596
597 return( 0 );
598}
599#endif /* MBEDTLS_SSL_RECORD_CHECKING */
600
Hanno Becker14219fe2019-07-03 17:02:43 +0100601static int recv_cb( void *ctx, unsigned char *buf, size_t len )
602{
603 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
604 size_t recv_len;
605 int ret;
606
607 if( opt.nbio == 2 )
608 ret = delayed_recv( io_ctx->net, buf, len );
609 else
610 ret = mbedtls_net_recv( io_ctx->net, buf, len );
611 if( ret < 0 )
612 return( ret );
613 recv_len = (size_t) ret;
614
615 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
616 {
617 /* Here's the place to do any datagram/record checking
618 * in between receiving the packet from the underlying
619 * transport and passing it on to the TLS stack. */
Hanno Beckerde9e36e2019-07-03 17:14:41 +0100620#if defined(MBEDTLS_SSL_RECORD_CHECKING)
621 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
622 return( -1 );
623#endif /* MBEDTLS_SSL_RECORD_CHECKING */
Hanno Becker14219fe2019-07-03 17:02:43 +0100624 }
625
626 return( (int) recv_len );
627}
628
629static int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len,
630 uint32_t timeout )
631{
632 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
633 int ret;
634 size_t recv_len;
635
636 ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout );
637 if( ret < 0 )
638 return( ret );
639 recv_len = (size_t) ret;
640
641 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
642 {
643 /* Here's the place to do any datagram/record checking
644 * in between receiving the packet from the underlying
645 * transport and passing it on to the TLS stack. */
Hanno Beckerde9e36e2019-07-03 17:14:41 +0100646#if defined(MBEDTLS_SSL_RECORD_CHECKING)
647 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
648 return( -1 );
649#endif /* MBEDTLS_SSL_RECORD_CHECKING */
Hanno Becker14219fe2019-07-03 17:02:43 +0100650 }
651
652 return( (int) recv_len );
653}
654
655static int send_cb( void *ctx, unsigned char const *buf, size_t len )
656{
657 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
658
659 if( opt.nbio == 2 )
660 return( delayed_send( io_ctx->net, buf, len ) );
661
662 return( mbedtls_net_send( io_ctx->net, buf, len ) );
663}
Manuel Pégourié-Gonnard2b29a372019-07-30 17:07:38 +0200664#endif /* !MBEDTLS_SSL_CONF_RECV &&
665 !MBEDTLS_SSL_CONF_SEND &&
666 !MBEDTLS_SSL_CONF_RECV_TIMEOUT */
Hanno Becker14219fe2019-07-03 17:02:43 +0100667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100669
670#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Hanno Beckerf9ca30d2019-02-26 11:38:29 +0000671static unsigned char peer_crt_info[1024];
Hanno Becker975c4632019-02-25 17:43:18 +0000672
Rich Evans85b05ec2015-02-12 11:37:29 +0000673/*
674 * Enabled if debug_level > 1 in code below
675 */
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100676static int my_verify( void *data, mbedtls_x509_crt *crt,
677 int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000678{
Hanno Becker02a21932019-06-10 15:08:43 +0100679#if !defined(MBEDTLS_X509_REMOVE_INFO)
Rich Evans85b05ec2015-02-12 11:37:29 +0000680 char buf[1024];
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600681#endif
Rich Evans85b05ec2015-02-12 11:37:29 +0000682 ((void) data);
683
Hanno Becker02a21932019-06-10 15:08:43 +0100684#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Hanno Becker975c4632019-02-25 17:43:18 +0000686 if( depth == 0 )
687 memcpy( peer_crt_info, buf, sizeof( buf ) );
688
689 if( opt.debug_level == 0 )
690 return( 0 );
691
692 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_printf( "%s", buf );
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600694#else
695 ((void) crt);
696 ((void) depth);
697#endif
Rich Evans85b05ec2015-02-12 11:37:29 +0000698
Rich Evans85b05ec2015-02-12 11:37:29 +0000699 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100701 else
702 {
Hanno Becker02a21932019-06-10 15:08:43 +0100703#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100704 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
705 mbedtls_printf( "%s\n", buf );
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600706#endif
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100707 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000708
709 return( 0 );
710}
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100711#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200712
Hanno Becker7f1c8052019-08-26 13:30:13 +0100713#endif /* MBEDTLS_X509_CRT_PARSE_C */
714
Hanno Becker10da2a32019-08-28 15:08:27 +0100715#if ( defined(MBEDTLS_X509_CRT_PARSE_C) && \
716 !defined(MBEDTLS_SSL_CONF_SINGLE_HASH) ) || \
717 !defined(MBEDTLS_CTR_DRBG_C)
Hanno Becker7f1c8052019-08-26 13:30:13 +0100718static int available_hashes[] = {
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200719#if defined(MBEDTLS_SHA512_C)
720 MBEDTLS_MD_SHA512,
721 MBEDTLS_MD_SHA384,
722#endif
723#if defined(MBEDTLS_SHA256_C)
724 MBEDTLS_MD_SHA256,
725 MBEDTLS_MD_SHA224,
726#endif
727#if defined(MBEDTLS_SHA1_C)
728 /* Allow SHA-1 as we use it extensively in tests. */
729 MBEDTLS_MD_SHA1,
730#endif
731 MBEDTLS_MD_NONE
732};
Hanno Becker10da2a32019-08-28 15:08:27 +0100733#endif /* ( MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_CONF_SINGLE_HASH ) ||
734 !MBEDTLS_CTR_DRBG_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000735
Hanno Becker16970d22017-10-10 15:56:37 +0100736/*
737 * Wait for an event from the underlying transport or the timer
738 * (Used in event-driven IO mode).
739 */
740#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000741int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000742 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100743#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000744int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000745 mbedtls_timing_delay_context *timer,
746 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100747#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000748{
Hanno Becker16970d22017-10-10 15:56:37 +0100749
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000750 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +0100751 int poll_type = 0;
752
753 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
754 poll_type = MBEDTLS_NET_POLL_WRITE;
755 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
756 poll_type = MBEDTLS_NET_POLL_READ;
757#if !defined(MBEDTLS_TIMING_C)
758 else
Hanno Beckeref527962018-03-15 15:49:24 +0000759 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100760#endif
761
762 while( 1 )
763 {
Hanno Becker197a91c2017-10-31 10:58:53 +0000764 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +0100765#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +0000766 if( timer != NULL &&
767 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +0100768 {
Hanno Becker16970d22017-10-10 15:56:37 +0100769 break;
770 }
Hanno Becker197a91c2017-10-31 10:58:53 +0000771#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +0100772
Hanno Becker197a91c2017-10-31 10:58:53 +0000773 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000774 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +0100775 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000776 ret = mbedtls_net_poll( fd, poll_type, 0 );
777 if( ret < 0 )
778 return( ret );
779 if( ret == poll_type )
780 break;
Hanno Becker16970d22017-10-10 15:56:37 +0100781 }
782 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000783
784 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100785}
786
Hanno Beckerec370302019-04-09 17:12:56 +0100787/* Unhexify `hex` into `dst`. `dst` must have
788 * size at least `strlen( hex ) / 2`. */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100789int unhexify( char const *hex, unsigned char *dst )
Hanno Beckerec370302019-04-09 17:12:56 +0100790{
791 unsigned char c;
792 size_t j;
793 size_t len = strlen( hex );
794
795 if( len % 2 != 0 )
796 return( -1 );
797
798 for( j = 0; j < len; j += 2 )
799 {
800 c = hex[j];
801 if( c >= '0' && c <= '9' )
802 c -= '0';
803 else if( c >= 'a' && c <= 'f' )
804 c -= 'a' - 10;
805 else if( c >= 'A' && c <= 'F' )
806 c -= 'A' - 10;
807 else
808 return( -1 );
809 dst[ j / 2 ] = c << 4;
810
811 c = hex[j + 1];
812 if( c >= '0' && c <= '9' )
813 c -= '0';
814 else if( c >= 'a' && c <= 'f' )
815 c -= 'a' - 10;
816 else if( c >= 'A' && c <= 'F' )
817 c -= 'A' - 10;
818 else
819 return( -1 );
820 dst[ j / 2 ] |= c;
821 }
822
823 return( 0 );
824}
825
Hanno Beckera5a2b082019-05-15 14:03:01 +0100826#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +0100827int report_cid_usage( mbedtls_ssl_context *ssl,
828 const char *additional_description )
829{
830 int ret;
831 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
832 size_t peer_cid_len;
833 int cid_negotiated;
834
835 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
836 return( 0 );
837
Hanno Beckerac363882019-05-22 16:59:25 +0100838 /* Check if the use of a CID has been negotiated,
839 * but don't ask for the CID value and length.
840 *
841 * Note: Here and below, we're demonstrating the various ways
842 * in which mbedtls_ssl_get_peer_cid() can be called,
843 * depending on whether or not the length/value of the
844 * peer's CID is needed.
845 *
846 * An actual application, however, should use
847 * just one call to mbedtls_ssl_get_peer_cid(). */
Hanno Becker96870292019-05-03 17:30:59 +0100848 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
Hanno Beckerac363882019-05-22 16:59:25 +0100849 NULL, NULL );
Hanno Becker96870292019-05-03 17:30:59 +0100850 if( ret != 0 )
851 {
852 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
853 -ret );
854 return( ret );
855 }
856
857 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
858 {
859 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
860 {
861 mbedtls_printf( "(%s) Use of Connection ID was rejected by the server.\n",
862 additional_description );
863 }
864 }
865 else
866 {
867 size_t idx=0;
868 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
869 additional_description );
Hanno Beckerac363882019-05-22 16:59:25 +0100870
871 /* Ask for just the length of the peer's CID. */
872 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
873 NULL, &peer_cid_len );
874 if( ret != 0 )
875 {
876 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
877 -ret );
878 return( ret );
879 }
880
881 /* Ask for just length + value of the peer's CID. */
882 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
883 peer_cid, &peer_cid_len );
884 if( ret != 0 )
885 {
886 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
887 -ret );
888 return( ret );
889 }
Hanno Becker96870292019-05-03 17:30:59 +0100890 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
891 additional_description,
892 (unsigned) peer_cid_len );
893 while( idx < peer_cid_len )
894 {
895 mbedtls_printf( "%02x ", peer_cid[ idx ] );
896 idx++;
897 }
898 mbedtls_printf( "\n" );
899 }
900
901 return( 0 );
902}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100903#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker96870292019-05-03 17:30:59 +0100904
Hanno Becker572d4482019-07-23 13:47:53 +0100905#if defined(MBEDTLS_SSL_CONF_RNG)
906int rng_wrap( void *ctx, unsigned char *dst, size_t len );
907
Hanno Becker7f1c8052019-08-26 13:30:13 +0100908#if defined(MBEDTLS_CTR_DRBG_C)
Hanno Becker572d4482019-07-23 13:47:53 +0100909mbedtls_ctr_drbg_context *rng_ctx_global = NULL;
Hanno Becker7f1c8052019-08-26 13:30:13 +0100910#else
911mbedtls_hmac_drbg_context *rng_ctx_global = NULL;
912#endif /* MBEDTLS_CTR_DRBG_C */
913
Hanno Becker572d4482019-07-23 13:47:53 +0100914int rng_wrap( void *ctx, unsigned char *dst, size_t len )
915{
916 /* We expect the NULL parameter here. */
917 if( ctx != NULL )
918 return( -1 );
919
Hanno Becker7f1c8052019-08-26 13:30:13 +0100920#if defined(MBEDTLS_CTR_DRBG_C)
Hanno Becker572d4482019-07-23 13:47:53 +0100921 return( mbedtls_ctr_drbg_random( rng_ctx_global, dst, len ) );
Hanno Becker7f1c8052019-08-26 13:30:13 +0100922#else
923 return( mbedtls_hmac_drbg_random( rng_ctx_global, dst, len ) );
924#endif
Hanno Becker572d4482019-07-23 13:47:53 +0100925}
926#endif /* MBEDTLS_SSL_CONF_RNG */
927
Jarno Lamsa642596e2019-10-04 12:52:42 +0300928#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
929int mbedtls_hardware_poll( void *data, unsigned char *output,
930 size_t len, size_t *olen )
931{
932 size_t i;
933 (void) data;
934 for( i = 0; i < len; ++i )
935 output[i] = rand();
936 *olen = len;
937 return( 0 );
938}
939#endif
940
Paul Bakker9caf2d22010-02-18 19:37:19 +0000941int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000942{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200943 int ret = 0, len, tail_len, i, written, frags, retry_left;
944 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnard2b29a372019-07-30 17:07:38 +0200945#if !defined(MBEDTLS_SSL_CONF_RECV) && \
946 !defined(MBEDTLS_SSL_CONF_SEND) && \
947 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Hanno Becker14219fe2019-07-03 17:02:43 +0100948 io_ctx_t io_ctx;
Manuel Pégourié-Gonnard2b29a372019-07-30 17:07:38 +0200949#endif
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100950
Teppo Järvelin8e0e4812019-10-21 10:33:11 +0300951 unsigned char *buf = NULL;
952 unsigned int main_buf_len = 0;
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
955 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200956 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200957#endif
Hanno Becker7a7aa192019-04-09 17:24:19 +0100958
Hanno Beckera5a2b082019-05-15 14:03:01 +0100959#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7a7aa192019-04-09 17:24:19 +0100960 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker96870292019-05-03 17:30:59 +0100961 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker7a7aa192019-04-09 17:24:19 +0100962 size_t cid_len = 0;
Hanno Becker96870292019-05-03 17:30:59 +0100963 size_t cid_renego_len = 0;
Hanno Becker7a7aa192019-04-09 17:24:19 +0100964#endif
965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100967 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200968#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +0100969#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Hanno Becker8651a432017-06-09 16:13:22 +0100970 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100971 const mbedtls_ecp_curve_info *curve_cur;
972#endif
973
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200974 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000975
Gilles Peskineef86ab22017-05-05 18:59:02 +0200976#if defined(MBEDTLS_X509_CRT_PARSE_C)
977 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
978#endif
Hanno Becker7d864c42019-09-19 16:51:41 +0100979 mbedtls_entropy_context *entropy = NULL;
Hanno Becker7f1c8052019-08-26 13:30:13 +0100980#if defined(MBEDTLS_CTR_DRBG_C)
Hanno Becker7d864c42019-09-19 16:51:41 +0100981 mbedtls_ctr_drbg_context *ctr_drbg = NULL;
Hanno Becker7f1c8052019-08-26 13:30:13 +0100982#else
Hanno Becker7d864c42019-09-19 16:51:41 +0100983 mbedtls_hmac_drbg_context *hmac_drbg = NULL;
Hanno Becker7f1c8052019-08-26 13:30:13 +0100984#endif
Hanno Becker7d864c42019-09-19 16:51:41 +0100985 mbedtls_ssl_context *ssl;
986 mbedtls_ssl_config *conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +0200988 unsigned char *session_data = NULL;
989 size_t session_data_len = 0;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200990#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200991 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200992#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200994 uint32_t flags;
Hanno Becker7d864c42019-09-19 16:51:41 +0100995 mbedtls_x509_crt *cacert = NULL;
996 mbedtls_x509_crt *clicert = NULL;
997 mbedtls_pk_context *pkey = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +0200998#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000999 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +00001000 const int *list;
Manuel Pégourié-Gonnardc9812292019-07-15 10:31:11 +02001001#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1002 unsigned char *context_buf = NULL;
1003 size_t context_buf_len;
1004#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +00001005
Hanno Becker7d864c42019-09-19 16:51:41 +01001006 ssl = mbedtls_calloc( 1, sizeof( *ssl ) );
1007 conf = mbedtls_calloc( 1, sizeof( *conf ) );
1008 entropy = mbedtls_calloc( 1, sizeof( *entropy ) );
1009#if defined(MBEDTLS_CTR_DRBG_C)
1010 ctr_drbg = mbedtls_calloc( 1, sizeof( *ctr_drbg ) );
1011#else
1012 hmac_drbg = mbedtls_calloc( 1, sizeof( *hmac_drbg ) );
1013#endif
Teppo Järvelin8e0fe192019-10-16 07:43:45 +03001014#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker7d864c42019-09-19 16:51:41 +01001015 cacert = mbedtls_calloc( 1, sizeof( *cacert ) );
1016 clicert = mbedtls_calloc( 1, sizeof( *clicert ) );
1017 pkey = mbedtls_calloc( 1, sizeof( *pkey ) );
Teppo Järvelin8e0fe192019-10-16 07:43:45 +03001018#endif
Hanno Becker7d864c42019-09-19 16:51:41 +01001019
Teppo Järvelin8e0fe192019-10-16 07:43:45 +03001020 if( ssl == NULL || entropy == NULL ||
1021#if defined(MBEDTLS_X509_CRT_PARSE_C)
1022 cacert == NULL ||
1023 clicert== NULL || pkey == NULL ||
1024#endif
Hanno Becker7d864c42019-09-19 16:51:41 +01001025#if defined(MBEDTLS_CTR_DRBG_C)
1026 ctr_drbg == NULL ||
1027#else
1028 hmac_drbg == NULL ||
1029#endif
Teppo Järvelin8e0fe192019-10-16 07:43:45 +03001030
1031 conf == NULL)
Hanno Becker7d864c42019-09-19 16:51:41 +01001032 {
1033 goto exit;
1034 }
1035
Paul Bakker1a207ec2011-02-06 13:22:40 +00001036 /*
1037 * Make sure memory references are valid.
1038 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001039 mbedtls_net_init( &server_fd );
Hanno Becker7d864c42019-09-19 16:51:41 +01001040 mbedtls_ssl_init( ssl );
1041 mbedtls_ssl_config_init( conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Hanno Becker7f1c8052019-08-26 13:30:13 +01001043#if defined(MBEDTLS_CTR_DRBG_C)
Hanno Becker7d864c42019-09-19 16:51:41 +01001044 mbedtls_ctr_drbg_init( ctr_drbg );
Hanno Becker7f1c8052019-08-26 13:30:13 +01001045#else
Hanno Becker7d864c42019-09-19 16:51:41 +01001046 mbedtls_hmac_drbg_init( hmac_drbg );
Hanno Becker7f1c8052019-08-26 13:30:13 +01001047#endif /* MBEDTLS_CTR_DRBG_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker7d864c42019-09-19 16:51:41 +01001049 mbedtls_x509_crt_init( cacert );
1050 mbedtls_x509_crt_init( clicert );
1051 mbedtls_pk_init( pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001052#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +02001054 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001055#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +00001056
Paul Bakker9caf2d22010-02-18 19:37:19 +00001057 if( argc == 0 )
1058 {
1059 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +00001060 if( ret == 0 )
1061 ret = 1;
1062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +00001064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +00001066 while( *list )
1067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001069 list++;
1070 if( !*list )
1071 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +00001073 list++;
1074 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +00001076 goto exit;
1077 }
1078
1079 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001080 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001081 opt.server_port = DFL_SERVER_PORT;
1082 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker7a7aa192019-04-09 17:24:19 +01001083 opt.cid_enabled = DFL_CID_ENABLED;
1084 opt.cid_val = DFL_CID_VALUE;
Hanno Becker96870292019-05-03 17:30:59 +01001085 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
1086 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001087 opt.nbio = DFL_NBIO;
Hanno Becker16970d22017-10-10 15:56:37 +01001088 opt.event = DFL_EVENT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001089 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001090 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001091 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +02001092 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +00001093 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +00001094 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +00001095 opt.crt_file = DFL_CRT_FILE;
1096 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001097 opt.psk = DFL_PSK;
1098 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001099 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001100 opt.ec_max_ops = DFL_EC_MAX_OPS;
Paul Bakker51936882011-02-20 16:05:58 +00001101 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +00001102 opt.renegotiation = DFL_RENEGOTIATION;
1103 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001104 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001105 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001106 opt.min_version = DFL_MIN_VERSION;
1107 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001108 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001109 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001110 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001111 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001112 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001113 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001114 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001115 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001116 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02001117 opt.reco_mode = DFL_RECO_MODE;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001118 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001119 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001120 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001121 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001122 opt.transport = DFL_TRANSPORT;
1123 opt.hs_to_min = DFL_HS_TO_MIN;
1124 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001125 opt.dtls_mtu = DFL_DTLS_MTU;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001126 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001127 opt.extended_ms = DFL_EXTENDED_MS;
Jarno Lamsa41b35912019-06-10 15:51:11 +03001128 opt.enforce_extended_master_secret = DFL_EXTENDED_MS_ENFORCE;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001129 opt.etm = DFL_ETM;
Hanno Becker4d615912018-08-14 13:33:30 +01001130 opt.dgram_packing = DFL_DGRAM_PACKING;
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03001131 opt.serialize = DFL_SERIALIZE;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001132
1133 for( i = 1; i < argc; i++ )
1134 {
Paul Bakker9caf2d22010-02-18 19:37:19 +00001135 p = argv[i];
1136 if( ( q = strchr( p, '=' ) ) == NULL )
1137 goto usage;
1138 *q++ = '\0';
1139
1140 if( strcmp( p, "server_name" ) == 0 )
1141 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001142 else if( strcmp( p, "server_addr" ) == 0 )
1143 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001144 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001145 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001146 else if( strcmp( p, "dtls" ) == 0 )
1147 {
1148 int t = atoi( q );
1149 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001151 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001153 else
1154 goto usage;
1155 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001156 else if( strcmp( p, "debug_level" ) == 0 )
1157 {
1158 opt.debug_level = atoi( q );
1159 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1160 goto usage;
1161 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001162 else if( strcmp( p, "nbio" ) == 0 )
1163 {
1164 opt.nbio = atoi( q );
1165 if( opt.nbio < 0 || opt.nbio > 2 )
1166 goto usage;
1167 }
Hanno Becker16970d22017-10-10 15:56:37 +01001168 else if( strcmp( p, "event" ) == 0 )
1169 {
1170 opt.event = atoi( q );
1171 if( opt.event < 0 || opt.event > 2 )
1172 goto usage;
1173 }
Hanno Becker1f835fa2019-06-13 10:14:59 +01001174#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001175 else if( strcmp( p, "read_timeout" ) == 0 )
1176 opt.read_timeout = atoi( q );
Hanno Becker1f835fa2019-06-13 10:14:59 +01001177#endif
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001178 else if( strcmp( p, "max_resend" ) == 0 )
1179 {
1180 opt.max_resend = atoi( q );
1181 if( opt.max_resend < 0 )
1182 goto usage;
1183 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001184 else if( strcmp( p, "request_page" ) == 0 )
1185 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +02001186 else if( strcmp( p, "request_size" ) == 0 )
1187 {
1188 opt.request_size = atoi( q );
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001189 if( opt.request_size < 0 ||
1190 opt.request_size > MAX_REQUEST_SIZE )
Paul Bakker93c32b22014-04-25 13:40:05 +02001191 goto usage;
1192 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001193 else if( strcmp( p, "ca_file" ) == 0 )
1194 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +00001195 else if( strcmp( p, "ca_path" ) == 0 )
1196 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +00001197 else if( strcmp( p, "crt_file" ) == 0 )
1198 opt.crt_file = q;
1199 else if( strcmp( p, "key_file" ) == 0 )
1200 opt.key_file = q;
Hanno Beckera5a2b082019-05-15 14:03:01 +01001201#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7a7aa192019-04-09 17:24:19 +01001202 else if( strcmp( p, "cid" ) == 0 )
1203 {
1204 opt.cid_enabled = atoi( q );
1205 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
1206 goto usage;
1207 }
Hanno Becker96870292019-05-03 17:30:59 +01001208 else if( strcmp( p, "cid_renego" ) == 0 )
1209 {
1210 opt.cid_enabled_renego = atoi( q );
1211 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
1212 goto usage;
1213 }
Hanno Becker7a7aa192019-04-09 17:24:19 +01001214 else if( strcmp( p, "cid_val" ) == 0 )
1215 {
1216 opt.cid_val = q;
1217 }
Hanno Becker96870292019-05-03 17:30:59 +01001218 else if( strcmp( p, "cid_val_renego" ) == 0 )
1219 {
1220 opt.cid_val_renego = q;
1221 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001222#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001223 else if( strcmp( p, "psk" ) == 0 )
1224 opt.psk = q;
1225 else if( strcmp( p, "psk_identity" ) == 0 )
1226 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001227 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1228 opt.ecjpake_pw = q;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001229 else if( strcmp( p, "ec_max_ops" ) == 0 )
1230 opt.ec_max_ops = atoi( q );
Paul Bakker51936882011-02-20 16:05:58 +00001231 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +00001234
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001235 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +00001236 {
1237 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +00001238 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +00001239 }
Paul Bakker51936882011-02-20 16:05:58 +00001240 opt.force_ciphersuite[1] = 0;
1241 }
Paul Bakker48916f92012-09-16 19:57:18 +00001242 else if( strcmp( p, "renegotiation" ) == 0 )
1243 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001244 opt.renegotiation = (atoi( q )) ?
1245 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1246 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +00001247 }
Hanno Beckerb0b2b672019-06-12 16:58:10 +01001248#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001249 else if( strcmp( p, "allow_legacy" ) == 0 )
1250 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001251 switch( atoi( q ) )
1252 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001253 case -1:
1254 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1255 break;
1256 case 0:
1257 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1258 break;
1259 case 1:
1260 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1261 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001262 default: goto usage;
1263 }
Paul Bakker48916f92012-09-16 19:57:18 +00001264 }
Hanno Beckerb0b2b672019-06-12 16:58:10 +01001265#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001266 else if( strcmp( p, "renegotiate" ) == 0 )
1267 {
1268 opt.renegotiate = atoi( q );
1269 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1270 goto usage;
1271 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001272 else if( strcmp( p, "exchanges" ) == 0 )
1273 {
1274 opt.exchanges = atoi( q );
1275 if( opt.exchanges < 1 )
1276 goto usage;
1277 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001278 else if( strcmp( p, "reconnect" ) == 0 )
1279 {
1280 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001281 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001282 goto usage;
1283 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001284 else if( strcmp( p, "reco_delay" ) == 0 )
1285 {
1286 opt.reco_delay = atoi( q );
1287 if( opt.reco_delay < 0 )
1288 goto usage;
1289 }
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02001290 else if( strcmp( p, "reco_mode" ) == 0 )
1291 {
1292 opt.reco_mode = atoi( q );
1293 if( opt.reco_mode < 0 )
1294 goto usage;
1295 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001296 else if( strcmp( p, "reconnect_hard" ) == 0 )
1297 {
1298 opt.reconnect_hard = atoi( q );
1299 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
1300 goto usage;
1301 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001302 else if( strcmp( p, "tickets" ) == 0 )
1303 {
1304 opt.tickets = atoi( q );
1305 if( opt.tickets < 0 || opt.tickets > 2 )
1306 goto usage;
1307 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001308 else if( strcmp( p, "alpn" ) == 0 )
1309 {
1310 opt.alpn_string = q;
1311 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001312 else if( strcmp( p, "fallback" ) == 0 )
1313 {
1314 switch( atoi( q ) )
1315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
1317 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001318 default: goto usage;
1319 }
1320 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001321 else if( strcmp( p, "extended_ms" ) == 0 )
1322 {
1323 switch( atoi( q ) )
1324 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001325 case 0:
1326 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1327 break;
1328 case 1:
1329 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1330 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001331 default: goto usage;
1332 }
1333 }
Jarno Lamsa41b35912019-06-10 15:51:11 +03001334 else if( strcmp( p, "enforce_extended_master_secret" ) == 0 )
1335 {
1336 switch( atoi( q ) )
1337 {
1338 case 0:
1339 opt.enforce_extended_master_secret =
1340 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
1341 break;
1342 case 1:
1343 opt.enforce_extended_master_secret =
1344 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_ENABLED;
1345 break;
1346 default: goto usage;
1347 }
1348 }
Hanno Beckerc1096e72019-06-19 12:30:41 +01001349#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Hanno Beckere6706e62017-05-15 16:05:15 +01001350 else if( strcmp( p, "curves" ) == 0 )
1351 opt.curves = q;
Hanno Beckerc1096e72019-06-19 12:30:41 +01001352#endif /* !MBEDTLS_SSL_CONF_SINGLE_EC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001353 else if( strcmp( p, "etm" ) == 0 )
1354 {
1355 switch( atoi( q ) )
1356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1358 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001359 default: goto usage;
1360 }
1361 }
Hanno Becker72e5ffc2019-07-05 11:35:08 +01001362#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
1363 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) || \
1364 !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
1365 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Paul Bakker1d29fb52012-09-28 13:28:45 +00001366 else if( strcmp( p, "min_version" ) == 0 )
1367 {
1368 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001370 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001372 else if( strcmp( q, "tls1_1" ) == 0 ||
1373 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001375 else if( strcmp( q, "tls1_2" ) == 0 ||
1376 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001378 else
1379 goto usage;
1380 }
1381 else if( strcmp( p, "max_version" ) == 0 )
1382 {
1383 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001385 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001387 else if( strcmp( q, "tls1_1" ) == 0 ||
1388 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001390 else if( strcmp( q, "tls1_2" ) == 0 ||
1391 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001393 else
1394 goto usage;
1395 }
1396 else if( strcmp( p, "force_version" ) == 0 )
1397 {
1398 if( strcmp( q, "ssl3" ) == 0 )
1399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1401 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001402 }
1403 else if( strcmp( q, "tls1" ) == 0 )
1404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1406 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001407 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001408 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1411 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001412 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001413 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1416 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001417 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001418 else if( strcmp( q, "dtls1" ) == 0 )
1419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1421 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1422 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001423 }
1424 else if( strcmp( q, "dtls1_2" ) == 0 )
1425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1427 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1428 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001429 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001430 else
1431 goto usage;
1432 }
Hanno Becker72e5ffc2019-07-05 11:35:08 +01001433#endif
1434 else if( strcmp( p, "arc4" ) == 0 )
1435 {
1436 switch( atoi( q ) )
1437 {
1438 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1439 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
1440 default: goto usage;
1441 }
1442 }
1443 else if( strcmp( p, "allow_sha1" ) == 0 )
1444 {
1445 switch( atoi( q ) )
1446 {
1447 case 0: opt.allow_sha1 = 0; break;
1448 case 1: opt.allow_sha1 = 1; break;
1449 default: goto usage;
1450 }
1451 }
Hanno Beckeracd4fc02019-06-12 16:40:50 +01001452#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Paul Bakker91ebfb52012-11-23 14:04:08 +01001453 else if( strcmp( p, "auth_mode" ) == 0 )
1454 {
1455 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001457 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001459 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001461 else
1462 goto usage;
1463 }
Hanno Beckeracd4fc02019-06-12 16:40:50 +01001464#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001465 else if( strcmp( p, "max_frag_len" ) == 0 )
1466 {
1467 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001469 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001471 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001473 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001475 else
1476 goto usage;
1477 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001478 else if( strcmp( p, "trunc_hmac" ) == 0 )
1479 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001480 switch( atoi( q ) )
1481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1483 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001484 default: goto usage;
1485 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001486 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001487 else if( strcmp( p, "hs_timeout" ) == 0 )
1488 {
1489 if( ( p = strchr( q, '-' ) ) == NULL )
1490 goto usage;
1491 *p++ = '\0';
1492 opt.hs_to_min = atoi( q );
1493 opt.hs_to_max = atoi( p );
1494 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1495 goto usage;
1496 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001497 else if( strcmp( p, "mtu" ) == 0 )
1498 {
1499 opt.dtls_mtu = atoi( q );
1500 if( opt.dtls_mtu < 0 )
1501 goto usage;
1502 }
Hanno Becker4d615912018-08-14 13:33:30 +01001503 else if( strcmp( p, "dgram_packing" ) == 0 )
1504 {
1505 opt.dgram_packing = atoi( q );
1506 if( opt.dgram_packing != 0 &&
1507 opt.dgram_packing != 1 )
1508 {
1509 goto usage;
1510 }
1511 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001512 else if( strcmp( p, "recsplit" ) == 0 )
1513 {
1514 opt.recsplit = atoi( q );
1515 if( opt.recsplit < 0 || opt.recsplit > 1 )
1516 goto usage;
1517 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001518 else if( strcmp( p, "dhmlen" ) == 0 )
1519 {
1520 opt.dhmlen = atoi( q );
1521 if( opt.dhmlen < 0 )
1522 goto usage;
1523 }
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +01001524 else if( strcmp( p, "query_config" ) == 0 )
1525 {
1526 return query_config( q );
1527 }
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03001528 else if( strcmp( p, "serialize") == 0 )
1529 {
1530 opt.serialize = atoi( q );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03001531 if( opt.serialize < 0 || opt.serialize > 2)
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03001532 goto usage;
1533 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001534 else
1535 goto usage;
1536 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001537
Teppo Järvelin78007192019-10-25 14:30:33 +03001538 /* try to use as small buf from the heap as possible */
1539 if( opt.request_size <= 0 )
1540 {
1541 main_buf_len = MBEDTLS_SSL_MAX_CONTENT_LEN + 1;
1542 }
1543 else if( opt.request_size < (int)sizeof(GET_REQUEST) )
1544 {
1545 main_buf_len = sizeof(GET_REQUEST) + 1;
1546 }
1547 else
1548 {
1549 main_buf_len = opt.request_size + 1;
1550 }
1551
1552 buf = mbedtls_calloc( 1, main_buf_len );
1553 if( buf == NULL )
1554 {
Teppo Järvelin8e0e4812019-10-21 10:33:11 +03001555 mbedtls_printf( "buf allocation failed!\n" );
1556 goto exit;
1557 }
1558
Hanno Becker16970d22017-10-10 15:56:37 +01001559 /* Event-driven IO is incompatible with the above custom
1560 * receive and send functions, as the polling builds on
1561 * refers to the underlying net_context. */
1562 if( opt.event == 1 && opt.nbio != 1 )
1563 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001564 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001565 opt.nbio = 1;
1566 }
1567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#if defined(MBEDTLS_DEBUG_C)
1569 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001570#endif
1571
Paul Bakkerc1516be2013-06-29 16:01:32 +02001572 if( opt.force_ciphersuite[0] > 0 )
1573 {
Hanno Becker473f98f2019-06-26 10:27:32 +01001574 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001575 ciphersuite_info =
1576 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001577
Paul Bakker66c48102013-07-26 14:05:32 +02001578 if( opt.max_version != -1 &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001579 mbedtls_ssl_ver_gt(
1580 mbedtls_ssl_suite_get_min_minor_ver( ciphersuite_info ),
1581 opt.max_version ) )
Paul Bakker66c48102013-07-26 14:05:32 +02001582 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001583 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker66c48102013-07-26 14:05:32 +02001584 ret = 2;
1585 goto usage;
1586 }
1587 if( opt.min_version != -1 &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001588 mbedtls_ssl_ver_lt(
1589 mbedtls_ssl_suite_get_max_minor_ver( ciphersuite_info ),
1590 opt.min_version ) )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001591 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001592 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001593 ret = 2;
1594 goto usage;
1595 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001596
1597 /* If the server selects a version that's not supported by
1598 * this suite, then there will be no common ciphersuite... */
1599 if( opt.max_version == -1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001600 mbedtls_ssl_ver_gt(
1601 opt.max_version,
1602 mbedtls_ssl_suite_get_max_minor_ver( ciphersuite_info ) ) )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001603 {
Hanno Becker473f98f2019-06-26 10:27:32 +01001604 opt.max_version = mbedtls_ssl_suite_get_max_minor_ver( ciphersuite_info );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001605 }
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001606 if( mbedtls_ssl_ver_lt(
1607 opt.min_version,
1608 mbedtls_ssl_suite_get_min_minor_ver( ciphersuite_info ) ) )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001609 {
Hanno Becker473f98f2019-06-26 10:27:32 +01001610 opt.min_version = mbedtls_ssl_suite_get_min_minor_ver( ciphersuite_info );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001611 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001613 mbedtls_ssl_ver_lt( opt.min_version,
1614 MBEDTLS_SSL_MINOR_VERSION_2 ) )
1615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001617 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001618 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001619
1620 /* Enable RC4 if needed and not explicitly disabled */
Hanno Becker473f98f2019-06-26 10:27:32 +01001621 if( mbedtls_ssl_suite_get_cipher( ciphersuite_info ) == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001624 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001625 mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001626 ret = 2;
1627 goto usage;
1628 }
1629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001631 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001632 }
1633
Hanno Beckera5a2b082019-05-15 14:03:01 +01001634#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01001635 cid_len = strlen( opt.cid_val ) / 2;
1636 if( cid_len > sizeof( cid ) )
1637 {
1638 mbedtls_printf( "CID too long\n" );
1639 goto exit;
1640 }
Hanno Becker7a7aa192019-04-09 17:24:19 +01001641
Hanno Becker96870292019-05-03 17:30:59 +01001642 if( unhexify( opt.cid_val, cid ) != 0 )
1643 {
1644 mbedtls_printf( "CID not valid hex\n" );
1645 goto exit;
1646 }
1647
1648 /* Keep CID settings for renegotiation unless
1649 * specified otherwise. */
1650 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
1651 opt.cid_enabled_renego = opt.cid_enabled;
1652 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
1653 opt.cid_val_renego = opt.cid_val;
1654
1655 cid_renego_len = strlen( opt.cid_val_renego ) / 2;
1656 if( cid_renego_len > sizeof( cid_renego ) )
1657 {
1658 mbedtls_printf( "CID too long\n" );
1659 goto exit;
1660 }
1661
1662 if( unhexify( opt.cid_val_renego, cid_renego ) != 0 )
1663 {
1664 mbedtls_printf( "CID not valid hex\n" );
1665 goto exit;
1666 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001667#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +01001668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001670 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001671 * Unhexify the pre-shared key if any is given
1672 */
1673 if( strlen( opt.psk ) )
1674 {
Hanno Beckerec370302019-04-09 17:12:56 +01001675 psk_len = strlen( opt.psk ) / 2;
1676 if( psk_len > sizeof( psk ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001677 {
Hanno Beckerec370302019-04-09 17:12:56 +01001678 mbedtls_printf( "pre-shared key too long\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001679 goto exit;
1680 }
1681
Hanno Beckerec370302019-04-09 17:12:56 +01001682 if( unhexify( opt.psk, psk ) != 0 )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001683 {
Hanno Beckerec370302019-04-09 17:12:56 +01001684 mbedtls_printf( "pre-shared key not valid hex\n" );
1685 goto exit;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001686 }
1687 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001689
Hanno Beckerc1096e72019-06-19 12:30:41 +01001690#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Hanno Beckere6706e62017-05-15 16:05:15 +01001691 if( opt.curves != NULL )
1692 {
1693 p = (char *) opt.curves;
1694 i = 0;
1695
1696 if( strcmp( p, "none" ) == 0 )
1697 {
1698 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1699 }
1700 else if( strcmp( p, "default" ) != 0 )
1701 {
1702 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001703 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001704 {
1705 q = p;
1706
1707 /* Terminate the current string */
1708 while( *p != ',' && *p != '\0' )
1709 p++;
1710 if( *p == ',' )
1711 *p++ = '\0';
1712
1713 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1714 {
1715 curve_list[i++] = curve_cur->grp_id;
1716 }
1717 else
1718 {
1719 mbedtls_printf( "unknown curve %s\n", q );
1720 mbedtls_printf( "supported curves: " );
1721 for( curve_cur = mbedtls_ecp_curve_list();
1722 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1723 curve_cur++ )
1724 {
1725 mbedtls_printf( "%s ", curve_cur->name );
1726 }
1727 mbedtls_printf( "\n" );
1728 goto exit;
1729 }
1730 }
1731
1732 mbedtls_printf("Number of curves: %d\n", i );
1733
Hanno Becker8651a432017-06-09 16:13:22 +01001734 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001735 {
Hanno Becker8651a432017-06-09 16:13:22 +01001736 mbedtls_printf( "curves list too long, maximum %d",
1737 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001738 goto exit;
1739 }
1740
1741 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1742 }
1743 }
Hanno Beckerc1096e72019-06-19 12:30:41 +01001744#endif /* MBEDTLS_ECP_C && !MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Beckere6706e62017-05-15 16:05:15 +01001745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001747 if( opt.alpn_string != NULL )
1748 {
1749 p = (char *) opt.alpn_string;
1750 i = 0;
1751
1752 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001753 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001754 {
1755 alpn_list[i++] = p;
1756
1757 /* Terminate the current string and move on to next one */
1758 while( *p != ',' && *p != '\0' )
1759 p++;
1760 if( *p == ',' )
1761 *p++ = '\0';
1762 }
1763 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001765
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001766 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001767 * 0. Initialize the RNG and the session data
1768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001770 fflush( stdout );
1771
Hanno Becker7d864c42019-09-19 16:51:41 +01001772 mbedtls_entropy_init( entropy );
Hanno Becker7f1c8052019-08-26 13:30:13 +01001773#if defined(MBEDTLS_CTR_DRBG_C)
Hanno Becker7d864c42019-09-19 16:51:41 +01001774 if( ( ret = mbedtls_ctr_drbg_seed( ctr_drbg, mbedtls_entropy_func,
1775 entropy, (const unsigned char *) pers,
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001776 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +00001777 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001778 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
1779 -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001780 goto exit;
1781 }
Hanno Becker7f1c8052019-08-26 13:30:13 +01001782#else /* MBEDTLS_CTR_DRBG_C */
Hanno Becker7d864c42019-09-19 16:51:41 +01001783 if( ( ret = mbedtls_hmac_drbg_seed( hmac_drbg,
Hanno Becker7f1c8052019-08-26 13:30:13 +01001784 mbedtls_md_info_from_type(
1785 available_hashes[0] ),
1786 mbedtls_entropy_func,
Hanno Becker7d864c42019-09-19 16:51:41 +01001787 entropy, (const unsigned char *) pers,
Hanno Becker7f1c8052019-08-26 13:30:13 +01001788 strlen( pers ) ) ) != 0 )
1789 {
1790 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
1791 -ret );
1792 goto exit;
1793 }
1794#endif /* MBEDTLS_CTR_DRBG */
Paul Bakker508ad5a2011-12-04 17:09:26 +00001795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001799 /*
1800 * 1.1. Load the trusted CA
1801 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001803 fflush( stdout );
1804
Hanno Beckera7242062019-03-05 16:02:15 +00001805 if( strcmp( opt.ca_path, "none" ) == 0 ||
1806 strcmp( opt.ca_file, "none" ) == 0 )
1807 {
1808 ret = 0;
1809 }
1810 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001812 if( strlen( opt.ca_path ) )
Hanno Becker7d864c42019-09-19 16:51:41 +01001813 ret = mbedtls_x509_crt_parse_path( cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001814 else if( strlen( opt.ca_file ) )
Hanno Becker7d864c42019-09-19 16:51:41 +01001815 ret = mbedtls_x509_crt_parse_file( cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001816 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001817#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818#if defined(MBEDTLS_CERTS_C)
Hanno Beckerbb676f72019-02-01 08:15:06 +00001819 {
1820#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001822 {
Hanno Becker7d864c42019-09-19 16:51:41 +01001823 ret = mbedtls_x509_crt_parse( cacert,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 (const unsigned char *) mbedtls_test_cas[i],
1825 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001826 if( ret != 0 )
1827 break;
1828 }
Hanno Beckerbb676f72019-02-01 08:15:06 +00001829 if( ret == 0 )
1830#endif /* MBEDTLS_PEM_PARSE_C */
1831 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
1832 {
Hanno Beckerc8284322019-09-19 16:58:57 +01001833 ret = mbedtls_x509_crt_parse_der_nocopy( cacert,
Hanno Beckerbb676f72019-02-01 08:15:06 +00001834 (const unsigned char *) mbedtls_test_cas_der[i],
1835 mbedtls_test_cas_der_len[i] );
1836 if( ret != 0 )
1837 break;
1838 }
1839 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001840#else
1841 {
1842 ret = 1;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001843 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001844 }
Hanno Beckerbb676f72019-02-01 08:15:06 +00001845#endif /* MBEDTLS_CERTS_C */
Paul Bakker42488232012-05-16 08:21:05 +00001846 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001847 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001848 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1849 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 goto exit;
1851 }
1852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001854
1855 /*
1856 * 1.2. Load own certificate and private key
1857 *
1858 * (can be skipped if client authentication is not required)
1859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001861 fflush( stdout );
1862
Hanno Beckera7242062019-03-05 16:02:15 +00001863 if( strcmp( opt.crt_file, "none" ) == 0 )
1864 ret = 0;
1865 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001867 if( strlen( opt.crt_file ) )
Hanno Becker7d864c42019-09-19 16:51:41 +01001868 ret = mbedtls_x509_crt_parse_file( clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001869 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001870#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871#if defined(MBEDTLS_CERTS_C)
Hanno Beckerc8284322019-09-19 16:58:57 +01001872#if defined(MBEDTLS_PEM_PARSE_C)
1873 ret = mbedtls_x509_crt_parse( clicert,
Hanno Becker16970d22017-10-10 15:56:37 +01001874 (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001876#else
Hanno Beckerc8284322019-09-19 16:58:57 +01001877 ret = mbedtls_x509_crt_parse_der_nocopy( clicert,
1878 (const unsigned char *) mbedtls_test_cli_crt,
1879 mbedtls_test_cli_crt_len );
1880#endif
1881#else
Paul Bakker5690efc2011-05-26 13:16:06 +00001882 {
1883 ret = 1;
Hanno Beckerc258c442018-12-05 16:49:42 +00001884 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001885 }
1886#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001887 if( ret != 0 )
1888 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001889 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1890 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001891 goto exit;
1892 }
1893
Hanno Beckera7242062019-03-05 16:02:15 +00001894 if( strcmp( opt.key_file, "none" ) == 0 )
1895 ret = 0;
1896 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001898 if( strlen( opt.key_file ) )
Hanno Becker7d864c42019-09-19 16:51:41 +01001899 ret = mbedtls_pk_parse_keyfile( pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001900 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001901#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902#if defined(MBEDTLS_CERTS_C)
Hanno Becker7d864c42019-09-19 16:51:41 +01001903 ret = mbedtls_pk_parse_key( pkey,
Hanno Becker16970d22017-10-10 15:56:37 +01001904 (const unsigned char *) mbedtls_test_cli_key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001906#else
1907 {
1908 ret = 1;
Hanno Beckerc258c442018-12-05 16:49:42 +00001909 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001910 }
1911#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001912 if( ret != 0 )
1913 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001914 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
1915 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001916 goto exit;
1917 }
1918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 mbedtls_printf( " ok\n" );
1920#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001921
1922 /*
1923 * 2. Start the connection
1924 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001925 if( opt.server_addr == NULL)
1926 opt.server_addr = opt.server_name;
1927
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001928 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001930 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001931 fflush( stdout );
1932
Hanno Becker16970d22017-10-10 15:56:37 +01001933 if( ( ret = mbedtls_net_connect( &server_fd,
1934 opt.server_addr, opt.server_port,
1935 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1936 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001938 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
1939 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001940 goto exit;
1941 }
1942
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001943 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001944 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001945 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001946 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001947 if( ret != 0 )
1948 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001949 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
1950 -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001951 goto exit;
1952 }
1953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001955
1956 /*
1957 * 3. Setup stuff
1958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 fflush( stdout );
1961
Hanno Becker7d864c42019-09-19 16:51:41 +01001962 if( ( ret = mbedtls_ssl_config_defaults( conf,
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001963 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001964 opt.transport,
1965 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001966 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001967 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
1968 -ret );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001969 goto exit;
1970 }
1971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001973 /* The default algorithms profile disables SHA-1, but our tests still
1974 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001975 if( opt.allow_sha1 > 0 )
1976 {
1977 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
Hanno Becker7d864c42019-09-19 16:51:41 +01001978 mbedtls_ssl_conf_cert_profile( conf, &crt_profile_for_test );
Hanno Becker56595f42019-06-19 16:31:38 +01001979#if !defined(MBEDTLS_SSL_CONF_SINGLE_HASH)
Hanno Becker7d864c42019-09-19 16:51:41 +01001980 mbedtls_ssl_conf_sig_hashes( conf, available_hashes );
Hanno Becker56595f42019-06-19 16:31:38 +01001981#endif
Gilles Peskinebc70a182017-05-09 15:59:24 +02001982 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001983
Hanno Becker9ec3fe02019-07-01 17:36:12 +01001984#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Hanno Becker7d864c42019-09-19 16:51:41 +01001985 mbedtls_ssl_conf_verify( conf, my_verify, NULL );
Hanno Beckerf9ca30d2019-02-26 11:38:29 +00001986 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Becker9ec3fe02019-07-01 17:36:12 +01001987#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Gilles Peskineef86ab22017-05-05 18:59:02 +02001988#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001989
Hanno Beckere0200da2019-06-13 09:23:43 +01001990#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
1991 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
1992 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Becker96870292019-05-03 17:30:59 +01001993 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckereec2be92019-05-03 13:06:44 +01001994 {
Hanno Becker96870292019-05-03 17:30:59 +01001995 if( opt.cid_enabled == 1 &&
1996 opt.cid_enabled_renego == 1 &&
1997 cid_len != cid_renego_len )
1998 {
1999 mbedtls_printf( "CID length must not change during renegotiation\n" );
2000 goto usage;
2001 }
2002
Hanno Becker96870292019-05-03 17:30:59 +01002003 if( opt.cid_enabled == 1 )
Hanno Becker7d864c42019-09-19 16:51:41 +01002004 ret = mbedtls_ssl_conf_cid( conf, cid_len,
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002005 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Becker96870292019-05-03 17:30:59 +01002006 else
Hanno Becker7d864c42019-09-19 16:51:41 +01002007 ret = mbedtls_ssl_conf_cid( conf, cid_renego_len,
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002008 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Becker96870292019-05-03 17:30:59 +01002009
Hanno Beckereec2be92019-05-03 13:06:44 +01002010 if( ret != 0 )
2011 {
Hanno Becker76581052019-05-23 16:58:22 +01002012 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
2013 -ret );
Hanno Beckereec2be92019-05-03 13:06:44 +01002014 goto exit;
2015 }
2016 }
Hanno Beckere0200da2019-06-13 09:23:43 +01002017#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
2018 !MBEDTLS_SSL_CONF_CID_LEN &&
2019 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +01002020
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01002021 if( opt.auth_mode != DFL_AUTH_MODE )
Hanno Becker7d864c42019-09-19 16:51:41 +01002022 mbedtls_ssl_conf_authmode( conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00002023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002025 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Hanno Becker7d864c42019-09-19 16:51:41 +01002026 mbedtls_ssl_conf_handshake_timeout( conf, opt.hs_to_min,
Hanno Becker16970d22017-10-10 15:56:37 +01002027 opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002028
Hanno Becker4d615912018-08-14 13:33:30 +01002029 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker7d864c42019-09-19 16:51:41 +01002030 mbedtls_ssl_set_datagram_packing( ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker7d864c42019-09-19 16:51:41 +01002034 if( ( ret = mbedtls_ssl_conf_max_frag_len( conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002035 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002036 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n",
2037 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002038 goto exit;
2039 }
Paul Bakker05decb22013-08-15 13:33:48 +02002040#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02002041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01002043 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Hanno Becker7d864c42019-09-19 16:51:41 +01002044 mbedtls_ssl_conf_truncated_hmac( conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02002045#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02002046
Hanno Beckerf765ce62019-06-21 13:17:14 +01002047#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
2048 !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET) && \
2049 !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002050 if( opt.extended_ms != DFL_EXTENDED_MS )
Hanno Becker7d864c42019-09-19 16:51:41 +01002051 mbedtls_ssl_conf_extended_master_secret( conf, opt.extended_ms );
Jarno Lamsa41b35912019-06-10 15:51:11 +03002052 if( opt.enforce_extended_master_secret != DFL_EXTENDED_MS_ENFORCE )
Hanno Becker7d864c42019-09-19 16:51:41 +01002053 mbedtls_ssl_conf_extended_master_secret_enforce( conf,
Jarno Lamsa41b35912019-06-10 15:51:11 +03002054 opt.enforce_extended_master_secret );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002055#endif
2056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002058 if( opt.etm != DFL_ETM )
Hanno Becker7d864c42019-09-19 16:51:41 +01002059 mbedtls_ssl_conf_encrypt_then_mac( conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002060#endif
2061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01002063 if( opt.recsplit != DFL_RECSPLIT )
Hanno Becker7d864c42019-09-19 16:51:41 +01002064 mbedtls_ssl_conf_cbc_record_splitting( conf, opt.recsplit
Hanno Becker16970d22017-10-10 15:56:37 +01002065 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
2066 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01002067#endif
2068
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02002069#if defined(MBEDTLS_DHM_C)
2070 if( opt.dhmlen != DFL_DHMLEN )
Hanno Becker7d864c42019-09-19 16:51:41 +01002071 mbedtls_ssl_conf_dhm_min_bitlen( conf, opt.dhmlen );
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02002072#endif
2073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002075 if( opt.alpn_string != NULL )
Hanno Becker7d864c42019-09-19 16:51:41 +01002076 if( ( ret = mbedtls_ssl_conf_alpn_protocols( conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002077 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002078 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
2079 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002080 goto exit;
2081 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002082#endif
2083
Hanno Becker7f1c8052019-08-26 13:30:13 +01002084#if defined(MBEDTLS_CTR_DRBG_C)
Hanno Beckerece325c2019-06-13 15:39:27 +01002085#if !defined(MBEDTLS_SSL_CONF_RNG)
Hanno Becker7d864c42019-09-19 16:51:41 +01002086 mbedtls_ssl_conf_rng( conf, mbedtls_ctr_drbg_random, ctr_drbg );
Hanno Beckerece325c2019-06-13 15:39:27 +01002087#else
Hanno Becker7d864c42019-09-19 16:51:41 +01002088 rng_ctx_global = ctr_drbg;
Hanno Beckerece325c2019-06-13 15:39:27 +01002089#endif
Hanno Becker7f1c8052019-08-26 13:30:13 +01002090#else /* MBEDTLS_CTR_DRBG_C */
2091#if !defined(MBEDTLS_SSL_CONF_RNG)
Hanno Becker7d864c42019-09-19 16:51:41 +01002092 mbedtls_ssl_conf_rng( conf, mbedtls_hmac_drbg_random, hmac_drbg );
Hanno Becker7f1c8052019-08-26 13:30:13 +01002093#else
Hanno Becker7d864c42019-09-19 16:51:41 +01002094 rng_ctx_global = hmac_drbg;
Hanno Becker7f1c8052019-08-26 13:30:13 +01002095#endif
2096#endif /* MBEDTLS_CTR_DRBG_C */
Hanno Beckerece325c2019-06-13 15:39:27 +01002097
Hanno Becker14a4a442019-07-02 17:00:34 +01002098#if defined(MBEDTLS_DEBUG_C)
Hanno Becker7d864c42019-09-19 16:51:41 +01002099 mbedtls_ssl_conf_dbg( conf, my_debug, stdout );
Hanno Becker14a4a442019-07-02 17:00:34 +01002100#endif
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002101
Hanno Becker1f835fa2019-06-13 10:14:59 +01002102#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Hanno Becker7d864c42019-09-19 16:51:41 +01002103 mbedtls_ssl_conf_read_timeout( conf, opt.read_timeout );
Hanno Becker1f835fa2019-06-13 10:14:59 +01002104#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker7d864c42019-09-19 16:51:41 +01002107 mbedtls_ssl_conf_session_tickets( conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02002108#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002109
Hanno Becker73f4cb12019-06-27 13:51:07 +01002110#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Paul Bakker645ce3a2012-10-31 12:32:41 +00002111 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Hanno Becker7d864c42019-09-19 16:51:41 +01002112 mbedtls_ssl_conf_ciphersuites( conf, opt.force_ciphersuite );
Hanno Becker73f4cb12019-06-27 13:51:07 +01002113#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002114
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002115#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002116 if( opt.arc4 != DFL_ARC4 )
Hanno Becker7d864c42019-09-19 16:51:41 +01002117 mbedtls_ssl_conf_arc4_support( conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002118#endif
Paul Bakker51936882011-02-20 16:05:58 +00002119
Hanno Beckerb0b2b672019-06-12 16:58:10 +01002120#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002121 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Hanno Becker7d864c42019-09-19 16:51:41 +01002122 mbedtls_ssl_conf_legacy_renegotiation( conf, opt.allow_legacy );
Hanno Beckerb0b2b672019-06-12 16:58:10 +01002123#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker7d864c42019-09-19 16:51:41 +01002125 mbedtls_ssl_conf_renegotiation( conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002126#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002129 if( strcmp( opt.ca_path, "none" ) != 0 &&
2130 strcmp( opt.ca_file, "none" ) != 0 )
2131 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002132 mbedtls_ssl_conf_ca_chain( conf, cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002133 }
2134 if( strcmp( opt.crt_file, "none" ) != 0 &&
2135 strcmp( opt.key_file, "none" ) != 0 )
2136 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002137 if( ( ret = mbedtls_ssl_conf_own_cert( conf, clicert, pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002138 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002139 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
2140 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002141 goto exit;
2142 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002143 }
Paul Bakkered27a042013-04-18 22:46:23 +02002144#endif
2145
Hanno Beckere6706e62017-05-15 16:05:15 +01002146#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01002147#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Hanno Beckere6706e62017-05-15 16:05:15 +01002148 if( opt.curves != NULL &&
2149 strcmp( opt.curves, "default" ) != 0 )
2150 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002151 mbedtls_ssl_conf_curves( conf, curve_list );
Hanno Beckere6706e62017-05-15 16:05:15 +01002152 }
Hanno Beckerc1096e72019-06-19 12:30:41 +01002153#endif /* !MBEDTLS_SSL_CONF_SINGLE_EC */
2154#endif /* MBEDTLS_ECP_C */
Hanno Beckere6706e62017-05-15 16:05:15 +01002155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Becker7d864c42019-09-19 16:51:41 +01002157 if( ( ret = mbedtls_ssl_conf_psk( conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002158 (const unsigned char *) opt.psk_identity,
2159 strlen( opt.psk_identity ) ) ) != 0 )
2160 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002161 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n",
2162 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002163 goto exit;
2164 }
Paul Bakkered27a042013-04-18 22:46:23 +02002165#endif
2166
Hanno Becker72e5ffc2019-07-05 11:35:08 +01002167#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
2168 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) || \
2169 !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
2170 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002171 if( opt.min_version != DFL_MIN_VERSION )
Hanno Becker7d864c42019-09-19 16:51:41 +01002172 mbedtls_ssl_conf_min_version( conf, MBEDTLS_SSL_MAJOR_VERSION_3,
Hanno Becker16970d22017-10-10 15:56:37 +01002173 opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01002174
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002175 if( opt.max_version != DFL_MAX_VERSION )
Hanno Becker7d864c42019-09-19 16:51:41 +01002176 mbedtls_ssl_conf_max_version( conf, MBEDTLS_SSL_MAJOR_VERSION_3,
Hanno Becker16970d22017-10-10 15:56:37 +01002177 opt.max_version );
Hanno Becker72e5ffc2019-07-05 11:35:08 +01002178#endif
Paul Bakker1d29fb52012-09-28 13:28:45 +00002179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02002181 if( opt.fallback != DFL_FALLBACK )
Hanno Becker7d864c42019-09-19 16:51:41 +01002182 mbedtls_ssl_conf_fallback( conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02002183#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002184
Hanno Becker7d864c42019-09-19 16:51:41 +01002185 if( ( ret = mbedtls_ssl_setup( ssl, conf ) ) != 0 )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002186 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002187 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
2188 -ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002189 goto exit;
2190 }
2191
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002192#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker7d864c42019-09-19 16:51:41 +01002193 if( ( ret = mbedtls_ssl_set_hostname( ssl, opt.server_name ) ) != 0 )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002194 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002195 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
2196 ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002197 goto exit;
2198 }
2199#endif
2200
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002201#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2202 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
2203 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002204 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( ssl,
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002205 (const unsigned char *) opt.ecjpake_pw,
2206 strlen( opt.ecjpake_pw ) ) ) != 0 )
2207 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002208 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
2209 ret );
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002210 goto exit;
2211 }
2212 }
2213#endif
2214
Hanno Beckera58a8962019-06-13 16:11:15 +01002215#if !defined(MBEDTLS_SSL_CONF_RECV) && \
2216 !defined(MBEDTLS_SSL_CONF_SEND) && \
2217 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Hanno Becker7d864c42019-09-19 16:51:41 +01002218 io_ctx.ssl = ssl;
Hanno Becker14219fe2019-07-03 17:02:43 +01002219 io_ctx.net = &server_fd;
Hanno Becker7d864c42019-09-19 16:51:41 +01002220 mbedtls_ssl_set_bio( ssl, &io_ctx, send_cb, recv_cb,
Hanno Becker14219fe2019-07-03 17:02:43 +01002221 opt.nbio == 0 ? recv_timeout_cb : NULL );
Hanno Beckera58a8962019-06-13 16:11:15 +01002222#else
Hanno Becker7d864c42019-09-19 16:51:41 +01002223 mbedtls_ssl_set_bio_ctx( ssl, &server_fd );
Hanno Beckera58a8962019-06-13 16:11:15 +01002224#endif
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002225
Hanno Beckera5a2b082019-05-15 14:03:01 +01002226#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7a7aa192019-04-09 17:24:19 +01002227 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2228 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002229 if( ( ret = mbedtls_ssl_set_cid( ssl, opt.cid_enabled,
Hanno Becker7a7aa192019-04-09 17:24:19 +01002230 cid, cid_len ) ) != 0 )
2231 {
2232 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2233 ret );
2234 goto exit;
2235 }
2236 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002237#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +01002238
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002239#if defined(MBEDTLS_SSL_PROTO_DTLS)
2240 if( opt.dtls_mtu != DFL_DTLS_MTU )
Hanno Becker7d864c42019-09-19 16:51:41 +01002241 mbedtls_ssl_set_mtu( ssl, opt.dtls_mtu );
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002242#endif
2243
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002244#if defined(MBEDTLS_TIMING_C)
Hanno Becker0ae6b242019-06-13 16:45:36 +01002245#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
2246 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Hanno Becker7d864c42019-09-19 16:51:41 +01002247 mbedtls_ssl_set_timer_cb( ssl, &timer, mbedtls_timing_set_delay,
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002248 mbedtls_timing_get_delay );
Hanno Becker0ae6b242019-06-13 16:45:36 +01002249#else
Hanno Becker7d864c42019-09-19 16:51:41 +01002250 mbedtls_ssl_set_timer_cb_ctx( ssl, &timer );
Hanno Becker0ae6b242019-06-13 16:45:36 +01002251#endif
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002252#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002253
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002254#if defined(MBEDTLS_ECP_RESTARTABLE)
2255 if( opt.ec_max_ops != DFL_EC_MAX_OPS )
2256 mbedtls_ecp_set_max_ops( opt.ec_max_ops );
2257#endif
2258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002260
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 /*
2262 * 4. Handshake
2263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 fflush( stdout );
2266
Hanno Becker7d864c42019-09-19 16:51:41 +01002267 while( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 {
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002269 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2270 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002271 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Paul Bakker40e46942009-01-03 21:51:57 +00002272 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002273 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
2274 -ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2276 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01002277 " Unable to verify the server's certificate. "
2278 "Either it is invalid,\n"
2279 " or you didn't set ca_file or ca_path "
2280 "to an appropriate value.\n"
2281 " Alternatively, you may want to use "
2282 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00002285 }
Hanno Becker16970d22017-10-10 15:56:37 +01002286
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002287#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002288 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2289 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002290#endif
2291
Hanno Becker16970d22017-10-10 15:56:37 +01002292 /* For event-driven IO, wait for socket to become available */
2293 if( opt.event == 1 /* level triggered IO */ )
2294 {
2295#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002296 ret = idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002297#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002298 ret = idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002299#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002300 if( ret != 0 )
2301 goto exit;
Hanno Becker16970d22017-10-10 15:56:37 +01002302 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002303 }
2304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Hanno Becker7d864c42019-09-19 16:51:41 +01002306 mbedtls_ssl_get_version( ssl ),
2307 mbedtls_ssl_get_ciphersuite( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002308
Hanno Becker7d864c42019-09-19 16:51:41 +01002309 if( ( ret = mbedtls_ssl_get_record_expansion( ssl ) ) >= 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002311 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002313
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002314#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2315 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
Hanno Becker7d864c42019-09-19 16:51:41 +01002316 (unsigned int) mbedtls_ssl_get_max_frag_len( ssl ) );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002317#endif
2318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002320 if( opt.alpn_string != NULL )
2321 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002322 const char *alp = mbedtls_ssl_get_alpn_protocol( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002324 alp ? alp : "(none)" );
2325 }
2326#endif
2327
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002328 if( opt.reconnect != 0 )
2329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002331 fflush( stdout );
2332
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02002333 if( opt.reco_mode == 1 )
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02002334 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02002335 /* free any previously saved data */
Manuel Pégourié-Gonnard4bb1b992019-05-24 10:26:41 +02002336 if( session_data != NULL )
2337 {
2338 mbedtls_platform_zeroize( session_data, session_data_len );
2339 mbedtls_free( session_data );
2340 session_data = NULL;
2341 }
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02002342
2343 /* get size of the buffer needed */
Hanno Becker7d864c42019-09-19 16:51:41 +01002344 mbedtls_ssl_session_save( mbedtls_ssl_get_session_pointer( ssl ),
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02002345 NULL, 0, &session_data_len );
2346 session_data = mbedtls_calloc( 1, session_data_len );
2347 if( session_data == NULL )
2348 {
2349 mbedtls_printf( " failed\n ! alloc %u bytes for session data\n",
2350 (unsigned) session_data_len );
2351 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
2352 goto exit;
2353 }
2354
2355 /* actually save session data */
Hanno Becker7d864c42019-09-19 16:51:41 +01002356 if( ( ret = mbedtls_ssl_session_save( mbedtls_ssl_get_session_pointer( ssl ),
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02002357 session_data, session_data_len,
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02002358 &session_data_len ) ) != 0 )
2359 {
2360 mbedtls_printf( " failed\n ! mbedtls_ssl_session_saved returned -0x%04x\n\n",
2361 -ret );
2362 goto exit;
2363 }
2364 }
2365 else
2366 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002367 if( ( ret = mbedtls_ssl_get_session( ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02002368 {
2369 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
2370 -ret );
2371 goto exit;
2372 }
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02002373 }
2374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02002376
2377 if( opt.reco_mode == 1 )
2378 {
2379 mbedtls_printf( " [ Saved %u bytes of session data]\n",
2380 (unsigned) session_data_len );
2381 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002382 }
2383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 /*
2386 * 5. Verify the server certificate
2387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002389
Hanno Becker7d864c42019-09-19 16:51:41 +01002390 if( ( flags = mbedtls_ssl_get_verify_result( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 {
Hanno Becker02a21932019-06-10 15:08:43 +01002392#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002393 char vrfy_buf[512];
Peter Kolbusdc470ae2018-12-11 13:55:56 -06002394#endif
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
Hanno Becker02a21932019-06-10 15:08:43 +01002398#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Becker16970d22017-10-10 15:56:37 +01002399 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ),
2400 " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00002401
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002402 mbedtls_printf( "%s\n", vrfy_buf );
Peter Kolbusdc470ae2018-12-11 13:55:56 -06002403#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 }
2405 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002407
Hanno Becker9ec3fe02019-07-01 17:36:12 +01002408#if !defined(MBEDTLS_X509_REMOVE_INFO) && \
2409 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Hanno Becker975c4632019-02-25 17:43:18 +00002410 mbedtls_printf( " . Peer certificate information ...\n" );
2411 mbedtls_printf( "%s\n", peer_crt_info );
Hanno Becker9ec3fe02019-07-01 17:36:12 +01002412#endif /* !MBEDTLS_X509_REMOVE_INFO && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002414
Hanno Beckera5a2b082019-05-15 14:03:01 +01002415#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7d864c42019-09-19 16:51:41 +01002416 ret = report_cid_usage( ssl, "initial handshake" );
Hanno Becker96870292019-05-03 17:30:59 +01002417 if( ret != 0 )
2418 goto exit;
2419
Hanno Becker7a7aa192019-04-09 17:24:19 +01002420 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2421 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002422 if( ( ret = mbedtls_ssl_set_cid( ssl, opt.cid_enabled_renego,
Hanno Becker96870292019-05-03 17:30:59 +01002423 cid_renego,
2424 cid_renego_len ) ) != 0 )
Hanno Becker7a7aa192019-04-09 17:24:19 +01002425 {
Hanno Becker96870292019-05-03 17:30:59 +01002426 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2427 ret );
2428 return( ret );
Hanno Becker7a7aa192019-04-09 17:24:19 +01002429 }
2430 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002431#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +01002432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002434 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002435 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002436 /*
2437 * Perform renegotiation (this must be done when the server is waiting
2438 * for input from our side).
2439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002441 fflush( stdout );
Hanno Becker7d864c42019-09-19 16:51:41 +01002442 while( ( ret = mbedtls_ssl_renegotiate( ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002443 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002444 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002445 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002446 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002447 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002448 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
2449 ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002450 goto exit;
2451 }
Hanno Becker16970d22017-10-10 15:56:37 +01002452
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002453#if defined(MBEDTLS_ECP_RESTARTABLE)
2454 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2455 continue;
2456#endif
2457
Hanno Becker16970d22017-10-10 15:56:37 +01002458 /* For event-driven IO, wait for socket to become available */
2459 if( opt.event == 1 /* level triggered IO */ )
2460 {
2461#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002462 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002463#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002464 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002465#endif
2466 }
2467
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002468 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002472
Hanno Beckera5a2b082019-05-15 14:03:01 +01002473#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7d864c42019-09-19 16:51:41 +01002474 ret = report_cid_usage( ssl, "after renegotiation" );
Hanno Becker96870292019-05-03 17:30:59 +01002475 if( ret != 0 )
2476 goto exit;
Hanno Beckera5a2b082019-05-15 14:03:01 +01002477#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker96870292019-05-03 17:30:59 +01002478
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 /*
2480 * 6. Write the GET request
2481 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002482 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002483send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 fflush( stdout );
2486
Teppo Järvelin8e0e4812019-10-21 10:33:11 +03002487 len = mbedtls_snprintf( (char *) buf, main_buf_len - 1, GET_REQUEST,
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002488 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002489 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002490
2491 /* Add padding to GET request to reach opt.request_size in length */
2492 if( opt.request_size != DFL_REQUEST_SIZE &&
2493 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02002494 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002495 memset( buf + len, 'A', opt.request_size - len - tail_len );
2496 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02002497 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002498
Teppo Järvelin8e0e4812019-10-21 10:33:11 +03002499 strncpy( (char *) buf + len, GET_REQUEST_END, main_buf_len - len - 1 );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002500 len += tail_len;
2501
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02002502 /* Truncate if request size is smaller than the "natural" size */
2503 if( opt.request_size != DFL_REQUEST_SIZE &&
2504 len > opt.request_size )
2505 {
2506 len = opt.request_size;
2507
2508 /* Still end with \r\n unless that's really not possible */
2509 if( len >= 2 ) buf[len - 2] = '\r';
2510 if( len >= 1 ) buf[len - 1] = '\n';
2511 }
2512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002514 {
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002515 written = 0;
2516 frags = 0;
2517
2518 do
Paul Bakker5121ce52009-01-03 21:22:43 +00002519 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002520 while( ( ret = mbedtls_ssl_write( ssl, buf + written,
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002521 len - written ) ) < 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002522 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002523 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002524 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002525 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002526 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002527 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
2528 -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002529 goto exit;
2530 }
Hanno Becker16970d22017-10-10 15:56:37 +01002531
2532 /* For event-driven IO, wait for socket to become available */
2533 if( opt.event == 1 /* level triggered IO */ )
2534 {
2535#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002536 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002537#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002538 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002539#endif
2540 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002541 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002542
2543 frags++;
2544 written += ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002545 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002546 while( written < len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002547 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002548 else /* Not stream, so datagram */
2549 {
Hanno Becker16970d22017-10-10 15:56:37 +01002550 while( 1 )
2551 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002552 ret = mbedtls_ssl_write( ssl, buf, len );
Hanno Becker16970d22017-10-10 15:56:37 +01002553
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002554#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002555 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002556 continue;
2557#endif
2558
Hanno Becker16970d22017-10-10 15:56:37 +01002559 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2560 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2561 break;
2562
2563 /* For event-driven IO, wait for socket to become available */
2564 if( opt.event == 1 /* level triggered IO */ )
2565 {
2566#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002567 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002568#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002569 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002570#endif
2571 }
2572 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002573
2574 if( ret < 0 )
2575 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002576 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n",
2577 ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002578 goto exit;
2579 }
2580
2581 frags = 1;
2582 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002583
2584 if( written < len )
2585 {
2586 mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
2587 "was truncated to size %u", (unsigned) written );
2588 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002589 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002590
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002591 buf[written] = '\0';
Hanno Becker16970d22017-10-10 15:56:37 +01002592 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
2593 written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002594
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002595 /* Send a non-empty request if request_size == 0 */
2596 if ( len == 0 )
2597 {
2598 opt.request_size = DFL_REQUEST_SIZE;
2599 goto send_request;
2600 }
2601
Paul Bakker5121ce52009-01-03 21:22:43 +00002602 /*
2603 * 7. Read the HTTP response
2604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002606 fflush( stdout );
2607
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002608 /*
2609 * TLS and DTLS need different reading styles (stream vs datagram)
2610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002612 {
2613 do
2614 {
Teppo Järvelin8e0e4812019-10-21 10:33:11 +03002615 len = main_buf_len - 1;
2616 memset( buf, 0, main_buf_len );
Hanno Becker7d864c42019-09-19 16:51:41 +01002617 ret = mbedtls_ssl_read( ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002618
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002619#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002620 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002621 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002622#endif
2623
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002624 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
2625 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Hanno Becker16970d22017-10-10 15:56:37 +01002626 {
2627 /* For event-driven IO, wait for socket to become available */
2628 if( opt.event == 1 /* level triggered IO */ )
2629 {
2630#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002631 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002632#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002633 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002634#endif
2635 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002636 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01002637 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002638
2639 if( ret <= 0 )
2640 {
2641 switch( ret )
2642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2644 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002645 ret = 0;
2646 goto close_notify;
2647
2648 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 case MBEDTLS_ERR_NET_CONN_RESET:
2650 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002651 ret = 0;
2652 goto reconnect;
2653
2654 default:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002655 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n",
2656 -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002657 goto exit;
2658 }
2659 }
2660
2661 len = ret;
2662 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002664
2665 /* End of message should be detected according to the syntax of the
2666 * application protocol (eg HTTP), just use a dummy test here. */
2667 if( ret > 0 && buf[len-1] == '\n' )
2668 {
2669 ret = 0;
2670 break;
2671 }
2672 }
2673 while( 1 );
2674 }
2675 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 {
Teppo Järvelin8e0e4812019-10-21 10:33:11 +03002677 len = main_buf_len - 1;
2678 memset( buf, 0, main_buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002679
Hanno Becker16970d22017-10-10 15:56:37 +01002680 while( 1 )
2681 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002682 ret = mbedtls_ssl_read( ssl, buf, len );
Hanno Becker16970d22017-10-10 15:56:37 +01002683
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002684#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002685 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002686 continue;
2687#endif
2688
Hanno Becker16970d22017-10-10 15:56:37 +01002689 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2690 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2691 break;
2692
2693 /* For event-driven IO, wait for socket to become available */
2694 if( opt.event == 1 /* level triggered IO */ )
2695 {
2696#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002697 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002698#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002699 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002700#endif
2701 }
2702 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002703
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002704 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002706 switch( ret )
2707 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002708 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002710 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02002711 goto send_request;
2712 goto exit;
2713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2715 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002716 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002717 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00002718
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002719 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002721 goto exit;
2722 }
Paul Bakker5690efc2011-05-26 13:16:06 +00002723 }
2724
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02002726 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002728 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002729 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002730
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002731 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002732 * 7b. Simulate hard reset and reconnect from same port?
2733 */
2734 if( opt.reconnect_hard != 0 )
2735 {
2736 opt.reconnect_hard = 0;
2737
2738 mbedtls_printf( " . Restarting connection from same port..." );
2739 fflush( stdout );
2740
Hanno Becker9ec3fe02019-07-01 17:36:12 +01002741#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
2742 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Hanno Beckerb7fab762019-02-26 12:36:53 +00002743 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Becker9ec3fe02019-07-01 17:36:12 +01002744#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Hanno Beckerb7fab762019-02-26 12:36:53 +00002745
Hanno Becker7d864c42019-09-19 16:51:41 +01002746 if( ( ret = mbedtls_ssl_session_reset( ssl ) ) != 0 )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002747 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002748 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2749 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002750 goto exit;
2751 }
2752
Hanno Becker7d864c42019-09-19 16:51:41 +01002753 while( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002754 {
2755 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002756 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002757 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002758 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002759 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2760 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002761 goto exit;
2762 }
Hanno Becker16970d22017-10-10 15:56:37 +01002763
2764 /* For event-driven IO, wait for socket to become available */
2765 if( opt.event == 1 /* level triggered IO */ )
2766 {
2767#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002768 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002769#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002770 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002771#endif
2772 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002773 }
2774
2775 mbedtls_printf( " ok\n" );
2776
2777 goto send_request;
2778 }
2779
2780 /*
Jarno Lamsad736d082019-05-29 15:15:08 +03002781 * 7c. Simulate serialize/deserialize and go back to data exchange
2782 */
Jarno Lamsacf1b6722019-06-04 11:06:31 +03002783#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03002784 if( opt.serialize != 0 )
Jarno Lamsad736d082019-05-29 15:15:08 +03002785 {
Jarno Lamsa034ae842019-06-06 15:10:07 +03002786 size_t buf_len;
Jarno Lamsad736d082019-05-29 15:15:08 +03002787
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002788 mbedtls_printf( " . Serializing live connection..." );
Jarno Lamsad736d082019-05-29 15:15:08 +03002789
Hanno Becker7d864c42019-09-19 16:51:41 +01002790 ret = mbedtls_ssl_context_save( ssl, NULL, 0, &buf_len );
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002791 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jarno Lamsad736d082019-05-29 15:15:08 +03002792 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03002793 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
2794 "-0x%x\n\n", -ret );
Jarno Lamsad736d082019-05-29 15:15:08 +03002795
2796 goto exit;
2797 }
2798
Jarno Lamsa034ae842019-06-06 15:10:07 +03002799 if( ( context_buf = mbedtls_calloc( 1, buf_len ) ) == NULL )
Jarno Lamsad736d082019-05-29 15:15:08 +03002800 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03002801 mbedtls_printf( " failed\n ! Couldn't allocate buffer for "
2802 "serialized context" );
Jarno Lamsad736d082019-05-29 15:15:08 +03002803
2804 goto exit;
2805 }
Manuel Pégourié-Gonnardc9812292019-07-15 10:31:11 +02002806 context_buf_len = buf_len;
Jarno Lamsad736d082019-05-29 15:15:08 +03002807
Hanno Becker7d864c42019-09-19 16:51:41 +01002808 if( ( ret = mbedtls_ssl_context_save( ssl, context_buf,
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03002809 buf_len, &buf_len ) ) != 0 )
Jarno Lamsad736d082019-05-29 15:15:08 +03002810 {
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002811 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
Jarno Lamsa5737ec92019-06-04 15:36:18 +03002812 "-0x%x\n\n", -ret );
Jarno Lamsad736d082019-05-29 15:15:08 +03002813
2814 goto exit;
2815 }
2816
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002817 mbedtls_printf( " ok\n" );
2818
2819 if( opt.serialize == 1 )
2820 {
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +02002821 /* nothing to do here, done by context_save() already */
2822 mbedtls_printf( " . Context has been reset... ok" );
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002823 }
2824
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03002825 if( opt.serialize == 2 )
2826 {
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002827 mbedtls_printf( " . Freeing and reinitializing context..." );
2828
Hanno Becker7d864c42019-09-19 16:51:41 +01002829 mbedtls_ssl_free( ssl );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03002830
Hanno Becker7d864c42019-09-19 16:51:41 +01002831 mbedtls_ssl_init( ssl );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03002832
Hanno Becker7d864c42019-09-19 16:51:41 +01002833 if( ( ret = mbedtls_ssl_setup( ssl, conf ) ) != 0 )
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03002834 {
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03002835 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned "
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002836 "-0x%x\n\n", -ret );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03002837 goto exit;
2838 }
2839
Hanno Becker41e5a682019-07-19 17:07:30 +01002840#if !defined(MBEDTLS_SSL_CONF_RECV) && \
2841 !defined(MBEDTLS_SSL_CONF_SEND) && \
2842 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Hanno Becker7d864c42019-09-19 16:51:41 +01002843 mbedtls_ssl_set_bio( ssl, &io_ctx, send_cb, recv_cb,
Hanno Becker14219fe2019-07-03 17:02:43 +01002844 opt.nbio == 0 ? recv_timeout_cb : NULL );
Hanno Becker41e5a682019-07-19 17:07:30 +01002845#else
Hanno Becker7d864c42019-09-19 16:51:41 +01002846 mbedtls_ssl_set_bio_ctx( ssl, &server_fd );
Hanno Becker41e5a682019-07-19 17:07:30 +01002847#endif
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03002848
Jarno Lamsa29a15c22019-06-13 11:45:06 +03002849#if defined(MBEDTLS_TIMING_C)
Hanno Becker0ae6b242019-06-13 16:45:36 +01002850#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
2851 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Hanno Becker7d864c42019-09-19 16:51:41 +01002852 mbedtls_ssl_set_timer_cb( ssl, &timer,
Manuel Pégourié-Gonnard86dfa0c2019-07-15 12:23:22 +02002853 mbedtls_timing_set_delay,
2854 mbedtls_timing_get_delay );
Hanno Becker0ae6b242019-06-13 16:45:36 +01002855#else
Hanno Becker7d864c42019-09-19 16:51:41 +01002856 mbedtls_ssl_set_timer_cb_ctx( ssl, &timer );
Hanno Becker0ae6b242019-06-13 16:45:36 +01002857#endif
Jarno Lamsa29a15c22019-06-13 11:45:06 +03002858#endif /* MBEDTLS_TIMING_C */
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002859
2860 mbedtls_printf( " ok\n" );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03002861 }
2862
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002863 mbedtls_printf( " . Deserializing connection..." );
Jarno Lamsad736d082019-05-29 15:15:08 +03002864
Hanno Becker7d864c42019-09-19 16:51:41 +01002865 if( ( ret = mbedtls_ssl_context_load( ssl, context_buf,
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03002866 buf_len ) ) != 0 )
Jarno Lamsad736d082019-05-29 15:15:08 +03002867 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03002868 mbedtls_printf( "failed\n ! mbedtls_ssl_context_load returned "
2869 "-0x%x\n\n", -ret );
Jarno Lamsad736d082019-05-29 15:15:08 +03002870
2871 goto exit;
2872 }
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002873
Manuel Pégourié-Gonnardc9812292019-07-15 10:31:11 +02002874 mbedtls_free( context_buf );
2875 context_buf = NULL;
2876 context_buf_len = 0;
2877
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002878 mbedtls_printf( " ok\n" );
Jarno Lamsad736d082019-05-29 15:15:08 +03002879 }
Jarno Lamsa5737ec92019-06-04 15:36:18 +03002880#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jarno Lamsad736d082019-05-29 15:15:08 +03002881
2882 /*
2883 * 7d. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002884 */
2885 if( --opt.exchanges > 0 )
2886 goto send_request;
2887
2888 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002889 * 8. Done, cleanly close the connection
2890 */
2891close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002893 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002894
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002895 /* No error checking, the connection might be closed already */
Hanno Becker7d864c42019-09-19 16:51:41 +01002896 do ret = mbedtls_ssl_close_notify( ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002897 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002898 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002901
2902 /*
2903 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002904 */
2905reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002906 if( opt.reconnect != 0 )
2907 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02002908 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002909
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002910 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002911
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002912#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002913 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02002914 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002915#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002918
Hanno Becker9ec3fe02019-07-01 17:36:12 +01002919#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
2920 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Hanno Beckerf9ca30d2019-02-26 11:38:29 +00002921 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Becker9ec3fe02019-07-01 17:36:12 +01002922#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Hanno Beckerf9ca30d2019-02-26 11:38:29 +00002923
Hanno Becker7d864c42019-09-19 16:51:41 +01002924 if( ( ret = mbedtls_ssl_session_reset( ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002925 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002926 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2927 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002928 goto exit;
2929 }
2930
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02002931 if( opt.reco_mode == 1 )
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02002932 {
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02002933 if( ( ret = mbedtls_ssl_session_load( &saved_session,
2934 session_data,
2935 session_data_len ) ) != 0 )
2936 {
2937 mbedtls_printf( " failed\n ! mbedtls_ssl_session_load returned -0x%x\n\n",
2938 -ret );
2939 goto exit;
2940 }
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02002941 }
2942
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03002943#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Hanno Becker7d864c42019-09-19 16:51:41 +01002944 if( ( ret = mbedtls_ssl_set_session( ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002945 {
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02002946 mbedtls_printf( " failed\n ! mbedtls_ssl_set_session returned -0x%x\n\n",
2947 -ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002948 goto exit;
2949 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03002950#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002951
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002952 if( ( ret = mbedtls_net_connect( &server_fd,
2953 opt.server_addr, opt.server_port,
2954 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2955 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002956 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002957 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
2958 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002959 goto exit;
2960 }
2961
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002962 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002963 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002964 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002965 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002966 if( ret != 0 )
2967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002969 -ret );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002970 goto exit;
2971 }
2972
Hanno Becker7d864c42019-09-19 16:51:41 +01002973 while( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002974 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002975 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002976 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002977 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002978 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002979 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2980 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002981 goto exit;
2982 }
2983 }
2984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002986
2987 goto send_request;
2988 }
2989
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002990 /*
2991 * Cleanup and exit
2992 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002993exit:
Hanno Becker7d864c42019-09-19 16:51:41 +01002994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00002996 if( ret != 0 )
2997 {
2998 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999 mbedtls_strerror( ret, error_buf, 100 );
3000 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00003001 }
3002#endif
3003
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003004 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker7d864c42019-09-19 16:51:41 +01003007 mbedtls_x509_crt_free( clicert );
3008 mbedtls_x509_crt_free( cacert );
3009 mbedtls_pk_free( pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02003010#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011 mbedtls_ssl_session_free( &saved_session );
Hanno Becker7d864c42019-09-19 16:51:41 +01003012 mbedtls_ssl_free( ssl );
3013 mbedtls_ssl_config_free( conf );
Hanno Becker7f1c8052019-08-26 13:30:13 +01003014#if defined(MBEDTLS_CTR_DRBG_C)
Hanno Becker7d864c42019-09-19 16:51:41 +01003015 mbedtls_ctr_drbg_free( ctr_drbg );
Hanno Becker7f1c8052019-08-26 13:30:13 +01003016#else
Hanno Becker7d864c42019-09-19 16:51:41 +01003017 mbedtls_hmac_drbg_free( hmac_drbg );
Hanno Becker7f1c8052019-08-26 13:30:13 +01003018#endif
Hanno Becker7d864c42019-09-19 16:51:41 +01003019 mbedtls_entropy_free( entropy );
Manuel Pégourié-Gonnard4bb1b992019-05-24 10:26:41 +02003020 if( session_data != NULL )
3021 mbedtls_platform_zeroize( session_data, session_data_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02003022 mbedtls_free( session_data );
Manuel Pégourié-Gonnardc9812292019-07-15 10:31:11 +02003023#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
3024 if( context_buf != NULL )
3025 mbedtls_platform_zeroize( context_buf, context_buf_len );
3026 mbedtls_free( context_buf );
3027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003028
Hanno Becker7d864c42019-09-19 16:51:41 +01003029 mbedtls_free( ssl );
3030 mbedtls_free( conf );
3031 mbedtls_free( entropy );
Teppo Järvelin8e0e4812019-10-21 10:33:11 +03003032 mbedtls_free( buf );
Hanno Becker7d864c42019-09-19 16:51:41 +01003033#if defined(MBEDTLS_CTR_DRBG_C)
3034 mbedtls_free( ctr_drbg );
3035#else
3036 mbedtls_free( hmac_drbg );
3037#endif
Teppo Järvelin8e0fe192019-10-16 07:43:45 +03003038#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker7d864c42019-09-19 16:51:41 +01003039 mbedtls_free( cacert );
3040 mbedtls_free( clicert );
3041 mbedtls_free( pkey );
Teppo Järvelin8e0fe192019-10-16 07:43:45 +03003042#endif
Paul Bakkercce9d772011-11-18 14:26:47 +00003043#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003045 fflush( stdout ); getchar();
3046#endif
3047
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02003048 // Shell can not handle large exit numbers -> 1 for errors
3049 if( ret < 0 )
3050 ret = 1;
3051
Paul Bakker5121ce52009-01-03 21:22:43 +00003052 return( ret );
3053}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
3055 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Hanno Becker7f1c8052019-08-26 13:30:13 +01003056 ( MBEDTLS_CTR_DRBG_C || MBEDTLS_HMAC_DRBG_C ) && MBEDTLS_TIMING_C */