blob: b0ffc753eb5ad51ed88d71871b2f8c3d53e1d1cf [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
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100148#define DFL_AUTH_MODE -1
Janos Follath4817e272017-04-10 13:44:33 +0100149#define DFL_CERT_REQ_CA_LIST MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100151#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200153#define DFL_TICKET_TIMEOUT 86400
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100154#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100155#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100156#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200157#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100158#define DFL_CURVES NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200159#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200161#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200162#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200163#define DFL_HS_TO_MIN 0
164#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200165#define DFL_DTLS_MTU -1
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200166#define DFL_BADMAC_LIMIT -1
Hanno Beckere7675d02018-08-14 13:28:56 +0100167#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200168#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100169#define DFL_ETM -1
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200170#define DFL_CA_CALLBACK 0
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300171#define DFL_EAP_TLS 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000172
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200173#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
174 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
175 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
176 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
177 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
178 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
179 "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 +0200180
Paul Bakker8e714d72013-07-18 11:05:13 +0200181/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
182 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000183#define HTTP_RESPONSE \
184 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000185 "<h2>mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200186 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000187
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200188/*
189 * Size of the basic I/O buffer. Able to hold our default response.
190 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 * 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 +0200192 * if you change this value to something outside the range <= 100 or > 500
193 */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200194#define DFL_IO_BUF_LEN 200
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#if defined(MBEDTLS_X509_CRT_PARSE_C)
197#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000198#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100199 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
200 " default: \"\" (pre-loaded)\n" \
Hanno Becker422d1992019-05-01 11:16:28 +0100201 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100202 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
203 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
Hanno Becker422d1992019-05-01 11:16:28 +0100204 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100205 " 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 +0200206 " default: see note after key_file2\n" \
207 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200208 " 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 +0200209 " default: see note after key_file2\n" \
210 " key_file2=%%s default: see note below\n" \
211 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200212 " preloaded certificate(s) and key(s) are used if available\n" \
213 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
214 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000215#else
216#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200217 "\n" \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 " No file operations available (MBEDTLS_FS_IO not defined)\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200219 "\n"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200221#else
222#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200224
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200225#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100226#define USAGE_SSL_ASYNC \
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100227 " async_operations=%%c... d=decrypt, s=sign (default: -=off)\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100228 " async_private_delay1=%%d Asynchronous delay for key_file or preloaded key\n" \
Gilles Peskine166ce742018-04-30 10:30:49 +0200229 " async_private_delay2=%%d Asynchronous delay for key_file2 and sni\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100230 " default: -1 (not asynchronous)\n" \
Gilles Peskine60ee4ca2018-01-08 11:28:05 +0100231 " async_private_error=%%d Async callback error injection (default=0=none,\n" \
Gilles Peskinec9125722018-04-26 07:15:40 +0200232 " 1=start, 2=cancel, 3=resume, negative=first time only)"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100233#else
234#define USAGE_SSL_ASYNC ""
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200235#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100238#define USAGE_PSK_RAW \
Andrzej Kurekb274f272019-02-05 05:06:35 -0500239 " psk=%%s default: \"\" (in hex, without 0x)\n" \
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100240 " psk_list=%%s default: \"\"\n" \
Andrzej Kurekb274f272019-02-05 05:06:35 -0500241 " A list of (PSK identity, PSK value) pairs.\n" \
242 " The PSK values are in hex, without 0x.\n" \
243 " id1,psk1[,id2,psk2[,...]]\n" \
244 " psk_identity=%%s default: \"Client_identity\"\n"
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100245#if defined(MBEDTLS_USE_PSA_CRYPTO)
246#define USAGE_PSK_SLOT \
Hanno Becker1d911cd2018-11-15 13:06:09 +0000247 " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
248 " Enable this to store the PSK configured through command line\n" \
249 " parameter `psk` in a PSA-based key slot.\n" \
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100250 " Note: Currently only supported in conjunction with\n" \
251 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
252 " to force a particular PSK-only ciphersuite.\n" \
253 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
254 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
255 " with prepopulated key slots instead of importing raw key material.\n" \
Hanno Becker1d911cd2018-11-15 13:06:09 +0000256 " psk_list_opaque=%%d default: 0 (don't use opaque dynamic PSKs)\n" \
257 " Enable this to store the list of dynamically chosen PSKs configured\n" \
258 " through the command line parameter `psk_list` in PSA-based key slots.\n" \
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100259 " Note: Currently only supported in conjunction with\n" \
260 " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
261 " to force a particular PSK-only ciphersuite.\n" \
262 " Note: This is to test integration of PSA-based opaque PSKs with\n" \
263 " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
264 " with prepopulated key slots instead of importing raw key material.\n"
265#else
266#define USAGE_PSK_SLOT ""
267#endif /* MBEDTLS_USE_PSA_CRYPTO */
268#define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
Paul Bakkered27a042013-04-18 22:46:23 +0200269#else
270#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200272#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
273#define USAGE_CA_CALLBACK \
274 " ca_callback=%%d default: 0 (disabled)\n" \
275 " Enable this to use the trusted certificate callback function\n"
276#else
277#define USAGE_CA_CALLBACK ""
278#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200280#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100281 " tickets=%%d default: 1 (enabled)\n" \
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200282 " ticket_timeout=%%d default: 86400 (one day)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200283#else
284#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200286
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300287#if defined(MBEDTLS_SSL_EXPORT_KEYS)
288#define USAGE_EAP_TLS \
289 " eap_tls=%%d default: 0 (disabled)\n"
290#else
291#define USAGE_EAP_TLS ""
292#endif /* MBEDTLS_SSL_EXPORT_KEYS */
293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100295#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100296 " cache_max=%%d default: cache default (50)\n" \
297 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100298#else
299#define USAGE_CACHE ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#endif /* MBEDTLS_SSL_CACHE_C */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100301
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200302#if defined(SNI_OPTION)
Ron Eldor80d04192019-04-04 15:02:01 +0300303#if defined(MBEDTLS_X509_CRL_PARSE_C)
304#define SNI_CRL ",crl"
305#else
306#define SNI_CRL ""
307#endif
308
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100309#define USAGE_SNI \
Ron Eldor80d04192019-04-04 15:02:01 +0300310 " sni=%%s name1,cert1,key1,ca1"SNI_CRL",auth1[,...]\n" \
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200311 " default: disabled\n"
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100312#else
313#define USAGE_SNI ""
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200314#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200317#define USAGE_MAX_FRAG_LEN \
318 " max_frag_len=%%d default: 16384 (tls default)\n" \
319 " options: 512, 1024, 2048, 4096\n"
320#else
321#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100325#define USAGE_TRUNC_HMAC \
326 " trunc_hmac=%%d default: library default\n"
327#else
328#define USAGE_TRUNC_HMAC ""
329#endif
330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200332#define USAGE_ALPN \
333 " alpn=%%s default: \"\" (disabled)\n" \
334 " example: spdy/1,http/1.1\n"
335#else
336#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200340#define USAGE_COOKIES \
341 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200342 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200343#else
344#define USAGE_COOKIES ""
345#endif
346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200348#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200349 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200350#else
351#define USAGE_ANTI_REPLAY ""
352#endif
353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200355#define USAGE_BADMAC_LIMIT \
356 " badmac_limit=%%d default: (library default: disabled)\n"
357#else
358#define USAGE_BADMAC_LIMIT ""
359#endif
360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200362#define USAGE_DTLS \
363 " dtls=%%d default: 0 (TLS)\n" \
364 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200365 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Beckere7675d02018-08-14 13:28:56 +0100366 " mtu=%%d default: (library default: unlimited)\n" \
367 " dgram_packing=%%d default: 1 (allowed)\n" \
368 " allow or forbid packing of multiple\n" \
369 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200370#else
371#define USAGE_DTLS ""
372#endif
373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200375#define USAGE_EMS \
376 " extended_ms=0/1 default: (library default: on)\n"
377#else
378#define USAGE_EMS ""
379#endif
380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100382#define USAGE_ETM \
383 " etm=0/1 default: (library default: on)\n"
384#else
385#define USAGE_ETM ""
386#endif
387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100389#define USAGE_RENEGO \
390 " renegotiation=%%d default: 0 (disabled)\n" \
391 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100392 " renego_delay=%%d default: -2 (library default)\n" \
Andres AG692ad842017-01-19 16:30:57 +0000393 " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100394#else
395#define USAGE_RENEGO ""
396#endif
397
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200398#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
399#define USAGE_ECJPAKE \
400 " ecjpake_pw=%%s default: none (disabled)\n"
401#else
402#define USAGE_ECJPAKE ""
403#endif
404
Hanno Beckere6706e62017-05-15 16:05:15 +0100405#if defined(MBEDTLS_ECP_C)
406#define USAGE_CURVES \
407 " curves=a,b,c,d default: \"default\" (library default)\n" \
408 " example: \"secp521r1,brainpoolP512r1\"\n" \
409 " - use \"none\" for empty list\n" \
410 " - see mbedtls_ecp_curve_list()\n" \
411 " for acceptable curve names\n"
412#else
413#define USAGE_CURVES ""
414#endif
415
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000416#define USAGE \
417 "\n usage: ssl_server2 param=<>...\n" \
418 "\n acceptable parameters:\n" \
Ron Eldor71f68c42017-09-26 11:29:11 +0300419 " server_addr=%%s default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000420 " server_port=%%d default: 4433\n" \
421 " debug_level=%%d default: 0 (disabled)\n" \
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200422 " buffer_size=%%d default: 200 \n" \
423 " (minimum: 1, max: 16385)\n" \
424 " response_size=%%d default: about 152 (basic response)\n" \
425 " (minimum: 0, max: 16384)\n" \
426 " increases buffer_size if bigger\n"\
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100427 " nbio=%%d default: 0 (blocking I/O)\n" \
428 " options: 1 (non-blocking), 2 (added delays)\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100429 " event=%%d default: 0 (loop)\n" \
430 " options: 1 (level-triggered, implies nbio=1),\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200431 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100432 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200433 USAGE_DTLS \
434 USAGE_COOKIES \
435 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200436 USAGE_BADMAC_LIMIT \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200437 "\n" \
Manuel Pégourié-Gonnardee6139c2015-05-07 10:18:26 +0100438 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100439 " options: none, optional, required\n" \
Janos Follath4817e272017-04-10 13:44:33 +0100440 " cert_req_ca_list=%%d default: 1 (send ca list)\n" \
441 " options: 1 (send ca list), 0 (don't send)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000442 USAGE_IO \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100443 USAGE_SSL_ASYNC \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100444 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100445 "\n" \
446 USAGE_PSK \
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200447 USAGE_CA_CALLBACK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200448 USAGE_ECJPAKE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100449 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100450 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100451 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200452 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200453 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100454 USAGE_TICKETS \
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300455 USAGE_EAP_TLS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100456 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100457 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100458 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200459 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200460 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100461 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100462 USAGE_CURVES \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100463 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100464 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200465 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200466 " min_version=%%s default: (library default: tls1)\n" \
467 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200468 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100469 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200470 "\n" \
471 " version_suites=a,b,c,d per-version ciphersuites\n" \
472 " in order from ssl3 to tls1_2\n" \
473 " default: all enabled\n" \
474 " force_ciphersuite=<name> default: all enabled\n" \
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100475 " query_config=<name> return 0 if the specified\n" \
476 " configuration macro is defined and 1\n" \
477 " otherwise. The expansion of the macro\n" \
478 " is printed if it is defined\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000479 " acceptable ciphersuite names:\n"
480
Andres AG692ad842017-01-19 16:30:57 +0000481
Hanno Becker8651a432017-06-09 16:13:22 +0100482#define ALPN_LIST_SIZE 10
483#define CURVE_LIST_SIZE 20
484
Andres AG692ad842017-01-19 16:30:57 +0000485#define PUT_UINT64_BE(out_be,in_le,i) \
486{ \
487 (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \
488 (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \
489 (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \
490 (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \
491 (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \
492 (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \
493 (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \
494 (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \
495}
496
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500497#if defined(MBEDTLS_CHECK_PARAMS)
498#include "mbedtls/platform_util.h"
499void mbedtls_param_failed( const char *failure_condition,
500 const char *file,
501 int line )
502{
503 mbedtls_printf( "%s:%i: Input param failed - %s\n",
504 file, line, failure_condition );
505 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
506}
507#endif
508
Rich Evans85b05ec2015-02-12 11:37:29 +0000509/*
510 * global options
511 */
512struct options
513{
514 const char *server_addr; /* address on which the ssl service runs */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200515 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000516 int debug_level; /* level of debugging */
517 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100518 int event; /* loop or event-driven IO? level or edge triggered? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200520 int response_size; /* pad response with header to requested size */
521 uint16_t buffer_size; /* IO buffer size */
Rich Evans85b05ec2015-02-12 11:37:29 +0000522 const char *ca_file; /* the file with the CA certificate(s) */
523 const char *ca_path; /* the path with the CA certificate(s) reside */
524 const char *crt_file; /* the file with the server certificate */
525 const char *key_file; /* the file with the server key */
526 const char *crt_file2; /* the file with the 2nd server certificate */
527 const char *key_file2; /* the file with the 2nd server key */
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100528 const char *async_operations; /* supported SSL asynchronous operations */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100529 int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
530 int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
531 int async_private_error; /* inject error in async private callback */
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100532#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +0000533 int psk_opaque;
534 int psk_list_opaque;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100535#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200536#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000537 int ca_callback; /* Use callback for trusted certificate list */
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200538#endif
Rich Evans85b05ec2015-02-12 11:37:29 +0000539 const char *psk; /* the pre-shared key */
540 const char *psk_identity; /* the pre-shared key identity */
541 char *psk_list; /* list of PSK id/key pairs for callback */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200542 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000543 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
544 const char *version_suites; /* per-version ciphersuites */
545 int renegotiation; /* enable / disable renegotiation */
546 int allow_legacy; /* allow legacy renegotiation */
547 int renegotiate; /* attempt renegotiation? */
548 int renego_delay; /* delay before enforcing renegotiation */
Andres AG692ad842017-01-19 16:30:57 +0000549 uint64_t renego_period; /* period for automatic renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000550 int exchanges; /* number of data exchanges */
551 int min_version; /* minimum protocol version accepted */
552 int max_version; /* maximum protocol version accepted */
553 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200554 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000555 int auth_mode; /* verify mode for connection */
Janos Follath4817e272017-04-10 13:44:33 +0100556 int cert_req_ca_list; /* should we send the CA list? */
Rich Evans85b05ec2015-02-12 11:37:29 +0000557 unsigned char mfl_code; /* code for maximum fragment length */
558 int trunc_hmac; /* accept truncated hmac? */
559 int tickets; /* enable / disable session tickets */
560 int ticket_timeout; /* session ticket lifetime */
561 int cache_max; /* max number of session cache entries */
562 int cache_timeout; /* expiration delay of session cache entries */
563 char *sni; /* string describing sni information */
Hanno Beckere6706e62017-05-15 16:05:15 +0100564 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000565 const char *alpn_string; /* ALPN supported protocols */
566 const char *dhm_file; /* the file with the DH parameters */
567 int extended_ms; /* allow negotiation of extended MS? */
568 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000569 int transport; /* TLS or DTLS? */
570 int cookies; /* Use cookies for DTLS? -1 to break them */
571 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
572 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
573 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200574 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Hanno Beckere7675d02018-08-14 13:28:56 +0100575 int dgram_packing; /* allow/forbid datagram packing */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000576 int badmac_limit; /* Limit of records with bad MAC */
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300577 int eap_tls; /* derive EAP-TLS keying material? */
Rich Evans85b05ec2015-02-12 11:37:29 +0000578} opt;
579
Andres Amaya Garciabc818842018-10-16 21:08:38 +0100580int query_config( const char *config );
581
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300582#if defined(MBEDTLS_SSL_EXPORT_KEYS)
583typedef struct eap_tls_keys
584{
585 unsigned char master_secret[48];
586 unsigned char randbytes[64];
Ron Eldor51d3ab52019-05-12 14:54:30 +0300587 mbedtls_tls_prf_types tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300588} eap_tls_keys;
589
590static int eap_tls_key_derivation ( void *p_expkey,
591 const unsigned char *ms,
592 const unsigned char *kb,
593 size_t maclen,
594 size_t keylen,
595 size_t ivlen,
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300596 unsigned char client_random[32],
Ron Eldor51d3ab52019-05-12 14:54:30 +0300597 unsigned char server_random[32],
598 mbedtls_tls_prf_types tls_prf_type )
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300599{
600 eap_tls_keys *keys = (eap_tls_keys *)p_expkey;
601
602 ( ( void ) kb );
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300603 memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) );
604 memcpy( keys->randbytes, client_random, 32 );
605 memcpy( keys->randbytes + 32, server_random, 32 );
Ron Eldor51d3ab52019-05-12 14:54:30 +0300606 keys->tls_prf_type = tls_prf_type;
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300607
Ron Eldorf75e2522019-05-14 20:38:49 +0300608 if( opt.debug_level > 2 )
609 {
Ron Eldor801faf02019-05-15 17:45:24 +0300610 mbedtls_printf("exported maclen is %u\n", (unsigned)maclen);
611 mbedtls_printf("exported keylen is %u\n", (unsigned)keylen);
612 mbedtls_printf("exported ivlen is %u\n", (unsigned)ivlen);
Ron Eldorf75e2522019-05-14 20:38:49 +0300613 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +0300614 return( 0 );
615}
616#endif
617
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200618static void my_debug( void *ctx, int level,
619 const char *file, int line,
620 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000621{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200622 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000623
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200624 /* Extract basename from file */
625 for( p = basename = file; *p != '\0'; p++ )
626 if( *p == '/' || *p == '\\' )
627 basename = p + 1;
628
629 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000630 fflush( (FILE *) ctx );
631}
632
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200633#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckercbb59032019-03-28 14:14:22 +0000634int ca_callback( void *data, mbedtls_x509_crt const *child,
635 mbedtls_x509_crt **candidates)
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200636{
Hanno Beckercbb59032019-03-28 14:14:22 +0000637 int ret = 0;
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200638 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +0000639 mbedtls_x509_crt *first;
640
641 /* This is a test-only implementation of the CA callback
642 * which always returns the entire list of trusted certificates.
643 * Production implementations managing a large number of CAs
644 * should use an efficient presentation and lookup for the
645 * set of trusted certificates (such as a hashtable) and only
646 * return those trusted certificates which satisfy basic
647 * parental checks, such as the matching of child `Issuer`
648 * and parent `Subject` field. */
649 ((void) child);
650
651 first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
652 if( first == NULL )
653 {
654 ret = -1;
655 goto exit;
656 }
657 mbedtls_x509_crt_init( first );
658
659 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
660 {
661 ret = -1;
662 goto exit;
663 }
664
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200665 while( ca->next != NULL )
666 {
667 ca = ca->next;
Hanno Beckercbb59032019-03-28 14:14:22 +0000668 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
669 {
670 ret = -1;
671 goto exit;
672 }
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200673 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000674
675exit:
676
677 if( ret != 0 )
678 {
679 mbedtls_x509_crt_free( first );
680 mbedtls_free( first );
681 first = NULL;
682 }
683
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200684 *candidates = first;
Hanno Beckercbb59032019-03-28 14:14:22 +0000685 return( ret );
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +0200686}
687#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
688
Rich Evans85b05ec2015-02-12 11:37:29 +0000689/*
690 * Test recv/send functions that make sure each try returns
691 * WANT_READ/WANT_WRITE at least once before sucesseding
692 */
693static int my_recv( void *ctx, unsigned char *buf, size_t len )
694{
695 static int first_try = 1;
696 int ret;
697
698 if( first_try )
699 {
700 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100701 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000702 }
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100705 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000706 first_try = 1; /* Next call will be a new operation */
707 return( ret );
708}
709
710static int my_send( void *ctx, const unsigned char *buf, size_t len )
711{
712 static int first_try = 1;
713 int ret;
714
715 if( first_try )
716 {
717 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100718 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000719 }
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100722 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000723 first_try = 1; /* Next call will be a new operation */
724 return( ret );
725}
726
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200727/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200728 * Return authmode from string, or -1 on error
729 */
730static int get_auth_mode( const char *s )
731{
732 if( strcmp( s, "none" ) == 0 )
733 return( MBEDTLS_SSL_VERIFY_NONE );
734 if( strcmp( s, "optional" ) == 0 )
735 return( MBEDTLS_SSL_VERIFY_OPTIONAL );
736 if( strcmp( s, "required" ) == 0 )
737 return( MBEDTLS_SSL_VERIFY_REQUIRED );
738
739 return( -1 );
740}
741
742/*
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200743 * Used by sni_parse and psk_parse to handle coma-separated lists
744 */
745#define GET_ITEM( dst ) \
Hanno Becker1eeca412018-10-15 12:01:35 +0100746 do \
747 { \
748 (dst) = p; \
749 while( *p != ',' ) \
750 if( ++p > end ) \
751 goto error; \
752 *p++ = '\0'; \
753 } while( 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200754
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200755#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100756typedef struct _sni_entry sni_entry;
757
758struct _sni_entry {
759 const char *name;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_x509_crt *cert;
761 mbedtls_pk_context *key;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200762 mbedtls_x509_crt* ca;
763 mbedtls_x509_crl* crl;
764 int authmode;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100765 sni_entry *next;
766};
767
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100768void sni_free( sni_entry *head )
769{
770 sni_entry *cur = head, *next;
771
772 while( cur != NULL )
773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_x509_crt_free( cur->cert );
775 mbedtls_free( cur->cert );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 mbedtls_pk_free( cur->key );
778 mbedtls_free( cur->key );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100779
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200780 mbedtls_x509_crt_free( cur->ca );
781 mbedtls_free( cur->ca );
Ron Eldor80d04192019-04-04 15:02:01 +0300782#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200783 mbedtls_x509_crl_free( cur->crl );
784 mbedtls_free( cur->crl );
Ron Eldor80d04192019-04-04 15:02:01 +0300785#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100786 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_free( cur );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100788 cur = next;
789 }
790}
791
792/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200793 * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
794 * into a usable sni_entry list. For ca1, crl1, auth1, the special value
795 * '-' means unset. If ca1 is unset, then crl1 is ignored too.
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000796 *
797 * Modifies the input string! This is not production quality!
798 */
799sni_entry *sni_parse( char *sni_string )
800{
801 sni_entry *cur = NULL, *new = NULL;
802 char *p = sni_string;
803 char *end = p;
Ron Eldor80d04192019-04-04 15:02:01 +0300804 char *crt_file, *key_file, *ca_file, *auth_str;
805#if defined(MBEDTLS_X509_CRL_PARSE_C)
806 char *crl_file;
807#endif
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000808
809 while( *end != '\0' )
810 ++end;
811 *end = ',';
812
813 while( p <= end )
814 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200815 if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000816 {
817 sni_free( cur );
818 return( NULL );
819 }
820
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200821 GET_ITEM( new->name );
822 GET_ITEM( crt_file );
823 GET_ITEM( key_file );
824 GET_ITEM( ca_file );
Ron Eldor80d04192019-04-04 15:02:01 +0300825#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200826 GET_ITEM( crl_file );
Ron Eldor80d04192019-04-04 15:02:01 +0300827#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200828 GET_ITEM( auth_str );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000829
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200830 if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
831 ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200832 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 mbedtls_x509_crt_init( new->cert );
835 mbedtls_pk_init( new->key );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
838 mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000839 goto error;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200840
841 if( strcmp( ca_file, "-" ) != 0 )
842 {
843 if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
844 goto error;
845
846 mbedtls_x509_crt_init( new->ca );
847
848 if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
849 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000850 }
851
Ron Eldor80d04192019-04-04 15:02:01 +0300852#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200853 if( strcmp( crl_file, "-" ) != 0 )
854 {
855 if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
856 goto error;
857
858 mbedtls_x509_crl_init( new->crl );
859
860 if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
861 goto error;
862 }
Ron Eldor80d04192019-04-04 15:02:01 +0300863#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200864
865 if( strcmp( auth_str, "-" ) != 0 )
866 {
867 if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
868 goto error;
869 }
870 else
871 new->authmode = DFL_AUTH_MODE;
872
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000873 new->next = cur;
874 cur = new;
875 }
876
877 return( cur );
878
879error:
880 sni_free( new );
881 sni_free( cur );
882 return( NULL );
883}
884
885/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100886 * SNI callback.
887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100889 const unsigned char *name, size_t name_len )
890{
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200891 const sni_entry *cur = (const sni_entry *) p_info;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100892
893 while( cur != NULL )
894 {
895 if( name_len == strlen( cur->name ) &&
896 memcmp( name, cur->name, name_len ) == 0 )
897 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200898 if( cur->ca != NULL )
899 mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
900
901 if( cur->authmode != DFL_AUTH_MODE )
902 mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
903
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200904 return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100905 }
906
907 cur = cur->next;
908 }
909
910 return( -1 );
911}
912
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200913#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200916
Hanno Becker1eeca412018-10-15 12:01:35 +0100917#define HEX2NUM( c ) \
918 do \
919 { \
920 if( (c) >= '0' && (c) <= '9' ) \
921 (c) -= '0'; \
922 else if( (c) >= 'a' && (c) <= 'f' ) \
923 (c) -= 'a' - 10; \
924 else if( (c) >= 'A' && (c) <= 'F' ) \
925 (c) -= 'A' - 10; \
926 else \
927 return( -1 ); \
928 } while( 0 )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200929
930/*
931 * Convert a hex string to bytes.
932 * Return 0 on success, -1 on error.
933 */
934int unhexify( unsigned char *output, const char *input, size_t *olen )
935{
936 unsigned char c;
937 size_t j;
938
939 *olen = strlen( input );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 if( *olen % 2 != 0 || *olen / 2 > MBEDTLS_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200941 return( -1 );
942 *olen /= 2;
943
944 for( j = 0; j < *olen * 2; j += 2 )
945 {
946 c = input[j];
947 HEX2NUM( c );
948 output[ j / 2 ] = c << 4;
949
950 c = input[j + 1];
951 HEX2NUM( c );
952 output[ j / 2 ] |= c;
953 }
954
955 return( 0 );
956}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200957
958typedef struct _psk_entry psk_entry;
959
960struct _psk_entry
961{
962 const char *name;
963 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 unsigned char key[MBEDTLS_PSK_MAX_LEN];
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100965#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500966 psa_key_handle_t slot;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +0100967#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200968 psk_entry *next;
969};
970
971/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000972 * Free a list of psk_entry's
973 */
Hanno Beckerc43b6ea2018-11-05 13:48:43 +0000974int psk_free( psk_entry *head )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000975{
976 psk_entry *next;
977
978 while( head != NULL )
979 {
Hanno Beckerc43b6ea2018-11-05 13:48:43 +0000980#if defined(MBEDTLS_USE_PSA_CRYPTO)
981 psa_status_t status;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500982 psa_key_handle_t const slot = head->slot;
Hanno Beckerc43b6ea2018-11-05 13:48:43 +0000983
984 if( slot != 0 )
985 {
986 status = psa_destroy_key( slot );
987 if( status != PSA_SUCCESS )
988 return( status );
989 }
990#endif /* MBEDTLS_USE_PSA_CRYPTO */
991
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000992 next = head->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 mbedtls_free( head );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000994 head = next;
995 }
Hanno Beckerc43b6ea2018-11-05 13:48:43 +0000996
997 return( 0 );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000998}
999
1000/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001001 * Parse a string of pairs name1,key1[,name2,key2[,...]]
1002 * into a usable psk_entry list.
1003 *
1004 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001005 */
1006psk_entry *psk_parse( char *psk_string )
1007{
1008 psk_entry *cur = NULL, *new = NULL;
1009 char *p = psk_string;
1010 char *end = p;
1011 char *key_hex;
1012
1013 while( *end != '\0' )
1014 ++end;
1015 *end = ',';
1016
1017 while( p <= end )
1018 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001019 if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +00001020 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001021
1022 memset( new, 0, sizeof( psk_entry ) );
1023
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +02001024 GET_ITEM( new->name );
1025 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001026
1027 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001028 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001029
1030 new->next = cur;
1031 cur = new;
1032 }
1033
1034 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001035
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +00001036error:
1037 psk_free( new );
1038 psk_free( cur );
1039 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001040}
1041
1042/*
1043 * PSK callback
1044 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001046 const unsigned char *name, size_t name_len )
1047{
1048 psk_entry *cur = (psk_entry *) p_info;
1049
1050 while( cur != NULL )
1051 {
1052 if( name_len == strlen( cur->name ) &&
1053 memcmp( name, cur->name, name_len ) == 0 )
1054 {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001055#if defined(MBEDTLS_USE_PSA_CRYPTO)
1056 if( cur->slot != 0 )
1057 return( mbedtls_ssl_set_hs_psk_opaque( ssl, cur->slot ) );
1058 else
1059#endif
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001060 return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001061 }
1062
1063 cur = cur->next;
1064 }
1065
1066 return( -1 );
1067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +02001069
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001070static mbedtls_net_context listen_fd, client_fd;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +02001071
1072/* Interruption handler to ensure clean exit (for valgrind testing) */
1073#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001074static int received_sigterm = 0;
1075void term_handler( int sig )
1076{
1077 ((void) sig);
1078 received_sigterm = 1;
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02001079 mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
1080 mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001081}
Paul Bakkerc1283d32014-08-18 11:05:51 +02001082#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001083
Gilles Peskine682df092017-05-11 19:01:11 +02001084#if defined(MBEDTLS_X509_CRT_PARSE_C)
1085static int ssl_sig_hashes_for_test[] = {
1086#if defined(MBEDTLS_SHA512_C)
1087 MBEDTLS_MD_SHA512,
1088 MBEDTLS_MD_SHA384,
1089#endif
1090#if defined(MBEDTLS_SHA256_C)
1091 MBEDTLS_MD_SHA256,
1092 MBEDTLS_MD_SHA224,
1093#endif
1094#if defined(MBEDTLS_SHA1_C)
1095 /* Allow SHA-1 as we use it extensively in tests. */
1096 MBEDTLS_MD_SHA1,
1097#endif
1098 MBEDTLS_MD_NONE
1099};
1100#endif /* MBEDTLS_X509_CRT_PARSE_C */
1101
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02001102/** Return true if \p ret is a status code indicating that there is an
1103 * operation in progress on an SSL connection, and false if it indicates
1104 * success or a fatal error.
1105 *
1106 * The possible operations in progress are:
1107 *
1108 * - A read, when the SSL input buffer does not contain a full message.
1109 * - A write, when the SSL output buffer contains some data that has not
1110 * been sent over the network yet.
1111 * - An asynchronous callback that has not completed yet. */
1112static int mbedtls_status_is_ssl_in_progress( int ret )
1113{
1114 return( ret == MBEDTLS_ERR_SSL_WANT_READ ||
1115 ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
1116 ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1117}
1118
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001119#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001120typedef struct
1121{
Gilles Peskine44817442018-06-13 18:09:28 +02001122 mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
1123 mbedtls_pk_context *pk; /*!< Private key */
1124 unsigned delay; /*!< Number of resume steps to go through */
1125 unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001126} ssl_async_key_slot_t;
1127
1128typedef enum {
Gilles Peskinead28bf02018-04-26 00:19:16 +02001129 SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
1130 SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
1131 SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start */
1132 SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
Gilles Peskinec9125722018-04-26 07:15:40 +02001133#define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001134} ssl_async_inject_error_t;
1135
1136typedef struct
1137{
Gilles Peskinee2479892018-06-13 18:06:51 +02001138 ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001139 size_t slots_used;
1140 ssl_async_inject_error_t inject_error;
1141 int (*f_rng)(void *, unsigned char *, size_t);
1142 void *p_rng;
1143} ssl_async_key_context_t;
1144
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001145int ssl_async_set_key( ssl_async_key_context_t *ctx,
Gilles Peskine44817442018-06-13 18:09:28 +02001146 mbedtls_x509_crt *cert,
1147 mbedtls_pk_context *pk,
1148 int pk_take_ownership,
1149 unsigned delay )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001150{
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001151 if( ctx->slots_used >= sizeof( ctx->slots ) / sizeof( *ctx->slots ) )
1152 return( -1 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001153 ctx->slots[ctx->slots_used].cert = cert;
1154 ctx->slots[ctx->slots_used].pk = pk;
1155 ctx->slots[ctx->slots_used].delay = delay;
Gilles Peskine44817442018-06-13 18:09:28 +02001156 ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001157 ++ctx->slots_used;
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001158 return( 0 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001159}
1160
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001161#define SSL_ASYNC_INPUT_MAX_SIZE 512
Gilles Peskined3268832018-04-26 06:23:59 +02001162
1163typedef enum
1164{
1165 ASYNC_OP_SIGN,
1166 ASYNC_OP_DECRYPT,
1167} ssl_async_operation_type_t;
1168/* Note that the enum above and the array below need to be kept in sync!
1169 * `ssl_async_operation_names[op]` is the name of op for each value `op`
1170 * of type `ssl_async_operation_type_t`. */
1171static const char *const ssl_async_operation_names[] =
1172{
1173 "sign",
1174 "decrypt",
1175};
1176
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001177typedef struct
1178{
Gilles Peskine6331d782018-04-26 13:27:43 +02001179 unsigned slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001180 ssl_async_operation_type_t operation_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001181 mbedtls_md_type_t md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001182 unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
1183 size_t input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001184 unsigned remaining_delay;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001185} ssl_async_operation_context_t;
1186
Gilles Peskine8f97af72018-04-26 11:46:10 +02001187static int ssl_async_start( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001188 mbedtls_x509_crt *cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001189 ssl_async_operation_type_t op_type,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001190 mbedtls_md_type_t md_alg,
1191 const unsigned char *input,
1192 size_t input_len )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001193{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001194 ssl_async_key_context_t *config_data =
1195 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskine6331d782018-04-26 13:27:43 +02001196 unsigned slot;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001197 ssl_async_operation_context_t *ctx = NULL;
Gilles Peskined3268832018-04-26 06:23:59 +02001198 const char *op_name = ssl_async_operation_names[op_type];
Gilles Peskinef1127252018-04-24 13:05:39 +02001199
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001200 {
1201 char dn[100];
Gilles Peskined5d983e2018-06-15 14:05:10 +02001202 if( mbedtls_x509_dn_gets( dn, sizeof( dn ), &cert->subject ) > 0 )
1203 mbedtls_printf( "Async %s callback: looking for DN=%s\n",
1204 op_name, dn );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001205 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001206
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001207 /* Look for a private key that matches the public key in cert.
1208 * Since this test code has the private key inside Mbed TLS,
1209 * we call mbedtls_pk_check_pair to match a private key with the
1210 * public key. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001211 for( slot = 0; slot < config_data->slots_used; slot++ )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001212 {
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001213 if( mbedtls_pk_check_pair( &cert->pk,
1214 config_data->slots[slot].pk ) == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001215 break;
1216 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001217 if( slot == config_data->slots_used )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001218 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001219 mbedtls_printf( "Async %s callback: no key matches this certificate.\n",
1220 op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001221 return( MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH );
1222 }
Gilles Peskine6331d782018-04-26 13:27:43 +02001223 mbedtls_printf( "Async %s callback: using key slot %u, delay=%u.\n",
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001224 op_name, slot, config_data->slots[slot].delay );
Gilles Peskinef1127252018-04-24 13:05:39 +02001225
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001226 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001227 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001228 mbedtls_printf( "Async %s callback: injected error\n", op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001229 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1230 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001231
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001232 if( input_len > SSL_ASYNC_INPUT_MAX_SIZE )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001233 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Gilles Peskinef1127252018-04-24 13:05:39 +02001234
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001235 ctx = mbedtls_calloc( 1, sizeof( *ctx ) );
1236 if( ctx == NULL )
1237 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1238 ctx->slot = slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001239 ctx->operation_type = op_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001240 ctx->md_alg = md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001241 memcpy( ctx->input, input, input_len );
1242 ctx->input_len = input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001243 ctx->remaining_delay = config_data->slots[slot].delay;
Gilles Peskinea668c602018-04-30 11:54:39 +02001244 mbedtls_ssl_set_async_operation_data( ssl, ctx );
Gilles Peskinef1127252018-04-24 13:05:39 +02001245
Gilles Peskineceb541b2018-04-26 06:30:45 +02001246 if( ctx->remaining_delay == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001247 return( 0 );
1248 else
1249 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1250}
1251
Gilles Peskine8f97af72018-04-26 11:46:10 +02001252static int ssl_async_sign( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001253 mbedtls_x509_crt *cert,
1254 mbedtls_md_type_t md_alg,
1255 const unsigned char *hash,
1256 size_t hash_len )
1257{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001258 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001259 ASYNC_OP_SIGN, md_alg,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001260 hash, hash_len ) );
1261}
1262
Gilles Peskine8f97af72018-04-26 11:46:10 +02001263static int ssl_async_decrypt( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001264 mbedtls_x509_crt *cert,
1265 const unsigned char *input,
1266 size_t input_len )
1267{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001268 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001269 ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001270 input, input_len ) );
1271}
1272
Gilles Peskine8f97af72018-04-26 11:46:10 +02001273static int ssl_async_resume( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001274 unsigned char *output,
1275 size_t *output_len,
1276 size_t output_size )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001277{
Gilles Peskinea668c602018-04-30 11:54:39 +02001278 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine8f97af72018-04-26 11:46:10 +02001279 ssl_async_key_context_t *config_data =
1280 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001281 ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001282 int ret;
Gilles Peskinef5a99962018-04-30 16:37:23 +02001283 const char *op_name;
Gilles Peskinef1127252018-04-24 13:05:39 +02001284
Gilles Peskineceb541b2018-04-26 06:30:45 +02001285 if( ctx->remaining_delay > 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001286 {
Gilles Peskineceb541b2018-04-26 06:30:45 +02001287 --ctx->remaining_delay;
Gilles Peskine6331d782018-04-26 13:27:43 +02001288 mbedtls_printf( "Async resume (slot %u): call %u more times.\n",
Gilles Peskineceb541b2018-04-26 06:30:45 +02001289 ctx->slot, ctx->remaining_delay );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001290 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1291 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001292
Gilles Peskined3268832018-04-26 06:23:59 +02001293 switch( ctx->operation_type )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001294 {
Gilles Peskined3268832018-04-26 06:23:59 +02001295 case ASYNC_OP_DECRYPT:
Gilles Peskined3268832018-04-26 06:23:59 +02001296 ret = mbedtls_pk_decrypt( key_slot->pk,
1297 ctx->input, ctx->input_len,
1298 output, output_len, output_size,
1299 config_data->f_rng, config_data->p_rng );
1300 break;
1301 case ASYNC_OP_SIGN:
Gilles Peskined3268832018-04-26 06:23:59 +02001302 ret = mbedtls_pk_sign( key_slot->pk,
1303 ctx->md_alg,
1304 ctx->input, ctx->input_len,
1305 output, output_len,
1306 config_data->f_rng, config_data->p_rng );
1307 break;
1308 default:
Gilles Peskine6331d782018-04-26 13:27:43 +02001309 mbedtls_printf( "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
Gilles Peskined3268832018-04-26 06:23:59 +02001310 ctx->slot, (long) ctx->operation_type );
Gilles Peskine44817442018-06-13 18:09:28 +02001311 mbedtls_free( ctx );
Gilles Peskined3268832018-04-26 06:23:59 +02001312 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1313 break;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001314 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001315
Gilles Peskinef5a99962018-04-30 16:37:23 +02001316 op_name = ssl_async_operation_names[ctx->operation_type];
1317
Gilles Peskinec9125722018-04-26 07:15:40 +02001318 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001319 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001320 mbedtls_printf( "Async resume callback: %s done but injected error\n",
1321 op_name );
Gilles Peskine2636fad2018-06-12 14:17:39 +02001322 mbedtls_free( ctx );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001323 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1324 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001325
Gilles Peskine6331d782018-04-26 13:27:43 +02001326 mbedtls_printf( "Async resume (slot %u): %s done, status=%d.\n",
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001327 ctx->slot, op_name, ret );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001328 mbedtls_free( ctx );
1329 return( ret );
1330}
1331
Gilles Peskine8f97af72018-04-26 11:46:10 +02001332static void ssl_async_cancel( mbedtls_ssl_context *ssl )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001333{
Gilles Peskinea668c602018-04-30 11:54:39 +02001334 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001335 mbedtls_printf( "Async cancel callback.\n" );
1336 mbedtls_free( ctx );
1337}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001338#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001339
Hanno Becker16970d22017-10-10 15:56:37 +01001340/*
1341 * Wait for an event from the underlying transport or the timer
1342 * (Used in event-driven IO mode).
1343 */
1344#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001345int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001346 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001347#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001348int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001349 mbedtls_timing_delay_context *timer,
1350 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001351#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001352{
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001353 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +01001354 int poll_type = 0;
1355
1356 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
1357 poll_type = MBEDTLS_NET_POLL_WRITE;
1358 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
1359 poll_type = MBEDTLS_NET_POLL_READ;
1360#if !defined(MBEDTLS_TIMING_C)
1361 else
Hanno Beckeref527962018-03-15 15:49:24 +00001362 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001363#endif
1364
1365 while( 1 )
1366 {
Hanno Becker197a91c2017-10-31 10:58:53 +00001367 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +01001368#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001369 if( timer != NULL &&
1370 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +01001371 {
Hanno Becker16970d22017-10-10 15:56:37 +01001372 break;
1373 }
Hanno Becker197a91c2017-10-31 10:58:53 +00001374#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +01001375
Hanno Becker197a91c2017-10-31 10:58:53 +00001376 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001377 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +01001378 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001379 ret = mbedtls_net_poll( fd, poll_type, 0 );
1380 if( ret < 0 )
1381 return( ret );
1382 if( ret == poll_type )
1383 break;
Hanno Becker16970d22017-10-10 15:56:37 +01001384 }
1385 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001386
1387 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001388}
1389
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001390#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001391static psa_status_t psa_setup_psk_key_slot( psa_key_handle_t slot,
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001392 psa_algorithm_t alg,
1393 unsigned char *psk,
1394 size_t psk_len )
1395{
1396 psa_status_t status;
1397 psa_key_policy_t policy;
1398
Hanno Becker9bd88422019-01-25 14:27:01 +00001399 policy = psa_key_policy_init();
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001400 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
1401
1402 status = psa_set_key_policy( slot, &policy );
1403 if( status != PSA_SUCCESS )
Hanno Becker1d911cd2018-11-15 13:06:09 +00001404 {
1405 fprintf( stderr, "POLICY\n" );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001406 return( status );
Hanno Becker1d911cd2018-11-15 13:06:09 +00001407 }
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001408
1409 status = psa_import_key( slot, PSA_KEY_TYPE_DERIVE, psk, psk_len );
1410 if( status != PSA_SUCCESS )
Hanno Becker1d911cd2018-11-15 13:06:09 +00001411 {
1412 fprintf( stderr, "IMPORT\n" );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001413 return( status );
Hanno Becker1d911cd2018-11-15 13:06:09 +00001414 }
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001415
1416 return( PSA_SUCCESS );
1417}
1418#endif /* MBEDTLS_USE_PSA_CRYPTO */
1419
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001420int main( int argc, char *argv[] )
1421{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001422 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001423 int version_suites[4][2];
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001424 unsigned char* buf = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001426#if defined(MBEDTLS_USE_PSA_CRYPTO)
1427 psa_algorithm_t alg = 0;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001428 psa_key_handle_t psk_slot = 0;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001429#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +02001431 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +02001432 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +02001433#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001434 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001435 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02001436 size_t cliip_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#if defined(MBEDTLS_SSL_COOKIE_C)
1438 mbedtls_ssl_cookie_ctx cookie_ctx;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001439#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001440
Gilles Peskineef86ab22017-05-05 18:59:02 +02001441#if defined(MBEDTLS_X509_CRT_PARSE_C)
1442 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
1443#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 mbedtls_entropy_context entropy;
1445 mbedtls_ctr_drbg_context ctr_drbg;
1446 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001447 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001448#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001449 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001450#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001452 unsigned char renego_period[8] = { 0 };
1453#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +02001455 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 mbedtls_x509_crt cacert;
1457 mbedtls_x509_crt srvcert;
1458 mbedtls_pk_context pkey;
1459 mbedtls_x509_crt srvcert2;
1460 mbedtls_pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001461 int key_cert_init = 0, key_cert_init2 = 0;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001462#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001463 ssl_async_key_context_t ssl_async_keys;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001464#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001465#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1467 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001468#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469#if defined(MBEDTLS_SSL_CACHE_C)
1470 mbedtls_ssl_cache_context cache;
Paul Bakker0a597072012-09-25 21:55:46 +00001471#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001472#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1473 mbedtls_ssl_ticket_context ticket_ctx;
1474#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001475#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001476 sni_entry *sni_info = NULL;
1477#endif
Hanno Beckere6706e62017-05-15 16:05:15 +01001478#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +01001479 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +01001480 const mbedtls_ecp_curve_info * curve_cur;
1481#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +01001483 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001484#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Simon Butchercce68be2018-07-23 14:26:09 +01001486 unsigned char alloc_buf[MEMORY_HEAP_SIZE];
Paul Bakker82024bf2013-07-04 11:52:32 +02001487#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001488
1489 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001490 char *p, *q;
1491 const int *list;
Hanno Becker5a9942e2018-11-12 17:47:48 +00001492#if defined(MBEDTLS_USE_PSA_CRYPTO)
1493 psa_status_t status;
1494#endif
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001495#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1496 unsigned char eap_tls_keymaterial[16];
1497 unsigned char eap_tls_iv[8];
1498 const char* eap_tls_label = "client EAP encryption";
1499 eap_tls_keys eap_tls_keying;
1500#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1503 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker82024bf2013-07-04 11:52:32 +02001504#endif
1505
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001506 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +02001507 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001508 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001509 mbedtls_net_init( &client_fd );
1510 mbedtls_net_init( &listen_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001511 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001512 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001513 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#if defined(MBEDTLS_X509_CRT_PARSE_C)
1515 mbedtls_x509_crt_init( &cacert );
1516 mbedtls_x509_crt_init( &srvcert );
1517 mbedtls_pk_init( &pkey );
1518 mbedtls_x509_crt_init( &srvcert2 );
1519 mbedtls_pk_init( &pkey2 );
Gilles Peskine4d9ec4d2018-04-26 14:33:43 +02001520#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
1521 memset( &ssl_async_keys, 0, sizeof( ssl_async_keys ) );
1522#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001523#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1525 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001526#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#if defined(MBEDTLS_SSL_CACHE_C)
1528 mbedtls_ssl_cache_init( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00001529#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001530#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1531 mbedtls_ssl_ticket_init( &ticket_ctx );
1532#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +02001534 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001535#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536#if defined(MBEDTLS_SSL_COOKIE_C)
1537 mbedtls_ssl_cookie_init( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001538#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001539
Hanno Becker5a9942e2018-11-12 17:47:48 +00001540#if defined(MBEDTLS_USE_PSA_CRYPTO)
1541 status = psa_crypto_init();
1542 if( status != PSA_SUCCESS )
1543 {
1544 mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
1545 (int) status );
1546 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1547 goto exit;
1548 }
1549#endif
1550
Paul Bakkerc1283d32014-08-18 11:05:51 +02001551#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001552 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001553 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001554 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +02001555#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001556
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001557 if( argc == 0 )
1558 {
1559 usage:
1560 if( ret == 0 )
1561 ret = 1;
1562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 mbedtls_printf( USAGE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 list = mbedtls_ssl_list_ciphersuites();
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001566 while( *list )
1567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001569 list++;
1570 if( !*list )
1571 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001573 list++;
1574 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 mbedtls_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001576 goto exit;
1577 }
1578
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001579 opt.buffer_size = DFL_IO_BUF_LEN;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001580 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001581 opt.server_port = DFL_SERVER_PORT;
1582 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker16970d22017-10-10 15:56:37 +01001583 opt.event = DFL_EVENT;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001584 opt.response_size = DFL_RESPONSE_SIZE;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001585 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001586 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001587 opt.ca_file = DFL_CA_FILE;
1588 opt.ca_path = DFL_CA_PATH;
1589 opt.crt_file = DFL_CRT_FILE;
1590 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001591 opt.crt_file2 = DFL_CRT_FILE2;
1592 opt.key_file2 = DFL_KEY_FILE2;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001593 opt.async_operations = DFL_ASYNC_OPERATIONS;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001594 opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
1595 opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
1596 opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
Paul Bakkerfbb17802013-04-17 19:10:21 +02001597 opt.psk = DFL_PSK;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001598#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001599 opt.psk_opaque = DFL_PSK_OPAQUE;
1600 opt.psk_list_opaque = DFL_PSK_LIST_OPAQUE;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001601#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001602#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1603 opt.ca_callback = DFL_CA_CALLBACK;
1604#endif
Paul Bakkerfbb17802013-04-17 19:10:21 +02001605 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001606 opt.psk_list = DFL_PSK_LIST;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001607 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001608 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001609 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001610 opt.renegotiation = DFL_RENEGOTIATION;
1611 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001612 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001613 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001614 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001615 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001616 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001617 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001618 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001619 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001620 opt.auth_mode = DFL_AUTH_MODE;
Janos Follath4817e272017-04-10 13:44:33 +01001621 opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001622 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001623 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001624 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001625 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001626 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001627 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001628 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001629 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001630 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001631 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001632 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001633 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001634 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001635 opt.hs_to_min = DFL_HS_TO_MIN;
1636 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001637 opt.dtls_mtu = DFL_DTLS_MTU;
Hanno Beckere7675d02018-08-14 13:28:56 +01001638 opt.dgram_packing = DFL_DGRAM_PACKING;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001639 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001640 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001641 opt.etm = DFL_ETM;
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001642 opt.eap_tls = DFL_EAP_TLS;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001643
1644 for( i = 1; i < argc; i++ )
1645 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001646 p = argv[i];
1647 if( ( q = strchr( p, '=' ) ) == NULL )
1648 goto usage;
1649 *q++ = '\0';
1650
1651 if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001652 opt.server_port = q;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001653 else if( strcmp( p, "server_addr" ) == 0 )
1654 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001655 else if( strcmp( p, "dtls" ) == 0 )
1656 {
1657 int t = atoi( q );
1658 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001660 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001662 else
1663 goto usage;
1664 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001665 else if( strcmp( p, "debug_level" ) == 0 )
1666 {
1667 opt.debug_level = atoi( q );
1668 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1669 goto usage;
1670 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001671 else if( strcmp( p, "nbio" ) == 0 )
1672 {
1673 opt.nbio = atoi( q );
1674 if( opt.nbio < 0 || opt.nbio > 2 )
1675 goto usage;
1676 }
Hanno Becker16970d22017-10-10 15:56:37 +01001677 else if( strcmp( p, "event" ) == 0 )
1678 {
1679 opt.event = atoi( q );
1680 if( opt.event < 0 || opt.event > 2 )
1681 goto usage;
1682 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001683 else if( strcmp( p, "read_timeout" ) == 0 )
1684 opt.read_timeout = atoi( q );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001685 else if( strcmp( p, "buffer_size" ) == 0 )
1686 {
1687 opt.buffer_size = atoi( q );
1688 if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
1689 goto usage;
1690 }
1691 else if( strcmp( p, "response_size" ) == 0 )
1692 {
1693 opt.response_size = atoi( q );
1694 if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
1695 goto usage;
1696 if( opt.buffer_size < opt.response_size )
1697 opt.buffer_size = opt.response_size;
1698 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001699 else if( strcmp( p, "ca_file" ) == 0 )
1700 opt.ca_file = q;
1701 else if( strcmp( p, "ca_path" ) == 0 )
1702 opt.ca_path = q;
1703 else if( strcmp( p, "crt_file" ) == 0 )
1704 opt.crt_file = q;
1705 else if( strcmp( p, "key_file" ) == 0 )
1706 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001707 else if( strcmp( p, "crt_file2" ) == 0 )
1708 opt.crt_file2 = q;
1709 else if( strcmp( p, "key_file2" ) == 0 )
1710 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001711 else if( strcmp( p, "dhm_file" ) == 0 )
1712 opt.dhm_file = q;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001713#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001714 else if( strcmp( p, "async_operations" ) == 0 )
1715 opt.async_operations = q;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001716 else if( strcmp( p, "async_private_delay1" ) == 0 )
1717 opt.async_private_delay1 = atoi( q );
1718 else if( strcmp( p, "async_private_delay2" ) == 0 )
1719 opt.async_private_delay2 = atoi( q );
1720 else if( strcmp( p, "async_private_error" ) == 0 )
1721 {
1722 int n = atoi( q );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01001723 if( n < -SSL_ASYNC_INJECT_ERROR_MAX ||
1724 n > SSL_ASYNC_INJECT_ERROR_MAX )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001725 {
1726 ret = 2;
1727 goto usage;
1728 }
1729 opt.async_private_error = n;
1730 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001731#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001732 else if( strcmp( p, "psk" ) == 0 )
1733 opt.psk = q;
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001734#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00001735 else if( strcmp( p, "psk_opaque" ) == 0 )
1736 opt.psk_opaque = atoi( q );
1737 else if( strcmp( p, "psk_list_opaque" ) == 0 )
1738 opt.psk_list_opaque = atoi( q );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01001739#endif
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02001740#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1741 else if( strcmp( p, "ca_callback" ) == 0)
1742 opt.ca_callback = atoi( q );
1743#endif
Paul Bakkerfbb17802013-04-17 19:10:21 +02001744 else if( strcmp( p, "psk_identity" ) == 0 )
1745 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001746 else if( strcmp( p, "psk_list" ) == 0 )
1747 opt.psk_list = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001748 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1749 opt.ecjpake_pw = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001750 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001753
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001754 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001755 {
1756 ret = 2;
1757 goto usage;
1758 }
1759 opt.force_ciphersuite[1] = 0;
1760 }
Hanno Beckere6706e62017-05-15 16:05:15 +01001761 else if( strcmp( p, "curves" ) == 0 )
1762 opt.curves = q;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001763 else if( strcmp( p, "version_suites" ) == 0 )
1764 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001765 else if( strcmp( p, "renegotiation" ) == 0 )
1766 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001767 opt.renegotiation = (atoi( q )) ?
1768 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1769 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001770 }
1771 else if( strcmp( p, "allow_legacy" ) == 0 )
1772 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001773 switch( atoi( q ) )
1774 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001775 case -1:
1776 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1777 break;
1778 case 0:
1779 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1780 break;
1781 case 1:
1782 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1783 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001784 default: goto usage;
1785 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001786 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001787 else if( strcmp( p, "renegotiate" ) == 0 )
1788 {
1789 opt.renegotiate = atoi( q );
1790 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1791 goto usage;
1792 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001793 else if( strcmp( p, "renego_delay" ) == 0 )
1794 {
1795 opt.renego_delay = atoi( q );
1796 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001797 else if( strcmp( p, "renego_period" ) == 0 )
1798 {
Andres AG0b736db2017-02-08 14:05:57 +00001799#if defined(_MSC_VER)
1800 opt.renego_period = _strtoui64( q, NULL, 10 );
1801#else
1802 if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
1803 goto usage;
1804#endif /* _MSC_VER */
1805 if( opt.renego_period < 2 )
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001806 goto usage;
1807 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001808 else if( strcmp( p, "exchanges" ) == 0 )
1809 {
1810 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001811 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001812 goto usage;
1813 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001814 else if( strcmp( p, "min_version" ) == 0 )
1815 {
1816 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001818 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001820 else if( strcmp( q, "tls1_1" ) == 0 ||
1821 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001823 else if( strcmp( q, "tls1_2" ) == 0 ||
1824 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001826 else
1827 goto usage;
1828 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001829 else if( strcmp( p, "max_version" ) == 0 )
1830 {
1831 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001833 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001835 else if( strcmp( q, "tls1_1" ) == 0 ||
1836 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001838 else if( strcmp( q, "tls1_2" ) == 0 ||
1839 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001841 else
1842 goto usage;
1843 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001844 else if( strcmp( p, "arc4" ) == 0 )
1845 {
1846 switch( atoi( q ) )
1847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1849 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001850 default: goto usage;
1851 }
1852 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02001853 else if( strcmp( p, "allow_sha1" ) == 0 )
1854 {
1855 switch( atoi( q ) )
1856 {
1857 case 0: opt.allow_sha1 = 0; break;
1858 case 1: opt.allow_sha1 = 1; break;
1859 default: goto usage;
1860 }
1861 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001862 else if( strcmp( p, "force_version" ) == 0 )
1863 {
1864 if( strcmp( q, "ssl3" ) == 0 )
1865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1867 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001868 }
1869 else if( strcmp( q, "tls1" ) == 0 )
1870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1872 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001873 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001874 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1877 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001878 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001879 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1882 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001883 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001884 else if( strcmp( q, "dtls1" ) == 0 )
1885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1887 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1888 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001889 }
1890 else if( strcmp( q, "dtls1_2" ) == 0 )
1891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1893 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1894 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001895 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001896 else
1897 goto usage;
1898 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001899 else if( strcmp( p, "auth_mode" ) == 0 )
1900 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001901 if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
Paul Bakker91ebfb52012-11-23 14:04:08 +01001902 goto usage;
1903 }
Janos Follath4817e272017-04-10 13:44:33 +01001904 else if( strcmp( p, "cert_req_ca_list" ) == 0 )
1905 {
1906 opt.cert_req_ca_list = atoi( q );
1907 if( opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1 )
1908 goto usage;
1909 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001910 else if( strcmp( p, "max_frag_len" ) == 0 )
1911 {
1912 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001914 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001916 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001918 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001920 else
1921 goto usage;
1922 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001923 else if( strcmp( p, "alpn" ) == 0 )
1924 {
1925 opt.alpn_string = q;
1926 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001927 else if( strcmp( p, "trunc_hmac" ) == 0 )
1928 {
1929 switch( atoi( q ) )
1930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1932 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001933 default: goto usage;
1934 }
1935 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001936 else if( strcmp( p, "extended_ms" ) == 0 )
1937 {
1938 switch( atoi( q ) )
1939 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001940 case 0:
1941 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1942 break;
1943 case 1:
1944 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1945 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001946 default: goto usage;
1947 }
1948 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001949 else if( strcmp( p, "etm" ) == 0 )
1950 {
1951 switch( atoi( q ) )
1952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1954 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001955 default: goto usage;
1956 }
1957 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001958 else if( strcmp( p, "tickets" ) == 0 )
1959 {
1960 opt.tickets = atoi( q );
1961 if( opt.tickets < 0 || opt.tickets > 1 )
1962 goto usage;
1963 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001964 else if( strcmp( p, "ticket_timeout" ) == 0 )
1965 {
1966 opt.ticket_timeout = atoi( q );
1967 if( opt.ticket_timeout < 0 )
1968 goto usage;
1969 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001970 else if( strcmp( p, "cache_max" ) == 0 )
1971 {
1972 opt.cache_max = atoi( q );
1973 if( opt.cache_max < 0 )
1974 goto usage;
1975 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001976 else if( strcmp( p, "cache_timeout" ) == 0 )
1977 {
1978 opt.cache_timeout = atoi( q );
1979 if( opt.cache_timeout < 0 )
1980 goto usage;
1981 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001982 else if( strcmp( p, "cookies" ) == 0 )
1983 {
1984 opt.cookies = atoi( q );
1985 if( opt.cookies < -1 || opt.cookies > 1)
1986 goto usage;
1987 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001988 else if( strcmp( p, "anti_replay" ) == 0 )
1989 {
1990 opt.anti_replay = atoi( q );
1991 if( opt.anti_replay < 0 || opt.anti_replay > 1)
1992 goto usage;
1993 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001994 else if( strcmp( p, "badmac_limit" ) == 0 )
1995 {
1996 opt.badmac_limit = atoi( q );
1997 if( opt.badmac_limit < 0 )
1998 goto usage;
1999 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002000 else if( strcmp( p, "hs_timeout" ) == 0 )
2001 {
2002 if( ( p = strchr( q, '-' ) ) == NULL )
2003 goto usage;
2004 *p++ = '\0';
2005 opt.hs_to_min = atoi( q );
2006 opt.hs_to_max = atoi( p );
2007 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
2008 goto usage;
2009 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002010 else if( strcmp( p, "mtu" ) == 0 )
2011 {
2012 opt.dtls_mtu = atoi( q );
2013 if( opt.dtls_mtu < 0 )
2014 goto usage;
2015 }
Hanno Beckere7675d02018-08-14 13:28:56 +01002016 else if( strcmp( p, "dgram_packing" ) == 0 )
2017 {
2018 opt.dgram_packing = atoi( q );
2019 if( opt.dgram_packing != 0 &&
2020 opt.dgram_packing != 1 )
2021 {
2022 goto usage;
2023 }
2024 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002025 else if( strcmp( p, "sni" ) == 0 )
2026 {
2027 opt.sni = q;
2028 }
Andres Amaya Garciabc818842018-10-16 21:08:38 +01002029 else if( strcmp( p, "query_config" ) == 0 )
2030 {
2031 return query_config( q );
2032 }
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002033 else if( strcmp( p, "eap_tls" ) == 0 )
2034 {
2035 opt.eap_tls = atoi( q );
2036 if( opt.eap_tls < 0 || opt.eap_tls > 1 )
2037 goto usage;
2038 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002039 else
2040 goto usage;
2041 }
2042
Hanno Becker16970d22017-10-10 15:56:37 +01002043 /* Event-driven IO is incompatible with the above custom
2044 * receive and send functions, as the polling builds on
2045 * refers to the underlying net_context. */
2046 if( opt.event == 1 && opt.nbio != 1 )
2047 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002048 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01002049 opt.nbio = 1;
2050 }
2051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052#if defined(MBEDTLS_DEBUG_C)
2053 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02002054#endif
Andrzej Kurekda4029d2018-06-20 07:07:55 -04002055 buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002056 if( buf == NULL )
2057 {
Andrzej Kurek755890f2018-06-20 08:17:04 -04002058 mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002059 ret = 3;
2060 goto exit;
2061 }
Paul Bakkerc73079a2014-04-25 16:34:30 +02002062
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002063#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002064 if( opt.psk_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002065 {
2066 if( strlen( opt.psk ) == 0 )
2067 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00002068 mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002069 ret = 2;
2070 goto usage;
2071 }
2072
2073 if( opt.force_ciphersuite[0] <= 0 )
2074 {
2075 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" );
2076 ret = 2;
2077 goto usage;
2078 }
2079 }
2080
Hanno Becker1d911cd2018-11-15 13:06:09 +00002081 if( opt.psk_list_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002082 {
2083 if( opt.psk_list == NULL )
2084 {
2085 mbedtls_printf( "psk_slot set but no psk to be imported specified.\n" );
2086 ret = 2;
2087 goto usage;
2088 }
2089
2090 if( opt.force_ciphersuite[0] <= 0 )
2091 {
2092 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" );
2093 ret = 2;
2094 goto usage;
2095 }
2096 }
2097#endif /* MBEDTLS_USE_PSA_CRYPTO */
2098
Paul Bakkerc1516be2013-06-29 16:01:32 +02002099 if( opt.force_ciphersuite[0] > 0 )
2100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002102 ciphersuite_info =
2103 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002104
Paul Bakker5b55b792013-07-19 13:43:43 +02002105 if( opt.max_version != -1 &&
2106 ciphersuite_info->min_minor_ver > opt.max_version )
2107 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002108 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker5b55b792013-07-19 13:43:43 +02002109 ret = 2;
2110 goto usage;
2111 }
2112 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02002113 ciphersuite_info->max_minor_ver < opt.min_version )
2114 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002115 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002116 ret = 2;
2117 goto usage;
2118 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002119
2120 /* If we select a version that's not supported by
2121 * this suite, then there will be no common ciphersuite... */
2122 if( opt.max_version == -1 ||
2123 opt.max_version > ciphersuite_info->max_minor_ver )
2124 {
Paul Bakker5b55b792013-07-19 13:43:43 +02002125 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002126 }
Paul Bakker5b55b792013-07-19 13:43:43 +02002127 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002128 {
Paul Bakker5b55b792013-07-19 13:43:43 +02002129 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002130 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2132 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
2133 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002134 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002135
2136 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002142 ret = 2;
2143 goto usage;
2144 }
2145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002147 }
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002148
2149#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002150 if( opt.psk_opaque != 0 || opt.psk_list_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002151 {
2152 /* Ensure that the chosen ciphersuite is PSK-only; we must know
2153 * the ciphersuite in advance to set the correct policy for the
2154 * PSK key slot. This limitation might go away in the future. */
2155 if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
2156 opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 )
2157 {
2158 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" );
2159 ret = 2;
2160 goto usage;
2161 }
2162
2163 /* Determine KDF algorithm the opaque PSK will be used in. */
2164#if defined(MBEDTLS_SHA512_C)
2165 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
2166 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2167 else
2168#endif /* MBEDTLS_SHA512_C */
2169 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2170 }
2171#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerc1516be2013-06-29 16:01:32 +02002172 }
2173
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002174 if( opt.version_suites != NULL )
2175 {
2176 const char *name[4] = { 0 };
2177
2178 /* Parse 4-element coma-separated list */
2179 for( i = 0, p = (char *) opt.version_suites;
2180 i < 4 && *p != '\0';
2181 i++ )
2182 {
2183 name[i] = p;
2184
2185 /* Terminate the current string and move on to next one */
2186 while( *p != ',' && *p != '\0' )
2187 p++;
2188 if( *p == ',' )
2189 *p++ = '\0';
2190 }
2191
2192 if( i != 4 )
2193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 mbedtls_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002195 ret = 1;
2196 goto exit;
2197 }
2198
2199 memset( version_suites, 0, sizeof( version_suites ) );
2200
2201 /* Get the suites identifiers from their name */
2202 for( i = 0; i < 4; i++ )
2203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002205
2206 if( version_suites[i][0] == 0 )
2207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002209 ret = 2;
2210 goto usage;
2211 }
2212 }
2213 }
2214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002216 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002217 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02002218 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002219 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 mbedtls_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002222 goto exit;
2223 }
2224
2225 if( opt.psk_list != NULL )
2226 {
2227 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 mbedtls_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002230 goto exit;
2231 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002232 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002234
Hanno Beckere6706e62017-05-15 16:05:15 +01002235#if defined(MBEDTLS_ECP_C)
2236 if( opt.curves != NULL )
2237 {
2238 p = (char *) opt.curves;
2239 i = 0;
2240
2241 if( strcmp( p, "none" ) == 0 )
2242 {
2243 curve_list[0] = MBEDTLS_ECP_DP_NONE;
2244 }
2245 else if( strcmp( p, "default" ) != 0 )
2246 {
2247 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01002248 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002249 {
2250 q = p;
2251
2252 /* Terminate the current string */
2253 while( *p != ',' && *p != '\0' )
2254 p++;
2255 if( *p == ',' )
2256 *p++ = '\0';
2257
2258 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
2259 {
2260 curve_list[i++] = curve_cur->grp_id;
2261 }
2262 else
2263 {
2264 mbedtls_printf( "unknown curve %s\n", q );
2265 mbedtls_printf( "supported curves: " );
2266 for( curve_cur = mbedtls_ecp_curve_list();
2267 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
2268 curve_cur++ )
2269 {
2270 mbedtls_printf( "%s ", curve_cur->name );
2271 }
2272 mbedtls_printf( "\n" );
2273 goto exit;
2274 }
2275 }
2276
2277 mbedtls_printf("Number of curves: %d\n", i );
2278
Hanno Becker8651a432017-06-09 16:13:22 +01002279 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002280 {
Hanno Becker8651a432017-06-09 16:13:22 +01002281 mbedtls_printf( "curves list too long, maximum %d",
2282 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01002283 goto exit;
2284 }
2285
2286 curve_list[i] = MBEDTLS_ECP_DP_NONE;
2287 }
2288 }
2289#endif /* MBEDTLS_ECP_C */
2290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002292 if( opt.alpn_string != NULL )
2293 {
2294 p = (char *) opt.alpn_string;
2295 i = 0;
2296
2297 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01002298 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002299 {
2300 alpn_list[i++] = p;
2301
2302 /* Terminate the current string and move on to next one */
2303 while( *p != ',' && *p != '\0' )
2304 p++;
2305 if( *p == ',' )
2306 *p++ = '\0';
2307 }
2308 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002310
Paul Bakkerfbb17802013-04-17 19:10:21 +02002311 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002312 * 0. Initialize the RNG and the session data
2313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002315 fflush( stdout );
2316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 mbedtls_entropy_init( &entropy );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002318 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
2319 &entropy, (const unsigned char *) pers,
2320 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002321 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002322 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
2323 -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002324 goto exit;
2325 }
2326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002330 /*
2331 * 1.1. Load the trusted CA
2332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002334 fflush( stdout );
2335
Hanno Becker8174bdf2019-03-05 16:22:07 +00002336 if( strcmp( opt.ca_path, "none" ) == 0 ||
2337 strcmp( opt.ca_file, "none" ) == 0 )
2338 {
2339 ret = 0;
2340 }
2341 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002343 if( strlen( opt.ca_path ) )
Hanno Becker8174bdf2019-03-05 16:22:07 +00002344 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002345 else if( strlen( opt.ca_file ) )
Hanno Becker8174bdf2019-03-05 16:22:07 +00002346 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02002347 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002348#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349#if defined(MBEDTLS_CERTS_C)
Hanno Becker09b8cae2019-02-01 08:13:19 +00002350 {
2351#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 ret = mbedtls_x509_crt_parse( &cacert,
2355 (const unsigned char *) mbedtls_test_cas[i],
2356 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002357 if( ret != 0 )
2358 break;
2359 }
Hanno Becker09b8cae2019-02-01 08:13:19 +00002360 if( ret == 0 )
2361#endif /* MBEDTLS_PEM_PARSE_C */
2362 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
2363 {
2364 ret = mbedtls_x509_crt_parse_der( &cacert,
2365 (const unsigned char *) mbedtls_test_cas_der[i],
2366 mbedtls_test_cas_der_len[i] );
2367 if( ret != 0 )
2368 break;
2369 }
2370 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002371#else
2372 {
2373 ret = 1;
Hanno Beckera0c5ceb2018-12-05 16:49:42 +00002374 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002375 }
Hanno Becker09b8cae2019-02-01 08:13:19 +00002376#endif /* MBEDTLS_CERTS_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002377 if( ret < 0 )
2378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002380 goto exit;
2381 }
2382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002384
2385 /*
2386 * 1.2. Load own certificate and private key
2387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002389 fflush( stdout );
2390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002392 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002393 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002394 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002398 -ret );
2399 goto exit;
2400 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002401 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002402 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002403 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002404 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002408 goto exit;
2409 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002410 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002411 if( key_cert_init == 1 )
2412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002414 goto exit;
2415 }
2416
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002417 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002418 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002419 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002423 -ret );
2424 goto exit;
2425 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002426 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002427 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002428 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002429 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002433 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002434 goto exit;
2435 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002436 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002437 if( key_cert_init2 == 1 )
2438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002440 goto exit;
2441 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002442#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002443 if( key_cert_init == 0 &&
2444 strcmp( opt.crt_file, "none" ) != 0 &&
2445 strcmp( opt.key_file, "none" ) != 0 &&
2446 key_cert_init2 == 0 &&
2447 strcmp( opt.crt_file2, "none" ) != 0 &&
2448 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450#if !defined(MBEDTLS_CERTS_C)
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002451 mbedtls_printf( "Not certificated or key provided, and \nMBEDTLS_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002452 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002453#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454#if defined(MBEDTLS_RSA_C)
2455 if( ( ret = mbedtls_x509_crt_parse( &srvcert,
2456 (const unsigned char *) mbedtls_test_srv_crt_rsa,
2457 mbedtls_test_srv_crt_rsa_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002458 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002459 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
2460 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002461 goto exit;
2462 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 if( ( ret = mbedtls_pk_parse_key( &pkey,
2464 (const unsigned char *) mbedtls_test_srv_key_rsa,
2465 mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002466 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002467 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
2468 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002469 goto exit;
2470 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002471 key_cert_init = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472#endif /* MBEDTLS_RSA_C */
2473#if defined(MBEDTLS_ECDSA_C)
2474 if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
2475 (const unsigned char *) mbedtls_test_srv_crt_ec,
2476 mbedtls_test_srv_crt_ec_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002477 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002478 mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n",
2479 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002480 goto exit;
2481 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482 if( ( ret = mbedtls_pk_parse_key( &pkey2,
2483 (const unsigned char *) mbedtls_test_srv_key_ec,
2484 mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002485 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002486 mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n",
2487 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002488 goto exit;
2489 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002490 key_cert_init2 = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491#endif /* MBEDTLS_ECDSA_C */
2492#endif /* MBEDTLS_CERTS_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002493 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 mbedtls_printf( " ok\n" );
2496#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002499 if( opt.dhm_file != NULL )
2500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 mbedtls_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002502 fflush( stdout );
2503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002507 -ret );
2508 goto exit;
2509 }
2510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002512 }
2513#endif
2514
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002515#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002516 if( opt.sni != NULL )
2517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 mbedtls_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002519 fflush( stdout );
2520
2521 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
2522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002524 goto exit;
2525 }
2526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002528 }
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002529#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002530
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002531 /*
2532 * 2. Setup the listening TCP socket
2533 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02002534 mbedtls_printf( " . Bind on %s://%s:%s/ ...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002536 opt.server_addr ? opt.server_addr : "*",
2537 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002538 fflush( stdout );
2539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
2541 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2542 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002545 goto exit;
2546 }
2547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002549
2550 /*
2551 * 3. Setup stuff
2552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002554 fflush( stdout );
2555
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02002556 if( ( ret = mbedtls_ssl_config_defaults( &conf,
2557 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02002558 opt.transport,
2559 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002560 {
2561 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
2562 goto exit;
2563 }
2564
Gilles Peskineef86ab22017-05-05 18:59:02 +02002565#if defined(MBEDTLS_X509_CRT_PARSE_C)
2566 /* The default algorithms profile disables SHA-1, but our tests still
2567 rely on it heavily. Hence we allow it here. A real-world server
2568 should use the default profile unless there is a good reason not to. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02002569 if( opt.allow_sha1 > 0 )
2570 {
2571 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
2572 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
Gilles Peskine682df092017-05-11 19:01:11 +02002573 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
Gilles Peskinebc70a182017-05-09 15:59:24 +02002574 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02002575#endif /* MBEDTLS_X509_CRT_PARSE_C */
2576
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01002577 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002578 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002579
Janos Follath4817e272017-04-10 13:44:33 +01002580 if( opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST )
2581 mbedtls_ssl_conf_cert_req_ca_list( &conf, opt.cert_req_ca_list );
2582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002584 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 +02002585 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002586
Hanno Beckere7675d02018-08-14 13:28:56 +01002587 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01002588 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002592 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002593 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002594 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002595 goto exit;
2596 };
Paul Bakker05decb22013-08-15 13:33:48 +02002597#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002600 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002601 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002602#endif
2603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002605 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002606 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002607#endif
2608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002610 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002611 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002612#endif
2613
Ron Eldorb7fd64c2019-05-12 11:03:32 +03002614#if defined(MBEDTLS_SSL_EXPORT_KEYS)
2615 if( opt.eap_tls != 0 )
2616 mbedtls_ssl_conf_export_keys_ext_cb( &conf, eap_tls_key_derivation,
2617 &eap_tls_keying );
2618#endif
2619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002621 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002622 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002623 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002624 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002625 goto exit;
2626 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002627#endif
2628
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002629 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
2630 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002633 if( opt.cache_max != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002635
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002636 if( opt.cache_timeout != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002638
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002639 mbedtls_ssl_conf_session_cache( &conf, &cache,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01002640 mbedtls_ssl_cache_get,
2641 mbedtls_ssl_cache_set );
Paul Bakker0a597072012-09-25 21:55:46 +00002642#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002645 if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002646 {
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002647 if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
2648 mbedtls_ctr_drbg_random, &ctr_drbg,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +02002649 MBEDTLS_CIPHER_AES_256_GCM,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002650 opt.ticket_timeout ) ) != 0 )
2651 {
2652 mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
2653 goto exit;
2654 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01002655
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002656 mbedtls_ssl_conf_session_tickets_cb( &conf,
2657 mbedtls_ssl_ticket_write,
2658 mbedtls_ssl_ticket_parse,
2659 &ticket_ctx );
2660 }
Paul Bakkera503a632013-08-14 13:48:06 +02002661#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663#if defined(MBEDTLS_SSL_PROTO_DTLS)
2664 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002667 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
2670 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672 mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002673 goto exit;
2674 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002675
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002676 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002677 &cookie_ctx );
2678 }
2679 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680#endif /* MBEDTLS_SSL_COOKIE_C */
2681#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002682 if( opt.cookies == 0 )
2683 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002684 mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002685 }
2686 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002688 {
2689 ; /* Nothing to do */
2690 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002693 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002694 mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002695#endif
2696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002698 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002699 mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002700#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002701 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002703
Paul Bakker41c83d32013-03-20 14:39:14 +01002704 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002705 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002706
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002707#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002708 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002709 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002710#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002711
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002712 if( opt.version_suites != NULL )
2713 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002714 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 MBEDTLS_SSL_MAJOR_VERSION_3,
2716 MBEDTLS_SSL_MINOR_VERSION_0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002717 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 MBEDTLS_SSL_MAJOR_VERSION_3,
2719 MBEDTLS_SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002720 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 MBEDTLS_SSL_MAJOR_VERSION_3,
2722 MBEDTLS_SSL_MINOR_VERSION_2 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002723 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724 MBEDTLS_SSL_MAJOR_VERSION_3,
2725 MBEDTLS_SSL_MINOR_VERSION_3 );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002726 }
2727
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002728 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002729 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002731 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002732
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02002733 if( opt.renego_delay != DFL_RENEGO_DELAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002734 mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002735
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002736 if( opt.renego_period != DFL_RENEGO_PERIOD )
2737 {
Andres AG692ad842017-01-19 16:30:57 +00002738 PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002739 mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002740 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002741#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002744 if( strcmp( opt.ca_path, "none" ) != 0 &&
2745 strcmp( opt.ca_file, "none" ) != 0 )
2746 {
Jarno Lamsa1b4a2ba2019-03-27 17:55:27 +02002747#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2748 if( opt.ca_callback != 0 )
2749 mbedtls_ssl_conf_ca_cb( &conf, ca_callback, &cacert);
2750 else
2751#endif
2752 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002753 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002754 if( key_cert_init )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002755 {
2756 mbedtls_pk_context *pk = &pkey;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002757#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002758 if( opt.async_private_delay1 >= 0 )
2759 {
Gilles Peskine44817442018-06-13 18:09:28 +02002760 ret = ssl_async_set_key( &ssl_async_keys, &srvcert, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002761 opt.async_private_delay1 );
2762 if( ret < 0 )
2763 {
2764 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2765 ret );
2766 goto exit;
2767 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002768 pk = NULL;
2769 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002770#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002771 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002772 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002773 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002774 goto exit;
2775 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002776 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002777 if( key_cert_init2 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002778 {
2779 mbedtls_pk_context *pk = &pkey2;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002780#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002781 if( opt.async_private_delay2 >= 0 )
2782 {
Gilles Peskine44817442018-06-13 18:09:28 +02002783 ret = ssl_async_set_key( &ssl_async_keys, &srvcert2, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002784 opt.async_private_delay2 );
2785 if( ret < 0 )
2786 {
2787 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2788 ret );
2789 goto exit;
2790 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002791 pk = NULL;
2792 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002793#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002794 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002795 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002796 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002797 goto exit;
2798 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002799 }
2800
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002801#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002802 if( opt.async_operations[0] != '-' )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002803 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002804 mbedtls_ssl_async_sign_t *sign = NULL;
2805 mbedtls_ssl_async_decrypt_t *decrypt = NULL;
Gilles Peskine12ab5d42018-04-24 12:32:04 +02002806 const char *r;
2807 for( r = opt.async_operations; *r; r++ )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002808 {
Gilles Peskine12ab5d42018-04-24 12:32:04 +02002809 switch( *r )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002810 {
2811 case 'd':
2812 decrypt = ssl_async_decrypt;
2813 break;
2814 case 's':
2815 sign = ssl_async_sign;
2816 break;
2817 }
2818 }
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002819 ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
2820 - opt.async_private_error :
2821 opt.async_private_error );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002822 ssl_async_keys.f_rng = mbedtls_ctr_drbg_random;
2823 ssl_async_keys.p_rng = &ctr_drbg;
2824 mbedtls_ssl_conf_async_private_cb( &conf,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002825 sign,
2826 decrypt,
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002827 ssl_async_resume,
2828 ssl_async_cancel,
2829 &ssl_async_keys );
2830 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002831#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002832#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +02002833
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002834#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002835 if( opt.sni != NULL )
Gilles Peskine166ce742018-04-30 10:30:49 +02002836 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002837 mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
Gilles Peskine166ce742018-04-30 10:30:49 +02002838#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
2839 if( opt.async_private_delay2 >= 0 )
2840 {
Gilles Peskinee2479892018-06-13 18:06:51 +02002841 sni_entry *cur;
2842 for( cur = sni_info; cur != NULL; cur = cur->next )
Gilles Peskine166ce742018-04-30 10:30:49 +02002843 {
Gilles Peskinee2479892018-06-13 18:06:51 +02002844 ret = ssl_async_set_key( &ssl_async_keys,
Gilles Peskine44817442018-06-13 18:09:28 +02002845 cur->cert, cur->key, 1,
Gilles Peskinee2479892018-06-13 18:06:51 +02002846 opt.async_private_delay2 );
2847 if( ret < 0 )
2848 {
2849 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2850 ret );
2851 goto exit;
2852 }
2853 cur->key = NULL;
Gilles Peskine166ce742018-04-30 10:30:49 +02002854 }
Gilles Peskine166ce742018-04-30 10:30:49 +02002855 }
2856#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
2857 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002858#endif
2859
Hanno Beckere6706e62017-05-15 16:05:15 +01002860#if defined(MBEDTLS_ECP_C)
2861 if( opt.curves != NULL &&
2862 strcmp( opt.curves, "default" ) != 0 )
2863 {
2864 mbedtls_ssl_conf_curves( &conf, curve_list );
2865 }
2866#endif
2867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002869
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002870 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
2871 {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002872#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002873 if( opt.psk_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002874 {
Hanno Becker32809e82019-01-25 14:27:15 +00002875 status = psa_allocate_key( &psk_slot );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002876 if( status != PSA_SUCCESS )
2877 {
Hanno Becker1d911cd2018-11-15 13:06:09 +00002878 fprintf( stderr, "ALLOC FAIL\n" );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002879 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2880 goto exit;
2881 }
2882
Hanno Becker1d911cd2018-11-15 13:06:09 +00002883 /* The algorithm has already been determined earlier. */
2884 status = psa_setup_psk_key_slot( psk_slot, alg, psk, psk_len );
2885 if( status != PSA_SUCCESS )
2886 {
2887 fprintf( stderr, "SETUP FAIL\n" );
2888 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2889 goto exit;
2890 }
2891 if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, psk_slot,
2892 (const unsigned char *) opt.psk_identity,
2893 strlen( opt.psk_identity ) ) ) != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002894 {
2895 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
2896 ret );
2897 goto exit;
2898 }
2899 }
2900 else
2901#endif /* MBEDTLS_USE_PSA_CRYPTO */
2902 if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
2903 (const unsigned char *) opt.psk_identity,
2904 strlen( opt.psk_identity ) ) ) != 0 )
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002905 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002906 mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002907 goto exit;
2908 }
2909 }
2910
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002911 if( opt.psk_list != NULL )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002912 {
2913#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00002914 if( opt.psk_list_opaque != 0 )
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002915 {
2916 psk_entry *cur_psk;
2917 for( cur_psk = psk_info; cur_psk != NULL; cur_psk = cur_psk->next )
2918 {
Hanno Becker32809e82019-01-25 14:27:15 +00002919 status = psa_allocate_key( &cur_psk->slot );
Hanno Becker1d911cd2018-11-15 13:06:09 +00002920 if( status != PSA_SUCCESS )
2921 {
2922 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2923 goto exit;
2924 }
2925
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002926 status = psa_setup_psk_key_slot( cur_psk->slot, alg,
2927 cur_psk->key,
2928 cur_psk->key_len );
2929 if( status != PSA_SUCCESS )
2930 {
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002931 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2932 goto exit;
2933 }
2934 }
2935 }
2936#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker1d911cd2018-11-15 13:06:09 +00002937
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002938 mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
Hanno Beckerb64ba5f2018-10-26 11:28:08 +01002939 }
Paul Bakkered27a042013-04-18 22:46:23 +02002940#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942#if defined(MBEDTLS_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00002943 /*
2944 * Use different group than default DHM group
2945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002947 if( opt.dhm_file != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002948 ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002949#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002950 if( ret != 0 )
2951 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002952 mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002953 goto exit;
2954 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002955#endif
2956
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002957 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002958 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00002959
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002960 if( opt.max_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002961 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002962
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002963 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
2964 {
2965 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
2966 goto exit;
2967 }
2968
2969 if( opt.nbio == 2 )
2970 mbedtls_ssl_set_bio( &ssl, &client_fd, my_send, my_recv, NULL );
2971 else
2972 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02002973 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002974
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002975#if defined(MBEDTLS_SSL_PROTO_DTLS)
2976 if( opt.dtls_mtu != DFL_DTLS_MTU )
2977 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
2978#endif
2979
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002980#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002981 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
2982 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002983#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002986
2987reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002988#if !defined(_WIN32)
2989 if( received_sigterm )
2990 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01002991 mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" );
2992 if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT )
2993 ret = 0;
2994
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002995 goto exit;
2996 }
2997#endif
2998
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002999 if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
3000 {
3001 mbedtls_printf( " ! Client initiated reconnection from same port\n" );
3002 goto handshake;
3003 }
3004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003006 if( ret != 0 )
3007 {
3008 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009 mbedtls_strerror( ret, error_buf, 100 );
3010 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003011 }
3012#endif
3013
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003014 mbedtls_net_free( &client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01003015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 mbedtls_ssl_session_reset( &ssl );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003017
3018 /*
3019 * 3. Wait until a client connects
3020 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 mbedtls_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003022 fflush( stdout );
3023
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003024 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02003025 client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003026 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02003027#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003028 if( received_sigterm )
3029 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01003030 mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" );
3031 if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED )
3032 ret = 0;
3033
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003034 goto exit;
3035 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02003036#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02003037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038 mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003039 goto exit;
3040 }
3041
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003042 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003043 ret = mbedtls_net_set_nonblock( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003044 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02003045 ret = mbedtls_net_set_block( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003046 if( ret != 0 )
3047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01003049 goto exit;
3050 }
3051
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003052 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
3055 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003056 {
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02003057 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
3058 client_ip, cliip_len ) ) != 0 )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003059 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01003060 mbedtls_printf( " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n",
3061 -ret );
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003062 goto exit;
3063 }
3064 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02003066
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02003067#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3068 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
3069 {
3070 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
3071 (const unsigned char *) opt.ecjpake_pw,
3072 strlen( opt.ecjpake_pw ) ) ) != 0 )
3073 {
3074 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
3075 goto exit;
3076 }
3077 }
3078#endif
3079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003081
3082 /*
3083 * 4. Handshake
3084 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003085handshake:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003087 fflush( stdout );
3088
Hanno Becker16970d22017-10-10 15:56:37 +01003089 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003090 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003091#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003092 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003093 ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003094 {
3095 mbedtls_printf( " cancelling on injected error\n" );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003096 break;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003097 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003098#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskineb44692f2018-04-24 12:18:19 +02003099
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003100 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003101 break;
3102
3103 /* For event-driven IO, wait for socket to become available */
3104 if( opt.event == 1 /* level triggered IO */ )
3105 {
3106#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003107 ret = idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003108#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003109 ret = idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003110#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00003111 if( ret != 0 )
3112 goto reset;
Hanno Becker16970d22017-10-10 15:56:37 +01003113 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01003114 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118 mbedtls_printf( " hello verification requested\n" );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003119 ret = 0;
3120 goto reset;
3121 }
3122 else if( ret != 0 )
3123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02003125
3126#if defined(MBEDTLS_X509_CRT_PARSE_C)
3127 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3128 {
3129 char vrfy_buf[512];
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02003130 flags = mbedtls_ssl_get_verify_result( &ssl );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02003131
3132 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
3133
3134 mbedtls_printf( "%s\n", vrfy_buf );
3135 }
3136#endif
3137
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003138#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01003139 if( opt.async_private_error < 0 )
3140 /* Injected error only the first time round, to test reset */
3141 ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
3142#endif
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003143 goto reset;
3144 }
3145 else /* ret == 0 */
3146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
3148 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02003149 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
3152 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02003153 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02003155
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003156#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3157 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
3158 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
3159#endif
3160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003162 if( opt.alpn_string != NULL )
3163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
3165 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003166 alp ? alp : "(none)" );
3167 }
3168#endif
3169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003171 /*
Ron Eldor2a47be52017-06-20 15:23:23 +03003172 * 5. Verify the client certificate
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003175
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003176 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003177 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003178 char vrfy_buf[512];
3179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180 mbedtls_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003181
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003182 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003183
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003184 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003185 }
3186 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003188
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003189 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003190 {
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003191 char crt_buf[512];
3192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 mbedtls_printf( " . Peer certificate information ...\n" );
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003194 mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195 mbedtls_ssl_get_peer_cert( &ssl ) );
Manuel Pégourié-Gonnard67557172015-07-02 11:15:48 +02003196 mbedtls_printf( "%s\n", crt_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003197 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003199
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003200#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Ron Eldor51d3ab52019-05-12 14:54:30 +03003201 if( opt.eap_tls != 0 )
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003202 {
3203 size_t j = 0;
Ron Eldor51d3ab52019-05-12 14:54:30 +03003204
3205 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type,
3206 eap_tls_keying.master_secret,
3207 sizeof( eap_tls_keying.master_secret ),
3208 eap_tls_label,
3209 eap_tls_keying.randbytes,
3210 sizeof( eap_tls_keying.randbytes ),
3211 eap_tls_keymaterial,
3212 sizeof( eap_tls_keymaterial ) ) )
3213 != 0 )
3214 {
3215 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
3216 -ret );
3217 goto exit;
3218 }
3219
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003220 mbedtls_printf( " EAP-TLS key material is:" );
3221 for( j = 0; j < sizeof( eap_tls_keymaterial ); j++ )
3222 {
3223 if( j % 8 == 0 )
3224 mbedtls_printf("\n ");
3225 mbedtls_printf("%02x ", eap_tls_keymaterial[j] );
3226 }
3227 mbedtls_printf("\n");
3228
Ron Eldor51d3ab52019-05-12 14:54:30 +03003229 if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type, NULL, 0,
Ron Eldor51c45072019-05-15 17:49:54 +03003230 eap_tls_label,
3231 eap_tls_keying.randbytes,
3232 sizeof( eap_tls_keying.randbytes ),
3233 eap_tls_iv,
3234 sizeof( eap_tls_iv ) ) ) != 0 )
Ron Eldor51d3ab52019-05-12 14:54:30 +03003235 {
3236 mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
3237 -ret );
3238 goto exit;
3239 }
3240
Ron Eldorb7fd64c2019-05-12 11:03:32 +03003241 mbedtls_printf( " EAP-TLS IV is:" );
3242 for( j = 0; j < sizeof( eap_tls_iv ); j++ )
3243 {
3244 if( j % 8 == 0 )
3245 mbedtls_printf("\n ");
3246 mbedtls_printf("%02x ", eap_tls_iv[j] );
3247 }
3248 mbedtls_printf("\n");
3249 }
3250#endif
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02003251 if( opt.exchanges == 0 )
3252 goto close_notify;
3253
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003254 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003255data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003256 /*
3257 * 6. Read the HTTP Request
3258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259 mbedtls_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003260 fflush( stdout );
3261
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003262 /*
3263 * TLS and DTLS need different reading styles (stream vs datagram)
3264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003266 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003267 do
3268 {
3269 int terminated = 0;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003270 len = opt.buffer_size - 1;
3271 memset( buf, 0, opt.buffer_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003273
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003274 if( mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003275 {
3276 if( opt.event == 1 /* level triggered IO */ )
3277 {
3278#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003279 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003280#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003281 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003282#endif
3283 }
3284
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003285 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01003286 }
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003287
3288 if( ret <= 0 )
3289 {
3290 switch( ret )
3291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3293 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003294 goto close_notify;
3295
3296 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297 case MBEDTLS_ERR_NET_CONN_RESET:
3298 mbedtls_printf( " connection was reset by peer\n" );
3299 ret = MBEDTLS_ERR_NET_CONN_RESET;
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003300 goto reset;
3301
3302 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003304 goto reset;
3305 }
3306 }
3307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308 if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003309 {
3310 len = ret;
3311 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003313
3314 /* End of message should be detected according to the syntax of the
3315 * application protocol (eg HTTP), just use a dummy test here. */
3316 if( buf[len - 1] == '\n' )
3317 terminated = 1;
3318 }
3319 else
3320 {
3321 int extra_len, ori_len;
3322 unsigned char *larger_buf;
3323
3324 ori_len = ret;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02003325 extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003326
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003327 larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003328 if( larger_buf == NULL )
3329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330 mbedtls_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003331 ret = 1;
3332 goto reset;
3333 }
3334
3335 memset( larger_buf, 0, ori_len + extra_len );
3336 memcpy( larger_buf, buf, ori_len );
3337
3338 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003340 if( ret != extra_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341 mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003344 ret = 1;
3345 goto reset;
3346 }
3347
3348 larger_buf[ori_len + extra_len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 mbedtls_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003350 ori_len + extra_len, ori_len, extra_len,
3351 (char *) larger_buf );
3352
3353 /* End of message should be detected according to the syntax of the
3354 * application protocol (eg HTTP), just use a dummy test here. */
3355 if( larger_buf[ori_len + extra_len - 1] == '\n' )
3356 terminated = 1;
3357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358 mbedtls_free( larger_buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003359 }
3360
3361 if( terminated )
3362 {
3363 ret = 0;
3364 break;
3365 }
3366 }
3367 while( 1 );
3368 }
3369 else /* Not stream, so datagram */
3370 {
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003371 len = opt.buffer_size - 1;
3372 memset( buf, 0, opt.buffer_size );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003373
Gilles Peskineb44692f2018-04-24 12:18:19 +02003374 do
Hanno Becker16970d22017-10-10 15:56:37 +01003375 {
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003376 /* Without the call to `mbedtls_ssl_check_pending`, it might
3377 * happen that the client sends application data in the same
3378 * datagram as the Finished message concluding the handshake.
3379 * In this case, the application data would be ready to be
3380 * processed while the underlying transport wouldn't signal
3381 * any further incoming data.
3382 *
3383 * See the test 'Event-driven I/O: session-id resume, UDP packing'
3384 * in tests/ssl-opt.sh.
3385 */
3386
3387 /* For event-driven IO, wait for socket to become available */
3388 if( mbedtls_ssl_check_pending( &ssl ) == 0 &&
3389 opt.event == 1 /* level triggered IO */ )
3390 {
3391#if defined(MBEDTLS_TIMING_C)
3392 idle( &client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ );
3393#else
3394 idle( &client_fd, MBEDTLS_ERR_SSL_WANT_READ );
3395#endif
3396 }
3397
Hanno Becker16970d22017-10-10 15:56:37 +01003398 ret = mbedtls_ssl_read( &ssl, buf, len );
3399
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003400 /* Note that even if `mbedtls_ssl_check_pending` returns true,
3401 * it can happen that the subsequent call to `mbedtls_ssl_read`
3402 * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
3403 * might be discarded (e.g. because they are retransmissions). */
Hanno Becker16970d22017-10-10 15:56:37 +01003404 }
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003405 while( mbedtls_status_is_ssl_in_progress( ret ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003406
3407 if( ret <= 0 )
3408 {
3409 switch( ret )
3410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3412 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003413 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003414 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003415
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003416 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003418 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003419 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003420 }
3421
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003422 len = ret;
3423 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003425 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003426 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003427
3428 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003429 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02003430 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00003433 if( opt.renegotiate && exchanges_left == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 mbedtls_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003436 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003439 {
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003440 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003443 goto reset;
3444 }
Hanno Becker16970d22017-10-10 15:56:37 +01003445
3446 /* For event-driven IO, wait for socket to become available */
3447 if( opt.event == 1 /* level triggered IO */ )
3448 {
3449#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003450 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003451#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003452 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003453#endif
3454 }
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003455 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003458 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003459#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003460
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003461 /*
3462 * 7. Write the 200 Response
3463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464 mbedtls_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003465 fflush( stdout );
3466
3467 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003469
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003470 /* Add padding to the response to reach opt.response_size in length */
3471 if( opt.response_size != DFL_RESPONSE_SIZE &&
3472 len < opt.response_size )
3473 {
3474 memset( buf + len, 'B', opt.response_size - len );
3475 len += opt.response_size - len;
3476 }
3477
3478 /* Truncate if response size is smaller than the "natural" size */
3479 if( opt.response_size != DFL_RESPONSE_SIZE &&
3480 len > opt.response_size )
3481 {
3482 len = opt.response_size;
3483
3484 /* Still end with \r\n unless that's really not possible */
3485 if( len >= 2 ) buf[len - 2] = '\r';
3486 if( len >= 1 ) buf[len - 1] = '\n';
3487 }
3488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003490 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003491 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003494 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498 mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003499 goto reset;
3500 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003501
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003502 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003504 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003505 goto reset;
3506 }
Hanno Becker16970d22017-10-10 15:56:37 +01003507
3508 /* For event-driven IO, wait for socket to become available */
3509 if( opt.event == 1 /* level triggered IO */ )
3510 {
3511#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003512 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003513#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003514 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003515#endif
3516 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003517 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003518 }
3519 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003520 else /* Not stream, so datagram */
3521 {
Hanno Becker16970d22017-10-10 15:56:37 +01003522 while( 1 )
3523 {
3524 ret = mbedtls_ssl_write( &ssl, buf, len );
3525
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003526 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003527 break;
3528
3529 /* For event-driven IO, wait for socket to become available */
3530 if( opt.event == 1 /* level triggered IO */ )
3531 {
3532#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003533 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003534#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003535 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003536#endif
3537 }
3538 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003539
3540 if( ret < 0 )
3541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003543 goto reset;
3544 }
3545
3546 frags = 1;
3547 written = ret;
3548 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003549
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003550 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003551 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 +01003552 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01003553
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003554 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003555 * 7b. Continue doing data exchanges?
3556 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003557 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003558 goto data_exchange;
3559
3560 /*
3561 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003562 */
3563close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01003565
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003566 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003568 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003569 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571 mbedtls_printf( " done\n" );
Rich Evansf90016a2015-01-19 14:26:37 +00003572
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003573 goto reset;
3574
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003575 /*
3576 * Cleanup and exit
3577 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003578exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003580 if( ret != 0 )
3581 {
3582 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 mbedtls_strerror( ret, error_buf, 100 );
3584 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003585 }
3586#endif
3587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 mbedtls_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003589 fflush( stdout );
3590
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003591 mbedtls_net_free( &client_fd );
3592 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +02003593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3595 mbedtls_dhm_free( &dhm );
Paul Bakkera317a982014-06-18 16:44:11 +02003596#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597#if defined(MBEDTLS_X509_CRT_PARSE_C)
3598 mbedtls_x509_crt_free( &cacert );
3599 mbedtls_x509_crt_free( &srvcert );
3600 mbedtls_pk_free( &pkey );
3601 mbedtls_x509_crt_free( &srvcert2 );
3602 mbedtls_pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02003603#endif
Gilles Peskine44817442018-06-13 18:09:28 +02003604#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3605 for( i = 0; (size_t) i < ssl_async_keys.slots_used; i++ )
3606 {
3607 if( ssl_async_keys.slots[i].pk_owned )
3608 {
3609 mbedtls_pk_free( ssl_async_keys.slots[i].pk );
3610 mbedtls_free( ssl_async_keys.slots[i].pk );
3611 ssl_async_keys.slots[i].pk = NULL;
3612 }
3613 }
3614#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02003615#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003616 sni_free( sni_info );
3617#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00003619 if( ( ret = psk_free( psk_info ) ) != 0 )
3620 mbedtls_printf( "Failed to list of opaque PSKs - error was %d\n", ret );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003621#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3623 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003624#endif
Paul Bakkered27a042013-04-18 22:46:23 +02003625
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00003626#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
3627 defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker1d911cd2018-11-15 13:06:09 +00003628 if( opt.psk_opaque != 0 )
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00003629 {
3630 /* This is ok even if the slot hasn't been
3631 * initialized (we might have jumed here
3632 * immediately because of bad cmd line params,
3633 * for example). */
Hanno Becker1d911cd2018-11-15 13:06:09 +00003634 status = psa_destroy_key( psk_slot );
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00003635 if( status != PSA_SUCCESS )
3636 {
3637 mbedtls_printf( "Failed to destroy key slot %u - error was %d",
Hanno Becker1d911cd2018-11-15 13:06:09 +00003638 (unsigned) psk_slot, (int) status );
Hanno Beckerc43b6ea2018-11-05 13:48:43 +00003639 }
3640 }
3641#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED &&
3642 MBEDTLS_USE_PSA_CRYPTO */
3643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003645 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 mbedtls_ctr_drbg_free( &ctr_drbg );
3647 mbedtls_entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649#if defined(MBEDTLS_SSL_CACHE_C)
3650 mbedtls_ssl_cache_free( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00003651#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003652#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3653 mbedtls_ssl_ticket_free( &ticket_ctx );
3654#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655#if defined(MBEDTLS_SSL_COOKIE_C)
3656 mbedtls_ssl_cookie_free( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02003657#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003658
Hanno Becker095d9cf2018-10-09 12:39:13 +01003659 mbedtls_free( buf );
3660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
3662#if defined(MBEDTLS_MEMORY_DEBUG)
3663 mbedtls_memory_buffer_alloc_status();
Paul Bakker82024bf2013-07-04 11:52:32 +02003664#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +02003666#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02003667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668 mbedtls_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003669
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003670#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003672 fflush( stdout ); getchar();
3673#endif
3674
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02003675 // Shell can not handle large exit numbers -> 1 for errors
3676 if( ret < 0 )
3677 ret = 1;
3678
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003679 return( ret );
3680}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003681#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
3682 MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003683 MBEDTLS_CTR_DRBG_C */