blob: 7aa977ef8351e6efd91eca90d06fe9c56a0be27b [file] [log] [blame]
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001/*
2 * SSL client with options
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb60b95f2012-09-25 09:05:17 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb60b95f2012-09-25 09:05:17 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020031#include <stdio.h>
Simon Butcherd3138c32016-04-27 01:26:50 +010032#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_free free
Simon Butcherd3138c32016-04-27 01:26:50 +010034#define mbedtls_time time
35#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020036#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define mbedtls_fprintf fprintf
38#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010039#define mbedtls_exit exit
40#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
41#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evans18b78c72015-02-11 14:06:19 +000042#endif
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020043
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020044#if !defined(MBEDTLS_ENTROPY_C) || \
45 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020046 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020047int main( void )
48{
49 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
50 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020051 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020052 return( 0 );
53}
54#else
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010055
Andres AG788aa4a2016-09-14 14:32:09 +010056#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/ssl.h"
58#include "mbedtls/entropy.h"
59#include "mbedtls/ctr_drbg.h"
60#include "mbedtls/certs.h"
61#include "mbedtls/x509.h"
62#include "mbedtls/error.h"
63#include "mbedtls/debug.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020064#include "mbedtls/timing.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000065
Rich Evans18b78c72015-02-11 14:06:19 +000066#include <stdio.h>
67#include <stdlib.h>
68#include <string.h>
Andres AG692ad842017-01-19 16:30:57 +000069#include <stdint.h>
Andres AG0b736db2017-02-08 14:05:57 +000070
71#if !defined(_MSC_VER)
Andres AG692ad842017-01-19 16:30:57 +000072#include <inttypes.h>
Andres AG0b736db2017-02-08 14:05:57 +000073#endif
Rich Evans18b78c72015-02-11 14:06:19 +000074
75#if !defined(_WIN32)
76#include <signal.h>
77#endif
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000080#include "mbedtls/ssl_cache.h"
Paul Bakker0a597072012-09-25 21:55:46 +000081#endif
82
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020083#if defined(MBEDTLS_SSL_TICKET_C)
84#include "mbedtls/ssl_ticket.h"
85#endif
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000088#include "mbedtls/ssl_cookie.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020089#endif
90
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000092#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker82024bf2013-07-04 11:52:32 +020093#endif
94
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020095#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO)
96#define SNI_OPTION
97#endif
98
99#if defined(_WIN32)
100#include <windows.h>
101#endif
102
Simon Butchercce68be2018-07-23 14:26:09 +0100103/* Size of memory to be allocated for the heap, when using the library's memory
104 * management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */
105#define MEMORY_HEAP_SIZE 120000
106
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100107#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200108#define DFL_SERVER_PORT "4433"
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200109#define DFL_RESPONSE_SIZE -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000110#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100111#define DFL_NBIO 0
Hanno Becker16970d22017-10-10 15:56:37 +0100112#define DFL_EVENT 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200113#define DFL_READ_TIMEOUT 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000114#define DFL_CA_FILE ""
115#define DFL_CA_PATH ""
116#define DFL_CRT_FILE ""
117#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200118#define DFL_CRT_FILE2 ""
119#define DFL_KEY_FILE2 ""
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100120#define DFL_ASYNC_OPERATIONS "-"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100121#define DFL_ASYNC_PRIVATE_DELAY1 ( -1 )
122#define DFL_ASYNC_PRIVATE_DELAY2 ( -1 )
Gilles Peskine3665f1d2018-01-05 21:22:12 +0100123#define DFL_ASYNC_PRIVATE_ERROR ( 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +0200124#define DFL_PSK ""
125#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200126#define DFL_ECJPAKE_PW NULL
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200127#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000128#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200129#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100131#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100132#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200133#define DFL_RENEGO_DELAY -2
Andres AG692ad842017-01-19 16:30:57 +0000134#define DFL_RENEGO_PERIOD ( (uint64_t)-1 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200135#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200136#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200137#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000138#define DFL_ARC4 -1
Gilles Peskinebc70a182017-05-09 15:59:24 +0200139#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100140#define DFL_AUTH_MODE -1
Janos Follath4817e272017-04-10 13:44:33 +0100141#define DFL_CERT_REQ_CA_LIST MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100143#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200145#define DFL_TICKET_TIMEOUT 86400
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100146#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100147#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100148#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200149#define DFL_ALPN_STRING NULL
Hanno Beckere6706e62017-05-15 16:05:15 +0100150#define DFL_CURVES NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200151#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200153#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200154#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200155#define DFL_HS_TO_MIN 0
156#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200157#define DFL_DTLS_MTU -1
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200158#define DFL_BADMAC_LIMIT -1
Hanno Beckere7675d02018-08-14 13:28:56 +0100159#define DFL_DGRAM_PACKING 1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200160#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100161#define DFL_ETM -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000162
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200163#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
164 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
165 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
166 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
167 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
168 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
169 "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 +0200170
Paul Bakker8e714d72013-07-18 11:05:13 +0200171/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
172 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000173#define HTTP_RESPONSE \
174 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000175 "<h2>mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200176 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000177
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200178/*
179 * Size of the basic I/O buffer. Able to hold our default response.
180 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 * 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 +0200182 * if you change this value to something outside the range <= 100 or > 500
183 */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200184#define DFL_IO_BUF_LEN 200
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#if defined(MBEDTLS_X509_CRT_PARSE_C)
187#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000188#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100189 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
190 " default: \"\" (pre-loaded)\n" \
Hanno Becker1ce1a512019-05-01 11:16:28 +0100191 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100192 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
193 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
Hanno Becker1ce1a512019-05-01 11:16:28 +0100194 " use \"none\" to skip loading any top-level CAs.\n" \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100195 " 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 +0200196 " default: see note after key_file2\n" \
197 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200198 " 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 +0200199 " default: see note after key_file2\n" \
200 " key_file2=%%s default: see note below\n" \
201 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200202 " preloaded certificate(s) and key(s) are used if available\n" \
203 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
204 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000205#else
206#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200207 "\n" \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 " No file operations available (MBEDTLS_FS_IO not defined)\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200209 "\n"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200211#else
212#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200214
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200215#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100216#define USAGE_SSL_ASYNC \
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100217 " async_operations=%%c... d=decrypt, s=sign (default: -=off)\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100218 " async_private_delay1=%%d Asynchronous delay for key_file or preloaded key\n" \
Gilles Peskine166ce742018-04-30 10:30:49 +0200219 " async_private_delay2=%%d Asynchronous delay for key_file2 and sni\n" \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100220 " default: -1 (not asynchronous)\n" \
Gilles Peskine60ee4ca2018-01-08 11:28:05 +0100221 " async_private_error=%%d Async callback error injection (default=0=none,\n" \
Gilles Peskinec9125722018-04-26 07:15:40 +0200222 " 1=start, 2=cancel, 3=resume, negative=first time only)"
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100223#else
224#define USAGE_SSL_ASYNC ""
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200225#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd0d01c52018-10-25 16:49:48 +0100228#define USAGE_PSK \
229 " psk=%%s default: \"\" (in hex, without 0x)\n" \
230 " psk_list=%%s default: \"\"\n" \
Hanno Becker5ddc0632018-10-26 10:04:13 +0100231 " A list of (PSK identity, PSK value) pairs.\n" \
232 " The PSK values are in hex, without 0x.\n" \
Hanno Beckerd0d01c52018-10-25 16:49:48 +0100233 " id1,psk1[,id2,psk2[,...]]\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200234 " psk_identity=%%s default: \"Client_identity\"\n"
235#else
236#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200240#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100241 " tickets=%%d default: 1 (enabled)\n" \
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200242 " ticket_timeout=%%d default: 86400 (one day)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200243#else
244#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100248#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100249 " cache_max=%%d default: cache default (50)\n" \
250 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100251#else
252#define USAGE_CACHE ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#endif /* MBEDTLS_SSL_CACHE_C */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100254
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200255#if defined(SNI_OPTION)
Ron Eldor24eec792019-04-04 15:02:01 +0300256#if defined(MBEDTLS_X509_CRL_PARSE_C)
257#define SNI_CRL ",crl"
258#else
259#define SNI_CRL ""
260#endif
261
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100262#define USAGE_SNI \
Ron Eldor24eec792019-04-04 15:02:01 +0300263 " sni=%%s name1,cert1,key1,ca1"SNI_CRL",auth1[,...]\n" \
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200264 " default: disabled\n"
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100265#else
266#define USAGE_SNI ""
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200267#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200270#define USAGE_MAX_FRAG_LEN \
271 " max_frag_len=%%d default: 16384 (tls default)\n" \
272 " options: 512, 1024, 2048, 4096\n"
273#else
274#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100278#define USAGE_TRUNC_HMAC \
279 " trunc_hmac=%%d default: library default\n"
280#else
281#define USAGE_TRUNC_HMAC ""
282#endif
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200285#define USAGE_ALPN \
286 " alpn=%%s default: \"\" (disabled)\n" \
287 " example: spdy/1,http/1.1\n"
288#else
289#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200293#define USAGE_COOKIES \
294 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200295 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200296#else
297#define USAGE_COOKIES ""
298#endif
299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200301#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200302 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200303#else
304#define USAGE_ANTI_REPLAY ""
305#endif
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200308#define USAGE_BADMAC_LIMIT \
309 " badmac_limit=%%d default: (library default: disabled)\n"
310#else
311#define USAGE_BADMAC_LIMIT ""
312#endif
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200315#define USAGE_DTLS \
316 " dtls=%%d default: 0 (TLS)\n" \
317 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200318 " range of DTLS handshake timeouts in millisecs\n" \
Hanno Beckere7675d02018-08-14 13:28:56 +0100319 " mtu=%%d default: (library default: unlimited)\n" \
320 " dgram_packing=%%d default: 1 (allowed)\n" \
321 " allow or forbid packing of multiple\n" \
322 " records within a single datgram.\n"
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200323#else
324#define USAGE_DTLS ""
325#endif
326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200328#define USAGE_EMS \
329 " extended_ms=0/1 default: (library default: on)\n"
330#else
331#define USAGE_EMS ""
332#endif
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100335#define USAGE_ETM \
336 " etm=0/1 default: (library default: on)\n"
337#else
338#define USAGE_ETM ""
339#endif
340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100342#define USAGE_RENEGO \
343 " renegotiation=%%d default: 0 (disabled)\n" \
344 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100345 " renego_delay=%%d default: -2 (library default)\n" \
Andres AG692ad842017-01-19 16:30:57 +0000346 " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100347#else
348#define USAGE_RENEGO ""
349#endif
350
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200351#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
352#define USAGE_ECJPAKE \
353 " ecjpake_pw=%%s default: none (disabled)\n"
354#else
355#define USAGE_ECJPAKE ""
356#endif
357
Hanno Beckere6706e62017-05-15 16:05:15 +0100358#if defined(MBEDTLS_ECP_C)
359#define USAGE_CURVES \
360 " curves=a,b,c,d default: \"default\" (library default)\n" \
361 " example: \"secp521r1,brainpoolP512r1\"\n" \
362 " - use \"none\" for empty list\n" \
363 " - see mbedtls_ecp_curve_list()\n" \
364 " for acceptable curve names\n"
365#else
366#define USAGE_CURVES ""
367#endif
368
Gilles Peskined135bbd2020-04-14 19:41:01 +0200369/* USAGE is arbitrarily split to stay under the portable string literal
370 * length limit: 4095 bytes in C99. */
371#define USAGE1 \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000372 "\n usage: ssl_server2 param=<>...\n" \
373 "\n acceptable parameters:\n" \
Ron Eldor71f68c42017-09-26 11:29:11 +0300374 " server_addr=%%s default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000375 " server_port=%%d default: 4433\n" \
376 " debug_level=%%d default: 0 (disabled)\n" \
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200377 " buffer_size=%%d default: 200 \n" \
378 " (minimum: 1, max: 16385)\n" \
379 " response_size=%%d default: about 152 (basic response)\n" \
380 " (minimum: 0, max: 16384)\n" \
381 " increases buffer_size if bigger\n"\
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100382 " nbio=%%d default: 0 (blocking I/O)\n" \
383 " options: 1 (non-blocking), 2 (added delays)\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100384 " event=%%d default: 0 (loop)\n" \
385 " options: 1 (level-triggered, implies nbio=1),\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200386 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100387 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200388 USAGE_DTLS \
389 USAGE_COOKIES \
390 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200391 USAGE_BADMAC_LIMIT \
Gilles Peskined135bbd2020-04-14 19:41:01 +0200392 "\n"
393#define USAGE2 \
Manuel Pégourié-Gonnardee6139c2015-05-07 10:18:26 +0100394 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100395 " options: none, optional, required\n" \
Janos Follath4817e272017-04-10 13:44:33 +0100396 " cert_req_ca_list=%%d default: 1 (send ca list)\n" \
397 " options: 1 (send ca list), 0 (don't send)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000398 USAGE_IO \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100399 USAGE_SSL_ASYNC \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100400 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100401 "\n" \
402 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200403 USAGE_ECJPAKE \
Gilles Peskined135bbd2020-04-14 19:41:01 +0200404 "\n"
405#define USAGE3 \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100406 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100407 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200408 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200409 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100410 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100411 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100412 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100413 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200414 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200415 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100416 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100417 USAGE_CURVES \
Gilles Peskined135bbd2020-04-14 19:41:01 +0200418 "\n"
419#define USAGE4 \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100420 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200421 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200422 " min_version=%%s default: (library default: tls1)\n" \
423 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200424 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100425 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200426 "\n" \
427 " version_suites=a,b,c,d per-version ciphersuites\n" \
428 " in order from ssl3 to tls1_2\n" \
429 " default: all enabled\n" \
430 " force_ciphersuite=<name> default: all enabled\n" \
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100431 " query_config=<name> return 0 if the specified\n" \
432 " configuration macro is defined and 1\n" \
433 " otherwise. The expansion of the macro\n" \
434 " is printed if it is defined\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000435 " acceptable ciphersuite names:\n"
436
Hanno Becker8651a432017-06-09 16:13:22 +0100437#define ALPN_LIST_SIZE 10
438#define CURVE_LIST_SIZE 20
439
Andres AG692ad842017-01-19 16:30:57 +0000440#define PUT_UINT64_BE(out_be,in_le,i) \
441{ \
442 (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \
443 (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \
444 (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \
445 (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \
446 (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \
447 (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \
448 (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \
449 (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \
450}
451
Simon Butcher63cb97e2018-12-06 17:43:31 +0000452
Rich Evans85b05ec2015-02-12 11:37:29 +0000453/*
454 * global options
455 */
456struct options
457{
458 const char *server_addr; /* address on which the ssl service runs */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200459 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000460 int debug_level; /* level of debugging */
461 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100462 int event; /* loop or event-driven IO? level or edge triggered? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200464 int response_size; /* pad response with header to requested size */
465 uint16_t buffer_size; /* IO buffer size */
Rich Evans85b05ec2015-02-12 11:37:29 +0000466 const char *ca_file; /* the file with the CA certificate(s) */
467 const char *ca_path; /* the path with the CA certificate(s) reside */
468 const char *crt_file; /* the file with the server certificate */
469 const char *key_file; /* the file with the server key */
470 const char *crt_file2; /* the file with the 2nd server certificate */
471 const char *key_file2; /* the file with the 2nd server key */
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100472 const char *async_operations; /* supported SSL asynchronous operations */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100473 int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
474 int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
475 int async_private_error; /* inject error in async private callback */
Rich Evans85b05ec2015-02-12 11:37:29 +0000476 const char *psk; /* the pre-shared key */
477 const char *psk_identity; /* the pre-shared key identity */
478 char *psk_list; /* list of PSK id/key pairs for callback */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200479 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000480 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
481 const char *version_suites; /* per-version ciphersuites */
482 int renegotiation; /* enable / disable renegotiation */
483 int allow_legacy; /* allow legacy renegotiation */
484 int renegotiate; /* attempt renegotiation? */
485 int renego_delay; /* delay before enforcing renegotiation */
Andres AG692ad842017-01-19 16:30:57 +0000486 uint64_t renego_period; /* period for automatic renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000487 int exchanges; /* number of data exchanges */
488 int min_version; /* minimum protocol version accepted */
489 int max_version; /* maximum protocol version accepted */
490 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200491 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000492 int auth_mode; /* verify mode for connection */
Janos Follath4817e272017-04-10 13:44:33 +0100493 int cert_req_ca_list; /* should we send the CA list? */
Rich Evans85b05ec2015-02-12 11:37:29 +0000494 unsigned char mfl_code; /* code for maximum fragment length */
495 int trunc_hmac; /* accept truncated hmac? */
496 int tickets; /* enable / disable session tickets */
497 int ticket_timeout; /* session ticket lifetime */
498 int cache_max; /* max number of session cache entries */
499 int cache_timeout; /* expiration delay of session cache entries */
500 char *sni; /* string describing sni information */
Hanno Beckere6706e62017-05-15 16:05:15 +0100501 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000502 const char *alpn_string; /* ALPN supported protocols */
503 const char *dhm_file; /* the file with the DH parameters */
504 int extended_ms; /* allow negotiation of extended MS? */
505 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000506 int transport; /* TLS or DTLS? */
507 int cookies; /* Use cookies for DTLS? -1 to break them */
508 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
509 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
510 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200511 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Hanno Beckere7675d02018-08-14 13:28:56 +0100512 int dgram_packing; /* allow/forbid datagram packing */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000513 int badmac_limit; /* Limit of records with bad MAC */
Rich Evans85b05ec2015-02-12 11:37:29 +0000514} opt;
515
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100516int query_config( const char *config );
517
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200518static void my_debug( void *ctx, int level,
519 const char *file, int line,
520 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000521{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200522 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000523
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200524 /* Extract basename from file */
525 for( p = basename = file; *p != '\0'; p++ )
526 if( *p == '/' || *p == '\\' )
527 basename = p + 1;
528
529 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000530 fflush( (FILE *) ctx );
531}
532
533/*
534 * Test recv/send functions that make sure each try returns
535 * WANT_READ/WANT_WRITE at least once before sucesseding
536 */
537static int my_recv( void *ctx, unsigned char *buf, size_t len )
538{
539 static int first_try = 1;
540 int ret;
541
542 if( first_try )
543 {
544 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100545 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000546 }
547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100549 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000550 first_try = 1; /* Next call will be a new operation */
551 return( ret );
552}
553
554static int my_send( void *ctx, const unsigned char *buf, size_t len )
555{
556 static int first_try = 1;
557 int ret;
558
559 if( first_try )
560 {
561 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100562 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000563 }
564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100566 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000567 first_try = 1; /* Next call will be a new operation */
568 return( ret );
569}
570
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200571/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200572 * Return authmode from string, or -1 on error
573 */
574static int get_auth_mode( const char *s )
575{
576 if( strcmp( s, "none" ) == 0 )
577 return( MBEDTLS_SSL_VERIFY_NONE );
578 if( strcmp( s, "optional" ) == 0 )
579 return( MBEDTLS_SSL_VERIFY_OPTIONAL );
580 if( strcmp( s, "required" ) == 0 )
581 return( MBEDTLS_SSL_VERIFY_REQUIRED );
582
583 return( -1 );
584}
585
586/*
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200587 * Used by sni_parse and psk_parse to handle coma-separated lists
588 */
589#define GET_ITEM( dst ) \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100590 do \
591 { \
592 (dst) = p; \
593 while( *p != ',' ) \
594 if( ++p > end ) \
595 goto error; \
596 *p++ = '\0'; \
597 } while( 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200598
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200599#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100600typedef struct _sni_entry sni_entry;
601
602struct _sni_entry {
603 const char *name;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 mbedtls_x509_crt *cert;
605 mbedtls_pk_context *key;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200606 mbedtls_x509_crt* ca;
607 mbedtls_x509_crl* crl;
608 int authmode;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100609 sni_entry *next;
610};
611
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100612void sni_free( sni_entry *head )
613{
614 sni_entry *cur = head, *next;
615
616 while( cur != NULL )
617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 mbedtls_x509_crt_free( cur->cert );
619 mbedtls_free( cur->cert );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_pk_free( cur->key );
622 mbedtls_free( cur->key );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100623
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200624 mbedtls_x509_crt_free( cur->ca );
625 mbedtls_free( cur->ca );
Ron Eldor24eec792019-04-04 15:02:01 +0300626#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200627 mbedtls_x509_crl_free( cur->crl );
628 mbedtls_free( cur->crl );
Ron Eldor24eec792019-04-04 15:02:01 +0300629#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100630 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_free( cur );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100632 cur = next;
633 }
634}
635
636/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200637 * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
638 * into a usable sni_entry list. For ca1, crl1, auth1, the special value
639 * '-' means unset. If ca1 is unset, then crl1 is ignored too.
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000640 *
641 * Modifies the input string! This is not production quality!
642 */
643sni_entry *sni_parse( char *sni_string )
644{
645 sni_entry *cur = NULL, *new = NULL;
646 char *p = sni_string;
647 char *end = p;
Ron Eldor24eec792019-04-04 15:02:01 +0300648 char *crt_file, *key_file, *ca_file, *auth_str;
649#if defined(MBEDTLS_X509_CRL_PARSE_C)
650 char *crl_file;
651#endif
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000652
653 while( *end != '\0' )
654 ++end;
655 *end = ',';
656
657 while( p <= end )
658 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200659 if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000660 {
661 sni_free( cur );
662 return( NULL );
663 }
664
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200665 GET_ITEM( new->name );
666 GET_ITEM( crt_file );
667 GET_ITEM( key_file );
668 GET_ITEM( ca_file );
Ron Eldor24eec792019-04-04 15:02:01 +0300669#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200670 GET_ITEM( crl_file );
Ron Eldor24eec792019-04-04 15:02:01 +0300671#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200672 GET_ITEM( auth_str );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000673
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200674 if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
675 ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200676 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_x509_crt_init( new->cert );
679 mbedtls_pk_init( new->key );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
682 mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000683 goto error;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200684
685 if( strcmp( ca_file, "-" ) != 0 )
686 {
687 if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
688 goto error;
689
690 mbedtls_x509_crt_init( new->ca );
691
692 if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
693 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000694 }
695
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 if( strcmp( crl_file, "-" ) != 0 )
698 {
699 if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
700 goto error;
701
702 mbedtls_x509_crl_init( new->crl );
703
704 if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
705 goto error;
706 }
Ron Eldor24eec792019-04-04 15:02:01 +0300707#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200708
709 if( strcmp( auth_str, "-" ) != 0 )
710 {
711 if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
712 goto error;
713 }
714 else
715 new->authmode = DFL_AUTH_MODE;
716
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000717 new->next = cur;
718 cur = new;
719 }
720
721 return( cur );
722
723error:
724 sni_free( new );
725 sni_free( cur );
726 return( NULL );
727}
728
729/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100730 * SNI callback.
731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100733 const unsigned char *name, size_t name_len )
734{
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200735 const sni_entry *cur = (const sni_entry *) p_info;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100736
737 while( cur != NULL )
738 {
739 if( name_len == strlen( cur->name ) &&
740 memcmp( name, cur->name, name_len ) == 0 )
741 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200742 if( cur->ca != NULL )
743 mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
744
745 if( cur->authmode != DFL_AUTH_MODE )
746 mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
747
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200748 return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100749 }
750
751 cur = cur->next;
752 }
753
754 return( -1 );
755}
756
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200757#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200760
Hanno Beckerd6028a12018-10-15 12:01:35 +0100761#define HEX2NUM( c ) \
762 do \
763 { \
764 if( (c) >= '0' && (c) <= '9' ) \
765 (c) -= '0'; \
766 else if( (c) >= 'a' && (c) <= 'f' ) \
767 (c) -= 'a' - 10; \
768 else if( (c) >= 'A' && (c) <= 'F' ) \
769 (c) -= 'A' - 10; \
770 else \
771 return( -1 ); \
772 } while( 0 )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200773
774/*
775 * Convert a hex string to bytes.
776 * Return 0 on success, -1 on error.
777 */
778int unhexify( unsigned char *output, const char *input, size_t *olen )
779{
780 unsigned char c;
781 size_t j;
782
783 *olen = strlen( input );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 if( *olen % 2 != 0 || *olen / 2 > MBEDTLS_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200785 return( -1 );
786 *olen /= 2;
787
788 for( j = 0; j < *olen * 2; j += 2 )
789 {
790 c = input[j];
791 HEX2NUM( c );
792 output[ j / 2 ] = c << 4;
793
794 c = input[j + 1];
795 HEX2NUM( c );
796 output[ j / 2 ] |= c;
797 }
798
799 return( 0 );
800}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200801
802typedef struct _psk_entry psk_entry;
803
804struct _psk_entry
805{
806 const char *name;
807 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 unsigned char key[MBEDTLS_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200809 psk_entry *next;
810};
811
812/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000813 * Free a list of psk_entry's
814 */
815void psk_free( psk_entry *head )
816{
817 psk_entry *next;
818
819 while( head != NULL )
820 {
821 next = head->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 mbedtls_free( head );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000823 head = next;
824 }
825}
826
827/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200828 * Parse a string of pairs name1,key1[,name2,key2[,...]]
829 * into a usable psk_entry list.
830 *
831 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200832 */
833psk_entry *psk_parse( char *psk_string )
834{
835 psk_entry *cur = NULL, *new = NULL;
836 char *p = psk_string;
837 char *end = p;
838 char *key_hex;
839
840 while( *end != '\0' )
841 ++end;
842 *end = ',';
843
844 while( p <= end )
845 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200846 if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +0000847 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200848
849 memset( new, 0, sizeof( psk_entry ) );
850
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200851 GET_ITEM( new->name );
852 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200853
854 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000855 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200856
857 new->next = cur;
858 cur = new;
859 }
860
861 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200862
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000863error:
864 psk_free( new );
865 psk_free( cur );
866 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200867}
868
869/*
870 * PSK callback
871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200873 const unsigned char *name, size_t name_len )
874{
875 psk_entry *cur = (psk_entry *) p_info;
876
877 while( cur != NULL )
878 {
879 if( name_len == strlen( cur->name ) &&
880 memcmp( name, cur->name, name_len ) == 0 )
881 {
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +0100882 return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200883 }
884
885 cur = cur->next;
886 }
887
888 return( -1 );
889}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200891
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200892static mbedtls_net_context listen_fd, client_fd;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200893
894/* Interruption handler to ensure clean exit (for valgrind testing) */
895#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200896static int received_sigterm = 0;
897void term_handler( int sig )
898{
899 ((void) sig);
900 received_sigterm = 1;
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200901 mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
902 mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200903}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200904#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200905
Gilles Peskine682df092017-05-11 19:01:11 +0200906#if defined(MBEDTLS_X509_CRT_PARSE_C)
907static int ssl_sig_hashes_for_test[] = {
908#if defined(MBEDTLS_SHA512_C)
909 MBEDTLS_MD_SHA512,
910 MBEDTLS_MD_SHA384,
911#endif
912#if defined(MBEDTLS_SHA256_C)
913 MBEDTLS_MD_SHA256,
914 MBEDTLS_MD_SHA224,
915#endif
916#if defined(MBEDTLS_SHA1_C)
917 /* Allow SHA-1 as we use it extensively in tests. */
918 MBEDTLS_MD_SHA1,
919#endif
920 MBEDTLS_MD_NONE
921};
922#endif /* MBEDTLS_X509_CRT_PARSE_C */
923
Gilles Peskinea36ac4f2018-04-26 08:05:02 +0200924/** Return true if \p ret is a status code indicating that there is an
925 * operation in progress on an SSL connection, and false if it indicates
926 * success or a fatal error.
927 *
928 * The possible operations in progress are:
929 *
930 * - A read, when the SSL input buffer does not contain a full message.
931 * - A write, when the SSL output buffer contains some data that has not
932 * been sent over the network yet.
933 * - An asynchronous callback that has not completed yet. */
934static int mbedtls_status_is_ssl_in_progress( int ret )
935{
936 return( ret == MBEDTLS_ERR_SSL_WANT_READ ||
937 ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
938 ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
939}
940
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200941#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100942typedef struct
943{
Gilles Peskine44817442018-06-13 18:09:28 +0200944 mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
945 mbedtls_pk_context *pk; /*!< Private key */
946 unsigned delay; /*!< Number of resume steps to go through */
947 unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100948} ssl_async_key_slot_t;
949
950typedef enum {
Gilles Peskinead28bf02018-04-26 00:19:16 +0200951 SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
952 SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
953 SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start */
954 SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
Gilles Peskinec9125722018-04-26 07:15:40 +0200955#define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100956} ssl_async_inject_error_t;
957
958typedef struct
959{
Gilles Peskinee2479892018-06-13 18:06:51 +0200960 ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100961 size_t slots_used;
962 ssl_async_inject_error_t inject_error;
963 int (*f_rng)(void *, unsigned char *, size_t);
964 void *p_rng;
965} ssl_async_key_context_t;
966
Gilles Peskined6fbfde2018-04-30 10:23:56 +0200967int ssl_async_set_key( ssl_async_key_context_t *ctx,
Gilles Peskine44817442018-06-13 18:09:28 +0200968 mbedtls_x509_crt *cert,
969 mbedtls_pk_context *pk,
970 int pk_take_ownership,
971 unsigned delay )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100972{
Gilles Peskined6fbfde2018-04-30 10:23:56 +0200973 if( ctx->slots_used >= sizeof( ctx->slots ) / sizeof( *ctx->slots ) )
974 return( -1 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100975 ctx->slots[ctx->slots_used].cert = cert;
976 ctx->slots[ctx->slots_used].pk = pk;
977 ctx->slots[ctx->slots_used].delay = delay;
Gilles Peskine44817442018-06-13 18:09:28 +0200978 ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100979 ++ctx->slots_used;
Gilles Peskined6fbfde2018-04-30 10:23:56 +0200980 return( 0 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100981}
982
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100983#define SSL_ASYNC_INPUT_MAX_SIZE 512
Gilles Peskined3268832018-04-26 06:23:59 +0200984
985typedef enum
986{
987 ASYNC_OP_SIGN,
988 ASYNC_OP_DECRYPT,
989} ssl_async_operation_type_t;
990/* Note that the enum above and the array below need to be kept in sync!
991 * `ssl_async_operation_names[op]` is the name of op for each value `op`
992 * of type `ssl_async_operation_type_t`. */
993static const char *const ssl_async_operation_names[] =
994{
995 "sign",
996 "decrypt",
997};
998
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100999typedef struct
1000{
Gilles Peskine6331d782018-04-26 13:27:43 +02001001 unsigned slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001002 ssl_async_operation_type_t operation_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001003 mbedtls_md_type_t md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001004 unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
1005 size_t input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001006 unsigned remaining_delay;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001007} ssl_async_operation_context_t;
1008
Gilles Peskine8f97af72018-04-26 11:46:10 +02001009static int ssl_async_start( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001010 mbedtls_x509_crt *cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001011 ssl_async_operation_type_t op_type,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001012 mbedtls_md_type_t md_alg,
1013 const unsigned char *input,
1014 size_t input_len )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001015{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001016 ssl_async_key_context_t *config_data =
1017 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskine6331d782018-04-26 13:27:43 +02001018 unsigned slot;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001019 ssl_async_operation_context_t *ctx = NULL;
Gilles Peskined3268832018-04-26 06:23:59 +02001020 const char *op_name = ssl_async_operation_names[op_type];
Gilles Peskinef1127252018-04-24 13:05:39 +02001021
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001022 {
1023 char dn[100];
Gilles Peskined5d983e2018-06-15 14:05:10 +02001024 if( mbedtls_x509_dn_gets( dn, sizeof( dn ), &cert->subject ) > 0 )
1025 mbedtls_printf( "Async %s callback: looking for DN=%s\n",
1026 op_name, dn );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001027 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001028
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001029 /* Look for a private key that matches the public key in cert.
1030 * Since this test code has the private key inside Mbed TLS,
1031 * we call mbedtls_pk_check_pair to match a private key with the
1032 * public key. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001033 for( slot = 0; slot < config_data->slots_used; slot++ )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001034 {
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001035 if( mbedtls_pk_check_pair( &cert->pk,
1036 config_data->slots[slot].pk ) == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001037 break;
1038 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001039 if( slot == config_data->slots_used )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001040 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001041 mbedtls_printf( "Async %s callback: no key matches this certificate.\n",
1042 op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001043 return( MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH );
1044 }
Gilles Peskine6331d782018-04-26 13:27:43 +02001045 mbedtls_printf( "Async %s callback: using key slot %u, delay=%u.\n",
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001046 op_name, slot, config_data->slots[slot].delay );
Gilles Peskinef1127252018-04-24 13:05:39 +02001047
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001048 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001049 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001050 mbedtls_printf( "Async %s callback: injected error\n", op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001051 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1052 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001053
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001054 if( input_len > SSL_ASYNC_INPUT_MAX_SIZE )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001055 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Gilles Peskinef1127252018-04-24 13:05:39 +02001056
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001057 ctx = mbedtls_calloc( 1, sizeof( *ctx ) );
1058 if( ctx == NULL )
1059 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1060 ctx->slot = slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001061 ctx->operation_type = op_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001062 ctx->md_alg = md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001063 memcpy( ctx->input, input, input_len );
1064 ctx->input_len = input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001065 ctx->remaining_delay = config_data->slots[slot].delay;
Gilles Peskinea668c602018-04-30 11:54:39 +02001066 mbedtls_ssl_set_async_operation_data( ssl, ctx );
Gilles Peskinef1127252018-04-24 13:05:39 +02001067
Gilles Peskineceb541b2018-04-26 06:30:45 +02001068 if( ctx->remaining_delay == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001069 return( 0 );
1070 else
1071 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1072}
1073
Gilles Peskine8f97af72018-04-26 11:46:10 +02001074static int ssl_async_sign( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001075 mbedtls_x509_crt *cert,
1076 mbedtls_md_type_t md_alg,
1077 const unsigned char *hash,
1078 size_t hash_len )
1079{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001080 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001081 ASYNC_OP_SIGN, md_alg,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001082 hash, hash_len ) );
1083}
1084
Gilles Peskine8f97af72018-04-26 11:46:10 +02001085static int ssl_async_decrypt( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001086 mbedtls_x509_crt *cert,
1087 const unsigned char *input,
1088 size_t input_len )
1089{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001090 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001091 ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001092 input, input_len ) );
1093}
1094
Gilles Peskine8f97af72018-04-26 11:46:10 +02001095static int ssl_async_resume( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001096 unsigned char *output,
1097 size_t *output_len,
1098 size_t output_size )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001099{
Gilles Peskinea668c602018-04-30 11:54:39 +02001100 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine8f97af72018-04-26 11:46:10 +02001101 ssl_async_key_context_t *config_data =
1102 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001103 ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001104 int ret;
Gilles Peskinef5a99962018-04-30 16:37:23 +02001105 const char *op_name;
Gilles Peskinef1127252018-04-24 13:05:39 +02001106
Gilles Peskineceb541b2018-04-26 06:30:45 +02001107 if( ctx->remaining_delay > 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001108 {
Gilles Peskineceb541b2018-04-26 06:30:45 +02001109 --ctx->remaining_delay;
Gilles Peskine6331d782018-04-26 13:27:43 +02001110 mbedtls_printf( "Async resume (slot %u): call %u more times.\n",
Gilles Peskineceb541b2018-04-26 06:30:45 +02001111 ctx->slot, ctx->remaining_delay );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001112 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1113 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001114
Gilles Peskined3268832018-04-26 06:23:59 +02001115 switch( ctx->operation_type )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001116 {
Gilles Peskined3268832018-04-26 06:23:59 +02001117 case ASYNC_OP_DECRYPT:
Gilles Peskined3268832018-04-26 06:23:59 +02001118 ret = mbedtls_pk_decrypt( key_slot->pk,
1119 ctx->input, ctx->input_len,
1120 output, output_len, output_size,
1121 config_data->f_rng, config_data->p_rng );
1122 break;
1123 case ASYNC_OP_SIGN:
Gilles Peskined3268832018-04-26 06:23:59 +02001124 ret = mbedtls_pk_sign( key_slot->pk,
1125 ctx->md_alg,
1126 ctx->input, ctx->input_len,
1127 output, output_len,
1128 config_data->f_rng, config_data->p_rng );
1129 break;
1130 default:
Gilles Peskine6331d782018-04-26 13:27:43 +02001131 mbedtls_printf( "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
Gilles Peskined3268832018-04-26 06:23:59 +02001132 ctx->slot, (long) ctx->operation_type );
Gilles Peskine44817442018-06-13 18:09:28 +02001133 mbedtls_free( ctx );
Gilles Peskined3268832018-04-26 06:23:59 +02001134 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1135 break;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001136 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001137
Gilles Peskinef5a99962018-04-30 16:37:23 +02001138 op_name = ssl_async_operation_names[ctx->operation_type];
1139
Gilles Peskinec9125722018-04-26 07:15:40 +02001140 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001141 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001142 mbedtls_printf( "Async resume callback: %s done but injected error\n",
1143 op_name );
Gilles Peskine2636fad2018-06-12 14:17:39 +02001144 mbedtls_free( ctx );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001145 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1146 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001147
Gilles Peskine6331d782018-04-26 13:27:43 +02001148 mbedtls_printf( "Async resume (slot %u): %s done, status=%d.\n",
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001149 ctx->slot, op_name, ret );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001150 mbedtls_free( ctx );
1151 return( ret );
1152}
1153
Gilles Peskine8f97af72018-04-26 11:46:10 +02001154static void ssl_async_cancel( mbedtls_ssl_context *ssl )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001155{
Gilles Peskinea668c602018-04-30 11:54:39 +02001156 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001157 mbedtls_printf( "Async cancel callback.\n" );
1158 mbedtls_free( ctx );
1159}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001160#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001161
Hanno Becker16970d22017-10-10 15:56:37 +01001162/*
1163 * Wait for an event from the underlying transport or the timer
1164 * (Used in event-driven IO mode).
1165 */
1166#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001167int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001168 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001169#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001170int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001171 mbedtls_timing_delay_context *timer,
1172 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001173#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001174{
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001175 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +01001176 int poll_type = 0;
1177
1178 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
1179 poll_type = MBEDTLS_NET_POLL_WRITE;
1180 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
1181 poll_type = MBEDTLS_NET_POLL_READ;
1182#if !defined(MBEDTLS_TIMING_C)
1183 else
Hanno Beckeref527962018-03-15 15:49:24 +00001184 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001185#endif
1186
1187 while( 1 )
1188 {
Hanno Becker197a91c2017-10-31 10:58:53 +00001189 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +01001190#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001191 if( timer != NULL &&
1192 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +01001193 {
Hanno Becker16970d22017-10-10 15:56:37 +01001194 break;
1195 }
Hanno Becker197a91c2017-10-31 10:58:53 +00001196#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +01001197
Hanno Becker197a91c2017-10-31 10:58:53 +00001198 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001199 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +01001200 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001201 ret = mbedtls_net_poll( fd, poll_type, 0 );
1202 if( ret < 0 )
1203 return( ret );
1204 if( ret == poll_type )
1205 break;
Hanno Becker16970d22017-10-10 15:56:37 +01001206 }
1207 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001208
1209 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001210}
1211
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001212int main( int argc, char *argv[] )
1213{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001214 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001215 int version_suites[4][2];
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001216 unsigned char* buf = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1218 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +02001219 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +02001220 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +02001221#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001222 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001223 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02001224 size_t cliip_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225#if defined(MBEDTLS_SSL_COOKIE_C)
1226 mbedtls_ssl_cookie_ctx cookie_ctx;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001227#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001228
Gilles Peskineef86ab22017-05-05 18:59:02 +02001229#if defined(MBEDTLS_X509_CRT_PARSE_C)
1230 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
1231#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 mbedtls_entropy_context entropy;
1233 mbedtls_ctr_drbg_context ctr_drbg;
1234 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001235 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001236#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001237 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001238#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001240 unsigned char renego_period[8] = { 0 };
1241#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +02001243 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 mbedtls_x509_crt cacert;
1245 mbedtls_x509_crt srvcert;
1246 mbedtls_pk_context pkey;
1247 mbedtls_x509_crt srvcert2;
1248 mbedtls_pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001249 int key_cert_init = 0, key_cert_init2 = 0;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001250#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001251 ssl_async_key_context_t ssl_async_keys;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001252#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001253#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1255 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001256#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257#if defined(MBEDTLS_SSL_CACHE_C)
1258 mbedtls_ssl_cache_context cache;
Paul Bakker0a597072012-09-25 21:55:46 +00001259#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001260#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1261 mbedtls_ssl_ticket_context ticket_ctx;
1262#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001263#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001264 sni_entry *sni_info = NULL;
1265#endif
Hanno Beckere6706e62017-05-15 16:05:15 +01001266#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +01001267 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +01001268 const mbedtls_ecp_curve_info * curve_cur;
1269#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +01001271 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001272#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Simon Butchercce68be2018-07-23 14:26:09 +01001274 unsigned char alloc_buf[MEMORY_HEAP_SIZE];
Paul Bakker82024bf2013-07-04 11:52:32 +02001275#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001276
1277 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001278 char *p, *q;
1279 const int *list;
1280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1282 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker82024bf2013-07-04 11:52:32 +02001283#endif
1284
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001285 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +02001286 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001287 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001288 mbedtls_net_init( &client_fd );
1289 mbedtls_net_init( &listen_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001290 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001291 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001292 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293#if defined(MBEDTLS_X509_CRT_PARSE_C)
1294 mbedtls_x509_crt_init( &cacert );
1295 mbedtls_x509_crt_init( &srvcert );
1296 mbedtls_pk_init( &pkey );
1297 mbedtls_x509_crt_init( &srvcert2 );
1298 mbedtls_pk_init( &pkey2 );
Gilles Peskine4d9ec4d2018-04-26 14:33:43 +02001299#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
1300 memset( &ssl_async_keys, 0, sizeof( ssl_async_keys ) );
1301#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001302#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1304 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001305#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#if defined(MBEDTLS_SSL_CACHE_C)
1307 mbedtls_ssl_cache_init( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00001308#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001309#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1310 mbedtls_ssl_ticket_init( &ticket_ctx );
1311#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +02001313 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001314#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315#if defined(MBEDTLS_SSL_COOKIE_C)
1316 mbedtls_ssl_cookie_init( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001317#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001318
Paul Bakkerc1283d32014-08-18 11:05:51 +02001319#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001320 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001321 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001322 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +02001323#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001324
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001325 if( argc == 0 )
1326 {
1327 usage:
1328 if( ret == 0 )
1329 ret = 1;
1330
Gilles Peskined135bbd2020-04-14 19:41:01 +02001331 mbedtls_printf( USAGE1 );
1332 mbedtls_printf( USAGE2 );
1333 mbedtls_printf( USAGE3 );
1334 mbedtls_printf( USAGE4 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 list = mbedtls_ssl_list_ciphersuites();
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001337 while( *list )
1338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001340 list++;
1341 if( !*list )
1342 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001344 list++;
1345 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 mbedtls_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001347 goto exit;
1348 }
1349
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001350 opt.buffer_size = DFL_IO_BUF_LEN;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001351 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001352 opt.server_port = DFL_SERVER_PORT;
1353 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker16970d22017-10-10 15:56:37 +01001354 opt.event = DFL_EVENT;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001355 opt.response_size = DFL_RESPONSE_SIZE;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001356 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001357 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001358 opt.ca_file = DFL_CA_FILE;
1359 opt.ca_path = DFL_CA_PATH;
1360 opt.crt_file = DFL_CRT_FILE;
1361 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001362 opt.crt_file2 = DFL_CRT_FILE2;
1363 opt.key_file2 = DFL_KEY_FILE2;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001364 opt.async_operations = DFL_ASYNC_OPERATIONS;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001365 opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
1366 opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
1367 opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
Paul Bakkerfbb17802013-04-17 19:10:21 +02001368 opt.psk = DFL_PSK;
1369 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001370 opt.psk_list = DFL_PSK_LIST;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001371 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001372 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001373 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001374 opt.renegotiation = DFL_RENEGOTIATION;
1375 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001376 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001377 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001378 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001379 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001380 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001381 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001382 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001383 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001384 opt.auth_mode = DFL_AUTH_MODE;
Janos Follath4817e272017-04-10 13:44:33 +01001385 opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001386 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001387 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001388 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001389 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001390 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001391 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001392 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001393 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001394 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001395 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001396 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001397 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001398 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001399 opt.hs_to_min = DFL_HS_TO_MIN;
1400 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001401 opt.dtls_mtu = DFL_DTLS_MTU;
Hanno Beckere7675d02018-08-14 13:28:56 +01001402 opt.dgram_packing = DFL_DGRAM_PACKING;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001403 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001404 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001405 opt.etm = DFL_ETM;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001406
1407 for( i = 1; i < argc; i++ )
1408 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001409 p = argv[i];
1410 if( ( q = strchr( p, '=' ) ) == NULL )
1411 goto usage;
1412 *q++ = '\0';
1413
1414 if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001415 opt.server_port = q;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001416 else if( strcmp( p, "server_addr" ) == 0 )
1417 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001418 else if( strcmp( p, "dtls" ) == 0 )
1419 {
1420 int t = atoi( q );
1421 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001423 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001425 else
1426 goto usage;
1427 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001428 else if( strcmp( p, "debug_level" ) == 0 )
1429 {
1430 opt.debug_level = atoi( q );
1431 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1432 goto usage;
1433 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001434 else if( strcmp( p, "nbio" ) == 0 )
1435 {
1436 opt.nbio = atoi( q );
1437 if( opt.nbio < 0 || opt.nbio > 2 )
1438 goto usage;
1439 }
Hanno Becker16970d22017-10-10 15:56:37 +01001440 else if( strcmp( p, "event" ) == 0 )
1441 {
1442 opt.event = atoi( q );
1443 if( opt.event < 0 || opt.event > 2 )
1444 goto usage;
1445 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001446 else if( strcmp( p, "read_timeout" ) == 0 )
1447 opt.read_timeout = atoi( q );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001448 else if( strcmp( p, "buffer_size" ) == 0 )
1449 {
1450 opt.buffer_size = atoi( q );
1451 if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
1452 goto usage;
1453 }
1454 else if( strcmp( p, "response_size" ) == 0 )
1455 {
1456 opt.response_size = atoi( q );
1457 if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
1458 goto usage;
1459 if( opt.buffer_size < opt.response_size )
1460 opt.buffer_size = opt.response_size;
1461 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001462 else if( strcmp( p, "ca_file" ) == 0 )
1463 opt.ca_file = q;
1464 else if( strcmp( p, "ca_path" ) == 0 )
1465 opt.ca_path = q;
1466 else if( strcmp( p, "crt_file" ) == 0 )
1467 opt.crt_file = q;
1468 else if( strcmp( p, "key_file" ) == 0 )
1469 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001470 else if( strcmp( p, "crt_file2" ) == 0 )
1471 opt.crt_file2 = q;
1472 else if( strcmp( p, "key_file2" ) == 0 )
1473 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001474 else if( strcmp( p, "dhm_file" ) == 0 )
1475 opt.dhm_file = q;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001476#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001477 else if( strcmp( p, "async_operations" ) == 0 )
1478 opt.async_operations = q;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001479 else if( strcmp( p, "async_private_delay1" ) == 0 )
1480 opt.async_private_delay1 = atoi( q );
1481 else if( strcmp( p, "async_private_delay2" ) == 0 )
1482 opt.async_private_delay2 = atoi( q );
1483 else if( strcmp( p, "async_private_error" ) == 0 )
1484 {
1485 int n = atoi( q );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01001486 if( n < -SSL_ASYNC_INJECT_ERROR_MAX ||
1487 n > SSL_ASYNC_INJECT_ERROR_MAX )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001488 {
1489 ret = 2;
1490 goto usage;
1491 }
1492 opt.async_private_error = n;
1493 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001494#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001495 else if( strcmp( p, "psk" ) == 0 )
1496 opt.psk = q;
1497 else if( strcmp( p, "psk_identity" ) == 0 )
1498 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001499 else if( strcmp( p, "psk_list" ) == 0 )
1500 opt.psk_list = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001501 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1502 opt.ecjpake_pw = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001503 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001506
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001507 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001508 {
1509 ret = 2;
1510 goto usage;
1511 }
1512 opt.force_ciphersuite[1] = 0;
1513 }
Hanno Beckere6706e62017-05-15 16:05:15 +01001514 else if( strcmp( p, "curves" ) == 0 )
1515 opt.curves = q;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001516 else if( strcmp( p, "version_suites" ) == 0 )
1517 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001518 else if( strcmp( p, "renegotiation" ) == 0 )
1519 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001520 opt.renegotiation = (atoi( q )) ?
1521 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1522 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001523 }
1524 else if( strcmp( p, "allow_legacy" ) == 0 )
1525 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001526 switch( atoi( q ) )
1527 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001528 case -1:
1529 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1530 break;
1531 case 0:
1532 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1533 break;
1534 case 1:
1535 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1536 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001537 default: goto usage;
1538 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001539 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001540 else if( strcmp( p, "renegotiate" ) == 0 )
1541 {
1542 opt.renegotiate = atoi( q );
1543 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1544 goto usage;
1545 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001546 else if( strcmp( p, "renego_delay" ) == 0 )
1547 {
1548 opt.renego_delay = atoi( q );
1549 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001550 else if( strcmp( p, "renego_period" ) == 0 )
1551 {
Andres AG0b736db2017-02-08 14:05:57 +00001552#if defined(_MSC_VER)
1553 opt.renego_period = _strtoui64( q, NULL, 10 );
1554#else
1555 if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
1556 goto usage;
1557#endif /* _MSC_VER */
1558 if( opt.renego_period < 2 )
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001559 goto usage;
1560 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001561 else if( strcmp( p, "exchanges" ) == 0 )
1562 {
1563 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001564 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001565 goto usage;
1566 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001567 else if( strcmp( p, "min_version" ) == 0 )
1568 {
1569 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001571 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001573 else if( strcmp( q, "tls1_1" ) == 0 ||
1574 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001576 else if( strcmp( q, "tls1_2" ) == 0 ||
1577 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001579 else
1580 goto usage;
1581 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001582 else if( strcmp( p, "max_version" ) == 0 )
1583 {
1584 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001586 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001588 else if( strcmp( q, "tls1_1" ) == 0 ||
1589 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001591 else if( strcmp( q, "tls1_2" ) == 0 ||
1592 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001594 else
1595 goto usage;
1596 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001597 else if( strcmp( p, "arc4" ) == 0 )
1598 {
1599 switch( atoi( q ) )
1600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1602 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001603 default: goto usage;
1604 }
1605 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02001606 else if( strcmp( p, "allow_sha1" ) == 0 )
1607 {
1608 switch( atoi( q ) )
1609 {
1610 case 0: opt.allow_sha1 = 0; break;
1611 case 1: opt.allow_sha1 = 1; break;
1612 default: goto usage;
1613 }
1614 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001615 else if( strcmp( p, "force_version" ) == 0 )
1616 {
1617 if( strcmp( q, "ssl3" ) == 0 )
1618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1620 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001621 }
1622 else if( strcmp( q, "tls1" ) == 0 )
1623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1625 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001626 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001627 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1630 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001631 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001632 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1635 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001636 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001637 else if( strcmp( q, "dtls1" ) == 0 )
1638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1640 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1641 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001642 }
1643 else if( strcmp( q, "dtls1_2" ) == 0 )
1644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1646 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1647 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001648 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001649 else
1650 goto usage;
1651 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001652 else if( strcmp( p, "auth_mode" ) == 0 )
1653 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001654 if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
Paul Bakker91ebfb52012-11-23 14:04:08 +01001655 goto usage;
1656 }
Janos Follath4817e272017-04-10 13:44:33 +01001657 else if( strcmp( p, "cert_req_ca_list" ) == 0 )
1658 {
1659 opt.cert_req_ca_list = atoi( q );
1660 if( opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1 )
1661 goto usage;
1662 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001663 else if( strcmp( p, "max_frag_len" ) == 0 )
1664 {
1665 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001667 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001669 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001671 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001673 else
1674 goto usage;
1675 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001676 else if( strcmp( p, "alpn" ) == 0 )
1677 {
1678 opt.alpn_string = q;
1679 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001680 else if( strcmp( p, "trunc_hmac" ) == 0 )
1681 {
1682 switch( atoi( q ) )
1683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1685 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001686 default: goto usage;
1687 }
1688 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001689 else if( strcmp( p, "extended_ms" ) == 0 )
1690 {
1691 switch( atoi( q ) )
1692 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001693 case 0:
1694 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1695 break;
1696 case 1:
1697 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1698 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001699 default: goto usage;
1700 }
1701 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001702 else if( strcmp( p, "etm" ) == 0 )
1703 {
1704 switch( atoi( q ) )
1705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1707 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001708 default: goto usage;
1709 }
1710 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001711 else if( strcmp( p, "tickets" ) == 0 )
1712 {
1713 opt.tickets = atoi( q );
1714 if( opt.tickets < 0 || opt.tickets > 1 )
1715 goto usage;
1716 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001717 else if( strcmp( p, "ticket_timeout" ) == 0 )
1718 {
1719 opt.ticket_timeout = atoi( q );
1720 if( opt.ticket_timeout < 0 )
1721 goto usage;
1722 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001723 else if( strcmp( p, "cache_max" ) == 0 )
1724 {
1725 opt.cache_max = atoi( q );
1726 if( opt.cache_max < 0 )
1727 goto usage;
1728 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001729 else if( strcmp( p, "cache_timeout" ) == 0 )
1730 {
1731 opt.cache_timeout = atoi( q );
1732 if( opt.cache_timeout < 0 )
1733 goto usage;
1734 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001735 else if( strcmp( p, "cookies" ) == 0 )
1736 {
1737 opt.cookies = atoi( q );
1738 if( opt.cookies < -1 || opt.cookies > 1)
1739 goto usage;
1740 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001741 else if( strcmp( p, "anti_replay" ) == 0 )
1742 {
1743 opt.anti_replay = atoi( q );
1744 if( opt.anti_replay < 0 || opt.anti_replay > 1)
1745 goto usage;
1746 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001747 else if( strcmp( p, "badmac_limit" ) == 0 )
1748 {
1749 opt.badmac_limit = atoi( q );
1750 if( opt.badmac_limit < 0 )
1751 goto usage;
1752 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001753 else if( strcmp( p, "hs_timeout" ) == 0 )
1754 {
1755 if( ( p = strchr( q, '-' ) ) == NULL )
1756 goto usage;
1757 *p++ = '\0';
1758 opt.hs_to_min = atoi( q );
1759 opt.hs_to_max = atoi( p );
1760 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1761 goto usage;
1762 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001763 else if( strcmp( p, "mtu" ) == 0 )
1764 {
1765 opt.dtls_mtu = atoi( q );
1766 if( opt.dtls_mtu < 0 )
1767 goto usage;
1768 }
Hanno Beckere7675d02018-08-14 13:28:56 +01001769 else if( strcmp( p, "dgram_packing" ) == 0 )
1770 {
1771 opt.dgram_packing = atoi( q );
1772 if( opt.dgram_packing != 0 &&
1773 opt.dgram_packing != 1 )
1774 {
1775 goto usage;
1776 }
1777 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001778 else if( strcmp( p, "sni" ) == 0 )
1779 {
1780 opt.sni = q;
1781 }
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +01001782 else if( strcmp( p, "query_config" ) == 0 )
1783 {
1784 return query_config( q );
1785 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001786 else
1787 goto usage;
1788 }
1789
Hanno Becker16970d22017-10-10 15:56:37 +01001790 /* Event-driven IO is incompatible with the above custom
1791 * receive and send functions, as the polling builds on
1792 * refers to the underlying net_context. */
1793 if( opt.event == 1 && opt.nbio != 1 )
1794 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001795 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001796 opt.nbio = 1;
1797 }
1798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799#if defined(MBEDTLS_DEBUG_C)
1800 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001801#endif
Andrzej Kurekda4029d2018-06-20 07:07:55 -04001802 buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001803 if( buf == NULL )
1804 {
Andrzej Kurek755890f2018-06-20 08:17:04 -04001805 mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001806 ret = 3;
1807 goto exit;
1808 }
Paul Bakkerc73079a2014-04-25 16:34:30 +02001809
Paul Bakkerc1516be2013-06-29 16:01:32 +02001810 if( opt.force_ciphersuite[0] > 0 )
1811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001813 ciphersuite_info =
1814 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001815
Paul Bakker5b55b792013-07-19 13:43:43 +02001816 if( opt.max_version != -1 &&
1817 ciphersuite_info->min_minor_ver > opt.max_version )
1818 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001819 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker5b55b792013-07-19 13:43:43 +02001820 ret = 2;
1821 goto usage;
1822 }
1823 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001824 ciphersuite_info->max_minor_ver < opt.min_version )
1825 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001826 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001827 ret = 2;
1828 goto usage;
1829 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001830
1831 /* If we select a version that's not supported by
1832 * this suite, then there will be no common ciphersuite... */
1833 if( opt.max_version == -1 ||
1834 opt.max_version > ciphersuite_info->max_minor_ver )
1835 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001836 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001837 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001838 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001839 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001840 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001841 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1843 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1844 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001845 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001846
1847 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001853 ret = 2;
1854 goto usage;
1855 }
1856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001858 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001859 }
1860
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001861 if( opt.version_suites != NULL )
1862 {
1863 const char *name[4] = { 0 };
1864
1865 /* Parse 4-element coma-separated list */
1866 for( i = 0, p = (char *) opt.version_suites;
1867 i < 4 && *p != '\0';
1868 i++ )
1869 {
1870 name[i] = p;
1871
1872 /* Terminate the current string and move on to next one */
1873 while( *p != ',' && *p != '\0' )
1874 p++;
1875 if( *p == ',' )
1876 *p++ = '\0';
1877 }
1878
1879 if( i != 4 )
1880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881 mbedtls_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001882 ret = 1;
1883 goto exit;
1884 }
1885
1886 memset( version_suites, 0, sizeof( version_suites ) );
1887
1888 /* Get the suites identifiers from their name */
1889 for( i = 0; i < 4; i++ )
1890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001892
1893 if( version_suites[i][0] == 0 )
1894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001896 ret = 2;
1897 goto usage;
1898 }
1899 }
1900 }
1901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001903 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001904 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001905 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001906 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 mbedtls_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001909 goto exit;
1910 }
1911
1912 if( opt.psk_list != NULL )
1913 {
1914 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 mbedtls_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001917 goto exit;
1918 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001919 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001921
Hanno Beckere6706e62017-05-15 16:05:15 +01001922#if defined(MBEDTLS_ECP_C)
1923 if( opt.curves != NULL )
1924 {
1925 p = (char *) opt.curves;
1926 i = 0;
1927
1928 if( strcmp( p, "none" ) == 0 )
1929 {
1930 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1931 }
1932 else if( strcmp( p, "default" ) != 0 )
1933 {
1934 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001935 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001936 {
1937 q = p;
1938
1939 /* Terminate the current string */
1940 while( *p != ',' && *p != '\0' )
1941 p++;
1942 if( *p == ',' )
1943 *p++ = '\0';
1944
1945 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1946 {
1947 curve_list[i++] = curve_cur->grp_id;
1948 }
1949 else
1950 {
1951 mbedtls_printf( "unknown curve %s\n", q );
1952 mbedtls_printf( "supported curves: " );
1953 for( curve_cur = mbedtls_ecp_curve_list();
1954 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1955 curve_cur++ )
1956 {
1957 mbedtls_printf( "%s ", curve_cur->name );
1958 }
1959 mbedtls_printf( "\n" );
1960 goto exit;
1961 }
1962 }
1963
1964 mbedtls_printf("Number of curves: %d\n", i );
1965
Hanno Becker8651a432017-06-09 16:13:22 +01001966 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001967 {
Hanno Becker8651a432017-06-09 16:13:22 +01001968 mbedtls_printf( "curves list too long, maximum %d",
1969 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001970 goto exit;
1971 }
1972
1973 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1974 }
1975 }
1976#endif /* MBEDTLS_ECP_C */
1977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001979 if( opt.alpn_string != NULL )
1980 {
1981 p = (char *) opt.alpn_string;
1982 i = 0;
1983
1984 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001985 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001986 {
1987 alpn_list[i++] = p;
1988
1989 /* Terminate the current string and move on to next one */
1990 while( *p != ',' && *p != '\0' )
1991 p++;
1992 if( *p == ',' )
1993 *p++ = '\0';
1994 }
1995 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001997
Paul Bakkerfbb17802013-04-17 19:10:21 +02001998 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001999 * 0. Initialize the RNG and the session data
2000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002002 fflush( stdout );
2003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 mbedtls_entropy_init( &entropy );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002005 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
2006 &entropy, (const unsigned char *) pers,
2007 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002008 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002009 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
2010 -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002011 goto exit;
2012 }
2013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002017 /*
2018 * 1.1. Load the trusted CA
2019 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002021 fflush( stdout );
2022
Hanno Becker7ae36e42019-03-05 16:22:07 +00002023 if( strcmp( opt.ca_path, "none" ) == 0 ||
2024 strcmp( opt.ca_file, "none" ) == 0 )
2025 {
2026 ret = 0;
2027 }
2028 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002030 if( strlen( opt.ca_path ) )
Hanno Becker7ae36e42019-03-05 16:22:07 +00002031 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002032 else if( strlen( opt.ca_file ) )
Hanno Becker7ae36e42019-03-05 16:22:07 +00002033 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02002034 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002035#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036#if defined(MBEDTLS_CERTS_C)
Hanno Becker38566cc2019-02-01 08:13:19 +00002037 {
2038#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 ret = mbedtls_x509_crt_parse( &cacert,
2042 (const unsigned char *) mbedtls_test_cas[i],
2043 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002044 if( ret != 0 )
2045 break;
2046 }
Hanno Becker38566cc2019-02-01 08:13:19 +00002047 if( ret == 0 )
2048#endif /* MBEDTLS_PEM_PARSE_C */
2049 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
2050 {
2051 ret = mbedtls_x509_crt_parse_der( &cacert,
2052 (const unsigned char *) mbedtls_test_cas_der[i],
2053 mbedtls_test_cas_der_len[i] );
2054 if( ret != 0 )
2055 break;
2056 }
2057 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002058#else
2059 {
2060 ret = 1;
Hanno Beckerc258c442018-12-05 16:49:42 +00002061 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002062 }
Hanno Becker38566cc2019-02-01 08:13:19 +00002063#endif /* MBEDTLS_CERTS_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002064 if( ret < 0 )
2065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002067 goto exit;
2068 }
2069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002071
2072 /*
2073 * 1.2. Load own certificate and private key
2074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002076 fflush( stdout );
2077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002079 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002080 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002081 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002085 -ret );
2086 goto exit;
2087 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002088 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002089 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002090 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002091 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002095 goto exit;
2096 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002097 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002098 if( key_cert_init == 1 )
2099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002101 goto exit;
2102 }
2103
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002104 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002105 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002106 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002110 -ret );
2111 goto exit;
2112 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002113 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002114 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002115 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002116 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002120 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002121 goto exit;
2122 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002123 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002124 if( key_cert_init2 == 1 )
2125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002127 goto exit;
2128 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002129#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002130 if( key_cert_init == 0 &&
2131 strcmp( opt.crt_file, "none" ) != 0 &&
2132 strcmp( opt.key_file, "none" ) != 0 &&
2133 key_cert_init2 == 0 &&
2134 strcmp( opt.crt_file2, "none" ) != 0 &&
2135 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137#if !defined(MBEDTLS_CERTS_C)
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002138 mbedtls_printf( "Not certificated or key provided, and \nMBEDTLS_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002139 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002140#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141#if defined(MBEDTLS_RSA_C)
2142 if( ( ret = mbedtls_x509_crt_parse( &srvcert,
2143 (const unsigned char *) mbedtls_test_srv_crt_rsa,
2144 mbedtls_test_srv_crt_rsa_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002145 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002146 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
2147 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002148 goto exit;
2149 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 if( ( ret = mbedtls_pk_parse_key( &pkey,
2151 (const unsigned char *) mbedtls_test_srv_key_rsa,
2152 mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002153 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002154 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
2155 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002156 goto exit;
2157 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002158 key_cert_init = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159#endif /* MBEDTLS_RSA_C */
2160#if defined(MBEDTLS_ECDSA_C)
2161 if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
2162 (const unsigned char *) mbedtls_test_srv_crt_ec,
2163 mbedtls_test_srv_crt_ec_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002164 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002165 mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n",
2166 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002167 goto exit;
2168 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 if( ( ret = mbedtls_pk_parse_key( &pkey2,
2170 (const unsigned char *) mbedtls_test_srv_key_ec,
2171 mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002172 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002173 mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n",
2174 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002175 goto exit;
2176 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002177 key_cert_init2 = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178#endif /* MBEDTLS_ECDSA_C */
2179#endif /* MBEDTLS_CERTS_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002180 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 mbedtls_printf( " ok\n" );
2183#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002186 if( opt.dhm_file != NULL )
2187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 mbedtls_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002189 fflush( stdout );
2190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002194 -ret );
2195 goto exit;
2196 }
2197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002199 }
2200#endif
2201
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002202#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002203 if( opt.sni != NULL )
2204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 mbedtls_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002206 fflush( stdout );
2207
2208 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
2209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002211 goto exit;
2212 }
2213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002215 }
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002216#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002217
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002218 /*
2219 * 2. Setup the listening TCP socket
2220 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02002221 mbedtls_printf( " . Bind on %s://%s:%s/ ...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002223 opt.server_addr ? opt.server_addr : "*",
2224 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002225 fflush( stdout );
2226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
2228 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2229 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002232 goto exit;
2233 }
2234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002236
2237 /*
2238 * 3. Setup stuff
2239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002241 fflush( stdout );
2242
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02002243 if( ( ret = mbedtls_ssl_config_defaults( &conf,
2244 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02002245 opt.transport,
2246 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002247 {
2248 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
2249 goto exit;
2250 }
2251
Gilles Peskineef86ab22017-05-05 18:59:02 +02002252#if defined(MBEDTLS_X509_CRT_PARSE_C)
2253 /* The default algorithms profile disables SHA-1, but our tests still
2254 rely on it heavily. Hence we allow it here. A real-world server
2255 should use the default profile unless there is a good reason not to. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02002256 if( opt.allow_sha1 > 0 )
2257 {
2258 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
2259 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
Gilles Peskine682df092017-05-11 19:01:11 +02002260 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
Gilles Peskinebc70a182017-05-09 15:59:24 +02002261 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02002262#endif /* MBEDTLS_X509_CRT_PARSE_C */
2263
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01002264 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002265 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002266
Janos Follath4817e272017-04-10 13:44:33 +01002267 if( opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST )
2268 mbedtls_ssl_conf_cert_req_ca_list( &conf, opt.cert_req_ca_list );
2269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002271 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 +02002272 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002273
Hanno Beckere7675d02018-08-14 13:28:56 +01002274 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01002275 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002279 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002280 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002281 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002282 goto exit;
2283 };
Paul Bakker05decb22013-08-15 13:33:48 +02002284#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002287 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002288 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002289#endif
2290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002292 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002293 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002294#endif
2295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002297 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002298 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002299#endif
2300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002302 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002303 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002304 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002305 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002306 goto exit;
2307 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002308#endif
2309
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002310 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
2311 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002314 if( opt.cache_max != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002316
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002317 if( opt.cache_timeout != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002319
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002320 mbedtls_ssl_conf_session_cache( &conf, &cache,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01002321 mbedtls_ssl_cache_get,
2322 mbedtls_ssl_cache_set );
Paul Bakker0a597072012-09-25 21:55:46 +00002323#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002326 if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002327 {
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002328 if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
2329 mbedtls_ctr_drbg_random, &ctr_drbg,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +02002330 MBEDTLS_CIPHER_AES_256_GCM,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002331 opt.ticket_timeout ) ) != 0 )
2332 {
2333 mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
2334 goto exit;
2335 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01002336
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002337 mbedtls_ssl_conf_session_tickets_cb( &conf,
2338 mbedtls_ssl_ticket_write,
2339 mbedtls_ssl_ticket_parse,
2340 &ticket_ctx );
2341 }
Paul Bakkera503a632013-08-14 13:48:06 +02002342#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344#if defined(MBEDTLS_SSL_PROTO_DTLS)
2345 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002348 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
2351 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002354 goto exit;
2355 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002356
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002357 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002358 &cookie_ctx );
2359 }
2360 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361#endif /* MBEDTLS_SSL_COOKIE_C */
2362#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002363 if( opt.cookies == 0 )
2364 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002365 mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002366 }
2367 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002369 {
2370 ; /* Nothing to do */
2371 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002374 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002375 mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002376#endif
2377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002379 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002380 mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002381#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002382 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002384
Paul Bakker41c83d32013-03-20 14:39:14 +01002385 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002386 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002387
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002388#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002389 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002390 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002391#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002392
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002393 if( opt.version_suites != NULL )
2394 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002395 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 MBEDTLS_SSL_MAJOR_VERSION_3,
2397 MBEDTLS_SSL_MINOR_VERSION_0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002398 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 MBEDTLS_SSL_MAJOR_VERSION_3,
2400 MBEDTLS_SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002401 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 MBEDTLS_SSL_MAJOR_VERSION_3,
2403 MBEDTLS_SSL_MINOR_VERSION_2 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002404 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 MBEDTLS_SSL_MAJOR_VERSION_3,
2406 MBEDTLS_SSL_MINOR_VERSION_3 );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002407 }
2408
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002409 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002410 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002412 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002413
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02002414 if( opt.renego_delay != DFL_RENEGO_DELAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002415 mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002416
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002417 if( opt.renego_period != DFL_RENEGO_PERIOD )
2418 {
Andres AG692ad842017-01-19 16:30:57 +00002419 PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002420 mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002421 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002422#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002425 if( strcmp( opt.ca_path, "none" ) != 0 &&
2426 strcmp( opt.ca_file, "none" ) != 0 )
2427 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002428 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002429 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002430 if( key_cert_init )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002431 {
2432 mbedtls_pk_context *pk = &pkey;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002433#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002434 if( opt.async_private_delay1 >= 0 )
2435 {
Gilles Peskine44817442018-06-13 18:09:28 +02002436 ret = ssl_async_set_key( &ssl_async_keys, &srvcert, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002437 opt.async_private_delay1 );
2438 if( ret < 0 )
2439 {
2440 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2441 ret );
2442 goto exit;
2443 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002444 pk = NULL;
2445 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002446#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002447 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002448 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002449 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002450 goto exit;
2451 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002452 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002453 if( key_cert_init2 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002454 {
2455 mbedtls_pk_context *pk = &pkey2;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002456#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002457 if( opt.async_private_delay2 >= 0 )
2458 {
Gilles Peskine44817442018-06-13 18:09:28 +02002459 ret = ssl_async_set_key( &ssl_async_keys, &srvcert2, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002460 opt.async_private_delay2 );
2461 if( ret < 0 )
2462 {
2463 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2464 ret );
2465 goto exit;
2466 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002467 pk = NULL;
2468 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002469#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002470 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002471 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002472 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002473 goto exit;
2474 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002475 }
2476
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002477#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002478 if( opt.async_operations[0] != '-' )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002479 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002480 mbedtls_ssl_async_sign_t *sign = NULL;
2481 mbedtls_ssl_async_decrypt_t *decrypt = NULL;
Gilles Peskine12ab5d42018-04-24 12:32:04 +02002482 const char *r;
2483 for( r = opt.async_operations; *r; r++ )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002484 {
Gilles Peskine12ab5d42018-04-24 12:32:04 +02002485 switch( *r )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002486 {
2487 case 'd':
2488 decrypt = ssl_async_decrypt;
2489 break;
2490 case 's':
2491 sign = ssl_async_sign;
2492 break;
2493 }
2494 }
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002495 ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
2496 - opt.async_private_error :
2497 opt.async_private_error );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002498 ssl_async_keys.f_rng = mbedtls_ctr_drbg_random;
2499 ssl_async_keys.p_rng = &ctr_drbg;
2500 mbedtls_ssl_conf_async_private_cb( &conf,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002501 sign,
2502 decrypt,
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002503 ssl_async_resume,
2504 ssl_async_cancel,
2505 &ssl_async_keys );
2506 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002507#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002508#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +02002509
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002510#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002511 if( opt.sni != NULL )
Gilles Peskine166ce742018-04-30 10:30:49 +02002512 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002513 mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
Gilles Peskine166ce742018-04-30 10:30:49 +02002514#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
2515 if( opt.async_private_delay2 >= 0 )
2516 {
Gilles Peskinee2479892018-06-13 18:06:51 +02002517 sni_entry *cur;
2518 for( cur = sni_info; cur != NULL; cur = cur->next )
Gilles Peskine166ce742018-04-30 10:30:49 +02002519 {
Gilles Peskinee2479892018-06-13 18:06:51 +02002520 ret = ssl_async_set_key( &ssl_async_keys,
Gilles Peskine44817442018-06-13 18:09:28 +02002521 cur->cert, cur->key, 1,
Gilles Peskinee2479892018-06-13 18:06:51 +02002522 opt.async_private_delay2 );
2523 if( ret < 0 )
2524 {
2525 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2526 ret );
2527 goto exit;
2528 }
2529 cur->key = NULL;
Gilles Peskine166ce742018-04-30 10:30:49 +02002530 }
Gilles Peskine166ce742018-04-30 10:30:49 +02002531 }
2532#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
2533 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002534#endif
2535
Hanno Beckere6706e62017-05-15 16:05:15 +01002536#if defined(MBEDTLS_ECP_C)
2537 if( opt.curves != NULL &&
2538 strcmp( opt.curves, "default" ) != 0 )
2539 {
2540 mbedtls_ssl_conf_curves( &conf, curve_list );
2541 }
2542#endif
2543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002545 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
2546 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002547 ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002548 (const unsigned char *) opt.psk_identity,
2549 strlen( opt.psk_identity ) );
2550 if( ret != 0 )
2551 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002552 mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002553 goto exit;
2554 }
2555 }
2556
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002557 if( opt.psk_list != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002558 mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02002559#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561#if defined(MBEDTLS_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00002562 /*
2563 * Use different group than default DHM group
2564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002566 if( opt.dhm_file != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002567 ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002568#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002569 if( ret != 0 )
2570 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002571 mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002572 goto exit;
2573 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002574#endif
2575
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002576 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002577 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00002578
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002579 if( opt.max_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002580 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002581
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002582 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
2583 {
2584 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
2585 goto exit;
2586 }
2587
2588 if( opt.nbio == 2 )
2589 mbedtls_ssl_set_bio( &ssl, &client_fd, my_send, my_recv, NULL );
2590 else
2591 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02002592 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002593
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002594#if defined(MBEDTLS_SSL_PROTO_DTLS)
2595 if( opt.dtls_mtu != DFL_DTLS_MTU )
2596 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
2597#endif
2598
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002599#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002600 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
2601 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002602#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002605
2606reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002607#if !defined(_WIN32)
2608 if( received_sigterm )
2609 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01002610 mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" );
2611 if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT )
2612 ret = 0;
2613
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002614 goto exit;
2615 }
2616#endif
2617
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002618 if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
2619 {
2620 mbedtls_printf( " ! Client initiated reconnection from same port\n" );
2621 goto handshake;
2622 }
2623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002625 if( ret != 0 )
2626 {
2627 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 mbedtls_strerror( ret, error_buf, 100 );
2629 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002630 }
2631#endif
2632
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002633 mbedtls_net_free( &client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 mbedtls_ssl_session_reset( &ssl );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002636
2637 /*
2638 * 3. Wait until a client connects
2639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 mbedtls_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002641 fflush( stdout );
2642
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002643 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02002644 client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002645 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02002646#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002647 if( received_sigterm )
2648 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01002649 mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" );
2650 if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED )
2651 ret = 0;
2652
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002653 goto exit;
2654 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02002655#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002658 goto exit;
2659 }
2660
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002661 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002662 ret = mbedtls_net_set_nonblock( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002663 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002664 ret = mbedtls_net_set_block( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002665 if( ret != 0 )
2666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002668 goto exit;
2669 }
2670
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002671 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2674 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002675 {
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02002676 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
2677 client_ip, cliip_len ) ) != 0 )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002678 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002679 mbedtls_printf( " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n",
2680 -ret );
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002681 goto exit;
2682 }
2683 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002685
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002686#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2687 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
2688 {
2689 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
2690 (const unsigned char *) opt.ecjpake_pw,
2691 strlen( opt.ecjpake_pw ) ) ) != 0 )
2692 {
2693 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
2694 goto exit;
2695 }
2696 }
2697#endif
2698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002700
2701 /*
2702 * 4. Handshake
2703 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002704handshake:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002706 fflush( stdout );
2707
Hanno Becker16970d22017-10-10 15:56:37 +01002708 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002709 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002710#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002711 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002712 ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002713 {
2714 mbedtls_printf( " cancelling on injected error\n" );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002715 break;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002716 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002717#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskineb44692f2018-04-24 12:18:19 +02002718
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02002719 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01002720 break;
2721
2722 /* For event-driven IO, wait for socket to become available */
2723 if( opt.event == 1 /* level triggered IO */ )
2724 {
2725#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002726 ret = idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002727#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002728 ret = idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002729#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002730 if( ret != 0 )
2731 goto reset;
Hanno Becker16970d22017-10-10 15:56:37 +01002732 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002733 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 mbedtls_printf( " hello verification requested\n" );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002738 ret = 0;
2739 goto reset;
2740 }
2741 else if( ret != 0 )
2742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002744
2745#if defined(MBEDTLS_X509_CRT_PARSE_C)
2746 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2747 {
2748 char vrfy_buf[512];
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02002749 flags = mbedtls_ssl_get_verify_result( &ssl );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002750
2751 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
2752
2753 mbedtls_printf( "%s\n", vrfy_buf );
2754 }
2755#endif
2756
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002757#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002758 if( opt.async_private_error < 0 )
2759 /* Injected error only the first time round, to test reset */
2760 ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
2761#endif
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002762 goto reset;
2763 }
2764 else /* ret == 0 */
2765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
2767 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002768 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
2771 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002772 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002774
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002775#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2776 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
2777 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
2778#endif
2779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002781 if( opt.alpn_string != NULL )
2782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
2784 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002785 alp ? alp : "(none)" );
2786 }
2787#endif
2788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002790 /*
Ron Eldor2a47be52017-06-20 15:23:23 +03002791 * 5. Verify the client certificate
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002794
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002795 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002796 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002797 char vrfy_buf[512];
2798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799 mbedtls_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002800
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002801 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002802
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002803 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002804 }
2805 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002807
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02002808 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002809 {
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02002810 char crt_buf[512];
2811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812 mbedtls_printf( " . Peer certificate information ...\n" );
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02002813 mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 mbedtls_ssl_get_peer_cert( &ssl ) );
Manuel Pégourié-Gonnard67557172015-07-02 11:15:48 +02002815 mbedtls_printf( "%s\n", crt_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002816 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002818
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02002819 if( opt.exchanges == 0 )
2820 goto close_notify;
2821
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00002822 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002823data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002824 /*
2825 * 6. Read the HTTP Request
2826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 mbedtls_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002828 fflush( stdout );
2829
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002830 /*
2831 * TLS and DTLS need different reading styles (stream vs datagram)
2832 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002834 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002835 do
2836 {
2837 int terminated = 0;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002838 len = opt.buffer_size - 1;
2839 memset( buf, 0, opt.buffer_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002841
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02002842 if( mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01002843 {
2844 if( opt.event == 1 /* level triggered IO */ )
2845 {
2846#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002847 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002848#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002849 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002850#endif
2851 }
2852
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002853 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01002854 }
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002855
2856 if( ret <= 0 )
2857 {
2858 switch( ret )
2859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2861 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002862 goto close_notify;
2863
2864 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865 case MBEDTLS_ERR_NET_CONN_RESET:
2866 mbedtls_printf( " connection was reset by peer\n" );
2867 ret = MBEDTLS_ERR_NET_CONN_RESET;
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002868 goto reset;
2869
2870 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002872 goto reset;
2873 }
2874 }
2875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002877 {
2878 len = ret;
2879 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880 mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002881
2882 /* End of message should be detected according to the syntax of the
2883 * application protocol (eg HTTP), just use a dummy test here. */
2884 if( buf[len - 1] == '\n' )
2885 terminated = 1;
2886 }
2887 else
2888 {
2889 int extra_len, ori_len;
2890 unsigned char *larger_buf;
2891
2892 ori_len = ret;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002893 extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002894
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002895 larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002896 if( larger_buf == NULL )
2897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 mbedtls_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002899 ret = 1;
2900 goto reset;
2901 }
2902
2903 memset( larger_buf, 0, ori_len + extra_len );
2904 memcpy( larger_buf, buf, ori_len );
2905
2906 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002908 if( ret != extra_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002912 ret = 1;
2913 goto reset;
2914 }
2915
2916 larger_buf[ori_len + extra_len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 mbedtls_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002918 ori_len + extra_len, ori_len, extra_len,
2919 (char *) larger_buf );
2920
2921 /* End of message should be detected according to the syntax of the
2922 * application protocol (eg HTTP), just use a dummy test here. */
2923 if( larger_buf[ori_len + extra_len - 1] == '\n' )
2924 terminated = 1;
2925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 mbedtls_free( larger_buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002927 }
2928
2929 if( terminated )
2930 {
2931 ret = 0;
2932 break;
2933 }
2934 }
2935 while( 1 );
2936 }
2937 else /* Not stream, so datagram */
2938 {
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002939 len = opt.buffer_size - 1;
2940 memset( buf, 0, opt.buffer_size );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002941
Gilles Peskineb44692f2018-04-24 12:18:19 +02002942 do
Hanno Becker16970d22017-10-10 15:56:37 +01002943 {
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00002944 /* Without the call to `mbedtls_ssl_check_pending`, it might
2945 * happen that the client sends application data in the same
2946 * datagram as the Finished message concluding the handshake.
2947 * In this case, the application data would be ready to be
2948 * processed while the underlying transport wouldn't signal
2949 * any further incoming data.
2950 *
2951 * See the test 'Event-driven I/O: session-id resume, UDP packing'
2952 * in tests/ssl-opt.sh.
2953 */
2954
2955 /* For event-driven IO, wait for socket to become available */
2956 if( mbedtls_ssl_check_pending( &ssl ) == 0 &&
2957 opt.event == 1 /* level triggered IO */ )
2958 {
2959#if defined(MBEDTLS_TIMING_C)
2960 idle( &client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ );
2961#else
2962 idle( &client_fd, MBEDTLS_ERR_SSL_WANT_READ );
2963#endif
2964 }
2965
Hanno Becker16970d22017-10-10 15:56:37 +01002966 ret = mbedtls_ssl_read( &ssl, buf, len );
2967
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00002968 /* Note that even if `mbedtls_ssl_check_pending` returns true,
2969 * it can happen that the subsequent call to `mbedtls_ssl_read`
2970 * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
2971 * might be discarded (e.g. because they are retransmissions). */
Hanno Becker16970d22017-10-10 15:56:37 +01002972 }
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02002973 while( mbedtls_status_is_ssl_in_progress( ret ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002974
2975 if( ret <= 0 )
2976 {
2977 switch( ret )
2978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2980 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002981 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002982 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002983
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002984 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002986 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002987 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002988 }
2989
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002990 len = ret;
2991 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002993 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002994 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002995
2996 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002997 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02002998 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002999 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00003001 if( opt.renegotiate && exchanges_left == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 mbedtls_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003004 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003007 {
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003008 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003011 goto reset;
3012 }
Hanno Becker16970d22017-10-10 15:56:37 +01003013
3014 /* For event-driven IO, wait for socket to become available */
3015 if( opt.event == 1 /* level triggered IO */ )
3016 {
3017#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003018 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003019#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003020 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003021#endif
3022 }
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003023 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003026 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003027#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003028
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003029 /*
3030 * 7. Write the 200 Response
3031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032 mbedtls_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003033 fflush( stdout );
3034
3035 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003037
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003038 /* Add padding to the response to reach opt.response_size in length */
3039 if( opt.response_size != DFL_RESPONSE_SIZE &&
3040 len < opt.response_size )
3041 {
3042 memset( buf + len, 'B', opt.response_size - len );
3043 len += opt.response_size - len;
3044 }
3045
3046 /* Truncate if response size is smaller than the "natural" size */
3047 if( opt.response_size != DFL_RESPONSE_SIZE &&
3048 len > opt.response_size )
3049 {
3050 len = opt.response_size;
3051
3052 /* Still end with \r\n unless that's really not possible */
3053 if( len >= 2 ) buf[len - 2] = '\r';
3054 if( len >= 1 ) buf[len - 1] = '\n';
3055 }
3056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003058 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003059 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003062 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066 mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003067 goto reset;
3068 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003069
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003070 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003073 goto reset;
3074 }
Hanno Becker16970d22017-10-10 15:56:37 +01003075
3076 /* For event-driven IO, wait for socket to become available */
3077 if( opt.event == 1 /* level triggered IO */ )
3078 {
3079#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003080 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003081#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003082 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003083#endif
3084 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003085 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003086 }
3087 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003088 else /* Not stream, so datagram */
3089 {
Hanno Becker16970d22017-10-10 15:56:37 +01003090 while( 1 )
3091 {
3092 ret = mbedtls_ssl_write( &ssl, buf, len );
3093
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003094 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003095 break;
3096
3097 /* For event-driven IO, wait for socket to become available */
3098 if( opt.event == 1 /* level triggered IO */ )
3099 {
3100#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003101 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003102#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003103 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003104#endif
3105 }
3106 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003107
3108 if( ret < 0 )
3109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003111 goto reset;
3112 }
3113
3114 frags = 1;
3115 written = ret;
3116 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003117
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003118 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119 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 +01003120 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01003121
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003122 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003123 * 7b. Continue doing data exchanges?
3124 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003125 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003126 goto data_exchange;
3127
3128 /*
3129 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003130 */
3131close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01003133
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003134 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003136 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003137 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 mbedtls_printf( " done\n" );
Rich Evansf90016a2015-01-19 14:26:37 +00003140
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003141 goto reset;
3142
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003143 /*
3144 * Cleanup and exit
3145 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003146exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003148 if( ret != 0 )
3149 {
3150 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151 mbedtls_strerror( ret, error_buf, 100 );
3152 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003153 }
3154#endif
3155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 mbedtls_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003157 fflush( stdout );
3158
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003159 mbedtls_net_free( &client_fd );
3160 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +02003161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3163 mbedtls_dhm_free( &dhm );
Paul Bakkera317a982014-06-18 16:44:11 +02003164#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165#if defined(MBEDTLS_X509_CRT_PARSE_C)
3166 mbedtls_x509_crt_free( &cacert );
3167 mbedtls_x509_crt_free( &srvcert );
3168 mbedtls_pk_free( &pkey );
3169 mbedtls_x509_crt_free( &srvcert2 );
3170 mbedtls_pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02003171#endif
Gilles Peskine44817442018-06-13 18:09:28 +02003172#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3173 for( i = 0; (size_t) i < ssl_async_keys.slots_used; i++ )
3174 {
3175 if( ssl_async_keys.slots[i].pk_owned )
3176 {
3177 mbedtls_pk_free( ssl_async_keys.slots[i].pk );
3178 mbedtls_free( ssl_async_keys.slots[i].pk );
3179 ssl_async_keys.slots[i].pk = NULL;
3180 }
3181 }
3182#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02003183#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003184 sni_free( sni_info );
3185#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003187 psk_free( psk_info );
3188#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3190 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003191#endif
Paul Bakkered27a042013-04-18 22:46:23 +02003192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003194 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195 mbedtls_ctr_drbg_free( &ctr_drbg );
3196 mbedtls_entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198#if defined(MBEDTLS_SSL_CACHE_C)
3199 mbedtls_ssl_cache_free( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00003200#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003201#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3202 mbedtls_ssl_ticket_free( &ticket_ctx );
3203#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204#if defined(MBEDTLS_SSL_COOKIE_C)
3205 mbedtls_ssl_cookie_free( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02003206#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003207
Hanno Becker095d9cf2018-10-09 12:39:13 +01003208 mbedtls_free( buf );
3209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
3211#if defined(MBEDTLS_MEMORY_DEBUG)
3212 mbedtls_memory_buffer_alloc_status();
Paul Bakker82024bf2013-07-04 11:52:32 +02003213#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +02003215#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02003216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217 mbedtls_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003218
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003219#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003221 fflush( stdout ); getchar();
3222#endif
3223
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02003224 // Shell can not handle large exit numbers -> 1 for errors
3225 if( ret < 0 )
3226 ret = 1;
3227
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003228 return( ret );
3229}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
3231 MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003232 MBEDTLS_CTR_DRBG_C */