blob: fb2327a9b7afa574d8c95da044b6f2cb7fcdaf65 [file] [log] [blame]
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001/*
2 * SSL client with options
3 *
Paul Bakker3c5ef712013-06-25 16:37:45 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000031
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020032#if !defined(POLARSSL_ENTROPY_C) || \
33 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
34 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
35#include <stdio.h>
36int main( int argc, char *argv[] )
37{
38 ((void) argc);
39 ((void) argv);
40
41 printf("POLARSSL_ENTROPY_C and/or "
42 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
43 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
44 return( 0 );
45}
46#else
47
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010048#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) && defined(POLARSSL_FS_IO)
49#define POLARSSL_SNI
50#endif
51
52#if defined(POLARSSL_PLATFORM_C)
53#include "polarssl/platform.h"
54#else
55#define polarssl_malloc malloc
56#define polarssl_free free
57#endif
58
Paul Bakkerb60b95f2012-09-25 09:05:17 +000059#if defined(_WIN32)
60#include <windows.h>
61#endif
62
63#include <string.h>
64#include <stdlib.h>
65#include <stdio.h>
Paul Bakkerc1283d32014-08-18 11:05:51 +020066
67#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +020068#include <signal.h>
Paul Bakkerc1283d32014-08-18 11:05:51 +020069#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000070
Paul Bakkerb60b95f2012-09-25 09:05:17 +000071#include "polarssl/net.h"
72#include "polarssl/ssl.h"
73#include "polarssl/entropy.h"
74#include "polarssl/ctr_drbg.h"
75#include "polarssl/certs.h"
76#include "polarssl/x509.h"
77#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020078#include "polarssl/debug.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000079
Paul Bakker0a597072012-09-25 21:55:46 +000080#if defined(POLARSSL_SSL_CACHE_C)
81#include "polarssl/ssl_cache.h"
82#endif
83
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +020084#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020085#include "polarssl/ssl_cookie.h"
86#endif
87
Paul Bakker82024bf2013-07-04 11:52:32 +020088#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
89#include "polarssl/memory.h"
90#endif
91
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +010092#define DFL_SERVER_ADDR NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000093#define DFL_SERVER_PORT 4433
Paul Bakkerb60b95f2012-09-25 09:05:17 +000094#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010095#define DFL_NBIO 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020096#define DFL_READ_TIMEOUT 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +000097#define DFL_CA_FILE ""
98#define DFL_CA_PATH ""
99#define DFL_CRT_FILE ""
100#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200101#define DFL_CRT_FILE2 ""
102#define DFL_KEY_FILE2 ""
Paul Bakkerfbb17802013-04-17 19:10:21 +0200103#define DFL_PSK ""
104#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200105#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000106#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200107#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +0200108#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100109#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100110#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200111#define DFL_RENEGO_DELAY -2
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100112#define DFL_RENEGO_PERIOD -1
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200113#define DFL_EXCHANGES 1
Paul Bakker1d29fb52012-09-28 13:28:45 +0000114#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200115#define DFL_MAX_VERSION -1
Paul Bakker91ebfb52012-11-23 14:04:08 +0100116#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200117#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200118#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100119#define DFL_TICKET_TIMEOUT -1
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100120#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100121#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100122#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200123#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200124#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100125#define DFL_TRANSPORT SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200126#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200127#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200128#define DFL_HS_TO_MIN 0
129#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200130#define DFL_BADMAC_LIMIT -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200131#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100132#define DFL_ETM -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000133
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200134#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
135 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
136 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
137 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
138 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
139 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
140 "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 +0200141
Paul Bakker8e714d72013-07-18 11:05:13 +0200142/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
143 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000144#define HTTP_RESPONSE \
145 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
146 "<h2>PolarSSL Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200147 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000148
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200149/*
150 * Size of the basic I/O buffer. Able to hold our default response.
151 *
152 * You will need to adapt the ssl_get_bytes_avail() test in ssl-opt.sh
153 * if you change this value to something outside the range <= 100 or > 500
154 */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200155#define IO_BUF_LEN 200
156
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000157/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000158 * global options
159 */
160struct options
161{
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100162 const char *server_addr; /* address on which the ssl service runs */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000163 int server_port; /* port on which the ssl service runs */
164 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100165 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200166 uint32_t read_timeout; /* timeout on ssl_read() in milliseconds */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200167 const char *ca_file; /* the file with the CA certificate(s) */
168 const char *ca_path; /* the path with the CA certificate(s) reside */
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200169 const char *crt_file; /* the file with the server certificate */
170 const char *key_file; /* the file with the server key */
171 const char *crt_file2; /* the file with the 2nd server certificate */
172 const char *key_file2; /* the file with the 2nd server key */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200173 const char *psk; /* the pre-shared key */
174 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200175 char *psk_list; /* list of PSK id/key pairs for callback */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000176 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200177 const char *version_suites; /* per-version ciphersuites */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000178 int renegotiation; /* enable / disable renegotiation */
179 int allow_legacy; /* allow legacy renegotiation */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100180 int renegotiate; /* attempt renegotiation? */
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200181 int renego_delay; /* delay before enforcing renegotiation */
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100182 int renego_period; /* period for automatic renegotiation */
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200183 int exchanges; /* number of data exchanges */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000184 int min_version; /* minimum protocol version accepted */
Paul Bakkerc1516be2013-06-29 16:01:32 +0200185 int max_version; /* maximum protocol version accepted */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100186 int auth_mode; /* verify mode for connection */
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200187 unsigned char mfl_code; /* code for maximum fragment length */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200188 int tickets; /* enable / disable session tickets */
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100189 int ticket_timeout; /* session ticket lifetime */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100190 int cache_max; /* max number of session cache entries */
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100191 int cache_timeout; /* expiration delay of session cache entries */
Paul Bakker1ebc0c52014-05-22 15:47:58 +0200192 char *sni; /* string describing sni information */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200193 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200194 const char *dhm_file; /* the file with the DH parameters */
Paul Bakkerb2eaac12015-01-13 17:15:31 +0100195 int extended_ms; /* allow negotiation of extended MS? */
196 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100197 int transport; /* TLS or DTLS? */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200198 int cookies; /* Use cookies for DTLS? -1 to break them */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200199 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200200 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
201 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200202 int badmac_limit; /* Limit of records with bad MAC */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000203} opt;
204
Paul Bakker3c5ef712013-06-25 16:37:45 +0200205static void my_debug( void *ctx, int level, const char *str )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000206{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200207 ((void) level);
208
209 fprintf( (FILE *) ctx, "%s", str );
210 fflush( (FILE *) ctx );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000211}
212
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100213/*
214 * Test recv/send functions that make sure each try returns
215 * WANT_READ/WANT_WRITE at least once before sucesseding
216 */
217static int my_recv( void *ctx, unsigned char *buf, size_t len )
218{
219 static int first_try = 1;
220 int ret;
221
222 if( first_try )
223 {
224 first_try = 0;
225 return( POLARSSL_ERR_NET_WANT_READ );
226 }
227
228 ret = net_recv( ctx, buf, len );
229 if( ret != POLARSSL_ERR_NET_WANT_READ )
230 first_try = 1; /* Next call will be a new operation */
231 return( ret );
232}
233
234static int my_send( void *ctx, const unsigned char *buf, size_t len )
235{
236 static int first_try = 1;
237 int ret;
238
239 if( first_try )
240 {
241 first_try = 0;
242 return( POLARSSL_ERR_NET_WANT_WRITE );
243 }
244
245 ret = net_send( ctx, buf, len );
246 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
247 first_try = 1; /* Next call will be a new operation */
248 return( ret );
249}
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200250
251#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000252#if defined(POLARSSL_FS_IO)
253#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100254 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
255 " default: \"\" (pre-loaded)\n" \
256 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
257 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
258 " 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 +0200259 " default: see note after key_file2\n" \
260 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200261 " 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 +0200262 " default: see note after key_file2\n" \
263 " key_file2=%%s default: see note below\n" \
264 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200265 " preloaded certificate(s) and key(s) are used if available\n" \
266 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
267 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000268#else
269#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200270 "\n" \
271 " No file operations available (POLARSSL_FS_IO not defined)\n" \
272 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000273#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200274#else
275#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200276#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200277
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200278#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200279#define USAGE_PSK \
280 " psk=%%s default: \"\" (in hex, without 0x)\n" \
281 " psk_identity=%%s default: \"Client_identity\"\n"
282#else
283#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200284#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000285
Paul Bakkera503a632013-08-14 13:48:06 +0200286#if defined(POLARSSL_SSL_SESSION_TICKETS)
287#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100288 " tickets=%%d default: 1 (enabled)\n" \
289 " ticket_timeout=%%d default: ticket default (1d)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200290#else
291#define USAGE_TICKETS ""
292#endif /* POLARSSL_SSL_SESSION_TICKETS */
293
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100294#if defined(POLARSSL_SSL_CACHE_C)
295#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100296 " cache_max=%%d default: cache default (50)\n" \
297 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100298#else
299#define USAGE_CACHE ""
300#endif /* POLARSSL_SSL_CACHE_C */
301
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100302#if defined(POLARSSL_SNI)
303#define USAGE_SNI \
304 " sni=%%s name1,cert1,key1[,name2,cert2,key2[,...]]\n" \
305 " default: disabled\n"
306#else
307#define USAGE_SNI ""
308#endif /* POLARSSL_SNI */
309
Paul Bakker05decb22013-08-15 13:33:48 +0200310#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
311#define USAGE_MAX_FRAG_LEN \
312 " max_frag_len=%%d default: 16384 (tls default)\n" \
313 " options: 512, 1024, 2048, 4096\n"
314#else
315#define USAGE_MAX_FRAG_LEN ""
316#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
317
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200318#if defined(POLARSSL_SSL_ALPN)
319#define USAGE_ALPN \
320 " alpn=%%s default: \"\" (disabled)\n" \
321 " example: spdy/1,http/1.1\n"
322#else
323#define USAGE_ALPN ""
324#endif /* POLARSSL_SSL_ALPN */
325
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200326#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
327#define USAGE_COOKIES \
328 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200329 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200330#else
331#define USAGE_COOKIES ""
332#endif
333
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200334#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
335#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200336 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200337#else
338#define USAGE_ANTI_REPLAY ""
339#endif
340
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200341#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
342#define USAGE_BADMAC_LIMIT \
343 " badmac_limit=%%d default: (library default: disabled)\n"
344#else
345#define USAGE_BADMAC_LIMIT ""
346#endif
347
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200348#if defined(POLARSSL_SSL_PROTO_DTLS)
349#define USAGE_DTLS \
350 " dtls=%%d default: 0 (TLS)\n" \
351 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
352 " range of DTLS handshake timeouts in millisecs\n"
353#else
354#define USAGE_DTLS ""
355#endif
356
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200357#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
358#define USAGE_EMS \
359 " extended_ms=0/1 default: (library default: on)\n"
360#else
361#define USAGE_EMS ""
362#endif
363
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100364#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
365#define USAGE_ETM \
366 " etm=0/1 default: (library default: on)\n"
367#else
368#define USAGE_ETM ""
369#endif
370
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100371#if defined(POLARSSL_SSL_RENEGOTIATION)
372#define USAGE_RENEGO \
373 " renegotiation=%%d default: 0 (disabled)\n" \
374 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100375 " renego_delay=%%d default: -2 (library default)\n" \
376 " renego_period=%%d default: (library default)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100377#else
378#define USAGE_RENEGO ""
379#endif
380
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000381#define USAGE \
382 "\n usage: ssl_server2 param=<>...\n" \
383 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100384 " server_addr=%%d default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000385 " server_port=%%d default: 4433\n" \
386 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100387 " nbio=%%d default: 0 (blocking I/O)\n" \
388 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200389 " read_timeout=%%d default: 0 (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100390 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200391 USAGE_DTLS \
392 USAGE_COOKIES \
393 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200394 USAGE_BADMAC_LIMIT \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200395 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100396 " auth_mode=%%s default: \"optional\"\n" \
397 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000398 USAGE_IO \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100399 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100400 "\n" \
401 USAGE_PSK \
402 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100403 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100404 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200405 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200406 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100407 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100408 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100409 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200410 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200411 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100412 USAGE_ETM \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100413 "\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000414 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200415 " max_version=%%s default: \"tls1_2\"\n" \
416 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100417 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200418 "\n" \
419 " version_suites=a,b,c,d per-version ciphersuites\n" \
420 " in order from ssl3 to tls1_2\n" \
421 " default: all enabled\n" \
422 " force_ciphersuite=<name> default: all enabled\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000423 " acceptable ciphersuite names:\n"
424
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200425/*
426 * Used by sni_parse and psk_parse to handle coma-separated lists
427 */
428#define GET_ITEM( dst ) \
429 dst = p; \
430 while( *p != ',' ) \
431 if( ++p > end ) \
432 return( NULL ); \
433 *p++ = '\0';
434
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100435#if defined(POLARSSL_SNI)
436typedef struct _sni_entry sni_entry;
437
438struct _sni_entry {
439 const char *name;
440 x509_crt *cert;
441 pk_context *key;
442 sni_entry *next;
443};
444
445/*
446 * Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]]
447 * into a usable sni_entry list.
448 *
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200449 * Modifies the input string! This is not production quality!
450 * (leaks memory if parsing fails, no error reporting, ...)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100451 */
452sni_entry *sni_parse( char *sni_string )
453{
454 sni_entry *cur = NULL, *new = NULL;
455 char *p = sni_string;
456 char *end = p;
457 char *crt_file, *key_file;
458
459 while( *end != '\0' )
460 ++end;
461 *end = ',';
462
463 while( p <= end )
464 {
465 if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL )
466 return( NULL );
467
468 memset( new, 0, sizeof( sni_entry ) );
469
470 if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ||
471 ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200472 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100473
474 x509_crt_init( new->cert );
475 pk_init( new->key );
476
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200477 GET_ITEM( new->name );
478 GET_ITEM( crt_file );
479 GET_ITEM( key_file );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100480
481 if( x509_crt_parse_file( new->cert, crt_file ) != 0 ||
482 pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200483 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100484
485 new->next = cur;
486 cur = new;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100487 }
488
489 return( cur );
490}
491
492void sni_free( sni_entry *head )
493{
494 sni_entry *cur = head, *next;
495
496 while( cur != NULL )
497 {
498 x509_crt_free( cur->cert );
499 polarssl_free( cur->cert );
500
501 pk_free( cur->key );
502 polarssl_free( cur->key );
503
504 next = cur->next;
505 polarssl_free( cur );
506 cur = next;
507 }
508}
509
510/*
511 * SNI callback.
512 */
513int sni_callback( void *p_info, ssl_context *ssl,
514 const unsigned char *name, size_t name_len )
515{
516 sni_entry *cur = (sni_entry *) p_info;
517
518 while( cur != NULL )
519 {
520 if( name_len == strlen( cur->name ) &&
521 memcmp( name, cur->name, name_len ) == 0 )
522 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200523 return( ssl_set_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100524 }
525
526 cur = cur->next;
527 }
528
529 return( -1 );
530}
531
532#endif /* POLARSSL_SNI */
533
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200534#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
535
536#define HEX2NUM( c ) \
537 if( c >= '0' && c <= '9' ) \
538 c -= '0'; \
539 else if( c >= 'a' && c <= 'f' ) \
540 c -= 'a' - 10; \
541 else if( c >= 'A' && c <= 'F' ) \
542 c -= 'A' - 10; \
543 else \
544 return( -1 );
545
546/*
547 * Convert a hex string to bytes.
548 * Return 0 on success, -1 on error.
549 */
550int unhexify( unsigned char *output, const char *input, size_t *olen )
551{
552 unsigned char c;
553 size_t j;
554
555 *olen = strlen( input );
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200556 if( *olen % 2 != 0 || *olen / 2 > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200557 return( -1 );
558 *olen /= 2;
559
560 for( j = 0; j < *olen * 2; j += 2 )
561 {
562 c = input[j];
563 HEX2NUM( c );
564 output[ j / 2 ] = c << 4;
565
566 c = input[j + 1];
567 HEX2NUM( c );
568 output[ j / 2 ] |= c;
569 }
570
571 return( 0 );
572}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200573
574typedef struct _psk_entry psk_entry;
575
576struct _psk_entry
577{
578 const char *name;
579 size_t key_len;
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200580 unsigned char key[POLARSSL_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200581 psk_entry *next;
582};
583
584/*
585 * Parse a string of pairs name1,key1[,name2,key2[,...]]
586 * into a usable psk_entry list.
587 *
588 * Modifies the input string! This is not production quality!
589 * (leaks memory if parsing fails, no error reporting, ...)
590 */
591psk_entry *psk_parse( char *psk_string )
592{
593 psk_entry *cur = NULL, *new = NULL;
594 char *p = psk_string;
595 char *end = p;
596 char *key_hex;
597
598 while( *end != '\0' )
599 ++end;
600 *end = ',';
601
602 while( p <= end )
603 {
604 if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL )
605 return( NULL );
606
607 memset( new, 0, sizeof( psk_entry ) );
608
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200609 GET_ITEM( new->name );
610 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200611
612 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
613 return( NULL );
614
615 new->next = cur;
616 cur = new;
617 }
618
619 return( cur );
620}
621
622/*
623 * Free a list of psk_entry's
624 */
625void psk_free( psk_entry *head )
626{
627 psk_entry *next;
628
629 while( head != NULL )
630 {
631 next = head->next;
632 polarssl_free( head );
633 head = next;
634 }
635}
636
637/*
638 * PSK callback
639 */
640int psk_callback( void *p_info, ssl_context *ssl,
641 const unsigned char *name, size_t name_len )
642{
643 psk_entry *cur = (psk_entry *) p_info;
644
645 while( cur != NULL )
646 {
647 if( name_len == strlen( cur->name ) &&
648 memcmp( name, cur->name, name_len ) == 0 )
649 {
650 return( ssl_set_psk( ssl, cur->key, cur->key_len,
651 name, name_len ) );
652 }
653
654 cur = cur->next;
655 }
656
657 return( -1 );
658}
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200659#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
660
Manuel Pégourié-Gonnarda9d7d032014-10-09 16:07:08 +0200661static int listen_fd, client_fd = -1;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200662
663/* Interruption handler to ensure clean exit (for valgrind testing) */
664#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200665static int received_sigterm = 0;
666void term_handler( int sig )
667{
668 ((void) sig);
669 received_sigterm = 1;
670 net_close( listen_fd ); /* causes net_accept() to abort */
Manuel Pégourié-Gonnarda9d7d032014-10-09 16:07:08 +0200671 net_close( client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200672}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200673#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200674
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000675int main( int argc, char *argv[] )
676{
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200677 int ret = 0, len, written, frags, exchanges;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200678 int version_suites[4][2];
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200679 unsigned char buf[IO_BUF_LEN];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200680#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200681 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +0200682 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +0200683 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +0200684#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200685 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +0200686 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +0200687#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200688 ssl_cookie_ctx cookie_ctx;
689#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000690
691 entropy_context entropy;
692 ctr_drbg_context ctr_drbg;
693 ssl_context ssl;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100694#if defined(POLARSSL_SSL_RENEGOTIATION)
695 unsigned char renego_period[8] = { 0 };
696#endif
Paul Bakker36713e82013-09-17 13:25:29 +0200697#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200698 x509_crt cacert;
699 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200700 pk_context pkey;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200701 x509_crt srvcert2;
702 pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200703 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200704#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200705#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
706 dhm_context dhm;
707#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000708#if defined(POLARSSL_SSL_CACHE_C)
709 ssl_cache_context cache;
710#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100711#if defined(POLARSSL_SNI)
712 sni_entry *sni_info = NULL;
713#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200714#if defined(POLARSSL_SSL_ALPN)
715 const char *alpn_list[10];
716#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200717#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
718 unsigned char alloc_buf[100000];
719#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000720
721 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000722 char *p, *q;
723 const int *list;
724
Paul Bakker82024bf2013-07-04 11:52:32 +0200725#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
726 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
727#endif
728
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000729 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200730 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000731 */
732 listen_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200733 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200734#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200735 x509_crt_init( &cacert );
736 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200737 pk_init( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200738 x509_crt_init( &srvcert2 );
739 pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200740#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200741#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
Paul Bakkera317a982014-06-18 16:44:11 +0200742 dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200743#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000744#if defined(POLARSSL_SSL_CACHE_C)
745 ssl_cache_init( &cache );
746#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200747#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200748 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200749#endif
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +0200750#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200751 ssl_cookie_init( &cookie_ctx );
752#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000753
Paul Bakkerc1283d32014-08-18 11:05:51 +0200754#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100755 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200756 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100757 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +0200758#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200759
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000760 if( argc == 0 )
761 {
762 usage:
763 if( ret == 0 )
764 ret = 1;
765
766 printf( USAGE );
767
768 list = ssl_list_ciphersuites();
769 while( *list )
770 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200771 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200772 list++;
773 if( !*list )
774 break;
775 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000776 list++;
777 }
778 printf("\n");
779 goto exit;
780 }
781
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100782 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000783 opt.server_port = DFL_SERVER_PORT;
784 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100785 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200786 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000787 opt.ca_file = DFL_CA_FILE;
788 opt.ca_path = DFL_CA_PATH;
789 opt.crt_file = DFL_CRT_FILE;
790 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200791 opt.crt_file2 = DFL_CRT_FILE2;
792 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200793 opt.psk = DFL_PSK;
794 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200795 opt.psk_list = DFL_PSK_LIST;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000796 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200797 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000798 opt.renegotiation = DFL_RENEGOTIATION;
799 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100800 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200801 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100802 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200803 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000804 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200805 opt.max_version = DFL_MAX_VERSION;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100806 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200807 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200808 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100809 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100810 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100811 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100812 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200813 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200814 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100815 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200816 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200817 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200818 opt.hs_to_min = DFL_HS_TO_MIN;
819 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200820 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200821 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100822 opt.etm = DFL_ETM;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000823
824 for( i = 1; i < argc; i++ )
825 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000826 p = argv[i];
827 if( ( q = strchr( p, '=' ) ) == NULL )
828 goto usage;
829 *q++ = '\0';
830
831 if( strcmp( p, "server_port" ) == 0 )
832 {
833 opt.server_port = atoi( q );
834 if( opt.server_port < 1 || opt.server_port > 65535 )
835 goto usage;
836 }
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100837 else if( strcmp( p, "server_addr" ) == 0 )
838 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100839 else if( strcmp( p, "dtls" ) == 0 )
840 {
841 int t = atoi( q );
842 if( t == 0 )
843 opt.transport = SSL_TRANSPORT_STREAM;
844 else if( t == 1 )
845 opt.transport = SSL_TRANSPORT_DATAGRAM;
846 else
847 goto usage;
848 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000849 else if( strcmp( p, "debug_level" ) == 0 )
850 {
851 opt.debug_level = atoi( q );
852 if( opt.debug_level < 0 || opt.debug_level > 65535 )
853 goto usage;
854 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100855 else if( strcmp( p, "nbio" ) == 0 )
856 {
857 opt.nbio = atoi( q );
858 if( opt.nbio < 0 || opt.nbio > 2 )
859 goto usage;
860 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200861 else if( strcmp( p, "read_timeout" ) == 0 )
862 opt.read_timeout = atoi( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000863 else if( strcmp( p, "ca_file" ) == 0 )
864 opt.ca_file = q;
865 else if( strcmp( p, "ca_path" ) == 0 )
866 opt.ca_path = q;
867 else if( strcmp( p, "crt_file" ) == 0 )
868 opt.crt_file = q;
869 else if( strcmp( p, "key_file" ) == 0 )
870 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200871 else if( strcmp( p, "crt_file2" ) == 0 )
872 opt.crt_file2 = q;
873 else if( strcmp( p, "key_file2" ) == 0 )
874 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200875 else if( strcmp( p, "dhm_file" ) == 0 )
876 opt.dhm_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200877 else if( strcmp( p, "psk" ) == 0 )
878 opt.psk = q;
879 else if( strcmp( p, "psk_identity" ) == 0 )
880 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200881 else if( strcmp( p, "psk_list" ) == 0 )
882 opt.psk_list = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000883 else if( strcmp( p, "force_ciphersuite" ) == 0 )
884 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000885 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
886
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200887 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000888 {
889 ret = 2;
890 goto usage;
891 }
892 opt.force_ciphersuite[1] = 0;
893 }
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200894 else if( strcmp( p, "version_suites" ) == 0 )
895 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000896 else if( strcmp( p, "renegotiation" ) == 0 )
897 {
898 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
899 SSL_RENEGOTIATION_DISABLED;
900 }
901 else if( strcmp( p, "allow_legacy" ) == 0 )
902 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100903 switch( atoi( q ) )
904 {
905 case -1: opt.allow_legacy = SSL_LEGACY_BREAK_HANDSHAKE; break;
906 case 0: opt.allow_legacy = SSL_LEGACY_NO_RENEGOTIATION; break;
907 case 1: opt.allow_legacy = SSL_LEGACY_ALLOW_RENEGOTIATION; break;
908 default: goto usage;
909 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000910 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100911 else if( strcmp( p, "renegotiate" ) == 0 )
912 {
913 opt.renegotiate = atoi( q );
914 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
915 goto usage;
916 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200917 else if( strcmp( p, "renego_delay" ) == 0 )
918 {
919 opt.renego_delay = atoi( q );
920 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100921 else if( strcmp( p, "renego_period" ) == 0 )
922 {
923 opt.renego_period = atoi( q );
924 if( opt.renego_period < 2 || opt.renego_period > 255 )
925 goto usage;
926 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200927 else if( strcmp( p, "exchanges" ) == 0 )
928 {
929 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +0200930 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200931 goto usage;
932 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000933 else if( strcmp( p, "min_version" ) == 0 )
934 {
935 if( strcmp( q, "ssl3" ) == 0 )
936 opt.min_version = SSL_MINOR_VERSION_0;
937 else if( strcmp( q, "tls1" ) == 0 )
938 opt.min_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100939 else if( strcmp( q, "tls1_1" ) == 0 ||
940 strcmp( q, "dtls1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000941 opt.min_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100942 else if( strcmp( q, "tls1_2" ) == 0 ||
943 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000944 opt.min_version = SSL_MINOR_VERSION_3;
945 else
946 goto usage;
947 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200948 else if( strcmp( p, "max_version" ) == 0 )
949 {
950 if( strcmp( q, "ssl3" ) == 0 )
951 opt.max_version = SSL_MINOR_VERSION_0;
952 else if( strcmp( q, "tls1" ) == 0 )
953 opt.max_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100954 else if( strcmp( q, "tls1_1" ) == 0 ||
955 strcmp( q, "dtls1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200956 opt.max_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100957 else if( strcmp( q, "tls1_2" ) == 0 ||
958 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200959 opt.max_version = SSL_MINOR_VERSION_3;
960 else
961 goto usage;
962 }
963 else if( strcmp( p, "force_version" ) == 0 )
964 {
965 if( strcmp( q, "ssl3" ) == 0 )
966 {
967 opt.min_version = SSL_MINOR_VERSION_0;
968 opt.max_version = SSL_MINOR_VERSION_0;
969 }
970 else if( strcmp( q, "tls1" ) == 0 )
971 {
972 opt.min_version = SSL_MINOR_VERSION_1;
973 opt.max_version = SSL_MINOR_VERSION_1;
974 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100975 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200976 {
977 opt.min_version = SSL_MINOR_VERSION_2;
978 opt.max_version = SSL_MINOR_VERSION_2;
979 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100980 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200981 {
982 opt.min_version = SSL_MINOR_VERSION_3;
983 opt.max_version = SSL_MINOR_VERSION_3;
984 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100985 else if( strcmp( q, "dtls1" ) == 0 )
986 {
987 opt.min_version = SSL_MINOR_VERSION_2;
988 opt.max_version = SSL_MINOR_VERSION_2;
989 opt.transport = SSL_TRANSPORT_DATAGRAM;
990 }
991 else if( strcmp( q, "dtls1_2" ) == 0 )
992 {
993 opt.min_version = SSL_MINOR_VERSION_3;
994 opt.max_version = SSL_MINOR_VERSION_3;
995 opt.transport = SSL_TRANSPORT_DATAGRAM;
996 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200997 else
998 goto usage;
999 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001000 else if( strcmp( p, "auth_mode" ) == 0 )
1001 {
1002 if( strcmp( q, "none" ) == 0 )
1003 opt.auth_mode = SSL_VERIFY_NONE;
1004 else if( strcmp( q, "optional" ) == 0 )
1005 opt.auth_mode = SSL_VERIFY_OPTIONAL;
1006 else if( strcmp( q, "required" ) == 0 )
1007 opt.auth_mode = SSL_VERIFY_REQUIRED;
1008 else
1009 goto usage;
1010 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001011 else if( strcmp( p, "max_frag_len" ) == 0 )
1012 {
1013 if( strcmp( q, "512" ) == 0 )
1014 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
1015 else if( strcmp( q, "1024" ) == 0 )
1016 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
1017 else if( strcmp( q, "2048" ) == 0 )
1018 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
1019 else if( strcmp( q, "4096" ) == 0 )
1020 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
1021 else
1022 goto usage;
1023 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001024 else if( strcmp( p, "alpn" ) == 0 )
1025 {
1026 opt.alpn_string = q;
1027 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001028 else if( strcmp( p, "extended_ms" ) == 0 )
1029 {
1030 switch( atoi( q ) )
1031 {
1032 case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break;
1033 case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break;
1034 default: goto usage;
1035 }
1036 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001037 else if( strcmp( p, "etm" ) == 0 )
1038 {
1039 switch( atoi( q ) )
1040 {
1041 case 0: opt.etm = SSL_ETM_DISABLED; break;
1042 case 1: opt.etm = SSL_ETM_ENABLED; break;
1043 default: goto usage;
1044 }
1045 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001046 else if( strcmp( p, "tickets" ) == 0 )
1047 {
1048 opt.tickets = atoi( q );
1049 if( opt.tickets < 0 || opt.tickets > 1 )
1050 goto usage;
1051 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001052 else if( strcmp( p, "ticket_timeout" ) == 0 )
1053 {
1054 opt.ticket_timeout = atoi( q );
1055 if( opt.ticket_timeout < 0 )
1056 goto usage;
1057 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001058 else if( strcmp( p, "cache_max" ) == 0 )
1059 {
1060 opt.cache_max = atoi( q );
1061 if( opt.cache_max < 0 )
1062 goto usage;
1063 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001064 else if( strcmp( p, "cache_timeout" ) == 0 )
1065 {
1066 opt.cache_timeout = atoi( q );
1067 if( opt.cache_timeout < 0 )
1068 goto usage;
1069 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001070 else if( strcmp( p, "cookies" ) == 0 )
1071 {
1072 opt.cookies = atoi( q );
1073 if( opt.cookies < -1 || opt.cookies > 1)
1074 goto usage;
1075 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001076 else if( strcmp( p, "anti_replay" ) == 0 )
1077 {
1078 opt.anti_replay = atoi( q );
1079 if( opt.anti_replay < 0 || opt.anti_replay > 1)
1080 goto usage;
1081 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001082 else if( strcmp( p, "badmac_limit" ) == 0 )
1083 {
1084 opt.badmac_limit = atoi( q );
1085 if( opt.badmac_limit < 0 )
1086 goto usage;
1087 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001088 else if( strcmp( p, "hs_timeout" ) == 0 )
1089 {
1090 if( ( p = strchr( q, '-' ) ) == NULL )
1091 goto usage;
1092 *p++ = '\0';
1093 opt.hs_to_min = atoi( q );
1094 opt.hs_to_max = atoi( p );
1095 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1096 goto usage;
1097 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001098 else if( strcmp( p, "sni" ) == 0 )
1099 {
1100 opt.sni = q;
1101 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001102 else
1103 goto usage;
1104 }
1105
Paul Bakkerc73079a2014-04-25 16:34:30 +02001106#if defined(POLARSSL_DEBUG_C)
1107 debug_set_threshold( opt.debug_level );
1108#endif
1109
Paul Bakkerc1516be2013-06-29 16:01:32 +02001110 if( opt.force_ciphersuite[0] > 0 )
1111 {
1112 const ssl_ciphersuite_t *ciphersuite_info;
1113 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1114
Paul Bakker5b55b792013-07-19 13:43:43 +02001115 if( opt.max_version != -1 &&
1116 ciphersuite_info->min_minor_ver > opt.max_version )
1117 {
1118 printf("forced ciphersuite not allowed with this protocol version\n");
1119 ret = 2;
1120 goto usage;
1121 }
1122 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001123 ciphersuite_info->max_minor_ver < opt.min_version )
1124 {
1125 printf("forced ciphersuite not allowed with this protocol version\n");
1126 ret = 2;
1127 goto usage;
1128 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001129
1130 /* If we select a version that's not supported by
1131 * this suite, then there will be no common ciphersuite... */
1132 if( opt.max_version == -1 ||
1133 opt.max_version > ciphersuite_info->max_minor_ver )
1134 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001135 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001136 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001137 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001138 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001139 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001140 /* DTLS starts with TLS 1.1 */
1141 if( opt.transport == SSL_TRANSPORT_DATAGRAM &&
1142 opt.min_version < SSL_MINOR_VERSION_2 )
1143 opt.min_version = SSL_MINOR_VERSION_2;
1144 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001145 }
1146
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001147 if( opt.version_suites != NULL )
1148 {
1149 const char *name[4] = { 0 };
1150
1151 /* Parse 4-element coma-separated list */
1152 for( i = 0, p = (char *) opt.version_suites;
1153 i < 4 && *p != '\0';
1154 i++ )
1155 {
1156 name[i] = p;
1157
1158 /* Terminate the current string and move on to next one */
1159 while( *p != ',' && *p != '\0' )
1160 p++;
1161 if( *p == ',' )
1162 *p++ = '\0';
1163 }
1164
1165 if( i != 4 )
1166 {
1167 printf( "too few values for version_suites\n" );
1168 ret = 1;
1169 goto exit;
1170 }
1171
1172 memset( version_suites, 0, sizeof( version_suites ) );
1173
1174 /* Get the suites identifiers from their name */
1175 for( i = 0; i < 4; i++ )
1176 {
1177 version_suites[i][0] = ssl_get_ciphersuite_id( name[i] );
1178
1179 if( version_suites[i][0] == 0 )
1180 {
1181 printf( "unknown ciphersuite: '%s'\n", name[i] );
1182 ret = 2;
1183 goto usage;
1184 }
1185 }
1186 }
1187
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001188#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001189 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001190 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001191 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001192 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001193 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001194 printf( "pre-shared key not valid hex\n" );
1195 goto exit;
1196 }
1197
1198 if( opt.psk_list != NULL )
1199 {
1200 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001201 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001202 printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001203 goto exit;
1204 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001205 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001206#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001207
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001208#if defined(POLARSSL_SSL_ALPN)
1209 if( opt.alpn_string != NULL )
1210 {
1211 p = (char *) opt.alpn_string;
1212 i = 0;
1213
1214 /* Leave room for a final NULL in alpn_list */
1215 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
1216 {
1217 alpn_list[i++] = p;
1218
1219 /* Terminate the current string and move on to next one */
1220 while( *p != ',' && *p != '\0' )
1221 p++;
1222 if( *p == ',' )
1223 *p++ = '\0';
1224 }
1225 }
1226#endif /* POLARSSL_SSL_ALPN */
1227
Paul Bakkerfbb17802013-04-17 19:10:21 +02001228 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001229 * 0. Initialize the RNG and the session data
1230 */
1231 printf( "\n . Seeding the random number generator..." );
1232 fflush( stdout );
1233
1234 entropy_init( &entropy );
1235 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001236 (const unsigned char *) pers,
1237 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001238 {
1239 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
1240 goto exit;
1241 }
1242
1243 printf( " ok\n" );
1244
Paul Bakker36713e82013-09-17 13:25:29 +02001245#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001246 /*
1247 * 1.1. Load the trusted CA
1248 */
1249 printf( " . Loading the CA root certificate ..." );
1250 fflush( stdout );
1251
1252#if defined(POLARSSL_FS_IO)
1253 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001254 if( strcmp( opt.ca_path, "none" ) == 0 )
1255 ret = 0;
1256 else
1257 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001258 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001259 if( strcmp( opt.ca_file, "none" ) == 0 )
1260 ret = 0;
1261 else
1262 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001263 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001264#endif
1265#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +02001266 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
1267 strlen( test_ca_list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001268#else
1269 {
1270 ret = 1;
1271 printf("POLARSSL_CERTS_C not defined.");
1272 }
1273#endif
1274 if( ret < 0 )
1275 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001276 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001277 goto exit;
1278 }
1279
1280 printf( " ok (%d skipped)\n", ret );
1281
1282 /*
1283 * 1.2. Load own certificate and private key
1284 */
1285 printf( " . Loading the server cert. and key..." );
1286 fflush( stdout );
1287
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001288#if defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001289 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001290 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001291 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001292 if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
1293 {
1294 printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n",
1295 -ret );
1296 goto exit;
1297 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001298 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001299 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001300 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001301 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001302 if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
1303 {
1304 printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret );
1305 goto exit;
1306 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001307 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001308 if( key_cert_init == 1 )
1309 {
1310 printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
1311 goto exit;
1312 }
1313
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001314 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001315 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001316 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001317 if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
1318 {
1319 printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n",
1320 -ret );
1321 goto exit;
1322 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001323 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001324 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001325 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001326 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001327 if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
1328 {
1329 printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n",
1330 -ret );
1331 goto exit;
1332 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001333 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001334 if( key_cert_init2 == 1 )
1335 {
1336 printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
1337 goto exit;
1338 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001339#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001340 if( key_cert_init == 0 &&
1341 strcmp( opt.crt_file, "none" ) != 0 &&
1342 strcmp( opt.key_file, "none" ) != 0 &&
1343 key_cert_init2 == 0 &&
1344 strcmp( opt.crt_file2, "none" ) != 0 &&
1345 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001346 {
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001347#if !defined(POLARSSL_CERTS_C)
1348 printf( "Not certificated or key provided, and \n"
1349 "POLARSSL_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001350 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001351#else
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001352#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001353 if( ( ret = x509_crt_parse( &srvcert,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001354 (const unsigned char *) test_srv_crt_rsa,
1355 strlen( test_srv_crt_rsa ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001356 {
1357 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
1358 goto exit;
1359 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001360 if( ( ret = pk_parse_key( &pkey,
1361 (const unsigned char *) test_srv_key_rsa,
1362 strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001363 {
1364 printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
1365 goto exit;
1366 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001367 key_cert_init = 2;
1368#endif /* POLARSSL_RSA_C */
1369#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001370 if( ( ret = x509_crt_parse( &srvcert2,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001371 (const unsigned char *) test_srv_crt_ec,
1372 strlen( test_srv_crt_ec ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001373 {
1374 printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
1375 goto exit;
1376 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001377 if( ( ret = pk_parse_key( &pkey2,
1378 (const unsigned char *) test_srv_key_ec,
1379 strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001380 {
1381 printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
1382 goto exit;
1383 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001384 key_cert_init2 = 2;
1385#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001386#endif /* POLARSSL_CERTS_C */
1387 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001388
1389 printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001390#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001391
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001392#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1393 if( opt.dhm_file != NULL )
1394 {
1395 printf( " . Loading DHM parameters..." );
1396 fflush( stdout );
1397
1398 if( ( ret = dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
1399 {
1400 printf( " failed\n ! dhm_parse_dhmfile returned -0x%04X\n\n",
1401 -ret );
1402 goto exit;
1403 }
1404
1405 printf( " ok\n" );
1406 }
1407#endif
1408
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001409#if defined(POLARSSL_SNI)
1410 if( opt.sni != NULL )
1411 {
1412 printf( " . Setting up SNI information..." );
1413 fflush( stdout );
1414
1415 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
1416 {
1417 printf( " failed\n" );
1418 goto exit;
1419 }
1420
1421 printf( " ok\n" );
1422 }
1423#endif /* POLARSSL_SNI */
1424
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001425 /*
1426 * 2. Setup the listening TCP socket
1427 */
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001428 printf( " . Bind on %s://%s:%-4d/ ...",
1429 opt.transport == SSL_TRANSPORT_STREAM ? "tcp" : "udp",
1430 opt.server_addr ? opt.server_addr : "*",
1431 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001432 fflush( stdout );
1433
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001434 if( ( ret = net_bind( &listen_fd, opt.server_addr, opt.server_port,
1435 opt.transport == SSL_TRANSPORT_STREAM ?
1436 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001437 {
1438 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1439 goto exit;
1440 }
1441
1442 printf( " ok\n" );
1443
1444 /*
1445 * 3. Setup stuff
1446 */
1447 printf( " . Setting up the SSL/TLS structure..." );
1448 fflush( stdout );
1449
1450 if( ( ret = ssl_init( &ssl ) ) != 0 )
1451 {
1452 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
1453 goto exit;
1454 }
1455
1456 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001457 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001458
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001459#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001460 if( ( ret = ssl_set_transport( &ssl, opt.transport ) ) != 0 )
1461 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001462 printf( " failed\n ! selected transport is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001463 goto exit;
1464 }
1465
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001466 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
1467 ssl_set_handshake_timeout( &ssl, opt.hs_to_min, opt.hs_to_max );
1468#endif /* POLARSSL_SSL_PROTO_DTLS */
1469
Paul Bakker05decb22013-08-15 13:33:48 +02001470#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001471 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1472 {
1473 printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
1474 goto exit;
1475 };
Paul Bakker05decb22013-08-15 13:33:48 +02001476#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001477
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001478#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1479 if( opt.extended_ms != DFL_EXTENDED_MS )
1480 ssl_set_extended_master_secret( &ssl, opt.extended_ms );
1481#endif
1482
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001483#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1484 if( opt.etm != DFL_ETM )
1485 ssl_set_encrypt_then_mac( &ssl, opt.etm );
1486#endif
1487
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001488#if defined(POLARSSL_SSL_ALPN)
1489 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001490 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1491 {
1492 printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
1493 goto exit;
1494 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001495#endif
1496
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001497 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
1498 ssl_set_dbg( &ssl, my_debug, stdout );
1499
Paul Bakker0a597072012-09-25 21:55:46 +00001500#if defined(POLARSSL_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001501 if( opt.cache_max != -1 )
1502 ssl_cache_set_max_entries( &cache, opt.cache_max );
1503
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001504 if( opt.cache_timeout != -1 )
1505 ssl_cache_set_timeout( &cache, opt.cache_timeout );
1506
Paul Bakker0a597072012-09-25 21:55:46 +00001507 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
1508 ssl_cache_set, &cache );
1509#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001510
Paul Bakkera503a632013-08-14 13:48:06 +02001511#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001512 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1513 {
1514 printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
1515 goto exit;
1516 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001517
1518 if( opt.ticket_timeout != -1 )
1519 ssl_set_session_ticket_lifetime( &ssl, opt.ticket_timeout );
Paul Bakkera503a632013-08-14 13:48:06 +02001520#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001521
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001522#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001523 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001524 {
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001525#if defined(POLARSSL_SSL_COOKIE_C)
1526 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001527 {
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001528 if( ( ret = ssl_cookie_setup( &cookie_ctx,
1529 ctr_drbg_random, &ctr_drbg ) ) != 0 )
1530 {
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001531 printf( " failed\n ! ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001532 goto exit;
1533 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001534
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001535 ssl_set_dtls_cookies( &ssl, ssl_cookie_write, ssl_cookie_check,
1536 &cookie_ctx );
1537 }
1538 else
1539#endif /* POLARSSL_SSL_COOKIE_C */
1540#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
1541 if( opt.cookies == 0 )
1542 {
1543 ssl_set_dtls_cookies( &ssl, NULL, NULL, NULL );
1544 }
1545 else
1546#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
1547 {
1548 ; /* Nothing to do */
1549 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001550
1551#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1552 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001553 ssl_set_dtls_anti_replay( &ssl, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001554#endif
1555
1556#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
1557 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
1558 ssl_set_dtls_badmac_limit( &ssl, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001559#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001560 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001561#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001562
Paul Bakker41c83d32013-03-20 14:39:14 +01001563 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001564 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
1565
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001566 if( opt.version_suites != NULL )
1567 {
1568 ssl_set_ciphersuites_for_version( &ssl, version_suites[0],
1569 SSL_MAJOR_VERSION_3,
1570 SSL_MINOR_VERSION_0 );
1571 ssl_set_ciphersuites_for_version( &ssl, version_suites[1],
1572 SSL_MAJOR_VERSION_3,
1573 SSL_MINOR_VERSION_1 );
1574 ssl_set_ciphersuites_for_version( &ssl, version_suites[2],
1575 SSL_MAJOR_VERSION_3,
1576 SSL_MINOR_VERSION_2 );
1577 ssl_set_ciphersuites_for_version( &ssl, version_suites[3],
1578 SSL_MAJOR_VERSION_3,
1579 SSL_MINOR_VERSION_3 );
1580 }
1581
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001582 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
1583 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001584#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001585 ssl_set_renegotiation( &ssl, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001586
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001587 if( opt.renego_delay != DFL_RENEGO_DELAY )
1588 ssl_set_renegotiation_enforced( &ssl, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001589
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001590 if( opt.renego_period != DFL_RENEGO_PERIOD )
1591 {
1592 renego_period[7] = opt.renego_period;
1593 ssl_set_renegotiation_period( &ssl, renego_period );
1594 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001595#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001596
Paul Bakker36713e82013-09-17 13:25:29 +02001597#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001598 if( strcmp( opt.ca_path, "none" ) != 0 &&
1599 strcmp( opt.ca_file, "none" ) != 0 )
1600 {
1601 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
1602 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001603 if( key_cert_init )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001604 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
1605 {
1606 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1607 goto exit;
1608 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001609 if( key_cert_init2 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001610 if( ( ret = ssl_set_own_cert( &ssl, &srvcert2, &pkey2 ) ) != 0 )
1611 {
1612 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1613 goto exit;
1614 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001615#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001616
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001617#if defined(POLARSSL_SNI)
1618 if( opt.sni != NULL )
1619 ssl_set_sni( &ssl, sni_callback, sni_info );
1620#endif
1621
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001622#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001623 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
1624 {
1625 ret = ssl_set_psk( &ssl, psk, psk_len,
1626 (const unsigned char *) opt.psk_identity,
1627 strlen( opt.psk_identity ) );
1628 if( ret != 0 )
1629 {
1630 printf( " failed\n ssl_set_psk returned -0x%04X\n\n", - ret );
1631 goto exit;
1632 }
1633 }
1634
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001635 if( opt.psk_list != NULL )
1636 ssl_set_psk_cb( &ssl, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02001637#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001638
1639#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00001640 /*
1641 * Use different group than default DHM group
1642 */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001643#if defined(POLARSSL_FS_IO)
1644 if( opt.dhm_file != NULL )
1645 ret = ssl_set_dh_param_ctx( &ssl, &dhm );
1646 else
1647#endif
1648 ret = ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
1649 POLARSSL_DHM_RFC5114_MODP_2048_G );
1650
1651 if( ret != 0 )
1652 {
1653 printf( " failed\n ssl_set_dh_param returned -0x%04X\n\n", - ret );
1654 goto exit;
1655 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001656#endif
1657
Paul Bakker1d29fb52012-09-28 13:28:45 +00001658 if( opt.min_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001659 {
1660 ret = ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1661 if( ret != 0 )
1662 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001663 printf( " failed\n ! selected min_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001664 goto exit;
1665 }
1666 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001667
Paul Bakkerc1516be2013-06-29 16:01:32 +02001668 if( opt.max_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001669 {
1670 ret = ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1671 if( ret != 0 )
1672 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001673 printf( " failed\n ! selected max_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001674 goto exit;
1675 }
1676 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001677
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001678 printf( " ok\n" );
1679
1680reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02001681#if !defined(_WIN32)
1682 if( received_sigterm )
1683 {
1684 printf( " interrupted by SIGTERM\n" );
1685 ret = 0;
1686 goto exit;
1687 }
1688#endif
1689
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001690#ifdef POLARSSL_ERROR_C
1691 if( ret != 0 )
1692 {
1693 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001694 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001695 printf("Last error was: %d - %s\n\n", ret, error_buf );
1696 }
1697#endif
1698
1699 if( client_fd != -1 )
Manuel Pégourié-Gonnard4ba6ab62014-08-07 17:21:47 +02001700 net_close( client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001701
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001702 ssl_session_reset( &ssl );
1703
1704 /*
1705 * 3. Wait until a client connects
1706 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001707 client_fd = -1;
1708
1709 printf( " . Waiting for a remote connection ..." );
1710 fflush( stdout );
1711
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001712 if( ( ret = net_accept( listen_fd, &client_fd, client_ip ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001713 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02001714#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001715 if( received_sigterm )
1716 {
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001717 printf( " interrupted by signal\n" );
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001718 ret = 0;
1719 goto exit;
1720 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02001721#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001722
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001723 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
1724 goto exit;
1725 }
1726
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001727 if( opt.nbio > 0 )
1728 ret = net_set_nonblock( client_fd );
1729 else
1730 ret = net_set_block( client_fd );
1731 if( ret != 0 )
1732 {
1733 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
1734 goto exit;
1735 }
1736
1737 if( opt.nbio == 2 )
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001738 ssl_set_bio_timeout( &ssl, &client_fd, my_send, my_recv, NULL, 0 );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001739 else
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001740 ssl_set_bio_timeout( &ssl, &client_fd, net_send, net_recv,
1741#if defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnardf0365122014-09-29 16:11:47 +02001742 opt.nbio == 0 ? net_recv_timeout : NULL,
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001743#else
1744 NULL,
1745#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001746 opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001747
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001748#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001749 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
1750 {
1751 if( ( ret = ssl_set_client_transport_id( &ssl, client_ip,
1752 sizeof( client_ip ) ) ) != 0 )
1753 {
1754 printf( " failed\n ! "
1755 "ssl_set_client_tranport_id() returned -0x%x\n\n", -ret );
1756 goto exit;
1757 }
1758 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001759#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001760
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001761 printf( " ok\n" );
1762
1763 /*
Manuel Pégourié-Gonnardbd97fdb2014-09-26 16:46:36 +02001764 * With UDP, bind_fd is hijacked by client_fd, so bind a new one
1765 */
1766#if defined(POLARSSL_SSL_PROTO_DTLS)
1767 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
1768 {
1769 printf( " . Re-bind on udp://%s:%-4d/ ...",
1770 opt.server_addr ? opt.server_addr : "*",
1771 opt.server_port );
1772 fflush( stdout );
1773
1774 if( ( ret = net_bind( &listen_fd, opt.server_addr,
1775 opt.server_port, NET_PROTO_UDP ) ) != 0 )
1776 {
1777 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1778 goto exit;
1779 }
1780
1781 printf( " ok\n" );
1782 }
1783#endif /* POLARSSL_SSL_PROTO_DTLS */
1784
1785 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001786 * 4. Handshake
1787 */
1788 printf( " . Performing the SSL/TLS handshake..." );
1789 fflush( stdout );
1790
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001791 do ret = ssl_handshake( &ssl );
1792 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1793 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001794
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001795 if( ret == POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED )
1796 {
1797 printf( " hello verification requested\n" );
1798 ret = 0;
1799 goto reset;
1800 }
1801 else if( ret != 0 )
1802 {
1803 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
1804 goto reset;
1805 }
1806 else /* ret == 0 */
1807 {
1808 printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1809 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
1810 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001811
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001812 if( ( ret = ssl_get_record_expansion( &ssl ) ) >= 0 )
1813 printf( " [ Record expansion is %d ]\n", ret );
1814 else
1815 printf( " [ Record expansion is unknown (compression) ]\n" );
1816
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001817#if defined(POLARSSL_SSL_ALPN)
1818 if( opt.alpn_string != NULL )
1819 {
1820 const char *alp = ssl_get_alpn_protocol( &ssl );
1821 printf( " [ Application Layer Protocol is %s ]\n",
1822 alp ? alp : "(none)" );
1823 }
1824#endif
1825
Paul Bakker36713e82013-09-17 13:25:29 +02001826#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001827 /*
1828 * 5. Verify the server certificate
1829 */
1830 printf( " . Verifying peer X.509 certificate..." );
1831
1832 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1833 {
1834 printf( " failed\n" );
1835
Paul Bakkerb0550d92012-10-30 07:51:03 +00001836 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001837 printf( " ! no client certificate sent\n" );
1838
1839 if( ( ret & BADCERT_EXPIRED ) != 0 )
1840 printf( " ! client certificate has expired\n" );
1841
1842 if( ( ret & BADCERT_REVOKED ) != 0 )
1843 printf( " ! client certificate has been revoked\n" );
1844
1845 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
1846 printf( " ! self-signed or not signed by a trusted CA\n" );
1847
1848 printf( "\n" );
1849 }
1850 else
1851 printf( " ok\n" );
1852
Paul Bakkerb0550d92012-10-30 07:51:03 +00001853 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001854 {
1855 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001856 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1857 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001858 printf( "%s\n", buf );
1859 }
Paul Bakker36713e82013-09-17 13:25:29 +02001860#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001861
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001862 if( opt.exchanges == 0 )
1863 goto close_notify;
1864
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001865 exchanges = opt.exchanges;
1866data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001867 /*
1868 * 6. Read the HTTP Request
1869 */
1870 printf( " < Read from client:" );
1871 fflush( stdout );
1872
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001873 /*
1874 * TLS and DTLS need different reading styles (stream vs datagram)
1875 */
1876 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001877 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001878 do
1879 {
1880 int terminated = 0;
1881 len = sizeof( buf ) - 1;
1882 memset( buf, 0, sizeof( buf ) );
1883 ret = ssl_read( &ssl, buf, len );
1884
1885 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1886 ret == POLARSSL_ERR_NET_WANT_WRITE )
1887 continue;
1888
1889 if( ret <= 0 )
1890 {
1891 switch( ret )
1892 {
1893 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1894 printf( " connection was closed gracefully\n" );
1895 goto close_notify;
1896
1897 case 0:
1898 case POLARSSL_ERR_NET_CONN_RESET:
1899 printf( " connection was reset by peer\n" );
1900 ret = POLARSSL_ERR_NET_CONN_RESET;
1901 goto reset;
1902
1903 default:
1904 printf( " ssl_read returned -0x%x\n", -ret );
1905 goto reset;
1906 }
1907 }
1908
1909 if( ssl_get_bytes_avail( &ssl ) == 0 )
1910 {
1911 len = ret;
1912 buf[len] = '\0';
1913 printf( " %d bytes read\n\n%s\n", len, (char *) buf );
1914
1915 /* End of message should be detected according to the syntax of the
1916 * application protocol (eg HTTP), just use a dummy test here. */
1917 if( buf[len - 1] == '\n' )
1918 terminated = 1;
1919 }
1920 else
1921 {
1922 int extra_len, ori_len;
1923 unsigned char *larger_buf;
1924
1925 ori_len = ret;
1926 extra_len = ssl_get_bytes_avail( &ssl );
1927
1928 larger_buf = polarssl_malloc( ori_len + extra_len + 1 );
1929 if( larger_buf == NULL )
1930 {
1931 printf( " ! memory allocation failed\n" );
1932 ret = 1;
1933 goto reset;
1934 }
1935
1936 memset( larger_buf, 0, ori_len + extra_len );
1937 memcpy( larger_buf, buf, ori_len );
1938
1939 /* This read should never fail and get the whole cached data */
1940 ret = ssl_read( &ssl, larger_buf + ori_len, extra_len );
1941 if( ret != extra_len ||
1942 ssl_get_bytes_avail( &ssl ) != 0 )
1943 {
1944 printf( " ! ssl_read failed on cached data\n" );
1945 ret = 1;
1946 goto reset;
1947 }
1948
1949 larger_buf[ori_len + extra_len] = '\0';
1950 printf( " %u bytes read (%u + %u)\n\n%s\n",
1951 ori_len + extra_len, ori_len, extra_len,
1952 (char *) larger_buf );
1953
1954 /* End of message should be detected according to the syntax of the
1955 * application protocol (eg HTTP), just use a dummy test here. */
1956 if( larger_buf[ori_len + extra_len - 1] == '\n' )
1957 terminated = 1;
1958
1959 polarssl_free( larger_buf );
1960 }
1961
1962 if( terminated )
1963 {
1964 ret = 0;
1965 break;
1966 }
1967 }
1968 while( 1 );
1969 }
1970 else /* Not stream, so datagram */
1971 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001972 len = sizeof( buf ) - 1;
1973 memset( buf, 0, sizeof( buf ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001974
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001975 do ret = ssl_read( &ssl, buf, len );
1976 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1977 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001978
1979 if( ret <= 0 )
1980 {
1981 switch( ret )
1982 {
1983 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1984 printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001985 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001986 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001987
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001988 default:
1989 printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001990 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001991 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001992 }
1993
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001994 len = ret;
1995 buf[len] = '\0';
1996 printf( " %d bytes read\n\n%s", len, (char *) buf );
1997 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001998 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001999
2000 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002001 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02002002 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002003 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002004#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02002005 if( opt.renegotiate && exchanges == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002006 {
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002007 printf( " . Requestion renegotiation..." );
2008 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002009
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002010 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
2011 {
2012 if( ret != POLARSSL_ERR_NET_WANT_READ &&
2013 ret != POLARSSL_ERR_NET_WANT_WRITE )
2014 {
2015 printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
2016 goto reset;
2017 }
2018 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002019
2020 printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002021 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002022#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002023
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002024 /*
2025 * 7. Write the 200 Response
2026 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002027 printf( " > Write to client:" );
2028 fflush( stdout );
2029
2030 len = sprintf( (char *) buf, HTTP_RESPONSE,
2031 ssl_get_ciphersuite( &ssl ) );
2032
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002033 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002034 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002035 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002036 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002037 while( ( ret = ssl_write( &ssl, buf + written, len - written ) )
2038 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002039 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002040 if( ret == POLARSSL_ERR_NET_CONN_RESET )
2041 {
2042 printf( " failed\n ! peer closed the connection\n\n" );
2043 goto reset;
2044 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002045
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002046 if( ret != POLARSSL_ERR_NET_WANT_READ &&
2047 ret != POLARSSL_ERR_NET_WANT_WRITE )
2048 {
2049 printf( " failed\n ! ssl_write returned %d\n\n", ret );
2050 goto reset;
2051 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002052 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002053 }
2054 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002055 else /* Not stream, so datagram */
2056 {
2057 do ret = ssl_write( &ssl, buf, len );
2058 while( ret == POLARSSL_ERR_NET_WANT_READ ||
2059 ret == POLARSSL_ERR_NET_WANT_WRITE );
2060
2061 if( ret < 0 )
2062 {
2063 printf( " failed\n ! ssl_write returned %d\n\n", ret );
2064 goto reset;
2065 }
2066
2067 frags = 1;
2068 written = ret;
2069 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002070
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002071 buf[written] = '\0';
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002072 printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Manuel Pégourié-Gonnarda92ed482015-01-14 10:46:08 +01002073 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002074
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002075 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002076 * 7b. Continue doing data exchanges?
2077 */
2078 if( --exchanges > 0 )
2079 goto data_exchange;
2080
2081 /*
2082 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002083 */
2084close_notify:
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002085 printf( " . Closing the connection..." );
2086
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002087 /* No error checking, the connection might be closed already */
2088 do
2089 ret = ssl_close_notify( &ssl );
2090 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
2091 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002092
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002093 printf( " done\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002094 goto reset;
2095
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002096 /*
2097 * Cleanup and exit
2098 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002099exit:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002100#ifdef POLARSSL_ERROR_C
2101 if( ret != 0 )
2102 {
2103 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02002104 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002105 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
2106 }
2107#endif
2108
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01002109 printf( " . Cleaning up..." );
2110 fflush( stdout );
2111
Paul Bakker0c226102014-04-17 16:02:36 +02002112 if( client_fd != -1 )
2113 net_close( client_fd );
2114
Paul Bakkera317a982014-06-18 16:44:11 +02002115#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
2116 dhm_free( &dhm );
2117#endif
Paul Bakker36713e82013-09-17 13:25:29 +02002118#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker36713e82013-09-17 13:25:29 +02002119 x509_crt_free( &cacert );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002120 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02002121 pk_free( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002122 x509_crt_free( &srvcert2 );
2123 pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02002124#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002125#if defined(POLARSSL_SNI)
2126 sni_free( sni_info );
2127#endif
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02002128#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
2129 psk_free( psk_info );
2130#endif
2131#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
2132 dhm_free( &dhm );
2133#endif
Paul Bakkered27a042013-04-18 22:46:23 +02002134
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002135 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02002136 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02002137 entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002138
Paul Bakker0a597072012-09-25 21:55:46 +00002139#if defined(POLARSSL_SSL_CACHE_C)
2140 ssl_cache_free( &cache );
2141#endif
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +02002142#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002143 ssl_cookie_free( &cookie_ctx );
2144#endif
Paul Bakker0a597072012-09-25 21:55:46 +00002145
Paul Bakker1337aff2013-09-29 14:45:34 +02002146#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
2147#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker82024bf2013-07-04 11:52:32 +02002148 memory_buffer_alloc_status();
2149#endif
Paul Bakker1337aff2013-09-29 14:45:34 +02002150 memory_buffer_alloc_free();
2151#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02002152
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01002153 printf( " done.\n" );
2154
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002155#if defined(_WIN32)
2156 printf( " + Press Enter to exit this program.\n" );
2157 fflush( stdout ); getchar();
2158#endif
2159
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002160 // Shell can not handle large exit numbers -> 1 for errors
2161 if( ret < 0 )
2162 ret = 1;
2163
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002164 return( ret );
2165}
2166#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
2167 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
2168 POLARSSL_CTR_DRBG_C */