blob: c29768a65ab4032812889661b02c1e3fe6e1ec9e [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
Hanno Beckerd6d100b2019-03-30 06:27:43 +000038#define mbedtls_calloc calloc
39#define mbedtls_free free
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050040#define mbedtls_exit exit
41#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
42#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evansf90016a2015-01-19 14:26:37 +000043#endif
44
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020045#if !defined(MBEDTLS_ENTROPY_C) || \
46 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020047 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020048int main( void )
49{
50 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
51 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020052 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020053 return( 0 );
54}
55#else
56
Andres AG788aa4a2016-09-14 14:32:09 +010057#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/ssl.h"
59#include "mbedtls/entropy.h"
60#include "mbedtls/ctr_drbg.h"
61#include "mbedtls/certs.h"
62#include "mbedtls/x509.h"
63#include "mbedtls/error.h"
64#include "mbedtls/debug.h"
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020065#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Hanno Beckerb2b468b2018-11-12 17:46:59 +000067#if defined(MBEDTLS_USE_PSA_CRYPTO)
68#include "psa/crypto.h"
Hanno Becker1d911cd2018-11-15 13:06:09 +000069#include "mbedtls/psa_util.h"
Hanno Beckerb2b468b2018-11-12 17:46:59 +000070#endif
71
Rich Evans18b78c72015-02-11 14:06:19 +000072#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010075
Hanno Beckere4ad3e82017-09-18 15:05:46 +010076#define MAX_REQUEST_SIZE 20000
77#define MAX_REQUEST_SIZE_STR "20000"
78
Paul Bakker9caf2d22010-02-18 19:37:19 +000079#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010080#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020081#define DFL_SERVER_PORT "4433"
Paul Bakker9caf2d22010-02-18 19:37:19 +000082#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +020083#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +000084#define DFL_DEBUG_LEVEL 0
Hanno Beckerbb425db2019-04-03 12:59:58 +010085#define DFL_CONTEXT_CRT_CB 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010086#define DFL_NBIO 0
Hanno Becker16970d22017-10-10 15:56:37 +010087#define DFL_EVENT 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020088#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +020089#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +000090#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000091#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000092#define DFL_CRT_FILE ""
93#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +010094#define DFL_KEY_OPAQUE 0
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020095#define DFL_PSK ""
Hanno Becker1d911cd2018-11-15 13:06:09 +000096#define DFL_PSK_OPAQUE 0
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020097#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +020098#define DFL_ECJPAKE_PW NULL
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +020099#define DFL_EC_MAX_OPS -1
Paul Bakker51936882011-02-20 16:05:58 +0000100#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100102#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100103#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200104#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200105#define DFL_MIN_VERSION -1
Paul Bakker1d29fb52012-09-28 13:28:45 +0000106#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000107#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +0200108#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100109#define DFL_AUTH_MODE -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100111#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100112#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200113#define DFL_DHMLEN -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200114#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100115#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +0200116#define DFL_RECO_MODE 1
Hanno Becker90cb3592019-04-09 17:24:19 +0100117#define DFL_CID_ENABLED 0
118#define DFL_CID_VALUE ""
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100119#define DFL_CID_ENABLED_RENEGO -1
120#define DFL_CID_VALUE_RENEGO NULL
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200121#define DFL_RECONNECT_HARD 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200123#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100124#define DFL_CURVES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200126#define DFL_HS_TO_MIN 0
127#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200128#define DFL_DTLS_MTU -1
Hanno Becker4d615912018-08-14 13:33:30 +0100129#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200130#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200131#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100132#define DFL_ETM -1
Jarno Lamsa9831c8a2019-05-29 13:33:32 +0300133#define DFL_SERIALIZE 0
134#define DFL_EXTENDED_MS_ENFORCE -1
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200135#define DFL_CA_CALLBACK 0
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300136#define DFL_EAP_TLS 0
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200137#define DFL_REPRODUCIBLE 0
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100138#define DFL_NSS_KEYLOG 0
139#define DFL_NSS_KEYLOG_FILE NULL
Paul Bakker0e6975b2009-02-10 22:19:10 +0000140
Paul Bakker93c32b22014-04-25 13:40:05 +0200141#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
142#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_X509_CRT_PARSE_C)
Janos Follathae13beb2019-04-05 14:06:58 +0100145#define USAGE_CONTEXT_CRT_CB \
Hanno Beckerbb425db2019-04-03 12:59:58 +0100146 " context_crt_cb=%%d This determines whether the CRT verification callback is bound\n" \
147 " to the SSL configuration of the SSL context.\n" \
148 " Possible values:\n"\
149 " - 0 (default): Use CRT callback bound to configuration\n" \
150 " - 1: Use CRT callback bound to SSL context\n"
151#else
Janos Follathae13beb2019-04-05 14:06:58 +0100152#define USAGE_CONTEXT_CRT_CB ""
Hanno Beckerbb425db2019-04-03 12:59:58 +0100153#endif /* MBEDTLS_X509_CRT_PARSE_C */
154#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000156#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100157 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
158 " default: \"\" (pre-loaded)\n" \
Hanno Becker422d1992019-05-01 11:16:28 +0100159 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100160 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
161 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
Hanno Becker422d1992019-05-01 11:16:28 +0100162 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100163 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
164 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000165 " key_file=%%s default: \"\" (pre-loaded)\n"
166#else
167#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 " No file operations available (MBEDTLS_FS_IO not defined)\n"
169#endif /* MBEDTLS_FS_IO */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100170#else /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200171#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100173#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
174#define USAGE_KEY_OPAQUE \
175 " key_opaque=%%d Handle your private key as if it were opaque\n" \
176 " default: 0 (disabled)\n"
177#else
178#define USAGE_KEY_OPAQUE ""
179#endif
Paul Bakkered27a042013-04-18 22:46:23 +0200180
Hanno Beckera0e20d02019-05-15 14:03:01 +0100181#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +0100182#define USAGE_CID \
183 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
184 " default: 0 (disabled)\n" \
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100185 " cid_renego=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension during renegotiation.\n" \
Hanno Becker32798222019-05-23 17:01:06 +0100186 " default: same as 'cid' parameter\n" \
Hanno Becker90cb3592019-04-09 17:24:19 +0100187 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100188 " default: \"\"\n" \
189 " cid_val_renego=%%s The CID to use for incoming messages (in hex, without 0x) after renegotiation.\n" \
Hanno Becker32798222019-05-23 17:01:06 +0100190 " default: same as 'cid_val' parameter\n"
Hanno Beckera0e20d02019-05-15 14:03:01 +0100191#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +0100192#define USAGE_CID ""
Hanno Beckera0e20d02019-05-15 14:03:01 +0100193#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +0100194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckere86964c2018-10-23 11:37:50 +0100196#define USAGE_PSK_RAW \
Paul Bakkered27a042013-04-18 22:46:23 +0200197 " psk=%%s default: \"\" (in hex, without 0x)\n" \
198 " psk_identity=%%s default: \"Client_identity\"\n"
Hanno Beckere86964c2018-10-23 11:37:50 +0100199#if defined(MBEDTLS_USE_PSA_CRYPTO)
200#define USAGE_PSK_SLOT \
Hanno Becker1d911cd2018-11-15 13:06:09 +0000201 " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
202 " Enable this to store the PSK configured through command line\n" \
203 " parameter `psk` in a PSA-based key slot.\n" \
Hanno Beckere86964c2018-10-23 11:37:50 +0100204 " Note: Currently only supported in conjunction with\n" \
205 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
206 " to force a particular PSK-only ciphersuite.\n" \
207 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
208 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
209 " with prepopulated key slots instead of importing raw key material.\n"
210#else
211#define USAGE_PSK_SLOT ""
212#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker1bac87c2019-03-29 12:02:26 +0000213#define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
214#else
215#define USAGE_PSK ""
216#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
217
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200218#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
219#define USAGE_CA_CALLBACK \
220 " ca_callback=%%d default: 0 (disabled)\n" \
221 " Enable this to use the trusted certificate callback function\n"
222#else
223#define USAGE_CA_CALLBACK ""
224#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5690efc2011-05-26 13:16:06 +0000225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200227#define USAGE_TICKETS \
228 " tickets=%%d default: 1 (enabled)\n"
229#else
230#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200232
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300233#if defined(MBEDTLS_SSL_EXPORT_KEYS)
234#define USAGE_EAP_TLS \
235 " eap_tls=%%d default: 0 (disabled)\n"
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100236#define USAGE_NSS_KEYLOG \
237 " nss_keylog=%%d default: 0 (disabled)\n"
238#define USAGE_NSS_KEYLOG_FILE \
239 " nss_keylog_file=%%s\n"
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300240#else
241#define USAGE_EAP_TLS ""
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100242#define USAGE_NSS_KEYLOG ""
243#define USAGE_NSS_KEYLOG_FILE ""
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300244#endif /* MBEDTLS_SSL_EXPORT_KEYS */
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200247#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100248 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200249#else
250#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200254#define USAGE_MAX_FRAG_LEN \
255 " max_frag_len=%%d default: 16384 (tls default)\n" \
256 " options: 512, 1024, 2048, 4096\n"
257#else
258#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100262#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200263 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100264#else
265#define USAGE_RECSPLIT
266#endif
267
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200268#if defined(MBEDTLS_DHM_C)
269#define USAGE_DHMLEN \
270 " dhmlen=%%d default: (library default: 1024 bits)\n"
271#else
272#define USAGE_DHMLEN
273#endif
274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200276#define USAGE_ALPN \
277 " alpn=%%s default: \"\" (disabled)\n" \
278 " example: spdy/1,http/1.1\n"
279#else
280#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200282
Hanno Beckere6706e62017-05-15 16:05:15 +0100283#if defined(MBEDTLS_ECP_C)
284#define USAGE_CURVES \
285 " curves=a,b,c,d default: \"default\" (library default)\n" \
286 " example: \"secp521r1,brainpoolP512r1\"\n" \
287 " - use \"none\" for empty list\n" \
288 " - see mbedtls_ecp_curve_list()\n" \
289 " for acceptable curve names\n"
290#else
291#define USAGE_CURVES ""
292#endif
293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200295#define USAGE_DTLS \
296 " dtls=%%d default: 0 (TLS)\n" \
297 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200298 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Becker4d615912018-08-14 13:33:30 +0100299 " mtu=%%d default: (library default: unlimited)\n" \
300 " dgram_packing=%%d default: 1 (allowed)\n" \
301 " allow or forbid packing of multiple\n" \
302 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200303#else
304#define USAGE_DTLS ""
305#endif
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200308#define USAGE_FALLBACK \
309 " fallback=0/1 default: (library default: off)\n"
310#else
311#define USAGE_FALLBACK ""
312#endif
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200315#define USAGE_EMS \
316 " extended_ms=0/1 default: (library default: on)\n"
317#else
318#define USAGE_EMS ""
319#endif
320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100322#define USAGE_ETM \
323 " etm=0/1 default: (library default: on)\n"
324#else
325#define USAGE_ETM ""
326#endif
327
Philippe Antoine738153a2019-06-18 20:16:43 +0200328#define USAGE_REPRODUCIBLE \
329 " reproducible=0/1 default: 0 (disabled)\n"
330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100332#define USAGE_RENEGO \
333 " renegotiation=%%d default: 0 (disabled)\n" \
334 " renegotiate=%%d default: 0 (disabled)\n"
335#else
336#define USAGE_RENEGO ""
337#endif
338
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200339#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
340#define USAGE_ECJPAKE \
341 " ecjpake_pw=%%s default: none (disabled)\n"
342#else
343#define USAGE_ECJPAKE ""
344#endif
345
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200346#if defined(MBEDTLS_ECP_RESTARTABLE)
347#define USAGE_ECRESTART \
348 " ec_max_ops=%%s default: library default (restart disabled)\n"
349#else
350#define USAGE_ECRESTART ""
351#endif
352
Jarno Lamsabbc7b412019-06-04 11:06:31 +0300353#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
354#define USAGE_SERIALIZATION \
Jarno Lamsa304d61c2019-06-06 10:40:52 +0300355 " serialize=%%d default: 0 (do not serialize/deserialize)\n" \
Jarno Lamsa1a7f7932019-06-07 08:39:24 +0300356 " options: 1 (serialize)\n" \
357 " 2 (serialize with re-initialization)\n"
Jarno Lamsabbc7b412019-06-04 11:06:31 +0300358#else
359#define USAGE_SERIALIZATION ""
360#endif
361
Paul Bakker9caf2d22010-02-18 19:37:19 +0000362#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000363 "\n usage: ssl_client2 param=<>...\n" \
364 "\n acceptable parameters:\n" \
365 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100366 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000367 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000368 " request_page=%%s default: \".\"\n" \
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +0100369 " request_size=%%d default: about 34 (basic request)\n" \
370 " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
371 " If 0, in the first exchange only an empty\n" \
372 " application data message is sent followed by\n" \
373 " a second non-empty message before attempting\n" \
374 " to read a response from the server\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100375 " debug_level=%%d default: 0 (disabled)\n" \
376 " nbio=%%d default: 0 (blocking I/O)\n" \
377 " options: 1 (non-blocking), 2 (added delays)\n" \
378 " event=%%d default: 0 (loop)\n" \
379 " options: 1 (level-triggered, implies nbio=1),\n" \
380 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200381 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100382 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200383 USAGE_DTLS \
Hanno Becker90cb3592019-04-09 17:24:19 +0100384 USAGE_CID \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200385 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100386 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100387 " options: none, optional, required\n" \
388 USAGE_IO \
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100389 USAGE_KEY_OPAQUE \
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200390 USAGE_CA_CALLBACK \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100391 "\n" \
392 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200393 USAGE_ECJPAKE \
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200394 USAGE_ECRESTART \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100395 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100396 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100397 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200398 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +0200399 " reconnect=%%d number of reconnections using session resumption\n" \
400 " default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200401 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +0200402 " reco_mode=%%d 0: copy session, 1: serialize session\n" \
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +0200403 " default: 1\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200404 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200405 USAGE_TICKETS \
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300406 USAGE_EAP_TLS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100407 USAGE_MAX_FRAG_LEN \
408 USAGE_TRUNC_HMAC \
Janos Follathae13beb2019-04-05 14:06:58 +0100409 USAGE_CONTEXT_CRT_CB \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200410 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200411 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200412 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100413 USAGE_ETM \
Philippe Antoine738153a2019-06-18 20:16:43 +0200414 USAGE_REPRODUCIBLE \
Hanno Beckere6706e62017-05-15 16:05:15 +0100415 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100416 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200417 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000418 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100419 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200420 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200421 " min_version=%%s default: (library default: tls1)\n" \
422 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000423 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100424 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000425 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000426 " force_ciphersuite=<name> default: all enabled\n"\
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100427 " query_config=<name> return 0 if the specified\n" \
428 " configuration macro is defined and 1\n" \
429 " otherwise. The expansion of the macro\n" \
430 " is printed if it is defined\n" \
Jarno Lamsabbc7b412019-06-04 11:06:31 +0300431 USAGE_SERIALIZATION \
Paul Bakker51936882011-02-20 16:05:58 +0000432 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000433
Hanno Becker8651a432017-06-09 16:13:22 +0100434#define ALPN_LIST_SIZE 10
435#define CURVE_LIST_SIZE 20
436
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500437
Rich Evans85b05ec2015-02-12 11:37:29 +0000438/*
439 * global options
440 */
441struct options
442{
443 const char *server_name; /* hostname of the server (client only) */
444 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200445 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000446 int debug_level; /* level of debugging */
447 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100448 int event; /* loop or event-driven IO? level or edge triggered? */
449 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000450 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000451 const char *request_page; /* page on server to request */
452 int request_size; /* pad request with header to requested size */
453 const char *ca_file; /* the file with the CA certificate(s) */
454 const char *ca_path; /* the path with the CA certificate(s) reside */
455 const char *crt_file; /* the file with the client certificate */
456 const char *key_file; /* the file with the client key */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100457 int key_opaque; /* handle private key as if it were opaque */
Hanno Beckere86964c2018-10-23 11:37:50 +0100458#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000459 int psk_opaque;
Hanno Beckere86964c2018-10-23 11:37:50 +0100460#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200461#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000462 int ca_callback; /* Use callback for trusted certificate list */
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200463#endif
Rich Evans85b05ec2015-02-12 11:37:29 +0000464 const char *psk; /* the pre-shared key */
465 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200466 const char *ecjpake_pw; /* the EC J-PAKE password */
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200467 int ec_max_ops; /* EC consecutive operations limit */
Rich Evans85b05ec2015-02-12 11:37:29 +0000468 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
469 int renegotiation; /* enable / disable renegotiation */
470 int allow_legacy; /* allow legacy renegotiation */
471 int renegotiate; /* attempt renegotiation? */
472 int renego_delay; /* delay before enforcing renegotiation */
473 int exchanges; /* number of data exchanges */
474 int min_version; /* minimum protocol version accepted */
475 int max_version; /* maximum protocol version accepted */
476 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200477 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000478 int auth_mode; /* verify mode for connection */
479 unsigned char mfl_code; /* code for maximum fragment length */
480 int trunc_hmac; /* negotiate truncated hmac or not */
481 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200482 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000483 int reconnect; /* attempt to resume session */
484 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +0200485 int reco_mode; /* how to keep the session around */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200486 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000487 int tickets; /* enable / disable session tickets */
Hanno Beckere6706e62017-05-15 16:05:15 +0100488 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000489 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000490 int transport; /* TLS or DTLS? */
491 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
492 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200493 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Rich Evans85b05ec2015-02-12 11:37:29 +0000494 int fallback; /* is this a fallback connection? */
Hanno Becker4d615912018-08-14 13:33:30 +0100495 int dgram_packing; /* allow/forbid datagram packing */
Rich Evans85b05ec2015-02-12 11:37:29 +0000496 int extended_ms; /* negotiate extended master secret? */
497 int etm; /* negotiate encrypt then mac? */
Hanno Beckerbb425db2019-04-03 12:59:58 +0100498 int context_crt_cb; /* use context-specific CRT verify callback */
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300499 int eap_tls; /* derive EAP-TLS keying material? */
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100500 int nss_keylog; /* export NSS key log material */
501 const char *nss_keylog_file; /* NSS key log file */
Hanno Becker90cb3592019-04-09 17:24:19 +0100502 int cid_enabled; /* whether to use the CID extension or not */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100503 int cid_enabled_renego; /* whether to use the CID extension or not
504 * during renegotiation */
Hanno Becker90cb3592019-04-09 17:24:19 +0100505 const char *cid_val; /* the CID to use for incoming messages */
Jarno Lamsa9831c8a2019-05-29 13:33:32 +0300506 int serialize; /* serialize/deserialize connection */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100507 const char *cid_val_renego; /* the CID to use for incoming messages
508 * after renegotiation */
Philippe Antoine154feb22019-06-11 17:50:23 +0200509 int reproducible; /* make communication reproducible */
Rich Evans85b05ec2015-02-12 11:37:29 +0000510} opt;
511
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100512int query_config( const char *config );
513
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300514#if defined(MBEDTLS_SSL_EXPORT_KEYS)
515typedef struct eap_tls_keys
516{
517 unsigned char master_secret[48];
518 unsigned char randbytes[64];
Ron Eldor51d3ab52019-05-12 14:54:30 +0300519 mbedtls_tls_prf_types tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300520} eap_tls_keys;
521
522static int eap_tls_key_derivation ( void *p_expkey,
523 const unsigned char *ms,
524 const unsigned char *kb,
525 size_t maclen,
526 size_t keylen,
527 size_t ivlen,
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300528 unsigned char client_random[32],
Ron Eldor51d3ab52019-05-12 14:54:30 +0300529 unsigned char server_random[32],
530 mbedtls_tls_prf_types tls_prf_type )
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300531{
532 eap_tls_keys *keys = (eap_tls_keys *)p_expkey;
533
534 ( ( void ) kb );
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300535 memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) );
536 memcpy( keys->randbytes, client_random, 32 );
537 memcpy( keys->randbytes + 32, server_random, 32 );
Ron Eldor51d3ab52019-05-12 14:54:30 +0300538 keys->tls_prf_type = tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300539
Ron Eldorf75e2522019-05-14 20:38:49 +0300540 if( opt.debug_level > 2 )
541 {
Ron Eldor801faf02019-05-15 17:45:24 +0300542 mbedtls_printf("exported maclen is %u\n", (unsigned)maclen);
543 mbedtls_printf("exported keylen is %u\n", (unsigned)keylen);
544 mbedtls_printf("exported ivlen is %u\n", (unsigned)ivlen);
Ron Eldorf75e2522019-05-14 20:38:49 +0300545 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300546 return( 0 );
547}
Hanno Becker48f3a3d2019-08-29 06:31:22 +0100548
549static int nss_keylog_export( void *p_expkey,
550 const unsigned char *ms,
551 const unsigned char *kb,
552 size_t maclen,
553 size_t keylen,
554 size_t ivlen,
555 unsigned char client_random[32],
556 unsigned char server_random[32],
557 mbedtls_tls_prf_types tls_prf_type )
558{
559 char nss_keylog_line[ 200 ];
560 size_t const client_random_len = 32;
561 size_t const master_secret_len = 48;
562 size_t len = 0;
563 size_t j;
564 int ret = 0;
565
566 ((void) p_expkey);
567 ((void) kb);
568 ((void) maclen);
569 ((void) keylen);
570 ((void) ivlen);
571 ((void) server_random);
572 ((void) tls_prf_type);
573
574 len += sprintf( nss_keylog_line + len,
575 "%s", "CLIENT_RANDOM " );
576
577 for( j = 0; j < client_random_len; j++ )
578 {
579 len += sprintf( nss_keylog_line + len,
580 "%02x", client_random[j] );
581 }
582
583 len += sprintf( nss_keylog_line + len, " " );
584
585 for( j = 0; j < master_secret_len; j++ )
586 {
587 len += sprintf( nss_keylog_line + len,
588 "%02x", ms[j] );
589 }
590
591 len += sprintf( nss_keylog_line + len, "\n" );
592 nss_keylog_line[ len ] = '\0';
593
594 mbedtls_printf( "\n" );
595 mbedtls_printf( "---------------- NSS KEYLOG -----------------\n" );
596 mbedtls_printf( "%s", nss_keylog_line );
597 mbedtls_printf( "---------------------------------------------\n" );
598
599 if( opt.nss_keylog_file != NULL )
600 {
601 FILE *f;
602
603 if( ( f = fopen( opt.nss_keylog_file, "a" ) ) == NULL )
604 {
605 ret = -1;
606 goto exit;
607 }
608
609 if( fwrite( nss_keylog_line, 1, len, f ) != len )
610 {
611 ret = -1;
612 goto exit;
613 }
614
615 fclose( f );
616 }
617
618exit:
619 mbedtls_platform_zeroize( nss_keylog_line,
620 sizeof( nss_keylog_line ) );
621 return( ret );
622}
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300623#endif
624
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200625static void my_debug( void *ctx, int level,
626 const char *file, int line,
627 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000628{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200629 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000630
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200631 /* Extract basename from file */
632 for( p = basename = file; *p != '\0'; p++ )
633 if( *p == '/' || *p == '\\' )
634 basename = p + 1;
635
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100636 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s",
637 basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000638 fflush( (FILE *) ctx );
639}
640
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200641
642mbedtls_time_t dummy_constant_time( mbedtls_time_t* time )
643{
644 (void) time;
645 return 0x5af2a056;
646}
647
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200648int dummy_entropy( void *data, unsigned char *output, size_t len )
649{
650 size_t i;
Philippe Antoine12e85de2019-06-11 16:07:53 +0200651 int ret;
Philippe Antoined2235f22019-06-11 16:29:28 +0200652 (void) data;
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200653
Philippe Antoine3ca50852019-06-07 22:31:59 +0200654 ret = mbedtls_entropy_func( data, output, len );
Philippe Antoine986b6f22019-06-07 15:04:32 +0200655 for ( i = 0; i < len; i++ )
656 {
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200657 //replace result with pseudo random
658 output[i] = (unsigned char) rand();
659 }
Philippe Antoine3ca50852019-06-07 22:31:59 +0200660 return( ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200661}
662
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200663#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000664int ca_callback( void *data, mbedtls_x509_crt const *child,
Janos Follathd7ecbd62019-04-05 14:52:17 +0100665 mbedtls_x509_crt **candidates )
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200666{
Hanno Beckercbb59032019-03-28 14:14:22 +0000667 int ret = 0;
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200668 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +0000669 mbedtls_x509_crt *first;
670
671 /* This is a test-only implementation of the CA callback
672 * which always returns the entire list of trusted certificates.
673 * Production implementations managing a large number of CAs
674 * should use an efficient presentation and lookup for the
675 * set of trusted certificates (such as a hashtable) and only
676 * return those trusted certificates which satisfy basic
677 * parental checks, such as the matching of child `Issuer`
Jarno Lamsaf7a7f9e2019-04-01 15:11:54 +0300678 * and parent `Subject` field or matching key identifiers. */
Hanno Beckercbb59032019-03-28 14:14:22 +0000679 ((void) child);
680
681 first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
682 if( first == NULL )
683 {
684 ret = -1;
685 goto exit;
686 }
687 mbedtls_x509_crt_init( first );
688
689 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
690 {
691 ret = -1;
692 goto exit;
693 }
694
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200695 while( ca->next != NULL )
696 {
697 ca = ca->next;
Hanno Beckercbb59032019-03-28 14:14:22 +0000698 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
699 {
700 ret = -1;
701 goto exit;
702 }
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200703 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000704
705exit:
706
707 if( ret != 0 )
708 {
709 mbedtls_x509_crt_free( first );
710 mbedtls_free( first );
711 first = NULL;
712 }
713
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200714 *candidates = first;
Hanno Beckercbb59032019-03-28 14:14:22 +0000715 return( ret );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200716}
717#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
718
Rich Evans85b05ec2015-02-12 11:37:29 +0000719/*
720 * Test recv/send functions that make sure each try returns
721 * WANT_READ/WANT_WRITE at least once before sucesseding
722 */
Hanno Becker8b1af2f2019-07-03 17:02:43 +0100723
724static int delayed_recv( void *ctx, unsigned char *buf, size_t len )
Rich Evans85b05ec2015-02-12 11:37:29 +0000725{
726 static int first_try = 1;
727 int ret;
728
729 if( first_try )
730 {
731 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100732 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000733 }
734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100736 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000737 first_try = 1; /* Next call will be a new operation */
738 return( ret );
739}
740
Hanno Becker8b1af2f2019-07-03 17:02:43 +0100741static int delayed_send( void *ctx, const unsigned char *buf, size_t len )
Rich Evans85b05ec2015-02-12 11:37:29 +0000742{
743 static int first_try = 1;
744 int ret;
745
746 if( first_try )
747 {
748 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100749 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000750 }
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100753 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000754 first_try = 1; /* Next call will be a new operation */
755 return( ret );
756}
757
Hanno Becker8b1af2f2019-07-03 17:02:43 +0100758typedef struct
759{
760 mbedtls_ssl_context *ssl;
761 mbedtls_net_context *net;
762} io_ctx_t;
763
Hanno Becker4b6649e2019-07-03 17:14:41 +0100764#if defined(MBEDTLS_SSL_RECORD_CHECKING)
765static int ssl_check_record( mbedtls_ssl_context const *ssl,
766 unsigned char const *buf, size_t len )
767{
768 int ret;
769 unsigned char *tmp_buf;
770
771 tmp_buf = mbedtls_calloc( 1, len );
772 if( tmp_buf == NULL )
773 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
774 memcpy( tmp_buf, buf, len );
775
776 ret = mbedtls_ssl_check_record( ssl, tmp_buf, len );
777 if( ret != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE )
778 {
779 int ret_repeated;
780
781 /* Test-only: Make sure that mbedtls_ssl_check_record()
782 * doesn't alter state. */
783 memcpy( tmp_buf, buf, len ); /* Restore buffer */
784 ret_repeated = mbedtls_ssl_check_record( ssl, tmp_buf, len );
785 if( ret != ret_repeated )
786 {
Hanno Becker91f83272019-07-24 13:59:07 +0100787 mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" );
788 return( -1 );
Hanno Becker4b6649e2019-07-03 17:14:41 +0100789 }
790
791 switch( ret )
792 {
793 case 0:
794 break;
795
796 case MBEDTLS_ERR_SSL_INVALID_RECORD:
797 if( opt.debug_level > 1 )
798 mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" );
799 break;
800
801 case MBEDTLS_ERR_SSL_INVALID_MAC:
802 if( opt.debug_level > 1 )
803 mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" );
804 break;
805
806 case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD:
807 if( opt.debug_level > 1 )
808 mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" );
809 break;
810
811 default:
812 mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", -ret );
813 return( -1 );
814 }
815
816 /* Regardless of the outcome, forward the record to the stack. */
817 }
818
Hanno Becker4b6649e2019-07-03 17:14:41 +0100819 mbedtls_free( tmp_buf );
820
821 return( 0 );
822}
823#endif /* MBEDTLS_SSL_RECORD_CHECKING */
824
Hanno Becker8b1af2f2019-07-03 17:02:43 +0100825static int recv_cb( void *ctx, unsigned char *buf, size_t len )
826{
827 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
828 size_t recv_len;
829 int ret;
830
831 if( opt.nbio == 2 )
832 ret = delayed_recv( io_ctx->net, buf, len );
833 else
834 ret = mbedtls_net_recv( io_ctx->net, buf, len );
835 if( ret < 0 )
836 return( ret );
837 recv_len = (size_t) ret;
838
839 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
840 {
841 /* Here's the place to do any datagram/record checking
842 * in between receiving the packet from the underlying
843 * transport and passing it on to the TLS stack. */
Hanno Becker4b6649e2019-07-03 17:14:41 +0100844#if defined(MBEDTLS_SSL_RECORD_CHECKING)
845 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
846 return( -1 );
847#endif /* MBEDTLS_SSL_RECORD_CHECKING */
Hanno Becker8b1af2f2019-07-03 17:02:43 +0100848 }
849
850 return( (int) recv_len );
851}
852
853static int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len,
854 uint32_t timeout )
855{
856 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
857 int ret;
858 size_t recv_len;
859
860 ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout );
861 if( ret < 0 )
862 return( ret );
863 recv_len = (size_t) ret;
864
865 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
866 {
867 /* Here's the place to do any datagram/record checking
868 * in between receiving the packet from the underlying
869 * transport and passing it on to the TLS stack. */
Hanno Becker4b6649e2019-07-03 17:14:41 +0100870#if defined(MBEDTLS_SSL_RECORD_CHECKING)
871 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
872 return( -1 );
873#endif /* MBEDTLS_SSL_RECORD_CHECKING */
Hanno Becker8b1af2f2019-07-03 17:02:43 +0100874 }
875
876 return( (int) recv_len );
877}
878
879static int send_cb( void *ctx, unsigned char const *buf, size_t len )
880{
881 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
882
883 if( opt.nbio == 2 )
884 return( delayed_send( io_ctx->net, buf, len ) );
885
886 return( mbedtls_net_send( io_ctx->net, buf, len ) );
887}
888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckera1051b42019-02-26 11:38:29 +0000890static unsigned char peer_crt_info[1024];
Hanno Beckera9766c2c2019-02-25 17:43:18 +0000891
Rich Evans85b05ec2015-02-12 11:37:29 +0000892/*
893 * Enabled if debug_level > 1 in code below
894 */
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100895static int my_verify( void *data, mbedtls_x509_crt *crt,
896 int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000897{
898 char buf[1024];
899 ((void) data);
900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Hanno Beckera9766c2c2019-02-25 17:43:18 +0000902 if( depth == 0 )
903 memcpy( peer_crt_info, buf, sizeof( buf ) );
904
905 if( opt.debug_level == 0 )
906 return( 0 );
907
908 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000910
Rich Evans85b05ec2015-02-12 11:37:29 +0000911 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100913 else
914 {
915 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
916 mbedtls_printf( "%s\n", buf );
917 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000918
919 return( 0 );
920}
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200921
922static int ssl_sig_hashes_for_test[] = {
923#if defined(MBEDTLS_SHA512_C)
924 MBEDTLS_MD_SHA512,
925 MBEDTLS_MD_SHA384,
926#endif
927#if defined(MBEDTLS_SHA256_C)
928 MBEDTLS_MD_SHA256,
929 MBEDTLS_MD_SHA224,
930#endif
931#if defined(MBEDTLS_SHA1_C)
932 /* Allow SHA-1 as we use it extensively in tests. */
933 MBEDTLS_MD_SHA1,
934#endif
935 MBEDTLS_MD_NONE
936};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000938
Hanno Becker16970d22017-10-10 15:56:37 +0100939/*
940 * Wait for an event from the underlying transport or the timer
941 * (Used in event-driven IO mode).
942 */
943#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000944int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000945 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100946#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000947int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000948 mbedtls_timing_delay_context *timer,
949 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100950#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000951{
Hanno Becker16970d22017-10-10 15:56:37 +0100952
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000953 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +0100954 int poll_type = 0;
955
956 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
957 poll_type = MBEDTLS_NET_POLL_WRITE;
958 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
959 poll_type = MBEDTLS_NET_POLL_READ;
960#if !defined(MBEDTLS_TIMING_C)
961 else
Hanno Beckeref527962018-03-15 15:49:24 +0000962 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100963#endif
964
965 while( 1 )
966 {
Hanno Becker197a91c2017-10-31 10:58:53 +0000967 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +0100968#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +0000969 if( timer != NULL &&
970 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +0100971 {
Hanno Becker16970d22017-10-10 15:56:37 +0100972 break;
973 }
Hanno Becker197a91c2017-10-31 10:58:53 +0000974#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +0100975
Hanno Becker197a91c2017-10-31 10:58:53 +0000976 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000977 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +0100978 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000979 ret = mbedtls_net_poll( fd, poll_type, 0 );
980 if( ret < 0 )
981 return( ret );
982 if( ret == poll_type )
983 break;
Hanno Becker16970d22017-10-10 15:56:37 +0100984 }
985 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000986
987 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100988}
989
Hanno Becker1f583ee2019-04-09 17:12:56 +0100990/* Unhexify `hex` into `dst`. `dst` must have
991 * size at least `strlen( hex ) / 2`. */
Hanno Becker90cb3592019-04-09 17:24:19 +0100992int unhexify( char const *hex, unsigned char *dst )
Hanno Becker1f583ee2019-04-09 17:12:56 +0100993{
994 unsigned char c;
995 size_t j;
996 size_t len = strlen( hex );
997
998 if( len % 2 != 0 )
999 return( -1 );
1000
1001 for( j = 0; j < len; j += 2 )
1002 {
1003 c = hex[j];
1004 if( c >= '0' && c <= '9' )
1005 c -= '0';
1006 else if( c >= 'a' && c <= 'f' )
1007 c -= 'a' - 10;
1008 else if( c >= 'A' && c <= 'F' )
1009 c -= 'A' - 10;
1010 else
1011 return( -1 );
1012 dst[ j / 2 ] = c << 4;
1013
1014 c = hex[j + 1];
1015 if( c >= '0' && c <= '9' )
1016 c -= '0';
1017 else if( c >= 'a' && c <= 'f' )
1018 c -= 'a' - 10;
1019 else if( c >= 'A' && c <= 'F' )
1020 c -= 'A' - 10;
1021 else
1022 return( -1 );
1023 dst[ j / 2 ] |= c;
1024 }
1025
1026 return( 0 );
1027}
1028
Hanno Beckera0e20d02019-05-15 14:03:01 +01001029#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001030int report_cid_usage( mbedtls_ssl_context *ssl,
1031 const char *additional_description )
1032{
1033 int ret;
1034 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
1035 size_t peer_cid_len;
1036 int cid_negotiated;
1037
1038 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1039 return( 0 );
1040
Hanno Becker6ae14c02019-05-22 16:59:25 +01001041 /* Check if the use of a CID has been negotiated,
1042 * but don't ask for the CID value and length.
1043 *
1044 * Note: Here and below, we're demonstrating the various ways
1045 * in which mbedtls_ssl_get_peer_cid() can be called,
1046 * depending on whether or not the length/value of the
1047 * peer's CID is needed.
1048 *
1049 * An actual application, however, should use
1050 * just one call to mbedtls_ssl_get_peer_cid(). */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001051 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
Hanno Becker6ae14c02019-05-22 16:59:25 +01001052 NULL, NULL );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001053 if( ret != 0 )
1054 {
1055 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
1056 -ret );
1057 return( ret );
1058 }
1059
1060 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
1061 {
1062 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
1063 {
1064 mbedtls_printf( "(%s) Use of Connection ID was rejected by the server.\n",
1065 additional_description );
1066 }
1067 }
1068 else
1069 {
1070 size_t idx=0;
1071 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
1072 additional_description );
Hanno Becker6ae14c02019-05-22 16:59:25 +01001073
1074 /* Ask for just the length of the peer's CID. */
1075 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
1076 NULL, &peer_cid_len );
1077 if( ret != 0 )
1078 {
1079 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
1080 -ret );
1081 return( ret );
1082 }
1083
1084 /* Ask for just length + value of the peer's CID. */
1085 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
1086 peer_cid, &peer_cid_len );
1087 if( ret != 0 )
1088 {
1089 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
1090 -ret );
1091 return( ret );
1092 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001093 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
1094 additional_description,
1095 (unsigned) peer_cid_len );
1096 while( idx < peer_cid_len )
1097 {
1098 mbedtls_printf( "%02x ", peer_cid[ idx ] );
1099 idx++;
1100 }
1101 mbedtls_printf( "\n" );
1102 }
1103
1104 return( 0 );
1105}
Hanno Beckera0e20d02019-05-15 14:03:01 +01001106#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001107
Paul Bakker9caf2d22010-02-18 19:37:19 +00001108int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001109{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001110 int ret = 0, len, tail_len, i, written, frags, retry_left;
1111 mbedtls_net_context server_fd;
Hanno Becker8b1af2f2019-07-03 17:02:43 +01001112 io_ctx_t io_ctx;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001113
1114 unsigned char buf[MAX_REQUEST_SIZE + 1];
1115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1117 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001118 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +02001119#endif
Hanno Becker90cb3592019-04-09 17:24:19 +01001120
Hanno Beckera0e20d02019-05-15 14:03:01 +01001121#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +01001122 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001123 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker90cb3592019-04-09 17:24:19 +01001124 size_t cid_len = 0;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001125 size_t cid_renego_len = 0;
Hanno Becker90cb3592019-04-09 17:24:19 +01001126#endif
1127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +01001129 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001130#endif
Hanno Beckere6706e62017-05-15 16:05:15 +01001131#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +01001132 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +01001133 const mbedtls_ecp_curve_info *curve_cur;
1134#endif
1135
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001136 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +00001137
Hanno Beckere86964c2018-10-23 11:37:50 +01001138#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001139 psa_key_handle_t slot = 0;
Hanno Beckere86964c2018-10-23 11:37:50 +01001140 psa_algorithm_t alg = 0;
Janos Follathbe4efc22019-08-08 11:38:18 +01001141 psa_key_attributes_t key_attributes;
Hanno Beckere86964c2018-10-23 11:37:50 +01001142 psa_status_t status;
1143#endif
1144
Gilles Peskineef86ab22017-05-05 18:59:02 +02001145#if defined(MBEDTLS_X509_CRT_PARSE_C)
1146 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
1147#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 mbedtls_entropy_context entropy;
1149 mbedtls_ctr_drbg_context ctr_drbg;
1150 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001151 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02001153 unsigned char *session_data = NULL;
1154 size_t session_data_len = 0;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001155#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001156 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001157#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +02001159 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 mbedtls_x509_crt cacert;
1161 mbedtls_x509_crt clicert;
1162 mbedtls_pk_context pkey;
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001163#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001164 psa_key_handle_t key_slot = 0; /* invalid key slot */
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001165#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001166#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +00001167 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +00001168 const int *list;
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02001169#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1170 unsigned char *context_buf = NULL;
1171 size_t context_buf_len;
1172#endif
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001173#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1174 unsigned char eap_tls_keymaterial[16];
1175 unsigned char eap_tls_iv[8];
1176 const char* eap_tls_label = "client EAP encryption";
1177 eap_tls_keys eap_tls_keying;
1178#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +00001179
Paul Bakker1a207ec2011-02-06 13:22:40 +00001180 /*
1181 * Make sure memory references are valid.
1182 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001183 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001184 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001185 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001187 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188#if defined(MBEDTLS_X509_CRT_PARSE_C)
1189 mbedtls_x509_crt_init( &cacert );
1190 mbedtls_x509_crt_init( &clicert );
1191 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001192#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +02001194 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001195#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +00001196
Hanno Beckerb2b468b2018-11-12 17:46:59 +00001197#if defined(MBEDTLS_USE_PSA_CRYPTO)
1198 status = psa_crypto_init();
1199 if( status != PSA_SUCCESS )
1200 {
1201 mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
1202 (int) status );
1203 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1204 goto exit;
1205 }
1206#endif
1207
Paul Bakker9caf2d22010-02-18 19:37:19 +00001208 if( argc == 0 )
1209 {
1210 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +00001211 if( ret == 0 )
1212 ret = 1;
1213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +00001215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +00001217 while( *list )
1218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001220 list++;
1221 if( !*list )
1222 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +00001224 list++;
1225 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +00001227 goto exit;
1228 }
1229
1230 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001231 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001232 opt.server_port = DFL_SERVER_PORT;
1233 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker90cb3592019-04-09 17:24:19 +01001234 opt.cid_enabled = DFL_CID_ENABLED;
1235 opt.cid_val = DFL_CID_VALUE;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001236 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
1237 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001238 opt.nbio = DFL_NBIO;
Hanno Becker16970d22017-10-10 15:56:37 +01001239 opt.event = DFL_EVENT;
Hanno Beckerbb425db2019-04-03 12:59:58 +01001240 opt.context_crt_cb = DFL_CONTEXT_CRT_CB;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001241 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001242 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001243 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +02001244 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +00001245 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +00001246 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +00001247 opt.crt_file = DFL_CRT_FILE;
1248 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001249 opt.key_opaque = DFL_KEY_OPAQUE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001250 opt.psk = DFL_PSK;
Hanno Beckere86964c2018-10-23 11:37:50 +01001251#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001252 opt.psk_opaque = DFL_PSK_OPAQUE;
Hanno Beckere86964c2018-10-23 11:37:50 +01001253#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001254#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1255 opt.ca_callback = DFL_CA_CALLBACK;
1256#endif
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001257 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001258 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001259 opt.ec_max_ops = DFL_EC_MAX_OPS;
Paul Bakker51936882011-02-20 16:05:58 +00001260 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +00001261 opt.renegotiation = DFL_RENEGOTIATION;
1262 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001263 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001264 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001265 opt.min_version = DFL_MIN_VERSION;
1266 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001267 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001268 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001269 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001270 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001271 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001272 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001273 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001274 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001275 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +02001276 opt.reco_mode = DFL_RECO_MODE;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001277 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001278 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001279 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001280 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001281 opt.transport = DFL_TRANSPORT;
1282 opt.hs_to_min = DFL_HS_TO_MIN;
1283 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001284 opt.dtls_mtu = DFL_DTLS_MTU;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001285 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001286 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001287 opt.etm = DFL_ETM;
Hanno Becker4d615912018-08-14 13:33:30 +01001288 opt.dgram_packing = DFL_DGRAM_PACKING;
Jarno Lamsa9831c8a2019-05-29 13:33:32 +03001289 opt.serialize = DFL_SERIALIZE;
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001290 opt.eap_tls = DFL_EAP_TLS;
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001291 opt.reproducible = DFL_REPRODUCIBLE;
Hanno Becker48f3a3d2019-08-29 06:31:22 +01001292 opt.nss_keylog = DFL_NSS_KEYLOG;
1293 opt.nss_keylog_file = DFL_NSS_KEYLOG_FILE;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001294
1295 for( i = 1; i < argc; i++ )
1296 {
Paul Bakker9caf2d22010-02-18 19:37:19 +00001297 p = argv[i];
1298 if( ( q = strchr( p, '=' ) ) == NULL )
1299 goto usage;
1300 *q++ = '\0';
1301
1302 if( strcmp( p, "server_name" ) == 0 )
1303 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001304 else if( strcmp( p, "server_addr" ) == 0 )
1305 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001306 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001307 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001308 else if( strcmp( p, "dtls" ) == 0 )
1309 {
1310 int t = atoi( q );
1311 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001313 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001315 else
1316 goto usage;
1317 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001318 else if( strcmp( p, "debug_level" ) == 0 )
1319 {
1320 opt.debug_level = atoi( q );
1321 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1322 goto usage;
1323 }
Hanno Beckerbb425db2019-04-03 12:59:58 +01001324 else if( strcmp( p, "context_crt_cb" ) == 0 )
1325 {
1326 opt.context_crt_cb = atoi( q );
1327 if( opt.context_crt_cb != 0 && opt.context_crt_cb != 1 )
1328 goto usage;
1329 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001330 else if( strcmp( p, "nbio" ) == 0 )
1331 {
1332 opt.nbio = atoi( q );
1333 if( opt.nbio < 0 || opt.nbio > 2 )
1334 goto usage;
1335 }
Hanno Becker16970d22017-10-10 15:56:37 +01001336 else if( strcmp( p, "event" ) == 0 )
1337 {
1338 opt.event = atoi( q );
1339 if( opt.event < 0 || opt.event > 2 )
1340 goto usage;
1341 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001342 else if( strcmp( p, "read_timeout" ) == 0 )
1343 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001344 else if( strcmp( p, "max_resend" ) == 0 )
1345 {
1346 opt.max_resend = atoi( q );
1347 if( opt.max_resend < 0 )
1348 goto usage;
1349 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001350 else if( strcmp( p, "request_page" ) == 0 )
1351 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +02001352 else if( strcmp( p, "request_size" ) == 0 )
1353 {
1354 opt.request_size = atoi( q );
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001355 if( opt.request_size < 0 ||
1356 opt.request_size > MAX_REQUEST_SIZE )
Paul Bakker93c32b22014-04-25 13:40:05 +02001357 goto usage;
1358 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001359 else if( strcmp( p, "ca_file" ) == 0 )
1360 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +00001361 else if( strcmp( p, "ca_path" ) == 0 )
1362 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +00001363 else if( strcmp( p, "crt_file" ) == 0 )
1364 opt.crt_file = q;
1365 else if( strcmp( p, "key_file" ) == 0 )
1366 opt.key_file = q;
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001367#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
1368 else if( strcmp( p, "key_opaque" ) == 0 )
1369 opt.key_opaque = atoi( q );
1370#endif
Hanno Beckera0e20d02019-05-15 14:03:01 +01001371#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +01001372 else if( strcmp( p, "cid" ) == 0 )
1373 {
1374 opt.cid_enabled = atoi( q );
1375 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
1376 goto usage;
1377 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001378 else if( strcmp( p, "cid_renego" ) == 0 )
1379 {
1380 opt.cid_enabled_renego = atoi( q );
1381 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
1382 goto usage;
1383 }
Hanno Becker90cb3592019-04-09 17:24:19 +01001384 else if( strcmp( p, "cid_val" ) == 0 )
1385 {
1386 opt.cid_val = q;
1387 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001388 else if( strcmp( p, "cid_val_renego" ) == 0 )
1389 {
1390 opt.cid_val_renego = q;
1391 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001392#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001393 else if( strcmp( p, "psk" ) == 0 )
1394 opt.psk = q;
Hanno Beckere86964c2018-10-23 11:37:50 +01001395#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001396 else if( strcmp( p, "psk_opaque" ) == 0 )
1397 opt.psk_opaque = atoi( q );
Hanno Beckere86964c2018-10-23 11:37:50 +01001398#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001399#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1400 else if( strcmp( p, "ca_callback" ) == 0)
1401 opt.ca_callback = atoi( q );
1402#endif
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001403 else if( strcmp( p, "psk_identity" ) == 0 )
1404 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001405 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1406 opt.ecjpake_pw = q;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001407 else if( strcmp( p, "ec_max_ops" ) == 0 )
1408 opt.ec_max_ops = atoi( q );
Paul Bakker51936882011-02-20 16:05:58 +00001409 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +00001412
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001413 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +00001414 {
1415 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +00001416 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +00001417 }
Paul Bakker51936882011-02-20 16:05:58 +00001418 opt.force_ciphersuite[1] = 0;
1419 }
Paul Bakker48916f92012-09-16 19:57:18 +00001420 else if( strcmp( p, "renegotiation" ) == 0 )
1421 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001422 opt.renegotiation = (atoi( q )) ?
1423 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1424 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +00001425 }
1426 else if( strcmp( p, "allow_legacy" ) == 0 )
1427 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001428 switch( atoi( q ) )
1429 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001430 case -1:
1431 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1432 break;
1433 case 0:
1434 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1435 break;
1436 case 1:
1437 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1438 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001439 default: goto usage;
1440 }
Paul Bakker48916f92012-09-16 19:57:18 +00001441 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001442 else if( strcmp( p, "renegotiate" ) == 0 )
1443 {
1444 opt.renegotiate = atoi( q );
1445 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1446 goto usage;
1447 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001448 else if( strcmp( p, "exchanges" ) == 0 )
1449 {
1450 opt.exchanges = atoi( q );
1451 if( opt.exchanges < 1 )
1452 goto usage;
1453 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001454 else if( strcmp( p, "reconnect" ) == 0 )
1455 {
1456 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001457 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001458 goto usage;
1459 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001460 else if( strcmp( p, "reco_delay" ) == 0 )
1461 {
1462 opt.reco_delay = atoi( q );
1463 if( opt.reco_delay < 0 )
1464 goto usage;
1465 }
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +02001466 else if( strcmp( p, "reco_mode" ) == 0 )
1467 {
1468 opt.reco_mode = atoi( q );
1469 if( opt.reco_mode < 0 )
1470 goto usage;
1471 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001472 else if( strcmp( p, "reconnect_hard" ) == 0 )
1473 {
1474 opt.reconnect_hard = atoi( q );
1475 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
1476 goto usage;
1477 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001478 else if( strcmp( p, "tickets" ) == 0 )
1479 {
1480 opt.tickets = atoi( q );
1481 if( opt.tickets < 0 || opt.tickets > 2 )
1482 goto usage;
1483 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001484 else if( strcmp( p, "alpn" ) == 0 )
1485 {
1486 opt.alpn_string = q;
1487 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001488 else if( strcmp( p, "fallback" ) == 0 )
1489 {
1490 switch( atoi( q ) )
1491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
1493 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001494 default: goto usage;
1495 }
1496 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001497 else if( strcmp( p, "extended_ms" ) == 0 )
1498 {
1499 switch( atoi( q ) )
1500 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001501 case 0:
1502 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1503 break;
1504 case 1:
1505 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1506 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001507 default: goto usage;
1508 }
1509 }
Hanno Beckere6706e62017-05-15 16:05:15 +01001510 else if( strcmp( p, "curves" ) == 0 )
1511 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001512 else if( strcmp( p, "etm" ) == 0 )
1513 {
1514 switch( atoi( q ) )
1515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1517 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001518 default: goto usage;
1519 }
1520 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001521 else if( strcmp( p, "min_version" ) == 0 )
1522 {
1523 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001525 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001527 else if( strcmp( q, "tls1_1" ) == 0 ||
1528 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001530 else if( strcmp( q, "tls1_2" ) == 0 ||
1531 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001533 else
1534 goto usage;
1535 }
1536 else if( strcmp( p, "max_version" ) == 0 )
1537 {
1538 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001540 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001542 else if( strcmp( q, "tls1_1" ) == 0 ||
1543 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001545 else if( strcmp( q, "tls1_2" ) == 0 ||
1546 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001548 else
1549 goto usage;
1550 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001551 else if( strcmp( p, "arc4" ) == 0 )
1552 {
1553 switch( atoi( q ) )
1554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1556 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001557 default: goto usage;
1558 }
1559 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02001560 else if( strcmp( p, "allow_sha1" ) == 0 )
1561 {
1562 switch( atoi( q ) )
1563 {
1564 case 0: opt.allow_sha1 = 0; break;
1565 case 1: opt.allow_sha1 = 1; break;
1566 default: goto usage;
1567 }
1568 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001569 else if( strcmp( p, "force_version" ) == 0 )
1570 {
1571 if( strcmp( q, "ssl3" ) == 0 )
1572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1574 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001575 }
1576 else if( strcmp( q, "tls1" ) == 0 )
1577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1579 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001580 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001581 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1584 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001585 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001586 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1589 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001590 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001591 else if( strcmp( q, "dtls1" ) == 0 )
1592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1594 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1595 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001596 }
1597 else if( strcmp( q, "dtls1_2" ) == 0 )
1598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1600 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1601 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001602 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001603 else
1604 goto usage;
1605 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001606 else if( strcmp( p, "auth_mode" ) == 0 )
1607 {
1608 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001610 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001612 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001614 else
1615 goto usage;
1616 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001617 else if( strcmp( p, "max_frag_len" ) == 0 )
1618 {
1619 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001621 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001623 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001625 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001627 else
1628 goto usage;
1629 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001630 else if( strcmp( p, "trunc_hmac" ) == 0 )
1631 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001632 switch( atoi( q ) )
1633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1635 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001636 default: goto usage;
1637 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001638 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001639 else if( strcmp( p, "hs_timeout" ) == 0 )
1640 {
1641 if( ( p = strchr( q, '-' ) ) == NULL )
1642 goto usage;
1643 *p++ = '\0';
1644 opt.hs_to_min = atoi( q );
1645 opt.hs_to_max = atoi( p );
1646 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1647 goto usage;
1648 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001649 else if( strcmp( p, "mtu" ) == 0 )
1650 {
1651 opt.dtls_mtu = atoi( q );
1652 if( opt.dtls_mtu < 0 )
1653 goto usage;
1654 }
Hanno Becker4d615912018-08-14 13:33:30 +01001655 else if( strcmp( p, "dgram_packing" ) == 0 )
1656 {
1657 opt.dgram_packing = atoi( q );
1658 if( opt.dgram_packing != 0 &&
1659 opt.dgram_packing != 1 )
1660 {
1661 goto usage;
1662 }
1663 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001664 else if( strcmp( p, "recsplit" ) == 0 )
1665 {
1666 opt.recsplit = atoi( q );
1667 if( opt.recsplit < 0 || opt.recsplit > 1 )
1668 goto usage;
1669 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001670 else if( strcmp( p, "dhmlen" ) == 0 )
1671 {
1672 opt.dhmlen = atoi( q );
1673 if( opt.dhmlen < 0 )
1674 goto usage;
1675 }
Andres Amaya Garciabc818842018-10-16 21:08:38 +01001676 else if( strcmp( p, "query_config" ) == 0 )
1677 {
1678 return query_config( q );
1679 }
Jarno Lamsa9831c8a2019-05-29 13:33:32 +03001680 else if( strcmp( p, "serialize") == 0 )
1681 {
1682 opt.serialize = atoi( q );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03001683 if( opt.serialize < 0 || opt.serialize > 2)
Jarno Lamsa9831c8a2019-05-29 13:33:32 +03001684 goto usage;
1685 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001686 else if( strcmp( p, "eap_tls" ) == 0 )
1687 {
1688 opt.eap_tls = atoi( q );
1689 if( opt.eap_tls < 0 || opt.eap_tls > 1 )
1690 goto usage;
1691 }
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001692 else if( strcmp( p, "reproducible" ) == 0 )
1693 {
1694 opt.reproducible = 1;
1695 }
Hanno Becker48f3a3d2019-08-29 06:31:22 +01001696 else if( strcmp( p, "nss_keylog" ) == 0 )
1697 {
1698 opt.nss_keylog = atoi( q );
1699 if( opt.nss_keylog < 0 || opt.nss_keylog > 1 )
1700 goto usage;
1701 }
1702 else if( strcmp( p, "nss_keylog_file" ) == 0 )
1703 {
1704 opt.nss_keylog_file = q;
1705 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001706 else
1707 goto usage;
1708 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001709
Hanno Becker16970d22017-10-10 15:56:37 +01001710 /* Event-driven IO is incompatible with the above custom
1711 * receive and send functions, as the polling builds on
1712 * refers to the underlying net_context. */
1713 if( opt.event == 1 && opt.nbio != 1 )
1714 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001715 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001716 opt.nbio = 1;
1717 }
1718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719#if defined(MBEDTLS_DEBUG_C)
1720 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001721#endif
1722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001724 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001725 * Unhexify the pre-shared key if any is given
1726 */
1727 if( strlen( opt.psk ) )
1728 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001729 psk_len = strlen( opt.psk ) / 2;
1730 if( psk_len > sizeof( psk ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001731 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001732 mbedtls_printf( "pre-shared key too long\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001733 goto exit;
1734 }
1735
Hanno Becker1f583ee2019-04-09 17:12:56 +01001736 if( unhexify( opt.psk, psk ) != 0 )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001737 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001738 mbedtls_printf( "pre-shared key not valid hex\n" );
1739 goto exit;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001740 }
1741 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001743
Hanno Beckere86964c2018-10-23 11:37:50 +01001744#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001745 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001746 {
1747 if( opt.psk == NULL )
1748 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00001749 mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
Hanno Beckere86964c2018-10-23 11:37:50 +01001750 ret = 2;
1751 goto usage;
1752 }
1753
1754 if( opt.force_ciphersuite[0] <= 0 )
1755 {
1756 mbedtls_printf( "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n" );
1757 ret = 2;
1758 goto usage;
1759 }
1760 }
1761#endif /* MBEDTLS_USE_PSA_CRYPTO */
1762
1763 if( opt.force_ciphersuite[0] > 0 )
1764 {
1765 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1766 ciphersuite_info =
1767 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1768
1769 if( opt.max_version != -1 &&
1770 ciphersuite_info->min_minor_ver > opt.max_version )
1771 {
1772 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
1773 ret = 2;
1774 goto usage;
1775 }
1776 if( opt.min_version != -1 &&
1777 ciphersuite_info->max_minor_ver < opt.min_version )
1778 {
1779 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
1780 ret = 2;
1781 goto usage;
1782 }
1783
1784 /* If the server selects a version that's not supported by
1785 * this suite, then there will be no common ciphersuite... */
1786 if( opt.max_version == -1 ||
1787 opt.max_version > ciphersuite_info->max_minor_ver )
1788 {
1789 opt.max_version = ciphersuite_info->max_minor_ver;
1790 }
1791 if( opt.min_version < ciphersuite_info->min_minor_ver )
1792 {
1793 opt.min_version = ciphersuite_info->min_minor_ver;
1794 /* DTLS starts with TLS 1.1 */
1795 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1796 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1797 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1798 }
1799
1800 /* Enable RC4 if needed and not explicitly disabled */
1801 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
1802 {
1803 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
1804 {
1805 mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" );
1806 ret = 2;
1807 goto usage;
1808 }
1809
1810 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
1811 }
1812
Hanno Becker90cb3592019-04-09 17:24:19 +01001813
Hanno Beckere86964c2018-10-23 11:37:50 +01001814#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001815 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001816 {
1817 /* Ensure that the chosen ciphersuite is PSK-only; we must know
1818 * the ciphersuite in advance to set the correct policy for the
1819 * PSK key slot. This limitation might go away in the future. */
1820 if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
1821 opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 )
1822 {
1823 mbedtls_printf( "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n" );
1824 ret = 2;
1825 goto usage;
1826 }
1827
1828 /* Determine KDF algorithm the opaque PSK will be used in. */
1829#if defined(MBEDTLS_SHA512_C)
1830 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1831 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1832 else
1833#endif /* MBEDTLS_SHA512_C */
1834 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1835 }
1836#endif /* MBEDTLS_USE_PSA_CRYPTO */
1837 }
1838
Hanno Beckera0e20d02019-05-15 14:03:01 +01001839#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001840 cid_len = strlen( opt.cid_val ) / 2;
1841 if( cid_len > sizeof( cid ) )
1842 {
1843 mbedtls_printf( "CID too long\n" );
1844 goto exit;
1845 }
Hanno Becker90cb3592019-04-09 17:24:19 +01001846
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001847 if( unhexify( opt.cid_val, cid ) != 0 )
1848 {
1849 mbedtls_printf( "CID not valid hex\n" );
1850 goto exit;
1851 }
1852
1853 /* Keep CID settings for renegotiation unless
1854 * specified otherwise. */
1855 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
1856 opt.cid_enabled_renego = opt.cid_enabled;
1857 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
1858 opt.cid_val_renego = opt.cid_val;
1859
1860 cid_renego_len = strlen( opt.cid_val_renego ) / 2;
1861 if( cid_renego_len > sizeof( cid_renego ) )
1862 {
1863 mbedtls_printf( "CID too long\n" );
1864 goto exit;
1865 }
1866
1867 if( unhexify( opt.cid_val_renego, cid_renego ) != 0 )
1868 {
1869 mbedtls_printf( "CID not valid hex\n" );
1870 goto exit;
1871 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001872#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01001873
Hanno Beckere6706e62017-05-15 16:05:15 +01001874#if defined(MBEDTLS_ECP_C)
1875 if( opt.curves != NULL )
1876 {
1877 p = (char *) opt.curves;
1878 i = 0;
1879
1880 if( strcmp( p, "none" ) == 0 )
1881 {
1882 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1883 }
1884 else if( strcmp( p, "default" ) != 0 )
1885 {
1886 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001887 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001888 {
1889 q = p;
1890
1891 /* Terminate the current string */
1892 while( *p != ',' && *p != '\0' )
1893 p++;
1894 if( *p == ',' )
1895 *p++ = '\0';
1896
1897 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1898 {
1899 curve_list[i++] = curve_cur->grp_id;
1900 }
1901 else
1902 {
1903 mbedtls_printf( "unknown curve %s\n", q );
1904 mbedtls_printf( "supported curves: " );
1905 for( curve_cur = mbedtls_ecp_curve_list();
1906 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1907 curve_cur++ )
1908 {
1909 mbedtls_printf( "%s ", curve_cur->name );
1910 }
1911 mbedtls_printf( "\n" );
1912 goto exit;
1913 }
1914 }
1915
1916 mbedtls_printf("Number of curves: %d\n", i );
1917
Hanno Becker8651a432017-06-09 16:13:22 +01001918 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001919 {
Hanno Becker8651a432017-06-09 16:13:22 +01001920 mbedtls_printf( "curves list too long, maximum %d",
1921 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001922 goto exit;
1923 }
1924
1925 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1926 }
1927 }
1928#endif /* MBEDTLS_ECP_C */
1929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001931 if( opt.alpn_string != NULL )
1932 {
1933 p = (char *) opt.alpn_string;
1934 i = 0;
1935
1936 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001937 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001938 {
1939 alpn_list[i++] = p;
1940
1941 /* Terminate the current string and move on to next one */
1942 while( *p != ',' && *p != '\0' )
1943 p++;
1944 if( *p == ',' )
1945 *p++ = '\0';
1946 }
1947 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001949
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001950 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001951 * 0. Initialize the RNG and the session data
1952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001954 fflush( stdout );
1955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 mbedtls_entropy_init( &entropy );
Philippe Antoine986b6f22019-06-07 15:04:32 +02001957 if (opt.reproducible)
Paul Bakker508ad5a2011-12-04 17:09:26 +00001958 {
Philippe Antoine738153a2019-06-18 20:16:43 +02001959 srand( 1 );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001960 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy,
Philippe Antoine986b6f22019-06-07 15:04:32 +02001961 &entropy, (const unsigned char *) pers,
1962 strlen( pers ) ) ) != 0 )
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001963 {
1964 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
Philippe Antoine986b6f22019-06-07 15:04:32 +02001965 -ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001966 goto exit;
1967 }
Philippe Antoine986b6f22019-06-07 15:04:32 +02001968 }
1969 else
1970 {
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001971 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
Philippe Antoine986b6f22019-06-07 15:04:32 +02001972 &entropy, (const unsigned char *) pers,
1973 strlen( pers ) ) ) != 0 )
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001974 {
1975 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
Philippe Antoine986b6f22019-06-07 15:04:32 +02001976 -ret );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001977 goto exit;
1978 }
Paul Bakker508ad5a2011-12-04 17:09:26 +00001979 }
1980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 /*
1985 * 1.1. Load the trusted CA
1986 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001988 fflush( stdout );
1989
Hanno Becker623e7b42019-03-05 16:02:15 +00001990 if( strcmp( opt.ca_path, "none" ) == 0 ||
1991 strcmp( opt.ca_file, "none" ) == 0 )
1992 {
1993 ret = 0;
1994 }
1995 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001997 if( strlen( opt.ca_path ) )
Hanno Becker623e7b42019-03-05 16:02:15 +00001998 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001999 else if( strlen( opt.ca_file ) )
Hanno Becker623e7b42019-03-05 16:02:15 +00002000 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02002001 else
Paul Bakker5690efc2011-05-26 13:16:06 +00002002#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003#if defined(MBEDTLS_CERTS_C)
Hanno Becker2900b142019-02-01 08:15:06 +00002004 {
2005#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 ret = mbedtls_x509_crt_parse( &cacert,
2009 (const unsigned char *) mbedtls_test_cas[i],
2010 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002011 if( ret != 0 )
2012 break;
2013 }
Hanno Becker2900b142019-02-01 08:15:06 +00002014 if( ret == 0 )
2015#endif /* MBEDTLS_PEM_PARSE_C */
2016 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
2017 {
2018 ret = mbedtls_x509_crt_parse_der( &cacert,
2019 (const unsigned char *) mbedtls_test_cas_der[i],
2020 mbedtls_test_cas_der_len[i] );
2021 if( ret != 0 )
2022 break;
2023 }
2024 }
Paul Bakker5690efc2011-05-26 13:16:06 +00002025#else
2026 {
2027 ret = 1;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002028 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00002029 }
Hanno Becker2900b142019-02-01 08:15:06 +00002030#endif /* MBEDTLS_CERTS_C */
Paul Bakker42488232012-05-16 08:21:05 +00002031 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002033 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
2034 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 goto exit;
2036 }
2037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002039
2040 /*
2041 * 1.2. Load own certificate and private key
2042 *
2043 * (can be skipped if client authentication is not required)
2044 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 fflush( stdout );
2047
Hanno Becker623e7b42019-03-05 16:02:15 +00002048 if( strcmp( opt.crt_file, "none" ) == 0 )
2049 ret = 0;
2050 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00002052 if( strlen( opt.crt_file ) )
Hanno Becker623e7b42019-03-05 16:02:15 +00002053 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02002054 else
Paul Bakker5690efc2011-05-26 13:16:06 +00002055#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01002057 ret = mbedtls_x509_crt_parse( &clicert,
2058 (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00002060#else
2061 {
2062 ret = 1;
Hanno Beckera0c5ceb2018-12-05 16:49:42 +00002063 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00002064 }
2065#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 if( ret != 0 )
2067 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002068 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
2069 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002070 goto exit;
2071 }
2072
Hanno Becker623e7b42019-03-05 16:02:15 +00002073 if( strcmp( opt.key_file, "none" ) == 0 )
2074 ret = 0;
2075 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00002077 if( strlen( opt.key_file ) )
Hanno Becker623e7b42019-03-05 16:02:15 +00002078 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00002079 else
Paul Bakker5690efc2011-05-26 13:16:06 +00002080#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01002082 ret = mbedtls_pk_parse_key( &pkey,
2083 (const unsigned char *) mbedtls_test_cli_key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00002085#else
2086 {
2087 ret = 1;
Hanno Beckera0c5ceb2018-12-05 16:49:42 +00002088 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00002089 }
2090#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 if( ret != 0 )
2092 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002093 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
2094 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 goto exit;
2096 }
2097
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01002098#if defined(MBEDTLS_USE_PSA_CRYPTO)
2099 if( opt.key_opaque != 0 )
2100 {
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01002101 if( ( ret = mbedtls_pk_wrap_as_opaque( &pkey, &key_slot,
2102 PSA_ALG_SHA_256 ) ) != 0 )
2103 {
2104 mbedtls_printf( " failed\n ! "
2105 "mbedtls_pk_wrap_as_opaque returned -0x%x\n\n", -ret );
2106 goto exit;
2107 }
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01002108 }
2109#endif /* MBEDTLS_USE_PSA_CRYPTO */
2110
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01002111 mbedtls_printf( " ok (key type: %s)\n", mbedtls_pk_get_name( &pkey ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002113
2114 /*
2115 * 2. Start the connection
2116 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01002117 if( opt.server_addr == NULL)
2118 opt.server_addr = opt.server_name;
2119
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02002120 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002122 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 fflush( stdout );
2124
Hanno Becker16970d22017-10-10 15:56:37 +01002125 if( ( ret = mbedtls_net_connect( &server_fd,
2126 opt.server_addr, opt.server_port,
2127 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2128 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002130 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
2131 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002132 goto exit;
2133 }
2134
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002135 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002136 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002137 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002138 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002139 if( ret != 0 )
2140 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002141 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
2142 -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002143 goto exit;
2144 }
2145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002147
2148 /*
2149 * 3. Setup stuff
2150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 fflush( stdout );
2153
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02002154 if( ( ret = mbedtls_ssl_config_defaults( &conf,
2155 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02002156 opt.transport,
2157 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002158 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002159 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
2160 -ret );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002161 goto exit;
2162 }
2163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02002165 /* The default algorithms profile disables SHA-1, but our tests still
2166 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02002167 if( opt.allow_sha1 > 0 )
2168 {
2169 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
2170 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
2171 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
2172 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02002173
Hanno Beckerbb425db2019-04-03 12:59:58 +01002174 if( opt.context_crt_cb == 0 )
2175 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
2176
Hanno Beckera1051b42019-02-26 11:38:29 +00002177 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Gilles Peskineef86ab22017-05-05 18:59:02 +02002178#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00002179
Hanno Beckera0e20d02019-05-15 14:03:01 +01002180#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002181 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckerad4a1372019-05-03 13:06:44 +01002182 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002183 if( opt.cid_enabled == 1 &&
2184 opt.cid_enabled_renego == 1 &&
2185 cid_len != cid_renego_len )
2186 {
2187 mbedtls_printf( "CID length must not change during renegotiation\n" );
2188 goto usage;
2189 }
2190
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002191 if( opt.cid_enabled == 1 )
Hanno Becker8367ccc2019-05-14 11:30:10 +01002192 ret = mbedtls_ssl_conf_cid( &conf, cid_len,
2193 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002194 else
Hanno Becker8367ccc2019-05-14 11:30:10 +01002195 ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
2196 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002197
Hanno Beckerad4a1372019-05-03 13:06:44 +01002198 if( ret != 0 )
2199 {
Hanno Beckerd5eed422019-05-23 16:58:22 +01002200 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
2201 -ret );
Hanno Beckerad4a1372019-05-03 13:06:44 +01002202 goto exit;
2203 }
2204 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002205#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerad4a1372019-05-03 13:06:44 +01002206
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01002207 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002208 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00002209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002211 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Hanno Becker16970d22017-10-10 15:56:37 +01002212 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min,
2213 opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002214
Hanno Becker4d615912018-08-14 13:33:30 +01002215 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01002216 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002220 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002221 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002222 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n",
2223 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002224 goto exit;
2225 }
Paul Bakker05decb22013-08-15 13:33:48 +02002226#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02002227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01002229 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002230 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02002231#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02002232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002234 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002235 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002236#endif
2237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002239 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002240 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002241#endif
2242
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002243#if defined(MBEDTLS_SSL_EXPORT_KEYS)
2244 if( opt.eap_tls != 0 )
Hanno Becker48f3a3d2019-08-29 06:31:22 +01002245 {
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002246 mbedtls_ssl_conf_export_keys_ext_cb( &conf, eap_tls_key_derivation,
2247 &eap_tls_keying );
Hanno Becker48f3a3d2019-08-29 06:31:22 +01002248 }
2249 else if( opt.nss_keylog != 0 )
2250 {
2251 mbedtls_ssl_conf_export_keys_ext_cb( &conf,
2252 nss_keylog_export,
2253 NULL );
2254 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002255#endif
2256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01002258 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002259 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Hanno Becker16970d22017-10-10 15:56:37 +01002260 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
2261 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01002262#endif
2263
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02002264#if defined(MBEDTLS_DHM_C)
2265 if( opt.dhmlen != DFL_DHMLEN )
2266 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
2267#endif
2268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002270 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002271 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002272 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002273 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
2274 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002275 goto exit;
2276 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002277#endif
2278
Philippe Antoine986b6f22019-06-07 15:04:32 +02002279 if (opt.reproducible)
2280 {
Philippe Antoinef91b3722019-06-11 16:02:43 +02002281#if defined(MBEDTLS_HAVE_TIME)
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002282#if defined(MBEDTLS_PLATFORM_TIME_ALT)
2283 mbedtls_platform_set_time( dummy_constant_time );
2284#else
Philippe Antoine7c9d7242019-06-11 12:11:36 +02002285 fprintf( stderr, "Warning: reproducible option used without constant time\n" );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002286#endif
Philippe Antoine0ff84fb2019-06-11 12:15:17 +02002287#endif
Philippe Antoine986b6f22019-06-07 15:04:32 +02002288 }
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002289 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
2290 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002291
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002292 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00002293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002295 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02002296#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002297
Paul Bakker645ce3a2012-10-31 12:32:41 +00002298 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002299 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002300
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002301#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002302 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002303 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002304#endif
Paul Bakker51936882011-02-20 16:05:58 +00002305
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002306 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002307 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002309 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002310#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002313 if( strcmp( opt.ca_path, "none" ) != 0 &&
2314 strcmp( opt.ca_file, "none" ) != 0 )
2315 {
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02002316#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2317 if( opt.ca_callback != 0 )
Hanno Beckercbb59032019-03-28 14:14:22 +00002318 mbedtls_ssl_conf_ca_cb( &conf, ca_callback, &cacert );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02002319 else
2320#endif
2321 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002322 }
2323 if( strcmp( opt.crt_file, "none" ) != 0 &&
2324 strcmp( opt.key_file, "none" ) != 0 )
2325 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002326 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002327 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002328 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
2329 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002330 goto exit;
2331 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002332 }
Paul Bakkered27a042013-04-18 22:46:23 +02002333#endif
2334
Hanno Beckere6706e62017-05-15 16:05:15 +01002335#if defined(MBEDTLS_ECP_C)
2336 if( opt.curves != NULL &&
2337 strcmp( opt.curves, "default" ) != 0 )
2338 {
2339 mbedtls_ssl_conf_curves( &conf, curve_list );
2340 }
2341#endif
2342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckere86964c2018-10-23 11:37:50 +01002344#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002345 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01002346 {
Janos Follathbe4efc22019-08-08 11:38:18 +01002347 key_attributes = psa_key_attributes_init();
2348 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
2349 psa_set_key_algorithm( &key_attributes, alg );
2350 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
Hanno Beckere86964c2018-10-23 11:37:50 +01002351
Janos Follathbe4efc22019-08-08 11:38:18 +01002352 status = psa_import_key( &key_attributes, psk, psk_len, &slot );
Hanno Beckere86964c2018-10-23 11:37:50 +01002353 if( status != PSA_SUCCESS )
2354 {
2355 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2356 goto exit;
2357 }
2358
2359 if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, slot,
2360 (const unsigned char *) opt.psk_identity,
Hanno Becker5cd607b2018-11-05 12:52:42 +00002361 strlen( opt.psk_identity ) ) ) != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01002362 {
2363 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
2364 ret );
2365 goto exit;
2366 }
2367 }
2368 else
2369#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002370 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002371 (const unsigned char *) opt.psk_identity,
2372 strlen( opt.psk_identity ) ) ) != 0 )
2373 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002374 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n",
2375 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002376 goto exit;
2377 }
Hanno Beckere86964c2018-10-23 11:37:50 +01002378#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002379
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002380 if( opt.min_version != DFL_MIN_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01002381 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
2382 opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01002383
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002384 if( opt.max_version != DFL_MAX_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01002385 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
2386 opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00002387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02002389 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002390 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02002391#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002392
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002393 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
2394 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002395 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
2396 -ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002397 goto exit;
2398 }
2399
2400#if defined(MBEDTLS_X509_CRT_PARSE_C)
2401 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
2402 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002403 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
2404 ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002405 goto exit;
2406 }
2407#endif
2408
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002409#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2410 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
2411 {
2412 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
2413 (const unsigned char *) opt.ecjpake_pw,
2414 strlen( opt.ecjpake_pw ) ) ) != 0 )
2415 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002416 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
2417 ret );
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002418 goto exit;
2419 }
2420 }
2421#endif
2422
Hanno Beckerbb425db2019-04-03 12:59:58 +01002423#if defined(MBEDTLS_X509_CRT_PARSE_C)
2424 if( opt.context_crt_cb == 1 )
2425 mbedtls_ssl_set_verify( &ssl, my_verify, NULL );
2426#endif /* MBEDTLS_X509_CRT_PARSE_C */
2427
Hanno Becker8b1af2f2019-07-03 17:02:43 +01002428 io_ctx.ssl = &ssl;
2429 io_ctx.net = &server_fd;
2430 mbedtls_ssl_set_bio( &ssl, &io_ctx, send_cb, recv_cb,
2431 opt.nbio == 0 ? recv_timeout_cb : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002432
Hanno Beckera0e20d02019-05-15 14:03:01 +01002433#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +01002434 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2435 {
2436 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
2437 cid, cid_len ) ) != 0 )
2438 {
2439 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2440 ret );
2441 goto exit;
2442 }
2443 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002444#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01002445
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002446#if defined(MBEDTLS_SSL_PROTO_DTLS)
2447 if( opt.dtls_mtu != DFL_DTLS_MTU )
2448 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
2449#endif
2450
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002451#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002452 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
2453 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002454#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002455
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002456#if defined(MBEDTLS_ECP_RESTARTABLE)
2457 if( opt.ec_max_ops != DFL_EC_MAX_OPS )
2458 mbedtls_ecp_set_max_ops( opt.ec_max_ops );
2459#endif
2460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002462
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 /*
2464 * 4. Handshake
2465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 fflush( stdout );
2468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 {
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002471 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2472 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002473 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Paul Bakker40e46942009-01-03 21:51:57 +00002474 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002475 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
2476 -ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2478 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01002479 " Unable to verify the server's certificate. "
2480 "Either it is invalid,\n"
2481 " or you didn't set ca_file or ca_path "
2482 "to an appropriate value.\n"
2483 " Alternatively, you may want to use "
2484 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00002487 }
Hanno Becker16970d22017-10-10 15:56:37 +01002488
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002489#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002490 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2491 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002492#endif
2493
Hanno Becker16970d22017-10-10 15:56:37 +01002494 /* For event-driven IO, wait for socket to become available */
2495 if( opt.event == 1 /* level triggered IO */ )
2496 {
2497#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002498 ret = idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002499#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002500 ret = idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002501#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002502 if( ret != 0 )
2503 goto exit;
Hanno Becker16970d22017-10-10 15:56:37 +01002504 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 }
2506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Hanno Becker16970d22017-10-10 15:56:37 +01002508 mbedtls_ssl_get_version( &ssl ),
2509 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
2512 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002513 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002515
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002516#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2517 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
2518 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
2519#endif
2520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002522 if( opt.alpn_string != NULL )
2523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
2525 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002526 alp ? alp : "(none)" );
2527 }
2528#endif
2529
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002530#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Ron Eldor51d3ab52019-05-12 14:54:30 +03002531 if( opt.eap_tls != 0 )
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002532 {
2533 size_t j = 0;
Ron Eldor51d3ab52019-05-12 14:54:30 +03002534
2535 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type,
2536 eap_tls_keying.master_secret,
2537 sizeof( eap_tls_keying.master_secret ),
2538 eap_tls_label,
2539 eap_tls_keying.randbytes,
2540 sizeof( eap_tls_keying.randbytes ),
2541 eap_tls_keymaterial,
2542 sizeof( eap_tls_keymaterial ) ) )
2543 != 0 )
2544 {
2545 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2546 -ret );
2547 goto exit;
2548 }
2549
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002550 mbedtls_printf( " EAP-TLS key material is:" );
2551 for( j = 0; j < sizeof( eap_tls_keymaterial ); j++ )
2552 {
2553 if( j % 8 == 0 )
2554 mbedtls_printf("\n ");
2555 mbedtls_printf("%02x ", eap_tls_keymaterial[j] );
2556 }
2557 mbedtls_printf("\n");
2558
Ron Eldor51d3ab52019-05-12 14:54:30 +03002559 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type, NULL, 0,
Ron Eldor51c45072019-05-15 17:49:54 +03002560 eap_tls_label,
2561 eap_tls_keying.randbytes,
2562 sizeof( eap_tls_keying.randbytes ),
2563 eap_tls_iv,
2564 sizeof( eap_tls_iv ) ) ) != 0 )
Ron Eldor51d3ab52019-05-12 14:54:30 +03002565 {
2566 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2567 -ret );
2568 goto exit;
2569 }
2570
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002571 mbedtls_printf( " EAP-TLS IV is:" );
2572 for( j = 0; j < sizeof( eap_tls_iv ); j++ )
2573 {
2574 if( j % 8 == 0 )
2575 mbedtls_printf("\n ");
2576 mbedtls_printf("%02x ", eap_tls_iv[j] );
2577 }
2578 mbedtls_printf("\n");
2579 }
2580#endif
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002581 if( opt.reconnect != 0 )
2582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002584 fflush( stdout );
2585
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +02002586 if( opt.reco_mode == 1 )
Manuel Pégourié-Gonnard21548632019-05-16 11:39:42 +02002587 {
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002588 /* free any previously saved data */
Manuel Pégourié-Gonnard4d591d62019-05-24 10:26:41 +02002589 if( session_data != NULL )
2590 {
2591 mbedtls_platform_zeroize( session_data, session_data_len );
2592 mbedtls_free( session_data );
2593 session_data = NULL;
2594 }
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002595
2596 /* get size of the buffer needed */
2597 mbedtls_ssl_session_save( mbedtls_ssl_get_session_pointer( &ssl ),
2598 NULL, 0, &session_data_len );
2599 session_data = mbedtls_calloc( 1, session_data_len );
2600 if( session_data == NULL )
2601 {
2602 mbedtls_printf( " failed\n ! alloc %u bytes for session data\n",
2603 (unsigned) session_data_len );
2604 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
2605 goto exit;
2606 }
2607
2608 /* actually save session data */
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +02002609 if( ( ret = mbedtls_ssl_session_save( mbedtls_ssl_get_session_pointer( &ssl ),
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002610 session_data, session_data_len,
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +02002611 &session_data_len ) ) != 0 )
2612 {
2613 mbedtls_printf( " failed\n ! mbedtls_ssl_session_saved returned -0x%04x\n\n",
2614 -ret );
2615 goto exit;
2616 }
2617 }
2618 else
2619 {
2620 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
2621 {
2622 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
2623 -ret );
2624 goto exit;
2625 }
Manuel Pégourié-Gonnard21548632019-05-16 11:39:42 +02002626 }
2627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002629
2630 if( opt.reco_mode == 1 )
2631 {
2632 mbedtls_printf( " [ Saved %u bytes of session data]\n",
2633 (unsigned) session_data_len );
2634 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002635 }
2636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 /*
2639 * 5. Verify the server certificate
2640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002642
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002643 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002644 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002645 char vrfy_buf[512];
2646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
Hanno Becker16970d22017-10-10 15:56:37 +01002649 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ),
2650 " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00002651
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002652 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 }
2654 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002656
Hanno Beckera9766c2c2019-02-25 17:43:18 +00002657 mbedtls_printf( " . Peer certificate information ...\n" );
2658 mbedtls_printf( "%s\n", peer_crt_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Hanno Beckera0e20d02019-05-15 14:03:01 +01002661#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002662 ret = report_cid_usage( &ssl, "initial handshake" );
2663 if( ret != 0 )
2664 goto exit;
2665
Hanno Becker90cb3592019-04-09 17:24:19 +01002666 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2667 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002668 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
2669 cid_renego,
2670 cid_renego_len ) ) != 0 )
Hanno Becker90cb3592019-04-09 17:24:19 +01002671 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002672 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2673 ret );
2674 return( ret );
Hanno Becker90cb3592019-04-09 17:24:19 +01002675 }
2676 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002677#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01002678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002680 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002681 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002682 /*
2683 * Perform renegotiation (this must be done when the server is waiting
2684 * for input from our side).
2685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002687 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002689 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002690 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002691 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002692 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002693 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002694 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
2695 ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002696 goto exit;
2697 }
Hanno Becker16970d22017-10-10 15:56:37 +01002698
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002699#if defined(MBEDTLS_ECP_RESTARTABLE)
2700 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2701 continue;
2702#endif
2703
Hanno Becker16970d22017-10-10 15:56:37 +01002704 /* For event-driven IO, wait for socket to become available */
2705 if( opt.event == 1 /* level triggered IO */ )
2706 {
2707#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002708 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002709#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002710 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002711#endif
2712 }
2713
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002714 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002716 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002718
Hanno Beckera0e20d02019-05-15 14:03:01 +01002719#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002720 ret = report_cid_usage( &ssl, "after renegotiation" );
2721 if( ret != 0 )
2722 goto exit;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002723#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002724
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 /*
2726 * 6. Write the GET request
2727 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002728 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002729send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002731 fflush( stdout );
2732
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002733 len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST,
2734 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002735 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002736
2737 /* Add padding to GET request to reach opt.request_size in length */
2738 if( opt.request_size != DFL_REQUEST_SIZE &&
2739 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02002740 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002741 memset( buf + len, 'A', opt.request_size - len - tail_len );
2742 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02002743 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002745 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002746 len += tail_len;
2747
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02002748 /* Truncate if request size is smaller than the "natural" size */
2749 if( opt.request_size != DFL_REQUEST_SIZE &&
2750 len > opt.request_size )
2751 {
2752 len = opt.request_size;
2753
2754 /* Still end with \r\n unless that's really not possible */
2755 if( len >= 2 ) buf[len - 2] = '\r';
2756 if( len >= 1 ) buf[len - 1] = '\n';
2757 }
2758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 {
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002761 written = 0;
2762 frags = 0;
2763
2764 do
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 {
Hanno Becker16970d22017-10-10 15:56:37 +01002766 while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002767 len - written ) ) < 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002768 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002769 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002770 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002771 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002772 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002773 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
2774 -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002775 goto exit;
2776 }
Hanno Becker16970d22017-10-10 15:56:37 +01002777
2778 /* For event-driven IO, wait for socket to become available */
2779 if( opt.event == 1 /* level triggered IO */ )
2780 {
2781#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002782 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002783#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002784 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002785#endif
2786 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002787 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002788
2789 frags++;
2790 written += ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002792 while( written < len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002793 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002794 else /* Not stream, so datagram */
2795 {
Hanno Becker16970d22017-10-10 15:56:37 +01002796 while( 1 )
2797 {
2798 ret = mbedtls_ssl_write( &ssl, buf, len );
2799
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002800#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002801 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002802 continue;
2803#endif
2804
Hanno Becker16970d22017-10-10 15:56:37 +01002805 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2806 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2807 break;
2808
2809 /* For event-driven IO, wait for socket to become available */
2810 if( opt.event == 1 /* level triggered IO */ )
2811 {
2812#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002813 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002814#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002815 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002816#endif
2817 }
2818 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002819
2820 if( ret < 0 )
2821 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002822 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n",
2823 ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002824 goto exit;
2825 }
2826
2827 frags = 1;
2828 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002829
2830 if( written < len )
2831 {
2832 mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
2833 "was truncated to size %u", (unsigned) written );
2834 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002835 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002837 buf[written] = '\0';
Hanno Becker16970d22017-10-10 15:56:37 +01002838 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
2839 written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002840
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002841 /* Send a non-empty request if request_size == 0 */
2842 if ( len == 0 )
2843 {
2844 opt.request_size = DFL_REQUEST_SIZE;
2845 goto send_request;
2846 }
2847
Paul Bakker5121ce52009-01-03 21:22:43 +00002848 /*
2849 * 7. Read the HTTP response
2850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002852 fflush( stdout );
2853
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002854 /*
2855 * TLS and DTLS need different reading styles (stream vs datagram)
2856 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002858 {
2859 do
2860 {
2861 len = sizeof( buf ) - 1;
2862 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002864
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002865#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002866 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002867 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002868#endif
2869
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002870 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
2871 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Hanno Becker16970d22017-10-10 15:56:37 +01002872 {
2873 /* For event-driven IO, wait for socket to become available */
2874 if( opt.event == 1 /* level triggered IO */ )
2875 {
2876#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002877 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002878#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002879 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002880#endif
2881 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002882 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01002883 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002884
2885 if( ret <= 0 )
2886 {
2887 switch( ret )
2888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2890 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002891 ret = 0;
2892 goto close_notify;
2893
2894 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895 case MBEDTLS_ERR_NET_CONN_RESET:
2896 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002897 ret = 0;
2898 goto reconnect;
2899
2900 default:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002901 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n",
2902 -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002903 goto exit;
2904 }
2905 }
2906
2907 len = ret;
2908 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002910
2911 /* End of message should be detected according to the syntax of the
2912 * application protocol (eg HTTP), just use a dummy test here. */
2913 if( ret > 0 && buf[len-1] == '\n' )
2914 {
2915 ret = 0;
2916 break;
2917 }
2918 }
2919 while( 1 );
2920 }
2921 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00002922 {
2923 len = sizeof( buf ) - 1;
2924 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002925
Hanno Becker16970d22017-10-10 15:56:37 +01002926 while( 1 )
2927 {
2928 ret = mbedtls_ssl_read( &ssl, buf, len );
2929
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002930#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002931 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002932 continue;
2933#endif
2934
Hanno Becker16970d22017-10-10 15:56:37 +01002935 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2936 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2937 break;
2938
2939 /* For event-driven IO, wait for socket to become available */
2940 if( opt.event == 1 /* level triggered IO */ )
2941 {
2942#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002943 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002944#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002945 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002946#endif
2947 }
2948 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002949
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002950 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002951 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002952 switch( ret )
2953 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002954 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002956 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02002957 goto send_request;
2958 goto exit;
2959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2961 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002962 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002963 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00002964
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002965 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002967 goto exit;
2968 }
Paul Bakker5690efc2011-05-26 13:16:06 +00002969 }
2970
Paul Bakker5121ce52009-01-03 21:22:43 +00002971 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02002972 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002974 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002975 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002976
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002977 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002978 * 7b. Simulate hard reset and reconnect from same port?
2979 */
2980 if( opt.reconnect_hard != 0 )
2981 {
2982 opt.reconnect_hard = 0;
2983
2984 mbedtls_printf( " . Restarting connection from same port..." );
2985 fflush( stdout );
2986
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002987#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker23699ef2019-02-26 12:36:53 +00002988 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002989#endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Becker23699ef2019-02-26 12:36:53 +00002990
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002991 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
2992 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002993 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2994 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002995 goto exit;
2996 }
2997
2998 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
2999 {
3000 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02003001 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003002 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02003003 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003004 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
3005 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02003006 goto exit;
3007 }
Hanno Becker16970d22017-10-10 15:56:37 +01003008
3009 /* For event-driven IO, wait for socket to become available */
3010 if( opt.event == 1 /* level triggered IO */ )
3011 {
3012#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003013 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003014#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003015 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003016#endif
3017 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02003018 }
3019
3020 mbedtls_printf( " ok\n" );
3021
3022 goto send_request;
3023 }
3024
3025 /*
Jarno Lamsa77e66652019-05-29 15:15:08 +03003026 * 7c. Simulate serialize/deserialize and go back to data exchange
3027 */
Jarno Lamsabbc7b412019-06-04 11:06:31 +03003028#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Jarno Lamsa304d61c2019-06-06 10:40:52 +03003029 if( opt.serialize != 0 )
Jarno Lamsa77e66652019-05-29 15:15:08 +03003030 {
Jarno Lamsa15b3a7a2019-06-06 15:10:07 +03003031 size_t buf_len;
Jarno Lamsa77e66652019-05-29 15:15:08 +03003032
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003033 mbedtls_printf( " . Serializing live connection..." );
Jarno Lamsa77e66652019-05-29 15:15:08 +03003034
Jarno Lamsa15b3a7a2019-06-06 15:10:07 +03003035 ret = mbedtls_ssl_context_save( &ssl, NULL, 0, &buf_len );
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003036 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jarno Lamsa77e66652019-05-29 15:15:08 +03003037 {
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03003038 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
3039 "-0x%x\n\n", -ret );
Jarno Lamsa77e66652019-05-29 15:15:08 +03003040
3041 goto exit;
3042 }
3043
Jarno Lamsa15b3a7a2019-06-06 15:10:07 +03003044 if( ( context_buf = mbedtls_calloc( 1, buf_len ) ) == NULL )
Jarno Lamsa77e66652019-05-29 15:15:08 +03003045 {
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03003046 mbedtls_printf( " failed\n ! Couldn't allocate buffer for "
3047 "serialized context" );
Jarno Lamsa77e66652019-05-29 15:15:08 +03003048
3049 goto exit;
3050 }
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02003051 context_buf_len = buf_len;
Jarno Lamsa77e66652019-05-29 15:15:08 +03003052
Jarno Lamsaddf72a12019-06-13 12:22:50 +03003053 if( ( ret = mbedtls_ssl_context_save( &ssl, context_buf,
3054 buf_len, &buf_len ) ) != 0 )
Jarno Lamsa77e66652019-05-29 15:15:08 +03003055 {
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003056 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03003057 "-0x%x\n\n", -ret );
Jarno Lamsa77e66652019-05-29 15:15:08 +03003058
3059 goto exit;
3060 }
3061
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003062 mbedtls_printf( " ok\n" );
3063
3064 if( opt.serialize == 1 )
3065 {
Manuel Pégourié-Gonnard9df5a822019-07-23 14:51:09 +02003066 /* nothing to do here, done by context_save() already */
3067 mbedtls_printf( " . Context has been reset... ok" );
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003068 }
3069
Jarno Lamsa304d61c2019-06-06 10:40:52 +03003070 if( opt.serialize == 2 )
3071 {
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003072 mbedtls_printf( " . Freeing and reinitializing context..." );
3073
Jarno Lamsa304d61c2019-06-06 10:40:52 +03003074 mbedtls_ssl_free( &ssl );
3075
3076 mbedtls_ssl_init( &ssl );
3077
3078 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
3079 {
Jarno Lamsaddf72a12019-06-13 12:22:50 +03003080 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned "
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003081 "-0x%x\n\n", -ret );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03003082 goto exit;
3083 }
3084
3085 if( opt.nbio == 2 )
Jarno Lamsaddf72a12019-06-13 12:22:50 +03003086 mbedtls_ssl_set_bio( &ssl, &server_fd, delayed_send,
3087 delayed_recv, NULL );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03003088 else
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003089 mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send,
3090 mbedtls_net_recv,
Jarno Lamsaddf72a12019-06-13 12:22:50 +03003091 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03003092
Jarno Lamsa8e253212019-06-13 11:45:06 +03003093#if defined(MBEDTLS_TIMING_C)
Jarno Lamsaddf72a12019-06-13 12:22:50 +03003094 mbedtls_ssl_set_timer_cb( &ssl, &timer,
3095 mbedtls_timing_set_delay,
3096 mbedtls_timing_get_delay );
Jarno Lamsa8e253212019-06-13 11:45:06 +03003097#endif /* MBEDTLS_TIMING_C */
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003098
3099 mbedtls_printf( " ok\n" );
Jarno Lamsa304d61c2019-06-06 10:40:52 +03003100 }
3101
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003102 mbedtls_printf( " . Deserializing connection..." );
Jarno Lamsa77e66652019-05-29 15:15:08 +03003103
Jarno Lamsaddf72a12019-06-13 12:22:50 +03003104 if( ( ret = mbedtls_ssl_context_load( &ssl, context_buf,
3105 buf_len ) ) != 0 )
Jarno Lamsa77e66652019-05-29 15:15:08 +03003106 {
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03003107 mbedtls_printf( "failed\n ! mbedtls_ssl_context_load returned "
3108 "-0x%x\n\n", -ret );
Jarno Lamsa77e66652019-05-29 15:15:08 +03003109
3110 goto exit;
3111 }
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003112
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02003113 mbedtls_free( context_buf );
3114 context_buf = NULL;
3115 context_buf_len = 0;
3116
Manuel Pégourié-Gonnarda88399c2019-07-12 10:41:55 +02003117 mbedtls_printf( " ok\n" );
Jarno Lamsa77e66652019-05-29 15:15:08 +03003118 }
Jarno Lamsa93c6ff22019-06-04 15:36:18 +03003119#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jarno Lamsa77e66652019-05-29 15:15:08 +03003120
3121 /*
3122 * 7d. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003123 */
3124 if( --opt.exchanges > 0 )
3125 goto send_request;
3126
3127 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02003128 * 8. Done, cleanly close the connection
3129 */
3130close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02003132 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02003133
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003134 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003136 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003137 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02003138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02003140
3141 /*
3142 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003143 */
3144reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02003145 if( opt.reconnect != 0 )
3146 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02003147 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02003148
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003149 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01003150
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003151#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01003152 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02003153 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003154#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02003157
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00003158#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckera1051b42019-02-26 11:38:29 +00003159 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00003160#endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Beckera1051b42019-02-26 11:38:29 +00003161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02003163 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003164 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
3165 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02003166 goto exit;
3167 }
3168
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +02003169 if( opt.reco_mode == 1 )
Manuel Pégourié-Gonnard21548632019-05-16 11:39:42 +02003170 {
Manuel Pégourié-Gonnarda7c37652019-05-20 12:46:26 +02003171 if( ( ret = mbedtls_ssl_session_load( &saved_session,
3172 session_data,
3173 session_data_len ) ) != 0 )
3174 {
3175 mbedtls_printf( " failed\n ! mbedtls_ssl_session_load returned -0x%x\n\n",
3176 -ret );
3177 goto exit;
3178 }
Manuel Pégourié-Gonnard21548632019-05-16 11:39:42 +02003179 }
3180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003182 {
Manuel Pégourié-Gonnard21548632019-05-16 11:39:42 +02003183 mbedtls_printf( " failed\n ! mbedtls_ssl_set_session returned -0x%x\n\n",
3184 -ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003185 goto exit;
3186 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02003187
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003188 if( ( ret = mbedtls_net_connect( &server_fd,
3189 opt.server_addr, opt.server_port,
3190 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
3191 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02003192 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003193 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
3194 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02003195 goto exit;
3196 }
3197
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02003198 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003199 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02003200 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003201 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02003202 if( ret != 0 )
3203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003205 -ret );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02003206 goto exit;
3207 }
3208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02003210 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003211 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02003212 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003213 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02003214 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003215 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
3216 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02003217 goto exit;
3218 }
3219 }
3220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02003222
3223 goto send_request;
3224 }
3225
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003226 /*
3227 * Cleanup and exit
3228 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003229exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00003231 if( ret != 0 )
3232 {
3233 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234 mbedtls_strerror( ret, error_buf, 100 );
3235 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00003236 }
3237#endif
3238
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003239 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241#if defined(MBEDTLS_X509_CRT_PARSE_C)
3242 mbedtls_x509_crt_free( &clicert );
3243 mbedtls_x509_crt_free( &cacert );
3244 mbedtls_pk_free( &pkey );
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01003245#if defined(MBEDTLS_USE_PSA_CRYPTO)
3246 psa_destroy_key( key_slot );
3247#endif
Paul Bakkered27a042013-04-18 22:46:23 +02003248#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249 mbedtls_ssl_session_free( &saved_session );
3250 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003251 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252 mbedtls_ctr_drbg_free( &ctr_drbg );
3253 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnard4d591d62019-05-24 10:26:41 +02003254 if( session_data != NULL )
3255 mbedtls_platform_zeroize( session_data, session_data_len );
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003256 mbedtls_free( session_data );
Manuel Pégourié-Gonnard3309a672019-07-15 10:31:11 +02003257#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
3258 if( context_buf != NULL )
3259 mbedtls_platform_zeroize( context_buf, context_buf_len );
3260 mbedtls_free( context_buf );
3261#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003262
Hanno Becker3f24ea92018-11-05 13:25:17 +00003263#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
3264 defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00003265 if( opt.psk_opaque != 0 )
Hanno Becker3f24ea92018-11-05 13:25:17 +00003266 {
3267 /* This is ok even if the slot hasn't been
3268 * initialized (we might have jumed here
3269 * immediately because of bad cmd line params,
3270 * for example). */
Hanno Becker1d911cd2018-11-15 13:06:09 +00003271 status = psa_destroy_key( slot );
Hanno Becker3f24ea92018-11-05 13:25:17 +00003272 if( status != PSA_SUCCESS )
3273 {
3274 mbedtls_printf( "Failed to destroy key slot %u - error was %d",
Hanno Becker1d911cd2018-11-15 13:06:09 +00003275 (unsigned) slot, (int) status );
Hanno Becker3f24ea92018-11-05 13:25:17 +00003276 if( ret == 0 )
3277 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
3278 }
3279 }
3280#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED &&
3281 MBEDTLS_USE_PSA_CRYPTO */
3282
Paul Bakkercce9d772011-11-18 14:26:47 +00003283#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003285 fflush( stdout ); getchar();
3286#endif
3287
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02003288 // Shell can not handle large exit numbers -> 1 for errors
3289 if( ret < 0 )
3290 ret = 1;
3291
Paul Bakker5121ce52009-01-03 21:22:43 +00003292 return( ret );
3293}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
3295 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003296 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */