blob: 90aeaf68c675aa76065b7f061bfec9fc07f3bdff [file] [log] [blame]
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001/*
2 * SSL client with options
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb60b95f2012-09-25 09:05:17 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000057#else
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020058#include <stdio.h>
Simon Butcherd3138c32016-04-27 01:26:50 +010059#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define mbedtls_free free
Simon Butcherd3138c32016-04-27 01:26:50 +010061#define mbedtls_time time
62#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020063#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_fprintf fprintf
65#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010066#define mbedtls_exit exit
67#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
68#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evans18b78c72015-02-11 14:06:19 +000069#endif
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020070
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020071#if !defined(MBEDTLS_ENTROPY_C) || \
72 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020073 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020074int main( void )
75{
76 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
77 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020078 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020079 mbedtls_exit( 0 );
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020080}
81#else
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010082
Andres AG788aa4a2016-09-14 14:32:09 +010083#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000084#include "mbedtls/ssl.h"
85#include "mbedtls/entropy.h"
86#include "mbedtls/ctr_drbg.h"
87#include "mbedtls/certs.h"
88#include "mbedtls/x509.h"
89#include "mbedtls/error.h"
90#include "mbedtls/debug.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020091#include "mbedtls/timing.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000092
Rich Evans18b78c72015-02-11 14:06:19 +000093#include <stdio.h>
94#include <stdlib.h>
95#include <string.h>
Andres AG692ad842017-01-19 16:30:57 +000096#include <stdint.h>
Andres AG0b736db2017-02-08 14:05:57 +000097
98#if !defined(_MSC_VER)
Andres AG692ad842017-01-19 16:30:57 +000099#include <inttypes.h>
Andres AG0b736db2017-02-08 14:05:57 +0000100#endif
Rich Evans18b78c72015-02-11 14:06:19 +0000101
102#if !defined(_WIN32)
103#include <signal.h>
104#endif
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000107#include "mbedtls/ssl_cache.h"
Paul Bakker0a597072012-09-25 21:55:46 +0000108#endif
109
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200110#if defined(MBEDTLS_SSL_TICKET_C)
111#include "mbedtls/ssl_ticket.h"
112#endif
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000115#include "mbedtls/ssl_cookie.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200116#endif
117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000119#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker82024bf2013-07-04 11:52:32 +0200120#endif
121
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200122#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO)
123#define SNI_OPTION
124#endif
125
126#if defined(_WIN32)
127#include <windows.h>
128#endif
129
Simon Butchercce68be2018-07-23 14:26:09 +0100130/* Size of memory to be allocated for the heap, when using the library's memory
131 * management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */
132#define MEMORY_HEAP_SIZE 120000
133
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100134#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200135#define DFL_SERVER_PORT "4433"
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200136#define DFL_RESPONSE_SIZE -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000137#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100138#define DFL_NBIO 0
Hanno Becker16970d22017-10-10 15:56:37 +0100139#define DFL_EVENT 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200140#define DFL_READ_TIMEOUT 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000141#define DFL_CA_FILE ""
142#define DFL_CA_PATH ""
143#define DFL_CRT_FILE ""
144#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200145#define DFL_CRT_FILE2 ""
146#define DFL_KEY_FILE2 ""
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100147#define DFL_ASYNC_OPERATIONS "-"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100148#define DFL_ASYNC_PRIVATE_DELAY1 ( -1 )
149#define DFL_ASYNC_PRIVATE_DELAY2 ( -1 )
Gilles Peskine3665f1d2018-01-05 21:22:12 +0100150#define DFL_ASYNC_PRIVATE_ERROR ( 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +0200151#define DFL_PSK ""
152#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200153#define DFL_ECJPAKE_PW NULL
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200154#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000155#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200156#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100158#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100159#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200160#define DFL_RENEGO_DELAY -2
Andres AG692ad842017-01-19 16:30:57 +0000161#define DFL_RENEGO_PERIOD ( (uint64_t)-1 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200162#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200163#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200164#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000165#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +0200166#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100167#define DFL_AUTH_MODE -1
Janos Follath4817e272017-04-10 13:44:33 +0100168#define DFL_CERT_REQ_CA_LIST MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100170#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200172#define DFL_TICKET_TIMEOUT 86400
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100173#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100174#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100175#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200176#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100177#define DFL_CURVES NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200178#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200180#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200181#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200182#define DFL_HS_TO_MIN 0
183#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200184#define DFL_DTLS_MTU -1
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200185#define DFL_BADMAC_LIMIT -1
Hanno Beckere7675d02018-08-14 13:28:56 +0100186#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200187#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100188#define DFL_ETM -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000189
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200190#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
191 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
192 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
193 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
194 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
195 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
196 "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 +0200197
Paul Bakker8e714d72013-07-18 11:05:13 +0200198/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
199 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000200#define HTTP_RESPONSE \
201 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000202 "<h2>mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200203 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000204
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200205/*
206 * Size of the basic I/O buffer. Able to hold our default response.
207 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 * 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 +0200209 * if you change this value to something outside the range <= 100 or > 500
210 */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200211#define DFL_IO_BUF_LEN 200
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#if defined(MBEDTLS_X509_CRT_PARSE_C)
214#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000215#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100216 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
217 " default: \"\" (pre-loaded)\n" \
Hanno Becker1ce1a512019-05-01 11:16:28 +0100218 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100219 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
220 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
Hanno Becker1ce1a512019-05-01 11:16:28 +0100221 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100222 " 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 +0200223 " default: see note after key_file2\n" \
224 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200225 " 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 +0200226 " default: see note after key_file2\n" \
227 " key_file2=%%s default: see note below\n" \
228 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200229 " preloaded certificate(s) and key(s) are used if available\n" \
230 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
231 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000232#else
233#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200234 "\n" \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 " No file operations available (MBEDTLS_FS_IO not defined)\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200236 "\n"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200238#else
239#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200241
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200242#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100243#define USAGE_SSL_ASYNC \
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100244 " async_operations=%%c... d=decrypt, s=sign (default: -=off)\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100245 " async_private_delay1=%%d Asynchronous delay for key_file or preloaded key\n" \
Gilles Peskine166ce742018-04-30 10:30:49 +0200246 " async_private_delay2=%%d Asynchronous delay for key_file2 and sni\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100247 " default: -1 (not asynchronous)\n" \
Gilles Peskine60ee4ca2018-01-08 11:28:05 +0100248 " async_private_error=%%d Async callback error injection (default=0=none,\n" \
Gilles Peskinec9125722018-04-26 07:15:40 +0200249 " 1=start, 2=cancel, 3=resume, negative=first time only)"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100250#else
251#define USAGE_SSL_ASYNC ""
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200252#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd0d01c52018-10-25 16:49:48 +0100255#define USAGE_PSK \
256 " psk=%%s default: \"\" (in hex, without 0x)\n" \
257 " psk_list=%%s default: \"\"\n" \
Hanno Becker5ddc0632018-10-26 10:04:13 +0100258 " A list of (PSK identity, PSK value) pairs.\n" \
259 " The PSK values are in hex, without 0x.\n" \
Hanno Beckerd0d01c52018-10-25 16:49:48 +0100260 " id1,psk1[,id2,psk2[,...]]\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200261 " psk_identity=%%s default: \"Client_identity\"\n"
262#else
263#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200267#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100268 " tickets=%%d default: 1 (enabled)\n" \
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200269 " ticket_timeout=%%d default: 86400 (one day)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200270#else
271#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100275#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100276 " cache_max=%%d default: cache default (50)\n" \
277 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100278#else
279#define USAGE_CACHE ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#endif /* MBEDTLS_SSL_CACHE_C */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100281
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200282#if defined(SNI_OPTION)
Ron Eldor24eec792019-04-04 15:02:01 +0300283#if defined(MBEDTLS_X509_CRL_PARSE_C)
284#define SNI_CRL ",crl"
285#else
286#define SNI_CRL ""
287#endif
288
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100289#define USAGE_SNI \
Ron Eldor24eec792019-04-04 15:02:01 +0300290 " sni=%%s name1,cert1,key1,ca1"SNI_CRL",auth1[,...]\n" \
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200291 " default: disabled\n"
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100292#else
293#define USAGE_SNI ""
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200294#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200297#define USAGE_MAX_FRAG_LEN \
298 " max_frag_len=%%d default: 16384 (tls default)\n" \
299 " options: 512, 1024, 2048, 4096\n"
300#else
301#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100305#define USAGE_TRUNC_HMAC \
306 " trunc_hmac=%%d default: library default\n"
307#else
308#define USAGE_TRUNC_HMAC ""
309#endif
310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200312#define USAGE_ALPN \
313 " alpn=%%s default: \"\" (disabled)\n" \
314 " example: spdy/1,http/1.1\n"
315#else
316#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200320#define USAGE_COOKIES \
321 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200322 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200323#else
324#define USAGE_COOKIES ""
325#endif
326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200328#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200329 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200330#else
331#define USAGE_ANTI_REPLAY ""
332#endif
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200335#define USAGE_BADMAC_LIMIT \
336 " badmac_limit=%%d default: (library default: disabled)\n"
337#else
338#define USAGE_BADMAC_LIMIT ""
339#endif
340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200342#define USAGE_DTLS \
343 " dtls=%%d default: 0 (TLS)\n" \
344 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200345 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Beckere7675d02018-08-14 13:28:56 +0100346 " mtu=%%d default: (library default: unlimited)\n" \
347 " dgram_packing=%%d default: 1 (allowed)\n" \
348 " allow or forbid packing of multiple\n" \
349 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200350#else
351#define USAGE_DTLS ""
352#endif
353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200355#define USAGE_EMS \
356 " extended_ms=0/1 default: (library default: on)\n"
357#else
358#define USAGE_EMS ""
359#endif
360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100362#define USAGE_ETM \
363 " etm=0/1 default: (library default: on)\n"
364#else
365#define USAGE_ETM ""
366#endif
367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100369#define USAGE_RENEGO \
370 " renegotiation=%%d default: 0 (disabled)\n" \
371 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100372 " renego_delay=%%d default: -2 (library default)\n" \
Andres AG692ad842017-01-19 16:30:57 +0000373 " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100374#else
375#define USAGE_RENEGO ""
376#endif
377
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200378#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
379#define USAGE_ECJPAKE \
380 " ecjpake_pw=%%s default: none (disabled)\n"
381#else
382#define USAGE_ECJPAKE ""
383#endif
384
Hanno Beckere6706e62017-05-15 16:05:15 +0100385#if defined(MBEDTLS_ECP_C)
386#define USAGE_CURVES \
387 " curves=a,b,c,d default: \"default\" (library default)\n" \
388 " example: \"secp521r1,brainpoolP512r1\"\n" \
389 " - use \"none\" for empty list\n" \
390 " - see mbedtls_ecp_curve_list()\n" \
391 " for acceptable curve names\n"
392#else
393#define USAGE_CURVES ""
394#endif
395
Gilles Peskined135bbd2020-04-14 19:41:01 +0200396/* USAGE is arbitrarily split to stay under the portable string literal
397 * length limit: 4095 bytes in C99. */
398#define USAGE1 \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000399 "\n usage: ssl_server2 param=<>...\n" \
400 "\n acceptable parameters:\n" \
Ron Eldor71f68c42017-09-26 11:29:11 +0300401 " server_addr=%%s default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000402 " server_port=%%d default: 4433\n" \
403 " debug_level=%%d default: 0 (disabled)\n" \
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200404 " buffer_size=%%d default: 200 \n" \
405 " (minimum: 1, max: 16385)\n" \
406 " response_size=%%d default: about 152 (basic response)\n" \
407 " (minimum: 0, max: 16384)\n" \
408 " increases buffer_size if bigger\n"\
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100409 " nbio=%%d default: 0 (blocking I/O)\n" \
410 " options: 1 (non-blocking), 2 (added delays)\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100411 " event=%%d default: 0 (loop)\n" \
412 " options: 1 (level-triggered, implies nbio=1),\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200413 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100414 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200415 USAGE_DTLS \
416 USAGE_COOKIES \
417 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200418 USAGE_BADMAC_LIMIT \
Gilles Peskined135bbd2020-04-14 19:41:01 +0200419 "\n"
420#define USAGE2 \
Manuel Pégourié-Gonnardee6139c2015-05-07 10:18:26 +0100421 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100422 " options: none, optional, required\n" \
Janos Follath4817e272017-04-10 13:44:33 +0100423 " cert_req_ca_list=%%d default: 1 (send ca list)\n" \
424 " options: 1 (send ca list), 0 (don't send)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000425 USAGE_IO \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100426 USAGE_SSL_ASYNC \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100427 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100428 "\n" \
429 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200430 USAGE_ECJPAKE \
Gilles Peskined135bbd2020-04-14 19:41:01 +0200431 "\n"
432#define USAGE3 \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100433 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100434 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200435 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200436 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100437 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100438 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100439 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100440 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200441 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200442 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100443 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100444 USAGE_CURVES \
Gilles Peskined135bbd2020-04-14 19:41:01 +0200445 "\n"
446#define USAGE4 \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100447 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200448 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200449 " min_version=%%s default: (library default: tls1)\n" \
450 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200451 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100452 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200453 "\n" \
454 " version_suites=a,b,c,d per-version ciphersuites\n" \
455 " in order from ssl3 to tls1_2\n" \
456 " default: all enabled\n" \
457 " force_ciphersuite=<name> default: all enabled\n" \
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100458 " query_config=<name> return 0 if the specified\n" \
459 " configuration macro is defined and 1\n" \
460 " otherwise. The expansion of the macro\n" \
461 " is printed if it is defined\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000462 " acceptable ciphersuite names:\n"
463
Hanno Becker8651a432017-06-09 16:13:22 +0100464#define ALPN_LIST_SIZE 10
465#define CURVE_LIST_SIZE 20
466
Andres AG692ad842017-01-19 16:30:57 +0000467#define PUT_UINT64_BE(out_be,in_le,i) \
468{ \
469 (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \
470 (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \
471 (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \
472 (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \
473 (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \
474 (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \
475 (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \
476 (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \
477}
478
Simon Butcher63cb97e2018-12-06 17:43:31 +0000479
Rich Evans85b05ec2015-02-12 11:37:29 +0000480/*
481 * global options
482 */
483struct options
484{
485 const char *server_addr; /* address on which the ssl service runs */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200486 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000487 int debug_level; /* level of debugging */
488 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100489 int event; /* loop or event-driven IO? level or edge triggered? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200491 int response_size; /* pad response with header to requested size */
492 uint16_t buffer_size; /* IO buffer size */
Rich Evans85b05ec2015-02-12 11:37:29 +0000493 const char *ca_file; /* the file with the CA certificate(s) */
494 const char *ca_path; /* the path with the CA certificate(s) reside */
495 const char *crt_file; /* the file with the server certificate */
496 const char *key_file; /* the file with the server key */
497 const char *crt_file2; /* the file with the 2nd server certificate */
498 const char *key_file2; /* the file with the 2nd server key */
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100499 const char *async_operations; /* supported SSL asynchronous operations */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100500 int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
501 int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
502 int async_private_error; /* inject error in async private callback */
Rich Evans85b05ec2015-02-12 11:37:29 +0000503 const char *psk; /* the pre-shared key */
504 const char *psk_identity; /* the pre-shared key identity */
505 char *psk_list; /* list of PSK id/key pairs for callback */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200506 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000507 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
508 const char *version_suites; /* per-version ciphersuites */
509 int renegotiation; /* enable / disable renegotiation */
510 int allow_legacy; /* allow legacy renegotiation */
511 int renegotiate; /* attempt renegotiation? */
512 int renego_delay; /* delay before enforcing renegotiation */
Andres AG692ad842017-01-19 16:30:57 +0000513 uint64_t renego_period; /* period for automatic renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000514 int exchanges; /* number of data exchanges */
515 int min_version; /* minimum protocol version accepted */
516 int max_version; /* maximum protocol version accepted */
517 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200518 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000519 int auth_mode; /* verify mode for connection */
Janos Follath4817e272017-04-10 13:44:33 +0100520 int cert_req_ca_list; /* should we send the CA list? */
Rich Evans85b05ec2015-02-12 11:37:29 +0000521 unsigned char mfl_code; /* code for maximum fragment length */
522 int trunc_hmac; /* accept truncated hmac? */
523 int tickets; /* enable / disable session tickets */
524 int ticket_timeout; /* session ticket lifetime */
525 int cache_max; /* max number of session cache entries */
526 int cache_timeout; /* expiration delay of session cache entries */
527 char *sni; /* string describing sni information */
Hanno Beckere6706e62017-05-15 16:05:15 +0100528 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000529 const char *alpn_string; /* ALPN supported protocols */
530 const char *dhm_file; /* the file with the DH parameters */
531 int extended_ms; /* allow negotiation of extended MS? */
532 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000533 int transport; /* TLS or DTLS? */
534 int cookies; /* Use cookies for DTLS? -1 to break them */
535 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
536 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
537 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200538 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Hanno Beckere7675d02018-08-14 13:28:56 +0100539 int dgram_packing; /* allow/forbid datagram packing */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000540 int badmac_limit; /* Limit of records with bad MAC */
Rich Evans85b05ec2015-02-12 11:37:29 +0000541} opt;
542
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100543int query_config( const char *config );
544
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200545static void my_debug( void *ctx, int level,
546 const char *file, int line,
547 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000548{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200549 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000550
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200551 /* Extract basename from file */
552 for( p = basename = file; *p != '\0'; p++ )
553 if( *p == '/' || *p == '\\' )
554 basename = p + 1;
555
556 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000557 fflush( (FILE *) ctx );
558}
559
560/*
561 * Test recv/send functions that make sure each try returns
562 * WANT_READ/WANT_WRITE at least once before sucesseding
563 */
564static int my_recv( void *ctx, unsigned char *buf, size_t len )
565{
566 static int first_try = 1;
567 int ret;
568
569 if( first_try )
570 {
571 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100572 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000573 }
574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100576 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000577 first_try = 1; /* Next call will be a new operation */
578 return( ret );
579}
580
581static int my_send( void *ctx, const unsigned char *buf, size_t len )
582{
583 static int first_try = 1;
584 int ret;
585
586 if( first_try )
587 {
588 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100589 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000590 }
591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100593 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000594 first_try = 1; /* Next call will be a new operation */
595 return( ret );
596}
597
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200598/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200599 * Return authmode from string, or -1 on error
600 */
601static int get_auth_mode( const char *s )
602{
603 if( strcmp( s, "none" ) == 0 )
604 return( MBEDTLS_SSL_VERIFY_NONE );
605 if( strcmp( s, "optional" ) == 0 )
606 return( MBEDTLS_SSL_VERIFY_OPTIONAL );
607 if( strcmp( s, "required" ) == 0 )
608 return( MBEDTLS_SSL_VERIFY_REQUIRED );
609
610 return( -1 );
611}
612
613/*
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200614 * Used by sni_parse and psk_parse to handle coma-separated lists
615 */
616#define GET_ITEM( dst ) \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100617 do \
618 { \
619 (dst) = p; \
620 while( *p != ',' ) \
621 if( ++p > end ) \
622 goto error; \
623 *p++ = '\0'; \
624 } while( 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200625
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200626#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100627typedef struct _sni_entry sni_entry;
628
629struct _sni_entry {
630 const char *name;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_x509_crt *cert;
632 mbedtls_pk_context *key;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200633 mbedtls_x509_crt* ca;
634 mbedtls_x509_crl* crl;
635 int authmode;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100636 sni_entry *next;
637};
638
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100639void sni_free( sni_entry *head )
640{
641 sni_entry *cur = head, *next;
642
643 while( cur != NULL )
644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_x509_crt_free( cur->cert );
646 mbedtls_free( cur->cert );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_pk_free( cur->key );
649 mbedtls_free( cur->key );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100650
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200651 mbedtls_x509_crt_free( cur->ca );
652 mbedtls_free( cur->ca );
Ron Eldor24eec792019-04-04 15:02:01 +0300653#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200654 mbedtls_x509_crl_free( cur->crl );
655 mbedtls_free( cur->crl );
Ron Eldor24eec792019-04-04 15:02:01 +0300656#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100657 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_free( cur );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100659 cur = next;
660 }
661}
662
663/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200664 * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
665 * into a usable sni_entry list. For ca1, crl1, auth1, the special value
666 * '-' means unset. If ca1 is unset, then crl1 is ignored too.
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000667 *
668 * Modifies the input string! This is not production quality!
669 */
670sni_entry *sni_parse( char *sni_string )
671{
672 sni_entry *cur = NULL, *new = NULL;
673 char *p = sni_string;
674 char *end = p;
Ron Eldor24eec792019-04-04 15:02:01 +0300675 char *crt_file, *key_file, *ca_file, *auth_str;
676#if defined(MBEDTLS_X509_CRL_PARSE_C)
677 char *crl_file;
678#endif
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000679
680 while( *end != '\0' )
681 ++end;
682 *end = ',';
683
684 while( p <= end )
685 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200686 if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000687 {
688 sni_free( cur );
689 return( NULL );
690 }
691
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200692 GET_ITEM( new->name );
693 GET_ITEM( crt_file );
694 GET_ITEM( key_file );
695 GET_ITEM( ca_file );
Ron Eldor24eec792019-04-04 15:02:01 +0300696#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200697 GET_ITEM( crl_file );
Ron Eldor24eec792019-04-04 15:02:01 +0300698#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200699 GET_ITEM( auth_str );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000700
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200701 if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
702 ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200703 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_x509_crt_init( new->cert );
706 mbedtls_pk_init( new->key );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
709 mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000710 goto error;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200711
712 if( strcmp( ca_file, "-" ) != 0 )
713 {
714 if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
715 goto error;
716
717 mbedtls_x509_crt_init( new->ca );
718
719 if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
720 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000721 }
722
Ron Eldor24eec792019-04-04 15:02:01 +0300723#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200724 if( strcmp( crl_file, "-" ) != 0 )
725 {
726 if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
727 goto error;
728
729 mbedtls_x509_crl_init( new->crl );
730
731 if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
732 goto error;
733 }
Ron Eldor24eec792019-04-04 15:02:01 +0300734#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200735
736 if( strcmp( auth_str, "-" ) != 0 )
737 {
738 if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
739 goto error;
740 }
741 else
742 new->authmode = DFL_AUTH_MODE;
743
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000744 new->next = cur;
745 cur = new;
746 }
747
748 return( cur );
749
750error:
751 sni_free( new );
752 sni_free( cur );
753 return( NULL );
754}
755
756/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100757 * SNI callback.
758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100760 const unsigned char *name, size_t name_len )
761{
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200762 const sni_entry *cur = (const sni_entry *) p_info;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100763
764 while( cur != NULL )
765 {
766 if( name_len == strlen( cur->name ) &&
767 memcmp( name, cur->name, name_len ) == 0 )
768 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200769 if( cur->ca != NULL )
770 mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
771
772 if( cur->authmode != DFL_AUTH_MODE )
773 mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
774
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200775 return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100776 }
777
778 cur = cur->next;
779 }
780
781 return( -1 );
782}
783
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200784#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200787
Hanno Beckerd6028a12018-10-15 12:01:35 +0100788#define HEX2NUM( c ) \
789 do \
790 { \
791 if( (c) >= '0' && (c) <= '9' ) \
792 (c) -= '0'; \
793 else if( (c) >= 'a' && (c) <= 'f' ) \
794 (c) -= 'a' - 10; \
795 else if( (c) >= 'A' && (c) <= 'F' ) \
796 (c) -= 'A' - 10; \
797 else \
798 return( -1 ); \
799 } while( 0 )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200800
801/*
802 * Convert a hex string to bytes.
803 * Return 0 on success, -1 on error.
804 */
805int unhexify( unsigned char *output, const char *input, size_t *olen )
806{
807 unsigned char c;
808 size_t j;
809
810 *olen = strlen( input );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 if( *olen % 2 != 0 || *olen / 2 > MBEDTLS_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200812 return( -1 );
813 *olen /= 2;
814
815 for( j = 0; j < *olen * 2; j += 2 )
816 {
817 c = input[j];
818 HEX2NUM( c );
819 output[ j / 2 ] = c << 4;
820
821 c = input[j + 1];
822 HEX2NUM( c );
823 output[ j / 2 ] |= c;
824 }
825
826 return( 0 );
827}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200828
829typedef struct _psk_entry psk_entry;
830
831struct _psk_entry
832{
833 const char *name;
834 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 unsigned char key[MBEDTLS_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200836 psk_entry *next;
837};
838
839/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000840 * Free a list of psk_entry's
841 */
842void psk_free( psk_entry *head )
843{
844 psk_entry *next;
845
846 while( head != NULL )
847 {
848 next = head->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_free( head );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000850 head = next;
851 }
852}
853
854/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200855 * Parse a string of pairs name1,key1[,name2,key2[,...]]
856 * into a usable psk_entry list.
857 *
858 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200859 */
860psk_entry *psk_parse( char *psk_string )
861{
862 psk_entry *cur = NULL, *new = NULL;
863 char *p = psk_string;
864 char *end = p;
865 char *key_hex;
866
867 while( *end != '\0' )
868 ++end;
869 *end = ',';
870
871 while( p <= end )
872 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200873 if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +0000874 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200875
876 memset( new, 0, sizeof( psk_entry ) );
877
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200878 GET_ITEM( new->name );
879 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200880
881 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000882 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200883
884 new->next = cur;
885 cur = new;
886 }
887
888 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200889
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000890error:
891 psk_free( new );
892 psk_free( cur );
893 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200894}
895
896/*
897 * PSK callback
898 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200900 const unsigned char *name, size_t name_len )
901{
902 psk_entry *cur = (psk_entry *) p_info;
903
904 while( cur != NULL )
905 {
906 if( name_len == strlen( cur->name ) &&
907 memcmp( name, cur->name, name_len ) == 0 )
908 {
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +0100909 return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200910 }
911
912 cur = cur->next;
913 }
914
915 return( -1 );
916}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200918
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200919static mbedtls_net_context listen_fd, client_fd;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200920
921/* Interruption handler to ensure clean exit (for valgrind testing) */
922#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200923static int received_sigterm = 0;
924void term_handler( int sig )
925{
926 ((void) sig);
927 received_sigterm = 1;
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200928 mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
929 mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200930}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200931#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200932
Gilles Peskine682df092017-05-11 19:01:11 +0200933#if defined(MBEDTLS_X509_CRT_PARSE_C)
934static int ssl_sig_hashes_for_test[] = {
935#if defined(MBEDTLS_SHA512_C)
936 MBEDTLS_MD_SHA512,
937 MBEDTLS_MD_SHA384,
938#endif
939#if defined(MBEDTLS_SHA256_C)
940 MBEDTLS_MD_SHA256,
941 MBEDTLS_MD_SHA224,
942#endif
943#if defined(MBEDTLS_SHA1_C)
944 /* Allow SHA-1 as we use it extensively in tests. */
945 MBEDTLS_MD_SHA1,
946#endif
947 MBEDTLS_MD_NONE
948};
949#endif /* MBEDTLS_X509_CRT_PARSE_C */
950
Gilles Peskinea36ac4f2018-04-26 08:05:02 +0200951/** Return true if \p ret is a status code indicating that there is an
952 * operation in progress on an SSL connection, and false if it indicates
953 * success or a fatal error.
954 *
955 * The possible operations in progress are:
956 *
957 * - A read, when the SSL input buffer does not contain a full message.
958 * - A write, when the SSL output buffer contains some data that has not
959 * been sent over the network yet.
960 * - An asynchronous callback that has not completed yet. */
961static int mbedtls_status_is_ssl_in_progress( int ret )
962{
963 return( ret == MBEDTLS_ERR_SSL_WANT_READ ||
964 ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
965 ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
966}
967
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200968#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100969typedef struct
970{
Gilles Peskine44817442018-06-13 18:09:28 +0200971 mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
972 mbedtls_pk_context *pk; /*!< Private key */
973 unsigned delay; /*!< Number of resume steps to go through */
974 unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100975} ssl_async_key_slot_t;
976
977typedef enum {
Gilles Peskinead28bf02018-04-26 00:19:16 +0200978 SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
979 SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
980 SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start */
981 SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
Gilles Peskinec9125722018-04-26 07:15:40 +0200982#define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100983} ssl_async_inject_error_t;
984
985typedef struct
986{
Gilles Peskinee2479892018-06-13 18:06:51 +0200987 ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100988 size_t slots_used;
989 ssl_async_inject_error_t inject_error;
990 int (*f_rng)(void *, unsigned char *, size_t);
991 void *p_rng;
992} ssl_async_key_context_t;
993
Gilles Peskined6fbfde2018-04-30 10:23:56 +0200994int ssl_async_set_key( ssl_async_key_context_t *ctx,
Gilles Peskine44817442018-06-13 18:09:28 +0200995 mbedtls_x509_crt *cert,
996 mbedtls_pk_context *pk,
997 int pk_take_ownership,
998 unsigned delay )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100999{
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001000 if( ctx->slots_used >= sizeof( ctx->slots ) / sizeof( *ctx->slots ) )
1001 return( -1 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001002 ctx->slots[ctx->slots_used].cert = cert;
1003 ctx->slots[ctx->slots_used].pk = pk;
1004 ctx->slots[ctx->slots_used].delay = delay;
Gilles Peskine44817442018-06-13 18:09:28 +02001005 ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001006 ++ctx->slots_used;
Gilles Peskined6fbfde2018-04-30 10:23:56 +02001007 return( 0 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001008}
1009
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001010#define SSL_ASYNC_INPUT_MAX_SIZE 512
Gilles Peskined3268832018-04-26 06:23:59 +02001011
1012typedef enum
1013{
1014 ASYNC_OP_SIGN,
1015 ASYNC_OP_DECRYPT,
1016} ssl_async_operation_type_t;
1017/* Note that the enum above and the array below need to be kept in sync!
1018 * `ssl_async_operation_names[op]` is the name of op for each value `op`
1019 * of type `ssl_async_operation_type_t`. */
1020static const char *const ssl_async_operation_names[] =
1021{
1022 "sign",
1023 "decrypt",
1024};
1025
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001026typedef struct
1027{
Gilles Peskine6331d782018-04-26 13:27:43 +02001028 unsigned slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001029 ssl_async_operation_type_t operation_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001030 mbedtls_md_type_t md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001031 unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
1032 size_t input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001033 unsigned remaining_delay;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001034} ssl_async_operation_context_t;
1035
Gilles Peskine8f97af72018-04-26 11:46:10 +02001036static int ssl_async_start( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001037 mbedtls_x509_crt *cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001038 ssl_async_operation_type_t op_type,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001039 mbedtls_md_type_t md_alg,
1040 const unsigned char *input,
1041 size_t input_len )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001042{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001043 ssl_async_key_context_t *config_data =
1044 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskine6331d782018-04-26 13:27:43 +02001045 unsigned slot;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001046 ssl_async_operation_context_t *ctx = NULL;
Gilles Peskined3268832018-04-26 06:23:59 +02001047 const char *op_name = ssl_async_operation_names[op_type];
Gilles Peskinef1127252018-04-24 13:05:39 +02001048
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001049 {
1050 char dn[100];
Gilles Peskined5d983e2018-06-15 14:05:10 +02001051 if( mbedtls_x509_dn_gets( dn, sizeof( dn ), &cert->subject ) > 0 )
1052 mbedtls_printf( "Async %s callback: looking for DN=%s\n",
1053 op_name, dn );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001054 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001055
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001056 /* Look for a private key that matches the public key in cert.
1057 * Since this test code has the private key inside Mbed TLS,
1058 * we call mbedtls_pk_check_pair to match a private key with the
1059 * public key. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001060 for( slot = 0; slot < config_data->slots_used; slot++ )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001061 {
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001062 if( mbedtls_pk_check_pair( &cert->pk,
1063 config_data->slots[slot].pk ) == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001064 break;
1065 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001066 if( slot == config_data->slots_used )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001067 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001068 mbedtls_printf( "Async %s callback: no key matches this certificate.\n",
1069 op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001070 return( MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH );
1071 }
Gilles Peskine6331d782018-04-26 13:27:43 +02001072 mbedtls_printf( "Async %s callback: using key slot %u, delay=%u.\n",
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001073 op_name, slot, config_data->slots[slot].delay );
Gilles Peskinef1127252018-04-24 13:05:39 +02001074
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001075 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001076 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001077 mbedtls_printf( "Async %s callback: injected error\n", op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001078 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1079 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001080
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001081 if( input_len > SSL_ASYNC_INPUT_MAX_SIZE )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001082 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Gilles Peskinef1127252018-04-24 13:05:39 +02001083
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001084 ctx = mbedtls_calloc( 1, sizeof( *ctx ) );
1085 if( ctx == NULL )
1086 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1087 ctx->slot = slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001088 ctx->operation_type = op_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001089 ctx->md_alg = md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001090 memcpy( ctx->input, input, input_len );
1091 ctx->input_len = input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001092 ctx->remaining_delay = config_data->slots[slot].delay;
Gilles Peskinea668c602018-04-30 11:54:39 +02001093 mbedtls_ssl_set_async_operation_data( ssl, ctx );
Gilles Peskinef1127252018-04-24 13:05:39 +02001094
Gilles Peskineceb541b2018-04-26 06:30:45 +02001095 if( ctx->remaining_delay == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001096 return( 0 );
1097 else
1098 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1099}
1100
Gilles Peskine8f97af72018-04-26 11:46:10 +02001101static int ssl_async_sign( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001102 mbedtls_x509_crt *cert,
1103 mbedtls_md_type_t md_alg,
1104 const unsigned char *hash,
1105 size_t hash_len )
1106{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001107 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001108 ASYNC_OP_SIGN, md_alg,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001109 hash, hash_len ) );
1110}
1111
Gilles Peskine8f97af72018-04-26 11:46:10 +02001112static int ssl_async_decrypt( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001113 mbedtls_x509_crt *cert,
1114 const unsigned char *input,
1115 size_t input_len )
1116{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001117 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001118 ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001119 input, input_len ) );
1120}
1121
Gilles Peskine8f97af72018-04-26 11:46:10 +02001122static int ssl_async_resume( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001123 unsigned char *output,
1124 size_t *output_len,
1125 size_t output_size )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001126{
Gilles Peskinea668c602018-04-30 11:54:39 +02001127 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine8f97af72018-04-26 11:46:10 +02001128 ssl_async_key_context_t *config_data =
1129 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001130 ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001131 int ret;
Gilles Peskinef5a99962018-04-30 16:37:23 +02001132 const char *op_name;
Gilles Peskinef1127252018-04-24 13:05:39 +02001133
Gilles Peskineceb541b2018-04-26 06:30:45 +02001134 if( ctx->remaining_delay > 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001135 {
Gilles Peskineceb541b2018-04-26 06:30:45 +02001136 --ctx->remaining_delay;
Gilles Peskine6331d782018-04-26 13:27:43 +02001137 mbedtls_printf( "Async resume (slot %u): call %u more times.\n",
Gilles Peskineceb541b2018-04-26 06:30:45 +02001138 ctx->slot, ctx->remaining_delay );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001139 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1140 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001141
Gilles Peskined3268832018-04-26 06:23:59 +02001142 switch( ctx->operation_type )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001143 {
Gilles Peskined3268832018-04-26 06:23:59 +02001144 case ASYNC_OP_DECRYPT:
Gilles Peskined3268832018-04-26 06:23:59 +02001145 ret = mbedtls_pk_decrypt( key_slot->pk,
1146 ctx->input, ctx->input_len,
1147 output, output_len, output_size,
1148 config_data->f_rng, config_data->p_rng );
1149 break;
1150 case ASYNC_OP_SIGN:
Gilles Peskined3268832018-04-26 06:23:59 +02001151 ret = mbedtls_pk_sign( key_slot->pk,
1152 ctx->md_alg,
1153 ctx->input, ctx->input_len,
1154 output, output_len,
1155 config_data->f_rng, config_data->p_rng );
1156 break;
1157 default:
Gilles Peskine6331d782018-04-26 13:27:43 +02001158 mbedtls_printf( "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
Gilles Peskined3268832018-04-26 06:23:59 +02001159 ctx->slot, (long) ctx->operation_type );
Gilles Peskine44817442018-06-13 18:09:28 +02001160 mbedtls_free( ctx );
Gilles Peskined3268832018-04-26 06:23:59 +02001161 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1162 break;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001163 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001164
Gilles Peskinef5a99962018-04-30 16:37:23 +02001165 op_name = ssl_async_operation_names[ctx->operation_type];
1166
Gilles Peskinec9125722018-04-26 07:15:40 +02001167 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001168 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001169 mbedtls_printf( "Async resume callback: %s done but injected error\n",
1170 op_name );
Gilles Peskine2636fad2018-06-12 14:17:39 +02001171 mbedtls_free( ctx );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001172 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1173 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001174
Gilles Peskine6331d782018-04-26 13:27:43 +02001175 mbedtls_printf( "Async resume (slot %u): %s done, status=%d.\n",
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001176 ctx->slot, op_name, ret );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001177 mbedtls_free( ctx );
1178 return( ret );
1179}
1180
Gilles Peskine8f97af72018-04-26 11:46:10 +02001181static void ssl_async_cancel( mbedtls_ssl_context *ssl )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001182{
Gilles Peskinea668c602018-04-30 11:54:39 +02001183 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001184 mbedtls_printf( "Async cancel callback.\n" );
1185 mbedtls_free( ctx );
1186}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001187#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001188
Hanno Becker16970d22017-10-10 15:56:37 +01001189/*
1190 * Wait for an event from the underlying transport or the timer
1191 * (Used in event-driven IO mode).
1192 */
1193#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001194int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001195 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001196#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001197int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001198 mbedtls_timing_delay_context *timer,
1199 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001200#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001201{
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001202 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +01001203 int poll_type = 0;
1204
1205 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
1206 poll_type = MBEDTLS_NET_POLL_WRITE;
1207 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
1208 poll_type = MBEDTLS_NET_POLL_READ;
1209#if !defined(MBEDTLS_TIMING_C)
1210 else
Hanno Beckeref527962018-03-15 15:49:24 +00001211 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001212#endif
1213
1214 while( 1 )
1215 {
Hanno Becker197a91c2017-10-31 10:58:53 +00001216 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +01001217#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001218 if( timer != NULL &&
1219 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +01001220 {
Hanno Becker16970d22017-10-10 15:56:37 +01001221 break;
1222 }
Hanno Becker197a91c2017-10-31 10:58:53 +00001223#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +01001224
Hanno Becker197a91c2017-10-31 10:58:53 +00001225 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001226 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +01001227 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001228 ret = mbedtls_net_poll( fd, poll_type, 0 );
1229 if( ret < 0 )
1230 return( ret );
1231 if( ret == poll_type )
1232 break;
Hanno Becker16970d22017-10-10 15:56:37 +01001233 }
1234 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001235
1236 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001237}
1238
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001239int main( int argc, char *argv[] )
1240{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001241 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001242 int version_suites[4][2];
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001243 unsigned char* buf = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1245 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +02001246 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +02001247 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +02001248#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001249 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001250 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02001251 size_t cliip_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252#if defined(MBEDTLS_SSL_COOKIE_C)
1253 mbedtls_ssl_cookie_ctx cookie_ctx;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001254#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001255
Gilles Peskineef86ab22017-05-05 18:59:02 +02001256#if defined(MBEDTLS_X509_CRT_PARSE_C)
1257 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
1258#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 mbedtls_entropy_context entropy;
1260 mbedtls_ctr_drbg_context ctr_drbg;
1261 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001262 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001263#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001264 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001265#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001267 unsigned char renego_period[8] = { 0 };
1268#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +02001270 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 mbedtls_x509_crt cacert;
1272 mbedtls_x509_crt srvcert;
1273 mbedtls_pk_context pkey;
1274 mbedtls_x509_crt srvcert2;
1275 mbedtls_pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001276 int key_cert_init = 0, key_cert_init2 = 0;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001277#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001278 ssl_async_key_context_t ssl_async_keys;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001279#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001280#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1282 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001283#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284#if defined(MBEDTLS_SSL_CACHE_C)
1285 mbedtls_ssl_cache_context cache;
Paul Bakker0a597072012-09-25 21:55:46 +00001286#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001287#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1288 mbedtls_ssl_ticket_context ticket_ctx;
1289#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001290#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001291 sni_entry *sni_info = NULL;
1292#endif
Hanno Beckere6706e62017-05-15 16:05:15 +01001293#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +01001294 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +01001295 const mbedtls_ecp_curve_info * curve_cur;
1296#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +01001298 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001299#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Simon Butchercce68be2018-07-23 14:26:09 +01001301 unsigned char alloc_buf[MEMORY_HEAP_SIZE];
Paul Bakker82024bf2013-07-04 11:52:32 +02001302#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001303
1304 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001305 char *p, *q;
1306 const int *list;
1307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1309 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker82024bf2013-07-04 11:52:32 +02001310#endif
1311
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001312 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +02001313 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001314 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001315 mbedtls_net_init( &client_fd );
1316 mbedtls_net_init( &listen_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001317 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001318 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001319 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320#if defined(MBEDTLS_X509_CRT_PARSE_C)
1321 mbedtls_x509_crt_init( &cacert );
1322 mbedtls_x509_crt_init( &srvcert );
1323 mbedtls_pk_init( &pkey );
1324 mbedtls_x509_crt_init( &srvcert2 );
1325 mbedtls_pk_init( &pkey2 );
Gilles Peskine4d9ec4d2018-04-26 14:33:43 +02001326#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
1327 memset( &ssl_async_keys, 0, sizeof( ssl_async_keys ) );
1328#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001329#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1331 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001332#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#if defined(MBEDTLS_SSL_CACHE_C)
1334 mbedtls_ssl_cache_init( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00001335#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001336#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1337 mbedtls_ssl_ticket_init( &ticket_ctx );
1338#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +02001340 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001341#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342#if defined(MBEDTLS_SSL_COOKIE_C)
1343 mbedtls_ssl_cookie_init( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001344#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001345
Paul Bakkerc1283d32014-08-18 11:05:51 +02001346#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001347 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001348 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001349 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +02001350#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001351
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001352 if( argc == 0 )
1353 {
1354 usage:
1355 if( ret == 0 )
1356 ret = 1;
1357
Gilles Peskined135bbd2020-04-14 19:41:01 +02001358 mbedtls_printf( USAGE1 );
1359 mbedtls_printf( USAGE2 );
1360 mbedtls_printf( USAGE3 );
1361 mbedtls_printf( USAGE4 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 list = mbedtls_ssl_list_ciphersuites();
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001364 while( *list )
1365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001367 list++;
1368 if( !*list )
1369 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001371 list++;
1372 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 mbedtls_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001374 goto exit;
1375 }
1376
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001377 opt.buffer_size = DFL_IO_BUF_LEN;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001378 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001379 opt.server_port = DFL_SERVER_PORT;
1380 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker16970d22017-10-10 15:56:37 +01001381 opt.event = DFL_EVENT;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001382 opt.response_size = DFL_RESPONSE_SIZE;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001383 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001384 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001385 opt.ca_file = DFL_CA_FILE;
1386 opt.ca_path = DFL_CA_PATH;
1387 opt.crt_file = DFL_CRT_FILE;
1388 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001389 opt.crt_file2 = DFL_CRT_FILE2;
1390 opt.key_file2 = DFL_KEY_FILE2;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001391 opt.async_operations = DFL_ASYNC_OPERATIONS;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001392 opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
1393 opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
1394 opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
Paul Bakkerfbb17802013-04-17 19:10:21 +02001395 opt.psk = DFL_PSK;
1396 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001397 opt.psk_list = DFL_PSK_LIST;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001398 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001399 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001400 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001401 opt.renegotiation = DFL_RENEGOTIATION;
1402 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001403 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001404 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001405 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001406 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001407 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001408 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001409 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001410 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001411 opt.auth_mode = DFL_AUTH_MODE;
Janos Follath4817e272017-04-10 13:44:33 +01001412 opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001413 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001414 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001415 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001416 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001417 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001418 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001419 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001420 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001421 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001422 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001423 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001424 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001425 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001426 opt.hs_to_min = DFL_HS_TO_MIN;
1427 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001428 opt.dtls_mtu = DFL_DTLS_MTU;
Hanno Beckere7675d02018-08-14 13:28:56 +01001429 opt.dgram_packing = DFL_DGRAM_PACKING;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001430 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001431 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001432 opt.etm = DFL_ETM;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001433
1434 for( i = 1; i < argc; i++ )
1435 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001436 p = argv[i];
1437 if( ( q = strchr( p, '=' ) ) == NULL )
1438 goto usage;
1439 *q++ = '\0';
1440
1441 if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001442 opt.server_port = q;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001443 else if( strcmp( p, "server_addr" ) == 0 )
1444 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001445 else if( strcmp( p, "dtls" ) == 0 )
1446 {
1447 int t = atoi( q );
1448 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001450 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001452 else
1453 goto usage;
1454 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001455 else if( strcmp( p, "debug_level" ) == 0 )
1456 {
1457 opt.debug_level = atoi( q );
1458 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1459 goto usage;
1460 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001461 else if( strcmp( p, "nbio" ) == 0 )
1462 {
1463 opt.nbio = atoi( q );
1464 if( opt.nbio < 0 || opt.nbio > 2 )
1465 goto usage;
1466 }
Hanno Becker16970d22017-10-10 15:56:37 +01001467 else if( strcmp( p, "event" ) == 0 )
1468 {
1469 opt.event = atoi( q );
1470 if( opt.event < 0 || opt.event > 2 )
1471 goto usage;
1472 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001473 else if( strcmp( p, "read_timeout" ) == 0 )
1474 opt.read_timeout = atoi( q );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001475 else if( strcmp( p, "buffer_size" ) == 0 )
1476 {
1477 opt.buffer_size = atoi( q );
1478 if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
1479 goto usage;
1480 }
1481 else if( strcmp( p, "response_size" ) == 0 )
1482 {
1483 opt.response_size = atoi( q );
1484 if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
1485 goto usage;
1486 if( opt.buffer_size < opt.response_size )
1487 opt.buffer_size = opt.response_size;
1488 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001489 else if( strcmp( p, "ca_file" ) == 0 )
1490 opt.ca_file = q;
1491 else if( strcmp( p, "ca_path" ) == 0 )
1492 opt.ca_path = q;
1493 else if( strcmp( p, "crt_file" ) == 0 )
1494 opt.crt_file = q;
1495 else if( strcmp( p, "key_file" ) == 0 )
1496 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001497 else if( strcmp( p, "crt_file2" ) == 0 )
1498 opt.crt_file2 = q;
1499 else if( strcmp( p, "key_file2" ) == 0 )
1500 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001501 else if( strcmp( p, "dhm_file" ) == 0 )
1502 opt.dhm_file = q;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001503#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001504 else if( strcmp( p, "async_operations" ) == 0 )
1505 opt.async_operations = q;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001506 else if( strcmp( p, "async_private_delay1" ) == 0 )
1507 opt.async_private_delay1 = atoi( q );
1508 else if( strcmp( p, "async_private_delay2" ) == 0 )
1509 opt.async_private_delay2 = atoi( q );
1510 else if( strcmp( p, "async_private_error" ) == 0 )
1511 {
1512 int n = atoi( q );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01001513 if( n < -SSL_ASYNC_INJECT_ERROR_MAX ||
1514 n > SSL_ASYNC_INJECT_ERROR_MAX )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001515 {
1516 ret = 2;
1517 goto usage;
1518 }
1519 opt.async_private_error = n;
1520 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001521#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001522 else if( strcmp( p, "psk" ) == 0 )
1523 opt.psk = q;
1524 else if( strcmp( p, "psk_identity" ) == 0 )
1525 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001526 else if( strcmp( p, "psk_list" ) == 0 )
1527 opt.psk_list = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001528 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1529 opt.ecjpake_pw = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001530 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001533
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001534 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001535 {
1536 ret = 2;
1537 goto usage;
1538 }
1539 opt.force_ciphersuite[1] = 0;
1540 }
Hanno Beckere6706e62017-05-15 16:05:15 +01001541 else if( strcmp( p, "curves" ) == 0 )
1542 opt.curves = q;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001543 else if( strcmp( p, "version_suites" ) == 0 )
1544 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001545 else if( strcmp( p, "renegotiation" ) == 0 )
1546 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001547 opt.renegotiation = (atoi( q )) ?
1548 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1549 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001550 }
1551 else if( strcmp( p, "allow_legacy" ) == 0 )
1552 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001553 switch( atoi( q ) )
1554 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001555 case -1:
1556 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1557 break;
1558 case 0:
1559 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1560 break;
1561 case 1:
1562 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1563 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001564 default: goto usage;
1565 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001566 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001567 else if( strcmp( p, "renegotiate" ) == 0 )
1568 {
1569 opt.renegotiate = atoi( q );
1570 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1571 goto usage;
1572 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001573 else if( strcmp( p, "renego_delay" ) == 0 )
1574 {
1575 opt.renego_delay = atoi( q );
1576 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001577 else if( strcmp( p, "renego_period" ) == 0 )
1578 {
Andres AG0b736db2017-02-08 14:05:57 +00001579#if defined(_MSC_VER)
1580 opt.renego_period = _strtoui64( q, NULL, 10 );
1581#else
1582 if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
1583 goto usage;
1584#endif /* _MSC_VER */
1585 if( opt.renego_period < 2 )
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001586 goto usage;
1587 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001588 else if( strcmp( p, "exchanges" ) == 0 )
1589 {
1590 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001591 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001592 goto usage;
1593 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001594 else if( strcmp( p, "min_version" ) == 0 )
1595 {
1596 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001598 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001600 else if( strcmp( q, "tls1_1" ) == 0 ||
1601 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001603 else if( strcmp( q, "tls1_2" ) == 0 ||
1604 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001606 else
1607 goto usage;
1608 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001609 else if( strcmp( p, "max_version" ) == 0 )
1610 {
1611 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001613 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001615 else if( strcmp( q, "tls1_1" ) == 0 ||
1616 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001618 else if( strcmp( q, "tls1_2" ) == 0 ||
1619 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001621 else
1622 goto usage;
1623 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001624 else if( strcmp( p, "arc4" ) == 0 )
1625 {
1626 switch( atoi( q ) )
1627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1629 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001630 default: goto usage;
1631 }
1632 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02001633 else if( strcmp( p, "allow_sha1" ) == 0 )
1634 {
1635 switch( atoi( q ) )
1636 {
1637 case 0: opt.allow_sha1 = 0; break;
1638 case 1: opt.allow_sha1 = 1; break;
1639 default: goto usage;
1640 }
1641 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001642 else if( strcmp( p, "force_version" ) == 0 )
1643 {
1644 if( strcmp( q, "ssl3" ) == 0 )
1645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1647 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001648 }
1649 else if( strcmp( q, "tls1" ) == 0 )
1650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1652 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001653 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001654 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1657 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001658 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001659 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1662 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001663 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001664 else if( strcmp( q, "dtls1" ) == 0 )
1665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1667 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1668 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001669 }
1670 else if( strcmp( q, "dtls1_2" ) == 0 )
1671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1673 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1674 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001675 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001676 else
1677 goto usage;
1678 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001679 else if( strcmp( p, "auth_mode" ) == 0 )
1680 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001681 if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
Paul Bakker91ebfb52012-11-23 14:04:08 +01001682 goto usage;
1683 }
Janos Follath4817e272017-04-10 13:44:33 +01001684 else if( strcmp( p, "cert_req_ca_list" ) == 0 )
1685 {
1686 opt.cert_req_ca_list = atoi( q );
1687 if( opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1 )
1688 goto usage;
1689 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001690 else if( strcmp( p, "max_frag_len" ) == 0 )
1691 {
1692 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001694 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001696 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001698 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001700 else
1701 goto usage;
1702 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001703 else if( strcmp( p, "alpn" ) == 0 )
1704 {
1705 opt.alpn_string = q;
1706 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001707 else if( strcmp( p, "trunc_hmac" ) == 0 )
1708 {
1709 switch( atoi( q ) )
1710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1712 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001713 default: goto usage;
1714 }
1715 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001716 else if( strcmp( p, "extended_ms" ) == 0 )
1717 {
1718 switch( atoi( q ) )
1719 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001720 case 0:
1721 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1722 break;
1723 case 1:
1724 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1725 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001726 default: goto usage;
1727 }
1728 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001729 else if( strcmp( p, "etm" ) == 0 )
1730 {
1731 switch( atoi( q ) )
1732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1734 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001735 default: goto usage;
1736 }
1737 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001738 else if( strcmp( p, "tickets" ) == 0 )
1739 {
1740 opt.tickets = atoi( q );
1741 if( opt.tickets < 0 || opt.tickets > 1 )
1742 goto usage;
1743 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001744 else if( strcmp( p, "ticket_timeout" ) == 0 )
1745 {
1746 opt.ticket_timeout = atoi( q );
1747 if( opt.ticket_timeout < 0 )
1748 goto usage;
1749 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001750 else if( strcmp( p, "cache_max" ) == 0 )
1751 {
1752 opt.cache_max = atoi( q );
1753 if( opt.cache_max < 0 )
1754 goto usage;
1755 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001756 else if( strcmp( p, "cache_timeout" ) == 0 )
1757 {
1758 opt.cache_timeout = atoi( q );
1759 if( opt.cache_timeout < 0 )
1760 goto usage;
1761 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001762 else if( strcmp( p, "cookies" ) == 0 )
1763 {
1764 opt.cookies = atoi( q );
1765 if( opt.cookies < -1 || opt.cookies > 1)
1766 goto usage;
1767 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001768 else if( strcmp( p, "anti_replay" ) == 0 )
1769 {
1770 opt.anti_replay = atoi( q );
1771 if( opt.anti_replay < 0 || opt.anti_replay > 1)
1772 goto usage;
1773 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001774 else if( strcmp( p, "badmac_limit" ) == 0 )
1775 {
1776 opt.badmac_limit = atoi( q );
1777 if( opt.badmac_limit < 0 )
1778 goto usage;
1779 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001780 else if( strcmp( p, "hs_timeout" ) == 0 )
1781 {
1782 if( ( p = strchr( q, '-' ) ) == NULL )
1783 goto usage;
1784 *p++ = '\0';
1785 opt.hs_to_min = atoi( q );
1786 opt.hs_to_max = atoi( p );
1787 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1788 goto usage;
1789 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001790 else if( strcmp( p, "mtu" ) == 0 )
1791 {
1792 opt.dtls_mtu = atoi( q );
1793 if( opt.dtls_mtu < 0 )
1794 goto usage;
1795 }
Hanno Beckere7675d02018-08-14 13:28:56 +01001796 else if( strcmp( p, "dgram_packing" ) == 0 )
1797 {
1798 opt.dgram_packing = atoi( q );
1799 if( opt.dgram_packing != 0 &&
1800 opt.dgram_packing != 1 )
1801 {
1802 goto usage;
1803 }
1804 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001805 else if( strcmp( p, "sni" ) == 0 )
1806 {
1807 opt.sni = q;
1808 }
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +01001809 else if( strcmp( p, "query_config" ) == 0 )
1810 {
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +02001811 mbedtls_exit( query_config( q ) );
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +01001812 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001813 else
1814 goto usage;
1815 }
1816
Hanno Becker16970d22017-10-10 15:56:37 +01001817 /* Event-driven IO is incompatible with the above custom
1818 * receive and send functions, as the polling builds on
1819 * refers to the underlying net_context. */
1820 if( opt.event == 1 && opt.nbio != 1 )
1821 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001822 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001823 opt.nbio = 1;
1824 }
1825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826#if defined(MBEDTLS_DEBUG_C)
1827 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001828#endif
Andrzej Kurekda4029d2018-06-20 07:07:55 -04001829 buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001830 if( buf == NULL )
1831 {
Andrzej Kurek755890f2018-06-20 08:17:04 -04001832 mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001833 ret = 3;
1834 goto exit;
1835 }
Paul Bakkerc73079a2014-04-25 16:34:30 +02001836
Paul Bakkerc1516be2013-06-29 16:01:32 +02001837 if( opt.force_ciphersuite[0] > 0 )
1838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001840 ciphersuite_info =
1841 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001842
Paul Bakker5b55b792013-07-19 13:43:43 +02001843 if( opt.max_version != -1 &&
1844 ciphersuite_info->min_minor_ver > opt.max_version )
1845 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001846 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker5b55b792013-07-19 13:43:43 +02001847 ret = 2;
1848 goto usage;
1849 }
1850 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001851 ciphersuite_info->max_minor_ver < opt.min_version )
1852 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001853 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001854 ret = 2;
1855 goto usage;
1856 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001857
1858 /* If we select a version that's not supported by
1859 * this suite, then there will be no common ciphersuite... */
1860 if( opt.max_version == -1 ||
1861 opt.max_version > ciphersuite_info->max_minor_ver )
1862 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001863 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001864 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001865 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001866 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001867 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001868 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1870 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1871 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001872 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001873
1874 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001880 ret = 2;
1881 goto usage;
1882 }
1883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001885 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001886 }
1887
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001888 if( opt.version_suites != NULL )
1889 {
1890 const char *name[4] = { 0 };
1891
1892 /* Parse 4-element coma-separated list */
1893 for( i = 0, p = (char *) opt.version_suites;
1894 i < 4 && *p != '\0';
1895 i++ )
1896 {
1897 name[i] = p;
1898
1899 /* Terminate the current string and move on to next one */
1900 while( *p != ',' && *p != '\0' )
1901 p++;
1902 if( *p == ',' )
1903 *p++ = '\0';
1904 }
1905
1906 if( i != 4 )
1907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 mbedtls_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001909 ret = 1;
1910 goto exit;
1911 }
1912
1913 memset( version_suites, 0, sizeof( version_suites ) );
1914
1915 /* Get the suites identifiers from their name */
1916 for( i = 0; i < 4; i++ )
1917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001919
1920 if( version_suites[i][0] == 0 )
1921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001923 ret = 2;
1924 goto usage;
1925 }
1926 }
1927 }
1928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001930 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001931 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001932 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001933 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 mbedtls_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001936 goto exit;
1937 }
1938
1939 if( opt.psk_list != NULL )
1940 {
1941 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 mbedtls_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001944 goto exit;
1945 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001946 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001948
Hanno Beckere6706e62017-05-15 16:05:15 +01001949#if defined(MBEDTLS_ECP_C)
1950 if( opt.curves != NULL )
1951 {
1952 p = (char *) opt.curves;
1953 i = 0;
1954
1955 if( strcmp( p, "none" ) == 0 )
1956 {
1957 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1958 }
1959 else if( strcmp( p, "default" ) != 0 )
1960 {
1961 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001962 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001963 {
1964 q = p;
1965
1966 /* Terminate the current string */
1967 while( *p != ',' && *p != '\0' )
1968 p++;
1969 if( *p == ',' )
1970 *p++ = '\0';
1971
1972 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1973 {
1974 curve_list[i++] = curve_cur->grp_id;
1975 }
1976 else
1977 {
1978 mbedtls_printf( "unknown curve %s\n", q );
1979 mbedtls_printf( "supported curves: " );
1980 for( curve_cur = mbedtls_ecp_curve_list();
1981 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1982 curve_cur++ )
1983 {
1984 mbedtls_printf( "%s ", curve_cur->name );
1985 }
1986 mbedtls_printf( "\n" );
1987 goto exit;
1988 }
1989 }
1990
1991 mbedtls_printf("Number of curves: %d\n", i );
1992
Hanno Becker8651a432017-06-09 16:13:22 +01001993 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001994 {
Hanno Becker8651a432017-06-09 16:13:22 +01001995 mbedtls_printf( "curves list too long, maximum %d",
1996 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001997 goto exit;
1998 }
1999
2000 curve_list[i] = MBEDTLS_ECP_DP_NONE;
2001 }
2002 }
2003#endif /* MBEDTLS_ECP_C */
2004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002006 if( opt.alpn_string != NULL )
2007 {
2008 p = (char *) opt.alpn_string;
2009 i = 0;
2010
2011 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01002012 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002013 {
2014 alpn_list[i++] = p;
2015
2016 /* Terminate the current string and move on to next one */
2017 while( *p != ',' && *p != '\0' )
2018 p++;
2019 if( *p == ',' )
2020 *p++ = '\0';
2021 }
2022 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002024
Paul Bakkerfbb17802013-04-17 19:10:21 +02002025 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002026 * 0. Initialize the RNG and the session data
2027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002029 fflush( stdout );
2030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 mbedtls_entropy_init( &entropy );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002032 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
2033 &entropy, (const unsigned char *) pers,
2034 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002035 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002036 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
2037 -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002038 goto exit;
2039 }
2040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002044 /*
2045 * 1.1. Load the trusted CA
2046 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002048 fflush( stdout );
2049
Hanno Becker7ae36e42019-03-05 16:22:07 +00002050 if( strcmp( opt.ca_path, "none" ) == 0 ||
2051 strcmp( opt.ca_file, "none" ) == 0 )
2052 {
2053 ret = 0;
2054 }
2055 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002057 if( strlen( opt.ca_path ) )
Hanno Becker7ae36e42019-03-05 16:22:07 +00002058 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002059 else if( strlen( opt.ca_file ) )
Hanno Becker7ae36e42019-03-05 16:22:07 +00002060 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02002061 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002062#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063#if defined(MBEDTLS_CERTS_C)
Hanno Becker38566cc2019-02-01 08:13:19 +00002064 {
2065#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068 ret = mbedtls_x509_crt_parse( &cacert,
2069 (const unsigned char *) mbedtls_test_cas[i],
2070 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002071 if( ret != 0 )
2072 break;
2073 }
Hanno Becker38566cc2019-02-01 08:13:19 +00002074 if( ret == 0 )
2075#endif /* MBEDTLS_PEM_PARSE_C */
2076 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
2077 {
2078 ret = mbedtls_x509_crt_parse_der( &cacert,
2079 (const unsigned char *) mbedtls_test_cas_der[i],
2080 mbedtls_test_cas_der_len[i] );
2081 if( ret != 0 )
2082 break;
2083 }
2084 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002085#else
2086 {
2087 ret = 1;
Hanno Beckerc258c442018-12-05 16:49:42 +00002088 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002089 }
Hanno Becker38566cc2019-02-01 08:13:19 +00002090#endif /* MBEDTLS_CERTS_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002091 if( ret < 0 )
2092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002094 goto exit;
2095 }
2096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002098
2099 /*
2100 * 1.2. Load own certificate and private key
2101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002103 fflush( stdout );
2104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002106 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002107 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002108 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002112 -ret );
2113 goto exit;
2114 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002115 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002116 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002117 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002118 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002122 goto exit;
2123 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002124 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002125 if( key_cert_init == 1 )
2126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002128 goto exit;
2129 }
2130
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002131 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002132 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002133 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002137 -ret );
2138 goto exit;
2139 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002140 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002141 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002142 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002143 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002147 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002148 goto exit;
2149 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002150 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002151 if( key_cert_init2 == 1 )
2152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002154 goto exit;
2155 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002156#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002157 if( key_cert_init == 0 &&
2158 strcmp( opt.crt_file, "none" ) != 0 &&
2159 strcmp( opt.key_file, "none" ) != 0 &&
2160 key_cert_init2 == 0 &&
2161 strcmp( opt.crt_file2, "none" ) != 0 &&
2162 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164#if !defined(MBEDTLS_CERTS_C)
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002165 mbedtls_printf( "Not certificated or key provided, and \nMBEDTLS_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002166 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002167#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168#if defined(MBEDTLS_RSA_C)
2169 if( ( ret = mbedtls_x509_crt_parse( &srvcert,
2170 (const unsigned char *) mbedtls_test_srv_crt_rsa,
2171 mbedtls_test_srv_crt_rsa_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002172 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002173 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
2174 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002175 goto exit;
2176 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 if( ( ret = mbedtls_pk_parse_key( &pkey,
2178 (const unsigned char *) mbedtls_test_srv_key_rsa,
2179 mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002180 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002181 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
2182 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002183 goto exit;
2184 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002185 key_cert_init = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186#endif /* MBEDTLS_RSA_C */
2187#if defined(MBEDTLS_ECDSA_C)
2188 if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
2189 (const unsigned char *) mbedtls_test_srv_crt_ec,
2190 mbedtls_test_srv_crt_ec_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002191 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002192 mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n",
2193 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002194 goto exit;
2195 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 if( ( ret = mbedtls_pk_parse_key( &pkey2,
2197 (const unsigned char *) mbedtls_test_srv_key_ec,
2198 mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002199 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002200 mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n",
2201 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002202 goto exit;
2203 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002204 key_cert_init2 = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205#endif /* MBEDTLS_ECDSA_C */
2206#endif /* MBEDTLS_CERTS_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002207 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 mbedtls_printf( " ok\n" );
2210#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002213 if( opt.dhm_file != NULL )
2214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 mbedtls_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002216 fflush( stdout );
2217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002221 -ret );
2222 goto exit;
2223 }
2224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002226 }
2227#endif
2228
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002229#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002230 if( opt.sni != NULL )
2231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 mbedtls_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002233 fflush( stdout );
2234
2235 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
2236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002238 goto exit;
2239 }
2240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002242 }
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002243#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002244
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002245 /*
2246 * 2. Setup the listening TCP socket
2247 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02002248 mbedtls_printf( " . Bind on %s://%s:%s/ ...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002250 opt.server_addr ? opt.server_addr : "*",
2251 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002252 fflush( stdout );
2253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
2255 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2256 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002259 goto exit;
2260 }
2261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002263
2264 /*
2265 * 3. Setup stuff
2266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002268 fflush( stdout );
2269
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02002270 if( ( ret = mbedtls_ssl_config_defaults( &conf,
2271 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02002272 opt.transport,
2273 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002274 {
2275 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
2276 goto exit;
2277 }
2278
Gilles Peskineef86ab22017-05-05 18:59:02 +02002279#if defined(MBEDTLS_X509_CRT_PARSE_C)
2280 /* The default algorithms profile disables SHA-1, but our tests still
2281 rely on it heavily. Hence we allow it here. A real-world server
2282 should use the default profile unless there is a good reason not to. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02002283 if( opt.allow_sha1 > 0 )
2284 {
2285 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
2286 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
Gilles Peskine682df092017-05-11 19:01:11 +02002287 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
Gilles Peskinebc70a182017-05-09 15:59:24 +02002288 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02002289#endif /* MBEDTLS_X509_CRT_PARSE_C */
2290
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01002291 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002292 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002293
Janos Follath4817e272017-04-10 13:44:33 +01002294 if( opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST )
2295 mbedtls_ssl_conf_cert_req_ca_list( &conf, opt.cert_req_ca_list );
2296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002298 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 +02002299 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002300
Hanno Beckere7675d02018-08-14 13:28:56 +01002301 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01002302 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002306 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002307 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002308 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002309 goto exit;
2310 };
Paul Bakker05decb22013-08-15 13:33:48 +02002311#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002314 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002315 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002316#endif
2317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002319 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002320 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002321#endif
2322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002324 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002325 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002326#endif
2327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002329 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002330 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002331 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002332 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002333 goto exit;
2334 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002335#endif
2336
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002337 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
2338 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002341 if( opt.cache_max != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002343
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002344 if( opt.cache_timeout != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002346
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002347 mbedtls_ssl_conf_session_cache( &conf, &cache,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01002348 mbedtls_ssl_cache_get,
2349 mbedtls_ssl_cache_set );
Paul Bakker0a597072012-09-25 21:55:46 +00002350#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002353 if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002354 {
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002355 if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
2356 mbedtls_ctr_drbg_random, &ctr_drbg,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +02002357 MBEDTLS_CIPHER_AES_256_GCM,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002358 opt.ticket_timeout ) ) != 0 )
2359 {
2360 mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
2361 goto exit;
2362 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01002363
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002364 mbedtls_ssl_conf_session_tickets_cb( &conf,
2365 mbedtls_ssl_ticket_write,
2366 mbedtls_ssl_ticket_parse,
2367 &ticket_ctx );
2368 }
Paul Bakkera503a632013-08-14 13:48:06 +02002369#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371#if defined(MBEDTLS_SSL_PROTO_DTLS)
2372 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002375 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
2378 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002381 goto exit;
2382 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002383
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002384 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002385 &cookie_ctx );
2386 }
2387 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388#endif /* MBEDTLS_SSL_COOKIE_C */
2389#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002390 if( opt.cookies == 0 )
2391 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002392 mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002393 }
2394 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002396 {
2397 ; /* Nothing to do */
2398 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002401 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002402 mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002403#endif
2404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002406 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002407 mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002408#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002409 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002411
Paul Bakker41c83d32013-03-20 14:39:14 +01002412 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002413 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002414
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002415#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002416 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002417 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002418#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002419
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002420 if( opt.version_suites != NULL )
2421 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002422 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 MBEDTLS_SSL_MAJOR_VERSION_3,
2424 MBEDTLS_SSL_MINOR_VERSION_0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002425 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 MBEDTLS_SSL_MAJOR_VERSION_3,
2427 MBEDTLS_SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002428 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 MBEDTLS_SSL_MAJOR_VERSION_3,
2430 MBEDTLS_SSL_MINOR_VERSION_2 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002431 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_SSL_MAJOR_VERSION_3,
2433 MBEDTLS_SSL_MINOR_VERSION_3 );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002434 }
2435
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002436 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002437 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002439 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002440
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02002441 if( opt.renego_delay != DFL_RENEGO_DELAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002442 mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002443
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002444 if( opt.renego_period != DFL_RENEGO_PERIOD )
2445 {
Andres AG692ad842017-01-19 16:30:57 +00002446 PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002447 mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002448 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002449#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002452 if( strcmp( opt.ca_path, "none" ) != 0 &&
2453 strcmp( opt.ca_file, "none" ) != 0 )
2454 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002455 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002456 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002457 if( key_cert_init )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002458 {
2459 mbedtls_pk_context *pk = &pkey;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002460#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002461 if( opt.async_private_delay1 >= 0 )
2462 {
Gilles Peskine44817442018-06-13 18:09:28 +02002463 ret = ssl_async_set_key( &ssl_async_keys, &srvcert, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002464 opt.async_private_delay1 );
2465 if( ret < 0 )
2466 {
2467 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2468 ret );
2469 goto exit;
2470 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002471 pk = NULL;
2472 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002473#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002474 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002475 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002476 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002477 goto exit;
2478 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002479 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002480 if( key_cert_init2 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002481 {
2482 mbedtls_pk_context *pk = &pkey2;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002483#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002484 if( opt.async_private_delay2 >= 0 )
2485 {
Gilles Peskine44817442018-06-13 18:09:28 +02002486 ret = ssl_async_set_key( &ssl_async_keys, &srvcert2, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002487 opt.async_private_delay2 );
2488 if( ret < 0 )
2489 {
2490 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2491 ret );
2492 goto exit;
2493 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002494 pk = NULL;
2495 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002496#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002497 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002498 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002499 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002500 goto exit;
2501 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002502 }
2503
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002504#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002505 if( opt.async_operations[0] != '-' )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002506 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002507 mbedtls_ssl_async_sign_t *sign = NULL;
2508 mbedtls_ssl_async_decrypt_t *decrypt = NULL;
Gilles Peskine12ab5d42018-04-24 12:32:04 +02002509 const char *r;
2510 for( r = opt.async_operations; *r; r++ )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002511 {
Gilles Peskine12ab5d42018-04-24 12:32:04 +02002512 switch( *r )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002513 {
2514 case 'd':
2515 decrypt = ssl_async_decrypt;
2516 break;
2517 case 's':
2518 sign = ssl_async_sign;
2519 break;
2520 }
2521 }
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002522 ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
2523 - opt.async_private_error :
2524 opt.async_private_error );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002525 ssl_async_keys.f_rng = mbedtls_ctr_drbg_random;
2526 ssl_async_keys.p_rng = &ctr_drbg;
2527 mbedtls_ssl_conf_async_private_cb( &conf,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002528 sign,
2529 decrypt,
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002530 ssl_async_resume,
2531 ssl_async_cancel,
2532 &ssl_async_keys );
2533 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002534#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002535#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +02002536
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002537#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002538 if( opt.sni != NULL )
Gilles Peskine166ce742018-04-30 10:30:49 +02002539 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002540 mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
Gilles Peskine166ce742018-04-30 10:30:49 +02002541#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
2542 if( opt.async_private_delay2 >= 0 )
2543 {
Gilles Peskinee2479892018-06-13 18:06:51 +02002544 sni_entry *cur;
2545 for( cur = sni_info; cur != NULL; cur = cur->next )
Gilles Peskine166ce742018-04-30 10:30:49 +02002546 {
Gilles Peskinee2479892018-06-13 18:06:51 +02002547 ret = ssl_async_set_key( &ssl_async_keys,
Gilles Peskine44817442018-06-13 18:09:28 +02002548 cur->cert, cur->key, 1,
Gilles Peskinee2479892018-06-13 18:06:51 +02002549 opt.async_private_delay2 );
2550 if( ret < 0 )
2551 {
2552 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2553 ret );
2554 goto exit;
2555 }
2556 cur->key = NULL;
Gilles Peskine166ce742018-04-30 10:30:49 +02002557 }
Gilles Peskine166ce742018-04-30 10:30:49 +02002558 }
2559#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
2560 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002561#endif
2562
Hanno Beckere6706e62017-05-15 16:05:15 +01002563#if defined(MBEDTLS_ECP_C)
2564 if( opt.curves != NULL &&
2565 strcmp( opt.curves, "default" ) != 0 )
2566 {
2567 mbedtls_ssl_conf_curves( &conf, curve_list );
2568 }
2569#endif
2570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002572 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
2573 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002574 ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002575 (const unsigned char *) opt.psk_identity,
2576 strlen( opt.psk_identity ) );
2577 if( ret != 0 )
2578 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002579 mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002580 goto exit;
2581 }
2582 }
2583
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002584 if( opt.psk_list != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002585 mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02002586#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#if defined(MBEDTLS_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00002589 /*
2590 * Use different group than default DHM group
2591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002593 if( opt.dhm_file != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002594 ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002595#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002596 if( ret != 0 )
2597 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002598 mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002599 goto exit;
2600 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002601#endif
2602
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002603 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002604 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00002605
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002606 if( opt.max_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002607 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002608
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002609 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
2610 {
2611 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
2612 goto exit;
2613 }
2614
2615 if( opt.nbio == 2 )
2616 mbedtls_ssl_set_bio( &ssl, &client_fd, my_send, my_recv, NULL );
2617 else
2618 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02002619 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002620
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002621#if defined(MBEDTLS_SSL_PROTO_DTLS)
2622 if( opt.dtls_mtu != DFL_DTLS_MTU )
2623 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
2624#endif
2625
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002626#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002627 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
2628 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002629#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002632
2633reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002634#if !defined(_WIN32)
2635 if( received_sigterm )
2636 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01002637 mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" );
2638 if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT )
2639 ret = 0;
2640
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002641 goto exit;
2642 }
2643#endif
2644
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002645 if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
2646 {
2647 mbedtls_printf( " ! Client initiated reconnection from same port\n" );
2648 goto handshake;
2649 }
2650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002652 if( ret != 0 )
2653 {
2654 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 mbedtls_strerror( ret, error_buf, 100 );
2656 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002657 }
2658#endif
2659
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002660 mbedtls_net_free( &client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 mbedtls_ssl_session_reset( &ssl );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002663
2664 /*
2665 * 3. Wait until a client connects
2666 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 mbedtls_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002668 fflush( stdout );
2669
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002670 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02002671 client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002672 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02002673#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002674 if( received_sigterm )
2675 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01002676 mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" );
2677 if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED )
2678 ret = 0;
2679
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002680 goto exit;
2681 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02002682#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002685 goto exit;
2686 }
2687
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002688 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002689 ret = mbedtls_net_set_nonblock( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002690 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002691 ret = mbedtls_net_set_block( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002692 if( ret != 0 )
2693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002695 goto exit;
2696 }
2697
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002698 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2701 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002702 {
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02002703 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
2704 client_ip, cliip_len ) ) != 0 )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002705 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002706 mbedtls_printf( " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n",
2707 -ret );
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002708 goto exit;
2709 }
2710 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002712
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002713#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2714 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
2715 {
2716 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
2717 (const unsigned char *) opt.ecjpake_pw,
2718 strlen( opt.ecjpake_pw ) ) ) != 0 )
2719 {
2720 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
2721 goto exit;
2722 }
2723 }
2724#endif
2725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002727
2728 /*
2729 * 4. Handshake
2730 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002731handshake:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002733 fflush( stdout );
2734
Hanno Becker16970d22017-10-10 15:56:37 +01002735 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002736 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002737#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002738 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002739 ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002740 {
2741 mbedtls_printf( " cancelling on injected error\n" );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002742 break;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002743 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002744#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskineb44692f2018-04-24 12:18:19 +02002745
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02002746 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01002747 break;
2748
2749 /* For event-driven IO, wait for socket to become available */
2750 if( opt.event == 1 /* level triggered IO */ )
2751 {
2752#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002753 ret = idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002754#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002755 ret = idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002756#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002757 if( ret != 0 )
2758 goto reset;
Hanno Becker16970d22017-10-10 15:56:37 +01002759 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002760 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 mbedtls_printf( " hello verification requested\n" );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002765 ret = 0;
2766 goto reset;
2767 }
2768 else if( ret != 0 )
2769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002771
2772#if defined(MBEDTLS_X509_CRT_PARSE_C)
2773 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2774 {
2775 char vrfy_buf[512];
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02002776 flags = mbedtls_ssl_get_verify_result( &ssl );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002777
2778 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
2779
2780 mbedtls_printf( "%s\n", vrfy_buf );
2781 }
2782#endif
2783
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002784#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002785 if( opt.async_private_error < 0 )
2786 /* Injected error only the first time round, to test reset */
2787 ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
2788#endif
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002789 goto reset;
2790 }
2791 else /* ret == 0 */
2792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
2794 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002795 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
2798 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002799 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002801
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002802#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2803 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
2804 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
2805#endif
2806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002808 if( opt.alpn_string != NULL )
2809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
2811 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002812 alp ? alp : "(none)" );
2813 }
2814#endif
2815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002817 /*
Ron Eldor2a47be52017-06-20 15:23:23 +03002818 * 5. Verify the client certificate
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002819 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002821
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002822 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002823 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002824 char vrfy_buf[512];
2825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 mbedtls_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002827
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002828 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002829
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002830 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002831 }
2832 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002834
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02002835 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002836 {
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02002837 char crt_buf[512];
2838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 mbedtls_printf( " . Peer certificate information ...\n" );
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02002840 mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841 mbedtls_ssl_get_peer_cert( &ssl ) );
Manuel Pégourié-Gonnard67557172015-07-02 11:15:48 +02002842 mbedtls_printf( "%s\n", crt_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002843 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002845
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02002846 if( opt.exchanges == 0 )
2847 goto close_notify;
2848
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00002849 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002850data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002851 /*
2852 * 6. Read the HTTP Request
2853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 mbedtls_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002855 fflush( stdout );
2856
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002857 /*
2858 * TLS and DTLS need different reading styles (stream vs datagram)
2859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002861 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002862 do
2863 {
2864 int terminated = 0;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002865 len = opt.buffer_size - 1;
2866 memset( buf, 0, opt.buffer_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002868
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02002869 if( mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01002870 {
2871 if( opt.event == 1 /* level triggered IO */ )
2872 {
2873#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002874 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002875#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002876 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002877#endif
2878 }
2879
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002880 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01002881 }
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002882
2883 if( ret <= 0 )
2884 {
2885 switch( ret )
2886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2888 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002889 goto close_notify;
2890
2891 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 case MBEDTLS_ERR_NET_CONN_RESET:
2893 mbedtls_printf( " connection was reset by peer\n" );
2894 ret = MBEDTLS_ERR_NET_CONN_RESET;
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002895 goto reset;
2896
2897 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002899 goto reset;
2900 }
2901 }
2902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002904 {
2905 len = ret;
2906 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002908
2909 /* End of message should be detected according to the syntax of the
2910 * application protocol (eg HTTP), just use a dummy test here. */
2911 if( buf[len - 1] == '\n' )
2912 terminated = 1;
2913 }
2914 else
2915 {
2916 int extra_len, ori_len;
2917 unsigned char *larger_buf;
2918
2919 ori_len = ret;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002920 extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002921
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002922 larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002923 if( larger_buf == NULL )
2924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 mbedtls_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002926 ret = 1;
2927 goto reset;
2928 }
2929
2930 memset( larger_buf, 0, ori_len + extra_len );
2931 memcpy( larger_buf, buf, ori_len );
2932
2933 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002935 if( ret != extra_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002939 ret = 1;
2940 goto reset;
2941 }
2942
2943 larger_buf[ori_len + extra_len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944 mbedtls_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002945 ori_len + extra_len, ori_len, extra_len,
2946 (char *) larger_buf );
2947
2948 /* End of message should be detected according to the syntax of the
2949 * application protocol (eg HTTP), just use a dummy test here. */
2950 if( larger_buf[ori_len + extra_len - 1] == '\n' )
2951 terminated = 1;
2952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 mbedtls_free( larger_buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002954 }
2955
2956 if( terminated )
2957 {
2958 ret = 0;
2959 break;
2960 }
2961 }
2962 while( 1 );
2963 }
2964 else /* Not stream, so datagram */
2965 {
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002966 len = opt.buffer_size - 1;
2967 memset( buf, 0, opt.buffer_size );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002968
Gilles Peskineb44692f2018-04-24 12:18:19 +02002969 do
Hanno Becker16970d22017-10-10 15:56:37 +01002970 {
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00002971 /* Without the call to `mbedtls_ssl_check_pending`, it might
2972 * happen that the client sends application data in the same
2973 * datagram as the Finished message concluding the handshake.
2974 * In this case, the application data would be ready to be
2975 * processed while the underlying transport wouldn't signal
2976 * any further incoming data.
2977 *
2978 * See the test 'Event-driven I/O: session-id resume, UDP packing'
2979 * in tests/ssl-opt.sh.
2980 */
2981
2982 /* For event-driven IO, wait for socket to become available */
2983 if( mbedtls_ssl_check_pending( &ssl ) == 0 &&
2984 opt.event == 1 /* level triggered IO */ )
2985 {
2986#if defined(MBEDTLS_TIMING_C)
2987 idle( &client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ );
2988#else
2989 idle( &client_fd, MBEDTLS_ERR_SSL_WANT_READ );
2990#endif
2991 }
2992
Hanno Becker16970d22017-10-10 15:56:37 +01002993 ret = mbedtls_ssl_read( &ssl, buf, len );
2994
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00002995 /* Note that even if `mbedtls_ssl_check_pending` returns true,
2996 * it can happen that the subsequent call to `mbedtls_ssl_read`
2997 * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
2998 * might be discarded (e.g. because they are retransmissions). */
Hanno Becker16970d22017-10-10 15:56:37 +01002999 }
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003000 while( mbedtls_status_is_ssl_in_progress( ret ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003001
3002 if( ret <= 0 )
3003 {
3004 switch( ret )
3005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
3007 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003008 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003009 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003010
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003011 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003013 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003014 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003015 }
3016
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003017 len = ret;
3018 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02003020 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003021 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003022
3023 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003024 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02003025 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003026 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003027#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00003028 if( opt.renegotiate && exchanges_left == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030 mbedtls_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003031 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003033 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003034 {
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003035 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003038 goto reset;
3039 }
Hanno Becker16970d22017-10-10 15:56:37 +01003040
3041 /* For event-driven IO, wait for socket to become available */
3042 if( opt.event == 1 /* level triggered IO */ )
3043 {
3044#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003045 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003046#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003047 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003048#endif
3049 }
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003050 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003053 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003055
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003056 /*
3057 * 7. Write the 200 Response
3058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059 mbedtls_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003060 fflush( stdout );
3061
3062 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003064
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003065 /* Add padding to the response to reach opt.response_size in length */
3066 if( opt.response_size != DFL_RESPONSE_SIZE &&
3067 len < opt.response_size )
3068 {
3069 memset( buf + len, 'B', opt.response_size - len );
3070 len += opt.response_size - len;
3071 }
3072
3073 /* Truncate if response size is smaller than the "natural" size */
3074 if( opt.response_size != DFL_RESPONSE_SIZE &&
3075 len > opt.response_size )
3076 {
3077 len = opt.response_size;
3078
3079 /* Still end with \r\n unless that's really not possible */
3080 if( len >= 2 ) buf[len - 2] = '\r';
3081 if( len >= 1 ) buf[len - 1] = '\n';
3082 }
3083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003085 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003086 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003089 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093 mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003094 goto reset;
3095 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003096
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003097 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003100 goto reset;
3101 }
Hanno Becker16970d22017-10-10 15:56:37 +01003102
3103 /* For event-driven IO, wait for socket to become available */
3104 if( opt.event == 1 /* level triggered IO */ )
3105 {
3106#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003107 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003108#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003109 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003110#endif
3111 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003112 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003113 }
3114 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003115 else /* Not stream, so datagram */
3116 {
Hanno Becker16970d22017-10-10 15:56:37 +01003117 while( 1 )
3118 {
3119 ret = mbedtls_ssl_write( &ssl, buf, len );
3120
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003121 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003122 break;
3123
3124 /* For event-driven IO, wait for socket to become available */
3125 if( opt.event == 1 /* level triggered IO */ )
3126 {
3127#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003128 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003129#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003130 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003131#endif
3132 }
3133 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003134
3135 if( ret < 0 )
3136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003138 goto reset;
3139 }
3140
3141 frags = 1;
3142 written = ret;
3143 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003144
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003145 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 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 +01003147 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01003148
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003149 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003150 * 7b. Continue doing data exchanges?
3151 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003152 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003153 goto data_exchange;
3154
3155 /*
3156 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003157 */
3158close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01003160
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003161 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003163 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003164 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166 mbedtls_printf( " done\n" );
Rich Evansf90016a2015-01-19 14:26:37 +00003167
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003168 goto reset;
3169
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003170 /*
3171 * Cleanup and exit
3172 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003173exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003175 if( ret != 0 )
3176 {
3177 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 mbedtls_strerror( ret, error_buf, 100 );
3179 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003180 }
3181#endif
3182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183 mbedtls_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003184 fflush( stdout );
3185
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003186 mbedtls_net_free( &client_fd );
3187 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +02003188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3190 mbedtls_dhm_free( &dhm );
Paul Bakkera317a982014-06-18 16:44:11 +02003191#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192#if defined(MBEDTLS_X509_CRT_PARSE_C)
3193 mbedtls_x509_crt_free( &cacert );
3194 mbedtls_x509_crt_free( &srvcert );
3195 mbedtls_pk_free( &pkey );
3196 mbedtls_x509_crt_free( &srvcert2 );
3197 mbedtls_pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02003198#endif
Gilles Peskine44817442018-06-13 18:09:28 +02003199#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3200 for( i = 0; (size_t) i < ssl_async_keys.slots_used; i++ )
3201 {
3202 if( ssl_async_keys.slots[i].pk_owned )
3203 {
3204 mbedtls_pk_free( ssl_async_keys.slots[i].pk );
3205 mbedtls_free( ssl_async_keys.slots[i].pk );
3206 ssl_async_keys.slots[i].pk = NULL;
3207 }
3208 }
3209#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02003210#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003211 sni_free( sni_info );
3212#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003214 psk_free( psk_info );
3215#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3217 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003218#endif
Paul Bakkered27a042013-04-18 22:46:23 +02003219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003221 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222 mbedtls_ctr_drbg_free( &ctr_drbg );
3223 mbedtls_entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225#if defined(MBEDTLS_SSL_CACHE_C)
3226 mbedtls_ssl_cache_free( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00003227#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003228#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3229 mbedtls_ssl_ticket_free( &ticket_ctx );
3230#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231#if defined(MBEDTLS_SSL_COOKIE_C)
3232 mbedtls_ssl_cookie_free( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02003233#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003234
Hanno Becker095d9cf2018-10-09 12:39:13 +01003235 mbedtls_free( buf );
3236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
3238#if defined(MBEDTLS_MEMORY_DEBUG)
3239 mbedtls_memory_buffer_alloc_status();
Paul Bakker82024bf2013-07-04 11:52:32 +02003240#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +02003242#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02003243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244 mbedtls_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003245
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003246#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003248 fflush( stdout ); getchar();
3249#endif
3250
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02003251 // Shell can not handle large exit numbers -> 1 for errors
3252 if( ret < 0 )
3253 ret = 1;
3254
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +02003255 mbedtls_exit( ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003256}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
3258 MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003259 MBEDTLS_CTR_DRBG_C */