blob: a4c5fab4b62307cefe763bb16cb2364abd819e76 [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
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000369#define USAGE \
370 "\n usage: ssl_server2 param=<>...\n" \
371 "\n acceptable parameters:\n" \
Ron Eldor71f68c42017-09-26 11:29:11 +0300372 " server_addr=%%s default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000373 " server_port=%%d default: 4433\n" \
374 " debug_level=%%d default: 0 (disabled)\n" \
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200375 " buffer_size=%%d default: 200 \n" \
376 " (minimum: 1, max: 16385)\n" \
377 " response_size=%%d default: about 152 (basic response)\n" \
378 " (minimum: 0, max: 16384)\n" \
379 " increases buffer_size if bigger\n"\
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100380 " nbio=%%d default: 0 (blocking I/O)\n" \
381 " options: 1 (non-blocking), 2 (added delays)\n" \
Hanno Becker16970d22017-10-10 15:56:37 +0100382 " event=%%d default: 0 (loop)\n" \
383 " options: 1 (level-triggered, implies nbio=1),\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200384 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100385 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200386 USAGE_DTLS \
387 USAGE_COOKIES \
388 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200389 USAGE_BADMAC_LIMIT \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200390 "\n" \
Manuel Pégourié-Gonnardee6139c2015-05-07 10:18:26 +0100391 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100392 " options: none, optional, required\n" \
Janos Follath4817e272017-04-10 13:44:33 +0100393 " cert_req_ca_list=%%d default: 1 (send ca list)\n" \
394 " options: 1 (send ca list), 0 (don't send)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000395 USAGE_IO \
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100396 USAGE_SSL_ASYNC \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100397 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100398 "\n" \
399 USAGE_PSK \
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200400 USAGE_ECJPAKE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100401 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100402 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100403 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200404 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200405 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100406 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100407 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100408 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100409 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200410 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200411 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100412 USAGE_ETM \
Hanno Beckere6706e62017-05-15 16:05:15 +0100413 USAGE_CURVES \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100414 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100415 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskinebc70a182017-05-09 15:59:24 +0200416 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200417 " min_version=%%s default: (library default: tls1)\n" \
418 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200419 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100420 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200421 "\n" \
422 " version_suites=a,b,c,d per-version ciphersuites\n" \
423 " in order from ssl3 to tls1_2\n" \
424 " default: all enabled\n" \
425 " force_ciphersuite=<name> default: all enabled\n" \
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100426 " query_config=<name> return 0 if the specified\n" \
427 " configuration macro is defined and 1\n" \
428 " otherwise. The expansion of the macro\n" \
429 " is printed if it is defined\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000430 " acceptable ciphersuite names:\n"
431
Andres AG692ad842017-01-19 16:30:57 +0000432
Hanno Becker8651a432017-06-09 16:13:22 +0100433#define ALPN_LIST_SIZE 10
434#define CURVE_LIST_SIZE 20
435
Andres AG692ad842017-01-19 16:30:57 +0000436#define PUT_UINT64_BE(out_be,in_le,i) \
437{ \
438 (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \
439 (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \
440 (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \
441 (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \
442 (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \
443 (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \
444 (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \
445 (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \
446}
447
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100448#if defined(MBEDTLS_CHECK_PARAMS)
449#include "mbedtls/platform_util.h"
450void mbedtls_param_failed( const char *failure_condition,
451 const char *file,
452 int line )
Simon Butcher63cb97e2018-12-06 17:43:31 +0000453{
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100454 mbedtls_printf( "%s:%i: Input param failed - %s\n",
455 file, line, failure_condition );
Simon Butcher63cb97e2018-12-06 17:43:31 +0000456 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
457}
458#endif
459
Rich Evans85b05ec2015-02-12 11:37:29 +0000460/*
461 * global options
462 */
463struct options
464{
465 const char *server_addr; /* address on which the ssl service runs */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200466 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000467 int debug_level; /* level of debugging */
468 int nbio; /* should I/O be blocking? */
Hanno Becker16970d22017-10-10 15:56:37 +0100469 int event; /* loop or event-driven IO? level or edge triggered? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Andrzej Kurek30e731d2017-10-12 13:50:29 +0200471 int response_size; /* pad response with header to requested size */
472 uint16_t buffer_size; /* IO buffer size */
Rich Evans85b05ec2015-02-12 11:37:29 +0000473 const char *ca_file; /* the file with the CA certificate(s) */
474 const char *ca_path; /* the path with the CA certificate(s) reside */
475 const char *crt_file; /* the file with the server certificate */
476 const char *key_file; /* the file with the server key */
477 const char *crt_file2; /* the file with the 2nd server certificate */
478 const char *key_file2; /* the file with the 2nd server key */
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100479 const char *async_operations; /* supported SSL asynchronous operations */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100480 int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
481 int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
482 int async_private_error; /* inject error in async private callback */
Rich Evans85b05ec2015-02-12 11:37:29 +0000483 const char *psk; /* the pre-shared key */
484 const char *psk_identity; /* the pre-shared key identity */
485 char *psk_list; /* list of PSK id/key pairs for callback */
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +0200486 const char *ecjpake_pw; /* the EC J-PAKE password */
Rich Evans85b05ec2015-02-12 11:37:29 +0000487 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
488 const char *version_suites; /* per-version ciphersuites */
489 int renegotiation; /* enable / disable renegotiation */
490 int allow_legacy; /* allow legacy renegotiation */
491 int renegotiate; /* attempt renegotiation? */
492 int renego_delay; /* delay before enforcing renegotiation */
Andres AG692ad842017-01-19 16:30:57 +0000493 uint64_t renego_period; /* period for automatic renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000494 int exchanges; /* number of data exchanges */
495 int min_version; /* minimum protocol version accepted */
496 int max_version; /* maximum protocol version accepted */
497 int arc4; /* flag for arc4 suites support */
Gilles Peskinebc70a182017-05-09 15:59:24 +0200498 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000499 int auth_mode; /* verify mode for connection */
Janos Follath4817e272017-04-10 13:44:33 +0100500 int cert_req_ca_list; /* should we send the CA list? */
Rich Evans85b05ec2015-02-12 11:37:29 +0000501 unsigned char mfl_code; /* code for maximum fragment length */
502 int trunc_hmac; /* accept truncated hmac? */
503 int tickets; /* enable / disable session tickets */
504 int ticket_timeout; /* session ticket lifetime */
505 int cache_max; /* max number of session cache entries */
506 int cache_timeout; /* expiration delay of session cache entries */
507 char *sni; /* string describing sni information */
Hanno Beckere6706e62017-05-15 16:05:15 +0100508 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000509 const char *alpn_string; /* ALPN supported protocols */
510 const char *dhm_file; /* the file with the DH parameters */
511 int extended_ms; /* allow negotiation of extended MS? */
512 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000513 int transport; /* TLS or DTLS? */
514 int cookies; /* Use cookies for DTLS? -1 to break them */
515 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
516 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
517 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +0200518 int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
Hanno Beckere7675d02018-08-14 13:28:56 +0100519 int dgram_packing; /* allow/forbid datagram packing */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000520 int badmac_limit; /* Limit of records with bad MAC */
Rich Evans85b05ec2015-02-12 11:37:29 +0000521} opt;
522
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +0100523int query_config( const char *config );
524
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200525static void my_debug( void *ctx, int level,
526 const char *file, int line,
527 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000528{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200529 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000530
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200531 /* Extract basename from file */
532 for( p = basename = file; *p != '\0'; p++ )
533 if( *p == '/' || *p == '\\' )
534 basename = p + 1;
535
536 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000537 fflush( (FILE *) ctx );
538}
539
540/*
541 * Test recv/send functions that make sure each try returns
542 * WANT_READ/WANT_WRITE at least once before sucesseding
543 */
544static int my_recv( void *ctx, unsigned char *buf, size_t len )
545{
546 static int first_try = 1;
547 int ret;
548
549 if( first_try )
550 {
551 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100552 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000553 }
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100556 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000557 first_try = 1; /* Next call will be a new operation */
558 return( ret );
559}
560
561static int my_send( void *ctx, const unsigned char *buf, size_t len )
562{
563 static int first_try = 1;
564 int ret;
565
566 if( first_try )
567 {
568 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100569 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000570 }
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100573 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000574 first_try = 1; /* Next call will be a new operation */
575 return( ret );
576}
577
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200578/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200579 * Return authmode from string, or -1 on error
580 */
581static int get_auth_mode( const char *s )
582{
583 if( strcmp( s, "none" ) == 0 )
584 return( MBEDTLS_SSL_VERIFY_NONE );
585 if( strcmp( s, "optional" ) == 0 )
586 return( MBEDTLS_SSL_VERIFY_OPTIONAL );
587 if( strcmp( s, "required" ) == 0 )
588 return( MBEDTLS_SSL_VERIFY_REQUIRED );
589
590 return( -1 );
591}
592
593/*
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200594 * Used by sni_parse and psk_parse to handle coma-separated lists
595 */
596#define GET_ITEM( dst ) \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100597 do \
598 { \
599 (dst) = p; \
600 while( *p != ',' ) \
601 if( ++p > end ) \
602 goto error; \
603 *p++ = '\0'; \
604 } while( 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200605
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200606#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100607typedef struct _sni_entry sni_entry;
608
609struct _sni_entry {
610 const char *name;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_x509_crt *cert;
612 mbedtls_pk_context *key;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200613 mbedtls_x509_crt* ca;
614 mbedtls_x509_crl* crl;
615 int authmode;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100616 sni_entry *next;
617};
618
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100619void sni_free( sni_entry *head )
620{
621 sni_entry *cur = head, *next;
622
623 while( cur != NULL )
624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_x509_crt_free( cur->cert );
626 mbedtls_free( cur->cert );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_pk_free( cur->key );
629 mbedtls_free( cur->key );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100630
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200631 mbedtls_x509_crt_free( cur->ca );
632 mbedtls_free( cur->ca );
Ron Eldor24eec792019-04-04 15:02:01 +0300633#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200634 mbedtls_x509_crl_free( cur->crl );
635 mbedtls_free( cur->crl );
Ron Eldor24eec792019-04-04 15:02:01 +0300636#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100637 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 mbedtls_free( cur );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100639 cur = next;
640 }
641}
642
643/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200644 * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
645 * into a usable sni_entry list. For ca1, crl1, auth1, the special value
646 * '-' means unset. If ca1 is unset, then crl1 is ignored too.
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000647 *
648 * Modifies the input string! This is not production quality!
649 */
650sni_entry *sni_parse( char *sni_string )
651{
652 sni_entry *cur = NULL, *new = NULL;
653 char *p = sni_string;
654 char *end = p;
Ron Eldor24eec792019-04-04 15:02:01 +0300655 char *crt_file, *key_file, *ca_file, *auth_str;
656#if defined(MBEDTLS_X509_CRL_PARSE_C)
657 char *crl_file;
658#endif
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000659
660 while( *end != '\0' )
661 ++end;
662 *end = ',';
663
664 while( p <= end )
665 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200666 if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000667 {
668 sni_free( cur );
669 return( NULL );
670 }
671
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200672 GET_ITEM( new->name );
673 GET_ITEM( crt_file );
674 GET_ITEM( key_file );
675 GET_ITEM( ca_file );
Ron Eldor24eec792019-04-04 15:02:01 +0300676#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200677 GET_ITEM( crl_file );
Ron Eldor24eec792019-04-04 15:02:01 +0300678#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200679 GET_ITEM( auth_str );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000680
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200681 if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
682 ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200683 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_x509_crt_init( new->cert );
686 mbedtls_pk_init( new->key );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
689 mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000690 goto error;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200691
692 if( strcmp( ca_file, "-" ) != 0 )
693 {
694 if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
695 goto error;
696
697 mbedtls_x509_crt_init( new->ca );
698
699 if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
700 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000701 }
702
Ron Eldor24eec792019-04-04 15:02:01 +0300703#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200704 if( strcmp( crl_file, "-" ) != 0 )
705 {
706 if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
707 goto error;
708
709 mbedtls_x509_crl_init( new->crl );
710
711 if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
712 goto error;
713 }
Ron Eldor24eec792019-04-04 15:02:01 +0300714#endif
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200715
716 if( strcmp( auth_str, "-" ) != 0 )
717 {
718 if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
719 goto error;
720 }
721 else
722 new->authmode = DFL_AUTH_MODE;
723
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000724 new->next = cur;
725 cur = new;
726 }
727
728 return( cur );
729
730error:
731 sni_free( new );
732 sni_free( cur );
733 return( NULL );
734}
735
736/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100737 * SNI callback.
738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100740 const unsigned char *name, size_t name_len )
741{
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200742 const sni_entry *cur = (const sni_entry *) p_info;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100743
744 while( cur != NULL )
745 {
746 if( name_len == strlen( cur->name ) &&
747 memcmp( name, cur->name, name_len ) == 0 )
748 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200749 if( cur->ca != NULL )
750 mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
751
752 if( cur->authmode != DFL_AUTH_MODE )
753 mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
754
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200755 return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100756 }
757
758 cur = cur->next;
759 }
760
761 return( -1 );
762}
763
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200764#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200767
Hanno Beckerd6028a12018-10-15 12:01:35 +0100768#define HEX2NUM( c ) \
769 do \
770 { \
771 if( (c) >= '0' && (c) <= '9' ) \
772 (c) -= '0'; \
773 else if( (c) >= 'a' && (c) <= 'f' ) \
774 (c) -= 'a' - 10; \
775 else if( (c) >= 'A' && (c) <= 'F' ) \
776 (c) -= 'A' - 10; \
777 else \
778 return( -1 ); \
779 } while( 0 )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200780
781/*
782 * Convert a hex string to bytes.
783 * Return 0 on success, -1 on error.
784 */
785int unhexify( unsigned char *output, const char *input, size_t *olen )
786{
787 unsigned char c;
788 size_t j;
789
790 *olen = strlen( input );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 if( *olen % 2 != 0 || *olen / 2 > MBEDTLS_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200792 return( -1 );
793 *olen /= 2;
794
795 for( j = 0; j < *olen * 2; j += 2 )
796 {
797 c = input[j];
798 HEX2NUM( c );
799 output[ j / 2 ] = c << 4;
800
801 c = input[j + 1];
802 HEX2NUM( c );
803 output[ j / 2 ] |= c;
804 }
805
806 return( 0 );
807}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200808
809typedef struct _psk_entry psk_entry;
810
811struct _psk_entry
812{
813 const char *name;
814 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 unsigned char key[MBEDTLS_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200816 psk_entry *next;
817};
818
819/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000820 * Free a list of psk_entry's
821 */
822void psk_free( psk_entry *head )
823{
824 psk_entry *next;
825
826 while( head != NULL )
827 {
828 next = head->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_free( head );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000830 head = next;
831 }
832}
833
834/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200835 * Parse a string of pairs name1,key1[,name2,key2[,...]]
836 * into a usable psk_entry list.
837 *
838 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200839 */
840psk_entry *psk_parse( char *psk_string )
841{
842 psk_entry *cur = NULL, *new = NULL;
843 char *p = psk_string;
844 char *end = p;
845 char *key_hex;
846
847 while( *end != '\0' )
848 ++end;
849 *end = ',';
850
851 while( p <= end )
852 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200853 if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +0000854 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200855
856 memset( new, 0, sizeof( psk_entry ) );
857
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200858 GET_ITEM( new->name );
859 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200860
861 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000862 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200863
864 new->next = cur;
865 cur = new;
866 }
867
868 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200869
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000870error:
871 psk_free( new );
872 psk_free( cur );
873 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200874}
875
876/*
877 * PSK callback
878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200880 const unsigned char *name, size_t name_len )
881{
882 psk_entry *cur = (psk_entry *) p_info;
883
884 while( cur != NULL )
885 {
886 if( name_len == strlen( cur->name ) &&
887 memcmp( name, cur->name, name_len ) == 0 )
888 {
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +0100889 return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200890 }
891
892 cur = cur->next;
893 }
894
895 return( -1 );
896}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200898
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200899static mbedtls_net_context listen_fd, client_fd;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200900
901/* Interruption handler to ensure clean exit (for valgrind testing) */
902#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200903static int received_sigterm = 0;
904void term_handler( int sig )
905{
906 ((void) sig);
907 received_sigterm = 1;
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200908 mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
909 mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200910}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200911#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200912
Gilles Peskine682df092017-05-11 19:01:11 +0200913#if defined(MBEDTLS_X509_CRT_PARSE_C)
914static int ssl_sig_hashes_for_test[] = {
915#if defined(MBEDTLS_SHA512_C)
916 MBEDTLS_MD_SHA512,
917 MBEDTLS_MD_SHA384,
918#endif
919#if defined(MBEDTLS_SHA256_C)
920 MBEDTLS_MD_SHA256,
921 MBEDTLS_MD_SHA224,
922#endif
923#if defined(MBEDTLS_SHA1_C)
924 /* Allow SHA-1 as we use it extensively in tests. */
925 MBEDTLS_MD_SHA1,
926#endif
927 MBEDTLS_MD_NONE
928};
929#endif /* MBEDTLS_X509_CRT_PARSE_C */
930
Gilles Peskinea36ac4f2018-04-26 08:05:02 +0200931/** Return true if \p ret is a status code indicating that there is an
932 * operation in progress on an SSL connection, and false if it indicates
933 * success or a fatal error.
934 *
935 * The possible operations in progress are:
936 *
937 * - A read, when the SSL input buffer does not contain a full message.
938 * - A write, when the SSL output buffer contains some data that has not
939 * been sent over the network yet.
940 * - An asynchronous callback that has not completed yet. */
941static int mbedtls_status_is_ssl_in_progress( int ret )
942{
943 return( ret == MBEDTLS_ERR_SSL_WANT_READ ||
944 ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
945 ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
946}
947
Gilles Peskineb74a1c72018-04-24 13:09:22 +0200948#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100949typedef struct
950{
Gilles Peskine44817442018-06-13 18:09:28 +0200951 mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
952 mbedtls_pk_context *pk; /*!< Private key */
953 unsigned delay; /*!< Number of resume steps to go through */
954 unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100955} ssl_async_key_slot_t;
956
957typedef enum {
Gilles Peskinead28bf02018-04-26 00:19:16 +0200958 SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
959 SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
960 SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start */
961 SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
Gilles Peskinec9125722018-04-26 07:15:40 +0200962#define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100963} ssl_async_inject_error_t;
964
965typedef struct
966{
Gilles Peskinee2479892018-06-13 18:06:51 +0200967 ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100968 size_t slots_used;
969 ssl_async_inject_error_t inject_error;
970 int (*f_rng)(void *, unsigned char *, size_t);
971 void *p_rng;
972} ssl_async_key_context_t;
973
Gilles Peskined6fbfde2018-04-30 10:23:56 +0200974int ssl_async_set_key( ssl_async_key_context_t *ctx,
Gilles Peskine44817442018-06-13 18:09:28 +0200975 mbedtls_x509_crt *cert,
976 mbedtls_pk_context *pk,
977 int pk_take_ownership,
978 unsigned delay )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100979{
Gilles Peskined6fbfde2018-04-30 10:23:56 +0200980 if( ctx->slots_used >= sizeof( ctx->slots ) / sizeof( *ctx->slots ) )
981 return( -1 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100982 ctx->slots[ctx->slots_used].cert = cert;
983 ctx->slots[ctx->slots_used].pk = pk;
984 ctx->slots[ctx->slots_used].delay = delay;
Gilles Peskine44817442018-06-13 18:09:28 +0200985 ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100986 ++ctx->slots_used;
Gilles Peskined6fbfde2018-04-30 10:23:56 +0200987 return( 0 );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +0100988}
989
Gilles Peskinefcca9d82018-01-12 13:47:48 +0100990#define SSL_ASYNC_INPUT_MAX_SIZE 512
Gilles Peskined3268832018-04-26 06:23:59 +0200991
992typedef enum
993{
994 ASYNC_OP_SIGN,
995 ASYNC_OP_DECRYPT,
996} ssl_async_operation_type_t;
997/* Note that the enum above and the array below need to be kept in sync!
998 * `ssl_async_operation_names[op]` is the name of op for each value `op`
999 * of type `ssl_async_operation_type_t`. */
1000static const char *const ssl_async_operation_names[] =
1001{
1002 "sign",
1003 "decrypt",
1004};
1005
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001006typedef struct
1007{
Gilles Peskine6331d782018-04-26 13:27:43 +02001008 unsigned slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001009 ssl_async_operation_type_t operation_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001010 mbedtls_md_type_t md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001011 unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
1012 size_t input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001013 unsigned remaining_delay;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001014} ssl_async_operation_context_t;
1015
Gilles Peskine8f97af72018-04-26 11:46:10 +02001016static int ssl_async_start( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001017 mbedtls_x509_crt *cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001018 ssl_async_operation_type_t op_type,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001019 mbedtls_md_type_t md_alg,
1020 const unsigned char *input,
1021 size_t input_len )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001022{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001023 ssl_async_key_context_t *config_data =
1024 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskine6331d782018-04-26 13:27:43 +02001025 unsigned slot;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001026 ssl_async_operation_context_t *ctx = NULL;
Gilles Peskined3268832018-04-26 06:23:59 +02001027 const char *op_name = ssl_async_operation_names[op_type];
Gilles Peskinef1127252018-04-24 13:05:39 +02001028
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001029 {
1030 char dn[100];
Gilles Peskined5d983e2018-06-15 14:05:10 +02001031 if( mbedtls_x509_dn_gets( dn, sizeof( dn ), &cert->subject ) > 0 )
1032 mbedtls_printf( "Async %s callback: looking for DN=%s\n",
1033 op_name, dn );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001034 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001035
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001036 /* Look for a private key that matches the public key in cert.
1037 * Since this test code has the private key inside Mbed TLS,
1038 * we call mbedtls_pk_check_pair to match a private key with the
1039 * public key. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001040 for( slot = 0; slot < config_data->slots_used; slot++ )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001041 {
Gilles Peskine3dae1cf2018-04-30 12:07:56 +02001042 if( mbedtls_pk_check_pair( &cert->pk,
1043 config_data->slots[slot].pk ) == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001044 break;
1045 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001046 if( slot == config_data->slots_used )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001047 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001048 mbedtls_printf( "Async %s callback: no key matches this certificate.\n",
1049 op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001050 return( MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH );
1051 }
Gilles Peskine6331d782018-04-26 13:27:43 +02001052 mbedtls_printf( "Async %s callback: using key slot %u, delay=%u.\n",
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001053 op_name, slot, config_data->slots[slot].delay );
Gilles Peskinef1127252018-04-24 13:05:39 +02001054
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001055 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001056 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001057 mbedtls_printf( "Async %s callback: injected error\n", op_name );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001058 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1059 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001060
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001061 if( input_len > SSL_ASYNC_INPUT_MAX_SIZE )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001062 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Gilles Peskinef1127252018-04-24 13:05:39 +02001063
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001064 ctx = mbedtls_calloc( 1, sizeof( *ctx ) );
1065 if( ctx == NULL )
1066 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1067 ctx->slot = slot;
Gilles Peskined3268832018-04-26 06:23:59 +02001068 ctx->operation_type = op_type;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001069 ctx->md_alg = md_alg;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001070 memcpy( ctx->input, input, input_len );
1071 ctx->input_len = input_len;
Gilles Peskineceb541b2018-04-26 06:30:45 +02001072 ctx->remaining_delay = config_data->slots[slot].delay;
Gilles Peskinea668c602018-04-30 11:54:39 +02001073 mbedtls_ssl_set_async_operation_data( ssl, ctx );
Gilles Peskinef1127252018-04-24 13:05:39 +02001074
Gilles Peskineceb541b2018-04-26 06:30:45 +02001075 if( ctx->remaining_delay == 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001076 return( 0 );
1077 else
1078 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1079}
1080
Gilles Peskine8f97af72018-04-26 11:46:10 +02001081static int ssl_async_sign( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001082 mbedtls_x509_crt *cert,
1083 mbedtls_md_type_t md_alg,
1084 const unsigned char *hash,
1085 size_t hash_len )
1086{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001087 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001088 ASYNC_OP_SIGN, md_alg,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001089 hash, hash_len ) );
1090}
1091
Gilles Peskine8f97af72018-04-26 11:46:10 +02001092static int ssl_async_decrypt( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001093 mbedtls_x509_crt *cert,
1094 const unsigned char *input,
1095 size_t input_len )
1096{
Gilles Peskine8f97af72018-04-26 11:46:10 +02001097 return( ssl_async_start( ssl, cert,
Gilles Peskined3268832018-04-26 06:23:59 +02001098 ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001099 input, input_len ) );
1100}
1101
Gilles Peskine8f97af72018-04-26 11:46:10 +02001102static int ssl_async_resume( mbedtls_ssl_context *ssl,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001103 unsigned char *output,
1104 size_t *output_len,
1105 size_t output_size )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001106{
Gilles Peskinea668c602018-04-30 11:54:39 +02001107 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine8f97af72018-04-26 11:46:10 +02001108 ssl_async_key_context_t *config_data =
1109 mbedtls_ssl_conf_get_async_config_data( ssl->conf );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02001110 ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001111 int ret;
Gilles Peskinef5a99962018-04-30 16:37:23 +02001112 const char *op_name;
Gilles Peskinef1127252018-04-24 13:05:39 +02001113
Gilles Peskineceb541b2018-04-26 06:30:45 +02001114 if( ctx->remaining_delay > 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001115 {
Gilles Peskineceb541b2018-04-26 06:30:45 +02001116 --ctx->remaining_delay;
Gilles Peskine6331d782018-04-26 13:27:43 +02001117 mbedtls_printf( "Async resume (slot %u): call %u more times.\n",
Gilles Peskineceb541b2018-04-26 06:30:45 +02001118 ctx->slot, ctx->remaining_delay );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001119 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
1120 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001121
Gilles Peskined3268832018-04-26 06:23:59 +02001122 switch( ctx->operation_type )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001123 {
Gilles Peskined3268832018-04-26 06:23:59 +02001124 case ASYNC_OP_DECRYPT:
Gilles Peskined3268832018-04-26 06:23:59 +02001125 ret = mbedtls_pk_decrypt( key_slot->pk,
1126 ctx->input, ctx->input_len,
1127 output, output_len, output_size,
1128 config_data->f_rng, config_data->p_rng );
1129 break;
1130 case ASYNC_OP_SIGN:
Gilles Peskined3268832018-04-26 06:23:59 +02001131 ret = mbedtls_pk_sign( key_slot->pk,
1132 ctx->md_alg,
1133 ctx->input, ctx->input_len,
1134 output, output_len,
1135 config_data->f_rng, config_data->p_rng );
1136 break;
1137 default:
Gilles Peskine6331d782018-04-26 13:27:43 +02001138 mbedtls_printf( "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
Gilles Peskined3268832018-04-26 06:23:59 +02001139 ctx->slot, (long) ctx->operation_type );
Gilles Peskine44817442018-06-13 18:09:28 +02001140 mbedtls_free( ctx );
Gilles Peskined3268832018-04-26 06:23:59 +02001141 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1142 break;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001143 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001144
Gilles Peskinef5a99962018-04-30 16:37:23 +02001145 op_name = ssl_async_operation_names[ctx->operation_type];
1146
Gilles Peskinec9125722018-04-26 07:15:40 +02001147 if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001148 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001149 mbedtls_printf( "Async resume callback: %s done but injected error\n",
1150 op_name );
Gilles Peskine2636fad2018-06-12 14:17:39 +02001151 mbedtls_free( ctx );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001152 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1153 }
Gilles Peskinef1127252018-04-24 13:05:39 +02001154
Gilles Peskine6331d782018-04-26 13:27:43 +02001155 mbedtls_printf( "Async resume (slot %u): %s done, status=%d.\n",
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001156 ctx->slot, op_name, ret );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001157 mbedtls_free( ctx );
1158 return( ret );
1159}
1160
Gilles Peskine8f97af72018-04-26 11:46:10 +02001161static void ssl_async_cancel( mbedtls_ssl_context *ssl )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001162{
Gilles Peskinea668c602018-04-30 11:54:39 +02001163 ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001164 mbedtls_printf( "Async cancel callback.\n" );
1165 mbedtls_free( ctx );
1166}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001167#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001168
Hanno Becker16970d22017-10-10 15:56:37 +01001169/*
1170 * Wait for an event from the underlying transport or the timer
1171 * (Used in event-driven IO mode).
1172 */
1173#if !defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001174int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001175 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001176#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001177int idle( mbedtls_net_context *fd,
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001178 mbedtls_timing_delay_context *timer,
1179 int idle_reason )
Hanno Becker16970d22017-10-10 15:56:37 +01001180#endif
Hanno Becker9b2b66e2018-03-15 12:21:15 +00001181{
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001182 int ret;
Hanno Becker16970d22017-10-10 15:56:37 +01001183 int poll_type = 0;
1184
1185 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
1186 poll_type = MBEDTLS_NET_POLL_WRITE;
1187 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
1188 poll_type = MBEDTLS_NET_POLL_READ;
1189#if !defined(MBEDTLS_TIMING_C)
1190 else
Hanno Beckeref527962018-03-15 15:49:24 +00001191 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001192#endif
1193
1194 while( 1 )
1195 {
Hanno Becker197a91c2017-10-31 10:58:53 +00001196 /* Check if timer has expired */
Hanno Becker16970d22017-10-10 15:56:37 +01001197#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00001198 if( timer != NULL &&
1199 mbedtls_timing_get_delay( timer ) == 2 )
Hanno Becker16970d22017-10-10 15:56:37 +01001200 {
Hanno Becker16970d22017-10-10 15:56:37 +01001201 break;
1202 }
Hanno Becker197a91c2017-10-31 10:58:53 +00001203#endif /* MBEDTLS_TIMING_C */
Hanno Becker16970d22017-10-10 15:56:37 +01001204
Hanno Becker197a91c2017-10-31 10:58:53 +00001205 /* Check if underlying transport became available */
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001206 if( poll_type != 0 )
Hanno Becker16970d22017-10-10 15:56:37 +01001207 {
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001208 ret = mbedtls_net_poll( fd, poll_type, 0 );
1209 if( ret < 0 )
1210 return( ret );
1211 if( ret == poll_type )
1212 break;
Hanno Becker16970d22017-10-10 15:56:37 +01001213 }
1214 }
Hanno Beckeradfa64f2018-03-15 11:35:07 +00001215
1216 return( 0 );
Hanno Becker16970d22017-10-10 15:56:37 +01001217}
1218
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001219int main( int argc, char *argv[] )
1220{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001221 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001222 int version_suites[4][2];
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001223 unsigned char* buf = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1225 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +02001226 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +02001227 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +02001228#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001229 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001230 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02001231 size_t cliip_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232#if defined(MBEDTLS_SSL_COOKIE_C)
1233 mbedtls_ssl_cookie_ctx cookie_ctx;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001234#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001235
Gilles Peskineef86ab22017-05-05 18:59:02 +02001236#if defined(MBEDTLS_X509_CRT_PARSE_C)
1237 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
1238#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 mbedtls_entropy_context entropy;
1240 mbedtls_ctr_drbg_context ctr_drbg;
1241 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001242 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001243#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02001244 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02001245#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001247 unsigned char renego_period[8] = { 0 };
1248#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +02001250 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 mbedtls_x509_crt cacert;
1252 mbedtls_x509_crt srvcert;
1253 mbedtls_pk_context pkey;
1254 mbedtls_x509_crt srvcert2;
1255 mbedtls_pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001256 int key_cert_init = 0, key_cert_init2 = 0;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001257#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001258 ssl_async_key_context_t ssl_async_keys;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001259#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001260#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1262 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001263#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_SSL_CACHE_C)
1265 mbedtls_ssl_cache_context cache;
Paul Bakker0a597072012-09-25 21:55:46 +00001266#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001267#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1268 mbedtls_ssl_ticket_context ticket_ctx;
1269#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001270#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001271 sni_entry *sni_info = NULL;
1272#endif
Hanno Beckere6706e62017-05-15 16:05:15 +01001273#if defined(MBEDTLS_ECP_C)
Hanno Becker8651a432017-06-09 16:13:22 +01001274 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Beckere6706e62017-05-15 16:05:15 +01001275 const mbedtls_ecp_curve_info * curve_cur;
1276#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker8651a432017-06-09 16:13:22 +01001278 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001279#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Simon Butchercce68be2018-07-23 14:26:09 +01001281 unsigned char alloc_buf[MEMORY_HEAP_SIZE];
Paul Bakker82024bf2013-07-04 11:52:32 +02001282#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001283
1284 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001285 char *p, *q;
1286 const int *list;
1287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1289 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker82024bf2013-07-04 11:52:32 +02001290#endif
1291
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001292 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +02001293 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001294 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02001295 mbedtls_net_init( &client_fd );
1296 mbedtls_net_init( &listen_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001297 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001298 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001299 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#if defined(MBEDTLS_X509_CRT_PARSE_C)
1301 mbedtls_x509_crt_init( &cacert );
1302 mbedtls_x509_crt_init( &srvcert );
1303 mbedtls_pk_init( &pkey );
1304 mbedtls_x509_crt_init( &srvcert2 );
1305 mbedtls_pk_init( &pkey2 );
Gilles Peskine4d9ec4d2018-04-26 14:33:43 +02001306#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
1307 memset( &ssl_async_keys, 0, sizeof( ssl_async_keys ) );
1308#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001309#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
1311 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001312#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313#if defined(MBEDTLS_SSL_CACHE_C)
1314 mbedtls_ssl_cache_init( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00001315#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001316#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1317 mbedtls_ssl_ticket_init( &ticket_ctx );
1318#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +02001320 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001321#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322#if defined(MBEDTLS_SSL_COOKIE_C)
1323 mbedtls_ssl_cookie_init( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001324#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001325
Paul Bakkerc1283d32014-08-18 11:05:51 +02001326#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001327 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001328 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001329 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +02001330#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001331
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001332 if( argc == 0 )
1333 {
1334 usage:
1335 if( ret == 0 )
1336 ret = 1;
1337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 mbedtls_printf( USAGE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 list = mbedtls_ssl_list_ciphersuites();
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001341 while( *list )
1342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001344 list++;
1345 if( !*list )
1346 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001348 list++;
1349 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 mbedtls_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001351 goto exit;
1352 }
1353
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001354 opt.buffer_size = DFL_IO_BUF_LEN;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001355 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001356 opt.server_port = DFL_SERVER_PORT;
1357 opt.debug_level = DFL_DEBUG_LEVEL;
Hanno Becker16970d22017-10-10 15:56:37 +01001358 opt.event = DFL_EVENT;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001359 opt.response_size = DFL_RESPONSE_SIZE;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001360 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001361 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001362 opt.ca_file = DFL_CA_FILE;
1363 opt.ca_path = DFL_CA_PATH;
1364 opt.crt_file = DFL_CRT_FILE;
1365 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001366 opt.crt_file2 = DFL_CRT_FILE2;
1367 opt.key_file2 = DFL_KEY_FILE2;
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001368 opt.async_operations = DFL_ASYNC_OPERATIONS;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001369 opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
1370 opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
1371 opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
Paul Bakkerfbb17802013-04-17 19:10:21 +02001372 opt.psk = DFL_PSK;
1373 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001374 opt.psk_list = DFL_PSK_LIST;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001375 opt.ecjpake_pw = DFL_ECJPAKE_PW;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001376 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001377 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001378 opt.renegotiation = DFL_RENEGOTIATION;
1379 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001380 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001381 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001382 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001383 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001384 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001385 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001386 opt.arc4 = DFL_ARC4;
Gilles Peskinebc70a182017-05-09 15:59:24 +02001387 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +01001388 opt.auth_mode = DFL_AUTH_MODE;
Janos Follath4817e272017-04-10 13:44:33 +01001389 opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001390 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001391 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001392 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001393 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001394 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001395 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001396 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001397 opt.alpn_string = DFL_ALPN_STRING;
Hanno Beckere6706e62017-05-15 16:05:15 +01001398 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001399 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001400 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001401 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001402 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001403 opt.hs_to_min = DFL_HS_TO_MIN;
1404 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001405 opt.dtls_mtu = DFL_DTLS_MTU;
Hanno Beckere7675d02018-08-14 13:28:56 +01001406 opt.dgram_packing = DFL_DGRAM_PACKING;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001407 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001408 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001409 opt.etm = DFL_ETM;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001410
1411 for( i = 1; i < argc; i++ )
1412 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001413 p = argv[i];
1414 if( ( q = strchr( p, '=' ) ) == NULL )
1415 goto usage;
1416 *q++ = '\0';
1417
1418 if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001419 opt.server_port = q;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001420 else if( strcmp( p, "server_addr" ) == 0 )
1421 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001422 else if( strcmp( p, "dtls" ) == 0 )
1423 {
1424 int t = atoi( q );
1425 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001427 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001429 else
1430 goto usage;
1431 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001432 else if( strcmp( p, "debug_level" ) == 0 )
1433 {
1434 opt.debug_level = atoi( q );
1435 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1436 goto usage;
1437 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001438 else if( strcmp( p, "nbio" ) == 0 )
1439 {
1440 opt.nbio = atoi( q );
1441 if( opt.nbio < 0 || opt.nbio > 2 )
1442 goto usage;
1443 }
Hanno Becker16970d22017-10-10 15:56:37 +01001444 else if( strcmp( p, "event" ) == 0 )
1445 {
1446 opt.event = atoi( q );
1447 if( opt.event < 0 || opt.event > 2 )
1448 goto usage;
1449 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001450 else if( strcmp( p, "read_timeout" ) == 0 )
1451 opt.read_timeout = atoi( q );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001452 else if( strcmp( p, "buffer_size" ) == 0 )
1453 {
1454 opt.buffer_size = atoi( q );
1455 if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
1456 goto usage;
1457 }
1458 else if( strcmp( p, "response_size" ) == 0 )
1459 {
1460 opt.response_size = atoi( q );
1461 if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
1462 goto usage;
1463 if( opt.buffer_size < opt.response_size )
1464 opt.buffer_size = opt.response_size;
1465 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001466 else if( strcmp( p, "ca_file" ) == 0 )
1467 opt.ca_file = q;
1468 else if( strcmp( p, "ca_path" ) == 0 )
1469 opt.ca_path = q;
1470 else if( strcmp( p, "crt_file" ) == 0 )
1471 opt.crt_file = q;
1472 else if( strcmp( p, "key_file" ) == 0 )
1473 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001474 else if( strcmp( p, "crt_file2" ) == 0 )
1475 opt.crt_file2 = q;
1476 else if( strcmp( p, "key_file2" ) == 0 )
1477 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001478 else if( strcmp( p, "dhm_file" ) == 0 )
1479 opt.dhm_file = q;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001480#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01001481 else if( strcmp( p, "async_operations" ) == 0 )
1482 opt.async_operations = q;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001483 else if( strcmp( p, "async_private_delay1" ) == 0 )
1484 opt.async_private_delay1 = atoi( q );
1485 else if( strcmp( p, "async_private_delay2" ) == 0 )
1486 opt.async_private_delay2 = atoi( q );
1487 else if( strcmp( p, "async_private_error" ) == 0 )
1488 {
1489 int n = atoi( q );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01001490 if( n < -SSL_ASYNC_INJECT_ERROR_MAX ||
1491 n > SSL_ASYNC_INJECT_ERROR_MAX )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01001492 {
1493 ret = 2;
1494 goto usage;
1495 }
1496 opt.async_private_error = n;
1497 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02001498#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001499 else if( strcmp( p, "psk" ) == 0 )
1500 opt.psk = q;
1501 else if( strcmp( p, "psk_identity" ) == 0 )
1502 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001503 else if( strcmp( p, "psk_list" ) == 0 )
1504 opt.psk_list = q;
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02001505 else if( strcmp( p, "ecjpake_pw" ) == 0 )
1506 opt.ecjpake_pw = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001507 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001510
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001511 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001512 {
1513 ret = 2;
1514 goto usage;
1515 }
1516 opt.force_ciphersuite[1] = 0;
1517 }
Hanno Beckere6706e62017-05-15 16:05:15 +01001518 else if( strcmp( p, "curves" ) == 0 )
1519 opt.curves = q;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001520 else if( strcmp( p, "version_suites" ) == 0 )
1521 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001522 else if( strcmp( p, "renegotiation" ) == 0 )
1523 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001524 opt.renegotiation = (atoi( q )) ?
1525 MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1526 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001527 }
1528 else if( strcmp( p, "allow_legacy" ) == 0 )
1529 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001530 switch( atoi( q ) )
1531 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001532 case -1:
1533 opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
1534 break;
1535 case 0:
1536 opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
1537 break;
1538 case 1:
1539 opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
1540 break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001541 default: goto usage;
1542 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001543 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001544 else if( strcmp( p, "renegotiate" ) == 0 )
1545 {
1546 opt.renegotiate = atoi( q );
1547 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1548 goto usage;
1549 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001550 else if( strcmp( p, "renego_delay" ) == 0 )
1551 {
1552 opt.renego_delay = atoi( q );
1553 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001554 else if( strcmp( p, "renego_period" ) == 0 )
1555 {
Andres AG0b736db2017-02-08 14:05:57 +00001556#if defined(_MSC_VER)
1557 opt.renego_period = _strtoui64( q, NULL, 10 );
1558#else
1559 if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
1560 goto usage;
1561#endif /* _MSC_VER */
1562 if( opt.renego_period < 2 )
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001563 goto usage;
1564 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001565 else if( strcmp( p, "exchanges" ) == 0 )
1566 {
1567 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001568 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001569 goto usage;
1570 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001571 else if( strcmp( p, "min_version" ) == 0 )
1572 {
1573 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001575 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001577 else if( strcmp( q, "tls1_1" ) == 0 ||
1578 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001580 else if( strcmp( q, "tls1_2" ) == 0 ||
1581 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001583 else
1584 goto usage;
1585 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001586 else if( strcmp( p, "max_version" ) == 0 )
1587 {
1588 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001590 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001592 else if( strcmp( q, "tls1_1" ) == 0 ||
1593 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001595 else if( strcmp( q, "tls1_2" ) == 0 ||
1596 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001598 else
1599 goto usage;
1600 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001601 else if( strcmp( p, "arc4" ) == 0 )
1602 {
1603 switch( atoi( q ) )
1604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1606 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001607 default: goto usage;
1608 }
1609 }
Gilles Peskinebc70a182017-05-09 15:59:24 +02001610 else if( strcmp( p, "allow_sha1" ) == 0 )
1611 {
1612 switch( atoi( q ) )
1613 {
1614 case 0: opt.allow_sha1 = 0; break;
1615 case 1: opt.allow_sha1 = 1; break;
1616 default: goto usage;
1617 }
1618 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001619 else if( strcmp( p, "force_version" ) == 0 )
1620 {
1621 if( strcmp( q, "ssl3" ) == 0 )
1622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1624 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001625 }
1626 else if( strcmp( q, "tls1" ) == 0 )
1627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1629 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001630 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001631 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1634 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001635 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001636 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1639 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001640 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001641 else if( strcmp( q, "dtls1" ) == 0 )
1642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1644 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1645 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001646 }
1647 else if( strcmp( q, "dtls1_2" ) == 0 )
1648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1650 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1651 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001652 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001653 else
1654 goto usage;
1655 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001656 else if( strcmp( p, "auth_mode" ) == 0 )
1657 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001658 if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
Paul Bakker91ebfb52012-11-23 14:04:08 +01001659 goto usage;
1660 }
Janos Follath4817e272017-04-10 13:44:33 +01001661 else if( strcmp( p, "cert_req_ca_list" ) == 0 )
1662 {
1663 opt.cert_req_ca_list = atoi( q );
1664 if( opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1 )
1665 goto usage;
1666 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001667 else if( strcmp( p, "max_frag_len" ) == 0 )
1668 {
1669 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001671 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001673 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001675 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001677 else
1678 goto usage;
1679 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001680 else if( strcmp( p, "alpn" ) == 0 )
1681 {
1682 opt.alpn_string = q;
1683 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001684 else if( strcmp( p, "trunc_hmac" ) == 0 )
1685 {
1686 switch( atoi( q ) )
1687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1689 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001690 default: goto usage;
1691 }
1692 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001693 else if( strcmp( p, "extended_ms" ) == 0 )
1694 {
1695 switch( atoi( q ) )
1696 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001697 case 0:
1698 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
1699 break;
1700 case 1:
1701 opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1702 break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001703 default: goto usage;
1704 }
1705 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001706 else if( strcmp( p, "etm" ) == 0 )
1707 {
1708 switch( atoi( q ) )
1709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1711 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001712 default: goto usage;
1713 }
1714 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001715 else if( strcmp( p, "tickets" ) == 0 )
1716 {
1717 opt.tickets = atoi( q );
1718 if( opt.tickets < 0 || opt.tickets > 1 )
1719 goto usage;
1720 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001721 else if( strcmp( p, "ticket_timeout" ) == 0 )
1722 {
1723 opt.ticket_timeout = atoi( q );
1724 if( opt.ticket_timeout < 0 )
1725 goto usage;
1726 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001727 else if( strcmp( p, "cache_max" ) == 0 )
1728 {
1729 opt.cache_max = atoi( q );
1730 if( opt.cache_max < 0 )
1731 goto usage;
1732 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001733 else if( strcmp( p, "cache_timeout" ) == 0 )
1734 {
1735 opt.cache_timeout = atoi( q );
1736 if( opt.cache_timeout < 0 )
1737 goto usage;
1738 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001739 else if( strcmp( p, "cookies" ) == 0 )
1740 {
1741 opt.cookies = atoi( q );
1742 if( opt.cookies < -1 || opt.cookies > 1)
1743 goto usage;
1744 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001745 else if( strcmp( p, "anti_replay" ) == 0 )
1746 {
1747 opt.anti_replay = atoi( q );
1748 if( opt.anti_replay < 0 || opt.anti_replay > 1)
1749 goto usage;
1750 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001751 else if( strcmp( p, "badmac_limit" ) == 0 )
1752 {
1753 opt.badmac_limit = atoi( q );
1754 if( opt.badmac_limit < 0 )
1755 goto usage;
1756 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001757 else if( strcmp( p, "hs_timeout" ) == 0 )
1758 {
1759 if( ( p = strchr( q, '-' ) ) == NULL )
1760 goto usage;
1761 *p++ = '\0';
1762 opt.hs_to_min = atoi( q );
1763 opt.hs_to_max = atoi( p );
1764 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1765 goto usage;
1766 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001767 else if( strcmp( p, "mtu" ) == 0 )
1768 {
1769 opt.dtls_mtu = atoi( q );
1770 if( opt.dtls_mtu < 0 )
1771 goto usage;
1772 }
Hanno Beckere7675d02018-08-14 13:28:56 +01001773 else if( strcmp( p, "dgram_packing" ) == 0 )
1774 {
1775 opt.dgram_packing = atoi( q );
1776 if( opt.dgram_packing != 0 &&
1777 opt.dgram_packing != 1 )
1778 {
1779 goto usage;
1780 }
1781 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001782 else if( strcmp( p, "sni" ) == 0 )
1783 {
1784 opt.sni = q;
1785 }
Andres Amaya Garciabfa3e092018-10-16 21:08:38 +01001786 else if( strcmp( p, "query_config" ) == 0 )
1787 {
1788 return query_config( q );
1789 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001790 else
1791 goto usage;
1792 }
1793
Hanno Becker16970d22017-10-10 15:56:37 +01001794 /* Event-driven IO is incompatible with the above custom
1795 * receive and send functions, as the polling builds on
1796 * refers to the underlying net_context. */
1797 if( opt.event == 1 && opt.nbio != 1 )
1798 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01001799 mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
Hanno Becker16970d22017-10-10 15:56:37 +01001800 opt.nbio = 1;
1801 }
1802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803#if defined(MBEDTLS_DEBUG_C)
1804 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001805#endif
Andrzej Kurekda4029d2018-06-20 07:07:55 -04001806 buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001807 if( buf == NULL )
1808 {
Andrzej Kurek755890f2018-06-20 08:17:04 -04001809 mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
Andrzej Kurek30e731d2017-10-12 13:50:29 +02001810 ret = 3;
1811 goto exit;
1812 }
Paul Bakkerc73079a2014-04-25 16:34:30 +02001813
Paul Bakkerc1516be2013-06-29 16:01:32 +02001814 if( opt.force_ciphersuite[0] > 0 )
1815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001817 ciphersuite_info =
1818 mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001819
Paul Bakker5b55b792013-07-19 13:43:43 +02001820 if( opt.max_version != -1 &&
1821 ciphersuite_info->min_minor_ver > opt.max_version )
1822 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001823 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakker5b55b792013-07-19 13:43:43 +02001824 ret = 2;
1825 goto usage;
1826 }
1827 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001828 ciphersuite_info->max_minor_ver < opt.min_version )
1829 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01001830 mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001831 ret = 2;
1832 goto usage;
1833 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001834
1835 /* If we select a version that's not supported by
1836 * this suite, then there will be no common ciphersuite... */
1837 if( opt.max_version == -1 ||
1838 opt.max_version > ciphersuite_info->max_minor_ver )
1839 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001840 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001841 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001842 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001843 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001844 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001845 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1847 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1848 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001849 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001850
1851 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001857 ret = 2;
1858 goto usage;
1859 }
1860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001862 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001863 }
1864
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001865 if( opt.version_suites != NULL )
1866 {
1867 const char *name[4] = { 0 };
1868
1869 /* Parse 4-element coma-separated list */
1870 for( i = 0, p = (char *) opt.version_suites;
1871 i < 4 && *p != '\0';
1872 i++ )
1873 {
1874 name[i] = p;
1875
1876 /* Terminate the current string and move on to next one */
1877 while( *p != ',' && *p != '\0' )
1878 p++;
1879 if( *p == ',' )
1880 *p++ = '\0';
1881 }
1882
1883 if( i != 4 )
1884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 mbedtls_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001886 ret = 1;
1887 goto exit;
1888 }
1889
1890 memset( version_suites, 0, sizeof( version_suites ) );
1891
1892 /* Get the suites identifiers from their name */
1893 for( i = 0; i < 4; i++ )
1894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001896
1897 if( version_suites[i][0] == 0 )
1898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001900 ret = 2;
1901 goto usage;
1902 }
1903 }
1904 }
1905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001907 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001908 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001909 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001910 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 mbedtls_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001913 goto exit;
1914 }
1915
1916 if( opt.psk_list != NULL )
1917 {
1918 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 mbedtls_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001921 goto exit;
1922 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001923 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001925
Hanno Beckere6706e62017-05-15 16:05:15 +01001926#if defined(MBEDTLS_ECP_C)
1927 if( opt.curves != NULL )
1928 {
1929 p = (char *) opt.curves;
1930 i = 0;
1931
1932 if( strcmp( p, "none" ) == 0 )
1933 {
1934 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1935 }
1936 else if( strcmp( p, "default" ) != 0 )
1937 {
1938 /* Leave room for a final NULL in curve list */
Hanno Becker8651a432017-06-09 16:13:22 +01001939 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001940 {
1941 q = p;
1942
1943 /* Terminate the current string */
1944 while( *p != ',' && *p != '\0' )
1945 p++;
1946 if( *p == ',' )
1947 *p++ = '\0';
1948
1949 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1950 {
1951 curve_list[i++] = curve_cur->grp_id;
1952 }
1953 else
1954 {
1955 mbedtls_printf( "unknown curve %s\n", q );
1956 mbedtls_printf( "supported curves: " );
1957 for( curve_cur = mbedtls_ecp_curve_list();
1958 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1959 curve_cur++ )
1960 {
1961 mbedtls_printf( "%s ", curve_cur->name );
1962 }
1963 mbedtls_printf( "\n" );
1964 goto exit;
1965 }
1966 }
1967
1968 mbedtls_printf("Number of curves: %d\n", i );
1969
Hanno Becker8651a432017-06-09 16:13:22 +01001970 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Beckere6706e62017-05-15 16:05:15 +01001971 {
Hanno Becker8651a432017-06-09 16:13:22 +01001972 mbedtls_printf( "curves list too long, maximum %d",
1973 CURVE_LIST_SIZE - 1 );
Hanno Beckere6706e62017-05-15 16:05:15 +01001974 goto exit;
1975 }
1976
1977 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1978 }
1979 }
1980#endif /* MBEDTLS_ECP_C */
1981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001983 if( opt.alpn_string != NULL )
1984 {
1985 p = (char *) opt.alpn_string;
1986 i = 0;
1987
1988 /* Leave room for a final NULL in alpn_list */
Hanno Becker8651a432017-06-09 16:13:22 +01001989 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001990 {
1991 alpn_list[i++] = p;
1992
1993 /* Terminate the current string and move on to next one */
1994 while( *p != ',' && *p != '\0' )
1995 p++;
1996 if( *p == ',' )
1997 *p++ = '\0';
1998 }
1999 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002001
Paul Bakkerfbb17802013-04-17 19:10:21 +02002002 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002003 * 0. Initialize the RNG and the session data
2004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002006 fflush( stdout );
2007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 mbedtls_entropy_init( &entropy );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002009 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
2010 &entropy, (const unsigned char *) pers,
2011 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002012 {
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01002013 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
2014 -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002015 goto exit;
2016 }
2017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002021 /*
2022 * 1.1. Load the trusted CA
2023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002025 fflush( stdout );
2026
Hanno Becker7ae36e42019-03-05 16:22:07 +00002027 if( strcmp( opt.ca_path, "none" ) == 0 ||
2028 strcmp( opt.ca_file, "none" ) == 0 )
2029 {
2030 ret = 0;
2031 }
2032 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002034 if( strlen( opt.ca_path ) )
Hanno Becker7ae36e42019-03-05 16:22:07 +00002035 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002036 else if( strlen( opt.ca_file ) )
Hanno Becker7ae36e42019-03-05 16:22:07 +00002037 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02002038 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040#if defined(MBEDTLS_CERTS_C)
Hanno Becker38566cc2019-02-01 08:13:19 +00002041 {
2042#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045 ret = mbedtls_x509_crt_parse( &cacert,
2046 (const unsigned char *) mbedtls_test_cas[i],
2047 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01002048 if( ret != 0 )
2049 break;
2050 }
Hanno Becker38566cc2019-02-01 08:13:19 +00002051 if( ret == 0 )
2052#endif /* MBEDTLS_PEM_PARSE_C */
2053 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
2054 {
2055 ret = mbedtls_x509_crt_parse_der( &cacert,
2056 (const unsigned char *) mbedtls_test_cas_der[i],
2057 mbedtls_test_cas_der_len[i] );
2058 if( ret != 0 )
2059 break;
2060 }
2061 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002062#else
2063 {
2064 ret = 1;
Hanno Beckerc258c442018-12-05 16:49:42 +00002065 mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002066 }
Hanno Becker38566cc2019-02-01 08:13:19 +00002067#endif /* MBEDTLS_CERTS_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002068 if( ret < 0 )
2069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002071 goto exit;
2072 }
2073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002075
2076 /*
2077 * 1.2. Load own certificate and private key
2078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002080 fflush( stdout );
2081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002083 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002084 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002085 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002089 -ret );
2090 goto exit;
2091 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002092 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002093 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002094 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002095 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002099 goto exit;
2100 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002101 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002102 if( key_cert_init == 1 )
2103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002105 goto exit;
2106 }
2107
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002108 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002109 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002110 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002114 -ret );
2115 goto exit;
2116 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002117 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002118 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002119 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002120 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002124 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002125 goto exit;
2126 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002127 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002128 if( key_cert_init2 == 1 )
2129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002131 goto exit;
2132 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002133#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002134 if( key_cert_init == 0 &&
2135 strcmp( opt.crt_file, "none" ) != 0 &&
2136 strcmp( opt.key_file, "none" ) != 0 &&
2137 key_cert_init2 == 0 &&
2138 strcmp( opt.crt_file2, "none" ) != 0 &&
2139 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141#if !defined(MBEDTLS_CERTS_C)
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002142 mbedtls_printf( "Not certificated or key provided, and \nMBEDTLS_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02002143 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002144#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145#if defined(MBEDTLS_RSA_C)
2146 if( ( ret = mbedtls_x509_crt_parse( &srvcert,
2147 (const unsigned char *) mbedtls_test_srv_crt_rsa,
2148 mbedtls_test_srv_crt_rsa_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002149 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002150 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
2151 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002152 goto exit;
2153 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 if( ( ret = mbedtls_pk_parse_key( &pkey,
2155 (const unsigned char *) mbedtls_test_srv_key_rsa,
2156 mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002157 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002158 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
2159 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002160 goto exit;
2161 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002162 key_cert_init = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163#endif /* MBEDTLS_RSA_C */
2164#if defined(MBEDTLS_ECDSA_C)
2165 if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
2166 (const unsigned char *) mbedtls_test_srv_crt_ec,
2167 mbedtls_test_srv_crt_ec_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002168 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002169 mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n",
2170 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002171 goto exit;
2172 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 if( ( ret = mbedtls_pk_parse_key( &pkey2,
2174 (const unsigned char *) mbedtls_test_srv_key_ec,
2175 mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002176 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002177 mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n",
2178 -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002179 goto exit;
2180 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002181 key_cert_init2 = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182#endif /* MBEDTLS_ECDSA_C */
2183#endif /* MBEDTLS_CERTS_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02002184 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 mbedtls_printf( " ok\n" );
2187#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002190 if( opt.dhm_file != NULL )
2191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 mbedtls_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002193 fflush( stdout );
2194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002198 -ret );
2199 goto exit;
2200 }
2201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002203 }
2204#endif
2205
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002206#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002207 if( opt.sni != NULL )
2208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 mbedtls_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002210 fflush( stdout );
2211
2212 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
2213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002215 goto exit;
2216 }
2217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002219 }
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002220#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002221
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002222 /*
2223 * 2. Setup the listening TCP socket
2224 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02002225 mbedtls_printf( " . Bind on %s://%s:%s/ ...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002227 opt.server_addr ? opt.server_addr : "*",
2228 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002229 fflush( stdout );
2230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
2232 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
2233 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002236 goto exit;
2237 }
2238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002240
2241 /*
2242 * 3. Setup stuff
2243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002245 fflush( stdout );
2246
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02002247 if( ( ret = mbedtls_ssl_config_defaults( &conf,
2248 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02002249 opt.transport,
2250 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002251 {
2252 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
2253 goto exit;
2254 }
2255
Gilles Peskineef86ab22017-05-05 18:59:02 +02002256#if defined(MBEDTLS_X509_CRT_PARSE_C)
2257 /* The default algorithms profile disables SHA-1, but our tests still
2258 rely on it heavily. Hence we allow it here. A real-world server
2259 should use the default profile unless there is a good reason not to. */
Gilles Peskinebc70a182017-05-09 15:59:24 +02002260 if( opt.allow_sha1 > 0 )
2261 {
2262 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
2263 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
Gilles Peskine682df092017-05-11 19:01:11 +02002264 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
Gilles Peskinebc70a182017-05-09 15:59:24 +02002265 }
Gilles Peskineef86ab22017-05-05 18:59:02 +02002266#endif /* MBEDTLS_X509_CRT_PARSE_C */
2267
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01002268 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002269 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002270
Janos Follath4817e272017-04-10 13:44:33 +01002271 if( opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST )
2272 mbedtls_ssl_conf_cert_req_ca_list( &conf, opt.cert_req_ca_list );
2273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002275 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 +02002276 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002277
Hanno Beckere7675d02018-08-14 13:28:56 +01002278 if( opt.dgram_packing != DFL_DGRAM_PACKING )
Hanno Becker1841b0a2018-08-24 11:13:57 +01002279 mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02002281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002283 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002284 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002285 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002286 goto exit;
2287 };
Paul Bakker05decb22013-08-15 13:33:48 +02002288#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002291 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002292 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01002293#endif
2294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002296 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002297 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002298#endif
2299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002301 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002302 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002303#endif
2304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002306 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002307 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002308 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002309 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002310 goto exit;
2311 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002312#endif
2313
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002314 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
2315 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002318 if( opt.cache_max != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01002320
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002321 if( opt.cache_timeout != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01002323
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002324 mbedtls_ssl_conf_session_cache( &conf, &cache,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01002325 mbedtls_ssl_cache_get,
2326 mbedtls_ssl_cache_set );
Paul Bakker0a597072012-09-25 21:55:46 +00002327#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002330 if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002331 {
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002332 if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
2333 mbedtls_ctr_drbg_random, &ctr_drbg,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +02002334 MBEDTLS_CIPHER_AES_256_GCM,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002335 opt.ticket_timeout ) ) != 0 )
2336 {
2337 mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
2338 goto exit;
2339 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01002340
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002341 mbedtls_ssl_conf_session_tickets_cb( &conf,
2342 mbedtls_ssl_ticket_write,
2343 mbedtls_ssl_ticket_parse,
2344 &ticket_ctx );
2345 }
Paul Bakkera503a632013-08-14 13:48:06 +02002346#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348#if defined(MBEDTLS_SSL_PROTO_DTLS)
2349 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002352 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
2355 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002358 goto exit;
2359 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002360
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002361 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002362 &cookie_ctx );
2363 }
2364 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365#endif /* MBEDTLS_SSL_COOKIE_C */
2366#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002367 if( opt.cookies == 0 )
2368 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002369 mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002370 }
2371 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02002373 {
2374 ; /* Nothing to do */
2375 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002378 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002379 mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002380#endif
2381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02002383 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002384 mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002385#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002386 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02002388
Paul Bakker41c83d32013-03-20 14:39:14 +01002389 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002390 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002391
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002392#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00002393 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002394 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02002395#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002396
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002397 if( opt.version_suites != NULL )
2398 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002399 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 MBEDTLS_SSL_MAJOR_VERSION_3,
2401 MBEDTLS_SSL_MINOR_VERSION_0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002402 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 MBEDTLS_SSL_MAJOR_VERSION_3,
2404 MBEDTLS_SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002405 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_MAJOR_VERSION_3,
2407 MBEDTLS_SSL_MINOR_VERSION_2 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002408 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 MBEDTLS_SSL_MAJOR_VERSION_3,
2410 MBEDTLS_SSL_MINOR_VERSION_3 );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02002411 }
2412
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01002413 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002414 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002416 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002417
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02002418 if( opt.renego_delay != DFL_RENEGO_DELAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002419 mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002420
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002421 if( opt.renego_period != DFL_RENEGO_PERIOD )
2422 {
Andres AG692ad842017-01-19 16:30:57 +00002423 PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002424 mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01002425 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002426#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002429 if( strcmp( opt.ca_path, "none" ) != 0 &&
2430 strcmp( opt.ca_file, "none" ) != 0 )
2431 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002432 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01002433 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002434 if( key_cert_init )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002435 {
2436 mbedtls_pk_context *pk = &pkey;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002437#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002438 if( opt.async_private_delay1 >= 0 )
2439 {
Gilles Peskine44817442018-06-13 18:09:28 +02002440 ret = ssl_async_set_key( &ssl_async_keys, &srvcert, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002441 opt.async_private_delay1 );
2442 if( ret < 0 )
2443 {
2444 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2445 ret );
2446 goto exit;
2447 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002448 pk = NULL;
2449 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002450#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002451 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002452 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002453 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002454 goto exit;
2455 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002456 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02002457 if( key_cert_init2 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002458 {
2459 mbedtls_pk_context *pk = &pkey2;
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002460#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002461 if( opt.async_private_delay2 >= 0 )
2462 {
Gilles Peskine44817442018-06-13 18:09:28 +02002463 ret = ssl_async_set_key( &ssl_async_keys, &srvcert2, pk, 0,
Gilles Peskined6fbfde2018-04-30 10:23:56 +02002464 opt.async_private_delay2 );
2465 if( ret < 0 )
2466 {
2467 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2468 ret );
2469 goto exit;
2470 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002471 pk = NULL;
2472 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002473#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002474 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, pk ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002475 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002476 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02002477 goto exit;
2478 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002479 }
2480
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002481#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002482 if( opt.async_operations[0] != '-' )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002483 {
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002484 mbedtls_ssl_async_sign_t *sign = NULL;
2485 mbedtls_ssl_async_decrypt_t *decrypt = NULL;
Gilles Peskine12ab5d42018-04-24 12:32:04 +02002486 const char *r;
2487 for( r = opt.async_operations; *r; r++ )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002488 {
Gilles Peskine12ab5d42018-04-24 12:32:04 +02002489 switch( *r )
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002490 {
2491 case 'd':
2492 decrypt = ssl_async_decrypt;
2493 break;
2494 case 's':
2495 sign = ssl_async_sign;
2496 break;
2497 }
2498 }
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002499 ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
2500 - opt.async_private_error :
2501 opt.async_private_error );
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002502 ssl_async_keys.f_rng = mbedtls_ctr_drbg_random;
2503 ssl_async_keys.p_rng = &ctr_drbg;
2504 mbedtls_ssl_conf_async_private_cb( &conf,
Gilles Peskinefcca9d82018-01-12 13:47:48 +01002505 sign,
2506 decrypt,
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002507 ssl_async_resume,
2508 ssl_async_cancel,
2509 &ssl_async_keys );
2510 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002511#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002512#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +02002513
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002514#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002515 if( opt.sni != NULL )
Gilles Peskine166ce742018-04-30 10:30:49 +02002516 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002517 mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
Gilles Peskine166ce742018-04-30 10:30:49 +02002518#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
2519 if( opt.async_private_delay2 >= 0 )
2520 {
Gilles Peskinee2479892018-06-13 18:06:51 +02002521 sni_entry *cur;
2522 for( cur = sni_info; cur != NULL; cur = cur->next )
Gilles Peskine166ce742018-04-30 10:30:49 +02002523 {
Gilles Peskinee2479892018-06-13 18:06:51 +02002524 ret = ssl_async_set_key( &ssl_async_keys,
Gilles Peskine44817442018-06-13 18:09:28 +02002525 cur->cert, cur->key, 1,
Gilles Peskinee2479892018-06-13 18:06:51 +02002526 opt.async_private_delay2 );
2527 if( ret < 0 )
2528 {
2529 mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
2530 ret );
2531 goto exit;
2532 }
2533 cur->key = NULL;
Gilles Peskine166ce742018-04-30 10:30:49 +02002534 }
Gilles Peskine166ce742018-04-30 10:30:49 +02002535 }
2536#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
2537 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002538#endif
2539
Hanno Beckere6706e62017-05-15 16:05:15 +01002540#if defined(MBEDTLS_ECP_C)
2541 if( opt.curves != NULL &&
2542 strcmp( opt.curves, "default" ) != 0 )
2543 {
2544 mbedtls_ssl_conf_curves( &conf, curve_list );
2545 }
2546#endif
2547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002549 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
2550 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002551 ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002552 (const unsigned char *) opt.psk_identity,
2553 strlen( opt.psk_identity ) );
2554 if( ret != 0 )
2555 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002556 mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02002557 goto exit;
2558 }
2559 }
2560
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02002561 if( opt.psk_list != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002562 mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02002563#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565#if defined(MBEDTLS_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00002566 /*
2567 * Use different group than default DHM group
2568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002570 if( opt.dhm_file != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002571 ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002572#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002573 if( ret != 0 )
2574 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002575 mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02002576 goto exit;
2577 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002578#endif
2579
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002580 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002581 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00002582
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02002583 if( opt.max_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002584 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakkerc1516be2013-06-29 16:01:32 +02002585
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002586 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
2587 {
2588 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
2589 goto exit;
2590 }
2591
2592 if( opt.nbio == 2 )
2593 mbedtls_ssl_set_bio( &ssl, &client_fd, my_send, my_recv, NULL );
2594 else
2595 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02002596 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002597
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02002598#if defined(MBEDTLS_SSL_PROTO_DTLS)
2599 if( opt.dtls_mtu != DFL_DTLS_MTU )
2600 mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
2601#endif
2602
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002603#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002604 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
2605 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002606#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002609
2610reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002611#if !defined(_WIN32)
2612 if( received_sigterm )
2613 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01002614 mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" );
2615 if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT )
2616 ret = 0;
2617
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002618 goto exit;
2619 }
2620#endif
2621
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002622 if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
2623 {
2624 mbedtls_printf( " ! Client initiated reconnection from same port\n" );
2625 goto handshake;
2626 }
2627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002629 if( ret != 0 )
2630 {
2631 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632 mbedtls_strerror( ret, error_buf, 100 );
2633 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002634 }
2635#endif
2636
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002637 mbedtls_net_free( &client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 mbedtls_ssl_session_reset( &ssl );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002640
2641 /*
2642 * 3. Wait until a client connects
2643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644 mbedtls_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002645 fflush( stdout );
2646
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002647 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02002648 client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002649 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02002650#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002651 if( received_sigterm )
2652 {
Manuel Pégourié-Gonnard4fa619f2018-01-22 10:55:10 +01002653 mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" );
2654 if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED )
2655 ret = 0;
2656
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002657 goto exit;
2658 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02002659#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002662 goto exit;
2663 }
2664
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002665 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002666 ret = mbedtls_net_set_nonblock( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002667 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002668 ret = mbedtls_net_set_block( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002669 if( ret != 0 )
2670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002672 goto exit;
2673 }
2674
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002675 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2678 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002679 {
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02002680 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
2681 client_ip, cliip_len ) ) != 0 )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002682 {
Hanno Beckerdf4180a2017-10-27 13:43:58 +01002683 mbedtls_printf( " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n",
2684 -ret );
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002685 goto exit;
2686 }
2687 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002689
Manuel Pégourié-Gonnard70905a72015-09-16 11:08:34 +02002690#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2691 if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
2692 {
2693 if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
2694 (const unsigned char *) opt.ecjpake_pw,
2695 strlen( opt.ecjpake_pw ) ) ) != 0 )
2696 {
2697 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
2698 goto exit;
2699 }
2700 }
2701#endif
2702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002704
2705 /*
2706 * 4. Handshake
2707 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002708handshake:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002710 fflush( stdout );
2711
Hanno Becker16970d22017-10-10 15:56:37 +01002712 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002713 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002714#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002715 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002716 ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL )
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002717 {
2718 mbedtls_printf( " cancelling on injected error\n" );
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002719 break;
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002720 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002721#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskineb44692f2018-04-24 12:18:19 +02002722
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02002723 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01002724 break;
2725
2726 /* For event-driven IO, wait for socket to become available */
2727 if( opt.event == 1 /* level triggered IO */ )
2728 {
2729#if defined(MBEDTLS_TIMING_C)
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002730 ret = idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002731#else
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002732 ret = idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002733#endif
Hanno Beckeradfa64f2018-03-15 11:35:07 +00002734 if( ret != 0 )
2735 goto reset;
Hanno Becker16970d22017-10-10 15:56:37 +01002736 }
Gilles Peskine9eb5e9a2018-01-05 21:15:57 +01002737 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 mbedtls_printf( " hello verification requested\n" );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002742 ret = 0;
2743 goto reset;
2744 }
2745 else if( ret != 0 )
2746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002748
2749#if defined(MBEDTLS_X509_CRT_PARSE_C)
2750 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2751 {
2752 char vrfy_buf[512];
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02002753 flags = mbedtls_ssl_get_verify_result( &ssl );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002754
2755 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
2756
2757 mbedtls_printf( "%s\n", vrfy_buf );
2758 }
2759#endif
2760
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002761#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine60ee4ca2018-01-08 11:28:05 +01002762 if( opt.async_private_error < 0 )
2763 /* Injected error only the first time round, to test reset */
2764 ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
2765#endif
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002766 goto reset;
2767 }
2768 else /* ret == 0 */
2769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
2771 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002772 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
2775 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002776 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002778
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002779#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2780 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
2781 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
2782#endif
2783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002785 if( opt.alpn_string != NULL )
2786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
2788 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002789 alp ? alp : "(none)" );
2790 }
2791#endif
2792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002794 /*
Ron Eldor2a47be52017-06-20 15:23:23 +03002795 * 5. Verify the client certificate
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002796 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002798
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002799 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002800 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002801 char vrfy_buf[512];
2802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 mbedtls_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002804
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002805 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002806
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002807 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002808 }
2809 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002811
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02002812 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002813 {
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02002814 char crt_buf[512];
2815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 mbedtls_printf( " . Peer certificate information ...\n" );
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02002817 mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818 mbedtls_ssl_get_peer_cert( &ssl ) );
Manuel Pégourié-Gonnard67557172015-07-02 11:15:48 +02002819 mbedtls_printf( "%s\n", crt_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002820 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002822
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02002823 if( opt.exchanges == 0 )
2824 goto close_notify;
2825
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00002826 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002827data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002828 /*
2829 * 6. Read the HTTP Request
2830 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831 mbedtls_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002832 fflush( stdout );
2833
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002834 /*
2835 * TLS and DTLS need different reading styles (stream vs datagram)
2836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002838 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002839 do
2840 {
2841 int terminated = 0;
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002842 len = opt.buffer_size - 1;
2843 memset( buf, 0, opt.buffer_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002845
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02002846 if( mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01002847 {
2848 if( opt.event == 1 /* level triggered IO */ )
2849 {
2850#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00002851 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002852#else
Hanno Becker197a91c2017-10-31 10:58:53 +00002853 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01002854#endif
2855 }
2856
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002857 continue;
Hanno Becker16970d22017-10-10 15:56:37 +01002858 }
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002859
2860 if( ret <= 0 )
2861 {
2862 switch( ret )
2863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2865 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002866 goto close_notify;
2867
2868 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869 case MBEDTLS_ERR_NET_CONN_RESET:
2870 mbedtls_printf( " connection was reset by peer\n" );
2871 ret = MBEDTLS_ERR_NET_CONN_RESET;
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002872 goto reset;
2873
2874 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002876 goto reset;
2877 }
2878 }
2879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880 if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002881 {
2882 len = ret;
2883 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884 mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002885
2886 /* End of message should be detected according to the syntax of the
2887 * application protocol (eg HTTP), just use a dummy test here. */
2888 if( buf[len - 1] == '\n' )
2889 terminated = 1;
2890 }
2891 else
2892 {
2893 int extra_len, ori_len;
2894 unsigned char *larger_buf;
2895
2896 ori_len = ret;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002897 extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002898
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002899 larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002900 if( larger_buf == NULL )
2901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 mbedtls_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002903 ret = 1;
2904 goto reset;
2905 }
2906
2907 memset( larger_buf, 0, ori_len + extra_len );
2908 memcpy( larger_buf, buf, ori_len );
2909
2910 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002912 if( ret != extra_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002916 ret = 1;
2917 goto reset;
2918 }
2919
2920 larger_buf[ori_len + extra_len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 mbedtls_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002922 ori_len + extra_len, ori_len, extra_len,
2923 (char *) larger_buf );
2924
2925 /* End of message should be detected according to the syntax of the
2926 * application protocol (eg HTTP), just use a dummy test here. */
2927 if( larger_buf[ori_len + extra_len - 1] == '\n' )
2928 terminated = 1;
2929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 mbedtls_free( larger_buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002931 }
2932
2933 if( terminated )
2934 {
2935 ret = 0;
2936 break;
2937 }
2938 }
2939 while( 1 );
2940 }
2941 else /* Not stream, so datagram */
2942 {
Andrzej Kurek30e731d2017-10-12 13:50:29 +02002943 len = opt.buffer_size - 1;
2944 memset( buf, 0, opt.buffer_size );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002945
Gilles Peskineb44692f2018-04-24 12:18:19 +02002946 do
Hanno Becker16970d22017-10-10 15:56:37 +01002947 {
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00002948 /* Without the call to `mbedtls_ssl_check_pending`, it might
2949 * happen that the client sends application data in the same
2950 * datagram as the Finished message concluding the handshake.
2951 * In this case, the application data would be ready to be
2952 * processed while the underlying transport wouldn't signal
2953 * any further incoming data.
2954 *
2955 * See the test 'Event-driven I/O: session-id resume, UDP packing'
2956 * in tests/ssl-opt.sh.
2957 */
2958
2959 /* For event-driven IO, wait for socket to become available */
2960 if( mbedtls_ssl_check_pending( &ssl ) == 0 &&
2961 opt.event == 1 /* level triggered IO */ )
2962 {
2963#if defined(MBEDTLS_TIMING_C)
2964 idle( &client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ );
2965#else
2966 idle( &client_fd, MBEDTLS_ERR_SSL_WANT_READ );
2967#endif
2968 }
2969
Hanno Becker16970d22017-10-10 15:56:37 +01002970 ret = mbedtls_ssl_read( &ssl, buf, len );
2971
Hanno Beckerddc3ebb2018-03-13 11:39:09 +00002972 /* Note that even if `mbedtls_ssl_check_pending` returns true,
2973 * it can happen that the subsequent call to `mbedtls_ssl_read`
2974 * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
2975 * might be discarded (e.g. because they are retransmissions). */
Hanno Becker16970d22017-10-10 15:56:37 +01002976 }
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02002977 while( mbedtls_status_is_ssl_in_progress( ret ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002978
2979 if( ret <= 0 )
2980 {
2981 switch( ret )
2982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2984 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002985 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002986 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002987
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002988 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002990 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002991 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002992 }
2993
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002994 len = ret;
2995 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002997 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002998 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002999
3000 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003001 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02003002 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003003 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00003005 if( opt.renegotiate && exchanges_left == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007 mbedtls_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003008 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003011 {
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003012 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003015 goto reset;
3016 }
Hanno Becker16970d22017-10-10 15:56:37 +01003017
3018 /* For event-driven IO, wait for socket to become available */
3019 if( opt.event == 1 /* level triggered IO */ )
3020 {
3021#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003022 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003023#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003024 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003025#endif
3026 }
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003027 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003030 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02003032
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003033 /*
3034 * 7. Write the 200 Response
3035 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 mbedtls_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003037 fflush( stdout );
3038
3039 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003041
Andrzej Kurek30e731d2017-10-12 13:50:29 +02003042 /* Add padding to the response to reach opt.response_size in length */
3043 if( opt.response_size != DFL_RESPONSE_SIZE &&
3044 len < opt.response_size )
3045 {
3046 memset( buf + len, 'B', opt.response_size - len );
3047 len += opt.response_size - len;
3048 }
3049
3050 /* Truncate if response size is smaller than the "natural" size */
3051 if( opt.response_size != DFL_RESPONSE_SIZE &&
3052 len > opt.response_size )
3053 {
3054 len = opt.response_size;
3055
3056 /* Still end with \r\n unless that's really not possible */
3057 if( len >= 2 ) buf[len - 2] = '\r';
3058 if( len >= 1 ) buf[len - 1] = '\n';
3059 }
3060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003062 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003063 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003066 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070 mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003071 goto reset;
3072 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003073
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003074 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003077 goto reset;
3078 }
Hanno Becker16970d22017-10-10 15:56:37 +01003079
3080 /* For event-driven IO, wait for socket to become available */
3081 if( opt.event == 1 /* level triggered IO */ )
3082 {
3083#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003084 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003085#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003086 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003087#endif
3088 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003089 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003090 }
3091 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003092 else /* Not stream, so datagram */
3093 {
Hanno Becker16970d22017-10-10 15:56:37 +01003094 while( 1 )
3095 {
3096 ret = mbedtls_ssl_write( &ssl, buf, len );
3097
Gilles Peskinea36ac4f2018-04-26 08:05:02 +02003098 if( ! mbedtls_status_is_ssl_in_progress( ret ) )
Hanno Becker16970d22017-10-10 15:56:37 +01003099 break;
3100
3101 /* For event-driven IO, wait for socket to become available */
3102 if( opt.event == 1 /* level triggered IO */ )
3103 {
3104#if defined(MBEDTLS_TIMING_C)
Hanno Becker197a91c2017-10-31 10:58:53 +00003105 idle( &client_fd, &timer, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003106#else
Hanno Becker197a91c2017-10-31 10:58:53 +00003107 idle( &client_fd, ret );
Hanno Becker16970d22017-10-10 15:56:37 +01003108#endif
3109 }
3110 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003111
3112 if( ret < 0 )
3113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02003115 goto reset;
3116 }
3117
3118 frags = 1;
3119 written = ret;
3120 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003121
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02003122 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123 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 +01003124 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01003125
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003126 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003127 * 7b. Continue doing data exchanges?
3128 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00003129 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02003130 goto data_exchange;
3131
3132 /*
3133 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003134 */
3135close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01003137
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003138 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003140 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02003141 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143 mbedtls_printf( " done\n" );
Rich Evansf90016a2015-01-19 14:26:37 +00003144
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003145 goto reset;
3146
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02003147 /*
3148 * Cleanup and exit
3149 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003150exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003152 if( ret != 0 )
3153 {
3154 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 mbedtls_strerror( ret, error_buf, 100 );
3156 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003157 }
3158#endif
3159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160 mbedtls_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003161 fflush( stdout );
3162
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02003163 mbedtls_net_free( &client_fd );
3164 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +02003165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3167 mbedtls_dhm_free( &dhm );
Paul Bakkera317a982014-06-18 16:44:11 +02003168#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169#if defined(MBEDTLS_X509_CRT_PARSE_C)
3170 mbedtls_x509_crt_free( &cacert );
3171 mbedtls_x509_crt_free( &srvcert );
3172 mbedtls_pk_free( &pkey );
3173 mbedtls_x509_crt_free( &srvcert2 );
3174 mbedtls_pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02003175#endif
Gilles Peskine44817442018-06-13 18:09:28 +02003176#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3177 for( i = 0; (size_t) i < ssl_async_keys.slots_used; i++ )
3178 {
3179 if( ssl_async_keys.slots[i].pk_owned )
3180 {
3181 mbedtls_pk_free( ssl_async_keys.slots[i].pk );
3182 mbedtls_free( ssl_async_keys.slots[i].pk );
3183 ssl_async_keys.slots[i].pk = NULL;
3184 }
3185 }
3186#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02003187#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01003188 sni_free( sni_info );
3189#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003191 psk_free( psk_info );
3192#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
3194 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02003195#endif
Paul Bakkered27a042013-04-18 22:46:23 +02003196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003198 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 mbedtls_ctr_drbg_free( &ctr_drbg );
3200 mbedtls_entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202#if defined(MBEDTLS_SSL_CACHE_C)
3203 mbedtls_ssl_cache_free( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00003204#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003205#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3206 mbedtls_ssl_ticket_free( &ticket_ctx );
3207#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208#if defined(MBEDTLS_SSL_COOKIE_C)
3209 mbedtls_ssl_cookie_free( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02003210#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003211
Hanno Becker095d9cf2018-10-09 12:39:13 +01003212 mbedtls_free( buf );
3213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
3215#if defined(MBEDTLS_MEMORY_DEBUG)
3216 mbedtls_memory_buffer_alloc_status();
Paul Bakker82024bf2013-07-04 11:52:32 +02003217#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +02003219#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02003220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 mbedtls_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01003222
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003223#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003225 fflush( stdout ); getchar();
3226#endif
3227
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02003228 // Shell can not handle large exit numbers -> 1 for errors
3229 if( ret < 0 )
3230 ret = 1;
3231
Paul Bakkerb60b95f2012-09-25 09:05:17 +00003232 return( ret );
3233}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
3235 MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02003236 MBEDTLS_CTR_DRBG_C */