blob: f7f79abd976025cef7bc6dc526a91468c814bdbe [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" \
Ron Eldor4ca4fd72017-09-26 11:29:11 +0300257 " server_addr=%%s 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
Hanno Becker569a4f42017-06-09 16:26:04 +0100306
307#define ALPN_LIST_SIZE 10
308#define CURVE_LIST_SIZE 20
309
Rich Evans85b05ec2015-02-12 11:37:29 +0000310/*
311 * global options
312 */
313struct options
314{
315 const char *server_addr; /* address on which the ssl service runs */
316 int server_port; /* port on which the ssl service runs */
317 int debug_level; /* level of debugging */
318 int nbio; /* should I/O be blocking? */
319 const char *ca_file; /* the file with the CA certificate(s) */
320 const char *ca_path; /* the path with the CA certificate(s) reside */
321 const char *crt_file; /* the file with the server certificate */
322 const char *key_file; /* the file with the server key */
323 const char *crt_file2; /* the file with the 2nd server certificate */
324 const char *key_file2; /* the file with the 2nd server key */
325 const char *psk; /* the pre-shared key */
326 const char *psk_identity; /* the pre-shared key identity */
327 char *psk_list; /* list of PSK id/key pairs for callback */
328 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
329 const char *version_suites; /* per-version ciphersuites */
330 int renegotiation; /* enable / disable renegotiation */
331 int allow_legacy; /* allow legacy renegotiation */
332 int renegotiate; /* attempt renegotiation? */
333 int renego_delay; /* delay before enforcing renegotiation */
334 int renego_period; /* period for automatic renegotiation */
335 int exchanges; /* number of data exchanges */
336 int min_version; /* minimum protocol version accepted */
337 int max_version; /* maximum protocol version accepted */
338 int arc4; /* flag for arc4 suites support */
339 int auth_mode; /* verify mode for connection */
340 unsigned char mfl_code; /* code for maximum fragment length */
341 int trunc_hmac; /* accept truncated hmac? */
342 int tickets; /* enable / disable session tickets */
343 int ticket_timeout; /* session ticket lifetime */
344 int cache_max; /* max number of session cache entries */
345 int cache_timeout; /* expiration delay of session cache entries */
346 char *sni; /* string describing sni information */
Hanno Becker6fd6d242017-05-25 17:51:31 +0100347 const char *curves; /* list of supported elliptic curves */
Rich Evans85b05ec2015-02-12 11:37:29 +0000348 const char *alpn_string; /* ALPN supported protocols */
349 const char *dhm_file; /* the file with the DH parameters */
350 int extended_ms; /* allow negotiation of extended MS? */
351 int etm; /* allow negotiation of encrypt-then-MAC? */
352} opt;
353
354static void my_debug( void *ctx, int level, const char *str )
355{
356 ((void) level);
357
358 polarssl_fprintf( (FILE *) ctx, "%s", str );
359 fflush( (FILE *) ctx );
360}
361
362/*
363 * Test recv/send functions that make sure each try returns
364 * WANT_READ/WANT_WRITE at least once before sucesseding
365 */
366static int my_recv( void *ctx, unsigned char *buf, size_t len )
367{
368 static int first_try = 1;
369 int ret;
370
371 if( first_try )
372 {
373 first_try = 0;
374 return( POLARSSL_ERR_NET_WANT_READ );
375 }
376
377 ret = net_recv( ctx, buf, len );
378 if( ret != POLARSSL_ERR_NET_WANT_READ )
379 first_try = 1; /* Next call will be a new operation */
380 return( ret );
381}
382
383static int my_send( void *ctx, const unsigned char *buf, size_t len )
384{
385 static int first_try = 1;
386 int ret;
387
388 if( first_try )
389 {
390 first_try = 0;
391 return( POLARSSL_ERR_NET_WANT_WRITE );
392 }
393
394 ret = net_send( ctx, buf, len );
395 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
396 first_try = 1; /* Next call will be a new operation */
397 return( ret );
398}
399
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200400/*
401 * Used by sni_parse and psk_parse to handle coma-separated lists
402 */
403#define GET_ITEM( dst ) \
404 dst = p; \
405 while( *p != ',' ) \
406 if( ++p > end ) \
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000407 goto error; \
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200408 *p++ = '\0';
409
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100410#if defined(POLARSSL_SNI)
411typedef struct _sni_entry sni_entry;
412
413struct _sni_entry {
414 const char *name;
415 x509_crt *cert;
416 pk_context *key;
417 sni_entry *next;
418};
419
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100420void sni_free( sni_entry *head )
421{
422 sni_entry *cur = head, *next;
423
424 while( cur != NULL )
425 {
426 x509_crt_free( cur->cert );
427 polarssl_free( cur->cert );
428
429 pk_free( cur->key );
430 polarssl_free( cur->key );
431
432 next = cur->next;
433 polarssl_free( cur );
434 cur = next;
435 }
436}
437
438/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000439 * Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]]
440 * into a usable sni_entry list.
441 *
442 * Modifies the input string! This is not production quality!
443 */
444sni_entry *sni_parse( char *sni_string )
445{
446 sni_entry *cur = NULL, *new = NULL;
447 char *p = sni_string;
448 char *end = p;
449 char *crt_file, *key_file;
450
451 while( *end != '\0' )
452 ++end;
453 *end = ',';
454
455 while( p <= end )
456 {
457 if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL )
458 {
459 sni_free( cur );
460 return( NULL );
461 }
462
463 memset( new, 0, sizeof( sni_entry ) );
464
465 if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ||
466 ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
467 {
468 polarssl_free( new->cert );
469 polarssl_free( new );
470 sni_free( cur );
471 return( NULL );
472 }
473
474 x509_crt_init( new->cert );
475 pk_init( new->key );
476
477 GET_ITEM( new->name );
478 GET_ITEM( crt_file );
479 GET_ITEM( key_file );
480
481 if( x509_crt_parse_file( new->cert, crt_file ) != 0 ||
482 pk_parse_keyfile( new->key, key_file, "" ) != 0 )
483 {
484 goto error;
485 }
486
487 new->next = cur;
488 cur = new;
489 }
490
491 return( cur );
492
493error:
494 sni_free( new );
495 sni_free( cur );
496 return( NULL );
497}
498
499/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100500 * SNI callback.
501 */
502int sni_callback( void *p_info, ssl_context *ssl,
503 const unsigned char *name, size_t name_len )
504{
505 sni_entry *cur = (sni_entry *) p_info;
506
507 while( cur != NULL )
508 {
509 if( name_len == strlen( cur->name ) &&
510 memcmp( name, cur->name, name_len ) == 0 )
511 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200512 return( ssl_set_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100513 }
514
515 cur = cur->next;
516 }
517
518 return( -1 );
519}
520
521#endif /* POLARSSL_SNI */
522
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200523#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
524
525#define HEX2NUM( c ) \
526 if( c >= '0' && c <= '9' ) \
527 c -= '0'; \
528 else if( c >= 'a' && c <= 'f' ) \
529 c -= 'a' - 10; \
530 else if( c >= 'A' && c <= 'F' ) \
531 c -= 'A' - 10; \
532 else \
533 return( -1 );
534
535/*
536 * Convert a hex string to bytes.
537 * Return 0 on success, -1 on error.
538 */
539int unhexify( unsigned char *output, const char *input, size_t *olen )
540{
541 unsigned char c;
542 size_t j;
543
544 *olen = strlen( input );
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200545 if( *olen % 2 != 0 || *olen / 2 > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200546 return( -1 );
547 *olen /= 2;
548
549 for( j = 0; j < *olen * 2; j += 2 )
550 {
551 c = input[j];
552 HEX2NUM( c );
553 output[ j / 2 ] = c << 4;
554
555 c = input[j + 1];
556 HEX2NUM( c );
557 output[ j / 2 ] |= c;
558 }
559
560 return( 0 );
561}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200562
563typedef struct _psk_entry psk_entry;
564
565struct _psk_entry
566{
567 const char *name;
568 size_t key_len;
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200569 unsigned char key[POLARSSL_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200570 psk_entry *next;
571};
572
573/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000574 * Free a list of psk_entry's
575 */
576void psk_free( psk_entry *head )
577{
578 psk_entry *next;
579
580 while( head != NULL )
581 {
582 next = head->next;
583 polarssl_free( head );
584 head = next;
585 }
586}
587
588/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200589 * Parse a string of pairs name1,key1[,name2,key2[,...]]
590 * into a usable psk_entry list.
591 *
592 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200593 */
594psk_entry *psk_parse( char *psk_string )
595{
596 psk_entry *cur = NULL, *new = NULL;
597 char *p = psk_string;
598 char *end = p;
599 char *key_hex;
600
601 while( *end != '\0' )
602 ++end;
603 *end = ',';
604
605 while( p <= end )
606 {
607 if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL )
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +0000608 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200609
610 memset( new, 0, sizeof( psk_entry ) );
611
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200612 GET_ITEM( new->name );
613 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200614
615 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000616 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200617
618 new->next = cur;
619 cur = new;
620 }
621
622 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200623
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000624error:
625 psk_free( new );
626 psk_free( cur );
627 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200628}
629
630/*
631 * PSK callback
632 */
633int psk_callback( void *p_info, ssl_context *ssl,
634 const unsigned char *name, size_t name_len )
635{
636 psk_entry *cur = (psk_entry *) p_info;
637
638 while( cur != NULL )
639 {
640 if( name_len == strlen( cur->name ) &&
641 memcmp( name, cur->name, name_len ) == 0 )
642 {
643 return( ssl_set_psk( ssl, cur->key, cur->key_len,
644 name, name_len ) );
645 }
646
647 cur = cur->next;
648 }
649
650 return( -1 );
651}
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200652#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
653
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +0200654static int listen_fd, client_fd = -1;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200655
656/* Interruption handler to ensure clean exit (for valgrind testing) */
657#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200658static int received_sigterm = 0;
659void term_handler( int sig )
660{
661 ((void) sig);
662 received_sigterm = 1;
663 net_close( listen_fd ); /* causes net_accept() to abort */
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +0200664 net_close( client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200665}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200666#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200667
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000668int main( int argc, char *argv[] )
669{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +0000670 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200671 int version_suites[4][2];
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200672 unsigned char buf[IO_BUF_LEN];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200673#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200674 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +0200675 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +0200676 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +0200677#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200678 const char *pers = "ssl_server2";
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000679
680 entropy_context entropy;
681 ctr_drbg_context ctr_drbg;
682 ssl_context ssl;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100683#if defined(POLARSSL_SSL_RENEGOTIATION)
684 unsigned char renego_period[8] = { 0 };
685#endif
Paul Bakker36713e82013-09-17 13:25:29 +0200686#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200687 x509_crt cacert;
688 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200689 pk_context pkey;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200690 x509_crt srvcert2;
691 pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200692 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200693#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200694#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
695 dhm_context dhm;
696#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000697#if defined(POLARSSL_SSL_CACHE_C)
698 ssl_cache_context cache;
699#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100700#if defined(POLARSSL_SNI)
701 sni_entry *sni_info = NULL;
702#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200703#if defined(POLARSSL_SSL_ALPN)
Hanno Becker569a4f42017-06-09 16:26:04 +0100704 const char *alpn_list[ALPN_LIST_SIZE];
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200705#endif
Hanno Becker6fd6d242017-05-25 17:51:31 +0100706#if defined(POLARSSL_SSL_SET_CURVES)
Hanno Becker569a4f42017-06-09 16:26:04 +0100707 ecp_group_id curve_list[CURVE_LIST_SIZE];
Hanno Becker6fd6d242017-05-25 17:51:31 +0100708 const ecp_curve_info *curve_cur;
709#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200710#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
711 unsigned char alloc_buf[100000];
712#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000713
714 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000715 char *p, *q;
716 const int *list;
717
Paul Bakker82024bf2013-07-04 11:52:32 +0200718#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
719 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
720#endif
721
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000722 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200723 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000724 */
725 listen_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200726 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200727#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200728 x509_crt_init( &cacert );
729 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200730 pk_init( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200731 x509_crt_init( &srvcert2 );
732 pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200733#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200734#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
Paul Bakkera317a982014-06-18 16:44:11 +0200735 dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200736#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000737#if defined(POLARSSL_SSL_CACHE_C)
738 ssl_cache_init( &cache );
739#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200740#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200741 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200742#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000743
Paul Bakkerc1283d32014-08-18 11:05:51 +0200744#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100745 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200746 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100747 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +0200748#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200749
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000750 if( argc == 0 )
751 {
752 usage:
753 if( ret == 0 )
754 ret = 1;
755
Rich Evansf90016a2015-01-19 14:26:37 +0000756 polarssl_printf( USAGE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000757
758 list = ssl_list_ciphersuites();
759 while( *list )
760 {
Rich Evansf90016a2015-01-19 14:26:37 +0000761 polarssl_printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200762 list++;
763 if( !*list )
764 break;
Rich Evansf90016a2015-01-19 14:26:37 +0000765 polarssl_printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000766 list++;
767 }
Rich Evansf90016a2015-01-19 14:26:37 +0000768 polarssl_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000769 goto exit;
770 }
771
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100772 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000773 opt.server_port = DFL_SERVER_PORT;
774 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100775 opt.nbio = DFL_NBIO;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000776 opt.ca_file = DFL_CA_FILE;
777 opt.ca_path = DFL_CA_PATH;
778 opt.crt_file = DFL_CRT_FILE;
779 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200780 opt.crt_file2 = DFL_CRT_FILE2;
781 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200782 opt.psk = DFL_PSK;
783 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200784 opt.psk_list = DFL_PSK_LIST;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000785 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200786 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000787 opt.renegotiation = DFL_RENEGOTIATION;
788 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100789 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200790 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100791 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200792 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000793 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200794 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100795 opt.arc4 = DFL_ARC4;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100796 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200797 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100798 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200799 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100800 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100801 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100802 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100803 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200804 opt.alpn_string = DFL_ALPN_STRING;
Hanno Becker6fd6d242017-05-25 17:51:31 +0100805 opt.curves = DFL_CURVES;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200806 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200807 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100808 opt.etm = DFL_ETM;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000809
810 for( i = 1; i < argc; i++ )
811 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000812 p = argv[i];
813 if( ( q = strchr( p, '=' ) ) == NULL )
814 goto usage;
815 *q++ = '\0';
816
817 if( strcmp( p, "server_port" ) == 0 )
818 {
819 opt.server_port = atoi( q );
820 if( opt.server_port < 1 || opt.server_port > 65535 )
821 goto usage;
822 }
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100823 else if( strcmp( p, "server_addr" ) == 0 )
824 opt.server_addr = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000825 else if( strcmp( p, "debug_level" ) == 0 )
826 {
827 opt.debug_level = atoi( q );
828 if( opt.debug_level < 0 || opt.debug_level > 65535 )
829 goto usage;
830 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100831 else if( strcmp( p, "nbio" ) == 0 )
832 {
833 opt.nbio = atoi( q );
834 if( opt.nbio < 0 || opt.nbio > 2 )
835 goto usage;
836 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000837 else if( strcmp( p, "ca_file" ) == 0 )
838 opt.ca_file = q;
839 else if( strcmp( p, "ca_path" ) == 0 )
840 opt.ca_path = q;
841 else if( strcmp( p, "crt_file" ) == 0 )
842 opt.crt_file = q;
843 else if( strcmp( p, "key_file" ) == 0 )
844 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200845 else if( strcmp( p, "crt_file2" ) == 0 )
846 opt.crt_file2 = q;
847 else if( strcmp( p, "key_file2" ) == 0 )
848 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200849 else if( strcmp( p, "dhm_file" ) == 0 )
850 opt.dhm_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200851 else if( strcmp( p, "psk" ) == 0 )
852 opt.psk = q;
853 else if( strcmp( p, "psk_identity" ) == 0 )
854 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200855 else if( strcmp( p, "psk_list" ) == 0 )
856 opt.psk_list = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000857 else if( strcmp( p, "force_ciphersuite" ) == 0 )
858 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000859 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
860
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200861 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000862 {
863 ret = 2;
864 goto usage;
865 }
866 opt.force_ciphersuite[1] = 0;
867 }
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200868 else if( strcmp( p, "version_suites" ) == 0 )
869 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000870 else if( strcmp( p, "renegotiation" ) == 0 )
871 {
872 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
873 SSL_RENEGOTIATION_DISABLED;
874 }
875 else if( strcmp( p, "allow_legacy" ) == 0 )
876 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100877 switch( atoi( q ) )
878 {
879 case -1: opt.allow_legacy = SSL_LEGACY_BREAK_HANDSHAKE; break;
880 case 0: opt.allow_legacy = SSL_LEGACY_NO_RENEGOTIATION; break;
881 case 1: opt.allow_legacy = SSL_LEGACY_ALLOW_RENEGOTIATION; break;
882 default: goto usage;
883 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000884 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100885 else if( strcmp( p, "renegotiate" ) == 0 )
886 {
887 opt.renegotiate = atoi( q );
888 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
889 goto usage;
890 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200891 else if( strcmp( p, "renego_delay" ) == 0 )
892 {
893 opt.renego_delay = atoi( q );
894 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100895 else if( strcmp( p, "renego_period" ) == 0 )
896 {
897 opt.renego_period = atoi( q );
898 if( opt.renego_period < 2 || opt.renego_period > 255 )
899 goto usage;
900 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200901 else if( strcmp( p, "exchanges" ) == 0 )
902 {
903 opt.exchanges = atoi( q );
904 if( opt.exchanges < 1 )
905 goto usage;
906 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000907 else if( strcmp( p, "min_version" ) == 0 )
908 {
909 if( strcmp( q, "ssl3" ) == 0 )
910 opt.min_version = SSL_MINOR_VERSION_0;
911 else if( strcmp( q, "tls1" ) == 0 )
912 opt.min_version = SSL_MINOR_VERSION_1;
913 else if( strcmp( q, "tls1_1" ) == 0 )
914 opt.min_version = SSL_MINOR_VERSION_2;
915 else if( strcmp( q, "tls1_2" ) == 0 )
916 opt.min_version = SSL_MINOR_VERSION_3;
917 else
918 goto usage;
919 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200920 else if( strcmp( p, "max_version" ) == 0 )
921 {
922 if( strcmp( q, "ssl3" ) == 0 )
923 opt.max_version = SSL_MINOR_VERSION_0;
924 else if( strcmp( q, "tls1" ) == 0 )
925 opt.max_version = SSL_MINOR_VERSION_1;
926 else if( strcmp( q, "tls1_1" ) == 0 )
927 opt.max_version = SSL_MINOR_VERSION_2;
928 else if( strcmp( q, "tls1_2" ) == 0 )
929 opt.max_version = SSL_MINOR_VERSION_3;
930 else
931 goto usage;
932 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100933 else if( strcmp( p, "arc4" ) == 0 )
934 {
935 switch( atoi( q ) )
936 {
937 case 0: opt.arc4 = SSL_ARC4_DISABLED; break;
938 case 1: opt.arc4 = SSL_ARC4_ENABLED; break;
939 default: goto usage;
940 }
941 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200942 else if( strcmp( p, "force_version" ) == 0 )
943 {
944 if( strcmp( q, "ssl3" ) == 0 )
945 {
946 opt.min_version = SSL_MINOR_VERSION_0;
947 opt.max_version = SSL_MINOR_VERSION_0;
948 }
949 else if( strcmp( q, "tls1" ) == 0 )
950 {
951 opt.min_version = SSL_MINOR_VERSION_1;
952 opt.max_version = SSL_MINOR_VERSION_1;
953 }
954 else if( strcmp( q, "tls1_1" ) == 0 )
955 {
956 opt.min_version = SSL_MINOR_VERSION_2;
957 opt.max_version = SSL_MINOR_VERSION_2;
958 }
959 else if( strcmp( q, "tls1_2" ) == 0 )
960 {
961 opt.min_version = SSL_MINOR_VERSION_3;
962 opt.max_version = SSL_MINOR_VERSION_3;
963 }
964 else
965 goto usage;
966 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100967 else if( strcmp( p, "auth_mode" ) == 0 )
968 {
969 if( strcmp( q, "none" ) == 0 )
970 opt.auth_mode = SSL_VERIFY_NONE;
971 else if( strcmp( q, "optional" ) == 0 )
972 opt.auth_mode = SSL_VERIFY_OPTIONAL;
973 else if( strcmp( q, "required" ) == 0 )
974 opt.auth_mode = SSL_VERIFY_REQUIRED;
975 else
976 goto usage;
977 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200978 else if( strcmp( p, "max_frag_len" ) == 0 )
979 {
980 if( strcmp( q, "512" ) == 0 )
981 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
982 else if( strcmp( q, "1024" ) == 0 )
983 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
984 else if( strcmp( q, "2048" ) == 0 )
985 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
986 else if( strcmp( q, "4096" ) == 0 )
987 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
988 else
989 goto usage;
990 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200991 else if( strcmp( p, "alpn" ) == 0 )
992 {
993 opt.alpn_string = q;
994 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100995 else if( strcmp( p, "trunc_hmac" ) == 0 )
996 {
997 switch( atoi( q ) )
998 {
999 case 0: opt.trunc_hmac = SSL_TRUNC_HMAC_DISABLED; break;
1000 case 1: opt.trunc_hmac = SSL_TRUNC_HMAC_ENABLED; break;
1001 default: goto usage;
1002 }
1003 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001004 else if( strcmp( p, "extended_ms" ) == 0 )
1005 {
1006 switch( atoi( q ) )
1007 {
1008 case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break;
1009 case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break;
1010 default: goto usage;
1011 }
1012 }
Hanno Becker6fd6d242017-05-25 17:51:31 +01001013 else if( strcmp( p, "curves" ) == 0 )
1014 opt.curves = q;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001015 else if( strcmp( p, "etm" ) == 0 )
1016 {
1017 switch( atoi( q ) )
1018 {
1019 case 0: opt.etm = SSL_ETM_DISABLED; break;
1020 case 1: opt.etm = SSL_ETM_ENABLED; break;
1021 default: goto usage;
1022 }
1023 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001024 else if( strcmp( p, "tickets" ) == 0 )
1025 {
1026 opt.tickets = atoi( q );
1027 if( opt.tickets < 0 || opt.tickets > 1 )
1028 goto usage;
1029 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001030 else if( strcmp( p, "ticket_timeout" ) == 0 )
1031 {
1032 opt.ticket_timeout = atoi( q );
1033 if( opt.ticket_timeout < 0 )
1034 goto usage;
1035 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001036 else if( strcmp( p, "cache_max" ) == 0 )
1037 {
1038 opt.cache_max = atoi( q );
1039 if( opt.cache_max < 0 )
1040 goto usage;
1041 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001042 else if( strcmp( p, "cache_timeout" ) == 0 )
1043 {
1044 opt.cache_timeout = atoi( q );
1045 if( opt.cache_timeout < 0 )
1046 goto usage;
1047 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001048 else if( strcmp( p, "sni" ) == 0 )
1049 {
1050 opt.sni = q;
1051 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001052 else
1053 goto usage;
1054 }
1055
Paul Bakkerc73079a2014-04-25 16:34:30 +02001056#if defined(POLARSSL_DEBUG_C)
1057 debug_set_threshold( opt.debug_level );
1058#endif
1059
Paul Bakkerc1516be2013-06-29 16:01:32 +02001060 if( opt.force_ciphersuite[0] > 0 )
1061 {
1062 const ssl_ciphersuite_t *ciphersuite_info;
1063 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1064
Paul Bakker5b55b792013-07-19 13:43:43 +02001065 if( opt.max_version != -1 &&
1066 ciphersuite_info->min_minor_ver > opt.max_version )
1067 {
Rich Evansf90016a2015-01-19 14:26:37 +00001068 polarssl_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakker5b55b792013-07-19 13:43:43 +02001069 ret = 2;
1070 goto usage;
1071 }
1072 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001073 ciphersuite_info->max_minor_ver < opt.min_version )
1074 {
Rich Evansf90016a2015-01-19 14:26:37 +00001075 polarssl_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +02001076 ret = 2;
1077 goto usage;
1078 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001079 if( opt.max_version > ciphersuite_info->max_minor_ver )
1080 opt.max_version = ciphersuite_info->max_minor_ver;
1081 if( opt.min_version < ciphersuite_info->min_minor_ver )
1082 opt.min_version = ciphersuite_info->min_minor_ver;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001083 }
1084
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001085 if( opt.version_suites != NULL )
1086 {
1087 const char *name[4] = { 0 };
1088
1089 /* Parse 4-element coma-separated list */
1090 for( i = 0, p = (char *) opt.version_suites;
1091 i < 4 && *p != '\0';
1092 i++ )
1093 {
1094 name[i] = p;
1095
1096 /* Terminate the current string and move on to next one */
1097 while( *p != ',' && *p != '\0' )
1098 p++;
1099 if( *p == ',' )
1100 *p++ = '\0';
1101 }
1102
1103 if( i != 4 )
1104 {
Rich Evansf90016a2015-01-19 14:26:37 +00001105 polarssl_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001106 ret = 1;
1107 goto exit;
1108 }
1109
1110 memset( version_suites, 0, sizeof( version_suites ) );
1111
1112 /* Get the suites identifiers from their name */
1113 for( i = 0; i < 4; i++ )
1114 {
1115 version_suites[i][0] = ssl_get_ciphersuite_id( name[i] );
1116
1117 if( version_suites[i][0] == 0 )
1118 {
Rich Evansf90016a2015-01-19 14:26:37 +00001119 polarssl_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001120 ret = 2;
1121 goto usage;
1122 }
1123 }
1124 }
1125
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001126#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001127 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001128 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001129 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001130 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001131 {
Rich Evansf90016a2015-01-19 14:26:37 +00001132 polarssl_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001133 goto exit;
1134 }
1135
1136 if( opt.psk_list != NULL )
1137 {
1138 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001139 {
Rich Evansf90016a2015-01-19 14:26:37 +00001140 polarssl_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001141 goto exit;
1142 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001143 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001144#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001145
Hanno Becker6fd6d242017-05-25 17:51:31 +01001146#if defined(POLARSSL_SSL_SET_CURVES)
1147 if( opt.curves != NULL )
1148 {
1149 p = (char *) opt.curves;
1150 i = 0;
1151
1152 if( strcmp( p, "none" ) == 0 )
1153 {
1154 curve_list[0] = POLARSSL_ECP_DP_NONE;
1155 }
1156 else if( strcmp( p, "default" ) != 0 )
1157 {
1158 /* Leave room for a final NULL in curve list */
Hanno Becker569a4f42017-06-09 16:26:04 +01001159 while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Becker6fd6d242017-05-25 17:51:31 +01001160 {
1161 q = p;
1162
1163 /* Terminate the current string */
1164 while( *p != ',' && *p != '\0' )
1165 p++;
1166 if( *p == ',' )
1167 *p++ = '\0';
1168
1169 if( ( curve_cur = ecp_curve_info_from_name( q ) ) != NULL )
1170 {
1171 curve_list[i++] = curve_cur->grp_id;
1172 }
1173 else
1174 {
1175 polarssl_printf( "unknown curve %s\n", q );
1176 polarssl_printf( "supported curves: " );
1177 for( curve_cur = ecp_curve_list();
1178 curve_cur->grp_id != POLARSSL_ECP_DP_NONE;
1179 curve_cur++ )
1180 {
1181 polarssl_printf( "%s ", curve_cur->name );
1182 }
1183 polarssl_printf( "\n" );
1184 goto exit;
1185 }
1186 }
1187
1188 polarssl_printf( "Number of curves: %d\n", i );
1189
Hanno Becker569a4f42017-06-09 16:26:04 +01001190 if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
Hanno Becker6fd6d242017-05-25 17:51:31 +01001191 {
Hanno Becker569a4f42017-06-09 16:26:04 +01001192 polarssl_printf( "curves list too long, maximum %d",
1193 CURVE_LIST_SIZE - 1 );
Hanno Becker6fd6d242017-05-25 17:51:31 +01001194 goto exit;
1195 }
1196
1197 curve_list[i] = POLARSSL_ECP_DP_NONE;
1198 }
1199 }
1200#endif /* POLARSSL_SSL_SET_CURVES */
1201
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001202#if defined(POLARSSL_SSL_ALPN)
1203 if( opt.alpn_string != NULL )
1204 {
1205 p = (char *) opt.alpn_string;
1206 i = 0;
1207
1208 /* Leave room for a final NULL in alpn_list */
Hanno Becker569a4f42017-06-09 16:26:04 +01001209 while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001210 {
1211 alpn_list[i++] = p;
1212
1213 /* Terminate the current string and move on to next one */
1214 while( *p != ',' && *p != '\0' )
1215 p++;
1216 if( *p == ',' )
1217 *p++ = '\0';
1218 }
1219 }
1220#endif /* POLARSSL_SSL_ALPN */
1221
Paul Bakkerfbb17802013-04-17 19:10:21 +02001222 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001223 * 0. Initialize the RNG and the session data
1224 */
Rich Evansf90016a2015-01-19 14:26:37 +00001225 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001226 fflush( stdout );
1227
1228 entropy_init( &entropy );
1229 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001230 (const unsigned char *) pers,
1231 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001232 {
Rich Evansf90016a2015-01-19 14:26:37 +00001233 polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001234 goto exit;
1235 }
1236
Rich Evansf90016a2015-01-19 14:26:37 +00001237 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001238
Paul Bakker36713e82013-09-17 13:25:29 +02001239#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001240 /*
1241 * 1.1. Load the trusted CA
1242 */
Rich Evansf90016a2015-01-19 14:26:37 +00001243 polarssl_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001244 fflush( stdout );
1245
1246#if defined(POLARSSL_FS_IO)
1247 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001248 if( strcmp( opt.ca_path, "none" ) == 0 )
1249 ret = 0;
1250 else
1251 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001252 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001253 if( strcmp( opt.ca_file, "none" ) == 0 )
1254 ret = 0;
1255 else
1256 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001257 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001258#endif
1259#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +02001260 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
1261 strlen( test_ca_list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001262#else
1263 {
1264 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +00001265 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001266 }
1267#endif
1268 if( ret < 0 )
1269 {
Rich Evansf90016a2015-01-19 14:26:37 +00001270 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001271 goto exit;
1272 }
1273
Rich Evansf90016a2015-01-19 14:26:37 +00001274 polarssl_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001275
1276 /*
1277 * 1.2. Load own certificate and private key
1278 */
Rich Evansf90016a2015-01-19 14:26:37 +00001279 polarssl_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001280 fflush( stdout );
1281
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001282#if defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001283 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001284 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001285 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001286 if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
1287 {
Rich Evansf90016a2015-01-19 14:26:37 +00001288 polarssl_printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001289 -ret );
1290 goto exit;
1291 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001292 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001293 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001294 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001295 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001296 if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
1297 {
Rich Evansf90016a2015-01-19 14:26:37 +00001298 polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001299 goto exit;
1300 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001301 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001302 if( key_cert_init == 1 )
1303 {
Rich Evansf90016a2015-01-19 14:26:37 +00001304 polarssl_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001305 goto exit;
1306 }
1307
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001308 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001309 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001310 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001311 if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
1312 {
Rich Evansf90016a2015-01-19 14:26:37 +00001313 polarssl_printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001314 -ret );
1315 goto exit;
1316 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001317 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001318 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001319 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001320 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001321 if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
1322 {
Rich Evansf90016a2015-01-19 14:26:37 +00001323 polarssl_printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001324 -ret );
1325 goto exit;
1326 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001327 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001328 if( key_cert_init2 == 1 )
1329 {
Rich Evansf90016a2015-01-19 14:26:37 +00001330 polarssl_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001331 goto exit;
1332 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001333#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001334 if( key_cert_init == 0 &&
1335 strcmp( opt.crt_file, "none" ) != 0 &&
1336 strcmp( opt.key_file, "none" ) != 0 &&
1337 key_cert_init2 == 0 &&
1338 strcmp( opt.crt_file2, "none" ) != 0 &&
1339 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001340 {
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001341#if !defined(POLARSSL_CERTS_C)
Rich Evansf90016a2015-01-19 14:26:37 +00001342 polarssl_printf( "Not certificated or key provided, and \n"
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001343 "POLARSSL_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001344 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001345#else
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001346#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001347 if( ( ret = x509_crt_parse( &srvcert,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001348 (const unsigned char *) test_srv_crt_rsa,
1349 strlen( test_srv_crt_rsa ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001350 {
Rich Evansf90016a2015-01-19 14:26:37 +00001351 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001352 goto exit;
1353 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001354 if( ( ret = pk_parse_key( &pkey,
1355 (const unsigned char *) test_srv_key_rsa,
1356 strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001357 {
Rich Evansf90016a2015-01-19 14:26:37 +00001358 polarssl_printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001359 goto exit;
1360 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001361 key_cert_init = 2;
1362#endif /* POLARSSL_RSA_C */
1363#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001364 if( ( ret = x509_crt_parse( &srvcert2,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001365 (const unsigned char *) test_srv_crt_ec,
1366 strlen( test_srv_crt_ec ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001367 {
Rich Evansf90016a2015-01-19 14:26:37 +00001368 polarssl_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001369 goto exit;
1370 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001371 if( ( ret = pk_parse_key( &pkey2,
1372 (const unsigned char *) test_srv_key_ec,
1373 strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001374 {
Rich Evansf90016a2015-01-19 14:26:37 +00001375 polarssl_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001376 goto exit;
1377 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001378 key_cert_init2 = 2;
1379#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001380#endif /* POLARSSL_CERTS_C */
1381 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001382
Rich Evansf90016a2015-01-19 14:26:37 +00001383 polarssl_printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001384#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001385
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001386#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1387 if( opt.dhm_file != NULL )
1388 {
Rich Evansf90016a2015-01-19 14:26:37 +00001389 polarssl_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001390 fflush( stdout );
1391
1392 if( ( ret = dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
1393 {
Rich Evansf90016a2015-01-19 14:26:37 +00001394 polarssl_printf( " failed\n ! dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001395 -ret );
1396 goto exit;
1397 }
1398
Rich Evansf90016a2015-01-19 14:26:37 +00001399 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001400 }
1401#endif
1402
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001403#if defined(POLARSSL_SNI)
1404 if( opt.sni != NULL )
1405 {
Rich Evansf90016a2015-01-19 14:26:37 +00001406 polarssl_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001407 fflush( stdout );
1408
1409 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
1410 {
Rich Evansf90016a2015-01-19 14:26:37 +00001411 polarssl_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001412 goto exit;
1413 }
1414
Rich Evansf90016a2015-01-19 14:26:37 +00001415 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001416 }
1417#endif /* POLARSSL_SNI */
1418
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001419 /*
1420 * 2. Setup the listening TCP socket
1421 */
Rich Evansf90016a2015-01-19 14:26:37 +00001422 polarssl_printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001423 fflush( stdout );
1424
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001425 if( ( ret = net_bind( &listen_fd, opt.server_addr,
1426 opt.server_port ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001427 {
Rich Evansf90016a2015-01-19 14:26:37 +00001428 polarssl_printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001429 goto exit;
1430 }
1431
Rich Evansf90016a2015-01-19 14:26:37 +00001432 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001433
1434 /*
1435 * 3. Setup stuff
1436 */
Rich Evansf90016a2015-01-19 14:26:37 +00001437 polarssl_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001438 fflush( stdout );
1439
1440 if( ( ret = ssl_init( &ssl ) ) != 0 )
1441 {
Rich Evansf90016a2015-01-19 14:26:37 +00001442 polarssl_printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001443 goto exit;
1444 }
1445
1446 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001447 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001448
Paul Bakker05decb22013-08-15 13:33:48 +02001449#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001450 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1451 {
Rich Evansf90016a2015-01-19 14:26:37 +00001452 polarssl_printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001453 goto exit;
1454 };
Paul Bakker05decb22013-08-15 13:33:48 +02001455#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001456
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001457#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1458 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
1459 ssl_set_truncated_hmac( &ssl, opt.trunc_hmac );
1460#endif
1461
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001462#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1463 if( opt.extended_ms != DFL_EXTENDED_MS )
1464 ssl_set_extended_master_secret( &ssl, opt.extended_ms );
1465#endif
1466
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001467#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1468 if( opt.etm != DFL_ETM )
1469 ssl_set_encrypt_then_mac( &ssl, opt.etm );
1470#endif
1471
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001472#if defined(POLARSSL_SSL_ALPN)
1473 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001474 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1475 {
Rich Evansf90016a2015-01-19 14:26:37 +00001476 polarssl_printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001477 goto exit;
1478 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001479#endif
1480
Hanno Becker6fd6d242017-05-25 17:51:31 +01001481#if defined(POLARSSL_SSL_SET_CURVES)
1482 if( opt.curves != NULL &&
1483 strcmp( opt.curves, "default" ) != 0 )
1484 {
1485 ssl_set_curves( &ssl, curve_list );
1486 }
1487#endif
1488
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001489 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
1490 ssl_set_dbg( &ssl, my_debug, stdout );
1491
Paul Bakker0a597072012-09-25 21:55:46 +00001492#if defined(POLARSSL_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001493 if( opt.cache_max != -1 )
1494 ssl_cache_set_max_entries( &cache, opt.cache_max );
1495
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001496 if( opt.cache_timeout != -1 )
1497 ssl_cache_set_timeout( &cache, opt.cache_timeout );
1498
Paul Bakker0a597072012-09-25 21:55:46 +00001499 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
1500 ssl_cache_set, &cache );
1501#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001502
Paul Bakkera503a632013-08-14 13:48:06 +02001503#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001504 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1505 {
Rich Evansf90016a2015-01-19 14:26:37 +00001506 polarssl_printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001507 goto exit;
1508 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001509
1510 if( opt.ticket_timeout != -1 )
1511 ssl_set_session_ticket_lifetime( &ssl, opt.ticket_timeout );
Paul Bakkera503a632013-08-14 13:48:06 +02001512#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001513
Paul Bakker41c83d32013-03-20 14:39:14 +01001514 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001515 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001516 else
1517 ssl_set_arc4_support( &ssl, opt.arc4 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001518
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001519 if( opt.version_suites != NULL )
1520 {
1521 ssl_set_ciphersuites_for_version( &ssl, version_suites[0],
1522 SSL_MAJOR_VERSION_3,
1523 SSL_MINOR_VERSION_0 );
1524 ssl_set_ciphersuites_for_version( &ssl, version_suites[1],
1525 SSL_MAJOR_VERSION_3,
1526 SSL_MINOR_VERSION_1 );
1527 ssl_set_ciphersuites_for_version( &ssl, version_suites[2],
1528 SSL_MAJOR_VERSION_3,
1529 SSL_MINOR_VERSION_2 );
1530 ssl_set_ciphersuites_for_version( &ssl, version_suites[3],
1531 SSL_MAJOR_VERSION_3,
1532 SSL_MINOR_VERSION_3 );
1533 }
1534
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001535 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
1536 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001537#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001538 ssl_set_renegotiation( &ssl, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001539
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001540 if( opt.renego_delay != DFL_RENEGO_DELAY )
1541 ssl_set_renegotiation_enforced( &ssl, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001542
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001543 if( opt.renego_period != DFL_RENEGO_PERIOD )
1544 {
1545 renego_period[7] = opt.renego_period;
1546 ssl_set_renegotiation_period( &ssl, renego_period );
1547 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001548#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001549
Paul Bakker36713e82013-09-17 13:25:29 +02001550#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001551 if( strcmp( opt.ca_path, "none" ) != 0 &&
1552 strcmp( opt.ca_file, "none" ) != 0 )
1553 {
1554 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
1555 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001556 if( key_cert_init )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001557 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
1558 {
Rich Evansf90016a2015-01-19 14:26:37 +00001559 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001560 goto exit;
1561 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001562 if( key_cert_init2 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001563 if( ( ret = ssl_set_own_cert( &ssl, &srvcert2, &pkey2 ) ) != 0 )
1564 {
Rich Evansf90016a2015-01-19 14:26:37 +00001565 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001566 goto exit;
1567 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001568#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001569
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001570#if defined(POLARSSL_SNI)
1571 if( opt.sni != NULL )
1572 ssl_set_sni( &ssl, sni_callback, sni_info );
1573#endif
1574
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001575#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001576 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
1577 {
1578 ret = ssl_set_psk( &ssl, psk, psk_len,
1579 (const unsigned char *) opt.psk_identity,
1580 strlen( opt.psk_identity ) );
1581 if( ret != 0 )
1582 {
Rich Evansf90016a2015-01-19 14:26:37 +00001583 polarssl_printf( " failed\n ssl_set_psk returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001584 goto exit;
1585 }
1586 }
1587
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001588 if( opt.psk_list != NULL )
1589 ssl_set_psk_cb( &ssl, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02001590#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001591
1592#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00001593 /*
1594 * Use different group than default DHM group
1595 */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001596#if defined(POLARSSL_FS_IO)
1597 if( opt.dhm_file != NULL )
1598 ret = ssl_set_dh_param_ctx( &ssl, &dhm );
1599 else
1600#endif
Hanno Beckerfffe3bd2017-10-13 17:00:12 +01001601 ret = ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC3526_MODP_2048_P,
1602 POLARSSL_DHM_RFC3526_MODP_2048_G );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001603
1604 if( ret != 0 )
1605 {
Rich Evansf90016a2015-01-19 14:26:37 +00001606 polarssl_printf( " failed\n ssl_set_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001607 goto exit;
1608 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001609#endif
1610
Paul Bakker1d29fb52012-09-28 13:28:45 +00001611 if( opt.min_version != -1 )
1612 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1613
Paul Bakkerc1516be2013-06-29 16:01:32 +02001614 if( opt.max_version != -1 )
1615 ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1616
Rich Evansf90016a2015-01-19 14:26:37 +00001617 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001618
1619reset:
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +02001620#if !defined(_WIN32)
1621 if( received_sigterm )
1622 {
Manuel Pégourié-Gonnarde10d6342018-01-22 10:55:10 +01001623 polarssl_printf( " interrupted by SIGTERM (not in net_accept())\n" );
1624 if( ret == POLARSSL_ERR_NET_RECV_FAILED ||
1625 ret == POLARSSL_ERR_NET_SEND_FAILED )
1626 {
1627 ret = 0;
1628 }
1629
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +02001630 goto exit;
1631 }
1632#endif
1633
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001634#ifdef POLARSSL_ERROR_C
1635 if( ret != 0 )
1636 {
1637 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001638 polarssl_strerror( ret, error_buf, 100 );
Rich Evansf90016a2015-01-19 14:26:37 +00001639 polarssl_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001640 }
1641#endif
1642
1643 if( client_fd != -1 )
1644 net_close( client_fd );
1645
1646 ssl_session_reset( &ssl );
1647
1648 /*
1649 * 3. Wait until a client connects
1650 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001651 client_fd = -1;
1652
Rich Evansf90016a2015-01-19 14:26:37 +00001653 polarssl_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001654 fflush( stdout );
1655
1656 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
1657 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02001658#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001659 if( received_sigterm )
1660 {
Manuel Pégourié-Gonnarde10d6342018-01-22 10:55:10 +01001661 polarssl_printf( " interrupted by SIGTERM (in net_accept())\n" );
1662 if( ret == POLARSSL_ERR_NET_ACCEPT_FAILED )
1663 ret = 0;
1664
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001665 goto exit;
1666 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02001667#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001668
Rich Evansf90016a2015-01-19 14:26:37 +00001669 polarssl_printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001670 goto exit;
1671 }
1672
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001673 if( opt.nbio > 0 )
1674 ret = net_set_nonblock( client_fd );
1675 else
1676 ret = net_set_block( client_fd );
1677 if( ret != 0 )
1678 {
Rich Evansf90016a2015-01-19 14:26:37 +00001679 polarssl_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001680 goto exit;
1681 }
1682
1683 if( opt.nbio == 2 )
1684 ssl_set_bio( &ssl, my_recv, &client_fd, my_send, &client_fd );
1685 else
1686 ssl_set_bio( &ssl, net_recv, &client_fd, net_send, &client_fd );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001687
Rich Evansf90016a2015-01-19 14:26:37 +00001688 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001689
1690 /*
1691 * 4. Handshake
1692 */
Rich Evansf90016a2015-01-19 14:26:37 +00001693 polarssl_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001694 fflush( stdout );
1695
1696 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1697 {
1698 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1699 {
Rich Evansf90016a2015-01-19 14:26:37 +00001700 polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001701 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001702 }
1703 }
1704
Rich Evansf90016a2015-01-19 14:26:37 +00001705 polarssl_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Manuel Pégourié-Gonnardc580a002014-02-12 10:15:30 +01001706 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001707
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001708#if defined(POLARSSL_SSL_ALPN)
1709 if( opt.alpn_string != NULL )
1710 {
1711 const char *alp = ssl_get_alpn_protocol( &ssl );
Rich Evansf90016a2015-01-19 14:26:37 +00001712 polarssl_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001713 alp ? alp : "(none)" );
1714 }
1715#endif
1716
Paul Bakker36713e82013-09-17 13:25:29 +02001717#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001718 /*
Ron Eldor1d260542017-06-20 15:23:23 +03001719 * 5. Verify the client certificate
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001720 */
Rich Evansf90016a2015-01-19 14:26:37 +00001721 polarssl_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001722
1723 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1724 {
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +02001725 char vrfy_buf[512];
1726
Rich Evansf90016a2015-01-19 14:26:37 +00001727 polarssl_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001728
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +02001729 x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001730
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +02001731 polarssl_printf( "%s\n", vrfy_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001732 }
1733 else
Rich Evansf90016a2015-01-19 14:26:37 +00001734 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001735
Paul Bakkerb0550d92012-10-30 07:51:03 +00001736 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001737 {
Rich Evansf90016a2015-01-19 14:26:37 +00001738 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001739 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1740 ssl_get_peer_cert( &ssl ) );
Rich Evansf90016a2015-01-19 14:26:37 +00001741 polarssl_printf( "%s\n", buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001742 }
Paul Bakker36713e82013-09-17 13:25:29 +02001743#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001744
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001745 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001746data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001747 /*
1748 * 6. Read the HTTP Request
1749 */
Rich Evansf90016a2015-01-19 14:26:37 +00001750 polarssl_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001751 fflush( stdout );
1752
1753 do
1754 {
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001755 int terminated = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001756 len = sizeof( buf ) - 1;
1757 memset( buf, 0, sizeof( buf ) );
1758 ret = ssl_read( &ssl, buf, len );
1759
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001760 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1761 ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001762 continue;
1763
1764 if( ret <= 0 )
1765 {
1766 switch( ret )
1767 {
1768 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Rich Evansf90016a2015-01-19 14:26:37 +00001769 polarssl_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001770 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001771
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001772 case 0:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001773 case POLARSSL_ERR_NET_CONN_RESET:
Rich Evansf90016a2015-01-19 14:26:37 +00001774 polarssl_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001775 ret = POLARSSL_ERR_NET_CONN_RESET;
1776 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001777
1778 default:
Rich Evansf90016a2015-01-19 14:26:37 +00001779 polarssl_printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001780 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001781 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001782 }
1783
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001784 if( ssl_get_bytes_avail( &ssl ) == 0 )
1785 {
1786 len = ret;
1787 buf[len] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +00001788 polarssl_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001789
1790 /* End of message should be detected according to the syntax of the
1791 * application protocol (eg HTTP), just use a dummy test here. */
1792 if( buf[len - 1] == '\n' )
1793 terminated = 1;
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001794 }
1795 else
1796 {
1797 int extra_len, ori_len;
1798 unsigned char *larger_buf;
1799
1800 ori_len = ret;
Andres Amaya Garcia2d0a5842017-07-06 14:07:53 +01001801 extra_len = (int) ssl_get_bytes_avail( &ssl );
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001802
1803 larger_buf = polarssl_malloc( ori_len + extra_len + 1 );
1804 if( larger_buf == NULL )
1805 {
Rich Evansf90016a2015-01-19 14:26:37 +00001806 polarssl_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001807 ret = 1;
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001808 goto reset;
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001809 }
1810
1811 memset( larger_buf, 0, ori_len + extra_len );
1812 memcpy( larger_buf, buf, ori_len );
1813
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +02001814 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001815 ret = ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +02001816 if( ret != extra_len ||
1817 ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001818 {
Rich Evansf90016a2015-01-19 14:26:37 +00001819 polarssl_printf( " ! ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001820 ret = 1;
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001821 goto reset;
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001822 }
1823
1824 larger_buf[ori_len + extra_len] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +00001825 polarssl_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnard0669f272014-06-18 13:07:20 +02001826 ori_len + extra_len, ori_len, extra_len,
1827 (char *) larger_buf );
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001828
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001829 /* End of message should be detected according to the syntax of the
1830 * application protocol (eg HTTP), just use a dummy test here. */
1831 if( larger_buf[ori_len + extra_len - 1] == '\n' )
1832 terminated = 1;
1833
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001834 polarssl_free( larger_buf );
1835 }
1836
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001837 if( terminated )
1838 {
1839 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001840 break;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001841 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001842 }
1843 while( 1 );
1844
1845 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001846 * 7a. Request renegotiation while client is waiting for input from us.
1847 * (only if we're going to exhange more data afterwards)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001848 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001849#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001850 if( opt.renegotiate && exchanges_left > 1 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001851 {
Rich Evansf90016a2015-01-19 14:26:37 +00001852 polarssl_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001853 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001854
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001855 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
1856 {
1857 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1858 ret != POLARSSL_ERR_NET_WANT_WRITE )
1859 {
Rich Evansf90016a2015-01-19 14:26:37 +00001860 polarssl_printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001861 goto reset;
1862 }
1863 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001864
Rich Evansf90016a2015-01-19 14:26:37 +00001865 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001866 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001867#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001868
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001869 /*
1870 * 7. Write the 200 Response
1871 */
Rich Evansf90016a2015-01-19 14:26:37 +00001872 polarssl_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001873 fflush( stdout );
1874
1875 len = sprintf( (char *) buf, HTTP_RESPONSE,
1876 ssl_get_ciphersuite( &ssl ) );
1877
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001878 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001879 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001880 while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001881 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001882 if( ret == POLARSSL_ERR_NET_CONN_RESET )
1883 {
Rich Evansf90016a2015-01-19 14:26:37 +00001884 polarssl_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001885 goto reset;
1886 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001887
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001888 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1889 {
Rich Evansf90016a2015-01-19 14:26:37 +00001890 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001891 goto reset;
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001892 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001893 }
1894 }
1895
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001896 buf[written] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +00001897 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 +01001898 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001899
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001900 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001901 * 7b. Continue doing data exchanges?
1902 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001903 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001904 goto data_exchange;
1905
1906 /*
1907 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001908 */
1909close_notify:
Rich Evansf90016a2015-01-19 14:26:37 +00001910 polarssl_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001911
Manuel Pégourié-Gonnard34377b12015-01-22 10:46:46 +00001912 /* No error checking, the connection might be closed already */
1913 do ret = ssl_close_notify( &ssl );
1914 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
1915 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001916
Rich Evansf90016a2015-01-19 14:26:37 +00001917 polarssl_printf( " done\n" );
1918
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001919 goto reset;
1920
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001921 /*
1922 * Cleanup and exit
1923 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001924exit:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001925#ifdef POLARSSL_ERROR_C
1926 if( ret != 0 )
1927 {
1928 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001929 polarssl_strerror( ret, error_buf, 100 );
Rich Evansf90016a2015-01-19 14:26:37 +00001930 polarssl_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001931 }
1932#endif
1933
Manuel Pégourié-Gonnard7e81e702015-01-29 11:47:41 +00001934 polarssl_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01001935 fflush( stdout );
1936
Paul Bakker0c226102014-04-17 16:02:36 +02001937 if( client_fd != -1 )
1938 net_close( client_fd );
1939
Paul Bakkera317a982014-06-18 16:44:11 +02001940#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1941 dhm_free( &dhm );
1942#endif
Paul Bakker36713e82013-09-17 13:25:29 +02001943#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker36713e82013-09-17 13:25:29 +02001944 x509_crt_free( &cacert );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001945 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02001946 pk_free( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001947 x509_crt_free( &srvcert2 );
1948 pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02001949#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001950#if defined(POLARSSL_SNI)
1951 sni_free( sni_info );
1952#endif
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02001953#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1954 psk_free( psk_info );
1955#endif
1956#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1957 dhm_free( &dhm );
1958#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001959
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001960 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02001961 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02001962 entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001963
Paul Bakker0a597072012-09-25 21:55:46 +00001964#if defined(POLARSSL_SSL_CACHE_C)
1965 ssl_cache_free( &cache );
1966#endif
1967
Paul Bakker1337aff2013-09-29 14:45:34 +02001968#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1969#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker82024bf2013-07-04 11:52:32 +02001970 memory_buffer_alloc_status();
1971#endif
Paul Bakker1337aff2013-09-29 14:45:34 +02001972 memory_buffer_alloc_free();
1973#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02001974
Manuel Pégourié-Gonnard7e81e702015-01-29 11:47:41 +00001975 polarssl_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01001976
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001977#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +00001978 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001979 fflush( stdout ); getchar();
1980#endif
1981
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001982 // Shell can not handle large exit numbers -> 1 for errors
1983 if( ret < 0 )
1984 ret = 1;
1985
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001986 return( ret );
1987}
1988#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
1989 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
1990 POLARSSL_CTR_DRBG_C */