blob: 5d751b6a720192a73c548a2ae11edb1a5adf64c5 [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>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_free free
Simon Butcherd3138c32016-04-27 01:26:50 +010034#define mbedtls_time time
35#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020036#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define mbedtls_fprintf fprintf
38#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010039#define mbedtls_exit exit
40#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
41#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evans18b78c72015-02-11 14:06:19 +000042#endif
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020043
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020044#if !defined(MBEDTLS_ENTROPY_C) || \
45 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020046 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020047int main( void )
48{
49 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
50 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020051 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020052 return( 0 );
53}
54#else
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010055
Andres AG788aa4a2016-09-14 14:32:09 +010056#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/ssl.h"
58#include "mbedtls/entropy.h"
59#include "mbedtls/ctr_drbg.h"
60#include "mbedtls/certs.h"
61#include "mbedtls/x509.h"
62#include "mbedtls/error.h"
63#include "mbedtls/debug.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020064#include "mbedtls/timing.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000065
Rich Evans18b78c72015-02-11 14:06:19 +000066#include <stdio.h>
67#include <stdlib.h>
68#include <string.h>
Andres AG692ad842017-01-19 16:30:57 +000069#include <stdint.h>
Andres AG0b736db2017-02-08 14:05:57 +000070
71#if !defined(_MSC_VER)
Andres AG692ad842017-01-19 16:30:57 +000072#include <inttypes.h>
Andres AG0b736db2017-02-08 14:05:57 +000073#endif
Rich Evans18b78c72015-02-11 14:06:19 +000074
75#if !defined(_WIN32)
76#include <signal.h>
77#endif
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000080#include "mbedtls/ssl_cache.h"
Paul Bakker0a597072012-09-25 21:55:46 +000081#endif
82
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020083#if defined(MBEDTLS_SSL_TICKET_C)
84#include "mbedtls/ssl_ticket.h"
85#endif
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000088#include "mbedtls/ssl_cookie.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020089#endif
90
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000092#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker82024bf2013-07-04 11:52:32 +020093#endif
94
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020095#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO)
96#define SNI_OPTION
97#endif
98
99#if defined(_WIN32)
100#include <windows.h>
101#endif
102
Simon Butchercce68be2018-07-23 14:26:09 +0100103/* Size of memory to be allocated for the heap, when using the library's memory
104 * management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */
105#define MEMORY_HEAP_SIZE 120000
106
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100107#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200108#define DFL_SERVER_PORT "4433"
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200109#define DFL_RESPONSE_SIZE -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000110#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100111#define DFL_NBIO 0
Hanno Becker16970d22017-10-10 15:56:37 +0100112#define DFL_EVENT 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200113#define DFL_READ_TIMEOUT 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000114#define DFL_CA_FILE ""
115#define DFL_CA_PATH ""
116#define DFL_CRT_FILE ""
117#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200118#define DFL_CRT_FILE2 ""
119#define DFL_KEY_FILE2 ""
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100120#define DFL_ASYNC_OPERATIONS "-"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100121#define DFL_ASYNC_PRIVATE_DELAY1 ( -1 )
122#define DFL_ASYNC_PRIVATE_DELAY2 ( -1 )
Gilles Peskine3665f1d2018-01-05 21:22:12 +0100123#define DFL_ASYNC_PRIVATE_ERROR ( 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +0200124#define DFL_PSK ""
125#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200126#define DFL_ECJPAKE_PW NULL
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200127#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000128#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200129#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100131#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100132#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200133#define DFL_RENEGO_DELAY -2
Andres AG692ad842017-01-19 16:30:57 +0000134#define DFL_RENEGO_PERIOD ( (uint64_t)-1 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200135#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200136#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200137#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000138#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +0200139#define DFL_SHA1 -1
Hanno Becker1029ace2019-04-09 17:28:10 +0100140#define DFL_CID_ENABLED 0
141#define DFL_CID_VALUE ""
Hanno Becker96870292019-05-03 17:30:59 +0100142#define DFL_CID_ENABLED_RENEGO -1
143#define DFL_CID_VALUE_RENEGO NULL
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100144#define DFL_AUTH_MODE -1
Janos Follath4817e272017-04-10 13:44:33 +0100145#define DFL_CERT_REQ_CA_LIST MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100147#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200149#define DFL_TICKET_TIMEOUT 86400
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100150#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100151#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100152#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200153#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100154#define DFL_CURVES NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200155#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200157#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200158#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200159#define DFL_HS_TO_MIN 0
160#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200161#define DFL_DTLS_MTU -1
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200162#define DFL_BADMAC_LIMIT -1
Hanno Beckere7675d02018-08-14 13:28:56 +0100163#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200164#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100165#define DFL_ETM -1
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +0300166#define DFL_SERIALIZE 0
Jarno Lamsa41b35912019-06-10 15:51:11 +0300167#define DFL_EXTENDED_MS_ENFORCE -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000168
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200169#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
170 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
171 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
172 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
173 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
174 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
175 "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 +0200176
Paul Bakker8e714d72013-07-18 11:05:13 +0200177/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
178 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000179#define HTTP_RESPONSE \
180 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000181 "<h2>mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200182 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000183
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200184/*
185 * Size of the basic I/O buffer. Able to hold our default response.
186 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 * 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 +0200188 * if you change this value to something outside the range <= 100 or > 500
189 */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200190#define DFL_IO_BUF_LEN 200
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#if defined(MBEDTLS_X509_CRT_PARSE_C)
193#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000194#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100195 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
196 " default: \"\" (pre-loaded)\n" \
Hanno Becker1ce1a512019-05-01 11:16:28 +0100197 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100198 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
199 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
Hanno Becker1ce1a512019-05-01 11:16:28 +0100200 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100201 " 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 +0200202 " default: see note after key_file2\n" \
203 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200204 " 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 +0200205 " default: see note after key_file2\n" \
206 " key_file2=%%s default: see note below\n" \
207 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200208 " preloaded certificate(s) and key(s) are used if available\n" \
209 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
210 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000211#else
212#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200213 "\n" \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 " No file operations available (MBEDTLS_FS_IO not defined)\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200215 "\n"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200217#else
218#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200220
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200221#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100222#define USAGE_SSL_ASYNC \
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100223 " async_operations=%%c... d=decrypt, s=sign (default: -=off)\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100224 " async_private_delay1=%%d Asynchronous delay for key_file or preloaded key\n" \
Gilles Peskine166ce742018-04-30 10:30:49 +0200225 " async_private_delay2=%%d Asynchronous delay for key_file2 and sni\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100226 " default: -1 (not asynchronous)\n" \
Gilles Peskine60ee4ca2018-01-08 11:28:05 +0100227 " async_private_error=%%d Async callback error injection (default=0=none,\n" \
Gilles Peskinec9125722018-04-26 07:15:40 +0200228 " 1=start, 2=cancel, 3=resume, negative=first time only)"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100229#else
230#define USAGE_SSL_ASYNC ""
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200231#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100232
Hanno Beckera5a2b082019-05-15 14:03:01 +0100233#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1029ace2019-04-09 17:28:10 +0100234#define USAGE_CID \
235 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
236 " default: 0 (disabled)\n" \
Hanno Becker96870292019-05-03 17:30:59 +0100237 " cid_renego=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension during renegotiation.\n" \
Hanno Beckerc8f43d82019-05-23 17:01:06 +0100238 " default: same as 'cid' parameter\n" \
Hanno Becker1029ace2019-04-09 17:28:10 +0100239 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
Hanno Becker96870292019-05-03 17:30:59 +0100240 " default: \"\"\n" \
241 " cid_val_renego=%%s The CID to use for incoming messages (in hex, without 0x) after renegotiation.\n" \
Hanno Beckerc8f43d82019-05-23 17:01:06 +0100242 " default: same as 'cid_val' parameter\n"
Hanno Beckera5a2b082019-05-15 14:03:01 +0100243#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1029ace2019-04-09 17:28:10 +0100244#define USAGE_CID ""
Hanno Beckera5a2b082019-05-15 14:03:01 +0100245#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1029ace2019-04-09 17:28:10 +0100246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd0d01c52018-10-25 16:49:48 +0100248#define USAGE_PSK \
249 " psk=%%s default: \"\" (in hex, without 0x)\n" \
250 " psk_list=%%s default: \"\"\n" \
Hanno Becker5ddc0632018-10-26 10:04:13 +0100251 " A list of (PSK identity, PSK value) pairs.\n" \
252 " The PSK values are in hex, without 0x.\n" \
Hanno Beckerd0d01c52018-10-25 16:49:48 +0100253 " id1,psk1[,id2,psk2[,...]]\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200254 " psk_identity=%%s default: \"Client_identity\"\n"
255#else
256#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200260#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100261 " tickets=%%d default: 1 (enabled)\n" \
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200262 " ticket_timeout=%%d default: 86400 (one day)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200263#else
264#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100268#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100269 " cache_max=%%d default: cache default (50)\n" \
270 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100271#else
272#define USAGE_CACHE ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273#endif /* MBEDTLS_SSL_CACHE_C */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100274
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200275#if defined(SNI_OPTION)
Ron Eldor24eec792019-04-04 15:02:01 +0300276#if defined(MBEDTLS_X509_CRL_PARSE_C)
277#define SNI_CRL ",crl"
278#else
279#define SNI_CRL ""
280#endif
281
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100282#define USAGE_SNI \
Ron Eldor24eec792019-04-04 15:02:01 +0300283 " sni=%%s name1,cert1,key1,ca1"SNI_CRL",auth1[,...]\n" \
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200284 " default: disabled\n"
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100285#else
286#define USAGE_SNI ""
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200287#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200290#define USAGE_MAX_FRAG_LEN \
291 " max_frag_len=%%d default: 16384 (tls default)\n" \
292 " options: 512, 1024, 2048, 4096\n"
293#else
294#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100298#define USAGE_TRUNC_HMAC \
299 " trunc_hmac=%%d default: library default\n"
300#else
301#define USAGE_TRUNC_HMAC ""
302#endif
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200305#define USAGE_ALPN \
306 " alpn=%%s default: \"\" (disabled)\n" \
307 " example: spdy/1,http/1.1\n"
308#else
309#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200313#define USAGE_COOKIES \
314 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200315 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200316#else
317#define USAGE_COOKIES ""
318#endif
319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200321#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200322 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200323#else
324#define USAGE_ANTI_REPLAY ""
325#endif
326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200328#define USAGE_BADMAC_LIMIT \
329 " badmac_limit=%%d default: (library default: disabled)\n"
330#else
331#define USAGE_BADMAC_LIMIT ""
332#endif
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200335#define USAGE_DTLS \
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +0100336 " dtls=%%d default: 0 (TLS) (if both enabled)\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200337 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200338 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Beckere7675d02018-08-14 13:28:56 +0100339 " mtu=%%d default: (library default: unlimited)\n" \
340 " dgram_packing=%%d default: 1 (allowed)\n" \
341 " allow or forbid packing of multiple\n" \
342 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200343#else
344#define USAGE_DTLS ""
345#endif
346
Hanno Beckerf765ce62019-06-21 13:17:14 +0100347#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
348 !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET) && \
349 !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200350#define USAGE_EMS \
Jarno Lamsa41b35912019-06-10 15:51:11 +0300351 " extended_ms=0/1 default: (library default: on)\n" \
352 " enforce_extended_master_secret=0/1 default: (library default: off)\n"
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200353#else
354#define USAGE_EMS ""
355#endif
356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100358#define USAGE_ETM \
359 " etm=0/1 default: (library default: on)\n"
360#else
361#define USAGE_ETM ""
362#endif
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100365#define USAGE_RENEGO \
366 " renegotiation=%%d default: 0 (disabled)\n" \
367 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100368 " renego_delay=%%d default: -2 (library default)\n" \
Andres AG692ad842017-01-19 16:30:57 +0000369 " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100370#else
371#define USAGE_RENEGO ""
372#endif
373
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200374#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
375#define USAGE_ECJPAKE \
376 " ecjpake_pw=%%s default: none (disabled)\n"
377#else
378#define USAGE_ECJPAKE ""
379#endif
380
Hanno Beckere6706e62017-05-15 16:05:15 +0100381#if defined(MBEDTLS_ECP_C)
382#define USAGE_CURVES \
383 " curves=a,b,c,d default: \"default\" (library default)\n" \
384 " example: \"secp521r1,brainpoolP512r1\"\n" \
385 " - use \"none\" for empty list\n" \
386 " - see mbedtls_ecp_curve_list()\n" \
387 " for acceptable curve names\n"
388#else
389#define USAGE_CURVES ""
390#endif
391
Jarno Lamsacf1b6722019-06-04 11:06:31 +0300392#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
393#define USAGE_SERIALIZATION \
Jarno Lamsab5ff6a42019-06-06 10:40:52 +0300394 " serialize=%%d default: 0 (do not serialize/deserialize)\n" \
Jarno Lamsa85c23802019-06-07 08:39:24 +0300395 " options: 1 (serialize)\n" \
396 " 2 (serialize with re-initialization)\n"
Jarno Lamsacf1b6722019-06-04 11:06:31 +0300397#else
398#define USAGE_SERIALIZATION ""
399#endif
400
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000401#define USAGE \
402 "\n usage: ssl_server2 param=<>...\n" \
403 "\n acceptable parameters:\n" \
Ron Eldor71f68c42017-09-26 11:29:11 +0300404 " server_addr=%%s default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000405 " server_port=%%d default: 4433\n" \
406 " debug_level=%%d default: 0 (disabled)\n" \
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200407 " buffer_size=%%d default: 200 \n" \
408 " (minimum: 1, max: 16385)\n" \
409 " response_size=%%d default: about 152 (basic response)\n" \
410 " (minimum: 0, max: 16384)\n" \
411 " increases buffer_size if bigger\n"\
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100412 " nbio=%%d default: 0 (blocking I/O)\n" \
413 " options: 1 (non-blocking), 2 (added delays)\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100414 " event=%%d default: 0 (loop)\n" \
415 " options: 1 (level-triggered, implies nbio=1),\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200416 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100417 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200418 USAGE_DTLS \
419 USAGE_COOKIES \
420 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200421 USAGE_BADMAC_LIMIT \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200422 "\n" \
Manuel Pégourié-Gonnardee6139c2015-05-07 10:18:26 +0100423 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100424 " options: none, optional, required\n" \
Janos Follath4817e272017-04-10 13:44:33 +0100425 " cert_req_ca_list=%%d default: 1 (send ca list)\n" \
426 " options: 1 (send ca list), 0 (don't send)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000427 USAGE_IO \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100428 USAGE_SSL_ASYNC \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100429 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100430 "\n" \
431 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200432 USAGE_ECJPAKE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100433 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100434 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100435 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200436 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200437 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100438 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100439 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100440 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100441 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200442 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200443 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100444 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100445 USAGE_CURVES \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100446 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100447 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200448 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200449 " min_version=%%s default: (library default: tls1)\n" \
450 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200451 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100452 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200453 "\n" \
454 " version_suites=a,b,c,d per-version ciphersuites\n" \
455 " in order from ssl3 to tls1_2\n" \
456 " default: all enabled\n" \
457 " force_ciphersuite=<name> default: all enabled\n" \
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100458 " query_config=<name> return 0 if the specified\n" \
459 " configuration macro is defined and 1\n" \
460 " otherwise. The expansion of the macro\n" \
461 " is printed if it is defined\n" \
Jarno Lamsacf1b6722019-06-04 11:06:31 +0300462 USAGE_SERIALIZATION \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000463 " acceptable ciphersuite names:\n"
464
Andres AG692ad842017-01-19 16:30:57 +0000465
Hanno Becker8651a432017-06-09 16:13:22 +0100466#define ALPN_LIST_SIZE 10
467#define CURVE_LIST_SIZE 20
468
Andres AG692ad842017-01-19 16:30:57 +0000469#define PUT_UINT64_BE(out_be,in_le,i) \
470{ \
471 (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \
472 (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \
473 (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \
474 (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \
475 (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \
476 (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \
477 (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \
478 (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \
479}
480
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100481#if defined(MBEDTLS_CHECK_PARAMS)
482#include "mbedtls/platform_util.h"
483void mbedtls_param_failed( const char *failure_condition,
484 const char *file,
485 int line )
Simon Butcher63cb97e2018-12-06 17:43:31 +0000486{
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100487 mbedtls_printf( "%s:%i: Input param failed - %s\n",
488 file, line, failure_condition );
Simon Butcher63cb97e2018-12-06 17:43:31 +0000489 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
490}
491#endif
492
Rich Evans85b05ec2015-02-12 11:37:29 +0000493/*
494 * global options
495 */
496struct options
497{
498 const char *server_addr; /* address on which the ssl service runs */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200499 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000500 int debug_level; /* level of debugging */
501 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100502 int event; /* loop or event-driven IO? level or edge triggered? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200504 int response_size; /* pad response with header to requested size */
505 uint16_t buffer_size; /* IO buffer size */
Rich Evans85b05ec2015-02-12 11:37:29 +0000506 const char *ca_file; /* the file with the CA certificate(s) */
507 const char *ca_path; /* the path with the CA certificate(s) reside */
508 const char *crt_file; /* the file with the server certificate */
509 const char *key_file; /* the file with the server key */
510 const char *crt_file2; /* the file with the 2nd server certificate */
511 const char *key_file2; /* the file with the 2nd server key */
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100512 const char *async_operations; /* supported SSL asynchronous operations */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100513 int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
514 int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
515 int async_private_error; /* inject error in async private callback */
Rich Evans85b05ec2015-02-12 11:37:29 +0000516 const char *psk; /* the pre-shared key */
517 const char *psk_identity; /* the pre-shared key identity */
518 char *psk_list; /* list of PSK id/key pairs for callback */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200519 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000520 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
521 const char *version_suites; /* per-version ciphersuites */
522 int renegotiation; /* enable / disable renegotiation */
523 int allow_legacy; /* allow legacy renegotiation */
524 int renegotiate; /* attempt renegotiation? */
525 int renego_delay; /* delay before enforcing renegotiation */
Andres AG692ad842017-01-19 16:30:57 +0000526 uint64_t renego_period; /* period for automatic renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000527 int exchanges; /* number of data exchanges */
528 int min_version; /* minimum protocol version accepted */
529 int max_version; /* maximum protocol version accepted */
530 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200531 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000532 int auth_mode; /* verify mode for connection */
Janos Follath4817e272017-04-10 13:44:33 +0100533 int cert_req_ca_list; /* should we send the CA list? */
Rich Evans85b05ec2015-02-12 11:37:29 +0000534 unsigned char mfl_code; /* code for maximum fragment length */
535 int trunc_hmac; /* accept truncated hmac? */
536 int tickets; /* enable / disable session tickets */
537 int ticket_timeout; /* session ticket lifetime */
538 int cache_max; /* max number of session cache entries */
539 int cache_timeout; /* expiration delay of session cache entries */
540 char *sni; /* string describing sni information */
Hanno Beckere6706e62017-05-15 16:05:15 +0100541 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000542 const char *alpn_string; /* ALPN supported protocols */
543 const char *dhm_file; /* the file with the DH parameters */
544 int extended_ms; /* allow negotiation of extended MS? */
Jarno Lamsa41b35912019-06-10 15:51:11 +0300545 int enforce_extended_master_secret; /* Enforce the usage of extended
546 * master secret */
Rich Evans85b05ec2015-02-12 11:37:29 +0000547 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000548 int transport; /* TLS or DTLS? */
549 int cookies; /* Use cookies for DTLS? -1 to break them */
550 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
551 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
552 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200553 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Hanno Beckere7675d02018-08-14 13:28:56 +0100554 int dgram_packing; /* allow/forbid datagram packing */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000555 int badmac_limit; /* Limit of records with bad MAC */
Hanno Becker1029ace2019-04-09 17:28:10 +0100556 int cid_enabled; /* whether to use the CID extension or not */
Hanno Becker96870292019-05-03 17:30:59 +0100557 int cid_enabled_renego; /* whether to use the CID extension or not
558 * during renegotiation */
Hanno Becker1029ace2019-04-09 17:28:10 +0100559 const char *cid_val; /* the CID to use for incoming messages */
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +0300560 int serialize; /* serialize/deserialize connection */
Hanno Becker96870292019-05-03 17:30:59 +0100561 const char *cid_val_renego; /* the CID to use for incoming messages
562 * after renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000563} opt;
564
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100565int query_config( const char *config );
566
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200567static void my_debug( void *ctx, int level,
568 const char *file, int line,
569 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000570{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200571 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000572
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200573 /* Extract basename from file */
574 for( p = basename = file; *p != '\0'; p++ )
575 if( *p == '/' || *p == '\\' )
576 basename = p + 1;
577
578 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000579 fflush( (FILE *) ctx );
580}
581
582/*
583 * Test recv/send functions that make sure each try returns
584 * WANT_READ/WANT_WRITE at least once before sucesseding
585 */
586static int my_recv( void *ctx, unsigned char *buf, size_t len )
587{
588 static int first_try = 1;
589 int ret;
590
591 if( first_try )
592 {
593 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100594 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000595 }
596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100598 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000599 first_try = 1; /* Next call will be a new operation */
600 return( ret );
601}
602
603static int my_send( void *ctx, const unsigned char *buf, size_t len )
604{
605 static int first_try = 1;
606 int ret;
607
608 if( first_try )
609 {
610 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100611 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000612 }
613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100615 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000616 first_try = 1; /* Next call will be a new operation */
617 return( ret );
618}
619
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200620/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200621 * Return authmode from string, or -1 on error
622 */
623static int get_auth_mode( const char *s )
624{
625 if( strcmp( s, "none" ) == 0 )
626 return( MBEDTLS_SSL_VERIFY_NONE );
627 if( strcmp( s, "optional" ) == 0 )
628 return( MBEDTLS_SSL_VERIFY_OPTIONAL );
629 if( strcmp( s, "required" ) == 0 )
630 return( MBEDTLS_SSL_VERIFY_REQUIRED );
631
632 return( -1 );
633}
634
635/*
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200636 * Used by sni_parse and psk_parse to handle coma-separated lists
637 */
638#define GET_ITEM( dst ) \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100639 do \
640 { \
641 (dst) = p; \
642 while( *p != ',' ) \
643 if( ++p > end ) \
644 goto error; \
645 *p++ = '\0'; \
646 } while( 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200647
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200648#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100649typedef struct _sni_entry sni_entry;
650
651struct _sni_entry {
652 const char *name;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_x509_crt *cert;
654 mbedtls_pk_context *key;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200655 mbedtls_x509_crt* ca;
656 mbedtls_x509_crl* crl;
657 int authmode;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100658 sni_entry *next;
659};
660
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100661void sni_free( sni_entry *head )
662{
663 sni_entry *cur = head, *next;
664
665 while( cur != NULL )
666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_x509_crt_free( cur->cert );
668 mbedtls_free( cur->cert );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_pk_free( cur->key );
671 mbedtls_free( cur->key );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100672
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200673 mbedtls_x509_crt_free( cur->ca );
674 mbedtls_free( cur->ca );
Ron Eldor24eec792019-04-04 15:02:01 +0300675#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200676 mbedtls_x509_crl_free( cur->crl );
677 mbedtls_free( cur->crl );
Ron Eldor24eec792019-04-04 15:02:01 +0300678#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100679 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_free( cur );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100681 cur = next;
682 }
683}
684
685/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200686 * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
687 * into a usable sni_entry list. For ca1, crl1, auth1, the special value
688 * '-' means unset. If ca1 is unset, then crl1 is ignored too.
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000689 *
690 * Modifies the input string! This is not production quality!
691 */
692sni_entry *sni_parse( char *sni_string )
693{
694 sni_entry *cur = NULL, *new = NULL;
695 char *p = sni_string;
696 char *end = p;
Ron Eldor24eec792019-04-04 15:02:01 +0300697 char *crt_file, *key_file, *ca_file, *auth_str;
698#if defined(MBEDTLS_X509_CRL_PARSE_C)
699 char *crl_file;
700#endif
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000701
702 while( *end != '\0' )
703 ++end;
704 *end = ',';
705
706 while( p <= end )
707 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200708 if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000709 {
710 sni_free( cur );
711 return( NULL );
712 }
713
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200714 GET_ITEM( new->name );
715 GET_ITEM( crt_file );
716 GET_ITEM( key_file );
717 GET_ITEM( ca_file );
Ron Eldor24eec792019-04-04 15:02:01 +0300718#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200719 GET_ITEM( crl_file );
Ron Eldor24eec792019-04-04 15:02:01 +0300720#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200721 GET_ITEM( auth_str );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000722
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200723 if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
724 ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200725 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_x509_crt_init( new->cert );
728 mbedtls_pk_init( new->key );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
731 mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000732 goto error;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200733
734 if( strcmp( ca_file, "-" ) != 0 )
735 {
736 if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
737 goto error;
738
739 mbedtls_x509_crt_init( new->ca );
740
741 if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
742 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000743 }
744
Ron Eldor24eec792019-04-04 15:02:01 +0300745#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200746 if( strcmp( crl_file, "-" ) != 0 )
747 {
748 if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
749 goto error;
750
751 mbedtls_x509_crl_init( new->crl );
752
753 if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
754 goto error;
755 }
Ron Eldor24eec792019-04-04 15:02:01 +0300756#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200757
758 if( strcmp( auth_str, "-" ) != 0 )
759 {
760 if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
761 goto error;
762 }
763 else
764 new->authmode = DFL_AUTH_MODE;
765
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000766 new->next = cur;
767 cur = new;
768 }
769
770 return( cur );
771
772error:
773 sni_free( new );
774 sni_free( cur );
775 return( NULL );
776}
777
778/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100779 * SNI callback.
780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100782 const unsigned char *name, size_t name_len )
783{
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200784 const sni_entry *cur = (const sni_entry *) p_info;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100785
786 while( cur != NULL )
787 {
788 if( name_len == strlen( cur->name ) &&
789 memcmp( name, cur->name, name_len ) == 0 )
790 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200791 if( cur->ca != NULL )
792 mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
793
794 if( cur->authmode != DFL_AUTH_MODE )
795 mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
796
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200797 return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100798 }
799
800 cur = cur->next;
801 }
802
803 return( -1 );
804}
805
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200806#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100807
Hanno Becker2e0bedc2019-04-30 14:18:06 +0100808#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) || \
Hanno Beckera5a2b082019-05-15 14:03:01 +0100809 defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200810
Hanno Beckerd6028a12018-10-15 12:01:35 +0100811#define HEX2NUM( c ) \
812 do \
813 { \
814 if( (c) >= '0' && (c) <= '9' ) \
815 (c) -= '0'; \
816 else if( (c) >= 'a' && (c) <= 'f' ) \
817 (c) -= 'a' - 10; \
818 else if( (c) >= 'A' && (c) <= 'F' ) \
819 (c) -= 'A' - 10; \
820 else \
821 return( -1 ); \
822 } while( 0 )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200823
824/*
825 * Convert a hex string to bytes.
826 * Return 0 on success, -1 on error.
827 */
828int unhexify( unsigned char *output, const char *input, size_t *olen )
829{
830 unsigned char c;
831 size_t j;
832
833 *olen = strlen( input );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 if( *olen % 2 != 0 || *olen / 2 > MBEDTLS_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200835 return( -1 );
836 *olen /= 2;
837
838 for( j = 0; j < *olen * 2; j += 2 )
839 {
840 c = input[j];
841 HEX2NUM( c );
842 output[ j / 2 ] = c << 4;
843
844 c = input[j + 1];
845 HEX2NUM( c );
846 output[ j / 2 ] |= c;
847 }
848
849 return( 0 );
850}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200851
Hanno Becker2e0bedc2019-04-30 14:18:06 +0100852#endif
853
854#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
855
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200856typedef struct _psk_entry psk_entry;
857
858struct _psk_entry
859{
860 const char *name;
861 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 unsigned char key[MBEDTLS_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200863 psk_entry *next;
864};
865
866/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000867 * Free a list of psk_entry's
868 */
869void psk_free( psk_entry *head )
870{
871 psk_entry *next;
872
873 while( head != NULL )
874 {
875 next = head->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 mbedtls_free( head );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000877 head = next;
878 }
879}
880
881/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200882 * Parse a string of pairs name1,key1[,name2,key2[,...]]
883 * into a usable psk_entry list.
884 *
885 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200886 */
887psk_entry *psk_parse( char *psk_string )
888{
889 psk_entry *cur = NULL, *new = NULL;
890 char *p = psk_string;
891 char *end = p;
892 char *key_hex;
893
894 while( *end != '\0' )
895 ++end;
896 *end = ',';
897
898 while( p <= end )
899 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200900 if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +0000901 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200902
903 memset( new, 0, sizeof( psk_entry ) );
904
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200905 GET_ITEM( new->name );
906 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200907
908 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000909 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200910
911 new->next = cur;
912 cur = new;
913 }
914
915 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200916
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000917error:
918 psk_free( new );
919 psk_free( cur );
920 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200921}
922
923/*
924 * PSK callback
925 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200927 const unsigned char *name, size_t name_len )
928{
929 psk_entry *cur = (psk_entry *) p_info;
930
931 while( cur != NULL )
932 {
933 if( name_len == strlen( cur->name ) &&
934 memcmp( name, cur->name, name_len ) == 0 )
935 {
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +0100936 return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200937 }
938
939 cur = cur->next;
940 }
941
942 return( -1 );
943}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200945
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200946static mbedtls_net_context listen_fd, client_fd;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200947
948/* Interruption handler to ensure clean exit (for valgrind testing) */
949#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200950static int received_sigterm = 0;
951void term_handler( int sig )
952{
953 ((void) sig);
954 received_sigterm = 1;
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200955 mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
956 mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200957}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200958#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200959
Gilles Peskine682df092017-05-11 19:01:11 +0200960#if defined(MBEDTLS_X509_CRT_PARSE_C)
961static int ssl_sig_hashes_for_test[] = {
962#if defined(MBEDTLS_SHA512_C)
963 MBEDTLS_MD_SHA512,
964 MBEDTLS_MD_SHA384,
965#endif
966#if defined(MBEDTLS_SHA256_C)
967 MBEDTLS_MD_SHA256,
968 MBEDTLS_MD_SHA224,
969#endif
970#if defined(MBEDTLS_SHA1_C)
971 /* Allow SHA-1 as we use it extensively in tests. */
972 MBEDTLS_MD_SHA1,
973#endif
974 MBEDTLS_MD_NONE
975};
976#endif /* MBEDTLS_X509_CRT_PARSE_C */
977
Gilles Peskinea36ac4f2018-04-26 08:05:02 +0200978/** Return true if \p ret is a status code indicating that there is an
979 * operation in progress on an SSL connection, and false if it indicates
980 * success or a fatal error.
981 *
982 * The possible operations in progress are:
983 *
984 * - A read, when the SSL input buffer does not contain a full message.
985 * - A write, when the SSL output buffer contains some data that has not
986 * been sent over the network yet.
987 * - An asynchronous callback that has not completed yet. */
988static int mbedtls_status_is_ssl_in_progress( int ret )
989{
990 return( ret == MBEDTLS_ERR_SSL_WANT_READ ||
991 ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
992 ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
993}
994
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200995#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100996typedef struct
997{
Gilles Peskine44817442018-06-13 18:09:28 +0200998 mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
999 mbedtls_pk_context *pk; /*!< Private key */
1000 unsigned delay; /*!< Number of resume steps to go through */
1001 unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001002} ssl_async_key_slot_t;
1003
1004typedef enum {
Gilles Peskinead28bf02018-04-26 00:19:16 +02001005 SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
1006 SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
1007 SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start */
1008 SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
Gilles Peskinec9125722018-04-26 07:15:40 +02001009#define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001010} ssl_async_inject_error_t;
1011
1012typedef struct
1013{
Gilles Peskinee2479892018-06-13 18:06:51 +02001014 ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001015 size_t slots_used;
1016 ssl_async_inject_error_t inject_error;
1017 int (*f_rng)(void *, unsigned char *, size_t);
1018 void *p_rng;
1019} ssl_async_key_context_t;
1020
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001021int ssl_async_set_key( ssl_async_key_context_t *ctx,
Gilles Peskine44817442018-06-13 18:09:28 +02001022 mbedtls_x509_crt *cert,
1023 mbedtls_pk_context *pk,
1024 int pk_take_ownership,
1025 unsigned delay )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001026{
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001027 if( ctx->slots_used >= sizeof( ctx->slots ) / sizeof( *ctx->slots ) )
1028 return( -1 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001029 ctx->slots[ctx->slots_used].cert = cert;
1030 ctx->slots[ctx->slots_used].pk = pk;
1031 ctx->slots[ctx->slots_used].delay = delay;
Gilles Peskine44817442018-06-13 18:09:28 +02001032 ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001033 ++ctx->slots_used;
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001034 return( 0 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001035}
1036
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001037#define SSL_ASYNC_INPUT_MAX_SIZE 512
Gilles Peskined3268832018-04-26 06:23:59 +02001038
1039typedef enum
1040{
1041 ASYNC_OP_SIGN,
1042 ASYNC_OP_DECRYPT,
1043} ssl_async_operation_type_t;
1044/* Note that the enum above and the array below need to be kept in sync!
1045 * `ssl_async_operation_names[op]` is the name of op for each value `op`
1046 * of type `ssl_async_operation_type_t`. */
1047static const char *const ssl_async_operation_names[] =
1048{
1049 "sign",
1050 "decrypt",
1051};
1052
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001053typedef struct
1054{
Gilles Peskine6331d782018-04-26 13:27:43 +02001055 unsigned slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001056 ssl_async_operation_type_t operation_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001057 mbedtls_md_type_t md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001058 unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
1059 size_t input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001060 unsigned remaining_delay;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001061} ssl_async_operation_context_t;
1062
Gilles Peskine8f97af72018-04-26 11:46:10 +02001063static int ssl_async_start( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001064 mbedtls_x509_crt *cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001065 ssl_async_operation_type_t op_type,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001066 mbedtls_md_type_t md_alg,
1067 const unsigned char *input,
1068 size_t input_len )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001069{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001070 ssl_async_key_context_t *config_data =
1071 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskine6331d782018-04-26 13:27:43 +02001072 unsigned slot;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001073 ssl_async_operation_context_t *ctx = NULL;
Gilles Peskined3268832018-04-26 06:23:59 +02001074 const char *op_name = ssl_async_operation_names[op_type];
Gilles Peskinef1127252018-04-24 13:05:39 +02001075
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001076 {
1077 char dn[100];
Gilles Peskined5d983e2018-06-15 14:05:10 +02001078 if( mbedtls_x509_dn_gets( dn, sizeof( dn ), &cert->subject ) > 0 )
1079 mbedtls_printf( "Async %s callback: looking for DN=%s\n",
1080 op_name, dn );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001081 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001082
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001083 /* Look for a private key that matches the public key in cert.
1084 * Since this test code has the private key inside Mbed TLS,
1085 * we call mbedtls_pk_check_pair to match a private key with the
1086 * public key. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001087 for( slot = 0; slot < config_data->slots_used; slot++ )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001088 {
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001089 if( mbedtls_pk_check_pair( &cert->pk,
1090 config_data->slots[slot].pk ) == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001091 break;
1092 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001093 if( slot == config_data->slots_used )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001094 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001095 mbedtls_printf( "Async %s callback: no key matches this certificate.\n",
1096 op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001097 return( MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH );
1098 }
Gilles Peskine6331d782018-04-26 13:27:43 +02001099 mbedtls_printf( "Async %s callback: using key slot %u, delay=%u.\n",
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001100 op_name, slot, config_data->slots[slot].delay );
Gilles Peskinef1127252018-04-24 13:05:39 +02001101
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001102 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001103 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001104 mbedtls_printf( "Async %s callback: injected error\n", op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001105 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1106 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001107
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001108 if( input_len > SSL_ASYNC_INPUT_MAX_SIZE )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001109 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Gilles Peskinef1127252018-04-24 13:05:39 +02001110
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001111 ctx = mbedtls_calloc( 1, sizeof( *ctx ) );
1112 if( ctx == NULL )
1113 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1114 ctx->slot = slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001115 ctx->operation_type = op_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001116 ctx->md_alg = md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001117 memcpy( ctx->input, input, input_len );
1118 ctx->input_len = input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001119 ctx->remaining_delay = config_data->slots[slot].delay;
Gilles Peskinea668c602018-04-30 11:54:39 +02001120 mbedtls_ssl_set_async_operation_data( ssl, ctx );
Gilles Peskinef1127252018-04-24 13:05:39 +02001121
Gilles Peskineceb541b2018-04-26 06:30:45 +02001122 if( ctx->remaining_delay == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001123 return( 0 );
1124 else
1125 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1126}
1127
Gilles Peskine8f97af72018-04-26 11:46:10 +02001128static int ssl_async_sign( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001129 mbedtls_x509_crt *cert,
1130 mbedtls_md_type_t md_alg,
1131 const unsigned char *hash,
1132 size_t hash_len )
1133{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001134 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001135 ASYNC_OP_SIGN, md_alg,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001136 hash, hash_len ) );
1137}
1138
Gilles Peskine8f97af72018-04-26 11:46:10 +02001139static int ssl_async_decrypt( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001140 mbedtls_x509_crt *cert,
1141 const unsigned char *input,
1142 size_t input_len )
1143{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001144 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001145 ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001146 input, input_len ) );
1147}
1148
Gilles Peskine8f97af72018-04-26 11:46:10 +02001149static int ssl_async_resume( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001150 unsigned char *output,
1151 size_t *output_len,
1152 size_t output_size )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001153{
Gilles Peskinea668c602018-04-30 11:54:39 +02001154 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine8f97af72018-04-26 11:46:10 +02001155 ssl_async_key_context_t *config_data =
1156 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001157 ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001158 int ret;
Gilles Peskinef5a99962018-04-30 16:37:23 +02001159 const char *op_name;
Gilles Peskinef1127252018-04-24 13:05:39 +02001160
Gilles Peskineceb541b2018-04-26 06:30:45 +02001161 if( ctx->remaining_delay > 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001162 {
Gilles Peskineceb541b2018-04-26 06:30:45 +02001163 --ctx->remaining_delay;
Gilles Peskine6331d782018-04-26 13:27:43 +02001164 mbedtls_printf( "Async resume (slot %u): call %u more times.\n",
Gilles Peskineceb541b2018-04-26 06:30:45 +02001165 ctx->slot, ctx->remaining_delay );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001166 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1167 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001168
Gilles Peskined3268832018-04-26 06:23:59 +02001169 switch( ctx->operation_type )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001170 {
Gilles Peskined3268832018-04-26 06:23:59 +02001171 case ASYNC_OP_DECRYPT:
Gilles Peskined3268832018-04-26 06:23:59 +02001172 ret = mbedtls_pk_decrypt( key_slot->pk,
1173 ctx->input, ctx->input_len,
1174 output, output_len, output_size,
1175 config_data->f_rng, config_data->p_rng );
1176 break;
1177 case ASYNC_OP_SIGN:
Gilles Peskined3268832018-04-26 06:23:59 +02001178 ret = mbedtls_pk_sign( key_slot->pk,
1179 ctx->md_alg,
1180 ctx->input, ctx->input_len,
1181 output, output_len,
1182 config_data->f_rng, config_data->p_rng );
1183 break;
1184 default:
Gilles Peskine6331d782018-04-26 13:27:43 +02001185 mbedtls_printf( "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
Gilles Peskined3268832018-04-26 06:23:59 +02001186 ctx->slot, (long) ctx->operation_type );
Gilles Peskine44817442018-06-13 18:09:28 +02001187 mbedtls_free( ctx );
Gilles Peskined3268832018-04-26 06:23:59 +02001188 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1189 break;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001190 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001191
Gilles Peskinef5a99962018-04-30 16:37:23 +02001192 op_name = ssl_async_operation_names[ctx->operation_type];
1193
Gilles Peskinec9125722018-04-26 07:15:40 +02001194 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001195 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001196 mbedtls_printf( "Async resume callback: %s done but injected error\n",
1197 op_name );
Gilles Peskine2636fad2018-06-12 14:17:39 +02001198 mbedtls_free( ctx );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001199 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1200 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001201
Gilles Peskine6331d782018-04-26 13:27:43 +02001202 mbedtls_printf( "Async resume (slot %u): %s done, status=%d.\n",
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001203 ctx->slot, op_name, ret );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001204 mbedtls_free( ctx );
1205 return( ret );
1206}
1207
Gilles Peskine8f97af72018-04-26 11:46:10 +02001208static void ssl_async_cancel( mbedtls_ssl_context *ssl )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001209{
Gilles Peskinea668c602018-04-30 11:54:39 +02001210 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001211 mbedtls_printf( "Async cancel callback.\n" );
1212 mbedtls_free( ctx );
1213}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001214#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001215
Hanno Becker16970d22017-10-10 15:56:37 +01001216/*
1217 * Wait for an event from the underlying transport or the timer
1218 * (Used in event-driven IO mode).
1219 */
1220#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001221int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001222 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001223#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001224int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001225 mbedtls_timing_delay_context *timer,
1226 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001227#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001228{
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001229 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +01001230 int poll_type = 0;
1231
1232 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
1233 poll_type = MBEDTLS_NET_POLL_WRITE;
1234 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
1235 poll_type = MBEDTLS_NET_POLL_READ;
1236#if !defined(MBEDTLS_TIMING_C)
1237 else
Hanno Beckeref527962018-03-15 15:49:24 +00001238 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001239#endif
1240
1241 while( 1 )
1242 {
Hanno Becker197a91c2017-10-31 10:58:53 +00001243 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +01001244#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001245 if( timer != NULL &&
1246 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +01001247 {
Hanno Becker16970d22017-10-10 15:56:37 +01001248 break;
1249 }
Hanno Becker197a91c2017-10-31 10:58:53 +00001250#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +01001251
Hanno Becker197a91c2017-10-31 10:58:53 +00001252 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001253 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +01001254 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001255 ret = mbedtls_net_poll( fd, poll_type, 0 );
1256 if( ret < 0 )
1257 return( ret );
1258 if( ret == poll_type )
1259 break;
Hanno Becker16970d22017-10-10 15:56:37 +01001260 }
1261 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001262
1263 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001264}
1265
Hanno Beckera5a2b082019-05-15 14:03:01 +01001266#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01001267int report_cid_usage( mbedtls_ssl_context *ssl,
1268 const char *additional_description )
1269{
1270 int ret;
1271 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
1272 size_t peer_cid_len;
1273 int cid_negotiated;
1274
1275 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1276 return( 0 );
1277
1278 /* Check if the use of a CID has been negotiated */
1279 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
1280 peer_cid, &peer_cid_len );
1281 if( ret != 0 )
1282 {
1283 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
1284 -ret );
1285 return( ret );
1286 }
1287
1288 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
1289 {
1290 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
1291 {
1292 mbedtls_printf( "(%s) Use of Connection ID was not offered by client.\n",
1293 additional_description );
1294 }
1295 }
1296 else
1297 {
1298 size_t idx=0;
1299 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
1300 additional_description );
1301 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
1302 additional_description,
1303 (unsigned) peer_cid_len );
1304 while( idx < peer_cid_len )
1305 {
1306 mbedtls_printf( "%02x ", peer_cid[ idx ] );
1307 idx++;
1308 }
1309 mbedtls_printf( "\n" );
1310 }
1311
1312 return( 0 );
1313}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001314#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker96870292019-05-03 17:30:59 +01001315
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001316int main( int argc, char *argv[] )
1317{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001318 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001319 int version_suites[4][2];
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001320 unsigned char* buf = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1322 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +02001323 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +02001324 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +02001325#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001326 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001327 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02001328 size_t cliip_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#if defined(MBEDTLS_SSL_COOKIE_C)
1330 mbedtls_ssl_cookie_ctx cookie_ctx;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001331#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001332
Gilles Peskineef86ab22017-05-05 18:59:02 +02001333#if defined(MBEDTLS_X509_CRT_PARSE_C)
1334 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
1335#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 mbedtls_entropy_context entropy;
1337 mbedtls_ctr_drbg_context ctr_drbg;
1338 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001339 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001340#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001341 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001342#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001344 unsigned char renego_period[8] = { 0 };
1345#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +02001347 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 mbedtls_x509_crt cacert;
1349 mbedtls_x509_crt srvcert;
1350 mbedtls_pk_context pkey;
1351 mbedtls_x509_crt srvcert2;
1352 mbedtls_pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001353 int key_cert_init = 0, key_cert_init2 = 0;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001354#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001355 ssl_async_key_context_t ssl_async_keys;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001356#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001357#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1359 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001360#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#if defined(MBEDTLS_SSL_CACHE_C)
1362 mbedtls_ssl_cache_context cache;
Paul Bakker0a597072012-09-25 21:55:46 +00001363#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001364#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1365 mbedtls_ssl_ticket_context ticket_ctx;
1366#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001367#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001368 sni_entry *sni_info = NULL;
1369#endif
Hanno Beckere6706e62017-05-15 16:05:15 +01001370#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +01001371 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +01001372 const mbedtls_ecp_curve_info * curve_cur;
1373#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +01001375 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001376#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Simon Butchercce68be2018-07-23 14:26:09 +01001378 unsigned char alloc_buf[MEMORY_HEAP_SIZE];
Paul Bakker82024bf2013-07-04 11:52:32 +02001379#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001380
Hanno Beckera5a2b082019-05-15 14:03:01 +01001381#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1029ace2019-04-09 17:28:10 +01001382 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker96870292019-05-03 17:30:59 +01001383 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker1029ace2019-04-09 17:28:10 +01001384 size_t cid_len = 0;
Hanno Becker96870292019-05-03 17:30:59 +01001385 size_t cid_renego_len = 0;
Hanno Becker1029ace2019-04-09 17:28:10 +01001386#endif
1387
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001388 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001389 char *p, *q;
1390 const int *list;
1391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1393 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker82024bf2013-07-04 11:52:32 +02001394#endif
1395
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001396 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +02001397 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001398 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001399 mbedtls_net_init( &client_fd );
1400 mbedtls_net_init( &listen_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001401 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001402 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001403 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404#if defined(MBEDTLS_X509_CRT_PARSE_C)
1405 mbedtls_x509_crt_init( &cacert );
1406 mbedtls_x509_crt_init( &srvcert );
1407 mbedtls_pk_init( &pkey );
1408 mbedtls_x509_crt_init( &srvcert2 );
1409 mbedtls_pk_init( &pkey2 );
Gilles Peskine4d9ec4d2018-04-26 14:33:43 +02001410#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
1411 memset( &ssl_async_keys, 0, sizeof( ssl_async_keys ) );
1412#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001413#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1415 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001416#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#if defined(MBEDTLS_SSL_CACHE_C)
1418 mbedtls_ssl_cache_init( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00001419#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001420#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1421 mbedtls_ssl_ticket_init( &ticket_ctx );
1422#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +02001424 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001425#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#if defined(MBEDTLS_SSL_COOKIE_C)
1427 mbedtls_ssl_cookie_init( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001428#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001429
Paul Bakkerc1283d32014-08-18 11:05:51 +02001430#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001431 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001432 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001433 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +02001434#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001435
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001436 if( argc == 0 )
1437 {
1438 usage:
1439 if( ret == 0 )
1440 ret = 1;
1441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 mbedtls_printf( USAGE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 list = mbedtls_ssl_list_ciphersuites();
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001445 while( *list )
1446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001448 list++;
1449 if( !*list )
1450 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001452 list++;
1453 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 mbedtls_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001455 goto exit;
1456 }
1457
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001458 opt.buffer_size = DFL_IO_BUF_LEN;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001459 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001460 opt.server_port = DFL_SERVER_PORT;
1461 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker16970d22017-10-10 15:56:37 +01001462 opt.event = DFL_EVENT;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001463 opt.response_size = DFL_RESPONSE_SIZE;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001464 opt.nbio = DFL_NBIO;
Hanno Becker1029ace2019-04-09 17:28:10 +01001465 opt.cid_enabled = DFL_CID_ENABLED;
Hanno Becker96870292019-05-03 17:30:59 +01001466 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
Hanno Becker1029ace2019-04-09 17:28:10 +01001467 opt.cid_val = DFL_CID_VALUE;
Hanno Becker96870292019-05-03 17:30:59 +01001468 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001469 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001470 opt.ca_file = DFL_CA_FILE;
1471 opt.ca_path = DFL_CA_PATH;
1472 opt.crt_file = DFL_CRT_FILE;
1473 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001474 opt.crt_file2 = DFL_CRT_FILE2;
1475 opt.key_file2 = DFL_KEY_FILE2;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001476 opt.async_operations = DFL_ASYNC_OPERATIONS;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001477 opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
1478 opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
1479 opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
Paul Bakkerfbb17802013-04-17 19:10:21 +02001480 opt.psk = DFL_PSK;
1481 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001482 opt.psk_list = DFL_PSK_LIST;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001483 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001484 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001485 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001486 opt.renegotiation = DFL_RENEGOTIATION;
1487 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001488 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001489 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001490 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001491 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001492 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001493 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001494 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001495 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001496 opt.auth_mode = DFL_AUTH_MODE;
Janos Follath4817e272017-04-10 13:44:33 +01001497 opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001498 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001499 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001500 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001501 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001502 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001503 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001504 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001505 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001506 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001507 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001508 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001509 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001510 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001511 opt.hs_to_min = DFL_HS_TO_MIN;
1512 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001513 opt.dtls_mtu = DFL_DTLS_MTU;
Hanno Beckere7675d02018-08-14 13:28:56 +01001514 opt.dgram_packing = DFL_DGRAM_PACKING;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001515 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001516 opt.extended_ms = DFL_EXTENDED_MS;
Jarno Lamsa41b35912019-06-10 15:51:11 +03001517 opt.enforce_extended_master_secret = DFL_EXTENDED_MS_ENFORCE;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001518 opt.etm = DFL_ETM;
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03001519 opt.serialize = DFL_SERIALIZE;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001520
1521 for( i = 1; i < argc; i++ )
1522 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001523 p = argv[i];
1524 if( ( q = strchr( p, '=' ) ) == NULL )
1525 goto usage;
1526 *q++ = '\0';
1527
1528 if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001529 opt.server_port = q;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001530 else if( strcmp( p, "server_addr" ) == 0 )
1531 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001532 else if( strcmp( p, "dtls" ) == 0 )
1533 {
1534 int t = atoi( q );
1535 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001537 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001539 else
1540 goto usage;
1541 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001542 else if( strcmp( p, "debug_level" ) == 0 )
1543 {
1544 opt.debug_level = atoi( q );
1545 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1546 goto usage;
1547 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001548 else if( strcmp( p, "nbio" ) == 0 )
1549 {
1550 opt.nbio = atoi( q );
1551 if( opt.nbio < 0 || opt.nbio > 2 )
1552 goto usage;
1553 }
Hanno Becker16970d22017-10-10 15:56:37 +01001554 else if( strcmp( p, "event" ) == 0 )
1555 {
1556 opt.event = atoi( q );
1557 if( opt.event < 0 || opt.event > 2 )
1558 goto usage;
1559 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001560 else if( strcmp( p, "read_timeout" ) == 0 )
1561 opt.read_timeout = atoi( q );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001562 else if( strcmp( p, "buffer_size" ) == 0 )
1563 {
1564 opt.buffer_size = atoi( q );
1565 if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
1566 goto usage;
1567 }
1568 else if( strcmp( p, "response_size" ) == 0 )
1569 {
1570 opt.response_size = atoi( q );
1571 if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
1572 goto usage;
1573 if( opt.buffer_size < opt.response_size )
1574 opt.buffer_size = opt.response_size;
1575 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001576 else if( strcmp( p, "ca_file" ) == 0 )
1577 opt.ca_file = q;
1578 else if( strcmp( p, "ca_path" ) == 0 )
1579 opt.ca_path = q;
1580 else if( strcmp( p, "crt_file" ) == 0 )
1581 opt.crt_file = q;
1582 else if( strcmp( p, "key_file" ) == 0 )
1583 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001584 else if( strcmp( p, "crt_file2" ) == 0 )
1585 opt.crt_file2 = q;
1586 else if( strcmp( p, "key_file2" ) == 0 )
1587 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001588 else if( strcmp( p, "dhm_file" ) == 0 )
1589 opt.dhm_file = q;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001590#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001591 else if( strcmp( p, "async_operations" ) == 0 )
1592 opt.async_operations = q;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001593 else if( strcmp( p, "async_private_delay1" ) == 0 )
1594 opt.async_private_delay1 = atoi( q );
1595 else if( strcmp( p, "async_private_delay2" ) == 0 )
1596 opt.async_private_delay2 = atoi( q );
1597 else if( strcmp( p, "async_private_error" ) == 0 )
1598 {
1599 int n = atoi( q );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01001600 if( n < -SSL_ASYNC_INJECT_ERROR_MAX ||
1601 n > SSL_ASYNC_INJECT_ERROR_MAX )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001602 {
1603 ret = 2;
1604 goto usage;
1605 }
1606 opt.async_private_error = n;
1607 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001608#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Hanno Beckera5a2b082019-05-15 14:03:01 +01001609#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1029ace2019-04-09 17:28:10 +01001610 else if( strcmp( p, "cid" ) == 0 )
1611 {
1612 opt.cid_enabled = atoi( q );
1613 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
1614 goto usage;
1615 }
Hanno Becker96870292019-05-03 17:30:59 +01001616 else if( strcmp( p, "cid_renego" ) == 0 )
1617 {
1618 opt.cid_enabled_renego = atoi( q );
1619 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
1620 goto usage;
1621 }
Hanno Becker1029ace2019-04-09 17:28:10 +01001622 else if( strcmp( p, "cid_val" ) == 0 )
1623 {
1624 opt.cid_val = q;
1625 }
Hanno Becker96870292019-05-03 17:30:59 +01001626 else if( strcmp( p, "cid_val_renego" ) == 0 )
1627 {
1628 opt.cid_val_renego = q;
1629 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001630#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001631 else if( strcmp( p, "psk" ) == 0 )
1632 opt.psk = q;
1633 else if( strcmp( p, "psk_identity" ) == 0 )
1634 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001635 else if( strcmp( p, "psk_list" ) == 0 )
1636 opt.psk_list = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001637 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1638 opt.ecjpake_pw = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001639 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001642
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001643 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001644 {
1645 ret = 2;
1646 goto usage;
1647 }
1648 opt.force_ciphersuite[1] = 0;
1649 }
Hanno Beckere6706e62017-05-15 16:05:15 +01001650 else if( strcmp( p, "curves" ) == 0 )
1651 opt.curves = q;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001652 else if( strcmp( p, "version_suites" ) == 0 )
1653 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001654 else if( strcmp( p, "renegotiation" ) == 0 )
1655 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001656 opt.renegotiation = (atoi( q )) ?
1657 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1658 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001659 }
1660 else if( strcmp( p, "allow_legacy" ) == 0 )
1661 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001662 switch( atoi( q ) )
1663 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001664 case -1:
1665 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1666 break;
1667 case 0:
1668 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1669 break;
1670 case 1:
1671 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1672 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001673 default: goto usage;
1674 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001675 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001676 else if( strcmp( p, "renegotiate" ) == 0 )
1677 {
1678 opt.renegotiate = atoi( q );
1679 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1680 goto usage;
1681 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001682 else if( strcmp( p, "renego_delay" ) == 0 )
1683 {
1684 opt.renego_delay = atoi( q );
1685 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001686 else if( strcmp( p, "renego_period" ) == 0 )
1687 {
Andres AG0b736db2017-02-08 14:05:57 +00001688#if defined(_MSC_VER)
1689 opt.renego_period = _strtoui64( q, NULL, 10 );
1690#else
1691 if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
1692 goto usage;
1693#endif /* _MSC_VER */
1694 if( opt.renego_period < 2 )
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001695 goto usage;
1696 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001697 else if( strcmp( p, "exchanges" ) == 0 )
1698 {
1699 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001700 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001701 goto usage;
1702 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001703 else if( strcmp( p, "min_version" ) == 0 )
1704 {
1705 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001707 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001709 else if( strcmp( q, "tls1_1" ) == 0 ||
1710 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001712 else if( strcmp( q, "tls1_2" ) == 0 ||
1713 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001715 else
1716 goto usage;
1717 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001718 else if( strcmp( p, "max_version" ) == 0 )
1719 {
1720 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001722 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001724 else if( strcmp( q, "tls1_1" ) == 0 ||
1725 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001727 else if( strcmp( q, "tls1_2" ) == 0 ||
1728 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001730 else
1731 goto usage;
1732 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001733 else if( strcmp( p, "arc4" ) == 0 )
1734 {
1735 switch( atoi( q ) )
1736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1738 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001739 default: goto usage;
1740 }
1741 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02001742 else if( strcmp( p, "allow_sha1" ) == 0 )
1743 {
1744 switch( atoi( q ) )
1745 {
1746 case 0: opt.allow_sha1 = 0; break;
1747 case 1: opt.allow_sha1 = 1; break;
1748 default: goto usage;
1749 }
1750 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001751 else if( strcmp( p, "force_version" ) == 0 )
1752 {
1753 if( strcmp( q, "ssl3" ) == 0 )
1754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1756 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001757 }
1758 else if( strcmp( q, "tls1" ) == 0 )
1759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1761 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001762 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001763 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1766 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001767 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001768 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1771 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001772 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001773 else if( strcmp( q, "dtls1" ) == 0 )
1774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1776 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1777 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001778 }
1779 else if( strcmp( q, "dtls1_2" ) == 0 )
1780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1782 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1783 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001784 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001785 else
1786 goto usage;
1787 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001788 else if( strcmp( p, "auth_mode" ) == 0 )
1789 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001790 if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
Paul Bakker91ebfb52012-11-23 14:04:08 +01001791 goto usage;
1792 }
Janos Follath4817e272017-04-10 13:44:33 +01001793 else if( strcmp( p, "cert_req_ca_list" ) == 0 )
1794 {
1795 opt.cert_req_ca_list = atoi( q );
1796 if( opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1 )
1797 goto usage;
1798 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001799 else if( strcmp( p, "max_frag_len" ) == 0 )
1800 {
1801 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001803 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001805 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001807 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001809 else
1810 goto usage;
1811 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001812 else if( strcmp( p, "alpn" ) == 0 )
1813 {
1814 opt.alpn_string = q;
1815 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001816 else if( strcmp( p, "trunc_hmac" ) == 0 )
1817 {
1818 switch( atoi( q ) )
1819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1821 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001822 default: goto usage;
1823 }
1824 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001825 else if( strcmp( p, "extended_ms" ) == 0 )
1826 {
1827 switch( atoi( q ) )
1828 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001829 case 0:
1830 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1831 break;
1832 case 1:
1833 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1834 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001835 default: goto usage;
1836 }
1837 }
Jarno Lamsa41b35912019-06-10 15:51:11 +03001838 else if( strcmp( p, "enforce_extended_master_secret" ) == 0 )
1839 {
1840 switch( atoi( q ) )
1841 {
1842 case 0:
1843 opt.enforce_extended_master_secret =
1844 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
1845 break;
1846 case 1:
1847 opt.enforce_extended_master_secret =
1848 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_ENABLED;
1849 break;
1850 default: goto usage;
1851 }
1852 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001853 else if( strcmp( p, "etm" ) == 0 )
1854 {
1855 switch( atoi( q ) )
1856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1858 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001859 default: goto usage;
1860 }
1861 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001862 else if( strcmp( p, "tickets" ) == 0 )
1863 {
1864 opt.tickets = atoi( q );
1865 if( opt.tickets < 0 || opt.tickets > 1 )
1866 goto usage;
1867 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001868 else if( strcmp( p, "ticket_timeout" ) == 0 )
1869 {
1870 opt.ticket_timeout = atoi( q );
1871 if( opt.ticket_timeout < 0 )
1872 goto usage;
1873 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001874 else if( strcmp( p, "cache_max" ) == 0 )
1875 {
1876 opt.cache_max = atoi( q );
1877 if( opt.cache_max < 0 )
1878 goto usage;
1879 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001880 else if( strcmp( p, "cache_timeout" ) == 0 )
1881 {
1882 opt.cache_timeout = atoi( q );
1883 if( opt.cache_timeout < 0 )
1884 goto usage;
1885 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001886 else if( strcmp( p, "cookies" ) == 0 )
1887 {
1888 opt.cookies = atoi( q );
1889 if( opt.cookies < -1 || opt.cookies > 1)
1890 goto usage;
1891 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001892 else if( strcmp( p, "anti_replay" ) == 0 )
1893 {
1894 opt.anti_replay = atoi( q );
1895 if( opt.anti_replay < 0 || opt.anti_replay > 1)
1896 goto usage;
1897 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001898 else if( strcmp( p, "badmac_limit" ) == 0 )
1899 {
1900 opt.badmac_limit = atoi( q );
1901 if( opt.badmac_limit < 0 )
1902 goto usage;
1903 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001904 else if( strcmp( p, "hs_timeout" ) == 0 )
1905 {
1906 if( ( p = strchr( q, '-' ) ) == NULL )
1907 goto usage;
1908 *p++ = '\0';
1909 opt.hs_to_min = atoi( q );
1910 opt.hs_to_max = atoi( p );
1911 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1912 goto usage;
1913 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001914 else if( strcmp( p, "mtu" ) == 0 )
1915 {
1916 opt.dtls_mtu = atoi( q );
1917 if( opt.dtls_mtu < 0 )
1918 goto usage;
1919 }
Hanno Beckere7675d02018-08-14 13:28:56 +01001920 else if( strcmp( p, "dgram_packing" ) == 0 )
1921 {
1922 opt.dgram_packing = atoi( q );
1923 if( opt.dgram_packing != 0 &&
1924 opt.dgram_packing != 1 )
1925 {
1926 goto usage;
1927 }
1928 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001929 else if( strcmp( p, "sni" ) == 0 )
1930 {
1931 opt.sni = q;
1932 }
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +01001933 else if( strcmp( p, "query_config" ) == 0 )
1934 {
1935 return query_config( q );
1936 }
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03001937 else if( strcmp( p, "serialize") == 0 )
1938 {
1939 opt.serialize = atoi( q );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03001940 if( opt.serialize < 0 || opt.serialize > 2)
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03001941 goto usage;
1942 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001943 else
1944 goto usage;
1945 }
1946
Hanno Becker16970d22017-10-10 15:56:37 +01001947 /* Event-driven IO is incompatible with the above custom
1948 * receive and send functions, as the polling builds on
1949 * refers to the underlying net_context. */
1950 if( opt.event == 1 && opt.nbio != 1 )
1951 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001952 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001953 opt.nbio = 1;
1954 }
1955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956#if defined(MBEDTLS_DEBUG_C)
1957 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001958#endif
Andrzej Kurekda4029d2018-06-20 07:07:55 -04001959 buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001960 if( buf == NULL )
1961 {
Andrzej Kurek755890f2018-06-20 08:17:04 -04001962 mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001963 ret = 3;
1964 goto exit;
1965 }
Paul Bakkerc73079a2014-04-25 16:34:30 +02001966
Paul Bakkerc1516be2013-06-29 16:01:32 +02001967 if( opt.force_ciphersuite[0] > 0 )
1968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001970 ciphersuite_info =
1971 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001972
Paul Bakker5b55b792013-07-19 13:43:43 +02001973 if( opt.max_version != -1 &&
1974 ciphersuite_info->min_minor_ver > opt.max_version )
1975 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001976 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker5b55b792013-07-19 13:43:43 +02001977 ret = 2;
1978 goto usage;
1979 }
1980 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001981 ciphersuite_info->max_minor_ver < opt.min_version )
1982 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001983 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001984 ret = 2;
1985 goto usage;
1986 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001987
1988 /* If we select a version that's not supported by
1989 * this suite, then there will be no common ciphersuite... */
1990 if( opt.max_version == -1 ||
1991 opt.max_version > ciphersuite_info->max_minor_ver )
1992 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001993 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001994 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001995 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001996 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001997 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001998 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2000 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
2001 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01002002 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002003
2004 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002010 ret = 2;
2011 goto usage;
2012 }
2013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002015 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002016 }
2017
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002018 if( opt.version_suites != NULL )
2019 {
2020 const char *name[4] = { 0 };
2021
2022 /* Parse 4-element coma-separated list */
2023 for( i = 0, p = (char *) opt.version_suites;
2024 i < 4 && *p != '\0';
2025 i++ )
2026 {
2027 name[i] = p;
2028
2029 /* Terminate the current string and move on to next one */
2030 while( *p != ',' && *p != '\0' )
2031 p++;
2032 if( *p == ',' )
2033 *p++ = '\0';
2034 }
2035
2036 if( i != 4 )
2037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038 mbedtls_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002039 ret = 1;
2040 goto exit;
2041 }
2042
2043 memset( version_suites, 0, sizeof( version_suites ) );
2044
2045 /* Get the suites identifiers from their name */
2046 for( i = 0; i < 4; i++ )
2047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048 version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002049
2050 if( version_suites[i][0] == 0 )
2051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002053 ret = 2;
2054 goto usage;
2055 }
2056 }
2057 }
2058
Hanno Beckera5a2b082019-05-15 14:03:01 +01002059#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01002060 if( unhexify( cid, opt.cid_val, &cid_len ) != 0 )
2061 {
2062 mbedtls_printf( "CID not valid hex\n" );
2063 goto exit;
2064 }
Hanno Becker1029ace2019-04-09 17:28:10 +01002065
Hanno Becker96870292019-05-03 17:30:59 +01002066 /* Keep CID settings for renegotiation unless
2067 * specified otherwise. */
2068 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
2069 opt.cid_enabled_renego = opt.cid_enabled;
2070 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
2071 opt.cid_val_renego = opt.cid_val;
2072
2073 if( unhexify( cid_renego, opt.cid_val_renego, &cid_renego_len ) != 0 )
2074 {
2075 mbedtls_printf( "CID not valid hex\n" );
2076 goto exit;
2077 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002078#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1029ace2019-04-09 17:28:10 +01002079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002081 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002082 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02002083 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002084 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 mbedtls_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002087 goto exit;
2088 }
2089
2090 if( opt.psk_list != NULL )
2091 {
2092 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 mbedtls_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002095 goto exit;
2096 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002097 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002099
Hanno Beckere6706e62017-05-15 16:05:15 +01002100#if defined(MBEDTLS_ECP_C)
2101 if( opt.curves != NULL )
2102 {
2103 p = (char *) opt.curves;
2104 i = 0;
2105
2106 if( strcmp( p, "none" ) == 0 )
2107 {
2108 curve_list[0] = MBEDTLS_ECP_DP_NONE;
2109 }
2110 else if( strcmp( p, "default" ) != 0 )
2111 {
2112 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01002113 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002114 {
2115 q = p;
2116
2117 /* Terminate the current string */
2118 while( *p != ',' && *p != '\0' )
2119 p++;
2120 if( *p == ',' )
2121 *p++ = '\0';
2122
2123 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
2124 {
2125 curve_list[i++] = curve_cur->grp_id;
2126 }
2127 else
2128 {
2129 mbedtls_printf( "unknown curve %s\n", q );
2130 mbedtls_printf( "supported curves: " );
2131 for( curve_cur = mbedtls_ecp_curve_list();
2132 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
2133 curve_cur++ )
2134 {
2135 mbedtls_printf( "%s ", curve_cur->name );
2136 }
2137 mbedtls_printf( "\n" );
2138 goto exit;
2139 }
2140 }
2141
2142 mbedtls_printf("Number of curves: %d\n", i );
2143
Hanno Becker8651a432017-06-09 16:13:22 +01002144 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002145 {
Hanno Becker8651a432017-06-09 16:13:22 +01002146 mbedtls_printf( "curves list too long, maximum %d",
2147 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01002148 goto exit;
2149 }
2150
2151 curve_list[i] = MBEDTLS_ECP_DP_NONE;
2152 }
2153 }
2154#endif /* MBEDTLS_ECP_C */
2155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002157 if( opt.alpn_string != NULL )
2158 {
2159 p = (char *) opt.alpn_string;
2160 i = 0;
2161
2162 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01002163 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002164 {
2165 alpn_list[i++] = p;
2166
2167 /* Terminate the current string and move on to next one */
2168 while( *p != ',' && *p != '\0' )
2169 p++;
2170 if( *p == ',' )
2171 *p++ = '\0';
2172 }
2173 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002175
Paul Bakkerfbb17802013-04-17 19:10:21 +02002176 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002177 * 0. Initialize the RNG and the session data
2178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002180 fflush( stdout );
2181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 mbedtls_entropy_init( &entropy );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002183 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
2184 &entropy, (const unsigned char *) pers,
2185 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002186 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002187 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
2188 -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002189 goto exit;
2190 }
2191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002195 /*
2196 * 1.1. Load the trusted CA
2197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002199 fflush( stdout );
2200
Hanno Becker7ae36e42019-03-05 16:22:07 +00002201 if( strcmp( opt.ca_path, "none" ) == 0 ||
2202 strcmp( opt.ca_file, "none" ) == 0 )
2203 {
2204 ret = 0;
2205 }
2206 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002208 if( strlen( opt.ca_path ) )
Hanno Becker7ae36e42019-03-05 16:22:07 +00002209 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002210 else if( strlen( opt.ca_file ) )
Hanno Becker7ae36e42019-03-05 16:22:07 +00002211 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02002212 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002213#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214#if defined(MBEDTLS_CERTS_C)
Hanno Becker38566cc2019-02-01 08:13:19 +00002215 {
2216#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 ret = mbedtls_x509_crt_parse( &cacert,
2220 (const unsigned char *) mbedtls_test_cas[i],
2221 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002222 if( ret != 0 )
2223 break;
2224 }
Hanno Becker38566cc2019-02-01 08:13:19 +00002225 if( ret == 0 )
2226#endif /* MBEDTLS_PEM_PARSE_C */
2227 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
2228 {
2229 ret = mbedtls_x509_crt_parse_der( &cacert,
2230 (const unsigned char *) mbedtls_test_cas_der[i],
2231 mbedtls_test_cas_der_len[i] );
2232 if( ret != 0 )
2233 break;
2234 }
2235 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002236#else
2237 {
2238 ret = 1;
Hanno Beckerc258c442018-12-05 16:49:42 +00002239 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002240 }
Hanno Becker38566cc2019-02-01 08:13:19 +00002241#endif /* MBEDTLS_CERTS_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002242 if( ret < 0 )
2243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002245 goto exit;
2246 }
2247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002249
2250 /*
2251 * 1.2. Load own certificate and private key
2252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002254 fflush( stdout );
2255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002257 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002258 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002259 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002263 -ret );
2264 goto exit;
2265 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002266 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002267 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002268 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002269 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002273 goto exit;
2274 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002275 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002276 if( key_cert_init == 1 )
2277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002279 goto exit;
2280 }
2281
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002282 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002283 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002284 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002288 -ret );
2289 goto exit;
2290 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002291 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002292 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002293 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002294 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002298 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002299 goto exit;
2300 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002301 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002302 if( key_cert_init2 == 1 )
2303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002305 goto exit;
2306 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002307#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002308 if( key_cert_init == 0 &&
2309 strcmp( opt.crt_file, "none" ) != 0 &&
2310 strcmp( opt.key_file, "none" ) != 0 &&
2311 key_cert_init2 == 0 &&
2312 strcmp( opt.crt_file2, "none" ) != 0 &&
2313 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315#if !defined(MBEDTLS_CERTS_C)
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002316 mbedtls_printf( "Not certificated or key provided, and \nMBEDTLS_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002317 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002318#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319#if defined(MBEDTLS_RSA_C)
2320 if( ( ret = mbedtls_x509_crt_parse( &srvcert,
2321 (const unsigned char *) mbedtls_test_srv_crt_rsa,
2322 mbedtls_test_srv_crt_rsa_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002323 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002324 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
2325 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002326 goto exit;
2327 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 if( ( ret = mbedtls_pk_parse_key( &pkey,
2329 (const unsigned char *) mbedtls_test_srv_key_rsa,
2330 mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002331 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002332 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
2333 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002334 goto exit;
2335 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002336 key_cert_init = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337#endif /* MBEDTLS_RSA_C */
2338#if defined(MBEDTLS_ECDSA_C)
2339 if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
2340 (const unsigned char *) mbedtls_test_srv_crt_ec,
2341 mbedtls_test_srv_crt_ec_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002342 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002343 mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n",
2344 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002345 goto exit;
2346 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 if( ( ret = mbedtls_pk_parse_key( &pkey2,
2348 (const unsigned char *) mbedtls_test_srv_key_ec,
2349 mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002350 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002351 mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n",
2352 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002353 goto exit;
2354 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002355 key_cert_init2 = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356#endif /* MBEDTLS_ECDSA_C */
2357#endif /* MBEDTLS_CERTS_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002358 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 mbedtls_printf( " ok\n" );
2361#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002364 if( opt.dhm_file != NULL )
2365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 mbedtls_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002367 fflush( stdout );
2368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002372 -ret );
2373 goto exit;
2374 }
2375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002377 }
2378#endif
2379
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002380#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002381 if( opt.sni != NULL )
2382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 mbedtls_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002384 fflush( stdout );
2385
2386 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
2387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002389 goto exit;
2390 }
2391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002393 }
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002394#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002395
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002396 /*
2397 * 2. Setup the listening TCP socket
2398 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02002399 mbedtls_printf( " . Bind on %s://%s:%s/ ...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002401 opt.server_addr ? opt.server_addr : "*",
2402 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002403 fflush( stdout );
2404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
2406 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2407 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002410 goto exit;
2411 }
2412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002414
2415 /*
2416 * 3. Setup stuff
2417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002419 fflush( stdout );
2420
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02002421 if( ( ret = mbedtls_ssl_config_defaults( &conf,
2422 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02002423 opt.transport,
2424 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002425 {
2426 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
2427 goto exit;
2428 }
2429
Gilles Peskineef86ab22017-05-05 18:59:02 +02002430#if defined(MBEDTLS_X509_CRT_PARSE_C)
2431 /* The default algorithms profile disables SHA-1, but our tests still
2432 rely on it heavily. Hence we allow it here. A real-world server
2433 should use the default profile unless there is a good reason not to. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02002434 if( opt.allow_sha1 > 0 )
2435 {
2436 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
2437 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
Gilles Peskine682df092017-05-11 19:01:11 +02002438 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
Gilles Peskinebc70a182017-05-09 15:59:24 +02002439 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02002440#endif /* MBEDTLS_X509_CRT_PARSE_C */
2441
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01002442 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002443 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002444
Janos Follath4817e272017-04-10 13:44:33 +01002445 if( opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST )
2446 mbedtls_ssl_conf_cert_req_ca_list( &conf, opt.cert_req_ca_list );
2447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002449 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 +02002450 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002451
Hanno Beckere7675d02018-08-14 13:28:56 +01002452 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01002453 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002457 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002458 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002459 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002460 goto exit;
2461 };
Paul Bakker05decb22013-08-15 13:33:48 +02002462#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002463
Hanno Beckera5a2b082019-05-15 14:03:01 +01002464#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01002465 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckereec2be92019-05-03 13:06:44 +01002466 {
Hanno Becker96870292019-05-03 17:30:59 +01002467 if( opt.cid_enabled == 1 &&
2468 opt.cid_enabled_renego == 1 &&
2469 cid_len != cid_renego_len )
2470 {
2471 mbedtls_printf( "CID length must not change during renegotiation\n" );
2472 goto usage;
2473 }
2474
2475 if( opt.cid_enabled == 1 )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002476 ret = mbedtls_ssl_conf_cid( &conf, cid_len,
2477 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Becker96870292019-05-03 17:30:59 +01002478 else
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002479 ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
2480 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Becker96870292019-05-03 17:30:59 +01002481
Hanno Beckereec2be92019-05-03 13:06:44 +01002482 if( ret != 0 )
2483 {
Hanno Becker76581052019-05-23 16:58:22 +01002484 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
2485 -ret );
Hanno Beckereec2be92019-05-03 13:06:44 +01002486 goto exit;
2487 }
2488 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002489#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckereec2be92019-05-03 13:06:44 +01002490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002492 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002493 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002494#endif
2495
Hanno Beckerf765ce62019-06-21 13:17:14 +01002496#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
2497 !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET) && \
2498 !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002499 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002500 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Jarno Lamsa41b35912019-06-10 15:51:11 +03002501 if( opt.enforce_extended_master_secret != DFL_EXTENDED_MS_ENFORCE )
2502 mbedtls_ssl_conf_extended_master_secret_enforce( &conf,
2503 opt.enforce_extended_master_secret );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002504#endif
2505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002507 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002508 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002509#endif
2510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002512 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002513 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002514 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002515 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002516 goto exit;
2517 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002518#endif
2519
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002520 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
2521 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002524 if( opt.cache_max != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525 mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002526
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002527 if( opt.cache_timeout != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002529
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002530 mbedtls_ssl_conf_session_cache( &conf, &cache,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01002531 mbedtls_ssl_cache_get,
2532 mbedtls_ssl_cache_set );
Paul Bakker0a597072012-09-25 21:55:46 +00002533#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002536 if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002537 {
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002538 if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
2539 mbedtls_ctr_drbg_random, &ctr_drbg,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +02002540 MBEDTLS_CIPHER_AES_256_GCM,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002541 opt.ticket_timeout ) ) != 0 )
2542 {
2543 mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
2544 goto exit;
2545 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01002546
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002547 mbedtls_ssl_conf_session_tickets_cb( &conf,
2548 mbedtls_ssl_ticket_write,
2549 mbedtls_ssl_ticket_parse,
2550 &ticket_ctx );
2551 }
Paul Bakkera503a632013-08-14 13:48:06 +02002552#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554#if defined(MBEDTLS_SSL_PROTO_DTLS)
2555 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002558 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
2561 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002564 goto exit;
2565 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002566
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002567 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002568 &cookie_ctx );
2569 }
2570 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571#endif /* MBEDTLS_SSL_COOKIE_C */
2572#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002573 if( opt.cookies == 0 )
2574 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002575 mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002576 }
2577 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002579 {
2580 ; /* Nothing to do */
2581 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002584 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002585 mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002586#endif
2587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002589 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002590 mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002591#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002592 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002594
Paul Bakker41c83d32013-03-20 14:39:14 +01002595 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002596 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002597
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002598#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002599 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002600 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002601#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002602
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002603 if( opt.version_suites != NULL )
2604 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002605 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606 MBEDTLS_SSL_MAJOR_VERSION_3,
2607 MBEDTLS_SSL_MINOR_VERSION_0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002608 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 MBEDTLS_SSL_MAJOR_VERSION_3,
2610 MBEDTLS_SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002611 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612 MBEDTLS_SSL_MAJOR_VERSION_3,
2613 MBEDTLS_SSL_MINOR_VERSION_2 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002614 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 MBEDTLS_SSL_MAJOR_VERSION_3,
2616 MBEDTLS_SSL_MINOR_VERSION_3 );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002617 }
2618
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002619 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002620 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002622 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002623
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02002624 if( opt.renego_delay != DFL_RENEGO_DELAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002625 mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002626
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002627 if( opt.renego_period != DFL_RENEGO_PERIOD )
2628 {
Andres AG692ad842017-01-19 16:30:57 +00002629 PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002630 mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002631 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002632#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002635 if( strcmp( opt.ca_path, "none" ) != 0 &&
2636 strcmp( opt.ca_file, "none" ) != 0 )
2637 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002638 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002639 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002640 if( key_cert_init )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002641 {
2642 mbedtls_pk_context *pk = &pkey;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002643#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002644 if( opt.async_private_delay1 >= 0 )
2645 {
Gilles Peskine44817442018-06-13 18:09:28 +02002646 ret = ssl_async_set_key( &ssl_async_keys, &srvcert, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002647 opt.async_private_delay1 );
2648 if( ret < 0 )
2649 {
2650 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2651 ret );
2652 goto exit;
2653 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002654 pk = NULL;
2655 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002656#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002657 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002658 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002659 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002660 goto exit;
2661 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002662 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002663 if( key_cert_init2 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002664 {
2665 mbedtls_pk_context *pk = &pkey2;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002666#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002667 if( opt.async_private_delay2 >= 0 )
2668 {
Gilles Peskine44817442018-06-13 18:09:28 +02002669 ret = ssl_async_set_key( &ssl_async_keys, &srvcert2, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002670 opt.async_private_delay2 );
2671 if( ret < 0 )
2672 {
2673 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2674 ret );
2675 goto exit;
2676 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002677 pk = NULL;
2678 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002679#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002680 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002681 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002682 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002683 goto exit;
2684 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002685 }
2686
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002687#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002688 if( opt.async_operations[0] != '-' )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002689 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002690 mbedtls_ssl_async_sign_t *sign = NULL;
2691 mbedtls_ssl_async_decrypt_t *decrypt = NULL;
Gilles Peskine12ab5d42018-04-24 12:32:04 +02002692 const char *r;
2693 for( r = opt.async_operations; *r; r++ )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002694 {
Gilles Peskine12ab5d42018-04-24 12:32:04 +02002695 switch( *r )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002696 {
2697 case 'd':
2698 decrypt = ssl_async_decrypt;
2699 break;
2700 case 's':
2701 sign = ssl_async_sign;
2702 break;
2703 }
2704 }
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002705 ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
2706 - opt.async_private_error :
2707 opt.async_private_error );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002708 ssl_async_keys.f_rng = mbedtls_ctr_drbg_random;
2709 ssl_async_keys.p_rng = &ctr_drbg;
2710 mbedtls_ssl_conf_async_private_cb( &conf,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002711 sign,
2712 decrypt,
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002713 ssl_async_resume,
2714 ssl_async_cancel,
2715 &ssl_async_keys );
2716 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002717#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002718#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +02002719
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002720#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002721 if( opt.sni != NULL )
Gilles Peskine166ce742018-04-30 10:30:49 +02002722 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002723 mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
Gilles Peskine166ce742018-04-30 10:30:49 +02002724#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
2725 if( opt.async_private_delay2 >= 0 )
2726 {
Gilles Peskinee2479892018-06-13 18:06:51 +02002727 sni_entry *cur;
2728 for( cur = sni_info; cur != NULL; cur = cur->next )
Gilles Peskine166ce742018-04-30 10:30:49 +02002729 {
Gilles Peskinee2479892018-06-13 18:06:51 +02002730 ret = ssl_async_set_key( &ssl_async_keys,
Gilles Peskine44817442018-06-13 18:09:28 +02002731 cur->cert, cur->key, 1,
Gilles Peskinee2479892018-06-13 18:06:51 +02002732 opt.async_private_delay2 );
2733 if( ret < 0 )
2734 {
2735 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2736 ret );
2737 goto exit;
2738 }
2739 cur->key = NULL;
Gilles Peskine166ce742018-04-30 10:30:49 +02002740 }
Gilles Peskine166ce742018-04-30 10:30:49 +02002741 }
2742#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
2743 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002744#endif
2745
Hanno Beckere6706e62017-05-15 16:05:15 +01002746#if defined(MBEDTLS_ECP_C)
2747 if( opt.curves != NULL &&
2748 strcmp( opt.curves, "default" ) != 0 )
2749 {
2750 mbedtls_ssl_conf_curves( &conf, curve_list );
2751 }
2752#endif
2753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002755 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
2756 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002757 ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002758 (const unsigned char *) opt.psk_identity,
2759 strlen( opt.psk_identity ) );
2760 if( ret != 0 )
2761 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002762 mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002763 goto exit;
2764 }
2765 }
2766
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002767 if( opt.psk_list != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002768 mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02002769#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771#if defined(MBEDTLS_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00002772 /*
2773 * Use different group than default DHM group
2774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002776 if( opt.dhm_file != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002777 ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002778#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002779 if( ret != 0 )
2780 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002781 mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002782 goto exit;
2783 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002784#endif
2785
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002786 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002787 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00002788
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002789 if( opt.max_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002790 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002791
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002792 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
2793 {
2794 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
2795 goto exit;
2796 }
2797
2798 if( opt.nbio == 2 )
2799 mbedtls_ssl_set_bio( &ssl, &client_fd, my_send, my_recv, NULL );
2800 else
2801 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02002802 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002803
Hanno Beckera5a2b082019-05-15 14:03:01 +01002804#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1029ace2019-04-09 17:28:10 +01002805 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2806 {
2807 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
2808 cid, cid_len ) ) != 0 )
2809 {
2810 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2811 ret );
2812 goto exit;
2813 }
2814 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002815#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1029ace2019-04-09 17:28:10 +01002816
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002817#if defined(MBEDTLS_SSL_PROTO_DTLS)
2818 if( opt.dtls_mtu != DFL_DTLS_MTU )
2819 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
2820#endif
2821
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002822#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002823 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
2824 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002825#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002828
2829reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002830#if !defined(_WIN32)
2831 if( received_sigterm )
2832 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01002833 mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" );
2834 if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT )
2835 ret = 0;
2836
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002837 goto exit;
2838 }
2839#endif
2840
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002841 if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
2842 {
2843 mbedtls_printf( " ! Client initiated reconnection from same port\n" );
2844 goto handshake;
2845 }
2846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002848 if( ret != 0 )
2849 {
2850 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 mbedtls_strerror( ret, error_buf, 100 );
2852 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002853 }
2854#endif
2855
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002856 mbedtls_net_free( &client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 mbedtls_ssl_session_reset( &ssl );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002859
2860 /*
2861 * 3. Wait until a client connects
2862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 mbedtls_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002864 fflush( stdout );
2865
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002866 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02002867 client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002868 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02002869#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002870 if( received_sigterm )
2871 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01002872 mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" );
2873 if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED )
2874 ret = 0;
2875
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002876 goto exit;
2877 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02002878#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880 mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002881 goto exit;
2882 }
2883
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002884 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002885 ret = mbedtls_net_set_nonblock( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002886 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002887 ret = mbedtls_net_set_block( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002888 if( ret != 0 )
2889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002891 goto exit;
2892 }
2893
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002894 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2897 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002898 {
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02002899 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
2900 client_ip, cliip_len ) ) != 0 )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002901 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002902 mbedtls_printf( " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n",
2903 -ret );
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002904 goto exit;
2905 }
2906 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002908
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002909#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2910 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
2911 {
2912 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
2913 (const unsigned char *) opt.ecjpake_pw,
2914 strlen( opt.ecjpake_pw ) ) ) != 0 )
2915 {
2916 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
2917 goto exit;
2918 }
2919 }
2920#endif
2921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002923
2924 /*
2925 * 4. Handshake
2926 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002927handshake:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002929 fflush( stdout );
2930
Hanno Becker16970d22017-10-10 15:56:37 +01002931 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002932 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002933#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002934 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002935 ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002936 {
2937 mbedtls_printf( " cancelling on injected error\n" );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002938 break;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002939 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002940#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskineb44692f2018-04-24 12:18:19 +02002941
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02002942 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01002943 break;
2944
2945 /* For event-driven IO, wait for socket to become available */
2946 if( opt.event == 1 /* level triggered IO */ )
2947 {
2948#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002949 ret = idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002950#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002951 ret = idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002952#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002953 if( ret != 0 )
2954 goto reset;
Hanno Becker16970d22017-10-10 15:56:37 +01002955 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002956 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 mbedtls_printf( " hello verification requested\n" );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002961 ret = 0;
2962 goto reset;
2963 }
2964 else if( ret != 0 )
2965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002967
Hanno Becker02a21932019-06-10 15:08:43 +01002968#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002969 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2970 {
2971 char vrfy_buf[512];
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02002972 flags = mbedtls_ssl_get_verify_result( &ssl );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002973
2974 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
2975
2976 mbedtls_printf( "%s\n", vrfy_buf );
2977 }
2978#endif
2979
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002980#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002981 if( opt.async_private_error < 0 )
2982 /* Injected error only the first time round, to test reset */
2983 ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
2984#endif
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002985 goto reset;
2986 }
2987 else /* ret == 0 */
2988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
2990 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002991 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
2994 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002995 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002997
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002998#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2999 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
3000 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
3001#endif
3002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003004 if( opt.alpn_string != NULL )
3005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
3007 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02003008 alp ? alp : "(none)" );
3009 }
3010#endif
3011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003013 /*
Ron Eldor2a47be52017-06-20 15:23:23 +03003014 * 5. Verify the client certificate
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003017
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003018 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003019 {
Hanno Becker02a21932019-06-10 15:08:43 +01003020#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003021 char vrfy_buf[512];
Peter Kolbusdc470ae2018-12-11 13:55:56 -06003022#endif
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024 mbedtls_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003025
Hanno Becker02a21932019-06-10 15:08:43 +01003026#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003027 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003028
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003029 mbedtls_printf( "%s\n", vrfy_buf );
Peter Kolbusdc470ae2018-12-11 13:55:56 -06003030#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003031 }
3032 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003033 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003034
Hanno Becker02a21932019-06-10 15:08:43 +01003035#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003036 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003037 {
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003038 char crt_buf[512];
3039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040 mbedtls_printf( " . Peer certificate information ...\n" );
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003041 mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042 mbedtls_ssl_get_peer_cert( &ssl ) );
Manuel Pégourié-Gonnard67557172015-07-02 11:15:48 +02003043 mbedtls_printf( "%s\n", crt_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003044 }
Hanno Becker02a21932019-06-10 15:08:43 +01003045#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003047
Hanno Beckera5a2b082019-05-15 14:03:01 +01003048#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01003049 ret = report_cid_usage( &ssl, "initial handshake" );
3050 if( ret != 0 )
3051 goto exit;
3052
Hanno Becker1029ace2019-04-09 17:28:10 +01003053 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3054 {
Hanno Becker96870292019-05-03 17:30:59 +01003055 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
3056 cid_renego, cid_renego_len ) ) != 0 )
Hanno Becker1029ace2019-04-09 17:28:10 +01003057 {
Hanno Becker96870292019-05-03 17:30:59 +01003058 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
3059 ret );
Hanno Becker1029ace2019-04-09 17:28:10 +01003060 goto exit;
3061 }
Hanno Becker1029ace2019-04-09 17:28:10 +01003062 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003063#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1029ace2019-04-09 17:28:10 +01003064
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02003065 if( opt.exchanges == 0 )
3066 goto close_notify;
3067
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003068 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003069data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003070 /*
3071 * 6. Read the HTTP Request
3072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073 mbedtls_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003074 fflush( stdout );
3075
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003076 /*
3077 * TLS and DTLS need different reading styles (stream vs datagram)
3078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003080 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003081 do
3082 {
3083 int terminated = 0;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003084 len = opt.buffer_size - 1;
3085 memset( buf, 0, opt.buffer_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003087
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003088 if( mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003089 {
3090 if( opt.event == 1 /* level triggered IO */ )
3091 {
3092#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003093 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003094#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003095 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003096#endif
3097 }
3098
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003099 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01003100 }
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003101
3102 if( ret <= 0 )
3103 {
3104 switch( ret )
3105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3107 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003108 goto close_notify;
3109
3110 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 case MBEDTLS_ERR_NET_CONN_RESET:
3112 mbedtls_printf( " connection was reset by peer\n" );
3113 ret = MBEDTLS_ERR_NET_CONN_RESET;
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003114 goto reset;
3115
3116 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003118 goto reset;
3119 }
3120 }
3121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122 if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003123 {
3124 len = ret;
3125 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003127
3128 /* End of message should be detected according to the syntax of the
3129 * application protocol (eg HTTP), just use a dummy test here. */
3130 if( buf[len - 1] == '\n' )
3131 terminated = 1;
3132 }
3133 else
3134 {
3135 int extra_len, ori_len;
3136 unsigned char *larger_buf;
3137
3138 ori_len = ret;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02003139 extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003140
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003141 larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003142 if( larger_buf == NULL )
3143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 mbedtls_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003145 ret = 1;
3146 goto reset;
3147 }
3148
3149 memset( larger_buf, 0, ori_len + extra_len );
3150 memcpy( larger_buf, buf, ori_len );
3151
3152 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153 ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003154 if( ret != extra_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003158 ret = 1;
3159 goto reset;
3160 }
3161
3162 larger_buf[ori_len + extra_len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163 mbedtls_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003164 ori_len + extra_len, ori_len, extra_len,
3165 (char *) larger_buf );
3166
3167 /* End of message should be detected according to the syntax of the
3168 * application protocol (eg HTTP), just use a dummy test here. */
3169 if( larger_buf[ori_len + extra_len - 1] == '\n' )
3170 terminated = 1;
3171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172 mbedtls_free( larger_buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003173 }
3174
3175 if( terminated )
3176 {
3177 ret = 0;
3178 break;
3179 }
3180 }
3181 while( 1 );
3182 }
3183 else /* Not stream, so datagram */
3184 {
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003185 len = opt.buffer_size - 1;
3186 memset( buf, 0, opt.buffer_size );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003187
Gilles Peskineb44692f2018-04-24 12:18:19 +02003188 do
Hanno Becker16970d22017-10-10 15:56:37 +01003189 {
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003190 /* Without the call to `mbedtls_ssl_check_pending`, it might
3191 * happen that the client sends application data in the same
3192 * datagram as the Finished message concluding the handshake.
3193 * In this case, the application data would be ready to be
3194 * processed while the underlying transport wouldn't signal
3195 * any further incoming data.
3196 *
3197 * See the test 'Event-driven I/O: session-id resume, UDP packing'
3198 * in tests/ssl-opt.sh.
3199 */
3200
3201 /* For event-driven IO, wait for socket to become available */
3202 if( mbedtls_ssl_check_pending( &ssl ) == 0 &&
3203 opt.event == 1 /* level triggered IO */ )
3204 {
3205#if defined(MBEDTLS_TIMING_C)
3206 idle( &client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ );
3207#else
3208 idle( &client_fd, MBEDTLS_ERR_SSL_WANT_READ );
3209#endif
3210 }
3211
Hanno Becker16970d22017-10-10 15:56:37 +01003212 ret = mbedtls_ssl_read( &ssl, buf, len );
3213
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003214 /* Note that even if `mbedtls_ssl_check_pending` returns true,
3215 * it can happen that the subsequent call to `mbedtls_ssl_read`
3216 * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
3217 * might be discarded (e.g. because they are retransmissions). */
Hanno Becker16970d22017-10-10 15:56:37 +01003218 }
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003219 while( mbedtls_status_is_ssl_in_progress( ret ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003220
3221 if( ret <= 0 )
3222 {
3223 switch( ret )
3224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3226 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003227 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003228 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003229
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003230 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003232 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003233 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003234 }
3235
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003236 len = ret;
3237 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003239 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003240 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003241
3242 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003243 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02003244 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00003247 if( opt.renegotiate && exchanges_left == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249 mbedtls_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003250 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003253 {
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003254 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003257 goto reset;
3258 }
Hanno Becker16970d22017-10-10 15:56:37 +01003259
3260 /* For event-driven IO, wait for socket to become available */
3261 if( opt.event == 1 /* level triggered IO */ )
3262 {
3263#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003264 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003265#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003266 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003267#endif
3268 }
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003269 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003272 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003274
Hanno Beckera5a2b082019-05-15 14:03:01 +01003275#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01003276 ret = report_cid_usage( &ssl, "after renegotiation" );
3277 if( ret != 0 )
3278 goto exit;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003279#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker96870292019-05-03 17:30:59 +01003280
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003281 /*
3282 * 7. Write the 200 Response
3283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 mbedtls_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003285 fflush( stdout );
3286
3287 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003289
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003290 /* Add padding to the response to reach opt.response_size in length */
3291 if( opt.response_size != DFL_RESPONSE_SIZE &&
3292 len < opt.response_size )
3293 {
3294 memset( buf + len, 'B', opt.response_size - len );
3295 len += opt.response_size - len;
3296 }
3297
3298 /* Truncate if response size is smaller than the "natural" size */
3299 if( opt.response_size != DFL_RESPONSE_SIZE &&
3300 len > opt.response_size )
3301 {
3302 len = opt.response_size;
3303
3304 /* Still end with \r\n unless that's really not possible */
3305 if( len >= 2 ) buf[len - 2] = '\r';
3306 if( len >= 1 ) buf[len - 1] = '\n';
3307 }
3308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003310 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003311 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003314 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003318 mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003319 goto reset;
3320 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003321
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003322 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003325 goto reset;
3326 }
Hanno Becker16970d22017-10-10 15:56:37 +01003327
3328 /* For event-driven IO, wait for socket to become available */
3329 if( opt.event == 1 /* level triggered IO */ )
3330 {
3331#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003332 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003333#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003334 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003335#endif
3336 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003337 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003338 }
3339 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003340 else /* Not stream, so datagram */
3341 {
Hanno Becker16970d22017-10-10 15:56:37 +01003342 while( 1 )
3343 {
3344 ret = mbedtls_ssl_write( &ssl, buf, len );
3345
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003346 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003347 break;
3348
3349 /* For event-driven IO, wait for socket to become available */
3350 if( opt.event == 1 /* level triggered IO */ )
3351 {
3352#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003353 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003354#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003355 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003356#endif
3357 }
3358 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003359
3360 if( ret < 0 )
3361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003363 goto reset;
3364 }
3365
3366 frags = 1;
3367 written = ret;
3368 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003369
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003370 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 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 +01003372 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01003373
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003374 /*
Jarno Lamsaf4572932019-05-29 15:41:21 +03003375 * 7b. Simulate serialize/deserialize and go back to data exchange
3376 */
Jarno Lamsacf1b6722019-06-04 11:06:31 +03003377#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003378 if( opt.serialize != 0 )
Jarno Lamsaf4572932019-05-29 15:41:21 +03003379 {
Jarno Lamsa034ae842019-06-06 15:10:07 +03003380 size_t buf_len;
3381 unsigned char *context_buf = NULL;
Jarno Lamsaf4572932019-05-29 15:41:21 +03003382
3383 opt.serialize = 0;
3384 mbedtls_printf( " Serializing live connection..." );
3385
Jarno Lamsa034ae842019-06-06 15:10:07 +03003386 ret = mbedtls_ssl_context_save( &ssl, NULL, 0, &buf_len );
Jarno Lamsaf4f8ed72019-06-04 16:03:28 +03003387
3388 /* Allow stub implementation returning 0 for now */
3389 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL &&
3390 ret != 0 )
Jarno Lamsaf4572932019-05-29 15:41:21 +03003391 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003392 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
3393 "-0x%x\n\n", -ret );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003394
3395 goto exit;
3396 }
3397
Jarno Lamsa034ae842019-06-06 15:10:07 +03003398 if( ( context_buf = mbedtls_calloc( 1, buf_len ) ) == NULL )
Jarno Lamsaf4572932019-05-29 15:41:21 +03003399 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003400 mbedtls_printf( " failed\n ! Couldn't allocate buffer for "
3401 "serialized context" );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003402
3403 goto exit;
3404 }
3405
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03003406 if( ( ret = mbedtls_ssl_context_save( &ssl, context_buf,
3407 buf_len, &buf_len ) ) != 0 )
Jarno Lamsaf4572932019-05-29 15:41:21 +03003408 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003409 mbedtls_printf( "failed\n ! mbedtls_ssl_context_save returned "
3410 "-0x%x\n\n", -ret );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003411
3412 goto exit;
3413 }
3414
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003415 if( opt.serialize == 2 )
3416 {
3417 mbedtls_ssl_free( &ssl );
3418
3419 mbedtls_ssl_init( &ssl );
3420
3421 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
3422 {
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03003423 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned "
3424 "-0x%x\n\n", -ret );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003425 goto exit;
3426 }
3427
3428 if( opt.nbio == 2 )
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03003429 mbedtls_ssl_set_bio( &ssl, &client_fd, my_send, my_recv,
3430 NULL );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003431 else
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03003432 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send,
3433 mbedtls_net_recv,
3434 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003435
Jarno Lamsa29a15c22019-06-13 11:45:06 +03003436#if defined(MBEDTLS_TIMING_C)
3437 if( opt.nbio != 0 && opt.read_timeout != 0 )
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03003438 mbedtls_ssl_set_timer_cb( &ssl, &timer,
3439 mbedtls_timing_set_delay,
3440 mbedtls_timing_get_delay );
Jarno Lamsa29a15c22019-06-13 11:45:06 +03003441#endif /* MBEDTLS_TIMING_C */
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003442 }
3443
Jarno Lamsaf4572932019-05-29 15:41:21 +03003444 mbedtls_printf( " Deserializing connection..." );
3445
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03003446 if( ( ret = mbedtls_ssl_context_load( &ssl, context_buf,
3447 buf_len ) ) != 0 )
Jarno Lamsaf4572932019-05-29 15:41:21 +03003448 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003449 mbedtls_printf( "failed\n ! mbedtls_ssl_context_load returned "
3450 "-0x%x\n\n", -ret );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003451
3452 goto exit;
3453 }
3454 }
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003455#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jarno Lamsaf4572932019-05-29 15:41:21 +03003456
3457 /*
3458 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003459 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003460 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003461 goto data_exchange;
3462
3463 /*
3464 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003465 */
3466close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01003468
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003469 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003471 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003472 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 mbedtls_printf( " done\n" );
Rich Evansf90016a2015-01-19 14:26:37 +00003475
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003476 goto reset;
3477
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003478 /*
3479 * Cleanup and exit
3480 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003481exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003483 if( ret != 0 )
3484 {
3485 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 mbedtls_strerror( ret, error_buf, 100 );
3487 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003488 }
3489#endif
3490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491 mbedtls_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003492 fflush( stdout );
3493
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003494 mbedtls_net_free( &client_fd );
3495 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +02003496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3498 mbedtls_dhm_free( &dhm );
Paul Bakkera317a982014-06-18 16:44:11 +02003499#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500#if defined(MBEDTLS_X509_CRT_PARSE_C)
3501 mbedtls_x509_crt_free( &cacert );
3502 mbedtls_x509_crt_free( &srvcert );
3503 mbedtls_pk_free( &pkey );
3504 mbedtls_x509_crt_free( &srvcert2 );
3505 mbedtls_pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02003506#endif
Gilles Peskine44817442018-06-13 18:09:28 +02003507#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3508 for( i = 0; (size_t) i < ssl_async_keys.slots_used; i++ )
3509 {
3510 if( ssl_async_keys.slots[i].pk_owned )
3511 {
3512 mbedtls_pk_free( ssl_async_keys.slots[i].pk );
3513 mbedtls_free( ssl_async_keys.slots[i].pk );
3514 ssl_async_keys.slots[i].pk = NULL;
3515 }
3516 }
3517#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02003518#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003519 sni_free( sni_info );
3520#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003522 psk_free( psk_info );
3523#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3525 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003526#endif
Paul Bakkered27a042013-04-18 22:46:23 +02003527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003529 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 mbedtls_ctr_drbg_free( &ctr_drbg );
3531 mbedtls_entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533#if defined(MBEDTLS_SSL_CACHE_C)
3534 mbedtls_ssl_cache_free( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00003535#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003536#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3537 mbedtls_ssl_ticket_free( &ticket_ctx );
3538#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539#if defined(MBEDTLS_SSL_COOKIE_C)
3540 mbedtls_ssl_cookie_free( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02003541#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003542
Hanno Becker095d9cf2018-10-09 12:39:13 +01003543 mbedtls_free( buf );
3544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
3546#if defined(MBEDTLS_MEMORY_DEBUG)
3547 mbedtls_memory_buffer_alloc_status();
Paul Bakker82024bf2013-07-04 11:52:32 +02003548#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +02003550#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02003551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552 mbedtls_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003553
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003554#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003556 fflush( stdout ); getchar();
3557#endif
3558
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02003559 // Shell can not handle large exit numbers -> 1 for errors
3560 if( ret < 0 )
3561 ret = 1;
3562
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003563 return( ret );
3564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003565#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
3566 MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003567 MBEDTLS_CTR_DRBG_C */