blob: 40140349a56bde68ea0d51f6ddc128fcca4c2b0d [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>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020033#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#define mbedtls_fprintf fprintf
35#define mbedtls_printf printf
Rich Evans18b78c72015-02-11 14:06:19 +000036#endif
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020037
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020038#if !defined(MBEDTLS_ENTROPY_C) || \
39 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_C) || \
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020040 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020041int main( void )
42{
43 mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
44 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +020045 "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020046 return( 0 );
47}
48#else
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010049
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/net.h"
51#include "mbedtls/ssl.h"
52#include "mbedtls/entropy.h"
53#include "mbedtls/ctr_drbg.h"
54#include "mbedtls/certs.h"
55#include "mbedtls/x509.h"
56#include "mbedtls/error.h"
57#include "mbedtls/debug.h"
Manuel Pégourié-Gonnard56273da2015-05-26 12:19:45 +020058#include "mbedtls/timing.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000059
Rich Evans18b78c72015-02-11 14:06:19 +000060#include <stdio.h>
61#include <stdlib.h>
62#include <string.h>
Andres AG9b1927b2017-01-19 16:30:57 +000063#include <stdint.h>
Andres AG0ac13922017-02-08 14:05:57 +000064
65#if !defined(_MSC_VER)
Andres AG9b1927b2017-01-19 16:30:57 +000066#include <inttypes.h>
Andres AG0ac13922017-02-08 14:05:57 +000067#endif
Rich Evans18b78c72015-02-11 14:06:19 +000068
69#if !defined(_WIN32)
70#include <signal.h>
71#endif
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000074#include "mbedtls/ssl_cache.h"
Paul Bakker0a597072012-09-25 21:55:46 +000075#endif
76
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020077#if defined(MBEDTLS_SSL_TICKET_C)
78#include "mbedtls/ssl_ticket.h"
79#endif
80
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000082#include "mbedtls/ssl_cookie.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020083#endif
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000086#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker82024bf2013-07-04 11:52:32 +020087#endif
88
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +020089#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO)
90#define SNI_OPTION
91#endif
92
93#if defined(_WIN32)
94#include <windows.h>
95#endif
96
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +010097#define DFL_SERVER_ADDR NULL
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020098#define DFL_SERVER_PORT "4433"
Andrzej Kurekf4f59c02018-06-28 03:42:01 -040099#define DFL_RESPONSE_SIZE -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000100#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100101#define DFL_NBIO 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200102#define DFL_READ_TIMEOUT 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000103#define DFL_CA_FILE ""
104#define DFL_CA_PATH ""
105#define DFL_CRT_FILE ""
106#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200107#define DFL_CRT_FILE2 ""
108#define DFL_KEY_FILE2 ""
Paul Bakkerfbb17802013-04-17 19:10:21 +0200109#define DFL_PSK ""
110#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200111#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000112#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200113#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100115#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100116#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200117#define DFL_RENEGO_DELAY -2
Andres AG9b1927b2017-01-19 16:30:57 +0000118#define DFL_RENEGO_PERIOD ( (uint64_t)-1 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200119#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200120#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200121#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +0000122#define DFL_ARC4 -1
Gilles Peskineae765992017-05-09 15:59:24 +0200123#define DFL_SHA1 -1
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100124#define DFL_AUTH_MODE -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100126#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200128#define DFL_TICKET_TIMEOUT 86400
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100129#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100130#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100131#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200132#define DFL_ALPN_STRING NULL
Hanno Becker61c0c702017-05-15 16:05:15 +0100133#define DFL_CURVES NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200134#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200136#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200137#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200138#define DFL_HS_TO_MIN 0
139#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200140#define DFL_BADMAC_LIMIT -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200141#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100142#define DFL_ETM -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000143
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200144#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
145 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
146 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
147 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
148 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
149 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
150 "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 +0200151
Paul Bakker8e714d72013-07-18 11:05:13 +0200152/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
153 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000154#define HTTP_RESPONSE \
155 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000156 "<h2>mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200157 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000158
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200159/*
160 * Size of the basic I/O buffer. Able to hold our default response.
161 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 * 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 +0200163 * if you change this value to something outside the range <= 100 or > 500
164 */
Andrzej Kurekf4f59c02018-06-28 03:42:01 -0400165#define DFL_IO_BUF_LEN 200
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_X509_CRT_PARSE_C)
168#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000169#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100170 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
171 " default: \"\" (pre-loaded)\n" \
172 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
173 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
174 " 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 +0200175 " default: see note after key_file2\n" \
176 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200177 " 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 +0200178 " default: see note after key_file2\n" \
179 " key_file2=%%s default: see note below\n" \
180 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200181 " preloaded certificate(s) and key(s) are used if available\n" \
182 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
183 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000184#else
185#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200186 "\n" \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 " No file operations available (MBEDTLS_FS_IO not defined)\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200188 "\n"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#endif /* MBEDTLS_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200190#else
191#define USAGE_IO ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200195#define USAGE_PSK \
196 " psk=%%s default: \"\" (in hex, without 0x)\n" \
197 " psk_identity=%%s default: \"Client_identity\"\n"
198#else
199#define USAGE_PSK ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Paul Bakkera503a632013-08-14 13:48:06 +0200203#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100204 " tickets=%%d default: 1 (enabled)\n" \
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200205 " ticket_timeout=%%d default: 86400 (one day)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200206#else
207#define USAGE_TICKETS ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakkera503a632013-08-14 13:48:06 +0200209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100211#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100212 " cache_max=%%d default: cache default (50)\n" \
213 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100214#else
215#define USAGE_CACHE ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#endif /* MBEDTLS_SSL_CACHE_C */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100217
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200218#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100219#define USAGE_SNI \
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200220 " sni=%%s name1,cert1,key1,ca1,crl1,auth1[,...]\n" \
221 " default: disabled\n"
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100222#else
223#define USAGE_SNI ""
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200224#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Paul Bakker05decb22013-08-15 13:33:48 +0200227#define USAGE_MAX_FRAG_LEN \
228 " max_frag_len=%%d default: 16384 (tls default)\n" \
229 " options: 512, 1024, 2048, 4096\n"
230#else
231#define USAGE_MAX_FRAG_LEN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker05decb22013-08-15 13:33:48 +0200233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100235#define USAGE_TRUNC_HMAC \
236 " trunc_hmac=%%d default: library default\n"
237#else
238#define USAGE_TRUNC_HMAC ""
239#endif
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200242#define USAGE_ALPN \
243 " alpn=%%s default: \"\" (disabled)\n" \
244 " example: spdy/1,http/1.1\n"
245#else
246#define USAGE_ALPN ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200250#define USAGE_COOKIES \
251 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200252 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200253#else
254#define USAGE_COOKIES ""
255#endif
256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200258#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200259 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200260#else
261#define USAGE_ANTI_REPLAY ""
262#endif
263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200265#define USAGE_BADMAC_LIMIT \
266 " badmac_limit=%%d default: (library default: disabled)\n"
267#else
268#define USAGE_BADMAC_LIMIT ""
269#endif
270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200272#define USAGE_DTLS \
273 " dtls=%%d default: 0 (TLS)\n" \
274 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
275 " range of DTLS handshake timeouts in millisecs\n"
276#else
277#define USAGE_DTLS ""
278#endif
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200281#define USAGE_EMS \
282 " extended_ms=0/1 default: (library default: on)\n"
283#else
284#define USAGE_EMS ""
285#endif
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100288#define USAGE_ETM \
289 " etm=0/1 default: (library default: on)\n"
290#else
291#define USAGE_ETM ""
292#endif
293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100295#define USAGE_RENEGO \
296 " renegotiation=%%d default: 0 (disabled)\n" \
297 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100298 " renego_delay=%%d default: -2 (library default)\n" \
Andres AG9b1927b2017-01-19 16:30:57 +0000299 " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100300#else
301#define USAGE_RENEGO ""
302#endif
303
Hanno Becker61c0c702017-05-15 16:05:15 +0100304#if defined(MBEDTLS_ECP_C)
305#define USAGE_CURVES \
306 " curves=a,b,c,d default: \"default\" (library default)\n" \
307 " example: \"secp521r1,brainpoolP512r1\"\n" \
308 " - use \"none\" for empty list\n" \
309 " - see mbedtls_ecp_curve_list()\n" \
310 " for acceptable curve names\n"
311#else
312#define USAGE_CURVES ""
313#endif
314
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000315#define USAGE \
316 "\n usage: ssl_server2 param=<>...\n" \
317 "\n acceptable parameters:\n" \
Ron Eldorbd257842017-09-26 11:29:11 +0300318 " server_addr=%%s default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000319 " server_port=%%d default: 4433\n" \
320 " debug_level=%%d default: 0 (disabled)\n" \
Andrzej Kurekf4f59c02018-06-28 03:42:01 -0400321 " buffer_size=%%d default: 200 \n" \
322 " (minimum: 1, max: 16385)\n" \
323 " response_size=%%d default: about 152 (basic response)\n" \
324 " (minimum: 0, max: 16384)\n" \
325 " increases buffer_size if bigger\n"\
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100326 " nbio=%%d default: 0 (blocking I/O)\n" \
327 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard22311ae2015-09-09 11:22:58 +0200328 " read_timeout=%%d default: 0 ms (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100329 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200330 USAGE_DTLS \
331 USAGE_COOKIES \
332 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200333 USAGE_BADMAC_LIMIT \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200334 "\n" \
Manuel Pégourié-Gonnardee6139c2015-05-07 10:18:26 +0100335 " auth_mode=%%s default: (library default: none)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100336 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000337 USAGE_IO \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100338 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100339 "\n" \
340 USAGE_PSK \
341 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100342 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100343 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200344 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200345 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100346 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100347 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100348 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100349 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200350 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200351 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100352 USAGE_ETM \
Hanno Becker61c0c702017-05-15 16:05:15 +0100353 USAGE_CURVES \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100354 "\n" \
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +0100355 " arc4=%%d default: (library default: 0)\n" \
Gilles Peskineae765992017-05-09 15:59:24 +0200356 " allow_sha1=%%d default: 0\n" \
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +0200357 " min_version=%%s default: (library default: tls1)\n" \
358 " max_version=%%s default: (library default: tls1_2)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200359 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100360 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200361 "\n" \
362 " version_suites=a,b,c,d per-version ciphersuites\n" \
363 " in order from ssl3 to tls1_2\n" \
364 " default: all enabled\n" \
365 " force_ciphersuite=<name> default: all enabled\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000366 " acceptable ciphersuite names:\n"
367
Andres AG9b1927b2017-01-19 16:30:57 +0000368
Hanno Becker46a16292017-06-09 16:13:22 +0100369#define ALPN_LIST_SIZE 10
370#define CURVE_LIST_SIZE 20
371
Andres AG9b1927b2017-01-19 16:30:57 +0000372#define PUT_UINT64_BE(out_be,in_le,i) \
373{ \
374 (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \
375 (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \
376 (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \
377 (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \
378 (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \
379 (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \
380 (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \
381 (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \
382}
383
Rich Evans85b05ec2015-02-12 11:37:29 +0000384/*
385 * global options
386 */
387struct options
388{
389 const char *server_addr; /* address on which the ssl service runs */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200390 const char *server_port; /* port on which the ssl service runs */
Rich Evans85b05ec2015-02-12 11:37:29 +0000391 int debug_level; /* level of debugging */
392 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
Andrzej Kurekf4f59c02018-06-28 03:42:01 -0400394 int response_size; /* pad response with header to requested size */
395 uint16_t buffer_size; /* IO buffer size */
Rich Evans85b05ec2015-02-12 11:37:29 +0000396 const char *ca_file; /* the file with the CA certificate(s) */
397 const char *ca_path; /* the path with the CA certificate(s) reside */
398 const char *crt_file; /* the file with the server certificate */
399 const char *key_file; /* the file with the server key */
400 const char *crt_file2; /* the file with the 2nd server certificate */
401 const char *key_file2; /* the file with the 2nd server key */
402 const char *psk; /* the pre-shared key */
403 const char *psk_identity; /* the pre-shared key identity */
404 char *psk_list; /* list of PSK id/key pairs for callback */
405 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
406 const char *version_suites; /* per-version ciphersuites */
407 int renegotiation; /* enable / disable renegotiation */
408 int allow_legacy; /* allow legacy renegotiation */
409 int renegotiate; /* attempt renegotiation? */
410 int renego_delay; /* delay before enforcing renegotiation */
Andres AG9b1927b2017-01-19 16:30:57 +0000411 uint64_t renego_period; /* period for automatic renegotiation */
Rich Evans85b05ec2015-02-12 11:37:29 +0000412 int exchanges; /* number of data exchanges */
413 int min_version; /* minimum protocol version accepted */
414 int max_version; /* maximum protocol version accepted */
415 int arc4; /* flag for arc4 suites support */
Gilles Peskineae765992017-05-09 15:59:24 +0200416 int allow_sha1; /* flag for SHA-1 support */
Rich Evans85b05ec2015-02-12 11:37:29 +0000417 int auth_mode; /* verify mode for connection */
418 unsigned char mfl_code; /* code for maximum fragment length */
419 int trunc_hmac; /* accept truncated hmac? */
420 int tickets; /* enable / disable session tickets */
421 int ticket_timeout; /* session ticket lifetime */
422 int cache_max; /* max number of session cache entries */
423 int cache_timeout; /* expiration delay of session cache entries */
424 char *sni; /* string describing sni information */
Hanno Becker61c0c702017-05-15 16:05:15 +0100425 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000426 const char *alpn_string; /* ALPN supported protocols */
427 const char *dhm_file; /* the file with the DH parameters */
428 int extended_ms; /* allow negotiation of extended MS? */
429 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000430 int transport; /* TLS or DTLS? */
431 int cookies; /* Use cookies for DTLS? -1 to break them */
432 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
433 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
434 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
435 int badmac_limit; /* Limit of records with bad MAC */
Rich Evans85b05ec2015-02-12 11:37:29 +0000436} opt;
437
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200438static void my_debug( void *ctx, int level,
439 const char *file, int line,
440 const char *str )
Rich Evans85b05ec2015-02-12 11:37:29 +0000441{
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200442 const char *p, *basename;
Rich Evans85b05ec2015-02-12 11:37:29 +0000443
Manuel Pégourié-Gonnard052f2882015-07-01 11:50:23 +0200444 /* Extract basename from file */
445 for( p = basename = file; *p != '\0'; p++ )
446 if( *p == '/' || *p == '\\' )
447 basename = p + 1;
448
449 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
Rich Evans85b05ec2015-02-12 11:37:29 +0000450 fflush( (FILE *) ctx );
451}
452
453/*
454 * Test recv/send functions that make sure each try returns
455 * WANT_READ/WANT_WRITE at least once before sucesseding
456 */
457static int my_recv( void *ctx, unsigned char *buf, size_t len )
458{
459 static int first_try = 1;
460 int ret;
461
462 if( first_try )
463 {
464 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100465 return( MBEDTLS_ERR_SSL_WANT_READ );
Rich Evans85b05ec2015-02-12 11:37:29 +0000466 }
467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 ret = mbedtls_net_recv( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100469 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
Rich Evans85b05ec2015-02-12 11:37:29 +0000470 first_try = 1; /* Next call will be a new operation */
471 return( ret );
472}
473
474static int my_send( void *ctx, const unsigned char *buf, size_t len )
475{
476 static int first_try = 1;
477 int ret;
478
479 if( first_try )
480 {
481 first_try = 0;
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100482 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Rich Evans85b05ec2015-02-12 11:37:29 +0000483 }
484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 ret = mbedtls_net_send( ctx, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100486 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Rich Evans85b05ec2015-02-12 11:37:29 +0000487 first_try = 1; /* Next call will be a new operation */
488 return( ret );
489}
490
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200491/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200492 * Return authmode from string, or -1 on error
493 */
494static int get_auth_mode( const char *s )
495{
496 if( strcmp( s, "none" ) == 0 )
497 return( MBEDTLS_SSL_VERIFY_NONE );
498 if( strcmp( s, "optional" ) == 0 )
499 return( MBEDTLS_SSL_VERIFY_OPTIONAL );
500 if( strcmp( s, "required" ) == 0 )
501 return( MBEDTLS_SSL_VERIFY_REQUIRED );
502
503 return( -1 );
504}
505
506/*
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200507 * Used by sni_parse and psk_parse to handle coma-separated lists
508 */
509#define GET_ITEM( dst ) \
510 dst = p; \
511 while( *p != ',' ) \
512 if( ++p > end ) \
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000513 goto error; \
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200514 *p++ = '\0';
515
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200516#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100517typedef struct _sni_entry sni_entry;
518
519struct _sni_entry {
520 const char *name;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_x509_crt *cert;
522 mbedtls_pk_context *key;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200523 mbedtls_x509_crt* ca;
524 mbedtls_x509_crl* crl;
525 int authmode;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100526 sni_entry *next;
527};
528
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100529void sni_free( sni_entry *head )
530{
531 sni_entry *cur = head, *next;
532
533 while( cur != NULL )
534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_x509_crt_free( cur->cert );
536 mbedtls_free( cur->cert );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_pk_free( cur->key );
539 mbedtls_free( cur->key );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100540
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200541 mbedtls_x509_crt_free( cur->ca );
542 mbedtls_free( cur->ca );
543
544 mbedtls_x509_crl_free( cur->crl );
545 mbedtls_free( cur->crl );
546
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100547 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_free( cur );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100549 cur = next;
550 }
551}
552
553/*
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200554 * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
555 * into a usable sni_entry list. For ca1, crl1, auth1, the special value
556 * '-' means unset. If ca1 is unset, then crl1 is ignored too.
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000557 *
558 * Modifies the input string! This is not production quality!
559 */
560sni_entry *sni_parse( char *sni_string )
561{
562 sni_entry *cur = NULL, *new = NULL;
563 char *p = sni_string;
564 char *end = p;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200565 char *crt_file, *key_file, *ca_file, *crl_file, *auth_str;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000566
567 while( *end != '\0' )
568 ++end;
569 *end = ',';
570
571 while( p <= end )
572 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200573 if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000574 {
575 sni_free( cur );
576 return( NULL );
577 }
578
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200579 GET_ITEM( new->name );
580 GET_ITEM( crt_file );
581 GET_ITEM( key_file );
582 GET_ITEM( ca_file );
583 GET_ITEM( crl_file );
584 GET_ITEM( auth_str );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000585
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200586 if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
587 ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200588 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 mbedtls_x509_crt_init( new->cert );
591 mbedtls_pk_init( new->key );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
594 mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000595 goto error;
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200596
597 if( strcmp( ca_file, "-" ) != 0 )
598 {
599 if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
600 goto error;
601
602 mbedtls_x509_crt_init( new->ca );
603
604 if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
605 goto error;
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000606 }
607
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200608 if( strcmp( crl_file, "-" ) != 0 )
609 {
610 if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
611 goto error;
612
613 mbedtls_x509_crl_init( new->crl );
614
615 if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
616 goto error;
617 }
618
619 if( strcmp( auth_str, "-" ) != 0 )
620 {
621 if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
622 goto error;
623 }
624 else
625 new->authmode = DFL_AUTH_MODE;
626
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000627 new->next = cur;
628 cur = new;
629 }
630
631 return( cur );
632
633error:
634 sni_free( new );
635 sni_free( cur );
636 return( NULL );
637}
638
639/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100640 * SNI callback.
641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100643 const unsigned char *name, size_t name_len )
644{
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200645 const sni_entry *cur = (const sni_entry *) p_info;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100646
647 while( cur != NULL )
648 {
649 if( name_len == strlen( cur->name ) &&
650 memcmp( name, cur->name, name_len ) == 0 )
651 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +0200652 if( cur->ca != NULL )
653 mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
654
655 if( cur->authmode != DFL_AUTH_MODE )
656 mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
657
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200658 return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100659 }
660
661 cur = cur->next;
662 }
663
664 return( -1 );
665}
666
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200667#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200670
671#define HEX2NUM( c ) \
672 if( c >= '0' && c <= '9' ) \
673 c -= '0'; \
674 else if( c >= 'a' && c <= 'f' ) \
675 c -= 'a' - 10; \
676 else if( c >= 'A' && c <= 'F' ) \
677 c -= 'A' - 10; \
678 else \
679 return( -1 );
680
681/*
682 * Convert a hex string to bytes.
683 * Return 0 on success, -1 on error.
684 */
685int unhexify( unsigned char *output, const char *input, size_t *olen )
686{
687 unsigned char c;
688 size_t j;
689
690 *olen = strlen( input );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 if( *olen % 2 != 0 || *olen / 2 > MBEDTLS_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200692 return( -1 );
693 *olen /= 2;
694
695 for( j = 0; j < *olen * 2; j += 2 )
696 {
697 c = input[j];
698 HEX2NUM( c );
699 output[ j / 2 ] = c << 4;
700
701 c = input[j + 1];
702 HEX2NUM( c );
703 output[ j / 2 ] |= c;
704 }
705
706 return( 0 );
707}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200708
709typedef struct _psk_entry psk_entry;
710
711struct _psk_entry
712{
713 const char *name;
714 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 unsigned char key[MBEDTLS_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200716 psk_entry *next;
717};
718
719/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000720 * Free a list of psk_entry's
721 */
722void psk_free( psk_entry *head )
723{
724 psk_entry *next;
725
726 while( head != NULL )
727 {
728 next = head->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_free( head );
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000730 head = next;
731 }
732}
733
734/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200735 * Parse a string of pairs name1,key1[,name2,key2[,...]]
736 * into a usable psk_entry list.
737 *
738 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200739 */
740psk_entry *psk_parse( char *psk_string )
741{
742 psk_entry *cur = NULL, *new = NULL;
743 char *p = psk_string;
744 char *end = p;
745 char *key_hex;
746
747 while( *end != '\0' )
748 ++end;
749 *end = ',';
750
751 while( p <= end )
752 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200753 if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +0000754 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200755
756 memset( new, 0, sizeof( psk_entry ) );
757
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200758 GET_ITEM( new->name );
759 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200760
761 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000762 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200763
764 new->next = cur;
765 cur = new;
766 }
767
768 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200769
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000770error:
771 psk_free( new );
772 psk_free( cur );
773 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200774}
775
776/*
777 * PSK callback
778 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200780 const unsigned char *name, size_t name_len )
781{
782 psk_entry *cur = (psk_entry *) p_info;
783
784 while( cur != NULL )
785 {
786 if( name_len == strlen( cur->name ) &&
787 memcmp( name, cur->name, name_len ) == 0 )
788 {
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +0100789 return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200790 }
791
792 cur = cur->next;
793 }
794
795 return( -1 );
796}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200798
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200799static mbedtls_net_context listen_fd, client_fd;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200800
801/* Interruption handler to ensure clean exit (for valgrind testing) */
802#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200803static int received_sigterm = 0;
804void term_handler( int sig )
805{
806 ((void) sig);
807 received_sigterm = 1;
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200808 mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
809 mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200810}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200811#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200812
Gilles Peskinedb56aca2017-05-11 19:01:11 +0200813#if defined(MBEDTLS_X509_CRT_PARSE_C)
814static int ssl_sig_hashes_for_test[] = {
815#if defined(MBEDTLS_SHA512_C)
816 MBEDTLS_MD_SHA512,
817 MBEDTLS_MD_SHA384,
818#endif
819#if defined(MBEDTLS_SHA256_C)
820 MBEDTLS_MD_SHA256,
821 MBEDTLS_MD_SHA224,
822#endif
823#if defined(MBEDTLS_SHA1_C)
824 /* Allow SHA-1 as we use it extensively in tests. */
825 MBEDTLS_MD_SHA1,
826#endif
827 MBEDTLS_MD_NONE
828};
829#endif /* MBEDTLS_X509_CRT_PARSE_C */
830
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000831int main( int argc, char *argv[] )
832{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +0000833 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200834 int version_suites[4][2];
Andrzej Kurekf4f59c02018-06-28 03:42:01 -0400835 unsigned char* buf = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
837 unsigned char psk[MBEDTLS_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +0200838 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +0200839 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +0200840#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200841 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +0200842 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200843 size_t cliip_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#if defined(MBEDTLS_SSL_COOKIE_C)
845 mbedtls_ssl_cookie_ctx cookie_ctx;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200846#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000847
Gilles Peskinedd57d752017-05-05 18:59:02 +0200848#if defined(MBEDTLS_X509_CRT_PARSE_C)
849 mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
850#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 mbedtls_entropy_context entropy;
852 mbedtls_ctr_drbg_context ctr_drbg;
853 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200854 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200855#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +0200856 mbedtls_timing_delay_context timer;
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +0200857#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100859 unsigned char renego_period[8] = { 0 };
860#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnarddb1cc762015-05-12 11:27:25 +0200862 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 mbedtls_x509_crt cacert;
864 mbedtls_x509_crt srvcert;
865 mbedtls_pk_context pkey;
866 mbedtls_x509_crt srvcert2;
867 mbedtls_pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200868 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200869#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
871 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200872#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873#if defined(MBEDTLS_SSL_CACHE_C)
874 mbedtls_ssl_cache_context cache;
Paul Bakker0a597072012-09-25 21:55:46 +0000875#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200876#if defined(MBEDTLS_SSL_SESSION_TICKETS)
877 mbedtls_ssl_ticket_context ticket_ctx;
878#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +0200879#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100880 sni_entry *sni_info = NULL;
881#endif
Hanno Becker61c0c702017-05-15 16:05:15 +0100882#if defined(MBEDTLS_ECP_C)
Hanno Becker46a16292017-06-09 16:13:22 +0100883 mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Becker61c0c702017-05-15 16:05:15 +0100884 const mbedtls_ecp_curve_info * curve_cur;
885#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker46a16292017-06-09 16:13:22 +0100887 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200888#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Paul Bakker82024bf2013-07-04 11:52:32 +0200890 unsigned char alloc_buf[100000];
891#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000892
893 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000894 char *p, *q;
895 const int *list;
896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
898 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker82024bf2013-07-04 11:52:32 +0200899#endif
900
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000901 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200902 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000903 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200904 mbedtls_net_init( &client_fd );
905 mbedtls_net_init( &listen_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200906 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200907 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200908 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#if defined(MBEDTLS_X509_CRT_PARSE_C)
910 mbedtls_x509_crt_init( &cacert );
911 mbedtls_x509_crt_init( &srvcert );
912 mbedtls_pk_init( &pkey );
913 mbedtls_x509_crt_init( &srvcert2 );
914 mbedtls_pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200915#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
917 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200918#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919#if defined(MBEDTLS_SSL_CACHE_C)
920 mbedtls_ssl_cache_init( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +0000921#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200922#if defined(MBEDTLS_SSL_SESSION_TICKETS)
923 mbedtls_ssl_ticket_init( &ticket_ctx );
924#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925#if defined(MBEDTLS_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200926 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200927#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928#if defined(MBEDTLS_SSL_COOKIE_C)
929 mbedtls_ssl_cookie_init( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200930#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000931
Paul Bakkerc1283d32014-08-18 11:05:51 +0200932#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100933 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200934 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100935 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +0200936#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200937
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000938 if( argc == 0 )
939 {
940 usage:
941 if( ret == 0 )
942 ret = 1;
943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 mbedtls_printf( USAGE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 list = mbedtls_ssl_list_ciphersuites();
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000947 while( *list )
948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200950 list++;
951 if( !*list )
952 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000954 list++;
955 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 mbedtls_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000957 goto exit;
958 }
959
Andrzej Kurekf4f59c02018-06-28 03:42:01 -0400960 opt.buffer_size = DFL_IO_BUF_LEN;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100961 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000962 opt.server_port = DFL_SERVER_PORT;
963 opt.debug_level = DFL_DEBUG_LEVEL;
Andrzej Kurekf4f59c02018-06-28 03:42:01 -0400964 opt.response_size = DFL_RESPONSE_SIZE;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100965 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200966 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000967 opt.ca_file = DFL_CA_FILE;
968 opt.ca_path = DFL_CA_PATH;
969 opt.crt_file = DFL_CRT_FILE;
970 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200971 opt.crt_file2 = DFL_CRT_FILE2;
972 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200973 opt.psk = DFL_PSK;
974 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200975 opt.psk_list = DFL_PSK_LIST;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000976 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200977 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000978 opt.renegotiation = DFL_RENEGOTIATION;
979 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100980 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200981 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100982 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200983 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000984 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200985 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100986 opt.arc4 = DFL_ARC4;
Gilles Peskineae765992017-05-09 15:59:24 +0200987 opt.allow_sha1 = DFL_SHA1;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100988 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200989 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100990 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200991 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100992 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100993 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100994 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100995 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200996 opt.alpn_string = DFL_ALPN_STRING;
Hanno Becker61c0c702017-05-15 16:05:15 +0100997 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200998 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100999 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001000 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001001 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001002 opt.hs_to_min = DFL_HS_TO_MIN;
1003 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001004 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001005 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001006 opt.etm = DFL_ETM;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001007
1008 for( i = 1; i < argc; i++ )
1009 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001010 p = argv[i];
1011 if( ( q = strchr( p, '=' ) ) == NULL )
1012 goto usage;
1013 *q++ = '\0';
1014
1015 if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001016 opt.server_port = q;
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001017 else if( strcmp( p, "server_addr" ) == 0 )
1018 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001019 else if( strcmp( p, "dtls" ) == 0 )
1020 {
1021 int t = atoi( q );
1022 if( t == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001024 else if( t == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +01001026 else
1027 goto usage;
1028 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001029 else if( strcmp( p, "debug_level" ) == 0 )
1030 {
1031 opt.debug_level = atoi( q );
1032 if( opt.debug_level < 0 || opt.debug_level > 65535 )
1033 goto usage;
1034 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001035 else if( strcmp( p, "nbio" ) == 0 )
1036 {
1037 opt.nbio = atoi( q );
1038 if( opt.nbio < 0 || opt.nbio > 2 )
1039 goto usage;
1040 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001041 else if( strcmp( p, "read_timeout" ) == 0 )
1042 opt.read_timeout = atoi( q );
Andrzej Kurekf4f59c02018-06-28 03:42:01 -04001043 else if( strcmp( p, "buffer_size" ) == 0 )
1044 {
1045 opt.buffer_size = atoi( q );
1046 if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
1047 goto usage;
1048 }
1049 else if( strcmp( p, "response_size" ) == 0 )
1050 {
1051 opt.response_size = atoi( q );
1052 if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
1053 goto usage;
1054 if( opt.buffer_size < opt.response_size )
1055 opt.buffer_size = opt.response_size;
1056 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001057 else if( strcmp( p, "ca_file" ) == 0 )
1058 opt.ca_file = q;
1059 else if( strcmp( p, "ca_path" ) == 0 )
1060 opt.ca_path = q;
1061 else if( strcmp( p, "crt_file" ) == 0 )
1062 opt.crt_file = q;
1063 else if( strcmp( p, "key_file" ) == 0 )
1064 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001065 else if( strcmp( p, "crt_file2" ) == 0 )
1066 opt.crt_file2 = q;
1067 else if( strcmp( p, "key_file2" ) == 0 )
1068 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001069 else if( strcmp( p, "dhm_file" ) == 0 )
1070 opt.dhm_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +02001071 else if( strcmp( p, "psk" ) == 0 )
1072 opt.psk = q;
1073 else if( strcmp( p, "psk_identity" ) == 0 )
1074 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001075 else if( strcmp( p, "psk_list" ) == 0 )
1076 opt.psk_list = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001077 else if( strcmp( p, "force_ciphersuite" ) == 0 )
1078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001080
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +02001081 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001082 {
1083 ret = 2;
1084 goto usage;
1085 }
1086 opt.force_ciphersuite[1] = 0;
1087 }
Hanno Becker61c0c702017-05-15 16:05:15 +01001088 else if( strcmp( p, "curves" ) == 0 )
1089 opt.curves = q;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001090 else if( strcmp( p, "version_suites" ) == 0 )
1091 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001092 else if( strcmp( p, "renegotiation" ) == 0 )
1093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 opt.renegotiation = (atoi( q )) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED :
1095 MBEDTLS_SSL_RENEGOTIATION_DISABLED;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001096 }
1097 else if( strcmp( p, "allow_legacy" ) == 0 )
1098 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001099 switch( atoi( q ) )
1100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 case -1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE; break;
1102 case 0: opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; break;
1103 case 1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION; break;
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001104 default: goto usage;
1105 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001106 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001107 else if( strcmp( p, "renegotiate" ) == 0 )
1108 {
1109 opt.renegotiate = atoi( q );
1110 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
1111 goto usage;
1112 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001113 else if( strcmp( p, "renego_delay" ) == 0 )
1114 {
1115 opt.renego_delay = atoi( q );
1116 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001117 else if( strcmp( p, "renego_period" ) == 0 )
1118 {
Andres AG0ac13922017-02-08 14:05:57 +00001119#if defined(_MSC_VER)
1120 opt.renego_period = _strtoui64( q, NULL, 10 );
1121#else
1122 if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
1123 goto usage;
1124#endif /* _MSC_VER */
1125 if( opt.renego_period < 2 )
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001126 goto usage;
1127 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001128 else if( strcmp( p, "exchanges" ) == 0 )
1129 {
1130 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001131 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +02001132 goto usage;
1133 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001134 else if( strcmp( p, "min_version" ) == 0 )
1135 {
1136 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001138 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001140 else if( strcmp( q, "tls1_1" ) == 0 ||
1141 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001143 else if( strcmp( q, "tls1_2" ) == 0 ||
1144 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakker1d29fb52012-09-28 13:28:45 +00001146 else
1147 goto usage;
1148 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001149 else if( strcmp( p, "max_version" ) == 0 )
1150 {
1151 if( strcmp( q, "ssl3" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001153 else if( strcmp( q, "tls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001155 else if( strcmp( q, "tls1_1" ) == 0 ||
1156 strcmp( q, "dtls1" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +01001158 else if( strcmp( q, "tls1_2" ) == 0 ||
1159 strcmp( q, "dtls1_2" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001161 else
1162 goto usage;
1163 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001164 else if( strcmp( p, "arc4" ) == 0 )
1165 {
1166 switch( atoi( q ) )
1167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
1169 case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001170 default: goto usage;
1171 }
1172 }
Gilles Peskineae765992017-05-09 15:59:24 +02001173 else if( strcmp( p, "allow_sha1" ) == 0 )
1174 {
1175 switch( atoi( q ) )
1176 {
1177 case 0: opt.allow_sha1 = 0; break;
1178 case 1: opt.allow_sha1 = 1; break;
1179 default: goto usage;
1180 }
1181 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001182 else if( strcmp( p, "force_version" ) == 0 )
1183 {
1184 if( strcmp( q, "ssl3" ) == 0 )
1185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
1187 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001188 }
1189 else if( strcmp( q, "tls1" ) == 0 )
1190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
1192 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001193 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001194 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1197 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001198 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001199 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1202 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001203 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001204 else if( strcmp( q, "dtls1" ) == 0 )
1205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
1207 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
1208 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001209 }
1210 else if( strcmp( q, "dtls1_2" ) == 0 )
1211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
1213 opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
1214 opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001215 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001216 else
1217 goto usage;
1218 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001219 else if( strcmp( p, "auth_mode" ) == 0 )
1220 {
Manuel Pégourié-Gonnard4d6f1782015-06-19 14:40:39 +02001221 if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
Paul Bakker91ebfb52012-11-23 14:04:08 +01001222 goto usage;
1223 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001224 else if( strcmp( p, "max_frag_len" ) == 0 )
1225 {
1226 if( strcmp( q, "512" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001228 else if( strcmp( q, "1024" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001230 else if( strcmp( q, "2048" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001232 else if( strcmp( q, "4096" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001234 else
1235 goto usage;
1236 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001237 else if( strcmp( p, "alpn" ) == 0 )
1238 {
1239 opt.alpn_string = q;
1240 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001241 else if( strcmp( p, "trunc_hmac" ) == 0 )
1242 {
1243 switch( atoi( q ) )
1244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
1246 case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001247 default: goto usage;
1248 }
1249 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001250 else if( strcmp( p, "extended_ms" ) == 0 )
1251 {
1252 switch( atoi( q ) )
1253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 case 0: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED; break;
1255 case 1: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; break;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001256 default: goto usage;
1257 }
1258 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001259 else if( strcmp( p, "etm" ) == 0 )
1260 {
1261 switch( atoi( q ) )
1262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
1264 case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001265 default: goto usage;
1266 }
1267 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001268 else if( strcmp( p, "tickets" ) == 0 )
1269 {
1270 opt.tickets = atoi( q );
1271 if( opt.tickets < 0 || opt.tickets > 1 )
1272 goto usage;
1273 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001274 else if( strcmp( p, "ticket_timeout" ) == 0 )
1275 {
1276 opt.ticket_timeout = atoi( q );
1277 if( opt.ticket_timeout < 0 )
1278 goto usage;
1279 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001280 else if( strcmp( p, "cache_max" ) == 0 )
1281 {
1282 opt.cache_max = atoi( q );
1283 if( opt.cache_max < 0 )
1284 goto usage;
1285 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001286 else if( strcmp( p, "cache_timeout" ) == 0 )
1287 {
1288 opt.cache_timeout = atoi( q );
1289 if( opt.cache_timeout < 0 )
1290 goto usage;
1291 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001292 else if( strcmp( p, "cookies" ) == 0 )
1293 {
1294 opt.cookies = atoi( q );
1295 if( opt.cookies < -1 || opt.cookies > 1)
1296 goto usage;
1297 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001298 else if( strcmp( p, "anti_replay" ) == 0 )
1299 {
1300 opt.anti_replay = atoi( q );
1301 if( opt.anti_replay < 0 || opt.anti_replay > 1)
1302 goto usage;
1303 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001304 else if( strcmp( p, "badmac_limit" ) == 0 )
1305 {
1306 opt.badmac_limit = atoi( q );
1307 if( opt.badmac_limit < 0 )
1308 goto usage;
1309 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001310 else if( strcmp( p, "hs_timeout" ) == 0 )
1311 {
1312 if( ( p = strchr( q, '-' ) ) == NULL )
1313 goto usage;
1314 *p++ = '\0';
1315 opt.hs_to_min = atoi( q );
1316 opt.hs_to_max = atoi( p );
1317 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1318 goto usage;
1319 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001320 else if( strcmp( p, "sni" ) == 0 )
1321 {
1322 opt.sni = q;
1323 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001324 else
1325 goto usage;
1326 }
1327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#if defined(MBEDTLS_DEBUG_C)
1329 mbedtls_debug_set_threshold( opt.debug_level );
Paul Bakkerc73079a2014-04-25 16:34:30 +02001330#endif
Andrzej Kurekf4f59c02018-06-28 03:42:01 -04001331 buf = malloc( opt.buffer_size );
1332 if( buf == NULL )
1333 {
1334 mbedtls_printf("Could not allocate %u bytes\n", opt.buffer_size);
1335 ret = 3;
1336 goto exit;
1337 }
Paul Bakkerc73079a2014-04-25 16:34:30 +02001338
Paul Bakkerc1516be2013-06-29 16:01:32 +02001339 if( opt.force_ciphersuite[0] > 0 )
1340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1342 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001343
Paul Bakker5b55b792013-07-19 13:43:43 +02001344 if( opt.max_version != -1 &&
1345 ciphersuite_info->min_minor_ver > opt.max_version )
1346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakker5b55b792013-07-19 13:43:43 +02001348 ret = 2;
1349 goto usage;
1350 }
1351 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001352 ciphersuite_info->max_minor_ver < opt.min_version )
1353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +02001355 ret = 2;
1356 goto usage;
1357 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001358
1359 /* If we select a version that's not supported by
1360 * this suite, then there will be no common ciphersuite... */
1361 if( opt.max_version == -1 ||
1362 opt.max_version > ciphersuite_info->max_minor_ver )
1363 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001364 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001365 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001366 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001367 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001368 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001369 /* DTLS starts with TLS 1.1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1371 opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
1372 opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001373 }
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001374
1375 /* Enable RC4 if needed and not explicitly disabled */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001381 ret = 2;
1382 goto usage;
1383 }
1384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001386 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001387 }
1388
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001389 if( opt.version_suites != NULL )
1390 {
1391 const char *name[4] = { 0 };
1392
1393 /* Parse 4-element coma-separated list */
1394 for( i = 0, p = (char *) opt.version_suites;
1395 i < 4 && *p != '\0';
1396 i++ )
1397 {
1398 name[i] = p;
1399
1400 /* Terminate the current string and move on to next one */
1401 while( *p != ',' && *p != '\0' )
1402 p++;
1403 if( *p == ',' )
1404 *p++ = '\0';
1405 }
1406
1407 if( i != 4 )
1408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 mbedtls_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001410 ret = 1;
1411 goto exit;
1412 }
1413
1414 memset( version_suites, 0, sizeof( version_suites ) );
1415
1416 /* Get the suites identifiers from their name */
1417 for( i = 0; i < 4; i++ )
1418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001420
1421 if( version_suites[i][0] == 0 )
1422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001424 ret = 2;
1425 goto usage;
1426 }
1427 }
1428 }
1429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001431 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001432 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001433 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001434 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 mbedtls_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001437 goto exit;
1438 }
1439
1440 if( opt.psk_list != NULL )
1441 {
1442 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 mbedtls_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001445 goto exit;
1446 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001447 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001449
Hanno Becker61c0c702017-05-15 16:05:15 +01001450#if defined(MBEDTLS_ECP_C)
1451 if( opt.curves != NULL )
1452 {
1453 p = (char *) opt.curves;
1454 i = 0;
1455
1456 if( strcmp( p, "none" ) == 0 )
1457 {
1458 curve_list[0] = MBEDTLS_ECP_DP_NONE;
1459 }
1460 else if( strcmp( p, "default" ) != 0 )
1461 {
1462 /* Leave room for a final NULL in curve list */
Hanno Becker46a16292017-06-09 16:13:22 +01001463 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Becker61c0c702017-05-15 16:05:15 +01001464 {
1465 q = p;
1466
1467 /* Terminate the current string */
1468 while( *p != ',' && *p != '\0' )
1469 p++;
1470 if( *p == ',' )
1471 *p++ = '\0';
1472
1473 if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
1474 {
1475 curve_list[i++] = curve_cur->grp_id;
1476 }
1477 else
1478 {
1479 mbedtls_printf( "unknown curve %s\n", q );
1480 mbedtls_printf( "supported curves: " );
1481 for( curve_cur = mbedtls_ecp_curve_list();
1482 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
1483 curve_cur++ )
1484 {
1485 mbedtls_printf( "%s ", curve_cur->name );
1486 }
1487 mbedtls_printf( "\n" );
1488 goto exit;
1489 }
1490 }
1491
1492 mbedtls_printf("Number of curves: %d\n", i );
1493
Hanno Becker46a16292017-06-09 16:13:22 +01001494 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Becker61c0c702017-05-15 16:05:15 +01001495 {
Hanno Becker46a16292017-06-09 16:13:22 +01001496 mbedtls_printf( "curves list too long, maximum %d",
1497 CURVE_LIST_SIZE - 1 );
Hanno Becker61c0c702017-05-15 16:05:15 +01001498 goto exit;
1499 }
1500
1501 curve_list[i] = MBEDTLS_ECP_DP_NONE;
1502 }
1503 }
1504#endif /* MBEDTLS_ECP_C */
1505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001507 if( opt.alpn_string != NULL )
1508 {
1509 p = (char *) opt.alpn_string;
1510 i = 0;
1511
1512 /* Leave room for a final NULL in alpn_list */
Hanno Becker46a16292017-06-09 16:13:22 +01001513 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001514 {
1515 alpn_list[i++] = p;
1516
1517 /* Terminate the current string and move on to next one */
1518 while( *p != ',' && *p != '\0' )
1519 p++;
1520 if( *p == ',' )
1521 *p++ = '\0';
1522 }
1523 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001525
Paul Bakkerfbb17802013-04-17 19:10:21 +02001526 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001527 * 0. Initialize the RNG and the session data
1528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001530 fflush( stdout );
1531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001533 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001534 (const unsigned char *) pers,
1535 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001536 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +02001537 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001538 goto exit;
1539 }
1540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001544 /*
1545 * 1.1. Load the trusted CA
1546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001548 fflush( stdout );
1549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550#if defined(MBEDTLS_FS_IO)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001551 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001552 if( strcmp( opt.ca_path, "none" ) == 0 )
1553 ret = 0;
1554 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001556 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001557 if( strcmp( opt.ca_file, "none" ) == 0 )
1558 ret = 0;
1559 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001561 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001562#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563#if defined(MBEDTLS_CERTS_C)
1564 for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 ret = mbedtls_x509_crt_parse( &cacert,
1567 (const unsigned char *) mbedtls_test_cas[i],
1568 mbedtls_test_cas_len[i] );
Manuel Pégourié-Gonnard2f165062015-03-27 10:20:26 +01001569 if( ret != 0 )
1570 break;
1571 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001572#else
1573 {
1574 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001576 }
1577#endif
1578 if( ret < 0 )
1579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001581 goto exit;
1582 }
1583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001585
1586 /*
1587 * 1.2. Load own certificate and private key
1588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 mbedtls_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001590 fflush( stdout );
1591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001593 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001594 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001595 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001599 -ret );
1600 goto exit;
1601 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001602 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001603 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001604 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001605 key_cert_init++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001609 goto exit;
1610 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001611 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001612 if( key_cert_init == 1 )
1613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001615 goto exit;
1616 }
1617
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001618 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001619 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001620 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001624 -ret );
1625 goto exit;
1626 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001627 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001628 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001629 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001630 key_cert_init2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001634 -ret );
1635 goto exit;
1636 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001637 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001638 if( key_cert_init2 == 1 )
1639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001641 goto exit;
1642 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001643#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001644 if( key_cert_init == 0 &&
1645 strcmp( opt.crt_file, "none" ) != 0 &&
1646 strcmp( opt.key_file, "none" ) != 0 &&
1647 key_cert_init2 == 0 &&
1648 strcmp( opt.crt_file2, "none" ) != 0 &&
1649 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#if !defined(MBEDTLS_CERTS_C)
1652 mbedtls_printf( "Not certificated or key provided, and \n"
1653 "MBEDTLS_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001654 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001655#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656#if defined(MBEDTLS_RSA_C)
1657 if( ( ret = mbedtls_x509_crt_parse( &srvcert,
1658 (const unsigned char *) mbedtls_test_srv_crt_rsa,
1659 mbedtls_test_srv_crt_rsa_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001662 goto exit;
1663 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 if( ( ret = mbedtls_pk_parse_key( &pkey,
1665 (const unsigned char *) mbedtls_test_srv_key_rsa,
1666 mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001669 goto exit;
1670 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001671 key_cert_init = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672#endif /* MBEDTLS_RSA_C */
1673#if defined(MBEDTLS_ECDSA_C)
1674 if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
1675 (const unsigned char *) mbedtls_test_srv_crt_ec,
1676 mbedtls_test_srv_crt_ec_len ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001679 goto exit;
1680 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 if( ( ret = mbedtls_pk_parse_key( &pkey2,
1682 (const unsigned char *) mbedtls_test_srv_key_ec,
1683 mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001686 goto exit;
1687 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001688 key_cert_init2 = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689#endif /* MBEDTLS_ECDSA_C */
1690#endif /* MBEDTLS_CERTS_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001691 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 mbedtls_printf( " ok\n" );
1694#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001697 if( opt.dhm_file != NULL )
1698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 mbedtls_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001700 fflush( stdout );
1701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001705 -ret );
1706 goto exit;
1707 }
1708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001710 }
1711#endif
1712
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001713#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001714 if( opt.sni != NULL )
1715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 mbedtls_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001717 fflush( stdout );
1718
1719 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
1720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001722 goto exit;
1723 }
1724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001726 }
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001727#endif /* SNI_OPTION */
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001728
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001729 /*
1730 * 2. Setup the listening TCP socket
1731 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +02001732 mbedtls_printf( " . Bind on %s://%s:%s/ ...",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001734 opt.server_addr ? opt.server_addr : "*",
1735 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001736 fflush( stdout );
1737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
1739 opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
1740 MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001743 goto exit;
1744 }
1745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001747
1748 /*
1749 * 3. Setup stuff
1750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001752 fflush( stdout );
1753
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001754 if( ( ret = mbedtls_ssl_config_defaults( &conf,
1755 MBEDTLS_SSL_IS_SERVER,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02001756 opt.transport,
1757 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001758 {
1759 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
1760 goto exit;
1761 }
1762
Gilles Peskinedd57d752017-05-05 18:59:02 +02001763#if defined(MBEDTLS_X509_CRT_PARSE_C)
1764 /* The default algorithms profile disables SHA-1, but our tests still
1765 rely on it heavily. Hence we allow it here. A real-world server
1766 should use the default profile unless there is a good reason not to. */
Gilles Peskineae765992017-05-09 15:59:24 +02001767 if( opt.allow_sha1 > 0 )
1768 {
1769 crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
1770 mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
Gilles Peskinedb56aca2017-05-11 19:01:11 +02001771 mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
Gilles Peskineae765992017-05-09 15:59:24 +02001772 }
Gilles Peskinedd57d752017-05-05 18:59:02 +02001773#endif /* MBEDTLS_X509_CRT_PARSE_C */
1774
Manuel Pégourié-Gonnardfa44f202015-03-27 17:52:25 +01001775 if( opt.auth_mode != DFL_AUTH_MODE )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001776 mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001779 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 +02001780 mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001784 if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001785 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001786 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001787 goto exit;
1788 };
Paul Bakker05decb22013-08-15 13:33:48 +02001789#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001792 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001793 mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001794#endif
1795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001797 if( opt.extended_ms != DFL_EXTENDED_MS )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001798 mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001799#endif
1800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001802 if( opt.etm != DFL_ETM )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001803 mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001804#endif
1805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001807 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001808 if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001809 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001810 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001811 goto exit;
1812 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001813#endif
1814
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001815 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
1816 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001819 if( opt.cache_max != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001821
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001822 if( opt.cache_timeout != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001824
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001825 mbedtls_ssl_conf_session_cache( &conf, &cache,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001826 mbedtls_ssl_cache_get,
1827 mbedtls_ssl_cache_set );
Paul Bakker0a597072012-09-25 21:55:46 +00001828#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001831 if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001832 {
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001833 if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
1834 mbedtls_ctr_drbg_random, &ctr_drbg,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +02001835 MBEDTLS_CIPHER_AES_256_GCM,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001836 opt.ticket_timeout ) ) != 0 )
1837 {
1838 mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
1839 goto exit;
1840 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001841
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02001842 mbedtls_ssl_conf_session_tickets_cb( &conf,
1843 mbedtls_ssl_ticket_write,
1844 mbedtls_ssl_ticket_parse,
1845 &ticket_ctx );
1846 }
Paul Bakkera503a632013-08-14 13:48:06 +02001847#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849#if defined(MBEDTLS_SSL_PROTO_DTLS)
1850 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001853 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
1856 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001859 goto exit;
1860 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001861
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001862 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001863 &cookie_ctx );
1864 }
1865 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866#endif /* MBEDTLS_SSL_COOKIE_C */
1867#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001868 if( opt.cookies == 0 )
1869 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001870 mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001871 }
1872 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001874 {
1875 ; /* Nothing to do */
1876 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001879 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001880 mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001881#endif
1882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001884 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001885 mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001886#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001887 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001889
Paul Bakker41c83d32013-03-20 14:39:14 +01001890 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001891 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001892
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001893#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardd42b7c82015-03-20 19:44:04 +00001894 if( opt.arc4 != DFL_ARC4 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001895 mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001896#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001897
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001898 if( opt.version_suites != NULL )
1899 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001900 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 MBEDTLS_SSL_MAJOR_VERSION_3,
1902 MBEDTLS_SSL_MINOR_VERSION_0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001903 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 MBEDTLS_SSL_MAJOR_VERSION_3,
1905 MBEDTLS_SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001906 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 MBEDTLS_SSL_MAJOR_VERSION_3,
1908 MBEDTLS_SSL_MINOR_VERSION_2 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001909 mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3],
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 MBEDTLS_SSL_MAJOR_VERSION_3,
1911 MBEDTLS_SSL_MINOR_VERSION_3 );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001912 }
1913
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001914 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001915 mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001917 mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001918
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001919 if( opt.renego_delay != DFL_RENEGO_DELAY )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001920 mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001921
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001922 if( opt.renego_period != DFL_RENEGO_PERIOD )
1923 {
Andres AG9b1927b2017-01-19 16:30:57 +00001924 PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001925 mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001926 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001927#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001930 if( strcmp( opt.ca_path, "none" ) != 0 &&
1931 strcmp( opt.ca_file, "none" ) != 0 )
1932 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001933 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001934 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001935 if( key_cert_init )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001936 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001937 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001938 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001939 goto exit;
1940 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001941 if( key_cert_init2 )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001942 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, &pkey2 ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001943 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001944 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001945 goto exit;
1946 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001947#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001948
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02001949#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001950 if( opt.sni != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001951 mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001952#endif
1953
Hanno Becker61c0c702017-05-15 16:05:15 +01001954#if defined(MBEDTLS_ECP_C)
1955 if( opt.curves != NULL &&
1956 strcmp( opt.curves, "default" ) != 0 )
1957 {
1958 mbedtls_ssl_conf_curves( &conf, curve_list );
1959 }
1960#endif
1961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001963 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
1964 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001965 ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001966 (const unsigned char *) opt.psk_identity,
1967 strlen( opt.psk_identity ) );
1968 if( ret != 0 )
1969 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001970 mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001971 goto exit;
1972 }
1973 }
1974
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001975 if( opt.psk_list != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001976 mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02001977#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979#if defined(MBEDTLS_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00001980 /*
1981 * Use different group than default DHM group
1982 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001984 if( opt.dhm_file != NULL )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001985 ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001986#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001987 if( ret != 0 )
1988 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001989 mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001990 goto exit;
1991 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001992#endif
1993
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001994 if( opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001995 mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001996
Manuel Pégourié-Gonnard8c8be1e2015-03-31 14:21:11 +02001997 if( opt.max_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001998 mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
Paul Bakkerc1516be2013-06-29 16:01:32 +02001999
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002000 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
2001 {
2002 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
2003 goto exit;
2004 }
2005
2006 if( opt.nbio == 2 )
2007 mbedtls_ssl_set_bio( &ssl, &client_fd, my_send, my_recv, NULL );
2008 else
2009 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv,
Manuel Pégourié-Gonnardd4f04db2015-05-14 18:58:17 +02002010 opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02002011
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002012#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002013 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
2014 mbedtls_timing_get_delay );
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002015#endif
Manuel Pégourié-Gonnarde3c41ad2015-05-13 10:04:32 +02002016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002018
2019reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002020#if !defined(_WIN32)
2021 if( received_sigterm )
2022 {
Manuel Pégourié-Gonnarde56c77e2018-01-22 10:55:10 +01002023 mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" );
2024 if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT )
2025 ret = 0;
2026
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02002027 goto exit;
2028 }
2029#endif
2030
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002031 if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
2032 {
2033 mbedtls_printf( " ! Client initiated reconnection from same port\n" );
2034 goto handshake;
2035 }
2036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002038 if( ret != 0 )
2039 {
2040 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 mbedtls_strerror( ret, error_buf, 100 );
2042 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002043 }
2044#endif
2045
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002046 mbedtls_net_free( &client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01002047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048 mbedtls_ssl_session_reset( &ssl );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002049
2050 /*
2051 * 3. Wait until a client connects
2052 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053 mbedtls_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002054 fflush( stdout );
2055
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002056 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02002057 client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002058 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02002059#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002060 if( received_sigterm )
2061 {
Manuel Pégourié-Gonnarde56c77e2018-01-22 10:55:10 +01002062 mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" );
2063 if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED )
2064 ret = 0;
2065
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002066 goto exit;
2067 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02002068#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02002069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002071 goto exit;
2072 }
2073
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002074 if( opt.nbio > 0 )
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002075 ret = mbedtls_net_set_nonblock( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002076 else
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +02002077 ret = mbedtls_net_set_block( &client_fd );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002078 if( ret != 0 )
2079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01002081 goto exit;
2082 }
2083
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002084 mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2087 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002088 {
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +02002089 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
2090 client_ip, cliip_len ) ) != 0 )
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 mbedtls_printf( " failed\n ! "
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02002093 "mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002094 goto exit;
2095 }
2096 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02002098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002100
2101 /*
2102 * 4. Handshake
2103 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02002104handshake:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002106 fflush( stdout );
2107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 do ret = mbedtls_ssl_handshake( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002109 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
2110 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 mbedtls_printf( " hello verification requested\n" );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002115 ret = 0;
2116 goto reset;
2117 }
2118 else if( ret != 0 )
2119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002121
2122#if defined(MBEDTLS_X509_CRT_PARSE_C)
2123 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
2124 {
2125 char vrfy_buf[512];
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02002126 flags = mbedtls_ssl_get_verify_result( &ssl );
Manuel Pégourié-Gonnard6ea831d2015-06-22 16:50:52 +02002127
2128 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
2129
2130 mbedtls_printf( "%s\n", vrfy_buf );
2131 }
2132#endif
2133
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002134 goto reset;
2135 }
2136 else /* ret == 0 */
2137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
2139 mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02002140 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
2143 mbedtls_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002144 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02002146
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002147#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2148 mbedtls_printf( " [ Maximum fragment length is %u ]\n",
2149 (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
2150#endif
2151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002153 if( opt.alpn_string != NULL )
2154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
2156 mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02002157 alp ? alp : "(none)" );
2158 }
2159#endif
2160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002162 /*
Ron Eldora905bdc2017-06-20 15:23:23 +03002163 * 5. Verify the client certificate
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002166
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002167 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002168 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002169 char vrfy_buf[512];
2170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 mbedtls_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002172
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002173 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002174
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +01002175 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002176 }
2177 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 mbedtls_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002179
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02002180 if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002181 {
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02002182 char crt_buf[512];
2183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 mbedtls_printf( " . Peer certificate information ...\n" );
Manuel Pégourié-Gonnard1c5b9fc2015-06-27 14:38:51 +02002185 mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 mbedtls_ssl_get_peer_cert( &ssl ) );
Manuel Pégourié-Gonnard67557172015-07-02 11:15:48 +02002187 mbedtls_printf( "%s\n", crt_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002188 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002190
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02002191 if( opt.exchanges == 0 )
2192 goto close_notify;
2193
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00002194 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002195data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002196 /*
2197 * 6. Read the HTTP Request
2198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 mbedtls_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002200 fflush( stdout );
2201
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002202 /*
2203 * TLS and DTLS need different reading styles (stream vs datagram)
2204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002206 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002207 do
2208 {
2209 int terminated = 0;
Andrzej Kurekf4f59c02018-06-28 03:42:01 -04002210 len = opt.buffer_size - 1;
2211 memset( buf, 0, opt.buffer_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002213
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002214 if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
2215 ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002216 continue;
2217
2218 if( ret <= 0 )
2219 {
2220 switch( ret )
2221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2223 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002224 goto close_notify;
2225
2226 case 0:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 case MBEDTLS_ERR_NET_CONN_RESET:
2228 mbedtls_printf( " connection was reset by peer\n" );
2229 ret = MBEDTLS_ERR_NET_CONN_RESET;
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002230 goto reset;
2231
2232 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002234 goto reset;
2235 }
2236 }
2237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002239 {
2240 len = ret;
2241 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002243
2244 /* End of message should be detected according to the syntax of the
2245 * application protocol (eg HTTP), just use a dummy test here. */
2246 if( buf[len - 1] == '\n' )
2247 terminated = 1;
2248 }
2249 else
2250 {
2251 int extra_len, ori_len;
2252 unsigned char *larger_buf;
2253
2254 ori_len = ret;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002255 extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002256
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002257 larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002258 if( larger_buf == NULL )
2259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 mbedtls_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002261 ret = 1;
2262 goto reset;
2263 }
2264
2265 memset( larger_buf, 0, ori_len + extra_len );
2266 memcpy( larger_buf, buf, ori_len );
2267
2268 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002270 if( ret != extra_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002274 ret = 1;
2275 goto reset;
2276 }
2277
2278 larger_buf[ori_len + extra_len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 mbedtls_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002280 ori_len + extra_len, ori_len, extra_len,
2281 (char *) larger_buf );
2282
2283 /* End of message should be detected according to the syntax of the
2284 * application protocol (eg HTTP), just use a dummy test here. */
2285 if( larger_buf[ori_len + extra_len - 1] == '\n' )
2286 terminated = 1;
2287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 mbedtls_free( larger_buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002289 }
2290
2291 if( terminated )
2292 {
2293 ret = 0;
2294 break;
2295 }
2296 }
2297 while( 1 );
2298 }
2299 else /* Not stream, so datagram */
2300 {
Andrzej Kurekf4f59c02018-06-28 03:42:01 -04002301 len = opt.buffer_size - 1;
2302 memset( buf, 0, opt.buffer_size );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 do ret = mbedtls_ssl_read( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002305 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
2306 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002307
2308 if( ret <= 0 )
2309 {
2310 switch( ret )
2311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
2313 mbedtls_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002314 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002315 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002316
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002317 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002319 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002320 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002321 }
2322
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002323 len = ret;
2324 buf[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002326 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002327 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002328
2329 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002330 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02002331 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00002334 if( opt.renegotiate && exchanges_left == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 mbedtls_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002337 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002340 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002341 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2342 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002345 goto reset;
2346 }
2347 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002350 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002352
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002353 /*
2354 * 7. Write the 200 Response
2355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 mbedtls_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002357 fflush( stdout );
2358
2359 len = sprintf( (char *) buf, HTTP_RESPONSE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 mbedtls_ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002361
Andrzej Kurekf4f59c02018-06-28 03:42:01 -04002362 /* Add padding to the response to reach opt.response_size in length */
2363 if( opt.response_size != DFL_RESPONSE_SIZE &&
2364 len < opt.response_size )
2365 {
2366 memset( buf + len, 'B', opt.response_size - len );
2367 len += opt.response_size - len;
2368 }
2369
2370 /* Truncate if response size is smaller than the "natural" size */
2371 if( opt.response_size != DFL_RESPONSE_SIZE &&
2372 len > opt.response_size )
2373 {
2374 len = opt.response_size;
2375
2376 /* Still end with \r\n unless that's really not possible */
2377 if( len >= 2 ) buf[len - 2] = '\r';
2378 if( len >= 1 ) buf[len - 1] = '\n';
2379 }
2380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381 if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002382 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002383 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002386 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002391 goto reset;
2392 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002393
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002394 if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
2395 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002398 goto reset;
2399 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002400 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002401 }
2402 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002403 else /* Not stream, so datagram */
2404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 do ret = mbedtls_ssl_write( &ssl, buf, len );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002406 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
2407 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002408
2409 if( ret < 0 )
2410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002412 goto reset;
2413 }
2414
2415 frags = 1;
2416 written = ret;
2417 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002418
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002419 buf[written] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 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 +01002421 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002422
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002423 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002424 * 7b. Continue doing data exchanges?
2425 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00002426 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002427 goto data_exchange;
2428
2429 /*
2430 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002431 */
2432close_notify:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 mbedtls_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002434
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002435 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 do ret = mbedtls_ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002437 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002438 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 mbedtls_printf( " done\n" );
Rich Evansf90016a2015-01-19 14:26:37 +00002441
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002442 goto reset;
2443
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002444 /*
2445 * Cleanup and exit
2446 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002447exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448#ifdef MBEDTLS_ERROR_C
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002449 if( ret != 0 )
2450 {
2451 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 mbedtls_strerror( ret, error_buf, 100 );
2453 mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002454 }
2455#endif
2456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 mbedtls_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01002458 fflush( stdout );
2459
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +02002460 mbedtls_net_free( &client_fd );
2461 mbedtls_net_free( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +02002462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
2464 mbedtls_dhm_free( &dhm );
Paul Bakkera317a982014-06-18 16:44:11 +02002465#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466#if defined(MBEDTLS_X509_CRT_PARSE_C)
2467 mbedtls_x509_crt_free( &cacert );
2468 mbedtls_x509_crt_free( &srvcert );
2469 mbedtls_pk_free( &pkey );
2470 mbedtls_x509_crt_free( &srvcert2 );
2471 mbedtls_pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02002472#endif
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02002473#if defined(SNI_OPTION)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002474 sni_free( sni_info );
2475#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02002477 psk_free( psk_info );
2478#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
2480 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02002481#endif
Paul Bakkered27a042013-04-18 22:46:23 +02002482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02002484 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 mbedtls_ctr_drbg_free( &ctr_drbg );
2486 mbedtls_entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488#if defined(MBEDTLS_SSL_CACHE_C)
2489 mbedtls_ssl_cache_free( &cache );
Paul Bakker0a597072012-09-25 21:55:46 +00002490#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002491#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2492 mbedtls_ssl_ticket_free( &ticket_ctx );
2493#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494#if defined(MBEDTLS_SSL_COOKIE_C)
2495 mbedtls_ssl_cookie_free( &cookie_ctx );
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002496#endif
Paul Bakker0a597072012-09-25 21:55:46 +00002497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
2499#if defined(MBEDTLS_MEMORY_DEBUG)
2500 mbedtls_memory_buffer_alloc_status();
Paul Bakker82024bf2013-07-04 11:52:32 +02002501#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +02002503#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02002504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505 mbedtls_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01002506
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002507#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002509 fflush( stdout ); getchar();
2510#endif
2511
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002512 // Shell can not handle large exit numbers -> 1 for errors
2513 if( ret < 0 )
2514 ret = 1;
2515
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002516 return( ret );
2517}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
2519 MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Manuel Pégourié-Gonnardd2377e72015-05-13 13:58:56 +02002520 MBEDTLS_CTR_DRBG_C */