blob: 3bf9e6243f6d32bdc2f9c3940c2030a8f25acdc7 [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");
Andrzej Kurek825ebd42020-05-18 11:47:25 -040056 mbedtls_exit( 0 );
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020057}
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
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400134#define DFL_SKIP_CLOSE_NOTIFY 0
Paul Bakker0e6975b2009-02-10 22:19:10 +0000135
Paul Bakker93c32b22014-04-25 13:40:05 +0200136#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
137#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if defined(MBEDTLS_X509_CRT_PARSE_C)
140#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000141#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100142 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
143 " default: \"\" (pre-loaded)\n" \
Hanno Becker1ce1a512019-05-01 11:16:28 +0100144 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100145 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
146 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
Hanno Becker1ce1a512019-05-01 11:16:28 +0100147 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100148 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
149 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000150 " key_file=%%s default: \"\" (pre-loaded)\n"
151#else
152#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 " No file operations available (MBEDTLS_FS_IO not defined)\n"
154#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200155#else
156#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200158
Hanno Beckera5a2b082019-05-15 14:03:01 +0100159#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7a7aa192019-04-09 17:24:19 +0100160#define USAGE_CID \
161 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
162 " default: 0 (disabled)\n" \
Hanno Becker96870292019-05-03 17:30:59 +0100163 " 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 +0100164 " default: same as 'cid' parameter\n" \
Hanno Becker7a7aa192019-04-09 17:24:19 +0100165 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
Hanno Becker96870292019-05-03 17:30:59 +0100166 " default: \"\"\n" \
167 " 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 +0100168 " default: same as 'cid_val' parameter\n"
Hanno Beckera5a2b082019-05-15 14:03:01 +0100169#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100170#define USAGE_CID ""
Hanno Beckera5a2b082019-05-15 14:03:01 +0100171#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200174#define USAGE_PSK \
175 " psk=%%s default: \"\" (in hex, without 0x)\n" \
176 " psk_identity=%%s default: \"Client_identity\"\n"
177#else
178#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200182#define USAGE_TICKETS \
183 " tickets=%%d default: 1 (enabled)\n"
184#else
185#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200189#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100190 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200191#else
192#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200196#define USAGE_MAX_FRAG_LEN \
197 " max_frag_len=%%d default: 16384 (tls default)\n" \
198 " options: 512, 1024, 2048, 4096\n"
199#else
200#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100204#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200205 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100206#else
207#define USAGE_RECSPLIT
208#endif
209
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200210#if defined(MBEDTLS_DHM_C)
211#define USAGE_DHMLEN \
212 " dhmlen=%%d default: (library default: 1024 bits)\n"
213#else
214#define USAGE_DHMLEN
215#endif
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200218#define USAGE_ALPN \
219 " alpn=%%s default: \"\" (disabled)\n" \
220 " example: spdy/1,http/1.1\n"
221#else
222#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200224
Hanno Beckerc1096e72019-06-19 12:30:41 +0100225#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Hanno Beckere6706e62017-05-15 16:05:15 +0100226#define USAGE_CURVES \
227 " curves=a,b,c,d default: \"default\" (library default)\n" \
228 " example: \"secp521r1,brainpoolP512r1\"\n" \
229 " - use \"none\" for empty list\n" \
230 " - see mbedtls_ecp_curve_list()\n" \
231 " for acceptable curve names\n"
232#else
233#define USAGE_CURVES ""
234#endif
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200237#define USAGE_DTLS \
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +0100238 " dtls=%%d default: 0 (TLS) (if both enabled)\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200239 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200240 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Becker4d615912018-08-14 13:33:30 +0100241 " mtu=%%d default: (library default: unlimited)\n" \
242 " dgram_packing=%%d default: 1 (allowed)\n" \
243 " allow or forbid packing of multiple\n" \
244 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200245#else
246#define USAGE_DTLS ""
247#endif
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200250#define USAGE_FALLBACK \
251 " fallback=0/1 default: (library default: off)\n"
252#else
253#define USAGE_FALLBACK ""
254#endif
255
Hanno Beckerf765ce62019-06-21 13:17:14 +0100256#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
257 !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET) && \
258 !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200259#define USAGE_EMS \
Jarno Lamsa41b35912019-06-10 15:51:11 +0300260 " extended_ms=0/1 default: (library default: on)\n" \
261 " enforce_extended_master_secret=0/1 default: (library default: off)\n"
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200262#else
263#define USAGE_EMS ""
264#endif
265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100267#define USAGE_ETM \
268 " etm=0/1 default: (library default: on)\n"
269#else
270#define USAGE_ETM ""
271#endif
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100274#define USAGE_RENEGO \
275 " renegotiation=%%d default: 0 (disabled)\n" \
276 " renegotiate=%%d default: 0 (disabled)\n"
277#else
278#define USAGE_RENEGO ""
279#endif
280
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200281#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
282#define USAGE_ECJPAKE \
283 " ecjpake_pw=%%s default: none (disabled)\n"
284#else
285#define USAGE_ECJPAKE ""
286#endif
287
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200288#if defined(MBEDTLS_ECP_RESTARTABLE)
289#define USAGE_ECRESTART \
290 " ec_max_ops=%%s default: library default (restart disabled)\n"
291#else
292#define USAGE_ECRESTART ""
293#endif
294
Jarno Lamsacf1b6722019-06-04 11:06:31 +0300295#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
296#define USAGE_SERIALIZATION \
Jarno Lamsab5ff6a42019-06-06 10:40:52 +0300297 " serialize=%%d default: 0 (do not serialize/deserialize)\n" \
Jarno Lamsa85c23802019-06-07 08:39:24 +0300298 " options: 1 (serialize)\n" \
299 " 2 (serialize with re-initialization)\n"
Jarno Lamsacf1b6722019-06-04 11:06:31 +0300300#else
301#define USAGE_SERIALIZATION ""
302#endif
303
Hanno Beckeracd4fc02019-06-12 16:40:50 +0100304#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
305#define USAGE_AUTH_MODE \
306 " auth_mode=%%s default: (library default: none)\n" \
307 " options: none, optional, required\n"
308#else
309#define USAGE_AUTH_MODE ""
310#endif
311
Hanno Beckerb0b2b672019-06-12 16:58:10 +0100312#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
313#define USAGE_ALLOW_LEGACY_RENEGO " allow_legacy=%%d default: (library default: no)\n"
314#else
315#define USAGE_ALLOW_LEGACY_RENEGO ""
316#endif
317
Hanno Becker1f835fa2019-06-13 10:14:59 +0100318#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
319#define USAGE_READ_TIMEOUT \
320 " read_timeout=%%d default: 0 ms (no timeout)\n"
321#else
322#define USAGE_READ_TIMEOUT ""
323#endif
324
Hanno Becker72e5ffc2019-07-05 11:35:08 +0100325#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
326 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) || \
327 !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
328 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
329#define USAGE_MAX_VERSION " max_version=%%s default: (library default: tls1_2)\n"
330#define USAGE_MIN_VERSION " min_version=%%s default: (library default: tls1)\n"
331#define USAGE_FORCE_VERSION " force_version=%%s default: \"\" (none)\n" \
332 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n"
333#else
334#define USAGE_MAX_VERSION ""
335#define USAGE_MIN_VERSION ""
336#define USAGE_FORCE_VERSION ""
337#endif
338
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400339/* USAGE is arbitrarily split to stay under the portable string literal
340 * length limit: 4095 bytes in C99. */
341#define USAGE1 \
Paul Bakker67968392010-07-18 08:28:20 +0000342 "\n usage: ssl_client2 param=<>...\n" \
343 "\n acceptable parameters:\n" \
344 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100345 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000346 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000347 " request_page=%%s default: \".\"\n" \
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +0100348 " request_size=%%d default: about 34 (basic request)\n" \
349 " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
350 " If 0, in the first exchange only an empty\n" \
351 " application data message is sent followed by\n" \
352 " a second non-empty message before attempting\n" \
353 " to read a response from the server\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100354 " debug_level=%%d default: 0 (disabled)\n" \
355 " nbio=%%d default: 0 (blocking I/O)\n" \
356 " options: 1 (non-blocking), 2 (added delays)\n" \
357 " event=%%d default: 0 (loop)\n" \
358 " options: 1 (level-triggered, implies nbio=1),\n" \
Hanno Becker1f835fa2019-06-13 10:14:59 +0100359 USAGE_READ_TIMEOUT \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200360 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400361 " skip_close_notify=%%d default: 0 (send close_notify)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100362 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200363 USAGE_DTLS \
Hanno Becker7a7aa192019-04-09 17:24:19 +0100364 USAGE_CID \
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400365 "\n"
366
367#define USAGE2 \
Hanno Beckeracd4fc02019-06-12 16:40:50 +0100368 USAGE_AUTH_MODE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100369 USAGE_IO \
370 "\n" \
371 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200372 USAGE_ECJPAKE \
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200373 USAGE_ECRESTART \
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400374 "\n"
375
376#define USAGE3 \
Hanno Beckerb0b2b672019-06-12 16:58:10 +0100377 USAGE_ALLOW_LEGACY_RENEGO \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100378 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200379 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +0200380 " reconnect=%%d number of reconnections using session resumption\n" \
381 " default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200382 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +0200383 " reco_mode=%%d 0: copy session, 1: serialize session\n" \
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +0200384 " default: 1\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200385 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200386 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100387 USAGE_MAX_FRAG_LEN \
388 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200389 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200390 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200391 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100392 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100393 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100394 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200395 USAGE_DHMLEN \
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400396 "\n"
397#define USAGE4 \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100398 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200399 " allow_sha1=%%d default: 0\n" \
Hanno Becker72e5ffc2019-07-05 11:35:08 +0100400 USAGE_MIN_VERSION \
401 USAGE_MAX_VERSION \
402 USAGE_FORCE_VERSION \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000403 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000404 " force_ciphersuite=<name> default: all enabled\n"\
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100405 " query_config=<name> return 0 if the specified\n" \
406 " configuration macro is defined and 1\n" \
407 " otherwise. The expansion of the macro\n" \
408 " is printed if it is defined\n" \
Jarno Lamsacf1b6722019-06-04 11:06:31 +0300409 USAGE_SERIALIZATION \
Paul Bakker51936882011-02-20 16:05:58 +0000410 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000411
Hanno Becker8651a432017-06-09 16:13:22 +0100412#define ALPN_LIST_SIZE 10
413#define CURVE_LIST_SIZE 20
414
Simon Butcher63cb97e2018-12-06 17:43:31 +0000415
Rich Evans85b05ec2015-02-12 11:37:29 +0000416/*
417 * global options
418 */
419struct options
420{
421 const char *server_name; /* hostname of the server (client only) */
422 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200423 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000424 int debug_level; /* level of debugging */
425 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100426 int event; /* loop or event-driven IO? level or edge triggered? */
427 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000428 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000429 const char *request_page; /* page on server to request */
430 int request_size; /* pad request with header to requested size */
431 const char *ca_file; /* the file with the CA certificate(s) */
432 const char *ca_path; /* the path with the CA certificate(s) reside */
433 const char *crt_file; /* the file with the client certificate */
434 const char *key_file; /* the file with the client key */
435 const char *psk; /* the pre-shared key */
436 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200437 const char *ecjpake_pw; /* the EC J-PAKE password */
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200438 int ec_max_ops; /* EC consecutive operations limit */
Rich Evans85b05ec2015-02-12 11:37:29 +0000439 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
440 int renegotiation; /* enable / disable renegotiation */
441 int allow_legacy; /* allow legacy renegotiation */
442 int renegotiate; /* attempt renegotiation? */
443 int renego_delay; /* delay before enforcing renegotiation */
444 int exchanges; /* number of data exchanges */
445 int min_version; /* minimum protocol version accepted */
446 int max_version; /* maximum protocol version accepted */
447 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200448 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000449 int auth_mode; /* verify mode for connection */
450 unsigned char mfl_code; /* code for maximum fragment length */
451 int trunc_hmac; /* negotiate truncated hmac or not */
452 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200453 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000454 int reconnect; /* attempt to resume session */
455 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +0200456 int reco_mode; /* how to keep the session around */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200457 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000458 int tickets; /* enable / disable session tickets */
Hanno Beckere6706e62017-05-15 16:05:15 +0100459 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000460 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000461 int transport; /* TLS or DTLS? */
462 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
463 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200464 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Rich Evans85b05ec2015-02-12 11:37:29 +0000465 int fallback; /* is this a fallback connection? */
Hanno Becker4d615912018-08-14 13:33:30 +0100466 int dgram_packing; /* allow/forbid datagram packing */
Rich Evans85b05ec2015-02-12 11:37:29 +0000467 int extended_ms; /* negotiate extended master secret? */
Jarno Lamsa41b35912019-06-10 15:51:11 +0300468 int enforce_extended_master_secret; /* Enforce the usage of extended
469 * master secret */
Rich Evans85b05ec2015-02-12 11:37:29 +0000470 int etm; /* negotiate encrypt then mac? */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100471 int cid_enabled; /* whether to use the CID extension or not */
Hanno Becker96870292019-05-03 17:30:59 +0100472 int cid_enabled_renego; /* whether to use the CID extension or not
473 * during renegotiation */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100474 const char *cid_val; /* the CID to use for incoming messages */
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +0300475 int serialize; /* serialize/deserialize connection */
Hanno Becker96870292019-05-03 17:30:59 +0100476 const char *cid_val_renego; /* the CID to use for incoming messages
477 * after renegotiation */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400478 int skip_close_notify; /* skip sending the close_notify alert */
Rich Evans85b05ec2015-02-12 11:37:29 +0000479} opt;
480
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100481int query_config( const char *config );
482
Hanno Becker14a4a442019-07-02 17:00:34 +0100483#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200484static void my_debug( void *ctx, int level,
485 const char *file, int line,
486 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000487{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200488 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000489
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200490 /* Extract basename from file */
491 for( p = basename = file; *p != '\0'; p++ )
492 if( *p == '/' || *p == '\\' )
493 basename = p + 1;
494
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100495 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s",
496 basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000497 fflush( (FILE *) ctx );
498}
Hanno Becker14a4a442019-07-02 17:00:34 +0100499#endif /* MBEDTLS_DEBUG_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000500
Hanno Beckera58a8962019-06-13 16:11:15 +0100501
502#if !defined(MBEDTLS_SSL_CONF_RECV) && \
503 !defined(MBEDTLS_SSL_CONF_SEND) && \
504 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Rich Evans85b05ec2015-02-12 11:37:29 +0000505/*
506 * Test recv/send functions that make sure each try returns
507 * WANT_READ/WANT_WRITE at least once before sucesseding
508 */
Hanno Becker14219fe2019-07-03 17:02:43 +0100509
510static int delayed_recv( void *ctx, unsigned char *buf, size_t len )
Rich Evans85b05ec2015-02-12 11:37:29 +0000511{
512 static int first_try = 1;
513 int ret;
514
515 if( first_try )
516 {
517 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100518 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000519 }
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100522 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000523 first_try = 1; /* Next call will be a new operation */
524 return( ret );
525}
526
Hanno Becker14219fe2019-07-03 17:02:43 +0100527static int delayed_send( void *ctx, const unsigned char *buf, size_t len )
Rich Evans85b05ec2015-02-12 11:37:29 +0000528{
529 static int first_try = 1;
530 int ret;
531
532 if( first_try )
533 {
534 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100535 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000536 }
537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100539 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000540 first_try = 1; /* Next call will be a new operation */
541 return( ret );
542}
543
Hanno Becker14219fe2019-07-03 17:02:43 +0100544typedef struct
545{
546 mbedtls_ssl_context *ssl;
547 mbedtls_net_context *net;
548} io_ctx_t;
549
Hanno Beckerde9e36e2019-07-03 17:14:41 +0100550#if defined(MBEDTLS_SSL_RECORD_CHECKING)
551static int ssl_check_record( mbedtls_ssl_context const *ssl,
552 unsigned char const *buf, size_t len )
553{
554 int ret;
555 unsigned char *tmp_buf;
556
557 tmp_buf = mbedtls_calloc( 1, len );
558 if( tmp_buf == NULL )
559 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
560 memcpy( tmp_buf, buf, len );
561
562 ret = mbedtls_ssl_check_record( ssl, tmp_buf, len );
563 if( ret != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE )
564 {
565 int ret_repeated;
566
567 /* Test-only: Make sure that mbedtls_ssl_check_record()
568 * doesn't alter state. */
569 memcpy( tmp_buf, buf, len ); /* Restore buffer */
570 ret_repeated = mbedtls_ssl_check_record( ssl, tmp_buf, len );
571 if( ret != ret_repeated )
572 {
Hanno Becker83b8c3b2019-07-24 13:59:07 +0100573 mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" );
574 return( -1 );
Hanno Beckerde9e36e2019-07-03 17:14:41 +0100575 }
576
577 switch( ret )
578 {
579 case 0:
580 break;
581
582 case MBEDTLS_ERR_SSL_INVALID_RECORD:
583 if( opt.debug_level > 1 )
584 mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" );
585 break;
586
587 case MBEDTLS_ERR_SSL_INVALID_MAC:
588 if( opt.debug_level > 1 )
589 mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" );
590 break;
591
592 case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD:
593 if( opt.debug_level > 1 )
594 mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" );
595 break;
596
597 default:
598 mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", -ret );
599 return( -1 );
600 }
601
602 /* Regardless of the outcome, forward the record to the stack. */
603 }
604
Hanno Beckerde9e36e2019-07-03 17:14:41 +0100605 mbedtls_free( tmp_buf );
606
607 return( 0 );
608}
609#endif /* MBEDTLS_SSL_RECORD_CHECKING */
610
Hanno Becker14219fe2019-07-03 17:02:43 +0100611static int recv_cb( void *ctx, unsigned char *buf, size_t len )
612{
613 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
614 size_t recv_len;
615 int ret;
616
617 if( opt.nbio == 2 )
618 ret = delayed_recv( io_ctx->net, buf, len );
619 else
620 ret = mbedtls_net_recv( io_ctx->net, buf, len );
621 if( ret < 0 )
622 return( ret );
623 recv_len = (size_t) ret;
624
625 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
626 {
627 /* Here's the place to do any datagram/record checking
628 * in between receiving the packet from the underlying
629 * transport and passing it on to the TLS stack. */
Hanno Beckerde9e36e2019-07-03 17:14:41 +0100630#if defined(MBEDTLS_SSL_RECORD_CHECKING)
631 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
632 return( -1 );
633#endif /* MBEDTLS_SSL_RECORD_CHECKING */
Hanno Becker14219fe2019-07-03 17:02:43 +0100634 }
635
636 return( (int) recv_len );
637}
638
639static int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len,
640 uint32_t timeout )
641{
642 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
643 int ret;
644 size_t recv_len;
645
646 ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout );
647 if( ret < 0 )
648 return( ret );
649 recv_len = (size_t) ret;
650
651 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
652 {
653 /* Here's the place to do any datagram/record checking
654 * in between receiving the packet from the underlying
655 * transport and passing it on to the TLS stack. */
Hanno Beckerde9e36e2019-07-03 17:14:41 +0100656#if defined(MBEDTLS_SSL_RECORD_CHECKING)
657 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
658 return( -1 );
659#endif /* MBEDTLS_SSL_RECORD_CHECKING */
Hanno Becker14219fe2019-07-03 17:02:43 +0100660 }
661
662 return( (int) recv_len );
663}
664
665static int send_cb( void *ctx, unsigned char const *buf, size_t len )
666{
667 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
668
669 if( opt.nbio == 2 )
670 return( delayed_send( io_ctx->net, buf, len ) );
671
672 return( mbedtls_net_send( io_ctx->net, buf, len ) );
673}
Manuel Pégourié-Gonnard2b29a372019-07-30 17:07:38 +0200674#endif /* !MBEDTLS_SSL_CONF_RECV &&
675 !MBEDTLS_SSL_CONF_SEND &&
676 !MBEDTLS_SSL_CONF_RECV_TIMEOUT */
Hanno Becker14219fe2019-07-03 17:02:43 +0100677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100679
680#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Hanno Beckerf9ca30d2019-02-26 11:38:29 +0000681static unsigned char peer_crt_info[1024];
Hanno Becker975c4632019-02-25 17:43:18 +0000682
Rich Evans85b05ec2015-02-12 11:37:29 +0000683/*
684 * Enabled if debug_level > 1 in code below
685 */
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100686static int my_verify( void *data, mbedtls_x509_crt *crt,
687 int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000688{
Hanno Becker02a21932019-06-10 15:08:43 +0100689#if !defined(MBEDTLS_X509_REMOVE_INFO)
Rich Evans85b05ec2015-02-12 11:37:29 +0000690 char buf[1024];
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600691#endif
Rich Evans85b05ec2015-02-12 11:37:29 +0000692 ((void) data);
693
Hanno Becker02a21932019-06-10 15:08:43 +0100694#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Hanno Becker975c4632019-02-25 17:43:18 +0000696 if( depth == 0 )
697 memcpy( peer_crt_info, buf, sizeof( buf ) );
698
699 if( opt.debug_level == 0 )
700 return( 0 );
701
702 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_printf( "%s", buf );
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600704#else
705 ((void) crt);
706 ((void) depth);
707#endif
Rich Evans85b05ec2015-02-12 11:37:29 +0000708
Rich Evans85b05ec2015-02-12 11:37:29 +0000709 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100711 else
712 {
Hanno Becker02a21932019-06-10 15:08:43 +0100713#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100714 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
715 mbedtls_printf( "%s\n", buf );
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600716#endif
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100717 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000718
719 return( 0 );
720}
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100721#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200722
Hanno Becker7f1c8052019-08-26 13:30:13 +0100723#endif /* MBEDTLS_X509_CRT_PARSE_C */
724
Hanno Becker10da2a32019-08-28 15:08:27 +0100725#if ( defined(MBEDTLS_X509_CRT_PARSE_C) && \
726 !defined(MBEDTLS_SSL_CONF_SINGLE_HASH) ) || \
727 !defined(MBEDTLS_CTR_DRBG_C)
Hanno Becker7f1c8052019-08-26 13:30:13 +0100728static int available_hashes[] = {
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200729#if defined(MBEDTLS_SHA512_C)
730 MBEDTLS_MD_SHA512,
731 MBEDTLS_MD_SHA384,
732#endif
733#if defined(MBEDTLS_SHA256_C)
734 MBEDTLS_MD_SHA256,
735 MBEDTLS_MD_SHA224,
736#endif
737#if defined(MBEDTLS_SHA1_C)
738 /* Allow SHA-1 as we use it extensively in tests. */
739 MBEDTLS_MD_SHA1,
740#endif
741 MBEDTLS_MD_NONE
742};
Hanno Becker10da2a32019-08-28 15:08:27 +0100743#endif /* ( MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_CONF_SINGLE_HASH ) ||
744 !MBEDTLS_CTR_DRBG_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000745
Hanno Becker16970d22017-10-10 15:56:37 +0100746/*
747 * Wait for an event from the underlying transport or the timer
748 * (Used in event-driven IO mode).
749 */
750#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000751int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000752 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100753#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000754int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000755 mbedtls_timing_delay_context *timer,
756 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100757#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000758{
Hanno Becker16970d22017-10-10 15:56:37 +0100759
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000760 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +0100761 int poll_type = 0;
762
763 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
764 poll_type = MBEDTLS_NET_POLL_WRITE;
765 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
766 poll_type = MBEDTLS_NET_POLL_READ;
767#if !defined(MBEDTLS_TIMING_C)
768 else
Hanno Beckeref527962018-03-15 15:49:24 +0000769 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100770#endif
771
772 while( 1 )
773 {
Hanno Becker197a91c2017-10-31 10:58:53 +0000774 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +0100775#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +0000776 if( timer != NULL &&
777 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +0100778 {
Hanno Becker16970d22017-10-10 15:56:37 +0100779 break;
780 }
Hanno Becker197a91c2017-10-31 10:58:53 +0000781#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +0100782
Hanno Becker197a91c2017-10-31 10:58:53 +0000783 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000784 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +0100785 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000786 ret = mbedtls_net_poll( fd, poll_type, 0 );
787 if( ret < 0 )
788 return( ret );
789 if( ret == poll_type )
790 break;
Hanno Becker16970d22017-10-10 15:56:37 +0100791 }
792 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000793
794 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100795}
796
Hanno Beckerec370302019-04-09 17:12:56 +0100797/* Unhexify `hex` into `dst`. `dst` must have
798 * size at least `strlen( hex ) / 2`. */
Hanno Becker7a7aa192019-04-09 17:24:19 +0100799int unhexify( char const *hex, unsigned char *dst )
Hanno Beckerec370302019-04-09 17:12:56 +0100800{
801 unsigned char c;
802 size_t j;
803 size_t len = strlen( hex );
804
805 if( len % 2 != 0 )
806 return( -1 );
807
808 for( j = 0; j < len; j += 2 )
809 {
810 c = hex[j];
811 if( c >= '0' && c <= '9' )
812 c -= '0';
813 else if( c >= 'a' && c <= 'f' )
814 c -= 'a' - 10;
815 else if( c >= 'A' && c <= 'F' )
816 c -= 'A' - 10;
817 else
818 return( -1 );
819 dst[ j / 2 ] = c << 4;
820
821 c = hex[j + 1];
822 if( c >= '0' && c <= '9' )
823 c -= '0';
824 else if( c >= 'a' && c <= 'f' )
825 c -= 'a' - 10;
826 else if( c >= 'A' && c <= 'F' )
827 c -= 'A' - 10;
828 else
829 return( -1 );
830 dst[ j / 2 ] |= c;
831 }
832
833 return( 0 );
834}
835
Hanno Beckera5a2b082019-05-15 14:03:01 +0100836#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +0100837int report_cid_usage( mbedtls_ssl_context *ssl,
838 const char *additional_description )
839{
840 int ret;
841 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
842 size_t peer_cid_len;
843 int cid_negotiated;
844
845 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
846 return( 0 );
847
Hanno Beckerac363882019-05-22 16:59:25 +0100848 /* Check if the use of a CID has been negotiated,
849 * but don't ask for the CID value and length.
850 *
851 * Note: Here and below, we're demonstrating the various ways
852 * in which mbedtls_ssl_get_peer_cid() can be called,
853 * depending on whether or not the length/value of the
854 * peer's CID is needed.
855 *
856 * An actual application, however, should use
857 * just one call to mbedtls_ssl_get_peer_cid(). */
Hanno Becker96870292019-05-03 17:30:59 +0100858 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
Hanno Beckerac363882019-05-22 16:59:25 +0100859 NULL, NULL );
Hanno Becker96870292019-05-03 17:30:59 +0100860 if( ret != 0 )
861 {
862 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
863 -ret );
864 return( ret );
865 }
866
867 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
868 {
869 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
870 {
871 mbedtls_printf( "(%s) Use of Connection ID was rejected by the server.\n",
872 additional_description );
873 }
874 }
875 else
876 {
877 size_t idx=0;
878 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
879 additional_description );
Hanno Beckerac363882019-05-22 16:59:25 +0100880
881 /* Ask for just the length of the peer's CID. */
882 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
883 NULL, &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 }
890
891 /* Ask for just length + value of the peer's CID. */
892 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
893 peer_cid, &peer_cid_len );
894 if( ret != 0 )
895 {
896 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
897 -ret );
898 return( ret );
899 }
Hanno Becker96870292019-05-03 17:30:59 +0100900 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
901 additional_description,
902 (unsigned) peer_cid_len );
903 while( idx < peer_cid_len )
904 {
905 mbedtls_printf( "%02x ", peer_cid[ idx ] );
906 idx++;
907 }
908 mbedtls_printf( "\n" );
909 }
910
911 return( 0 );
912}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100913#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker96870292019-05-03 17:30:59 +0100914
Hanno Becker572d4482019-07-23 13:47:53 +0100915#if defined(MBEDTLS_SSL_CONF_RNG)
916int rng_wrap( void *ctx, unsigned char *dst, size_t len );
917
Hanno Becker7f1c8052019-08-26 13:30:13 +0100918#if defined(MBEDTLS_CTR_DRBG_C)
Hanno Becker572d4482019-07-23 13:47:53 +0100919mbedtls_ctr_drbg_context *rng_ctx_global = NULL;
Hanno Becker7f1c8052019-08-26 13:30:13 +0100920#else
921mbedtls_hmac_drbg_context *rng_ctx_global = NULL;
922#endif /* MBEDTLS_CTR_DRBG_C */
923
Hanno Becker572d4482019-07-23 13:47:53 +0100924int rng_wrap( void *ctx, unsigned char *dst, size_t len )
925{
926 /* We expect the NULL parameter here. */
927 if( ctx != NULL )
928 return( -1 );
929
Hanno Becker7f1c8052019-08-26 13:30:13 +0100930#if defined(MBEDTLS_CTR_DRBG_C)
Hanno Becker572d4482019-07-23 13:47:53 +0100931 return( mbedtls_ctr_drbg_random( rng_ctx_global, dst, len ) );
Hanno Becker7f1c8052019-08-26 13:30:13 +0100932#else
933 return( mbedtls_hmac_drbg_random( rng_ctx_global, dst, len ) );
934#endif
Hanno Becker572d4482019-07-23 13:47:53 +0100935}
936#endif /* MBEDTLS_SSL_CONF_RNG */
937
Jarno Lamsa642596e2019-10-04 12:52:42 +0300938#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
939int mbedtls_hardware_poll( void *data, unsigned char *output,
940 size_t len, size_t *olen )
941{
942 size_t i;
943 (void) data;
944 for( i = 0; i < len; ++i )
945 output[i] = rand();
946 *olen = len;
947 return( 0 );
948}
949#endif
950
Paul Bakker9caf2d22010-02-18 19:37:19 +0000951int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000952{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200953 int ret = 0, len, tail_len, i, written, frags, retry_left;
954 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnard2b29a372019-07-30 17:07:38 +0200955#if !defined(MBEDTLS_SSL_CONF_RECV) && \
956 !defined(MBEDTLS_SSL_CONF_SEND) && \
957 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Hanno Becker14219fe2019-07-03 17:02:43 +0100958 io_ctx_t io_ctx;
Manuel Pégourié-Gonnard2b29a372019-07-30 17:07:38 +0200959#endif
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100960
Teppo Järvelin8e0e4812019-10-21 10:33:11 +0300961 unsigned char *buf = NULL;
962 unsigned int main_buf_len = 0;
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
965 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200966 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200967#endif
Hanno Becker7a7aa192019-04-09 17:24:19 +0100968
Hanno Beckera5a2b082019-05-15 14:03:01 +0100969#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7a7aa192019-04-09 17:24:19 +0100970 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker96870292019-05-03 17:30:59 +0100971 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker7a7aa192019-04-09 17:24:19 +0100972 size_t cid_len = 0;
Hanno Becker96870292019-05-03 17:30:59 +0100973 size_t cid_renego_len = 0;
Hanno Becker7a7aa192019-04-09 17:24:19 +0100974#endif
975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100977 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200978#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +0100979#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Hanno Becker8651a432017-06-09 16:13:22 +0100980 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100981 const mbedtls_ecp_curve_info *curve_cur;
982#endif
983
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200984 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000985
Gilles Peskineef86ab22017-05-05 18:59:02 +0200986#if defined(MBEDTLS_X509_CRT_PARSE_C)
987 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
988#endif
Hanno Becker7d864c42019-09-19 16:51:41 +0100989 mbedtls_entropy_context *entropy = NULL;
Hanno Becker7f1c8052019-08-26 13:30:13 +0100990#if defined(MBEDTLS_CTR_DRBG_C)
Hanno Becker7d864c42019-09-19 16:51:41 +0100991 mbedtls_ctr_drbg_context *ctr_drbg = NULL;
Hanno Becker7f1c8052019-08-26 13:30:13 +0100992#else
Hanno Becker7d864c42019-09-19 16:51:41 +0100993 mbedtls_hmac_drbg_context *hmac_drbg = NULL;
Hanno Becker7f1c8052019-08-26 13:30:13 +0100994#endif
Hanno Becker7d864c42019-09-19 16:51:41 +0100995 mbedtls_ssl_context *ssl;
996 mbedtls_ssl_config *conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +0200998 unsigned char *session_data = NULL;
999 size_t session_data_len = 0;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001000#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001001 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001002#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +02001004 uint32_t flags;
Hanno Becker7d864c42019-09-19 16:51:41 +01001005 mbedtls_x509_crt *cacert = NULL;
1006 mbedtls_x509_crt *clicert = NULL;
1007 mbedtls_pk_context *pkey = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +02001008#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +00001009 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +00001010 const int *list;
Manuel Pégourié-Gonnardc9812292019-07-15 10:31:11 +02001011#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1012 unsigned char *context_buf = NULL;
1013 size_t context_buf_len;
1014#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +00001015
Hanno Becker7d864c42019-09-19 16:51:41 +01001016 ssl = mbedtls_calloc( 1, sizeof( *ssl ) );
1017 conf = mbedtls_calloc( 1, sizeof( *conf ) );
1018 entropy = mbedtls_calloc( 1, sizeof( *entropy ) );
1019#if defined(MBEDTLS_CTR_DRBG_C)
1020 ctr_drbg = mbedtls_calloc( 1, sizeof( *ctr_drbg ) );
1021#else
1022 hmac_drbg = mbedtls_calloc( 1, sizeof( *hmac_drbg ) );
1023#endif
Teppo Järvelin8e0fe192019-10-16 07:43:45 +03001024#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker7d864c42019-09-19 16:51:41 +01001025 cacert = mbedtls_calloc( 1, sizeof( *cacert ) );
1026 clicert = mbedtls_calloc( 1, sizeof( *clicert ) );
1027 pkey = mbedtls_calloc( 1, sizeof( *pkey ) );
Teppo Järvelin8e0fe192019-10-16 07:43:45 +03001028#endif
Hanno Becker7d864c42019-09-19 16:51:41 +01001029
Teppo Järvelin8e0fe192019-10-16 07:43:45 +03001030 if( ssl == NULL || entropy == NULL ||
1031#if defined(MBEDTLS_X509_CRT_PARSE_C)
1032 cacert == NULL ||
1033 clicert== NULL || pkey == NULL ||
1034#endif
Hanno Becker7d864c42019-09-19 16:51:41 +01001035#if defined(MBEDTLS_CTR_DRBG_C)
1036 ctr_drbg == NULL ||
1037#else
1038 hmac_drbg == NULL ||
1039#endif
Teppo Järvelin8e0fe192019-10-16 07:43:45 +03001040
1041 conf == NULL)
Hanno Becker7d864c42019-09-19 16:51:41 +01001042 {
1043 goto exit;
1044 }
1045
Paul Bakker1a207ec2011-02-06 13:22:40 +00001046 /*
1047 * Make sure memory references are valid.
1048 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001049 mbedtls_net_init( &server_fd );
Hanno Becker7d864c42019-09-19 16:51:41 +01001050 mbedtls_ssl_init( ssl );
1051 mbedtls_ssl_config_init( conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Hanno Becker7f1c8052019-08-26 13:30:13 +01001053#if defined(MBEDTLS_CTR_DRBG_C)
Hanno Becker7d864c42019-09-19 16:51:41 +01001054 mbedtls_ctr_drbg_init( ctr_drbg );
Hanno Becker7f1c8052019-08-26 13:30:13 +01001055#else
Hanno Becker7d864c42019-09-19 16:51:41 +01001056 mbedtls_hmac_drbg_init( hmac_drbg );
Hanno Becker7f1c8052019-08-26 13:30:13 +01001057#endif /* MBEDTLS_CTR_DRBG_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker7d864c42019-09-19 16:51:41 +01001059 mbedtls_x509_crt_init( cacert );
1060 mbedtls_x509_crt_init( clicert );
1061 mbedtls_pk_init( pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001062#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +02001064 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001065#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +00001066
Paul Bakker9caf2d22010-02-18 19:37:19 +00001067 if( argc == 0 )
1068 {
1069 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +00001070 if( ret == 0 )
1071 ret = 1;
1072
Andrzej Kurek825ebd42020-05-18 11:47:25 -04001073 mbedtls_printf( USAGE1 );
1074 mbedtls_printf( USAGE2 );
1075 mbedtls_printf( USAGE3 );
1076 mbedtls_printf( USAGE4 );
Paul Bakker51936882011-02-20 16:05:58 +00001077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +00001079 while( *list )
1080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001082 list++;
1083 if( !*list )
1084 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +00001086 list++;
1087 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +00001089 goto exit;
1090 }
1091
1092 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001093 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001094 opt.server_port = DFL_SERVER_PORT;
1095 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker7a7aa192019-04-09 17:24:19 +01001096 opt.cid_enabled = DFL_CID_ENABLED;
1097 opt.cid_val = DFL_CID_VALUE;
Hanno Becker96870292019-05-03 17:30:59 +01001098 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
1099 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001100 opt.nbio = DFL_NBIO;
Hanno Becker16970d22017-10-10 15:56:37 +01001101 opt.event = DFL_EVENT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001102 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001103 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001104 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +02001105 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +00001106 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +00001107 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +00001108 opt.crt_file = DFL_CRT_FILE;
1109 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001110 opt.psk = DFL_PSK;
1111 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001112 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001113 opt.ec_max_ops = DFL_EC_MAX_OPS;
Paul Bakker51936882011-02-20 16:05:58 +00001114 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +00001115 opt.renegotiation = DFL_RENEGOTIATION;
1116 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001117 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001118 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001119 opt.min_version = DFL_MIN_VERSION;
1120 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001121 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001122 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001123 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001124 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001125 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001126 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001127 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001128 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001129 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02001130 opt.reco_mode = DFL_RECO_MODE;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001131 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001132 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001133 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001134 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001135 opt.transport = DFL_TRANSPORT;
1136 opt.hs_to_min = DFL_HS_TO_MIN;
1137 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001138 opt.dtls_mtu = DFL_DTLS_MTU;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001139 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001140 opt.extended_ms = DFL_EXTENDED_MS;
Jarno Lamsa41b35912019-06-10 15:51:11 +03001141 opt.enforce_extended_master_secret = DFL_EXTENDED_MS_ENFORCE;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001142 opt.etm = DFL_ETM;
Hanno Becker4d615912018-08-14 13:33:30 +01001143 opt.dgram_packing = DFL_DGRAM_PACKING;
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03001144 opt.serialize = DFL_SERIALIZE;
Andrzej Kurek825ebd42020-05-18 11:47:25 -04001145 opt.skip_close_notify = DFL_SKIP_CLOSE_NOTIFY;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001146
1147 for( i = 1; i < argc; i++ )
1148 {
Paul Bakker9caf2d22010-02-18 19:37:19 +00001149 p = argv[i];
1150 if( ( q = strchr( p, '=' ) ) == NULL )
1151 goto usage;
1152 *q++ = '\0';
1153
1154 if( strcmp( p, "server_name" ) == 0 )
1155 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001156 else if( strcmp( p, "server_addr" ) == 0 )
1157 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001158 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001159 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001160 else if( strcmp( p, "dtls" ) == 0 )
1161 {
1162 int t = atoi( q );
1163 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001165 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001167 else
1168 goto usage;
1169 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001170 else if( strcmp( p, "debug_level" ) == 0 )
1171 {
1172 opt.debug_level = atoi( q );
1173 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1174 goto usage;
1175 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001176 else if( strcmp( p, "nbio" ) == 0 )
1177 {
1178 opt.nbio = atoi( q );
1179 if( opt.nbio < 0 || opt.nbio > 2 )
1180 goto usage;
1181 }
Hanno Becker16970d22017-10-10 15:56:37 +01001182 else if( strcmp( p, "event" ) == 0 )
1183 {
1184 opt.event = atoi( q );
1185 if( opt.event < 0 || opt.event > 2 )
1186 goto usage;
1187 }
Hanno Becker1f835fa2019-06-13 10:14:59 +01001188#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001189 else if( strcmp( p, "read_timeout" ) == 0 )
1190 opt.read_timeout = atoi( q );
Hanno Becker1f835fa2019-06-13 10:14:59 +01001191#endif
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001192 else if( strcmp( p, "max_resend" ) == 0 )
1193 {
1194 opt.max_resend = atoi( q );
1195 if( opt.max_resend < 0 )
1196 goto usage;
1197 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001198 else if( strcmp( p, "request_page" ) == 0 )
1199 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +02001200 else if( strcmp( p, "request_size" ) == 0 )
1201 {
1202 opt.request_size = atoi( q );
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001203 if( opt.request_size < 0 ||
1204 opt.request_size > MAX_REQUEST_SIZE )
Paul Bakker93c32b22014-04-25 13:40:05 +02001205 goto usage;
1206 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001207 else if( strcmp( p, "ca_file" ) == 0 )
1208 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +00001209 else if( strcmp( p, "ca_path" ) == 0 )
1210 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +00001211 else if( strcmp( p, "crt_file" ) == 0 )
1212 opt.crt_file = q;
1213 else if( strcmp( p, "key_file" ) == 0 )
1214 opt.key_file = q;
Hanno Beckera5a2b082019-05-15 14:03:01 +01001215#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7a7aa192019-04-09 17:24:19 +01001216 else if( strcmp( p, "cid" ) == 0 )
1217 {
1218 opt.cid_enabled = atoi( q );
1219 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
1220 goto usage;
1221 }
Hanno Becker96870292019-05-03 17:30:59 +01001222 else if( strcmp( p, "cid_renego" ) == 0 )
1223 {
1224 opt.cid_enabled_renego = atoi( q );
1225 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
1226 goto usage;
1227 }
Hanno Becker7a7aa192019-04-09 17:24:19 +01001228 else if( strcmp( p, "cid_val" ) == 0 )
1229 {
1230 opt.cid_val = q;
1231 }
Hanno Becker96870292019-05-03 17:30:59 +01001232 else if( strcmp( p, "cid_val_renego" ) == 0 )
1233 {
1234 opt.cid_val_renego = q;
1235 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001236#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001237 else if( strcmp( p, "psk" ) == 0 )
1238 opt.psk = q;
1239 else if( strcmp( p, "psk_identity" ) == 0 )
1240 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001241 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1242 opt.ecjpake_pw = q;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001243 else if( strcmp( p, "ec_max_ops" ) == 0 )
1244 opt.ec_max_ops = atoi( q );
Paul Bakker51936882011-02-20 16:05:58 +00001245 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +00001248
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001249 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +00001250 {
1251 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +00001252 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +00001253 }
Paul Bakker51936882011-02-20 16:05:58 +00001254 opt.force_ciphersuite[1] = 0;
1255 }
Paul Bakker48916f92012-09-16 19:57:18 +00001256 else if( strcmp( p, "renegotiation" ) == 0 )
1257 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001258 opt.renegotiation = (atoi( q )) ?
1259 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1260 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +00001261 }
Hanno Beckerb0b2b672019-06-12 16:58:10 +01001262#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001263 else if( strcmp( p, "allow_legacy" ) == 0 )
1264 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001265 switch( atoi( q ) )
1266 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001267 case -1:
1268 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1269 break;
1270 case 0:
1271 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1272 break;
1273 case 1:
1274 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1275 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001276 default: goto usage;
1277 }
Paul Bakker48916f92012-09-16 19:57:18 +00001278 }
Hanno Beckerb0b2b672019-06-12 16:58:10 +01001279#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001280 else if( strcmp( p, "renegotiate" ) == 0 )
1281 {
1282 opt.renegotiate = atoi( q );
1283 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1284 goto usage;
1285 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001286 else if( strcmp( p, "exchanges" ) == 0 )
1287 {
1288 opt.exchanges = atoi( q );
1289 if( opt.exchanges < 1 )
1290 goto usage;
1291 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001292 else if( strcmp( p, "reconnect" ) == 0 )
1293 {
1294 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001295 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001296 goto usage;
1297 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001298 else if( strcmp( p, "reco_delay" ) == 0 )
1299 {
1300 opt.reco_delay = atoi( q );
1301 if( opt.reco_delay < 0 )
1302 goto usage;
1303 }
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02001304 else if( strcmp( p, "reco_mode" ) == 0 )
1305 {
1306 opt.reco_mode = atoi( q );
1307 if( opt.reco_mode < 0 )
1308 goto usage;
1309 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001310 else if( strcmp( p, "reconnect_hard" ) == 0 )
1311 {
1312 opt.reconnect_hard = atoi( q );
1313 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
1314 goto usage;
1315 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001316 else if( strcmp( p, "tickets" ) == 0 )
1317 {
1318 opt.tickets = atoi( q );
1319 if( opt.tickets < 0 || opt.tickets > 2 )
1320 goto usage;
1321 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001322 else if( strcmp( p, "alpn" ) == 0 )
1323 {
1324 opt.alpn_string = q;
1325 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001326 else if( strcmp( p, "fallback" ) == 0 )
1327 {
1328 switch( atoi( q ) )
1329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
1331 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001332 default: goto usage;
1333 }
1334 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001335 else if( strcmp( p, "extended_ms" ) == 0 )
1336 {
1337 switch( atoi( q ) )
1338 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001339 case 0:
1340 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1341 break;
1342 case 1:
1343 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1344 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001345 default: goto usage;
1346 }
1347 }
Jarno Lamsa41b35912019-06-10 15:51:11 +03001348 else if( strcmp( p, "enforce_extended_master_secret" ) == 0 )
1349 {
1350 switch( atoi( q ) )
1351 {
1352 case 0:
1353 opt.enforce_extended_master_secret =
1354 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
1355 break;
1356 case 1:
1357 opt.enforce_extended_master_secret =
1358 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_ENABLED;
1359 break;
1360 default: goto usage;
1361 }
1362 }
Hanno Beckerc1096e72019-06-19 12:30:41 +01001363#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Hanno Beckere6706e62017-05-15 16:05:15 +01001364 else if( strcmp( p, "curves" ) == 0 )
1365 opt.curves = q;
Hanno Beckerc1096e72019-06-19 12:30:41 +01001366#endif /* !MBEDTLS_SSL_CONF_SINGLE_EC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001367 else if( strcmp( p, "etm" ) == 0 )
1368 {
1369 switch( atoi( q ) )
1370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1372 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001373 default: goto usage;
1374 }
1375 }
Hanno Becker72e5ffc2019-07-05 11:35:08 +01001376#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
1377 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) || \
1378 !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
1379 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Paul Bakker1d29fb52012-09-28 13:28:45 +00001380 else if( strcmp( p, "min_version" ) == 0 )
1381 {
1382 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001384 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001386 else if( strcmp( q, "tls1_1" ) == 0 ||
1387 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001389 else if( strcmp( q, "tls1_2" ) == 0 ||
1390 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001392 else
1393 goto usage;
1394 }
1395 else if( strcmp( p, "max_version" ) == 0 )
1396 {
1397 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001399 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001401 else if( strcmp( q, "tls1_1" ) == 0 ||
1402 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001404 else if( strcmp( q, "tls1_2" ) == 0 ||
1405 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001407 else
1408 goto usage;
1409 }
1410 else if( strcmp( p, "force_version" ) == 0 )
1411 {
1412 if( strcmp( q, "ssl3" ) == 0 )
1413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1415 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001416 }
1417 else if( strcmp( q, "tls1" ) == 0 )
1418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1420 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001421 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001422 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1425 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001426 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001427 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1430 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001431 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001432 else if( strcmp( q, "dtls1" ) == 0 )
1433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1435 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1436 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001437 }
1438 else if( strcmp( q, "dtls1_2" ) == 0 )
1439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1441 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1442 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001443 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001444 else
1445 goto usage;
1446 }
Hanno Becker72e5ffc2019-07-05 11:35:08 +01001447#endif
1448 else if( strcmp( p, "arc4" ) == 0 )
1449 {
1450 switch( atoi( q ) )
1451 {
1452 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1453 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
1454 default: goto usage;
1455 }
1456 }
1457 else if( strcmp( p, "allow_sha1" ) == 0 )
1458 {
1459 switch( atoi( q ) )
1460 {
1461 case 0: opt.allow_sha1 = 0; break;
1462 case 1: opt.allow_sha1 = 1; break;
1463 default: goto usage;
1464 }
1465 }
Hanno Beckeracd4fc02019-06-12 16:40:50 +01001466#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Paul Bakker91ebfb52012-11-23 14:04:08 +01001467 else if( strcmp( p, "auth_mode" ) == 0 )
1468 {
1469 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001471 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001473 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001475 else
1476 goto usage;
1477 }
Hanno Beckeracd4fc02019-06-12 16:40:50 +01001478#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001479 else if( strcmp( p, "max_frag_len" ) == 0 )
1480 {
1481 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001483 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001485 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001487 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001489 else
1490 goto usage;
1491 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001492 else if( strcmp( p, "trunc_hmac" ) == 0 )
1493 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001494 switch( atoi( q ) )
1495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1497 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001498 default: goto usage;
1499 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001500 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001501 else if( strcmp( p, "hs_timeout" ) == 0 )
1502 {
1503 if( ( p = strchr( q, '-' ) ) == NULL )
1504 goto usage;
1505 *p++ = '\0';
1506 opt.hs_to_min = atoi( q );
1507 opt.hs_to_max = atoi( p );
1508 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1509 goto usage;
1510 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001511 else if( strcmp( p, "mtu" ) == 0 )
1512 {
1513 opt.dtls_mtu = atoi( q );
1514 if( opt.dtls_mtu < 0 )
1515 goto usage;
1516 }
Hanno Becker4d615912018-08-14 13:33:30 +01001517 else if( strcmp( p, "dgram_packing" ) == 0 )
1518 {
1519 opt.dgram_packing = atoi( q );
1520 if( opt.dgram_packing != 0 &&
1521 opt.dgram_packing != 1 )
1522 {
1523 goto usage;
1524 }
1525 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001526 else if( strcmp( p, "recsplit" ) == 0 )
1527 {
1528 opt.recsplit = atoi( q );
1529 if( opt.recsplit < 0 || opt.recsplit > 1 )
1530 goto usage;
1531 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001532 else if( strcmp( p, "dhmlen" ) == 0 )
1533 {
1534 opt.dhmlen = atoi( q );
1535 if( opt.dhmlen < 0 )
1536 goto usage;
1537 }
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +01001538 else if( strcmp( p, "query_config" ) == 0 )
1539 {
Andrzej Kurek825ebd42020-05-18 11:47:25 -04001540 mbedtls_exit( query_config( q ) );
1541 }
1542 else if( strcmp( p, "skip_close_notify" ) == 0 )
1543 {
1544 opt.skip_close_notify = atoi( q );
1545 if( opt.skip_close_notify < 0 || opt.skip_close_notify > 1 )
1546 goto usage;
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +01001547 }
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03001548 else if( strcmp( p, "serialize") == 0 )
1549 {
1550 opt.serialize = atoi( q );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03001551 if( opt.serialize < 0 || opt.serialize > 2)
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03001552 goto usage;
1553 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001554 else
1555 goto usage;
1556 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001557
Teppo Järvelin78007192019-10-25 14:30:33 +03001558 /* try to use as small buf from the heap as possible */
1559 if( opt.request_size <= 0 )
1560 {
1561 main_buf_len = MBEDTLS_SSL_MAX_CONTENT_LEN + 1;
1562 }
1563 else if( opt.request_size < (int)sizeof(GET_REQUEST) )
1564 {
1565 main_buf_len = sizeof(GET_REQUEST) + 1;
1566 }
1567 else
1568 {
1569 main_buf_len = opt.request_size + 1;
1570 }
1571
1572 buf = mbedtls_calloc( 1, main_buf_len );
1573 if( buf == NULL )
1574 {
Teppo Järvelin8e0e4812019-10-21 10:33:11 +03001575 mbedtls_printf( "buf allocation failed!\n" );
1576 goto exit;
1577 }
1578
Hanno Becker16970d22017-10-10 15:56:37 +01001579 /* Event-driven IO is incompatible with the above custom
1580 * receive and send functions, as the polling builds on
1581 * refers to the underlying net_context. */
1582 if( opt.event == 1 && opt.nbio != 1 )
1583 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001584 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001585 opt.nbio = 1;
1586 }
1587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588#if defined(MBEDTLS_DEBUG_C)
1589 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001590#endif
1591
Paul Bakkerc1516be2013-06-29 16:01:32 +02001592 if( opt.force_ciphersuite[0] > 0 )
1593 {
Hanno Becker473f98f2019-06-26 10:27:32 +01001594 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001595 ciphersuite_info =
1596 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001597
Paul Bakker66c48102013-07-26 14:05:32 +02001598 if( opt.max_version != -1 &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001599 mbedtls_ssl_ver_gt(
1600 mbedtls_ssl_suite_get_min_minor_ver( ciphersuite_info ),
1601 opt.max_version ) )
Paul Bakker66c48102013-07-26 14:05:32 +02001602 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001603 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker66c48102013-07-26 14:05:32 +02001604 ret = 2;
1605 goto usage;
1606 }
1607 if( opt.min_version != -1 &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001608 mbedtls_ssl_ver_lt(
1609 mbedtls_ssl_suite_get_max_minor_ver( ciphersuite_info ),
1610 opt.min_version ) )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001611 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001612 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001613 ret = 2;
1614 goto usage;
1615 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001616
1617 /* If the server selects a version that's not supported by
1618 * this suite, then there will be no common ciphersuite... */
1619 if( opt.max_version == -1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001620 mbedtls_ssl_ver_gt(
1621 opt.max_version,
1622 mbedtls_ssl_suite_get_max_minor_ver( ciphersuite_info ) ) )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001623 {
Hanno Becker473f98f2019-06-26 10:27:32 +01001624 opt.max_version = mbedtls_ssl_suite_get_max_minor_ver( ciphersuite_info );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001625 }
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001626 if( mbedtls_ssl_ver_lt(
1627 opt.min_version,
1628 mbedtls_ssl_suite_get_min_minor_ver( ciphersuite_info ) ) )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001629 {
Hanno Becker473f98f2019-06-26 10:27:32 +01001630 opt.min_version = mbedtls_ssl_suite_get_min_minor_ver( ciphersuite_info );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001631 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001633 mbedtls_ssl_ver_lt( opt.min_version,
1634 MBEDTLS_SSL_MINOR_VERSION_2 ) )
1635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001637 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001638 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001639
1640 /* Enable RC4 if needed and not explicitly disabled */
Hanno Becker473f98f2019-06-26 10:27:32 +01001641 if( mbedtls_ssl_suite_get_cipher( ciphersuite_info ) == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001644 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001645 mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001646 ret = 2;
1647 goto usage;
1648 }
1649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001651 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001652 }
1653
Hanno Beckera5a2b082019-05-15 14:03:01 +01001654#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01001655 cid_len = strlen( opt.cid_val ) / 2;
1656 if( cid_len > sizeof( cid ) )
1657 {
1658 mbedtls_printf( "CID too long\n" );
1659 goto exit;
1660 }
Hanno Becker7a7aa192019-04-09 17:24:19 +01001661
Hanno Becker96870292019-05-03 17:30:59 +01001662 if( unhexify( opt.cid_val, cid ) != 0 )
1663 {
1664 mbedtls_printf( "CID not valid hex\n" );
1665 goto exit;
1666 }
1667
1668 /* Keep CID settings for renegotiation unless
1669 * specified otherwise. */
1670 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
1671 opt.cid_enabled_renego = opt.cid_enabled;
1672 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
1673 opt.cid_val_renego = opt.cid_val;
1674
1675 cid_renego_len = strlen( opt.cid_val_renego ) / 2;
1676 if( cid_renego_len > sizeof( cid_renego ) )
1677 {
1678 mbedtls_printf( "CID too long\n" );
1679 goto exit;
1680 }
1681
1682 if( unhexify( opt.cid_val_renego, cid_renego ) != 0 )
1683 {
1684 mbedtls_printf( "CID not valid hex\n" );
1685 goto exit;
1686 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001687#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +01001688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001690 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001691 * Unhexify the pre-shared key if any is given
1692 */
1693 if( strlen( opt.psk ) )
1694 {
Hanno Beckerec370302019-04-09 17:12:56 +01001695 psk_len = strlen( opt.psk ) / 2;
1696 if( psk_len > sizeof( psk ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001697 {
Hanno Beckerec370302019-04-09 17:12:56 +01001698 mbedtls_printf( "pre-shared key too long\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001699 goto exit;
1700 }
1701
Hanno Beckerec370302019-04-09 17:12:56 +01001702 if( unhexify( opt.psk, psk ) != 0 )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001703 {
Hanno Beckerec370302019-04-09 17:12:56 +01001704 mbedtls_printf( "pre-shared key not valid hex\n" );
1705 goto exit;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001706 }
1707 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001709
Hanno Beckerc1096e72019-06-19 12:30:41 +01001710#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Hanno Beckere6706e62017-05-15 16:05:15 +01001711 if( opt.curves != NULL )
1712 {
1713 p = (char *) opt.curves;
1714 i = 0;
1715
1716 if( strcmp( p, "none" ) == 0 )
1717 {
1718 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1719 }
1720 else if( strcmp( p, "default" ) != 0 )
1721 {
1722 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001723 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001724 {
1725 q = p;
1726
1727 /* Terminate the current string */
1728 while( *p != ',' && *p != '\0' )
1729 p++;
1730 if( *p == ',' )
1731 *p++ = '\0';
1732
1733 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1734 {
1735 curve_list[i++] = curve_cur->grp_id;
1736 }
1737 else
1738 {
1739 mbedtls_printf( "unknown curve %s\n", q );
1740 mbedtls_printf( "supported curves: " );
1741 for( curve_cur = mbedtls_ecp_curve_list();
1742 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1743 curve_cur++ )
1744 {
1745 mbedtls_printf( "%s ", curve_cur->name );
1746 }
1747 mbedtls_printf( "\n" );
1748 goto exit;
1749 }
1750 }
1751
1752 mbedtls_printf("Number of curves: %d\n", i );
1753
Hanno Becker8651a432017-06-09 16:13:22 +01001754 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001755 {
Hanno Becker8651a432017-06-09 16:13:22 +01001756 mbedtls_printf( "curves list too long, maximum %d",
1757 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001758 goto exit;
1759 }
1760
1761 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1762 }
1763 }
Hanno Beckerc1096e72019-06-19 12:30:41 +01001764#endif /* MBEDTLS_ECP_C && !MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Beckere6706e62017-05-15 16:05:15 +01001765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001767 if( opt.alpn_string != NULL )
1768 {
1769 p = (char *) opt.alpn_string;
1770 i = 0;
1771
1772 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001773 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001774 {
1775 alpn_list[i++] = p;
1776
1777 /* Terminate the current string and move on to next one */
1778 while( *p != ',' && *p != '\0' )
1779 p++;
1780 if( *p == ',' )
1781 *p++ = '\0';
1782 }
1783 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001785
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001786 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001787 * 0. Initialize the RNG and the session data
1788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001790 fflush( stdout );
1791
Hanno Becker7d864c42019-09-19 16:51:41 +01001792 mbedtls_entropy_init( entropy );
Hanno Becker7f1c8052019-08-26 13:30:13 +01001793#if defined(MBEDTLS_CTR_DRBG_C)
Hanno Becker7d864c42019-09-19 16:51:41 +01001794 if( ( ret = mbedtls_ctr_drbg_seed( ctr_drbg, mbedtls_entropy_func,
1795 entropy, (const unsigned char *) pers,
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001796 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +00001797 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001798 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
1799 -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001800 goto exit;
1801 }
Hanno Becker7f1c8052019-08-26 13:30:13 +01001802#else /* MBEDTLS_CTR_DRBG_C */
Hanno Becker7d864c42019-09-19 16:51:41 +01001803 if( ( ret = mbedtls_hmac_drbg_seed( hmac_drbg,
Hanno Becker7f1c8052019-08-26 13:30:13 +01001804 mbedtls_md_info_from_type(
1805 available_hashes[0] ),
1806 mbedtls_entropy_func,
Hanno Becker7d864c42019-09-19 16:51:41 +01001807 entropy, (const unsigned char *) pers,
Hanno Becker7f1c8052019-08-26 13:30:13 +01001808 strlen( pers ) ) ) != 0 )
1809 {
1810 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
1811 -ret );
1812 goto exit;
1813 }
1814#endif /* MBEDTLS_CTR_DRBG */
Paul Bakker508ad5a2011-12-04 17:09:26 +00001815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001819 /*
1820 * 1.1. Load the trusted CA
1821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001823 fflush( stdout );
1824
Hanno Beckera7242062019-03-05 16:02:15 +00001825 if( strcmp( opt.ca_path, "none" ) == 0 ||
1826 strcmp( opt.ca_file, "none" ) == 0 )
1827 {
1828 ret = 0;
1829 }
1830 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001832 if( strlen( opt.ca_path ) )
Hanno Becker7d864c42019-09-19 16:51:41 +01001833 ret = mbedtls_x509_crt_parse_path( cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001834 else if( strlen( opt.ca_file ) )
Hanno Becker7d864c42019-09-19 16:51:41 +01001835 ret = mbedtls_x509_crt_parse_file( cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001836 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001837#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838#if defined(MBEDTLS_CERTS_C)
Hanno Beckerbb676f72019-02-01 08:15:06 +00001839 {
1840#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001842 {
Hanno Becker7d864c42019-09-19 16:51:41 +01001843 ret = mbedtls_x509_crt_parse( cacert,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 (const unsigned char *) mbedtls_test_cas[i],
1845 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001846 if( ret != 0 )
1847 break;
1848 }
Hanno Beckerbb676f72019-02-01 08:15:06 +00001849 if( ret == 0 )
1850#endif /* MBEDTLS_PEM_PARSE_C */
1851 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
1852 {
Hanno Beckerc8284322019-09-19 16:58:57 +01001853 ret = mbedtls_x509_crt_parse_der_nocopy( cacert,
Hanno Beckerbb676f72019-02-01 08:15:06 +00001854 (const unsigned char *) mbedtls_test_cas_der[i],
1855 mbedtls_test_cas_der_len[i] );
1856 if( ret != 0 )
1857 break;
1858 }
1859 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001860#else
1861 {
1862 ret = 1;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001863 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001864 }
Hanno Beckerbb676f72019-02-01 08:15:06 +00001865#endif /* MBEDTLS_CERTS_C */
Paul Bakker42488232012-05-16 08:21:05 +00001866 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001867 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001868 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1869 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001870 goto exit;
1871 }
1872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001874
1875 /*
1876 * 1.2. Load own certificate and private key
1877 *
1878 * (can be skipped if client authentication is not required)
1879 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001881 fflush( stdout );
1882
Hanno Beckera7242062019-03-05 16:02:15 +00001883 if( strcmp( opt.crt_file, "none" ) == 0 )
1884 ret = 0;
1885 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001887 if( strlen( opt.crt_file ) )
Hanno Becker7d864c42019-09-19 16:51:41 +01001888 ret = mbedtls_x509_crt_parse_file( clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001889 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001890#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891#if defined(MBEDTLS_CERTS_C)
Hanno Beckerc8284322019-09-19 16:58:57 +01001892#if defined(MBEDTLS_PEM_PARSE_C)
1893 ret = mbedtls_x509_crt_parse( clicert,
Hanno Becker16970d22017-10-10 15:56:37 +01001894 (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001896#else
Hanno Beckerc8284322019-09-19 16:58:57 +01001897 ret = mbedtls_x509_crt_parse_der_nocopy( clicert,
1898 (const unsigned char *) mbedtls_test_cli_crt,
1899 mbedtls_test_cli_crt_len );
1900#endif
1901#else
Paul Bakker5690efc2011-05-26 13:16:06 +00001902 {
1903 ret = 1;
Hanno Beckerc258c442018-12-05 16:49:42 +00001904 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001905 }
1906#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001907 if( ret != 0 )
1908 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001909 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1910 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001911 goto exit;
1912 }
1913
Hanno Beckera7242062019-03-05 16:02:15 +00001914 if( strcmp( opt.key_file, "none" ) == 0 )
1915 ret = 0;
1916 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001918 if( strlen( opt.key_file ) )
Hanno Becker7d864c42019-09-19 16:51:41 +01001919 ret = mbedtls_pk_parse_keyfile( pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001920 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001921#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922#if defined(MBEDTLS_CERTS_C)
Hanno Becker7d864c42019-09-19 16:51:41 +01001923 ret = mbedtls_pk_parse_key( pkey,
Hanno Becker16970d22017-10-10 15:56:37 +01001924 (const unsigned char *) mbedtls_test_cli_key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001926#else
1927 {
1928 ret = 1;
Hanno Beckerc258c442018-12-05 16:49:42 +00001929 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001930 }
1931#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001932 if( ret != 0 )
1933 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001934 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
1935 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 goto exit;
1937 }
1938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 mbedtls_printf( " ok\n" );
1940#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001941
1942 /*
1943 * 2. Start the connection
1944 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001945 if( opt.server_addr == NULL)
1946 opt.server_addr = opt.server_name;
1947
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001948 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001950 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001951 fflush( stdout );
1952
Hanno Becker16970d22017-10-10 15:56:37 +01001953 if( ( ret = mbedtls_net_connect( &server_fd,
1954 opt.server_addr, opt.server_port,
1955 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1956 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001958 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
1959 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 goto exit;
1961 }
1962
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001963 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001964 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001965 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001966 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001967 if( ret != 0 )
1968 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001969 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
1970 -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001971 goto exit;
1972 }
1973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001975
1976 /*
1977 * 3. Setup stuff
1978 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 fflush( stdout );
1981
Hanno Becker7d864c42019-09-19 16:51:41 +01001982 if( ( ret = mbedtls_ssl_config_defaults( conf,
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001983 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001984 opt.transport,
1985 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001986 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001987 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
1988 -ret );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001989 goto exit;
1990 }
1991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001993 /* The default algorithms profile disables SHA-1, but our tests still
1994 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001995 if( opt.allow_sha1 > 0 )
1996 {
1997 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
Hanno Becker7d864c42019-09-19 16:51:41 +01001998 mbedtls_ssl_conf_cert_profile( conf, &crt_profile_for_test );
Hanno Becker56595f42019-06-19 16:31:38 +01001999#if !defined(MBEDTLS_SSL_CONF_SINGLE_HASH)
Hanno Becker7d864c42019-09-19 16:51:41 +01002000 mbedtls_ssl_conf_sig_hashes( conf, available_hashes );
Hanno Becker56595f42019-06-19 16:31:38 +01002001#endif
Gilles Peskinebc70a182017-05-09 15:59:24 +02002002 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02002003
Hanno Becker9ec3fe02019-07-01 17:36:12 +01002004#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Hanno Becker7d864c42019-09-19 16:51:41 +01002005 mbedtls_ssl_conf_verify( conf, my_verify, NULL );
Hanno Beckerf9ca30d2019-02-26 11:38:29 +00002006 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Becker9ec3fe02019-07-01 17:36:12 +01002007#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Gilles Peskineef86ab22017-05-05 18:59:02 +02002008#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00002009
Hanno Beckere0200da2019-06-13 09:23:43 +01002010#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
2011 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
2012 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Becker96870292019-05-03 17:30:59 +01002013 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckereec2be92019-05-03 13:06:44 +01002014 {
Hanno Becker96870292019-05-03 17:30:59 +01002015 if( opt.cid_enabled == 1 &&
2016 opt.cid_enabled_renego == 1 &&
2017 cid_len != cid_renego_len )
2018 {
2019 mbedtls_printf( "CID length must not change during renegotiation\n" );
2020 goto usage;
2021 }
2022
Hanno Becker96870292019-05-03 17:30:59 +01002023 if( opt.cid_enabled == 1 )
Hanno Becker7d864c42019-09-19 16:51:41 +01002024 ret = mbedtls_ssl_conf_cid( conf, cid_len,
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002025 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Becker96870292019-05-03 17:30:59 +01002026 else
Hanno Becker7d864c42019-09-19 16:51:41 +01002027 ret = mbedtls_ssl_conf_cid( conf, cid_renego_len,
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002028 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Becker96870292019-05-03 17:30:59 +01002029
Hanno Beckereec2be92019-05-03 13:06:44 +01002030 if( ret != 0 )
2031 {
Hanno Becker76581052019-05-23 16:58:22 +01002032 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
2033 -ret );
Hanno Beckereec2be92019-05-03 13:06:44 +01002034 goto exit;
2035 }
2036 }
Hanno Beckere0200da2019-06-13 09:23:43 +01002037#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
2038 !MBEDTLS_SSL_CONF_CID_LEN &&
2039 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +01002040
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01002041 if( opt.auth_mode != DFL_AUTH_MODE )
Hanno Becker7d864c42019-09-19 16:51:41 +01002042 mbedtls_ssl_conf_authmode( conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00002043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002045 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Hanno Becker7d864c42019-09-19 16:51:41 +01002046 mbedtls_ssl_conf_handshake_timeout( conf, opt.hs_to_min,
Hanno Becker16970d22017-10-10 15:56:37 +01002047 opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002048
Hanno Becker4d615912018-08-14 13:33:30 +01002049 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker7d864c42019-09-19 16:51:41 +01002050 mbedtls_ssl_set_datagram_packing( ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker7d864c42019-09-19 16:51:41 +01002054 if( ( ret = mbedtls_ssl_conf_max_frag_len( conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002055 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002056 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n",
2057 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002058 goto exit;
2059 }
Paul Bakker05decb22013-08-15 13:33:48 +02002060#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02002061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01002063 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Hanno Becker7d864c42019-09-19 16:51:41 +01002064 mbedtls_ssl_conf_truncated_hmac( conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02002065#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02002066
Hanno Beckerf765ce62019-06-21 13:17:14 +01002067#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
2068 !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET) && \
2069 !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002070 if( opt.extended_ms != DFL_EXTENDED_MS )
Hanno Becker7d864c42019-09-19 16:51:41 +01002071 mbedtls_ssl_conf_extended_master_secret( conf, opt.extended_ms );
Jarno Lamsa41b35912019-06-10 15:51:11 +03002072 if( opt.enforce_extended_master_secret != DFL_EXTENDED_MS_ENFORCE )
Hanno Becker7d864c42019-09-19 16:51:41 +01002073 mbedtls_ssl_conf_extended_master_secret_enforce( conf,
Jarno Lamsa41b35912019-06-10 15:51:11 +03002074 opt.enforce_extended_master_secret );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002075#endif
2076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002078 if( opt.etm != DFL_ETM )
Hanno Becker7d864c42019-09-19 16:51:41 +01002079 mbedtls_ssl_conf_encrypt_then_mac( conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002080#endif
2081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01002083 if( opt.recsplit != DFL_RECSPLIT )
Hanno Becker7d864c42019-09-19 16:51:41 +01002084 mbedtls_ssl_conf_cbc_record_splitting( conf, opt.recsplit
Hanno Becker16970d22017-10-10 15:56:37 +01002085 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
2086 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01002087#endif
2088
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02002089#if defined(MBEDTLS_DHM_C)
2090 if( opt.dhmlen != DFL_DHMLEN )
Hanno Becker7d864c42019-09-19 16:51:41 +01002091 mbedtls_ssl_conf_dhm_min_bitlen( conf, opt.dhmlen );
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02002092#endif
2093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002095 if( opt.alpn_string != NULL )
Hanno Becker7d864c42019-09-19 16:51:41 +01002096 if( ( ret = mbedtls_ssl_conf_alpn_protocols( conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002097 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002098 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
2099 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002100 goto exit;
2101 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002102#endif
2103
Hanno Becker7f1c8052019-08-26 13:30:13 +01002104#if defined(MBEDTLS_CTR_DRBG_C)
Hanno Beckerece325c2019-06-13 15:39:27 +01002105#if !defined(MBEDTLS_SSL_CONF_RNG)
Hanno Becker7d864c42019-09-19 16:51:41 +01002106 mbedtls_ssl_conf_rng( conf, mbedtls_ctr_drbg_random, ctr_drbg );
Hanno Beckerece325c2019-06-13 15:39:27 +01002107#else
Hanno Becker7d864c42019-09-19 16:51:41 +01002108 rng_ctx_global = ctr_drbg;
Hanno Beckerece325c2019-06-13 15:39:27 +01002109#endif
Hanno Becker7f1c8052019-08-26 13:30:13 +01002110#else /* MBEDTLS_CTR_DRBG_C */
2111#if !defined(MBEDTLS_SSL_CONF_RNG)
Hanno Becker7d864c42019-09-19 16:51:41 +01002112 mbedtls_ssl_conf_rng( conf, mbedtls_hmac_drbg_random, hmac_drbg );
Hanno Becker7f1c8052019-08-26 13:30:13 +01002113#else
Hanno Becker7d864c42019-09-19 16:51:41 +01002114 rng_ctx_global = hmac_drbg;
Hanno Becker7f1c8052019-08-26 13:30:13 +01002115#endif
2116#endif /* MBEDTLS_CTR_DRBG_C */
Hanno Beckerece325c2019-06-13 15:39:27 +01002117
Hanno Becker14a4a442019-07-02 17:00:34 +01002118#if defined(MBEDTLS_DEBUG_C)
Hanno Becker7d864c42019-09-19 16:51:41 +01002119 mbedtls_ssl_conf_dbg( conf, my_debug, stdout );
Hanno Becker14a4a442019-07-02 17:00:34 +01002120#endif
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002121
Hanno Becker1f835fa2019-06-13 10:14:59 +01002122#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Hanno Becker7d864c42019-09-19 16:51:41 +01002123 mbedtls_ssl_conf_read_timeout( conf, opt.read_timeout );
Hanno Becker1f835fa2019-06-13 10:14:59 +01002124#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker7d864c42019-09-19 16:51:41 +01002127 mbedtls_ssl_conf_session_tickets( conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02002128#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002129
Hanno Becker73f4cb12019-06-27 13:51:07 +01002130#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Paul Bakker645ce3a2012-10-31 12:32:41 +00002131 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Hanno Becker7d864c42019-09-19 16:51:41 +01002132 mbedtls_ssl_conf_ciphersuites( conf, opt.force_ciphersuite );
Hanno Becker73f4cb12019-06-27 13:51:07 +01002133#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002134
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002135#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002136 if( opt.arc4 != DFL_ARC4 )
Hanno Becker7d864c42019-09-19 16:51:41 +01002137 mbedtls_ssl_conf_arc4_support( conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002138#endif
Paul Bakker51936882011-02-20 16:05:58 +00002139
Hanno Beckerb0b2b672019-06-12 16:58:10 +01002140#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002141 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Hanno Becker7d864c42019-09-19 16:51:41 +01002142 mbedtls_ssl_conf_legacy_renegotiation( conf, opt.allow_legacy );
Hanno Beckerb0b2b672019-06-12 16:58:10 +01002143#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker7d864c42019-09-19 16:51:41 +01002145 mbedtls_ssl_conf_renegotiation( conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002146#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002149 if( strcmp( opt.ca_path, "none" ) != 0 &&
2150 strcmp( opt.ca_file, "none" ) != 0 )
2151 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002152 mbedtls_ssl_conf_ca_chain( conf, cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002153 }
2154 if( strcmp( opt.crt_file, "none" ) != 0 &&
2155 strcmp( opt.key_file, "none" ) != 0 )
2156 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002157 if( ( ret = mbedtls_ssl_conf_own_cert( conf, clicert, pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002158 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002159 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
2160 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002161 goto exit;
2162 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002163 }
Paul Bakkered27a042013-04-18 22:46:23 +02002164#endif
2165
Hanno Beckere6706e62017-05-15 16:05:15 +01002166#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01002167#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Hanno Beckere6706e62017-05-15 16:05:15 +01002168 if( opt.curves != NULL &&
2169 strcmp( opt.curves, "default" ) != 0 )
2170 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002171 mbedtls_ssl_conf_curves( conf, curve_list );
Hanno Beckere6706e62017-05-15 16:05:15 +01002172 }
Hanno Beckerc1096e72019-06-19 12:30:41 +01002173#endif /* !MBEDTLS_SSL_CONF_SINGLE_EC */
2174#endif /* MBEDTLS_ECP_C */
Hanno Beckere6706e62017-05-15 16:05:15 +01002175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Becker7d864c42019-09-19 16:51:41 +01002177 if( ( ret = mbedtls_ssl_conf_psk( conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002178 (const unsigned char *) opt.psk_identity,
2179 strlen( opt.psk_identity ) ) ) != 0 )
2180 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002181 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n",
2182 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002183 goto exit;
2184 }
Paul Bakkered27a042013-04-18 22:46:23 +02002185#endif
2186
Hanno Becker72e5ffc2019-07-05 11:35:08 +01002187#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
2188 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) || \
2189 !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
2190 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002191 if( opt.min_version != DFL_MIN_VERSION )
Hanno Becker7d864c42019-09-19 16:51:41 +01002192 mbedtls_ssl_conf_min_version( conf, MBEDTLS_SSL_MAJOR_VERSION_3,
Hanno Becker16970d22017-10-10 15:56:37 +01002193 opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01002194
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002195 if( opt.max_version != DFL_MAX_VERSION )
Hanno Becker7d864c42019-09-19 16:51:41 +01002196 mbedtls_ssl_conf_max_version( conf, MBEDTLS_SSL_MAJOR_VERSION_3,
Hanno Becker16970d22017-10-10 15:56:37 +01002197 opt.max_version );
Hanno Becker72e5ffc2019-07-05 11:35:08 +01002198#endif
Paul Bakker1d29fb52012-09-28 13:28:45 +00002199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02002201 if( opt.fallback != DFL_FALLBACK )
Hanno Becker7d864c42019-09-19 16:51:41 +01002202 mbedtls_ssl_conf_fallback( conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02002203#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002204
Hanno Becker7d864c42019-09-19 16:51:41 +01002205 if( ( ret = mbedtls_ssl_setup( ssl, conf ) ) != 0 )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002206 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002207 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
2208 -ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002209 goto exit;
2210 }
2211
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002212#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker7d864c42019-09-19 16:51:41 +01002213 if( ( ret = mbedtls_ssl_set_hostname( ssl, opt.server_name ) ) != 0 )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002214 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002215 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
2216 ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002217 goto exit;
2218 }
2219#endif
2220
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002221#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2222 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
2223 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002224 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( ssl,
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002225 (const unsigned char *) opt.ecjpake_pw,
2226 strlen( opt.ecjpake_pw ) ) ) != 0 )
2227 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002228 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
2229 ret );
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002230 goto exit;
2231 }
2232 }
2233#endif
2234
Hanno Beckera58a8962019-06-13 16:11:15 +01002235#if !defined(MBEDTLS_SSL_CONF_RECV) && \
2236 !defined(MBEDTLS_SSL_CONF_SEND) && \
2237 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Hanno Becker7d864c42019-09-19 16:51:41 +01002238 io_ctx.ssl = ssl;
Hanno Becker14219fe2019-07-03 17:02:43 +01002239 io_ctx.net = &server_fd;
Hanno Becker7d864c42019-09-19 16:51:41 +01002240 mbedtls_ssl_set_bio( ssl, &io_ctx, send_cb, recv_cb,
Hanno Becker14219fe2019-07-03 17:02:43 +01002241 opt.nbio == 0 ? recv_timeout_cb : NULL );
Hanno Beckera58a8962019-06-13 16:11:15 +01002242#else
Hanno Becker7d864c42019-09-19 16:51:41 +01002243 mbedtls_ssl_set_bio_ctx( ssl, &server_fd );
Hanno Beckera58a8962019-06-13 16:11:15 +01002244#endif
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002245
Hanno Beckera5a2b082019-05-15 14:03:01 +01002246#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7a7aa192019-04-09 17:24:19 +01002247 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2248 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002249 if( ( ret = mbedtls_ssl_set_cid( ssl, opt.cid_enabled,
Hanno Becker7a7aa192019-04-09 17:24:19 +01002250 cid, cid_len ) ) != 0 )
2251 {
2252 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2253 ret );
2254 goto exit;
2255 }
2256 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002257#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +01002258
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002259#if defined(MBEDTLS_SSL_PROTO_DTLS)
2260 if( opt.dtls_mtu != DFL_DTLS_MTU )
Hanno Becker7d864c42019-09-19 16:51:41 +01002261 mbedtls_ssl_set_mtu( ssl, opt.dtls_mtu );
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002262#endif
2263
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002264#if defined(MBEDTLS_TIMING_C)
Hanno Becker0ae6b242019-06-13 16:45:36 +01002265#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
2266 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Hanno Becker7d864c42019-09-19 16:51:41 +01002267 mbedtls_ssl_set_timer_cb( ssl, &timer, mbedtls_timing_set_delay,
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002268 mbedtls_timing_get_delay );
Hanno Becker0ae6b242019-06-13 16:45:36 +01002269#else
Hanno Becker7d864c42019-09-19 16:51:41 +01002270 mbedtls_ssl_set_timer_cb_ctx( ssl, &timer );
Hanno Becker0ae6b242019-06-13 16:45:36 +01002271#endif
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002272#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002273
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002274#if defined(MBEDTLS_ECP_RESTARTABLE)
2275 if( opt.ec_max_ops != DFL_EC_MAX_OPS )
2276 mbedtls_ecp_set_max_ops( opt.ec_max_ops );
2277#endif
2278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002280
Paul Bakker5121ce52009-01-03 21:22:43 +00002281 /*
2282 * 4. Handshake
2283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002285 fflush( stdout );
2286
Hanno Becker7d864c42019-09-19 16:51:41 +01002287 while( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002288 {
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002289 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2290 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002291 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Paul Bakker40e46942009-01-03 21:51:57 +00002292 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002293 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
2294 -ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2296 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01002297 " Unable to verify the server's certificate. "
2298 "Either it is invalid,\n"
2299 " or you didn't set ca_file or ca_path "
2300 "to an appropriate value.\n"
2301 " Alternatively, you may want to use "
2302 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002304 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00002305 }
Hanno Becker16970d22017-10-10 15:56:37 +01002306
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002307#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002308 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2309 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002310#endif
2311
Hanno Becker16970d22017-10-10 15:56:37 +01002312 /* For event-driven IO, wait for socket to become available */
2313 if( opt.event == 1 /* level triggered IO */ )
2314 {
2315#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002316 ret = idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002317#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002318 ret = idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002319#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002320 if( ret != 0 )
2321 goto exit;
Hanno Becker16970d22017-10-10 15:56:37 +01002322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002323 }
2324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Hanno Becker7d864c42019-09-19 16:51:41 +01002326 mbedtls_ssl_get_version( ssl ),
2327 mbedtls_ssl_get_ciphersuite( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002328
Hanno Becker7d864c42019-09-19 16:51:41 +01002329 if( ( ret = mbedtls_ssl_get_record_expansion( ssl ) ) >= 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002331 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002333
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002334#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2335 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
Hanno Becker7d864c42019-09-19 16:51:41 +01002336 (unsigned int) mbedtls_ssl_get_max_frag_len( ssl ) );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002337#endif
2338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002340 if( opt.alpn_string != NULL )
2341 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002342 const char *alp = mbedtls_ssl_get_alpn_protocol( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002344 alp ? alp : "(none)" );
2345 }
2346#endif
2347
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002348 if( opt.reconnect != 0 )
2349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002351 fflush( stdout );
2352
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02002353 if( opt.reco_mode == 1 )
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02002354 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02002355 /* free any previously saved data */
Manuel Pégourié-Gonnard4bb1b992019-05-24 10:26:41 +02002356 if( session_data != NULL )
2357 {
2358 mbedtls_platform_zeroize( session_data, session_data_len );
2359 mbedtls_free( session_data );
2360 session_data = NULL;
2361 }
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02002362
2363 /* get size of the buffer needed */
Hanno Becker7d864c42019-09-19 16:51:41 +01002364 mbedtls_ssl_session_save( mbedtls_ssl_get_session_pointer( ssl ),
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02002365 NULL, 0, &session_data_len );
2366 session_data = mbedtls_calloc( 1, session_data_len );
2367 if( session_data == NULL )
2368 {
2369 mbedtls_printf( " failed\n ! alloc %u bytes for session data\n",
2370 (unsigned) session_data_len );
2371 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
2372 goto exit;
2373 }
2374
2375 /* actually save session data */
Hanno Becker7d864c42019-09-19 16:51:41 +01002376 if( ( ret = mbedtls_ssl_session_save( mbedtls_ssl_get_session_pointer( ssl ),
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02002377 session_data, session_data_len,
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02002378 &session_data_len ) ) != 0 )
2379 {
2380 mbedtls_printf( " failed\n ! mbedtls_ssl_session_saved returned -0x%04x\n\n",
2381 -ret );
2382 goto exit;
2383 }
2384 }
2385 else
2386 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002387 if( ( ret = mbedtls_ssl_get_session( ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02002388 {
2389 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
2390 -ret );
2391 goto exit;
2392 }
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02002393 }
2394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02002396
2397 if( opt.reco_mode == 1 )
2398 {
2399 mbedtls_printf( " [ Saved %u bytes of session data]\n",
2400 (unsigned) session_data_len );
2401 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002402 }
2403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 /*
2406 * 5. Verify the server certificate
2407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002409
Hanno Becker7d864c42019-09-19 16:51:41 +01002410 if( ( flags = mbedtls_ssl_get_verify_result( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 {
Hanno Becker02a21932019-06-10 15:08:43 +01002412#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002413 char vrfy_buf[512];
Peter Kolbusdc470ae2018-12-11 13:55:56 -06002414#endif
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002417
Hanno Becker02a21932019-06-10 15:08:43 +01002418#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Becker16970d22017-10-10 15:56:37 +01002419 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ),
2420 " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002422 mbedtls_printf( "%s\n", vrfy_buf );
Peter Kolbusdc470ae2018-12-11 13:55:56 -06002423#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 }
2425 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002427
Hanno Becker9ec3fe02019-07-01 17:36:12 +01002428#if !defined(MBEDTLS_X509_REMOVE_INFO) && \
2429 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Hanno Becker975c4632019-02-25 17:43:18 +00002430 mbedtls_printf( " . Peer certificate information ...\n" );
2431 mbedtls_printf( "%s\n", peer_crt_info );
Hanno Becker9ec3fe02019-07-01 17:36:12 +01002432#endif /* !MBEDTLS_X509_REMOVE_INFO && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002434
Hanno Beckera5a2b082019-05-15 14:03:01 +01002435#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7d864c42019-09-19 16:51:41 +01002436 ret = report_cid_usage( ssl, "initial handshake" );
Hanno Becker96870292019-05-03 17:30:59 +01002437 if( ret != 0 )
2438 goto exit;
2439
Hanno Becker7a7aa192019-04-09 17:24:19 +01002440 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2441 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002442 if( ( ret = mbedtls_ssl_set_cid( ssl, opt.cid_enabled_renego,
Hanno Becker96870292019-05-03 17:30:59 +01002443 cid_renego,
2444 cid_renego_len ) ) != 0 )
Hanno Becker7a7aa192019-04-09 17:24:19 +01002445 {
Hanno Becker96870292019-05-03 17:30:59 +01002446 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2447 ret );
2448 return( ret );
Hanno Becker7a7aa192019-04-09 17:24:19 +01002449 }
2450 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002451#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker7a7aa192019-04-09 17:24:19 +01002452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002454 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002455 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002456 /*
2457 * Perform renegotiation (this must be done when the server is waiting
2458 * for input from our side).
2459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002461 fflush( stdout );
Hanno Becker7d864c42019-09-19 16:51:41 +01002462 while( ( ret = mbedtls_ssl_renegotiate( ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002463 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002464 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002465 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002466 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002467 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002468 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
2469 ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002470 goto exit;
2471 }
Hanno Becker16970d22017-10-10 15:56:37 +01002472
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002473#if defined(MBEDTLS_ECP_RESTARTABLE)
2474 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2475 continue;
2476#endif
2477
Hanno Becker16970d22017-10-10 15:56:37 +01002478 /* For event-driven IO, wait for socket to become available */
2479 if( opt.event == 1 /* level triggered IO */ )
2480 {
2481#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002482 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002483#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002484 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002485#endif
2486 }
2487
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002488 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002490 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002492
Hanno Beckera5a2b082019-05-15 14:03:01 +01002493#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker7d864c42019-09-19 16:51:41 +01002494 ret = report_cid_usage( ssl, "after renegotiation" );
Hanno Becker96870292019-05-03 17:30:59 +01002495 if( ret != 0 )
2496 goto exit;
Hanno Beckera5a2b082019-05-15 14:03:01 +01002497#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker96870292019-05-03 17:30:59 +01002498
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 /*
2500 * 6. Write the GET request
2501 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002502 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002503send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 fflush( stdout );
2506
Teppo Järvelin8e0e4812019-10-21 10:33:11 +03002507 len = mbedtls_snprintf( (char *) buf, main_buf_len - 1, GET_REQUEST,
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002508 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002509 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002510
2511 /* Add padding to GET request to reach opt.request_size in length */
2512 if( opt.request_size != DFL_REQUEST_SIZE &&
2513 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02002514 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002515 memset( buf + len, 'A', opt.request_size - len - tail_len );
2516 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02002517 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002518
Teppo Järvelin8e0e4812019-10-21 10:33:11 +03002519 strncpy( (char *) buf + len, GET_REQUEST_END, main_buf_len - len - 1 );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002520 len += tail_len;
2521
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02002522 /* Truncate if request size is smaller than the "natural" size */
2523 if( opt.request_size != DFL_REQUEST_SIZE &&
2524 len > opt.request_size )
2525 {
2526 len = opt.request_size;
2527
2528 /* Still end with \r\n unless that's really not possible */
2529 if( len >= 2 ) buf[len - 2] = '\r';
2530 if( len >= 1 ) buf[len - 1] = '\n';
2531 }
2532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002534 {
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002535 written = 0;
2536 frags = 0;
2537
2538 do
Paul Bakker5121ce52009-01-03 21:22:43 +00002539 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002540 while( ( ret = mbedtls_ssl_write( ssl, buf + written,
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002541 len - written ) ) < 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002542 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002543 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002544 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002545 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002546 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002547 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
2548 -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002549 goto exit;
2550 }
Hanno Becker16970d22017-10-10 15:56:37 +01002551
2552 /* For event-driven IO, wait for socket to become available */
2553 if( opt.event == 1 /* level triggered IO */ )
2554 {
2555#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002556 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002557#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002558 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002559#endif
2560 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002561 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002562
2563 frags++;
2564 written += ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002566 while( written < len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002567 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002568 else /* Not stream, so datagram */
2569 {
Hanno Becker16970d22017-10-10 15:56:37 +01002570 while( 1 )
2571 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002572 ret = mbedtls_ssl_write( ssl, buf, len );
Hanno Becker16970d22017-10-10 15:56:37 +01002573
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002574#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002575 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002576 continue;
2577#endif
2578
Hanno Becker16970d22017-10-10 15:56:37 +01002579 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2580 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2581 break;
2582
2583 /* For event-driven IO, wait for socket to become available */
2584 if( opt.event == 1 /* level triggered IO */ )
2585 {
2586#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002587 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002588#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002589 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002590#endif
2591 }
2592 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002593
2594 if( ret < 0 )
2595 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002596 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n",
2597 ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002598 goto exit;
2599 }
2600
2601 frags = 1;
2602 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002603
2604 if( written < len )
2605 {
2606 mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
2607 "was truncated to size %u", (unsigned) written );
2608 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002609 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002610
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002611 buf[written] = '\0';
Hanno Becker16970d22017-10-10 15:56:37 +01002612 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
2613 written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002614
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002615 /* Send a non-empty request if request_size == 0 */
2616 if ( len == 0 )
2617 {
2618 opt.request_size = DFL_REQUEST_SIZE;
2619 goto send_request;
2620 }
2621
Paul Bakker5121ce52009-01-03 21:22:43 +00002622 /*
2623 * 7. Read the HTTP response
2624 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 fflush( stdout );
2627
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002628 /*
2629 * TLS and DTLS need different reading styles (stream vs datagram)
2630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002632 {
2633 do
2634 {
Teppo Järvelin8e0e4812019-10-21 10:33:11 +03002635 len = main_buf_len - 1;
2636 memset( buf, 0, main_buf_len );
Hanno Becker7d864c42019-09-19 16:51:41 +01002637 ret = mbedtls_ssl_read( ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002638
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002639#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002640 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002641 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002642#endif
2643
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002644 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
2645 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Hanno Becker16970d22017-10-10 15:56:37 +01002646 {
2647 /* For event-driven IO, wait for socket to become available */
2648 if( opt.event == 1 /* level triggered IO */ )
2649 {
2650#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002651 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002652#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002653 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002654#endif
2655 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002656 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01002657 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002658
2659 if( ret <= 0 )
2660 {
2661 switch( ret )
2662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2664 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002665 ret = 0;
2666 goto close_notify;
2667
2668 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 case MBEDTLS_ERR_NET_CONN_RESET:
2670 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002671 ret = 0;
2672 goto reconnect;
2673
2674 default:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002675 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n",
2676 -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002677 goto exit;
2678 }
2679 }
2680
2681 len = ret;
2682 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002684
2685 /* End of message should be detected according to the syntax of the
2686 * application protocol (eg HTTP), just use a dummy test here. */
2687 if( ret > 0 && buf[len-1] == '\n' )
2688 {
2689 ret = 0;
2690 break;
2691 }
2692 }
2693 while( 1 );
2694 }
2695 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 {
Teppo Järvelin8e0e4812019-10-21 10:33:11 +03002697 len = main_buf_len - 1;
2698 memset( buf, 0, main_buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
Hanno Becker16970d22017-10-10 15:56:37 +01002700 while( 1 )
2701 {
Hanno Becker7d864c42019-09-19 16:51:41 +01002702 ret = mbedtls_ssl_read( ssl, buf, len );
Hanno Becker16970d22017-10-10 15:56:37 +01002703
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002704#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002705 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002706 continue;
2707#endif
2708
Hanno Becker16970d22017-10-10 15:56:37 +01002709 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2710 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2711 break;
2712
2713 /* For event-driven IO, wait for socket to become available */
2714 if( opt.event == 1 /* level triggered IO */ )
2715 {
2716#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002717 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002718#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002719 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002720#endif
2721 }
2722 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002723
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002724 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002726 switch( ret )
2727 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002728 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002730 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02002731 goto send_request;
2732 goto exit;
2733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2735 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002736 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002737 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00002738
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002739 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002741 goto exit;
2742 }
Paul Bakker5690efc2011-05-26 13:16:06 +00002743 }
2744
Paul Bakker5121ce52009-01-03 21:22:43 +00002745 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02002746 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002748 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002750
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002751 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002752 * 7b. Simulate hard reset and reconnect from same port?
2753 */
2754 if( opt.reconnect_hard != 0 )
2755 {
2756 opt.reconnect_hard = 0;
2757
2758 mbedtls_printf( " . Restarting connection from same port..." );
2759 fflush( stdout );
2760
Hanno Becker9ec3fe02019-07-01 17:36:12 +01002761#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
2762 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Hanno Beckerb7fab762019-02-26 12:36:53 +00002763 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Becker9ec3fe02019-07-01 17:36:12 +01002764#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Hanno Beckerb7fab762019-02-26 12:36:53 +00002765
Hanno Becker7d864c42019-09-19 16:51:41 +01002766 if( ( ret = mbedtls_ssl_session_reset( ssl ) ) != 0 )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002767 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002768 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2769 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002770 goto exit;
2771 }
2772
Hanno Becker7d864c42019-09-19 16:51:41 +01002773 while( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002774 {
2775 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002776 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002777 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002778 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002779 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2780 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002781 goto exit;
2782 }
Hanno Becker16970d22017-10-10 15:56:37 +01002783
2784 /* For event-driven IO, wait for socket to become available */
2785 if( opt.event == 1 /* level triggered IO */ )
2786 {
2787#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002788 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002789#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002790 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002791#endif
2792 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002793 }
2794
2795 mbedtls_printf( " ok\n" );
2796
2797 goto send_request;
2798 }
2799
2800 /*
Jarno Lamsad736d082019-05-29 15:15:08 +03002801 * 7c. Simulate serialize/deserialize and go back to data exchange
2802 */
Jarno Lamsacf1b6722019-06-04 11:06:31 +03002803#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03002804 if( opt.serialize != 0 )
Jarno Lamsad736d082019-05-29 15:15:08 +03002805 {
Jarno Lamsa034ae842019-06-06 15:10:07 +03002806 size_t buf_len;
Jarno Lamsad736d082019-05-29 15:15:08 +03002807
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002808 mbedtls_printf( " . Serializing live connection..." );
Jarno Lamsad736d082019-05-29 15:15:08 +03002809
Hanno Becker7d864c42019-09-19 16:51:41 +01002810 ret = mbedtls_ssl_context_save( ssl, NULL, 0, &buf_len );
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002811 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jarno Lamsad736d082019-05-29 15:15:08 +03002812 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03002813 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
2814 "-0x%x\n\n", -ret );
Jarno Lamsad736d082019-05-29 15:15:08 +03002815
2816 goto exit;
2817 }
2818
Jarno Lamsa034ae842019-06-06 15:10:07 +03002819 if( ( context_buf = mbedtls_calloc( 1, buf_len ) ) == NULL )
Jarno Lamsad736d082019-05-29 15:15:08 +03002820 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03002821 mbedtls_printf( " failed\n ! Couldn't allocate buffer for "
2822 "serialized context" );
Jarno Lamsad736d082019-05-29 15:15:08 +03002823
2824 goto exit;
2825 }
Manuel Pégourié-Gonnardc9812292019-07-15 10:31:11 +02002826 context_buf_len = buf_len;
Jarno Lamsad736d082019-05-29 15:15:08 +03002827
Hanno Becker7d864c42019-09-19 16:51:41 +01002828 if( ( ret = mbedtls_ssl_context_save( ssl, context_buf,
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03002829 buf_len, &buf_len ) ) != 0 )
Jarno Lamsad736d082019-05-29 15:15:08 +03002830 {
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002831 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
Jarno Lamsa5737ec92019-06-04 15:36:18 +03002832 "-0x%x\n\n", -ret );
Jarno Lamsad736d082019-05-29 15:15:08 +03002833
2834 goto exit;
2835 }
2836
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002837 mbedtls_printf( " ok\n" );
2838
2839 if( opt.serialize == 1 )
2840 {
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +02002841 /* nothing to do here, done by context_save() already */
2842 mbedtls_printf( " . Context has been reset... ok" );
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002843 }
2844
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03002845 if( opt.serialize == 2 )
2846 {
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002847 mbedtls_printf( " . Freeing and reinitializing context..." );
2848
Hanno Becker7d864c42019-09-19 16:51:41 +01002849 mbedtls_ssl_free( ssl );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03002850
Hanno Becker7d864c42019-09-19 16:51:41 +01002851 mbedtls_ssl_init( ssl );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03002852
Hanno Becker7d864c42019-09-19 16:51:41 +01002853 if( ( ret = mbedtls_ssl_setup( ssl, conf ) ) != 0 )
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03002854 {
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03002855 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned "
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002856 "-0x%x\n\n", -ret );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03002857 goto exit;
2858 }
2859
Hanno Becker41e5a682019-07-19 17:07:30 +01002860#if !defined(MBEDTLS_SSL_CONF_RECV) && \
2861 !defined(MBEDTLS_SSL_CONF_SEND) && \
2862 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Hanno Becker7d864c42019-09-19 16:51:41 +01002863 mbedtls_ssl_set_bio( ssl, &io_ctx, send_cb, recv_cb,
Hanno Becker14219fe2019-07-03 17:02:43 +01002864 opt.nbio == 0 ? recv_timeout_cb : NULL );
Hanno Becker41e5a682019-07-19 17:07:30 +01002865#else
Hanno Becker7d864c42019-09-19 16:51:41 +01002866 mbedtls_ssl_set_bio_ctx( ssl, &server_fd );
Hanno Becker41e5a682019-07-19 17:07:30 +01002867#endif
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03002868
Jarno Lamsa29a15c22019-06-13 11:45:06 +03002869#if defined(MBEDTLS_TIMING_C)
Hanno Becker0ae6b242019-06-13 16:45:36 +01002870#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
2871 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Hanno Becker7d864c42019-09-19 16:51:41 +01002872 mbedtls_ssl_set_timer_cb( ssl, &timer,
Manuel Pégourié-Gonnard86dfa0c2019-07-15 12:23:22 +02002873 mbedtls_timing_set_delay,
2874 mbedtls_timing_get_delay );
Hanno Becker0ae6b242019-06-13 16:45:36 +01002875#else
Hanno Becker7d864c42019-09-19 16:51:41 +01002876 mbedtls_ssl_set_timer_cb_ctx( ssl, &timer );
Hanno Becker0ae6b242019-06-13 16:45:36 +01002877#endif
Jarno Lamsa29a15c22019-06-13 11:45:06 +03002878#endif /* MBEDTLS_TIMING_C */
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002879
2880 mbedtls_printf( " ok\n" );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03002881 }
2882
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002883 mbedtls_printf( " . Deserializing connection..." );
Jarno Lamsad736d082019-05-29 15:15:08 +03002884
Hanno Becker7d864c42019-09-19 16:51:41 +01002885 if( ( ret = mbedtls_ssl_context_load( ssl, context_buf,
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03002886 buf_len ) ) != 0 )
Jarno Lamsad736d082019-05-29 15:15:08 +03002887 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03002888 mbedtls_printf( "failed\n ! mbedtls_ssl_context_load returned "
2889 "-0x%x\n\n", -ret );
Jarno Lamsad736d082019-05-29 15:15:08 +03002890
2891 goto exit;
2892 }
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002893
Manuel Pégourié-Gonnardc9812292019-07-15 10:31:11 +02002894 mbedtls_free( context_buf );
2895 context_buf = NULL;
2896 context_buf_len = 0;
2897
Manuel Pégourié-Gonnard3b23c7d2019-07-12 10:41:55 +02002898 mbedtls_printf( " ok\n" );
Jarno Lamsad736d082019-05-29 15:15:08 +03002899 }
Jarno Lamsa5737ec92019-06-04 15:36:18 +03002900#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jarno Lamsad736d082019-05-29 15:15:08 +03002901
2902 /*
2903 * 7d. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002904 */
2905 if( --opt.exchanges > 0 )
2906 goto send_request;
2907
2908 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002909 * 8. Done, cleanly close the connection
2910 */
2911close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002913 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002914
Andrzej Kurek825ebd42020-05-18 11:47:25 -04002915 /*
2916 * Most of the time sending a close_notify before closing is the right
2917 * thing to do. However, when the server already knows how many messages
2918 * are expected and closes the connection by itself, this alert becomes
2919 * redundant. Sometimes with DTLS this redundancy becomes a problem by
2920 * leading to a race condition where the server might close the connection
2921 * before seeing the alert, and since UDP is connection-less when the
2922 * alert arrives it will be seen as a new connection, which will fail as
2923 * the alert is clearly not a valid ClientHello. This may cause spurious
2924 * failures in tests that use DTLS and resumption with ssl_server2 in
2925 * ssl-opt.sh, avoided by enabling skip_close_notify client-side.
2926 */
2927 if( opt.skip_close_notify == 0 )
2928 {
2929 /* No error checking, the connection might be closed already */
2930 do ret = mbedtls_ssl_close_notify( ssl );
2931 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2932 ret = 0;
2933 }
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002936
2937 /*
2938 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002939 */
2940reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002941 if( opt.reconnect != 0 )
2942 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02002943 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002944
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002945 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002946
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002947#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002948 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02002949 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002950#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002953
Hanno Becker9ec3fe02019-07-01 17:36:12 +01002954#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
2955 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Hanno Beckerf9ca30d2019-02-26 11:38:29 +00002956 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Becker9ec3fe02019-07-01 17:36:12 +01002957#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Hanno Beckerf9ca30d2019-02-26 11:38:29 +00002958
Hanno Becker7d864c42019-09-19 16:51:41 +01002959 if( ( ret = mbedtls_ssl_session_reset( ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002960 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002961 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2962 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002963 goto exit;
2964 }
2965
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02002966 if( opt.reco_mode == 1 )
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02002967 {
Manuel Pégourié-Gonnard57a348b2019-05-20 12:46:26 +02002968 if( ( ret = mbedtls_ssl_session_load( &saved_session,
2969 session_data,
2970 session_data_len ) ) != 0 )
2971 {
2972 mbedtls_printf( " failed\n ! mbedtls_ssl_session_load returned -0x%x\n\n",
2973 -ret );
2974 goto exit;
2975 }
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02002976 }
2977
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03002978#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Hanno Becker7d864c42019-09-19 16:51:41 +01002979 if( ( ret = mbedtls_ssl_set_session( ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002980 {
Manuel Pégourié-Gonnardfbb44a42019-05-16 11:39:42 +02002981 mbedtls_printf( " failed\n ! mbedtls_ssl_set_session returned -0x%x\n\n",
2982 -ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002983 goto exit;
2984 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03002985#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002986
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002987 if( ( ret = mbedtls_net_connect( &server_fd,
2988 opt.server_addr, opt.server_port,
2989 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2990 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002991 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002992 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
2993 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002994 goto exit;
2995 }
2996
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002997 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002998 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002999 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003000 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02003001 if( ret != 0 )
3002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003004 -ret );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02003005 goto exit;
3006 }
3007
Hanno Becker7d864c42019-09-19 16:51:41 +01003008 while( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02003009 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003010 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02003011 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003012 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02003013 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003014 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
3015 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02003016 goto exit;
3017 }
3018 }
3019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003020 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02003021
3022 goto send_request;
3023 }
3024
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003025 /*
3026 * Cleanup and exit
3027 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003028exit:
Hanno Becker7d864c42019-09-19 16:51:41 +01003029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00003031 if( ret != 0 )
3032 {
3033 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034 mbedtls_strerror( ret, error_buf, 100 );
3035 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00003036 }
3037#endif
3038
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003039 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker7d864c42019-09-19 16:51:41 +01003042 mbedtls_x509_crt_free( clicert );
3043 mbedtls_x509_crt_free( cacert );
3044 mbedtls_pk_free( pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02003045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046 mbedtls_ssl_session_free( &saved_session );
Hanno Becker7d864c42019-09-19 16:51:41 +01003047 mbedtls_ssl_free( ssl );
3048 mbedtls_ssl_config_free( conf );
Hanno Becker7f1c8052019-08-26 13:30:13 +01003049#if defined(MBEDTLS_CTR_DRBG_C)
Hanno Becker7d864c42019-09-19 16:51:41 +01003050 mbedtls_ctr_drbg_free( ctr_drbg );
Hanno Becker7f1c8052019-08-26 13:30:13 +01003051#else
Hanno Becker7d864c42019-09-19 16:51:41 +01003052 mbedtls_hmac_drbg_free( hmac_drbg );
Hanno Becker7f1c8052019-08-26 13:30:13 +01003053#endif
Hanno Becker7d864c42019-09-19 16:51:41 +01003054 mbedtls_entropy_free( entropy );
Manuel Pégourié-Gonnard4bb1b992019-05-24 10:26:41 +02003055 if( session_data != NULL )
3056 mbedtls_platform_zeroize( session_data, session_data_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02003057 mbedtls_free( session_data );
Manuel Pégourié-Gonnardc9812292019-07-15 10:31:11 +02003058#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
3059 if( context_buf != NULL )
3060 mbedtls_platform_zeroize( context_buf, context_buf_len );
3061 mbedtls_free( context_buf );
3062#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003063
Hanno Becker7d864c42019-09-19 16:51:41 +01003064 mbedtls_free( ssl );
3065 mbedtls_free( conf );
3066 mbedtls_free( entropy );
Teppo Järvelin8e0e4812019-10-21 10:33:11 +03003067 mbedtls_free( buf );
Hanno Becker7d864c42019-09-19 16:51:41 +01003068#if defined(MBEDTLS_CTR_DRBG_C)
3069 mbedtls_free( ctr_drbg );
3070#else
3071 mbedtls_free( hmac_drbg );
3072#endif
Teppo Järvelin8e0fe192019-10-16 07:43:45 +03003073#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker7d864c42019-09-19 16:51:41 +01003074 mbedtls_free( cacert );
3075 mbedtls_free( clicert );
3076 mbedtls_free( pkey );
Teppo Järvelin8e0fe192019-10-16 07:43:45 +03003077#endif
Paul Bakkercce9d772011-11-18 14:26:47 +00003078#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003080 fflush( stdout ); getchar();
3081#endif
3082
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02003083 // Shell can not handle large exit numbers -> 1 for errors
3084 if( ret < 0 )
3085 ret = 1;
3086
Andrzej Kurek825ebd42020-05-18 11:47:25 -04003087 mbedtls_exit( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003088}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
3090 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Hanno Becker7f1c8052019-08-26 13:30:13 +01003091 ( MBEDTLS_CTR_DRBG_C || MBEDTLS_HMAC_DRBG_C ) && MBEDTLS_TIMING_C */