blob: 515a42d3ea783f949992d951faa05528bca2b9f7 [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
Hanno Becker90cb3592019-04-09 17:24:19 +0100116#define DFL_CID_ENABLED 0
117#define DFL_CID_VALUE ""
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100118#define DFL_CID_ENABLED_RENEGO -1
119#define DFL_CID_VALUE_RENEGO NULL
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200120#define DFL_RECONNECT_HARD 0
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200122#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100123#define DFL_CURVES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200125#define DFL_HS_TO_MIN 0
126#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200127#define DFL_DTLS_MTU -1
Hanno Becker4d615912018-08-14 13:33:30 +0100128#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200129#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200130#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100131#define DFL_ETM -1
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200132#define DFL_CA_CALLBACK 0
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300133#define DFL_EAP_TLS 0
Paul Bakker0e6975b2009-02-10 22:19:10 +0000134
Paul Bakker93c32b22014-04-25 13:40:05 +0200135#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
136#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_X509_CRT_PARSE_C)
Janos Follathae13beb2019-04-05 14:06:58 +0100139#define USAGE_CONTEXT_CRT_CB \
Hanno Beckerbb425db2019-04-03 12:59:58 +0100140 " context_crt_cb=%%d This determines whether the CRT verification callback is bound\n" \
141 " to the SSL configuration of the SSL context.\n" \
142 " Possible values:\n"\
143 " - 0 (default): Use CRT callback bound to configuration\n" \
144 " - 1: Use CRT callback bound to SSL context\n"
145#else
Janos Follathae13beb2019-04-05 14:06:58 +0100146#define USAGE_CONTEXT_CRT_CB ""
Hanno Beckerbb425db2019-04-03 12:59:58 +0100147#endif /* MBEDTLS_X509_CRT_PARSE_C */
148#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000150#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100151 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
152 " default: \"\" (pre-loaded)\n" \
Hanno Becker422d1992019-05-01 11:16:28 +0100153 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100154 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
155 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
Hanno Becker422d1992019-05-01 11:16:28 +0100156 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100157 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
158 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000159 " key_file=%%s default: \"\" (pre-loaded)\n"
160#else
161#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 " No file operations available (MBEDTLS_FS_IO not defined)\n"
163#endif /* MBEDTLS_FS_IO */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100164#else /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200165#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100167#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
168#define USAGE_KEY_OPAQUE \
169 " key_opaque=%%d Handle your private key as if it were opaque\n" \
170 " default: 0 (disabled)\n"
171#else
172#define USAGE_KEY_OPAQUE ""
173#endif
Paul Bakkered27a042013-04-18 22:46:23 +0200174
Hanno Beckera0e20d02019-05-15 14:03:01 +0100175#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +0100176#define USAGE_CID \
177 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
178 " default: 0 (disabled)\n" \
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100179 " 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 +0100180 " default: same as 'cid' parameter\n" \
Hanno Becker90cb3592019-04-09 17:24:19 +0100181 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100182 " default: \"\"\n" \
183 " 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 +0100184 " default: same as 'cid_val' parameter\n"
Hanno Beckera0e20d02019-05-15 14:03:01 +0100185#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +0100186#define USAGE_CID ""
Hanno Beckera0e20d02019-05-15 14:03:01 +0100187#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +0100188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckere86964c2018-10-23 11:37:50 +0100190#define USAGE_PSK_RAW \
Paul Bakkered27a042013-04-18 22:46:23 +0200191 " psk=%%s default: \"\" (in hex, without 0x)\n" \
192 " psk_identity=%%s default: \"Client_identity\"\n"
Hanno Beckere86964c2018-10-23 11:37:50 +0100193#if defined(MBEDTLS_USE_PSA_CRYPTO)
194#define USAGE_PSK_SLOT \
Hanno Becker1d911cd2018-11-15 13:06:09 +0000195 " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
196 " Enable this to store the PSK configured through command line\n" \
197 " parameter `psk` in a PSA-based key slot.\n" \
Hanno Beckere86964c2018-10-23 11:37:50 +0100198 " Note: Currently only supported in conjunction with\n" \
199 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
200 " to force a particular PSK-only ciphersuite.\n" \
201 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
202 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
203 " with prepopulated key slots instead of importing raw key material.\n"
204#else
205#define USAGE_PSK_SLOT ""
206#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker1bac87c2019-03-29 12:02:26 +0000207#define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
208#else
209#define USAGE_PSK ""
210#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
211
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200212#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
213#define USAGE_CA_CALLBACK \
214 " ca_callback=%%d default: 0 (disabled)\n" \
215 " Enable this to use the trusted certificate callback function\n"
216#else
217#define USAGE_CA_CALLBACK ""
218#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5690efc2011-05-26 13:16:06 +0000219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200221#define USAGE_TICKETS \
222 " tickets=%%d default: 1 (enabled)\n"
223#else
224#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200226
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300227#if defined(MBEDTLS_SSL_EXPORT_KEYS)
228#define USAGE_EAP_TLS \
229 " eap_tls=%%d default: 0 (disabled)\n"
230#else
231#define USAGE_EAP_TLS ""
232#endif /* MBEDTLS_SSL_EXPORT_KEYS */
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Paul Bakker1f2bc622013-08-15 13:45:55 +0200235#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100236 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200237#else
238#define USAGE_TRUNC_HMAC ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Paul Bakker1f2bc622013-08-15 13:45:55 +0200240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200242#define USAGE_MAX_FRAG_LEN \
243 " max_frag_len=%%d default: 16384 (tls default)\n" \
244 " options: 512, 1024, 2048, 4096\n"
245#else
246#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100250#define USAGE_RECSPLIT \
Manuel Pégourié-Gonnardbf27eaa2015-06-11 17:02:10 +0200251 " recsplit=0/1 default: (library default: on)\n"
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100252#else
253#define USAGE_RECSPLIT
254#endif
255
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200256#if defined(MBEDTLS_DHM_C)
257#define USAGE_DHMLEN \
258 " dhmlen=%%d default: (library default: 1024 bits)\n"
259#else
260#define USAGE_DHMLEN
261#endif
262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200264#define USAGE_ALPN \
265 " alpn=%%s default: \"\" (disabled)\n" \
266 " example: spdy/1,http/1.1\n"
267#else
268#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200270
Hanno Beckere6706e62017-05-15 16:05:15 +0100271#if defined(MBEDTLS_ECP_C)
272#define USAGE_CURVES \
273 " curves=a,b,c,d default: \"default\" (library default)\n" \
274 " example: \"secp521r1,brainpoolP512r1\"\n" \
275 " - use \"none\" for empty list\n" \
276 " - see mbedtls_ecp_curve_list()\n" \
277 " for acceptable curve names\n"
278#else
279#define USAGE_CURVES ""
280#endif
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200283#define USAGE_DTLS \
284 " dtls=%%d default: 0 (TLS)\n" \
285 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200286 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Becker4d615912018-08-14 13:33:30 +0100287 " mtu=%%d default: (library default: unlimited)\n" \
288 " dgram_packing=%%d default: 1 (allowed)\n" \
289 " allow or forbid packing of multiple\n" \
290 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200291#else
292#define USAGE_DTLS ""
293#endif
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200296#define USAGE_FALLBACK \
297 " fallback=0/1 default: (library default: off)\n"
298#else
299#define USAGE_FALLBACK ""
300#endif
301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200303#define USAGE_EMS \
304 " extended_ms=0/1 default: (library default: on)\n"
305#else
306#define USAGE_EMS ""
307#endif
308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100310#define USAGE_ETM \
311 " etm=0/1 default: (library default: on)\n"
312#else
313#define USAGE_ETM ""
314#endif
315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100317#define USAGE_RENEGO \
318 " renegotiation=%%d default: 0 (disabled)\n" \
319 " renegotiate=%%d default: 0 (disabled)\n"
320#else
321#define USAGE_RENEGO ""
322#endif
323
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200324#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
325#define USAGE_ECJPAKE \
326 " ecjpake_pw=%%s default: none (disabled)\n"
327#else
328#define USAGE_ECJPAKE ""
329#endif
330
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200331#if defined(MBEDTLS_ECP_RESTARTABLE)
332#define USAGE_ECRESTART \
333 " ec_max_ops=%%s default: library default (restart disabled)\n"
334#else
335#define USAGE_ECRESTART ""
336#endif
337
Paul Bakker9caf2d22010-02-18 19:37:19 +0000338#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000339 "\n usage: ssl_client2 param=<>...\n" \
340 "\n acceptable parameters:\n" \
341 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100342 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000343 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000344 " request_page=%%s default: \".\"\n" \
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +0100345 " request_size=%%d default: about 34 (basic request)\n" \
346 " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
347 " If 0, in the first exchange only an empty\n" \
348 " application data message is sent followed by\n" \
349 " a second non-empty message before attempting\n" \
350 " to read a response from the server\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100351 " debug_level=%%d default: 0 (disabled)\n" \
352 " nbio=%%d default: 0 (blocking I/O)\n" \
353 " options: 1 (non-blocking), 2 (added delays)\n" \
354 " event=%%d default: 0 (loop)\n" \
355 " options: 1 (level-triggered, implies nbio=1),\n" \
356 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200357 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100358 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200359 USAGE_DTLS \
Hanno Becker90cb3592019-04-09 17:24:19 +0100360 USAGE_CID \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200361 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100362 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100363 " options: none, optional, required\n" \
364 USAGE_IO \
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100365 USAGE_KEY_OPAQUE \
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200366 USAGE_CA_CALLBACK \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100367 "\n" \
368 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200369 USAGE_ECJPAKE \
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200370 USAGE_ECRESTART \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100371 "\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100372 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100373 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200374 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200375 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200376 " reco_delay=%%d default: 0 seconds\n" \
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200377 " reconnect_hard=%%d default: 0 (disabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200378 USAGE_TICKETS \
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300379 USAGE_EAP_TLS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100380 USAGE_MAX_FRAG_LEN \
381 USAGE_TRUNC_HMAC \
Janos Follathae13beb2019-04-05 14:06:58 +0100382 USAGE_CONTEXT_CRT_CB \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200383 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200384 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200385 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100386 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100387 USAGE_CURVES \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100388 USAGE_RECSPLIT \
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200389 USAGE_DHMLEN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000390 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100391 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200392 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200393 " min_version=%%s default: (library default: tls1)\n" \
394 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000395 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100396 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000397 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000398 " force_ciphersuite=<name> default: all enabled\n"\
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100399 " query_config=<name> return 0 if the specified\n" \
400 " configuration macro is defined and 1\n" \
401 " otherwise. The expansion of the macro\n" \
402 " is printed if it is defined\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000403 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000404
Hanno Becker8651a432017-06-09 16:13:22 +0100405#define ALPN_LIST_SIZE 10
406#define CURVE_LIST_SIZE 20
407
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500408#if defined(MBEDTLS_CHECK_PARAMS)
409#include "mbedtls/platform_util.h"
410void mbedtls_param_failed( const char *failure_condition,
411 const char *file,
412 int line )
413{
414 mbedtls_printf( "%s:%i: Input param failed - %s\n",
415 file, line, failure_condition );
416 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
417}
418#endif
419
Rich Evans85b05ec2015-02-12 11:37:29 +0000420/*
421 * global options
422 */
423struct options
424{
425 const char *server_name; /* hostname of the server (client only) */
426 const char *server_addr; /* address of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200427 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000428 int debug_level; /* level of debugging */
429 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100430 int event; /* loop or event-driven IO? level or edge triggered? */
431 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000432 int max_resend; /* DTLS times to resend on read timeout */
Rich Evans85b05ec2015-02-12 11:37:29 +0000433 const char *request_page; /* page on server to request */
434 int request_size; /* pad request with header to requested size */
435 const char *ca_file; /* the file with the CA certificate(s) */
436 const char *ca_path; /* the path with the CA certificate(s) reside */
437 const char *crt_file; /* the file with the client certificate */
438 const char *key_file; /* the file with the client key */
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100439 int key_opaque; /* handle private key as if it were opaque */
Hanno Beckere86964c2018-10-23 11:37:50 +0100440#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000441 int psk_opaque;
Hanno Beckere86964c2018-10-23 11:37:50 +0100442#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200443#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000444 int ca_callback; /* Use callback for trusted certificate list */
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200445#endif
Rich Evans85b05ec2015-02-12 11:37:29 +0000446 const char *psk; /* the pre-shared key */
447 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200448 const char *ecjpake_pw; /* the EC J-PAKE password */
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +0200449 int ec_max_ops; /* EC consecutive operations limit */
Rich Evans85b05ec2015-02-12 11:37:29 +0000450 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
451 int renegotiation; /* enable / disable renegotiation */
452 int allow_legacy; /* allow legacy renegotiation */
453 int renegotiate; /* attempt renegotiation? */
454 int renego_delay; /* delay before enforcing renegotiation */
455 int exchanges; /* number of data exchanges */
456 int min_version; /* minimum protocol version accepted */
457 int max_version; /* maximum protocol version accepted */
458 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200459 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000460 int auth_mode; /* verify mode for connection */
461 unsigned char mfl_code; /* code for maximum fragment length */
462 int trunc_hmac; /* negotiate truncated hmac or not */
463 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +0200464 int dhmlen; /* minimum DHM params len in bits */
Rich Evans85b05ec2015-02-12 11:37:29 +0000465 int reconnect; /* attempt to resume session */
466 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +0200467 int reconnect_hard; /* unexpectedly reconnect from the same port */
Rich Evans85b05ec2015-02-12 11:37:29 +0000468 int tickets; /* enable / disable session tickets */
Hanno Beckere6706e62017-05-15 16:05:15 +0100469 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000470 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000471 int transport; /* TLS or DTLS? */
472 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
473 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200474 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Rich Evans85b05ec2015-02-12 11:37:29 +0000475 int fallback; /* is this a fallback connection? */
Hanno Becker4d615912018-08-14 13:33:30 +0100476 int dgram_packing; /* allow/forbid datagram packing */
Rich Evans85b05ec2015-02-12 11:37:29 +0000477 int extended_ms; /* negotiate extended master secret? */
478 int etm; /* negotiate encrypt then mac? */
Hanno Beckerbb425db2019-04-03 12:59:58 +0100479 int context_crt_cb; /* use context-specific CRT verify callback */
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300480 int eap_tls; /* derive EAP-TLS keying material? */
Hanno Becker90cb3592019-04-09 17:24:19 +0100481 int cid_enabled; /* whether to use the CID extension or not */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100482 int cid_enabled_renego; /* whether to use the CID extension or not
483 * during renegotiation */
Hanno Becker90cb3592019-04-09 17:24:19 +0100484 const char *cid_val; /* the CID to use for incoming messages */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100485 const char *cid_val_renego; /* the CID to use for incoming messages
486 * after renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000487} opt;
488
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100489int query_config( const char *config );
490
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300491#if defined(MBEDTLS_SSL_EXPORT_KEYS)
492typedef struct eap_tls_keys
493{
494 unsigned char master_secret[48];
495 unsigned char randbytes[64];
Ron Eldor51d3ab52019-05-12 14:54:30 +0300496 mbedtls_tls_prf_types tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300497} eap_tls_keys;
498
499static int eap_tls_key_derivation ( void *p_expkey,
500 const unsigned char *ms,
501 const unsigned char *kb,
502 size_t maclen,
503 size_t keylen,
504 size_t ivlen,
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300505 unsigned char client_random[32],
Ron Eldor51d3ab52019-05-12 14:54:30 +0300506 unsigned char server_random[32],
507 mbedtls_tls_prf_types tls_prf_type )
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300508{
509 eap_tls_keys *keys = (eap_tls_keys *)p_expkey;
510
511 ( ( void ) kb );
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300512 memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) );
513 memcpy( keys->randbytes, client_random, 32 );
514 memcpy( keys->randbytes + 32, server_random, 32 );
Ron Eldor51d3ab52019-05-12 14:54:30 +0300515 keys->tls_prf_type = tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300516
Ron Eldorf75e2522019-05-14 20:38:49 +0300517 if( opt.debug_level > 2 )
518 {
Ron Eldor801faf02019-05-15 17:45:24 +0300519 mbedtls_printf("exported maclen is %u\n", (unsigned)maclen);
520 mbedtls_printf("exported keylen is %u\n", (unsigned)keylen);
521 mbedtls_printf("exported ivlen is %u\n", (unsigned)ivlen);
Ron Eldorf75e2522019-05-14 20:38:49 +0300522 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300523 return( 0 );
524}
525#endif
526
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200527static void my_debug( void *ctx, int level,
528 const char *file, int line,
529 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000530{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200531 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000532
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200533 /* Extract basename from file */
534 for( p = basename = file; *p != '\0'; p++ )
535 if( *p == '/' || *p == '\\' )
536 basename = p + 1;
537
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100538 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s",
539 basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000540 fflush( (FILE *) ctx );
541}
542
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200543#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000544int ca_callback( void *data, mbedtls_x509_crt const *child,
Janos Follathd7ecbd62019-04-05 14:52:17 +0100545 mbedtls_x509_crt **candidates )
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200546{
Hanno Beckercbb59032019-03-28 14:14:22 +0000547 int ret = 0;
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200548 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +0000549 mbedtls_x509_crt *first;
550
551 /* This is a test-only implementation of the CA callback
552 * which always returns the entire list of trusted certificates.
553 * Production implementations managing a large number of CAs
554 * should use an efficient presentation and lookup for the
555 * set of trusted certificates (such as a hashtable) and only
556 * return those trusted certificates which satisfy basic
557 * parental checks, such as the matching of child `Issuer`
Jarno Lamsaf7a7f9e2019-04-01 15:11:54 +0300558 * and parent `Subject` field or matching key identifiers. */
Hanno Beckercbb59032019-03-28 14:14:22 +0000559 ((void) child);
560
561 first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
562 if( first == NULL )
563 {
564 ret = -1;
565 goto exit;
566 }
567 mbedtls_x509_crt_init( first );
568
569 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
570 {
571 ret = -1;
572 goto exit;
573 }
574
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200575 while( ca->next != NULL )
576 {
577 ca = ca->next;
Hanno Beckercbb59032019-03-28 14:14:22 +0000578 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
579 {
580 ret = -1;
581 goto exit;
582 }
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200583 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000584
585exit:
586
587 if( ret != 0 )
588 {
589 mbedtls_x509_crt_free( first );
590 mbedtls_free( first );
591 first = NULL;
592 }
593
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200594 *candidates = first;
Hanno Beckercbb59032019-03-28 14:14:22 +0000595 return( ret );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200596}
597#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
598
Rich Evans85b05ec2015-02-12 11:37:29 +0000599/*
600 * Test recv/send functions that make sure each try returns
601 * WANT_READ/WANT_WRITE at least once before sucesseding
602 */
603static int my_recv( void *ctx, unsigned char *buf, size_t len )
604{
605 static int first_try = 1;
606 int ret;
607
608 if( first_try )
609 {
610 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100611 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000612 }
613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100615 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000616 first_try = 1; /* Next call will be a new operation */
617 return( ret );
618}
619
620static int my_send( void *ctx, const unsigned char *buf, size_t len )
621{
622 static int first_try = 1;
623 int ret;
624
625 if( first_try )
626 {
627 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100628 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000629 }
630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100632 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000633 first_try = 1; /* Next call will be a new operation */
634 return( ret );
635}
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckera1051b42019-02-26 11:38:29 +0000638static unsigned char peer_crt_info[1024];
Hanno Beckera9766c2c2019-02-25 17:43:18 +0000639
Rich Evans85b05ec2015-02-12 11:37:29 +0000640/*
641 * Enabled if debug_level > 1 in code below
642 */
Hanno Becker4cb1f4d2017-10-10 15:59:57 +0100643static int my_verify( void *data, mbedtls_x509_crt *crt,
644 int depth, uint32_t *flags )
Rich Evans85b05ec2015-02-12 11:37:29 +0000645{
646 char buf[1024];
647 ((void) data);
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Hanno Beckera9766c2c2019-02-25 17:43:18 +0000650 if( depth == 0 )
651 memcpy( peer_crt_info, buf, sizeof( buf ) );
652
653 if( opt.debug_level == 0 )
654 return( 0 );
655
656 mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_printf( "%s", buf );
Rich Evans85b05ec2015-02-12 11:37:29 +0000658
Rich Evans85b05ec2015-02-12 11:37:29 +0000659 if ( ( *flags ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100661 else
662 {
663 mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
664 mbedtls_printf( "%s\n", buf );
665 }
Rich Evans85b05ec2015-02-12 11:37:29 +0000666
667 return( 0 );
668}
Gilles Peskinecd3c8452017-05-09 14:57:45 +0200669
670static int ssl_sig_hashes_for_test[] = {
671#if defined(MBEDTLS_SHA512_C)
672 MBEDTLS_MD_SHA512,
673 MBEDTLS_MD_SHA384,
674#endif
675#if defined(MBEDTLS_SHA256_C)
676 MBEDTLS_MD_SHA256,
677 MBEDTLS_MD_SHA224,
678#endif
679#if defined(MBEDTLS_SHA1_C)
680 /* Allow SHA-1 as we use it extensively in tests. */
681 MBEDTLS_MD_SHA1,
682#endif
683 MBEDTLS_MD_NONE
684};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#endif /* MBEDTLS_X509_CRT_PARSE_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000686
Hanno Becker16970d22017-10-10 15:56:37 +0100687/*
688 * Wait for an event from the underlying transport or the timer
689 * (Used in event-driven IO mode).
690 */
691#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000692int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000693 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100694#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000695int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000696 mbedtls_timing_delay_context *timer,
697 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +0100698#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +0000699{
Hanno Becker16970d22017-10-10 15:56:37 +0100700
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000701 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +0100702 int poll_type = 0;
703
704 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
705 poll_type = MBEDTLS_NET_POLL_WRITE;
706 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
707 poll_type = MBEDTLS_NET_POLL_READ;
708#if !defined(MBEDTLS_TIMING_C)
709 else
Hanno Beckeref527962018-03-15 15:49:24 +0000710 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100711#endif
712
713 while( 1 )
714 {
Hanno Becker197a91c2017-10-31 10:58:53 +0000715 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +0100716#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +0000717 if( timer != NULL &&
718 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +0100719 {
Hanno Becker16970d22017-10-10 15:56:37 +0100720 break;
721 }
Hanno Becker197a91c2017-10-31 10:58:53 +0000722#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +0100723
Hanno Becker197a91c2017-10-31 10:58:53 +0000724 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000725 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +0100726 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000727 ret = mbedtls_net_poll( fd, poll_type, 0 );
728 if( ret < 0 )
729 return( ret );
730 if( ret == poll_type )
731 break;
Hanno Becker16970d22017-10-10 15:56:37 +0100732 }
733 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +0000734
735 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +0100736}
737
Hanno Becker1f583ee2019-04-09 17:12:56 +0100738/* Unhexify `hex` into `dst`. `dst` must have
739 * size at least `strlen( hex ) / 2`. */
Hanno Becker90cb3592019-04-09 17:24:19 +0100740int unhexify( char const *hex, unsigned char *dst )
Hanno Becker1f583ee2019-04-09 17:12:56 +0100741{
742 unsigned char c;
743 size_t j;
744 size_t len = strlen( hex );
745
746 if( len % 2 != 0 )
747 return( -1 );
748
749 for( j = 0; j < len; j += 2 )
750 {
751 c = hex[j];
752 if( c >= '0' && c <= '9' )
753 c -= '0';
754 else if( c >= 'a' && c <= 'f' )
755 c -= 'a' - 10;
756 else if( c >= 'A' && c <= 'F' )
757 c -= 'A' - 10;
758 else
759 return( -1 );
760 dst[ j / 2 ] = c << 4;
761
762 c = hex[j + 1];
763 if( c >= '0' && c <= '9' )
764 c -= '0';
765 else if( c >= 'a' && c <= 'f' )
766 c -= 'a' - 10;
767 else if( c >= 'A' && c <= 'F' )
768 c -= 'A' - 10;
769 else
770 return( -1 );
771 dst[ j / 2 ] |= c;
772 }
773
774 return( 0 );
775}
776
Hanno Beckera0e20d02019-05-15 14:03:01 +0100777#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100778int report_cid_usage( mbedtls_ssl_context *ssl,
779 const char *additional_description )
780{
781 int ret;
782 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
783 size_t peer_cid_len;
784 int cid_negotiated;
785
786 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
787 return( 0 );
788
Hanno Becker6ae14c02019-05-22 16:59:25 +0100789 /* Check if the use of a CID has been negotiated,
790 * but don't ask for the CID value and length.
791 *
792 * Note: Here and below, we're demonstrating the various ways
793 * in which mbedtls_ssl_get_peer_cid() can be called,
794 * depending on whether or not the length/value of the
795 * peer's CID is needed.
796 *
797 * An actual application, however, should use
798 * just one call to mbedtls_ssl_get_peer_cid(). */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100799 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
Hanno Becker6ae14c02019-05-22 16:59:25 +0100800 NULL, NULL );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100801 if( ret != 0 )
802 {
803 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
804 -ret );
805 return( ret );
806 }
807
808 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
809 {
810 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
811 {
812 mbedtls_printf( "(%s) Use of Connection ID was rejected by the server.\n",
813 additional_description );
814 }
815 }
816 else
817 {
818 size_t idx=0;
819 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
820 additional_description );
Hanno Becker6ae14c02019-05-22 16:59:25 +0100821
822 /* Ask for just the length of the peer's CID. */
823 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
824 NULL, &peer_cid_len );
825 if( ret != 0 )
826 {
827 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
828 -ret );
829 return( ret );
830 }
831
832 /* Ask for just length + value of the peer's CID. */
833 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
834 peer_cid, &peer_cid_len );
835 if( ret != 0 )
836 {
837 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
838 -ret );
839 return( ret );
840 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100841 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
842 additional_description,
843 (unsigned) peer_cid_len );
844 while( idx < peer_cid_len )
845 {
846 mbedtls_printf( "%02x ", peer_cid[ idx ] );
847 idx++;
848 }
849 mbedtls_printf( "\n" );
850 }
851
852 return( 0 );
853}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100854#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100855
Paul Bakker9caf2d22010-02-18 19:37:19 +0000856int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000857{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200858 int ret = 0, len, tail_len, i, written, frags, retry_left;
859 mbedtls_net_context server_fd;
Hanno Beckere4ad3e82017-09-18 15:05:46 +0100860
861 unsigned char buf[MAX_REQUEST_SIZE + 1];
862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
864 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200865 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200866#endif
Hanno Becker90cb3592019-04-09 17:24:19 +0100867
Hanno Beckera0e20d02019-05-15 14:03:01 +0100868#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +0100869 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100870 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker90cb3592019-04-09 17:24:19 +0100871 size_t cid_len = 0;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100872 size_t cid_renego_len = 0;
Hanno Becker90cb3592019-04-09 17:24:19 +0100873#endif
874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +0100876 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200877#endif
Hanno Beckere6706e62017-05-15 16:05:15 +0100878#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +0100879 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +0100880 const mbedtls_ecp_curve_info *curve_cur;
881#endif
882
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200883 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000884
Hanno Beckere86964c2018-10-23 11:37:50 +0100885#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500886 psa_key_handle_t slot = 0;
Hanno Beckere86964c2018-10-23 11:37:50 +0100887 psa_algorithm_t alg = 0;
888 psa_key_policy_t policy;
889 psa_status_t status;
890#endif
891
Gilles Peskineef86ab22017-05-05 18:59:02 +0200892#if defined(MBEDTLS_X509_CRT_PARSE_C)
893 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
894#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 mbedtls_entropy_context entropy;
896 mbedtls_ctr_drbg_context ctr_drbg;
897 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200898 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 mbedtls_ssl_session saved_session;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200900#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200901 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200902#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200904 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 mbedtls_x509_crt cacert;
906 mbedtls_x509_crt clicert;
907 mbedtls_pk_context pkey;
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +0100908#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500909 psa_key_handle_t key_slot = 0; /* invalid key slot */
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +0100910#endif
Paul Bakkered27a042013-04-18 22:46:23 +0200911#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000912 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000913 const int *list;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300914#if defined(MBEDTLS_SSL_EXPORT_KEYS)
915 unsigned char eap_tls_keymaterial[16];
916 unsigned char eap_tls_iv[8];
917 const char* eap_tls_label = "client EAP encryption";
918 eap_tls_keys eap_tls_keying;
919#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000920
Paul Bakker1a207ec2011-02-06 13:22:40 +0000921 /*
922 * Make sure memory references are valid.
923 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200924 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200925 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200926 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200928 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929#if defined(MBEDTLS_X509_CRT_PARSE_C)
930 mbedtls_x509_crt_init( &cacert );
931 mbedtls_x509_crt_init( &clicert );
932 mbedtls_pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200933#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200935 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200936#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000937
Hanno Beckerb2b468b2018-11-12 17:46:59 +0000938#if defined(MBEDTLS_USE_PSA_CRYPTO)
939 status = psa_crypto_init();
940 if( status != PSA_SUCCESS )
941 {
942 mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
943 (int) status );
944 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
945 goto exit;
946 }
947#endif
948
Paul Bakker9caf2d22010-02-18 19:37:19 +0000949 if( argc == 0 )
950 {
951 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000952 if( ret == 0 )
953 ret = 1;
954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 mbedtls_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker51936882011-02-20 16:05:58 +0000958 while( *list )
959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200961 list++;
962 if( !*list )
963 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000965 list++;
966 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 mbedtls_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000968 goto exit;
969 }
970
971 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100972 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000973 opt.server_port = DFL_SERVER_PORT;
974 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker90cb3592019-04-09 17:24:19 +0100975 opt.cid_enabled = DFL_CID_ENABLED;
976 opt.cid_val = DFL_CID_VALUE;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100977 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
978 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100979 opt.nbio = DFL_NBIO;
Hanno Becker16970d22017-10-10 15:56:37 +0100980 opt.event = DFL_EVENT;
Hanno Beckerbb425db2019-04-03 12:59:58 +0100981 opt.context_crt_cb = DFL_CONTEXT_CRT_CB;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200982 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200983 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000984 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200985 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000986 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000987 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000988 opt.crt_file = DFL_CRT_FILE;
989 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +0100990 opt.key_opaque = DFL_KEY_OPAQUE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200991 opt.psk = DFL_PSK;
Hanno Beckere86964c2018-10-23 11:37:50 +0100992#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000993 opt.psk_opaque = DFL_PSK_OPAQUE;
Hanno Beckere86964c2018-10-23 11:37:50 +0100994#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200995#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
996 opt.ca_callback = DFL_CA_CALLBACK;
997#endif
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200998 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200999 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001000 opt.ec_max_ops = DFL_EC_MAX_OPS;
Paul Bakker51936882011-02-20 16:05:58 +00001001 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +00001002 opt.renegotiation = DFL_RENEGOTIATION;
1003 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001004 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001005 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001006 opt.min_version = DFL_MIN_VERSION;
1007 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001008 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001009 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001010 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001011 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001012 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001013 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001014 opt.dhmlen = DFL_DHMLEN;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001015 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001016 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001017 opt.reconnect_hard = DFL_RECONNECT_HARD;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001018 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001019 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001020 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001021 opt.transport = DFL_TRANSPORT;
1022 opt.hs_to_min = DFL_HS_TO_MIN;
1023 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001024 opt.dtls_mtu = DFL_DTLS_MTU;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001025 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001026 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001027 opt.etm = DFL_ETM;
Hanno Becker4d615912018-08-14 13:33:30 +01001028 opt.dgram_packing = DFL_DGRAM_PACKING;
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001029 opt.eap_tls = DFL_EAP_TLS;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001030
1031 for( i = 1; i < argc; i++ )
1032 {
Paul Bakker9caf2d22010-02-18 19:37:19 +00001033 p = argv[i];
1034 if( ( q = strchr( p, '=' ) ) == NULL )
1035 goto usage;
1036 *q++ = '\0';
1037
1038 if( strcmp( p, "server_name" ) == 0 )
1039 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001040 else if( strcmp( p, "server_addr" ) == 0 )
1041 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +00001042 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001043 opt.server_port = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001044 else if( strcmp( p, "dtls" ) == 0 )
1045 {
1046 int t = atoi( q );
1047 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001049 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001051 else
1052 goto usage;
1053 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001054 else if( strcmp( p, "debug_level" ) == 0 )
1055 {
1056 opt.debug_level = atoi( q );
1057 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1058 goto usage;
1059 }
Hanno Beckerbb425db2019-04-03 12:59:58 +01001060 else if( strcmp( p, "context_crt_cb" ) == 0 )
1061 {
1062 opt.context_crt_cb = atoi( q );
1063 if( opt.context_crt_cb != 0 && opt.context_crt_cb != 1 )
1064 goto usage;
1065 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001066 else if( strcmp( p, "nbio" ) == 0 )
1067 {
1068 opt.nbio = atoi( q );
1069 if( opt.nbio < 0 || opt.nbio > 2 )
1070 goto usage;
1071 }
Hanno Becker16970d22017-10-10 15:56:37 +01001072 else if( strcmp( p, "event" ) == 0 )
1073 {
1074 opt.event = atoi( q );
1075 if( opt.event < 0 || opt.event > 2 )
1076 goto usage;
1077 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001078 else if( strcmp( p, "read_timeout" ) == 0 )
1079 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001080 else if( strcmp( p, "max_resend" ) == 0 )
1081 {
1082 opt.max_resend = atoi( q );
1083 if( opt.max_resend < 0 )
1084 goto usage;
1085 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001086 else if( strcmp( p, "request_page" ) == 0 )
1087 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +02001088 else if( strcmp( p, "request_size" ) == 0 )
1089 {
1090 opt.request_size = atoi( q );
Hanno Beckere4ad3e82017-09-18 15:05:46 +01001091 if( opt.request_size < 0 ||
1092 opt.request_size > MAX_REQUEST_SIZE )
Paul Bakker93c32b22014-04-25 13:40:05 +02001093 goto usage;
1094 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001095 else if( strcmp( p, "ca_file" ) == 0 )
1096 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +00001097 else if( strcmp( p, "ca_path" ) == 0 )
1098 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +00001099 else if( strcmp( p, "crt_file" ) == 0 )
1100 opt.crt_file = q;
1101 else if( strcmp( p, "key_file" ) == 0 )
1102 opt.key_file = q;
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001103#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
1104 else if( strcmp( p, "key_opaque" ) == 0 )
1105 opt.key_opaque = atoi( q );
1106#endif
Hanno Beckera0e20d02019-05-15 14:03:01 +01001107#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +01001108 else if( strcmp( p, "cid" ) == 0 )
1109 {
1110 opt.cid_enabled = atoi( q );
1111 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
1112 goto usage;
1113 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001114 else if( strcmp( p, "cid_renego" ) == 0 )
1115 {
1116 opt.cid_enabled_renego = atoi( q );
1117 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
1118 goto usage;
1119 }
Hanno Becker90cb3592019-04-09 17:24:19 +01001120 else if( strcmp( p, "cid_val" ) == 0 )
1121 {
1122 opt.cid_val = q;
1123 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001124 else if( strcmp( p, "cid_val_renego" ) == 0 )
1125 {
1126 opt.cid_val_renego = q;
1127 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001128#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001129 else if( strcmp( p, "psk" ) == 0 )
1130 opt.psk = q;
Hanno Beckere86964c2018-10-23 11:37:50 +01001131#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001132 else if( strcmp( p, "psk_opaque" ) == 0 )
1133 opt.psk_opaque = atoi( q );
Hanno Beckere86964c2018-10-23 11:37:50 +01001134#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001135#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1136 else if( strcmp( p, "ca_callback" ) == 0)
1137 opt.ca_callback = atoi( q );
1138#endif
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001139 else if( strcmp( p, "psk_identity" ) == 0 )
1140 opt.psk_identity = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001141 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1142 opt.ecjpake_pw = q;
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02001143 else if( strcmp( p, "ec_max_ops" ) == 0 )
1144 opt.ec_max_ops = atoi( q );
Paul Bakker51936882011-02-20 16:05:58 +00001145 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker51936882011-02-20 16:05:58 +00001148
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001149 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +00001150 {
1151 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +00001152 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +00001153 }
Paul Bakker51936882011-02-20 16:05:58 +00001154 opt.force_ciphersuite[1] = 0;
1155 }
Paul Bakker48916f92012-09-16 19:57:18 +00001156 else if( strcmp( p, "renegotiation" ) == 0 )
1157 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001158 opt.renegotiation = (atoi( q )) ?
1159 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1160 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakker48916f92012-09-16 19:57:18 +00001161 }
1162 else if( strcmp( p, "allow_legacy" ) == 0 )
1163 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001164 switch( atoi( q ) )
1165 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001166 case -1:
1167 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1168 break;
1169 case 0:
1170 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1171 break;
1172 case 1:
1173 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1174 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001175 default: goto usage;
1176 }
Paul Bakker48916f92012-09-16 19:57:18 +00001177 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001178 else if( strcmp( p, "renegotiate" ) == 0 )
1179 {
1180 opt.renegotiate = atoi( q );
1181 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1182 goto usage;
1183 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001184 else if( strcmp( p, "exchanges" ) == 0 )
1185 {
1186 opt.exchanges = atoi( q );
1187 if( opt.exchanges < 1 )
1188 goto usage;
1189 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001190 else if( strcmp( p, "reconnect" ) == 0 )
1191 {
1192 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001193 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001194 goto usage;
1195 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001196 else if( strcmp( p, "reco_delay" ) == 0 )
1197 {
1198 opt.reco_delay = atoi( q );
1199 if( opt.reco_delay < 0 )
1200 goto usage;
1201 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02001202 else if( strcmp( p, "reconnect_hard" ) == 0 )
1203 {
1204 opt.reconnect_hard = atoi( q );
1205 if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
1206 goto usage;
1207 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001208 else if( strcmp( p, "tickets" ) == 0 )
1209 {
1210 opt.tickets = atoi( q );
1211 if( opt.tickets < 0 || opt.tickets > 2 )
1212 goto usage;
1213 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001214 else if( strcmp( p, "alpn" ) == 0 )
1215 {
1216 opt.alpn_string = q;
1217 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001218 else if( strcmp( p, "fallback" ) == 0 )
1219 {
1220 switch( atoi( q ) )
1221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
1223 case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001224 default: goto usage;
1225 }
1226 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001227 else if( strcmp( p, "extended_ms" ) == 0 )
1228 {
1229 switch( atoi( q ) )
1230 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001231 case 0:
1232 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1233 break;
1234 case 1:
1235 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1236 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001237 default: goto usage;
1238 }
1239 }
Hanno Beckere6706e62017-05-15 16:05:15 +01001240 else if( strcmp( p, "curves" ) == 0 )
1241 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001242 else if( strcmp( p, "etm" ) == 0 )
1243 {
1244 switch( atoi( q ) )
1245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1247 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001248 default: goto usage;
1249 }
1250 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001251 else if( strcmp( p, "min_version" ) == 0 )
1252 {
1253 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001255 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001257 else if( strcmp( q, "tls1_1" ) == 0 ||
1258 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001260 else if( strcmp( q, "tls1_2" ) == 0 ||
1261 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001263 else
1264 goto usage;
1265 }
1266 else if( strcmp( p, "max_version" ) == 0 )
1267 {
1268 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001270 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001272 else if( strcmp( q, "tls1_1" ) == 0 ||
1273 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001275 else if( strcmp( q, "tls1_2" ) == 0 ||
1276 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001278 else
1279 goto usage;
1280 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001281 else if( strcmp( p, "arc4" ) == 0 )
1282 {
1283 switch( atoi( q ) )
1284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1286 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001287 default: goto usage;
1288 }
1289 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02001290 else if( strcmp( p, "allow_sha1" ) == 0 )
1291 {
1292 switch( atoi( q ) )
1293 {
1294 case 0: opt.allow_sha1 = 0; break;
1295 case 1: opt.allow_sha1 = 1; break;
1296 default: goto usage;
1297 }
1298 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001299 else if( strcmp( p, "force_version" ) == 0 )
1300 {
1301 if( strcmp( q, "ssl3" ) == 0 )
1302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1304 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001305 }
1306 else if( strcmp( q, "tls1" ) == 0 )
1307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1309 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001310 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001311 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1314 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001315 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001316 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1319 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001320 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001321 else if( strcmp( q, "dtls1" ) == 0 )
1322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1324 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1325 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001326 }
1327 else if( strcmp( q, "dtls1_2" ) == 0 )
1328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1330 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1331 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001332 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001333 else
1334 goto usage;
1335 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001336 else if( strcmp( p, "auth_mode" ) == 0 )
1337 {
1338 if( strcmp( q, "none" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001340 else if( strcmp( q, "optional" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001342 else if( strcmp( q, "required" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001344 else
1345 goto usage;
1346 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001347 else if( strcmp( p, "max_frag_len" ) == 0 )
1348 {
1349 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001351 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001353 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001355 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001357 else
1358 goto usage;
1359 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001360 else if( strcmp( p, "trunc_hmac" ) == 0 )
1361 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001362 switch( atoi( q ) )
1363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1365 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001366 default: goto usage;
1367 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001368 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001369 else if( strcmp( p, "hs_timeout" ) == 0 )
1370 {
1371 if( ( p = strchr( q, '-' ) ) == NULL )
1372 goto usage;
1373 *p++ = '\0';
1374 opt.hs_to_min = atoi( q );
1375 opt.hs_to_max = atoi( p );
1376 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1377 goto usage;
1378 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001379 else if( strcmp( p, "mtu" ) == 0 )
1380 {
1381 opt.dtls_mtu = atoi( q );
1382 if( opt.dtls_mtu < 0 )
1383 goto usage;
1384 }
Hanno Becker4d615912018-08-14 13:33:30 +01001385 else if( strcmp( p, "dgram_packing" ) == 0 )
1386 {
1387 opt.dgram_packing = atoi( q );
1388 if( opt.dgram_packing != 0 &&
1389 opt.dgram_packing != 1 )
1390 {
1391 goto usage;
1392 }
1393 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001394 else if( strcmp( p, "recsplit" ) == 0 )
1395 {
1396 opt.recsplit = atoi( q );
1397 if( opt.recsplit < 0 || opt.recsplit > 1 )
1398 goto usage;
1399 }
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001400 else if( strcmp( p, "dhmlen" ) == 0 )
1401 {
1402 opt.dhmlen = atoi( q );
1403 if( opt.dhmlen < 0 )
1404 goto usage;
1405 }
Andres Amaya Garciabc818842018-10-16 21:08:38 +01001406 else if( strcmp( p, "query_config" ) == 0 )
1407 {
1408 return query_config( q );
1409 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001410 else if( strcmp( p, "eap_tls" ) == 0 )
1411 {
1412 opt.eap_tls = atoi( q );
1413 if( opt.eap_tls < 0 || opt.eap_tls > 1 )
1414 goto usage;
1415 }
Paul Bakker9caf2d22010-02-18 19:37:19 +00001416 else
1417 goto usage;
1418 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001419
Hanno Becker16970d22017-10-10 15:56:37 +01001420 /* Event-driven IO is incompatible with the above custom
1421 * receive and send functions, as the polling builds on
1422 * refers to the underlying net_context. */
1423 if( opt.event == 1 && opt.nbio != 1 )
1424 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001425 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001426 opt.nbio = 1;
1427 }
1428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#if defined(MBEDTLS_DEBUG_C)
1430 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001431#endif
1432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001434 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001435 * Unhexify the pre-shared key if any is given
1436 */
1437 if( strlen( opt.psk ) )
1438 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001439 psk_len = strlen( opt.psk ) / 2;
1440 if( psk_len > sizeof( psk ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001441 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001442 mbedtls_printf( "pre-shared key too long\n" );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001443 goto exit;
1444 }
1445
Hanno Becker1f583ee2019-04-09 17:12:56 +01001446 if( unhexify( opt.psk, psk ) != 0 )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001447 {
Hanno Becker1f583ee2019-04-09 17:12:56 +01001448 mbedtls_printf( "pre-shared key not valid hex\n" );
1449 goto exit;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001450 }
1451 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001453
Hanno Beckere86964c2018-10-23 11:37:50 +01001454
1455#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001456 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001457 {
1458 if( opt.psk == NULL )
1459 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00001460 mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
Hanno Beckere86964c2018-10-23 11:37:50 +01001461 ret = 2;
1462 goto usage;
1463 }
1464
1465 if( opt.force_ciphersuite[0] <= 0 )
1466 {
1467 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" );
1468 ret = 2;
1469 goto usage;
1470 }
1471 }
1472#endif /* MBEDTLS_USE_PSA_CRYPTO */
1473
1474 if( opt.force_ciphersuite[0] > 0 )
1475 {
1476 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1477 ciphersuite_info =
1478 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1479
1480 if( opt.max_version != -1 &&
1481 ciphersuite_info->min_minor_ver > opt.max_version )
1482 {
1483 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
1484 ret = 2;
1485 goto usage;
1486 }
1487 if( opt.min_version != -1 &&
1488 ciphersuite_info->max_minor_ver < opt.min_version )
1489 {
1490 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
1491 ret = 2;
1492 goto usage;
1493 }
1494
1495 /* If the server selects a version that's not supported by
1496 * this suite, then there will be no common ciphersuite... */
1497 if( opt.max_version == -1 ||
1498 opt.max_version > ciphersuite_info->max_minor_ver )
1499 {
1500 opt.max_version = ciphersuite_info->max_minor_ver;
1501 }
1502 if( opt.min_version < ciphersuite_info->min_minor_ver )
1503 {
1504 opt.min_version = ciphersuite_info->min_minor_ver;
1505 /* DTLS starts with TLS 1.1 */
1506 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1507 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1508 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1509 }
1510
1511 /* Enable RC4 if needed and not explicitly disabled */
1512 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
1513 {
1514 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
1515 {
1516 mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" );
1517 ret = 2;
1518 goto usage;
1519 }
1520
1521 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
1522 }
1523
Hanno Becker90cb3592019-04-09 17:24:19 +01001524
Hanno Beckere86964c2018-10-23 11:37:50 +01001525#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001526 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01001527 {
1528 /* Ensure that the chosen ciphersuite is PSK-only; we must know
1529 * the ciphersuite in advance to set the correct policy for the
1530 * PSK key slot. This limitation might go away in the future. */
1531 if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
1532 opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 )
1533 {
1534 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" );
1535 ret = 2;
1536 goto usage;
1537 }
1538
1539 /* Determine KDF algorithm the opaque PSK will be used in. */
1540#if defined(MBEDTLS_SHA512_C)
1541 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1542 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1543 else
1544#endif /* MBEDTLS_SHA512_C */
1545 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1546 }
1547#endif /* MBEDTLS_USE_PSA_CRYPTO */
1548 }
1549
Hanno Beckera0e20d02019-05-15 14:03:01 +01001550#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001551 cid_len = strlen( opt.cid_val ) / 2;
1552 if( cid_len > sizeof( cid ) )
1553 {
1554 mbedtls_printf( "CID too long\n" );
1555 goto exit;
1556 }
Hanno Becker90cb3592019-04-09 17:24:19 +01001557
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001558 if( unhexify( opt.cid_val, cid ) != 0 )
1559 {
1560 mbedtls_printf( "CID not valid hex\n" );
1561 goto exit;
1562 }
1563
1564 /* Keep CID settings for renegotiation unless
1565 * specified otherwise. */
1566 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
1567 opt.cid_enabled_renego = opt.cid_enabled;
1568 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
1569 opt.cid_val_renego = opt.cid_val;
1570
1571 cid_renego_len = strlen( opt.cid_val_renego ) / 2;
1572 if( cid_renego_len > sizeof( cid_renego ) )
1573 {
1574 mbedtls_printf( "CID too long\n" );
1575 goto exit;
1576 }
1577
1578 if( unhexify( opt.cid_val_renego, cid_renego ) != 0 )
1579 {
1580 mbedtls_printf( "CID not valid hex\n" );
1581 goto exit;
1582 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001583#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01001584
Hanno Beckere6706e62017-05-15 16:05:15 +01001585#if defined(MBEDTLS_ECP_C)
1586 if( opt.curves != NULL )
1587 {
1588 p = (char *) opt.curves;
1589 i = 0;
1590
1591 if( strcmp( p, "none" ) == 0 )
1592 {
1593 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1594 }
1595 else if( strcmp( p, "default" ) != 0 )
1596 {
1597 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001598 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001599 {
1600 q = p;
1601
1602 /* Terminate the current string */
1603 while( *p != ',' && *p != '\0' )
1604 p++;
1605 if( *p == ',' )
1606 *p++ = '\0';
1607
1608 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1609 {
1610 curve_list[i++] = curve_cur->grp_id;
1611 }
1612 else
1613 {
1614 mbedtls_printf( "unknown curve %s\n", q );
1615 mbedtls_printf( "supported curves: " );
1616 for( curve_cur = mbedtls_ecp_curve_list();
1617 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1618 curve_cur++ )
1619 {
1620 mbedtls_printf( "%s ", curve_cur->name );
1621 }
1622 mbedtls_printf( "\n" );
1623 goto exit;
1624 }
1625 }
1626
1627 mbedtls_printf("Number of curves: %d\n", i );
1628
Hanno Becker8651a432017-06-09 16:13:22 +01001629 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001630 {
Hanno Becker8651a432017-06-09 16:13:22 +01001631 mbedtls_printf( "curves list too long, maximum %d",
1632 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001633 goto exit;
1634 }
1635
1636 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1637 }
1638 }
1639#endif /* MBEDTLS_ECP_C */
1640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001642 if( opt.alpn_string != NULL )
1643 {
1644 p = (char *) opt.alpn_string;
1645 i = 0;
1646
1647 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001648 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001649 {
1650 alpn_list[i++] = p;
1651
1652 /* Terminate the current string and move on to next one */
1653 while( *p != ',' && *p != '\0' )
1654 p++;
1655 if( *p == ',' )
1656 *p++ = '\0';
1657 }
1658 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001660
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001661 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001662 * 0. Initialize the RNG and the session data
1663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001665 fflush( stdout );
1666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 mbedtls_entropy_init( &entropy );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001668 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1669 &entropy, (const unsigned char *) pers,
1670 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +00001671 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001672 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
1673 -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +00001674 goto exit;
1675 }
1676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001680 /*
1681 * 1.1. Load the trusted CA
1682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001684 fflush( stdout );
1685
Hanno Becker623e7b42019-03-05 16:02:15 +00001686 if( strcmp( opt.ca_path, "none" ) == 0 ||
1687 strcmp( opt.ca_file, "none" ) == 0 )
1688 {
1689 ret = 0;
1690 }
1691 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#if defined(MBEDTLS_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +00001693 if( strlen( opt.ca_path ) )
Hanno Becker623e7b42019-03-05 16:02:15 +00001694 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +00001695 else if( strlen( opt.ca_file ) )
Hanno Becker623e7b42019-03-05 16:02:15 +00001696 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001697 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001698#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699#if defined(MBEDTLS_CERTS_C)
Hanno Becker2900b142019-02-01 08:15:06 +00001700 {
1701#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 ret = mbedtls_x509_crt_parse( &cacert,
1705 (const unsigned char *) mbedtls_test_cas[i],
1706 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001707 if( ret != 0 )
1708 break;
1709 }
Hanno Becker2900b142019-02-01 08:15:06 +00001710 if( ret == 0 )
1711#endif /* MBEDTLS_PEM_PARSE_C */
1712 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
1713 {
1714 ret = mbedtls_x509_crt_parse_der( &cacert,
1715 (const unsigned char *) mbedtls_test_cas_der[i],
1716 mbedtls_test_cas_der_len[i] );
1717 if( ret != 0 )
1718 break;
1719 }
1720 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001721#else
1722 {
1723 ret = 1;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001724 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001725 }
Hanno Becker2900b142019-02-01 08:15:06 +00001726#endif /* MBEDTLS_CERTS_C */
Paul Bakker42488232012-05-16 08:21:05 +00001727 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001728 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001729 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1730 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001731 goto exit;
1732 }
1733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001735
1736 /*
1737 * 1.2. Load own certificate and private key
1738 *
1739 * (can be skipped if client authentication is not required)
1740 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001742 fflush( stdout );
1743
Hanno Becker623e7b42019-03-05 16:02:15 +00001744 if( strcmp( opt.crt_file, "none" ) == 0 )
1745 ret = 0;
1746 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001748 if( strlen( opt.crt_file ) )
Hanno Becker623e7b42019-03-05 16:02:15 +00001749 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +02001750 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001751#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001753 ret = mbedtls_x509_crt_parse( &clicert,
1754 (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +00001756#else
1757 {
1758 ret = 1;
Hanno Beckera0c5ceb2018-12-05 16:49:42 +00001759 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001760 }
1761#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001762 if( ret != 0 )
1763 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001764 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
1765 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001766 goto exit;
1767 }
1768
Hanno Becker623e7b42019-03-05 16:02:15 +00001769 if( strcmp( opt.key_file, "none" ) == 0 )
1770 ret = 0;
1771 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772#if defined(MBEDTLS_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +00001773 if( strlen( opt.key_file ) )
Hanno Becker623e7b42019-03-05 16:02:15 +00001774 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +00001775 else
Paul Bakker5690efc2011-05-26 13:16:06 +00001776#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777#if defined(MBEDTLS_CERTS_C)
Hanno Becker16970d22017-10-10 15:56:37 +01001778 ret = mbedtls_pk_parse_key( &pkey,
1779 (const unsigned char *) mbedtls_test_cli_key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +00001781#else
1782 {
1783 ret = 1;
Hanno Beckera0c5ceb2018-12-05 16:49:42 +00001784 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakker5690efc2011-05-26 13:16:06 +00001785 }
1786#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001787 if( ret != 0 )
1788 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001789 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
1790 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001791 goto exit;
1792 }
1793
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001794#if defined(MBEDTLS_USE_PSA_CRYPTO)
1795 if( opt.key_opaque != 0 )
1796 {
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001797 if( ( ret = mbedtls_pk_wrap_as_opaque( &pkey, &key_slot,
1798 PSA_ALG_SHA_256 ) ) != 0 )
1799 {
1800 mbedtls_printf( " failed\n ! "
1801 "mbedtls_pk_wrap_as_opaque returned -0x%x\n\n", -ret );
1802 goto exit;
1803 }
Manuel Pégourié-Gonnardef68be42018-11-07 09:42:35 +01001804 }
1805#endif /* MBEDTLS_USE_PSA_CRYPTO */
1806
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01001807 mbedtls_printf( " ok (key type: %s)\n", mbedtls_pk_get_name( &pkey ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001809
1810 /*
1811 * 2. Start the connection
1812 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001813 if( opt.server_addr == NULL)
1814 opt.server_addr = opt.server_name;
1815
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001816 mbedtls_printf( " . Connecting to %s/%s/%s...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001818 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001819 fflush( stdout );
1820
Hanno Becker16970d22017-10-10 15:56:37 +01001821 if( ( ret = mbedtls_net_connect( &server_fd,
1822 opt.server_addr, opt.server_port,
1823 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1824 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001825 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001826 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
1827 -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001828 goto exit;
1829 }
1830
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001831 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001832 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001833 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001834 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001835 if( ret != 0 )
1836 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001837 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
1838 -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001839 goto exit;
1840 }
1841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001843
1844 /*
1845 * 3. Setup stuff
1846 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001848 fflush( stdout );
1849
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001850 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1851 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001852 opt.transport,
1853 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001854 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001855 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
1856 -ret );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001857 goto exit;
1858 }
1859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskineef86ab22017-05-05 18:59:02 +02001861 /* The default algorithms profile disables SHA-1, but our tests still
1862 rely on it heavily. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02001863 if( opt.allow_sha1 > 0 )
1864 {
1865 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1866 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
1867 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
1868 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02001869
Hanno Beckerbb425db2019-04-03 12:59:58 +01001870 if( opt.context_crt_cb == 0 )
1871 mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
1872
Hanno Beckera1051b42019-02-26 11:38:29 +00001873 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Gilles Peskineef86ab22017-05-05 18:59:02 +02001874#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +00001875
Hanno Beckera0e20d02019-05-15 14:03:01 +01001876#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001877 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckerad4a1372019-05-03 13:06:44 +01001878 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001879 if( opt.cid_enabled == 1 &&
1880 opt.cid_enabled_renego == 1 &&
1881 cid_len != cid_renego_len )
1882 {
1883 mbedtls_printf( "CID length must not change during renegotiation\n" );
1884 goto usage;
1885 }
1886
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001887 if( opt.cid_enabled == 1 )
Hanno Becker8367ccc2019-05-14 11:30:10 +01001888 ret = mbedtls_ssl_conf_cid( &conf, cid_len,
1889 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001890 else
Hanno Becker8367ccc2019-05-14 11:30:10 +01001891 ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
1892 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001893
Hanno Beckerad4a1372019-05-03 13:06:44 +01001894 if( ret != 0 )
1895 {
Hanno Beckerd5eed422019-05-23 16:58:22 +01001896 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
1897 -ret );
Hanno Beckerad4a1372019-05-03 13:06:44 +01001898 goto exit;
1899 }
1900 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001901#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerad4a1372019-05-03 13:06:44 +01001902
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001903 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001904 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001907 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Hanno Becker16970d22017-10-10 15:56:37 +01001908 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min,
1909 opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001910
Hanno Becker4d615912018-08-14 13:33:30 +01001911 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01001912 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001916 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001917 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001918 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n",
1919 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001920 goto exit;
1921 }
Paul Bakker05decb22013-08-15 13:33:48 +02001922#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001925 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001926 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001927#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001930 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001931 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001932#endif
1933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001935 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001936 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001937#endif
1938
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001939#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1940 if( opt.eap_tls != 0 )
1941 mbedtls_ssl_conf_export_keys_ext_cb( &conf, eap_tls_key_derivation,
1942 &eap_tls_keying );
1943#endif
1944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001946 if( opt.recsplit != DFL_RECSPLIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001947 mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
Hanno Becker16970d22017-10-10 15:56:37 +01001948 ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
1949 : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001950#endif
1951
Manuel Pégourié-Gonnard90966822015-06-11 17:02:29 +02001952#if defined(MBEDTLS_DHM_C)
1953 if( opt.dhmlen != DFL_DHMLEN )
1954 mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
1955#endif
1956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001958 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001959 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001960 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001961 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
1962 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001963 goto exit;
1964 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001965#endif
1966
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001967 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1968 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001969
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001970 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02001973 mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +02001974#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001975
Paul Bakker645ce3a2012-10-31 12:32:41 +00001976 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001977 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001978
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001979#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001980 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001981 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001982#endif
Paul Bakker51936882011-02-20 16:05:58 +00001983
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001984 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001985 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001987 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001988#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001991 if( strcmp( opt.ca_path, "none" ) != 0 &&
1992 strcmp( opt.ca_file, "none" ) != 0 )
1993 {
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001994#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1995 if( opt.ca_callback != 0 )
Hanno Beckercbb59032019-03-28 14:14:22 +00001996 mbedtls_ssl_conf_ca_cb( &conf, ca_callback, &cacert );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001997 else
1998#endif
1999 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002000 }
2001 if( strcmp( opt.crt_file, "none" ) != 0 &&
2002 strcmp( opt.key_file, "none" ) != 0 )
2003 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002004 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002005 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002006 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
2007 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002008 goto exit;
2009 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002010 }
Paul Bakkered27a042013-04-18 22:46:23 +02002011#endif
2012
Hanno Beckere6706e62017-05-15 16:05:15 +01002013#if defined(MBEDTLS_ECP_C)
2014 if( opt.curves != NULL &&
2015 strcmp( opt.curves, "default" ) != 0 )
2016 {
2017 mbedtls_ssl_conf_curves( &conf, curve_list );
2018 }
2019#endif
2020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckere86964c2018-10-23 11:37:50 +01002022#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002023 if( opt.psk_opaque != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01002024 {
2025 /* The algorithm has already been determined earlier. */
Hanno Becker37519ea2019-01-25 14:26:01 +00002026 status = psa_allocate_key( &slot );
Hanno Becker1d911cd2018-11-15 13:06:09 +00002027 if( status != PSA_SUCCESS )
2028 {
2029 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2030 goto exit;
2031 }
Hanno Beckere86964c2018-10-23 11:37:50 +01002032
Hanno Becker13871242019-01-25 14:26:26 +00002033 policy = psa_key_policy_init();
Hanno Beckere86964c2018-10-23 11:37:50 +01002034 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2035
2036 status = psa_set_key_policy( slot, &policy );
2037 if( status != PSA_SUCCESS )
2038 {
2039 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2040 goto exit;
2041 }
2042
2043 status = psa_import_key( slot, PSA_KEY_TYPE_DERIVE, psk, psk_len );
2044 if( status != PSA_SUCCESS )
2045 {
2046 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2047 goto exit;
2048 }
2049
2050 if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, slot,
2051 (const unsigned char *) opt.psk_identity,
Hanno Becker5cd607b2018-11-05 12:52:42 +00002052 strlen( opt.psk_identity ) ) ) != 0 )
Hanno Beckere86964c2018-10-23 11:37:50 +01002053 {
2054 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
2055 ret );
2056 goto exit;
2057 }
2058 }
2059 else
2060#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002061 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002062 (const unsigned char *) opt.psk_identity,
2063 strlen( opt.psk_identity ) ) ) != 0 )
2064 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002065 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n",
2066 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002067 goto exit;
2068 }
Hanno Beckere86964c2018-10-23 11:37:50 +01002069#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002070
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002071 if( opt.min_version != DFL_MIN_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01002072 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
2073 opt.min_version );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01002074
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002075 if( opt.max_version != DFL_MAX_VERSION )
Hanno Becker16970d22017-10-10 15:56:37 +01002076 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
2077 opt.max_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00002078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02002080 if( opt.fallback != DFL_FALLBACK )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002081 mbedtls_ssl_conf_fallback( &conf, opt.fallback );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02002082#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002083
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002084 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
2085 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002086 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
2087 -ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002088 goto exit;
2089 }
2090
2091#if defined(MBEDTLS_X509_CRT_PARSE_C)
2092 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
2093 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002094 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
2095 ret );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002096 goto exit;
2097 }
2098#endif
2099
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002100#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2101 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
2102 {
2103 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
2104 (const unsigned char *) opt.ecjpake_pw,
2105 strlen( opt.ecjpake_pw ) ) ) != 0 )
2106 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002107 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
2108 ret );
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002109 goto exit;
2110 }
2111 }
2112#endif
2113
Hanno Beckerbb425db2019-04-03 12:59:58 +01002114#if defined(MBEDTLS_X509_CRT_PARSE_C)
2115 if( opt.context_crt_cb == 1 )
2116 mbedtls_ssl_set_verify( &ssl, my_verify, NULL );
2117#endif /* MBEDTLS_X509_CRT_PARSE_C */
2118
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002119 if( opt.nbio == 2 )
2120 mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
2121 else
Hanno Becker16970d22017-10-10 15:56:37 +01002122 mbedtls_ssl_set_bio( &ssl, &server_fd,
2123 mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02002124 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002125
Hanno Beckera0e20d02019-05-15 14:03:01 +01002126#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker90cb3592019-04-09 17:24:19 +01002127 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2128 {
2129 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
2130 cid, cid_len ) ) != 0 )
2131 {
2132 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2133 ret );
2134 goto exit;
2135 }
2136 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002137#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01002138
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002139#if defined(MBEDTLS_SSL_PROTO_DTLS)
2140 if( opt.dtls_mtu != DFL_DTLS_MTU )
2141 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
2142#endif
2143
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002144#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002145 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
2146 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002147#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002148
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002149#if defined(MBEDTLS_ECP_RESTARTABLE)
2150 if( opt.ec_max_ops != DFL_EC_MAX_OPS )
2151 mbedtls_ecp_set_max_ops( opt.ec_max_ops );
2152#endif
2153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002155
Paul Bakker5121ce52009-01-03 21:22:43 +00002156 /*
2157 * 4. Handshake
2158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002160 fflush( stdout );
2161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 {
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002164 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2165 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002166 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Paul Bakker40e46942009-01-03 21:51:57 +00002167 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002168 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
2169 -ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2171 mbedtls_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01002172 " Unable to verify the server's certificate. "
2173 "Either it is invalid,\n"
2174 " or you didn't set ca_file or ca_path "
2175 "to an appropriate value.\n"
2176 " Alternatively, you may want to use "
2177 "auth_mode=optional for testing purposes.\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00002180 }
Hanno Becker16970d22017-10-10 15:56:37 +01002181
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002182#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002183 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2184 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002185#endif
2186
Hanno Becker16970d22017-10-10 15:56:37 +01002187 /* For event-driven IO, wait for socket to become available */
2188 if( opt.event == 1 /* level triggered IO */ )
2189 {
2190#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002191 ret = idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002192#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002193 ret = idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002194#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002195 if( ret != 0 )
2196 goto exit;
Hanno Becker16970d22017-10-10 15:56:37 +01002197 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 }
2199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Hanno Becker16970d22017-10-10 15:56:37 +01002201 mbedtls_ssl_get_version( &ssl ),
2202 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
2205 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002206 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002208
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002209#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2210 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
2211 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
2212#endif
2213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002215 if( opt.alpn_string != NULL )
2216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
2218 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002219 alp ? alp : "(none)" );
2220 }
2221#endif
2222
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002223#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Ron Eldor51d3ab52019-05-12 14:54:30 +03002224 if( opt.eap_tls != 0 )
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002225 {
2226 size_t j = 0;
Ron Eldor51d3ab52019-05-12 14:54:30 +03002227
2228 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type,
2229 eap_tls_keying.master_secret,
2230 sizeof( eap_tls_keying.master_secret ),
2231 eap_tls_label,
2232 eap_tls_keying.randbytes,
2233 sizeof( eap_tls_keying.randbytes ),
2234 eap_tls_keymaterial,
2235 sizeof( eap_tls_keymaterial ) ) )
2236 != 0 )
2237 {
2238 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2239 -ret );
2240 goto exit;
2241 }
2242
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002243 mbedtls_printf( " EAP-TLS key material is:" );
2244 for( j = 0; j < sizeof( eap_tls_keymaterial ); j++ )
2245 {
2246 if( j % 8 == 0 )
2247 mbedtls_printf("\n ");
2248 mbedtls_printf("%02x ", eap_tls_keymaterial[j] );
2249 }
2250 mbedtls_printf("\n");
2251
Ron Eldor51d3ab52019-05-12 14:54:30 +03002252 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type, NULL, 0,
Ron Eldor51c45072019-05-15 17:49:54 +03002253 eap_tls_label,
2254 eap_tls_keying.randbytes,
2255 sizeof( eap_tls_keying.randbytes ),
2256 eap_tls_iv,
2257 sizeof( eap_tls_iv ) ) ) != 0 )
Ron Eldor51d3ab52019-05-12 14:54:30 +03002258 {
2259 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
2260 -ret );
2261 goto exit;
2262 }
2263
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002264 mbedtls_printf( " EAP-TLS IV is:" );
2265 for( j = 0; j < sizeof( eap_tls_iv ); j++ )
2266 {
2267 if( j % 8 == 0 )
2268 mbedtls_printf("\n ");
2269 mbedtls_printf("%02x ", eap_tls_iv[j] );
2270 }
2271 mbedtls_printf("\n");
2272 }
2273#endif
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002274 if( opt.reconnect != 0 )
2275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 mbedtls_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002277 fflush( stdout );
2278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002280 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002281 mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
2282 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002283 goto exit;
2284 }
2285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002287 }
2288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002290 /*
2291 * 5. Verify the server certificate
2292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002295 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002297 char vrfy_buf[512];
2298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 mbedtls_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002300
Hanno Becker16970d22017-10-10 15:56:37 +01002301 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ),
2302 " ! ", flags );
Paul Bakker5121ce52009-01-03 21:22:43 +00002303
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002304 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002305 }
2306 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002308
Hanno Beckera9766c2c2019-02-25 17:43:18 +00002309 mbedtls_printf( " . Peer certificate information ...\n" );
2310 mbedtls_printf( "%s\n", peer_crt_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002312
Hanno Beckera0e20d02019-05-15 14:03:01 +01002313#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002314 ret = report_cid_usage( &ssl, "initial handshake" );
2315 if( ret != 0 )
2316 goto exit;
2317
Hanno Becker90cb3592019-04-09 17:24:19 +01002318 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2319 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002320 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
2321 cid_renego,
2322 cid_renego_len ) ) != 0 )
Hanno Becker90cb3592019-04-09 17:24:19 +01002323 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002324 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2325 ret );
2326 return( ret );
Hanno Becker90cb3592019-04-09 17:24:19 +01002327 }
2328 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002329#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker90cb3592019-04-09 17:24:19 +01002330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002332 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002333 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002334 /*
2335 * Perform renegotiation (this must be done when the server is waiting
2336 * for input from our side).
2337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 mbedtls_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002339 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002341 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002342 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002343 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002344 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002345 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002346 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
2347 ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01002348 goto exit;
2349 }
Hanno Becker16970d22017-10-10 15:56:37 +01002350
Manuel Pégourié-Gonnard25781f92018-10-15 15:28:16 +02002351#if defined(MBEDTLS_ECP_RESTARTABLE)
2352 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
2353 continue;
2354#endif
2355
Hanno Becker16970d22017-10-10 15:56:37 +01002356 /* For event-driven IO, wait for socket to become available */
2357 if( opt.event == 1 /* level triggered IO */ )
2358 {
2359#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002360 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002361#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002362 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002363#endif
2364 }
2365
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002366 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002368 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01002370
Hanno Beckera0e20d02019-05-15 14:03:01 +01002371#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002372 ret = report_cid_usage( &ssl, "after renegotiation" );
2373 if( ret != 0 )
2374 goto exit;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002375#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002376
Paul Bakker5121ce52009-01-03 21:22:43 +00002377 /*
2378 * 6. Write the GET request
2379 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002380 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002381send_request:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002383 fflush( stdout );
2384
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002385 len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST,
2386 opt.request_page );
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002387 tail_len = (int) strlen( GET_REQUEST_END );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002388
2389 /* Add padding to GET request to reach opt.request_size in length */
2390 if( opt.request_size != DFL_REQUEST_SIZE &&
2391 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02002392 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002393 memset( buf + len, 'A', opt.request_size - len - tail_len );
2394 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02002395 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002396
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002397 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 );
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02002398 len += tail_len;
2399
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02002400 /* Truncate if request size is smaller than the "natural" size */
2401 if( opt.request_size != DFL_REQUEST_SIZE &&
2402 len > opt.request_size )
2403 {
2404 len = opt.request_size;
2405
2406 /* Still end with \r\n unless that's really not possible */
2407 if( len >= 2 ) buf[len - 2] = '\r';
2408 if( len >= 1 ) buf[len - 1] = '\n';
2409 }
2410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 {
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002413 written = 0;
2414 frags = 0;
2415
2416 do
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 {
Hanno Becker16970d22017-10-10 15:56:37 +01002418 while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002419 len - written ) ) < 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002420 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002421 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002422 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002423 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002424 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002425 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
2426 -ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002427 goto exit;
2428 }
Hanno Becker16970d22017-10-10 15:56:37 +01002429
2430 /* For event-driven IO, wait for socket to become available */
2431 if( opt.event == 1 /* level triggered IO */ )
2432 {
2433#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002434 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002435#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002436 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002437#endif
2438 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002439 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002440
2441 frags++;
2442 written += ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002443 }
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002444 while( written < len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002446 else /* Not stream, so datagram */
2447 {
Hanno Becker16970d22017-10-10 15:56:37 +01002448 while( 1 )
2449 {
2450 ret = mbedtls_ssl_write( &ssl, buf, len );
2451
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002452#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002453 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002454 continue;
2455#endif
2456
Hanno Becker16970d22017-10-10 15:56:37 +01002457 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2458 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2459 break;
2460
2461 /* For event-driven IO, wait for socket to become available */
2462 if( opt.event == 1 /* level triggered IO */ )
2463 {
2464#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002465 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002466#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002467 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002468#endif
2469 }
2470 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002471
2472 if( ret < 0 )
2473 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002474 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n",
2475 ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002476 goto exit;
2477 }
2478
2479 frags = 1;
2480 written = ret;
Hanno Beckere4ad3e82017-09-18 15:05:46 +01002481
2482 if( written < len )
2483 {
2484 mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
2485 "was truncated to size %u", (unsigned) written );
2486 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002487 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002488
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02002489 buf[written] = '\0';
Hanno Becker16970d22017-10-10 15:56:37 +01002490 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
2491 written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
Andres Amaya Garciace6fbac2018-07-04 09:29:34 +01002493 /* Send a non-empty request if request_size == 0 */
2494 if ( len == 0 )
2495 {
2496 opt.request_size = DFL_REQUEST_SIZE;
2497 goto send_request;
2498 }
2499
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 /*
2501 * 7. Read the HTTP response
2502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 fflush( stdout );
2505
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002506 /*
2507 * TLS and DTLS need different reading styles (stream vs datagram)
2508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002510 {
2511 do
2512 {
2513 len = sizeof( buf ) - 1;
2514 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002516
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002517#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002518 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002519 continue;
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002520#endif
2521
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002522 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
2523 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Hanno Becker16970d22017-10-10 15:56:37 +01002524 {
2525 /* For event-driven IO, wait for socket to become available */
2526 if( opt.event == 1 /* level triggered IO */ )
2527 {
2528#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002529 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002530#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002531 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002532#endif
2533 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002534 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01002535 }
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002536
2537 if( ret <= 0 )
2538 {
2539 switch( ret )
2540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2542 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002543 ret = 0;
2544 goto close_notify;
2545
2546 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 case MBEDTLS_ERR_NET_CONN_RESET:
2548 mbedtls_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002549 ret = 0;
2550 goto reconnect;
2551
2552 default:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002553 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n",
2554 -ret );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002555 goto exit;
2556 }
2557 }
2558
2559 len = ret;
2560 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002562
2563 /* End of message should be detected according to the syntax of the
2564 * application protocol (eg HTTP), just use a dummy test here. */
2565 if( ret > 0 && buf[len-1] == '\n' )
2566 {
2567 ret = 0;
2568 break;
2569 }
2570 }
2571 while( 1 );
2572 }
2573 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 {
2575 len = sizeof( buf ) - 1;
2576 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
Hanno Becker16970d22017-10-10 15:56:37 +01002578 while( 1 )
2579 {
2580 ret = mbedtls_ssl_read( &ssl, buf, len );
2581
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002582#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002583 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002584 continue;
2585#endif
2586
Hanno Becker16970d22017-10-10 15:56:37 +01002587 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2588 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
2589 break;
2590
2591 /* For event-driven IO, wait for socket to become available */
2592 if( opt.event == 1 /* level triggered IO */ )
2593 {
2594#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002595 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002596#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002597 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002598#endif
2599 }
2600 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002602 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002604 switch( ret )
2605 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002606 case MBEDTLS_ERR_SSL_TIMEOUT:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 mbedtls_printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02002608 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02002609 goto send_request;
2610 goto exit;
2611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2613 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002614 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002615 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00002616
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002617 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002619 goto exit;
2620 }
Paul Bakker5690efc2011-05-26 13:16:06 +00002621 }
2622
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02002624 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02002626 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002627 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002628
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002629 /*
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002630 * 7b. Simulate hard reset and reconnect from same port?
2631 */
2632 if( opt.reconnect_hard != 0 )
2633 {
2634 opt.reconnect_hard = 0;
2635
2636 mbedtls_printf( " . Restarting connection from same port..." );
2637 fflush( stdout );
2638
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002639#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker23699ef2019-02-26 12:36:53 +00002640 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002641#endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Becker23699ef2019-02-26 12:36:53 +00002642
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002643 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
2644 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002645 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2646 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002647 goto exit;
2648 }
2649
2650 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
2651 {
2652 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002653 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002654 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002655 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002656 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2657 -ret );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002658 goto exit;
2659 }
Hanno Becker16970d22017-10-10 15:56:37 +01002660
2661 /* For event-driven IO, wait for socket to become available */
2662 if( opt.event == 1 /* level triggered IO */ )
2663 {
2664#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002665 idle( &server_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002666#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002667 idle( &server_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002668#endif
2669 }
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002670 }
2671
2672 mbedtls_printf( " ok\n" );
2673
2674 goto send_request;
2675 }
2676
2677 /*
2678 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002679 */
2680 if( --opt.exchanges > 0 )
2681 goto send_request;
2682
2683 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002684 * 8. Done, cleanly close the connection
2685 */
2686close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarddbd23072015-09-04 10:20:17 +02002688 fflush( stdout );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002689
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002690 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002692 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002693 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 mbedtls_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02002696
2697 /*
2698 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002699 */
2700reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002701 if( opt.reconnect != 0 )
2702 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02002703 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002704
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002705 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002706
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002707#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002708 if( opt.reco_delay > 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +02002709 mbedtls_net_usleep( 1000000 * opt.reco_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002710#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 mbedtls_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002713
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002714#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckera1051b42019-02-26 11:38:29 +00002715 memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
Hanno Beckerbdf75eb2019-02-27 08:34:31 +00002716#endif /* MBEDTLS_X509_CRT_PARSE_C */
Hanno Beckera1051b42019-02-26 11:38:29 +00002717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002719 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002720 mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
2721 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002722 goto exit;
2723 }
2724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725 if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002726 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002727 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n",
2728 ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002729 goto exit;
2730 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002731
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002732 if( ( ret = mbedtls_net_connect( &server_fd,
2733 opt.server_addr, opt.server_port,
2734 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2735 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002736 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002737 mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
2738 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002739 goto exit;
2740 }
2741
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002742 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002743 ret = mbedtls_net_set_nonblock( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002744 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002745 ret = mbedtls_net_set_block( &server_fd );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002746 if( ret != 0 )
2747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002749 -ret );
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02002750 goto exit;
2751 }
2752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002754 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002755 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
Manuel Pégourié-Gonnardb3c83072017-05-16 08:50:24 +02002756 ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002757 ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002758 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002759 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
2760 -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002761 goto exit;
2762 }
2763 }
2764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02002766
2767 goto send_request;
2768 }
2769
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002770 /*
2771 * Cleanup and exit
2772 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002773exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +00002775 if( ret != 0 )
2776 {
2777 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778 mbedtls_strerror( ret, error_buf, 100 );
2779 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002780 }
2781#endif
2782
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002783 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785#if defined(MBEDTLS_X509_CRT_PARSE_C)
2786 mbedtls_x509_crt_free( &clicert );
2787 mbedtls_x509_crt_free( &cacert );
2788 mbedtls_pk_free( &pkey );
Manuel Pégourié-Gonnardcfdf8f42018-11-08 09:52:25 +01002789#if defined(MBEDTLS_USE_PSA_CRYPTO)
2790 psa_destroy_key( key_slot );
2791#endif
Paul Bakkered27a042013-04-18 22:46:23 +02002792#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 mbedtls_ssl_session_free( &saved_session );
2794 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002795 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796 mbedtls_ctr_drbg_free( &ctr_drbg );
2797 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002798
Hanno Becker3f24ea92018-11-05 13:25:17 +00002799#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
2800 defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002801 if( opt.psk_opaque != 0 )
Hanno Becker3f24ea92018-11-05 13:25:17 +00002802 {
2803 /* This is ok even if the slot hasn't been
2804 * initialized (we might have jumed here
2805 * immediately because of bad cmd line params,
2806 * for example). */
Hanno Becker1d911cd2018-11-15 13:06:09 +00002807 status = psa_destroy_key( slot );
Hanno Becker3f24ea92018-11-05 13:25:17 +00002808 if( status != PSA_SUCCESS )
2809 {
2810 mbedtls_printf( "Failed to destroy key slot %u - error was %d",
Hanno Becker1d911cd2018-11-15 13:06:09 +00002811 (unsigned) slot, (int) status );
Hanno Becker3f24ea92018-11-05 13:25:17 +00002812 if( ret == 0 )
2813 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2814 }
2815 }
2816#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED &&
2817 MBEDTLS_USE_PSA_CRYPTO */
2818
Paul Bakkercce9d772011-11-18 14:26:47 +00002819#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002821 fflush( stdout ); getchar();
2822#endif
2823
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002824 // Shell can not handle large exit numbers -> 1 for errors
2825 if( ret < 0 )
2826 ret = 1;
2827
Paul Bakker5121ce52009-01-03 21:22:43 +00002828 return( ret );
2829}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
2831 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002832 MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */