blob: eb1f9ae2a04a52fa157d7a7cd13effdebc200ccf [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é-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
Paul Bakkerb60b95f2012-09-25 09:05:17 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020025#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#else
27#include POLARSSL_CONFIG_FILE
28#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000029
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020030#if !defined(POLARSSL_ENTROPY_C) || \
31 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
32 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
33#include <stdio.h>
34int main( int argc, char *argv[] )
35{
36 ((void) argc);
37 ((void) argv);
38
39 printf("POLARSSL_ENTROPY_C and/or "
40 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
41 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
42 return( 0 );
43}
44#else
45
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010046#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) && defined(POLARSSL_FS_IO)
47#define POLARSSL_SNI
48#endif
49
50#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
52#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakkerb60b95f2012-09-25 09:05:17 +000057#if defined(_WIN32)
58#include <windows.h>
59#endif
60
61#include <string.h>
62#include <stdlib.h>
63#include <stdio.h>
Paul Bakkerc1283d32014-08-18 11:05:51 +020064
65#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +020066#include <signal.h>
Paul Bakkerc1283d32014-08-18 11:05:51 +020067#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000068
Paul Bakkerb60b95f2012-09-25 09:05:17 +000069#include "polarssl/net.h"
70#include "polarssl/ssl.h"
71#include "polarssl/entropy.h"
72#include "polarssl/ctr_drbg.h"
73#include "polarssl/certs.h"
74#include "polarssl/x509.h"
75#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020076#include "polarssl/debug.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000077
Paul Bakker0a597072012-09-25 21:55:46 +000078#if defined(POLARSSL_SSL_CACHE_C)
79#include "polarssl/ssl_cache.h"
80#endif
81
Paul Bakker82024bf2013-07-04 11:52:32 +020082#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
83#include "polarssl/memory.h"
84#endif
85
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +010086#define DFL_SERVER_ADDR NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000087#define DFL_SERVER_PORT 4433
Paul Bakkerb60b95f2012-09-25 09:05:17 +000088#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010089#define DFL_NBIO 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +000090#define DFL_CA_FILE ""
91#define DFL_CA_PATH ""
92#define DFL_CRT_FILE ""
93#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +020094#define DFL_CRT_FILE2 ""
95#define DFL_KEY_FILE2 ""
Paul Bakkerfbb17802013-04-17 19:10:21 +020096#define DFL_PSK ""
97#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +020098#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000099#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200100#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +0200101#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100102#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100103#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200104#define DFL_RENEGO_DELAY -2
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100105#define DFL_RENEGO_PERIOD -1
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200106#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100107#define DFL_MIN_VERSION SSL_MINOR_VERSION_1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200108#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100109#define DFL_ARC4 SSL_ARC4_DISABLED
Paul Bakker91ebfb52012-11-23 14:04:08 +0100110#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200111#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100112#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200113#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100114#define DFL_TICKET_TIMEOUT -1
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100115#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100116#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100117#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200118#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200119#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200120#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100121#define DFL_ETM -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000122
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200123#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
124 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
125 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
126 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
127 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
128 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
129 "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 +0200130
Paul Bakker8e714d72013-07-18 11:05:13 +0200131/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
132 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000133#define HTTP_RESPONSE \
134 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000135 "<h2>mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200136 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000137
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200138/*
139 * Size of the basic I/O buffer. Able to hold our default response.
140 *
141 * You will need to adapt the ssl_get_bytes_avail() test in ssl-opt.sh
142 * if you change this value to something outside the range <= 100 or > 500
143 */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200144#define IO_BUF_LEN 200
145
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000146/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000147 * global options
148 */
149struct options
150{
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100151 const char *server_addr; /* address on which the ssl service runs */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000152 int server_port; /* port on which the ssl service runs */
153 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100154 int nbio; /* should I/O be blocking? */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200155 const char *ca_file; /* the file with the CA certificate(s) */
156 const char *ca_path; /* the path with the CA certificate(s) reside */
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200157 const char *crt_file; /* the file with the server certificate */
158 const char *key_file; /* the file with the server key */
159 const char *crt_file2; /* the file with the 2nd server certificate */
160 const char *key_file2; /* the file with the 2nd server key */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200161 const char *psk; /* the pre-shared key */
162 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200163 char *psk_list; /* list of PSK id/key pairs for callback */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000164 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200165 const char *version_suites; /* per-version ciphersuites */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000166 int renegotiation; /* enable / disable renegotiation */
167 int allow_legacy; /* allow legacy renegotiation */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100168 int renegotiate; /* attempt renegotiation? */
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200169 int renego_delay; /* delay before enforcing renegotiation */
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100170 int renego_period; /* period for automatic renegotiation */
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200171 int exchanges; /* number of data exchanges */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000172 int min_version; /* minimum protocol version accepted */
Paul Bakkerc1516be2013-06-29 16:01:32 +0200173 int max_version; /* maximum protocol version accepted */
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100174 int arc4; /* flag for arc4 suites support */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100175 int auth_mode; /* verify mode for connection */
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200176 unsigned char mfl_code; /* code for maximum fragment length */
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100177 int trunc_hmac; /* accept truncated hmac? */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200178 int tickets; /* enable / disable session tickets */
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100179 int ticket_timeout; /* session ticket lifetime */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100180 int cache_max; /* max number of session cache entries */
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100181 int cache_timeout; /* expiration delay of session cache entries */
Paul Bakker1ebc0c52014-05-22 15:47:58 +0200182 char *sni; /* string describing sni information */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200183 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200184 const char *dhm_file; /* the file with the DH parameters */
Paul Bakkerb2eaac12015-01-13 17:15:31 +0100185 int extended_ms; /* allow negotiation of extended MS? */
186 int etm; /* allow negotiation of encrypt-then-MAC? */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000187} opt;
188
Paul Bakker3c5ef712013-06-25 16:37:45 +0200189static void my_debug( void *ctx, int level, const char *str )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000190{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200191 ((void) level);
192
193 fprintf( (FILE *) ctx, "%s", str );
194 fflush( (FILE *) ctx );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000195}
196
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100197/*
198 * Test recv/send functions that make sure each try returns
199 * WANT_READ/WANT_WRITE at least once before sucesseding
200 */
201static int my_recv( void *ctx, unsigned char *buf, size_t len )
202{
203 static int first_try = 1;
204 int ret;
205
206 if( first_try )
207 {
208 first_try = 0;
209 return( POLARSSL_ERR_NET_WANT_READ );
210 }
211
212 ret = net_recv( ctx, buf, len );
213 if( ret != POLARSSL_ERR_NET_WANT_READ )
214 first_try = 1; /* Next call will be a new operation */
215 return( ret );
216}
217
218static int my_send( void *ctx, const unsigned char *buf, size_t len )
219{
220 static int first_try = 1;
221 int ret;
222
223 if( first_try )
224 {
225 first_try = 0;
226 return( POLARSSL_ERR_NET_WANT_WRITE );
227 }
228
229 ret = net_send( ctx, buf, len );
230 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
231 first_try = 1; /* Next call will be a new operation */
232 return( ret );
233}
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200234
235#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000236#if defined(POLARSSL_FS_IO)
237#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100238 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
239 " default: \"\" (pre-loaded)\n" \
240 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
241 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
242 " 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 +0200243 " default: see note after key_file2\n" \
244 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200245 " 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 +0200246 " default: see note after key_file2\n" \
247 " key_file2=%%s default: see note below\n" \
248 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200249 " preloaded certificate(s) and key(s) are used if available\n" \
250 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
251 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000252#else
253#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200254 "\n" \
255 " No file operations available (POLARSSL_FS_IO not defined)\n" \
256 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000257#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200258#else
259#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200260#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200261
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200262#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200263#define USAGE_PSK \
264 " psk=%%s default: \"\" (in hex, without 0x)\n" \
265 " psk_identity=%%s default: \"Client_identity\"\n"
266#else
267#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200268#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000269
Paul Bakkera503a632013-08-14 13:48:06 +0200270#if defined(POLARSSL_SSL_SESSION_TICKETS)
271#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100272 " tickets=%%d default: 1 (enabled)\n" \
273 " ticket_timeout=%%d default: ticket default (1d)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200274#else
275#define USAGE_TICKETS ""
276#endif /* POLARSSL_SSL_SESSION_TICKETS */
277
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100278#if defined(POLARSSL_SSL_CACHE_C)
279#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100280 " cache_max=%%d default: cache default (50)\n" \
281 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100282#else
283#define USAGE_CACHE ""
284#endif /* POLARSSL_SSL_CACHE_C */
285
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100286#if defined(POLARSSL_SNI)
287#define USAGE_SNI \
288 " sni=%%s name1,cert1,key1[,name2,cert2,key2[,...]]\n" \
289 " default: disabled\n"
290#else
291#define USAGE_SNI ""
292#endif /* POLARSSL_SNI */
293
Paul Bakker05decb22013-08-15 13:33:48 +0200294#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
295#define USAGE_MAX_FRAG_LEN \
296 " max_frag_len=%%d default: 16384 (tls default)\n" \
297 " options: 512, 1024, 2048, 4096\n"
298#else
299#define USAGE_MAX_FRAG_LEN ""
300#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
301
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100302#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
303#define USAGE_TRUNC_HMAC \
304 " trunc_hmac=%%d default: library default\n"
305#else
306#define USAGE_TRUNC_HMAC ""
307#endif
308
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200309#if defined(POLARSSL_SSL_ALPN)
310#define USAGE_ALPN \
311 " alpn=%%s default: \"\" (disabled)\n" \
312 " example: spdy/1,http/1.1\n"
313#else
314#define USAGE_ALPN ""
315#endif /* POLARSSL_SSL_ALPN */
316
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200317#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
318#define USAGE_EMS \
319 " extended_ms=0/1 default: (library default: on)\n"
320#else
321#define USAGE_EMS ""
322#endif
323
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100324#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
325#define USAGE_ETM \
326 " etm=0/1 default: (library default: on)\n"
327#else
328#define USAGE_ETM ""
329#endif
330
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100331#if defined(POLARSSL_SSL_RENEGOTIATION)
332#define USAGE_RENEGO \
333 " renegotiation=%%d default: 0 (disabled)\n" \
334 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100335 " renego_delay=%%d default: -2 (library default)\n" \
336 " renego_period=%%d default: (library default)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100337#else
338#define USAGE_RENEGO ""
339#endif
340
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000341#define USAGE \
342 "\n usage: ssl_server2 param=<>...\n" \
343 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100344 " server_addr=%%d default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000345 " server_port=%%d default: 4433\n" \
346 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100347 " nbio=%%d default: 0 (blocking I/O)\n" \
348 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100349 "\n" \
350 " auth_mode=%%s default: \"optional\"\n" \
351 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000352 USAGE_IO \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100353 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100354 "\n" \
355 USAGE_PSK \
356 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100357 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100358 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200359 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100360 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100361 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100362 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100363 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200364 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200365 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100366 USAGE_ETM \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100367 "\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000368 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200369 " max_version=%%s default: \"tls1_2\"\n" \
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100370 " arc4=%%d default: 0 (disabled)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200371 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200372 " options: ssl3, tls1, tls1_1, tls1_2\n" \
373 "\n" \
374 " version_suites=a,b,c,d per-version ciphersuites\n" \
375 " in order from ssl3 to tls1_2\n" \
376 " default: all enabled\n" \
377 " force_ciphersuite=<name> default: all enabled\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000378 " acceptable ciphersuite names:\n"
379
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200380/*
381 * Used by sni_parse and psk_parse to handle coma-separated lists
382 */
383#define GET_ITEM( dst ) \
384 dst = p; \
385 while( *p != ',' ) \
386 if( ++p > end ) \
387 return( NULL ); \
388 *p++ = '\0';
389
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100390#if defined(POLARSSL_SNI)
391typedef struct _sni_entry sni_entry;
392
393struct _sni_entry {
394 const char *name;
395 x509_crt *cert;
396 pk_context *key;
397 sni_entry *next;
398};
399
400/*
401 * Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]]
402 * into a usable sni_entry list.
403 *
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200404 * Modifies the input string! This is not production quality!
405 * (leaks memory if parsing fails, no error reporting, ...)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100406 */
407sni_entry *sni_parse( char *sni_string )
408{
409 sni_entry *cur = NULL, *new = NULL;
410 char *p = sni_string;
411 char *end = p;
412 char *crt_file, *key_file;
413
414 while( *end != '\0' )
415 ++end;
416 *end = ',';
417
418 while( p <= end )
419 {
420 if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL )
421 return( NULL );
422
423 memset( new, 0, sizeof( sni_entry ) );
424
425 if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ||
426 ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200427 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100428
429 x509_crt_init( new->cert );
430 pk_init( new->key );
431
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200432 GET_ITEM( new->name );
433 GET_ITEM( crt_file );
434 GET_ITEM( key_file );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100435
436 if( x509_crt_parse_file( new->cert, crt_file ) != 0 ||
437 pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200438 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100439
440 new->next = cur;
441 cur = new;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100442 }
443
444 return( cur );
445}
446
447void sni_free( sni_entry *head )
448{
449 sni_entry *cur = head, *next;
450
451 while( cur != NULL )
452 {
453 x509_crt_free( cur->cert );
454 polarssl_free( cur->cert );
455
456 pk_free( cur->key );
457 polarssl_free( cur->key );
458
459 next = cur->next;
460 polarssl_free( cur );
461 cur = next;
462 }
463}
464
465/*
466 * SNI callback.
467 */
468int sni_callback( void *p_info, ssl_context *ssl,
469 const unsigned char *name, size_t name_len )
470{
471 sni_entry *cur = (sni_entry *) p_info;
472
473 while( cur != NULL )
474 {
475 if( name_len == strlen( cur->name ) &&
476 memcmp( name, cur->name, name_len ) == 0 )
477 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200478 return( ssl_set_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100479 }
480
481 cur = cur->next;
482 }
483
484 return( -1 );
485}
486
487#endif /* POLARSSL_SNI */
488
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200489#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
490
491#define HEX2NUM( c ) \
492 if( c >= '0' && c <= '9' ) \
493 c -= '0'; \
494 else if( c >= 'a' && c <= 'f' ) \
495 c -= 'a' - 10; \
496 else if( c >= 'A' && c <= 'F' ) \
497 c -= 'A' - 10; \
498 else \
499 return( -1 );
500
501/*
502 * Convert a hex string to bytes.
503 * Return 0 on success, -1 on error.
504 */
505int unhexify( unsigned char *output, const char *input, size_t *olen )
506{
507 unsigned char c;
508 size_t j;
509
510 *olen = strlen( input );
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200511 if( *olen % 2 != 0 || *olen / 2 > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200512 return( -1 );
513 *olen /= 2;
514
515 for( j = 0; j < *olen * 2; j += 2 )
516 {
517 c = input[j];
518 HEX2NUM( c );
519 output[ j / 2 ] = c << 4;
520
521 c = input[j + 1];
522 HEX2NUM( c );
523 output[ j / 2 ] |= c;
524 }
525
526 return( 0 );
527}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200528
529typedef struct _psk_entry psk_entry;
530
531struct _psk_entry
532{
533 const char *name;
534 size_t key_len;
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200535 unsigned char key[POLARSSL_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200536 psk_entry *next;
537};
538
539/*
540 * Parse a string of pairs name1,key1[,name2,key2[,...]]
541 * into a usable psk_entry list.
542 *
543 * Modifies the input string! This is not production quality!
544 * (leaks memory if parsing fails, no error reporting, ...)
545 */
546psk_entry *psk_parse( char *psk_string )
547{
548 psk_entry *cur = NULL, *new = NULL;
549 char *p = psk_string;
550 char *end = p;
551 char *key_hex;
552
553 while( *end != '\0' )
554 ++end;
555 *end = ',';
556
557 while( p <= end )
558 {
559 if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL )
560 return( NULL );
561
562 memset( new, 0, sizeof( psk_entry ) );
563
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200564 GET_ITEM( new->name );
565 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200566
567 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
568 return( NULL );
569
570 new->next = cur;
571 cur = new;
572 }
573
574 return( cur );
575}
576
577/*
578 * Free a list of psk_entry's
579 */
580void psk_free( psk_entry *head )
581{
582 psk_entry *next;
583
584 while( head != NULL )
585 {
586 next = head->next;
587 polarssl_free( head );
588 head = next;
589 }
590}
591
592/*
593 * PSK callback
594 */
595int psk_callback( void *p_info, ssl_context *ssl,
596 const unsigned char *name, size_t name_len )
597{
598 psk_entry *cur = (psk_entry *) p_info;
599
600 while( cur != NULL )
601 {
602 if( name_len == strlen( cur->name ) &&
603 memcmp( name, cur->name, name_len ) == 0 )
604 {
605 return( ssl_set_psk( ssl, cur->key, cur->key_len,
606 name, name_len ) );
607 }
608
609 cur = cur->next;
610 }
611
612 return( -1 );
613}
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200614#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
615
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +0200616static int listen_fd, client_fd = -1;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200617
618/* Interruption handler to ensure clean exit (for valgrind testing) */
619#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200620static int received_sigterm = 0;
621void term_handler( int sig )
622{
623 ((void) sig);
624 received_sigterm = 1;
625 net_close( listen_fd ); /* causes net_accept() to abort */
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +0200626 net_close( client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200627}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200628#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200629
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000630int main( int argc, char *argv[] )
631{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +0000632 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200633 int version_suites[4][2];
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200634 unsigned char buf[IO_BUF_LEN];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200635#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200636 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +0200637 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +0200638 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +0200639#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200640 const char *pers = "ssl_server2";
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000641
642 entropy_context entropy;
643 ctr_drbg_context ctr_drbg;
644 ssl_context ssl;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100645#if defined(POLARSSL_SSL_RENEGOTIATION)
646 unsigned char renego_period[8] = { 0 };
647#endif
Paul Bakker36713e82013-09-17 13:25:29 +0200648#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200649 x509_crt cacert;
650 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200651 pk_context pkey;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200652 x509_crt srvcert2;
653 pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200654 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200655#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200656#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
657 dhm_context dhm;
658#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000659#if defined(POLARSSL_SSL_CACHE_C)
660 ssl_cache_context cache;
661#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100662#if defined(POLARSSL_SNI)
663 sni_entry *sni_info = NULL;
664#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200665#if defined(POLARSSL_SSL_ALPN)
666 const char *alpn_list[10];
667#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200668#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
669 unsigned char alloc_buf[100000];
670#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000671
672 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000673 char *p, *q;
674 const int *list;
675
Paul Bakker82024bf2013-07-04 11:52:32 +0200676#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
677 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
678#endif
679
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000680 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200681 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000682 */
683 listen_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200684 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200685#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200686 x509_crt_init( &cacert );
687 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200688 pk_init( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200689 x509_crt_init( &srvcert2 );
690 pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200691#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200692#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
Paul Bakkera317a982014-06-18 16:44:11 +0200693 dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200694#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000695#if defined(POLARSSL_SSL_CACHE_C)
696 ssl_cache_init( &cache );
697#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200698#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200699 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200700#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000701
Paul Bakkerc1283d32014-08-18 11:05:51 +0200702#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100703 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200704 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100705 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +0200706#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200707
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000708 if( argc == 0 )
709 {
710 usage:
711 if( ret == 0 )
712 ret = 1;
713
714 printf( USAGE );
715
716 list = ssl_list_ciphersuites();
717 while( *list )
718 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200719 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200720 list++;
721 if( !*list )
722 break;
723 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000724 list++;
725 }
726 printf("\n");
727 goto exit;
728 }
729
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100730 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000731 opt.server_port = DFL_SERVER_PORT;
732 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100733 opt.nbio = DFL_NBIO;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000734 opt.ca_file = DFL_CA_FILE;
735 opt.ca_path = DFL_CA_PATH;
736 opt.crt_file = DFL_CRT_FILE;
737 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200738 opt.crt_file2 = DFL_CRT_FILE2;
739 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200740 opt.psk = DFL_PSK;
741 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200742 opt.psk_list = DFL_PSK_LIST;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000743 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200744 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000745 opt.renegotiation = DFL_RENEGOTIATION;
746 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100747 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200748 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100749 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200750 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000751 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200752 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100753 opt.arc4 = DFL_ARC4;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100754 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200755 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100756 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200757 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100758 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100759 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100760 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100761 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200762 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200763 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200764 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100765 opt.etm = DFL_ETM;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000766
767 for( i = 1; i < argc; i++ )
768 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000769 p = argv[i];
770 if( ( q = strchr( p, '=' ) ) == NULL )
771 goto usage;
772 *q++ = '\0';
773
774 if( strcmp( p, "server_port" ) == 0 )
775 {
776 opt.server_port = atoi( q );
777 if( opt.server_port < 1 || opt.server_port > 65535 )
778 goto usage;
779 }
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100780 else if( strcmp( p, "server_addr" ) == 0 )
781 opt.server_addr = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000782 else if( strcmp( p, "debug_level" ) == 0 )
783 {
784 opt.debug_level = atoi( q );
785 if( opt.debug_level < 0 || opt.debug_level > 65535 )
786 goto usage;
787 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100788 else if( strcmp( p, "nbio" ) == 0 )
789 {
790 opt.nbio = atoi( q );
791 if( opt.nbio < 0 || opt.nbio > 2 )
792 goto usage;
793 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000794 else if( strcmp( p, "ca_file" ) == 0 )
795 opt.ca_file = q;
796 else if( strcmp( p, "ca_path" ) == 0 )
797 opt.ca_path = q;
798 else if( strcmp( p, "crt_file" ) == 0 )
799 opt.crt_file = q;
800 else if( strcmp( p, "key_file" ) == 0 )
801 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200802 else if( strcmp( p, "crt_file2" ) == 0 )
803 opt.crt_file2 = q;
804 else if( strcmp( p, "key_file2" ) == 0 )
805 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200806 else if( strcmp( p, "dhm_file" ) == 0 )
807 opt.dhm_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200808 else if( strcmp( p, "psk" ) == 0 )
809 opt.psk = q;
810 else if( strcmp( p, "psk_identity" ) == 0 )
811 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200812 else if( strcmp( p, "psk_list" ) == 0 )
813 opt.psk_list = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000814 else if( strcmp( p, "force_ciphersuite" ) == 0 )
815 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000816 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
817
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200818 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000819 {
820 ret = 2;
821 goto usage;
822 }
823 opt.force_ciphersuite[1] = 0;
824 }
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200825 else if( strcmp( p, "version_suites" ) == 0 )
826 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000827 else if( strcmp( p, "renegotiation" ) == 0 )
828 {
829 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
830 SSL_RENEGOTIATION_DISABLED;
831 }
832 else if( strcmp( p, "allow_legacy" ) == 0 )
833 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100834 switch( atoi( q ) )
835 {
836 case -1: opt.allow_legacy = SSL_LEGACY_BREAK_HANDSHAKE; break;
837 case 0: opt.allow_legacy = SSL_LEGACY_NO_RENEGOTIATION; break;
838 case 1: opt.allow_legacy = SSL_LEGACY_ALLOW_RENEGOTIATION; break;
839 default: goto usage;
840 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000841 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100842 else if( strcmp( p, "renegotiate" ) == 0 )
843 {
844 opt.renegotiate = atoi( q );
845 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
846 goto usage;
847 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200848 else if( strcmp( p, "renego_delay" ) == 0 )
849 {
850 opt.renego_delay = atoi( q );
851 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100852 else if( strcmp( p, "renego_period" ) == 0 )
853 {
854 opt.renego_period = atoi( q );
855 if( opt.renego_period < 2 || opt.renego_period > 255 )
856 goto usage;
857 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200858 else if( strcmp( p, "exchanges" ) == 0 )
859 {
860 opt.exchanges = atoi( q );
861 if( opt.exchanges < 1 )
862 goto usage;
863 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000864 else if( strcmp( p, "min_version" ) == 0 )
865 {
866 if( strcmp( q, "ssl3" ) == 0 )
867 opt.min_version = SSL_MINOR_VERSION_0;
868 else if( strcmp( q, "tls1" ) == 0 )
869 opt.min_version = SSL_MINOR_VERSION_1;
870 else if( strcmp( q, "tls1_1" ) == 0 )
871 opt.min_version = SSL_MINOR_VERSION_2;
872 else if( strcmp( q, "tls1_2" ) == 0 )
873 opt.min_version = SSL_MINOR_VERSION_3;
874 else
875 goto usage;
876 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200877 else if( strcmp( p, "max_version" ) == 0 )
878 {
879 if( strcmp( q, "ssl3" ) == 0 )
880 opt.max_version = SSL_MINOR_VERSION_0;
881 else if( strcmp( q, "tls1" ) == 0 )
882 opt.max_version = SSL_MINOR_VERSION_1;
883 else if( strcmp( q, "tls1_1" ) == 0 )
884 opt.max_version = SSL_MINOR_VERSION_2;
885 else if( strcmp( q, "tls1_2" ) == 0 )
886 opt.max_version = SSL_MINOR_VERSION_3;
887 else
888 goto usage;
889 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100890 else if( strcmp( p, "arc4" ) == 0 )
891 {
892 switch( atoi( q ) )
893 {
894 case 0: opt.arc4 = SSL_ARC4_DISABLED; break;
895 case 1: opt.arc4 = SSL_ARC4_ENABLED; break;
896 default: goto usage;
897 }
898 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200899 else if( strcmp( p, "force_version" ) == 0 )
900 {
901 if( strcmp( q, "ssl3" ) == 0 )
902 {
903 opt.min_version = SSL_MINOR_VERSION_0;
904 opt.max_version = SSL_MINOR_VERSION_0;
905 }
906 else if( strcmp( q, "tls1" ) == 0 )
907 {
908 opt.min_version = SSL_MINOR_VERSION_1;
909 opt.max_version = SSL_MINOR_VERSION_1;
910 }
911 else if( strcmp( q, "tls1_1" ) == 0 )
912 {
913 opt.min_version = SSL_MINOR_VERSION_2;
914 opt.max_version = SSL_MINOR_VERSION_2;
915 }
916 else if( strcmp( q, "tls1_2" ) == 0 )
917 {
918 opt.min_version = SSL_MINOR_VERSION_3;
919 opt.max_version = SSL_MINOR_VERSION_3;
920 }
921 else
922 goto usage;
923 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100924 else if( strcmp( p, "auth_mode" ) == 0 )
925 {
926 if( strcmp( q, "none" ) == 0 )
927 opt.auth_mode = SSL_VERIFY_NONE;
928 else if( strcmp( q, "optional" ) == 0 )
929 opt.auth_mode = SSL_VERIFY_OPTIONAL;
930 else if( strcmp( q, "required" ) == 0 )
931 opt.auth_mode = SSL_VERIFY_REQUIRED;
932 else
933 goto usage;
934 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200935 else if( strcmp( p, "max_frag_len" ) == 0 )
936 {
937 if( strcmp( q, "512" ) == 0 )
938 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
939 else if( strcmp( q, "1024" ) == 0 )
940 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
941 else if( strcmp( q, "2048" ) == 0 )
942 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
943 else if( strcmp( q, "4096" ) == 0 )
944 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
945 else
946 goto usage;
947 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200948 else if( strcmp( p, "alpn" ) == 0 )
949 {
950 opt.alpn_string = q;
951 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100952 else if( strcmp( p, "trunc_hmac" ) == 0 )
953 {
954 switch( atoi( q ) )
955 {
956 case 0: opt.trunc_hmac = SSL_TRUNC_HMAC_DISABLED; break;
957 case 1: opt.trunc_hmac = SSL_TRUNC_HMAC_ENABLED; break;
958 default: goto usage;
959 }
960 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200961 else if( strcmp( p, "extended_ms" ) == 0 )
962 {
963 switch( atoi( q ) )
964 {
965 case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break;
966 case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break;
967 default: goto usage;
968 }
969 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100970 else if( strcmp( p, "etm" ) == 0 )
971 {
972 switch( atoi( q ) )
973 {
974 case 0: opt.etm = SSL_ETM_DISABLED; break;
975 case 1: opt.etm = SSL_ETM_ENABLED; break;
976 default: goto usage;
977 }
978 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200979 else if( strcmp( p, "tickets" ) == 0 )
980 {
981 opt.tickets = atoi( q );
982 if( opt.tickets < 0 || opt.tickets > 1 )
983 goto usage;
984 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100985 else if( strcmp( p, "ticket_timeout" ) == 0 )
986 {
987 opt.ticket_timeout = atoi( q );
988 if( opt.ticket_timeout < 0 )
989 goto usage;
990 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100991 else if( strcmp( p, "cache_max" ) == 0 )
992 {
993 opt.cache_max = atoi( q );
994 if( opt.cache_max < 0 )
995 goto usage;
996 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100997 else if( strcmp( p, "cache_timeout" ) == 0 )
998 {
999 opt.cache_timeout = atoi( q );
1000 if( opt.cache_timeout < 0 )
1001 goto usage;
1002 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001003 else if( strcmp( p, "sni" ) == 0 )
1004 {
1005 opt.sni = q;
1006 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001007 else
1008 goto usage;
1009 }
1010
Paul Bakkerc73079a2014-04-25 16:34:30 +02001011#if defined(POLARSSL_DEBUG_C)
1012 debug_set_threshold( opt.debug_level );
1013#endif
1014
Paul Bakkerc1516be2013-06-29 16:01:32 +02001015 if( opt.force_ciphersuite[0] > 0 )
1016 {
1017 const ssl_ciphersuite_t *ciphersuite_info;
1018 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1019
Paul Bakker5b55b792013-07-19 13:43:43 +02001020 if( opt.max_version != -1 &&
1021 ciphersuite_info->min_minor_ver > opt.max_version )
1022 {
1023 printf("forced ciphersuite not allowed with this protocol version\n");
1024 ret = 2;
1025 goto usage;
1026 }
1027 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001028 ciphersuite_info->max_minor_ver < opt.min_version )
1029 {
1030 printf("forced ciphersuite not allowed with this protocol version\n");
1031 ret = 2;
1032 goto usage;
1033 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001034 if( opt.max_version > ciphersuite_info->max_minor_ver )
1035 opt.max_version = ciphersuite_info->max_minor_ver;
1036 if( opt.min_version < ciphersuite_info->min_minor_ver )
1037 opt.min_version = ciphersuite_info->min_minor_ver;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001038 }
1039
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001040 if( opt.version_suites != NULL )
1041 {
1042 const char *name[4] = { 0 };
1043
1044 /* Parse 4-element coma-separated list */
1045 for( i = 0, p = (char *) opt.version_suites;
1046 i < 4 && *p != '\0';
1047 i++ )
1048 {
1049 name[i] = p;
1050
1051 /* Terminate the current string and move on to next one */
1052 while( *p != ',' && *p != '\0' )
1053 p++;
1054 if( *p == ',' )
1055 *p++ = '\0';
1056 }
1057
1058 if( i != 4 )
1059 {
1060 printf( "too few values for version_suites\n" );
1061 ret = 1;
1062 goto exit;
1063 }
1064
1065 memset( version_suites, 0, sizeof( version_suites ) );
1066
1067 /* Get the suites identifiers from their name */
1068 for( i = 0; i < 4; i++ )
1069 {
1070 version_suites[i][0] = ssl_get_ciphersuite_id( name[i] );
1071
1072 if( version_suites[i][0] == 0 )
1073 {
1074 printf( "unknown ciphersuite: '%s'\n", name[i] );
1075 ret = 2;
1076 goto usage;
1077 }
1078 }
1079 }
1080
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001081#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001082 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001083 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001084 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001085 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001086 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001087 printf( "pre-shared key not valid hex\n" );
1088 goto exit;
1089 }
1090
1091 if( opt.psk_list != NULL )
1092 {
1093 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001094 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001095 printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001096 goto exit;
1097 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001098 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001099#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001100
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001101#if defined(POLARSSL_SSL_ALPN)
1102 if( opt.alpn_string != NULL )
1103 {
1104 p = (char *) opt.alpn_string;
1105 i = 0;
1106
1107 /* Leave room for a final NULL in alpn_list */
1108 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
1109 {
1110 alpn_list[i++] = p;
1111
1112 /* Terminate the current string and move on to next one */
1113 while( *p != ',' && *p != '\0' )
1114 p++;
1115 if( *p == ',' )
1116 *p++ = '\0';
1117 }
1118 }
1119#endif /* POLARSSL_SSL_ALPN */
1120
Paul Bakkerfbb17802013-04-17 19:10:21 +02001121 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001122 * 0. Initialize the RNG and the session data
1123 */
1124 printf( "\n . Seeding the random number generator..." );
1125 fflush( stdout );
1126
1127 entropy_init( &entropy );
1128 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001129 (const unsigned char *) pers,
1130 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001131 {
1132 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
1133 goto exit;
1134 }
1135
1136 printf( " ok\n" );
1137
Paul Bakker36713e82013-09-17 13:25:29 +02001138#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001139 /*
1140 * 1.1. Load the trusted CA
1141 */
1142 printf( " . Loading the CA root certificate ..." );
1143 fflush( stdout );
1144
1145#if defined(POLARSSL_FS_IO)
1146 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001147 if( strcmp( opt.ca_path, "none" ) == 0 )
1148 ret = 0;
1149 else
1150 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001151 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001152 if( strcmp( opt.ca_file, "none" ) == 0 )
1153 ret = 0;
1154 else
1155 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001156 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001157#endif
1158#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +02001159 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
1160 strlen( test_ca_list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001161#else
1162 {
1163 ret = 1;
1164 printf("POLARSSL_CERTS_C not defined.");
1165 }
1166#endif
1167 if( ret < 0 )
1168 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001169 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001170 goto exit;
1171 }
1172
1173 printf( " ok (%d skipped)\n", ret );
1174
1175 /*
1176 * 1.2. Load own certificate and private key
1177 */
1178 printf( " . Loading the server cert. and key..." );
1179 fflush( stdout );
1180
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001181#if defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001182 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001183 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001184 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001185 if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
1186 {
1187 printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n",
1188 -ret );
1189 goto exit;
1190 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001191 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001192 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001193 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001194 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001195 if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
1196 {
1197 printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret );
1198 goto exit;
1199 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001200 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001201 if( key_cert_init == 1 )
1202 {
1203 printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
1204 goto exit;
1205 }
1206
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001207 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001208 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001209 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001210 if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
1211 {
1212 printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n",
1213 -ret );
1214 goto exit;
1215 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001216 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001217 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001218 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001219 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001220 if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
1221 {
1222 printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n",
1223 -ret );
1224 goto exit;
1225 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001226 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001227 if( key_cert_init2 == 1 )
1228 {
1229 printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
1230 goto exit;
1231 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001232#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001233 if( key_cert_init == 0 &&
1234 strcmp( opt.crt_file, "none" ) != 0 &&
1235 strcmp( opt.key_file, "none" ) != 0 &&
1236 key_cert_init2 == 0 &&
1237 strcmp( opt.crt_file2, "none" ) != 0 &&
1238 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001239 {
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001240#if !defined(POLARSSL_CERTS_C)
1241 printf( "Not certificated or key provided, and \n"
1242 "POLARSSL_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001243 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001244#else
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001245#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001246 if( ( ret = x509_crt_parse( &srvcert,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001247 (const unsigned char *) test_srv_crt_rsa,
1248 strlen( test_srv_crt_rsa ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001249 {
1250 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
1251 goto exit;
1252 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001253 if( ( ret = pk_parse_key( &pkey,
1254 (const unsigned char *) test_srv_key_rsa,
1255 strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001256 {
1257 printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
1258 goto exit;
1259 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001260 key_cert_init = 2;
1261#endif /* POLARSSL_RSA_C */
1262#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001263 if( ( ret = x509_crt_parse( &srvcert2,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001264 (const unsigned char *) test_srv_crt_ec,
1265 strlen( test_srv_crt_ec ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001266 {
1267 printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
1268 goto exit;
1269 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001270 if( ( ret = pk_parse_key( &pkey2,
1271 (const unsigned char *) test_srv_key_ec,
1272 strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001273 {
1274 printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
1275 goto exit;
1276 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001277 key_cert_init2 = 2;
1278#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001279#endif /* POLARSSL_CERTS_C */
1280 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001281
1282 printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001283#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001284
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001285#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1286 if( opt.dhm_file != NULL )
1287 {
1288 printf( " . Loading DHM parameters..." );
1289 fflush( stdout );
1290
1291 if( ( ret = dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
1292 {
1293 printf( " failed\n ! dhm_parse_dhmfile returned -0x%04X\n\n",
1294 -ret );
1295 goto exit;
1296 }
1297
1298 printf( " ok\n" );
1299 }
1300#endif
1301
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001302#if defined(POLARSSL_SNI)
1303 if( opt.sni != NULL )
1304 {
1305 printf( " . Setting up SNI information..." );
1306 fflush( stdout );
1307
1308 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
1309 {
1310 printf( " failed\n" );
1311 goto exit;
1312 }
1313
1314 printf( " ok\n" );
1315 }
1316#endif /* POLARSSL_SNI */
1317
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001318 /*
1319 * 2. Setup the listening TCP socket
1320 */
1321 printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
1322 fflush( stdout );
1323
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001324 if( ( ret = net_bind( &listen_fd, opt.server_addr,
1325 opt.server_port ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001326 {
1327 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1328 goto exit;
1329 }
1330
1331 printf( " ok\n" );
1332
1333 /*
1334 * 3. Setup stuff
1335 */
1336 printf( " . Setting up the SSL/TLS structure..." );
1337 fflush( stdout );
1338
1339 if( ( ret = ssl_init( &ssl ) ) != 0 )
1340 {
1341 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
1342 goto exit;
1343 }
1344
1345 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001346 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001347
Paul Bakker05decb22013-08-15 13:33:48 +02001348#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001349 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1350 {
1351 printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
1352 goto exit;
1353 };
Paul Bakker05decb22013-08-15 13:33:48 +02001354#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001355
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001356#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1357 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
1358 ssl_set_truncated_hmac( &ssl, opt.trunc_hmac );
1359#endif
1360
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001361#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1362 if( opt.extended_ms != DFL_EXTENDED_MS )
1363 ssl_set_extended_master_secret( &ssl, opt.extended_ms );
1364#endif
1365
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001366#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1367 if( opt.etm != DFL_ETM )
1368 ssl_set_encrypt_then_mac( &ssl, opt.etm );
1369#endif
1370
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001371#if defined(POLARSSL_SSL_ALPN)
1372 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001373 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1374 {
1375 printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
1376 goto exit;
1377 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001378#endif
1379
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001380 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
1381 ssl_set_dbg( &ssl, my_debug, stdout );
1382
Paul Bakker0a597072012-09-25 21:55:46 +00001383#if defined(POLARSSL_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001384 if( opt.cache_max != -1 )
1385 ssl_cache_set_max_entries( &cache, opt.cache_max );
1386
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001387 if( opt.cache_timeout != -1 )
1388 ssl_cache_set_timeout( &cache, opt.cache_timeout );
1389
Paul Bakker0a597072012-09-25 21:55:46 +00001390 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
1391 ssl_cache_set, &cache );
1392#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001393
Paul Bakkera503a632013-08-14 13:48:06 +02001394#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001395 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1396 {
1397 printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
1398 goto exit;
1399 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001400
1401 if( opt.ticket_timeout != -1 )
1402 ssl_set_session_ticket_lifetime( &ssl, opt.ticket_timeout );
Paul Bakkera503a632013-08-14 13:48:06 +02001403#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001404
Paul Bakker41c83d32013-03-20 14:39:14 +01001405 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001406 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001407 else
1408 ssl_set_arc4_support( &ssl, opt.arc4 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001409
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001410 if( opt.version_suites != NULL )
1411 {
1412 ssl_set_ciphersuites_for_version( &ssl, version_suites[0],
1413 SSL_MAJOR_VERSION_3,
1414 SSL_MINOR_VERSION_0 );
1415 ssl_set_ciphersuites_for_version( &ssl, version_suites[1],
1416 SSL_MAJOR_VERSION_3,
1417 SSL_MINOR_VERSION_1 );
1418 ssl_set_ciphersuites_for_version( &ssl, version_suites[2],
1419 SSL_MAJOR_VERSION_3,
1420 SSL_MINOR_VERSION_2 );
1421 ssl_set_ciphersuites_for_version( &ssl, version_suites[3],
1422 SSL_MAJOR_VERSION_3,
1423 SSL_MINOR_VERSION_3 );
1424 }
1425
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001426 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
1427 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001428#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001429 ssl_set_renegotiation( &ssl, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001430
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001431 if( opt.renego_delay != DFL_RENEGO_DELAY )
1432 ssl_set_renegotiation_enforced( &ssl, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001433
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001434 if( opt.renego_period != DFL_RENEGO_PERIOD )
1435 {
1436 renego_period[7] = opt.renego_period;
1437 ssl_set_renegotiation_period( &ssl, renego_period );
1438 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001439#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001440
Paul Bakker36713e82013-09-17 13:25:29 +02001441#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001442 if( strcmp( opt.ca_path, "none" ) != 0 &&
1443 strcmp( opt.ca_file, "none" ) != 0 )
1444 {
1445 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
1446 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001447 if( key_cert_init )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001448 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
1449 {
1450 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1451 goto exit;
1452 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001453 if( key_cert_init2 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001454 if( ( ret = ssl_set_own_cert( &ssl, &srvcert2, &pkey2 ) ) != 0 )
1455 {
1456 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1457 goto exit;
1458 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001459#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001460
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001461#if defined(POLARSSL_SNI)
1462 if( opt.sni != NULL )
1463 ssl_set_sni( &ssl, sni_callback, sni_info );
1464#endif
1465
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001466#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001467 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
1468 {
1469 ret = ssl_set_psk( &ssl, psk, psk_len,
1470 (const unsigned char *) opt.psk_identity,
1471 strlen( opt.psk_identity ) );
1472 if( ret != 0 )
1473 {
1474 printf( " failed\n ssl_set_psk returned -0x%04X\n\n", - ret );
1475 goto exit;
1476 }
1477 }
1478
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001479 if( opt.psk_list != NULL )
1480 ssl_set_psk_cb( &ssl, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02001481#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001482
1483#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00001484 /*
1485 * Use different group than default DHM group
1486 */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001487#if defined(POLARSSL_FS_IO)
1488 if( opt.dhm_file != NULL )
1489 ret = ssl_set_dh_param_ctx( &ssl, &dhm );
1490 else
1491#endif
1492 ret = ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
1493 POLARSSL_DHM_RFC5114_MODP_2048_G );
1494
1495 if( ret != 0 )
1496 {
1497 printf( " failed\n ssl_set_dh_param returned -0x%04X\n\n", - ret );
1498 goto exit;
1499 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001500#endif
1501
Paul Bakker1d29fb52012-09-28 13:28:45 +00001502 if( opt.min_version != -1 )
1503 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1504
Paul Bakkerc1516be2013-06-29 16:01:32 +02001505 if( opt.max_version != -1 )
1506 ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1507
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001508 printf( " ok\n" );
1509
1510reset:
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +02001511#if !defined(_WIN32)
1512 if( received_sigterm )
1513 {
1514 printf( " interrupted by SIGTERM\n" );
1515 ret = 0;
1516 goto exit;
1517 }
1518#endif
1519
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001520#ifdef POLARSSL_ERROR_C
1521 if( ret != 0 )
1522 {
1523 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001524 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001525 printf("Last error was: %d - %s\n\n", ret, error_buf );
1526 }
1527#endif
1528
1529 if( client_fd != -1 )
1530 net_close( client_fd );
1531
1532 ssl_session_reset( &ssl );
1533
1534 /*
1535 * 3. Wait until a client connects
1536 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001537 client_fd = -1;
1538
1539 printf( " . Waiting for a remote connection ..." );
1540 fflush( stdout );
1541
1542 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
1543 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02001544#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001545 if( received_sigterm )
1546 {
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001547 printf( " interrupted by signal\n" );
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001548 ret = 0;
1549 goto exit;
1550 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02001551#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001552
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001553 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
1554 goto exit;
1555 }
1556
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001557 if( opt.nbio > 0 )
1558 ret = net_set_nonblock( client_fd );
1559 else
1560 ret = net_set_block( client_fd );
1561 if( ret != 0 )
1562 {
1563 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
1564 goto exit;
1565 }
1566
1567 if( opt.nbio == 2 )
1568 ssl_set_bio( &ssl, my_recv, &client_fd, my_send, &client_fd );
1569 else
1570 ssl_set_bio( &ssl, net_recv, &client_fd, net_send, &client_fd );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001571
1572 printf( " ok\n" );
1573
1574 /*
1575 * 4. Handshake
1576 */
1577 printf( " . Performing the SSL/TLS handshake..." );
1578 fflush( stdout );
1579
1580 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1581 {
1582 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1583 {
1584 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001585 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001586 }
1587 }
1588
Manuel Pégourié-Gonnardc580a002014-02-12 10:15:30 +01001589 printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1590 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001591
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001592#if defined(POLARSSL_SSL_ALPN)
1593 if( opt.alpn_string != NULL )
1594 {
1595 const char *alp = ssl_get_alpn_protocol( &ssl );
1596 printf( " [ Application Layer Protocol is %s ]\n",
1597 alp ? alp : "(none)" );
1598 }
1599#endif
1600
Paul Bakker36713e82013-09-17 13:25:29 +02001601#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001602 /*
1603 * 5. Verify the server certificate
1604 */
1605 printf( " . Verifying peer X.509 certificate..." );
1606
1607 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1608 {
1609 printf( " failed\n" );
1610
Paul Bakkerb0550d92012-10-30 07:51:03 +00001611 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001612 printf( " ! no client certificate sent\n" );
1613
1614 if( ( ret & BADCERT_EXPIRED ) != 0 )
1615 printf( " ! client certificate has expired\n" );
1616
1617 if( ( ret & BADCERT_REVOKED ) != 0 )
1618 printf( " ! client certificate has been revoked\n" );
1619
1620 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
1621 printf( " ! self-signed or not signed by a trusted CA\n" );
1622
1623 printf( "\n" );
1624 }
1625 else
1626 printf( " ok\n" );
1627
Paul Bakkerb0550d92012-10-30 07:51:03 +00001628 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001629 {
1630 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001631 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1632 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001633 printf( "%s\n", buf );
1634 }
Paul Bakker36713e82013-09-17 13:25:29 +02001635#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001636
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001637 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001638data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001639 /*
1640 * 6. Read the HTTP Request
1641 */
1642 printf( " < Read from client:" );
1643 fflush( stdout );
1644
1645 do
1646 {
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001647 int terminated = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001648 len = sizeof( buf ) - 1;
1649 memset( buf, 0, sizeof( buf ) );
1650 ret = ssl_read( &ssl, buf, len );
1651
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001652 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1653 ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001654 continue;
1655
1656 if( ret <= 0 )
1657 {
1658 switch( ret )
1659 {
1660 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1661 printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001662 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001663
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001664 case 0:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001665 case POLARSSL_ERR_NET_CONN_RESET:
1666 printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001667 ret = POLARSSL_ERR_NET_CONN_RESET;
1668 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001669
1670 default:
1671 printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001672 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001673 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001674 }
1675
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001676 if( ssl_get_bytes_avail( &ssl ) == 0 )
1677 {
1678 len = ret;
1679 buf[len] = '\0';
1680 printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001681
1682 /* End of message should be detected according to the syntax of the
1683 * application protocol (eg HTTP), just use a dummy test here. */
1684 if( buf[len - 1] == '\n' )
1685 terminated = 1;
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001686 }
1687 else
1688 {
1689 int extra_len, ori_len;
1690 unsigned char *larger_buf;
1691
1692 ori_len = ret;
1693 extra_len = ssl_get_bytes_avail( &ssl );
1694
1695 larger_buf = polarssl_malloc( ori_len + extra_len + 1 );
1696 if( larger_buf == NULL )
1697 {
1698 printf( " ! memory allocation failed\n" );
1699 ret = 1;
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001700 goto reset;
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001701 }
1702
1703 memset( larger_buf, 0, ori_len + extra_len );
1704 memcpy( larger_buf, buf, ori_len );
1705
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +02001706 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001707 ret = ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +02001708 if( ret != extra_len ||
1709 ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001710 {
1711 printf( " ! ssl_read failed on cached data\n" );
1712 ret = 1;
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001713 goto reset;
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001714 }
1715
1716 larger_buf[ori_len + extra_len] = '\0';
1717 printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnard0669f272014-06-18 13:07:20 +02001718 ori_len + extra_len, ori_len, extra_len,
1719 (char *) larger_buf );
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001720
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001721 /* End of message should be detected according to the syntax of the
1722 * application protocol (eg HTTP), just use a dummy test here. */
1723 if( larger_buf[ori_len + extra_len - 1] == '\n' )
1724 terminated = 1;
1725
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001726 polarssl_free( larger_buf );
1727 }
1728
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001729 if( terminated )
1730 {
1731 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001732 break;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001733 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001734 }
1735 while( 1 );
1736
1737 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001738 * 7a. Request renegotiation while client is waiting for input from us.
1739 * (only if we're going to exhange more data afterwards)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001740 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001741#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001742 if( opt.renegotiate && exchanges_left > 1 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001743 {
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001744 printf( " . Requestion renegotiation..." );
1745 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001746
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001747 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
1748 {
1749 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1750 ret != POLARSSL_ERR_NET_WANT_WRITE )
1751 {
1752 printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
1753 goto reset;
1754 }
1755 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001756
1757 printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001758 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001759#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001760
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001761 /*
1762 * 7. Write the 200 Response
1763 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001764 printf( " > Write to client:" );
1765 fflush( stdout );
1766
1767 len = sprintf( (char *) buf, HTTP_RESPONSE,
1768 ssl_get_ciphersuite( &ssl ) );
1769
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001770 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001771 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001772 while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001773 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001774 if( ret == POLARSSL_ERR_NET_CONN_RESET )
1775 {
1776 printf( " failed\n ! peer closed the connection\n\n" );
1777 goto reset;
1778 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001779
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001780 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1781 {
1782 printf( " failed\n ! ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001783 goto reset;
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001784 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001785 }
1786 }
1787
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001788 buf[written] = '\0';
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001789 printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Manuel Pégourié-Gonnarda92ed482015-01-14 10:46:08 +01001790 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001791
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001792 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001793 * 7b. Continue doing data exchanges?
1794 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001795 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001796 goto data_exchange;
1797
1798 /*
1799 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001800 */
1801close_notify:
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001802 printf( " . Closing the connection..." );
1803
Manuel Pégourié-Gonnard34377b12015-01-22 10:46:46 +00001804 /* No error checking, the connection might be closed already */
1805 do ret = ssl_close_notify( &ssl );
1806 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
1807 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001808
Manuel Pégourié-Gonnard687f89b2015-01-13 21:48:12 +01001809 printf( " done\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001810 goto reset;
1811
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001812 /*
1813 * Cleanup and exit
1814 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001815exit:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001816#ifdef POLARSSL_ERROR_C
1817 if( ret != 0 )
1818 {
1819 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001820 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001821 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
1822 }
1823#endif
1824
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01001825 printf( " . Cleaning up..." );
1826 fflush( stdout );
1827
Paul Bakker0c226102014-04-17 16:02:36 +02001828 if( client_fd != -1 )
1829 net_close( client_fd );
1830
Paul Bakkera317a982014-06-18 16:44:11 +02001831#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1832 dhm_free( &dhm );
1833#endif
Paul Bakker36713e82013-09-17 13:25:29 +02001834#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker36713e82013-09-17 13:25:29 +02001835 x509_crt_free( &cacert );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001836 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02001837 pk_free( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001838 x509_crt_free( &srvcert2 );
1839 pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02001840#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001841#if defined(POLARSSL_SNI)
1842 sni_free( sni_info );
1843#endif
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02001844#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1845 psk_free( psk_info );
1846#endif
1847#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1848 dhm_free( &dhm );
1849#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001850
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001851 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02001852 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02001853 entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001854
Paul Bakker0a597072012-09-25 21:55:46 +00001855#if defined(POLARSSL_SSL_CACHE_C)
1856 ssl_cache_free( &cache );
1857#endif
1858
Paul Bakker1337aff2013-09-29 14:45:34 +02001859#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1860#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker82024bf2013-07-04 11:52:32 +02001861 memory_buffer_alloc_status();
1862#endif
Paul Bakker1337aff2013-09-29 14:45:34 +02001863 memory_buffer_alloc_free();
1864#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02001865
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01001866 printf( " done.\n" );
1867
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001868#if defined(_WIN32)
1869 printf( " + Press Enter to exit this program.\n" );
1870 fflush( stdout ); getchar();
1871#endif
1872
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001873 // Shell can not handle large exit numbers -> 1 for errors
1874 if( ret < 0 )
1875 ret = 1;
1876
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001877 return( ret );
1878}
1879#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
1880 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
1881 POLARSSL_CTR_DRBG_C */