blob: 95438f906e6c2eb1dc1960683bbcc396f13e02dc [file] [log] [blame]
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001/*
2 * SSL client with options
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerb60b95f2012-09-25 09:05:17 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00007 *
Paul Bakkerb60b95f2012-09-25 09:05:17 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020032#include <stdio.h>
Rich Evans18b78c72015-02-11 14:06:19 +000033#define polarssl_free free
34#define polarssl_malloc malloc
35#define polarssl_fprintf fprintf
36#define polarssl_printf printf
37#endif
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020038
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010039#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) && defined(POLARSSL_FS_IO)
40#define POLARSSL_SNI
41#endif
42
Paul Bakkerb60b95f2012-09-25 09:05:17 +000043#if defined(_WIN32)
44#include <windows.h>
45#endif
46
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +000047#if defined(POLARSSL_ENTROPY_C) && \
48 defined(POLARSSL_SSL_TLS_C) && defined(POLARSSL_SSL_SRV_C) && \
Rich Evans18b78c72015-02-11 14:06:19 +000049 defined(POLARSSL_NET_C) && defined(POLARSSL_CTR_DRBG_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +000050#include "polarssl/net.h"
51#include "polarssl/ssl.h"
52#include "polarssl/entropy.h"
53#include "polarssl/ctr_drbg.h"
54#include "polarssl/certs.h"
55#include "polarssl/x509.h"
56#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020057#include "polarssl/debug.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000058
Rich Evans18b78c72015-02-11 14:06:19 +000059#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#endif
63
64#if !defined(_WIN32)
65#include <signal.h>
66#endif
67
Paul Bakker0a597072012-09-25 21:55:46 +000068#if defined(POLARSSL_SSL_CACHE_C)
69#include "polarssl/ssl_cache.h"
70#endif
71
Paul Bakker82024bf2013-07-04 11:52:32 +020072#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardd43ccb62015-01-23 17:38:09 +000073#include "polarssl/memory_buffer_alloc.h"
Paul Bakker82024bf2013-07-04 11:52:32 +020074#endif
75
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +010076#define DFL_SERVER_ADDR NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000077#define DFL_SERVER_PORT 4433
Paul Bakkerb60b95f2012-09-25 09:05:17 +000078#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010079#define DFL_NBIO 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +000080#define DFL_CA_FILE ""
81#define DFL_CA_PATH ""
82#define DFL_CRT_FILE ""
83#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +020084#define DFL_CRT_FILE2 ""
85#define DFL_KEY_FILE2 ""
Paul Bakkerfbb17802013-04-17 19:10:21 +020086#define DFL_PSK ""
87#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +020088#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000089#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +020090#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +020091#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010092#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010093#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +020094#define DFL_RENEGO_DELAY -2
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +010095#define DFL_RENEGO_PERIOD -1
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +020096#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +010097#define DFL_MIN_VERSION SSL_MINOR_VERSION_1
Paul Bakkerc1516be2013-06-29 16:01:32 +020098#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +010099#define DFL_ARC4 SSL_ARC4_DISABLED
Paul Bakker91ebfb52012-11-23 14:04:08 +0100100#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200101#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100102#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200103#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100104#define DFL_TICKET_TIMEOUT -1
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100105#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100106#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100107#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200108#define DFL_ALPN_STRING NULL
Hanno Becker6fd6d242017-05-25 17:51:31 +0100109#define DFL_CURVES NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200110#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200111#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100112#define DFL_ETM -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000113
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200114#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
115 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
116 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
117 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
118 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
119 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
120 "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 +0200121
Paul Bakker8e714d72013-07-18 11:05:13 +0200122/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
123 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000124#define HTTP_RESPONSE \
125 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000126 "<h2>mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200127 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000128
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200129/*
130 * Size of the basic I/O buffer. Able to hold our default response.
131 *
132 * You will need to adapt the ssl_get_bytes_avail() test in ssl-opt.sh
133 * if you change this value to something outside the range <= 100 or > 500
134 */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200135#define IO_BUF_LEN 200
136
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200137#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000138#if defined(POLARSSL_FS_IO)
139#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100140 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
141 " default: \"\" (pre-loaded)\n" \
142 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
143 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
144 " 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 +0200145 " default: see note after key_file2\n" \
146 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200147 " 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 +0200148 " default: see note after key_file2\n" \
149 " key_file2=%%s default: see note below\n" \
150 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200151 " preloaded certificate(s) and key(s) are used if available\n" \
152 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
153 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000154#else
155#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200156 "\n" \
157 " No file operations available (POLARSSL_FS_IO not defined)\n" \
158 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000159#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200160#else
161#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200162#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200163
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200164#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200165#define USAGE_PSK \
166 " psk=%%s default: \"\" (in hex, without 0x)\n" \
167 " psk_identity=%%s default: \"Client_identity\"\n"
168#else
169#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200170#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000171
Paul Bakkera503a632013-08-14 13:48:06 +0200172#if defined(POLARSSL_SSL_SESSION_TICKETS)
173#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100174 " tickets=%%d default: 1 (enabled)\n" \
175 " ticket_timeout=%%d default: ticket default (1d)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200176#else
177#define USAGE_TICKETS ""
178#endif /* POLARSSL_SSL_SESSION_TICKETS */
179
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100180#if defined(POLARSSL_SSL_CACHE_C)
181#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100182 " cache_max=%%d default: cache default (50)\n" \
183 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100184#else
185#define USAGE_CACHE ""
186#endif /* POLARSSL_SSL_CACHE_C */
187
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100188#if defined(POLARSSL_SNI)
189#define USAGE_SNI \
190 " sni=%%s name1,cert1,key1[,name2,cert2,key2[,...]]\n" \
191 " default: disabled\n"
192#else
193#define USAGE_SNI ""
194#endif /* POLARSSL_SNI */
195
Paul Bakker05decb22013-08-15 13:33:48 +0200196#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
197#define USAGE_MAX_FRAG_LEN \
198 " max_frag_len=%%d default: 16384 (tls default)\n" \
199 " options: 512, 1024, 2048, 4096\n"
200#else
201#define USAGE_MAX_FRAG_LEN ""
202#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
203
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100204#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
205#define USAGE_TRUNC_HMAC \
206 " trunc_hmac=%%d default: library default\n"
207#else
208#define USAGE_TRUNC_HMAC ""
209#endif
210
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200211#if defined(POLARSSL_SSL_ALPN)
212#define USAGE_ALPN \
213 " alpn=%%s default: \"\" (disabled)\n" \
214 " example: spdy/1,http/1.1\n"
215#else
216#define USAGE_ALPN ""
217#endif /* POLARSSL_SSL_ALPN */
218
Hanno Becker6fd6d242017-05-25 17:51:31 +0100219#if defined(POLARSSL_SSL_SET_CURVES)
220#define USAGE_CURVES \
221 " curves=a,b,c,d default: \"default\" (library default)\n" \
222 " example: \"secp521r1,brainpoolP512r1\"\n" \
223 " - use \"none\" for empty list\n" \
224 " - see ecp_curve_list()\n" \
225 " for acceptable curve names\n"
226#else
227#define USAGE_CURVES ""
228#endif /* POLARSSL_SSL_SET_CURVES */
229
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200230#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
231#define USAGE_EMS \
232 " extended_ms=0/1 default: (library default: on)\n"
233#else
234#define USAGE_EMS ""
235#endif
236
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100237#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
238#define USAGE_ETM \
239 " etm=0/1 default: (library default: on)\n"
240#else
241#define USAGE_ETM ""
242#endif
243
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100244#if defined(POLARSSL_SSL_RENEGOTIATION)
245#define USAGE_RENEGO \
246 " renegotiation=%%d default: 0 (disabled)\n" \
247 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100248 " renego_delay=%%d default: -2 (library default)\n" \
249 " renego_period=%%d default: (library default)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100250#else
251#define USAGE_RENEGO ""
252#endif
253
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000254#define USAGE \
255 "\n usage: ssl_server2 param=<>...\n" \
256 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100257 " server_addr=%%d default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000258 " server_port=%%d default: 4433\n" \
259 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100260 " nbio=%%d default: 0 (blocking I/O)\n" \
261 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100262 "\n" \
263 " auth_mode=%%s default: \"optional\"\n" \
264 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000265 USAGE_IO \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100266 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100267 "\n" \
268 USAGE_PSK \
269 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100270 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100271 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200272 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100273 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100274 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100275 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100276 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200277 USAGE_ALPN \
Hanno Becker6fd6d242017-05-25 17:51:31 +0100278 USAGE_CURVES \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200279 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100280 USAGE_ETM \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100281 "\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000282 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200283 " max_version=%%s default: \"tls1_2\"\n" \
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100284 " arc4=%%d default: 0 (disabled)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200285 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200286 " options: ssl3, tls1, tls1_1, tls1_2\n" \
287 "\n" \
288 " version_suites=a,b,c,d per-version ciphersuites\n" \
289 " in order from ssl3 to tls1_2\n" \
290 " default: all enabled\n" \
291 " force_ciphersuite=<name> default: all enabled\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000292 " acceptable ciphersuite names:\n"
293
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +0000294#if !defined(POLARSSL_ENTROPY_C) || \
Rich Evans85b05ec2015-02-12 11:37:29 +0000295 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
296 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
297#include <stdio.h>
298int main( void )
299{
300 polarssl_printf("POLARSSL_ENTROPY_C and/or "
301 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
302 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
303 return( 0 );
304}
305#else
306/*
307 * global options
308 */
309struct options
310{
311 const char *server_addr; /* address on which the ssl service runs */
312 int server_port; /* port on which the ssl service runs */
313 int debug_level; /* level of debugging */
314 int nbio; /* should I/O be blocking? */
315 const char *ca_file; /* the file with the CA certificate(s) */
316 const char *ca_path; /* the path with the CA certificate(s) reside */
317 const char *crt_file; /* the file with the server certificate */
318 const char *key_file; /* the file with the server key */
319 const char *crt_file2; /* the file with the 2nd server certificate */
320 const char *key_file2; /* the file with the 2nd server key */
321 const char *psk; /* the pre-shared key */
322 const char *psk_identity; /* the pre-shared key identity */
323 char *psk_list; /* list of PSK id/key pairs for callback */
324 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
325 const char *version_suites; /* per-version ciphersuites */
326 int renegotiation; /* enable / disable renegotiation */
327 int allow_legacy; /* allow legacy renegotiation */
328 int renegotiate; /* attempt renegotiation? */
329 int renego_delay; /* delay before enforcing renegotiation */
330 int renego_period; /* period for automatic renegotiation */
331 int exchanges; /* number of data exchanges */
332 int min_version; /* minimum protocol version accepted */
333 int max_version; /* maximum protocol version accepted */
334 int arc4; /* flag for arc4 suites support */
335 int auth_mode; /* verify mode for connection */
336 unsigned char mfl_code; /* code for maximum fragment length */
337 int trunc_hmac; /* accept truncated hmac? */
338 int tickets; /* enable / disable session tickets */
339 int ticket_timeout; /* session ticket lifetime */
340 int cache_max; /* max number of session cache entries */
341 int cache_timeout; /* expiration delay of session cache entries */
342 char *sni; /* string describing sni information */
Hanno Becker6fd6d242017-05-25 17:51:31 +0100343 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000344 const char *alpn_string; /* ALPN supported protocols */
345 const char *dhm_file; /* the file with the DH parameters */
346 int extended_ms; /* allow negotiation of extended MS? */
347 int etm; /* allow negotiation of encrypt-then-MAC? */
348} opt;
349
350static void my_debug( void *ctx, int level, const char *str )
351{
352 ((void) level);
353
354 polarssl_fprintf( (FILE *) ctx, "%s", str );
355 fflush( (FILE *) ctx );
356}
357
358/*
359 * Test recv/send functions that make sure each try returns
360 * WANT_READ/WANT_WRITE at least once before sucesseding
361 */
362static int my_recv( void *ctx, unsigned char *buf, size_t len )
363{
364 static int first_try = 1;
365 int ret;
366
367 if( first_try )
368 {
369 first_try = 0;
370 return( POLARSSL_ERR_NET_WANT_READ );
371 }
372
373 ret = net_recv( ctx, buf, len );
374 if( ret != POLARSSL_ERR_NET_WANT_READ )
375 first_try = 1; /* Next call will be a new operation */
376 return( ret );
377}
378
379static int my_send( void *ctx, const unsigned char *buf, size_t len )
380{
381 static int first_try = 1;
382 int ret;
383
384 if( first_try )
385 {
386 first_try = 0;
387 return( POLARSSL_ERR_NET_WANT_WRITE );
388 }
389
390 ret = net_send( ctx, buf, len );
391 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
392 first_try = 1; /* Next call will be a new operation */
393 return( ret );
394}
395
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200396/*
397 * Used by sni_parse and psk_parse to handle coma-separated lists
398 */
399#define GET_ITEM( dst ) \
400 dst = p; \
401 while( *p != ',' ) \
402 if( ++p > end ) \
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000403 goto error; \
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200404 *p++ = '\0';
405
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100406#if defined(POLARSSL_SNI)
407typedef struct _sni_entry sni_entry;
408
409struct _sni_entry {
410 const char *name;
411 x509_crt *cert;
412 pk_context *key;
413 sni_entry *next;
414};
415
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100416void sni_free( sni_entry *head )
417{
418 sni_entry *cur = head, *next;
419
420 while( cur != NULL )
421 {
422 x509_crt_free( cur->cert );
423 polarssl_free( cur->cert );
424
425 pk_free( cur->key );
426 polarssl_free( cur->key );
427
428 next = cur->next;
429 polarssl_free( cur );
430 cur = next;
431 }
432}
433
434/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000435 * Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]]
436 * into a usable sni_entry list.
437 *
438 * Modifies the input string! This is not production quality!
439 */
440sni_entry *sni_parse( char *sni_string )
441{
442 sni_entry *cur = NULL, *new = NULL;
443 char *p = sni_string;
444 char *end = p;
445 char *crt_file, *key_file;
446
447 while( *end != '\0' )
448 ++end;
449 *end = ',';
450
451 while( p <= end )
452 {
453 if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL )
454 {
455 sni_free( cur );
456 return( NULL );
457 }
458
459 memset( new, 0, sizeof( sni_entry ) );
460
461 if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ||
462 ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
463 {
464 polarssl_free( new->cert );
465 polarssl_free( new );
466 sni_free( cur );
467 return( NULL );
468 }
469
470 x509_crt_init( new->cert );
471 pk_init( new->key );
472
473 GET_ITEM( new->name );
474 GET_ITEM( crt_file );
475 GET_ITEM( key_file );
476
477 if( x509_crt_parse_file( new->cert, crt_file ) != 0 ||
478 pk_parse_keyfile( new->key, key_file, "" ) != 0 )
479 {
480 goto error;
481 }
482
483 new->next = cur;
484 cur = new;
485 }
486
487 return( cur );
488
489error:
490 sni_free( new );
491 sni_free( cur );
492 return( NULL );
493}
494
495/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100496 * SNI callback.
497 */
498int sni_callback( void *p_info, ssl_context *ssl,
499 const unsigned char *name, size_t name_len )
500{
501 sni_entry *cur = (sni_entry *) p_info;
502
503 while( cur != NULL )
504 {
505 if( name_len == strlen( cur->name ) &&
506 memcmp( name, cur->name, name_len ) == 0 )
507 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200508 return( ssl_set_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100509 }
510
511 cur = cur->next;
512 }
513
514 return( -1 );
515}
516
517#endif /* POLARSSL_SNI */
518
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200519#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
520
521#define HEX2NUM( c ) \
522 if( c >= '0' && c <= '9' ) \
523 c -= '0'; \
524 else if( c >= 'a' && c <= 'f' ) \
525 c -= 'a' - 10; \
526 else if( c >= 'A' && c <= 'F' ) \
527 c -= 'A' - 10; \
528 else \
529 return( -1 );
530
531/*
532 * Convert a hex string to bytes.
533 * Return 0 on success, -1 on error.
534 */
535int unhexify( unsigned char *output, const char *input, size_t *olen )
536{
537 unsigned char c;
538 size_t j;
539
540 *olen = strlen( input );
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200541 if( *olen % 2 != 0 || *olen / 2 > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200542 return( -1 );
543 *olen /= 2;
544
545 for( j = 0; j < *olen * 2; j += 2 )
546 {
547 c = input[j];
548 HEX2NUM( c );
549 output[ j / 2 ] = c << 4;
550
551 c = input[j + 1];
552 HEX2NUM( c );
553 output[ j / 2 ] |= c;
554 }
555
556 return( 0 );
557}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200558
559typedef struct _psk_entry psk_entry;
560
561struct _psk_entry
562{
563 const char *name;
564 size_t key_len;
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200565 unsigned char key[POLARSSL_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200566 psk_entry *next;
567};
568
569/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000570 * Free a list of psk_entry's
571 */
572void psk_free( psk_entry *head )
573{
574 psk_entry *next;
575
576 while( head != NULL )
577 {
578 next = head->next;
579 polarssl_free( head );
580 head = next;
581 }
582}
583
584/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200585 * Parse a string of pairs name1,key1[,name2,key2[,...]]
586 * into a usable psk_entry list.
587 *
588 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200589 */
590psk_entry *psk_parse( char *psk_string )
591{
592 psk_entry *cur = NULL, *new = NULL;
593 char *p = psk_string;
594 char *end = p;
595 char *key_hex;
596
597 while( *end != '\0' )
598 ++end;
599 *end = ',';
600
601 while( p <= end )
602 {
603 if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL )
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +0000604 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200605
606 memset( new, 0, sizeof( psk_entry ) );
607
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200608 GET_ITEM( new->name );
609 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200610
611 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000612 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200613
614 new->next = cur;
615 cur = new;
616 }
617
618 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200619
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000620error:
621 psk_free( new );
622 psk_free( cur );
623 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200624}
625
626/*
627 * PSK callback
628 */
629int psk_callback( void *p_info, ssl_context *ssl,
630 const unsigned char *name, size_t name_len )
631{
632 psk_entry *cur = (psk_entry *) p_info;
633
634 while( cur != NULL )
635 {
636 if( name_len == strlen( cur->name ) &&
637 memcmp( name, cur->name, name_len ) == 0 )
638 {
639 return( ssl_set_psk( ssl, cur->key, cur->key_len,
640 name, name_len ) );
641 }
642
643 cur = cur->next;
644 }
645
646 return( -1 );
647}
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200648#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
649
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +0200650static int listen_fd, client_fd = -1;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200651
652/* Interruption handler to ensure clean exit (for valgrind testing) */
653#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200654static int received_sigterm = 0;
655void term_handler( int sig )
656{
657 ((void) sig);
658 received_sigterm = 1;
659 net_close( listen_fd ); /* causes net_accept() to abort */
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +0200660 net_close( client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200661}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200662#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200663
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000664int main( int argc, char *argv[] )
665{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +0000666 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200667 int version_suites[4][2];
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200668 unsigned char buf[IO_BUF_LEN];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200669#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200670 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +0200671 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +0200672 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +0200673#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200674 const char *pers = "ssl_server2";
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000675
676 entropy_context entropy;
677 ctr_drbg_context ctr_drbg;
678 ssl_context ssl;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100679#if defined(POLARSSL_SSL_RENEGOTIATION)
680 unsigned char renego_period[8] = { 0 };
681#endif
Paul Bakker36713e82013-09-17 13:25:29 +0200682#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200683 x509_crt cacert;
684 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200685 pk_context pkey;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200686 x509_crt srvcert2;
687 pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200688 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200689#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200690#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
691 dhm_context dhm;
692#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000693#if defined(POLARSSL_SSL_CACHE_C)
694 ssl_cache_context cache;
695#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100696#if defined(POLARSSL_SNI)
697 sni_entry *sni_info = NULL;
698#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200699#if defined(POLARSSL_SSL_ALPN)
700 const char *alpn_list[10];
701#endif
Hanno Becker6fd6d242017-05-25 17:51:31 +0100702#if defined(POLARSSL_SSL_SET_CURVES)
703 ecp_group_id curve_list[20];
704 const ecp_curve_info *curve_cur;
705#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200706#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
707 unsigned char alloc_buf[100000];
708#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000709
710 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000711 char *p, *q;
712 const int *list;
713
Paul Bakker82024bf2013-07-04 11:52:32 +0200714#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
715 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
716#endif
717
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000718 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200719 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000720 */
721 listen_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200722 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200723#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200724 x509_crt_init( &cacert );
725 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200726 pk_init( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200727 x509_crt_init( &srvcert2 );
728 pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200729#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200730#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
Paul Bakkera317a982014-06-18 16:44:11 +0200731 dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200732#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000733#if defined(POLARSSL_SSL_CACHE_C)
734 ssl_cache_init( &cache );
735#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200736#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200737 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200738#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000739
Paul Bakkerc1283d32014-08-18 11:05:51 +0200740#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100741 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200742 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100743 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +0200744#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200745
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000746 if( argc == 0 )
747 {
748 usage:
749 if( ret == 0 )
750 ret = 1;
751
Rich Evansf90016a2015-01-19 14:26:37 +0000752 polarssl_printf( USAGE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000753
754 list = ssl_list_ciphersuites();
755 while( *list )
756 {
Rich Evansf90016a2015-01-19 14:26:37 +0000757 polarssl_printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200758 list++;
759 if( !*list )
760 break;
Rich Evansf90016a2015-01-19 14:26:37 +0000761 polarssl_printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000762 list++;
763 }
Rich Evansf90016a2015-01-19 14:26:37 +0000764 polarssl_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000765 goto exit;
766 }
767
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100768 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000769 opt.server_port = DFL_SERVER_PORT;
770 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100771 opt.nbio = DFL_NBIO;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000772 opt.ca_file = DFL_CA_FILE;
773 opt.ca_path = DFL_CA_PATH;
774 opt.crt_file = DFL_CRT_FILE;
775 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200776 opt.crt_file2 = DFL_CRT_FILE2;
777 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200778 opt.psk = DFL_PSK;
779 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200780 opt.psk_list = DFL_PSK_LIST;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000781 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200782 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000783 opt.renegotiation = DFL_RENEGOTIATION;
784 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100785 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200786 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100787 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200788 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000789 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200790 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100791 opt.arc4 = DFL_ARC4;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100792 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200793 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100794 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200795 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100796 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100797 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100798 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100799 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200800 opt.alpn_string = DFL_ALPN_STRING;
Hanno Becker6fd6d242017-05-25 17:51:31 +0100801 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200802 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200803 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100804 opt.etm = DFL_ETM;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000805
806 for( i = 1; i < argc; i++ )
807 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000808 p = argv[i];
809 if( ( q = strchr( p, '=' ) ) == NULL )
810 goto usage;
811 *q++ = '\0';
812
813 if( strcmp( p, "server_port" ) == 0 )
814 {
815 opt.server_port = atoi( q );
816 if( opt.server_port < 1 || opt.server_port > 65535 )
817 goto usage;
818 }
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100819 else if( strcmp( p, "server_addr" ) == 0 )
820 opt.server_addr = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000821 else if( strcmp( p, "debug_level" ) == 0 )
822 {
823 opt.debug_level = atoi( q );
824 if( opt.debug_level < 0 || opt.debug_level > 65535 )
825 goto usage;
826 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100827 else if( strcmp( p, "nbio" ) == 0 )
828 {
829 opt.nbio = atoi( q );
830 if( opt.nbio < 0 || opt.nbio > 2 )
831 goto usage;
832 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000833 else if( strcmp( p, "ca_file" ) == 0 )
834 opt.ca_file = q;
835 else if( strcmp( p, "ca_path" ) == 0 )
836 opt.ca_path = q;
837 else if( strcmp( p, "crt_file" ) == 0 )
838 opt.crt_file = q;
839 else if( strcmp( p, "key_file" ) == 0 )
840 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200841 else if( strcmp( p, "crt_file2" ) == 0 )
842 opt.crt_file2 = q;
843 else if( strcmp( p, "key_file2" ) == 0 )
844 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200845 else if( strcmp( p, "dhm_file" ) == 0 )
846 opt.dhm_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200847 else if( strcmp( p, "psk" ) == 0 )
848 opt.psk = q;
849 else if( strcmp( p, "psk_identity" ) == 0 )
850 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200851 else if( strcmp( p, "psk_list" ) == 0 )
852 opt.psk_list = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000853 else if( strcmp( p, "force_ciphersuite" ) == 0 )
854 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000855 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
856
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200857 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000858 {
859 ret = 2;
860 goto usage;
861 }
862 opt.force_ciphersuite[1] = 0;
863 }
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200864 else if( strcmp( p, "version_suites" ) == 0 )
865 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000866 else if( strcmp( p, "renegotiation" ) == 0 )
867 {
868 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
869 SSL_RENEGOTIATION_DISABLED;
870 }
871 else if( strcmp( p, "allow_legacy" ) == 0 )
872 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100873 switch( atoi( q ) )
874 {
875 case -1: opt.allow_legacy = SSL_LEGACY_BREAK_HANDSHAKE; break;
876 case 0: opt.allow_legacy = SSL_LEGACY_NO_RENEGOTIATION; break;
877 case 1: opt.allow_legacy = SSL_LEGACY_ALLOW_RENEGOTIATION; break;
878 default: goto usage;
879 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000880 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100881 else if( strcmp( p, "renegotiate" ) == 0 )
882 {
883 opt.renegotiate = atoi( q );
884 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
885 goto usage;
886 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200887 else if( strcmp( p, "renego_delay" ) == 0 )
888 {
889 opt.renego_delay = atoi( q );
890 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100891 else if( strcmp( p, "renego_period" ) == 0 )
892 {
893 opt.renego_period = atoi( q );
894 if( opt.renego_period < 2 || opt.renego_period > 255 )
895 goto usage;
896 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200897 else if( strcmp( p, "exchanges" ) == 0 )
898 {
899 opt.exchanges = atoi( q );
900 if( opt.exchanges < 1 )
901 goto usage;
902 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000903 else if( strcmp( p, "min_version" ) == 0 )
904 {
905 if( strcmp( q, "ssl3" ) == 0 )
906 opt.min_version = SSL_MINOR_VERSION_0;
907 else if( strcmp( q, "tls1" ) == 0 )
908 opt.min_version = SSL_MINOR_VERSION_1;
909 else if( strcmp( q, "tls1_1" ) == 0 )
910 opt.min_version = SSL_MINOR_VERSION_2;
911 else if( strcmp( q, "tls1_2" ) == 0 )
912 opt.min_version = SSL_MINOR_VERSION_3;
913 else
914 goto usage;
915 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200916 else if( strcmp( p, "max_version" ) == 0 )
917 {
918 if( strcmp( q, "ssl3" ) == 0 )
919 opt.max_version = SSL_MINOR_VERSION_0;
920 else if( strcmp( q, "tls1" ) == 0 )
921 opt.max_version = SSL_MINOR_VERSION_1;
922 else if( strcmp( q, "tls1_1" ) == 0 )
923 opt.max_version = SSL_MINOR_VERSION_2;
924 else if( strcmp( q, "tls1_2" ) == 0 )
925 opt.max_version = SSL_MINOR_VERSION_3;
926 else
927 goto usage;
928 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100929 else if( strcmp( p, "arc4" ) == 0 )
930 {
931 switch( atoi( q ) )
932 {
933 case 0: opt.arc4 = SSL_ARC4_DISABLED; break;
934 case 1: opt.arc4 = SSL_ARC4_ENABLED; break;
935 default: goto usage;
936 }
937 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200938 else if( strcmp( p, "force_version" ) == 0 )
939 {
940 if( strcmp( q, "ssl3" ) == 0 )
941 {
942 opt.min_version = SSL_MINOR_VERSION_0;
943 opt.max_version = SSL_MINOR_VERSION_0;
944 }
945 else if( strcmp( q, "tls1" ) == 0 )
946 {
947 opt.min_version = SSL_MINOR_VERSION_1;
948 opt.max_version = SSL_MINOR_VERSION_1;
949 }
950 else if( strcmp( q, "tls1_1" ) == 0 )
951 {
952 opt.min_version = SSL_MINOR_VERSION_2;
953 opt.max_version = SSL_MINOR_VERSION_2;
954 }
955 else if( strcmp( q, "tls1_2" ) == 0 )
956 {
957 opt.min_version = SSL_MINOR_VERSION_3;
958 opt.max_version = SSL_MINOR_VERSION_3;
959 }
960 else
961 goto usage;
962 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100963 else if( strcmp( p, "auth_mode" ) == 0 )
964 {
965 if( strcmp( q, "none" ) == 0 )
966 opt.auth_mode = SSL_VERIFY_NONE;
967 else if( strcmp( q, "optional" ) == 0 )
968 opt.auth_mode = SSL_VERIFY_OPTIONAL;
969 else if( strcmp( q, "required" ) == 0 )
970 opt.auth_mode = SSL_VERIFY_REQUIRED;
971 else
972 goto usage;
973 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200974 else if( strcmp( p, "max_frag_len" ) == 0 )
975 {
976 if( strcmp( q, "512" ) == 0 )
977 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
978 else if( strcmp( q, "1024" ) == 0 )
979 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
980 else if( strcmp( q, "2048" ) == 0 )
981 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
982 else if( strcmp( q, "4096" ) == 0 )
983 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
984 else
985 goto usage;
986 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200987 else if( strcmp( p, "alpn" ) == 0 )
988 {
989 opt.alpn_string = q;
990 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100991 else if( strcmp( p, "trunc_hmac" ) == 0 )
992 {
993 switch( atoi( q ) )
994 {
995 case 0: opt.trunc_hmac = SSL_TRUNC_HMAC_DISABLED; break;
996 case 1: opt.trunc_hmac = SSL_TRUNC_HMAC_ENABLED; break;
997 default: goto usage;
998 }
999 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001000 else if( strcmp( p, "extended_ms" ) == 0 )
1001 {
1002 switch( atoi( q ) )
1003 {
1004 case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break;
1005 case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break;
1006 default: goto usage;
1007 }
1008 }
Hanno Becker6fd6d242017-05-25 17:51:31 +01001009 else if( strcmp( p, "curves" ) == 0 )
1010 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001011 else if( strcmp( p, "etm" ) == 0 )
1012 {
1013 switch( atoi( q ) )
1014 {
1015 case 0: opt.etm = SSL_ETM_DISABLED; break;
1016 case 1: opt.etm = SSL_ETM_ENABLED; break;
1017 default: goto usage;
1018 }
1019 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001020 else if( strcmp( p, "tickets" ) == 0 )
1021 {
1022 opt.tickets = atoi( q );
1023 if( opt.tickets < 0 || opt.tickets > 1 )
1024 goto usage;
1025 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001026 else if( strcmp( p, "ticket_timeout" ) == 0 )
1027 {
1028 opt.ticket_timeout = atoi( q );
1029 if( opt.ticket_timeout < 0 )
1030 goto usage;
1031 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001032 else if( strcmp( p, "cache_max" ) == 0 )
1033 {
1034 opt.cache_max = atoi( q );
1035 if( opt.cache_max < 0 )
1036 goto usage;
1037 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001038 else if( strcmp( p, "cache_timeout" ) == 0 )
1039 {
1040 opt.cache_timeout = atoi( q );
1041 if( opt.cache_timeout < 0 )
1042 goto usage;
1043 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001044 else if( strcmp( p, "sni" ) == 0 )
1045 {
1046 opt.sni = q;
1047 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001048 else
1049 goto usage;
1050 }
1051
Paul Bakkerc73079a2014-04-25 16:34:30 +02001052#if defined(POLARSSL_DEBUG_C)
1053 debug_set_threshold( opt.debug_level );
1054#endif
1055
Paul Bakkerc1516be2013-06-29 16:01:32 +02001056 if( opt.force_ciphersuite[0] > 0 )
1057 {
1058 const ssl_ciphersuite_t *ciphersuite_info;
1059 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1060
Paul Bakker5b55b792013-07-19 13:43:43 +02001061 if( opt.max_version != -1 &&
1062 ciphersuite_info->min_minor_ver > opt.max_version )
1063 {
Rich Evansf90016a2015-01-19 14:26:37 +00001064 polarssl_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakker5b55b792013-07-19 13:43:43 +02001065 ret = 2;
1066 goto usage;
1067 }
1068 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001069 ciphersuite_info->max_minor_ver < opt.min_version )
1070 {
Rich Evansf90016a2015-01-19 14:26:37 +00001071 polarssl_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +02001072 ret = 2;
1073 goto usage;
1074 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001075 if( opt.max_version > ciphersuite_info->max_minor_ver )
1076 opt.max_version = ciphersuite_info->max_minor_ver;
1077 if( opt.min_version < ciphersuite_info->min_minor_ver )
1078 opt.min_version = ciphersuite_info->min_minor_ver;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001079 }
1080
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001081 if( opt.version_suites != NULL )
1082 {
1083 const char *name[4] = { 0 };
1084
1085 /* Parse 4-element coma-separated list */
1086 for( i = 0, p = (char *) opt.version_suites;
1087 i < 4 && *p != '\0';
1088 i++ )
1089 {
1090 name[i] = p;
1091
1092 /* Terminate the current string and move on to next one */
1093 while( *p != ',' && *p != '\0' )
1094 p++;
1095 if( *p == ',' )
1096 *p++ = '\0';
1097 }
1098
1099 if( i != 4 )
1100 {
Rich Evansf90016a2015-01-19 14:26:37 +00001101 polarssl_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001102 ret = 1;
1103 goto exit;
1104 }
1105
1106 memset( version_suites, 0, sizeof( version_suites ) );
1107
1108 /* Get the suites identifiers from their name */
1109 for( i = 0; i < 4; i++ )
1110 {
1111 version_suites[i][0] = ssl_get_ciphersuite_id( name[i] );
1112
1113 if( version_suites[i][0] == 0 )
1114 {
Rich Evansf90016a2015-01-19 14:26:37 +00001115 polarssl_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001116 ret = 2;
1117 goto usage;
1118 }
1119 }
1120 }
1121
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001122#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001123 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001124 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001125 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001126 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001127 {
Rich Evansf90016a2015-01-19 14:26:37 +00001128 polarssl_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001129 goto exit;
1130 }
1131
1132 if( opt.psk_list != NULL )
1133 {
1134 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001135 {
Rich Evansf90016a2015-01-19 14:26:37 +00001136 polarssl_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001137 goto exit;
1138 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001139 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001140#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001141
Hanno Becker6fd6d242017-05-25 17:51:31 +01001142#if defined(POLARSSL_SSL_SET_CURVES)
1143 if( opt.curves != NULL )
1144 {
1145 p = (char *) opt.curves;
1146 i = 0;
1147
1148 if( strcmp( p, "none" ) == 0 )
1149 {
1150 curve_list[0] = POLARSSL_ECP_DP_NONE;
1151 }
1152 else if( strcmp( p, "default" ) != 0 )
1153 {
1154 /* Leave room for a final NULL in curve list */
1155 while( i < (int) ( sizeof( curve_list ) / sizeof( *curve_list ) ) - 1
1156 && *p != '\0' )
1157 {
1158 q = p;
1159
1160 /* Terminate the current string */
1161 while( *p != ',' && *p != '\0' )
1162 p++;
1163 if( *p == ',' )
1164 *p++ = '\0';
1165
1166 if( ( curve_cur = ecp_curve_info_from_name( q ) ) != NULL )
1167 {
1168 curve_list[i++] = curve_cur->grp_id;
1169 }
1170 else
1171 {
1172 polarssl_printf( "unknown curve %s\n", q );
1173 polarssl_printf( "supported curves: " );
1174 for( curve_cur = ecp_curve_list();
1175 curve_cur->grp_id != POLARSSL_ECP_DP_NONE;
1176 curve_cur++ )
1177 {
1178 polarssl_printf( "%s ", curve_cur->name );
1179 }
1180 polarssl_printf( "\n" );
1181 goto exit;
1182 }
1183 }
1184
1185 polarssl_printf( "Number of curves: %d\n", i );
1186
1187 if( i == (int) ( sizeof( curve_list ) / sizeof( *curve_list ) ) - 1
1188 && *p != '\0' )
1189 {
1190 polarssl_printf( "curves list too long, maximum %zu",
1191 (size_t) ( sizeof( curve_list ) / sizeof( *curve_list ) - 1 ) );
1192 goto exit;
1193 }
1194
1195 curve_list[i] = POLARSSL_ECP_DP_NONE;
1196 }
1197 }
1198#endif /* POLARSSL_SSL_SET_CURVES */
1199
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001200#if defined(POLARSSL_SSL_ALPN)
1201 if( opt.alpn_string != NULL )
1202 {
1203 p = (char *) opt.alpn_string;
1204 i = 0;
1205
1206 /* Leave room for a final NULL in alpn_list */
1207 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
1208 {
1209 alpn_list[i++] = p;
1210
1211 /* Terminate the current string and move on to next one */
1212 while( *p != ',' && *p != '\0' )
1213 p++;
1214 if( *p == ',' )
1215 *p++ = '\0';
1216 }
1217 }
1218#endif /* POLARSSL_SSL_ALPN */
1219
Paul Bakkerfbb17802013-04-17 19:10:21 +02001220 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001221 * 0. Initialize the RNG and the session data
1222 */
Rich Evansf90016a2015-01-19 14:26:37 +00001223 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001224 fflush( stdout );
1225
1226 entropy_init( &entropy );
1227 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001228 (const unsigned char *) pers,
1229 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001230 {
Rich Evansf90016a2015-01-19 14:26:37 +00001231 polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001232 goto exit;
1233 }
1234
Rich Evansf90016a2015-01-19 14:26:37 +00001235 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001236
Paul Bakker36713e82013-09-17 13:25:29 +02001237#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001238 /*
1239 * 1.1. Load the trusted CA
1240 */
Rich Evansf90016a2015-01-19 14:26:37 +00001241 polarssl_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001242 fflush( stdout );
1243
1244#if defined(POLARSSL_FS_IO)
1245 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001246 if( strcmp( opt.ca_path, "none" ) == 0 )
1247 ret = 0;
1248 else
1249 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001250 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001251 if( strcmp( opt.ca_file, "none" ) == 0 )
1252 ret = 0;
1253 else
1254 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001255 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001256#endif
1257#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +02001258 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
1259 strlen( test_ca_list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001260#else
1261 {
1262 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +00001263 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001264 }
1265#endif
1266 if( ret < 0 )
1267 {
Rich Evansf90016a2015-01-19 14:26:37 +00001268 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001269 goto exit;
1270 }
1271
Rich Evansf90016a2015-01-19 14:26:37 +00001272 polarssl_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001273
1274 /*
1275 * 1.2. Load own certificate and private key
1276 */
Rich Evansf90016a2015-01-19 14:26:37 +00001277 polarssl_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001278 fflush( stdout );
1279
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001280#if defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001281 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001282 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001283 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001284 if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
1285 {
Rich Evansf90016a2015-01-19 14:26:37 +00001286 polarssl_printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001287 -ret );
1288 goto exit;
1289 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001290 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001291 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001292 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001293 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001294 if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
1295 {
Rich Evansf90016a2015-01-19 14:26:37 +00001296 polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001297 goto exit;
1298 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001299 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001300 if( key_cert_init == 1 )
1301 {
Rich Evansf90016a2015-01-19 14:26:37 +00001302 polarssl_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001303 goto exit;
1304 }
1305
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001306 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001307 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001308 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001309 if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
1310 {
Rich Evansf90016a2015-01-19 14:26:37 +00001311 polarssl_printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001312 -ret );
1313 goto exit;
1314 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001315 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001316 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001317 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001318 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001319 if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
1320 {
Rich Evansf90016a2015-01-19 14:26:37 +00001321 polarssl_printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001322 -ret );
1323 goto exit;
1324 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001325 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001326 if( key_cert_init2 == 1 )
1327 {
Rich Evansf90016a2015-01-19 14:26:37 +00001328 polarssl_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001329 goto exit;
1330 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001331#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001332 if( key_cert_init == 0 &&
1333 strcmp( opt.crt_file, "none" ) != 0 &&
1334 strcmp( opt.key_file, "none" ) != 0 &&
1335 key_cert_init2 == 0 &&
1336 strcmp( opt.crt_file2, "none" ) != 0 &&
1337 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001338 {
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001339#if !defined(POLARSSL_CERTS_C)
Rich Evansf90016a2015-01-19 14:26:37 +00001340 polarssl_printf( "Not certificated or key provided, and \n"
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001341 "POLARSSL_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001342 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001343#else
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001344#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001345 if( ( ret = x509_crt_parse( &srvcert,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001346 (const unsigned char *) test_srv_crt_rsa,
1347 strlen( test_srv_crt_rsa ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001348 {
Rich Evansf90016a2015-01-19 14:26:37 +00001349 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001350 goto exit;
1351 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001352 if( ( ret = pk_parse_key( &pkey,
1353 (const unsigned char *) test_srv_key_rsa,
1354 strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001355 {
Rich Evansf90016a2015-01-19 14:26:37 +00001356 polarssl_printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001357 goto exit;
1358 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001359 key_cert_init = 2;
1360#endif /* POLARSSL_RSA_C */
1361#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001362 if( ( ret = x509_crt_parse( &srvcert2,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001363 (const unsigned char *) test_srv_crt_ec,
1364 strlen( test_srv_crt_ec ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001365 {
Rich Evansf90016a2015-01-19 14:26:37 +00001366 polarssl_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001367 goto exit;
1368 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001369 if( ( ret = pk_parse_key( &pkey2,
1370 (const unsigned char *) test_srv_key_ec,
1371 strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001372 {
Rich Evansf90016a2015-01-19 14:26:37 +00001373 polarssl_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001374 goto exit;
1375 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001376 key_cert_init2 = 2;
1377#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001378#endif /* POLARSSL_CERTS_C */
1379 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001380
Rich Evansf90016a2015-01-19 14:26:37 +00001381 polarssl_printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001382#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001383
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001384#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1385 if( opt.dhm_file != NULL )
1386 {
Rich Evansf90016a2015-01-19 14:26:37 +00001387 polarssl_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001388 fflush( stdout );
1389
1390 if( ( ret = dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
1391 {
Rich Evansf90016a2015-01-19 14:26:37 +00001392 polarssl_printf( " failed\n ! dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001393 -ret );
1394 goto exit;
1395 }
1396
Rich Evansf90016a2015-01-19 14:26:37 +00001397 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001398 }
1399#endif
1400
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001401#if defined(POLARSSL_SNI)
1402 if( opt.sni != NULL )
1403 {
Rich Evansf90016a2015-01-19 14:26:37 +00001404 polarssl_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001405 fflush( stdout );
1406
1407 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
1408 {
Rich Evansf90016a2015-01-19 14:26:37 +00001409 polarssl_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001410 goto exit;
1411 }
1412
Rich Evansf90016a2015-01-19 14:26:37 +00001413 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001414 }
1415#endif /* POLARSSL_SNI */
1416
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001417 /*
1418 * 2. Setup the listening TCP socket
1419 */
Rich Evansf90016a2015-01-19 14:26:37 +00001420 polarssl_printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001421 fflush( stdout );
1422
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001423 if( ( ret = net_bind( &listen_fd, opt.server_addr,
1424 opt.server_port ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001425 {
Rich Evansf90016a2015-01-19 14:26:37 +00001426 polarssl_printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001427 goto exit;
1428 }
1429
Rich Evansf90016a2015-01-19 14:26:37 +00001430 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001431
1432 /*
1433 * 3. Setup stuff
1434 */
Rich Evansf90016a2015-01-19 14:26:37 +00001435 polarssl_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001436 fflush( stdout );
1437
1438 if( ( ret = ssl_init( &ssl ) ) != 0 )
1439 {
Rich Evansf90016a2015-01-19 14:26:37 +00001440 polarssl_printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001441 goto exit;
1442 }
1443
1444 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001445 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001446
Paul Bakker05decb22013-08-15 13:33:48 +02001447#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001448 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1449 {
Rich Evansf90016a2015-01-19 14:26:37 +00001450 polarssl_printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001451 goto exit;
1452 };
Paul Bakker05decb22013-08-15 13:33:48 +02001453#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001454
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001455#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1456 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
1457 ssl_set_truncated_hmac( &ssl, opt.trunc_hmac );
1458#endif
1459
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001460#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1461 if( opt.extended_ms != DFL_EXTENDED_MS )
1462 ssl_set_extended_master_secret( &ssl, opt.extended_ms );
1463#endif
1464
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001465#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1466 if( opt.etm != DFL_ETM )
1467 ssl_set_encrypt_then_mac( &ssl, opt.etm );
1468#endif
1469
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001470#if defined(POLARSSL_SSL_ALPN)
1471 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001472 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1473 {
Rich Evansf90016a2015-01-19 14:26:37 +00001474 polarssl_printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001475 goto exit;
1476 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001477#endif
1478
Hanno Becker6fd6d242017-05-25 17:51:31 +01001479#if defined(POLARSSL_SSL_SET_CURVES)
1480 if( opt.curves != NULL &&
1481 strcmp( opt.curves, "default" ) != 0 )
1482 {
1483 ssl_set_curves( &ssl, curve_list );
1484 }
1485#endif
1486
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001487 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
1488 ssl_set_dbg( &ssl, my_debug, stdout );
1489
Paul Bakker0a597072012-09-25 21:55:46 +00001490#if defined(POLARSSL_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001491 if( opt.cache_max != -1 )
1492 ssl_cache_set_max_entries( &cache, opt.cache_max );
1493
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001494 if( opt.cache_timeout != -1 )
1495 ssl_cache_set_timeout( &cache, opt.cache_timeout );
1496
Paul Bakker0a597072012-09-25 21:55:46 +00001497 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
1498 ssl_cache_set, &cache );
1499#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001500
Paul Bakkera503a632013-08-14 13:48:06 +02001501#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001502 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1503 {
Rich Evansf90016a2015-01-19 14:26:37 +00001504 polarssl_printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001505 goto exit;
1506 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001507
1508 if( opt.ticket_timeout != -1 )
1509 ssl_set_session_ticket_lifetime( &ssl, opt.ticket_timeout );
Paul Bakkera503a632013-08-14 13:48:06 +02001510#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001511
Paul Bakker41c83d32013-03-20 14:39:14 +01001512 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001513 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001514 else
1515 ssl_set_arc4_support( &ssl, opt.arc4 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001516
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001517 if( opt.version_suites != NULL )
1518 {
1519 ssl_set_ciphersuites_for_version( &ssl, version_suites[0],
1520 SSL_MAJOR_VERSION_3,
1521 SSL_MINOR_VERSION_0 );
1522 ssl_set_ciphersuites_for_version( &ssl, version_suites[1],
1523 SSL_MAJOR_VERSION_3,
1524 SSL_MINOR_VERSION_1 );
1525 ssl_set_ciphersuites_for_version( &ssl, version_suites[2],
1526 SSL_MAJOR_VERSION_3,
1527 SSL_MINOR_VERSION_2 );
1528 ssl_set_ciphersuites_for_version( &ssl, version_suites[3],
1529 SSL_MAJOR_VERSION_3,
1530 SSL_MINOR_VERSION_3 );
1531 }
1532
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001533 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
1534 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001535#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001536 ssl_set_renegotiation( &ssl, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001537
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001538 if( opt.renego_delay != DFL_RENEGO_DELAY )
1539 ssl_set_renegotiation_enforced( &ssl, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001540
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001541 if( opt.renego_period != DFL_RENEGO_PERIOD )
1542 {
1543 renego_period[7] = opt.renego_period;
1544 ssl_set_renegotiation_period( &ssl, renego_period );
1545 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001546#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001547
Paul Bakker36713e82013-09-17 13:25:29 +02001548#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001549 if( strcmp( opt.ca_path, "none" ) != 0 &&
1550 strcmp( opt.ca_file, "none" ) != 0 )
1551 {
1552 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
1553 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001554 if( key_cert_init )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001555 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
1556 {
Rich Evansf90016a2015-01-19 14:26:37 +00001557 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001558 goto exit;
1559 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001560 if( key_cert_init2 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001561 if( ( ret = ssl_set_own_cert( &ssl, &srvcert2, &pkey2 ) ) != 0 )
1562 {
Rich Evansf90016a2015-01-19 14:26:37 +00001563 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001564 goto exit;
1565 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001566#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001567
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001568#if defined(POLARSSL_SNI)
1569 if( opt.sni != NULL )
1570 ssl_set_sni( &ssl, sni_callback, sni_info );
1571#endif
1572
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001573#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001574 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
1575 {
1576 ret = ssl_set_psk( &ssl, psk, psk_len,
1577 (const unsigned char *) opt.psk_identity,
1578 strlen( opt.psk_identity ) );
1579 if( ret != 0 )
1580 {
Rich Evansf90016a2015-01-19 14:26:37 +00001581 polarssl_printf( " failed\n ssl_set_psk returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001582 goto exit;
1583 }
1584 }
1585
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001586 if( opt.psk_list != NULL )
1587 ssl_set_psk_cb( &ssl, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02001588#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001589
1590#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00001591 /*
1592 * Use different group than default DHM group
1593 */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001594#if defined(POLARSSL_FS_IO)
1595 if( opt.dhm_file != NULL )
1596 ret = ssl_set_dh_param_ctx( &ssl, &dhm );
1597 else
1598#endif
1599 ret = ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
1600 POLARSSL_DHM_RFC5114_MODP_2048_G );
1601
1602 if( ret != 0 )
1603 {
Rich Evansf90016a2015-01-19 14:26:37 +00001604 polarssl_printf( " failed\n ssl_set_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001605 goto exit;
1606 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001607#endif
1608
Paul Bakker1d29fb52012-09-28 13:28:45 +00001609 if( opt.min_version != -1 )
1610 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1611
Paul Bakkerc1516be2013-06-29 16:01:32 +02001612 if( opt.max_version != -1 )
1613 ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1614
Rich Evansf90016a2015-01-19 14:26:37 +00001615 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001616
1617reset:
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +02001618#if !defined(_WIN32)
1619 if( received_sigterm )
1620 {
Manuel Pégourié-Gonnard7e81e702015-01-29 11:47:41 +00001621 polarssl_printf( " interrupted by SIGTERM\n" );
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +02001622 ret = 0;
1623 goto exit;
1624 }
1625#endif
1626
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001627#ifdef POLARSSL_ERROR_C
1628 if( ret != 0 )
1629 {
1630 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001631 polarssl_strerror( ret, error_buf, 100 );
Rich Evansf90016a2015-01-19 14:26:37 +00001632 polarssl_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001633 }
1634#endif
1635
1636 if( client_fd != -1 )
1637 net_close( client_fd );
1638
1639 ssl_session_reset( &ssl );
1640
1641 /*
1642 * 3. Wait until a client connects
1643 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001644 client_fd = -1;
1645
Rich Evansf90016a2015-01-19 14:26:37 +00001646 polarssl_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001647 fflush( stdout );
1648
1649 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
1650 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02001651#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001652 if( received_sigterm )
1653 {
Rich Evansf90016a2015-01-19 14:26:37 +00001654 polarssl_printf( " interrupted by signal\n" );
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001655 ret = 0;
1656 goto exit;
1657 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02001658#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001659
Rich Evansf90016a2015-01-19 14:26:37 +00001660 polarssl_printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001661 goto exit;
1662 }
1663
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001664 if( opt.nbio > 0 )
1665 ret = net_set_nonblock( client_fd );
1666 else
1667 ret = net_set_block( client_fd );
1668 if( ret != 0 )
1669 {
Rich Evansf90016a2015-01-19 14:26:37 +00001670 polarssl_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001671 goto exit;
1672 }
1673
1674 if( opt.nbio == 2 )
1675 ssl_set_bio( &ssl, my_recv, &client_fd, my_send, &client_fd );
1676 else
1677 ssl_set_bio( &ssl, net_recv, &client_fd, net_send, &client_fd );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001678
Rich Evansf90016a2015-01-19 14:26:37 +00001679 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001680
1681 /*
1682 * 4. Handshake
1683 */
Rich Evansf90016a2015-01-19 14:26:37 +00001684 polarssl_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001685 fflush( stdout );
1686
1687 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1688 {
1689 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1690 {
Rich Evansf90016a2015-01-19 14:26:37 +00001691 polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001692 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001693 }
1694 }
1695
Rich Evansf90016a2015-01-19 14:26:37 +00001696 polarssl_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Manuel Pégourié-Gonnardc580a002014-02-12 10:15:30 +01001697 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001698
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001699#if defined(POLARSSL_SSL_ALPN)
1700 if( opt.alpn_string != NULL )
1701 {
1702 const char *alp = ssl_get_alpn_protocol( &ssl );
Rich Evansf90016a2015-01-19 14:26:37 +00001703 polarssl_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001704 alp ? alp : "(none)" );
1705 }
1706#endif
1707
Paul Bakker36713e82013-09-17 13:25:29 +02001708#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001709 /*
1710 * 5. Verify the server certificate
1711 */
Rich Evansf90016a2015-01-19 14:26:37 +00001712 polarssl_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001713
1714 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1715 {
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +02001716 char vrfy_buf[512];
1717
Rich Evansf90016a2015-01-19 14:26:37 +00001718 polarssl_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001719
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +02001720 x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001721
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +02001722 polarssl_printf( "%s\n", vrfy_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001723 }
1724 else
Rich Evansf90016a2015-01-19 14:26:37 +00001725 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001726
Paul Bakkerb0550d92012-10-30 07:51:03 +00001727 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001728 {
Rich Evansf90016a2015-01-19 14:26:37 +00001729 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001730 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1731 ssl_get_peer_cert( &ssl ) );
Rich Evansf90016a2015-01-19 14:26:37 +00001732 polarssl_printf( "%s\n", buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001733 }
Paul Bakker36713e82013-09-17 13:25:29 +02001734#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001735
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001736 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001737data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001738 /*
1739 * 6. Read the HTTP Request
1740 */
Rich Evansf90016a2015-01-19 14:26:37 +00001741 polarssl_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001742 fflush( stdout );
1743
1744 do
1745 {
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001746 int terminated = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001747 len = sizeof( buf ) - 1;
1748 memset( buf, 0, sizeof( buf ) );
1749 ret = ssl_read( &ssl, buf, len );
1750
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001751 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1752 ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001753 continue;
1754
1755 if( ret <= 0 )
1756 {
1757 switch( ret )
1758 {
1759 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Rich Evansf90016a2015-01-19 14:26:37 +00001760 polarssl_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001761 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001762
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001763 case 0:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001764 case POLARSSL_ERR_NET_CONN_RESET:
Rich Evansf90016a2015-01-19 14:26:37 +00001765 polarssl_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001766 ret = POLARSSL_ERR_NET_CONN_RESET;
1767 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001768
1769 default:
Rich Evansf90016a2015-01-19 14:26:37 +00001770 polarssl_printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001771 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001772 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001773 }
1774
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001775 if( ssl_get_bytes_avail( &ssl ) == 0 )
1776 {
1777 len = ret;
1778 buf[len] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +00001779 polarssl_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001780
1781 /* End of message should be detected according to the syntax of the
1782 * application protocol (eg HTTP), just use a dummy test here. */
1783 if( buf[len - 1] == '\n' )
1784 terminated = 1;
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001785 }
1786 else
1787 {
1788 int extra_len, ori_len;
1789 unsigned char *larger_buf;
1790
1791 ori_len = ret;
1792 extra_len = ssl_get_bytes_avail( &ssl );
1793
1794 larger_buf = polarssl_malloc( ori_len + extra_len + 1 );
1795 if( larger_buf == NULL )
1796 {
Rich Evansf90016a2015-01-19 14:26:37 +00001797 polarssl_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001798 ret = 1;
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001799 goto reset;
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001800 }
1801
1802 memset( larger_buf, 0, ori_len + extra_len );
1803 memcpy( larger_buf, buf, ori_len );
1804
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +02001805 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001806 ret = ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +02001807 if( ret != extra_len ||
1808 ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001809 {
Rich Evansf90016a2015-01-19 14:26:37 +00001810 polarssl_printf( " ! ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001811 ret = 1;
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001812 goto reset;
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001813 }
1814
1815 larger_buf[ori_len + extra_len] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +00001816 polarssl_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnard0669f272014-06-18 13:07:20 +02001817 ori_len + extra_len, ori_len, extra_len,
1818 (char *) larger_buf );
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001819
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001820 /* End of message should be detected according to the syntax of the
1821 * application protocol (eg HTTP), just use a dummy test here. */
1822 if( larger_buf[ori_len + extra_len - 1] == '\n' )
1823 terminated = 1;
1824
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001825 polarssl_free( larger_buf );
1826 }
1827
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001828 if( terminated )
1829 {
1830 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001831 break;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001832 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001833 }
1834 while( 1 );
1835
1836 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001837 * 7a. Request renegotiation while client is waiting for input from us.
1838 * (only if we're going to exhange more data afterwards)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001839 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001840#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001841 if( opt.renegotiate && exchanges_left > 1 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001842 {
Rich Evansf90016a2015-01-19 14:26:37 +00001843 polarssl_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001844 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001845
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001846 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
1847 {
1848 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1849 ret != POLARSSL_ERR_NET_WANT_WRITE )
1850 {
Rich Evansf90016a2015-01-19 14:26:37 +00001851 polarssl_printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001852 goto reset;
1853 }
1854 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001855
Rich Evansf90016a2015-01-19 14:26:37 +00001856 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001857 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001858#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001859
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001860 /*
1861 * 7. Write the 200 Response
1862 */
Rich Evansf90016a2015-01-19 14:26:37 +00001863 polarssl_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001864 fflush( stdout );
1865
1866 len = sprintf( (char *) buf, HTTP_RESPONSE,
1867 ssl_get_ciphersuite( &ssl ) );
1868
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001869 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001870 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001871 while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001872 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001873 if( ret == POLARSSL_ERR_NET_CONN_RESET )
1874 {
Rich Evansf90016a2015-01-19 14:26:37 +00001875 polarssl_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001876 goto reset;
1877 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001878
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001879 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1880 {
Rich Evansf90016a2015-01-19 14:26:37 +00001881 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001882 goto reset;
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001883 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001884 }
1885 }
1886
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001887 buf[written] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +00001888 polarssl_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Manuel Pégourié-Gonnarda92ed482015-01-14 10:46:08 +01001889 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001890
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001891 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001892 * 7b. Continue doing data exchanges?
1893 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001894 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001895 goto data_exchange;
1896
1897 /*
1898 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001899 */
1900close_notify:
Rich Evansf90016a2015-01-19 14:26:37 +00001901 polarssl_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001902
Manuel Pégourié-Gonnard34377b12015-01-22 10:46:46 +00001903 /* No error checking, the connection might be closed already */
1904 do ret = ssl_close_notify( &ssl );
1905 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
1906 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001907
Rich Evansf90016a2015-01-19 14:26:37 +00001908 polarssl_printf( " done\n" );
1909
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001910 goto reset;
1911
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001912 /*
1913 * Cleanup and exit
1914 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001915exit:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001916#ifdef POLARSSL_ERROR_C
1917 if( ret != 0 )
1918 {
1919 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001920 polarssl_strerror( ret, error_buf, 100 );
Rich Evansf90016a2015-01-19 14:26:37 +00001921 polarssl_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001922 }
1923#endif
1924
Manuel Pégourié-Gonnard7e81e702015-01-29 11:47:41 +00001925 polarssl_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01001926 fflush( stdout );
1927
Paul Bakker0c226102014-04-17 16:02:36 +02001928 if( client_fd != -1 )
1929 net_close( client_fd );
1930
Paul Bakkera317a982014-06-18 16:44:11 +02001931#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1932 dhm_free( &dhm );
1933#endif
Paul Bakker36713e82013-09-17 13:25:29 +02001934#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker36713e82013-09-17 13:25:29 +02001935 x509_crt_free( &cacert );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001936 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02001937 pk_free( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001938 x509_crt_free( &srvcert2 );
1939 pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02001940#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001941#if defined(POLARSSL_SNI)
1942 sni_free( sni_info );
1943#endif
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02001944#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1945 psk_free( psk_info );
1946#endif
1947#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1948 dhm_free( &dhm );
1949#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001950
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001951 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02001952 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02001953 entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001954
Paul Bakker0a597072012-09-25 21:55:46 +00001955#if defined(POLARSSL_SSL_CACHE_C)
1956 ssl_cache_free( &cache );
1957#endif
1958
Paul Bakker1337aff2013-09-29 14:45:34 +02001959#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1960#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker82024bf2013-07-04 11:52:32 +02001961 memory_buffer_alloc_status();
1962#endif
Paul Bakker1337aff2013-09-29 14:45:34 +02001963 memory_buffer_alloc_free();
1964#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02001965
Manuel Pégourié-Gonnard7e81e702015-01-29 11:47:41 +00001966 polarssl_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01001967
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001968#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +00001969 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001970 fflush( stdout ); getchar();
1971#endif
1972
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001973 // Shell can not handle large exit numbers -> 1 for errors
1974 if( ret < 0 )
1975 ret = 1;
1976
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001977 return( ret );
1978}
1979#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
1980 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
1981 POLARSSL_CTR_DRBG_C */