blob: b0c48764a7da44706d38443e4ace804efdd8c28c [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
Hanno Becker8651a432017-06-09 16:13:22 +0100465#define ALPN_LIST_SIZE 10
466#define CURVE_LIST_SIZE 20
467
Andres AG692ad842017-01-19 16:30:57 +0000468#define PUT_UINT64_BE(out_be,in_le,i) \
469{ \
470 (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \
471 (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \
472 (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \
473 (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \
474 (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \
475 (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \
476 (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \
477 (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \
478}
479
Simon Butcher63cb97e2018-12-06 17:43:31 +0000480
Rich Evans85b05ec2015-02-12 11:37:29 +0000481/*
482 * global options
483 */
484struct options
485{
486 const char *server_addr; /* address on which the ssl service runs */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200487 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000488 int debug_level; /* level of debugging */
489 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100490 int event; /* loop or event-driven IO? level or edge triggered? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200492 int response_size; /* pad response with header to requested size */
493 uint16_t buffer_size; /* IO buffer size */
Rich Evans85b05ec2015-02-12 11:37:29 +0000494 const char *ca_file; /* the file with the CA certificate(s) */
495 const char *ca_path; /* the path with the CA certificate(s) reside */
496 const char *crt_file; /* the file with the server certificate */
497 const char *key_file; /* the file with the server key */
498 const char *crt_file2; /* the file with the 2nd server certificate */
499 const char *key_file2; /* the file with the 2nd server key */
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100500 const char *async_operations; /* supported SSL asynchronous operations */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100501 int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
502 int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
503 int async_private_error; /* inject error in async private callback */
Rich Evans85b05ec2015-02-12 11:37:29 +0000504 const char *psk; /* the pre-shared key */
505 const char *psk_identity; /* the pre-shared key identity */
506 char *psk_list; /* list of PSK id/key pairs for callback */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200507 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000508 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
509 const char *version_suites; /* per-version ciphersuites */
510 int renegotiation; /* enable / disable renegotiation */
511 int allow_legacy; /* allow legacy renegotiation */
512 int renegotiate; /* attempt renegotiation? */
513 int renego_delay; /* delay before enforcing renegotiation */
Andres AG692ad842017-01-19 16:30:57 +0000514 uint64_t renego_period; /* period for automatic renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000515 int exchanges; /* number of data exchanges */
516 int min_version; /* minimum protocol version accepted */
517 int max_version; /* maximum protocol version accepted */
518 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200519 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000520 int auth_mode; /* verify mode for connection */
Janos Follath4817e272017-04-10 13:44:33 +0100521 int cert_req_ca_list; /* should we send the CA list? */
Rich Evans85b05ec2015-02-12 11:37:29 +0000522 unsigned char mfl_code; /* code for maximum fragment length */
523 int trunc_hmac; /* accept truncated hmac? */
524 int tickets; /* enable / disable session tickets */
525 int ticket_timeout; /* session ticket lifetime */
526 int cache_max; /* max number of session cache entries */
527 int cache_timeout; /* expiration delay of session cache entries */
528 char *sni; /* string describing sni information */
Hanno Beckere6706e62017-05-15 16:05:15 +0100529 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000530 const char *alpn_string; /* ALPN supported protocols */
531 const char *dhm_file; /* the file with the DH parameters */
532 int extended_ms; /* allow negotiation of extended MS? */
Jarno Lamsa41b35912019-06-10 15:51:11 +0300533 int enforce_extended_master_secret; /* Enforce the usage of extended
534 * master secret */
Rich Evans85b05ec2015-02-12 11:37:29 +0000535 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000536 int transport; /* TLS or DTLS? */
537 int cookies; /* Use cookies for DTLS? -1 to break them */
538 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
539 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
540 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200541 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Hanno Beckere7675d02018-08-14 13:28:56 +0100542 int dgram_packing; /* allow/forbid datagram packing */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000543 int badmac_limit; /* Limit of records with bad MAC */
Hanno Becker1029ace2019-04-09 17:28:10 +0100544 int cid_enabled; /* whether to use the CID extension or not */
Hanno Becker96870292019-05-03 17:30:59 +0100545 int cid_enabled_renego; /* whether to use the CID extension or not
546 * during renegotiation */
Hanno Becker1029ace2019-04-09 17:28:10 +0100547 const char *cid_val; /* the CID to use for incoming messages */
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +0300548 int serialize; /* serialize/deserialize connection */
Hanno Becker96870292019-05-03 17:30:59 +0100549 const char *cid_val_renego; /* the CID to use for incoming messages
550 * after renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000551} opt;
552
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100553int query_config( const char *config );
554
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200555static void my_debug( void *ctx, int level,
556 const char *file, int line,
557 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000558{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200559 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000560
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200561 /* Extract basename from file */
562 for( p = basename = file; *p != '\0'; p++ )
563 if( *p == '/' || *p == '\\' )
564 basename = p + 1;
565
566 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000567 fflush( (FILE *) ctx );
568}
569
570/*
571 * Test recv/send functions that make sure each try returns
572 * WANT_READ/WANT_WRITE at least once before sucesseding
573 */
574static int my_recv( void *ctx, unsigned char *buf, size_t len )
575{
576 static int first_try = 1;
577 int ret;
578
579 if( first_try )
580 {
581 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100582 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000583 }
584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100586 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000587 first_try = 1; /* Next call will be a new operation */
588 return( ret );
589}
590
591static int my_send( void *ctx, const unsigned char *buf, size_t len )
592{
593 static int first_try = 1;
594 int ret;
595
596 if( first_try )
597 {
598 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100599 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000600 }
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100603 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000604 first_try = 1; /* Next call will be a new operation */
605 return( ret );
606}
607
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200608/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200609 * Return authmode from string, or -1 on error
610 */
611static int get_auth_mode( const char *s )
612{
613 if( strcmp( s, "none" ) == 0 )
614 return( MBEDTLS_SSL_VERIFY_NONE );
615 if( strcmp( s, "optional" ) == 0 )
616 return( MBEDTLS_SSL_VERIFY_OPTIONAL );
617 if( strcmp( s, "required" ) == 0 )
618 return( MBEDTLS_SSL_VERIFY_REQUIRED );
619
620 return( -1 );
621}
622
623/*
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200624 * Used by sni_parse and psk_parse to handle coma-separated lists
625 */
626#define GET_ITEM( dst ) \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100627 do \
628 { \
629 (dst) = p; \
630 while( *p != ',' ) \
631 if( ++p > end ) \
632 goto error; \
633 *p++ = '\0'; \
634 } while( 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200635
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200636#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100637typedef struct _sni_entry sni_entry;
638
639struct _sni_entry {
640 const char *name;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_x509_crt *cert;
642 mbedtls_pk_context *key;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200643 mbedtls_x509_crt* ca;
644 mbedtls_x509_crl* crl;
645 int authmode;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100646 sni_entry *next;
647};
648
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100649void sni_free( sni_entry *head )
650{
651 sni_entry *cur = head, *next;
652
653 while( cur != NULL )
654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_x509_crt_free( cur->cert );
656 mbedtls_free( cur->cert );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_pk_free( cur->key );
659 mbedtls_free( cur->key );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100660
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200661 mbedtls_x509_crt_free( cur->ca );
662 mbedtls_free( cur->ca );
Ron Eldor24eec792019-04-04 15:02:01 +0300663#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200664 mbedtls_x509_crl_free( cur->crl );
665 mbedtls_free( cur->crl );
Ron Eldor24eec792019-04-04 15:02:01 +0300666#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100667 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_free( cur );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100669 cur = next;
670 }
671}
672
673/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200674 * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
675 * into a usable sni_entry list. For ca1, crl1, auth1, the special value
676 * '-' means unset. If ca1 is unset, then crl1 is ignored too.
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000677 *
678 * Modifies the input string! This is not production quality!
679 */
680sni_entry *sni_parse( char *sni_string )
681{
682 sni_entry *cur = NULL, *new = NULL;
683 char *p = sni_string;
684 char *end = p;
Ron Eldor24eec792019-04-04 15:02:01 +0300685 char *crt_file, *key_file, *ca_file, *auth_str;
686#if defined(MBEDTLS_X509_CRL_PARSE_C)
687 char *crl_file;
688#endif
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000689
690 while( *end != '\0' )
691 ++end;
692 *end = ',';
693
694 while( p <= end )
695 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200696 if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000697 {
698 sni_free( cur );
699 return( NULL );
700 }
701
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200702 GET_ITEM( new->name );
703 GET_ITEM( crt_file );
704 GET_ITEM( key_file );
705 GET_ITEM( ca_file );
Ron Eldor24eec792019-04-04 15:02:01 +0300706#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200707 GET_ITEM( crl_file );
Ron Eldor24eec792019-04-04 15:02:01 +0300708#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200709 GET_ITEM( auth_str );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000710
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200711 if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
712 ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200713 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_x509_crt_init( new->cert );
716 mbedtls_pk_init( new->key );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
719 mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000720 goto error;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200721
722 if( strcmp( ca_file, "-" ) != 0 )
723 {
724 if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
725 goto error;
726
727 mbedtls_x509_crt_init( new->ca );
728
729 if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
730 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000731 }
732
Ron Eldor24eec792019-04-04 15:02:01 +0300733#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200734 if( strcmp( crl_file, "-" ) != 0 )
735 {
736 if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
737 goto error;
738
739 mbedtls_x509_crl_init( new->crl );
740
741 if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
742 goto error;
743 }
Ron Eldor24eec792019-04-04 15:02:01 +0300744#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200745
746 if( strcmp( auth_str, "-" ) != 0 )
747 {
748 if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
749 goto error;
750 }
751 else
752 new->authmode = DFL_AUTH_MODE;
753
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000754 new->next = cur;
755 cur = new;
756 }
757
758 return( cur );
759
760error:
761 sni_free( new );
762 sni_free( cur );
763 return( NULL );
764}
765
766/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100767 * SNI callback.
768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100770 const unsigned char *name, size_t name_len )
771{
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200772 const sni_entry *cur = (const sni_entry *) p_info;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100773
774 while( cur != NULL )
775 {
776 if( name_len == strlen( cur->name ) &&
777 memcmp( name, cur->name, name_len ) == 0 )
778 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200779 if( cur->ca != NULL )
780 mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
781
782 if( cur->authmode != DFL_AUTH_MODE )
783 mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
784
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200785 return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100786 }
787
788 cur = cur->next;
789 }
790
791 return( -1 );
792}
793
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200794#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100795
Hanno Becker2e0bedc2019-04-30 14:18:06 +0100796#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) || \
Hanno Beckera5a2b082019-05-15 14:03:01 +0100797 defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200798
Hanno Beckerd6028a12018-10-15 12:01:35 +0100799#define HEX2NUM( c ) \
800 do \
801 { \
802 if( (c) >= '0' && (c) <= '9' ) \
803 (c) -= '0'; \
804 else if( (c) >= 'a' && (c) <= 'f' ) \
805 (c) -= 'a' - 10; \
806 else if( (c) >= 'A' && (c) <= 'F' ) \
807 (c) -= 'A' - 10; \
808 else \
809 return( -1 ); \
810 } while( 0 )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200811
812/*
813 * Convert a hex string to bytes.
814 * Return 0 on success, -1 on error.
815 */
816int unhexify( unsigned char *output, const char *input, size_t *olen )
817{
818 unsigned char c;
819 size_t j;
820
821 *olen = strlen( input );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 if( *olen % 2 != 0 || *olen / 2 > MBEDTLS_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200823 return( -1 );
824 *olen /= 2;
825
826 for( j = 0; j < *olen * 2; j += 2 )
827 {
828 c = input[j];
829 HEX2NUM( c );
830 output[ j / 2 ] = c << 4;
831
832 c = input[j + 1];
833 HEX2NUM( c );
834 output[ j / 2 ] |= c;
835 }
836
837 return( 0 );
838}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200839
Hanno Becker2e0bedc2019-04-30 14:18:06 +0100840#endif
841
842#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
843
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200844typedef struct _psk_entry psk_entry;
845
846struct _psk_entry
847{
848 const char *name;
849 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 unsigned char key[MBEDTLS_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200851 psk_entry *next;
852};
853
854/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000855 * Free a list of psk_entry's
856 */
857void psk_free( psk_entry *head )
858{
859 psk_entry *next;
860
861 while( head != NULL )
862 {
863 next = head->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 mbedtls_free( head );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000865 head = next;
866 }
867}
868
869/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200870 * Parse a string of pairs name1,key1[,name2,key2[,...]]
871 * into a usable psk_entry list.
872 *
873 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200874 */
875psk_entry *psk_parse( char *psk_string )
876{
877 psk_entry *cur = NULL, *new = NULL;
878 char *p = psk_string;
879 char *end = p;
880 char *key_hex;
881
882 while( *end != '\0' )
883 ++end;
884 *end = ',';
885
886 while( p <= end )
887 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200888 if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +0000889 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200890
891 memset( new, 0, sizeof( psk_entry ) );
892
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200893 GET_ITEM( new->name );
894 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200895
896 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000897 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200898
899 new->next = cur;
900 cur = new;
901 }
902
903 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200904
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000905error:
906 psk_free( new );
907 psk_free( cur );
908 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200909}
910
911/*
912 * PSK callback
913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200915 const unsigned char *name, size_t name_len )
916{
917 psk_entry *cur = (psk_entry *) p_info;
918
919 while( cur != NULL )
920 {
921 if( name_len == strlen( cur->name ) &&
922 memcmp( name, cur->name, name_len ) == 0 )
923 {
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +0100924 return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200925 }
926
927 cur = cur->next;
928 }
929
930 return( -1 );
931}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200933
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200934static mbedtls_net_context listen_fd, client_fd;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200935
936/* Interruption handler to ensure clean exit (for valgrind testing) */
937#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200938static int received_sigterm = 0;
939void term_handler( int sig )
940{
941 ((void) sig);
942 received_sigterm = 1;
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200943 mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
944 mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200945}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200946#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200947
Gilles Peskine682df092017-05-11 19:01:11 +0200948#if defined(MBEDTLS_X509_CRT_PARSE_C)
949static int ssl_sig_hashes_for_test[] = {
950#if defined(MBEDTLS_SHA512_C)
951 MBEDTLS_MD_SHA512,
952 MBEDTLS_MD_SHA384,
953#endif
954#if defined(MBEDTLS_SHA256_C)
955 MBEDTLS_MD_SHA256,
956 MBEDTLS_MD_SHA224,
957#endif
958#if defined(MBEDTLS_SHA1_C)
959 /* Allow SHA-1 as we use it extensively in tests. */
960 MBEDTLS_MD_SHA1,
961#endif
962 MBEDTLS_MD_NONE
963};
964#endif /* MBEDTLS_X509_CRT_PARSE_C */
965
Gilles Peskinea36ac4f2018-04-26 08:05:02 +0200966/** Return true if \p ret is a status code indicating that there is an
967 * operation in progress on an SSL connection, and false if it indicates
968 * success or a fatal error.
969 *
970 * The possible operations in progress are:
971 *
972 * - A read, when the SSL input buffer does not contain a full message.
973 * - A write, when the SSL output buffer contains some data that has not
974 * been sent over the network yet.
975 * - An asynchronous callback that has not completed yet. */
976static int mbedtls_status_is_ssl_in_progress( int ret )
977{
978 return( ret == MBEDTLS_ERR_SSL_WANT_READ ||
979 ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
980 ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
981}
982
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200983#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100984typedef struct
985{
Gilles Peskine44817442018-06-13 18:09:28 +0200986 mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
987 mbedtls_pk_context *pk; /*!< Private key */
988 unsigned delay; /*!< Number of resume steps to go through */
989 unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100990} ssl_async_key_slot_t;
991
992typedef enum {
Gilles Peskinead28bf02018-04-26 00:19:16 +0200993 SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
994 SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
995 SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start */
996 SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
Gilles Peskinec9125722018-04-26 07:15:40 +0200997#define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100998} ssl_async_inject_error_t;
999
1000typedef struct
1001{
Gilles Peskinee2479892018-06-13 18:06:51 +02001002 ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001003 size_t slots_used;
1004 ssl_async_inject_error_t inject_error;
1005 int (*f_rng)(void *, unsigned char *, size_t);
1006 void *p_rng;
1007} ssl_async_key_context_t;
1008
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001009int ssl_async_set_key( ssl_async_key_context_t *ctx,
Gilles Peskine44817442018-06-13 18:09:28 +02001010 mbedtls_x509_crt *cert,
1011 mbedtls_pk_context *pk,
1012 int pk_take_ownership,
1013 unsigned delay )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001014{
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001015 if( ctx->slots_used >= sizeof( ctx->slots ) / sizeof( *ctx->slots ) )
1016 return( -1 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001017 ctx->slots[ctx->slots_used].cert = cert;
1018 ctx->slots[ctx->slots_used].pk = pk;
1019 ctx->slots[ctx->slots_used].delay = delay;
Gilles Peskine44817442018-06-13 18:09:28 +02001020 ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001021 ++ctx->slots_used;
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001022 return( 0 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001023}
1024
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001025#define SSL_ASYNC_INPUT_MAX_SIZE 512
Gilles Peskined3268832018-04-26 06:23:59 +02001026
1027typedef enum
1028{
1029 ASYNC_OP_SIGN,
1030 ASYNC_OP_DECRYPT,
1031} ssl_async_operation_type_t;
1032/* Note that the enum above and the array below need to be kept in sync!
1033 * `ssl_async_operation_names[op]` is the name of op for each value `op`
1034 * of type `ssl_async_operation_type_t`. */
1035static const char *const ssl_async_operation_names[] =
1036{
1037 "sign",
1038 "decrypt",
1039};
1040
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001041typedef struct
1042{
Gilles Peskine6331d782018-04-26 13:27:43 +02001043 unsigned slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001044 ssl_async_operation_type_t operation_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001045 mbedtls_md_type_t md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001046 unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
1047 size_t input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001048 unsigned remaining_delay;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001049} ssl_async_operation_context_t;
1050
Gilles Peskine8f97af72018-04-26 11:46:10 +02001051static int ssl_async_start( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001052 mbedtls_x509_crt *cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001053 ssl_async_operation_type_t op_type,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001054 mbedtls_md_type_t md_alg,
1055 const unsigned char *input,
1056 size_t input_len )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001057{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001058 ssl_async_key_context_t *config_data =
1059 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskine6331d782018-04-26 13:27:43 +02001060 unsigned slot;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001061 ssl_async_operation_context_t *ctx = NULL;
Gilles Peskined3268832018-04-26 06:23:59 +02001062 const char *op_name = ssl_async_operation_names[op_type];
Gilles Peskinef1127252018-04-24 13:05:39 +02001063
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001064 {
1065 char dn[100];
Gilles Peskined5d983e2018-06-15 14:05:10 +02001066 if( mbedtls_x509_dn_gets( dn, sizeof( dn ), &cert->subject ) > 0 )
1067 mbedtls_printf( "Async %s callback: looking for DN=%s\n",
1068 op_name, dn );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001069 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001070
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001071 /* Look for a private key that matches the public key in cert.
1072 * Since this test code has the private key inside Mbed TLS,
1073 * we call mbedtls_pk_check_pair to match a private key with the
1074 * public key. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001075 for( slot = 0; slot < config_data->slots_used; slot++ )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001076 {
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001077 if( mbedtls_pk_check_pair( &cert->pk,
1078 config_data->slots[slot].pk ) == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001079 break;
1080 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001081 if( slot == config_data->slots_used )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001082 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001083 mbedtls_printf( "Async %s callback: no key matches this certificate.\n",
1084 op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001085 return( MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH );
1086 }
Gilles Peskine6331d782018-04-26 13:27:43 +02001087 mbedtls_printf( "Async %s callback: using key slot %u, delay=%u.\n",
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001088 op_name, slot, config_data->slots[slot].delay );
Gilles Peskinef1127252018-04-24 13:05:39 +02001089
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001090 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001091 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001092 mbedtls_printf( "Async %s callback: injected error\n", op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001093 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1094 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001095
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001096 if( input_len > SSL_ASYNC_INPUT_MAX_SIZE )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001097 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Gilles Peskinef1127252018-04-24 13:05:39 +02001098
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001099 ctx = mbedtls_calloc( 1, sizeof( *ctx ) );
1100 if( ctx == NULL )
1101 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1102 ctx->slot = slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001103 ctx->operation_type = op_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001104 ctx->md_alg = md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001105 memcpy( ctx->input, input, input_len );
1106 ctx->input_len = input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001107 ctx->remaining_delay = config_data->slots[slot].delay;
Gilles Peskinea668c602018-04-30 11:54:39 +02001108 mbedtls_ssl_set_async_operation_data( ssl, ctx );
Gilles Peskinef1127252018-04-24 13:05:39 +02001109
Gilles Peskineceb541b2018-04-26 06:30:45 +02001110 if( ctx->remaining_delay == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001111 return( 0 );
1112 else
1113 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1114}
1115
Gilles Peskine8f97af72018-04-26 11:46:10 +02001116static int ssl_async_sign( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001117 mbedtls_x509_crt *cert,
1118 mbedtls_md_type_t md_alg,
1119 const unsigned char *hash,
1120 size_t hash_len )
1121{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001122 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001123 ASYNC_OP_SIGN, md_alg,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001124 hash, hash_len ) );
1125}
1126
Gilles Peskine8f97af72018-04-26 11:46:10 +02001127static int ssl_async_decrypt( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001128 mbedtls_x509_crt *cert,
1129 const unsigned char *input,
1130 size_t input_len )
1131{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001132 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001133 ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001134 input, input_len ) );
1135}
1136
Gilles Peskine8f97af72018-04-26 11:46:10 +02001137static int ssl_async_resume( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001138 unsigned char *output,
1139 size_t *output_len,
1140 size_t output_size )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001141{
Gilles Peskinea668c602018-04-30 11:54:39 +02001142 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine8f97af72018-04-26 11:46:10 +02001143 ssl_async_key_context_t *config_data =
1144 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001145 ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001146 int ret;
Gilles Peskinef5a99962018-04-30 16:37:23 +02001147 const char *op_name;
Gilles Peskinef1127252018-04-24 13:05:39 +02001148
Gilles Peskineceb541b2018-04-26 06:30:45 +02001149 if( ctx->remaining_delay > 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001150 {
Gilles Peskineceb541b2018-04-26 06:30:45 +02001151 --ctx->remaining_delay;
Gilles Peskine6331d782018-04-26 13:27:43 +02001152 mbedtls_printf( "Async resume (slot %u): call %u more times.\n",
Gilles Peskineceb541b2018-04-26 06:30:45 +02001153 ctx->slot, ctx->remaining_delay );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001154 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1155 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001156
Gilles Peskined3268832018-04-26 06:23:59 +02001157 switch( ctx->operation_type )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001158 {
Gilles Peskined3268832018-04-26 06:23:59 +02001159 case ASYNC_OP_DECRYPT:
Gilles Peskined3268832018-04-26 06:23:59 +02001160 ret = mbedtls_pk_decrypt( key_slot->pk,
1161 ctx->input, ctx->input_len,
1162 output, output_len, output_size,
1163 config_data->f_rng, config_data->p_rng );
1164 break;
1165 case ASYNC_OP_SIGN:
Gilles Peskined3268832018-04-26 06:23:59 +02001166 ret = mbedtls_pk_sign( key_slot->pk,
1167 ctx->md_alg,
1168 ctx->input, ctx->input_len,
1169 output, output_len,
1170 config_data->f_rng, config_data->p_rng );
1171 break;
1172 default:
Gilles Peskine6331d782018-04-26 13:27:43 +02001173 mbedtls_printf( "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
Gilles Peskined3268832018-04-26 06:23:59 +02001174 ctx->slot, (long) ctx->operation_type );
Gilles Peskine44817442018-06-13 18:09:28 +02001175 mbedtls_free( ctx );
Gilles Peskined3268832018-04-26 06:23:59 +02001176 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1177 break;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001178 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001179
Gilles Peskinef5a99962018-04-30 16:37:23 +02001180 op_name = ssl_async_operation_names[ctx->operation_type];
1181
Gilles Peskinec9125722018-04-26 07:15:40 +02001182 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001183 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001184 mbedtls_printf( "Async resume callback: %s done but injected error\n",
1185 op_name );
Gilles Peskine2636fad2018-06-12 14:17:39 +02001186 mbedtls_free( ctx );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001187 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1188 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001189
Gilles Peskine6331d782018-04-26 13:27:43 +02001190 mbedtls_printf( "Async resume (slot %u): %s done, status=%d.\n",
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001191 ctx->slot, op_name, ret );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001192 mbedtls_free( ctx );
1193 return( ret );
1194}
1195
Gilles Peskine8f97af72018-04-26 11:46:10 +02001196static void ssl_async_cancel( mbedtls_ssl_context *ssl )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001197{
Gilles Peskinea668c602018-04-30 11:54:39 +02001198 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001199 mbedtls_printf( "Async cancel callback.\n" );
1200 mbedtls_free( ctx );
1201}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001202#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001203
Hanno Becker16970d22017-10-10 15:56:37 +01001204/*
1205 * Wait for an event from the underlying transport or the timer
1206 * (Used in event-driven IO mode).
1207 */
1208#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001209int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001210 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001211#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001212int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001213 mbedtls_timing_delay_context *timer,
1214 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001215#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001216{
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001217 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +01001218 int poll_type = 0;
1219
1220 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
1221 poll_type = MBEDTLS_NET_POLL_WRITE;
1222 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
1223 poll_type = MBEDTLS_NET_POLL_READ;
1224#if !defined(MBEDTLS_TIMING_C)
1225 else
Hanno Beckeref527962018-03-15 15:49:24 +00001226 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001227#endif
1228
1229 while( 1 )
1230 {
Hanno Becker197a91c2017-10-31 10:58:53 +00001231 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +01001232#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001233 if( timer != NULL &&
1234 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +01001235 {
Hanno Becker16970d22017-10-10 15:56:37 +01001236 break;
1237 }
Hanno Becker197a91c2017-10-31 10:58:53 +00001238#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +01001239
Hanno Becker197a91c2017-10-31 10:58:53 +00001240 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001241 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +01001242 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001243 ret = mbedtls_net_poll( fd, poll_type, 0 );
1244 if( ret < 0 )
1245 return( ret );
1246 if( ret == poll_type )
1247 break;
Hanno Becker16970d22017-10-10 15:56:37 +01001248 }
1249 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001250
1251 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001252}
1253
Hanno Beckera5a2b082019-05-15 14:03:01 +01001254#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01001255int report_cid_usage( mbedtls_ssl_context *ssl,
1256 const char *additional_description )
1257{
1258 int ret;
1259 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
1260 size_t peer_cid_len;
1261 int cid_negotiated;
1262
1263 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1264 return( 0 );
1265
1266 /* Check if the use of a CID has been negotiated */
1267 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
1268 peer_cid, &peer_cid_len );
1269 if( ret != 0 )
1270 {
1271 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
1272 -ret );
1273 return( ret );
1274 }
1275
1276 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
1277 {
1278 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
1279 {
1280 mbedtls_printf( "(%s) Use of Connection ID was not offered by client.\n",
1281 additional_description );
1282 }
1283 }
1284 else
1285 {
1286 size_t idx=0;
1287 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
1288 additional_description );
1289 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
1290 additional_description,
1291 (unsigned) peer_cid_len );
1292 while( idx < peer_cid_len )
1293 {
1294 mbedtls_printf( "%02x ", peer_cid[ idx ] );
1295 idx++;
1296 }
1297 mbedtls_printf( "\n" );
1298 }
1299
1300 return( 0 );
1301}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001302#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker96870292019-05-03 17:30:59 +01001303
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001304int main( int argc, char *argv[] )
1305{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001306 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001307 int version_suites[4][2];
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001308 unsigned char* buf = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1310 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +02001311 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +02001312 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +02001313#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001314 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001315 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02001316 size_t cliip_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317#if defined(MBEDTLS_SSL_COOKIE_C)
1318 mbedtls_ssl_cookie_ctx cookie_ctx;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001319#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001320
Gilles Peskineef86ab22017-05-05 18:59:02 +02001321#if defined(MBEDTLS_X509_CRT_PARSE_C)
1322 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
1323#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 mbedtls_entropy_context entropy;
1325 mbedtls_ctr_drbg_context ctr_drbg;
1326 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001327 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001328#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001329 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001330#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001332 unsigned char renego_period[8] = { 0 };
1333#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +02001335 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 mbedtls_x509_crt cacert;
1337 mbedtls_x509_crt srvcert;
1338 mbedtls_pk_context pkey;
1339 mbedtls_x509_crt srvcert2;
1340 mbedtls_pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001341 int key_cert_init = 0, key_cert_init2 = 0;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001342#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001343 ssl_async_key_context_t ssl_async_keys;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001344#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001345#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1347 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001348#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#if defined(MBEDTLS_SSL_CACHE_C)
1350 mbedtls_ssl_cache_context cache;
Paul Bakker0a597072012-09-25 21:55:46 +00001351#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001352#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1353 mbedtls_ssl_ticket_context ticket_ctx;
1354#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001355#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001356 sni_entry *sni_info = NULL;
1357#endif
Hanno Beckere6706e62017-05-15 16:05:15 +01001358#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +01001359 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +01001360 const mbedtls_ecp_curve_info * curve_cur;
1361#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +01001363 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001364#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Simon Butchercce68be2018-07-23 14:26:09 +01001366 unsigned char alloc_buf[MEMORY_HEAP_SIZE];
Paul Bakker82024bf2013-07-04 11:52:32 +02001367#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001368
Hanno Beckera5a2b082019-05-15 14:03:01 +01001369#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1029ace2019-04-09 17:28:10 +01001370 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker96870292019-05-03 17:30:59 +01001371 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker1029ace2019-04-09 17:28:10 +01001372 size_t cid_len = 0;
Hanno Becker96870292019-05-03 17:30:59 +01001373 size_t cid_renego_len = 0;
Hanno Becker1029ace2019-04-09 17:28:10 +01001374#endif
1375
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001376 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001377 char *p, *q;
1378 const int *list;
1379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1381 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker82024bf2013-07-04 11:52:32 +02001382#endif
1383
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001384 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +02001385 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001386 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001387 mbedtls_net_init( &client_fd );
1388 mbedtls_net_init( &listen_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001389 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001390 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001391 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#if defined(MBEDTLS_X509_CRT_PARSE_C)
1393 mbedtls_x509_crt_init( &cacert );
1394 mbedtls_x509_crt_init( &srvcert );
1395 mbedtls_pk_init( &pkey );
1396 mbedtls_x509_crt_init( &srvcert2 );
1397 mbedtls_pk_init( &pkey2 );
Gilles Peskine4d9ec4d2018-04-26 14:33:43 +02001398#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
1399 memset( &ssl_async_keys, 0, sizeof( ssl_async_keys ) );
1400#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001401#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1403 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001404#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#if defined(MBEDTLS_SSL_CACHE_C)
1406 mbedtls_ssl_cache_init( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00001407#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001408#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1409 mbedtls_ssl_ticket_init( &ticket_ctx );
1410#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +02001412 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001413#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#if defined(MBEDTLS_SSL_COOKIE_C)
1415 mbedtls_ssl_cookie_init( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001416#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001417
Paul Bakkerc1283d32014-08-18 11:05:51 +02001418#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001419 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001420 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001421 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +02001422#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001423
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001424 if( argc == 0 )
1425 {
1426 usage:
1427 if( ret == 0 )
1428 ret = 1;
1429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 mbedtls_printf( USAGE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 list = mbedtls_ssl_list_ciphersuites();
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001433 while( *list )
1434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001436 list++;
1437 if( !*list )
1438 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001440 list++;
1441 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 mbedtls_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001443 goto exit;
1444 }
1445
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001446 opt.buffer_size = DFL_IO_BUF_LEN;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001447 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001448 opt.server_port = DFL_SERVER_PORT;
1449 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker16970d22017-10-10 15:56:37 +01001450 opt.event = DFL_EVENT;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001451 opt.response_size = DFL_RESPONSE_SIZE;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001452 opt.nbio = DFL_NBIO;
Hanno Becker1029ace2019-04-09 17:28:10 +01001453 opt.cid_enabled = DFL_CID_ENABLED;
Hanno Becker96870292019-05-03 17:30:59 +01001454 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
Hanno Becker1029ace2019-04-09 17:28:10 +01001455 opt.cid_val = DFL_CID_VALUE;
Hanno Becker96870292019-05-03 17:30:59 +01001456 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001457 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001458 opt.ca_file = DFL_CA_FILE;
1459 opt.ca_path = DFL_CA_PATH;
1460 opt.crt_file = DFL_CRT_FILE;
1461 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001462 opt.crt_file2 = DFL_CRT_FILE2;
1463 opt.key_file2 = DFL_KEY_FILE2;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001464 opt.async_operations = DFL_ASYNC_OPERATIONS;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001465 opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
1466 opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
1467 opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
Paul Bakkerfbb17802013-04-17 19:10:21 +02001468 opt.psk = DFL_PSK;
1469 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001470 opt.psk_list = DFL_PSK_LIST;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001471 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001472 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001473 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001474 opt.renegotiation = DFL_RENEGOTIATION;
1475 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001476 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001477 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001478 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001479 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001480 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001481 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001482 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001483 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001484 opt.auth_mode = DFL_AUTH_MODE;
Janos Follath4817e272017-04-10 13:44:33 +01001485 opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001486 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001487 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001488 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001489 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001490 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001491 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001492 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001493 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001494 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001495 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001496 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001497 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001498 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001499 opt.hs_to_min = DFL_HS_TO_MIN;
1500 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001501 opt.dtls_mtu = DFL_DTLS_MTU;
Hanno Beckere7675d02018-08-14 13:28:56 +01001502 opt.dgram_packing = DFL_DGRAM_PACKING;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001503 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001504 opt.extended_ms = DFL_EXTENDED_MS;
Jarno Lamsa41b35912019-06-10 15:51:11 +03001505 opt.enforce_extended_master_secret = DFL_EXTENDED_MS_ENFORCE;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001506 opt.etm = DFL_ETM;
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03001507 opt.serialize = DFL_SERIALIZE;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001508
1509 for( i = 1; i < argc; i++ )
1510 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001511 p = argv[i];
1512 if( ( q = strchr( p, '=' ) ) == NULL )
1513 goto usage;
1514 *q++ = '\0';
1515
1516 if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001517 opt.server_port = q;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001518 else if( strcmp( p, "server_addr" ) == 0 )
1519 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001520 else if( strcmp( p, "dtls" ) == 0 )
1521 {
1522 int t = atoi( q );
1523 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001525 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001527 else
1528 goto usage;
1529 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001530 else if( strcmp( p, "debug_level" ) == 0 )
1531 {
1532 opt.debug_level = atoi( q );
1533 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1534 goto usage;
1535 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001536 else if( strcmp( p, "nbio" ) == 0 )
1537 {
1538 opt.nbio = atoi( q );
1539 if( opt.nbio < 0 || opt.nbio > 2 )
1540 goto usage;
1541 }
Hanno Becker16970d22017-10-10 15:56:37 +01001542 else if( strcmp( p, "event" ) == 0 )
1543 {
1544 opt.event = atoi( q );
1545 if( opt.event < 0 || opt.event > 2 )
1546 goto usage;
1547 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001548 else if( strcmp( p, "read_timeout" ) == 0 )
1549 opt.read_timeout = atoi( q );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001550 else if( strcmp( p, "buffer_size" ) == 0 )
1551 {
1552 opt.buffer_size = atoi( q );
1553 if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
1554 goto usage;
1555 }
1556 else if( strcmp( p, "response_size" ) == 0 )
1557 {
1558 opt.response_size = atoi( q );
1559 if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
1560 goto usage;
1561 if( opt.buffer_size < opt.response_size )
1562 opt.buffer_size = opt.response_size;
1563 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001564 else if( strcmp( p, "ca_file" ) == 0 )
1565 opt.ca_file = q;
1566 else if( strcmp( p, "ca_path" ) == 0 )
1567 opt.ca_path = q;
1568 else if( strcmp( p, "crt_file" ) == 0 )
1569 opt.crt_file = q;
1570 else if( strcmp( p, "key_file" ) == 0 )
1571 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001572 else if( strcmp( p, "crt_file2" ) == 0 )
1573 opt.crt_file2 = q;
1574 else if( strcmp( p, "key_file2" ) == 0 )
1575 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001576 else if( strcmp( p, "dhm_file" ) == 0 )
1577 opt.dhm_file = q;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001578#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001579 else if( strcmp( p, "async_operations" ) == 0 )
1580 opt.async_operations = q;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001581 else if( strcmp( p, "async_private_delay1" ) == 0 )
1582 opt.async_private_delay1 = atoi( q );
1583 else if( strcmp( p, "async_private_delay2" ) == 0 )
1584 opt.async_private_delay2 = atoi( q );
1585 else if( strcmp( p, "async_private_error" ) == 0 )
1586 {
1587 int n = atoi( q );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01001588 if( n < -SSL_ASYNC_INJECT_ERROR_MAX ||
1589 n > SSL_ASYNC_INJECT_ERROR_MAX )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001590 {
1591 ret = 2;
1592 goto usage;
1593 }
1594 opt.async_private_error = n;
1595 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001596#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Hanno Beckera5a2b082019-05-15 14:03:01 +01001597#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1029ace2019-04-09 17:28:10 +01001598 else if( strcmp( p, "cid" ) == 0 )
1599 {
1600 opt.cid_enabled = atoi( q );
1601 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
1602 goto usage;
1603 }
Hanno Becker96870292019-05-03 17:30:59 +01001604 else if( strcmp( p, "cid_renego" ) == 0 )
1605 {
1606 opt.cid_enabled_renego = atoi( q );
1607 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
1608 goto usage;
1609 }
Hanno Becker1029ace2019-04-09 17:28:10 +01001610 else if( strcmp( p, "cid_val" ) == 0 )
1611 {
1612 opt.cid_val = q;
1613 }
Hanno Becker96870292019-05-03 17:30:59 +01001614 else if( strcmp( p, "cid_val_renego" ) == 0 )
1615 {
1616 opt.cid_val_renego = q;
1617 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001618#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001619 else if( strcmp( p, "psk" ) == 0 )
1620 opt.psk = q;
1621 else if( strcmp( p, "psk_identity" ) == 0 )
1622 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001623 else if( strcmp( p, "psk_list" ) == 0 )
1624 opt.psk_list = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001625 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1626 opt.ecjpake_pw = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001627 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001630
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001631 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001632 {
1633 ret = 2;
1634 goto usage;
1635 }
1636 opt.force_ciphersuite[1] = 0;
1637 }
Hanno Beckere6706e62017-05-15 16:05:15 +01001638 else if( strcmp( p, "curves" ) == 0 )
1639 opt.curves = q;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001640 else if( strcmp( p, "version_suites" ) == 0 )
1641 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001642 else if( strcmp( p, "renegotiation" ) == 0 )
1643 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001644 opt.renegotiation = (atoi( q )) ?
1645 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1646 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001647 }
1648 else if( strcmp( p, "allow_legacy" ) == 0 )
1649 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001650 switch( atoi( q ) )
1651 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001652 case -1:
1653 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1654 break;
1655 case 0:
1656 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1657 break;
1658 case 1:
1659 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1660 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001661 default: goto usage;
1662 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001663 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001664 else if( strcmp( p, "renegotiate" ) == 0 )
1665 {
1666 opt.renegotiate = atoi( q );
1667 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1668 goto usage;
1669 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001670 else if( strcmp( p, "renego_delay" ) == 0 )
1671 {
1672 opt.renego_delay = atoi( q );
1673 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001674 else if( strcmp( p, "renego_period" ) == 0 )
1675 {
Andres AG0b736db2017-02-08 14:05:57 +00001676#if defined(_MSC_VER)
1677 opt.renego_period = _strtoui64( q, NULL, 10 );
1678#else
1679 if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
1680 goto usage;
1681#endif /* _MSC_VER */
1682 if( opt.renego_period < 2 )
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001683 goto usage;
1684 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001685 else if( strcmp( p, "exchanges" ) == 0 )
1686 {
1687 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001688 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001689 goto usage;
1690 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001691 else if( strcmp( p, "min_version" ) == 0 )
1692 {
1693 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001695 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001697 else if( strcmp( q, "tls1_1" ) == 0 ||
1698 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001700 else if( strcmp( q, "tls1_2" ) == 0 ||
1701 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001703 else
1704 goto usage;
1705 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001706 else if( strcmp( p, "max_version" ) == 0 )
1707 {
1708 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001710 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001712 else if( strcmp( q, "tls1_1" ) == 0 ||
1713 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001715 else if( strcmp( q, "tls1_2" ) == 0 ||
1716 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001718 else
1719 goto usage;
1720 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001721 else if( strcmp( p, "arc4" ) == 0 )
1722 {
1723 switch( atoi( q ) )
1724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1726 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001727 default: goto usage;
1728 }
1729 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02001730 else if( strcmp( p, "allow_sha1" ) == 0 )
1731 {
1732 switch( atoi( q ) )
1733 {
1734 case 0: opt.allow_sha1 = 0; break;
1735 case 1: opt.allow_sha1 = 1; break;
1736 default: goto usage;
1737 }
1738 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001739 else if( strcmp( p, "force_version" ) == 0 )
1740 {
1741 if( strcmp( q, "ssl3" ) == 0 )
1742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1744 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001745 }
1746 else if( strcmp( q, "tls1" ) == 0 )
1747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1749 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001750 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001751 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1754 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001755 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001756 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1759 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001760 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001761 else if( strcmp( q, "dtls1" ) == 0 )
1762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1764 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1765 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001766 }
1767 else if( strcmp( q, "dtls1_2" ) == 0 )
1768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1770 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1771 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001772 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001773 else
1774 goto usage;
1775 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001776 else if( strcmp( p, "auth_mode" ) == 0 )
1777 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001778 if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
Paul Bakker91ebfb52012-11-23 14:04:08 +01001779 goto usage;
1780 }
Janos Follath4817e272017-04-10 13:44:33 +01001781 else if( strcmp( p, "cert_req_ca_list" ) == 0 )
1782 {
1783 opt.cert_req_ca_list = atoi( q );
1784 if( opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1 )
1785 goto usage;
1786 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001787 else if( strcmp( p, "max_frag_len" ) == 0 )
1788 {
1789 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001791 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001793 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001795 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001797 else
1798 goto usage;
1799 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001800 else if( strcmp( p, "alpn" ) == 0 )
1801 {
1802 opt.alpn_string = q;
1803 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001804 else if( strcmp( p, "trunc_hmac" ) == 0 )
1805 {
1806 switch( atoi( q ) )
1807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1809 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001810 default: goto usage;
1811 }
1812 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001813 else if( strcmp( p, "extended_ms" ) == 0 )
1814 {
1815 switch( atoi( q ) )
1816 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001817 case 0:
1818 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1819 break;
1820 case 1:
1821 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1822 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001823 default: goto usage;
1824 }
1825 }
Jarno Lamsa41b35912019-06-10 15:51:11 +03001826 else if( strcmp( p, "enforce_extended_master_secret" ) == 0 )
1827 {
1828 switch( atoi( q ) )
1829 {
1830 case 0:
1831 opt.enforce_extended_master_secret =
1832 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
1833 break;
1834 case 1:
1835 opt.enforce_extended_master_secret =
1836 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_ENABLED;
1837 break;
1838 default: goto usage;
1839 }
1840 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001841 else if( strcmp( p, "etm" ) == 0 )
1842 {
1843 switch( atoi( q ) )
1844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1846 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001847 default: goto usage;
1848 }
1849 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001850 else if( strcmp( p, "tickets" ) == 0 )
1851 {
1852 opt.tickets = atoi( q );
1853 if( opt.tickets < 0 || opt.tickets > 1 )
1854 goto usage;
1855 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001856 else if( strcmp( p, "ticket_timeout" ) == 0 )
1857 {
1858 opt.ticket_timeout = atoi( q );
1859 if( opt.ticket_timeout < 0 )
1860 goto usage;
1861 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001862 else if( strcmp( p, "cache_max" ) == 0 )
1863 {
1864 opt.cache_max = atoi( q );
1865 if( opt.cache_max < 0 )
1866 goto usage;
1867 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001868 else if( strcmp( p, "cache_timeout" ) == 0 )
1869 {
1870 opt.cache_timeout = atoi( q );
1871 if( opt.cache_timeout < 0 )
1872 goto usage;
1873 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001874 else if( strcmp( p, "cookies" ) == 0 )
1875 {
1876 opt.cookies = atoi( q );
1877 if( opt.cookies < -1 || opt.cookies > 1)
1878 goto usage;
1879 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001880 else if( strcmp( p, "anti_replay" ) == 0 )
1881 {
1882 opt.anti_replay = atoi( q );
1883 if( opt.anti_replay < 0 || opt.anti_replay > 1)
1884 goto usage;
1885 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001886 else if( strcmp( p, "badmac_limit" ) == 0 )
1887 {
1888 opt.badmac_limit = atoi( q );
1889 if( opt.badmac_limit < 0 )
1890 goto usage;
1891 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001892 else if( strcmp( p, "hs_timeout" ) == 0 )
1893 {
1894 if( ( p = strchr( q, '-' ) ) == NULL )
1895 goto usage;
1896 *p++ = '\0';
1897 opt.hs_to_min = atoi( q );
1898 opt.hs_to_max = atoi( p );
1899 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1900 goto usage;
1901 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001902 else if( strcmp( p, "mtu" ) == 0 )
1903 {
1904 opt.dtls_mtu = atoi( q );
1905 if( opt.dtls_mtu < 0 )
1906 goto usage;
1907 }
Hanno Beckere7675d02018-08-14 13:28:56 +01001908 else if( strcmp( p, "dgram_packing" ) == 0 )
1909 {
1910 opt.dgram_packing = atoi( q );
1911 if( opt.dgram_packing != 0 &&
1912 opt.dgram_packing != 1 )
1913 {
1914 goto usage;
1915 }
1916 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001917 else if( strcmp( p, "sni" ) == 0 )
1918 {
1919 opt.sni = q;
1920 }
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +01001921 else if( strcmp( p, "query_config" ) == 0 )
1922 {
1923 return query_config( q );
1924 }
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03001925 else if( strcmp( p, "serialize") == 0 )
1926 {
1927 opt.serialize = atoi( q );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03001928 if( opt.serialize < 0 || opt.serialize > 2)
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03001929 goto usage;
1930 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001931 else
1932 goto usage;
1933 }
1934
Hanno Becker16970d22017-10-10 15:56:37 +01001935 /* Event-driven IO is incompatible with the above custom
1936 * receive and send functions, as the polling builds on
1937 * refers to the underlying net_context. */
1938 if( opt.event == 1 && opt.nbio != 1 )
1939 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001940 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001941 opt.nbio = 1;
1942 }
1943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944#if defined(MBEDTLS_DEBUG_C)
1945 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001946#endif
Andrzej Kurekda4029d2018-06-20 07:07:55 -04001947 buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001948 if( buf == NULL )
1949 {
Andrzej Kurek755890f2018-06-20 08:17:04 -04001950 mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001951 ret = 3;
1952 goto exit;
1953 }
Paul Bakkerc73079a2014-04-25 16:34:30 +02001954
Paul Bakkerc1516be2013-06-29 16:01:32 +02001955 if( opt.force_ciphersuite[0] > 0 )
1956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001958 ciphersuite_info =
1959 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001960
Paul Bakker5b55b792013-07-19 13:43:43 +02001961 if( opt.max_version != -1 &&
1962 ciphersuite_info->min_minor_ver > opt.max_version )
1963 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001964 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker5b55b792013-07-19 13:43:43 +02001965 ret = 2;
1966 goto usage;
1967 }
1968 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001969 ciphersuite_info->max_minor_ver < opt.min_version )
1970 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001971 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001972 ret = 2;
1973 goto usage;
1974 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001975
1976 /* If we select a version that's not supported by
1977 * this suite, then there will be no common ciphersuite... */
1978 if( opt.max_version == -1 ||
1979 opt.max_version > ciphersuite_info->max_minor_ver )
1980 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001981 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001982 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001983 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001984 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001985 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001986 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1988 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1989 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001990 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001991
1992 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001998 ret = 2;
1999 goto usage;
2000 }
2001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002003 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002004 }
2005
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002006 if( opt.version_suites != NULL )
2007 {
2008 const char *name[4] = { 0 };
2009
2010 /* Parse 4-element coma-separated list */
2011 for( i = 0, p = (char *) opt.version_suites;
2012 i < 4 && *p != '\0';
2013 i++ )
2014 {
2015 name[i] = p;
2016
2017 /* Terminate the current string and move on to next one */
2018 while( *p != ',' && *p != '\0' )
2019 p++;
2020 if( *p == ',' )
2021 *p++ = '\0';
2022 }
2023
2024 if( i != 4 )
2025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 mbedtls_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002027 ret = 1;
2028 goto exit;
2029 }
2030
2031 memset( version_suites, 0, sizeof( version_suites ) );
2032
2033 /* Get the suites identifiers from their name */
2034 for( i = 0; i < 4; i++ )
2035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002037
2038 if( version_suites[i][0] == 0 )
2039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002041 ret = 2;
2042 goto usage;
2043 }
2044 }
2045 }
2046
Hanno Beckera5a2b082019-05-15 14:03:01 +01002047#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01002048 if( unhexify( cid, opt.cid_val, &cid_len ) != 0 )
2049 {
2050 mbedtls_printf( "CID not valid hex\n" );
2051 goto exit;
2052 }
Hanno Becker1029ace2019-04-09 17:28:10 +01002053
Hanno Becker96870292019-05-03 17:30:59 +01002054 /* Keep CID settings for renegotiation unless
2055 * specified otherwise. */
2056 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
2057 opt.cid_enabled_renego = opt.cid_enabled;
2058 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
2059 opt.cid_val_renego = opt.cid_val;
2060
2061 if( unhexify( cid_renego, opt.cid_val_renego, &cid_renego_len ) != 0 )
2062 {
2063 mbedtls_printf( "CID not valid hex\n" );
2064 goto exit;
2065 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002066#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1029ace2019-04-09 17:28:10 +01002067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002069 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002070 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02002071 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002072 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 mbedtls_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002075 goto exit;
2076 }
2077
2078 if( opt.psk_list != NULL )
2079 {
2080 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 mbedtls_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002083 goto exit;
2084 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002085 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002087
Hanno Beckere6706e62017-05-15 16:05:15 +01002088#if defined(MBEDTLS_ECP_C)
2089 if( opt.curves != NULL )
2090 {
2091 p = (char *) opt.curves;
2092 i = 0;
2093
2094 if( strcmp( p, "none" ) == 0 )
2095 {
2096 curve_list[0] = MBEDTLS_ECP_DP_NONE;
2097 }
2098 else if( strcmp( p, "default" ) != 0 )
2099 {
2100 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01002101 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002102 {
2103 q = p;
2104
2105 /* Terminate the current string */
2106 while( *p != ',' && *p != '\0' )
2107 p++;
2108 if( *p == ',' )
2109 *p++ = '\0';
2110
2111 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
2112 {
2113 curve_list[i++] = curve_cur->grp_id;
2114 }
2115 else
2116 {
2117 mbedtls_printf( "unknown curve %s\n", q );
2118 mbedtls_printf( "supported curves: " );
2119 for( curve_cur = mbedtls_ecp_curve_list();
2120 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
2121 curve_cur++ )
2122 {
2123 mbedtls_printf( "%s ", curve_cur->name );
2124 }
2125 mbedtls_printf( "\n" );
2126 goto exit;
2127 }
2128 }
2129
2130 mbedtls_printf("Number of curves: %d\n", i );
2131
Hanno Becker8651a432017-06-09 16:13:22 +01002132 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002133 {
Hanno Becker8651a432017-06-09 16:13:22 +01002134 mbedtls_printf( "curves list too long, maximum %d",
2135 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01002136 goto exit;
2137 }
2138
2139 curve_list[i] = MBEDTLS_ECP_DP_NONE;
2140 }
2141 }
2142#endif /* MBEDTLS_ECP_C */
2143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002145 if( opt.alpn_string != NULL )
2146 {
2147 p = (char *) opt.alpn_string;
2148 i = 0;
2149
2150 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01002151 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002152 {
2153 alpn_list[i++] = p;
2154
2155 /* Terminate the current string and move on to next one */
2156 while( *p != ',' && *p != '\0' )
2157 p++;
2158 if( *p == ',' )
2159 *p++ = '\0';
2160 }
2161 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002163
Paul Bakkerfbb17802013-04-17 19:10:21 +02002164 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002165 * 0. Initialize the RNG and the session data
2166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002168 fflush( stdout );
2169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 mbedtls_entropy_init( &entropy );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002171 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
2172 &entropy, (const unsigned char *) pers,
2173 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002174 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002175 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
2176 -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002177 goto exit;
2178 }
2179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002183 /*
2184 * 1.1. Load the trusted CA
2185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002187 fflush( stdout );
2188
Hanno Becker7ae36e42019-03-05 16:22:07 +00002189 if( strcmp( opt.ca_path, "none" ) == 0 ||
2190 strcmp( opt.ca_file, "none" ) == 0 )
2191 {
2192 ret = 0;
2193 }
2194 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002196 if( strlen( opt.ca_path ) )
Hanno Becker7ae36e42019-03-05 16:22:07 +00002197 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002198 else if( strlen( opt.ca_file ) )
Hanno Becker7ae36e42019-03-05 16:22:07 +00002199 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02002200 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002201#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202#if defined(MBEDTLS_CERTS_C)
Hanno Becker38566cc2019-02-01 08:13:19 +00002203 {
2204#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 ret = mbedtls_x509_crt_parse( &cacert,
2208 (const unsigned char *) mbedtls_test_cas[i],
2209 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002210 if( ret != 0 )
2211 break;
2212 }
Hanno Becker38566cc2019-02-01 08:13:19 +00002213 if( ret == 0 )
2214#endif /* MBEDTLS_PEM_PARSE_C */
2215 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
2216 {
2217 ret = mbedtls_x509_crt_parse_der( &cacert,
2218 (const unsigned char *) mbedtls_test_cas_der[i],
2219 mbedtls_test_cas_der_len[i] );
2220 if( ret != 0 )
2221 break;
2222 }
2223 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002224#else
2225 {
2226 ret = 1;
Hanno Beckerc258c442018-12-05 16:49:42 +00002227 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002228 }
Hanno Becker38566cc2019-02-01 08:13:19 +00002229#endif /* MBEDTLS_CERTS_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002230 if( ret < 0 )
2231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002233 goto exit;
2234 }
2235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002237
2238 /*
2239 * 1.2. Load own certificate and private key
2240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002242 fflush( stdout );
2243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002245 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002246 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002247 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002251 -ret );
2252 goto exit;
2253 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002254 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002255 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002256 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002257 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002261 goto exit;
2262 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002263 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002264 if( key_cert_init == 1 )
2265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002267 goto exit;
2268 }
2269
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002270 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002271 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002272 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002276 -ret );
2277 goto exit;
2278 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002279 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002280 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002281 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002282 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002286 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002287 goto exit;
2288 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002289 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002290 if( key_cert_init2 == 1 )
2291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002293 goto exit;
2294 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002295#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002296 if( key_cert_init == 0 &&
2297 strcmp( opt.crt_file, "none" ) != 0 &&
2298 strcmp( opt.key_file, "none" ) != 0 &&
2299 key_cert_init2 == 0 &&
2300 strcmp( opt.crt_file2, "none" ) != 0 &&
2301 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303#if !defined(MBEDTLS_CERTS_C)
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002304 mbedtls_printf( "Not certificated or key provided, and \nMBEDTLS_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002305 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002306#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307#if defined(MBEDTLS_RSA_C)
2308 if( ( ret = mbedtls_x509_crt_parse( &srvcert,
2309 (const unsigned char *) mbedtls_test_srv_crt_rsa,
2310 mbedtls_test_srv_crt_rsa_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002311 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002312 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
2313 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002314 goto exit;
2315 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 if( ( ret = mbedtls_pk_parse_key( &pkey,
2317 (const unsigned char *) mbedtls_test_srv_key_rsa,
2318 mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002319 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002320 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
2321 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002322 goto exit;
2323 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002324 key_cert_init = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325#endif /* MBEDTLS_RSA_C */
2326#if defined(MBEDTLS_ECDSA_C)
2327 if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
2328 (const unsigned char *) mbedtls_test_srv_crt_ec,
2329 mbedtls_test_srv_crt_ec_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002330 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002331 mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n",
2332 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002333 goto exit;
2334 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 if( ( ret = mbedtls_pk_parse_key( &pkey2,
2336 (const unsigned char *) mbedtls_test_srv_key_ec,
2337 mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002338 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002339 mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n",
2340 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002341 goto exit;
2342 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002343 key_cert_init2 = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344#endif /* MBEDTLS_ECDSA_C */
2345#endif /* MBEDTLS_CERTS_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002346 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 mbedtls_printf( " ok\n" );
2349#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002352 if( opt.dhm_file != NULL )
2353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 mbedtls_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002355 fflush( stdout );
2356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002360 -ret );
2361 goto exit;
2362 }
2363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002365 }
2366#endif
2367
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002368#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002369 if( opt.sni != NULL )
2370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 mbedtls_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002372 fflush( stdout );
2373
2374 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
2375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002377 goto exit;
2378 }
2379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002381 }
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002382#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002383
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002384 /*
2385 * 2. Setup the listening TCP socket
2386 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02002387 mbedtls_printf( " . Bind on %s://%s:%s/ ...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002389 opt.server_addr ? opt.server_addr : "*",
2390 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002391 fflush( stdout );
2392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
2394 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2395 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002398 goto exit;
2399 }
2400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002402
2403 /*
2404 * 3. Setup stuff
2405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002407 fflush( stdout );
2408
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02002409 if( ( ret = mbedtls_ssl_config_defaults( &conf,
2410 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02002411 opt.transport,
2412 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002413 {
2414 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
2415 goto exit;
2416 }
2417
Gilles Peskineef86ab22017-05-05 18:59:02 +02002418#if defined(MBEDTLS_X509_CRT_PARSE_C)
2419 /* The default algorithms profile disables SHA-1, but our tests still
2420 rely on it heavily. Hence we allow it here. A real-world server
2421 should use the default profile unless there is a good reason not to. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02002422 if( opt.allow_sha1 > 0 )
2423 {
2424 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
2425 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
Gilles Peskine682df092017-05-11 19:01:11 +02002426 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
Gilles Peskinebc70a182017-05-09 15:59:24 +02002427 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02002428#endif /* MBEDTLS_X509_CRT_PARSE_C */
2429
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01002430 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002431 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002432
Janos Follath4817e272017-04-10 13:44:33 +01002433 if( opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST )
2434 mbedtls_ssl_conf_cert_req_ca_list( &conf, opt.cert_req_ca_list );
2435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002437 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 +02002438 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002439
Hanno Beckere7675d02018-08-14 13:28:56 +01002440 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01002441 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002445 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002446 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002447 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002448 goto exit;
2449 };
Paul Bakker05decb22013-08-15 13:33:48 +02002450#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002451
Hanno Beckera5a2b082019-05-15 14:03:01 +01002452#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01002453 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckereec2be92019-05-03 13:06:44 +01002454 {
Hanno Becker96870292019-05-03 17:30:59 +01002455 if( opt.cid_enabled == 1 &&
2456 opt.cid_enabled_renego == 1 &&
2457 cid_len != cid_renego_len )
2458 {
2459 mbedtls_printf( "CID length must not change during renegotiation\n" );
2460 goto usage;
2461 }
2462
2463 if( opt.cid_enabled == 1 )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002464 ret = mbedtls_ssl_conf_cid( &conf, cid_len,
2465 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Becker96870292019-05-03 17:30:59 +01002466 else
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002467 ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
2468 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Becker96870292019-05-03 17:30:59 +01002469
Hanno Beckereec2be92019-05-03 13:06:44 +01002470 if( ret != 0 )
2471 {
Hanno Becker76581052019-05-23 16:58:22 +01002472 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
2473 -ret );
Hanno Beckereec2be92019-05-03 13:06:44 +01002474 goto exit;
2475 }
2476 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002477#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckereec2be92019-05-03 13:06:44 +01002478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002480 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002481 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002482#endif
2483
Hanno Beckerf765ce62019-06-21 13:17:14 +01002484#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
2485 !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET) && \
2486 !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002487 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002488 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Jarno Lamsa41b35912019-06-10 15:51:11 +03002489 if( opt.enforce_extended_master_secret != DFL_EXTENDED_MS_ENFORCE )
2490 mbedtls_ssl_conf_extended_master_secret_enforce( &conf,
2491 opt.enforce_extended_master_secret );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002492#endif
2493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002495 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002496 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002497#endif
2498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002500 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002501 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002502 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002503 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002504 goto exit;
2505 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002506#endif
2507
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002508 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
2509 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002512 if( opt.cache_max != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002514
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002515 if( opt.cache_timeout != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516 mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002517
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002518 mbedtls_ssl_conf_session_cache( &conf, &cache,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01002519 mbedtls_ssl_cache_get,
2520 mbedtls_ssl_cache_set );
Paul Bakker0a597072012-09-25 21:55:46 +00002521#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002524 if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002525 {
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002526 if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
2527 mbedtls_ctr_drbg_random, &ctr_drbg,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +02002528 MBEDTLS_CIPHER_AES_256_GCM,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002529 opt.ticket_timeout ) ) != 0 )
2530 {
2531 mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
2532 goto exit;
2533 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01002534
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002535 mbedtls_ssl_conf_session_tickets_cb( &conf,
2536 mbedtls_ssl_ticket_write,
2537 mbedtls_ssl_ticket_parse,
2538 &ticket_ctx );
2539 }
Paul Bakkera503a632013-08-14 13:48:06 +02002540#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542#if defined(MBEDTLS_SSL_PROTO_DTLS)
2543 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002546 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
2549 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002552 goto exit;
2553 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002554
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002555 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002556 &cookie_ctx );
2557 }
2558 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559#endif /* MBEDTLS_SSL_COOKIE_C */
2560#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002561 if( opt.cookies == 0 )
2562 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002563 mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002564 }
2565 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002567 {
2568 ; /* Nothing to do */
2569 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002572 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002573 mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002574#endif
2575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002577 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002578 mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002579#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002580 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002582
Paul Bakker41c83d32013-03-20 14:39:14 +01002583 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002584 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002585
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002586#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002587 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002588 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002589#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002590
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002591 if( opt.version_suites != NULL )
2592 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002593 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 MBEDTLS_SSL_MAJOR_VERSION_3,
2595 MBEDTLS_SSL_MINOR_VERSION_0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002596 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 MBEDTLS_SSL_MAJOR_VERSION_3,
2598 MBEDTLS_SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002599 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 MBEDTLS_SSL_MAJOR_VERSION_3,
2601 MBEDTLS_SSL_MINOR_VERSION_2 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002602 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603 MBEDTLS_SSL_MAJOR_VERSION_3,
2604 MBEDTLS_SSL_MINOR_VERSION_3 );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002605 }
2606
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002607 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002608 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002610 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002611
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02002612 if( opt.renego_delay != DFL_RENEGO_DELAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002613 mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002614
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002615 if( opt.renego_period != DFL_RENEGO_PERIOD )
2616 {
Andres AG692ad842017-01-19 16:30:57 +00002617 PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002618 mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002619 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002620#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002623 if( strcmp( opt.ca_path, "none" ) != 0 &&
2624 strcmp( opt.ca_file, "none" ) != 0 )
2625 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002626 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002627 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002628 if( key_cert_init )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002629 {
2630 mbedtls_pk_context *pk = &pkey;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002631#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002632 if( opt.async_private_delay1 >= 0 )
2633 {
Gilles Peskine44817442018-06-13 18:09:28 +02002634 ret = ssl_async_set_key( &ssl_async_keys, &srvcert, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002635 opt.async_private_delay1 );
2636 if( ret < 0 )
2637 {
2638 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2639 ret );
2640 goto exit;
2641 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002642 pk = NULL;
2643 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002644#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002645 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002646 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002647 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002648 goto exit;
2649 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002650 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002651 if( key_cert_init2 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002652 {
2653 mbedtls_pk_context *pk = &pkey2;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002654#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002655 if( opt.async_private_delay2 >= 0 )
2656 {
Gilles Peskine44817442018-06-13 18:09:28 +02002657 ret = ssl_async_set_key( &ssl_async_keys, &srvcert2, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002658 opt.async_private_delay2 );
2659 if( ret < 0 )
2660 {
2661 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2662 ret );
2663 goto exit;
2664 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002665 pk = NULL;
2666 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002667#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002668 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002669 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002670 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002671 goto exit;
2672 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002673 }
2674
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002675#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002676 if( opt.async_operations[0] != '-' )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002677 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002678 mbedtls_ssl_async_sign_t *sign = NULL;
2679 mbedtls_ssl_async_decrypt_t *decrypt = NULL;
Gilles Peskine12ab5d42018-04-24 12:32:04 +02002680 const char *r;
2681 for( r = opt.async_operations; *r; r++ )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002682 {
Gilles Peskine12ab5d42018-04-24 12:32:04 +02002683 switch( *r )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002684 {
2685 case 'd':
2686 decrypt = ssl_async_decrypt;
2687 break;
2688 case 's':
2689 sign = ssl_async_sign;
2690 break;
2691 }
2692 }
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002693 ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
2694 - opt.async_private_error :
2695 opt.async_private_error );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002696 ssl_async_keys.f_rng = mbedtls_ctr_drbg_random;
2697 ssl_async_keys.p_rng = &ctr_drbg;
2698 mbedtls_ssl_conf_async_private_cb( &conf,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002699 sign,
2700 decrypt,
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002701 ssl_async_resume,
2702 ssl_async_cancel,
2703 &ssl_async_keys );
2704 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002705#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002706#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +02002707
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002708#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002709 if( opt.sni != NULL )
Gilles Peskine166ce742018-04-30 10:30:49 +02002710 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002711 mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
Gilles Peskine166ce742018-04-30 10:30:49 +02002712#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
2713 if( opt.async_private_delay2 >= 0 )
2714 {
Gilles Peskinee2479892018-06-13 18:06:51 +02002715 sni_entry *cur;
2716 for( cur = sni_info; cur != NULL; cur = cur->next )
Gilles Peskine166ce742018-04-30 10:30:49 +02002717 {
Gilles Peskinee2479892018-06-13 18:06:51 +02002718 ret = ssl_async_set_key( &ssl_async_keys,
Gilles Peskine44817442018-06-13 18:09:28 +02002719 cur->cert, cur->key, 1,
Gilles Peskinee2479892018-06-13 18:06:51 +02002720 opt.async_private_delay2 );
2721 if( ret < 0 )
2722 {
2723 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2724 ret );
2725 goto exit;
2726 }
2727 cur->key = NULL;
Gilles Peskine166ce742018-04-30 10:30:49 +02002728 }
Gilles Peskine166ce742018-04-30 10:30:49 +02002729 }
2730#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
2731 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002732#endif
2733
Hanno Beckere6706e62017-05-15 16:05:15 +01002734#if defined(MBEDTLS_ECP_C)
2735 if( opt.curves != NULL &&
2736 strcmp( opt.curves, "default" ) != 0 )
2737 {
2738 mbedtls_ssl_conf_curves( &conf, curve_list );
2739 }
2740#endif
2741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002743 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
2744 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002745 ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002746 (const unsigned char *) opt.psk_identity,
2747 strlen( opt.psk_identity ) );
2748 if( ret != 0 )
2749 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002750 mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002751 goto exit;
2752 }
2753 }
2754
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002755 if( opt.psk_list != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002756 mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02002757#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759#if defined(MBEDTLS_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00002760 /*
2761 * Use different group than default DHM group
2762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002764 if( opt.dhm_file != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002765 ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002766#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002767 if( ret != 0 )
2768 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002769 mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002770 goto exit;
2771 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002772#endif
2773
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002774 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002775 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00002776
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002777 if( opt.max_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002778 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002779
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002780 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
2781 {
2782 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
2783 goto exit;
2784 }
2785
2786 if( opt.nbio == 2 )
2787 mbedtls_ssl_set_bio( &ssl, &client_fd, my_send, my_recv, NULL );
2788 else
2789 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02002790 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002791
Hanno Beckera5a2b082019-05-15 14:03:01 +01002792#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1029ace2019-04-09 17:28:10 +01002793 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2794 {
2795 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
2796 cid, cid_len ) ) != 0 )
2797 {
2798 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2799 ret );
2800 goto exit;
2801 }
2802 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002803#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1029ace2019-04-09 17:28:10 +01002804
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002805#if defined(MBEDTLS_SSL_PROTO_DTLS)
2806 if( opt.dtls_mtu != DFL_DTLS_MTU )
2807 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
2808#endif
2809
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002810#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002811 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
2812 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002813#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002816
2817reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002818#if !defined(_WIN32)
2819 if( received_sigterm )
2820 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01002821 mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" );
2822 if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT )
2823 ret = 0;
2824
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002825 goto exit;
2826 }
2827#endif
2828
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002829 if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
2830 {
2831 mbedtls_printf( " ! Client initiated reconnection from same port\n" );
2832 goto handshake;
2833 }
2834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002836 if( ret != 0 )
2837 {
2838 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 mbedtls_strerror( ret, error_buf, 100 );
2840 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002841 }
2842#endif
2843
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002844 mbedtls_net_free( &client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 mbedtls_ssl_session_reset( &ssl );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002847
2848 /*
2849 * 3. Wait until a client connects
2850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 mbedtls_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002852 fflush( stdout );
2853
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002854 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02002855 client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002856 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02002857#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002858 if( received_sigterm )
2859 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01002860 mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" );
2861 if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED )
2862 ret = 0;
2863
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002864 goto exit;
2865 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02002866#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868 mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002869 goto exit;
2870 }
2871
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002872 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002873 ret = mbedtls_net_set_nonblock( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002874 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002875 ret = mbedtls_net_set_block( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002876 if( ret != 0 )
2877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002879 goto exit;
2880 }
2881
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002882 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2885 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002886 {
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02002887 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
2888 client_ip, cliip_len ) ) != 0 )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002889 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002890 mbedtls_printf( " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n",
2891 -ret );
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002892 goto exit;
2893 }
2894 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002896
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002897#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2898 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
2899 {
2900 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
2901 (const unsigned char *) opt.ecjpake_pw,
2902 strlen( opt.ecjpake_pw ) ) ) != 0 )
2903 {
2904 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
2905 goto exit;
2906 }
2907 }
2908#endif
2909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002911
2912 /*
2913 * 4. Handshake
2914 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002915handshake:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002917 fflush( stdout );
2918
Hanno Becker16970d22017-10-10 15:56:37 +01002919 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002920 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002921#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002922 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002923 ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002924 {
2925 mbedtls_printf( " cancelling on injected error\n" );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002926 break;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002927 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002928#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskineb44692f2018-04-24 12:18:19 +02002929
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02002930 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01002931 break;
2932
2933 /* For event-driven IO, wait for socket to become available */
2934 if( opt.event == 1 /* level triggered IO */ )
2935 {
2936#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002937 ret = idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002938#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002939 ret = idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002940#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002941 if( ret != 0 )
2942 goto reset;
Hanno Becker16970d22017-10-10 15:56:37 +01002943 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002944 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948 mbedtls_printf( " hello verification requested\n" );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002949 ret = 0;
2950 goto reset;
2951 }
2952 else if( ret != 0 )
2953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002955
Hanno Becker02a21932019-06-10 15:08:43 +01002956#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002957 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2958 {
2959 char vrfy_buf[512];
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02002960 flags = mbedtls_ssl_get_verify_result( &ssl );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002961
2962 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
2963
2964 mbedtls_printf( "%s\n", vrfy_buf );
2965 }
2966#endif
2967
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002968#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002969 if( opt.async_private_error < 0 )
2970 /* Injected error only the first time round, to test reset */
2971 ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
2972#endif
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002973 goto reset;
2974 }
2975 else /* ret == 0 */
2976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
2978 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002979 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
2982 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002983 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002985
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002986#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2987 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
2988 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
2989#endif
2990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002992 if( opt.alpn_string != NULL )
2993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
2995 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002996 alp ? alp : "(none)" );
2997 }
2998#endif
2999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003001 /*
Ron Eldor2a47be52017-06-20 15:23:23 +03003002 * 5. Verify the client certificate
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003003 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003005
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003006 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003007 {
Hanno Becker02a21932019-06-10 15:08:43 +01003008#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003009 char vrfy_buf[512];
Peter Kolbusdc470ae2018-12-11 13:55:56 -06003010#endif
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 mbedtls_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003013
Hanno Becker02a21932019-06-10 15:08:43 +01003014#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003015 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003016
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003017 mbedtls_printf( "%s\n", vrfy_buf );
Peter Kolbusdc470ae2018-12-11 13:55:56 -06003018#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003019 }
3020 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003022
Hanno Becker02a21932019-06-10 15:08:43 +01003023#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003024 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003025 {
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003026 char crt_buf[512];
3027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028 mbedtls_printf( " . Peer certificate information ...\n" );
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003029 mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030 mbedtls_ssl_get_peer_cert( &ssl ) );
Manuel Pégourié-Gonnard67557172015-07-02 11:15:48 +02003031 mbedtls_printf( "%s\n", crt_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003032 }
Hanno Becker02a21932019-06-10 15:08:43 +01003033#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003035
Hanno Beckera5a2b082019-05-15 14:03:01 +01003036#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01003037 ret = report_cid_usage( &ssl, "initial handshake" );
3038 if( ret != 0 )
3039 goto exit;
3040
Hanno Becker1029ace2019-04-09 17:28:10 +01003041 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3042 {
Hanno Becker96870292019-05-03 17:30:59 +01003043 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
3044 cid_renego, cid_renego_len ) ) != 0 )
Hanno Becker1029ace2019-04-09 17:28:10 +01003045 {
Hanno Becker96870292019-05-03 17:30:59 +01003046 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
3047 ret );
Hanno Becker1029ace2019-04-09 17:28:10 +01003048 goto exit;
3049 }
Hanno Becker1029ace2019-04-09 17:28:10 +01003050 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003051#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1029ace2019-04-09 17:28:10 +01003052
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02003053 if( opt.exchanges == 0 )
3054 goto close_notify;
3055
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003056 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003057data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003058 /*
3059 * 6. Read the HTTP Request
3060 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 mbedtls_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003062 fflush( stdout );
3063
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003064 /*
3065 * TLS and DTLS need different reading styles (stream vs datagram)
3066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003068 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003069 do
3070 {
3071 int terminated = 0;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003072 len = opt.buffer_size - 1;
3073 memset( buf, 0, opt.buffer_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003075
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003076 if( mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003077 {
3078 if( opt.event == 1 /* level triggered IO */ )
3079 {
3080#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003081 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003082#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003083 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003084#endif
3085 }
3086
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003087 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01003088 }
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003089
3090 if( ret <= 0 )
3091 {
3092 switch( ret )
3093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3095 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003096 goto close_notify;
3097
3098 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 case MBEDTLS_ERR_NET_CONN_RESET:
3100 mbedtls_printf( " connection was reset by peer\n" );
3101 ret = MBEDTLS_ERR_NET_CONN_RESET;
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003102 goto reset;
3103
3104 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003106 goto reset;
3107 }
3108 }
3109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003111 {
3112 len = ret;
3113 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003115
3116 /* End of message should be detected according to the syntax of the
3117 * application protocol (eg HTTP), just use a dummy test here. */
3118 if( buf[len - 1] == '\n' )
3119 terminated = 1;
3120 }
3121 else
3122 {
3123 int extra_len, ori_len;
3124 unsigned char *larger_buf;
3125
3126 ori_len = ret;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02003127 extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003128
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003129 larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003130 if( larger_buf == NULL )
3131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 mbedtls_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003133 ret = 1;
3134 goto reset;
3135 }
3136
3137 memset( larger_buf, 0, ori_len + extra_len );
3138 memcpy( larger_buf, buf, ori_len );
3139
3140 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003142 if( ret != extra_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143 mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145 mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003146 ret = 1;
3147 goto reset;
3148 }
3149
3150 larger_buf[ori_len + extra_len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151 mbedtls_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003152 ori_len + extra_len, ori_len, extra_len,
3153 (char *) larger_buf );
3154
3155 /* End of message should be detected according to the syntax of the
3156 * application protocol (eg HTTP), just use a dummy test here. */
3157 if( larger_buf[ori_len + extra_len - 1] == '\n' )
3158 terminated = 1;
3159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160 mbedtls_free( larger_buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003161 }
3162
3163 if( terminated )
3164 {
3165 ret = 0;
3166 break;
3167 }
3168 }
3169 while( 1 );
3170 }
3171 else /* Not stream, so datagram */
3172 {
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003173 len = opt.buffer_size - 1;
3174 memset( buf, 0, opt.buffer_size );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003175
Gilles Peskineb44692f2018-04-24 12:18:19 +02003176 do
Hanno Becker16970d22017-10-10 15:56:37 +01003177 {
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003178 /* Without the call to `mbedtls_ssl_check_pending`, it might
3179 * happen that the client sends application data in the same
3180 * datagram as the Finished message concluding the handshake.
3181 * In this case, the application data would be ready to be
3182 * processed while the underlying transport wouldn't signal
3183 * any further incoming data.
3184 *
3185 * See the test 'Event-driven I/O: session-id resume, UDP packing'
3186 * in tests/ssl-opt.sh.
3187 */
3188
3189 /* For event-driven IO, wait for socket to become available */
3190 if( mbedtls_ssl_check_pending( &ssl ) == 0 &&
3191 opt.event == 1 /* level triggered IO */ )
3192 {
3193#if defined(MBEDTLS_TIMING_C)
3194 idle( &client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ );
3195#else
3196 idle( &client_fd, MBEDTLS_ERR_SSL_WANT_READ );
3197#endif
3198 }
3199
Hanno Becker16970d22017-10-10 15:56:37 +01003200 ret = mbedtls_ssl_read( &ssl, buf, len );
3201
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003202 /* Note that even if `mbedtls_ssl_check_pending` returns true,
3203 * it can happen that the subsequent call to `mbedtls_ssl_read`
3204 * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
3205 * might be discarded (e.g. because they are retransmissions). */
Hanno Becker16970d22017-10-10 15:56:37 +01003206 }
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003207 while( mbedtls_status_is_ssl_in_progress( ret ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003208
3209 if( ret <= 0 )
3210 {
3211 switch( ret )
3212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3214 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003215 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003216 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003217
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003218 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003220 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003221 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003222 }
3223
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003224 len = ret;
3225 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003227 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003228 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003229
3230 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003231 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02003232 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00003235 if( opt.renegotiate && exchanges_left == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237 mbedtls_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003238 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003241 {
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003242 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003245 goto reset;
3246 }
Hanno Becker16970d22017-10-10 15:56:37 +01003247
3248 /* For event-driven IO, wait for socket to become available */
3249 if( opt.event == 1 /* level triggered IO */ )
3250 {
3251#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003252 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003253#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003254 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003255#endif
3256 }
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003257 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003260 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003262
Hanno Beckera5a2b082019-05-15 14:03:01 +01003263#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01003264 ret = report_cid_usage( &ssl, "after renegotiation" );
3265 if( ret != 0 )
3266 goto exit;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003267#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker96870292019-05-03 17:30:59 +01003268
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003269 /*
3270 * 7. Write the 200 Response
3271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272 mbedtls_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003273 fflush( stdout );
3274
3275 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003277
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003278 /* Add padding to the response to reach opt.response_size in length */
3279 if( opt.response_size != DFL_RESPONSE_SIZE &&
3280 len < opt.response_size )
3281 {
3282 memset( buf + len, 'B', opt.response_size - len );
3283 len += opt.response_size - len;
3284 }
3285
3286 /* Truncate if response size is smaller than the "natural" size */
3287 if( opt.response_size != DFL_RESPONSE_SIZE &&
3288 len > opt.response_size )
3289 {
3290 len = opt.response_size;
3291
3292 /* Still end with \r\n unless that's really not possible */
3293 if( len >= 2 ) buf[len - 2] = '\r';
3294 if( len >= 1 ) buf[len - 1] = '\n';
3295 }
3296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003298 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003299 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003302 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003307 goto reset;
3308 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003309
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003310 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003313 goto reset;
3314 }
Hanno Becker16970d22017-10-10 15:56:37 +01003315
3316 /* For event-driven IO, wait for socket to become available */
3317 if( opt.event == 1 /* level triggered IO */ )
3318 {
3319#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003320 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003321#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003322 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003323#endif
3324 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003325 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003326 }
3327 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003328 else /* Not stream, so datagram */
3329 {
Hanno Becker16970d22017-10-10 15:56:37 +01003330 while( 1 )
3331 {
3332 ret = mbedtls_ssl_write( &ssl, buf, len );
3333
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003334 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003335 break;
3336
3337 /* For event-driven IO, wait for socket to become available */
3338 if( opt.event == 1 /* level triggered IO */ )
3339 {
3340#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003341 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003342#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003343 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003344#endif
3345 }
3346 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003347
3348 if( ret < 0 )
3349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003351 goto reset;
3352 }
3353
3354 frags = 1;
3355 written = ret;
3356 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003357
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003358 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 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 +01003360 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01003361
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003362 /*
Jarno Lamsaf4572932019-05-29 15:41:21 +03003363 * 7b. Simulate serialize/deserialize and go back to data exchange
3364 */
Jarno Lamsacf1b6722019-06-04 11:06:31 +03003365#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003366 if( opt.serialize != 0 )
Jarno Lamsaf4572932019-05-29 15:41:21 +03003367 {
Jarno Lamsa034ae842019-06-06 15:10:07 +03003368 size_t buf_len;
3369 unsigned char *context_buf = NULL;
Jarno Lamsaf4572932019-05-29 15:41:21 +03003370
3371 opt.serialize = 0;
3372 mbedtls_printf( " Serializing live connection..." );
3373
Jarno Lamsa034ae842019-06-06 15:10:07 +03003374 ret = mbedtls_ssl_context_save( &ssl, NULL, 0, &buf_len );
Jarno Lamsaf4f8ed72019-06-04 16:03:28 +03003375
3376 /* Allow stub implementation returning 0 for now */
3377 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL &&
3378 ret != 0 )
Jarno Lamsaf4572932019-05-29 15:41:21 +03003379 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003380 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
3381 "-0x%x\n\n", -ret );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003382
3383 goto exit;
3384 }
3385
Jarno Lamsa034ae842019-06-06 15:10:07 +03003386 if( ( context_buf = mbedtls_calloc( 1, buf_len ) ) == NULL )
Jarno Lamsaf4572932019-05-29 15:41:21 +03003387 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003388 mbedtls_printf( " failed\n ! Couldn't allocate buffer for "
3389 "serialized context" );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003390
3391 goto exit;
3392 }
3393
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03003394 if( ( ret = mbedtls_ssl_context_save( &ssl, context_buf,
3395 buf_len, &buf_len ) ) != 0 )
Jarno Lamsaf4572932019-05-29 15:41:21 +03003396 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003397 mbedtls_printf( "failed\n ! mbedtls_ssl_context_save returned "
3398 "-0x%x\n\n", -ret );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003399
3400 goto exit;
3401 }
3402
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003403 if( opt.serialize == 2 )
3404 {
3405 mbedtls_ssl_free( &ssl );
3406
3407 mbedtls_ssl_init( &ssl );
3408
3409 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
3410 {
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03003411 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned "
3412 "-0x%x\n\n", -ret );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003413 goto exit;
3414 }
3415
3416 if( opt.nbio == 2 )
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03003417 mbedtls_ssl_set_bio( &ssl, &client_fd, my_send, my_recv,
3418 NULL );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003419 else
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03003420 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send,
3421 mbedtls_net_recv,
3422 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003423
Jarno Lamsa29a15c22019-06-13 11:45:06 +03003424#if defined(MBEDTLS_TIMING_C)
3425 if( opt.nbio != 0 && opt.read_timeout != 0 )
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03003426 mbedtls_ssl_set_timer_cb( &ssl, &timer,
3427 mbedtls_timing_set_delay,
3428 mbedtls_timing_get_delay );
Jarno Lamsa29a15c22019-06-13 11:45:06 +03003429#endif /* MBEDTLS_TIMING_C */
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003430 }
3431
Jarno Lamsaf4572932019-05-29 15:41:21 +03003432 mbedtls_printf( " Deserializing connection..." );
3433
Jarno Lamsa8b2608b2019-06-13 12:22:50 +03003434 if( ( ret = mbedtls_ssl_context_load( &ssl, context_buf,
3435 buf_len ) ) != 0 )
Jarno Lamsaf4572932019-05-29 15:41:21 +03003436 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003437 mbedtls_printf( "failed\n ! mbedtls_ssl_context_load returned "
3438 "-0x%x\n\n", -ret );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003439
3440 goto exit;
3441 }
3442 }
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003443#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jarno Lamsaf4572932019-05-29 15:41:21 +03003444
3445 /*
3446 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003447 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003448 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003449 goto data_exchange;
3450
3451 /*
3452 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003453 */
3454close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01003456
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003457 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003459 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003460 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462 mbedtls_printf( " done\n" );
Rich Evansf90016a2015-01-19 14:26:37 +00003463
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003464 goto reset;
3465
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003466 /*
3467 * Cleanup and exit
3468 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003469exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003471 if( ret != 0 )
3472 {
3473 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 mbedtls_strerror( ret, error_buf, 100 );
3475 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003476 }
3477#endif
3478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 mbedtls_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003480 fflush( stdout );
3481
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003482 mbedtls_net_free( &client_fd );
3483 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +02003484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3486 mbedtls_dhm_free( &dhm );
Paul Bakkera317a982014-06-18 16:44:11 +02003487#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488#if defined(MBEDTLS_X509_CRT_PARSE_C)
3489 mbedtls_x509_crt_free( &cacert );
3490 mbedtls_x509_crt_free( &srvcert );
3491 mbedtls_pk_free( &pkey );
3492 mbedtls_x509_crt_free( &srvcert2 );
3493 mbedtls_pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02003494#endif
Gilles Peskine44817442018-06-13 18:09:28 +02003495#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3496 for( i = 0; (size_t) i < ssl_async_keys.slots_used; i++ )
3497 {
3498 if( ssl_async_keys.slots[i].pk_owned )
3499 {
3500 mbedtls_pk_free( ssl_async_keys.slots[i].pk );
3501 mbedtls_free( ssl_async_keys.slots[i].pk );
3502 ssl_async_keys.slots[i].pk = NULL;
3503 }
3504 }
3505#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02003506#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003507 sni_free( sni_info );
3508#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003510 psk_free( psk_info );
3511#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3513 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003514#endif
Paul Bakkered27a042013-04-18 22:46:23 +02003515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003517 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 mbedtls_ctr_drbg_free( &ctr_drbg );
3519 mbedtls_entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521#if defined(MBEDTLS_SSL_CACHE_C)
3522 mbedtls_ssl_cache_free( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00003523#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003524#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3525 mbedtls_ssl_ticket_free( &ticket_ctx );
3526#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527#if defined(MBEDTLS_SSL_COOKIE_C)
3528 mbedtls_ssl_cookie_free( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02003529#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003530
Hanno Becker095d9cf2018-10-09 12:39:13 +01003531 mbedtls_free( buf );
3532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
3534#if defined(MBEDTLS_MEMORY_DEBUG)
3535 mbedtls_memory_buffer_alloc_status();
Paul Bakker82024bf2013-07-04 11:52:32 +02003536#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +02003538#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02003539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540 mbedtls_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003541
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003542#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003543 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003544 fflush( stdout ); getchar();
3545#endif
3546
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02003547 // Shell can not handle large exit numbers -> 1 for errors
3548 if( ret < 0 )
3549 ret = 1;
3550
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003551 return( ret );
3552}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
3554 MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003555 MBEDTLS_CTR_DRBG_C */