blob: d1b76ac4637fd6408088b05f35951404c86016b1 [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00007 *
Paul Bakkerb60b95f2012-09-25 09:05:17 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020032#include <stdio.h>
Rich Evans18b78c72015-02-11 14:06:19 +000033#define polarssl_free free
34#define polarssl_malloc malloc
35#define polarssl_fprintf fprintf
36#define polarssl_printf printf
37#endif
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020038
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010039#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) && defined(POLARSSL_FS_IO)
40#define POLARSSL_SNI
41#endif
42
Paul Bakkerb60b95f2012-09-25 09:05:17 +000043#if defined(_WIN32)
44#include <windows.h>
45#endif
46
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +000047#if defined(POLARSSL_ENTROPY_C) && \
48 defined(POLARSSL_SSL_TLS_C) && defined(POLARSSL_SSL_SRV_C) && \
Rich Evans18b78c72015-02-11 14:06:19 +000049 defined(POLARSSL_NET_C) && defined(POLARSSL_CTR_DRBG_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +000050#include "polarssl/net.h"
51#include "polarssl/ssl.h"
52#include "polarssl/entropy.h"
53#include "polarssl/ctr_drbg.h"
54#include "polarssl/certs.h"
55#include "polarssl/x509.h"
56#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020057#include "polarssl/debug.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000058
Rich Evans18b78c72015-02-11 14:06:19 +000059#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#endif
63
64#if !defined(_WIN32)
65#include <signal.h>
66#endif
67
Paul Bakker0a597072012-09-25 21:55:46 +000068#if defined(POLARSSL_SSL_CACHE_C)
69#include "polarssl/ssl_cache.h"
70#endif
71
Paul Bakker82024bf2013-07-04 11:52:32 +020072#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardd43ccb62015-01-23 17:38:09 +000073#include "polarssl/memory_buffer_alloc.h"
Paul Bakker82024bf2013-07-04 11:52:32 +020074#endif
75
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +010076#define DFL_SERVER_ADDR NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000077#define DFL_SERVER_PORT 4433
Paul Bakkerb60b95f2012-09-25 09:05:17 +000078#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010079#define DFL_NBIO 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +000080#define DFL_CA_FILE ""
81#define DFL_CA_PATH ""
82#define DFL_CRT_FILE ""
83#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +020084#define DFL_CRT_FILE2 ""
85#define DFL_KEY_FILE2 ""
Paul Bakkerfbb17802013-04-17 19:10:21 +020086#define DFL_PSK ""
87#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +020088#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000089#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +020090#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +020091#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010092#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010093#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +020094#define DFL_RENEGO_DELAY -2
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +010095#define DFL_RENEGO_PERIOD -1
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +020096#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +010097#define DFL_MIN_VERSION SSL_MINOR_VERSION_1
Paul Bakkerc1516be2013-06-29 16:01:32 +020098#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +010099#define DFL_ARC4 SSL_ARC4_DISABLED
Paul Bakker91ebfb52012-11-23 14:04:08 +0100100#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200101#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100102#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200103#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100104#define DFL_TICKET_TIMEOUT -1
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100105#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100106#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100107#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200108#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200109#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200110#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100111#define DFL_ETM -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000112
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200113#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
114 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
115 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
116 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
117 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
118 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
119 "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 +0200120
Paul Bakker8e714d72013-07-18 11:05:13 +0200121/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
122 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000123#define HTTP_RESPONSE \
124 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000125 "<h2>mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200126 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000127
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200128/*
129 * Size of the basic I/O buffer. Able to hold our default response.
130 *
131 * You will need to adapt the ssl_get_bytes_avail() test in ssl-opt.sh
132 * if you change this value to something outside the range <= 100 or > 500
133 */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200134#define IO_BUF_LEN 200
135
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200136#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000137#if defined(POLARSSL_FS_IO)
138#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100139 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
140 " default: \"\" (pre-loaded)\n" \
141 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
142 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
143 " 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 +0200144 " default: see note after key_file2\n" \
145 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200146 " 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 +0200147 " default: see note after key_file2\n" \
148 " key_file2=%%s default: see note below\n" \
149 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200150 " preloaded certificate(s) and key(s) are used if available\n" \
151 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
152 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000153#else
154#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200155 "\n" \
156 " No file operations available (POLARSSL_FS_IO not defined)\n" \
157 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000158#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200159#else
160#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200161#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200162
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200163#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200164#define USAGE_PSK \
165 " psk=%%s default: \"\" (in hex, without 0x)\n" \
166 " psk_identity=%%s default: \"Client_identity\"\n"
167#else
168#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200169#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000170
Paul Bakkera503a632013-08-14 13:48:06 +0200171#if defined(POLARSSL_SSL_SESSION_TICKETS)
172#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100173 " tickets=%%d default: 1 (enabled)\n" \
174 " ticket_timeout=%%d default: ticket default (1d)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200175#else
176#define USAGE_TICKETS ""
177#endif /* POLARSSL_SSL_SESSION_TICKETS */
178
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100179#if defined(POLARSSL_SSL_CACHE_C)
180#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100181 " cache_max=%%d default: cache default (50)\n" \
182 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100183#else
184#define USAGE_CACHE ""
185#endif /* POLARSSL_SSL_CACHE_C */
186
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100187#if defined(POLARSSL_SNI)
188#define USAGE_SNI \
189 " sni=%%s name1,cert1,key1[,name2,cert2,key2[,...]]\n" \
190 " default: disabled\n"
191#else
192#define USAGE_SNI ""
193#endif /* POLARSSL_SNI */
194
Paul Bakker05decb22013-08-15 13:33:48 +0200195#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
196#define USAGE_MAX_FRAG_LEN \
197 " max_frag_len=%%d default: 16384 (tls default)\n" \
198 " options: 512, 1024, 2048, 4096\n"
199#else
200#define USAGE_MAX_FRAG_LEN ""
201#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
202
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100203#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
204#define USAGE_TRUNC_HMAC \
205 " trunc_hmac=%%d default: library default\n"
206#else
207#define USAGE_TRUNC_HMAC ""
208#endif
209
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200210#if defined(POLARSSL_SSL_ALPN)
211#define USAGE_ALPN \
212 " alpn=%%s default: \"\" (disabled)\n" \
213 " example: spdy/1,http/1.1\n"
214#else
215#define USAGE_ALPN ""
216#endif /* POLARSSL_SSL_ALPN */
217
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200218#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
219#define USAGE_EMS \
220 " extended_ms=0/1 default: (library default: on)\n"
221#else
222#define USAGE_EMS ""
223#endif
224
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100225#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
226#define USAGE_ETM \
227 " etm=0/1 default: (library default: on)\n"
228#else
229#define USAGE_ETM ""
230#endif
231
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100232#if defined(POLARSSL_SSL_RENEGOTIATION)
233#define USAGE_RENEGO \
234 " renegotiation=%%d default: 0 (disabled)\n" \
235 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100236 " renego_delay=%%d default: -2 (library default)\n" \
237 " renego_period=%%d default: (library default)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100238#else
239#define USAGE_RENEGO ""
240#endif
241
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000242#define USAGE \
243 "\n usage: ssl_server2 param=<>...\n" \
244 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100245 " server_addr=%%d default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000246 " server_port=%%d default: 4433\n" \
247 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100248 " nbio=%%d default: 0 (blocking I/O)\n" \
249 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100250 "\n" \
251 " auth_mode=%%s default: \"optional\"\n" \
252 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000253 USAGE_IO \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100254 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100255 "\n" \
256 USAGE_PSK \
257 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100258 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100259 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200260 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100261 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100262 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100263 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100264 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200265 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200266 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100267 USAGE_ETM \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100268 "\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000269 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200270 " max_version=%%s default: \"tls1_2\"\n" \
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100271 " arc4=%%d default: 0 (disabled)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200272 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200273 " options: ssl3, tls1, tls1_1, tls1_2\n" \
274 "\n" \
275 " version_suites=a,b,c,d per-version ciphersuites\n" \
276 " in order from ssl3 to tls1_2\n" \
277 " default: all enabled\n" \
278 " force_ciphersuite=<name> default: all enabled\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000279 " acceptable ciphersuite names:\n"
280
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +0000281#if !defined(POLARSSL_ENTROPY_C) || \
Rich Evans85b05ec2015-02-12 11:37:29 +0000282 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
283 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
284#include <stdio.h>
285int main( void )
286{
287 polarssl_printf("POLARSSL_ENTROPY_C and/or "
288 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
289 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
290 return( 0 );
291}
292#else
293/*
294 * global options
295 */
296struct options
297{
298 const char *server_addr; /* address on which the ssl service runs */
299 int server_port; /* port on which the ssl service runs */
300 int debug_level; /* level of debugging */
301 int nbio; /* should I/O be blocking? */
302 const char *ca_file; /* the file with the CA certificate(s) */
303 const char *ca_path; /* the path with the CA certificate(s) reside */
304 const char *crt_file; /* the file with the server certificate */
305 const char *key_file; /* the file with the server key */
306 const char *crt_file2; /* the file with the 2nd server certificate */
307 const char *key_file2; /* the file with the 2nd server key */
308 const char *psk; /* the pre-shared key */
309 const char *psk_identity; /* the pre-shared key identity */
310 char *psk_list; /* list of PSK id/key pairs for callback */
311 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
312 const char *version_suites; /* per-version ciphersuites */
313 int renegotiation; /* enable / disable renegotiation */
314 int allow_legacy; /* allow legacy renegotiation */
315 int renegotiate; /* attempt renegotiation? */
316 int renego_delay; /* delay before enforcing renegotiation */
317 int renego_period; /* period for automatic renegotiation */
318 int exchanges; /* number of data exchanges */
319 int min_version; /* minimum protocol version accepted */
320 int max_version; /* maximum protocol version accepted */
321 int arc4; /* flag for arc4 suites support */
322 int auth_mode; /* verify mode for connection */
323 unsigned char mfl_code; /* code for maximum fragment length */
324 int trunc_hmac; /* accept truncated hmac? */
325 int tickets; /* enable / disable session tickets */
326 int ticket_timeout; /* session ticket lifetime */
327 int cache_max; /* max number of session cache entries */
328 int cache_timeout; /* expiration delay of session cache entries */
329 char *sni; /* string describing sni information */
330 const char *alpn_string; /* ALPN supported protocols */
331 const char *dhm_file; /* the file with the DH parameters */
332 int extended_ms; /* allow negotiation of extended MS? */
333 int etm; /* allow negotiation of encrypt-then-MAC? */
334} opt;
335
336static void my_debug( void *ctx, int level, const char *str )
337{
338 ((void) level);
339
340 polarssl_fprintf( (FILE *) ctx, "%s", str );
341 fflush( (FILE *) ctx );
342}
343
344/*
345 * Test recv/send functions that make sure each try returns
346 * WANT_READ/WANT_WRITE at least once before sucesseding
347 */
348static int my_recv( void *ctx, unsigned char *buf, size_t len )
349{
350 static int first_try = 1;
351 int ret;
352
353 if( first_try )
354 {
355 first_try = 0;
356 return( POLARSSL_ERR_NET_WANT_READ );
357 }
358
359 ret = net_recv( ctx, buf, len );
360 if( ret != POLARSSL_ERR_NET_WANT_READ )
361 first_try = 1; /* Next call will be a new operation */
362 return( ret );
363}
364
365static int my_send( void *ctx, const unsigned char *buf, size_t len )
366{
367 static int first_try = 1;
368 int ret;
369
370 if( first_try )
371 {
372 first_try = 0;
373 return( POLARSSL_ERR_NET_WANT_WRITE );
374 }
375
376 ret = net_send( ctx, buf, len );
377 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
378 first_try = 1; /* Next call will be a new operation */
379 return( ret );
380}
381
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200382/*
383 * Used by sni_parse and psk_parse to handle coma-separated lists
384 */
385#define GET_ITEM( dst ) \
386 dst = p; \
387 while( *p != ',' ) \
388 if( ++p > end ) \
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000389 goto error; \
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200390 *p++ = '\0';
391
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100392#if defined(POLARSSL_SNI)
393typedef struct _sni_entry sni_entry;
394
395struct _sni_entry {
396 const char *name;
397 x509_crt *cert;
398 pk_context *key;
399 sni_entry *next;
400};
401
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100402void sni_free( sni_entry *head )
403{
404 sni_entry *cur = head, *next;
405
406 while( cur != NULL )
407 {
408 x509_crt_free( cur->cert );
409 polarssl_free( cur->cert );
410
411 pk_free( cur->key );
412 polarssl_free( cur->key );
413
414 next = cur->next;
415 polarssl_free( cur );
416 cur = next;
417 }
418}
419
420/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000421 * Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]]
422 * into a usable sni_entry list.
423 *
424 * Modifies the input string! This is not production quality!
425 */
426sni_entry *sni_parse( char *sni_string )
427{
428 sni_entry *cur = NULL, *new = NULL;
429 char *p = sni_string;
430 char *end = p;
431 char *crt_file, *key_file;
432
433 while( *end != '\0' )
434 ++end;
435 *end = ',';
436
437 while( p <= end )
438 {
439 if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL )
440 {
441 sni_free( cur );
442 return( NULL );
443 }
444
445 memset( new, 0, sizeof( sni_entry ) );
446
447 if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ||
448 ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
449 {
450 polarssl_free( new->cert );
451 polarssl_free( new );
452 sni_free( cur );
453 return( NULL );
454 }
455
456 x509_crt_init( new->cert );
457 pk_init( new->key );
458
459 GET_ITEM( new->name );
460 GET_ITEM( crt_file );
461 GET_ITEM( key_file );
462
463 if( x509_crt_parse_file( new->cert, crt_file ) != 0 ||
464 pk_parse_keyfile( new->key, key_file, "" ) != 0 )
465 {
466 goto error;
467 }
468
469 new->next = cur;
470 cur = new;
471 }
472
473 return( cur );
474
475error:
476 sni_free( new );
477 sni_free( cur );
478 return( NULL );
479}
480
481/*
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100482 * SNI callback.
483 */
484int sni_callback( void *p_info, ssl_context *ssl,
485 const unsigned char *name, size_t name_len )
486{
487 sni_entry *cur = (sni_entry *) p_info;
488
489 while( cur != NULL )
490 {
491 if( name_len == strlen( cur->name ) &&
492 memcmp( name, cur->name, name_len ) == 0 )
493 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200494 return( ssl_set_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100495 }
496
497 cur = cur->next;
498 }
499
500 return( -1 );
501}
502
503#endif /* POLARSSL_SNI */
504
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200505#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
506
507#define HEX2NUM( c ) \
508 if( c >= '0' && c <= '9' ) \
509 c -= '0'; \
510 else if( c >= 'a' && c <= 'f' ) \
511 c -= 'a' - 10; \
512 else if( c >= 'A' && c <= 'F' ) \
513 c -= 'A' - 10; \
514 else \
515 return( -1 );
516
517/*
518 * Convert a hex string to bytes.
519 * Return 0 on success, -1 on error.
520 */
521int unhexify( unsigned char *output, const char *input, size_t *olen )
522{
523 unsigned char c;
524 size_t j;
525
526 *olen = strlen( input );
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200527 if( *olen % 2 != 0 || *olen / 2 > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200528 return( -1 );
529 *olen /= 2;
530
531 for( j = 0; j < *olen * 2; j += 2 )
532 {
533 c = input[j];
534 HEX2NUM( c );
535 output[ j / 2 ] = c << 4;
536
537 c = input[j + 1];
538 HEX2NUM( c );
539 output[ j / 2 ] |= c;
540 }
541
542 return( 0 );
543}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200544
545typedef struct _psk_entry psk_entry;
546
547struct _psk_entry
548{
549 const char *name;
550 size_t key_len;
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200551 unsigned char key[POLARSSL_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200552 psk_entry *next;
553};
554
555/*
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000556 * Free a list of psk_entry's
557 */
558void psk_free( psk_entry *head )
559{
560 psk_entry *next;
561
562 while( head != NULL )
563 {
564 next = head->next;
565 polarssl_free( head );
566 head = next;
567 }
568}
569
570/*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200571 * Parse a string of pairs name1,key1[,name2,key2[,...]]
572 * into a usable psk_entry list.
573 *
574 * Modifies the input string! This is not production quality!
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200575 */
576psk_entry *psk_parse( char *psk_string )
577{
578 psk_entry *cur = NULL, *new = NULL;
579 char *p = psk_string;
580 char *end = p;
581 char *key_hex;
582
583 while( *end != '\0' )
584 ++end;
585 *end = ',';
586
587 while( p <= end )
588 {
589 if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL )
590 return( NULL );
591
592 memset( new, 0, sizeof( psk_entry ) );
593
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200594 GET_ITEM( new->name );
595 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200596
597 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000598 goto error;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200599
600 new->next = cur;
601 cur = new;
602 }
603
604 return( cur );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200605
Manuel Pégourié-Gonnard5c078e12015-02-14 13:56:39 +0000606error:
607 psk_free( new );
608 psk_free( cur );
609 return( 0 );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200610}
611
612/*
613 * PSK callback
614 */
615int psk_callback( void *p_info, ssl_context *ssl,
616 const unsigned char *name, size_t name_len )
617{
618 psk_entry *cur = (psk_entry *) p_info;
619
620 while( cur != NULL )
621 {
622 if( name_len == strlen( cur->name ) &&
623 memcmp( name, cur->name, name_len ) == 0 )
624 {
625 return( ssl_set_psk( ssl, cur->key, cur->key_len,
626 name, name_len ) );
627 }
628
629 cur = cur->next;
630 }
631
632 return( -1 );
633}
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200634#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
635
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +0200636static int listen_fd, client_fd = -1;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200637
638/* Interruption handler to ensure clean exit (for valgrind testing) */
639#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200640static int received_sigterm = 0;
641void term_handler( int sig )
642{
643 ((void) sig);
644 received_sigterm = 1;
645 net_close( listen_fd ); /* causes net_accept() to abort */
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +0200646 net_close( client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200647}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200648#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200649
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000650int main( int argc, char *argv[] )
651{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +0000652 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200653 int version_suites[4][2];
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200654 unsigned char buf[IO_BUF_LEN];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200655#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200656 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +0200657 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +0200658 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +0200659#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200660 const char *pers = "ssl_server2";
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000661
662 entropy_context entropy;
663 ctr_drbg_context ctr_drbg;
664 ssl_context ssl;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100665#if defined(POLARSSL_SSL_RENEGOTIATION)
666 unsigned char renego_period[8] = { 0 };
667#endif
Paul Bakker36713e82013-09-17 13:25:29 +0200668#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200669 x509_crt cacert;
670 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200671 pk_context pkey;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200672 x509_crt srvcert2;
673 pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200674 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200675#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200676#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
677 dhm_context dhm;
678#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000679#if defined(POLARSSL_SSL_CACHE_C)
680 ssl_cache_context cache;
681#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100682#if defined(POLARSSL_SNI)
683 sni_entry *sni_info = NULL;
684#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200685#if defined(POLARSSL_SSL_ALPN)
686 const char *alpn_list[10];
687#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200688#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
689 unsigned char alloc_buf[100000];
690#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000691
692 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000693 char *p, *q;
694 const int *list;
695
Paul Bakker82024bf2013-07-04 11:52:32 +0200696#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
697 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
698#endif
699
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000700 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200701 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000702 */
703 listen_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200704 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200705#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200706 x509_crt_init( &cacert );
707 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200708 pk_init( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200709 x509_crt_init( &srvcert2 );
710 pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200711#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200712#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
Paul Bakkera317a982014-06-18 16:44:11 +0200713 dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200714#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000715#if defined(POLARSSL_SSL_CACHE_C)
716 ssl_cache_init( &cache );
717#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200718#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200719 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200720#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000721
Paul Bakkerc1283d32014-08-18 11:05:51 +0200722#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100723 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200724 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100725 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +0200726#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200727
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000728 if( argc == 0 )
729 {
730 usage:
731 if( ret == 0 )
732 ret = 1;
733
Rich Evansf90016a2015-01-19 14:26:37 +0000734 polarssl_printf( USAGE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000735
736 list = ssl_list_ciphersuites();
737 while( *list )
738 {
Rich Evansf90016a2015-01-19 14:26:37 +0000739 polarssl_printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200740 list++;
741 if( !*list )
742 break;
Rich Evansf90016a2015-01-19 14:26:37 +0000743 polarssl_printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000744 list++;
745 }
Rich Evansf90016a2015-01-19 14:26:37 +0000746 polarssl_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000747 goto exit;
748 }
749
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100750 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000751 opt.server_port = DFL_SERVER_PORT;
752 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100753 opt.nbio = DFL_NBIO;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000754 opt.ca_file = DFL_CA_FILE;
755 opt.ca_path = DFL_CA_PATH;
756 opt.crt_file = DFL_CRT_FILE;
757 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200758 opt.crt_file2 = DFL_CRT_FILE2;
759 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200760 opt.psk = DFL_PSK;
761 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200762 opt.psk_list = DFL_PSK_LIST;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000763 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200764 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000765 opt.renegotiation = DFL_RENEGOTIATION;
766 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100767 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200768 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100769 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200770 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000771 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200772 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100773 opt.arc4 = DFL_ARC4;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100774 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200775 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100776 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200777 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100778 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100779 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100780 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100781 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200782 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200783 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200784 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100785 opt.etm = DFL_ETM;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000786
787 for( i = 1; i < argc; i++ )
788 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000789 p = argv[i];
790 if( ( q = strchr( p, '=' ) ) == NULL )
791 goto usage;
792 *q++ = '\0';
793
794 if( strcmp( p, "server_port" ) == 0 )
795 {
796 opt.server_port = atoi( q );
797 if( opt.server_port < 1 || opt.server_port > 65535 )
798 goto usage;
799 }
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100800 else if( strcmp( p, "server_addr" ) == 0 )
801 opt.server_addr = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000802 else if( strcmp( p, "debug_level" ) == 0 )
803 {
804 opt.debug_level = atoi( q );
805 if( opt.debug_level < 0 || opt.debug_level > 65535 )
806 goto usage;
807 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100808 else if( strcmp( p, "nbio" ) == 0 )
809 {
810 opt.nbio = atoi( q );
811 if( opt.nbio < 0 || opt.nbio > 2 )
812 goto usage;
813 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000814 else if( strcmp( p, "ca_file" ) == 0 )
815 opt.ca_file = q;
816 else if( strcmp( p, "ca_path" ) == 0 )
817 opt.ca_path = q;
818 else if( strcmp( p, "crt_file" ) == 0 )
819 opt.crt_file = q;
820 else if( strcmp( p, "key_file" ) == 0 )
821 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200822 else if( strcmp( p, "crt_file2" ) == 0 )
823 opt.crt_file2 = q;
824 else if( strcmp( p, "key_file2" ) == 0 )
825 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200826 else if( strcmp( p, "dhm_file" ) == 0 )
827 opt.dhm_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200828 else if( strcmp( p, "psk" ) == 0 )
829 opt.psk = q;
830 else if( strcmp( p, "psk_identity" ) == 0 )
831 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200832 else if( strcmp( p, "psk_list" ) == 0 )
833 opt.psk_list = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000834 else if( strcmp( p, "force_ciphersuite" ) == 0 )
835 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000836 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
837
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200838 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000839 {
840 ret = 2;
841 goto usage;
842 }
843 opt.force_ciphersuite[1] = 0;
844 }
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200845 else if( strcmp( p, "version_suites" ) == 0 )
846 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000847 else if( strcmp( p, "renegotiation" ) == 0 )
848 {
849 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
850 SSL_RENEGOTIATION_DISABLED;
851 }
852 else if( strcmp( p, "allow_legacy" ) == 0 )
853 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100854 switch( atoi( q ) )
855 {
856 case -1: opt.allow_legacy = SSL_LEGACY_BREAK_HANDSHAKE; break;
857 case 0: opt.allow_legacy = SSL_LEGACY_NO_RENEGOTIATION; break;
858 case 1: opt.allow_legacy = SSL_LEGACY_ALLOW_RENEGOTIATION; break;
859 default: goto usage;
860 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000861 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100862 else if( strcmp( p, "renegotiate" ) == 0 )
863 {
864 opt.renegotiate = atoi( q );
865 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
866 goto usage;
867 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200868 else if( strcmp( p, "renego_delay" ) == 0 )
869 {
870 opt.renego_delay = atoi( q );
871 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100872 else if( strcmp( p, "renego_period" ) == 0 )
873 {
874 opt.renego_period = atoi( q );
875 if( opt.renego_period < 2 || opt.renego_period > 255 )
876 goto usage;
877 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200878 else if( strcmp( p, "exchanges" ) == 0 )
879 {
880 opt.exchanges = atoi( q );
881 if( opt.exchanges < 1 )
882 goto usage;
883 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000884 else if( strcmp( p, "min_version" ) == 0 )
885 {
886 if( strcmp( q, "ssl3" ) == 0 )
887 opt.min_version = SSL_MINOR_VERSION_0;
888 else if( strcmp( q, "tls1" ) == 0 )
889 opt.min_version = SSL_MINOR_VERSION_1;
890 else if( strcmp( q, "tls1_1" ) == 0 )
891 opt.min_version = SSL_MINOR_VERSION_2;
892 else if( strcmp( q, "tls1_2" ) == 0 )
893 opt.min_version = SSL_MINOR_VERSION_3;
894 else
895 goto usage;
896 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200897 else if( strcmp( p, "max_version" ) == 0 )
898 {
899 if( strcmp( q, "ssl3" ) == 0 )
900 opt.max_version = SSL_MINOR_VERSION_0;
901 else if( strcmp( q, "tls1" ) == 0 )
902 opt.max_version = SSL_MINOR_VERSION_1;
903 else if( strcmp( q, "tls1_1" ) == 0 )
904 opt.max_version = SSL_MINOR_VERSION_2;
905 else if( strcmp( q, "tls1_2" ) == 0 )
906 opt.max_version = SSL_MINOR_VERSION_3;
907 else
908 goto usage;
909 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100910 else if( strcmp( p, "arc4" ) == 0 )
911 {
912 switch( atoi( q ) )
913 {
914 case 0: opt.arc4 = SSL_ARC4_DISABLED; break;
915 case 1: opt.arc4 = SSL_ARC4_ENABLED; break;
916 default: goto usage;
917 }
918 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200919 else if( strcmp( p, "force_version" ) == 0 )
920 {
921 if( strcmp( q, "ssl3" ) == 0 )
922 {
923 opt.min_version = SSL_MINOR_VERSION_0;
924 opt.max_version = SSL_MINOR_VERSION_0;
925 }
926 else if( strcmp( q, "tls1" ) == 0 )
927 {
928 opt.min_version = SSL_MINOR_VERSION_1;
929 opt.max_version = SSL_MINOR_VERSION_1;
930 }
931 else if( strcmp( q, "tls1_1" ) == 0 )
932 {
933 opt.min_version = SSL_MINOR_VERSION_2;
934 opt.max_version = SSL_MINOR_VERSION_2;
935 }
936 else if( strcmp( q, "tls1_2" ) == 0 )
937 {
938 opt.min_version = SSL_MINOR_VERSION_3;
939 opt.max_version = SSL_MINOR_VERSION_3;
940 }
941 else
942 goto usage;
943 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100944 else if( strcmp( p, "auth_mode" ) == 0 )
945 {
946 if( strcmp( q, "none" ) == 0 )
947 opt.auth_mode = SSL_VERIFY_NONE;
948 else if( strcmp( q, "optional" ) == 0 )
949 opt.auth_mode = SSL_VERIFY_OPTIONAL;
950 else if( strcmp( q, "required" ) == 0 )
951 opt.auth_mode = SSL_VERIFY_REQUIRED;
952 else
953 goto usage;
954 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200955 else if( strcmp( p, "max_frag_len" ) == 0 )
956 {
957 if( strcmp( q, "512" ) == 0 )
958 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
959 else if( strcmp( q, "1024" ) == 0 )
960 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
961 else if( strcmp( q, "2048" ) == 0 )
962 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
963 else if( strcmp( q, "4096" ) == 0 )
964 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
965 else
966 goto usage;
967 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200968 else if( strcmp( p, "alpn" ) == 0 )
969 {
970 opt.alpn_string = q;
971 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100972 else if( strcmp( p, "trunc_hmac" ) == 0 )
973 {
974 switch( atoi( q ) )
975 {
976 case 0: opt.trunc_hmac = SSL_TRUNC_HMAC_DISABLED; break;
977 case 1: opt.trunc_hmac = SSL_TRUNC_HMAC_ENABLED; break;
978 default: goto usage;
979 }
980 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200981 else if( strcmp( p, "extended_ms" ) == 0 )
982 {
983 switch( atoi( q ) )
984 {
985 case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break;
986 case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break;
987 default: goto usage;
988 }
989 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100990 else if( strcmp( p, "etm" ) == 0 )
991 {
992 switch( atoi( q ) )
993 {
994 case 0: opt.etm = SSL_ETM_DISABLED; break;
995 case 1: opt.etm = SSL_ETM_ENABLED; break;
996 default: goto usage;
997 }
998 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200999 else if( strcmp( p, "tickets" ) == 0 )
1000 {
1001 opt.tickets = atoi( q );
1002 if( opt.tickets < 0 || opt.tickets > 1 )
1003 goto usage;
1004 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001005 else if( strcmp( p, "ticket_timeout" ) == 0 )
1006 {
1007 opt.ticket_timeout = atoi( q );
1008 if( opt.ticket_timeout < 0 )
1009 goto usage;
1010 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001011 else if( strcmp( p, "cache_max" ) == 0 )
1012 {
1013 opt.cache_max = atoi( q );
1014 if( opt.cache_max < 0 )
1015 goto usage;
1016 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001017 else if( strcmp( p, "cache_timeout" ) == 0 )
1018 {
1019 opt.cache_timeout = atoi( q );
1020 if( opt.cache_timeout < 0 )
1021 goto usage;
1022 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001023 else if( strcmp( p, "sni" ) == 0 )
1024 {
1025 opt.sni = q;
1026 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001027 else
1028 goto usage;
1029 }
1030
Paul Bakkerc73079a2014-04-25 16:34:30 +02001031#if defined(POLARSSL_DEBUG_C)
1032 debug_set_threshold( opt.debug_level );
1033#endif
1034
Paul Bakkerc1516be2013-06-29 16:01:32 +02001035 if( opt.force_ciphersuite[0] > 0 )
1036 {
1037 const ssl_ciphersuite_t *ciphersuite_info;
1038 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1039
Paul Bakker5b55b792013-07-19 13:43:43 +02001040 if( opt.max_version != -1 &&
1041 ciphersuite_info->min_minor_ver > opt.max_version )
1042 {
Rich Evansf90016a2015-01-19 14:26:37 +00001043 polarssl_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakker5b55b792013-07-19 13:43:43 +02001044 ret = 2;
1045 goto usage;
1046 }
1047 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001048 ciphersuite_info->max_minor_ver < opt.min_version )
1049 {
Rich Evansf90016a2015-01-19 14:26:37 +00001050 polarssl_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +02001051 ret = 2;
1052 goto usage;
1053 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001054 if( opt.max_version > ciphersuite_info->max_minor_ver )
1055 opt.max_version = ciphersuite_info->max_minor_ver;
1056 if( opt.min_version < ciphersuite_info->min_minor_ver )
1057 opt.min_version = ciphersuite_info->min_minor_ver;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001058 }
1059
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001060 if( opt.version_suites != NULL )
1061 {
1062 const char *name[4] = { 0 };
1063
1064 /* Parse 4-element coma-separated list */
1065 for( i = 0, p = (char *) opt.version_suites;
1066 i < 4 && *p != '\0';
1067 i++ )
1068 {
1069 name[i] = p;
1070
1071 /* Terminate the current string and move on to next one */
1072 while( *p != ',' && *p != '\0' )
1073 p++;
1074 if( *p == ',' )
1075 *p++ = '\0';
1076 }
1077
1078 if( i != 4 )
1079 {
Rich Evansf90016a2015-01-19 14:26:37 +00001080 polarssl_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001081 ret = 1;
1082 goto exit;
1083 }
1084
1085 memset( version_suites, 0, sizeof( version_suites ) );
1086
1087 /* Get the suites identifiers from their name */
1088 for( i = 0; i < 4; i++ )
1089 {
1090 version_suites[i][0] = ssl_get_ciphersuite_id( name[i] );
1091
1092 if( version_suites[i][0] == 0 )
1093 {
Rich Evansf90016a2015-01-19 14:26:37 +00001094 polarssl_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001095 ret = 2;
1096 goto usage;
1097 }
1098 }
1099 }
1100
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001101#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001102 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001103 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001104 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001105 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001106 {
Rich Evansf90016a2015-01-19 14:26:37 +00001107 polarssl_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001108 goto exit;
1109 }
1110
1111 if( opt.psk_list != NULL )
1112 {
1113 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001114 {
Rich Evansf90016a2015-01-19 14:26:37 +00001115 polarssl_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001116 goto exit;
1117 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001118 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001119#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001120
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001121#if defined(POLARSSL_SSL_ALPN)
1122 if( opt.alpn_string != NULL )
1123 {
1124 p = (char *) opt.alpn_string;
1125 i = 0;
1126
1127 /* Leave room for a final NULL in alpn_list */
1128 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
1129 {
1130 alpn_list[i++] = p;
1131
1132 /* Terminate the current string and move on to next one */
1133 while( *p != ',' && *p != '\0' )
1134 p++;
1135 if( *p == ',' )
1136 *p++ = '\0';
1137 }
1138 }
1139#endif /* POLARSSL_SSL_ALPN */
1140
Paul Bakkerfbb17802013-04-17 19:10:21 +02001141 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001142 * 0. Initialize the RNG and the session data
1143 */
Rich Evansf90016a2015-01-19 14:26:37 +00001144 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001145 fflush( stdout );
1146
1147 entropy_init( &entropy );
1148 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001149 (const unsigned char *) pers,
1150 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001151 {
Rich Evansf90016a2015-01-19 14:26:37 +00001152 polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001153 goto exit;
1154 }
1155
Rich Evansf90016a2015-01-19 14:26:37 +00001156 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001157
Paul Bakker36713e82013-09-17 13:25:29 +02001158#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001159 /*
1160 * 1.1. Load the trusted CA
1161 */
Rich Evansf90016a2015-01-19 14:26:37 +00001162 polarssl_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001163 fflush( stdout );
1164
1165#if defined(POLARSSL_FS_IO)
1166 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001167 if( strcmp( opt.ca_path, "none" ) == 0 )
1168 ret = 0;
1169 else
1170 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001171 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001172 if( strcmp( opt.ca_file, "none" ) == 0 )
1173 ret = 0;
1174 else
1175 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001176 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001177#endif
1178#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +02001179 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
1180 strlen( test_ca_list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001181#else
1182 {
1183 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +00001184 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001185 }
1186#endif
1187 if( ret < 0 )
1188 {
Rich Evansf90016a2015-01-19 14:26:37 +00001189 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001190 goto exit;
1191 }
1192
Rich Evansf90016a2015-01-19 14:26:37 +00001193 polarssl_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001194
1195 /*
1196 * 1.2. Load own certificate and private key
1197 */
Rich Evansf90016a2015-01-19 14:26:37 +00001198 polarssl_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001199 fflush( stdout );
1200
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001201#if defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001202 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001203 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001204 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001205 if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
1206 {
Rich Evansf90016a2015-01-19 14:26:37 +00001207 polarssl_printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001208 -ret );
1209 goto exit;
1210 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001211 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001212 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001213 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001214 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001215 if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
1216 {
Rich Evansf90016a2015-01-19 14:26:37 +00001217 polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001218 goto exit;
1219 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001220 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001221 if( key_cert_init == 1 )
1222 {
Rich Evansf90016a2015-01-19 14:26:37 +00001223 polarssl_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001224 goto exit;
1225 }
1226
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001227 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001228 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001229 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001230 if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
1231 {
Rich Evansf90016a2015-01-19 14:26:37 +00001232 polarssl_printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001233 -ret );
1234 goto exit;
1235 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001236 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001237 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001238 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001239 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001240 if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
1241 {
Rich Evansf90016a2015-01-19 14:26:37 +00001242 polarssl_printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001243 -ret );
1244 goto exit;
1245 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001246 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001247 if( key_cert_init2 == 1 )
1248 {
Rich Evansf90016a2015-01-19 14:26:37 +00001249 polarssl_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001250 goto exit;
1251 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001252#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001253 if( key_cert_init == 0 &&
1254 strcmp( opt.crt_file, "none" ) != 0 &&
1255 strcmp( opt.key_file, "none" ) != 0 &&
1256 key_cert_init2 == 0 &&
1257 strcmp( opt.crt_file2, "none" ) != 0 &&
1258 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001259 {
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001260#if !defined(POLARSSL_CERTS_C)
Rich Evansf90016a2015-01-19 14:26:37 +00001261 polarssl_printf( "Not certificated or key provided, and \n"
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001262 "POLARSSL_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001263 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001264#else
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001265#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001266 if( ( ret = x509_crt_parse( &srvcert,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001267 (const unsigned char *) test_srv_crt_rsa,
1268 strlen( test_srv_crt_rsa ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001269 {
Rich Evansf90016a2015-01-19 14:26:37 +00001270 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001271 goto exit;
1272 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001273 if( ( ret = pk_parse_key( &pkey,
1274 (const unsigned char *) test_srv_key_rsa,
1275 strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001276 {
Rich Evansf90016a2015-01-19 14:26:37 +00001277 polarssl_printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001278 goto exit;
1279 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001280 key_cert_init = 2;
1281#endif /* POLARSSL_RSA_C */
1282#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001283 if( ( ret = x509_crt_parse( &srvcert2,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001284 (const unsigned char *) test_srv_crt_ec,
1285 strlen( test_srv_crt_ec ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001286 {
Rich Evansf90016a2015-01-19 14:26:37 +00001287 polarssl_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001288 goto exit;
1289 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001290 if( ( ret = pk_parse_key( &pkey2,
1291 (const unsigned char *) test_srv_key_ec,
1292 strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001293 {
Rich Evansf90016a2015-01-19 14:26:37 +00001294 polarssl_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001295 goto exit;
1296 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001297 key_cert_init2 = 2;
1298#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001299#endif /* POLARSSL_CERTS_C */
1300 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001301
Rich Evansf90016a2015-01-19 14:26:37 +00001302 polarssl_printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001303#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001304
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001305#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1306 if( opt.dhm_file != NULL )
1307 {
Rich Evansf90016a2015-01-19 14:26:37 +00001308 polarssl_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001309 fflush( stdout );
1310
1311 if( ( ret = dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
1312 {
Rich Evansf90016a2015-01-19 14:26:37 +00001313 polarssl_printf( " failed\n ! dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001314 -ret );
1315 goto exit;
1316 }
1317
Rich Evansf90016a2015-01-19 14:26:37 +00001318 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001319 }
1320#endif
1321
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001322#if defined(POLARSSL_SNI)
1323 if( opt.sni != NULL )
1324 {
Rich Evansf90016a2015-01-19 14:26:37 +00001325 polarssl_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001326 fflush( stdout );
1327
1328 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
1329 {
Rich Evansf90016a2015-01-19 14:26:37 +00001330 polarssl_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001331 goto exit;
1332 }
1333
Rich Evansf90016a2015-01-19 14:26:37 +00001334 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001335 }
1336#endif /* POLARSSL_SNI */
1337
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001338 /*
1339 * 2. Setup the listening TCP socket
1340 */
Rich Evansf90016a2015-01-19 14:26:37 +00001341 polarssl_printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001342 fflush( stdout );
1343
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001344 if( ( ret = net_bind( &listen_fd, opt.server_addr,
1345 opt.server_port ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001346 {
Rich Evansf90016a2015-01-19 14:26:37 +00001347 polarssl_printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001348 goto exit;
1349 }
1350
Rich Evansf90016a2015-01-19 14:26:37 +00001351 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001352
1353 /*
1354 * 3. Setup stuff
1355 */
Rich Evansf90016a2015-01-19 14:26:37 +00001356 polarssl_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001357 fflush( stdout );
1358
1359 if( ( ret = ssl_init( &ssl ) ) != 0 )
1360 {
Rich Evansf90016a2015-01-19 14:26:37 +00001361 polarssl_printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001362 goto exit;
1363 }
1364
1365 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001366 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001367
Paul Bakker05decb22013-08-15 13:33:48 +02001368#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001369 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1370 {
Rich Evansf90016a2015-01-19 14:26:37 +00001371 polarssl_printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001372 goto exit;
1373 };
Paul Bakker05decb22013-08-15 13:33:48 +02001374#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001375
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001376#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1377 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
1378 ssl_set_truncated_hmac( &ssl, opt.trunc_hmac );
1379#endif
1380
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001381#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1382 if( opt.extended_ms != DFL_EXTENDED_MS )
1383 ssl_set_extended_master_secret( &ssl, opt.extended_ms );
1384#endif
1385
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001386#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1387 if( opt.etm != DFL_ETM )
1388 ssl_set_encrypt_then_mac( &ssl, opt.etm );
1389#endif
1390
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001391#if defined(POLARSSL_SSL_ALPN)
1392 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001393 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1394 {
Rich Evansf90016a2015-01-19 14:26:37 +00001395 polarssl_printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001396 goto exit;
1397 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001398#endif
1399
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001400 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
1401 ssl_set_dbg( &ssl, my_debug, stdout );
1402
Paul Bakker0a597072012-09-25 21:55:46 +00001403#if defined(POLARSSL_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001404 if( opt.cache_max != -1 )
1405 ssl_cache_set_max_entries( &cache, opt.cache_max );
1406
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001407 if( opt.cache_timeout != -1 )
1408 ssl_cache_set_timeout( &cache, opt.cache_timeout );
1409
Paul Bakker0a597072012-09-25 21:55:46 +00001410 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
1411 ssl_cache_set, &cache );
1412#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001413
Paul Bakkera503a632013-08-14 13:48:06 +02001414#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001415 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1416 {
Rich Evansf90016a2015-01-19 14:26:37 +00001417 polarssl_printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001418 goto exit;
1419 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001420
1421 if( opt.ticket_timeout != -1 )
1422 ssl_set_session_ticket_lifetime( &ssl, opt.ticket_timeout );
Paul Bakkera503a632013-08-14 13:48:06 +02001423#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001424
Paul Bakker41c83d32013-03-20 14:39:14 +01001425 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001426 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001427 else
1428 ssl_set_arc4_support( &ssl, opt.arc4 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001429
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001430 if( opt.version_suites != NULL )
1431 {
1432 ssl_set_ciphersuites_for_version( &ssl, version_suites[0],
1433 SSL_MAJOR_VERSION_3,
1434 SSL_MINOR_VERSION_0 );
1435 ssl_set_ciphersuites_for_version( &ssl, version_suites[1],
1436 SSL_MAJOR_VERSION_3,
1437 SSL_MINOR_VERSION_1 );
1438 ssl_set_ciphersuites_for_version( &ssl, version_suites[2],
1439 SSL_MAJOR_VERSION_3,
1440 SSL_MINOR_VERSION_2 );
1441 ssl_set_ciphersuites_for_version( &ssl, version_suites[3],
1442 SSL_MAJOR_VERSION_3,
1443 SSL_MINOR_VERSION_3 );
1444 }
1445
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001446 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
1447 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001448#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001449 ssl_set_renegotiation( &ssl, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001450
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001451 if( opt.renego_delay != DFL_RENEGO_DELAY )
1452 ssl_set_renegotiation_enforced( &ssl, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001453
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001454 if( opt.renego_period != DFL_RENEGO_PERIOD )
1455 {
1456 renego_period[7] = opt.renego_period;
1457 ssl_set_renegotiation_period( &ssl, renego_period );
1458 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001459#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001460
Paul Bakker36713e82013-09-17 13:25:29 +02001461#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001462 if( strcmp( opt.ca_path, "none" ) != 0 &&
1463 strcmp( opt.ca_file, "none" ) != 0 )
1464 {
1465 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
1466 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001467 if( key_cert_init )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001468 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
1469 {
Rich Evansf90016a2015-01-19 14:26:37 +00001470 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001471 goto exit;
1472 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001473 if( key_cert_init2 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001474 if( ( ret = ssl_set_own_cert( &ssl, &srvcert2, &pkey2 ) ) != 0 )
1475 {
Rich Evansf90016a2015-01-19 14:26:37 +00001476 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001477 goto exit;
1478 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001479#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001480
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001481#if defined(POLARSSL_SNI)
1482 if( opt.sni != NULL )
1483 ssl_set_sni( &ssl, sni_callback, sni_info );
1484#endif
1485
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001486#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001487 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
1488 {
1489 ret = ssl_set_psk( &ssl, psk, psk_len,
1490 (const unsigned char *) opt.psk_identity,
1491 strlen( opt.psk_identity ) );
1492 if( ret != 0 )
1493 {
Rich Evansf90016a2015-01-19 14:26:37 +00001494 polarssl_printf( " failed\n ssl_set_psk returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001495 goto exit;
1496 }
1497 }
1498
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001499 if( opt.psk_list != NULL )
1500 ssl_set_psk_cb( &ssl, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02001501#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001502
1503#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00001504 /*
1505 * Use different group than default DHM group
1506 */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001507#if defined(POLARSSL_FS_IO)
1508 if( opt.dhm_file != NULL )
1509 ret = ssl_set_dh_param_ctx( &ssl, &dhm );
1510 else
1511#endif
1512 ret = ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
1513 POLARSSL_DHM_RFC5114_MODP_2048_G );
1514
1515 if( ret != 0 )
1516 {
Rich Evansf90016a2015-01-19 14:26:37 +00001517 polarssl_printf( " failed\n ssl_set_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001518 goto exit;
1519 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001520#endif
1521
Paul Bakker1d29fb52012-09-28 13:28:45 +00001522 if( opt.min_version != -1 )
1523 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1524
Paul Bakkerc1516be2013-06-29 16:01:32 +02001525 if( opt.max_version != -1 )
1526 ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1527
Rich Evansf90016a2015-01-19 14:26:37 +00001528 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001529
1530reset:
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +02001531#if !defined(_WIN32)
1532 if( received_sigterm )
1533 {
Manuel Pégourié-Gonnard7e81e702015-01-29 11:47:41 +00001534 polarssl_printf( " interrupted by SIGTERM\n" );
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +02001535 ret = 0;
1536 goto exit;
1537 }
1538#endif
1539
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001540#ifdef POLARSSL_ERROR_C
1541 if( ret != 0 )
1542 {
1543 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001544 polarssl_strerror( ret, error_buf, 100 );
Rich Evansf90016a2015-01-19 14:26:37 +00001545 polarssl_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001546 }
1547#endif
1548
1549 if( client_fd != -1 )
1550 net_close( client_fd );
1551
1552 ssl_session_reset( &ssl );
1553
1554 /*
1555 * 3. Wait until a client connects
1556 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001557 client_fd = -1;
1558
Rich Evansf90016a2015-01-19 14:26:37 +00001559 polarssl_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001560 fflush( stdout );
1561
1562 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
1563 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02001564#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001565 if( received_sigterm )
1566 {
Rich Evansf90016a2015-01-19 14:26:37 +00001567 polarssl_printf( " interrupted by signal\n" );
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001568 ret = 0;
1569 goto exit;
1570 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02001571#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001572
Rich Evansf90016a2015-01-19 14:26:37 +00001573 polarssl_printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001574 goto exit;
1575 }
1576
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001577 if( opt.nbio > 0 )
1578 ret = net_set_nonblock( client_fd );
1579 else
1580 ret = net_set_block( client_fd );
1581 if( ret != 0 )
1582 {
Rich Evansf90016a2015-01-19 14:26:37 +00001583 polarssl_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001584 goto exit;
1585 }
1586
1587 if( opt.nbio == 2 )
1588 ssl_set_bio( &ssl, my_recv, &client_fd, my_send, &client_fd );
1589 else
1590 ssl_set_bio( &ssl, net_recv, &client_fd, net_send, &client_fd );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001591
Rich Evansf90016a2015-01-19 14:26:37 +00001592 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001593
1594 /*
1595 * 4. Handshake
1596 */
Rich Evansf90016a2015-01-19 14:26:37 +00001597 polarssl_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001598 fflush( stdout );
1599
1600 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1601 {
1602 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1603 {
Rich Evansf90016a2015-01-19 14:26:37 +00001604 polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001605 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001606 }
1607 }
1608
Rich Evansf90016a2015-01-19 14:26:37 +00001609 polarssl_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Manuel Pégourié-Gonnardc580a002014-02-12 10:15:30 +01001610 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001611
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001612#if defined(POLARSSL_SSL_ALPN)
1613 if( opt.alpn_string != NULL )
1614 {
1615 const char *alp = ssl_get_alpn_protocol( &ssl );
Rich Evansf90016a2015-01-19 14:26:37 +00001616 polarssl_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001617 alp ? alp : "(none)" );
1618 }
1619#endif
1620
Paul Bakker36713e82013-09-17 13:25:29 +02001621#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001622 /*
1623 * 5. Verify the server certificate
1624 */
Rich Evansf90016a2015-01-19 14:26:37 +00001625 polarssl_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001626
1627 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1628 {
Rich Evansf90016a2015-01-19 14:26:37 +00001629 polarssl_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001630
Paul Bakkerb0550d92012-10-30 07:51:03 +00001631 if( !ssl_get_peer_cert( &ssl ) )
Rich Evansf90016a2015-01-19 14:26:37 +00001632 polarssl_printf( " ! no client certificate sent\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001633
1634 if( ( ret & BADCERT_EXPIRED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001635 polarssl_printf( " ! client certificate has expired\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001636
1637 if( ( ret & BADCERT_REVOKED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001638 polarssl_printf( " ! client certificate has been revoked\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001639
1640 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001641 polarssl_printf( " ! self-signed or not signed by a trusted CA\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001642
Rich Evansf90016a2015-01-19 14:26:37 +00001643 polarssl_printf( "\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001644 }
1645 else
Rich Evansf90016a2015-01-19 14:26:37 +00001646 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001647
Paul Bakkerb0550d92012-10-30 07:51:03 +00001648 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001649 {
Rich Evansf90016a2015-01-19 14:26:37 +00001650 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001651 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1652 ssl_get_peer_cert( &ssl ) );
Rich Evansf90016a2015-01-19 14:26:37 +00001653 polarssl_printf( "%s\n", buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001654 }
Paul Bakker36713e82013-09-17 13:25:29 +02001655#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001656
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001657 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001658data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001659 /*
1660 * 6. Read the HTTP Request
1661 */
Rich Evansf90016a2015-01-19 14:26:37 +00001662 polarssl_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001663 fflush( stdout );
1664
1665 do
1666 {
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001667 int terminated = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001668 len = sizeof( buf ) - 1;
1669 memset( buf, 0, sizeof( buf ) );
1670 ret = ssl_read( &ssl, buf, len );
1671
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001672 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1673 ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001674 continue;
1675
1676 if( ret <= 0 )
1677 {
1678 switch( ret )
1679 {
1680 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Rich Evansf90016a2015-01-19 14:26:37 +00001681 polarssl_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001682 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001683
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001684 case 0:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001685 case POLARSSL_ERR_NET_CONN_RESET:
Rich Evansf90016a2015-01-19 14:26:37 +00001686 polarssl_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001687 ret = POLARSSL_ERR_NET_CONN_RESET;
1688 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001689
1690 default:
Rich Evansf90016a2015-01-19 14:26:37 +00001691 polarssl_printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001692 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001693 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001694 }
1695
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001696 if( ssl_get_bytes_avail( &ssl ) == 0 )
1697 {
1698 len = ret;
1699 buf[len] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +00001700 polarssl_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001701
1702 /* End of message should be detected according to the syntax of the
1703 * application protocol (eg HTTP), just use a dummy test here. */
1704 if( buf[len - 1] == '\n' )
1705 terminated = 1;
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001706 }
1707 else
1708 {
1709 int extra_len, ori_len;
1710 unsigned char *larger_buf;
1711
1712 ori_len = ret;
1713 extra_len = ssl_get_bytes_avail( &ssl );
1714
1715 larger_buf = polarssl_malloc( ori_len + extra_len + 1 );
1716 if( larger_buf == NULL )
1717 {
Rich Evansf90016a2015-01-19 14:26:37 +00001718 polarssl_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001719 ret = 1;
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001720 goto reset;
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001721 }
1722
1723 memset( larger_buf, 0, ori_len + extra_len );
1724 memcpy( larger_buf, buf, ori_len );
1725
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +02001726 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001727 ret = ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +02001728 if( ret != extra_len ||
1729 ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001730 {
Rich Evansf90016a2015-01-19 14:26:37 +00001731 polarssl_printf( " ! ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001732 ret = 1;
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001733 goto reset;
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001734 }
1735
1736 larger_buf[ori_len + extra_len] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +00001737 polarssl_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnard0669f272014-06-18 13:07:20 +02001738 ori_len + extra_len, ori_len, extra_len,
1739 (char *) larger_buf );
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001740
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001741 /* End of message should be detected according to the syntax of the
1742 * application protocol (eg HTTP), just use a dummy test here. */
1743 if( larger_buf[ori_len + extra_len - 1] == '\n' )
1744 terminated = 1;
1745
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001746 polarssl_free( larger_buf );
1747 }
1748
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001749 if( terminated )
1750 {
1751 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001752 break;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001753 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001754 }
1755 while( 1 );
1756
1757 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001758 * 7a. Request renegotiation while client is waiting for input from us.
1759 * (only if we're going to exhange more data afterwards)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001760 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001761#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001762 if( opt.renegotiate && exchanges_left > 1 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001763 {
Rich Evansf90016a2015-01-19 14:26:37 +00001764 polarssl_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001765 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001766
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001767 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
1768 {
1769 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1770 ret != POLARSSL_ERR_NET_WANT_WRITE )
1771 {
Rich Evansf90016a2015-01-19 14:26:37 +00001772 polarssl_printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001773 goto reset;
1774 }
1775 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001776
Rich Evansf90016a2015-01-19 14:26:37 +00001777 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001778 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001779#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001780
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001781 /*
1782 * 7. Write the 200 Response
1783 */
Rich Evansf90016a2015-01-19 14:26:37 +00001784 polarssl_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001785 fflush( stdout );
1786
1787 len = sprintf( (char *) buf, HTTP_RESPONSE,
1788 ssl_get_ciphersuite( &ssl ) );
1789
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001790 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001791 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001792 while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001793 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001794 if( ret == POLARSSL_ERR_NET_CONN_RESET )
1795 {
Rich Evansf90016a2015-01-19 14:26:37 +00001796 polarssl_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001797 goto reset;
1798 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001799
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001800 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1801 {
Rich Evansf90016a2015-01-19 14:26:37 +00001802 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001803 goto reset;
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001804 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001805 }
1806 }
1807
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001808 buf[written] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +00001809 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 +01001810 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001811
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001812 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001813 * 7b. Continue doing data exchanges?
1814 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001815 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001816 goto data_exchange;
1817
1818 /*
1819 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001820 */
1821close_notify:
Rich Evansf90016a2015-01-19 14:26:37 +00001822 polarssl_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001823
Manuel Pégourié-Gonnard34377b12015-01-22 10:46:46 +00001824 /* No error checking, the connection might be closed already */
1825 do ret = ssl_close_notify( &ssl );
1826 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
1827 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001828
Rich Evansf90016a2015-01-19 14:26:37 +00001829 polarssl_printf( " done\n" );
1830
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001831 goto reset;
1832
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001833 /*
1834 * Cleanup and exit
1835 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001836exit:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001837#ifdef POLARSSL_ERROR_C
1838 if( ret != 0 )
1839 {
1840 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001841 polarssl_strerror( ret, error_buf, 100 );
Rich Evansf90016a2015-01-19 14:26:37 +00001842 polarssl_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001843 }
1844#endif
1845
Manuel Pégourié-Gonnard7e81e702015-01-29 11:47:41 +00001846 polarssl_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01001847 fflush( stdout );
1848
Paul Bakker0c226102014-04-17 16:02:36 +02001849 if( client_fd != -1 )
1850 net_close( client_fd );
1851
Paul Bakkera317a982014-06-18 16:44:11 +02001852#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1853 dhm_free( &dhm );
1854#endif
Paul Bakker36713e82013-09-17 13:25:29 +02001855#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker36713e82013-09-17 13:25:29 +02001856 x509_crt_free( &cacert );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001857 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02001858 pk_free( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001859 x509_crt_free( &srvcert2 );
1860 pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02001861#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001862#if defined(POLARSSL_SNI)
1863 sni_free( sni_info );
1864#endif
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02001865#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1866 psk_free( psk_info );
1867#endif
1868#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1869 dhm_free( &dhm );
1870#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001871
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001872 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02001873 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02001874 entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001875
Paul Bakker0a597072012-09-25 21:55:46 +00001876#if defined(POLARSSL_SSL_CACHE_C)
1877 ssl_cache_free( &cache );
1878#endif
1879
Paul Bakker1337aff2013-09-29 14:45:34 +02001880#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1881#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker82024bf2013-07-04 11:52:32 +02001882 memory_buffer_alloc_status();
1883#endif
Paul Bakker1337aff2013-09-29 14:45:34 +02001884 memory_buffer_alloc_free();
1885#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02001886
Manuel Pégourié-Gonnard7e81e702015-01-29 11:47:41 +00001887 polarssl_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01001888
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001889#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +00001890 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001891 fflush( stdout ); getchar();
1892#endif
1893
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001894 // Shell can not handle large exit numbers -> 1 for errors
1895 if( ret < 0 )
1896 ret = 1;
1897
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001898 return( ret );
1899}
1900#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
1901 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
1902 POLARSSL_CTR_DRBG_C */