blob: 31159661bdc01904f42dd7dd34d731a180ef48fd [file] [log] [blame]
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001/*
2 * SSL client with options
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb60b95f2012-09-25 09:05:17 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakkerb60b95f2012-09-25 09:05:17 +000045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000055#else
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020056#include <stdio.h>
Simon Butcherd3138c32016-04-27 01:26:50 +010057#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_free free
Simon Butcherd3138c32016-04-27 01:26:50 +010059#define mbedtls_time time
60#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020061#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define mbedtls_fprintf fprintf
63#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010064#define mbedtls_exit exit
65#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
66#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evans18b78c72015-02-11 14:06:19 +000067#endif
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020068
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020069#if !defined(MBEDTLS_ENTROPY_C) || \
70 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020071 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020072int main( void )
73{
74 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
75 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020076 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020077 mbedtls_exit( 0 );
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020078}
79#else
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010080
Andres AG788aa4a2016-09-14 14:32:09 +010081#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000082#include "mbedtls/ssl.h"
83#include "mbedtls/entropy.h"
84#include "mbedtls/ctr_drbg.h"
85#include "mbedtls/certs.h"
86#include "mbedtls/x509.h"
87#include "mbedtls/error.h"
88#include "mbedtls/debug.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020089#include "mbedtls/timing.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000090
Rich Evans18b78c72015-02-11 14:06:19 +000091#include <stdio.h>
92#include <stdlib.h>
93#include <string.h>
Andres AG692ad842017-01-19 16:30:57 +000094#include <stdint.h>
Andres AG0b736db2017-02-08 14:05:57 +000095
96#if !defined(_MSC_VER)
Andres AG692ad842017-01-19 16:30:57 +000097#include <inttypes.h>
Andres AG0b736db2017-02-08 14:05:57 +000098#endif
Rich Evans18b78c72015-02-11 14:06:19 +000099
100#if !defined(_WIN32)
101#include <signal.h>
102#endif
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000105#include "mbedtls/ssl_cache.h"
Paul Bakker0a597072012-09-25 21:55:46 +0000106#endif
107
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200108#if defined(MBEDTLS_SSL_TICKET_C)
109#include "mbedtls/ssl_ticket.h"
110#endif
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000113#include "mbedtls/ssl_cookie.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200114#endif
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000117#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker82024bf2013-07-04 11:52:32 +0200118#endif
119
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200120#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO)
121#define SNI_OPTION
122#endif
123
124#if defined(_WIN32)
125#include <windows.h>
126#endif
127
Simon Butchercce68be2018-07-23 14:26:09 +0100128/* Size of memory to be allocated for the heap, when using the library's memory
129 * management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */
130#define MEMORY_HEAP_SIZE 120000
131
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100132#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200133#define DFL_SERVER_PORT "4433"
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200134#define DFL_RESPONSE_SIZE -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000135#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100136#define DFL_NBIO 0
Hanno Becker16970d22017-10-10 15:56:37 +0100137#define DFL_EVENT 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200138#define DFL_READ_TIMEOUT 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000139#define DFL_CA_FILE ""
140#define DFL_CA_PATH ""
141#define DFL_CRT_FILE ""
142#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200143#define DFL_CRT_FILE2 ""
144#define DFL_KEY_FILE2 ""
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100145#define DFL_ASYNC_OPERATIONS "-"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100146#define DFL_ASYNC_PRIVATE_DELAY1 ( -1 )
147#define DFL_ASYNC_PRIVATE_DELAY2 ( -1 )
Gilles Peskine3665f1d2018-01-05 21:22:12 +0100148#define DFL_ASYNC_PRIVATE_ERROR ( 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +0200149#define DFL_PSK ""
150#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200151#define DFL_ECJPAKE_PW NULL
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200152#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000153#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200154#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100156#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100157#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200158#define DFL_RENEGO_DELAY -2
Andres AG692ad842017-01-19 16:30:57 +0000159#define DFL_RENEGO_PERIOD ( (uint64_t)-1 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200160#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200161#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200162#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000163#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +0200164#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100165#define DFL_AUTH_MODE -1
Janos Follath4817e272017-04-10 13:44:33 +0100166#define DFL_CERT_REQ_CA_LIST MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100168#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200170#define DFL_TICKET_TIMEOUT 86400
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100171#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100172#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100173#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200174#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100175#define DFL_CURVES NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200176#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200178#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200179#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200180#define DFL_HS_TO_MIN 0
181#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200182#define DFL_DTLS_MTU -1
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200183#define DFL_BADMAC_LIMIT -1
Hanno Beckere7675d02018-08-14 13:28:56 +0100184#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200185#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100186#define DFL_ETM -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000187
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200188#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
189 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
190 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
191 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
192 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
193 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
194 "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 +0200195
Paul Bakker8e714d72013-07-18 11:05:13 +0200196/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
197 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000198#define HTTP_RESPONSE \
199 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000200 "<h2>mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200201 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000202
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200203/*
204 * Size of the basic I/O buffer. Able to hold our default response.
205 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 * 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 +0200207 * if you change this value to something outside the range <= 100 or > 500
208 */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200209#define DFL_IO_BUF_LEN 200
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_X509_CRT_PARSE_C)
212#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000213#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100214 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
215 " default: \"\" (pre-loaded)\n" \
Hanno Becker1ce1a512019-05-01 11:16:28 +0100216 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100217 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
218 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
Hanno Becker1ce1a512019-05-01 11:16:28 +0100219 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100220 " 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 +0200221 " default: see note after key_file2\n" \
222 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200223 " 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 +0200224 " default: see note after key_file2\n" \
225 " key_file2=%%s default: see note below\n" \
226 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200227 " preloaded certificate(s) and key(s) are used if available\n" \
228 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
229 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000230#else
231#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200232 "\n" \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 " No file operations available (MBEDTLS_FS_IO not defined)\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200234 "\n"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200236#else
237#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200239
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200240#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100241#define USAGE_SSL_ASYNC \
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100242 " async_operations=%%c... d=decrypt, s=sign (default: -=off)\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100243 " async_private_delay1=%%d Asynchronous delay for key_file or preloaded key\n" \
Gilles Peskine166ce742018-04-30 10:30:49 +0200244 " async_private_delay2=%%d Asynchronous delay for key_file2 and sni\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100245 " default: -1 (not asynchronous)\n" \
Gilles Peskine60ee4ca2018-01-08 11:28:05 +0100246 " async_private_error=%%d Async callback error injection (default=0=none,\n" \
Gilles Peskinec9125722018-04-26 07:15:40 +0200247 " 1=start, 2=cancel, 3=resume, negative=first time only)"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100248#else
249#define USAGE_SSL_ASYNC ""
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200250#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd0d01c52018-10-25 16:49:48 +0100253#define USAGE_PSK \
254 " psk=%%s default: \"\" (in hex, without 0x)\n" \
255 " psk_list=%%s default: \"\"\n" \
Hanno Becker5ddc0632018-10-26 10:04:13 +0100256 " A list of (PSK identity, PSK value) pairs.\n" \
257 " The PSK values are in hex, without 0x.\n" \
Hanno Beckerd0d01c52018-10-25 16:49:48 +0100258 " id1,psk1[,id2,psk2[,...]]\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200259 " psk_identity=%%s default: \"Client_identity\"\n"
260#else
261#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200265#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100266 " tickets=%%d default: 1 (enabled)\n" \
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200267 " ticket_timeout=%%d default: 86400 (one day)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200268#else
269#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100273#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100274 " cache_max=%%d default: cache default (50)\n" \
275 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100276#else
277#define USAGE_CACHE ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278#endif /* MBEDTLS_SSL_CACHE_C */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100279
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200280#if defined(SNI_OPTION)
Ron Eldor24eec792019-04-04 15:02:01 +0300281#if defined(MBEDTLS_X509_CRL_PARSE_C)
282#define SNI_CRL ",crl"
283#else
284#define SNI_CRL ""
285#endif
286
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100287#define USAGE_SNI \
Ron Eldor24eec792019-04-04 15:02:01 +0300288 " sni=%%s name1,cert1,key1,ca1"SNI_CRL",auth1[,...]\n" \
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200289 " default: disabled\n"
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100290#else
291#define USAGE_SNI ""
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200292#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200295#define USAGE_MAX_FRAG_LEN \
296 " max_frag_len=%%d default: 16384 (tls default)\n" \
297 " options: 512, 1024, 2048, 4096\n"
298#else
299#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100303#define USAGE_TRUNC_HMAC \
304 " trunc_hmac=%%d default: library default\n"
305#else
306#define USAGE_TRUNC_HMAC ""
307#endif
308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200310#define USAGE_ALPN \
311 " alpn=%%s default: \"\" (disabled)\n" \
312 " example: spdy/1,http/1.1\n"
313#else
314#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200318#define USAGE_COOKIES \
319 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200320 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200321#else
322#define USAGE_COOKIES ""
323#endif
324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200326#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200327 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200328#else
329#define USAGE_ANTI_REPLAY ""
330#endif
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200333#define USAGE_BADMAC_LIMIT \
334 " badmac_limit=%%d default: (library default: disabled)\n"
335#else
336#define USAGE_BADMAC_LIMIT ""
337#endif
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200340#define USAGE_DTLS \
341 " dtls=%%d default: 0 (TLS)\n" \
342 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200343 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Beckere7675d02018-08-14 13:28:56 +0100344 " mtu=%%d default: (library default: unlimited)\n" \
345 " dgram_packing=%%d default: 1 (allowed)\n" \
346 " allow or forbid packing of multiple\n" \
347 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200348#else
349#define USAGE_DTLS ""
350#endif
351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200353#define USAGE_EMS \
354 " extended_ms=0/1 default: (library default: on)\n"
355#else
356#define USAGE_EMS ""
357#endif
358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100360#define USAGE_ETM \
361 " etm=0/1 default: (library default: on)\n"
362#else
363#define USAGE_ETM ""
364#endif
365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100367#define USAGE_RENEGO \
368 " renegotiation=%%d default: 0 (disabled)\n" \
369 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100370 " renego_delay=%%d default: -2 (library default)\n" \
Andres AG692ad842017-01-19 16:30:57 +0000371 " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100372#else
373#define USAGE_RENEGO ""
374#endif
375
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200376#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
377#define USAGE_ECJPAKE \
378 " ecjpake_pw=%%s default: none (disabled)\n"
379#else
380#define USAGE_ECJPAKE ""
381#endif
382
Hanno Beckere6706e62017-05-15 16:05:15 +0100383#if defined(MBEDTLS_ECP_C)
384#define USAGE_CURVES \
385 " curves=a,b,c,d default: \"default\" (library default)\n" \
386 " example: \"secp521r1,brainpoolP512r1\"\n" \
387 " - use \"none\" for empty list\n" \
388 " - see mbedtls_ecp_curve_list()\n" \
389 " for acceptable curve names\n"
390#else
391#define USAGE_CURVES ""
392#endif
393
Gilles Peskined135bbd2020-04-14 19:41:01 +0200394/* USAGE is arbitrarily split to stay under the portable string literal
395 * length limit: 4095 bytes in C99. */
396#define USAGE1 \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000397 "\n usage: ssl_server2 param=<>...\n" \
398 "\n acceptable parameters:\n" \
Ron Eldor71f68c42017-09-26 11:29:11 +0300399 " server_addr=%%s default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000400 " server_port=%%d default: 4433\n" \
401 " debug_level=%%d default: 0 (disabled)\n" \
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200402 " buffer_size=%%d default: 200 \n" \
403 " (minimum: 1, max: 16385)\n" \
404 " response_size=%%d default: about 152 (basic response)\n" \
405 " (minimum: 0, max: 16384)\n" \
406 " increases buffer_size if bigger\n"\
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100407 " nbio=%%d default: 0 (blocking I/O)\n" \
408 " options: 1 (non-blocking), 2 (added delays)\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100409 " event=%%d default: 0 (loop)\n" \
410 " options: 1 (level-triggered, implies nbio=1),\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200411 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100412 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200413 USAGE_DTLS \
414 USAGE_COOKIES \
415 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200416 USAGE_BADMAC_LIMIT \
Gilles Peskined135bbd2020-04-14 19:41:01 +0200417 "\n"
418#define USAGE2 \
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 \
Gilles Peskined135bbd2020-04-14 19:41:01 +0200429 "\n"
430#define USAGE3 \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100431 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100432 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200433 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200434 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100435 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100436 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100437 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100438 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200439 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200440 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100441 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100442 USAGE_CURVES \
Gilles Peskined135bbd2020-04-14 19:41:01 +0200443 "\n"
444#define USAGE4 \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100445 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200446 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200447 " min_version=%%s default: (library default: tls1)\n" \
448 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200449 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100450 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200451 "\n" \
452 " version_suites=a,b,c,d per-version ciphersuites\n" \
453 " in order from ssl3 to tls1_2\n" \
454 " default: all enabled\n" \
455 " force_ciphersuite=<name> default: all enabled\n" \
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100456 " query_config=<name> return 0 if the specified\n" \
457 " configuration macro is defined and 1\n" \
458 " otherwise. The expansion of the macro\n" \
459 " is printed if it is defined\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000460 " acceptable ciphersuite names:\n"
461
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
Simon Butcher63cb97e2018-12-06 17:43:31 +0000477
Rich Evans85b05ec2015-02-12 11:37:29 +0000478/*
479 * global options
480 */
481struct options
482{
483 const char *server_addr; /* address on which the ssl service runs */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200484 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000485 int debug_level; /* level of debugging */
486 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100487 int event; /* loop or event-driven IO? level or edge triggered? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200489 int response_size; /* pad response with header to requested size */
490 uint16_t buffer_size; /* IO buffer size */
Rich Evans85b05ec2015-02-12 11:37:29 +0000491 const char *ca_file; /* the file with the CA certificate(s) */
492 const char *ca_path; /* the path with the CA certificate(s) reside */
493 const char *crt_file; /* the file with the server certificate */
494 const char *key_file; /* the file with the server key */
495 const char *crt_file2; /* the file with the 2nd server certificate */
496 const char *key_file2; /* the file with the 2nd server key */
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100497 const char *async_operations; /* supported SSL asynchronous operations */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100498 int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
499 int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
500 int async_private_error; /* inject error in async private callback */
Rich Evans85b05ec2015-02-12 11:37:29 +0000501 const char *psk; /* the pre-shared key */
502 const char *psk_identity; /* the pre-shared key identity */
503 char *psk_list; /* list of PSK id/key pairs for callback */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200504 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000505 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
506 const char *version_suites; /* per-version ciphersuites */
507 int renegotiation; /* enable / disable renegotiation */
508 int allow_legacy; /* allow legacy renegotiation */
509 int renegotiate; /* attempt renegotiation? */
510 int renego_delay; /* delay before enforcing renegotiation */
Andres AG692ad842017-01-19 16:30:57 +0000511 uint64_t renego_period; /* period for automatic renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000512 int exchanges; /* number of data exchanges */
513 int min_version; /* minimum protocol version accepted */
514 int max_version; /* maximum protocol version accepted */
515 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200516 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000517 int auth_mode; /* verify mode for connection */
Janos Follath4817e272017-04-10 13:44:33 +0100518 int cert_req_ca_list; /* should we send the CA list? */
Rich Evans85b05ec2015-02-12 11:37:29 +0000519 unsigned char mfl_code; /* code for maximum fragment length */
520 int trunc_hmac; /* accept truncated hmac? */
521 int tickets; /* enable / disable session tickets */
522 int ticket_timeout; /* session ticket lifetime */
523 int cache_max; /* max number of session cache entries */
524 int cache_timeout; /* expiration delay of session cache entries */
525 char *sni; /* string describing sni information */
Hanno Beckere6706e62017-05-15 16:05:15 +0100526 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000527 const char *alpn_string; /* ALPN supported protocols */
528 const char *dhm_file; /* the file with the DH parameters */
529 int extended_ms; /* allow negotiation of extended MS? */
530 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000531 int transport; /* TLS or DTLS? */
532 int cookies; /* Use cookies for DTLS? -1 to break them */
533 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
534 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
535 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200536 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Hanno Beckere7675d02018-08-14 13:28:56 +0100537 int dgram_packing; /* allow/forbid datagram packing */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000538 int badmac_limit; /* Limit of records with bad MAC */
Rich Evans85b05ec2015-02-12 11:37:29 +0000539} opt;
540
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100541int query_config( const char *config );
542
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200543static void my_debug( void *ctx, int level,
544 const char *file, int line,
545 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000546{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200547 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000548
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200549 /* Extract basename from file */
550 for( p = basename = file; *p != '\0'; p++ )
551 if( *p == '/' || *p == '\\' )
552 basename = p + 1;
553
554 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000555 fflush( (FILE *) ctx );
556}
557
558/*
559 * Test recv/send functions that make sure each try returns
560 * WANT_READ/WANT_WRITE at least once before sucesseding
561 */
562static int my_recv( void *ctx, unsigned char *buf, size_t len )
563{
564 static int first_try = 1;
565 int ret;
566
567 if( first_try )
568 {
569 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100570 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000571 }
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100574 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000575 first_try = 1; /* Next call will be a new operation */
576 return( ret );
577}
578
579static int my_send( void *ctx, const unsigned char *buf, size_t len )
580{
581 static int first_try = 1;
582 int ret;
583
584 if( first_try )
585 {
586 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100587 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000588 }
589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100591 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000592 first_try = 1; /* Next call will be a new operation */
593 return( ret );
594}
595
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200596/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200597 * Return authmode from string, or -1 on error
598 */
599static int get_auth_mode( const char *s )
600{
601 if( strcmp( s, "none" ) == 0 )
602 return( MBEDTLS_SSL_VERIFY_NONE );
603 if( strcmp( s, "optional" ) == 0 )
604 return( MBEDTLS_SSL_VERIFY_OPTIONAL );
605 if( strcmp( s, "required" ) == 0 )
606 return( MBEDTLS_SSL_VERIFY_REQUIRED );
607
608 return( -1 );
609}
610
611/*
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200612 * Used by sni_parse and psk_parse to handle coma-separated lists
613 */
614#define GET_ITEM( dst ) \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100615 do \
616 { \
617 (dst) = p; \
618 while( *p != ',' ) \
619 if( ++p > end ) \
620 goto error; \
621 *p++ = '\0'; \
622 } while( 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200623
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200624#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100625typedef struct _sni_entry sni_entry;
626
627struct _sni_entry {
628 const char *name;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_x509_crt *cert;
630 mbedtls_pk_context *key;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200631 mbedtls_x509_crt* ca;
632 mbedtls_x509_crl* crl;
633 int authmode;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100634 sni_entry *next;
635};
636
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100637void sni_free( sni_entry *head )
638{
639 sni_entry *cur = head, *next;
640
641 while( cur != NULL )
642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_x509_crt_free( cur->cert );
644 mbedtls_free( cur->cert );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_pk_free( cur->key );
647 mbedtls_free( cur->key );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100648
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200649 mbedtls_x509_crt_free( cur->ca );
650 mbedtls_free( cur->ca );
Ron Eldor24eec792019-04-04 15:02:01 +0300651#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200652 mbedtls_x509_crl_free( cur->crl );
653 mbedtls_free( cur->crl );
Ron Eldor24eec792019-04-04 15:02:01 +0300654#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100655 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_free( cur );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100657 cur = next;
658 }
659}
660
661/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200662 * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
663 * into a usable sni_entry list. For ca1, crl1, auth1, the special value
664 * '-' means unset. If ca1 is unset, then crl1 is ignored too.
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000665 *
666 * Modifies the input string! This is not production quality!
667 */
668sni_entry *sni_parse( char *sni_string )
669{
670 sni_entry *cur = NULL, *new = NULL;
671 char *p = sni_string;
672 char *end = p;
Ron Eldor24eec792019-04-04 15:02:01 +0300673 char *crt_file, *key_file, *ca_file, *auth_str;
674#if defined(MBEDTLS_X509_CRL_PARSE_C)
675 char *crl_file;
676#endif
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000677
678 while( *end != '\0' )
679 ++end;
680 *end = ',';
681
682 while( p <= end )
683 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200684 if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000685 {
686 sni_free( cur );
687 return( NULL );
688 }
689
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200690 GET_ITEM( new->name );
691 GET_ITEM( crt_file );
692 GET_ITEM( key_file );
693 GET_ITEM( ca_file );
Ron Eldor24eec792019-04-04 15:02:01 +0300694#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200695 GET_ITEM( crl_file );
Ron Eldor24eec792019-04-04 15:02:01 +0300696#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200697 GET_ITEM( auth_str );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000698
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200699 if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
700 ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200701 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_x509_crt_init( new->cert );
704 mbedtls_pk_init( new->key );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
707 mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000708 goto error;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200709
710 if( strcmp( ca_file, "-" ) != 0 )
711 {
712 if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
713 goto error;
714
715 mbedtls_x509_crt_init( new->ca );
716
717 if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
718 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000719 }
720
Ron Eldor24eec792019-04-04 15:02:01 +0300721#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200722 if( strcmp( crl_file, "-" ) != 0 )
723 {
724 if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
725 goto error;
726
727 mbedtls_x509_crl_init( new->crl );
728
729 if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
730 goto error;
731 }
Ron Eldor24eec792019-04-04 15:02:01 +0300732#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200733
734 if( strcmp( auth_str, "-" ) != 0 )
735 {
736 if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
737 goto error;
738 }
739 else
740 new->authmode = DFL_AUTH_MODE;
741
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000742 new->next = cur;
743 cur = new;
744 }
745
746 return( cur );
747
748error:
749 sni_free( new );
750 sni_free( cur );
751 return( NULL );
752}
753
754/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100755 * SNI callback.
756 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100758 const unsigned char *name, size_t name_len )
759{
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200760 const sni_entry *cur = (const sni_entry *) p_info;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100761
762 while( cur != NULL )
763 {
764 if( name_len == strlen( cur->name ) &&
765 memcmp( name, cur->name, name_len ) == 0 )
766 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200767 if( cur->ca != NULL )
768 mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
769
770 if( cur->authmode != DFL_AUTH_MODE )
771 mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
772
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200773 return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100774 }
775
776 cur = cur->next;
777 }
778
779 return( -1 );
780}
781
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200782#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200785
Hanno Beckerd6028a12018-10-15 12:01:35 +0100786#define HEX2NUM( c ) \
787 do \
788 { \
789 if( (c) >= '0' && (c) <= '9' ) \
790 (c) -= '0'; \
791 else if( (c) >= 'a' && (c) <= 'f' ) \
792 (c) -= 'a' - 10; \
793 else if( (c) >= 'A' && (c) <= 'F' ) \
794 (c) -= 'A' - 10; \
795 else \
796 return( -1 ); \
797 } while( 0 )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200798
799/*
800 * Convert a hex string to bytes.
801 * Return 0 on success, -1 on error.
802 */
803int unhexify( unsigned char *output, const char *input, size_t *olen )
804{
805 unsigned char c;
806 size_t j;
807
808 *olen = strlen( input );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 if( *olen % 2 != 0 || *olen / 2 > MBEDTLS_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200810 return( -1 );
811 *olen /= 2;
812
813 for( j = 0; j < *olen * 2; j += 2 )
814 {
815 c = input[j];
816 HEX2NUM( c );
817 output[ j / 2 ] = c << 4;
818
819 c = input[j + 1];
820 HEX2NUM( c );
821 output[ j / 2 ] |= c;
822 }
823
824 return( 0 );
825}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200826
827typedef struct _psk_entry psk_entry;
828
829struct _psk_entry
830{
831 const char *name;
832 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 unsigned char key[MBEDTLS_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200834 psk_entry *next;
835};
836
837/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000838 * Free a list of psk_entry's
839 */
840void psk_free( psk_entry *head )
841{
842 psk_entry *next;
843
844 while( head != NULL )
845 {
846 next = head->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 mbedtls_free( head );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000848 head = next;
849 }
850}
851
852/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200853 * Parse a string of pairs name1,key1[,name2,key2[,...]]
854 * into a usable psk_entry list.
855 *
856 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200857 */
858psk_entry *psk_parse( char *psk_string )
859{
860 psk_entry *cur = NULL, *new = NULL;
861 char *p = psk_string;
862 char *end = p;
863 char *key_hex;
864
865 while( *end != '\0' )
866 ++end;
867 *end = ',';
868
869 while( p <= end )
870 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200871 if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +0000872 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200873
874 memset( new, 0, sizeof( psk_entry ) );
875
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200876 GET_ITEM( new->name );
877 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200878
879 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000880 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200881
882 new->next = cur;
883 cur = new;
884 }
885
886 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200887
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000888error:
889 psk_free( new );
890 psk_free( cur );
891 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200892}
893
894/*
895 * PSK callback
896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200898 const unsigned char *name, size_t name_len )
899{
900 psk_entry *cur = (psk_entry *) p_info;
901
902 while( cur != NULL )
903 {
904 if( name_len == strlen( cur->name ) &&
905 memcmp( name, cur->name, name_len ) == 0 )
906 {
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +0100907 return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200908 }
909
910 cur = cur->next;
911 }
912
913 return( -1 );
914}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200916
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200917static mbedtls_net_context listen_fd, client_fd;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200918
919/* Interruption handler to ensure clean exit (for valgrind testing) */
920#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200921static int received_sigterm = 0;
922void term_handler( int sig )
923{
924 ((void) sig);
925 received_sigterm = 1;
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200926 mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
927 mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200928}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200929#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200930
Gilles Peskine682df092017-05-11 19:01:11 +0200931#if defined(MBEDTLS_X509_CRT_PARSE_C)
932static int ssl_sig_hashes_for_test[] = {
933#if defined(MBEDTLS_SHA512_C)
934 MBEDTLS_MD_SHA512,
935 MBEDTLS_MD_SHA384,
936#endif
937#if defined(MBEDTLS_SHA256_C)
938 MBEDTLS_MD_SHA256,
939 MBEDTLS_MD_SHA224,
940#endif
941#if defined(MBEDTLS_SHA1_C)
942 /* Allow SHA-1 as we use it extensively in tests. */
943 MBEDTLS_MD_SHA1,
944#endif
945 MBEDTLS_MD_NONE
946};
947#endif /* MBEDTLS_X509_CRT_PARSE_C */
948
Gilles Peskinea36ac4f2018-04-26 08:05:02 +0200949/** Return true if \p ret is a status code indicating that there is an
950 * operation in progress on an SSL connection, and false if it indicates
951 * success or a fatal error.
952 *
953 * The possible operations in progress are:
954 *
955 * - A read, when the SSL input buffer does not contain a full message.
956 * - A write, when the SSL output buffer contains some data that has not
957 * been sent over the network yet.
958 * - An asynchronous callback that has not completed yet. */
959static int mbedtls_status_is_ssl_in_progress( int ret )
960{
961 return( ret == MBEDTLS_ERR_SSL_WANT_READ ||
962 ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
963 ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
964}
965
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200966#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100967typedef struct
968{
Gilles Peskine44817442018-06-13 18:09:28 +0200969 mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
970 mbedtls_pk_context *pk; /*!< Private key */
971 unsigned delay; /*!< Number of resume steps to go through */
972 unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100973} ssl_async_key_slot_t;
974
975typedef enum {
Gilles Peskinead28bf02018-04-26 00:19:16 +0200976 SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
977 SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
978 SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start */
979 SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
Gilles Peskinec9125722018-04-26 07:15:40 +0200980#define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100981} ssl_async_inject_error_t;
982
983typedef struct
984{
Gilles Peskinee2479892018-06-13 18:06:51 +0200985 ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100986 size_t slots_used;
987 ssl_async_inject_error_t inject_error;
988 int (*f_rng)(void *, unsigned char *, size_t);
989 void *p_rng;
990} ssl_async_key_context_t;
991
Gilles Peskined6fbfde2018-04-30 10:23:56 +0200992int ssl_async_set_key( ssl_async_key_context_t *ctx,
Gilles Peskine44817442018-06-13 18:09:28 +0200993 mbedtls_x509_crt *cert,
994 mbedtls_pk_context *pk,
995 int pk_take_ownership,
996 unsigned delay )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100997{
Gilles Peskined6fbfde2018-04-30 10:23:56 +0200998 if( ctx->slots_used >= sizeof( ctx->slots ) / sizeof( *ctx->slots ) )
999 return( -1 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001000 ctx->slots[ctx->slots_used].cert = cert;
1001 ctx->slots[ctx->slots_used].pk = pk;
1002 ctx->slots[ctx->slots_used].delay = delay;
Gilles Peskine44817442018-06-13 18:09:28 +02001003 ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001004 ++ctx->slots_used;
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001005 return( 0 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001006}
1007
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001008#define SSL_ASYNC_INPUT_MAX_SIZE 512
Gilles Peskined3268832018-04-26 06:23:59 +02001009
1010typedef enum
1011{
1012 ASYNC_OP_SIGN,
1013 ASYNC_OP_DECRYPT,
1014} ssl_async_operation_type_t;
1015/* Note that the enum above and the array below need to be kept in sync!
1016 * `ssl_async_operation_names[op]` is the name of op for each value `op`
1017 * of type `ssl_async_operation_type_t`. */
1018static const char *const ssl_async_operation_names[] =
1019{
1020 "sign",
1021 "decrypt",
1022};
1023
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001024typedef struct
1025{
Gilles Peskine6331d782018-04-26 13:27:43 +02001026 unsigned slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001027 ssl_async_operation_type_t operation_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001028 mbedtls_md_type_t md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001029 unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
1030 size_t input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001031 unsigned remaining_delay;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001032} ssl_async_operation_context_t;
1033
Gilles Peskine8f97af72018-04-26 11:46:10 +02001034static int ssl_async_start( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001035 mbedtls_x509_crt *cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001036 ssl_async_operation_type_t op_type,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001037 mbedtls_md_type_t md_alg,
1038 const unsigned char *input,
1039 size_t input_len )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001040{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001041 ssl_async_key_context_t *config_data =
1042 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskine6331d782018-04-26 13:27:43 +02001043 unsigned slot;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001044 ssl_async_operation_context_t *ctx = NULL;
Gilles Peskined3268832018-04-26 06:23:59 +02001045 const char *op_name = ssl_async_operation_names[op_type];
Gilles Peskinef1127252018-04-24 13:05:39 +02001046
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001047 {
1048 char dn[100];
Gilles Peskined5d983e2018-06-15 14:05:10 +02001049 if( mbedtls_x509_dn_gets( dn, sizeof( dn ), &cert->subject ) > 0 )
1050 mbedtls_printf( "Async %s callback: looking for DN=%s\n",
1051 op_name, dn );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001052 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001053
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001054 /* Look for a private key that matches the public key in cert.
1055 * Since this test code has the private key inside Mbed TLS,
1056 * we call mbedtls_pk_check_pair to match a private key with the
1057 * public key. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001058 for( slot = 0; slot < config_data->slots_used; slot++ )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001059 {
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001060 if( mbedtls_pk_check_pair( &cert->pk,
1061 config_data->slots[slot].pk ) == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001062 break;
1063 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001064 if( slot == config_data->slots_used )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001065 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001066 mbedtls_printf( "Async %s callback: no key matches this certificate.\n",
1067 op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001068 return( MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH );
1069 }
Gilles Peskine6331d782018-04-26 13:27:43 +02001070 mbedtls_printf( "Async %s callback: using key slot %u, delay=%u.\n",
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001071 op_name, slot, config_data->slots[slot].delay );
Gilles Peskinef1127252018-04-24 13:05:39 +02001072
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001073 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001074 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001075 mbedtls_printf( "Async %s callback: injected error\n", op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001076 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1077 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001078
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001079 if( input_len > SSL_ASYNC_INPUT_MAX_SIZE )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Gilles Peskinef1127252018-04-24 13:05:39 +02001081
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001082 ctx = mbedtls_calloc( 1, sizeof( *ctx ) );
1083 if( ctx == NULL )
1084 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1085 ctx->slot = slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001086 ctx->operation_type = op_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001087 ctx->md_alg = md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001088 memcpy( ctx->input, input, input_len );
1089 ctx->input_len = input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001090 ctx->remaining_delay = config_data->slots[slot].delay;
Gilles Peskinea668c602018-04-30 11:54:39 +02001091 mbedtls_ssl_set_async_operation_data( ssl, ctx );
Gilles Peskinef1127252018-04-24 13:05:39 +02001092
Gilles Peskineceb541b2018-04-26 06:30:45 +02001093 if( ctx->remaining_delay == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001094 return( 0 );
1095 else
1096 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1097}
1098
Gilles Peskine8f97af72018-04-26 11:46:10 +02001099static int ssl_async_sign( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001100 mbedtls_x509_crt *cert,
1101 mbedtls_md_type_t md_alg,
1102 const unsigned char *hash,
1103 size_t hash_len )
1104{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001105 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001106 ASYNC_OP_SIGN, md_alg,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001107 hash, hash_len ) );
1108}
1109
Gilles Peskine8f97af72018-04-26 11:46:10 +02001110static int ssl_async_decrypt( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001111 mbedtls_x509_crt *cert,
1112 const unsigned char *input,
1113 size_t input_len )
1114{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001115 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001116 ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001117 input, input_len ) );
1118}
1119
Gilles Peskine8f97af72018-04-26 11:46:10 +02001120static int ssl_async_resume( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001121 unsigned char *output,
1122 size_t *output_len,
1123 size_t output_size )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001124{
Gilles Peskinea668c602018-04-30 11:54:39 +02001125 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine8f97af72018-04-26 11:46:10 +02001126 ssl_async_key_context_t *config_data =
1127 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001128 ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001129 int ret;
Gilles Peskinef5a99962018-04-30 16:37:23 +02001130 const char *op_name;
Gilles Peskinef1127252018-04-24 13:05:39 +02001131
Gilles Peskineceb541b2018-04-26 06:30:45 +02001132 if( ctx->remaining_delay > 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001133 {
Gilles Peskineceb541b2018-04-26 06:30:45 +02001134 --ctx->remaining_delay;
Gilles Peskine6331d782018-04-26 13:27:43 +02001135 mbedtls_printf( "Async resume (slot %u): call %u more times.\n",
Gilles Peskineceb541b2018-04-26 06:30:45 +02001136 ctx->slot, ctx->remaining_delay );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001137 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1138 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001139
Gilles Peskined3268832018-04-26 06:23:59 +02001140 switch( ctx->operation_type )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001141 {
Gilles Peskined3268832018-04-26 06:23:59 +02001142 case ASYNC_OP_DECRYPT:
Gilles Peskined3268832018-04-26 06:23:59 +02001143 ret = mbedtls_pk_decrypt( key_slot->pk,
1144 ctx->input, ctx->input_len,
1145 output, output_len, output_size,
1146 config_data->f_rng, config_data->p_rng );
1147 break;
1148 case ASYNC_OP_SIGN:
Gilles Peskined3268832018-04-26 06:23:59 +02001149 ret = mbedtls_pk_sign( key_slot->pk,
1150 ctx->md_alg,
1151 ctx->input, ctx->input_len,
1152 output, output_len,
1153 config_data->f_rng, config_data->p_rng );
1154 break;
1155 default:
Gilles Peskine6331d782018-04-26 13:27:43 +02001156 mbedtls_printf( "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
Gilles Peskined3268832018-04-26 06:23:59 +02001157 ctx->slot, (long) ctx->operation_type );
Gilles Peskine44817442018-06-13 18:09:28 +02001158 mbedtls_free( ctx );
Gilles Peskined3268832018-04-26 06:23:59 +02001159 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1160 break;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001161 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001162
Gilles Peskinef5a99962018-04-30 16:37:23 +02001163 op_name = ssl_async_operation_names[ctx->operation_type];
1164
Gilles Peskinec9125722018-04-26 07:15:40 +02001165 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001166 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001167 mbedtls_printf( "Async resume callback: %s done but injected error\n",
1168 op_name );
Gilles Peskine2636fad2018-06-12 14:17:39 +02001169 mbedtls_free( ctx );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001170 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1171 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001172
Gilles Peskine6331d782018-04-26 13:27:43 +02001173 mbedtls_printf( "Async resume (slot %u): %s done, status=%d.\n",
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001174 ctx->slot, op_name, ret );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001175 mbedtls_free( ctx );
1176 return( ret );
1177}
1178
Gilles Peskine8f97af72018-04-26 11:46:10 +02001179static void ssl_async_cancel( mbedtls_ssl_context *ssl )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001180{
Gilles Peskinea668c602018-04-30 11:54:39 +02001181 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001182 mbedtls_printf( "Async cancel callback.\n" );
1183 mbedtls_free( ctx );
1184}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001185#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001186
Hanno Becker16970d22017-10-10 15:56:37 +01001187/*
1188 * Wait for an event from the underlying transport or the timer
1189 * (Used in event-driven IO mode).
1190 */
1191#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001192int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001193 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001194#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001195int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001196 mbedtls_timing_delay_context *timer,
1197 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001198#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001199{
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001200 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +01001201 int poll_type = 0;
1202
1203 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
1204 poll_type = MBEDTLS_NET_POLL_WRITE;
1205 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
1206 poll_type = MBEDTLS_NET_POLL_READ;
1207#if !defined(MBEDTLS_TIMING_C)
1208 else
Hanno Beckeref527962018-03-15 15:49:24 +00001209 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001210#endif
1211
1212 while( 1 )
1213 {
Hanno Becker197a91c2017-10-31 10:58:53 +00001214 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +01001215#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001216 if( timer != NULL &&
1217 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +01001218 {
Hanno Becker16970d22017-10-10 15:56:37 +01001219 break;
1220 }
Hanno Becker197a91c2017-10-31 10:58:53 +00001221#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +01001222
Hanno Becker197a91c2017-10-31 10:58:53 +00001223 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001224 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +01001225 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001226 ret = mbedtls_net_poll( fd, poll_type, 0 );
1227 if( ret < 0 )
1228 return( ret );
1229 if( ret == poll_type )
1230 break;
Hanno Becker16970d22017-10-10 15:56:37 +01001231 }
1232 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001233
1234 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001235}
1236
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001237int main( int argc, char *argv[] )
1238{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001239 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001240 int version_suites[4][2];
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001241 unsigned char* buf = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1243 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +02001244 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +02001245 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +02001246#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001247 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001248 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02001249 size_t cliip_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250#if defined(MBEDTLS_SSL_COOKIE_C)
1251 mbedtls_ssl_cookie_ctx cookie_ctx;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001252#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001253
Gilles Peskineef86ab22017-05-05 18:59:02 +02001254#if defined(MBEDTLS_X509_CRT_PARSE_C)
1255 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
1256#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 mbedtls_entropy_context entropy;
1258 mbedtls_ctr_drbg_context ctr_drbg;
1259 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001260 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001261#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001262 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001263#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001265 unsigned char renego_period[8] = { 0 };
1266#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +02001268 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 mbedtls_x509_crt cacert;
1270 mbedtls_x509_crt srvcert;
1271 mbedtls_pk_context pkey;
1272 mbedtls_x509_crt srvcert2;
1273 mbedtls_pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001274 int key_cert_init = 0, key_cert_init2 = 0;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001275#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001276 ssl_async_key_context_t ssl_async_keys;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001277#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001278#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1280 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001281#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282#if defined(MBEDTLS_SSL_CACHE_C)
1283 mbedtls_ssl_cache_context cache;
Paul Bakker0a597072012-09-25 21:55:46 +00001284#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001285#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1286 mbedtls_ssl_ticket_context ticket_ctx;
1287#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001288#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001289 sni_entry *sni_info = NULL;
1290#endif
Hanno Beckere6706e62017-05-15 16:05:15 +01001291#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +01001292 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +01001293 const mbedtls_ecp_curve_info * curve_cur;
1294#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +01001296 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001297#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Simon Butchercce68be2018-07-23 14:26:09 +01001299 unsigned char alloc_buf[MEMORY_HEAP_SIZE];
Paul Bakker82024bf2013-07-04 11:52:32 +02001300#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001301
1302 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001303 char *p, *q;
1304 const int *list;
1305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1307 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker82024bf2013-07-04 11:52:32 +02001308#endif
1309
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001310 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +02001311 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001312 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001313 mbedtls_net_init( &client_fd );
1314 mbedtls_net_init( &listen_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001315 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001316 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001317 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318#if defined(MBEDTLS_X509_CRT_PARSE_C)
1319 mbedtls_x509_crt_init( &cacert );
1320 mbedtls_x509_crt_init( &srvcert );
1321 mbedtls_pk_init( &pkey );
1322 mbedtls_x509_crt_init( &srvcert2 );
1323 mbedtls_pk_init( &pkey2 );
Gilles Peskine4d9ec4d2018-04-26 14:33:43 +02001324#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
1325 memset( &ssl_async_keys, 0, sizeof( ssl_async_keys ) );
1326#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001327#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1329 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001330#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#if defined(MBEDTLS_SSL_CACHE_C)
1332 mbedtls_ssl_cache_init( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00001333#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001334#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1335 mbedtls_ssl_ticket_init( &ticket_ctx );
1336#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +02001338 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001339#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#if defined(MBEDTLS_SSL_COOKIE_C)
1341 mbedtls_ssl_cookie_init( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001342#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001343
Paul Bakkerc1283d32014-08-18 11:05:51 +02001344#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001345 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001346 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001347 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +02001348#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001349
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001350 if( argc == 0 )
1351 {
1352 usage:
1353 if( ret == 0 )
1354 ret = 1;
1355
Gilles Peskined135bbd2020-04-14 19:41:01 +02001356 mbedtls_printf( USAGE1 );
1357 mbedtls_printf( USAGE2 );
1358 mbedtls_printf( USAGE3 );
1359 mbedtls_printf( USAGE4 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 list = mbedtls_ssl_list_ciphersuites();
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001362 while( *list )
1363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001365 list++;
1366 if( !*list )
1367 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001369 list++;
1370 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 mbedtls_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001372 goto exit;
1373 }
1374
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001375 opt.buffer_size = DFL_IO_BUF_LEN;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001376 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001377 opt.server_port = DFL_SERVER_PORT;
1378 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker16970d22017-10-10 15:56:37 +01001379 opt.event = DFL_EVENT;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001380 opt.response_size = DFL_RESPONSE_SIZE;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001381 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001382 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001383 opt.ca_file = DFL_CA_FILE;
1384 opt.ca_path = DFL_CA_PATH;
1385 opt.crt_file = DFL_CRT_FILE;
1386 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001387 opt.crt_file2 = DFL_CRT_FILE2;
1388 opt.key_file2 = DFL_KEY_FILE2;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001389 opt.async_operations = DFL_ASYNC_OPERATIONS;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001390 opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
1391 opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
1392 opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
Paul Bakkerfbb17802013-04-17 19:10:21 +02001393 opt.psk = DFL_PSK;
1394 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001395 opt.psk_list = DFL_PSK_LIST;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001396 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001397 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001398 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001399 opt.renegotiation = DFL_RENEGOTIATION;
1400 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001401 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001402 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001403 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001404 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001405 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001406 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001407 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001408 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001409 opt.auth_mode = DFL_AUTH_MODE;
Janos Follath4817e272017-04-10 13:44:33 +01001410 opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001411 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001412 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001413 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001414 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001415 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001416 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001417 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001418 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001419 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001420 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001421 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001422 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001423 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001424 opt.hs_to_min = DFL_HS_TO_MIN;
1425 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001426 opt.dtls_mtu = DFL_DTLS_MTU;
Hanno Beckere7675d02018-08-14 13:28:56 +01001427 opt.dgram_packing = DFL_DGRAM_PACKING;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001428 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001429 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001430 opt.etm = DFL_ETM;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001431
1432 for( i = 1; i < argc; i++ )
1433 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001434 p = argv[i];
1435 if( ( q = strchr( p, '=' ) ) == NULL )
1436 goto usage;
1437 *q++ = '\0';
1438
1439 if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001440 opt.server_port = q;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001441 else if( strcmp( p, "server_addr" ) == 0 )
1442 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001443 else if( strcmp( p, "dtls" ) == 0 )
1444 {
1445 int t = atoi( q );
1446 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001448 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001450 else
1451 goto usage;
1452 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001453 else if( strcmp( p, "debug_level" ) == 0 )
1454 {
1455 opt.debug_level = atoi( q );
1456 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1457 goto usage;
1458 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001459 else if( strcmp( p, "nbio" ) == 0 )
1460 {
1461 opt.nbio = atoi( q );
1462 if( opt.nbio < 0 || opt.nbio > 2 )
1463 goto usage;
1464 }
Hanno Becker16970d22017-10-10 15:56:37 +01001465 else if( strcmp( p, "event" ) == 0 )
1466 {
1467 opt.event = atoi( q );
1468 if( opt.event < 0 || opt.event > 2 )
1469 goto usage;
1470 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001471 else if( strcmp( p, "read_timeout" ) == 0 )
1472 opt.read_timeout = atoi( q );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001473 else if( strcmp( p, "buffer_size" ) == 0 )
1474 {
1475 opt.buffer_size = atoi( q );
1476 if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
1477 goto usage;
1478 }
1479 else if( strcmp( p, "response_size" ) == 0 )
1480 {
1481 opt.response_size = atoi( q );
1482 if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
1483 goto usage;
1484 if( opt.buffer_size < opt.response_size )
1485 opt.buffer_size = opt.response_size;
1486 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001487 else if( strcmp( p, "ca_file" ) == 0 )
1488 opt.ca_file = q;
1489 else if( strcmp( p, "ca_path" ) == 0 )
1490 opt.ca_path = q;
1491 else if( strcmp( p, "crt_file" ) == 0 )
1492 opt.crt_file = q;
1493 else if( strcmp( p, "key_file" ) == 0 )
1494 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001495 else if( strcmp( p, "crt_file2" ) == 0 )
1496 opt.crt_file2 = q;
1497 else if( strcmp( p, "key_file2" ) == 0 )
1498 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001499 else if( strcmp( p, "dhm_file" ) == 0 )
1500 opt.dhm_file = q;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001501#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001502 else if( strcmp( p, "async_operations" ) == 0 )
1503 opt.async_operations = q;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001504 else if( strcmp( p, "async_private_delay1" ) == 0 )
1505 opt.async_private_delay1 = atoi( q );
1506 else if( strcmp( p, "async_private_delay2" ) == 0 )
1507 opt.async_private_delay2 = atoi( q );
1508 else if( strcmp( p, "async_private_error" ) == 0 )
1509 {
1510 int n = atoi( q );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01001511 if( n < -SSL_ASYNC_INJECT_ERROR_MAX ||
1512 n > SSL_ASYNC_INJECT_ERROR_MAX )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001513 {
1514 ret = 2;
1515 goto usage;
1516 }
1517 opt.async_private_error = n;
1518 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001519#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001520 else if( strcmp( p, "psk" ) == 0 )
1521 opt.psk = q;
1522 else if( strcmp( p, "psk_identity" ) == 0 )
1523 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001524 else if( strcmp( p, "psk_list" ) == 0 )
1525 opt.psk_list = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001526 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1527 opt.ecjpake_pw = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001528 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001531
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001532 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001533 {
1534 ret = 2;
1535 goto usage;
1536 }
1537 opt.force_ciphersuite[1] = 0;
1538 }
Hanno Beckere6706e62017-05-15 16:05:15 +01001539 else if( strcmp( p, "curves" ) == 0 )
1540 opt.curves = q;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001541 else if( strcmp( p, "version_suites" ) == 0 )
1542 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001543 else if( strcmp( p, "renegotiation" ) == 0 )
1544 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001545 opt.renegotiation = (atoi( q )) ?
1546 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1547 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001548 }
1549 else if( strcmp( p, "allow_legacy" ) == 0 )
1550 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001551 switch( atoi( q ) )
1552 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001553 case -1:
1554 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1555 break;
1556 case 0:
1557 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1558 break;
1559 case 1:
1560 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1561 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001562 default: goto usage;
1563 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001564 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001565 else if( strcmp( p, "renegotiate" ) == 0 )
1566 {
1567 opt.renegotiate = atoi( q );
1568 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1569 goto usage;
1570 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001571 else if( strcmp( p, "renego_delay" ) == 0 )
1572 {
1573 opt.renego_delay = atoi( q );
1574 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001575 else if( strcmp( p, "renego_period" ) == 0 )
1576 {
Andres AG0b736db2017-02-08 14:05:57 +00001577#if defined(_MSC_VER)
1578 opt.renego_period = _strtoui64( q, NULL, 10 );
1579#else
1580 if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
1581 goto usage;
1582#endif /* _MSC_VER */
1583 if( opt.renego_period < 2 )
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001584 goto usage;
1585 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001586 else if( strcmp( p, "exchanges" ) == 0 )
1587 {
1588 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001589 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001590 goto usage;
1591 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001592 else if( strcmp( p, "min_version" ) == 0 )
1593 {
1594 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001596 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001598 else if( strcmp( q, "tls1_1" ) == 0 ||
1599 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001601 else if( strcmp( q, "tls1_2" ) == 0 ||
1602 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001604 else
1605 goto usage;
1606 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001607 else if( strcmp( p, "max_version" ) == 0 )
1608 {
1609 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001611 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001613 else if( strcmp( q, "tls1_1" ) == 0 ||
1614 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001616 else if( strcmp( q, "tls1_2" ) == 0 ||
1617 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001619 else
1620 goto usage;
1621 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001622 else if( strcmp( p, "arc4" ) == 0 )
1623 {
1624 switch( atoi( q ) )
1625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1627 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001628 default: goto usage;
1629 }
1630 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02001631 else if( strcmp( p, "allow_sha1" ) == 0 )
1632 {
1633 switch( atoi( q ) )
1634 {
1635 case 0: opt.allow_sha1 = 0; break;
1636 case 1: opt.allow_sha1 = 1; break;
1637 default: goto usage;
1638 }
1639 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001640 else if( strcmp( p, "force_version" ) == 0 )
1641 {
1642 if( strcmp( q, "ssl3" ) == 0 )
1643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1645 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001646 }
1647 else if( strcmp( q, "tls1" ) == 0 )
1648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1650 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001651 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001652 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1655 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001656 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001657 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1660 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001661 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001662 else if( strcmp( q, "dtls1" ) == 0 )
1663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1665 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1666 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001667 }
1668 else if( strcmp( q, "dtls1_2" ) == 0 )
1669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1671 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1672 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001673 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001674 else
1675 goto usage;
1676 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001677 else if( strcmp( p, "auth_mode" ) == 0 )
1678 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001679 if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
Paul Bakker91ebfb52012-11-23 14:04:08 +01001680 goto usage;
1681 }
Janos Follath4817e272017-04-10 13:44:33 +01001682 else if( strcmp( p, "cert_req_ca_list" ) == 0 )
1683 {
1684 opt.cert_req_ca_list = atoi( q );
1685 if( opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1 )
1686 goto usage;
1687 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001688 else if( strcmp( p, "max_frag_len" ) == 0 )
1689 {
1690 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001692 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001694 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001696 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001698 else
1699 goto usage;
1700 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001701 else if( strcmp( p, "alpn" ) == 0 )
1702 {
1703 opt.alpn_string = q;
1704 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001705 else if( strcmp( p, "trunc_hmac" ) == 0 )
1706 {
1707 switch( atoi( q ) )
1708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1710 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001711 default: goto usage;
1712 }
1713 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001714 else if( strcmp( p, "extended_ms" ) == 0 )
1715 {
1716 switch( atoi( q ) )
1717 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001718 case 0:
1719 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1720 break;
1721 case 1:
1722 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1723 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001724 default: goto usage;
1725 }
1726 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001727 else if( strcmp( p, "etm" ) == 0 )
1728 {
1729 switch( atoi( q ) )
1730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1732 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001733 default: goto usage;
1734 }
1735 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001736 else if( strcmp( p, "tickets" ) == 0 )
1737 {
1738 opt.tickets = atoi( q );
1739 if( opt.tickets < 0 || opt.tickets > 1 )
1740 goto usage;
1741 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001742 else if( strcmp( p, "ticket_timeout" ) == 0 )
1743 {
1744 opt.ticket_timeout = atoi( q );
1745 if( opt.ticket_timeout < 0 )
1746 goto usage;
1747 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001748 else if( strcmp( p, "cache_max" ) == 0 )
1749 {
1750 opt.cache_max = atoi( q );
1751 if( opt.cache_max < 0 )
1752 goto usage;
1753 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001754 else if( strcmp( p, "cache_timeout" ) == 0 )
1755 {
1756 opt.cache_timeout = atoi( q );
1757 if( opt.cache_timeout < 0 )
1758 goto usage;
1759 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001760 else if( strcmp( p, "cookies" ) == 0 )
1761 {
1762 opt.cookies = atoi( q );
1763 if( opt.cookies < -1 || opt.cookies > 1)
1764 goto usage;
1765 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001766 else if( strcmp( p, "anti_replay" ) == 0 )
1767 {
1768 opt.anti_replay = atoi( q );
1769 if( opt.anti_replay < 0 || opt.anti_replay > 1)
1770 goto usage;
1771 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001772 else if( strcmp( p, "badmac_limit" ) == 0 )
1773 {
1774 opt.badmac_limit = atoi( q );
1775 if( opt.badmac_limit < 0 )
1776 goto usage;
1777 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001778 else if( strcmp( p, "hs_timeout" ) == 0 )
1779 {
1780 if( ( p = strchr( q, '-' ) ) == NULL )
1781 goto usage;
1782 *p++ = '\0';
1783 opt.hs_to_min = atoi( q );
1784 opt.hs_to_max = atoi( p );
1785 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1786 goto usage;
1787 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001788 else if( strcmp( p, "mtu" ) == 0 )
1789 {
1790 opt.dtls_mtu = atoi( q );
1791 if( opt.dtls_mtu < 0 )
1792 goto usage;
1793 }
Hanno Beckere7675d02018-08-14 13:28:56 +01001794 else if( strcmp( p, "dgram_packing" ) == 0 )
1795 {
1796 opt.dgram_packing = atoi( q );
1797 if( opt.dgram_packing != 0 &&
1798 opt.dgram_packing != 1 )
1799 {
1800 goto usage;
1801 }
1802 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001803 else if( strcmp( p, "sni" ) == 0 )
1804 {
1805 opt.sni = q;
1806 }
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +01001807 else if( strcmp( p, "query_config" ) == 0 )
1808 {
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +02001809 mbedtls_exit( query_config( q ) );
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +01001810 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001811 else
1812 goto usage;
1813 }
1814
Hanno Becker16970d22017-10-10 15:56:37 +01001815 /* Event-driven IO is incompatible with the above custom
1816 * receive and send functions, as the polling builds on
1817 * refers to the underlying net_context. */
1818 if( opt.event == 1 && opt.nbio != 1 )
1819 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001820 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001821 opt.nbio = 1;
1822 }
1823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824#if defined(MBEDTLS_DEBUG_C)
1825 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001826#endif
Andrzej Kurekda4029d2018-06-20 07:07:55 -04001827 buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001828 if( buf == NULL )
1829 {
Andrzej Kurek755890f2018-06-20 08:17:04 -04001830 mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001831 ret = 3;
1832 goto exit;
1833 }
Paul Bakkerc73079a2014-04-25 16:34:30 +02001834
Paul Bakkerc1516be2013-06-29 16:01:32 +02001835 if( opt.force_ciphersuite[0] > 0 )
1836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001838 ciphersuite_info =
1839 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001840
Paul Bakker5b55b792013-07-19 13:43:43 +02001841 if( opt.max_version != -1 &&
1842 ciphersuite_info->min_minor_ver > opt.max_version )
1843 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001844 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker5b55b792013-07-19 13:43:43 +02001845 ret = 2;
1846 goto usage;
1847 }
1848 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001849 ciphersuite_info->max_minor_ver < opt.min_version )
1850 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001851 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001852 ret = 2;
1853 goto usage;
1854 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001855
1856 /* If we select a version that's not supported by
1857 * this suite, then there will be no common ciphersuite... */
1858 if( opt.max_version == -1 ||
1859 opt.max_version > ciphersuite_info->max_minor_ver )
1860 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001861 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001862 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001863 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001864 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001865 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001866 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1868 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1869 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001870 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001871
1872 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001878 ret = 2;
1879 goto usage;
1880 }
1881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001883 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001884 }
1885
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001886 if( opt.version_suites != NULL )
1887 {
1888 const char *name[4] = { 0 };
1889
1890 /* Parse 4-element coma-separated list */
1891 for( i = 0, p = (char *) opt.version_suites;
1892 i < 4 && *p != '\0';
1893 i++ )
1894 {
1895 name[i] = p;
1896
1897 /* Terminate the current string and move on to next one */
1898 while( *p != ',' && *p != '\0' )
1899 p++;
1900 if( *p == ',' )
1901 *p++ = '\0';
1902 }
1903
1904 if( i != 4 )
1905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 mbedtls_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001907 ret = 1;
1908 goto exit;
1909 }
1910
1911 memset( version_suites, 0, sizeof( version_suites ) );
1912
1913 /* Get the suites identifiers from their name */
1914 for( i = 0; i < 4; i++ )
1915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001917
1918 if( version_suites[i][0] == 0 )
1919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001921 ret = 2;
1922 goto usage;
1923 }
1924 }
1925 }
1926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001928 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001929 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001930 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001931 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 mbedtls_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001934 goto exit;
1935 }
1936
1937 if( opt.psk_list != NULL )
1938 {
1939 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 mbedtls_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001942 goto exit;
1943 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001944 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001946
Hanno Beckere6706e62017-05-15 16:05:15 +01001947#if defined(MBEDTLS_ECP_C)
1948 if( opt.curves != NULL )
1949 {
1950 p = (char *) opt.curves;
1951 i = 0;
1952
1953 if( strcmp( p, "none" ) == 0 )
1954 {
1955 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1956 }
1957 else if( strcmp( p, "default" ) != 0 )
1958 {
1959 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001960 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001961 {
1962 q = p;
1963
1964 /* Terminate the current string */
1965 while( *p != ',' && *p != '\0' )
1966 p++;
1967 if( *p == ',' )
1968 *p++ = '\0';
1969
1970 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1971 {
1972 curve_list[i++] = curve_cur->grp_id;
1973 }
1974 else
1975 {
1976 mbedtls_printf( "unknown curve %s\n", q );
1977 mbedtls_printf( "supported curves: " );
1978 for( curve_cur = mbedtls_ecp_curve_list();
1979 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1980 curve_cur++ )
1981 {
1982 mbedtls_printf( "%s ", curve_cur->name );
1983 }
1984 mbedtls_printf( "\n" );
1985 goto exit;
1986 }
1987 }
1988
1989 mbedtls_printf("Number of curves: %d\n", i );
1990
Hanno Becker8651a432017-06-09 16:13:22 +01001991 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001992 {
Hanno Becker8651a432017-06-09 16:13:22 +01001993 mbedtls_printf( "curves list too long, maximum %d",
1994 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001995 goto exit;
1996 }
1997
1998 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1999 }
2000 }
2001#endif /* MBEDTLS_ECP_C */
2002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002004 if( opt.alpn_string != NULL )
2005 {
2006 p = (char *) opt.alpn_string;
2007 i = 0;
2008
2009 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01002010 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002011 {
2012 alpn_list[i++] = p;
2013
2014 /* Terminate the current string and move on to next one */
2015 while( *p != ',' && *p != '\0' )
2016 p++;
2017 if( *p == ',' )
2018 *p++ = '\0';
2019 }
2020 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002022
Paul Bakkerfbb17802013-04-17 19:10:21 +02002023 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002024 * 0. Initialize the RNG and the session data
2025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002027 fflush( stdout );
2028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029 mbedtls_entropy_init( &entropy );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002030 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
2031 &entropy, (const unsigned char *) pers,
2032 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002033 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002034 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
2035 -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002036 goto exit;
2037 }
2038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002042 /*
2043 * 1.1. Load the trusted CA
2044 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002046 fflush( stdout );
2047
Hanno Becker7ae36e42019-03-05 16:22:07 +00002048 if( strcmp( opt.ca_path, "none" ) == 0 ||
2049 strcmp( opt.ca_file, "none" ) == 0 )
2050 {
2051 ret = 0;
2052 }
2053 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002055 if( strlen( opt.ca_path ) )
Hanno Becker7ae36e42019-03-05 16:22:07 +00002056 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002057 else if( strlen( opt.ca_file ) )
Hanno Becker7ae36e42019-03-05 16:22:07 +00002058 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02002059 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002060#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061#if defined(MBEDTLS_CERTS_C)
Hanno Becker38566cc2019-02-01 08:13:19 +00002062 {
2063#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 ret = mbedtls_x509_crt_parse( &cacert,
2067 (const unsigned char *) mbedtls_test_cas[i],
2068 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002069 if( ret != 0 )
2070 break;
2071 }
Hanno Becker38566cc2019-02-01 08:13:19 +00002072 if( ret == 0 )
2073#endif /* MBEDTLS_PEM_PARSE_C */
2074 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
2075 {
2076 ret = mbedtls_x509_crt_parse_der( &cacert,
2077 (const unsigned char *) mbedtls_test_cas_der[i],
2078 mbedtls_test_cas_der_len[i] );
2079 if( ret != 0 )
2080 break;
2081 }
2082 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002083#else
2084 {
2085 ret = 1;
Hanno Beckerc258c442018-12-05 16:49:42 +00002086 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002087 }
Hanno Becker38566cc2019-02-01 08:13:19 +00002088#endif /* MBEDTLS_CERTS_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002089 if( ret < 0 )
2090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002092 goto exit;
2093 }
2094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002096
2097 /*
2098 * 1.2. Load own certificate and private key
2099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002101 fflush( stdout );
2102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002104 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002105 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002106 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002110 -ret );
2111 goto exit;
2112 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002113 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002114 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002115 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002116 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002120 goto exit;
2121 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002122 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002123 if( key_cert_init == 1 )
2124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002126 goto exit;
2127 }
2128
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002129 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002130 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002131 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002135 -ret );
2136 goto exit;
2137 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002138 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002139 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002140 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002141 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002145 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002146 goto exit;
2147 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002148 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002149 if( key_cert_init2 == 1 )
2150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002152 goto exit;
2153 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002154#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002155 if( key_cert_init == 0 &&
2156 strcmp( opt.crt_file, "none" ) != 0 &&
2157 strcmp( opt.key_file, "none" ) != 0 &&
2158 key_cert_init2 == 0 &&
2159 strcmp( opt.crt_file2, "none" ) != 0 &&
2160 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162#if !defined(MBEDTLS_CERTS_C)
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002163 mbedtls_printf( "Not certificated or key provided, and \nMBEDTLS_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002164 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002165#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166#if defined(MBEDTLS_RSA_C)
2167 if( ( ret = mbedtls_x509_crt_parse( &srvcert,
2168 (const unsigned char *) mbedtls_test_srv_crt_rsa,
2169 mbedtls_test_srv_crt_rsa_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002170 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002171 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
2172 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002173 goto exit;
2174 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 if( ( ret = mbedtls_pk_parse_key( &pkey,
2176 (const unsigned char *) mbedtls_test_srv_key_rsa,
2177 mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002178 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002179 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
2180 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002181 goto exit;
2182 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002183 key_cert_init = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184#endif /* MBEDTLS_RSA_C */
2185#if defined(MBEDTLS_ECDSA_C)
2186 if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
2187 (const unsigned char *) mbedtls_test_srv_crt_ec,
2188 mbedtls_test_srv_crt_ec_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002189 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002190 mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n",
2191 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002192 goto exit;
2193 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 if( ( ret = mbedtls_pk_parse_key( &pkey2,
2195 (const unsigned char *) mbedtls_test_srv_key_ec,
2196 mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002197 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002198 mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n",
2199 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002200 goto exit;
2201 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002202 key_cert_init2 = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203#endif /* MBEDTLS_ECDSA_C */
2204#endif /* MBEDTLS_CERTS_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002205 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 mbedtls_printf( " ok\n" );
2208#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002211 if( opt.dhm_file != NULL )
2212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 mbedtls_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002214 fflush( stdout );
2215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002219 -ret );
2220 goto exit;
2221 }
2222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002224 }
2225#endif
2226
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002227#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002228 if( opt.sni != NULL )
2229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 mbedtls_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002231 fflush( stdout );
2232
2233 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
2234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002236 goto exit;
2237 }
2238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002240 }
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002241#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002242
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002243 /*
2244 * 2. Setup the listening TCP socket
2245 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02002246 mbedtls_printf( " . Bind on %s://%s:%s/ ...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002248 opt.server_addr ? opt.server_addr : "*",
2249 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002250 fflush( stdout );
2251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
2253 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2254 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002257 goto exit;
2258 }
2259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002261
2262 /*
2263 * 3. Setup stuff
2264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002266 fflush( stdout );
2267
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02002268 if( ( ret = mbedtls_ssl_config_defaults( &conf,
2269 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02002270 opt.transport,
2271 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002272 {
2273 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
2274 goto exit;
2275 }
2276
Gilles Peskineef86ab22017-05-05 18:59:02 +02002277#if defined(MBEDTLS_X509_CRT_PARSE_C)
2278 /* The default algorithms profile disables SHA-1, but our tests still
2279 rely on it heavily. Hence we allow it here. A real-world server
2280 should use the default profile unless there is a good reason not to. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02002281 if( opt.allow_sha1 > 0 )
2282 {
2283 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
2284 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
Gilles Peskine682df092017-05-11 19:01:11 +02002285 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
Gilles Peskinebc70a182017-05-09 15:59:24 +02002286 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02002287#endif /* MBEDTLS_X509_CRT_PARSE_C */
2288
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01002289 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002290 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002291
Janos Follath4817e272017-04-10 13:44:33 +01002292 if( opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST )
2293 mbedtls_ssl_conf_cert_req_ca_list( &conf, opt.cert_req_ca_list );
2294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002296 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 +02002297 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002298
Hanno Beckere7675d02018-08-14 13:28:56 +01002299 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01002300 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002304 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002305 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002306 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002307 goto exit;
2308 };
Paul Bakker05decb22013-08-15 13:33:48 +02002309#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002312 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002313 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002314#endif
2315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002317 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002318 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002319#endif
2320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002322 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002323 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002324#endif
2325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002327 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002328 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002329 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002330 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002331 goto exit;
2332 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002333#endif
2334
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002335 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
2336 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002339 if( opt.cache_max != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002341
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002342 if( opt.cache_timeout != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002344
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002345 mbedtls_ssl_conf_session_cache( &conf, &cache,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01002346 mbedtls_ssl_cache_get,
2347 mbedtls_ssl_cache_set );
Paul Bakker0a597072012-09-25 21:55:46 +00002348#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002351 if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002352 {
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002353 if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
2354 mbedtls_ctr_drbg_random, &ctr_drbg,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +02002355 MBEDTLS_CIPHER_AES_256_GCM,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002356 opt.ticket_timeout ) ) != 0 )
2357 {
2358 mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
2359 goto exit;
2360 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01002361
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002362 mbedtls_ssl_conf_session_tickets_cb( &conf,
2363 mbedtls_ssl_ticket_write,
2364 mbedtls_ssl_ticket_parse,
2365 &ticket_ctx );
2366 }
Paul Bakkera503a632013-08-14 13:48:06 +02002367#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369#if defined(MBEDTLS_SSL_PROTO_DTLS)
2370 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002373 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
2376 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002379 goto exit;
2380 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002381
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002382 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002383 &cookie_ctx );
2384 }
2385 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386#endif /* MBEDTLS_SSL_COOKIE_C */
2387#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002388 if( opt.cookies == 0 )
2389 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002390 mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002391 }
2392 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002394 {
2395 ; /* Nothing to do */
2396 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002399 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002400 mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002401#endif
2402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002404 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002405 mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002406#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002407 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002409
Paul Bakker41c83d32013-03-20 14:39:14 +01002410 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002411 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002412
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002413#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002414 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002415 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002416#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002417
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002418 if( opt.version_suites != NULL )
2419 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002420 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 MBEDTLS_SSL_MAJOR_VERSION_3,
2422 MBEDTLS_SSL_MINOR_VERSION_0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002423 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 MBEDTLS_SSL_MAJOR_VERSION_3,
2425 MBEDTLS_SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002426 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_SSL_MAJOR_VERSION_3,
2428 MBEDTLS_SSL_MINOR_VERSION_2 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002429 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 MBEDTLS_SSL_MAJOR_VERSION_3,
2431 MBEDTLS_SSL_MINOR_VERSION_3 );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002432 }
2433
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002434 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002435 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002437 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002438
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02002439 if( opt.renego_delay != DFL_RENEGO_DELAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002440 mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002441
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002442 if( opt.renego_period != DFL_RENEGO_PERIOD )
2443 {
Andres AG692ad842017-01-19 16:30:57 +00002444 PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002445 mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002446 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002447#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002450 if( strcmp( opt.ca_path, "none" ) != 0 &&
2451 strcmp( opt.ca_file, "none" ) != 0 )
2452 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002453 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002454 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002455 if( key_cert_init )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002456 {
2457 mbedtls_pk_context *pk = &pkey;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002458#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002459 if( opt.async_private_delay1 >= 0 )
2460 {
Gilles Peskine44817442018-06-13 18:09:28 +02002461 ret = ssl_async_set_key( &ssl_async_keys, &srvcert, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002462 opt.async_private_delay1 );
2463 if( ret < 0 )
2464 {
2465 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2466 ret );
2467 goto exit;
2468 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002469 pk = NULL;
2470 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002471#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002472 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002473 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002474 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002475 goto exit;
2476 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002477 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002478 if( key_cert_init2 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002479 {
2480 mbedtls_pk_context *pk = &pkey2;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002481#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002482 if( opt.async_private_delay2 >= 0 )
2483 {
Gilles Peskine44817442018-06-13 18:09:28 +02002484 ret = ssl_async_set_key( &ssl_async_keys, &srvcert2, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002485 opt.async_private_delay2 );
2486 if( ret < 0 )
2487 {
2488 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2489 ret );
2490 goto exit;
2491 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002492 pk = NULL;
2493 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002494#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002495 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002496 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002497 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002498 goto exit;
2499 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002500 }
2501
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002502#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002503 if( opt.async_operations[0] != '-' )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002504 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002505 mbedtls_ssl_async_sign_t *sign = NULL;
2506 mbedtls_ssl_async_decrypt_t *decrypt = NULL;
Gilles Peskine12ab5d42018-04-24 12:32:04 +02002507 const char *r;
2508 for( r = opt.async_operations; *r; r++ )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002509 {
Gilles Peskine12ab5d42018-04-24 12:32:04 +02002510 switch( *r )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002511 {
2512 case 'd':
2513 decrypt = ssl_async_decrypt;
2514 break;
2515 case 's':
2516 sign = ssl_async_sign;
2517 break;
2518 }
2519 }
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002520 ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
2521 - opt.async_private_error :
2522 opt.async_private_error );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002523 ssl_async_keys.f_rng = mbedtls_ctr_drbg_random;
2524 ssl_async_keys.p_rng = &ctr_drbg;
2525 mbedtls_ssl_conf_async_private_cb( &conf,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002526 sign,
2527 decrypt,
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002528 ssl_async_resume,
2529 ssl_async_cancel,
2530 &ssl_async_keys );
2531 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002532#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002533#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +02002534
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002535#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002536 if( opt.sni != NULL )
Gilles Peskine166ce742018-04-30 10:30:49 +02002537 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002538 mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
Gilles Peskine166ce742018-04-30 10:30:49 +02002539#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
2540 if( opt.async_private_delay2 >= 0 )
2541 {
Gilles Peskinee2479892018-06-13 18:06:51 +02002542 sni_entry *cur;
2543 for( cur = sni_info; cur != NULL; cur = cur->next )
Gilles Peskine166ce742018-04-30 10:30:49 +02002544 {
Gilles Peskinee2479892018-06-13 18:06:51 +02002545 ret = ssl_async_set_key( &ssl_async_keys,
Gilles Peskine44817442018-06-13 18:09:28 +02002546 cur->cert, cur->key, 1,
Gilles Peskinee2479892018-06-13 18:06:51 +02002547 opt.async_private_delay2 );
2548 if( ret < 0 )
2549 {
2550 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2551 ret );
2552 goto exit;
2553 }
2554 cur->key = NULL;
Gilles Peskine166ce742018-04-30 10:30:49 +02002555 }
Gilles Peskine166ce742018-04-30 10:30:49 +02002556 }
2557#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
2558 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002559#endif
2560
Hanno Beckere6706e62017-05-15 16:05:15 +01002561#if defined(MBEDTLS_ECP_C)
2562 if( opt.curves != NULL &&
2563 strcmp( opt.curves, "default" ) != 0 )
2564 {
2565 mbedtls_ssl_conf_curves( &conf, curve_list );
2566 }
2567#endif
2568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002570 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
2571 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002572 ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002573 (const unsigned char *) opt.psk_identity,
2574 strlen( opt.psk_identity ) );
2575 if( ret != 0 )
2576 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002577 mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002578 goto exit;
2579 }
2580 }
2581
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002582 if( opt.psk_list != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002583 mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02002584#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586#if defined(MBEDTLS_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00002587 /*
2588 * Use different group than default DHM group
2589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002591 if( opt.dhm_file != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002592 ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002593#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002594 if( ret != 0 )
2595 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002596 mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002597 goto exit;
2598 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002599#endif
2600
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002601 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002602 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00002603
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002604 if( opt.max_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002605 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002606
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002607 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
2608 {
2609 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
2610 goto exit;
2611 }
2612
2613 if( opt.nbio == 2 )
2614 mbedtls_ssl_set_bio( &ssl, &client_fd, my_send, my_recv, NULL );
2615 else
2616 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02002617 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002618
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002619#if defined(MBEDTLS_SSL_PROTO_DTLS)
2620 if( opt.dtls_mtu != DFL_DTLS_MTU )
2621 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
2622#endif
2623
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002624#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002625 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
2626 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002627#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002630
2631reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002632#if !defined(_WIN32)
2633 if( received_sigterm )
2634 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01002635 mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" );
2636 if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT )
2637 ret = 0;
2638
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002639 goto exit;
2640 }
2641#endif
2642
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002643 if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
2644 {
2645 mbedtls_printf( " ! Client initiated reconnection from same port\n" );
2646 goto handshake;
2647 }
2648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002650 if( ret != 0 )
2651 {
2652 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 mbedtls_strerror( ret, error_buf, 100 );
2654 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002655 }
2656#endif
2657
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002658 mbedtls_net_free( &client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 mbedtls_ssl_session_reset( &ssl );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002661
2662 /*
2663 * 3. Wait until a client connects
2664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 mbedtls_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002666 fflush( stdout );
2667
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002668 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02002669 client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002670 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02002671#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002672 if( received_sigterm )
2673 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01002674 mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" );
2675 if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED )
2676 ret = 0;
2677
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002678 goto exit;
2679 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02002680#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002683 goto exit;
2684 }
2685
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002686 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002687 ret = mbedtls_net_set_nonblock( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002688 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002689 ret = mbedtls_net_set_block( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002690 if( ret != 0 )
2691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002693 goto exit;
2694 }
2695
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002696 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2699 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002700 {
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02002701 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
2702 client_ip, cliip_len ) ) != 0 )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002703 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002704 mbedtls_printf( " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n",
2705 -ret );
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002706 goto exit;
2707 }
2708 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002710
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002711#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2712 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
2713 {
2714 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
2715 (const unsigned char *) opt.ecjpake_pw,
2716 strlen( opt.ecjpake_pw ) ) ) != 0 )
2717 {
2718 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
2719 goto exit;
2720 }
2721 }
2722#endif
2723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002725
2726 /*
2727 * 4. Handshake
2728 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002729handshake:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002731 fflush( stdout );
2732
Hanno Becker16970d22017-10-10 15:56:37 +01002733 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002734 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002735#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002736 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002737 ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002738 {
2739 mbedtls_printf( " cancelling on injected error\n" );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002740 break;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002741 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002742#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskineb44692f2018-04-24 12:18:19 +02002743
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02002744 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01002745 break;
2746
2747 /* For event-driven IO, wait for socket to become available */
2748 if( opt.event == 1 /* level triggered IO */ )
2749 {
2750#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002751 ret = idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002752#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002753 ret = idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002754#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002755 if( ret != 0 )
2756 goto reset;
Hanno Becker16970d22017-10-10 15:56:37 +01002757 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002758 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 mbedtls_printf( " hello verification requested\n" );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002763 ret = 0;
2764 goto reset;
2765 }
2766 else if( ret != 0 )
2767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002769
2770#if defined(MBEDTLS_X509_CRT_PARSE_C)
2771 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2772 {
2773 char vrfy_buf[512];
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02002774 flags = mbedtls_ssl_get_verify_result( &ssl );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002775
2776 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
2777
2778 mbedtls_printf( "%s\n", vrfy_buf );
2779 }
2780#endif
2781
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002782#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002783 if( opt.async_private_error < 0 )
2784 /* Injected error only the first time round, to test reset */
2785 ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
2786#endif
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002787 goto reset;
2788 }
2789 else /* ret == 0 */
2790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
2792 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002793 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
2796 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002797 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002799
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002800#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2801 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
2802 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
2803#endif
2804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002806 if( opt.alpn_string != NULL )
2807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
2809 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002810 alp ? alp : "(none)" );
2811 }
2812#endif
2813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002815 /*
Ron Eldor2a47be52017-06-20 15:23:23 +03002816 * 5. Verify the client certificate
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002819
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002820 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002821 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002822 char vrfy_buf[512];
2823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 mbedtls_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002825
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002826 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002827
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002828 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002829 }
2830 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002832
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02002833 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002834 {
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02002835 char crt_buf[512];
2836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837 mbedtls_printf( " . Peer certificate information ...\n" );
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02002838 mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 mbedtls_ssl_get_peer_cert( &ssl ) );
Manuel Pégourié-Gonnard67557172015-07-02 11:15:48 +02002840 mbedtls_printf( "%s\n", crt_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002841 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002843
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02002844 if( opt.exchanges == 0 )
2845 goto close_notify;
2846
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00002847 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002848data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002849 /*
2850 * 6. Read the HTTP Request
2851 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 mbedtls_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002853 fflush( stdout );
2854
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002855 /*
2856 * TLS and DTLS need different reading styles (stream vs datagram)
2857 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002859 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002860 do
2861 {
2862 int terminated = 0;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002863 len = opt.buffer_size - 1;
2864 memset( buf, 0, opt.buffer_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002866
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02002867 if( mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01002868 {
2869 if( opt.event == 1 /* level triggered IO */ )
2870 {
2871#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002872 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002873#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002874 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002875#endif
2876 }
2877
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002878 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01002879 }
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002880
2881 if( ret <= 0 )
2882 {
2883 switch( ret )
2884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2886 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002887 goto close_notify;
2888
2889 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 case MBEDTLS_ERR_NET_CONN_RESET:
2891 mbedtls_printf( " connection was reset by peer\n" );
2892 ret = MBEDTLS_ERR_NET_CONN_RESET;
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002893 goto reset;
2894
2895 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002897 goto reset;
2898 }
2899 }
2900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002902 {
2903 len = ret;
2904 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002906
2907 /* End of message should be detected according to the syntax of the
2908 * application protocol (eg HTTP), just use a dummy test here. */
2909 if( buf[len - 1] == '\n' )
2910 terminated = 1;
2911 }
2912 else
2913 {
2914 int extra_len, ori_len;
2915 unsigned char *larger_buf;
2916
2917 ori_len = ret;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002918 extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002919
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002920 larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002921 if( larger_buf == NULL )
2922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 mbedtls_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002924 ret = 1;
2925 goto reset;
2926 }
2927
2928 memset( larger_buf, 0, ori_len + extra_len );
2929 memcpy( larger_buf, buf, ori_len );
2930
2931 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002933 if( ret != extra_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002937 ret = 1;
2938 goto reset;
2939 }
2940
2941 larger_buf[ori_len + extra_len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 mbedtls_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002943 ori_len + extra_len, ori_len, extra_len,
2944 (char *) larger_buf );
2945
2946 /* End of message should be detected according to the syntax of the
2947 * application protocol (eg HTTP), just use a dummy test here. */
2948 if( larger_buf[ori_len + extra_len - 1] == '\n' )
2949 terminated = 1;
2950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 mbedtls_free( larger_buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002952 }
2953
2954 if( terminated )
2955 {
2956 ret = 0;
2957 break;
2958 }
2959 }
2960 while( 1 );
2961 }
2962 else /* Not stream, so datagram */
2963 {
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002964 len = opt.buffer_size - 1;
2965 memset( buf, 0, opt.buffer_size );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002966
Gilles Peskineb44692f2018-04-24 12:18:19 +02002967 do
Hanno Becker16970d22017-10-10 15:56:37 +01002968 {
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00002969 /* Without the call to `mbedtls_ssl_check_pending`, it might
2970 * happen that the client sends application data in the same
2971 * datagram as the Finished message concluding the handshake.
2972 * In this case, the application data would be ready to be
2973 * processed while the underlying transport wouldn't signal
2974 * any further incoming data.
2975 *
2976 * See the test 'Event-driven I/O: session-id resume, UDP packing'
2977 * in tests/ssl-opt.sh.
2978 */
2979
2980 /* For event-driven IO, wait for socket to become available */
2981 if( mbedtls_ssl_check_pending( &ssl ) == 0 &&
2982 opt.event == 1 /* level triggered IO */ )
2983 {
2984#if defined(MBEDTLS_TIMING_C)
2985 idle( &client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ );
2986#else
2987 idle( &client_fd, MBEDTLS_ERR_SSL_WANT_READ );
2988#endif
2989 }
2990
Hanno Becker16970d22017-10-10 15:56:37 +01002991 ret = mbedtls_ssl_read( &ssl, buf, len );
2992
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00002993 /* Note that even if `mbedtls_ssl_check_pending` returns true,
2994 * it can happen that the subsequent call to `mbedtls_ssl_read`
2995 * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
2996 * might be discarded (e.g. because they are retransmissions). */
Hanno Becker16970d22017-10-10 15:56:37 +01002997 }
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02002998 while( mbedtls_status_is_ssl_in_progress( ret ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002999
3000 if( ret <= 0 )
3001 {
3002 switch( ret )
3003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3005 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003006 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003007 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003008
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003009 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003011 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003012 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003013 }
3014
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003015 len = ret;
3016 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003018 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003019 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003020
3021 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003022 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02003023 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00003026 if( opt.renegotiate && exchanges_left == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028 mbedtls_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003029 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003032 {
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003033 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003036 goto reset;
3037 }
Hanno Becker16970d22017-10-10 15:56:37 +01003038
3039 /* For event-driven IO, wait for socket to become available */
3040 if( opt.event == 1 /* level triggered IO */ )
3041 {
3042#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003043 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003044#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003045 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003046#endif
3047 }
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003048 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003051 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003053
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003054 /*
3055 * 7. Write the 200 Response
3056 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 mbedtls_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003058 fflush( stdout );
3059
3060 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003062
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003063 /* Add padding to the response to reach opt.response_size in length */
3064 if( opt.response_size != DFL_RESPONSE_SIZE &&
3065 len < opt.response_size )
3066 {
3067 memset( buf + len, 'B', opt.response_size - len );
3068 len += opt.response_size - len;
3069 }
3070
3071 /* Truncate if response size is smaller than the "natural" size */
3072 if( opt.response_size != DFL_RESPONSE_SIZE &&
3073 len > opt.response_size )
3074 {
3075 len = opt.response_size;
3076
3077 /* Still end with \r\n unless that's really not possible */
3078 if( len >= 2 ) buf[len - 2] = '\r';
3079 if( len >= 1 ) buf[len - 1] = '\n';
3080 }
3081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003083 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003084 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003087 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003092 goto reset;
3093 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003094
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003095 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003098 goto reset;
3099 }
Hanno Becker16970d22017-10-10 15:56:37 +01003100
3101 /* For event-driven IO, wait for socket to become available */
3102 if( opt.event == 1 /* level triggered IO */ )
3103 {
3104#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003105 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003106#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003107 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003108#endif
3109 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003110 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003111 }
3112 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003113 else /* Not stream, so datagram */
3114 {
Hanno Becker16970d22017-10-10 15:56:37 +01003115 while( 1 )
3116 {
3117 ret = mbedtls_ssl_write( &ssl, buf, len );
3118
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003119 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003120 break;
3121
3122 /* For event-driven IO, wait for socket to become available */
3123 if( opt.event == 1 /* level triggered IO */ )
3124 {
3125#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003126 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003127#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003128 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003129#endif
3130 }
3131 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003132
3133 if( ret < 0 )
3134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003136 goto reset;
3137 }
3138
3139 frags = 1;
3140 written = ret;
3141 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003142
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003143 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 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 +01003145 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01003146
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003147 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003148 * 7b. Continue doing data exchanges?
3149 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003150 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003151 goto data_exchange;
3152
3153 /*
3154 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003155 */
3156close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01003158
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003159 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003161 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003162 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164 mbedtls_printf( " done\n" );
Rich Evansf90016a2015-01-19 14:26:37 +00003165
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003166 goto reset;
3167
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003168 /*
3169 * Cleanup and exit
3170 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003171exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003173 if( ret != 0 )
3174 {
3175 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 mbedtls_strerror( ret, error_buf, 100 );
3177 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003178 }
3179#endif
3180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181 mbedtls_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003182 fflush( stdout );
3183
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003184 mbedtls_net_free( &client_fd );
3185 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +02003186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3188 mbedtls_dhm_free( &dhm );
Paul Bakkera317a982014-06-18 16:44:11 +02003189#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190#if defined(MBEDTLS_X509_CRT_PARSE_C)
3191 mbedtls_x509_crt_free( &cacert );
3192 mbedtls_x509_crt_free( &srvcert );
3193 mbedtls_pk_free( &pkey );
3194 mbedtls_x509_crt_free( &srvcert2 );
3195 mbedtls_pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02003196#endif
Gilles Peskine44817442018-06-13 18:09:28 +02003197#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3198 for( i = 0; (size_t) i < ssl_async_keys.slots_used; i++ )
3199 {
3200 if( ssl_async_keys.slots[i].pk_owned )
3201 {
3202 mbedtls_pk_free( ssl_async_keys.slots[i].pk );
3203 mbedtls_free( ssl_async_keys.slots[i].pk );
3204 ssl_async_keys.slots[i].pk = NULL;
3205 }
3206 }
3207#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02003208#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003209 sni_free( sni_info );
3210#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003212 psk_free( psk_info );
3213#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3215 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003216#endif
Paul Bakkered27a042013-04-18 22:46:23 +02003217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003219 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 mbedtls_ctr_drbg_free( &ctr_drbg );
3221 mbedtls_entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223#if defined(MBEDTLS_SSL_CACHE_C)
3224 mbedtls_ssl_cache_free( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00003225#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003226#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3227 mbedtls_ssl_ticket_free( &ticket_ctx );
3228#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229#if defined(MBEDTLS_SSL_COOKIE_C)
3230 mbedtls_ssl_cookie_free( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02003231#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003232
Hanno Becker095d9cf2018-10-09 12:39:13 +01003233 mbedtls_free( buf );
3234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
3236#if defined(MBEDTLS_MEMORY_DEBUG)
3237 mbedtls_memory_buffer_alloc_status();
Paul Bakker82024bf2013-07-04 11:52:32 +02003238#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +02003240#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02003241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 mbedtls_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003243
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003244#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003246 fflush( stdout ); getchar();
3247#endif
3248
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02003249 // Shell can not handle large exit numbers -> 1 for errors
3250 if( ret < 0 )
3251 ret = 1;
3252
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +02003253 mbedtls_exit( ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003254}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
3256 MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003257 MBEDTLS_CTR_DRBG_C */