blob: 586b87119dfb9cde9fe341cfb5591a6b982cc353 [file] [log] [blame]
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001/*
2 * SSL client with options
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 Bakkerb60b95f2012-09-25 09:05:17 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb60b95f2012-09-25 09:05:17 +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 Bakkerb60b95f2012-09-25 09:05:17 +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
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020031#include <stdio.h>
Simon Butcherd3138c32016-04-27 01:26:50 +010032#include <stdlib.h>
Hanno Beckerd6d100b2019-03-30 06:27:43 +000033#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#define mbedtls_free free
Simon Butcherd3138c32016-04-27 01:26:50 +010035#define mbedtls_time time
36#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020037#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#define mbedtls_fprintf fprintf
39#define mbedtls_printf printf
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 Evans18b78c72015-02-11 14:06:19 +000043#endif
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020044
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020045#if !defined(MBEDTLS_ENTROPY_C) || \
46 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_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_SRV_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
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010056
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é-Gonnard56273da2015-05-26 12:19:45 +020065#include "mbedtls/timing.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000066
Hanno Becker5a9942e2018-11-12 17:47:48 +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 Becker5a9942e2018-11-12 17:47:48 +000070#endif
71
Rich Evans18b78c72015-02-11 14:06:19 +000072#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
Andres AG692ad842017-01-19 16:30:57 +000075#include <stdint.h>
Andres AG0b736db2017-02-08 14:05:57 +000076
77#if !defined(_MSC_VER)
Andres AG692ad842017-01-19 16:30:57 +000078#include <inttypes.h>
Andres AG0b736db2017-02-08 14:05:57 +000079#endif
Rich Evans18b78c72015-02-11 14:06:19 +000080
81#if !defined(_WIN32)
82#include <signal.h>
83#endif
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000086#include "mbedtls/ssl_cache.h"
Paul Bakker0a597072012-09-25 21:55:46 +000087#endif
88
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020089#if defined(MBEDTLS_SSL_TICKET_C)
90#include "mbedtls/ssl_ticket.h"
91#endif
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000094#include "mbedtls/ssl_cookie.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020095#endif
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000098#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker82024bf2013-07-04 11:52:32 +020099#endif
100
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200101#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO)
102#define SNI_OPTION
103#endif
104
105#if defined(_WIN32)
106#include <windows.h>
107#endif
108
Simon Butchercce68be2018-07-23 14:26:09 +0100109/* Size of memory to be allocated for the heap, when using the library's memory
110 * management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */
111#define MEMORY_HEAP_SIZE 120000
112
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100113#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200114#define DFL_SERVER_PORT "4433"
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200115#define DFL_RESPONSE_SIZE -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000116#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100117#define DFL_NBIO 0
Hanno Becker16970d22017-10-10 15:56:37 +0100118#define DFL_EVENT 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200119#define DFL_READ_TIMEOUT 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000120#define DFL_CA_FILE ""
121#define DFL_CA_PATH ""
122#define DFL_CRT_FILE ""
123#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200124#define DFL_CRT_FILE2 ""
125#define DFL_KEY_FILE2 ""
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100126#define DFL_ASYNC_OPERATIONS "-"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100127#define DFL_ASYNC_PRIVATE_DELAY1 ( -1 )
128#define DFL_ASYNC_PRIVATE_DELAY2 ( -1 )
Gilles Peskine3665f1d2018-01-05 21:22:12 +0100129#define DFL_ASYNC_PRIVATE_ERROR ( 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +0200130#define DFL_PSK ""
Hanno Becker1d911cd2018-11-15 13:06:09 +0000131#define DFL_PSK_OPAQUE 0
132#define DFL_PSK_LIST_OPAQUE 0
Paul Bakkerfbb17802013-04-17 19:10:21 +0200133#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200134#define DFL_ECJPAKE_PW NULL
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200135#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000136#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200137#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100139#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100140#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200141#define DFL_RENEGO_DELAY -2
Andres AG692ad842017-01-19 16:30:57 +0000142#define DFL_RENEGO_PERIOD ( (uint64_t)-1 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200143#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200144#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200145#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000146#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +0200147#define DFL_SHA1 -1
Hanno Beckera7d25422019-04-09 17:28:10 +0100148#define DFL_CID_ENABLED 0
149#define DFL_CID_VALUE ""
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100150#define DFL_CID_ENABLED_RENEGO -1
151#define DFL_CID_VALUE_RENEGO NULL
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100152#define DFL_AUTH_MODE -1
Janos Follath4817e272017-04-10 13:44:33 +0100153#define DFL_CERT_REQ_CA_LIST MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100155#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200157#define DFL_TICKET_TIMEOUT 86400
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100158#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100159#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100160#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200161#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100162#define DFL_CURVES NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200163#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200165#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200166#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200167#define DFL_HS_TO_MIN 0
168#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200169#define DFL_DTLS_MTU -1
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200170#define DFL_BADMAC_LIMIT -1
Hanno Beckere7675d02018-08-14 13:28:56 +0100171#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200172#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100173#define DFL_ETM -1
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200174#define DFL_CA_CALLBACK 0
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300175#define DFL_EAP_TLS 0
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200176#define DFL_REPRODUCIBLE 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000177
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200178#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
179 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
180 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
181 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
182 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
183 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
184 "07-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah</p>\r\n"
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200185
Paul Bakker8e714d72013-07-18 11:05:13 +0200186/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
187 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000188#define HTTP_RESPONSE \
189 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000190 "<h2>mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200191 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000192
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200193/*
194 * Size of the basic I/O buffer. Able to hold our default response.
195 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 * You will need to adapt the mbedtls_ssl_get_bytes_avail() test in ssl-opt.sh
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200197 * if you change this value to something outside the range <= 100 or > 500
198 */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200199#define DFL_IO_BUF_LEN 200
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_X509_CRT_PARSE_C)
202#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000203#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100204 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
205 " default: \"\" (pre-loaded)\n" \
206 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
207 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
208 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200209 " default: see note after key_file2\n" \
210 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200211 " crt_file2=%%s Your second cert and chain (in bottom to top order, top may be omitted)\n" \
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200212 " default: see note after key_file2\n" \
213 " key_file2=%%s default: see note below\n" \
214 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200215 " preloaded certificate(s) and key(s) are used if available\n" \
216 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
217 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000218#else
219#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200220 "\n" \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 " No file operations available (MBEDTLS_FS_IO not defined)\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200222 "\n"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200224#else
225#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200227
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200228#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100229#define USAGE_SSL_ASYNC \
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100230 " async_operations=%%c... d=decrypt, s=sign (default: -=off)\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100231 " async_private_delay1=%%d Asynchronous delay for key_file or preloaded key\n" \
Gilles Peskine166ce742018-04-30 10:30:49 +0200232 " async_private_delay2=%%d Asynchronous delay for key_file2 and sni\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100233 " default: -1 (not asynchronous)\n" \
Gilles Peskine60ee4ca2018-01-08 11:28:05 +0100234 " async_private_error=%%d Async callback error injection (default=0=none,\n" \
Gilles Peskinec9125722018-04-26 07:15:40 +0200235 " 1=start, 2=cancel, 3=resume, negative=first time only)"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100236#else
237#define USAGE_SSL_ASYNC ""
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200238#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100239
Hanno Beckera0e20d02019-05-15 14:03:01 +0100240#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +0100241#define USAGE_CID \
242 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
243 " default: 0 (disabled)\n" \
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100244 " 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 +0100245 " default: same as 'cid' parameter\n" \
Hanno Beckera7d25422019-04-09 17:28:10 +0100246 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100247 " default: \"\"\n" \
248 " 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 +0100249 " default: same as 'cid_val' parameter\n"
Hanno Beckera0e20d02019-05-15 14:03:01 +0100250#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +0100251#define USAGE_CID ""
Hanno Beckera0e20d02019-05-15 14:03:01 +0100252#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +0100253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100255#define USAGE_PSK_RAW \
Andrzej Kurekb274f272019-02-05 05:06:35 -0500256 " psk=%%s default: \"\" (in hex, without 0x)\n" \
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100257 " psk_list=%%s default: \"\"\n" \
Andrzej Kurekb274f272019-02-05 05:06:35 -0500258 " A list of (PSK identity, PSK value) pairs.\n" \
259 " The PSK values are in hex, without 0x.\n" \
260 " id1,psk1[,id2,psk2[,...]]\n" \
261 " psk_identity=%%s default: \"Client_identity\"\n"
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100262#if defined(MBEDTLS_USE_PSA_CRYPTO)
263#define USAGE_PSK_SLOT \
Hanno Becker1d911cd2018-11-15 13:06:09 +0000264 " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
265 " Enable this to store the PSK configured through command line\n" \
266 " parameter `psk` in a PSA-based key slot.\n" \
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100267 " Note: Currently only supported in conjunction with\n" \
268 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
269 " to force a particular PSK-only ciphersuite.\n" \
270 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
271 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
272 " with prepopulated key slots instead of importing raw key material.\n" \
Hanno Becker1d911cd2018-11-15 13:06:09 +0000273 " psk_list_opaque=%%d default: 0 (don't use opaque dynamic PSKs)\n" \
274 " Enable this to store the list of dynamically chosen PSKs configured\n" \
275 " through the command line parameter `psk_list` in PSA-based key slots.\n" \
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100276 " Note: Currently only supported in conjunction with\n" \
277 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
278 " to force a particular PSK-only ciphersuite.\n" \
279 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
280 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
281 " with prepopulated key slots instead of importing raw key material.\n"
282#else
283#define USAGE_PSK_SLOT ""
284#endif /* MBEDTLS_USE_PSA_CRYPTO */
285#define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
Paul Bakkered27a042013-04-18 22:46:23 +0200286#else
287#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200289#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
290#define USAGE_CA_CALLBACK \
291 " ca_callback=%%d default: 0 (disabled)\n" \
292 " Enable this to use the trusted certificate callback function\n"
293#else
294#define USAGE_CA_CALLBACK ""
295#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200297#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100298 " tickets=%%d default: 1 (enabled)\n" \
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200299 " ticket_timeout=%%d default: 86400 (one day)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200300#else
301#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200303
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300304#if defined(MBEDTLS_SSL_EXPORT_KEYS)
305#define USAGE_EAP_TLS \
306 " eap_tls=%%d default: 0 (disabled)\n"
307#else
308#define USAGE_EAP_TLS ""
309#endif /* MBEDTLS_SSL_EXPORT_KEYS */
310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100312#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100313 " cache_max=%%d default: cache default (50)\n" \
314 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100315#else
316#define USAGE_CACHE ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#endif /* MBEDTLS_SSL_CACHE_C */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100318
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200319#if defined(SNI_OPTION)
Ron Eldor80d04192019-04-04 15:02:01 +0300320#if defined(MBEDTLS_X509_CRL_PARSE_C)
321#define SNI_CRL ",crl"
322#else
323#define SNI_CRL ""
324#endif
325
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100326#define USAGE_SNI \
Ron Eldor80d04192019-04-04 15:02:01 +0300327 " sni=%%s name1,cert1,key1,ca1"SNI_CRL",auth1[,...]\n" \
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200328 " default: disabled\n"
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100329#else
330#define USAGE_SNI ""
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200331#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200334#define USAGE_MAX_FRAG_LEN \
335 " max_frag_len=%%d default: 16384 (tls default)\n" \
336 " options: 512, 1024, 2048, 4096\n"
337#else
338#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100342#define USAGE_TRUNC_HMAC \
343 " trunc_hmac=%%d default: library default\n"
344#else
345#define USAGE_TRUNC_HMAC ""
346#endif
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200349#define USAGE_ALPN \
350 " alpn=%%s default: \"\" (disabled)\n" \
351 " example: spdy/1,http/1.1\n"
352#else
353#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200357#define USAGE_COOKIES \
358 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200359 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200360#else
361#define USAGE_COOKIES ""
362#endif
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200365#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200366 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200367#else
368#define USAGE_ANTI_REPLAY ""
369#endif
370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200372#define USAGE_BADMAC_LIMIT \
373 " badmac_limit=%%d default: (library default: disabled)\n"
374#else
375#define USAGE_BADMAC_LIMIT ""
376#endif
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200379#define USAGE_DTLS \
380 " dtls=%%d default: 0 (TLS)\n" \
381 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200382 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Beckere7675d02018-08-14 13:28:56 +0100383 " mtu=%%d default: (library default: unlimited)\n" \
384 " dgram_packing=%%d default: 1 (allowed)\n" \
385 " allow or forbid packing of multiple\n" \
386 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200387#else
388#define USAGE_DTLS ""
389#endif
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200392#define USAGE_EMS \
393 " extended_ms=0/1 default: (library default: on)\n"
394#else
395#define USAGE_EMS ""
396#endif
397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100399#define USAGE_ETM \
400 " etm=0/1 default: (library default: on)\n"
401#else
402#define USAGE_ETM ""
403#endif
404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100406#define USAGE_RENEGO \
407 " renegotiation=%%d default: 0 (disabled)\n" \
408 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100409 " renego_delay=%%d default: -2 (library default)\n" \
Andres AG692ad842017-01-19 16:30:57 +0000410 " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100411#else
412#define USAGE_RENEGO ""
413#endif
414
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200415#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
416#define USAGE_ECJPAKE \
417 " ecjpake_pw=%%s default: none (disabled)\n"
418#else
419#define USAGE_ECJPAKE ""
420#endif
421
Hanno Beckere6706e62017-05-15 16:05:15 +0100422#if defined(MBEDTLS_ECP_C)
423#define USAGE_CURVES \
424 " curves=a,b,c,d default: \"default\" (library default)\n" \
425 " example: \"secp521r1,brainpoolP512r1\"\n" \
426 " - use \"none\" for empty list\n" \
427 " - see mbedtls_ecp_curve_list()\n" \
428 " for acceptable curve names\n"
429#else
430#define USAGE_CURVES ""
431#endif
432
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000433#define USAGE \
434 "\n usage: ssl_server2 param=<>...\n" \
435 "\n acceptable parameters:\n" \
Ron Eldor71f68c42017-09-26 11:29:11 +0300436 " server_addr=%%s default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000437 " server_port=%%d default: 4433\n" \
438 " debug_level=%%d default: 0 (disabled)\n" \
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200439 " buffer_size=%%d default: 200 \n" \
440 " (minimum: 1, max: 16385)\n" \
441 " response_size=%%d default: about 152 (basic response)\n" \
442 " (minimum: 0, max: 16384)\n" \
443 " increases buffer_size if bigger\n"\
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100444 " nbio=%%d default: 0 (blocking I/O)\n" \
445 " options: 1 (non-blocking), 2 (added delays)\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100446 " event=%%d default: 0 (loop)\n" \
447 " options: 1 (level-triggered, implies nbio=1),\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200448 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100449 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200450 USAGE_DTLS \
451 USAGE_COOKIES \
452 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200453 USAGE_BADMAC_LIMIT \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200454 "\n" \
Manuel Pégourié-Gonnardee6139c2015-05-07 10:18:26 +0100455 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100456 " options: none, optional, required\n" \
Janos Follath4817e272017-04-10 13:44:33 +0100457 " cert_req_ca_list=%%d default: 1 (send ca list)\n" \
458 " options: 1 (send ca list), 0 (don't send)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000459 USAGE_IO \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100460 USAGE_SSL_ASYNC \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100461 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100462 "\n" \
463 USAGE_PSK \
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200464 USAGE_CA_CALLBACK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200465 USAGE_ECJPAKE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100466 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100467 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100468 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200469 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200470 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100471 USAGE_TICKETS \
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300472 USAGE_EAP_TLS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100473 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100474 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100475 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200476 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200477 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100478 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100479 USAGE_CURVES \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100480 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100481 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200482 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200483 " min_version=%%s default: (library default: tls1)\n" \
484 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200485 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100486 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200487 "\n" \
488 " version_suites=a,b,c,d per-version ciphersuites\n" \
489 " in order from ssl3 to tls1_2\n" \
490 " default: all enabled\n" \
491 " force_ciphersuite=<name> default: all enabled\n" \
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100492 " query_config=<name> return 0 if the specified\n" \
493 " configuration macro is defined and 1\n" \
494 " otherwise. The expansion of the macro\n" \
495 " is printed if it is defined\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000496 " acceptable ciphersuite names:\n"
497
Andres AG692ad842017-01-19 16:30:57 +0000498
Hanno Becker8651a432017-06-09 16:13:22 +0100499#define ALPN_LIST_SIZE 10
500#define CURVE_LIST_SIZE 20
501
Andres AG692ad842017-01-19 16:30:57 +0000502#define PUT_UINT64_BE(out_be,in_le,i) \
503{ \
504 (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \
505 (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \
506 (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \
507 (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \
508 (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \
509 (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \
510 (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \
511 (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \
512}
513
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500514#if defined(MBEDTLS_CHECK_PARAMS)
515#include "mbedtls/platform_util.h"
516void mbedtls_param_failed( const char *failure_condition,
517 const char *file,
518 int line )
519{
520 mbedtls_printf( "%s:%i: Input param failed - %s\n",
521 file, line, failure_condition );
522 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
523}
524#endif
525
Rich Evans85b05ec2015-02-12 11:37:29 +0000526/*
527 * global options
528 */
529struct options
530{
531 const char *server_addr; /* address on which the ssl service runs */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200532 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000533 int debug_level; /* level of debugging */
534 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100535 int event; /* loop or event-driven IO? level or edge triggered? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200537 int response_size; /* pad response with header to requested size */
538 uint16_t buffer_size; /* IO buffer size */
Rich Evans85b05ec2015-02-12 11:37:29 +0000539 const char *ca_file; /* the file with the CA certificate(s) */
540 const char *ca_path; /* the path with the CA certificate(s) reside */
541 const char *crt_file; /* the file with the server certificate */
542 const char *key_file; /* the file with the server key */
543 const char *crt_file2; /* the file with the 2nd server certificate */
544 const char *key_file2; /* the file with the 2nd server key */
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100545 const char *async_operations; /* supported SSL asynchronous operations */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100546 int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
547 int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
548 int async_private_error; /* inject error in async private callback */
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100549#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000550 int psk_opaque;
551 int psk_list_opaque;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100552#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200553#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000554 int ca_callback; /* Use callback for trusted certificate list */
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200555#endif
Rich Evans85b05ec2015-02-12 11:37:29 +0000556 const char *psk; /* the pre-shared key */
557 const char *psk_identity; /* the pre-shared key identity */
558 char *psk_list; /* list of PSK id/key pairs for callback */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200559 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000560 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
561 const char *version_suites; /* per-version ciphersuites */
562 int renegotiation; /* enable / disable renegotiation */
563 int allow_legacy; /* allow legacy renegotiation */
564 int renegotiate; /* attempt renegotiation? */
565 int renego_delay; /* delay before enforcing renegotiation */
Andres AG692ad842017-01-19 16:30:57 +0000566 uint64_t renego_period; /* period for automatic renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000567 int exchanges; /* number of data exchanges */
568 int min_version; /* minimum protocol version accepted */
569 int max_version; /* maximum protocol version accepted */
570 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200571 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000572 int auth_mode; /* verify mode for connection */
Janos Follath4817e272017-04-10 13:44:33 +0100573 int cert_req_ca_list; /* should we send the CA list? */
Rich Evans85b05ec2015-02-12 11:37:29 +0000574 unsigned char mfl_code; /* code for maximum fragment length */
575 int trunc_hmac; /* accept truncated hmac? */
576 int tickets; /* enable / disable session tickets */
577 int ticket_timeout; /* session ticket lifetime */
578 int cache_max; /* max number of session cache entries */
579 int cache_timeout; /* expiration delay of session cache entries */
580 char *sni; /* string describing sni information */
Hanno Beckere6706e62017-05-15 16:05:15 +0100581 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000582 const char *alpn_string; /* ALPN supported protocols */
583 const char *dhm_file; /* the file with the DH parameters */
584 int extended_ms; /* allow negotiation of extended MS? */
585 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000586 int transport; /* TLS or DTLS? */
587 int cookies; /* Use cookies for DTLS? -1 to break them */
588 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
589 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
590 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200591 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Hanno Beckere7675d02018-08-14 13:28:56 +0100592 int dgram_packing; /* allow/forbid datagram packing */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000593 int badmac_limit; /* Limit of records with bad MAC */
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300594 int eap_tls; /* derive EAP-TLS keying material? */
Hanno Beckera7d25422019-04-09 17:28:10 +0100595 int cid_enabled; /* whether to use the CID extension or not */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100596 int cid_enabled_renego; /* whether to use the CID extension or not
597 * during renegotiation */
Hanno Beckera7d25422019-04-09 17:28:10 +0100598 const char *cid_val; /* the CID to use for incoming messages */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +0100599 const char *cid_val_renego; /* the CID to use for incoming messages
600 * after renegotiation */
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200601 int reproducible; /* make communication reproducible */
Rich Evans85b05ec2015-02-12 11:37:29 +0000602} opt;
603
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100604int query_config( const char *config );
605
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300606#if defined(MBEDTLS_SSL_EXPORT_KEYS)
607typedef struct eap_tls_keys
608{
609 unsigned char master_secret[48];
610 unsigned char randbytes[64];
Ron Eldor51d3ab52019-05-12 14:54:30 +0300611 mbedtls_tls_prf_types tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300612} eap_tls_keys;
613
614static int eap_tls_key_derivation ( void *p_expkey,
615 const unsigned char *ms,
616 const unsigned char *kb,
617 size_t maclen,
618 size_t keylen,
619 size_t ivlen,
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300620 unsigned char client_random[32],
Ron Eldor51d3ab52019-05-12 14:54:30 +0300621 unsigned char server_random[32],
622 mbedtls_tls_prf_types tls_prf_type )
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300623{
624 eap_tls_keys *keys = (eap_tls_keys *)p_expkey;
625
626 ( ( void ) kb );
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300627 memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) );
628 memcpy( keys->randbytes, client_random, 32 );
629 memcpy( keys->randbytes + 32, server_random, 32 );
Ron Eldor51d3ab52019-05-12 14:54:30 +0300630 keys->tls_prf_type = tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300631
Ron Eldorf75e2522019-05-14 20:38:49 +0300632 if( opt.debug_level > 2 )
633 {
Ron Eldor801faf02019-05-15 17:45:24 +0300634 mbedtls_printf("exported maclen is %u\n", (unsigned)maclen);
635 mbedtls_printf("exported keylen is %u\n", (unsigned)keylen);
636 mbedtls_printf("exported ivlen is %u\n", (unsigned)ivlen);
Ron Eldorf75e2522019-05-14 20:38:49 +0300637 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300638 return( 0 );
639}
640#endif
641
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200642static void my_debug( void *ctx, int level,
643 const char *file, int line,
644 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000645{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200646 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000647
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200648 /* Extract basename from file */
649 for( p = basename = file; *p != '\0'; p++ )
650 if( *p == '/' || *p == '\\' )
651 basename = p + 1;
652
653 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000654 fflush( (FILE *) ctx );
655}
656
Philippe Antoineaa4d1522019-06-06 21:24:31 +0200657mbedtls_time_t dummy_constant_time( mbedtls_time_t* time )
658{
659 (void) time;
660 return 0x5af2a056;
661}
662
663int dummy_random( void *p_rng, unsigned char *output, size_t output_len )
664{
665 int ret;
666 size_t i;
667
668 //use mbedtls_ctr_drbg_random to find bugs in it
669 ret = mbedtls_ctr_drbg_random(p_rng, output, output_len);
670 for (i=0; i<output_len; i++) {
671 //replace result with pseudo random
672 output[i] = (unsigned char) rand();
673 }
674 return( ret );
675}
676
677int dummy_entropy( void *data, unsigned char *output, size_t len )
678{
679 size_t i;
680 (void) data;
681
682 //use mbedtls_entropy_func to find bugs in it
683 //test performance impact of entropy
684 //ret = mbedtls_entropy_func(data, output, len);
685 for (i=0; i<len; i++) {
686 //replace result with pseudo random
687 output[i] = (unsigned char) rand();
688 }
689 return( 0 );
690}
691
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200692#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000693int ca_callback( void *data, mbedtls_x509_crt const *child,
694 mbedtls_x509_crt **candidates)
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200695{
Hanno Beckercbb59032019-03-28 14:14:22 +0000696 int ret = 0;
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200697 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +0000698 mbedtls_x509_crt *first;
699
700 /* This is a test-only implementation of the CA callback
701 * which always returns the entire list of trusted certificates.
702 * Production implementations managing a large number of CAs
703 * should use an efficient presentation and lookup for the
704 * set of trusted certificates (such as a hashtable) and only
705 * return those trusted certificates which satisfy basic
706 * parental checks, such as the matching of child `Issuer`
707 * and parent `Subject` field. */
708 ((void) child);
709
710 first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
711 if( first == NULL )
712 {
713 ret = -1;
714 goto exit;
715 }
716 mbedtls_x509_crt_init( first );
717
718 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
719 {
720 ret = -1;
721 goto exit;
722 }
723
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200724 while( ca->next != NULL )
725 {
726 ca = ca->next;
Hanno Beckercbb59032019-03-28 14:14:22 +0000727 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
728 {
729 ret = -1;
730 goto exit;
731 }
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200732 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000733
734exit:
735
736 if( ret != 0 )
737 {
738 mbedtls_x509_crt_free( first );
739 mbedtls_free( first );
740 first = NULL;
741 }
742
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200743 *candidates = first;
Hanno Beckercbb59032019-03-28 14:14:22 +0000744 return( ret );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200745}
746#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
747
Rich Evans85b05ec2015-02-12 11:37:29 +0000748/*
749 * Test recv/send functions that make sure each try returns
750 * WANT_READ/WANT_WRITE at least once before sucesseding
751 */
752static int my_recv( void *ctx, unsigned char *buf, size_t len )
753{
754 static int first_try = 1;
755 int ret;
756
757 if( first_try )
758 {
759 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100760 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000761 }
762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100764 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000765 first_try = 1; /* Next call will be a new operation */
766 return( ret );
767}
768
769static int my_send( void *ctx, const unsigned char *buf, size_t len )
770{
771 static int first_try = 1;
772 int ret;
773
774 if( first_try )
775 {
776 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100777 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000778 }
779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100781 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000782 first_try = 1; /* Next call will be a new operation */
783 return( ret );
784}
785
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200786/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200787 * Return authmode from string, or -1 on error
788 */
789static int get_auth_mode( const char *s )
790{
791 if( strcmp( s, "none" ) == 0 )
792 return( MBEDTLS_SSL_VERIFY_NONE );
793 if( strcmp( s, "optional" ) == 0 )
794 return( MBEDTLS_SSL_VERIFY_OPTIONAL );
795 if( strcmp( s, "required" ) == 0 )
796 return( MBEDTLS_SSL_VERIFY_REQUIRED );
797
798 return( -1 );
799}
800
801/*
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200802 * Used by sni_parse and psk_parse to handle coma-separated lists
803 */
804#define GET_ITEM( dst ) \
Hanno Becker1eeca412018-10-15 12:01:35 +0100805 do \
806 { \
807 (dst) = p; \
808 while( *p != ',' ) \
809 if( ++p > end ) \
810 goto error; \
811 *p++ = '\0'; \
812 } while( 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200813
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200814#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100815typedef struct _sni_entry sni_entry;
816
817struct _sni_entry {
818 const char *name;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 mbedtls_x509_crt *cert;
820 mbedtls_pk_context *key;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200821 mbedtls_x509_crt* ca;
822 mbedtls_x509_crl* crl;
823 int authmode;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100824 sni_entry *next;
825};
826
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100827void sni_free( sni_entry *head )
828{
829 sni_entry *cur = head, *next;
830
831 while( cur != NULL )
832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_x509_crt_free( cur->cert );
834 mbedtls_free( cur->cert );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_pk_free( cur->key );
837 mbedtls_free( cur->key );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100838
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200839 mbedtls_x509_crt_free( cur->ca );
840 mbedtls_free( cur->ca );
Ron Eldor80d04192019-04-04 15:02:01 +0300841#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200842 mbedtls_x509_crl_free( cur->crl );
843 mbedtls_free( cur->crl );
Ron Eldor80d04192019-04-04 15:02:01 +0300844#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100845 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 mbedtls_free( cur );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100847 cur = next;
848 }
849}
850
851/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200852 * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
853 * into a usable sni_entry list. For ca1, crl1, auth1, the special value
854 * '-' means unset. If ca1 is unset, then crl1 is ignored too.
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000855 *
856 * Modifies the input string! This is not production quality!
857 */
858sni_entry *sni_parse( char *sni_string )
859{
860 sni_entry *cur = NULL, *new = NULL;
861 char *p = sni_string;
862 char *end = p;
Ron Eldor80d04192019-04-04 15:02:01 +0300863 char *crt_file, *key_file, *ca_file, *auth_str;
864#if defined(MBEDTLS_X509_CRL_PARSE_C)
865 char *crl_file;
866#endif
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000867
868 while( *end != '\0' )
869 ++end;
870 *end = ',';
871
872 while( p <= end )
873 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200874 if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000875 {
876 sni_free( cur );
877 return( NULL );
878 }
879
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200880 GET_ITEM( new->name );
881 GET_ITEM( crt_file );
882 GET_ITEM( key_file );
883 GET_ITEM( ca_file );
Ron Eldor80d04192019-04-04 15:02:01 +0300884#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200885 GET_ITEM( crl_file );
Ron Eldor80d04192019-04-04 15:02:01 +0300886#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200887 GET_ITEM( auth_str );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000888
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200889 if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
890 ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200891 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 mbedtls_x509_crt_init( new->cert );
894 mbedtls_pk_init( new->key );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
897 mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000898 goto error;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200899
900 if( strcmp( ca_file, "-" ) != 0 )
901 {
902 if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
903 goto error;
904
905 mbedtls_x509_crt_init( new->ca );
906
907 if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
908 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000909 }
910
Ron Eldor80d04192019-04-04 15:02:01 +0300911#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200912 if( strcmp( crl_file, "-" ) != 0 )
913 {
914 if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
915 goto error;
916
917 mbedtls_x509_crl_init( new->crl );
918
919 if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
920 goto error;
921 }
Ron Eldor80d04192019-04-04 15:02:01 +0300922#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200923
924 if( strcmp( auth_str, "-" ) != 0 )
925 {
926 if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
927 goto error;
928 }
929 else
930 new->authmode = DFL_AUTH_MODE;
931
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000932 new->next = cur;
933 cur = new;
934 }
935
936 return( cur );
937
938error:
939 sni_free( new );
940 sni_free( cur );
941 return( NULL );
942}
943
944/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100945 * SNI callback.
946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100948 const unsigned char *name, size_t name_len )
949{
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200950 const sni_entry *cur = (const sni_entry *) p_info;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100951
952 while( cur != NULL )
953 {
954 if( name_len == strlen( cur->name ) &&
955 memcmp( name, cur->name, name_len ) == 0 )
956 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200957 if( cur->ca != NULL )
958 mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
959
960 if( cur->authmode != DFL_AUTH_MODE )
961 mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
962
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200963 return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100964 }
965
966 cur = cur->next;
967 }
968
969 return( -1 );
970}
971
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200972#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100973
Hanno Becker554b6ea2019-04-30 14:18:06 +0100974#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) || \
Hanno Beckera0e20d02019-05-15 14:03:01 +0100975 defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200976
Hanno Becker1eeca412018-10-15 12:01:35 +0100977#define HEX2NUM( c ) \
978 do \
979 { \
980 if( (c) >= '0' && (c) <= '9' ) \
981 (c) -= '0'; \
982 else if( (c) >= 'a' && (c) <= 'f' ) \
983 (c) -= 'a' - 10; \
984 else if( (c) >= 'A' && (c) <= 'F' ) \
985 (c) -= 'A' - 10; \
986 else \
987 return( -1 ); \
988 } while( 0 )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200989
990/*
991 * Convert a hex string to bytes.
992 * Return 0 on success, -1 on error.
993 */
994int unhexify( unsigned char *output, const char *input, size_t *olen )
995{
996 unsigned char c;
997 size_t j;
998
999 *olen = strlen( input );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 if( *olen % 2 != 0 || *olen / 2 > MBEDTLS_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001001 return( -1 );
1002 *olen /= 2;
1003
1004 for( j = 0; j < *olen * 2; j += 2 )
1005 {
1006 c = input[j];
1007 HEX2NUM( c );
1008 output[ j / 2 ] = c << 4;
1009
1010 c = input[j + 1];
1011 HEX2NUM( c );
1012 output[ j / 2 ] |= c;
1013 }
1014
1015 return( 0 );
1016}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001017
Hanno Becker554b6ea2019-04-30 14:18:06 +01001018#endif
1019
1020#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1021
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001022typedef struct _psk_entry psk_entry;
1023
1024struct _psk_entry
1025{
1026 const char *name;
1027 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 unsigned char key[MBEDTLS_PSK_MAX_LEN];
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001029#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001030 psa_key_handle_t slot;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001031#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001032 psk_entry *next;
1033};
1034
1035/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001036 * Free a list of psk_entry's
1037 */
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001038int psk_free( psk_entry *head )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001039{
1040 psk_entry *next;
1041
1042 while( head != NULL )
1043 {
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001044#if defined(MBEDTLS_USE_PSA_CRYPTO)
1045 psa_status_t status;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001046 psa_key_handle_t const slot = head->slot;
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001047
1048 if( slot != 0 )
1049 {
1050 status = psa_destroy_key( slot );
1051 if( status != PSA_SUCCESS )
1052 return( status );
1053 }
1054#endif /* MBEDTLS_USE_PSA_CRYPTO */
1055
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001056 next = head->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_free( head );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001058 head = next;
1059 }
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00001060
1061 return( 0 );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001062}
1063
1064/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001065 * Parse a string of pairs name1,key1[,name2,key2[,...]]
1066 * into a usable psk_entry list.
1067 *
1068 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001069 */
1070psk_entry *psk_parse( char *psk_string )
1071{
1072 psk_entry *cur = NULL, *new = NULL;
1073 char *p = psk_string;
1074 char *end = p;
1075 char *key_hex;
1076
1077 while( *end != '\0' )
1078 ++end;
1079 *end = ',';
1080
1081 while( p <= end )
1082 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001083 if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +00001084 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001085
1086 memset( new, 0, sizeof( psk_entry ) );
1087
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +02001088 GET_ITEM( new->name );
1089 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001090
1091 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001092 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001093
1094 new->next = cur;
1095 cur = new;
1096 }
1097
1098 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001099
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001100error:
1101 psk_free( new );
1102 psk_free( cur );
1103 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001104}
1105
1106/*
1107 * PSK callback
1108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001110 const unsigned char *name, size_t name_len )
1111{
1112 psk_entry *cur = (psk_entry *) p_info;
1113
1114 while( cur != NULL )
1115 {
1116 if( name_len == strlen( cur->name ) &&
1117 memcmp( name, cur->name, name_len ) == 0 )
1118 {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001119#if defined(MBEDTLS_USE_PSA_CRYPTO)
1120 if( cur->slot != 0 )
1121 return( mbedtls_ssl_set_hs_psk_opaque( ssl, cur->slot ) );
1122 else
1123#endif
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001124 return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001125 }
1126
1127 cur = cur->next;
1128 }
1129
1130 return( -1 );
1131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001133
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001134static mbedtls_net_context listen_fd, client_fd;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +02001135
1136/* Interruption handler to ensure clean exit (for valgrind testing) */
1137#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001138static int received_sigterm = 0;
1139void term_handler( int sig )
1140{
1141 ((void) sig);
1142 received_sigterm = 1;
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001143 mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
1144 mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001145}
Paul Bakkerc1283d32014-08-18 11:05:51 +02001146#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001147
Gilles Peskine682df092017-05-11 19:01:11 +02001148#if defined(MBEDTLS_X509_CRT_PARSE_C)
1149static int ssl_sig_hashes_for_test[] = {
1150#if defined(MBEDTLS_SHA512_C)
1151 MBEDTLS_MD_SHA512,
1152 MBEDTLS_MD_SHA384,
1153#endif
1154#if defined(MBEDTLS_SHA256_C)
1155 MBEDTLS_MD_SHA256,
1156 MBEDTLS_MD_SHA224,
1157#endif
1158#if defined(MBEDTLS_SHA1_C)
1159 /* Allow SHA-1 as we use it extensively in tests. */
1160 MBEDTLS_MD_SHA1,
1161#endif
1162 MBEDTLS_MD_NONE
1163};
1164#endif /* MBEDTLS_X509_CRT_PARSE_C */
1165
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02001166/** Return true if \p ret is a status code indicating that there is an
1167 * operation in progress on an SSL connection, and false if it indicates
1168 * success or a fatal error.
1169 *
1170 * The possible operations in progress are:
1171 *
1172 * - A read, when the SSL input buffer does not contain a full message.
1173 * - A write, when the SSL output buffer contains some data that has not
1174 * been sent over the network yet.
1175 * - An asynchronous callback that has not completed yet. */
1176static int mbedtls_status_is_ssl_in_progress( int ret )
1177{
1178 return( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1179 ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
1180 ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1181}
1182
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001183#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001184typedef struct
1185{
Gilles Peskine44817442018-06-13 18:09:28 +02001186 mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
1187 mbedtls_pk_context *pk; /*!< Private key */
1188 unsigned delay; /*!< Number of resume steps to go through */
1189 unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001190} ssl_async_key_slot_t;
1191
1192typedef enum {
Gilles Peskinead28bf02018-04-26 00:19:16 +02001193 SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
1194 SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
1195 SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start */
1196 SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
Gilles Peskinec9125722018-04-26 07:15:40 +02001197#define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001198} ssl_async_inject_error_t;
1199
1200typedef struct
1201{
Gilles Peskinee2479892018-06-13 18:06:51 +02001202 ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001203 size_t slots_used;
1204 ssl_async_inject_error_t inject_error;
1205 int (*f_rng)(void *, unsigned char *, size_t);
1206 void *p_rng;
1207} ssl_async_key_context_t;
1208
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001209int ssl_async_set_key( ssl_async_key_context_t *ctx,
Gilles Peskine44817442018-06-13 18:09:28 +02001210 mbedtls_x509_crt *cert,
1211 mbedtls_pk_context *pk,
1212 int pk_take_ownership,
1213 unsigned delay )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001214{
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001215 if( ctx->slots_used >= sizeof( ctx->slots ) / sizeof( *ctx->slots ) )
1216 return( -1 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001217 ctx->slots[ctx->slots_used].cert = cert;
1218 ctx->slots[ctx->slots_used].pk = pk;
1219 ctx->slots[ctx->slots_used].delay = delay;
Gilles Peskine44817442018-06-13 18:09:28 +02001220 ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001221 ++ctx->slots_used;
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001222 return( 0 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001223}
1224
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001225#define SSL_ASYNC_INPUT_MAX_SIZE 512
Gilles Peskined3268832018-04-26 06:23:59 +02001226
1227typedef enum
1228{
1229 ASYNC_OP_SIGN,
1230 ASYNC_OP_DECRYPT,
1231} ssl_async_operation_type_t;
1232/* Note that the enum above and the array below need to be kept in sync!
1233 * `ssl_async_operation_names[op]` is the name of op for each value `op`
1234 * of type `ssl_async_operation_type_t`. */
1235static const char *const ssl_async_operation_names[] =
1236{
1237 "sign",
1238 "decrypt",
1239};
1240
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001241typedef struct
1242{
Gilles Peskine6331d782018-04-26 13:27:43 +02001243 unsigned slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001244 ssl_async_operation_type_t operation_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001245 mbedtls_md_type_t md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001246 unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
1247 size_t input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001248 unsigned remaining_delay;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001249} ssl_async_operation_context_t;
1250
Gilles Peskine8f97af72018-04-26 11:46:10 +02001251static int ssl_async_start( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001252 mbedtls_x509_crt *cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001253 ssl_async_operation_type_t op_type,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001254 mbedtls_md_type_t md_alg,
1255 const unsigned char *input,
1256 size_t input_len )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001257{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001258 ssl_async_key_context_t *config_data =
1259 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskine6331d782018-04-26 13:27:43 +02001260 unsigned slot;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001261 ssl_async_operation_context_t *ctx = NULL;
Gilles Peskined3268832018-04-26 06:23:59 +02001262 const char *op_name = ssl_async_operation_names[op_type];
Gilles Peskinef1127252018-04-24 13:05:39 +02001263
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001264 {
1265 char dn[100];
Gilles Peskined5d983e2018-06-15 14:05:10 +02001266 if( mbedtls_x509_dn_gets( dn, sizeof( dn ), &cert->subject ) > 0 )
1267 mbedtls_printf( "Async %s callback: looking for DN=%s\n",
1268 op_name, dn );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001269 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001270
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001271 /* Look for a private key that matches the public key in cert.
1272 * Since this test code has the private key inside Mbed TLS,
1273 * we call mbedtls_pk_check_pair to match a private key with the
1274 * public key. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001275 for( slot = 0; slot < config_data->slots_used; slot++ )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001276 {
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001277 if( mbedtls_pk_check_pair( &cert->pk,
1278 config_data->slots[slot].pk ) == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001279 break;
1280 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001281 if( slot == config_data->slots_used )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001282 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001283 mbedtls_printf( "Async %s callback: no key matches this certificate.\n",
1284 op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001285 return( MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH );
1286 }
Gilles Peskine6331d782018-04-26 13:27:43 +02001287 mbedtls_printf( "Async %s callback: using key slot %u, delay=%u.\n",
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001288 op_name, slot, config_data->slots[slot].delay );
Gilles Peskinef1127252018-04-24 13:05:39 +02001289
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001290 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001291 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001292 mbedtls_printf( "Async %s callback: injected error\n", op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001293 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1294 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001295
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001296 if( input_len > SSL_ASYNC_INPUT_MAX_SIZE )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001297 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Gilles Peskinef1127252018-04-24 13:05:39 +02001298
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001299 ctx = mbedtls_calloc( 1, sizeof( *ctx ) );
1300 if( ctx == NULL )
1301 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1302 ctx->slot = slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001303 ctx->operation_type = op_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001304 ctx->md_alg = md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001305 memcpy( ctx->input, input, input_len );
1306 ctx->input_len = input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001307 ctx->remaining_delay = config_data->slots[slot].delay;
Gilles Peskinea668c602018-04-30 11:54:39 +02001308 mbedtls_ssl_set_async_operation_data( ssl, ctx );
Gilles Peskinef1127252018-04-24 13:05:39 +02001309
Gilles Peskineceb541b2018-04-26 06:30:45 +02001310 if( ctx->remaining_delay == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001311 return( 0 );
1312 else
1313 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1314}
1315
Gilles Peskine8f97af72018-04-26 11:46:10 +02001316static int ssl_async_sign( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001317 mbedtls_x509_crt *cert,
1318 mbedtls_md_type_t md_alg,
1319 const unsigned char *hash,
1320 size_t hash_len )
1321{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001322 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001323 ASYNC_OP_SIGN, md_alg,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001324 hash, hash_len ) );
1325}
1326
Gilles Peskine8f97af72018-04-26 11:46:10 +02001327static int ssl_async_decrypt( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001328 mbedtls_x509_crt *cert,
1329 const unsigned char *input,
1330 size_t input_len )
1331{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001332 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001333 ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001334 input, input_len ) );
1335}
1336
Gilles Peskine8f97af72018-04-26 11:46:10 +02001337static int ssl_async_resume( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001338 unsigned char *output,
1339 size_t *output_len,
1340 size_t output_size )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001341{
Gilles Peskinea668c602018-04-30 11:54:39 +02001342 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine8f97af72018-04-26 11:46:10 +02001343 ssl_async_key_context_t *config_data =
1344 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001345 ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001346 int ret;
Gilles Peskinef5a99962018-04-30 16:37:23 +02001347 const char *op_name;
Gilles Peskinef1127252018-04-24 13:05:39 +02001348
Gilles Peskineceb541b2018-04-26 06:30:45 +02001349 if( ctx->remaining_delay > 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001350 {
Gilles Peskineceb541b2018-04-26 06:30:45 +02001351 --ctx->remaining_delay;
Gilles Peskine6331d782018-04-26 13:27:43 +02001352 mbedtls_printf( "Async resume (slot %u): call %u more times.\n",
Gilles Peskineceb541b2018-04-26 06:30:45 +02001353 ctx->slot, ctx->remaining_delay );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001354 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1355 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001356
Gilles Peskined3268832018-04-26 06:23:59 +02001357 switch( ctx->operation_type )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001358 {
Gilles Peskined3268832018-04-26 06:23:59 +02001359 case ASYNC_OP_DECRYPT:
Gilles Peskined3268832018-04-26 06:23:59 +02001360 ret = mbedtls_pk_decrypt( key_slot->pk,
1361 ctx->input, ctx->input_len,
1362 output, output_len, output_size,
1363 config_data->f_rng, config_data->p_rng );
1364 break;
1365 case ASYNC_OP_SIGN:
Gilles Peskined3268832018-04-26 06:23:59 +02001366 ret = mbedtls_pk_sign( key_slot->pk,
1367 ctx->md_alg,
1368 ctx->input, ctx->input_len,
1369 output, output_len,
1370 config_data->f_rng, config_data->p_rng );
1371 break;
1372 default:
Gilles Peskine6331d782018-04-26 13:27:43 +02001373 mbedtls_printf( "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
Gilles Peskined3268832018-04-26 06:23:59 +02001374 ctx->slot, (long) ctx->operation_type );
Gilles Peskine44817442018-06-13 18:09:28 +02001375 mbedtls_free( ctx );
Gilles Peskined3268832018-04-26 06:23:59 +02001376 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1377 break;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001378 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001379
Gilles Peskinef5a99962018-04-30 16:37:23 +02001380 op_name = ssl_async_operation_names[ctx->operation_type];
1381
Gilles Peskinec9125722018-04-26 07:15:40 +02001382 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001383 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001384 mbedtls_printf( "Async resume callback: %s done but injected error\n",
1385 op_name );
Gilles Peskine2636fad2018-06-12 14:17:39 +02001386 mbedtls_free( ctx );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001387 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1388 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001389
Gilles Peskine6331d782018-04-26 13:27:43 +02001390 mbedtls_printf( "Async resume (slot %u): %s done, status=%d.\n",
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001391 ctx->slot, op_name, ret );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001392 mbedtls_free( ctx );
1393 return( ret );
1394}
1395
Gilles Peskine8f97af72018-04-26 11:46:10 +02001396static void ssl_async_cancel( mbedtls_ssl_context *ssl )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001397{
Gilles Peskinea668c602018-04-30 11:54:39 +02001398 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001399 mbedtls_printf( "Async cancel callback.\n" );
1400 mbedtls_free( ctx );
1401}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001402#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001403
Hanno Becker16970d22017-10-10 15:56:37 +01001404/*
1405 * Wait for an event from the underlying transport or the timer
1406 * (Used in event-driven IO mode).
1407 */
1408#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001409int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001410 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001411#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001412int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001413 mbedtls_timing_delay_context *timer,
1414 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001415#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001416{
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001417 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +01001418 int poll_type = 0;
1419
1420 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
1421 poll_type = MBEDTLS_NET_POLL_WRITE;
1422 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
1423 poll_type = MBEDTLS_NET_POLL_READ;
1424#if !defined(MBEDTLS_TIMING_C)
1425 else
Hanno Beckeref527962018-03-15 15:49:24 +00001426 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001427#endif
1428
1429 while( 1 )
1430 {
Hanno Becker197a91c2017-10-31 10:58:53 +00001431 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +01001432#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001433 if( timer != NULL &&
1434 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +01001435 {
Hanno Becker16970d22017-10-10 15:56:37 +01001436 break;
1437 }
Hanno Becker197a91c2017-10-31 10:58:53 +00001438#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +01001439
Hanno Becker197a91c2017-10-31 10:58:53 +00001440 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001441 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +01001442 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001443 ret = mbedtls_net_poll( fd, poll_type, 0 );
1444 if( ret < 0 )
1445 return( ret );
1446 if( ret == poll_type )
1447 break;
Hanno Becker16970d22017-10-10 15:56:37 +01001448 }
1449 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001450
1451 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001452}
1453
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001454#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001455static psa_status_t psa_setup_psk_key_slot( psa_key_handle_t slot,
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001456 psa_algorithm_t alg,
1457 unsigned char *psk,
1458 size_t psk_len )
1459{
1460 psa_status_t status;
1461 psa_key_policy_t policy;
1462
Hanno Becker9bd88422019-01-25 14:27:01 +00001463 policy = psa_key_policy_init();
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001464 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
1465
1466 status = psa_set_key_policy( slot, &policy );
1467 if( status != PSA_SUCCESS )
Hanno Becker1d911cd2018-11-15 13:06:09 +00001468 {
1469 fprintf( stderr, "POLICY\n" );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001470 return( status );
Hanno Becker1d911cd2018-11-15 13:06:09 +00001471 }
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001472
1473 status = psa_import_key( slot, PSA_KEY_TYPE_DERIVE, psk, psk_len );
1474 if( status != PSA_SUCCESS )
Hanno Becker1d911cd2018-11-15 13:06:09 +00001475 {
1476 fprintf( stderr, "IMPORT\n" );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001477 return( status );
Hanno Becker1d911cd2018-11-15 13:06:09 +00001478 }
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001479
1480 return( PSA_SUCCESS );
1481}
1482#endif /* MBEDTLS_USE_PSA_CRYPTO */
1483
Hanno Beckera0e20d02019-05-15 14:03:01 +01001484#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001485int report_cid_usage( mbedtls_ssl_context *ssl,
1486 const char *additional_description )
1487{
1488 int ret;
1489 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
1490 size_t peer_cid_len;
1491 int cid_negotiated;
1492
1493 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1494 return( 0 );
1495
1496 /* Check if the use of a CID has been negotiated */
1497 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
1498 peer_cid, &peer_cid_len );
1499 if( ret != 0 )
1500 {
1501 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
1502 -ret );
1503 return( ret );
1504 }
1505
1506 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
1507 {
1508 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
1509 {
1510 mbedtls_printf( "(%s) Use of Connection ID was not offered by client.\n",
1511 additional_description );
1512 }
1513 }
1514 else
1515 {
1516 size_t idx=0;
1517 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
1518 additional_description );
1519 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
1520 additional_description,
1521 (unsigned) peer_cid_len );
1522 while( idx < peer_cid_len )
1523 {
1524 mbedtls_printf( "%02x ", peer_cid[ idx ] );
1525 idx++;
1526 }
1527 mbedtls_printf( "\n" );
1528 }
1529
1530 return( 0 );
1531}
Hanno Beckera0e20d02019-05-15 14:03:01 +01001532#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001533
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001534int main( int argc, char *argv[] )
1535{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001536 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001537 int version_suites[4][2];
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001538 unsigned char* buf = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001540#if defined(MBEDTLS_USE_PSA_CRYPTO)
1541 psa_algorithm_t alg = 0;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001542 psa_key_handle_t psk_slot = 0;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001543#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +02001545 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +02001546 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +02001547#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001548 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001549 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02001550 size_t cliip_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551#if defined(MBEDTLS_SSL_COOKIE_C)
1552 mbedtls_ssl_cookie_ctx cookie_ctx;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001553#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001554
Gilles Peskineef86ab22017-05-05 18:59:02 +02001555#if defined(MBEDTLS_X509_CRT_PARSE_C)
1556 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
1557#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 mbedtls_entropy_context entropy;
1559 mbedtls_ctr_drbg_context ctr_drbg;
1560 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001561 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001562#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001563 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001564#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001566 unsigned char renego_period[8] = { 0 };
1567#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +02001569 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 mbedtls_x509_crt cacert;
1571 mbedtls_x509_crt srvcert;
1572 mbedtls_pk_context pkey;
1573 mbedtls_x509_crt srvcert2;
1574 mbedtls_pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001575 int key_cert_init = 0, key_cert_init2 = 0;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001576#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001577 ssl_async_key_context_t ssl_async_keys;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001578#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001579#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1581 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001582#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#if defined(MBEDTLS_SSL_CACHE_C)
1584 mbedtls_ssl_cache_context cache;
Paul Bakker0a597072012-09-25 21:55:46 +00001585#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001586#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1587 mbedtls_ssl_ticket_context ticket_ctx;
1588#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001589#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001590 sni_entry *sni_info = NULL;
1591#endif
Hanno Beckere6706e62017-05-15 16:05:15 +01001592#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +01001593 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +01001594 const mbedtls_ecp_curve_info * curve_cur;
1595#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +01001597 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001598#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Simon Butchercce68be2018-07-23 14:26:09 +01001600 unsigned char alloc_buf[MEMORY_HEAP_SIZE];
Paul Bakker82024bf2013-07-04 11:52:32 +02001601#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001602
Hanno Beckera0e20d02019-05-15 14:03:01 +01001603#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +01001604 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001605 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckera7d25422019-04-09 17:28:10 +01001606 size_t cid_len = 0;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001607 size_t cid_renego_len = 0;
Hanno Beckera7d25422019-04-09 17:28:10 +01001608#endif
1609
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001610 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001611 char *p, *q;
1612 const int *list;
Hanno Becker5a9942e2018-11-12 17:47:48 +00001613#if defined(MBEDTLS_USE_PSA_CRYPTO)
1614 psa_status_t status;
1615#endif
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001616#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1617 unsigned char eap_tls_keymaterial[16];
1618 unsigned char eap_tls_iv[8];
1619 const char* eap_tls_label = "client EAP encryption";
1620 eap_tls_keys eap_tls_keying;
1621#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1624 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker82024bf2013-07-04 11:52:32 +02001625#endif
1626
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001627 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +02001628 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001629 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001630 mbedtls_net_init( &client_fd );
1631 mbedtls_net_init( &listen_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001632 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001633 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001634 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#if defined(MBEDTLS_X509_CRT_PARSE_C)
1636 mbedtls_x509_crt_init( &cacert );
1637 mbedtls_x509_crt_init( &srvcert );
1638 mbedtls_pk_init( &pkey );
1639 mbedtls_x509_crt_init( &srvcert2 );
1640 mbedtls_pk_init( &pkey2 );
Gilles Peskine4d9ec4d2018-04-26 14:33:43 +02001641#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
1642 memset( &ssl_async_keys, 0, sizeof( ssl_async_keys ) );
1643#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001644#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1646 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001647#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#if defined(MBEDTLS_SSL_CACHE_C)
1649 mbedtls_ssl_cache_init( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00001650#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001651#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1652 mbedtls_ssl_ticket_init( &ticket_ctx );
1653#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +02001655 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001656#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#if defined(MBEDTLS_SSL_COOKIE_C)
1658 mbedtls_ssl_cookie_init( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001659#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001660
Hanno Becker5a9942e2018-11-12 17:47:48 +00001661#if defined(MBEDTLS_USE_PSA_CRYPTO)
1662 status = psa_crypto_init();
1663 if( status != PSA_SUCCESS )
1664 {
1665 mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
1666 (int) status );
1667 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1668 goto exit;
1669 }
1670#endif
1671
Paul Bakkerc1283d32014-08-18 11:05:51 +02001672#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001673 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001674 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001675 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +02001676#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001677
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001678 if( argc == 0 )
1679 {
1680 usage:
1681 if( ret == 0 )
1682 ret = 1;
1683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 mbedtls_printf( USAGE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 list = mbedtls_ssl_list_ciphersuites();
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001687 while( *list )
1688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001690 list++;
1691 if( !*list )
1692 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001694 list++;
1695 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 mbedtls_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001697 goto exit;
1698 }
1699
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001700 opt.buffer_size = DFL_IO_BUF_LEN;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001701 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001702 opt.server_port = DFL_SERVER_PORT;
1703 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker16970d22017-10-10 15:56:37 +01001704 opt.event = DFL_EVENT;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001705 opt.response_size = DFL_RESPONSE_SIZE;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001706 opt.nbio = DFL_NBIO;
Hanno Beckera7d25422019-04-09 17:28:10 +01001707 opt.cid_enabled = DFL_CID_ENABLED;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001708 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
Hanno Beckera7d25422019-04-09 17:28:10 +01001709 opt.cid_val = DFL_CID_VALUE;
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001710 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001711 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001712 opt.ca_file = DFL_CA_FILE;
1713 opt.ca_path = DFL_CA_PATH;
1714 opt.crt_file = DFL_CRT_FILE;
1715 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001716 opt.crt_file2 = DFL_CRT_FILE2;
1717 opt.key_file2 = DFL_KEY_FILE2;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001718 opt.async_operations = DFL_ASYNC_OPERATIONS;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001719 opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
1720 opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
1721 opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
Paul Bakkerfbb17802013-04-17 19:10:21 +02001722 opt.psk = DFL_PSK;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001723#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001724 opt.psk_opaque = DFL_PSK_OPAQUE;
1725 opt.psk_list_opaque = DFL_PSK_LIST_OPAQUE;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001726#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001727#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1728 opt.ca_callback = DFL_CA_CALLBACK;
1729#endif
Paul Bakkerfbb17802013-04-17 19:10:21 +02001730 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001731 opt.psk_list = DFL_PSK_LIST;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001732 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001733 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001734 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001735 opt.renegotiation = DFL_RENEGOTIATION;
1736 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001737 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001738 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001739 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001740 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001741 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001742 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001743 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001744 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001745 opt.auth_mode = DFL_AUTH_MODE;
Janos Follath4817e272017-04-10 13:44:33 +01001746 opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001747 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001748 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001749 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001750 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001751 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001752 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001753 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001754 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001755 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001756 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001757 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001758 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001759 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001760 opt.hs_to_min = DFL_HS_TO_MIN;
1761 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001762 opt.dtls_mtu = DFL_DTLS_MTU;
Hanno Beckere7675d02018-08-14 13:28:56 +01001763 opt.dgram_packing = DFL_DGRAM_PACKING;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001764 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001765 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001766 opt.etm = DFL_ETM;
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001767 opt.eap_tls = DFL_EAP_TLS;
Philippe Antoineaa4d1522019-06-06 21:24:31 +02001768 opt.reproducible = DFL_REPRODUCIBLE;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001769
1770 for( i = 1; i < argc; i++ )
1771 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001772 p = argv[i];
1773 if( ( q = strchr( p, '=' ) ) == NULL )
1774 goto usage;
1775 *q++ = '\0';
1776
1777 if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001778 opt.server_port = q;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001779 else if( strcmp( p, "server_addr" ) == 0 )
1780 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001781 else if( strcmp( p, "dtls" ) == 0 )
1782 {
1783 int t = atoi( q );
1784 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001786 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001788 else
1789 goto usage;
1790 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001791 else if( strcmp( p, "debug_level" ) == 0 )
1792 {
1793 opt.debug_level = atoi( q );
1794 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1795 goto usage;
1796 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001797 else if( strcmp( p, "nbio" ) == 0 )
1798 {
1799 opt.nbio = atoi( q );
1800 if( opt.nbio < 0 || opt.nbio > 2 )
1801 goto usage;
1802 }
Hanno Becker16970d22017-10-10 15:56:37 +01001803 else if( strcmp( p, "event" ) == 0 )
1804 {
1805 opt.event = atoi( q );
1806 if( opt.event < 0 || opt.event > 2 )
1807 goto usage;
1808 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001809 else if( strcmp( p, "read_timeout" ) == 0 )
1810 opt.read_timeout = atoi( q );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001811 else if( strcmp( p, "buffer_size" ) == 0 )
1812 {
1813 opt.buffer_size = atoi( q );
1814 if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
1815 goto usage;
1816 }
1817 else if( strcmp( p, "response_size" ) == 0 )
1818 {
1819 opt.response_size = atoi( q );
1820 if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
1821 goto usage;
1822 if( opt.buffer_size < opt.response_size )
1823 opt.buffer_size = opt.response_size;
1824 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001825 else if( strcmp( p, "ca_file" ) == 0 )
1826 opt.ca_file = q;
1827 else if( strcmp( p, "ca_path" ) == 0 )
1828 opt.ca_path = q;
1829 else if( strcmp( p, "crt_file" ) == 0 )
1830 opt.crt_file = q;
1831 else if( strcmp( p, "key_file" ) == 0 )
1832 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001833 else if( strcmp( p, "crt_file2" ) == 0 )
1834 opt.crt_file2 = q;
1835 else if( strcmp( p, "key_file2" ) == 0 )
1836 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001837 else if( strcmp( p, "dhm_file" ) == 0 )
1838 opt.dhm_file = q;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001839#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001840 else if( strcmp( p, "async_operations" ) == 0 )
1841 opt.async_operations = q;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001842 else if( strcmp( p, "async_private_delay1" ) == 0 )
1843 opt.async_private_delay1 = atoi( q );
1844 else if( strcmp( p, "async_private_delay2" ) == 0 )
1845 opt.async_private_delay2 = atoi( q );
1846 else if( strcmp( p, "async_private_error" ) == 0 )
1847 {
1848 int n = atoi( q );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01001849 if( n < -SSL_ASYNC_INJECT_ERROR_MAX ||
1850 n > SSL_ASYNC_INJECT_ERROR_MAX )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001851 {
1852 ret = 2;
1853 goto usage;
1854 }
1855 opt.async_private_error = n;
1856 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001857#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Hanno Beckera0e20d02019-05-15 14:03:01 +01001858#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +01001859 else if( strcmp( p, "cid" ) == 0 )
1860 {
1861 opt.cid_enabled = atoi( q );
1862 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
1863 goto usage;
1864 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001865 else if( strcmp( p, "cid_renego" ) == 0 )
1866 {
1867 opt.cid_enabled_renego = atoi( q );
1868 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
1869 goto usage;
1870 }
Hanno Beckera7d25422019-04-09 17:28:10 +01001871 else if( strcmp( p, "cid_val" ) == 0 )
1872 {
1873 opt.cid_val = q;
1874 }
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01001875 else if( strcmp( p, "cid_val_renego" ) == 0 )
1876 {
1877 opt.cid_val_renego = q;
1878 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001879#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001880 else if( strcmp( p, "psk" ) == 0 )
1881 opt.psk = q;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001882#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001883 else if( strcmp( p, "psk_opaque" ) == 0 )
1884 opt.psk_opaque = atoi( q );
1885 else if( strcmp( p, "psk_list_opaque" ) == 0 )
1886 opt.psk_list_opaque = atoi( q );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001887#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001888#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1889 else if( strcmp( p, "ca_callback" ) == 0)
1890 opt.ca_callback = atoi( q );
1891#endif
Paul Bakkerfbb17802013-04-17 19:10:21 +02001892 else if( strcmp( p, "psk_identity" ) == 0 )
1893 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001894 else if( strcmp( p, "psk_list" ) == 0 )
1895 opt.psk_list = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001896 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1897 opt.ecjpake_pw = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001898 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001901
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001902 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001903 {
1904 ret = 2;
1905 goto usage;
1906 }
1907 opt.force_ciphersuite[1] = 0;
1908 }
Hanno Beckere6706e62017-05-15 16:05:15 +01001909 else if( strcmp( p, "curves" ) == 0 )
1910 opt.curves = q;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001911 else if( strcmp( p, "version_suites" ) == 0 )
1912 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001913 else if( strcmp( p, "renegotiation" ) == 0 )
1914 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001915 opt.renegotiation = (atoi( q )) ?
1916 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1917 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001918 }
1919 else if( strcmp( p, "allow_legacy" ) == 0 )
1920 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001921 switch( atoi( q ) )
1922 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001923 case -1:
1924 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1925 break;
1926 case 0:
1927 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1928 break;
1929 case 1:
1930 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1931 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001932 default: goto usage;
1933 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001934 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001935 else if( strcmp( p, "renegotiate" ) == 0 )
1936 {
1937 opt.renegotiate = atoi( q );
1938 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1939 goto usage;
1940 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001941 else if( strcmp( p, "renego_delay" ) == 0 )
1942 {
1943 opt.renego_delay = atoi( q );
1944 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001945 else if( strcmp( p, "renego_period" ) == 0 )
1946 {
Andres AG0b736db2017-02-08 14:05:57 +00001947#if defined(_MSC_VER)
1948 opt.renego_period = _strtoui64( q, NULL, 10 );
1949#else
1950 if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
1951 goto usage;
1952#endif /* _MSC_VER */
1953 if( opt.renego_period < 2 )
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001954 goto usage;
1955 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001956 else if( strcmp( p, "exchanges" ) == 0 )
1957 {
1958 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001959 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001960 goto usage;
1961 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001962 else if( strcmp( p, "min_version" ) == 0 )
1963 {
1964 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001966 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001968 else if( strcmp( q, "tls1_1" ) == 0 ||
1969 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001971 else if( strcmp( q, "tls1_2" ) == 0 ||
1972 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001974 else
1975 goto usage;
1976 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001977 else if( strcmp( p, "max_version" ) == 0 )
1978 {
1979 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001981 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001983 else if( strcmp( q, "tls1_1" ) == 0 ||
1984 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001986 else if( strcmp( q, "tls1_2" ) == 0 ||
1987 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001989 else
1990 goto usage;
1991 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001992 else if( strcmp( p, "arc4" ) == 0 )
1993 {
1994 switch( atoi( q ) )
1995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1997 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001998 default: goto usage;
1999 }
2000 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02002001 else if( strcmp( p, "allow_sha1" ) == 0 )
2002 {
2003 switch( atoi( q ) )
2004 {
2005 case 0: opt.allow_sha1 = 0; break;
2006 case 1: opt.allow_sha1 = 1; break;
2007 default: goto usage;
2008 }
2009 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002010 else if( strcmp( p, "force_version" ) == 0 )
2011 {
2012 if( strcmp( q, "ssl3" ) == 0 )
2013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
2015 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002016 }
2017 else if( strcmp( q, "tls1" ) == 0 )
2018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
2020 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002021 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002022 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02002023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
2025 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002026 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002027 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02002028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
2030 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02002031 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002032 else if( strcmp( q, "dtls1" ) == 0 )
2033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
2035 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
2036 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002037 }
2038 else if( strcmp( q, "dtls1_2" ) == 0 )
2039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
2041 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
2042 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01002043 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002044 else
2045 goto usage;
2046 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01002047 else if( strcmp( p, "auth_mode" ) == 0 )
2048 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02002049 if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
Paul Bakker91ebfb52012-11-23 14:04:08 +01002050 goto usage;
2051 }
Janos Follath4817e272017-04-10 13:44:33 +01002052 else if( strcmp( p, "cert_req_ca_list" ) == 0 )
2053 {
2054 opt.cert_req_ca_list = atoi( q );
2055 if( opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1 )
2056 goto usage;
2057 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002058 else if( strcmp( p, "max_frag_len" ) == 0 )
2059 {
2060 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002062 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002064 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002066 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002067 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002068 else
2069 goto usage;
2070 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002071 else if( strcmp( p, "alpn" ) == 0 )
2072 {
2073 opt.alpn_string = q;
2074 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002075 else if( strcmp( p, "trunc_hmac" ) == 0 )
2076 {
2077 switch( atoi( q ) )
2078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
2080 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002081 default: goto usage;
2082 }
2083 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002084 else if( strcmp( p, "extended_ms" ) == 0 )
2085 {
2086 switch( atoi( q ) )
2087 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002088 case 0:
2089 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
2090 break;
2091 case 1:
2092 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
2093 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002094 default: goto usage;
2095 }
2096 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002097 else if( strcmp( p, "etm" ) == 0 )
2098 {
2099 switch( atoi( q ) )
2100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
2102 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002103 default: goto usage;
2104 }
2105 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002106 else if( strcmp( p, "tickets" ) == 0 )
2107 {
2108 opt.tickets = atoi( q );
2109 if( opt.tickets < 0 || opt.tickets > 1 )
2110 goto usage;
2111 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01002112 else if( strcmp( p, "ticket_timeout" ) == 0 )
2113 {
2114 opt.ticket_timeout = atoi( q );
2115 if( opt.ticket_timeout < 0 )
2116 goto usage;
2117 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002118 else if( strcmp( p, "cache_max" ) == 0 )
2119 {
2120 opt.cache_max = atoi( q );
2121 if( opt.cache_max < 0 )
2122 goto usage;
2123 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002124 else if( strcmp( p, "cache_timeout" ) == 0 )
2125 {
2126 opt.cache_timeout = atoi( q );
2127 if( opt.cache_timeout < 0 )
2128 goto usage;
2129 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002130 else if( strcmp( p, "cookies" ) == 0 )
2131 {
2132 opt.cookies = atoi( q );
2133 if( opt.cookies < -1 || opt.cookies > 1)
2134 goto usage;
2135 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002136 else if( strcmp( p, "anti_replay" ) == 0 )
2137 {
2138 opt.anti_replay = atoi( q );
2139 if( opt.anti_replay < 0 || opt.anti_replay > 1)
2140 goto usage;
2141 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002142 else if( strcmp( p, "badmac_limit" ) == 0 )
2143 {
2144 opt.badmac_limit = atoi( q );
2145 if( opt.badmac_limit < 0 )
2146 goto usage;
2147 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002148 else if( strcmp( p, "hs_timeout" ) == 0 )
2149 {
2150 if( ( p = strchr( q, '-' ) ) == NULL )
2151 goto usage;
2152 *p++ = '\0';
2153 opt.hs_to_min = atoi( q );
2154 opt.hs_to_max = atoi( p );
2155 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
2156 goto usage;
2157 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002158 else if( strcmp( p, "mtu" ) == 0 )
2159 {
2160 opt.dtls_mtu = atoi( q );
2161 if( opt.dtls_mtu < 0 )
2162 goto usage;
2163 }
Hanno Beckere7675d02018-08-14 13:28:56 +01002164 else if( strcmp( p, "dgram_packing" ) == 0 )
2165 {
2166 opt.dgram_packing = atoi( q );
2167 if( opt.dgram_packing != 0 &&
2168 opt.dgram_packing != 1 )
2169 {
2170 goto usage;
2171 }
2172 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002173 else if( strcmp( p, "sni" ) == 0 )
2174 {
2175 opt.sni = q;
2176 }
Andres Amaya Garciabc818842018-10-16 21:08:38 +01002177 else if( strcmp( p, "query_config" ) == 0 )
2178 {
2179 return query_config( q );
2180 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002181 else if( strcmp( p, "eap_tls" ) == 0 )
2182 {
2183 opt.eap_tls = atoi( q );
2184 if( opt.eap_tls < 0 || opt.eap_tls > 1 )
2185 goto usage;
2186 }
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002187 else if( strcmp( p, "reproducible" ) == 0 )
2188 {
2189 opt.reproducible = 1;
2190 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002191 else
2192 goto usage;
2193 }
2194
Hanno Becker16970d22017-10-10 15:56:37 +01002195 /* Event-driven IO is incompatible with the above custom
2196 * receive and send functions, as the polling builds on
2197 * refers to the underlying net_context. */
2198 if( opt.event == 1 && opt.nbio != 1 )
2199 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002200 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01002201 opt.nbio = 1;
2202 }
2203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204#if defined(MBEDTLS_DEBUG_C)
2205 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02002206#endif
Andrzej Kurekda4029d2018-06-20 07:07:55 -04002207 buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002208 if( buf == NULL )
2209 {
Andrzej Kurek755890f2018-06-20 08:17:04 -04002210 mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002211 ret = 3;
2212 goto exit;
2213 }
Paul Bakkerc73079a2014-04-25 16:34:30 +02002214
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002215#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002216 if( opt.psk_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002217 {
2218 if( strlen( opt.psk ) == 0 )
2219 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00002220 mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002221 ret = 2;
2222 goto usage;
2223 }
2224
2225 if( opt.force_ciphersuite[0] <= 0 )
2226 {
2227 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" );
2228 ret = 2;
2229 goto usage;
2230 }
2231 }
2232
Hanno Becker1d911cd2018-11-15 13:06:09 +00002233 if( opt.psk_list_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002234 {
2235 if( opt.psk_list == NULL )
2236 {
2237 mbedtls_printf( "psk_slot set but no psk to be imported specified.\n" );
2238 ret = 2;
2239 goto usage;
2240 }
2241
2242 if( opt.force_ciphersuite[0] <= 0 )
2243 {
2244 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" );
2245 ret = 2;
2246 goto usage;
2247 }
2248 }
2249#endif /* MBEDTLS_USE_PSA_CRYPTO */
2250
Paul Bakkerc1516be2013-06-29 16:01:32 +02002251 if( opt.force_ciphersuite[0] > 0 )
2252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002254 ciphersuite_info =
2255 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002256
Paul Bakker5b55b792013-07-19 13:43:43 +02002257 if( opt.max_version != -1 &&
2258 ciphersuite_info->min_minor_ver > opt.max_version )
2259 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002260 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker5b55b792013-07-19 13:43:43 +02002261 ret = 2;
2262 goto usage;
2263 }
2264 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02002265 ciphersuite_info->max_minor_ver < opt.min_version )
2266 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002267 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002268 ret = 2;
2269 goto usage;
2270 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002271
2272 /* If we select a version that's not supported by
2273 * this suite, then there will be no common ciphersuite... */
2274 if( opt.max_version == -1 ||
2275 opt.max_version > ciphersuite_info->max_minor_ver )
2276 {
Paul Bakker5b55b792013-07-19 13:43:43 +02002277 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002278 }
Paul Bakker5b55b792013-07-19 13:43:43 +02002279 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002280 {
Paul Bakker5b55b792013-07-19 13:43:43 +02002281 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002282 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2284 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
2285 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002286 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002287
2288 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002294 ret = 2;
2295 goto usage;
2296 }
2297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002299 }
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002300
2301#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002302 if( opt.psk_opaque != 0 || opt.psk_list_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002303 {
2304 /* Ensure that the chosen ciphersuite is PSK-only; we must know
2305 * the ciphersuite in advance to set the correct policy for the
2306 * PSK key slot. This limitation might go away in the future. */
2307 if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
2308 opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 )
2309 {
2310 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" );
2311 ret = 2;
2312 goto usage;
2313 }
2314
2315 /* Determine KDF algorithm the opaque PSK will be used in. */
2316#if defined(MBEDTLS_SHA512_C)
2317 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
2318 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2319 else
2320#endif /* MBEDTLS_SHA512_C */
2321 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2322 }
2323#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerc1516be2013-06-29 16:01:32 +02002324 }
2325
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002326 if( opt.version_suites != NULL )
2327 {
2328 const char *name[4] = { 0 };
2329
2330 /* Parse 4-element coma-separated list */
2331 for( i = 0, p = (char *) opt.version_suites;
2332 i < 4 && *p != '\0';
2333 i++ )
2334 {
2335 name[i] = p;
2336
2337 /* Terminate the current string and move on to next one */
2338 while( *p != ',' && *p != '\0' )
2339 p++;
2340 if( *p == ',' )
2341 *p++ = '\0';
2342 }
2343
2344 if( i != 4 )
2345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 mbedtls_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002347 ret = 1;
2348 goto exit;
2349 }
2350
2351 memset( version_suites, 0, sizeof( version_suites ) );
2352
2353 /* Get the suites identifiers from their name */
2354 for( i = 0; i < 4; i++ )
2355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002357
2358 if( version_suites[i][0] == 0 )
2359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002361 ret = 2;
2362 goto usage;
2363 }
2364 }
2365 }
2366
Hanno Beckera0e20d02019-05-15 14:03:01 +01002367#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002368 if( unhexify( cid, opt.cid_val, &cid_len ) != 0 )
2369 {
2370 mbedtls_printf( "CID not valid hex\n" );
2371 goto exit;
2372 }
2373
2374 /* Keep CID settings for renegotiation unless
2375 * specified otherwise. */
2376 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
2377 opt.cid_enabled_renego = opt.cid_enabled;
2378 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
2379 opt.cid_val_renego = opt.cid_val;
2380
2381 if( unhexify( cid_renego, opt.cid_val_renego, &cid_renego_len ) != 0 )
2382 {
2383 mbedtls_printf( "CID not valid hex\n" );
2384 goto exit;
2385 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002386#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +01002387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002389 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002390 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02002391 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002392 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 mbedtls_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002395 goto exit;
2396 }
2397
2398 if( opt.psk_list != NULL )
2399 {
2400 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 mbedtls_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002403 goto exit;
2404 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002405 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002407
Hanno Beckere6706e62017-05-15 16:05:15 +01002408#if defined(MBEDTLS_ECP_C)
2409 if( opt.curves != NULL )
2410 {
2411 p = (char *) opt.curves;
2412 i = 0;
2413
2414 if( strcmp( p, "none" ) == 0 )
2415 {
2416 curve_list[0] = MBEDTLS_ECP_DP_NONE;
2417 }
2418 else if( strcmp( p, "default" ) != 0 )
2419 {
2420 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01002421 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002422 {
2423 q = p;
2424
2425 /* Terminate the current string */
2426 while( *p != ',' && *p != '\0' )
2427 p++;
2428 if( *p == ',' )
2429 *p++ = '\0';
2430
2431 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
2432 {
2433 curve_list[i++] = curve_cur->grp_id;
2434 }
2435 else
2436 {
2437 mbedtls_printf( "unknown curve %s\n", q );
2438 mbedtls_printf( "supported curves: " );
2439 for( curve_cur = mbedtls_ecp_curve_list();
2440 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
2441 curve_cur++ )
2442 {
2443 mbedtls_printf( "%s ", curve_cur->name );
2444 }
2445 mbedtls_printf( "\n" );
2446 goto exit;
2447 }
2448 }
2449
2450 mbedtls_printf("Number of curves: %d\n", i );
2451
Hanno Becker8651a432017-06-09 16:13:22 +01002452 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002453 {
Hanno Becker8651a432017-06-09 16:13:22 +01002454 mbedtls_printf( "curves list too long, maximum %d",
2455 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01002456 goto exit;
2457 }
2458
2459 curve_list[i] = MBEDTLS_ECP_DP_NONE;
2460 }
2461 }
2462#endif /* MBEDTLS_ECP_C */
2463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002465 if( opt.alpn_string != NULL )
2466 {
2467 p = (char *) opt.alpn_string;
2468 i = 0;
2469
2470 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01002471 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002472 {
2473 alpn_list[i++] = p;
2474
2475 /* Terminate the current string and move on to next one */
2476 while( *p != ',' && *p != '\0' )
2477 p++;
2478 if( *p == ',' )
2479 *p++ = '\0';
2480 }
2481 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002483
Paul Bakkerfbb17802013-04-17 19:10:21 +02002484 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002485 * 0. Initialize the RNG and the session data
2486 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002488 fflush( stdout );
2489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 mbedtls_entropy_init( &entropy );
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002491 if (opt.reproducible) {
2492 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy,
2493 &entropy, (const unsigned char *) pers,
2494 strlen( pers ) ) ) != 0 )
2495 {
2496 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
2497 -ret );
2498 goto exit;
2499 }
2500 } else {
2501 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
2502 &entropy, (const unsigned char *) pers,
2503 strlen( pers ) ) ) != 0 )
2504 {
2505 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
2506 -ret );
2507 goto exit;
2508 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002509 }
2510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002514 /*
2515 * 1.1. Load the trusted CA
2516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002518 fflush( stdout );
2519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002521 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002522 if( strcmp( opt.ca_path, "none" ) == 0 )
2523 ret = 0;
2524 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002526 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002527 if( strcmp( opt.ca_file, "none" ) == 0 )
2528 ret = 0;
2529 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02002531 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002532#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533#if defined(MBEDTLS_CERTS_C)
2534 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 ret = mbedtls_x509_crt_parse( &cacert,
2537 (const unsigned char *) mbedtls_test_cas[i],
2538 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002539 if( ret != 0 )
2540 break;
2541 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002542#else
2543 {
2544 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002546 }
2547#endif
2548 if( ret < 0 )
2549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002551 goto exit;
2552 }
2553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002555
2556 /*
2557 * 1.2. Load own certificate and private key
2558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002560 fflush( stdout );
2561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002563 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002564 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002565 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002569 -ret );
2570 goto exit;
2571 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002572 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002573 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002574 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002575 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002579 goto exit;
2580 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002581 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002582 if( key_cert_init == 1 )
2583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584 mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002585 goto exit;
2586 }
2587
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002588 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002589 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002590 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002594 -ret );
2595 goto exit;
2596 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002597 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002598 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002599 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002600 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002604 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002605 goto exit;
2606 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002607 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002608 if( key_cert_init2 == 1 )
2609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610 mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002611 goto exit;
2612 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002613#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002614 if( key_cert_init == 0 &&
2615 strcmp( opt.crt_file, "none" ) != 0 &&
2616 strcmp( opt.key_file, "none" ) != 0 &&
2617 key_cert_init2 == 0 &&
2618 strcmp( opt.crt_file2, "none" ) != 0 &&
2619 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621#if !defined(MBEDTLS_CERTS_C)
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002622 mbedtls_printf( "Not certificated or key provided, and \nMBEDTLS_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002623 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002624#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625#if defined(MBEDTLS_RSA_C)
2626 if( ( ret = mbedtls_x509_crt_parse( &srvcert,
2627 (const unsigned char *) mbedtls_test_srv_crt_rsa,
2628 mbedtls_test_srv_crt_rsa_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002629 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002630 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
2631 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002632 goto exit;
2633 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 if( ( ret = mbedtls_pk_parse_key( &pkey,
2635 (const unsigned char *) mbedtls_test_srv_key_rsa,
2636 mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002637 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002638 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
2639 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002640 goto exit;
2641 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002642 key_cert_init = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643#endif /* MBEDTLS_RSA_C */
2644#if defined(MBEDTLS_ECDSA_C)
2645 if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
2646 (const unsigned char *) mbedtls_test_srv_crt_ec,
2647 mbedtls_test_srv_crt_ec_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002648 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002649 mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n",
2650 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002651 goto exit;
2652 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 if( ( ret = mbedtls_pk_parse_key( &pkey2,
2654 (const unsigned char *) mbedtls_test_srv_key_ec,
2655 mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002656 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002657 mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n",
2658 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002659 goto exit;
2660 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002661 key_cert_init2 = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662#endif /* MBEDTLS_ECDSA_C */
2663#endif /* MBEDTLS_CERTS_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002664 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 mbedtls_printf( " ok\n" );
2667#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002670 if( opt.dhm_file != NULL )
2671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672 mbedtls_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002673 fflush( stdout );
2674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677 mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002678 -ret );
2679 goto exit;
2680 }
2681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002683 }
2684#endif
2685
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002686#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002687 if( opt.sni != NULL )
2688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 mbedtls_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002690 fflush( stdout );
2691
2692 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
2693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002695 goto exit;
2696 }
2697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002699 }
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002700#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002701
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002702 /*
2703 * 2. Setup the listening TCP socket
2704 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02002705 mbedtls_printf( " . Bind on %s://%s:%s/ ...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002707 opt.server_addr ? opt.server_addr : "*",
2708 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002709 fflush( stdout );
2710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
2712 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2713 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002716 goto exit;
2717 }
2718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002720
2721 /*
2722 * 3. Setup stuff
2723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002725 fflush( stdout );
2726
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02002727 if( ( ret = mbedtls_ssl_config_defaults( &conf,
2728 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02002729 opt.transport,
2730 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002731 {
2732 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
2733 goto exit;
2734 }
2735
Gilles Peskineef86ab22017-05-05 18:59:02 +02002736#if defined(MBEDTLS_X509_CRT_PARSE_C)
2737 /* The default algorithms profile disables SHA-1, but our tests still
2738 rely on it heavily. Hence we allow it here. A real-world server
2739 should use the default profile unless there is a good reason not to. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02002740 if( opt.allow_sha1 > 0 )
2741 {
2742 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
2743 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
Gilles Peskine682df092017-05-11 19:01:11 +02002744 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
Gilles Peskinebc70a182017-05-09 15:59:24 +02002745 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02002746#endif /* MBEDTLS_X509_CRT_PARSE_C */
2747
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01002748 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002749 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002750
Janos Follath4817e272017-04-10 13:44:33 +01002751 if( opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST )
2752 mbedtls_ssl_conf_cert_req_ca_list( &conf, opt.cert_req_ca_list );
2753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002755 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002756 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002757
Hanno Beckere7675d02018-08-14 13:28:56 +01002758 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01002759 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002763 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002764 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002765 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002766 goto exit;
2767 };
Paul Bakker05decb22013-08-15 13:33:48 +02002768#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002769
Hanno Beckera0e20d02019-05-15 14:03:01 +01002770#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002771 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckerad4a1372019-05-03 13:06:44 +01002772 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002773 if( opt.cid_enabled == 1 &&
2774 opt.cid_enabled_renego == 1 &&
2775 cid_len != cid_renego_len )
2776 {
2777 mbedtls_printf( "CID length must not change during renegotiation\n" );
2778 goto usage;
2779 }
2780
2781 if( opt.cid_enabled == 1 )
Hanno Becker8367ccc2019-05-14 11:30:10 +01002782 ret = mbedtls_ssl_conf_cid( &conf, cid_len,
2783 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002784 else
Hanno Becker8367ccc2019-05-14 11:30:10 +01002785 ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
2786 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01002787
Hanno Beckerad4a1372019-05-03 13:06:44 +01002788 if( ret != 0 )
2789 {
Hanno Beckerd5eed422019-05-23 16:58:22 +01002790 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
2791 -ret );
Hanno Beckerad4a1372019-05-03 13:06:44 +01002792 goto exit;
2793 }
2794 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002795#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerad4a1372019-05-03 13:06:44 +01002796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002798 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002799 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002800#endif
2801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002803 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002804 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002805#endif
2806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002808 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002809 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002810#endif
2811
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002812#if defined(MBEDTLS_SSL_EXPORT_KEYS)
2813 if( opt.eap_tls != 0 )
2814 mbedtls_ssl_conf_export_keys_ext_cb( &conf, eap_tls_key_derivation,
2815 &eap_tls_keying );
2816#endif
2817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002819 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002820 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002821 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002822 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002823 goto exit;
2824 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002825#endif
2826
Philippe Antoineaa4d1522019-06-06 21:24:31 +02002827 if (opt.reproducible) {
2828 srand(1);
2829 mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg );
2830#if defined(MBEDTLS_PLATFORM_TIME_ALT)
2831 mbedtls_platform_set_time( dummy_constant_time );
2832#else
2833 fprintf(stderr, "Warning: reprpduce without constant time\n");
2834#endif
2835 } else {
2836 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
2837 }
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002838 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002841 if( opt.cache_max != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842 mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002843
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002844 if( opt.cache_timeout != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002846
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002847 mbedtls_ssl_conf_session_cache( &conf, &cache,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01002848 mbedtls_ssl_cache_get,
2849 mbedtls_ssl_cache_set );
Paul Bakker0a597072012-09-25 21:55:46 +00002850#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002853 if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002854 {
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002855 if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
2856 mbedtls_ctr_drbg_random, &ctr_drbg,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +02002857 MBEDTLS_CIPHER_AES_256_GCM,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002858 opt.ticket_timeout ) ) != 0 )
2859 {
2860 mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
2861 goto exit;
2862 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01002863
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002864 mbedtls_ssl_conf_session_tickets_cb( &conf,
2865 mbedtls_ssl_ticket_write,
2866 mbedtls_ssl_ticket_parse,
2867 &ticket_ctx );
2868 }
Paul Bakkera503a632013-08-14 13:48:06 +02002869#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871#if defined(MBEDTLS_SSL_PROTO_DTLS)
2872 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002875 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
2878 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880 mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002881 goto exit;
2882 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002883
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002884 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002885 &cookie_ctx );
2886 }
2887 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888#endif /* MBEDTLS_SSL_COOKIE_C */
2889#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002890 if( opt.cookies == 0 )
2891 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002892 mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002893 }
2894 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002896 {
2897 ; /* Nothing to do */
2898 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002901 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002902 mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002903#endif
2904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002906 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002907 mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002908#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002909 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002911
Paul Bakker41c83d32013-03-20 14:39:14 +01002912 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002913 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002914
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002915#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002916 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002917 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002918#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002919
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002920 if( opt.version_suites != NULL )
2921 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002922 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_MAJOR_VERSION_3,
2924 MBEDTLS_SSL_MINOR_VERSION_0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002925 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 MBEDTLS_SSL_MAJOR_VERSION_3,
2927 MBEDTLS_SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002928 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929 MBEDTLS_SSL_MAJOR_VERSION_3,
2930 MBEDTLS_SSL_MINOR_VERSION_2 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002931 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 MBEDTLS_SSL_MAJOR_VERSION_3,
2933 MBEDTLS_SSL_MINOR_VERSION_3 );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002934 }
2935
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002936 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002937 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002939 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002940
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02002941 if( opt.renego_delay != DFL_RENEGO_DELAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002942 mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002943
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002944 if( opt.renego_period != DFL_RENEGO_PERIOD )
2945 {
Andres AG692ad842017-01-19 16:30:57 +00002946 PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002947 mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002948 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002949#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002952 if( strcmp( opt.ca_path, "none" ) != 0 &&
2953 strcmp( opt.ca_file, "none" ) != 0 )
2954 {
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02002955#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2956 if( opt.ca_callback != 0 )
2957 mbedtls_ssl_conf_ca_cb( &conf, ca_callback, &cacert);
2958 else
2959#endif
2960 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002961 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002962 if( key_cert_init )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002963 {
2964 mbedtls_pk_context *pk = &pkey;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002965#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002966 if( opt.async_private_delay1 >= 0 )
2967 {
Gilles Peskine44817442018-06-13 18:09:28 +02002968 ret = ssl_async_set_key( &ssl_async_keys, &srvcert, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002969 opt.async_private_delay1 );
2970 if( ret < 0 )
2971 {
2972 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2973 ret );
2974 goto exit;
2975 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002976 pk = NULL;
2977 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002978#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002979 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002980 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002981 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002982 goto exit;
2983 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002984 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002985 if( key_cert_init2 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002986 {
2987 mbedtls_pk_context *pk = &pkey2;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002988#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002989 if( opt.async_private_delay2 >= 0 )
2990 {
Gilles Peskine44817442018-06-13 18:09:28 +02002991 ret = ssl_async_set_key( &ssl_async_keys, &srvcert2, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002992 opt.async_private_delay2 );
2993 if( ret < 0 )
2994 {
2995 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2996 ret );
2997 goto exit;
2998 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002999 pk = NULL;
3000 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003001#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003002 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003003 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003004 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02003005 goto exit;
3006 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003007 }
3008
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003009#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003010 if( opt.async_operations[0] != '-' )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003011 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003012 mbedtls_ssl_async_sign_t *sign = NULL;
3013 mbedtls_ssl_async_decrypt_t *decrypt = NULL;
Gilles Peskine12ab5d42018-04-24 12:32:04 +02003014 const char *r;
3015 for( r = opt.async_operations; *r; r++ )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003016 {
Gilles Peskine12ab5d42018-04-24 12:32:04 +02003017 switch( *r )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003018 {
3019 case 'd':
3020 decrypt = ssl_async_decrypt;
3021 break;
3022 case 's':
3023 sign = ssl_async_sign;
3024 break;
3025 }
3026 }
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003027 ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
3028 - opt.async_private_error :
3029 opt.async_private_error );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003030 ssl_async_keys.f_rng = mbedtls_ctr_drbg_random;
3031 ssl_async_keys.p_rng = &ctr_drbg;
3032 mbedtls_ssl_conf_async_private_cb( &conf,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01003033 sign,
3034 decrypt,
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003035 ssl_async_resume,
3036 ssl_async_cancel,
3037 &ssl_async_keys );
3038 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003039#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003040#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +02003041
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02003042#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003043 if( opt.sni != NULL )
Gilles Peskine166ce742018-04-30 10:30:49 +02003044 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003045 mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
Gilles Peskine166ce742018-04-30 10:30:49 +02003046#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3047 if( opt.async_private_delay2 >= 0 )
3048 {
Gilles Peskinee2479892018-06-13 18:06:51 +02003049 sni_entry *cur;
3050 for( cur = sni_info; cur != NULL; cur = cur->next )
Gilles Peskine166ce742018-04-30 10:30:49 +02003051 {
Gilles Peskinee2479892018-06-13 18:06:51 +02003052 ret = ssl_async_set_key( &ssl_async_keys,
Gilles Peskine44817442018-06-13 18:09:28 +02003053 cur->cert, cur->key, 1,
Gilles Peskinee2479892018-06-13 18:06:51 +02003054 opt.async_private_delay2 );
3055 if( ret < 0 )
3056 {
3057 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
3058 ret );
3059 goto exit;
3060 }
3061 cur->key = NULL;
Gilles Peskine166ce742018-04-30 10:30:49 +02003062 }
Gilles Peskine166ce742018-04-30 10:30:49 +02003063 }
3064#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3065 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003066#endif
3067
Hanno Beckere6706e62017-05-15 16:05:15 +01003068#if defined(MBEDTLS_ECP_C)
3069 if( opt.curves != NULL &&
3070 strcmp( opt.curves, "default" ) != 0 )
3071 {
3072 mbedtls_ssl_conf_curves( &conf, curve_list );
3073 }
3074#endif
3075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003077
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02003078 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
3079 {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003080#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00003081 if( opt.psk_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003082 {
Hanno Becker32809e82019-01-25 14:27:15 +00003083 status = psa_allocate_key( &psk_slot );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003084 if( status != PSA_SUCCESS )
3085 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00003086 fprintf( stderr, "ALLOC FAIL\n" );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003087 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
3088 goto exit;
3089 }
3090
Hanno Becker1d911cd2018-11-15 13:06:09 +00003091 /* The algorithm has already been determined earlier. */
3092 status = psa_setup_psk_key_slot( psk_slot, alg, psk, psk_len );
3093 if( status != PSA_SUCCESS )
3094 {
3095 fprintf( stderr, "SETUP FAIL\n" );
3096 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
3097 goto exit;
3098 }
3099 if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, psk_slot,
3100 (const unsigned char *) opt.psk_identity,
3101 strlen( opt.psk_identity ) ) ) != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003102 {
3103 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
3104 ret );
3105 goto exit;
3106 }
3107 }
3108 else
3109#endif /* MBEDTLS_USE_PSA_CRYPTO */
3110 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
3111 (const unsigned char *) opt.psk_identity,
3112 strlen( opt.psk_identity ) ) ) != 0 )
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02003113 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003114 mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02003115 goto exit;
3116 }
3117 }
3118
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02003119 if( opt.psk_list != NULL )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003120 {
3121#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00003122 if( opt.psk_list_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003123 {
3124 psk_entry *cur_psk;
3125 for( cur_psk = psk_info; cur_psk != NULL; cur_psk = cur_psk->next )
3126 {
Hanno Becker32809e82019-01-25 14:27:15 +00003127 status = psa_allocate_key( &cur_psk->slot );
Hanno Becker1d911cd2018-11-15 13:06:09 +00003128 if( status != PSA_SUCCESS )
3129 {
3130 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
3131 goto exit;
3132 }
3133
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003134 status = psa_setup_psk_key_slot( cur_psk->slot, alg,
3135 cur_psk->key,
3136 cur_psk->key_len );
3137 if( status != PSA_SUCCESS )
3138 {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003139 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
3140 goto exit;
3141 }
3142 }
3143 }
3144#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker1d911cd2018-11-15 13:06:09 +00003145
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003146 mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01003147 }
Paul Bakkered27a042013-04-18 22:46:23 +02003148#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150#if defined(MBEDTLS_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00003151 /*
3152 * Use different group than default DHM group
3153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003155 if( opt.dhm_file != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003156 ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003157#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003158 if( ret != 0 )
3159 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003160 mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02003161 goto exit;
3162 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003163#endif
3164
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02003165 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003166 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00003167
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02003168 if( opt.max_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003169 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakkerc1516be2013-06-29 16:01:32 +02003170
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003171 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
3172 {
3173 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
3174 goto exit;
3175 }
3176
3177 if( opt.nbio == 2 )
3178 mbedtls_ssl_set_bio( &ssl, &client_fd, my_send, my_recv, NULL );
3179 else
3180 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02003181 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003182
Hanno Beckera0e20d02019-05-15 14:03:01 +01003183#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera7d25422019-04-09 17:28:10 +01003184 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3185 {
3186 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
3187 cid, cid_len ) ) != 0 )
3188 {
3189 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
3190 ret );
3191 goto exit;
3192 }
3193 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003194#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +01003195
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02003196#if defined(MBEDTLS_SSL_PROTO_DTLS)
3197 if( opt.dtls_mtu != DFL_DTLS_MTU )
3198 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
3199#endif
3200
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003201#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02003202 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
3203 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003204#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02003205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003207
3208reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02003209#if !defined(_WIN32)
3210 if( received_sigterm )
3211 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01003212 mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" );
3213 if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT )
3214 ret = 0;
3215
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02003216 goto exit;
3217 }
3218#endif
3219
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003220 if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
3221 {
3222 mbedtls_printf( " ! Client initiated reconnection from same port\n" );
3223 goto handshake;
3224 }
3225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003227 if( ret != 0 )
3228 {
3229 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 mbedtls_strerror( ret, error_buf, 100 );
3231 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003232 }
3233#endif
3234
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003235 mbedtls_net_free( &client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01003236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237 mbedtls_ssl_session_reset( &ssl );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003238
3239 /*
3240 * 3. Wait until a client connects
3241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 mbedtls_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003243 fflush( stdout );
3244
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003245 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02003246 client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003247 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02003248#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003249 if( received_sigterm )
3250 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01003251 mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" );
3252 if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED )
3253 ret = 0;
3254
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003255 goto exit;
3256 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02003257#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259 mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003260 goto exit;
3261 }
3262
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003263 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003264 ret = mbedtls_net_set_nonblock( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003265 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003266 ret = mbedtls_net_set_block( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003267 if( ret != 0 )
3268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003269 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003270 goto exit;
3271 }
3272
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003273 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
3276 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003277 {
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02003278 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
3279 client_ip, cliip_len ) ) != 0 )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003280 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01003281 mbedtls_printf( " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n",
3282 -ret );
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003283 goto exit;
3284 }
3285 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003287
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02003288#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3289 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
3290 {
3291 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
3292 (const unsigned char *) opt.ecjpake_pw,
3293 strlen( opt.ecjpake_pw ) ) ) != 0 )
3294 {
3295 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
3296 goto exit;
3297 }
3298 }
3299#endif
3300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003302
3303 /*
3304 * 4. Handshake
3305 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003306handshake:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003308 fflush( stdout );
3309
Hanno Becker16970d22017-10-10 15:56:37 +01003310 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003311 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003312#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003313 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003314 ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003315 {
3316 mbedtls_printf( " cancelling on injected error\n" );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003317 break;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003318 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003319#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskineb44692f2018-04-24 12:18:19 +02003320
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003321 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003322 break;
3323
3324 /* For event-driven IO, wait for socket to become available */
3325 if( opt.event == 1 /* level triggered IO */ )
3326 {
3327#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003328 ret = idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003329#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003330 ret = idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003331#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003332 if( ret != 0 )
3333 goto reset;
Hanno Becker16970d22017-10-10 15:56:37 +01003334 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003335 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 mbedtls_printf( " hello verification requested\n" );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003340 ret = 0;
3341 goto reset;
3342 }
3343 else if( ret != 0 )
3344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02003346
3347#if defined(MBEDTLS_X509_CRT_PARSE_C)
3348 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3349 {
3350 char vrfy_buf[512];
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02003351 flags = mbedtls_ssl_get_verify_result( &ssl );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02003352
3353 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
3354
3355 mbedtls_printf( "%s\n", vrfy_buf );
3356 }
3357#endif
3358
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003359#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003360 if( opt.async_private_error < 0 )
3361 /* Injected error only the first time round, to test reset */
3362 ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
3363#endif
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003364 goto reset;
3365 }
3366 else /* ret == 0 */
3367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
3369 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003370 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
3373 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02003374 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02003376
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003377#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3378 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
3379 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
3380#endif
3381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003383 if( opt.alpn_string != NULL )
3384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
3386 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003387 alp ? alp : "(none)" );
3388 }
3389#endif
3390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003392 /*
Ron Eldor2a47be52017-06-20 15:23:23 +03003393 * 5. Verify the client certificate
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003396
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003397 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003398 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003399 char vrfy_buf[512];
3400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401 mbedtls_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003402
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003403 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003404
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003405 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003406 }
3407 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003409
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003410 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003411 {
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003412 char crt_buf[512];
3413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 mbedtls_printf( " . Peer certificate information ...\n" );
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003415 mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 mbedtls_ssl_get_peer_cert( &ssl ) );
Manuel Pégourié-Gonnard67557172015-07-02 11:15:48 +02003417 mbedtls_printf( "%s\n", crt_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003418 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003420
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003421#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Ron Eldor51d3ab52019-05-12 14:54:30 +03003422 if( opt.eap_tls != 0 )
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003423 {
3424 size_t j = 0;
Ron Eldor51d3ab52019-05-12 14:54:30 +03003425
3426 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type,
3427 eap_tls_keying.master_secret,
3428 sizeof( eap_tls_keying.master_secret ),
3429 eap_tls_label,
3430 eap_tls_keying.randbytes,
3431 sizeof( eap_tls_keying.randbytes ),
3432 eap_tls_keymaterial,
3433 sizeof( eap_tls_keymaterial ) ) )
3434 != 0 )
3435 {
3436 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
3437 -ret );
3438 goto exit;
3439 }
3440
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003441 mbedtls_printf( " EAP-TLS key material is:" );
3442 for( j = 0; j < sizeof( eap_tls_keymaterial ); j++ )
3443 {
3444 if( j % 8 == 0 )
3445 mbedtls_printf("\n ");
3446 mbedtls_printf("%02x ", eap_tls_keymaterial[j] );
3447 }
3448 mbedtls_printf("\n");
3449
Ron Eldor51d3ab52019-05-12 14:54:30 +03003450 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type, NULL, 0,
Ron Eldor51c45072019-05-15 17:49:54 +03003451 eap_tls_label,
3452 eap_tls_keying.randbytes,
3453 sizeof( eap_tls_keying.randbytes ),
3454 eap_tls_iv,
3455 sizeof( eap_tls_iv ) ) ) != 0 )
Ron Eldor51d3ab52019-05-12 14:54:30 +03003456 {
3457 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
3458 -ret );
3459 goto exit;
3460 }
3461
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003462 mbedtls_printf( " EAP-TLS IV is:" );
3463 for( j = 0; j < sizeof( eap_tls_iv ); j++ )
3464 {
3465 if( j % 8 == 0 )
3466 mbedtls_printf("\n ");
3467 mbedtls_printf("%02x ", eap_tls_iv[j] );
3468 }
3469 mbedtls_printf("\n");
3470 }
3471#endif
Hanno Beckera7d25422019-04-09 17:28:10 +01003472
Hanno Beckera0e20d02019-05-15 14:03:01 +01003473#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003474 ret = report_cid_usage( &ssl, "initial handshake" );
3475 if( ret != 0 )
3476 goto exit;
3477
Hanno Beckera7d25422019-04-09 17:28:10 +01003478 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3479 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003480 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
3481 cid_renego, cid_renego_len ) ) != 0 )
Hanno Beckera7d25422019-04-09 17:28:10 +01003482 {
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003483 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
3484 ret );
Hanno Beckera7d25422019-04-09 17:28:10 +01003485 goto exit;
3486 }
Hanno Beckera7d25422019-04-09 17:28:10 +01003487 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003488#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera7d25422019-04-09 17:28:10 +01003489
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02003490 if( opt.exchanges == 0 )
3491 goto close_notify;
3492
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003493 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003494data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003495 /*
3496 * 6. Read the HTTP Request
3497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498 mbedtls_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003499 fflush( stdout );
3500
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003501 /*
3502 * TLS and DTLS need different reading styles (stream vs datagram)
3503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003504 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003505 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003506 do
3507 {
3508 int terminated = 0;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003509 len = opt.buffer_size - 1;
3510 memset( buf, 0, opt.buffer_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003512
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003513 if( mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003514 {
3515 if( opt.event == 1 /* level triggered IO */ )
3516 {
3517#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003518 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003519#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003520 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003521#endif
3522 }
3523
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003524 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01003525 }
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003526
3527 if( ret <= 0 )
3528 {
3529 switch( ret )
3530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3532 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003533 goto close_notify;
3534
3535 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536 case MBEDTLS_ERR_NET_CONN_RESET:
3537 mbedtls_printf( " connection was reset by peer\n" );
3538 ret = MBEDTLS_ERR_NET_CONN_RESET;
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003539 goto reset;
3540
3541 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003543 goto reset;
3544 }
3545 }
3546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547 if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003548 {
3549 len = ret;
3550 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003551 mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003552
3553 /* End of message should be detected according to the syntax of the
3554 * application protocol (eg HTTP), just use a dummy test here. */
3555 if( buf[len - 1] == '\n' )
3556 terminated = 1;
3557 }
3558 else
3559 {
3560 int extra_len, ori_len;
3561 unsigned char *larger_buf;
3562
3563 ori_len = ret;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02003564 extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003565
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003566 larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003567 if( larger_buf == NULL )
3568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 mbedtls_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003570 ret = 1;
3571 goto reset;
3572 }
3573
3574 memset( larger_buf, 0, ori_len + extra_len );
3575 memcpy( larger_buf, buf, ori_len );
3576
3577 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003579 if( ret != extra_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003583 ret = 1;
3584 goto reset;
3585 }
3586
3587 larger_buf[ori_len + extra_len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 mbedtls_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003589 ori_len + extra_len, ori_len, extra_len,
3590 (char *) larger_buf );
3591
3592 /* End of message should be detected according to the syntax of the
3593 * application protocol (eg HTTP), just use a dummy test here. */
3594 if( larger_buf[ori_len + extra_len - 1] == '\n' )
3595 terminated = 1;
3596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597 mbedtls_free( larger_buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003598 }
3599
3600 if( terminated )
3601 {
3602 ret = 0;
3603 break;
3604 }
3605 }
3606 while( 1 );
3607 }
3608 else /* Not stream, so datagram */
3609 {
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003610 len = opt.buffer_size - 1;
3611 memset( buf, 0, opt.buffer_size );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003612
Gilles Peskineb44692f2018-04-24 12:18:19 +02003613 do
Hanno Becker16970d22017-10-10 15:56:37 +01003614 {
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003615 /* Without the call to `mbedtls_ssl_check_pending`, it might
3616 * happen that the client sends application data in the same
3617 * datagram as the Finished message concluding the handshake.
3618 * In this case, the application data would be ready to be
3619 * processed while the underlying transport wouldn't signal
3620 * any further incoming data.
3621 *
3622 * See the test 'Event-driven I/O: session-id resume, UDP packing'
3623 * in tests/ssl-opt.sh.
3624 */
3625
3626 /* For event-driven IO, wait for socket to become available */
3627 if( mbedtls_ssl_check_pending( &ssl ) == 0 &&
3628 opt.event == 1 /* level triggered IO */ )
3629 {
3630#if defined(MBEDTLS_TIMING_C)
3631 idle( &client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ );
3632#else
3633 idle( &client_fd, MBEDTLS_ERR_SSL_WANT_READ );
3634#endif
3635 }
3636
Hanno Becker16970d22017-10-10 15:56:37 +01003637 ret = mbedtls_ssl_read( &ssl, buf, len );
3638
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003639 /* Note that even if `mbedtls_ssl_check_pending` returns true,
3640 * it can happen that the subsequent call to `mbedtls_ssl_read`
3641 * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
3642 * might be discarded (e.g. because they are retransmissions). */
Hanno Becker16970d22017-10-10 15:56:37 +01003643 }
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003644 while( mbedtls_status_is_ssl_in_progress( ret ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003645
3646 if( ret <= 0 )
3647 {
3648 switch( ret )
3649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3651 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003652 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003653 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003654
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003655 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003657 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003658 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003659 }
3660
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003661 len = ret;
3662 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003664 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003665 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003666
3667 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003668 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02003669 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003670 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00003672 if( opt.renegotiate && exchanges_left == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674 mbedtls_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003675 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003678 {
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003679 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003681 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003682 goto reset;
3683 }
Hanno Becker16970d22017-10-10 15:56:37 +01003684
3685 /* For event-driven IO, wait for socket to become available */
3686 if( opt.event == 1 /* level triggered IO */ )
3687 {
3688#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003689 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003690#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003691 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003692#endif
3693 }
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003694 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003697 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003699
Hanno Beckera0e20d02019-05-15 14:03:01 +01003700#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003701 ret = report_cid_usage( &ssl, "after renegotiation" );
3702 if( ret != 0 )
3703 goto exit;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003704#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb42ec0d2019-05-03 17:30:59 +01003705
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003706 /*
3707 * 7. Write the 200 Response
3708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709 mbedtls_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003710 fflush( stdout );
3711
3712 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003714
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003715 /* Add padding to the response to reach opt.response_size in length */
3716 if( opt.response_size != DFL_RESPONSE_SIZE &&
3717 len < opt.response_size )
3718 {
3719 memset( buf + len, 'B', opt.response_size - len );
3720 len += opt.response_size - len;
3721 }
3722
3723 /* Truncate if response size is smaller than the "natural" size */
3724 if( opt.response_size != DFL_RESPONSE_SIZE &&
3725 len > opt.response_size )
3726 {
3727 len = opt.response_size;
3728
3729 /* Still end with \r\n unless that's really not possible */
3730 if( len >= 2 ) buf[len - 2] = '\r';
3731 if( len >= 1 ) buf[len - 1] = '\n';
3732 }
3733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003735 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003736 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003739 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003744 goto reset;
3745 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003746
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003747 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003749 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003750 goto reset;
3751 }
Hanno Becker16970d22017-10-10 15:56:37 +01003752
3753 /* For event-driven IO, wait for socket to become available */
3754 if( opt.event == 1 /* level triggered IO */ )
3755 {
3756#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003757 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003758#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003759 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003760#endif
3761 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003762 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003763 }
3764 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003765 else /* Not stream, so datagram */
3766 {
Hanno Becker16970d22017-10-10 15:56:37 +01003767 while( 1 )
3768 {
3769 ret = mbedtls_ssl_write( &ssl, buf, len );
3770
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003771 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003772 break;
3773
3774 /* For event-driven IO, wait for socket to become available */
3775 if( opt.event == 1 /* level triggered IO */ )
3776 {
3777#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003778 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003779#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003780 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003781#endif
3782 }
3783 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003784
3785 if( ret < 0 )
3786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003788 goto reset;
3789 }
3790
3791 frags = 1;
3792 written = ret;
3793 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003794
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003795 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003796 mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Manuel Pégourié-Gonnarda92ed482015-01-14 10:46:08 +01003797 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01003798
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003799 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003800 * 7b. Continue doing data exchanges?
3801 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003802 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003803 goto data_exchange;
3804
3805 /*
3806 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003807 */
3808close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003809 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01003810
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003811 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003812 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003813 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003814 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816 mbedtls_printf( " done\n" );
Rich Evansf90016a2015-01-19 14:26:37 +00003817
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003818 goto reset;
3819
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003820 /*
3821 * Cleanup and exit
3822 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003823exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003825 if( ret != 0 )
3826 {
3827 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828 mbedtls_strerror( ret, error_buf, 100 );
3829 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003830 }
3831#endif
3832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003833 mbedtls_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003834 fflush( stdout );
3835
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003836 mbedtls_net_free( &client_fd );
3837 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +02003838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003839#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3840 mbedtls_dhm_free( &dhm );
Paul Bakkera317a982014-06-18 16:44:11 +02003841#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842#if defined(MBEDTLS_X509_CRT_PARSE_C)
3843 mbedtls_x509_crt_free( &cacert );
3844 mbedtls_x509_crt_free( &srvcert );
3845 mbedtls_pk_free( &pkey );
3846 mbedtls_x509_crt_free( &srvcert2 );
3847 mbedtls_pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02003848#endif
Gilles Peskine44817442018-06-13 18:09:28 +02003849#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3850 for( i = 0; (size_t) i < ssl_async_keys.slots_used; i++ )
3851 {
3852 if( ssl_async_keys.slots[i].pk_owned )
3853 {
3854 mbedtls_pk_free( ssl_async_keys.slots[i].pk );
3855 mbedtls_free( ssl_async_keys.slots[i].pk );
3856 ssl_async_keys.slots[i].pk = NULL;
3857 }
3858 }
3859#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02003860#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003861 sni_free( sni_info );
3862#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00003864 if( ( ret = psk_free( psk_info ) ) != 0 )
3865 mbedtls_printf( "Failed to list of opaque PSKs - error was %d\n", ret );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003866#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3868 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003869#endif
Paul Bakkered27a042013-04-18 22:46:23 +02003870
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00003871#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
3872 defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00003873 if( opt.psk_opaque != 0 )
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00003874 {
3875 /* This is ok even if the slot hasn't been
3876 * initialized (we might have jumed here
3877 * immediately because of bad cmd line params,
3878 * for example). */
Hanno Becker1d911cd2018-11-15 13:06:09 +00003879 status = psa_destroy_key( psk_slot );
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00003880 if( status != PSA_SUCCESS )
3881 {
3882 mbedtls_printf( "Failed to destroy key slot %u - error was %d",
Hanno Becker1d911cd2018-11-15 13:06:09 +00003883 (unsigned) psk_slot, (int) status );
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00003884 }
3885 }
3886#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED &&
3887 MBEDTLS_USE_PSA_CRYPTO */
3888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003889 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003890 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003891 mbedtls_ctr_drbg_free( &ctr_drbg );
3892 mbedtls_entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003894#if defined(MBEDTLS_SSL_CACHE_C)
3895 mbedtls_ssl_cache_free( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00003896#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003897#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3898 mbedtls_ssl_ticket_free( &ticket_ctx );
3899#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003900#if defined(MBEDTLS_SSL_COOKIE_C)
3901 mbedtls_ssl_cookie_free( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02003902#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003903
Hanno Becker095d9cf2018-10-09 12:39:13 +01003904 mbedtls_free( buf );
3905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003906#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
3907#if defined(MBEDTLS_MEMORY_DEBUG)
3908 mbedtls_memory_buffer_alloc_status();
Paul Bakker82024bf2013-07-04 11:52:32 +02003909#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003910 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +02003911#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02003912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003913 mbedtls_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003914
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003915#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003917 fflush( stdout ); getchar();
3918#endif
3919
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02003920 // Shell can not handle large exit numbers -> 1 for errors
3921 if( ret < 0 )
3922 ret = 1;
3923
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003924 return( ret );
3925}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
3927 MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003928 MBEDTLS_CTR_DRBG_C */