blob: ac956813e125d5f36b767051179d407028cd16ca [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" \
197 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
198 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
199 " 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 +0200200 " default: see note after key_file2\n" \
201 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200202 " 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 +0200203 " default: see note after key_file2\n" \
204 " key_file2=%%s default: see note below\n" \
205 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200206 " preloaded certificate(s) and key(s) are used if available\n" \
207 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
208 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000209#else
210#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200211 "\n" \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 " No file operations available (MBEDTLS_FS_IO not defined)\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200213 "\n"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200215#else
216#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200218
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200219#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100220#define USAGE_SSL_ASYNC \
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100221 " async_operations=%%c... d=decrypt, s=sign (default: -=off)\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100222 " async_private_delay1=%%d Asynchronous delay for key_file or preloaded key\n" \
Gilles Peskine166ce742018-04-30 10:30:49 +0200223 " async_private_delay2=%%d Asynchronous delay for key_file2 and sni\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100224 " default: -1 (not asynchronous)\n" \
Gilles Peskine60ee4ca2018-01-08 11:28:05 +0100225 " async_private_error=%%d Async callback error injection (default=0=none,\n" \
Gilles Peskinec9125722018-04-26 07:15:40 +0200226 " 1=start, 2=cancel, 3=resume, negative=first time only)"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100227#else
228#define USAGE_SSL_ASYNC ""
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200229#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100230
Hanno Beckera5a2b082019-05-15 14:03:01 +0100231#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1029ace2019-04-09 17:28:10 +0100232#define USAGE_CID \
233 " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
234 " default: 0 (disabled)\n" \
Hanno Becker96870292019-05-03 17:30:59 +0100235 " 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 +0100236 " default: same as 'cid' parameter\n" \
Hanno Becker1029ace2019-04-09 17:28:10 +0100237 " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
Hanno Becker96870292019-05-03 17:30:59 +0100238 " default: \"\"\n" \
239 " 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 +0100240 " default: same as 'cid_val' parameter\n"
Hanno Beckera5a2b082019-05-15 14:03:01 +0100241#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1029ace2019-04-09 17:28:10 +0100242#define USAGE_CID ""
Hanno Beckera5a2b082019-05-15 14:03:01 +0100243#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1029ace2019-04-09 17:28:10 +0100244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd0d01c52018-10-25 16:49:48 +0100246#define USAGE_PSK \
247 " psk=%%s default: \"\" (in hex, without 0x)\n" \
248 " psk_list=%%s default: \"\"\n" \
Hanno Becker5ddc0632018-10-26 10:04:13 +0100249 " A list of (PSK identity, PSK value) pairs.\n" \
250 " The PSK values are in hex, without 0x.\n" \
Hanno Beckerd0d01c52018-10-25 16:49:48 +0100251 " id1,psk1[,id2,psk2[,...]]\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200252 " psk_identity=%%s default: \"Client_identity\"\n"
253#else
254#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200258#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100259 " tickets=%%d default: 1 (enabled)\n" \
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200260 " ticket_timeout=%%d default: 86400 (one day)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200261#else
262#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100266#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100267 " cache_max=%%d default: cache default (50)\n" \
268 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100269#else
270#define USAGE_CACHE ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#endif /* MBEDTLS_SSL_CACHE_C */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100272
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200273#if defined(SNI_OPTION)
Ron Eldor24eec792019-04-04 15:02:01 +0300274#if defined(MBEDTLS_X509_CRL_PARSE_C)
275#define SNI_CRL ",crl"
276#else
277#define SNI_CRL ""
278#endif
279
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100280#define USAGE_SNI \
Ron Eldor24eec792019-04-04 15:02:01 +0300281 " sni=%%s name1,cert1,key1,ca1"SNI_CRL",auth1[,...]\n" \
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200282 " default: disabled\n"
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100283#else
284#define USAGE_SNI ""
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200285#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200288#define USAGE_MAX_FRAG_LEN \
289 " max_frag_len=%%d default: 16384 (tls default)\n" \
290 " options: 512, 1024, 2048, 4096\n"
291#else
292#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100296#define USAGE_TRUNC_HMAC \
297 " trunc_hmac=%%d default: library default\n"
298#else
299#define USAGE_TRUNC_HMAC ""
300#endif
301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200303#define USAGE_ALPN \
304 " alpn=%%s default: \"\" (disabled)\n" \
305 " example: spdy/1,http/1.1\n"
306#else
307#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200311#define USAGE_COOKIES \
312 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200313 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200314#else
315#define USAGE_COOKIES ""
316#endif
317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200319#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200320 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200321#else
322#define USAGE_ANTI_REPLAY ""
323#endif
324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200326#define USAGE_BADMAC_LIMIT \
327 " badmac_limit=%%d default: (library default: disabled)\n"
328#else
329#define USAGE_BADMAC_LIMIT ""
330#endif
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200333#define USAGE_DTLS \
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +0100334 " dtls=%%d default: 0 (TLS) (if both enabled)\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200335 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200336 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Beckere7675d02018-08-14 13:28:56 +0100337 " mtu=%%d default: (library default: unlimited)\n" \
338 " dgram_packing=%%d default: 1 (allowed)\n" \
339 " allow or forbid packing of multiple\n" \
340 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200341#else
342#define USAGE_DTLS ""
343#endif
344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200346#define USAGE_EMS \
Jarno Lamsa41b35912019-06-10 15:51:11 +0300347 " extended_ms=0/1 default: (library default: on)\n" \
348 " enforce_extended_master_secret=0/1 default: (library default: off)\n"
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200349#else
350#define USAGE_EMS ""
351#endif
352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100354#define USAGE_ETM \
355 " etm=0/1 default: (library default: on)\n"
356#else
357#define USAGE_ETM ""
358#endif
359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100361#define USAGE_RENEGO \
362 " renegotiation=%%d default: 0 (disabled)\n" \
363 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100364 " renego_delay=%%d default: -2 (library default)\n" \
Andres AG692ad842017-01-19 16:30:57 +0000365 " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100366#else
367#define USAGE_RENEGO ""
368#endif
369
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200370#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
371#define USAGE_ECJPAKE \
372 " ecjpake_pw=%%s default: none (disabled)\n"
373#else
374#define USAGE_ECJPAKE ""
375#endif
376
Hanno Beckere6706e62017-05-15 16:05:15 +0100377#if defined(MBEDTLS_ECP_C)
378#define USAGE_CURVES \
379 " curves=a,b,c,d default: \"default\" (library default)\n" \
380 " example: \"secp521r1,brainpoolP512r1\"\n" \
381 " - use \"none\" for empty list\n" \
382 " - see mbedtls_ecp_curve_list()\n" \
383 " for acceptable curve names\n"
384#else
385#define USAGE_CURVES ""
386#endif
387
Jarno Lamsacf1b6722019-06-04 11:06:31 +0300388#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
389#define USAGE_SERIALIZATION \
Jarno Lamsab5ff6a42019-06-06 10:40:52 +0300390 " serialize=%%d default: 0 (do not serialize/deserialize)\n" \
391 " options: 1 (serialize)\n" \
392 " 2 (serialize with re-initialization)\n"
Jarno Lamsacf1b6722019-06-04 11:06:31 +0300393#else
394#define USAGE_SERIALIZATION ""
395#endif
396
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000397#define USAGE \
398 "\n usage: ssl_server2 param=<>...\n" \
399 "\n acceptable parameters:\n" \
Ron Eldor71f68c42017-09-26 11:29:11 +0300400 " server_addr=%%s default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000401 " server_port=%%d default: 4433\n" \
402 " debug_level=%%d default: 0 (disabled)\n" \
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200403 " buffer_size=%%d default: 200 \n" \
404 " (minimum: 1, max: 16385)\n" \
405 " response_size=%%d default: about 152 (basic response)\n" \
406 " (minimum: 0, max: 16384)\n" \
407 " increases buffer_size if bigger\n"\
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100408 " nbio=%%d default: 0 (blocking I/O)\n" \
409 " options: 1 (non-blocking), 2 (added delays)\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100410 " event=%%d default: 0 (loop)\n" \
411 " options: 1 (level-triggered, implies nbio=1),\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200412 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100413 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200414 USAGE_DTLS \
415 USAGE_COOKIES \
416 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200417 USAGE_BADMAC_LIMIT \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200418 "\n" \
Manuel Pégourié-Gonnardee6139c2015-05-07 10:18:26 +0100419 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100420 " options: none, optional, required\n" \
Janos Follath4817e272017-04-10 13:44:33 +0100421 " cert_req_ca_list=%%d default: 1 (send ca list)\n" \
422 " options: 1 (send ca list), 0 (don't send)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000423 USAGE_IO \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100424 USAGE_SSL_ASYNC \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100425 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100426 "\n" \
427 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200428 USAGE_ECJPAKE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100429 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100430 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100431 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200432 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200433 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100434 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100435 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100436 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100437 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200438 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200439 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100440 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100441 USAGE_CURVES \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100442 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100443 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200444 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200445 " min_version=%%s default: (library default: tls1)\n" \
446 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200447 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100448 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200449 "\n" \
450 " version_suites=a,b,c,d per-version ciphersuites\n" \
451 " in order from ssl3 to tls1_2\n" \
452 " default: all enabled\n" \
453 " force_ciphersuite=<name> default: all enabled\n" \
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100454 " query_config=<name> return 0 if the specified\n" \
455 " configuration macro is defined and 1\n" \
456 " otherwise. The expansion of the macro\n" \
457 " is printed if it is defined\n" \
Jarno Lamsacf1b6722019-06-04 11:06:31 +0300458 USAGE_SERIALIZATION \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000459 " acceptable ciphersuite names:\n"
460
Andres AG692ad842017-01-19 16:30:57 +0000461
Hanno Becker8651a432017-06-09 16:13:22 +0100462#define ALPN_LIST_SIZE 10
463#define CURVE_LIST_SIZE 20
464
Andres AG692ad842017-01-19 16:30:57 +0000465#define PUT_UINT64_BE(out_be,in_le,i) \
466{ \
467 (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \
468 (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \
469 (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \
470 (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \
471 (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \
472 (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \
473 (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \
474 (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \
475}
476
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100477#if defined(MBEDTLS_CHECK_PARAMS)
478#include "mbedtls/platform_util.h"
479void mbedtls_param_failed( const char *failure_condition,
480 const char *file,
481 int line )
Simon Butcher63cb97e2018-12-06 17:43:31 +0000482{
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100483 mbedtls_printf( "%s:%i: Input param failed - %s\n",
484 file, line, failure_condition );
Simon Butcher63cb97e2018-12-06 17:43:31 +0000485 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
486}
487#endif
488
Rich Evans85b05ec2015-02-12 11:37:29 +0000489/*
490 * global options
491 */
492struct options
493{
494 const char *server_addr; /* address on which the ssl service runs */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200495 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000496 int debug_level; /* level of debugging */
497 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100498 int event; /* loop or event-driven IO? level or edge triggered? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200500 int response_size; /* pad response with header to requested size */
501 uint16_t buffer_size; /* IO buffer size */
Rich Evans85b05ec2015-02-12 11:37:29 +0000502 const char *ca_file; /* the file with the CA certificate(s) */
503 const char *ca_path; /* the path with the CA certificate(s) reside */
504 const char *crt_file; /* the file with the server certificate */
505 const char *key_file; /* the file with the server key */
506 const char *crt_file2; /* the file with the 2nd server certificate */
507 const char *key_file2; /* the file with the 2nd server key */
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100508 const char *async_operations; /* supported SSL asynchronous operations */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100509 int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
510 int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
511 int async_private_error; /* inject error in async private callback */
Rich Evans85b05ec2015-02-12 11:37:29 +0000512 const char *psk; /* the pre-shared key */
513 const char *psk_identity; /* the pre-shared key identity */
514 char *psk_list; /* list of PSK id/key pairs for callback */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200515 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000516 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
517 const char *version_suites; /* per-version ciphersuites */
518 int renegotiation; /* enable / disable renegotiation */
519 int allow_legacy; /* allow legacy renegotiation */
520 int renegotiate; /* attempt renegotiation? */
521 int renego_delay; /* delay before enforcing renegotiation */
Andres AG692ad842017-01-19 16:30:57 +0000522 uint64_t renego_period; /* period for automatic renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000523 int exchanges; /* number of data exchanges */
524 int min_version; /* minimum protocol version accepted */
525 int max_version; /* maximum protocol version accepted */
526 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200527 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000528 int auth_mode; /* verify mode for connection */
Janos Follath4817e272017-04-10 13:44:33 +0100529 int cert_req_ca_list; /* should we send the CA list? */
Rich Evans85b05ec2015-02-12 11:37:29 +0000530 unsigned char mfl_code; /* code for maximum fragment length */
531 int trunc_hmac; /* accept truncated hmac? */
532 int tickets; /* enable / disable session tickets */
533 int ticket_timeout; /* session ticket lifetime */
534 int cache_max; /* max number of session cache entries */
535 int cache_timeout; /* expiration delay of session cache entries */
536 char *sni; /* string describing sni information */
Hanno Beckere6706e62017-05-15 16:05:15 +0100537 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000538 const char *alpn_string; /* ALPN supported protocols */
539 const char *dhm_file; /* the file with the DH parameters */
540 int extended_ms; /* allow negotiation of extended MS? */
Jarno Lamsa41b35912019-06-10 15:51:11 +0300541 int enforce_extended_master_secret; /* Enforce the usage of extended
542 * master secret */
Rich Evans85b05ec2015-02-12 11:37:29 +0000543 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000544 int transport; /* TLS or DTLS? */
545 int cookies; /* Use cookies for DTLS? -1 to break them */
546 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
547 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
548 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200549 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Hanno Beckere7675d02018-08-14 13:28:56 +0100550 int dgram_packing; /* allow/forbid datagram packing */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000551 int badmac_limit; /* Limit of records with bad MAC */
Hanno Becker1029ace2019-04-09 17:28:10 +0100552 int cid_enabled; /* whether to use the CID extension or not */
Hanno Becker96870292019-05-03 17:30:59 +0100553 int cid_enabled_renego; /* whether to use the CID extension or not
554 * during renegotiation */
Hanno Becker1029ace2019-04-09 17:28:10 +0100555 const char *cid_val; /* the CID to use for incoming messages */
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +0300556 int serialize; /* serialize/deserialize connection */
Hanno Becker96870292019-05-03 17:30:59 +0100557 const char *cid_val_renego; /* the CID to use for incoming messages
558 * after renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000559} opt;
560
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100561int query_config( const char *config );
562
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200563static void my_debug( void *ctx, int level,
564 const char *file, int line,
565 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000566{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200567 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000568
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200569 /* Extract basename from file */
570 for( p = basename = file; *p != '\0'; p++ )
571 if( *p == '/' || *p == '\\' )
572 basename = p + 1;
573
574 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000575 fflush( (FILE *) ctx );
576}
577
578/*
579 * Test recv/send functions that make sure each try returns
580 * WANT_READ/WANT_WRITE at least once before sucesseding
581 */
582static int my_recv( void *ctx, unsigned char *buf, size_t len )
583{
584 static int first_try = 1;
585 int ret;
586
587 if( first_try )
588 {
589 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100590 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000591 }
592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100594 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000595 first_try = 1; /* Next call will be a new operation */
596 return( ret );
597}
598
599static int my_send( void *ctx, const unsigned char *buf, size_t len )
600{
601 static int first_try = 1;
602 int ret;
603
604 if( first_try )
605 {
606 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100607 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000608 }
609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100611 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000612 first_try = 1; /* Next call will be a new operation */
613 return( ret );
614}
615
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200616/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200617 * Return authmode from string, or -1 on error
618 */
619static int get_auth_mode( const char *s )
620{
621 if( strcmp( s, "none" ) == 0 )
622 return( MBEDTLS_SSL_VERIFY_NONE );
623 if( strcmp( s, "optional" ) == 0 )
624 return( MBEDTLS_SSL_VERIFY_OPTIONAL );
625 if( strcmp( s, "required" ) == 0 )
626 return( MBEDTLS_SSL_VERIFY_REQUIRED );
627
628 return( -1 );
629}
630
631/*
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200632 * Used by sni_parse and psk_parse to handle coma-separated lists
633 */
634#define GET_ITEM( dst ) \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100635 do \
636 { \
637 (dst) = p; \
638 while( *p != ',' ) \
639 if( ++p > end ) \
640 goto error; \
641 *p++ = '\0'; \
642 } while( 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200643
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200644#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100645typedef struct _sni_entry sni_entry;
646
647struct _sni_entry {
648 const char *name;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_x509_crt *cert;
650 mbedtls_pk_context *key;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200651 mbedtls_x509_crt* ca;
652 mbedtls_x509_crl* crl;
653 int authmode;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100654 sni_entry *next;
655};
656
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100657void sni_free( sni_entry *head )
658{
659 sni_entry *cur = head, *next;
660
661 while( cur != NULL )
662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_x509_crt_free( cur->cert );
664 mbedtls_free( cur->cert );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_pk_free( cur->key );
667 mbedtls_free( cur->key );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100668
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200669 mbedtls_x509_crt_free( cur->ca );
670 mbedtls_free( cur->ca );
Ron Eldor24eec792019-04-04 15:02:01 +0300671#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200672 mbedtls_x509_crl_free( cur->crl );
673 mbedtls_free( cur->crl );
Ron Eldor24eec792019-04-04 15:02:01 +0300674#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100675 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_free( cur );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100677 cur = next;
678 }
679}
680
681/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200682 * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
683 * into a usable sni_entry list. For ca1, crl1, auth1, the special value
684 * '-' means unset. If ca1 is unset, then crl1 is ignored too.
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000685 *
686 * Modifies the input string! This is not production quality!
687 */
688sni_entry *sni_parse( char *sni_string )
689{
690 sni_entry *cur = NULL, *new = NULL;
691 char *p = sni_string;
692 char *end = p;
Ron Eldor24eec792019-04-04 15:02:01 +0300693 char *crt_file, *key_file, *ca_file, *auth_str;
694#if defined(MBEDTLS_X509_CRL_PARSE_C)
695 char *crl_file;
696#endif
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000697
698 while( *end != '\0' )
699 ++end;
700 *end = ',';
701
702 while( p <= end )
703 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200704 if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000705 {
706 sni_free( cur );
707 return( NULL );
708 }
709
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200710 GET_ITEM( new->name );
711 GET_ITEM( crt_file );
712 GET_ITEM( key_file );
713 GET_ITEM( ca_file );
Ron Eldor24eec792019-04-04 15:02:01 +0300714#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200715 GET_ITEM( crl_file );
Ron Eldor24eec792019-04-04 15:02:01 +0300716#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200717 GET_ITEM( auth_str );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000718
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200719 if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
720 ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200721 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_x509_crt_init( new->cert );
724 mbedtls_pk_init( new->key );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
727 mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000728 goto error;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200729
730 if( strcmp( ca_file, "-" ) != 0 )
731 {
732 if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
733 goto error;
734
735 mbedtls_x509_crt_init( new->ca );
736
737 if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
738 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000739 }
740
Ron Eldor24eec792019-04-04 15:02:01 +0300741#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200742 if( strcmp( crl_file, "-" ) != 0 )
743 {
744 if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
745 goto error;
746
747 mbedtls_x509_crl_init( new->crl );
748
749 if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
750 goto error;
751 }
Ron Eldor24eec792019-04-04 15:02:01 +0300752#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200753
754 if( strcmp( auth_str, "-" ) != 0 )
755 {
756 if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
757 goto error;
758 }
759 else
760 new->authmode = DFL_AUTH_MODE;
761
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000762 new->next = cur;
763 cur = new;
764 }
765
766 return( cur );
767
768error:
769 sni_free( new );
770 sni_free( cur );
771 return( NULL );
772}
773
774/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100775 * SNI callback.
776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100778 const unsigned char *name, size_t name_len )
779{
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200780 const sni_entry *cur = (const sni_entry *) p_info;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100781
782 while( cur != NULL )
783 {
784 if( name_len == strlen( cur->name ) &&
785 memcmp( name, cur->name, name_len ) == 0 )
786 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200787 if( cur->ca != NULL )
788 mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
789
790 if( cur->authmode != DFL_AUTH_MODE )
791 mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
792
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200793 return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100794 }
795
796 cur = cur->next;
797 }
798
799 return( -1 );
800}
801
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200802#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100803
Hanno Becker2e0bedc2019-04-30 14:18:06 +0100804#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) || \
Hanno Beckera5a2b082019-05-15 14:03:01 +0100805 defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200806
Hanno Beckerd6028a12018-10-15 12:01:35 +0100807#define HEX2NUM( c ) \
808 do \
809 { \
810 if( (c) >= '0' && (c) <= '9' ) \
811 (c) -= '0'; \
812 else if( (c) >= 'a' && (c) <= 'f' ) \
813 (c) -= 'a' - 10; \
814 else if( (c) >= 'A' && (c) <= 'F' ) \
815 (c) -= 'A' - 10; \
816 else \
817 return( -1 ); \
818 } while( 0 )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200819
820/*
821 * Convert a hex string to bytes.
822 * Return 0 on success, -1 on error.
823 */
824int unhexify( unsigned char *output, const char *input, size_t *olen )
825{
826 unsigned char c;
827 size_t j;
828
829 *olen = strlen( input );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 if( *olen % 2 != 0 || *olen / 2 > MBEDTLS_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200831 return( -1 );
832 *olen /= 2;
833
834 for( j = 0; j < *olen * 2; j += 2 )
835 {
836 c = input[j];
837 HEX2NUM( c );
838 output[ j / 2 ] = c << 4;
839
840 c = input[j + 1];
841 HEX2NUM( c );
842 output[ j / 2 ] |= c;
843 }
844
845 return( 0 );
846}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200847
Hanno Becker2e0bedc2019-04-30 14:18:06 +0100848#endif
849
850#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
851
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200852typedef struct _psk_entry psk_entry;
853
854struct _psk_entry
855{
856 const char *name;
857 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 unsigned char key[MBEDTLS_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200859 psk_entry *next;
860};
861
862/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000863 * Free a list of psk_entry's
864 */
865void psk_free( psk_entry *head )
866{
867 psk_entry *next;
868
869 while( head != NULL )
870 {
871 next = head->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 mbedtls_free( head );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000873 head = next;
874 }
875}
876
877/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200878 * Parse a string of pairs name1,key1[,name2,key2[,...]]
879 * into a usable psk_entry list.
880 *
881 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200882 */
883psk_entry *psk_parse( char *psk_string )
884{
885 psk_entry *cur = NULL, *new = NULL;
886 char *p = psk_string;
887 char *end = p;
888 char *key_hex;
889
890 while( *end != '\0' )
891 ++end;
892 *end = ',';
893
894 while( p <= end )
895 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200896 if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +0000897 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200898
899 memset( new, 0, sizeof( psk_entry ) );
900
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200901 GET_ITEM( new->name );
902 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200903
904 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000905 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200906
907 new->next = cur;
908 cur = new;
909 }
910
911 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200912
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000913error:
914 psk_free( new );
915 psk_free( cur );
916 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200917}
918
919/*
920 * PSK callback
921 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200923 const unsigned char *name, size_t name_len )
924{
925 psk_entry *cur = (psk_entry *) p_info;
926
927 while( cur != NULL )
928 {
929 if( name_len == strlen( cur->name ) &&
930 memcmp( name, cur->name, name_len ) == 0 )
931 {
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +0100932 return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200933 }
934
935 cur = cur->next;
936 }
937
938 return( -1 );
939}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200941
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200942static mbedtls_net_context listen_fd, client_fd;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200943
944/* Interruption handler to ensure clean exit (for valgrind testing) */
945#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200946static int received_sigterm = 0;
947void term_handler( int sig )
948{
949 ((void) sig);
950 received_sigterm = 1;
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200951 mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
952 mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200953}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200954#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200955
Gilles Peskine682df092017-05-11 19:01:11 +0200956#if defined(MBEDTLS_X509_CRT_PARSE_C)
957static int ssl_sig_hashes_for_test[] = {
958#if defined(MBEDTLS_SHA512_C)
959 MBEDTLS_MD_SHA512,
960 MBEDTLS_MD_SHA384,
961#endif
962#if defined(MBEDTLS_SHA256_C)
963 MBEDTLS_MD_SHA256,
964 MBEDTLS_MD_SHA224,
965#endif
966#if defined(MBEDTLS_SHA1_C)
967 /* Allow SHA-1 as we use it extensively in tests. */
968 MBEDTLS_MD_SHA1,
969#endif
970 MBEDTLS_MD_NONE
971};
972#endif /* MBEDTLS_X509_CRT_PARSE_C */
973
Gilles Peskinea36ac4f2018-04-26 08:05:02 +0200974/** Return true if \p ret is a status code indicating that there is an
975 * operation in progress on an SSL connection, and false if it indicates
976 * success or a fatal error.
977 *
978 * The possible operations in progress are:
979 *
980 * - A read, when the SSL input buffer does not contain a full message.
981 * - A write, when the SSL output buffer contains some data that has not
982 * been sent over the network yet.
983 * - An asynchronous callback that has not completed yet. */
984static int mbedtls_status_is_ssl_in_progress( int ret )
985{
986 return( ret == MBEDTLS_ERR_SSL_WANT_READ ||
987 ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
988 ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
989}
990
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200991#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100992typedef struct
993{
Gilles Peskine44817442018-06-13 18:09:28 +0200994 mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
995 mbedtls_pk_context *pk; /*!< Private key */
996 unsigned delay; /*!< Number of resume steps to go through */
997 unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100998} ssl_async_key_slot_t;
999
1000typedef enum {
Gilles Peskinead28bf02018-04-26 00:19:16 +02001001 SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
1002 SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
1003 SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start */
1004 SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
Gilles Peskinec9125722018-04-26 07:15:40 +02001005#define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001006} ssl_async_inject_error_t;
1007
1008typedef struct
1009{
Gilles Peskinee2479892018-06-13 18:06:51 +02001010 ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001011 size_t slots_used;
1012 ssl_async_inject_error_t inject_error;
1013 int (*f_rng)(void *, unsigned char *, size_t);
1014 void *p_rng;
1015} ssl_async_key_context_t;
1016
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001017int ssl_async_set_key( ssl_async_key_context_t *ctx,
Gilles Peskine44817442018-06-13 18:09:28 +02001018 mbedtls_x509_crt *cert,
1019 mbedtls_pk_context *pk,
1020 int pk_take_ownership,
1021 unsigned delay )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001022{
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001023 if( ctx->slots_used >= sizeof( ctx->slots ) / sizeof( *ctx->slots ) )
1024 return( -1 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001025 ctx->slots[ctx->slots_used].cert = cert;
1026 ctx->slots[ctx->slots_used].pk = pk;
1027 ctx->slots[ctx->slots_used].delay = delay;
Gilles Peskine44817442018-06-13 18:09:28 +02001028 ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001029 ++ctx->slots_used;
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001030 return( 0 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001031}
1032
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001033#define SSL_ASYNC_INPUT_MAX_SIZE 512
Gilles Peskined3268832018-04-26 06:23:59 +02001034
1035typedef enum
1036{
1037 ASYNC_OP_SIGN,
1038 ASYNC_OP_DECRYPT,
1039} ssl_async_operation_type_t;
1040/* Note that the enum above and the array below need to be kept in sync!
1041 * `ssl_async_operation_names[op]` is the name of op for each value `op`
1042 * of type `ssl_async_operation_type_t`. */
1043static const char *const ssl_async_operation_names[] =
1044{
1045 "sign",
1046 "decrypt",
1047};
1048
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001049typedef struct
1050{
Gilles Peskine6331d782018-04-26 13:27:43 +02001051 unsigned slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001052 ssl_async_operation_type_t operation_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001053 mbedtls_md_type_t md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001054 unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
1055 size_t input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001056 unsigned remaining_delay;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001057} ssl_async_operation_context_t;
1058
Gilles Peskine8f97af72018-04-26 11:46:10 +02001059static int ssl_async_start( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001060 mbedtls_x509_crt *cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001061 ssl_async_operation_type_t op_type,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001062 mbedtls_md_type_t md_alg,
1063 const unsigned char *input,
1064 size_t input_len )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001065{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001066 ssl_async_key_context_t *config_data =
1067 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskine6331d782018-04-26 13:27:43 +02001068 unsigned slot;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001069 ssl_async_operation_context_t *ctx = NULL;
Gilles Peskined3268832018-04-26 06:23:59 +02001070 const char *op_name = ssl_async_operation_names[op_type];
Gilles Peskinef1127252018-04-24 13:05:39 +02001071
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001072 {
1073 char dn[100];
Gilles Peskined5d983e2018-06-15 14:05:10 +02001074 if( mbedtls_x509_dn_gets( dn, sizeof( dn ), &cert->subject ) > 0 )
1075 mbedtls_printf( "Async %s callback: looking for DN=%s\n",
1076 op_name, dn );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001077 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001078
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001079 /* Look for a private key that matches the public key in cert.
1080 * Since this test code has the private key inside Mbed TLS,
1081 * we call mbedtls_pk_check_pair to match a private key with the
1082 * public key. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001083 for( slot = 0; slot < config_data->slots_used; slot++ )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001084 {
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001085 if( mbedtls_pk_check_pair( &cert->pk,
1086 config_data->slots[slot].pk ) == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001087 break;
1088 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001089 if( slot == config_data->slots_used )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001090 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001091 mbedtls_printf( "Async %s callback: no key matches this certificate.\n",
1092 op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001093 return( MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH );
1094 }
Gilles Peskine6331d782018-04-26 13:27:43 +02001095 mbedtls_printf( "Async %s callback: using key slot %u, delay=%u.\n",
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001096 op_name, slot, config_data->slots[slot].delay );
Gilles Peskinef1127252018-04-24 13:05:39 +02001097
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001098 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001099 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001100 mbedtls_printf( "Async %s callback: injected error\n", op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001101 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1102 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001103
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001104 if( input_len > SSL_ASYNC_INPUT_MAX_SIZE )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Gilles Peskinef1127252018-04-24 13:05:39 +02001106
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001107 ctx = mbedtls_calloc( 1, sizeof( *ctx ) );
1108 if( ctx == NULL )
1109 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1110 ctx->slot = slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001111 ctx->operation_type = op_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001112 ctx->md_alg = md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001113 memcpy( ctx->input, input, input_len );
1114 ctx->input_len = input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001115 ctx->remaining_delay = config_data->slots[slot].delay;
Gilles Peskinea668c602018-04-30 11:54:39 +02001116 mbedtls_ssl_set_async_operation_data( ssl, ctx );
Gilles Peskinef1127252018-04-24 13:05:39 +02001117
Gilles Peskineceb541b2018-04-26 06:30:45 +02001118 if( ctx->remaining_delay == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001119 return( 0 );
1120 else
1121 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1122}
1123
Gilles Peskine8f97af72018-04-26 11:46:10 +02001124static int ssl_async_sign( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001125 mbedtls_x509_crt *cert,
1126 mbedtls_md_type_t md_alg,
1127 const unsigned char *hash,
1128 size_t hash_len )
1129{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001130 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001131 ASYNC_OP_SIGN, md_alg,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001132 hash, hash_len ) );
1133}
1134
Gilles Peskine8f97af72018-04-26 11:46:10 +02001135static int ssl_async_decrypt( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001136 mbedtls_x509_crt *cert,
1137 const unsigned char *input,
1138 size_t input_len )
1139{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001140 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001141 ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001142 input, input_len ) );
1143}
1144
Gilles Peskine8f97af72018-04-26 11:46:10 +02001145static int ssl_async_resume( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001146 unsigned char *output,
1147 size_t *output_len,
1148 size_t output_size )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001149{
Gilles Peskinea668c602018-04-30 11:54:39 +02001150 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine8f97af72018-04-26 11:46:10 +02001151 ssl_async_key_context_t *config_data =
1152 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001153 ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001154 int ret;
Gilles Peskinef5a99962018-04-30 16:37:23 +02001155 const char *op_name;
Gilles Peskinef1127252018-04-24 13:05:39 +02001156
Gilles Peskineceb541b2018-04-26 06:30:45 +02001157 if( ctx->remaining_delay > 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001158 {
Gilles Peskineceb541b2018-04-26 06:30:45 +02001159 --ctx->remaining_delay;
Gilles Peskine6331d782018-04-26 13:27:43 +02001160 mbedtls_printf( "Async resume (slot %u): call %u more times.\n",
Gilles Peskineceb541b2018-04-26 06:30:45 +02001161 ctx->slot, ctx->remaining_delay );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001162 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1163 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001164
Gilles Peskined3268832018-04-26 06:23:59 +02001165 switch( ctx->operation_type )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001166 {
Gilles Peskined3268832018-04-26 06:23:59 +02001167 case ASYNC_OP_DECRYPT:
Gilles Peskined3268832018-04-26 06:23:59 +02001168 ret = mbedtls_pk_decrypt( key_slot->pk,
1169 ctx->input, ctx->input_len,
1170 output, output_len, output_size,
1171 config_data->f_rng, config_data->p_rng );
1172 break;
1173 case ASYNC_OP_SIGN:
Gilles Peskined3268832018-04-26 06:23:59 +02001174 ret = mbedtls_pk_sign( key_slot->pk,
1175 ctx->md_alg,
1176 ctx->input, ctx->input_len,
1177 output, output_len,
1178 config_data->f_rng, config_data->p_rng );
1179 break;
1180 default:
Gilles Peskine6331d782018-04-26 13:27:43 +02001181 mbedtls_printf( "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
Gilles Peskined3268832018-04-26 06:23:59 +02001182 ctx->slot, (long) ctx->operation_type );
Gilles Peskine44817442018-06-13 18:09:28 +02001183 mbedtls_free( ctx );
Gilles Peskined3268832018-04-26 06:23:59 +02001184 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1185 break;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001186 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001187
Gilles Peskinef5a99962018-04-30 16:37:23 +02001188 op_name = ssl_async_operation_names[ctx->operation_type];
1189
Gilles Peskinec9125722018-04-26 07:15:40 +02001190 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001191 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001192 mbedtls_printf( "Async resume callback: %s done but injected error\n",
1193 op_name );
Gilles Peskine2636fad2018-06-12 14:17:39 +02001194 mbedtls_free( ctx );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001195 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1196 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001197
Gilles Peskine6331d782018-04-26 13:27:43 +02001198 mbedtls_printf( "Async resume (slot %u): %s done, status=%d.\n",
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001199 ctx->slot, op_name, ret );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001200 mbedtls_free( ctx );
1201 return( ret );
1202}
1203
Gilles Peskine8f97af72018-04-26 11:46:10 +02001204static void ssl_async_cancel( mbedtls_ssl_context *ssl )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001205{
Gilles Peskinea668c602018-04-30 11:54:39 +02001206 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001207 mbedtls_printf( "Async cancel callback.\n" );
1208 mbedtls_free( ctx );
1209}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001210#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001211
Hanno Becker16970d22017-10-10 15:56:37 +01001212/*
1213 * Wait for an event from the underlying transport or the timer
1214 * (Used in event-driven IO mode).
1215 */
1216#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001217int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001218 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001219#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001220int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001221 mbedtls_timing_delay_context *timer,
1222 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001223#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001224{
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001225 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +01001226 int poll_type = 0;
1227
1228 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
1229 poll_type = MBEDTLS_NET_POLL_WRITE;
1230 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
1231 poll_type = MBEDTLS_NET_POLL_READ;
1232#if !defined(MBEDTLS_TIMING_C)
1233 else
Hanno Beckeref527962018-03-15 15:49:24 +00001234 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001235#endif
1236
1237 while( 1 )
1238 {
Hanno Becker197a91c2017-10-31 10:58:53 +00001239 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +01001240#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001241 if( timer != NULL &&
1242 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +01001243 {
Hanno Becker16970d22017-10-10 15:56:37 +01001244 break;
1245 }
Hanno Becker197a91c2017-10-31 10:58:53 +00001246#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +01001247
Hanno Becker197a91c2017-10-31 10:58:53 +00001248 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001249 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +01001250 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001251 ret = mbedtls_net_poll( fd, poll_type, 0 );
1252 if( ret < 0 )
1253 return( ret );
1254 if( ret == poll_type )
1255 break;
Hanno Becker16970d22017-10-10 15:56:37 +01001256 }
1257 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001258
1259 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001260}
1261
Hanno Beckera5a2b082019-05-15 14:03:01 +01001262#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01001263int report_cid_usage( mbedtls_ssl_context *ssl,
1264 const char *additional_description )
1265{
1266 int ret;
1267 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
1268 size_t peer_cid_len;
1269 int cid_negotiated;
1270
1271 if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1272 return( 0 );
1273
1274 /* Check if the use of a CID has been negotiated */
1275 ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
1276 peer_cid, &peer_cid_len );
1277 if( ret != 0 )
1278 {
1279 mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
1280 -ret );
1281 return( ret );
1282 }
1283
1284 if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
1285 {
1286 if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
1287 {
1288 mbedtls_printf( "(%s) Use of Connection ID was not offered by client.\n",
1289 additional_description );
1290 }
1291 }
1292 else
1293 {
1294 size_t idx=0;
1295 mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
1296 additional_description );
1297 mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
1298 additional_description,
1299 (unsigned) peer_cid_len );
1300 while( idx < peer_cid_len )
1301 {
1302 mbedtls_printf( "%02x ", peer_cid[ idx ] );
1303 idx++;
1304 }
1305 mbedtls_printf( "\n" );
1306 }
1307
1308 return( 0 );
1309}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001310#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker96870292019-05-03 17:30:59 +01001311
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001312int main( int argc, char *argv[] )
1313{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001314 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001315 int version_suites[4][2];
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001316 unsigned char* buf = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1318 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +02001319 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +02001320 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +02001321#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001322 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001323 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02001324 size_t cliip_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#if defined(MBEDTLS_SSL_COOKIE_C)
1326 mbedtls_ssl_cookie_ctx cookie_ctx;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001327#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001328
Gilles Peskineef86ab22017-05-05 18:59:02 +02001329#if defined(MBEDTLS_X509_CRT_PARSE_C)
1330 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
1331#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 mbedtls_entropy_context entropy;
1333 mbedtls_ctr_drbg_context ctr_drbg;
1334 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001335 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001336#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001337 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001338#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001340 unsigned char renego_period[8] = { 0 };
1341#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +02001343 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 mbedtls_x509_crt cacert;
1345 mbedtls_x509_crt srvcert;
1346 mbedtls_pk_context pkey;
1347 mbedtls_x509_crt srvcert2;
1348 mbedtls_pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001349 int key_cert_init = 0, key_cert_init2 = 0;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001350#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001351 ssl_async_key_context_t ssl_async_keys;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001352#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001353#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1355 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001356#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357#if defined(MBEDTLS_SSL_CACHE_C)
1358 mbedtls_ssl_cache_context cache;
Paul Bakker0a597072012-09-25 21:55:46 +00001359#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001360#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1361 mbedtls_ssl_ticket_context ticket_ctx;
1362#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001363#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001364 sni_entry *sni_info = NULL;
1365#endif
Hanno Beckere6706e62017-05-15 16:05:15 +01001366#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +01001367 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +01001368 const mbedtls_ecp_curve_info * curve_cur;
1369#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +01001371 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001372#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Simon Butchercce68be2018-07-23 14:26:09 +01001374 unsigned char alloc_buf[MEMORY_HEAP_SIZE];
Paul Bakker82024bf2013-07-04 11:52:32 +02001375#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001376
Hanno Beckera5a2b082019-05-15 14:03:01 +01001377#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1029ace2019-04-09 17:28:10 +01001378 unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker96870292019-05-03 17:30:59 +01001379 unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Becker1029ace2019-04-09 17:28:10 +01001380 size_t cid_len = 0;
Hanno Becker96870292019-05-03 17:30:59 +01001381 size_t cid_renego_len = 0;
Hanno Becker1029ace2019-04-09 17:28:10 +01001382#endif
1383
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001384 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001385 char *p, *q;
1386 const int *list;
1387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1389 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker82024bf2013-07-04 11:52:32 +02001390#endif
1391
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001392 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +02001393 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001394 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001395 mbedtls_net_init( &client_fd );
1396 mbedtls_net_init( &listen_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001397 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001398 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001399 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#if defined(MBEDTLS_X509_CRT_PARSE_C)
1401 mbedtls_x509_crt_init( &cacert );
1402 mbedtls_x509_crt_init( &srvcert );
1403 mbedtls_pk_init( &pkey );
1404 mbedtls_x509_crt_init( &srvcert2 );
1405 mbedtls_pk_init( &pkey2 );
Gilles Peskine4d9ec4d2018-04-26 14:33:43 +02001406#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
1407 memset( &ssl_async_keys, 0, sizeof( ssl_async_keys ) );
1408#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001409#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1411 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001412#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413#if defined(MBEDTLS_SSL_CACHE_C)
1414 mbedtls_ssl_cache_init( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00001415#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001416#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1417 mbedtls_ssl_ticket_init( &ticket_ctx );
1418#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +02001420 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001421#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#if defined(MBEDTLS_SSL_COOKIE_C)
1423 mbedtls_ssl_cookie_init( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001424#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001425
Paul Bakkerc1283d32014-08-18 11:05:51 +02001426#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001427 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001428 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001429 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +02001430#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001431
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001432 if( argc == 0 )
1433 {
1434 usage:
1435 if( ret == 0 )
1436 ret = 1;
1437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 mbedtls_printf( USAGE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 list = mbedtls_ssl_list_ciphersuites();
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001441 while( *list )
1442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001444 list++;
1445 if( !*list )
1446 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001448 list++;
1449 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 mbedtls_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001451 goto exit;
1452 }
1453
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001454 opt.buffer_size = DFL_IO_BUF_LEN;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001455 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001456 opt.server_port = DFL_SERVER_PORT;
1457 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker16970d22017-10-10 15:56:37 +01001458 opt.event = DFL_EVENT;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001459 opt.response_size = DFL_RESPONSE_SIZE;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001460 opt.nbio = DFL_NBIO;
Hanno Becker1029ace2019-04-09 17:28:10 +01001461 opt.cid_enabled = DFL_CID_ENABLED;
Hanno Becker96870292019-05-03 17:30:59 +01001462 opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
Hanno Becker1029ace2019-04-09 17:28:10 +01001463 opt.cid_val = DFL_CID_VALUE;
Hanno Becker96870292019-05-03 17:30:59 +01001464 opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001465 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001466 opt.ca_file = DFL_CA_FILE;
1467 opt.ca_path = DFL_CA_PATH;
1468 opt.crt_file = DFL_CRT_FILE;
1469 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001470 opt.crt_file2 = DFL_CRT_FILE2;
1471 opt.key_file2 = DFL_KEY_FILE2;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001472 opt.async_operations = DFL_ASYNC_OPERATIONS;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001473 opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
1474 opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
1475 opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
Paul Bakkerfbb17802013-04-17 19:10:21 +02001476 opt.psk = DFL_PSK;
1477 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001478 opt.psk_list = DFL_PSK_LIST;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001479 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001480 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001481 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001482 opt.renegotiation = DFL_RENEGOTIATION;
1483 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001484 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001485 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001486 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001487 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001488 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001489 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001490 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001491 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001492 opt.auth_mode = DFL_AUTH_MODE;
Janos Follath4817e272017-04-10 13:44:33 +01001493 opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001494 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001495 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001496 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001497 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001498 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001499 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001500 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001501 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001502 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001503 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001504 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001505 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001506 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001507 opt.hs_to_min = DFL_HS_TO_MIN;
1508 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001509 opt.dtls_mtu = DFL_DTLS_MTU;
Hanno Beckere7675d02018-08-14 13:28:56 +01001510 opt.dgram_packing = DFL_DGRAM_PACKING;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001511 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001512 opt.extended_ms = DFL_EXTENDED_MS;
Jarno Lamsa41b35912019-06-10 15:51:11 +03001513 opt.enforce_extended_master_secret = DFL_EXTENDED_MS_ENFORCE;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001514 opt.etm = DFL_ETM;
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03001515 opt.serialize = DFL_SERIALIZE;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001516
1517 for( i = 1; i < argc; i++ )
1518 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001519 p = argv[i];
1520 if( ( q = strchr( p, '=' ) ) == NULL )
1521 goto usage;
1522 *q++ = '\0';
1523
1524 if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001525 opt.server_port = q;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001526 else if( strcmp( p, "server_addr" ) == 0 )
1527 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001528 else if( strcmp( p, "dtls" ) == 0 )
1529 {
1530 int t = atoi( q );
1531 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001533 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001535 else
1536 goto usage;
1537 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001538 else if( strcmp( p, "debug_level" ) == 0 )
1539 {
1540 opt.debug_level = atoi( q );
1541 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1542 goto usage;
1543 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001544 else if( strcmp( p, "nbio" ) == 0 )
1545 {
1546 opt.nbio = atoi( q );
1547 if( opt.nbio < 0 || opt.nbio > 2 )
1548 goto usage;
1549 }
Hanno Becker16970d22017-10-10 15:56:37 +01001550 else if( strcmp( p, "event" ) == 0 )
1551 {
1552 opt.event = atoi( q );
1553 if( opt.event < 0 || opt.event > 2 )
1554 goto usage;
1555 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001556 else if( strcmp( p, "read_timeout" ) == 0 )
1557 opt.read_timeout = atoi( q );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001558 else if( strcmp( p, "buffer_size" ) == 0 )
1559 {
1560 opt.buffer_size = atoi( q );
1561 if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
1562 goto usage;
1563 }
1564 else if( strcmp( p, "response_size" ) == 0 )
1565 {
1566 opt.response_size = atoi( q );
1567 if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
1568 goto usage;
1569 if( opt.buffer_size < opt.response_size )
1570 opt.buffer_size = opt.response_size;
1571 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001572 else if( strcmp( p, "ca_file" ) == 0 )
1573 opt.ca_file = q;
1574 else if( strcmp( p, "ca_path" ) == 0 )
1575 opt.ca_path = q;
1576 else if( strcmp( p, "crt_file" ) == 0 )
1577 opt.crt_file = q;
1578 else if( strcmp( p, "key_file" ) == 0 )
1579 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001580 else if( strcmp( p, "crt_file2" ) == 0 )
1581 opt.crt_file2 = q;
1582 else if( strcmp( p, "key_file2" ) == 0 )
1583 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001584 else if( strcmp( p, "dhm_file" ) == 0 )
1585 opt.dhm_file = q;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001586#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001587 else if( strcmp( p, "async_operations" ) == 0 )
1588 opt.async_operations = q;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001589 else if( strcmp( p, "async_private_delay1" ) == 0 )
1590 opt.async_private_delay1 = atoi( q );
1591 else if( strcmp( p, "async_private_delay2" ) == 0 )
1592 opt.async_private_delay2 = atoi( q );
1593 else if( strcmp( p, "async_private_error" ) == 0 )
1594 {
1595 int n = atoi( q );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01001596 if( n < -SSL_ASYNC_INJECT_ERROR_MAX ||
1597 n > SSL_ASYNC_INJECT_ERROR_MAX )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001598 {
1599 ret = 2;
1600 goto usage;
1601 }
1602 opt.async_private_error = n;
1603 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001604#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Hanno Beckera5a2b082019-05-15 14:03:01 +01001605#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1029ace2019-04-09 17:28:10 +01001606 else if( strcmp( p, "cid" ) == 0 )
1607 {
1608 opt.cid_enabled = atoi( q );
1609 if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
1610 goto usage;
1611 }
Hanno Becker96870292019-05-03 17:30:59 +01001612 else if( strcmp( p, "cid_renego" ) == 0 )
1613 {
1614 opt.cid_enabled_renego = atoi( q );
1615 if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
1616 goto usage;
1617 }
Hanno Becker1029ace2019-04-09 17:28:10 +01001618 else if( strcmp( p, "cid_val" ) == 0 )
1619 {
1620 opt.cid_val = q;
1621 }
Hanno Becker96870292019-05-03 17:30:59 +01001622 else if( strcmp( p, "cid_val_renego" ) == 0 )
1623 {
1624 opt.cid_val_renego = q;
1625 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001626#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001627 else if( strcmp( p, "psk" ) == 0 )
1628 opt.psk = q;
1629 else if( strcmp( p, "psk_identity" ) == 0 )
1630 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001631 else if( strcmp( p, "psk_list" ) == 0 )
1632 opt.psk_list = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001633 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1634 opt.ecjpake_pw = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001635 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001638
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001639 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001640 {
1641 ret = 2;
1642 goto usage;
1643 }
1644 opt.force_ciphersuite[1] = 0;
1645 }
Hanno Beckere6706e62017-05-15 16:05:15 +01001646 else if( strcmp( p, "curves" ) == 0 )
1647 opt.curves = q;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001648 else if( strcmp( p, "version_suites" ) == 0 )
1649 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001650 else if( strcmp( p, "renegotiation" ) == 0 )
1651 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001652 opt.renegotiation = (atoi( q )) ?
1653 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1654 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001655 }
1656 else if( strcmp( p, "allow_legacy" ) == 0 )
1657 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001658 switch( atoi( q ) )
1659 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001660 case -1:
1661 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1662 break;
1663 case 0:
1664 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1665 break;
1666 case 1:
1667 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1668 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001669 default: goto usage;
1670 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001671 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001672 else if( strcmp( p, "renegotiate" ) == 0 )
1673 {
1674 opt.renegotiate = atoi( q );
1675 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1676 goto usage;
1677 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001678 else if( strcmp( p, "renego_delay" ) == 0 )
1679 {
1680 opt.renego_delay = atoi( q );
1681 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001682 else if( strcmp( p, "renego_period" ) == 0 )
1683 {
Andres AG0b736db2017-02-08 14:05:57 +00001684#if defined(_MSC_VER)
1685 opt.renego_period = _strtoui64( q, NULL, 10 );
1686#else
1687 if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
1688 goto usage;
1689#endif /* _MSC_VER */
1690 if( opt.renego_period < 2 )
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001691 goto usage;
1692 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001693 else if( strcmp( p, "exchanges" ) == 0 )
1694 {
1695 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001696 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001697 goto usage;
1698 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001699 else if( strcmp( p, "min_version" ) == 0 )
1700 {
1701 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001703 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001705 else if( strcmp( q, "tls1_1" ) == 0 ||
1706 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001708 else if( strcmp( q, "tls1_2" ) == 0 ||
1709 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001711 else
1712 goto usage;
1713 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001714 else if( strcmp( p, "max_version" ) == 0 )
1715 {
1716 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001718 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001720 else if( strcmp( q, "tls1_1" ) == 0 ||
1721 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001723 else if( strcmp( q, "tls1_2" ) == 0 ||
1724 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001726 else
1727 goto usage;
1728 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001729 else if( strcmp( p, "arc4" ) == 0 )
1730 {
1731 switch( atoi( q ) )
1732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1734 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001735 default: goto usage;
1736 }
1737 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02001738 else if( strcmp( p, "allow_sha1" ) == 0 )
1739 {
1740 switch( atoi( q ) )
1741 {
1742 case 0: opt.allow_sha1 = 0; break;
1743 case 1: opt.allow_sha1 = 1; break;
1744 default: goto usage;
1745 }
1746 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001747 else if( strcmp( p, "force_version" ) == 0 )
1748 {
1749 if( strcmp( q, "ssl3" ) == 0 )
1750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1752 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001753 }
1754 else if( strcmp( q, "tls1" ) == 0 )
1755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1757 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001758 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001759 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1762 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001763 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001764 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1767 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001768 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001769 else if( strcmp( q, "dtls1" ) == 0 )
1770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1772 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1773 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001774 }
1775 else if( strcmp( q, "dtls1_2" ) == 0 )
1776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1778 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1779 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001780 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001781 else
1782 goto usage;
1783 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001784 else if( strcmp( p, "auth_mode" ) == 0 )
1785 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001786 if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
Paul Bakker91ebfb52012-11-23 14:04:08 +01001787 goto usage;
1788 }
Janos Follath4817e272017-04-10 13:44:33 +01001789 else if( strcmp( p, "cert_req_ca_list" ) == 0 )
1790 {
1791 opt.cert_req_ca_list = atoi( q );
1792 if( opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1 )
1793 goto usage;
1794 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001795 else if( strcmp( p, "max_frag_len" ) == 0 )
1796 {
1797 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001799 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001801 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001803 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001805 else
1806 goto usage;
1807 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001808 else if( strcmp( p, "alpn" ) == 0 )
1809 {
1810 opt.alpn_string = q;
1811 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001812 else if( strcmp( p, "trunc_hmac" ) == 0 )
1813 {
1814 switch( atoi( q ) )
1815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1817 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001818 default: goto usage;
1819 }
1820 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001821 else if( strcmp( p, "extended_ms" ) == 0 )
1822 {
1823 switch( atoi( q ) )
1824 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001825 case 0:
1826 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1827 break;
1828 case 1:
1829 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1830 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001831 default: goto usage;
1832 }
1833 }
Jarno Lamsa41b35912019-06-10 15:51:11 +03001834 else if( strcmp( p, "enforce_extended_master_secret" ) == 0 )
1835 {
1836 switch( atoi( q ) )
1837 {
1838 case 0:
1839 opt.enforce_extended_master_secret =
1840 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
1841 break;
1842 case 1:
1843 opt.enforce_extended_master_secret =
1844 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_ENABLED;
1845 break;
1846 default: goto usage;
1847 }
1848 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001849 else if( strcmp( p, "etm" ) == 0 )
1850 {
1851 switch( atoi( q ) )
1852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1854 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001855 default: goto usage;
1856 }
1857 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001858 else if( strcmp( p, "tickets" ) == 0 )
1859 {
1860 opt.tickets = atoi( q );
1861 if( opt.tickets < 0 || opt.tickets > 1 )
1862 goto usage;
1863 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001864 else if( strcmp( p, "ticket_timeout" ) == 0 )
1865 {
1866 opt.ticket_timeout = atoi( q );
1867 if( opt.ticket_timeout < 0 )
1868 goto usage;
1869 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001870 else if( strcmp( p, "cache_max" ) == 0 )
1871 {
1872 opt.cache_max = atoi( q );
1873 if( opt.cache_max < 0 )
1874 goto usage;
1875 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001876 else if( strcmp( p, "cache_timeout" ) == 0 )
1877 {
1878 opt.cache_timeout = atoi( q );
1879 if( opt.cache_timeout < 0 )
1880 goto usage;
1881 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001882 else if( strcmp( p, "cookies" ) == 0 )
1883 {
1884 opt.cookies = atoi( q );
1885 if( opt.cookies < -1 || opt.cookies > 1)
1886 goto usage;
1887 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001888 else if( strcmp( p, "anti_replay" ) == 0 )
1889 {
1890 opt.anti_replay = atoi( q );
1891 if( opt.anti_replay < 0 || opt.anti_replay > 1)
1892 goto usage;
1893 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001894 else if( strcmp( p, "badmac_limit" ) == 0 )
1895 {
1896 opt.badmac_limit = atoi( q );
1897 if( opt.badmac_limit < 0 )
1898 goto usage;
1899 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001900 else if( strcmp( p, "hs_timeout" ) == 0 )
1901 {
1902 if( ( p = strchr( q, '-' ) ) == NULL )
1903 goto usage;
1904 *p++ = '\0';
1905 opt.hs_to_min = atoi( q );
1906 opt.hs_to_max = atoi( p );
1907 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1908 goto usage;
1909 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001910 else if( strcmp( p, "mtu" ) == 0 )
1911 {
1912 opt.dtls_mtu = atoi( q );
1913 if( opt.dtls_mtu < 0 )
1914 goto usage;
1915 }
Hanno Beckere7675d02018-08-14 13:28:56 +01001916 else if( strcmp( p, "dgram_packing" ) == 0 )
1917 {
1918 opt.dgram_packing = atoi( q );
1919 if( opt.dgram_packing != 0 &&
1920 opt.dgram_packing != 1 )
1921 {
1922 goto usage;
1923 }
1924 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001925 else if( strcmp( p, "sni" ) == 0 )
1926 {
1927 opt.sni = q;
1928 }
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +01001929 else if( strcmp( p, "query_config" ) == 0 )
1930 {
1931 return query_config( q );
1932 }
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03001933 else if( strcmp( p, "serialize") == 0 )
1934 {
1935 opt.serialize = atoi( q );
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03001936 if( opt.serialize < 0 || opt.serialize > 2)
Jarno Lamsa0ea3cfe2019-05-29 13:33:32 +03001937 goto usage;
1938 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001939 else
1940 goto usage;
1941 }
1942
Hanno Becker16970d22017-10-10 15:56:37 +01001943 /* Event-driven IO is incompatible with the above custom
1944 * receive and send functions, as the polling builds on
1945 * refers to the underlying net_context. */
1946 if( opt.event == 1 && opt.nbio != 1 )
1947 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001948 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001949 opt.nbio = 1;
1950 }
1951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952#if defined(MBEDTLS_DEBUG_C)
1953 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001954#endif
Andrzej Kurekda4029d2018-06-20 07:07:55 -04001955 buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001956 if( buf == NULL )
1957 {
Andrzej Kurek755890f2018-06-20 08:17:04 -04001958 mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001959 ret = 3;
1960 goto exit;
1961 }
Paul Bakkerc73079a2014-04-25 16:34:30 +02001962
Paul Bakkerc1516be2013-06-29 16:01:32 +02001963 if( opt.force_ciphersuite[0] > 0 )
1964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001966 ciphersuite_info =
1967 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001968
Paul Bakker5b55b792013-07-19 13:43:43 +02001969 if( opt.max_version != -1 &&
1970 ciphersuite_info->min_minor_ver > opt.max_version )
1971 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001972 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker5b55b792013-07-19 13:43:43 +02001973 ret = 2;
1974 goto usage;
1975 }
1976 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001977 ciphersuite_info->max_minor_ver < opt.min_version )
1978 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001979 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001980 ret = 2;
1981 goto usage;
1982 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001983
1984 /* If we select a version that's not supported by
1985 * this suite, then there will be no common ciphersuite... */
1986 if( opt.max_version == -1 ||
1987 opt.max_version > ciphersuite_info->max_minor_ver )
1988 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001989 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001990 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001991 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001992 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001993 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001994 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1996 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1997 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001998 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001999
2000 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002006 ret = 2;
2007 goto usage;
2008 }
2009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002011 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02002012 }
2013
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002014 if( opt.version_suites != NULL )
2015 {
2016 const char *name[4] = { 0 };
2017
2018 /* Parse 4-element coma-separated list */
2019 for( i = 0, p = (char *) opt.version_suites;
2020 i < 4 && *p != '\0';
2021 i++ )
2022 {
2023 name[i] = p;
2024
2025 /* Terminate the current string and move on to next one */
2026 while( *p != ',' && *p != '\0' )
2027 p++;
2028 if( *p == ',' )
2029 *p++ = '\0';
2030 }
2031
2032 if( i != 4 )
2033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 mbedtls_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002035 ret = 1;
2036 goto exit;
2037 }
2038
2039 memset( version_suites, 0, sizeof( version_suites ) );
2040
2041 /* Get the suites identifiers from their name */
2042 for( i = 0; i < 4; i++ )
2043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002045
2046 if( version_suites[i][0] == 0 )
2047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048 mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002049 ret = 2;
2050 goto usage;
2051 }
2052 }
2053 }
2054
Hanno Beckera5a2b082019-05-15 14:03:01 +01002055#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01002056 if( unhexify( cid, opt.cid_val, &cid_len ) != 0 )
2057 {
2058 mbedtls_printf( "CID not valid hex\n" );
2059 goto exit;
2060 }
Hanno Becker1029ace2019-04-09 17:28:10 +01002061
Hanno Becker96870292019-05-03 17:30:59 +01002062 /* Keep CID settings for renegotiation unless
2063 * specified otherwise. */
2064 if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
2065 opt.cid_enabled_renego = opt.cid_enabled;
2066 if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
2067 opt.cid_val_renego = opt.cid_val;
2068
2069 if( unhexify( cid_renego, opt.cid_val_renego, &cid_renego_len ) != 0 )
2070 {
2071 mbedtls_printf( "CID not valid hex\n" );
2072 goto exit;
2073 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002074#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1029ace2019-04-09 17:28:10 +01002075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002077 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002078 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02002079 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002080 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 mbedtls_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002083 goto exit;
2084 }
2085
2086 if( opt.psk_list != NULL )
2087 {
2088 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 mbedtls_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002091 goto exit;
2092 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002093 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002095
Hanno Beckere6706e62017-05-15 16:05:15 +01002096#if defined(MBEDTLS_ECP_C)
2097 if( opt.curves != NULL )
2098 {
2099 p = (char *) opt.curves;
2100 i = 0;
2101
2102 if( strcmp( p, "none" ) == 0 )
2103 {
2104 curve_list[0] = MBEDTLS_ECP_DP_NONE;
2105 }
2106 else if( strcmp( p, "default" ) != 0 )
2107 {
2108 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01002109 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002110 {
2111 q = p;
2112
2113 /* Terminate the current string */
2114 while( *p != ',' && *p != '\0' )
2115 p++;
2116 if( *p == ',' )
2117 *p++ = '\0';
2118
2119 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
2120 {
2121 curve_list[i++] = curve_cur->grp_id;
2122 }
2123 else
2124 {
2125 mbedtls_printf( "unknown curve %s\n", q );
2126 mbedtls_printf( "supported curves: " );
2127 for( curve_cur = mbedtls_ecp_curve_list();
2128 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
2129 curve_cur++ )
2130 {
2131 mbedtls_printf( "%s ", curve_cur->name );
2132 }
2133 mbedtls_printf( "\n" );
2134 goto exit;
2135 }
2136 }
2137
2138 mbedtls_printf("Number of curves: %d\n", i );
2139
Hanno Becker8651a432017-06-09 16:13:22 +01002140 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01002141 {
Hanno Becker8651a432017-06-09 16:13:22 +01002142 mbedtls_printf( "curves list too long, maximum %d",
2143 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01002144 goto exit;
2145 }
2146
2147 curve_list[i] = MBEDTLS_ECP_DP_NONE;
2148 }
2149 }
2150#endif /* MBEDTLS_ECP_C */
2151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002153 if( opt.alpn_string != NULL )
2154 {
2155 p = (char *) opt.alpn_string;
2156 i = 0;
2157
2158 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01002159 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002160 {
2161 alpn_list[i++] = p;
2162
2163 /* Terminate the current string and move on to next one */
2164 while( *p != ',' && *p != '\0' )
2165 p++;
2166 if( *p == ',' )
2167 *p++ = '\0';
2168 }
2169 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002171
Paul Bakkerfbb17802013-04-17 19:10:21 +02002172 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002173 * 0. Initialize the RNG and the session data
2174 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002176 fflush( stdout );
2177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 mbedtls_entropy_init( &entropy );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002179 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
2180 &entropy, (const unsigned char *) pers,
2181 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002182 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002183 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
2184 -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002185 goto exit;
2186 }
2187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002191 /*
2192 * 1.1. Load the trusted CA
2193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002195 fflush( stdout );
2196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002198 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002199 if( strcmp( opt.ca_path, "none" ) == 0 )
2200 ret = 0;
2201 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002203 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002204 if( strcmp( opt.ca_file, "none" ) == 0 )
2205 ret = 0;
2206 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02002208 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002209#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210#if defined(MBEDTLS_CERTS_C)
2211 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 ret = mbedtls_x509_crt_parse( &cacert,
2214 (const unsigned char *) mbedtls_test_cas[i],
2215 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002216 if( ret != 0 )
2217 break;
2218 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002219#else
2220 {
2221 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002223 }
2224#endif
2225 if( ret < 0 )
2226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002228 goto exit;
2229 }
2230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002232
2233 /*
2234 * 1.2. Load own certificate and private key
2235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002237 fflush( stdout );
2238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002240 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002241 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002242 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002246 -ret );
2247 goto exit;
2248 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002249 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002250 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002251 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002252 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002256 goto exit;
2257 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002258 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002259 if( key_cert_init == 1 )
2260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002262 goto exit;
2263 }
2264
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002265 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002266 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002267 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002271 -ret );
2272 goto exit;
2273 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002274 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002275 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002276 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002277 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002281 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002282 goto exit;
2283 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002284 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002285 if( key_cert_init2 == 1 )
2286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002288 goto exit;
2289 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002290#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002291 if( key_cert_init == 0 &&
2292 strcmp( opt.crt_file, "none" ) != 0 &&
2293 strcmp( opt.key_file, "none" ) != 0 &&
2294 key_cert_init2 == 0 &&
2295 strcmp( opt.crt_file2, "none" ) != 0 &&
2296 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298#if !defined(MBEDTLS_CERTS_C)
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002299 mbedtls_printf( "Not certificated or key provided, and \nMBEDTLS_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002300 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002301#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302#if defined(MBEDTLS_RSA_C)
2303 if( ( ret = mbedtls_x509_crt_parse( &srvcert,
2304 (const unsigned char *) mbedtls_test_srv_crt_rsa,
2305 mbedtls_test_srv_crt_rsa_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002306 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002307 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
2308 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002309 goto exit;
2310 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 if( ( ret = mbedtls_pk_parse_key( &pkey,
2312 (const unsigned char *) mbedtls_test_srv_key_rsa,
2313 mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002314 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002315 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
2316 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002317 goto exit;
2318 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002319 key_cert_init = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320#endif /* MBEDTLS_RSA_C */
2321#if defined(MBEDTLS_ECDSA_C)
2322 if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
2323 (const unsigned char *) mbedtls_test_srv_crt_ec,
2324 mbedtls_test_srv_crt_ec_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002325 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002326 mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n",
2327 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002328 goto exit;
2329 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 if( ( ret = mbedtls_pk_parse_key( &pkey2,
2331 (const unsigned char *) mbedtls_test_srv_key_ec,
2332 mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002333 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002334 mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n",
2335 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002336 goto exit;
2337 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002338 key_cert_init2 = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339#endif /* MBEDTLS_ECDSA_C */
2340#endif /* MBEDTLS_CERTS_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002341 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 mbedtls_printf( " ok\n" );
2344#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002347 if( opt.dhm_file != NULL )
2348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 mbedtls_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002350 fflush( stdout );
2351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002355 -ret );
2356 goto exit;
2357 }
2358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002360 }
2361#endif
2362
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002363#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002364 if( opt.sni != NULL )
2365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 mbedtls_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002367 fflush( stdout );
2368
2369 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
2370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002372 goto exit;
2373 }
2374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002376 }
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002377#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002378
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002379 /*
2380 * 2. Setup the listening TCP socket
2381 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02002382 mbedtls_printf( " . Bind on %s://%s:%s/ ...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002384 opt.server_addr ? opt.server_addr : "*",
2385 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002386 fflush( stdout );
2387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
2389 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2390 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002393 goto exit;
2394 }
2395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002397
2398 /*
2399 * 3. Setup stuff
2400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002402 fflush( stdout );
2403
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02002404 if( ( ret = mbedtls_ssl_config_defaults( &conf,
2405 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02002406 opt.transport,
2407 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002408 {
2409 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
2410 goto exit;
2411 }
2412
Gilles Peskineef86ab22017-05-05 18:59:02 +02002413#if defined(MBEDTLS_X509_CRT_PARSE_C)
2414 /* The default algorithms profile disables SHA-1, but our tests still
2415 rely on it heavily. Hence we allow it here. A real-world server
2416 should use the default profile unless there is a good reason not to. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02002417 if( opt.allow_sha1 > 0 )
2418 {
2419 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
2420 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
Gilles Peskine682df092017-05-11 19:01:11 +02002421 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
Gilles Peskinebc70a182017-05-09 15:59:24 +02002422 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02002423#endif /* MBEDTLS_X509_CRT_PARSE_C */
2424
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01002425 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002426 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002427
Janos Follath4817e272017-04-10 13:44:33 +01002428 if( opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST )
2429 mbedtls_ssl_conf_cert_req_ca_list( &conf, opt.cert_req_ca_list );
2430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002432 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 +02002433 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002434
Hanno Beckere7675d02018-08-14 13:28:56 +01002435 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01002436 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002440 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002441 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002442 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002443 goto exit;
2444 };
Paul Bakker05decb22013-08-15 13:33:48 +02002445#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002446
Hanno Beckera5a2b082019-05-15 14:03:01 +01002447#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01002448 if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
Hanno Beckereec2be92019-05-03 13:06:44 +01002449 {
Hanno Becker96870292019-05-03 17:30:59 +01002450 if( opt.cid_enabled == 1 &&
2451 opt.cid_enabled_renego == 1 &&
2452 cid_len != cid_renego_len )
2453 {
2454 mbedtls_printf( "CID length must not change during renegotiation\n" );
2455 goto usage;
2456 }
2457
2458 if( opt.cid_enabled == 1 )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002459 ret = mbedtls_ssl_conf_cid( &conf, cid_len,
2460 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Becker96870292019-05-03 17:30:59 +01002461 else
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002462 ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
2463 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
Hanno Becker96870292019-05-03 17:30:59 +01002464
Hanno Beckereec2be92019-05-03 13:06:44 +01002465 if( ret != 0 )
2466 {
Hanno Becker76581052019-05-23 16:58:22 +01002467 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
2468 -ret );
Hanno Beckereec2be92019-05-03 13:06:44 +01002469 goto exit;
2470 }
2471 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002472#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckereec2be92019-05-03 13:06:44 +01002473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002475 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002476 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002477#endif
2478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002480 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002481 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Jarno Lamsa41b35912019-06-10 15:51:11 +03002482 if( opt.enforce_extended_master_secret != DFL_EXTENDED_MS_ENFORCE )
2483 mbedtls_ssl_conf_extended_master_secret_enforce( &conf,
2484 opt.enforce_extended_master_secret );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002485#endif
2486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002488 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002489 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002490#endif
2491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002493 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002494 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002495 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002496 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002497 goto exit;
2498 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002499#endif
2500
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002501 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
2502 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002505 if( opt.cache_max != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002507
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002508 if( opt.cache_timeout != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002510
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002511 mbedtls_ssl_conf_session_cache( &conf, &cache,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01002512 mbedtls_ssl_cache_get,
2513 mbedtls_ssl_cache_set );
Paul Bakker0a597072012-09-25 21:55:46 +00002514#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002517 if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002518 {
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002519 if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
2520 mbedtls_ctr_drbg_random, &ctr_drbg,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +02002521 MBEDTLS_CIPHER_AES_256_GCM,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002522 opt.ticket_timeout ) ) != 0 )
2523 {
2524 mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
2525 goto exit;
2526 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01002527
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002528 mbedtls_ssl_conf_session_tickets_cb( &conf,
2529 mbedtls_ssl_ticket_write,
2530 mbedtls_ssl_ticket_parse,
2531 &ticket_ctx );
2532 }
Paul Bakkera503a632013-08-14 13:48:06 +02002533#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535#if defined(MBEDTLS_SSL_PROTO_DTLS)
2536 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002539 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
2542 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002545 goto exit;
2546 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002547
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002548 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002549 &cookie_ctx );
2550 }
2551 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552#endif /* MBEDTLS_SSL_COOKIE_C */
2553#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002554 if( opt.cookies == 0 )
2555 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002556 mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002557 }
2558 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002560 {
2561 ; /* Nothing to do */
2562 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002565 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002566 mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002567#endif
2568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002570 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002571 mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002572#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002573 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002575
Paul Bakker41c83d32013-03-20 14:39:14 +01002576 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002577 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002578
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002579#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002580 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002581 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002582#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002583
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002584 if( opt.version_suites != NULL )
2585 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002586 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 MBEDTLS_SSL_MAJOR_VERSION_3,
2588 MBEDTLS_SSL_MINOR_VERSION_0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002589 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 MBEDTLS_SSL_MAJOR_VERSION_3,
2591 MBEDTLS_SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002592 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593 MBEDTLS_SSL_MAJOR_VERSION_3,
2594 MBEDTLS_SSL_MINOR_VERSION_2 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002595 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 MBEDTLS_SSL_MAJOR_VERSION_3,
2597 MBEDTLS_SSL_MINOR_VERSION_3 );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002598 }
2599
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002600 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002601 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002603 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002604
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02002605 if( opt.renego_delay != DFL_RENEGO_DELAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002606 mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002607
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002608 if( opt.renego_period != DFL_RENEGO_PERIOD )
2609 {
Andres AG692ad842017-01-19 16:30:57 +00002610 PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002611 mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002612 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002613#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002616 if( strcmp( opt.ca_path, "none" ) != 0 &&
2617 strcmp( opt.ca_file, "none" ) != 0 )
2618 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002619 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002620 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002621 if( key_cert_init )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002622 {
2623 mbedtls_pk_context *pk = &pkey;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002624#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002625 if( opt.async_private_delay1 >= 0 )
2626 {
Gilles Peskine44817442018-06-13 18:09:28 +02002627 ret = ssl_async_set_key( &ssl_async_keys, &srvcert, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002628 opt.async_private_delay1 );
2629 if( ret < 0 )
2630 {
2631 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2632 ret );
2633 goto exit;
2634 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002635 pk = NULL;
2636 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002637#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002638 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002639 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002640 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002641 goto exit;
2642 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002643 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002644 if( key_cert_init2 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002645 {
2646 mbedtls_pk_context *pk = &pkey2;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002647#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002648 if( opt.async_private_delay2 >= 0 )
2649 {
Gilles Peskine44817442018-06-13 18:09:28 +02002650 ret = ssl_async_set_key( &ssl_async_keys, &srvcert2, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002651 opt.async_private_delay2 );
2652 if( ret < 0 )
2653 {
2654 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2655 ret );
2656 goto exit;
2657 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002658 pk = NULL;
2659 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002660#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002661 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002662 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002663 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002664 goto exit;
2665 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002666 }
2667
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002668#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002669 if( opt.async_operations[0] != '-' )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002670 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002671 mbedtls_ssl_async_sign_t *sign = NULL;
2672 mbedtls_ssl_async_decrypt_t *decrypt = NULL;
Gilles Peskine12ab5d42018-04-24 12:32:04 +02002673 const char *r;
2674 for( r = opt.async_operations; *r; r++ )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002675 {
Gilles Peskine12ab5d42018-04-24 12:32:04 +02002676 switch( *r )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002677 {
2678 case 'd':
2679 decrypt = ssl_async_decrypt;
2680 break;
2681 case 's':
2682 sign = ssl_async_sign;
2683 break;
2684 }
2685 }
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002686 ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
2687 - opt.async_private_error :
2688 opt.async_private_error );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002689 ssl_async_keys.f_rng = mbedtls_ctr_drbg_random;
2690 ssl_async_keys.p_rng = &ctr_drbg;
2691 mbedtls_ssl_conf_async_private_cb( &conf,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002692 sign,
2693 decrypt,
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002694 ssl_async_resume,
2695 ssl_async_cancel,
2696 &ssl_async_keys );
2697 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002698#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002699#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +02002700
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002701#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002702 if( opt.sni != NULL )
Gilles Peskine166ce742018-04-30 10:30:49 +02002703 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002704 mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
Gilles Peskine166ce742018-04-30 10:30:49 +02002705#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
2706 if( opt.async_private_delay2 >= 0 )
2707 {
Gilles Peskinee2479892018-06-13 18:06:51 +02002708 sni_entry *cur;
2709 for( cur = sni_info; cur != NULL; cur = cur->next )
Gilles Peskine166ce742018-04-30 10:30:49 +02002710 {
Gilles Peskinee2479892018-06-13 18:06:51 +02002711 ret = ssl_async_set_key( &ssl_async_keys,
Gilles Peskine44817442018-06-13 18:09:28 +02002712 cur->cert, cur->key, 1,
Gilles Peskinee2479892018-06-13 18:06:51 +02002713 opt.async_private_delay2 );
2714 if( ret < 0 )
2715 {
2716 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2717 ret );
2718 goto exit;
2719 }
2720 cur->key = NULL;
Gilles Peskine166ce742018-04-30 10:30:49 +02002721 }
Gilles Peskine166ce742018-04-30 10:30:49 +02002722 }
2723#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
2724 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002725#endif
2726
Hanno Beckere6706e62017-05-15 16:05:15 +01002727#if defined(MBEDTLS_ECP_C)
2728 if( opt.curves != NULL &&
2729 strcmp( opt.curves, "default" ) != 0 )
2730 {
2731 mbedtls_ssl_conf_curves( &conf, curve_list );
2732 }
2733#endif
2734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002736 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
2737 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002738 ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002739 (const unsigned char *) opt.psk_identity,
2740 strlen( opt.psk_identity ) );
2741 if( ret != 0 )
2742 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002743 mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002744 goto exit;
2745 }
2746 }
2747
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002748 if( opt.psk_list != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002749 mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02002750#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752#if defined(MBEDTLS_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00002753 /*
2754 * Use different group than default DHM group
2755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002757 if( opt.dhm_file != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002758 ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002759#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002760 if( ret != 0 )
2761 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002762 mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002763 goto exit;
2764 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002765#endif
2766
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002767 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002768 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00002769
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002770 if( opt.max_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002771 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002772
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002773 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
2774 {
2775 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
2776 goto exit;
2777 }
2778
2779 if( opt.nbio == 2 )
2780 mbedtls_ssl_set_bio( &ssl, &client_fd, my_send, my_recv, NULL );
2781 else
2782 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02002783 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002784
Hanno Beckera5a2b082019-05-15 14:03:01 +01002785#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1029ace2019-04-09 17:28:10 +01002786 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2787 {
2788 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
2789 cid, cid_len ) ) != 0 )
2790 {
2791 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
2792 ret );
2793 goto exit;
2794 }
2795 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002796#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1029ace2019-04-09 17:28:10 +01002797
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002798#if defined(MBEDTLS_SSL_PROTO_DTLS)
2799 if( opt.dtls_mtu != DFL_DTLS_MTU )
2800 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
2801#endif
2802
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002803#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002804 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
2805 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002806#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002809
2810reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002811#if !defined(_WIN32)
2812 if( received_sigterm )
2813 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01002814 mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" );
2815 if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT )
2816 ret = 0;
2817
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002818 goto exit;
2819 }
2820#endif
2821
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002822 if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
2823 {
2824 mbedtls_printf( " ! Client initiated reconnection from same port\n" );
2825 goto handshake;
2826 }
2827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002829 if( ret != 0 )
2830 {
2831 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832 mbedtls_strerror( ret, error_buf, 100 );
2833 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002834 }
2835#endif
2836
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002837 mbedtls_net_free( &client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 mbedtls_ssl_session_reset( &ssl );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002840
2841 /*
2842 * 3. Wait until a client connects
2843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 mbedtls_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002845 fflush( stdout );
2846
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002847 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02002848 client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002849 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02002850#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002851 if( received_sigterm )
2852 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01002853 mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" );
2854 if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED )
2855 ret = 0;
2856
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002857 goto exit;
2858 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02002859#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861 mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002862 goto exit;
2863 }
2864
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002865 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002866 ret = mbedtls_net_set_nonblock( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002867 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002868 ret = mbedtls_net_set_block( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002869 if( ret != 0 )
2870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002872 goto exit;
2873 }
2874
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002875 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2878 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002879 {
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02002880 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
2881 client_ip, cliip_len ) ) != 0 )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002882 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002883 mbedtls_printf( " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n",
2884 -ret );
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002885 goto exit;
2886 }
2887 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002889
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002890#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2891 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
2892 {
2893 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
2894 (const unsigned char *) opt.ecjpake_pw,
2895 strlen( opt.ecjpake_pw ) ) ) != 0 )
2896 {
2897 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
2898 goto exit;
2899 }
2900 }
2901#endif
2902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002904
2905 /*
2906 * 4. Handshake
2907 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002908handshake:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002910 fflush( stdout );
2911
Hanno Becker16970d22017-10-10 15:56:37 +01002912 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002913 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002914#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002915 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002916 ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002917 {
2918 mbedtls_printf( " cancelling on injected error\n" );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002919 break;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002920 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002921#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskineb44692f2018-04-24 12:18:19 +02002922
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02002923 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01002924 break;
2925
2926 /* For event-driven IO, wait for socket to become available */
2927 if( opt.event == 1 /* level triggered IO */ )
2928 {
2929#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002930 ret = idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002931#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002932 ret = idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002933#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002934 if( ret != 0 )
2935 goto reset;
Hanno Becker16970d22017-10-10 15:56:37 +01002936 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002937 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941 mbedtls_printf( " hello verification requested\n" );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002942 ret = 0;
2943 goto reset;
2944 }
2945 else if( ret != 0 )
2946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002948
2949#if defined(MBEDTLS_X509_CRT_PARSE_C)
2950 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2951 {
2952 char vrfy_buf[512];
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02002953 flags = mbedtls_ssl_get_verify_result( &ssl );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002954
2955 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
2956
2957 mbedtls_printf( "%s\n", vrfy_buf );
2958 }
2959#endif
2960
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002961#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002962 if( opt.async_private_error < 0 )
2963 /* Injected error only the first time round, to test reset */
2964 ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
2965#endif
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002966 goto reset;
2967 }
2968 else /* ret == 0 */
2969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
2971 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002972 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
2975 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002976 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002978
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002979#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2980 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
2981 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
2982#endif
2983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002985 if( opt.alpn_string != NULL )
2986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
2988 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002989 alp ? alp : "(none)" );
2990 }
2991#endif
2992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002994 /*
Ron Eldor2a47be52017-06-20 15:23:23 +03002995 * 5. Verify the client certificate
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002998
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002999 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003000 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003001 char vrfy_buf[512];
3002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 mbedtls_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003004
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003005 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003006
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01003007 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003008 }
3009 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003011
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003012 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003013 {
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003014 char crt_buf[512];
3015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 mbedtls_printf( " . Peer certificate information ...\n" );
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02003017 mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018 mbedtls_ssl_get_peer_cert( &ssl ) );
Manuel Pégourié-Gonnard67557172015-07-02 11:15:48 +02003019 mbedtls_printf( "%s\n", crt_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003020 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003022
Hanno Beckera5a2b082019-05-15 14:03:01 +01003023#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01003024 ret = report_cid_usage( &ssl, "initial handshake" );
3025 if( ret != 0 )
3026 goto exit;
3027
Hanno Becker1029ace2019-04-09 17:28:10 +01003028 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3029 {
Hanno Becker96870292019-05-03 17:30:59 +01003030 if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
3031 cid_renego, cid_renego_len ) ) != 0 )
Hanno Becker1029ace2019-04-09 17:28:10 +01003032 {
Hanno Becker96870292019-05-03 17:30:59 +01003033 mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
3034 ret );
Hanno Becker1029ace2019-04-09 17:28:10 +01003035 goto exit;
3036 }
Hanno Becker1029ace2019-04-09 17:28:10 +01003037 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003038#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1029ace2019-04-09 17:28:10 +01003039
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02003040 if( opt.exchanges == 0 )
3041 goto close_notify;
3042
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003043 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003044data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003045 /*
3046 * 6. Read the HTTP Request
3047 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048 mbedtls_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003049 fflush( stdout );
3050
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003051 /*
3052 * TLS and DTLS need different reading styles (stream vs datagram)
3053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003055 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003056 do
3057 {
3058 int terminated = 0;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003059 len = opt.buffer_size - 1;
3060 memset( buf, 0, opt.buffer_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003062
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003063 if( mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003064 {
3065 if( opt.event == 1 /* level triggered IO */ )
3066 {
3067#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003068 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003069#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003070 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003071#endif
3072 }
3073
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003074 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01003075 }
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003076
3077 if( ret <= 0 )
3078 {
3079 switch( ret )
3080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3082 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003083 goto close_notify;
3084
3085 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 case MBEDTLS_ERR_NET_CONN_RESET:
3087 mbedtls_printf( " connection was reset by peer\n" );
3088 ret = MBEDTLS_ERR_NET_CONN_RESET;
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003089 goto reset;
3090
3091 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003093 goto reset;
3094 }
3095 }
3096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003098 {
3099 len = ret;
3100 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101 mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003102
3103 /* End of message should be detected according to the syntax of the
3104 * application protocol (eg HTTP), just use a dummy test here. */
3105 if( buf[len - 1] == '\n' )
3106 terminated = 1;
3107 }
3108 else
3109 {
3110 int extra_len, ori_len;
3111 unsigned char *larger_buf;
3112
3113 ori_len = ret;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02003114 extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003115
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003116 larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003117 if( larger_buf == NULL )
3118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119 mbedtls_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003120 ret = 1;
3121 goto reset;
3122 }
3123
3124 memset( larger_buf, 0, ori_len + extra_len );
3125 memcpy( larger_buf, buf, ori_len );
3126
3127 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003129 if( ret != extra_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130 mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003133 ret = 1;
3134 goto reset;
3135 }
3136
3137 larger_buf[ori_len + extra_len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138 mbedtls_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003139 ori_len + extra_len, ori_len, extra_len,
3140 (char *) larger_buf );
3141
3142 /* End of message should be detected according to the syntax of the
3143 * application protocol (eg HTTP), just use a dummy test here. */
3144 if( larger_buf[ori_len + extra_len - 1] == '\n' )
3145 terminated = 1;
3146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147 mbedtls_free( larger_buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003148 }
3149
3150 if( terminated )
3151 {
3152 ret = 0;
3153 break;
3154 }
3155 }
3156 while( 1 );
3157 }
3158 else /* Not stream, so datagram */
3159 {
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003160 len = opt.buffer_size - 1;
3161 memset( buf, 0, opt.buffer_size );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003162
Gilles Peskineb44692f2018-04-24 12:18:19 +02003163 do
Hanno Becker16970d22017-10-10 15:56:37 +01003164 {
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003165 /* Without the call to `mbedtls_ssl_check_pending`, it might
3166 * happen that the client sends application data in the same
3167 * datagram as the Finished message concluding the handshake.
3168 * In this case, the application data would be ready to be
3169 * processed while the underlying transport wouldn't signal
3170 * any further incoming data.
3171 *
3172 * See the test 'Event-driven I/O: session-id resume, UDP packing'
3173 * in tests/ssl-opt.sh.
3174 */
3175
3176 /* For event-driven IO, wait for socket to become available */
3177 if( mbedtls_ssl_check_pending( &ssl ) == 0 &&
3178 opt.event == 1 /* level triggered IO */ )
3179 {
3180#if defined(MBEDTLS_TIMING_C)
3181 idle( &client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ );
3182#else
3183 idle( &client_fd, MBEDTLS_ERR_SSL_WANT_READ );
3184#endif
3185 }
3186
Hanno Becker16970d22017-10-10 15:56:37 +01003187 ret = mbedtls_ssl_read( &ssl, buf, len );
3188
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00003189 /* Note that even if `mbedtls_ssl_check_pending` returns true,
3190 * it can happen that the subsequent call to `mbedtls_ssl_read`
3191 * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
3192 * might be discarded (e.g. because they are retransmissions). */
Hanno Becker16970d22017-10-10 15:56:37 +01003193 }
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003194 while( mbedtls_status_is_ssl_in_progress( ret ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003195
3196 if( ret <= 0 )
3197 {
3198 switch( ret )
3199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3201 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003202 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003203 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003204
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003205 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003207 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003208 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003209 }
3210
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003211 len = ret;
3212 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003214 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003215 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003216
3217 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003218 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02003219 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00003222 if( opt.renegotiate && exchanges_left == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224 mbedtls_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003225 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003228 {
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003229 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003232 goto reset;
3233 }
Hanno Becker16970d22017-10-10 15:56:37 +01003234
3235 /* For event-driven IO, wait for socket to become available */
3236 if( opt.event == 1 /* level triggered IO */ )
3237 {
3238#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003239 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003240#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003241 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003242#endif
3243 }
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003244 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003247 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003249
Hanno Beckera5a2b082019-05-15 14:03:01 +01003250#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker96870292019-05-03 17:30:59 +01003251 ret = report_cid_usage( &ssl, "after renegotiation" );
3252 if( ret != 0 )
3253 goto exit;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003254#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker96870292019-05-03 17:30:59 +01003255
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003256 /*
3257 * 7. Write the 200 Response
3258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259 mbedtls_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003260 fflush( stdout );
3261
3262 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003264
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003265 /* Add padding to the response to reach opt.response_size in length */
3266 if( opt.response_size != DFL_RESPONSE_SIZE &&
3267 len < opt.response_size )
3268 {
3269 memset( buf + len, 'B', opt.response_size - len );
3270 len += opt.response_size - len;
3271 }
3272
3273 /* Truncate if response size is smaller than the "natural" size */
3274 if( opt.response_size != DFL_RESPONSE_SIZE &&
3275 len > opt.response_size )
3276 {
3277 len = opt.response_size;
3278
3279 /* Still end with \r\n unless that's really not possible */
3280 if( len >= 2 ) buf[len - 2] = '\r';
3281 if( len >= 1 ) buf[len - 1] = '\n';
3282 }
3283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003285 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003286 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003289 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293 mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003294 goto reset;
3295 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003296
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003297 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003300 goto reset;
3301 }
Hanno Becker16970d22017-10-10 15:56:37 +01003302
3303 /* For event-driven IO, wait for socket to become available */
3304 if( opt.event == 1 /* level triggered IO */ )
3305 {
3306#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003307 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003308#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003309 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003310#endif
3311 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003312 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003313 }
3314 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003315 else /* Not stream, so datagram */
3316 {
Hanno Becker16970d22017-10-10 15:56:37 +01003317 while( 1 )
3318 {
3319 ret = mbedtls_ssl_write( &ssl, buf, len );
3320
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003321 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003322 break;
3323
3324 /* For event-driven IO, wait for socket to become available */
3325 if( opt.event == 1 /* level triggered IO */ )
3326 {
3327#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003328 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003329#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003330 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003331#endif
3332 }
3333 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003334
3335 if( ret < 0 )
3336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003338 goto reset;
3339 }
3340
3341 frags = 1;
3342 written = ret;
3343 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003344
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003345 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 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 +01003347 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01003348
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003349 /*
Jarno Lamsaf4572932019-05-29 15:41:21 +03003350 * 7b. Simulate serialize/deserialize and go back to data exchange
3351 */
Jarno Lamsacf1b6722019-06-04 11:06:31 +03003352#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003353 if( opt.serialize != 0 )
Jarno Lamsaf4572932019-05-29 15:41:21 +03003354 {
3355 size_t len;
3356 unsigned char *buf = NULL;
3357
3358 opt.serialize = 0;
3359 mbedtls_printf( " Serializing live connection..." );
3360
Jarno Lamsaf4f8ed72019-06-04 16:03:28 +03003361 ret = mbedtls_ssl_context_save( &ssl, NULL, 0, &len );
3362
3363 /* Allow stub implementation returning 0 for now */
3364 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL &&
3365 ret != 0 )
Jarno Lamsaf4572932019-05-29 15:41:21 +03003366 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003367 mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
3368 "-0x%x\n\n", -ret );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003369
3370 goto exit;
3371 }
3372
Jarno Lamsabff4a912019-06-06 10:23:16 +03003373 if( ( buf = mbedtls_calloc( 1, len ) ) == NULL )
Jarno Lamsaf4572932019-05-29 15:41:21 +03003374 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003375 mbedtls_printf( " failed\n ! Couldn't allocate buffer for "
3376 "serialized context" );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003377
3378 goto exit;
3379 }
3380
3381 if( ( ret = mbedtls_ssl_context_save( &ssl, buf, len, &len ) ) != 0 )
3382 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003383 mbedtls_printf( "failed\n ! mbedtls_ssl_context_save returned "
3384 "-0x%x\n\n", -ret );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003385
3386 goto exit;
3387 }
3388
Jarno Lamsab5ff6a42019-06-06 10:40:52 +03003389 if( opt.serialize == 2 )
3390 {
3391 mbedtls_ssl_free( &ssl );
3392
3393 mbedtls_ssl_init( &ssl );
3394
3395 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
3396 {
3397 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
3398 -ret );
3399 goto exit;
3400 }
3401
3402 if( opt.nbio == 2 )
3403 mbedtls_ssl_set_bio( &ssl, &client_fd, my_send, my_recv, NULL );
3404 else
3405 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv,
3406 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
3407
3408 }
3409
Jarno Lamsaf4572932019-05-29 15:41:21 +03003410 mbedtls_printf( " Deserializing connection..." );
3411
Jarno Lamsaf4572932019-05-29 15:41:21 +03003412 if( ( ret = mbedtls_ssl_context_load( &ssl, buf, len ) ) != 0 )
3413 {
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003414 mbedtls_printf( "failed\n ! mbedtls_ssl_context_load returned "
3415 "-0x%x\n\n", -ret );
Jarno Lamsaf4572932019-05-29 15:41:21 +03003416
3417 goto exit;
3418 }
3419 }
Jarno Lamsa5737ec92019-06-04 15:36:18 +03003420#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jarno Lamsaf4572932019-05-29 15:41:21 +03003421
3422 /*
3423 * 7c. Continue doing data exchanges?
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003424 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003425 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003426 goto data_exchange;
3427
3428 /*
3429 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003430 */
3431close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01003433
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003434 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003436 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003437 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439 mbedtls_printf( " done\n" );
Rich Evansf90016a2015-01-19 14:26:37 +00003440
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003441 goto reset;
3442
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003443 /*
3444 * Cleanup and exit
3445 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003446exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003447#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003448 if( ret != 0 )
3449 {
3450 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 mbedtls_strerror( ret, error_buf, 100 );
3452 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003453 }
3454#endif
3455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003456 mbedtls_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003457 fflush( stdout );
3458
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003459 mbedtls_net_free( &client_fd );
3460 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +02003461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3463 mbedtls_dhm_free( &dhm );
Paul Bakkera317a982014-06-18 16:44:11 +02003464#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465#if defined(MBEDTLS_X509_CRT_PARSE_C)
3466 mbedtls_x509_crt_free( &cacert );
3467 mbedtls_x509_crt_free( &srvcert );
3468 mbedtls_pk_free( &pkey );
3469 mbedtls_x509_crt_free( &srvcert2 );
3470 mbedtls_pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02003471#endif
Gilles Peskine44817442018-06-13 18:09:28 +02003472#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3473 for( i = 0; (size_t) i < ssl_async_keys.slots_used; i++ )
3474 {
3475 if( ssl_async_keys.slots[i].pk_owned )
3476 {
3477 mbedtls_pk_free( ssl_async_keys.slots[i].pk );
3478 mbedtls_free( ssl_async_keys.slots[i].pk );
3479 ssl_async_keys.slots[i].pk = NULL;
3480 }
3481 }
3482#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02003483#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003484 sni_free( sni_info );
3485#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003487 psk_free( psk_info );
3488#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3490 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003491#endif
Paul Bakkered27a042013-04-18 22:46:23 +02003492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003494 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495 mbedtls_ctr_drbg_free( &ctr_drbg );
3496 mbedtls_entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498#if defined(MBEDTLS_SSL_CACHE_C)
3499 mbedtls_ssl_cache_free( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00003500#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003501#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3502 mbedtls_ssl_ticket_free( &ticket_ctx );
3503#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003504#if defined(MBEDTLS_SSL_COOKIE_C)
3505 mbedtls_ssl_cookie_free( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02003506#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003507
Hanno Becker095d9cf2018-10-09 12:39:13 +01003508 mbedtls_free( buf );
3509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
3511#if defined(MBEDTLS_MEMORY_DEBUG)
3512 mbedtls_memory_buffer_alloc_status();
Paul Bakker82024bf2013-07-04 11:52:32 +02003513#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +02003515#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02003516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003517 mbedtls_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003518
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003519#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003521 fflush( stdout ); getchar();
3522#endif
3523
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02003524 // Shell can not handle large exit numbers -> 1 for errors
3525 if( ret < 0 )
3526 ret = 1;
3527
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003528 return( ret );
3529}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
3531 MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003532 MBEDTLS_CTR_DRBG_C */