blob: ad0631b56e5734c3b44cab816cb536aaf7706c4b [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 *
8 * 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
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +020072#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020073#include "polarssl/ssl_cookie.h"
74#endif
75
Paul Bakker82024bf2013-07-04 11:52:32 +020076#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardd43ccb62015-01-23 17:38:09 +000077#include "polarssl/memory_buffer_alloc.h"
Paul Bakker82024bf2013-07-04 11:52:32 +020078#endif
79
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +010080#define DFL_SERVER_ADDR NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000081#define DFL_SERVER_PORT 4433
Paul Bakkerb60b95f2012-09-25 09:05:17 +000082#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010083#define DFL_NBIO 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020084#define DFL_READ_TIMEOUT 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +000085#define DFL_CA_FILE ""
86#define DFL_CA_PATH ""
87#define DFL_CRT_FILE ""
88#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +020089#define DFL_CRT_FILE2 ""
90#define DFL_KEY_FILE2 ""
Paul Bakkerfbb17802013-04-17 19:10:21 +020091#define DFL_PSK ""
92#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +020093#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000094#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +020095#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +020096#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010097#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010098#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +020099#define DFL_RENEGO_DELAY -2
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100100#define DFL_RENEGO_PERIOD -1
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200101#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100102#define DFL_MIN_VERSION SSL_MINOR_VERSION_1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200103#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100104#define DFL_ARC4 SSL_ARC4_DISABLED
Paul Bakker91ebfb52012-11-23 14:04:08 +0100105#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200106#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100107#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200108#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100109#define DFL_TICKET_TIMEOUT -1
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100110#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100111#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100112#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200113#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200114#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100115#define DFL_TRANSPORT SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200116#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200117#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200118#define DFL_HS_TO_MIN 0
119#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200120#define DFL_BADMAC_LIMIT -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200121#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100122#define DFL_ETM -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000123
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200124#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
125 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
126 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
127 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
128 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
129 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
130 "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 +0200131
Paul Bakker8e714d72013-07-18 11:05:13 +0200132/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
133 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000134#define HTTP_RESPONSE \
135 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000136 "<h2>mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200137 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000138
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200139/*
140 * Size of the basic I/O buffer. Able to hold our default response.
141 *
142 * You will need to adapt the ssl_get_bytes_avail() test in ssl-opt.sh
143 * if you change this value to something outside the range <= 100 or > 500
144 */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200145#define IO_BUF_LEN 200
146
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200147#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000148#if defined(POLARSSL_FS_IO)
149#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100150 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
151 " default: \"\" (pre-loaded)\n" \
152 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
153 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
154 " 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 +0200155 " default: see note after key_file2\n" \
156 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200157 " 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 +0200158 " default: see note after key_file2\n" \
159 " key_file2=%%s default: see note below\n" \
160 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200161 " preloaded certificate(s) and key(s) are used if available\n" \
162 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
163 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000164#else
165#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200166 "\n" \
167 " No file operations available (POLARSSL_FS_IO not defined)\n" \
168 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000169#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200170#else
171#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200172#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200173
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200174#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200175#define USAGE_PSK \
176 " psk=%%s default: \"\" (in hex, without 0x)\n" \
177 " psk_identity=%%s default: \"Client_identity\"\n"
178#else
179#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200180#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000181
Paul Bakkera503a632013-08-14 13:48:06 +0200182#if defined(POLARSSL_SSL_SESSION_TICKETS)
183#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100184 " tickets=%%d default: 1 (enabled)\n" \
185 " ticket_timeout=%%d default: ticket default (1d)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200186#else
187#define USAGE_TICKETS ""
188#endif /* POLARSSL_SSL_SESSION_TICKETS */
189
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100190#if defined(POLARSSL_SSL_CACHE_C)
191#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100192 " cache_max=%%d default: cache default (50)\n" \
193 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100194#else
195#define USAGE_CACHE ""
196#endif /* POLARSSL_SSL_CACHE_C */
197
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100198#if defined(POLARSSL_SNI)
199#define USAGE_SNI \
200 " sni=%%s name1,cert1,key1[,name2,cert2,key2[,...]]\n" \
201 " default: disabled\n"
202#else
203#define USAGE_SNI ""
204#endif /* POLARSSL_SNI */
205
Paul Bakker05decb22013-08-15 13:33:48 +0200206#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
207#define USAGE_MAX_FRAG_LEN \
208 " max_frag_len=%%d default: 16384 (tls default)\n" \
209 " options: 512, 1024, 2048, 4096\n"
210#else
211#define USAGE_MAX_FRAG_LEN ""
212#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
213
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100214#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
215#define USAGE_TRUNC_HMAC \
216 " trunc_hmac=%%d default: library default\n"
217#else
218#define USAGE_TRUNC_HMAC ""
219#endif
220
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200221#if defined(POLARSSL_SSL_ALPN)
222#define USAGE_ALPN \
223 " alpn=%%s default: \"\" (disabled)\n" \
224 " example: spdy/1,http/1.1\n"
225#else
226#define USAGE_ALPN ""
227#endif /* POLARSSL_SSL_ALPN */
228
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200229#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
230#define USAGE_COOKIES \
231 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200232 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200233#else
234#define USAGE_COOKIES ""
235#endif
236
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200237#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
238#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200239 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200240#else
241#define USAGE_ANTI_REPLAY ""
242#endif
243
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200244#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
245#define USAGE_BADMAC_LIMIT \
246 " badmac_limit=%%d default: (library default: disabled)\n"
247#else
248#define USAGE_BADMAC_LIMIT ""
249#endif
250
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200251#if defined(POLARSSL_SSL_PROTO_DTLS)
252#define USAGE_DTLS \
253 " dtls=%%d default: 0 (TLS)\n" \
254 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
255 " range of DTLS handshake timeouts in millisecs\n"
256#else
257#define USAGE_DTLS ""
258#endif
259
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200260#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
261#define USAGE_EMS \
262 " extended_ms=0/1 default: (library default: on)\n"
263#else
264#define USAGE_EMS ""
265#endif
266
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100267#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
268#define USAGE_ETM \
269 " etm=0/1 default: (library default: on)\n"
270#else
271#define USAGE_ETM ""
272#endif
273
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100274#if defined(POLARSSL_SSL_RENEGOTIATION)
275#define USAGE_RENEGO \
276 " renegotiation=%%d default: 0 (disabled)\n" \
277 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100278 " renego_delay=%%d default: -2 (library default)\n" \
279 " renego_period=%%d default: (library default)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100280#else
281#define USAGE_RENEGO ""
282#endif
283
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000284#define USAGE \
285 "\n usage: ssl_server2 param=<>...\n" \
286 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100287 " server_addr=%%d default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000288 " server_port=%%d default: 4433\n" \
289 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100290 " nbio=%%d default: 0 (blocking I/O)\n" \
291 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200292 " read_timeout=%%d default: 0 (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100293 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200294 USAGE_DTLS \
295 USAGE_COOKIES \
296 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200297 USAGE_BADMAC_LIMIT \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200298 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100299 " auth_mode=%%s default: \"optional\"\n" \
300 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000301 USAGE_IO \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100302 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100303 "\n" \
304 USAGE_PSK \
305 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100306 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100307 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200308 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200309 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100310 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100311 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100312 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100313 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200314 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200315 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100316 USAGE_ETM \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100317 "\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000318 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200319 " max_version=%%s default: \"tls1_2\"\n" \
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100320 " arc4=%%d default: 0 (disabled)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200321 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100322 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200323 "\n" \
324 " version_suites=a,b,c,d per-version ciphersuites\n" \
325 " in order from ssl3 to tls1_2\n" \
326 " default: all enabled\n" \
327 " force_ciphersuite=<name> default: all enabled\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000328 " acceptable ciphersuite names:\n"
329
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +0000330#if !defined(POLARSSL_ENTROPY_C) || \
Rich Evans85b05ec2015-02-12 11:37:29 +0000331 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
332 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
333#include <stdio.h>
334int main( void )
335{
336 polarssl_printf("POLARSSL_ENTROPY_C and/or "
337 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
338 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
339 return( 0 );
340}
341#else
342/*
343 * global options
344 */
345struct options
346{
347 const char *server_addr; /* address on which the ssl service runs */
348 int server_port; /* port on which the ssl service runs */
349 int debug_level; /* level of debugging */
350 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000351 uint32_t read_timeout; /* timeout on ssl_read() in milliseconds */
Rich Evans85b05ec2015-02-12 11:37:29 +0000352 const char *ca_file; /* the file with the CA certificate(s) */
353 const char *ca_path; /* the path with the CA certificate(s) reside */
354 const char *crt_file; /* the file with the server certificate */
355 const char *key_file; /* the file with the server key */
356 const char *crt_file2; /* the file with the 2nd server certificate */
357 const char *key_file2; /* the file with the 2nd server key */
358 const char *psk; /* the pre-shared key */
359 const char *psk_identity; /* the pre-shared key identity */
360 char *psk_list; /* list of PSK id/key pairs for callback */
361 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
362 const char *version_suites; /* per-version ciphersuites */
363 int renegotiation; /* enable / disable renegotiation */
364 int allow_legacy; /* allow legacy renegotiation */
365 int renegotiate; /* attempt renegotiation? */
366 int renego_delay; /* delay before enforcing renegotiation */
367 int renego_period; /* period for automatic renegotiation */
368 int exchanges; /* number of data exchanges */
369 int min_version; /* minimum protocol version accepted */
370 int max_version; /* maximum protocol version accepted */
371 int arc4; /* flag for arc4 suites support */
372 int auth_mode; /* verify mode for connection */
373 unsigned char mfl_code; /* code for maximum fragment length */
374 int trunc_hmac; /* accept truncated hmac? */
375 int tickets; /* enable / disable session tickets */
376 int ticket_timeout; /* session ticket lifetime */
377 int cache_max; /* max number of session cache entries */
378 int cache_timeout; /* expiration delay of session cache entries */
379 char *sni; /* string describing sni information */
380 const char *alpn_string; /* ALPN supported protocols */
381 const char *dhm_file; /* the file with the DH parameters */
382 int extended_ms; /* allow negotiation of extended MS? */
383 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +0000384 int transport; /* TLS or DTLS? */
385 int cookies; /* Use cookies for DTLS? -1 to break them */
386 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
387 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
388 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
389 int badmac_limit; /* Limit of records with bad MAC */
Rich Evans85b05ec2015-02-12 11:37:29 +0000390} opt;
391
392static void my_debug( void *ctx, int level, const char *str )
393{
394 ((void) level);
395
396 polarssl_fprintf( (FILE *) ctx, "%s", str );
397 fflush( (FILE *) ctx );
398}
399
400/*
401 * Test recv/send functions that make sure each try returns
402 * WANT_READ/WANT_WRITE at least once before sucesseding
403 */
404static int my_recv( void *ctx, unsigned char *buf, size_t len )
405{
406 static int first_try = 1;
407 int ret;
408
409 if( first_try )
410 {
411 first_try = 0;
412 return( POLARSSL_ERR_NET_WANT_READ );
413 }
414
415 ret = net_recv( ctx, buf, len );
416 if( ret != POLARSSL_ERR_NET_WANT_READ )
417 first_try = 1; /* Next call will be a new operation */
418 return( ret );
419}
420
421static int my_send( void *ctx, const unsigned char *buf, size_t len )
422{
423 static int first_try = 1;
424 int ret;
425
426 if( first_try )
427 {
428 first_try = 0;
429 return( POLARSSL_ERR_NET_WANT_WRITE );
430 }
431
432 ret = net_send( ctx, buf, len );
433 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
434 first_try = 1; /* Next call will be a new operation */
435 return( ret );
436}
437
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200438/*
439 * Used by sni_parse and psk_parse to handle coma-separated lists
440 */
441#define GET_ITEM( dst ) \
442 dst = p; \
443 while( *p != ',' ) \
444 if( ++p > end ) \
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000445 goto error; \
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200446 *p++ = '\0';
447
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100448#if defined(POLARSSL_SNI)
449typedef struct _sni_entry sni_entry;
450
451struct _sni_entry {
452 const char *name;
453 x509_crt *cert;
454 pk_context *key;
455 sni_entry *next;
456};
457
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100458void sni_free( sni_entry *head )
459{
460 sni_entry *cur = head, *next;
461
462 while( cur != NULL )
463 {
464 x509_crt_free( cur->cert );
465 polarssl_free( cur->cert );
466
467 pk_free( cur->key );
468 polarssl_free( cur->key );
469
470 next = cur->next;
471 polarssl_free( cur );
472 cur = next;
473 }
474}
475
476/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000477 * Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]]
478 * into a usable sni_entry list.
479 *
480 * Modifies the input string! This is not production quality!
481 */
482sni_entry *sni_parse( char *sni_string )
483{
484 sni_entry *cur = NULL, *new = NULL;
485 char *p = sni_string;
486 char *end = p;
487 char *crt_file, *key_file;
488
489 while( *end != '\0' )
490 ++end;
491 *end = ',';
492
493 while( p <= end )
494 {
495 if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL )
496 {
497 sni_free( cur );
498 return( NULL );
499 }
500
501 memset( new, 0, sizeof( sni_entry ) );
502
503 if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ||
504 ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
505 {
506 polarssl_free( new->cert );
507 polarssl_free( new );
508 sni_free( cur );
509 return( NULL );
510 }
511
512 x509_crt_init( new->cert );
513 pk_init( new->key );
514
515 GET_ITEM( new->name );
516 GET_ITEM( crt_file );
517 GET_ITEM( key_file );
518
519 if( x509_crt_parse_file( new->cert, crt_file ) != 0 ||
520 pk_parse_keyfile( new->key, key_file, "" ) != 0 )
521 {
522 goto error;
523 }
524
525 new->next = cur;
526 cur = new;
527 }
528
529 return( cur );
530
531error:
532 sni_free( new );
533 sni_free( cur );
534 return( NULL );
535}
536
537/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100538 * SNI callback.
539 */
540int sni_callback( void *p_info, ssl_context *ssl,
541 const unsigned char *name, size_t name_len )
542{
543 sni_entry *cur = (sni_entry *) p_info;
544
545 while( cur != NULL )
546 {
547 if( name_len == strlen( cur->name ) &&
548 memcmp( name, cur->name, name_len ) == 0 )
549 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200550 return( ssl_set_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100551 }
552
553 cur = cur->next;
554 }
555
556 return( -1 );
557}
558
559#endif /* POLARSSL_SNI */
560
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200561#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
562
563#define HEX2NUM( c ) \
564 if( c >= '0' && c <= '9' ) \
565 c -= '0'; \
566 else if( c >= 'a' && c <= 'f' ) \
567 c -= 'a' - 10; \
568 else if( c >= 'A' && c <= 'F' ) \
569 c -= 'A' - 10; \
570 else \
571 return( -1 );
572
573/*
574 * Convert a hex string to bytes.
575 * Return 0 on success, -1 on error.
576 */
577int unhexify( unsigned char *output, const char *input, size_t *olen )
578{
579 unsigned char c;
580 size_t j;
581
582 *olen = strlen( input );
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200583 if( *olen % 2 != 0 || *olen / 2 > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200584 return( -1 );
585 *olen /= 2;
586
587 for( j = 0; j < *olen * 2; j += 2 )
588 {
589 c = input[j];
590 HEX2NUM( c );
591 output[ j / 2 ] = c << 4;
592
593 c = input[j + 1];
594 HEX2NUM( c );
595 output[ j / 2 ] |= c;
596 }
597
598 return( 0 );
599}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200600
601typedef struct _psk_entry psk_entry;
602
603struct _psk_entry
604{
605 const char *name;
606 size_t key_len;
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200607 unsigned char key[POLARSSL_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200608 psk_entry *next;
609};
610
611/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000612 * Free a list of psk_entry's
613 */
614void psk_free( psk_entry *head )
615{
616 psk_entry *next;
617
618 while( head != NULL )
619 {
620 next = head->next;
621 polarssl_free( head );
622 head = next;
623 }
624}
625
626/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200627 * Parse a string of pairs name1,key1[,name2,key2[,...]]
628 * into a usable psk_entry list.
629 *
630 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200631 */
632psk_entry *psk_parse( char *psk_string )
633{
634 psk_entry *cur = NULL, *new = NULL;
635 char *p = psk_string;
636 char *end = p;
637 char *key_hex;
638
639 while( *end != '\0' )
640 ++end;
641 *end = ',';
642
643 while( p <= end )
644 {
645 if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL )
Manuel Pégourié-Gonnardb1990952015-02-18 09:32:06 +0000646 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200647
648 memset( new, 0, sizeof( psk_entry ) );
649
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200650 GET_ITEM( new->name );
651 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200652
653 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000654 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200655
656 new->next = cur;
657 cur = new;
658 }
659
660 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200661
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000662error:
663 psk_free( new );
664 psk_free( cur );
665 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200666}
667
668/*
669 * PSK callback
670 */
671int psk_callback( void *p_info, ssl_context *ssl,
672 const unsigned char *name, size_t name_len )
673{
674 psk_entry *cur = (psk_entry *) p_info;
675
676 while( cur != NULL )
677 {
678 if( name_len == strlen( cur->name ) &&
679 memcmp( name, cur->name, name_len ) == 0 )
680 {
681 return( ssl_set_psk( ssl, cur->key, cur->key_len,
682 name, name_len ) );
683 }
684
685 cur = cur->next;
686 }
687
688 return( -1 );
689}
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200690#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
691
Manuel Pégourié-Gonnarda9d7d032014-10-09 16:07:08 +0200692static int listen_fd, client_fd = -1;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200693
694/* Interruption handler to ensure clean exit (for valgrind testing) */
695#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200696static int received_sigterm = 0;
697void term_handler( int sig )
698{
699 ((void) sig);
700 received_sigterm = 1;
701 net_close( listen_fd ); /* causes net_accept() to abort */
Manuel Pégourié-Gonnarda9d7d032014-10-09 16:07:08 +0200702 net_close( client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200703}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200704#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200705
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000706int main( int argc, char *argv[] )
707{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +0000708 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200709 int version_suites[4][2];
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200710 unsigned char buf[IO_BUF_LEN];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200711#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200712 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +0200713 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +0200714 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +0200715#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200716 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +0200717 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +0200718#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200719 ssl_cookie_ctx cookie_ctx;
720#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000721
722 entropy_context entropy;
723 ctr_drbg_context ctr_drbg;
724 ssl_context ssl;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100725#if defined(POLARSSL_SSL_RENEGOTIATION)
726 unsigned char renego_period[8] = { 0 };
727#endif
Paul Bakker36713e82013-09-17 13:25:29 +0200728#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200729 x509_crt cacert;
730 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200731 pk_context pkey;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200732 x509_crt srvcert2;
733 pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200734 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200735#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200736#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
737 dhm_context dhm;
738#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000739#if defined(POLARSSL_SSL_CACHE_C)
740 ssl_cache_context cache;
741#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100742#if defined(POLARSSL_SNI)
743 sni_entry *sni_info = NULL;
744#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200745#if defined(POLARSSL_SSL_ALPN)
746 const char *alpn_list[10];
747#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200748#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
749 unsigned char alloc_buf[100000];
750#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000751
752 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000753 char *p, *q;
754 const int *list;
755
Paul Bakker82024bf2013-07-04 11:52:32 +0200756#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
757 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
758#endif
759
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000760 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200761 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000762 */
763 listen_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200764 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200765#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200766 x509_crt_init( &cacert );
767 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200768 pk_init( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200769 x509_crt_init( &srvcert2 );
770 pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200771#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200772#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
Paul Bakkera317a982014-06-18 16:44:11 +0200773 dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200774#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000775#if defined(POLARSSL_SSL_CACHE_C)
776 ssl_cache_init( &cache );
777#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200778#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200779 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200780#endif
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +0200781#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200782 ssl_cookie_init( &cookie_ctx );
783#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000784
Paul Bakkerc1283d32014-08-18 11:05:51 +0200785#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100786 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200787 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100788 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +0200789#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200790
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000791 if( argc == 0 )
792 {
793 usage:
794 if( ret == 0 )
795 ret = 1;
796
Rich Evansf90016a2015-01-19 14:26:37 +0000797 polarssl_printf( USAGE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000798
799 list = ssl_list_ciphersuites();
800 while( *list )
801 {
Rich Evansf90016a2015-01-19 14:26:37 +0000802 polarssl_printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200803 list++;
804 if( !*list )
805 break;
Rich Evansf90016a2015-01-19 14:26:37 +0000806 polarssl_printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000807 list++;
808 }
Rich Evansf90016a2015-01-19 14:26:37 +0000809 polarssl_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000810 goto exit;
811 }
812
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100813 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000814 opt.server_port = DFL_SERVER_PORT;
815 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100816 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200817 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000818 opt.ca_file = DFL_CA_FILE;
819 opt.ca_path = DFL_CA_PATH;
820 opt.crt_file = DFL_CRT_FILE;
821 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200822 opt.crt_file2 = DFL_CRT_FILE2;
823 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200824 opt.psk = DFL_PSK;
825 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200826 opt.psk_list = DFL_PSK_LIST;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000827 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200828 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000829 opt.renegotiation = DFL_RENEGOTIATION;
830 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100831 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200832 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100833 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200834 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000835 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200836 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100837 opt.arc4 = DFL_ARC4;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100838 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200839 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100840 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200841 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100842 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100843 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100844 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100845 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200846 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200847 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100848 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200849 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200850 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200851 opt.hs_to_min = DFL_HS_TO_MIN;
852 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200853 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200854 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100855 opt.etm = DFL_ETM;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000856
857 for( i = 1; i < argc; i++ )
858 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000859 p = argv[i];
860 if( ( q = strchr( p, '=' ) ) == NULL )
861 goto usage;
862 *q++ = '\0';
863
864 if( strcmp( p, "server_port" ) == 0 )
865 {
866 opt.server_port = atoi( q );
867 if( opt.server_port < 1 || opt.server_port > 65535 )
868 goto usage;
869 }
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100870 else if( strcmp( p, "server_addr" ) == 0 )
871 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100872 else if( strcmp( p, "dtls" ) == 0 )
873 {
874 int t = atoi( q );
875 if( t == 0 )
876 opt.transport = SSL_TRANSPORT_STREAM;
877 else if( t == 1 )
878 opt.transport = SSL_TRANSPORT_DATAGRAM;
879 else
880 goto usage;
881 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000882 else if( strcmp( p, "debug_level" ) == 0 )
883 {
884 opt.debug_level = atoi( q );
885 if( opt.debug_level < 0 || opt.debug_level > 65535 )
886 goto usage;
887 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100888 else if( strcmp( p, "nbio" ) == 0 )
889 {
890 opt.nbio = atoi( q );
891 if( opt.nbio < 0 || opt.nbio > 2 )
892 goto usage;
893 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200894 else if( strcmp( p, "read_timeout" ) == 0 )
895 opt.read_timeout = atoi( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000896 else if( strcmp( p, "ca_file" ) == 0 )
897 opt.ca_file = q;
898 else if( strcmp( p, "ca_path" ) == 0 )
899 opt.ca_path = q;
900 else if( strcmp( p, "crt_file" ) == 0 )
901 opt.crt_file = q;
902 else if( strcmp( p, "key_file" ) == 0 )
903 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200904 else if( strcmp( p, "crt_file2" ) == 0 )
905 opt.crt_file2 = q;
906 else if( strcmp( p, "key_file2" ) == 0 )
907 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200908 else if( strcmp( p, "dhm_file" ) == 0 )
909 opt.dhm_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200910 else if( strcmp( p, "psk" ) == 0 )
911 opt.psk = q;
912 else if( strcmp( p, "psk_identity" ) == 0 )
913 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200914 else if( strcmp( p, "psk_list" ) == 0 )
915 opt.psk_list = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000916 else if( strcmp( p, "force_ciphersuite" ) == 0 )
917 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000918 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
919
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200920 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000921 {
922 ret = 2;
923 goto usage;
924 }
925 opt.force_ciphersuite[1] = 0;
926 }
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200927 else if( strcmp( p, "version_suites" ) == 0 )
928 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000929 else if( strcmp( p, "renegotiation" ) == 0 )
930 {
931 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
932 SSL_RENEGOTIATION_DISABLED;
933 }
934 else if( strcmp( p, "allow_legacy" ) == 0 )
935 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100936 switch( atoi( q ) )
937 {
938 case -1: opt.allow_legacy = SSL_LEGACY_BREAK_HANDSHAKE; break;
939 case 0: opt.allow_legacy = SSL_LEGACY_NO_RENEGOTIATION; break;
940 case 1: opt.allow_legacy = SSL_LEGACY_ALLOW_RENEGOTIATION; break;
941 default: goto usage;
942 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000943 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100944 else if( strcmp( p, "renegotiate" ) == 0 )
945 {
946 opt.renegotiate = atoi( q );
947 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
948 goto usage;
949 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200950 else if( strcmp( p, "renego_delay" ) == 0 )
951 {
952 opt.renego_delay = atoi( q );
953 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100954 else if( strcmp( p, "renego_period" ) == 0 )
955 {
956 opt.renego_period = atoi( q );
957 if( opt.renego_period < 2 || opt.renego_period > 255 )
958 goto usage;
959 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200960 else if( strcmp( p, "exchanges" ) == 0 )
961 {
962 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +0200963 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200964 goto usage;
965 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000966 else if( strcmp( p, "min_version" ) == 0 )
967 {
968 if( strcmp( q, "ssl3" ) == 0 )
969 opt.min_version = SSL_MINOR_VERSION_0;
970 else if( strcmp( q, "tls1" ) == 0 )
971 opt.min_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100972 else if( strcmp( q, "tls1_1" ) == 0 ||
973 strcmp( q, "dtls1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000974 opt.min_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100975 else if( strcmp( q, "tls1_2" ) == 0 ||
976 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000977 opt.min_version = SSL_MINOR_VERSION_3;
978 else
979 goto usage;
980 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200981 else if( strcmp( p, "max_version" ) == 0 )
982 {
983 if( strcmp( q, "ssl3" ) == 0 )
984 opt.max_version = SSL_MINOR_VERSION_0;
985 else if( strcmp( q, "tls1" ) == 0 )
986 opt.max_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100987 else if( strcmp( q, "tls1_1" ) == 0 ||
988 strcmp( q, "dtls1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200989 opt.max_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100990 else if( strcmp( q, "tls1_2" ) == 0 ||
991 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200992 opt.max_version = SSL_MINOR_VERSION_3;
993 else
994 goto usage;
995 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100996 else if( strcmp( p, "arc4" ) == 0 )
997 {
998 switch( atoi( q ) )
999 {
1000 case 0: opt.arc4 = SSL_ARC4_DISABLED; break;
1001 case 1: opt.arc4 = SSL_ARC4_ENABLED; break;
1002 default: goto usage;
1003 }
1004 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001005 else if( strcmp( p, "force_version" ) == 0 )
1006 {
1007 if( strcmp( q, "ssl3" ) == 0 )
1008 {
1009 opt.min_version = SSL_MINOR_VERSION_0;
1010 opt.max_version = SSL_MINOR_VERSION_0;
1011 }
1012 else if( strcmp( q, "tls1" ) == 0 )
1013 {
1014 opt.min_version = SSL_MINOR_VERSION_1;
1015 opt.max_version = SSL_MINOR_VERSION_1;
1016 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001017 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001018 {
1019 opt.min_version = SSL_MINOR_VERSION_2;
1020 opt.max_version = SSL_MINOR_VERSION_2;
1021 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001022 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001023 {
1024 opt.min_version = SSL_MINOR_VERSION_3;
1025 opt.max_version = SSL_MINOR_VERSION_3;
1026 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001027 else if( strcmp( q, "dtls1" ) == 0 )
1028 {
1029 opt.min_version = SSL_MINOR_VERSION_2;
1030 opt.max_version = SSL_MINOR_VERSION_2;
1031 opt.transport = SSL_TRANSPORT_DATAGRAM;
1032 }
1033 else if( strcmp( q, "dtls1_2" ) == 0 )
1034 {
1035 opt.min_version = SSL_MINOR_VERSION_3;
1036 opt.max_version = SSL_MINOR_VERSION_3;
1037 opt.transport = SSL_TRANSPORT_DATAGRAM;
1038 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001039 else
1040 goto usage;
1041 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001042 else if( strcmp( p, "auth_mode" ) == 0 )
1043 {
1044 if( strcmp( q, "none" ) == 0 )
1045 opt.auth_mode = SSL_VERIFY_NONE;
1046 else if( strcmp( q, "optional" ) == 0 )
1047 opt.auth_mode = SSL_VERIFY_OPTIONAL;
1048 else if( strcmp( q, "required" ) == 0 )
1049 opt.auth_mode = SSL_VERIFY_REQUIRED;
1050 else
1051 goto usage;
1052 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001053 else if( strcmp( p, "max_frag_len" ) == 0 )
1054 {
1055 if( strcmp( q, "512" ) == 0 )
1056 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
1057 else if( strcmp( q, "1024" ) == 0 )
1058 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
1059 else if( strcmp( q, "2048" ) == 0 )
1060 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
1061 else if( strcmp( q, "4096" ) == 0 )
1062 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
1063 else
1064 goto usage;
1065 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001066 else if( strcmp( p, "alpn" ) == 0 )
1067 {
1068 opt.alpn_string = q;
1069 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001070 else if( strcmp( p, "trunc_hmac" ) == 0 )
1071 {
1072 switch( atoi( q ) )
1073 {
1074 case 0: opt.trunc_hmac = SSL_TRUNC_HMAC_DISABLED; break;
1075 case 1: opt.trunc_hmac = SSL_TRUNC_HMAC_ENABLED; break;
1076 default: goto usage;
1077 }
1078 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001079 else if( strcmp( p, "extended_ms" ) == 0 )
1080 {
1081 switch( atoi( q ) )
1082 {
1083 case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break;
1084 case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break;
1085 default: goto usage;
1086 }
1087 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001088 else if( strcmp( p, "etm" ) == 0 )
1089 {
1090 switch( atoi( q ) )
1091 {
1092 case 0: opt.etm = SSL_ETM_DISABLED; break;
1093 case 1: opt.etm = SSL_ETM_ENABLED; break;
1094 default: goto usage;
1095 }
1096 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001097 else if( strcmp( p, "tickets" ) == 0 )
1098 {
1099 opt.tickets = atoi( q );
1100 if( opt.tickets < 0 || opt.tickets > 1 )
1101 goto usage;
1102 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001103 else if( strcmp( p, "ticket_timeout" ) == 0 )
1104 {
1105 opt.ticket_timeout = atoi( q );
1106 if( opt.ticket_timeout < 0 )
1107 goto usage;
1108 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001109 else if( strcmp( p, "cache_max" ) == 0 )
1110 {
1111 opt.cache_max = atoi( q );
1112 if( opt.cache_max < 0 )
1113 goto usage;
1114 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001115 else if( strcmp( p, "cache_timeout" ) == 0 )
1116 {
1117 opt.cache_timeout = atoi( q );
1118 if( opt.cache_timeout < 0 )
1119 goto usage;
1120 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001121 else if( strcmp( p, "cookies" ) == 0 )
1122 {
1123 opt.cookies = atoi( q );
1124 if( opt.cookies < -1 || opt.cookies > 1)
1125 goto usage;
1126 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001127 else if( strcmp( p, "anti_replay" ) == 0 )
1128 {
1129 opt.anti_replay = atoi( q );
1130 if( opt.anti_replay < 0 || opt.anti_replay > 1)
1131 goto usage;
1132 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001133 else if( strcmp( p, "badmac_limit" ) == 0 )
1134 {
1135 opt.badmac_limit = atoi( q );
1136 if( opt.badmac_limit < 0 )
1137 goto usage;
1138 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001139 else if( strcmp( p, "hs_timeout" ) == 0 )
1140 {
1141 if( ( p = strchr( q, '-' ) ) == NULL )
1142 goto usage;
1143 *p++ = '\0';
1144 opt.hs_to_min = atoi( q );
1145 opt.hs_to_max = atoi( p );
1146 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1147 goto usage;
1148 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001149 else if( strcmp( p, "sni" ) == 0 )
1150 {
1151 opt.sni = q;
1152 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001153 else
1154 goto usage;
1155 }
1156
Paul Bakkerc73079a2014-04-25 16:34:30 +02001157#if defined(POLARSSL_DEBUG_C)
1158 debug_set_threshold( opt.debug_level );
1159#endif
1160
Paul Bakkerc1516be2013-06-29 16:01:32 +02001161 if( opt.force_ciphersuite[0] > 0 )
1162 {
1163 const ssl_ciphersuite_t *ciphersuite_info;
1164 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1165
Paul Bakker5b55b792013-07-19 13:43:43 +02001166 if( opt.max_version != -1 &&
1167 ciphersuite_info->min_minor_ver > opt.max_version )
1168 {
Rich Evansf90016a2015-01-19 14:26:37 +00001169 polarssl_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakker5b55b792013-07-19 13:43:43 +02001170 ret = 2;
1171 goto usage;
1172 }
1173 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001174 ciphersuite_info->max_minor_ver < opt.min_version )
1175 {
Rich Evansf90016a2015-01-19 14:26:37 +00001176 polarssl_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +02001177 ret = 2;
1178 goto usage;
1179 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001180
1181 /* If we select a version that's not supported by
1182 * this suite, then there will be no common ciphersuite... */
1183 if( opt.max_version == -1 ||
1184 opt.max_version > ciphersuite_info->max_minor_ver )
1185 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001186 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001187 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001188 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001189 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001190 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001191 /* DTLS starts with TLS 1.1 */
1192 if( opt.transport == SSL_TRANSPORT_DATAGRAM &&
1193 opt.min_version < SSL_MINOR_VERSION_2 )
1194 opt.min_version = SSL_MINOR_VERSION_2;
1195 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001196 }
1197
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001198 if( opt.version_suites != NULL )
1199 {
1200 const char *name[4] = { 0 };
1201
1202 /* Parse 4-element coma-separated list */
1203 for( i = 0, p = (char *) opt.version_suites;
1204 i < 4 && *p != '\0';
1205 i++ )
1206 {
1207 name[i] = p;
1208
1209 /* Terminate the current string and move on to next one */
1210 while( *p != ',' && *p != '\0' )
1211 p++;
1212 if( *p == ',' )
1213 *p++ = '\0';
1214 }
1215
1216 if( i != 4 )
1217 {
Rich Evansf90016a2015-01-19 14:26:37 +00001218 polarssl_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001219 ret = 1;
1220 goto exit;
1221 }
1222
1223 memset( version_suites, 0, sizeof( version_suites ) );
1224
1225 /* Get the suites identifiers from their name */
1226 for( i = 0; i < 4; i++ )
1227 {
1228 version_suites[i][0] = ssl_get_ciphersuite_id( name[i] );
1229
1230 if( version_suites[i][0] == 0 )
1231 {
Rich Evansf90016a2015-01-19 14:26:37 +00001232 polarssl_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001233 ret = 2;
1234 goto usage;
1235 }
1236 }
1237 }
1238
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001239#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001240 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001241 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001242 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001243 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001244 {
Rich Evansf90016a2015-01-19 14:26:37 +00001245 polarssl_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001246 goto exit;
1247 }
1248
1249 if( opt.psk_list != NULL )
1250 {
1251 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001252 {
Rich Evansf90016a2015-01-19 14:26:37 +00001253 polarssl_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001254 goto exit;
1255 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001256 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001257#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001258
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001259#if defined(POLARSSL_SSL_ALPN)
1260 if( opt.alpn_string != NULL )
1261 {
1262 p = (char *) opt.alpn_string;
1263 i = 0;
1264
1265 /* Leave room for a final NULL in alpn_list */
1266 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
1267 {
1268 alpn_list[i++] = p;
1269
1270 /* Terminate the current string and move on to next one */
1271 while( *p != ',' && *p != '\0' )
1272 p++;
1273 if( *p == ',' )
1274 *p++ = '\0';
1275 }
1276 }
1277#endif /* POLARSSL_SSL_ALPN */
1278
Paul Bakkerfbb17802013-04-17 19:10:21 +02001279 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001280 * 0. Initialize the RNG and the session data
1281 */
Rich Evansf90016a2015-01-19 14:26:37 +00001282 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001283 fflush( stdout );
1284
1285 entropy_init( &entropy );
1286 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001287 (const unsigned char *) pers,
1288 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001289 {
Rich Evansf90016a2015-01-19 14:26:37 +00001290 polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001291 goto exit;
1292 }
1293
Rich Evansf90016a2015-01-19 14:26:37 +00001294 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001295
Paul Bakker36713e82013-09-17 13:25:29 +02001296#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001297 /*
1298 * 1.1. Load the trusted CA
1299 */
Rich Evansf90016a2015-01-19 14:26:37 +00001300 polarssl_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001301 fflush( stdout );
1302
1303#if defined(POLARSSL_FS_IO)
1304 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001305 if( strcmp( opt.ca_path, "none" ) == 0 )
1306 ret = 0;
1307 else
1308 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001309 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001310 if( strcmp( opt.ca_file, "none" ) == 0 )
1311 ret = 0;
1312 else
1313 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001314 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001315#endif
1316#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +02001317 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
1318 strlen( test_ca_list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001319#else
1320 {
1321 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +00001322 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001323 }
1324#endif
1325 if( ret < 0 )
1326 {
Rich Evansf90016a2015-01-19 14:26:37 +00001327 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001328 goto exit;
1329 }
1330
Rich Evansf90016a2015-01-19 14:26:37 +00001331 polarssl_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001332
1333 /*
1334 * 1.2. Load own certificate and private key
1335 */
Rich Evansf90016a2015-01-19 14:26:37 +00001336 polarssl_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001337 fflush( stdout );
1338
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001339#if defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001340 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001341 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001342 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001343 if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
1344 {
Rich Evansf90016a2015-01-19 14:26:37 +00001345 polarssl_printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001346 -ret );
1347 goto exit;
1348 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001349 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001350 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001351 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001352 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001353 if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
1354 {
Rich Evansf90016a2015-01-19 14:26:37 +00001355 polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001356 goto exit;
1357 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001358 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001359 if( key_cert_init == 1 )
1360 {
Rich Evansf90016a2015-01-19 14:26:37 +00001361 polarssl_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001362 goto exit;
1363 }
1364
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001365 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001366 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001367 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001368 if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
1369 {
Rich Evansf90016a2015-01-19 14:26:37 +00001370 polarssl_printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001371 -ret );
1372 goto exit;
1373 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001374 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001375 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001376 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001377 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001378 if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
1379 {
Rich Evansf90016a2015-01-19 14:26:37 +00001380 polarssl_printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001381 -ret );
1382 goto exit;
1383 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001384 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001385 if( key_cert_init2 == 1 )
1386 {
Rich Evansf90016a2015-01-19 14:26:37 +00001387 polarssl_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001388 goto exit;
1389 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001390#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001391 if( key_cert_init == 0 &&
1392 strcmp( opt.crt_file, "none" ) != 0 &&
1393 strcmp( opt.key_file, "none" ) != 0 &&
1394 key_cert_init2 == 0 &&
1395 strcmp( opt.crt_file2, "none" ) != 0 &&
1396 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001397 {
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001398#if !defined(POLARSSL_CERTS_C)
Rich Evansf90016a2015-01-19 14:26:37 +00001399 polarssl_printf( "Not certificated or key provided, and \n"
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001400 "POLARSSL_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001401 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001402#else
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001403#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001404 if( ( ret = x509_crt_parse( &srvcert,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001405 (const unsigned char *) test_srv_crt_rsa,
1406 strlen( test_srv_crt_rsa ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001407 {
Rich Evansf90016a2015-01-19 14:26:37 +00001408 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001409 goto exit;
1410 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001411 if( ( ret = pk_parse_key( &pkey,
1412 (const unsigned char *) test_srv_key_rsa,
1413 strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001414 {
Rich Evansf90016a2015-01-19 14:26:37 +00001415 polarssl_printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001416 goto exit;
1417 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001418 key_cert_init = 2;
1419#endif /* POLARSSL_RSA_C */
1420#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001421 if( ( ret = x509_crt_parse( &srvcert2,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001422 (const unsigned char *) test_srv_crt_ec,
1423 strlen( test_srv_crt_ec ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001424 {
Rich Evansf90016a2015-01-19 14:26:37 +00001425 polarssl_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001426 goto exit;
1427 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001428 if( ( ret = pk_parse_key( &pkey2,
1429 (const unsigned char *) test_srv_key_ec,
1430 strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001431 {
Rich Evansf90016a2015-01-19 14:26:37 +00001432 polarssl_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001433 goto exit;
1434 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001435 key_cert_init2 = 2;
1436#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001437#endif /* POLARSSL_CERTS_C */
1438 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001439
Rich Evansf90016a2015-01-19 14:26:37 +00001440 polarssl_printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001441#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001442
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001443#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1444 if( opt.dhm_file != NULL )
1445 {
Rich Evansf90016a2015-01-19 14:26:37 +00001446 polarssl_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001447 fflush( stdout );
1448
1449 if( ( ret = dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
1450 {
Rich Evansf90016a2015-01-19 14:26:37 +00001451 polarssl_printf( " failed\n ! dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001452 -ret );
1453 goto exit;
1454 }
1455
Rich Evansf90016a2015-01-19 14:26:37 +00001456 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001457 }
1458#endif
1459
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001460#if defined(POLARSSL_SNI)
1461 if( opt.sni != NULL )
1462 {
Rich Evansf90016a2015-01-19 14:26:37 +00001463 polarssl_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001464 fflush( stdout );
1465
1466 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
1467 {
Rich Evansf90016a2015-01-19 14:26:37 +00001468 polarssl_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001469 goto exit;
1470 }
1471
Rich Evansf90016a2015-01-19 14:26:37 +00001472 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001473 }
1474#endif /* POLARSSL_SNI */
1475
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001476 /*
1477 * 2. Setup the listening TCP socket
1478 */
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00001479 polarssl_printf( " . Bind on %s://%s:%-4d/ ...",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001480 opt.transport == SSL_TRANSPORT_STREAM ? "tcp" : "udp",
1481 opt.server_addr ? opt.server_addr : "*",
1482 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001483 fflush( stdout );
1484
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001485 if( ( ret = net_bind( &listen_fd, opt.server_addr, opt.server_port,
1486 opt.transport == SSL_TRANSPORT_STREAM ?
1487 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001488 {
Rich Evansf90016a2015-01-19 14:26:37 +00001489 polarssl_printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001490 goto exit;
1491 }
1492
Rich Evansf90016a2015-01-19 14:26:37 +00001493 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001494
1495 /*
1496 * 3. Setup stuff
1497 */
Rich Evansf90016a2015-01-19 14:26:37 +00001498 polarssl_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001499 fflush( stdout );
1500
1501 if( ( ret = ssl_init( &ssl ) ) != 0 )
1502 {
Rich Evansf90016a2015-01-19 14:26:37 +00001503 polarssl_printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001504 goto exit;
1505 }
1506
1507 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001508 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001509
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001510#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001511 if( ( ret = ssl_set_transport( &ssl, opt.transport ) ) != 0 )
1512 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +00001513 polarssl_printf( " failed\n ! selected transport is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001514 goto exit;
1515 }
1516
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001517 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
1518 ssl_set_handshake_timeout( &ssl, opt.hs_to_min, opt.hs_to_max );
1519#endif /* POLARSSL_SSL_PROTO_DTLS */
1520
Paul Bakker05decb22013-08-15 13:33:48 +02001521#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001522 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1523 {
Rich Evansf90016a2015-01-19 14:26:37 +00001524 polarssl_printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001525 goto exit;
1526 };
Paul Bakker05decb22013-08-15 13:33:48 +02001527#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001528
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001529#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1530 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
1531 ssl_set_truncated_hmac( &ssl, opt.trunc_hmac );
1532#endif
1533
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001534#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1535 if( opt.extended_ms != DFL_EXTENDED_MS )
1536 ssl_set_extended_master_secret( &ssl, opt.extended_ms );
1537#endif
1538
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001539#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1540 if( opt.etm != DFL_ETM )
1541 ssl_set_encrypt_then_mac( &ssl, opt.etm );
1542#endif
1543
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001544#if defined(POLARSSL_SSL_ALPN)
1545 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001546 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1547 {
Rich Evansf90016a2015-01-19 14:26:37 +00001548 polarssl_printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001549 goto exit;
1550 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001551#endif
1552
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001553 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
1554 ssl_set_dbg( &ssl, my_debug, stdout );
1555
Paul Bakker0a597072012-09-25 21:55:46 +00001556#if defined(POLARSSL_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001557 if( opt.cache_max != -1 )
1558 ssl_cache_set_max_entries( &cache, opt.cache_max );
1559
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001560 if( opt.cache_timeout != -1 )
1561 ssl_cache_set_timeout( &cache, opt.cache_timeout );
1562
Paul Bakker0a597072012-09-25 21:55:46 +00001563 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
1564 ssl_cache_set, &cache );
1565#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001566
Paul Bakkera503a632013-08-14 13:48:06 +02001567#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001568 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1569 {
Rich Evansf90016a2015-01-19 14:26:37 +00001570 polarssl_printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001571 goto exit;
1572 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001573
1574 if( opt.ticket_timeout != -1 )
1575 ssl_set_session_ticket_lifetime( &ssl, opt.ticket_timeout );
Paul Bakkera503a632013-08-14 13:48:06 +02001576#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001577
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001578#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001579 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001580 {
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001581#if defined(POLARSSL_SSL_COOKIE_C)
1582 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001583 {
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001584 if( ( ret = ssl_cookie_setup( &cookie_ctx,
1585 ctr_drbg_random, &ctr_drbg ) ) != 0 )
1586 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +00001587 polarssl_printf( " failed\n ! ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001588 goto exit;
1589 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001590
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001591 ssl_set_dtls_cookies( &ssl, ssl_cookie_write, ssl_cookie_check,
1592 &cookie_ctx );
1593 }
1594 else
1595#endif /* POLARSSL_SSL_COOKIE_C */
1596#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
1597 if( opt.cookies == 0 )
1598 {
1599 ssl_set_dtls_cookies( &ssl, NULL, NULL, NULL );
1600 }
1601 else
1602#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
1603 {
1604 ; /* Nothing to do */
1605 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001606
1607#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1608 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001609 ssl_set_dtls_anti_replay( &ssl, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001610#endif
1611
1612#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
1613 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
1614 ssl_set_dtls_badmac_limit( &ssl, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001615#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001616 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001617#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001618
Paul Bakker41c83d32013-03-20 14:39:14 +01001619 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001620 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001621 else
1622 ssl_set_arc4_support( &ssl, opt.arc4 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001623
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001624 if( opt.version_suites != NULL )
1625 {
1626 ssl_set_ciphersuites_for_version( &ssl, version_suites[0],
1627 SSL_MAJOR_VERSION_3,
1628 SSL_MINOR_VERSION_0 );
1629 ssl_set_ciphersuites_for_version( &ssl, version_suites[1],
1630 SSL_MAJOR_VERSION_3,
1631 SSL_MINOR_VERSION_1 );
1632 ssl_set_ciphersuites_for_version( &ssl, version_suites[2],
1633 SSL_MAJOR_VERSION_3,
1634 SSL_MINOR_VERSION_2 );
1635 ssl_set_ciphersuites_for_version( &ssl, version_suites[3],
1636 SSL_MAJOR_VERSION_3,
1637 SSL_MINOR_VERSION_3 );
1638 }
1639
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001640 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
1641 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001642#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001643 ssl_set_renegotiation( &ssl, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001644
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001645 if( opt.renego_delay != DFL_RENEGO_DELAY )
1646 ssl_set_renegotiation_enforced( &ssl, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001647
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001648 if( opt.renego_period != DFL_RENEGO_PERIOD )
1649 {
1650 renego_period[7] = opt.renego_period;
1651 ssl_set_renegotiation_period( &ssl, renego_period );
1652 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001653#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001654
Paul Bakker36713e82013-09-17 13:25:29 +02001655#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001656 if( strcmp( opt.ca_path, "none" ) != 0 &&
1657 strcmp( opt.ca_file, "none" ) != 0 )
1658 {
1659 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
1660 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001661 if( key_cert_init )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001662 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
1663 {
Rich Evansf90016a2015-01-19 14:26:37 +00001664 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001665 goto exit;
1666 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001667 if( key_cert_init2 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001668 if( ( ret = ssl_set_own_cert( &ssl, &srvcert2, &pkey2 ) ) != 0 )
1669 {
Rich Evansf90016a2015-01-19 14:26:37 +00001670 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001671 goto exit;
1672 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001673#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001674
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001675#if defined(POLARSSL_SNI)
1676 if( opt.sni != NULL )
1677 ssl_set_sni( &ssl, sni_callback, sni_info );
1678#endif
1679
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001680#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001681 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
1682 {
1683 ret = ssl_set_psk( &ssl, psk, psk_len,
1684 (const unsigned char *) opt.psk_identity,
1685 strlen( opt.psk_identity ) );
1686 if( ret != 0 )
1687 {
Rich Evansf90016a2015-01-19 14:26:37 +00001688 polarssl_printf( " failed\n ssl_set_psk returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001689 goto exit;
1690 }
1691 }
1692
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001693 if( opt.psk_list != NULL )
1694 ssl_set_psk_cb( &ssl, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02001695#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001696
1697#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00001698 /*
1699 * Use different group than default DHM group
1700 */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001701#if defined(POLARSSL_FS_IO)
1702 if( opt.dhm_file != NULL )
1703 ret = ssl_set_dh_param_ctx( &ssl, &dhm );
1704 else
1705#endif
1706 ret = ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
1707 POLARSSL_DHM_RFC5114_MODP_2048_G );
1708
1709 if( ret != 0 )
1710 {
Rich Evansf90016a2015-01-19 14:26:37 +00001711 polarssl_printf( " failed\n ssl_set_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001712 goto exit;
1713 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001714#endif
1715
Paul Bakker1d29fb52012-09-28 13:28:45 +00001716 if( opt.min_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001717 {
1718 ret = ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
Manuel Pégourié-Gonnard23eb74d2015-01-21 14:37:13 +00001719 if( ret != 0 && opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001720 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +00001721 polarssl_printf( " failed\n ! selected min_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001722 goto exit;
1723 }
1724 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001725
Paul Bakkerc1516be2013-06-29 16:01:32 +02001726 if( opt.max_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001727 {
1728 ret = ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1729 if( ret != 0 )
1730 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +00001731 polarssl_printf( " failed\n ! selected max_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001732 goto exit;
1733 }
1734 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001735
Rich Evansf90016a2015-01-19 14:26:37 +00001736 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001737
1738reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02001739#if !defined(_WIN32)
1740 if( received_sigterm )
1741 {
Manuel Pégourié-Gonnard7e81e702015-01-29 11:47:41 +00001742 polarssl_printf( " interrupted by SIGTERM\n" );
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02001743 ret = 0;
1744 goto exit;
1745 }
1746#endif
1747
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001748#ifdef POLARSSL_ERROR_C
1749 if( ret != 0 )
1750 {
1751 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001752 polarssl_strerror( ret, error_buf, 100 );
Rich Evansf90016a2015-01-19 14:26:37 +00001753 polarssl_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001754 }
1755#endif
1756
1757 if( client_fd != -1 )
Manuel Pégourié-Gonnard4ba6ab62014-08-07 17:21:47 +02001758 net_close( client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001759
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001760 ssl_session_reset( &ssl );
1761
1762 /*
1763 * 3. Wait until a client connects
1764 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001765 client_fd = -1;
1766
Rich Evansf90016a2015-01-19 14:26:37 +00001767 polarssl_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001768 fflush( stdout );
1769
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001770 if( ( ret = net_accept( listen_fd, &client_fd, client_ip ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001771 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02001772#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001773 if( received_sigterm )
1774 {
Rich Evansf90016a2015-01-19 14:26:37 +00001775 polarssl_printf( " interrupted by signal\n" );
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001776 ret = 0;
1777 goto exit;
1778 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02001779#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001780
Rich Evansf90016a2015-01-19 14:26:37 +00001781 polarssl_printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001782 goto exit;
1783 }
1784
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001785 if( opt.nbio > 0 )
1786 ret = net_set_nonblock( client_fd );
1787 else
1788 ret = net_set_block( client_fd );
1789 if( ret != 0 )
1790 {
Rich Evansf90016a2015-01-19 14:26:37 +00001791 polarssl_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001792 goto exit;
1793 }
1794
1795 if( opt.nbio == 2 )
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001796 ssl_set_bio_timeout( &ssl, &client_fd, my_send, my_recv, NULL, 0 );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001797 else
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001798 ssl_set_bio_timeout( &ssl, &client_fd, net_send, net_recv,
1799#if defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnardf0365122014-09-29 16:11:47 +02001800 opt.nbio == 0 ? net_recv_timeout : NULL,
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001801#else
1802 NULL,
1803#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001804 opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001805
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001806#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001807 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
1808 {
1809 if( ( ret = ssl_set_client_transport_id( &ssl, client_ip,
1810 sizeof( client_ip ) ) ) != 0 )
1811 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +00001812 polarssl_printf( " failed\n ! "
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001813 "ssl_set_client_tranport_id() returned -0x%x\n\n", -ret );
1814 goto exit;
1815 }
1816 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001817#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001818
Rich Evansf90016a2015-01-19 14:26:37 +00001819 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001820
1821 /*
Manuel Pégourié-Gonnardbd97fdb2014-09-26 16:46:36 +02001822 * With UDP, bind_fd is hijacked by client_fd, so bind a new one
1823 */
1824#if defined(POLARSSL_SSL_PROTO_DTLS)
1825 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
1826 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +00001827 polarssl_printf( " . Re-bind on udp://%s:%-4d/ ...",
Manuel Pégourié-Gonnardbd97fdb2014-09-26 16:46:36 +02001828 opt.server_addr ? opt.server_addr : "*",
1829 opt.server_port );
1830 fflush( stdout );
1831
1832 if( ( ret = net_bind( &listen_fd, opt.server_addr,
1833 opt.server_port, NET_PROTO_UDP ) ) != 0 )
1834 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +00001835 polarssl_printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardbd97fdb2014-09-26 16:46:36 +02001836 goto exit;
1837 }
1838
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +00001839 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnardbd97fdb2014-09-26 16:46:36 +02001840 }
1841#endif /* POLARSSL_SSL_PROTO_DTLS */
1842
1843 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001844 * 4. Handshake
1845 */
Rich Evansf90016a2015-01-19 14:26:37 +00001846 polarssl_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001847 fflush( stdout );
1848
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001849 do ret = ssl_handshake( &ssl );
1850 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1851 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001852
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001853 if( ret == POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED )
1854 {
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00001855 polarssl_printf( " hello verification requested\n" );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001856 ret = 0;
1857 goto reset;
1858 }
1859 else if( ret != 0 )
1860 {
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00001861 polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001862 goto reset;
1863 }
1864 else /* ret == 0 */
1865 {
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00001866 polarssl_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001867 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
1868 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001869
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001870 if( ( ret = ssl_get_record_expansion( &ssl ) ) >= 0 )
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00001871 polarssl_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001872 else
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00001873 polarssl_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001874
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001875#if defined(POLARSSL_SSL_ALPN)
1876 if( opt.alpn_string != NULL )
1877 {
1878 const char *alp = ssl_get_alpn_protocol( &ssl );
Rich Evansf90016a2015-01-19 14:26:37 +00001879 polarssl_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001880 alp ? alp : "(none)" );
1881 }
1882#endif
1883
Paul Bakker36713e82013-09-17 13:25:29 +02001884#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001885 /*
1886 * 5. Verify the server certificate
1887 */
Rich Evansf90016a2015-01-19 14:26:37 +00001888 polarssl_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001889
1890 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1891 {
Rich Evansf90016a2015-01-19 14:26:37 +00001892 polarssl_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001893
Paul Bakkerb0550d92012-10-30 07:51:03 +00001894 if( !ssl_get_peer_cert( &ssl ) )
Rich Evansf90016a2015-01-19 14:26:37 +00001895 polarssl_printf( " ! no client certificate sent\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001896
1897 if( ( ret & BADCERT_EXPIRED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001898 polarssl_printf( " ! client certificate has expired\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001899
1900 if( ( ret & BADCERT_REVOKED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001901 polarssl_printf( " ! client certificate has been revoked\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001902
1903 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001904 polarssl_printf( " ! self-signed or not signed by a trusted CA\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001905
Rich Evansf90016a2015-01-19 14:26:37 +00001906 polarssl_printf( "\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001907 }
1908 else
Rich Evansf90016a2015-01-19 14:26:37 +00001909 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001910
Paul Bakkerb0550d92012-10-30 07:51:03 +00001911 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001912 {
Rich Evansf90016a2015-01-19 14:26:37 +00001913 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001914 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1915 ssl_get_peer_cert( &ssl ) );
Rich Evansf90016a2015-01-19 14:26:37 +00001916 polarssl_printf( "%s\n", buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001917 }
Paul Bakker36713e82013-09-17 13:25:29 +02001918#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001919
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001920 if( opt.exchanges == 0 )
1921 goto close_notify;
1922
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001923 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001924data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001925 /*
1926 * 6. Read the HTTP Request
1927 */
Rich Evansf90016a2015-01-19 14:26:37 +00001928 polarssl_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001929 fflush( stdout );
1930
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001931 /*
1932 * TLS and DTLS need different reading styles (stream vs datagram)
1933 */
1934 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001935 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001936 do
1937 {
1938 int terminated = 0;
1939 len = sizeof( buf ) - 1;
1940 memset( buf, 0, sizeof( buf ) );
1941 ret = ssl_read( &ssl, buf, len );
1942
1943 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1944 ret == POLARSSL_ERR_NET_WANT_WRITE )
1945 continue;
1946
1947 if( ret <= 0 )
1948 {
1949 switch( ret )
1950 {
1951 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00001952 polarssl_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001953 goto close_notify;
1954
1955 case 0:
1956 case POLARSSL_ERR_NET_CONN_RESET:
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00001957 polarssl_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001958 ret = POLARSSL_ERR_NET_CONN_RESET;
1959 goto reset;
1960
1961 default:
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00001962 polarssl_printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001963 goto reset;
1964 }
1965 }
1966
1967 if( ssl_get_bytes_avail( &ssl ) == 0 )
1968 {
1969 len = ret;
1970 buf[len] = '\0';
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +00001971 polarssl_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001972
1973 /* End of message should be detected according to the syntax of the
1974 * application protocol (eg HTTP), just use a dummy test here. */
1975 if( buf[len - 1] == '\n' )
1976 terminated = 1;
1977 }
1978 else
1979 {
1980 int extra_len, ori_len;
1981 unsigned char *larger_buf;
1982
1983 ori_len = ret;
1984 extra_len = ssl_get_bytes_avail( &ssl );
1985
1986 larger_buf = polarssl_malloc( ori_len + extra_len + 1 );
1987 if( larger_buf == NULL )
1988 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +00001989 polarssl_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001990 ret = 1;
1991 goto reset;
1992 }
1993
1994 memset( larger_buf, 0, ori_len + extra_len );
1995 memcpy( larger_buf, buf, ori_len );
1996
1997 /* This read should never fail and get the whole cached data */
1998 ret = ssl_read( &ssl, larger_buf + ori_len, extra_len );
1999 if( ret != extra_len ||
2000 ssl_get_bytes_avail( &ssl ) != 0 )
2001 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +00002002 polarssl_printf( " ! ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002003 ret = 1;
2004 goto reset;
2005 }
2006
2007 larger_buf[ori_len + extra_len] = '\0';
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +00002008 polarssl_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002009 ori_len + extra_len, ori_len, extra_len,
2010 (char *) larger_buf );
2011
2012 /* End of message should be detected according to the syntax of the
2013 * application protocol (eg HTTP), just use a dummy test here. */
2014 if( larger_buf[ori_len + extra_len - 1] == '\n' )
2015 terminated = 1;
2016
2017 polarssl_free( larger_buf );
2018 }
2019
2020 if( terminated )
2021 {
2022 ret = 0;
2023 break;
2024 }
2025 }
2026 while( 1 );
2027 }
2028 else /* Not stream, so datagram */
2029 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002030 len = sizeof( buf ) - 1;
2031 memset( buf, 0, sizeof( buf ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002032
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002033 do ret = ssl_read( &ssl, buf, len );
2034 while( ret == POLARSSL_ERR_NET_WANT_READ ||
2035 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002036
2037 if( ret <= 0 )
2038 {
2039 switch( ret )
2040 {
2041 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +00002042 polarssl_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002043 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002044 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002045
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002046 default:
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +00002047 polarssl_printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002048 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002049 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002050 }
2051
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002052 len = ret;
2053 buf[len] = '\0';
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +00002054 polarssl_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002055 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002056 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002057
2058 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002059 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02002060 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002061 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002062#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00002063 if( opt.renegotiate && exchanges_left == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002064 {
Rich Evansf90016a2015-01-19 14:26:37 +00002065 polarssl_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002066 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002067
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002068 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
2069 {
2070 if( ret != POLARSSL_ERR_NET_WANT_READ &&
2071 ret != POLARSSL_ERR_NET_WANT_WRITE )
2072 {
Rich Evansf90016a2015-01-19 14:26:37 +00002073 polarssl_printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002074 goto reset;
2075 }
2076 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002077
Rich Evansf90016a2015-01-19 14:26:37 +00002078 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002079 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002080#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002081
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002082 /*
2083 * 7. Write the 200 Response
2084 */
Rich Evansf90016a2015-01-19 14:26:37 +00002085 polarssl_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002086 fflush( stdout );
2087
2088 len = sprintf( (char *) buf, HTTP_RESPONSE,
2089 ssl_get_ciphersuite( &ssl ) );
2090
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002091 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002092 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002093 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002094 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002095 while( ( ret = ssl_write( &ssl, buf + written, len - written ) )
2096 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002097 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002098 if( ret == POLARSSL_ERR_NET_CONN_RESET )
2099 {
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00002100 polarssl_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002101 goto reset;
2102 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002103
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002104 if( ret != POLARSSL_ERR_NET_WANT_READ &&
2105 ret != POLARSSL_ERR_NET_WANT_WRITE )
2106 {
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00002107 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002108 goto reset;
2109 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002110 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002111 }
2112 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002113 else /* Not stream, so datagram */
2114 {
2115 do ret = ssl_write( &ssl, buf, len );
2116 while( ret == POLARSSL_ERR_NET_WANT_READ ||
2117 ret == POLARSSL_ERR_NET_WANT_WRITE );
2118
2119 if( ret < 0 )
2120 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +00002121 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002122 goto reset;
2123 }
2124
2125 frags = 1;
2126 written = ret;
2127 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002128
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002129 buf[written] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +00002130 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 +01002131 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002132
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002133 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002134 * 7b. Continue doing data exchanges?
2135 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00002136 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002137 goto data_exchange;
2138
2139 /*
2140 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002141 */
2142close_notify:
Rich Evansf90016a2015-01-19 14:26:37 +00002143 polarssl_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002144
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002145 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard34377b12015-01-22 10:46:46 +00002146 do ret = ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002147 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
2148 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002149
Rich Evansf90016a2015-01-19 14:26:37 +00002150 polarssl_printf( " done\n" );
2151
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002152 goto reset;
2153
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002154 /*
2155 * Cleanup and exit
2156 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002157exit:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002158#ifdef POLARSSL_ERROR_C
2159 if( ret != 0 )
2160 {
2161 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02002162 polarssl_strerror( ret, error_buf, 100 );
Rich Evansf90016a2015-01-19 14:26:37 +00002163 polarssl_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002164 }
2165#endif
2166
Manuel Pégourié-Gonnard7e81e702015-01-29 11:47:41 +00002167 polarssl_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01002168 fflush( stdout );
2169
Paul Bakker0c226102014-04-17 16:02:36 +02002170 if( client_fd != -1 )
2171 net_close( client_fd );
2172
Paul Bakkera317a982014-06-18 16:44:11 +02002173#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
2174 dhm_free( &dhm );
2175#endif
Paul Bakker36713e82013-09-17 13:25:29 +02002176#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker36713e82013-09-17 13:25:29 +02002177 x509_crt_free( &cacert );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002178 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02002179 pk_free( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002180 x509_crt_free( &srvcert2 );
2181 pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02002182#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002183#if defined(POLARSSL_SNI)
2184 sni_free( sni_info );
2185#endif
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02002186#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
2187 psk_free( psk_info );
2188#endif
2189#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
2190 dhm_free( &dhm );
2191#endif
Paul Bakkered27a042013-04-18 22:46:23 +02002192
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002193 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02002194 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02002195 entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002196
Paul Bakker0a597072012-09-25 21:55:46 +00002197#if defined(POLARSSL_SSL_CACHE_C)
2198 ssl_cache_free( &cache );
2199#endif
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +02002200#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002201 ssl_cookie_free( &cookie_ctx );
2202#endif
Paul Bakker0a597072012-09-25 21:55:46 +00002203
Paul Bakker1337aff2013-09-29 14:45:34 +02002204#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
2205#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker82024bf2013-07-04 11:52:32 +02002206 memory_buffer_alloc_status();
2207#endif
Paul Bakker1337aff2013-09-29 14:45:34 +02002208 memory_buffer_alloc_free();
2209#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02002210
Manuel Pégourié-Gonnard7e81e702015-01-29 11:47:41 +00002211 polarssl_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01002212
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002213#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +00002214 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002215 fflush( stdout ); getchar();
2216#endif
2217
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002218 // Shell can not handle large exit numbers -> 1 for errors
2219 if( ret < 0 )
2220 ret = 1;
2221
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002222 return( ret );
2223}
2224#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
2225 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
2226 POLARSSL_CTR_DRBG_C */