blob: 53e0fac9a07678f2cb9de7cf8c70329dc69043d8 [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
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100114#define DFL_MIN_VERSION SSL_MINOR_VERSION_1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200115#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100116#define DFL_ARC4 SSL_ARC4_DISABLED
Paul Bakker91ebfb52012-11-23 14:04:08 +0100117#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200118#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100119#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200120#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100121#define DFL_TICKET_TIMEOUT -1
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100122#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100123#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100124#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200125#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200126#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100127#define DFL_TRANSPORT SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200128#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200129#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200130#define DFL_HS_TO_MIN 0
131#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200132#define DFL_BADMAC_LIMIT -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200133#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100134#define DFL_ETM -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000135
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200136#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
137 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
138 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
139 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
140 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
141 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
142 "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 +0200143
Paul Bakker8e714d72013-07-18 11:05:13 +0200144/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
145 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000146#define HTTP_RESPONSE \
147 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
148 "<h2>PolarSSL Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200149 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000150
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200151/*
152 * Size of the basic I/O buffer. Able to hold our default response.
153 *
154 * You will need to adapt the ssl_get_bytes_avail() test in ssl-opt.sh
155 * if you change this value to something outside the range <= 100 or > 500
156 */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200157#define IO_BUF_LEN 200
158
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000159/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000160 * global options
161 */
162struct options
163{
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100164 const char *server_addr; /* address on which the ssl service runs */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000165 int server_port; /* port on which the ssl service runs */
166 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100167 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200168 uint32_t read_timeout; /* timeout on ssl_read() in milliseconds */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200169 const char *ca_file; /* the file with the CA certificate(s) */
170 const char *ca_path; /* the path with the CA certificate(s) reside */
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200171 const char *crt_file; /* the file with the server certificate */
172 const char *key_file; /* the file with the server key */
173 const char *crt_file2; /* the file with the 2nd server certificate */
174 const char *key_file2; /* the file with the 2nd server key */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200175 const char *psk; /* the pre-shared key */
176 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200177 char *psk_list; /* list of PSK id/key pairs for callback */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000178 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200179 const char *version_suites; /* per-version ciphersuites */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000180 int renegotiation; /* enable / disable renegotiation */
181 int allow_legacy; /* allow legacy renegotiation */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100182 int renegotiate; /* attempt renegotiation? */
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200183 int renego_delay; /* delay before enforcing renegotiation */
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100184 int renego_period; /* period for automatic renegotiation */
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200185 int exchanges; /* number of data exchanges */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000186 int min_version; /* minimum protocol version accepted */
Paul Bakkerc1516be2013-06-29 16:01:32 +0200187 int max_version; /* maximum protocol version accepted */
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100188 int arc4; /* flag for arc4 suites support */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100189 int auth_mode; /* verify mode for connection */
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200190 unsigned char mfl_code; /* code for maximum fragment length */
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100191 int trunc_hmac; /* accept truncated hmac? */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200192 int tickets; /* enable / disable session tickets */
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100193 int ticket_timeout; /* session ticket lifetime */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100194 int cache_max; /* max number of session cache entries */
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100195 int cache_timeout; /* expiration delay of session cache entries */
Paul Bakker1ebc0c52014-05-22 15:47:58 +0200196 char *sni; /* string describing sni information */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200197 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200198 const char *dhm_file; /* the file with the DH parameters */
Paul Bakkerb2eaac12015-01-13 17:15:31 +0100199 int extended_ms; /* allow negotiation of extended MS? */
200 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100201 int transport; /* TLS or DTLS? */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200202 int cookies; /* Use cookies for DTLS? -1 to break them */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200203 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200204 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
205 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200206 int badmac_limit; /* Limit of records with bad MAC */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000207} opt;
208
Paul Bakker3c5ef712013-06-25 16:37:45 +0200209static void my_debug( void *ctx, int level, const char *str )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000210{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200211 ((void) level);
212
213 fprintf( (FILE *) ctx, "%s", str );
214 fflush( (FILE *) ctx );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000215}
216
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100217/*
218 * Test recv/send functions that make sure each try returns
219 * WANT_READ/WANT_WRITE at least once before sucesseding
220 */
221static int my_recv( void *ctx, unsigned char *buf, size_t len )
222{
223 static int first_try = 1;
224 int ret;
225
226 if( first_try )
227 {
228 first_try = 0;
229 return( POLARSSL_ERR_NET_WANT_READ );
230 }
231
232 ret = net_recv( ctx, buf, len );
233 if( ret != POLARSSL_ERR_NET_WANT_READ )
234 first_try = 1; /* Next call will be a new operation */
235 return( ret );
236}
237
238static int my_send( void *ctx, const unsigned char *buf, size_t len )
239{
240 static int first_try = 1;
241 int ret;
242
243 if( first_try )
244 {
245 first_try = 0;
246 return( POLARSSL_ERR_NET_WANT_WRITE );
247 }
248
249 ret = net_send( ctx, buf, len );
250 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
251 first_try = 1; /* Next call will be a new operation */
252 return( ret );
253}
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200254
255#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000256#if defined(POLARSSL_FS_IO)
257#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100258 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
259 " default: \"\" (pre-loaded)\n" \
260 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
261 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
262 " 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 +0200263 " default: see note after key_file2\n" \
264 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200265 " 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 +0200266 " default: see note after key_file2\n" \
267 " key_file2=%%s default: see note below\n" \
268 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200269 " preloaded certificate(s) and key(s) are used if available\n" \
270 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
271 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000272#else
273#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200274 "\n" \
275 " No file operations available (POLARSSL_FS_IO not defined)\n" \
276 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000277#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200278#else
279#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200280#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200281
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200282#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200283#define USAGE_PSK \
284 " psk=%%s default: \"\" (in hex, without 0x)\n" \
285 " psk_identity=%%s default: \"Client_identity\"\n"
286#else
287#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200288#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000289
Paul Bakkera503a632013-08-14 13:48:06 +0200290#if defined(POLARSSL_SSL_SESSION_TICKETS)
291#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100292 " tickets=%%d default: 1 (enabled)\n" \
293 " ticket_timeout=%%d default: ticket default (1d)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200294#else
295#define USAGE_TICKETS ""
296#endif /* POLARSSL_SSL_SESSION_TICKETS */
297
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100298#if defined(POLARSSL_SSL_CACHE_C)
299#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100300 " cache_max=%%d default: cache default (50)\n" \
301 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100302#else
303#define USAGE_CACHE ""
304#endif /* POLARSSL_SSL_CACHE_C */
305
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100306#if defined(POLARSSL_SNI)
307#define USAGE_SNI \
308 " sni=%%s name1,cert1,key1[,name2,cert2,key2[,...]]\n" \
309 " default: disabled\n"
310#else
311#define USAGE_SNI ""
312#endif /* POLARSSL_SNI */
313
Paul Bakker05decb22013-08-15 13:33:48 +0200314#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
315#define USAGE_MAX_FRAG_LEN \
316 " max_frag_len=%%d default: 16384 (tls default)\n" \
317 " options: 512, 1024, 2048, 4096\n"
318#else
319#define USAGE_MAX_FRAG_LEN ""
320#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
321
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100322#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
323#define USAGE_TRUNC_HMAC \
324 " trunc_hmac=%%d default: library default\n"
325#else
326#define USAGE_TRUNC_HMAC ""
327#endif
328
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200329#if defined(POLARSSL_SSL_ALPN)
330#define USAGE_ALPN \
331 " alpn=%%s default: \"\" (disabled)\n" \
332 " example: spdy/1,http/1.1\n"
333#else
334#define USAGE_ALPN ""
335#endif /* POLARSSL_SSL_ALPN */
336
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200337#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
338#define USAGE_COOKIES \
339 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200340 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200341#else
342#define USAGE_COOKIES ""
343#endif
344
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200345#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
346#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200347 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200348#else
349#define USAGE_ANTI_REPLAY ""
350#endif
351
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200352#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
353#define USAGE_BADMAC_LIMIT \
354 " badmac_limit=%%d default: (library default: disabled)\n"
355#else
356#define USAGE_BADMAC_LIMIT ""
357#endif
358
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200359#if defined(POLARSSL_SSL_PROTO_DTLS)
360#define USAGE_DTLS \
361 " dtls=%%d default: 0 (TLS)\n" \
362 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
363 " range of DTLS handshake timeouts in millisecs\n"
364#else
365#define USAGE_DTLS ""
366#endif
367
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200368#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
369#define USAGE_EMS \
370 " extended_ms=0/1 default: (library default: on)\n"
371#else
372#define USAGE_EMS ""
373#endif
374
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100375#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
376#define USAGE_ETM \
377 " etm=0/1 default: (library default: on)\n"
378#else
379#define USAGE_ETM ""
380#endif
381
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100382#if defined(POLARSSL_SSL_RENEGOTIATION)
383#define USAGE_RENEGO \
384 " renegotiation=%%d default: 0 (disabled)\n" \
385 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100386 " renego_delay=%%d default: -2 (library default)\n" \
387 " renego_period=%%d default: (library default)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100388#else
389#define USAGE_RENEGO ""
390#endif
391
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000392#define USAGE \
393 "\n usage: ssl_server2 param=<>...\n" \
394 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100395 " server_addr=%%d default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000396 " server_port=%%d default: 4433\n" \
397 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100398 " nbio=%%d default: 0 (blocking I/O)\n" \
399 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200400 " read_timeout=%%d default: 0 (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100401 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200402 USAGE_DTLS \
403 USAGE_COOKIES \
404 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200405 USAGE_BADMAC_LIMIT \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200406 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100407 " auth_mode=%%s default: \"optional\"\n" \
408 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000409 USAGE_IO \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100410 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100411 "\n" \
412 USAGE_PSK \
413 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100414 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100415 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200416 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200417 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100418 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100419 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100420 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100421 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200422 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200423 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100424 USAGE_ETM \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100425 "\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000426 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200427 " max_version=%%s default: \"tls1_2\"\n" \
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100428 " arc4=%%d default: 0 (disabled)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200429 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100430 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200431 "\n" \
432 " version_suites=a,b,c,d per-version ciphersuites\n" \
433 " in order from ssl3 to tls1_2\n" \
434 " default: all enabled\n" \
435 " force_ciphersuite=<name> default: all enabled\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000436 " acceptable ciphersuite names:\n"
437
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200438/*
439 * Used by sni_parse and psk_parse to handle coma-separated lists
440 */
441#define GET_ITEM( dst ) \
442 dst = p; \
443 while( *p != ',' ) \
444 if( ++p > end ) \
445 return( NULL ); \
446 *p++ = '\0';
447
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100448#if defined(POLARSSL_SNI)
449typedef struct _sni_entry sni_entry;
450
451struct _sni_entry {
452 const char *name;
453 x509_crt *cert;
454 pk_context *key;
455 sni_entry *next;
456};
457
458/*
459 * Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]]
460 * into a usable sni_entry list.
461 *
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200462 * Modifies the input string! This is not production quality!
463 * (leaks memory if parsing fails, no error reporting, ...)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100464 */
465sni_entry *sni_parse( char *sni_string )
466{
467 sni_entry *cur = NULL, *new = NULL;
468 char *p = sni_string;
469 char *end = p;
470 char *crt_file, *key_file;
471
472 while( *end != '\0' )
473 ++end;
474 *end = ',';
475
476 while( p <= end )
477 {
478 if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL )
479 return( NULL );
480
481 memset( new, 0, sizeof( sni_entry ) );
482
483 if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ||
484 ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200485 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100486
487 x509_crt_init( new->cert );
488 pk_init( new->key );
489
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200490 GET_ITEM( new->name );
491 GET_ITEM( crt_file );
492 GET_ITEM( key_file );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100493
494 if( x509_crt_parse_file( new->cert, crt_file ) != 0 ||
495 pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200496 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100497
498 new->next = cur;
499 cur = new;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100500 }
501
502 return( cur );
503}
504
505void sni_free( sni_entry *head )
506{
507 sni_entry *cur = head, *next;
508
509 while( cur != NULL )
510 {
511 x509_crt_free( cur->cert );
512 polarssl_free( cur->cert );
513
514 pk_free( cur->key );
515 polarssl_free( cur->key );
516
517 next = cur->next;
518 polarssl_free( cur );
519 cur = next;
520 }
521}
522
523/*
524 * SNI callback.
525 */
526int sni_callback( void *p_info, ssl_context *ssl,
527 const unsigned char *name, size_t name_len )
528{
529 sni_entry *cur = (sni_entry *) p_info;
530
531 while( cur != NULL )
532 {
533 if( name_len == strlen( cur->name ) &&
534 memcmp( name, cur->name, name_len ) == 0 )
535 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200536 return( ssl_set_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100537 }
538
539 cur = cur->next;
540 }
541
542 return( -1 );
543}
544
545#endif /* POLARSSL_SNI */
546
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200547#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
548
549#define HEX2NUM( c ) \
550 if( c >= '0' && c <= '9' ) \
551 c -= '0'; \
552 else if( c >= 'a' && c <= 'f' ) \
553 c -= 'a' - 10; \
554 else if( c >= 'A' && c <= 'F' ) \
555 c -= 'A' - 10; \
556 else \
557 return( -1 );
558
559/*
560 * Convert a hex string to bytes.
561 * Return 0 on success, -1 on error.
562 */
563int unhexify( unsigned char *output, const char *input, size_t *olen )
564{
565 unsigned char c;
566 size_t j;
567
568 *olen = strlen( input );
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200569 if( *olen % 2 != 0 || *olen / 2 > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200570 return( -1 );
571 *olen /= 2;
572
573 for( j = 0; j < *olen * 2; j += 2 )
574 {
575 c = input[j];
576 HEX2NUM( c );
577 output[ j / 2 ] = c << 4;
578
579 c = input[j + 1];
580 HEX2NUM( c );
581 output[ j / 2 ] |= c;
582 }
583
584 return( 0 );
585}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200586
587typedef struct _psk_entry psk_entry;
588
589struct _psk_entry
590{
591 const char *name;
592 size_t key_len;
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200593 unsigned char key[POLARSSL_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200594 psk_entry *next;
595};
596
597/*
598 * Parse a string of pairs name1,key1[,name2,key2[,...]]
599 * into a usable psk_entry list.
600 *
601 * Modifies the input string! This is not production quality!
602 * (leaks memory if parsing fails, no error reporting, ...)
603 */
604psk_entry *psk_parse( char *psk_string )
605{
606 psk_entry *cur = NULL, *new = NULL;
607 char *p = psk_string;
608 char *end = p;
609 char *key_hex;
610
611 while( *end != '\0' )
612 ++end;
613 *end = ',';
614
615 while( p <= end )
616 {
617 if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL )
618 return( NULL );
619
620 memset( new, 0, sizeof( psk_entry ) );
621
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200622 GET_ITEM( new->name );
623 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200624
625 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
626 return( NULL );
627
628 new->next = cur;
629 cur = new;
630 }
631
632 return( cur );
633}
634
635/*
636 * Free a list of psk_entry's
637 */
638void psk_free( psk_entry *head )
639{
640 psk_entry *next;
641
642 while( head != NULL )
643 {
644 next = head->next;
645 polarssl_free( head );
646 head = next;
647 }
648}
649
650/*
651 * PSK callback
652 */
653int psk_callback( void *p_info, ssl_context *ssl,
654 const unsigned char *name, size_t name_len )
655{
656 psk_entry *cur = (psk_entry *) p_info;
657
658 while( cur != NULL )
659 {
660 if( name_len == strlen( cur->name ) &&
661 memcmp( name, cur->name, name_len ) == 0 )
662 {
663 return( ssl_set_psk( ssl, cur->key, cur->key_len,
664 name, name_len ) );
665 }
666
667 cur = cur->next;
668 }
669
670 return( -1 );
671}
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200672#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
673
Manuel Pégourié-Gonnarda9d7d032014-10-09 16:07:08 +0200674static int listen_fd, client_fd = -1;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200675
676/* Interruption handler to ensure clean exit (for valgrind testing) */
677#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200678static int received_sigterm = 0;
679void term_handler( int sig )
680{
681 ((void) sig);
682 received_sigterm = 1;
683 net_close( listen_fd ); /* causes net_accept() to abort */
Manuel Pégourié-Gonnarda9d7d032014-10-09 16:07:08 +0200684 net_close( client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200685}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200686#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200687
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000688int main( int argc, char *argv[] )
689{
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200690 int ret = 0, len, written, frags, exchanges;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200691 int version_suites[4][2];
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200692 unsigned char buf[IO_BUF_LEN];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200693#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200694 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +0200695 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +0200696 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +0200697#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200698 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +0200699 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +0200700#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200701 ssl_cookie_ctx cookie_ctx;
702#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000703
704 entropy_context entropy;
705 ctr_drbg_context ctr_drbg;
706 ssl_context ssl;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100707#if defined(POLARSSL_SSL_RENEGOTIATION)
708 unsigned char renego_period[8] = { 0 };
709#endif
Paul Bakker36713e82013-09-17 13:25:29 +0200710#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200711 x509_crt cacert;
712 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200713 pk_context pkey;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200714 x509_crt srvcert2;
715 pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200716 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200717#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200718#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
719 dhm_context dhm;
720#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000721#if defined(POLARSSL_SSL_CACHE_C)
722 ssl_cache_context cache;
723#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100724#if defined(POLARSSL_SNI)
725 sni_entry *sni_info = NULL;
726#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200727#if defined(POLARSSL_SSL_ALPN)
728 const char *alpn_list[10];
729#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200730#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
731 unsigned char alloc_buf[100000];
732#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000733
734 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000735 char *p, *q;
736 const int *list;
737
Paul Bakker82024bf2013-07-04 11:52:32 +0200738#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
739 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
740#endif
741
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000742 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200743 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000744 */
745 listen_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200746 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200747#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200748 x509_crt_init( &cacert );
749 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200750 pk_init( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200751 x509_crt_init( &srvcert2 );
752 pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200753#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200754#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
Paul Bakkera317a982014-06-18 16:44:11 +0200755 dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200756#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000757#if defined(POLARSSL_SSL_CACHE_C)
758 ssl_cache_init( &cache );
759#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200760#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200761 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200762#endif
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +0200763#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200764 ssl_cookie_init( &cookie_ctx );
765#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000766
Paul Bakkerc1283d32014-08-18 11:05:51 +0200767#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100768 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200769 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100770 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +0200771#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200772
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000773 if( argc == 0 )
774 {
775 usage:
776 if( ret == 0 )
777 ret = 1;
778
779 printf( USAGE );
780
781 list = ssl_list_ciphersuites();
782 while( *list )
783 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200784 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200785 list++;
786 if( !*list )
787 break;
788 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000789 list++;
790 }
791 printf("\n");
792 goto exit;
793 }
794
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100795 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000796 opt.server_port = DFL_SERVER_PORT;
797 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100798 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200799 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000800 opt.ca_file = DFL_CA_FILE;
801 opt.ca_path = DFL_CA_PATH;
802 opt.crt_file = DFL_CRT_FILE;
803 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200804 opt.crt_file2 = DFL_CRT_FILE2;
805 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200806 opt.psk = DFL_PSK;
807 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200808 opt.psk_list = DFL_PSK_LIST;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000809 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200810 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000811 opt.renegotiation = DFL_RENEGOTIATION;
812 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100813 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200814 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100815 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200816 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000817 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200818 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100819 opt.arc4 = DFL_ARC4;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100820 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200821 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100822 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200823 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100824 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100825 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100826 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100827 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200828 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200829 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100830 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200831 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200832 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200833 opt.hs_to_min = DFL_HS_TO_MIN;
834 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200835 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200836 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100837 opt.etm = DFL_ETM;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000838
839 for( i = 1; i < argc; i++ )
840 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000841 p = argv[i];
842 if( ( q = strchr( p, '=' ) ) == NULL )
843 goto usage;
844 *q++ = '\0';
845
846 if( strcmp( p, "server_port" ) == 0 )
847 {
848 opt.server_port = atoi( q );
849 if( opt.server_port < 1 || opt.server_port > 65535 )
850 goto usage;
851 }
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100852 else if( strcmp( p, "server_addr" ) == 0 )
853 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100854 else if( strcmp( p, "dtls" ) == 0 )
855 {
856 int t = atoi( q );
857 if( t == 0 )
858 opt.transport = SSL_TRANSPORT_STREAM;
859 else if( t == 1 )
860 opt.transport = SSL_TRANSPORT_DATAGRAM;
861 else
862 goto usage;
863 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000864 else if( strcmp( p, "debug_level" ) == 0 )
865 {
866 opt.debug_level = atoi( q );
867 if( opt.debug_level < 0 || opt.debug_level > 65535 )
868 goto usage;
869 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100870 else if( strcmp( p, "nbio" ) == 0 )
871 {
872 opt.nbio = atoi( q );
873 if( opt.nbio < 0 || opt.nbio > 2 )
874 goto usage;
875 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200876 else if( strcmp( p, "read_timeout" ) == 0 )
877 opt.read_timeout = atoi( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000878 else if( strcmp( p, "ca_file" ) == 0 )
879 opt.ca_file = q;
880 else if( strcmp( p, "ca_path" ) == 0 )
881 opt.ca_path = q;
882 else if( strcmp( p, "crt_file" ) == 0 )
883 opt.crt_file = q;
884 else if( strcmp( p, "key_file" ) == 0 )
885 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200886 else if( strcmp( p, "crt_file2" ) == 0 )
887 opt.crt_file2 = q;
888 else if( strcmp( p, "key_file2" ) == 0 )
889 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200890 else if( strcmp( p, "dhm_file" ) == 0 )
891 opt.dhm_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200892 else if( strcmp( p, "psk" ) == 0 )
893 opt.psk = q;
894 else if( strcmp( p, "psk_identity" ) == 0 )
895 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200896 else if( strcmp( p, "psk_list" ) == 0 )
897 opt.psk_list = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000898 else if( strcmp( p, "force_ciphersuite" ) == 0 )
899 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000900 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
901
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200902 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000903 {
904 ret = 2;
905 goto usage;
906 }
907 opt.force_ciphersuite[1] = 0;
908 }
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200909 else if( strcmp( p, "version_suites" ) == 0 )
910 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000911 else if( strcmp( p, "renegotiation" ) == 0 )
912 {
913 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
914 SSL_RENEGOTIATION_DISABLED;
915 }
916 else if( strcmp( p, "allow_legacy" ) == 0 )
917 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100918 switch( atoi( q ) )
919 {
920 case -1: opt.allow_legacy = SSL_LEGACY_BREAK_HANDSHAKE; break;
921 case 0: opt.allow_legacy = SSL_LEGACY_NO_RENEGOTIATION; break;
922 case 1: opt.allow_legacy = SSL_LEGACY_ALLOW_RENEGOTIATION; break;
923 default: goto usage;
924 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000925 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100926 else if( strcmp( p, "renegotiate" ) == 0 )
927 {
928 opt.renegotiate = atoi( q );
929 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
930 goto usage;
931 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200932 else if( strcmp( p, "renego_delay" ) == 0 )
933 {
934 opt.renego_delay = atoi( q );
935 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100936 else if( strcmp( p, "renego_period" ) == 0 )
937 {
938 opt.renego_period = atoi( q );
939 if( opt.renego_period < 2 || opt.renego_period > 255 )
940 goto usage;
941 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200942 else if( strcmp( p, "exchanges" ) == 0 )
943 {
944 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +0200945 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200946 goto usage;
947 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000948 else if( strcmp( p, "min_version" ) == 0 )
949 {
950 if( strcmp( q, "ssl3" ) == 0 )
951 opt.min_version = SSL_MINOR_VERSION_0;
952 else if( strcmp( q, "tls1" ) == 0 )
953 opt.min_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 Bakker1d29fb52012-09-28 13:28:45 +0000956 opt.min_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 Bakker1d29fb52012-09-28 13:28:45 +0000959 opt.min_version = SSL_MINOR_VERSION_3;
960 else
961 goto usage;
962 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200963 else if( strcmp( p, "max_version" ) == 0 )
964 {
965 if( strcmp( q, "ssl3" ) == 0 )
966 opt.max_version = SSL_MINOR_VERSION_0;
967 else if( strcmp( q, "tls1" ) == 0 )
968 opt.max_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100969 else if( strcmp( q, "tls1_1" ) == 0 ||
970 strcmp( q, "dtls1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200971 opt.max_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100972 else if( strcmp( q, "tls1_2" ) == 0 ||
973 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200974 opt.max_version = SSL_MINOR_VERSION_3;
975 else
976 goto usage;
977 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100978 else if( strcmp( p, "arc4" ) == 0 )
979 {
980 switch( atoi( q ) )
981 {
982 case 0: opt.arc4 = SSL_ARC4_DISABLED; break;
983 case 1: opt.arc4 = SSL_ARC4_ENABLED; break;
984 default: goto usage;
985 }
986 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200987 else if( strcmp( p, "force_version" ) == 0 )
988 {
989 if( strcmp( q, "ssl3" ) == 0 )
990 {
991 opt.min_version = SSL_MINOR_VERSION_0;
992 opt.max_version = SSL_MINOR_VERSION_0;
993 }
994 else if( strcmp( q, "tls1" ) == 0 )
995 {
996 opt.min_version = SSL_MINOR_VERSION_1;
997 opt.max_version = SSL_MINOR_VERSION_1;
998 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100999 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001000 {
1001 opt.min_version = SSL_MINOR_VERSION_2;
1002 opt.max_version = SSL_MINOR_VERSION_2;
1003 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001004 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001005 {
1006 opt.min_version = SSL_MINOR_VERSION_3;
1007 opt.max_version = SSL_MINOR_VERSION_3;
1008 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001009 else if( strcmp( q, "dtls1" ) == 0 )
1010 {
1011 opt.min_version = SSL_MINOR_VERSION_2;
1012 opt.max_version = SSL_MINOR_VERSION_2;
1013 opt.transport = SSL_TRANSPORT_DATAGRAM;
1014 }
1015 else if( strcmp( q, "dtls1_2" ) == 0 )
1016 {
1017 opt.min_version = SSL_MINOR_VERSION_3;
1018 opt.max_version = SSL_MINOR_VERSION_3;
1019 opt.transport = SSL_TRANSPORT_DATAGRAM;
1020 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001021 else
1022 goto usage;
1023 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001024 else if( strcmp( p, "auth_mode" ) == 0 )
1025 {
1026 if( strcmp( q, "none" ) == 0 )
1027 opt.auth_mode = SSL_VERIFY_NONE;
1028 else if( strcmp( q, "optional" ) == 0 )
1029 opt.auth_mode = SSL_VERIFY_OPTIONAL;
1030 else if( strcmp( q, "required" ) == 0 )
1031 opt.auth_mode = SSL_VERIFY_REQUIRED;
1032 else
1033 goto usage;
1034 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001035 else if( strcmp( p, "max_frag_len" ) == 0 )
1036 {
1037 if( strcmp( q, "512" ) == 0 )
1038 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
1039 else if( strcmp( q, "1024" ) == 0 )
1040 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
1041 else if( strcmp( q, "2048" ) == 0 )
1042 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
1043 else if( strcmp( q, "4096" ) == 0 )
1044 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
1045 else
1046 goto usage;
1047 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001048 else if( strcmp( p, "alpn" ) == 0 )
1049 {
1050 opt.alpn_string = q;
1051 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001052 else if( strcmp( p, "trunc_hmac" ) == 0 )
1053 {
1054 switch( atoi( q ) )
1055 {
1056 case 0: opt.trunc_hmac = SSL_TRUNC_HMAC_DISABLED; break;
1057 case 1: opt.trunc_hmac = SSL_TRUNC_HMAC_ENABLED; break;
1058 default: goto usage;
1059 }
1060 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001061 else if( strcmp( p, "extended_ms" ) == 0 )
1062 {
1063 switch( atoi( q ) )
1064 {
1065 case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break;
1066 case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break;
1067 default: goto usage;
1068 }
1069 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001070 else if( strcmp( p, "etm" ) == 0 )
1071 {
1072 switch( atoi( q ) )
1073 {
1074 case 0: opt.etm = SSL_ETM_DISABLED; break;
1075 case 1: opt.etm = SSL_ETM_ENABLED; break;
1076 default: goto usage;
1077 }
1078 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001079 else if( strcmp( p, "tickets" ) == 0 )
1080 {
1081 opt.tickets = atoi( q );
1082 if( opt.tickets < 0 || opt.tickets > 1 )
1083 goto usage;
1084 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001085 else if( strcmp( p, "ticket_timeout" ) == 0 )
1086 {
1087 opt.ticket_timeout = atoi( q );
1088 if( opt.ticket_timeout < 0 )
1089 goto usage;
1090 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001091 else if( strcmp( p, "cache_max" ) == 0 )
1092 {
1093 opt.cache_max = atoi( q );
1094 if( opt.cache_max < 0 )
1095 goto usage;
1096 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001097 else if( strcmp( p, "cache_timeout" ) == 0 )
1098 {
1099 opt.cache_timeout = atoi( q );
1100 if( opt.cache_timeout < 0 )
1101 goto usage;
1102 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001103 else if( strcmp( p, "cookies" ) == 0 )
1104 {
1105 opt.cookies = atoi( q );
1106 if( opt.cookies < -1 || opt.cookies > 1)
1107 goto usage;
1108 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001109 else if( strcmp( p, "anti_replay" ) == 0 )
1110 {
1111 opt.anti_replay = atoi( q );
1112 if( opt.anti_replay < 0 || opt.anti_replay > 1)
1113 goto usage;
1114 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001115 else if( strcmp( p, "badmac_limit" ) == 0 )
1116 {
1117 opt.badmac_limit = atoi( q );
1118 if( opt.badmac_limit < 0 )
1119 goto usage;
1120 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001121 else if( strcmp( p, "hs_timeout" ) == 0 )
1122 {
1123 if( ( p = strchr( q, '-' ) ) == NULL )
1124 goto usage;
1125 *p++ = '\0';
1126 opt.hs_to_min = atoi( q );
1127 opt.hs_to_max = atoi( p );
1128 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1129 goto usage;
1130 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001131 else if( strcmp( p, "sni" ) == 0 )
1132 {
1133 opt.sni = q;
1134 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001135 else
1136 goto usage;
1137 }
1138
Paul Bakkerc73079a2014-04-25 16:34:30 +02001139#if defined(POLARSSL_DEBUG_C)
1140 debug_set_threshold( opt.debug_level );
1141#endif
1142
Paul Bakkerc1516be2013-06-29 16:01:32 +02001143 if( opt.force_ciphersuite[0] > 0 )
1144 {
1145 const ssl_ciphersuite_t *ciphersuite_info;
1146 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1147
Paul Bakker5b55b792013-07-19 13:43:43 +02001148 if( opt.max_version != -1 &&
1149 ciphersuite_info->min_minor_ver > opt.max_version )
1150 {
1151 printf("forced ciphersuite not allowed with this protocol version\n");
1152 ret = 2;
1153 goto usage;
1154 }
1155 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001156 ciphersuite_info->max_minor_ver < opt.min_version )
1157 {
1158 printf("forced ciphersuite not allowed with this protocol version\n");
1159 ret = 2;
1160 goto usage;
1161 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001162
1163 /* If we select a version that's not supported by
1164 * this suite, then there will be no common ciphersuite... */
1165 if( opt.max_version == -1 ||
1166 opt.max_version > ciphersuite_info->max_minor_ver )
1167 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001168 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001169 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001170 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001171 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001172 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001173 /* DTLS starts with TLS 1.1 */
1174 if( opt.transport == SSL_TRANSPORT_DATAGRAM &&
1175 opt.min_version < SSL_MINOR_VERSION_2 )
1176 opt.min_version = SSL_MINOR_VERSION_2;
1177 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001178 }
1179
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001180 if( opt.version_suites != NULL )
1181 {
1182 const char *name[4] = { 0 };
1183
1184 /* Parse 4-element coma-separated list */
1185 for( i = 0, p = (char *) opt.version_suites;
1186 i < 4 && *p != '\0';
1187 i++ )
1188 {
1189 name[i] = p;
1190
1191 /* Terminate the current string and move on to next one */
1192 while( *p != ',' && *p != '\0' )
1193 p++;
1194 if( *p == ',' )
1195 *p++ = '\0';
1196 }
1197
1198 if( i != 4 )
1199 {
1200 printf( "too few values for version_suites\n" );
1201 ret = 1;
1202 goto exit;
1203 }
1204
1205 memset( version_suites, 0, sizeof( version_suites ) );
1206
1207 /* Get the suites identifiers from their name */
1208 for( i = 0; i < 4; i++ )
1209 {
1210 version_suites[i][0] = ssl_get_ciphersuite_id( name[i] );
1211
1212 if( version_suites[i][0] == 0 )
1213 {
1214 printf( "unknown ciphersuite: '%s'\n", name[i] );
1215 ret = 2;
1216 goto usage;
1217 }
1218 }
1219 }
1220
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001221#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001222 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001223 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001224 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001225 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001226 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001227 printf( "pre-shared key not valid hex\n" );
1228 goto exit;
1229 }
1230
1231 if( opt.psk_list != NULL )
1232 {
1233 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001234 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001235 printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001236 goto exit;
1237 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001238 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001239#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001240
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001241#if defined(POLARSSL_SSL_ALPN)
1242 if( opt.alpn_string != NULL )
1243 {
1244 p = (char *) opt.alpn_string;
1245 i = 0;
1246
1247 /* Leave room for a final NULL in alpn_list */
1248 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
1249 {
1250 alpn_list[i++] = p;
1251
1252 /* Terminate the current string and move on to next one */
1253 while( *p != ',' && *p != '\0' )
1254 p++;
1255 if( *p == ',' )
1256 *p++ = '\0';
1257 }
1258 }
1259#endif /* POLARSSL_SSL_ALPN */
1260
Paul Bakkerfbb17802013-04-17 19:10:21 +02001261 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001262 * 0. Initialize the RNG and the session data
1263 */
1264 printf( "\n . Seeding the random number generator..." );
1265 fflush( stdout );
1266
1267 entropy_init( &entropy );
1268 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001269 (const unsigned char *) pers,
1270 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001271 {
1272 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
1273 goto exit;
1274 }
1275
1276 printf( " ok\n" );
1277
Paul Bakker36713e82013-09-17 13:25:29 +02001278#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001279 /*
1280 * 1.1. Load the trusted CA
1281 */
1282 printf( " . Loading the CA root certificate ..." );
1283 fflush( stdout );
1284
1285#if defined(POLARSSL_FS_IO)
1286 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001287 if( strcmp( opt.ca_path, "none" ) == 0 )
1288 ret = 0;
1289 else
1290 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001291 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001292 if( strcmp( opt.ca_file, "none" ) == 0 )
1293 ret = 0;
1294 else
1295 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001296 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001297#endif
1298#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +02001299 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
1300 strlen( test_ca_list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001301#else
1302 {
1303 ret = 1;
1304 printf("POLARSSL_CERTS_C not defined.");
1305 }
1306#endif
1307 if( ret < 0 )
1308 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001309 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001310 goto exit;
1311 }
1312
1313 printf( " ok (%d skipped)\n", ret );
1314
1315 /*
1316 * 1.2. Load own certificate and private key
1317 */
1318 printf( " . Loading the server cert. and key..." );
1319 fflush( stdout );
1320
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001321#if defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001322 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001323 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001324 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001325 if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
1326 {
1327 printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n",
1328 -ret );
1329 goto exit;
1330 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001331 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001332 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001333 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001334 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001335 if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
1336 {
1337 printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret );
1338 goto exit;
1339 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001340 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001341 if( key_cert_init == 1 )
1342 {
1343 printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
1344 goto exit;
1345 }
1346
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001347 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001348 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001349 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001350 if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
1351 {
1352 printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n",
1353 -ret );
1354 goto exit;
1355 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001356 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001357 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001358 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001359 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001360 if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
1361 {
1362 printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n",
1363 -ret );
1364 goto exit;
1365 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001366 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001367 if( key_cert_init2 == 1 )
1368 {
1369 printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
1370 goto exit;
1371 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001372#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001373 if( key_cert_init == 0 &&
1374 strcmp( opt.crt_file, "none" ) != 0 &&
1375 strcmp( opt.key_file, "none" ) != 0 &&
1376 key_cert_init2 == 0 &&
1377 strcmp( opt.crt_file2, "none" ) != 0 &&
1378 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001379 {
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001380#if !defined(POLARSSL_CERTS_C)
1381 printf( "Not certificated or key provided, and \n"
1382 "POLARSSL_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001383 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001384#else
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001385#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001386 if( ( ret = x509_crt_parse( &srvcert,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001387 (const unsigned char *) test_srv_crt_rsa,
1388 strlen( test_srv_crt_rsa ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001389 {
1390 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
1391 goto exit;
1392 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001393 if( ( ret = pk_parse_key( &pkey,
1394 (const unsigned char *) test_srv_key_rsa,
1395 strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001396 {
1397 printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
1398 goto exit;
1399 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001400 key_cert_init = 2;
1401#endif /* POLARSSL_RSA_C */
1402#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001403 if( ( ret = x509_crt_parse( &srvcert2,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001404 (const unsigned char *) test_srv_crt_ec,
1405 strlen( test_srv_crt_ec ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001406 {
1407 printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
1408 goto exit;
1409 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001410 if( ( ret = pk_parse_key( &pkey2,
1411 (const unsigned char *) test_srv_key_ec,
1412 strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001413 {
1414 printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
1415 goto exit;
1416 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001417 key_cert_init2 = 2;
1418#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001419#endif /* POLARSSL_CERTS_C */
1420 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001421
1422 printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001423#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001424
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001425#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1426 if( opt.dhm_file != NULL )
1427 {
1428 printf( " . Loading DHM parameters..." );
1429 fflush( stdout );
1430
1431 if( ( ret = dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
1432 {
1433 printf( " failed\n ! dhm_parse_dhmfile returned -0x%04X\n\n",
1434 -ret );
1435 goto exit;
1436 }
1437
1438 printf( " ok\n" );
1439 }
1440#endif
1441
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001442#if defined(POLARSSL_SNI)
1443 if( opt.sni != NULL )
1444 {
1445 printf( " . Setting up SNI information..." );
1446 fflush( stdout );
1447
1448 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
1449 {
1450 printf( " failed\n" );
1451 goto exit;
1452 }
1453
1454 printf( " ok\n" );
1455 }
1456#endif /* POLARSSL_SNI */
1457
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001458 /*
1459 * 2. Setup the listening TCP socket
1460 */
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001461 printf( " . Bind on %s://%s:%-4d/ ...",
1462 opt.transport == SSL_TRANSPORT_STREAM ? "tcp" : "udp",
1463 opt.server_addr ? opt.server_addr : "*",
1464 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001465 fflush( stdout );
1466
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001467 if( ( ret = net_bind( &listen_fd, opt.server_addr, opt.server_port,
1468 opt.transport == SSL_TRANSPORT_STREAM ?
1469 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001470 {
1471 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1472 goto exit;
1473 }
1474
1475 printf( " ok\n" );
1476
1477 /*
1478 * 3. Setup stuff
1479 */
1480 printf( " . Setting up the SSL/TLS structure..." );
1481 fflush( stdout );
1482
1483 if( ( ret = ssl_init( &ssl ) ) != 0 )
1484 {
1485 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
1486 goto exit;
1487 }
1488
1489 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001490 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001491
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001492#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001493 if( ( ret = ssl_set_transport( &ssl, opt.transport ) ) != 0 )
1494 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001495 printf( " failed\n ! selected transport is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001496 goto exit;
1497 }
1498
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001499 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
1500 ssl_set_handshake_timeout( &ssl, opt.hs_to_min, opt.hs_to_max );
1501#endif /* POLARSSL_SSL_PROTO_DTLS */
1502
Paul Bakker05decb22013-08-15 13:33:48 +02001503#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001504 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1505 {
1506 printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
1507 goto exit;
1508 };
Paul Bakker05decb22013-08-15 13:33:48 +02001509#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001510
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001511#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1512 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
1513 ssl_set_truncated_hmac( &ssl, opt.trunc_hmac );
1514#endif
1515
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001516#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1517 if( opt.extended_ms != DFL_EXTENDED_MS )
1518 ssl_set_extended_master_secret( &ssl, opt.extended_ms );
1519#endif
1520
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001521#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1522 if( opt.etm != DFL_ETM )
1523 ssl_set_encrypt_then_mac( &ssl, opt.etm );
1524#endif
1525
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001526#if defined(POLARSSL_SSL_ALPN)
1527 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001528 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1529 {
1530 printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
1531 goto exit;
1532 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001533#endif
1534
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001535 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
1536 ssl_set_dbg( &ssl, my_debug, stdout );
1537
Paul Bakker0a597072012-09-25 21:55:46 +00001538#if defined(POLARSSL_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001539 if( opt.cache_max != -1 )
1540 ssl_cache_set_max_entries( &cache, opt.cache_max );
1541
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001542 if( opt.cache_timeout != -1 )
1543 ssl_cache_set_timeout( &cache, opt.cache_timeout );
1544
Paul Bakker0a597072012-09-25 21:55:46 +00001545 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
1546 ssl_cache_set, &cache );
1547#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001548
Paul Bakkera503a632013-08-14 13:48:06 +02001549#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001550 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1551 {
1552 printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
1553 goto exit;
1554 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001555
1556 if( opt.ticket_timeout != -1 )
1557 ssl_set_session_ticket_lifetime( &ssl, opt.ticket_timeout );
Paul Bakkera503a632013-08-14 13:48:06 +02001558#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001559
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001560#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001561 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001562 {
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001563#if defined(POLARSSL_SSL_COOKIE_C)
1564 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001565 {
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001566 if( ( ret = ssl_cookie_setup( &cookie_ctx,
1567 ctr_drbg_random, &ctr_drbg ) ) != 0 )
1568 {
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001569 printf( " failed\n ! ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001570 goto exit;
1571 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001572
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001573 ssl_set_dtls_cookies( &ssl, ssl_cookie_write, ssl_cookie_check,
1574 &cookie_ctx );
1575 }
1576 else
1577#endif /* POLARSSL_SSL_COOKIE_C */
1578#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
1579 if( opt.cookies == 0 )
1580 {
1581 ssl_set_dtls_cookies( &ssl, NULL, NULL, NULL );
1582 }
1583 else
1584#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
1585 {
1586 ; /* Nothing to do */
1587 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001588
1589#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1590 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001591 ssl_set_dtls_anti_replay( &ssl, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001592#endif
1593
1594#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
1595 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
1596 ssl_set_dtls_badmac_limit( &ssl, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001597#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001598 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001599#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001600
Paul Bakker41c83d32013-03-20 14:39:14 +01001601 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001602 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001603 else
1604 ssl_set_arc4_support( &ssl, opt.arc4 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001605
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001606 if( opt.version_suites != NULL )
1607 {
1608 ssl_set_ciphersuites_for_version( &ssl, version_suites[0],
1609 SSL_MAJOR_VERSION_3,
1610 SSL_MINOR_VERSION_0 );
1611 ssl_set_ciphersuites_for_version( &ssl, version_suites[1],
1612 SSL_MAJOR_VERSION_3,
1613 SSL_MINOR_VERSION_1 );
1614 ssl_set_ciphersuites_for_version( &ssl, version_suites[2],
1615 SSL_MAJOR_VERSION_3,
1616 SSL_MINOR_VERSION_2 );
1617 ssl_set_ciphersuites_for_version( &ssl, version_suites[3],
1618 SSL_MAJOR_VERSION_3,
1619 SSL_MINOR_VERSION_3 );
1620 }
1621
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001622 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
1623 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001624#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001625 ssl_set_renegotiation( &ssl, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001626
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001627 if( opt.renego_delay != DFL_RENEGO_DELAY )
1628 ssl_set_renegotiation_enforced( &ssl, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001629
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001630 if( opt.renego_period != DFL_RENEGO_PERIOD )
1631 {
1632 renego_period[7] = opt.renego_period;
1633 ssl_set_renegotiation_period( &ssl, renego_period );
1634 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001635#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001636
Paul Bakker36713e82013-09-17 13:25:29 +02001637#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001638 if( strcmp( opt.ca_path, "none" ) != 0 &&
1639 strcmp( opt.ca_file, "none" ) != 0 )
1640 {
1641 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
1642 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001643 if( key_cert_init )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001644 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
1645 {
1646 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1647 goto exit;
1648 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001649 if( key_cert_init2 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001650 if( ( ret = ssl_set_own_cert( &ssl, &srvcert2, &pkey2 ) ) != 0 )
1651 {
1652 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1653 goto exit;
1654 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001655#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001656
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001657#if defined(POLARSSL_SNI)
1658 if( opt.sni != NULL )
1659 ssl_set_sni( &ssl, sni_callback, sni_info );
1660#endif
1661
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001662#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001663 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
1664 {
1665 ret = ssl_set_psk( &ssl, psk, psk_len,
1666 (const unsigned char *) opt.psk_identity,
1667 strlen( opt.psk_identity ) );
1668 if( ret != 0 )
1669 {
1670 printf( " failed\n ssl_set_psk returned -0x%04X\n\n", - ret );
1671 goto exit;
1672 }
1673 }
1674
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001675 if( opt.psk_list != NULL )
1676 ssl_set_psk_cb( &ssl, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02001677#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001678
1679#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00001680 /*
1681 * Use different group than default DHM group
1682 */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001683#if defined(POLARSSL_FS_IO)
1684 if( opt.dhm_file != NULL )
1685 ret = ssl_set_dh_param_ctx( &ssl, &dhm );
1686 else
1687#endif
1688 ret = ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
1689 POLARSSL_DHM_RFC5114_MODP_2048_G );
1690
1691 if( ret != 0 )
1692 {
1693 printf( " failed\n ssl_set_dh_param returned -0x%04X\n\n", - ret );
1694 goto exit;
1695 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001696#endif
1697
Paul Bakker1d29fb52012-09-28 13:28:45 +00001698 if( opt.min_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001699 {
1700 ret = ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1701 if( ret != 0 )
1702 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001703 printf( " failed\n ! selected min_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001704 goto exit;
1705 }
1706 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001707
Paul Bakkerc1516be2013-06-29 16:01:32 +02001708 if( opt.max_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001709 {
1710 ret = ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1711 if( ret != 0 )
1712 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001713 printf( " failed\n ! selected max_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001714 goto exit;
1715 }
1716 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001717
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001718 printf( " ok\n" );
1719
1720reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02001721#if !defined(_WIN32)
1722 if( received_sigterm )
1723 {
1724 printf( " interrupted by SIGTERM\n" );
1725 ret = 0;
1726 goto exit;
1727 }
1728#endif
1729
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001730#ifdef POLARSSL_ERROR_C
1731 if( ret != 0 )
1732 {
1733 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001734 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001735 printf("Last error was: %d - %s\n\n", ret, error_buf );
1736 }
1737#endif
1738
1739 if( client_fd != -1 )
Manuel Pégourié-Gonnard4ba6ab62014-08-07 17:21:47 +02001740 net_close( client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001741
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001742 ssl_session_reset( &ssl );
1743
1744 /*
1745 * 3. Wait until a client connects
1746 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001747 client_fd = -1;
1748
1749 printf( " . Waiting for a remote connection ..." );
1750 fflush( stdout );
1751
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001752 if( ( ret = net_accept( listen_fd, &client_fd, client_ip ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001753 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02001754#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001755 if( received_sigterm )
1756 {
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001757 printf( " interrupted by signal\n" );
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001758 ret = 0;
1759 goto exit;
1760 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02001761#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001762
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001763 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
1764 goto exit;
1765 }
1766
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001767 if( opt.nbio > 0 )
1768 ret = net_set_nonblock( client_fd );
1769 else
1770 ret = net_set_block( client_fd );
1771 if( ret != 0 )
1772 {
1773 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
1774 goto exit;
1775 }
1776
1777 if( opt.nbio == 2 )
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001778 ssl_set_bio_timeout( &ssl, &client_fd, my_send, my_recv, NULL, 0 );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001779 else
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001780 ssl_set_bio_timeout( &ssl, &client_fd, net_send, net_recv,
1781#if defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnardf0365122014-09-29 16:11:47 +02001782 opt.nbio == 0 ? net_recv_timeout : NULL,
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001783#else
1784 NULL,
1785#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001786 opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001787
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001788#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001789 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
1790 {
1791 if( ( ret = ssl_set_client_transport_id( &ssl, client_ip,
1792 sizeof( client_ip ) ) ) != 0 )
1793 {
1794 printf( " failed\n ! "
1795 "ssl_set_client_tranport_id() returned -0x%x\n\n", -ret );
1796 goto exit;
1797 }
1798 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001799#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001800
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001801 printf( " ok\n" );
1802
1803 /*
Manuel Pégourié-Gonnardbd97fdb2014-09-26 16:46:36 +02001804 * With UDP, bind_fd is hijacked by client_fd, so bind a new one
1805 */
1806#if defined(POLARSSL_SSL_PROTO_DTLS)
1807 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
1808 {
1809 printf( " . Re-bind on udp://%s:%-4d/ ...",
1810 opt.server_addr ? opt.server_addr : "*",
1811 opt.server_port );
1812 fflush( stdout );
1813
1814 if( ( ret = net_bind( &listen_fd, opt.server_addr,
1815 opt.server_port, NET_PROTO_UDP ) ) != 0 )
1816 {
1817 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1818 goto exit;
1819 }
1820
1821 printf( " ok\n" );
1822 }
1823#endif /* POLARSSL_SSL_PROTO_DTLS */
1824
1825 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001826 * 4. Handshake
1827 */
1828 printf( " . Performing the SSL/TLS handshake..." );
1829 fflush( stdout );
1830
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001831 do ret = ssl_handshake( &ssl );
1832 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1833 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001834
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001835 if( ret == POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED )
1836 {
1837 printf( " hello verification requested\n" );
1838 ret = 0;
1839 goto reset;
1840 }
1841 else if( ret != 0 )
1842 {
1843 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
1844 goto reset;
1845 }
1846 else /* ret == 0 */
1847 {
1848 printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1849 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
1850 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001851
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001852 if( ( ret = ssl_get_record_expansion( &ssl ) ) >= 0 )
1853 printf( " [ Record expansion is %d ]\n", ret );
1854 else
1855 printf( " [ Record expansion is unknown (compression) ]\n" );
1856
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001857#if defined(POLARSSL_SSL_ALPN)
1858 if( opt.alpn_string != NULL )
1859 {
1860 const char *alp = ssl_get_alpn_protocol( &ssl );
1861 printf( " [ Application Layer Protocol is %s ]\n",
1862 alp ? alp : "(none)" );
1863 }
1864#endif
1865
Paul Bakker36713e82013-09-17 13:25:29 +02001866#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001867 /*
1868 * 5. Verify the server certificate
1869 */
1870 printf( " . Verifying peer X.509 certificate..." );
1871
1872 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1873 {
1874 printf( " failed\n" );
1875
Paul Bakkerb0550d92012-10-30 07:51:03 +00001876 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001877 printf( " ! no client certificate sent\n" );
1878
1879 if( ( ret & BADCERT_EXPIRED ) != 0 )
1880 printf( " ! client certificate has expired\n" );
1881
1882 if( ( ret & BADCERT_REVOKED ) != 0 )
1883 printf( " ! client certificate has been revoked\n" );
1884
1885 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
1886 printf( " ! self-signed or not signed by a trusted CA\n" );
1887
1888 printf( "\n" );
1889 }
1890 else
1891 printf( " ok\n" );
1892
Paul Bakkerb0550d92012-10-30 07:51:03 +00001893 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001894 {
1895 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001896 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1897 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001898 printf( "%s\n", buf );
1899 }
Paul Bakker36713e82013-09-17 13:25:29 +02001900#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001901
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001902 if( opt.exchanges == 0 )
1903 goto close_notify;
1904
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001905 exchanges = opt.exchanges;
1906data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001907 /*
1908 * 6. Read the HTTP Request
1909 */
1910 printf( " < Read from client:" );
1911 fflush( stdout );
1912
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001913 /*
1914 * TLS and DTLS need different reading styles (stream vs datagram)
1915 */
1916 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001917 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001918 do
1919 {
1920 int terminated = 0;
1921 len = sizeof( buf ) - 1;
1922 memset( buf, 0, sizeof( buf ) );
1923 ret = ssl_read( &ssl, buf, len );
1924
1925 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1926 ret == POLARSSL_ERR_NET_WANT_WRITE )
1927 continue;
1928
1929 if( ret <= 0 )
1930 {
1931 switch( ret )
1932 {
1933 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1934 printf( " connection was closed gracefully\n" );
1935 goto close_notify;
1936
1937 case 0:
1938 case POLARSSL_ERR_NET_CONN_RESET:
1939 printf( " connection was reset by peer\n" );
1940 ret = POLARSSL_ERR_NET_CONN_RESET;
1941 goto reset;
1942
1943 default:
1944 printf( " ssl_read returned -0x%x\n", -ret );
1945 goto reset;
1946 }
1947 }
1948
1949 if( ssl_get_bytes_avail( &ssl ) == 0 )
1950 {
1951 len = ret;
1952 buf[len] = '\0';
1953 printf( " %d bytes read\n\n%s\n", len, (char *) buf );
1954
1955 /* End of message should be detected according to the syntax of the
1956 * application protocol (eg HTTP), just use a dummy test here. */
1957 if( buf[len - 1] == '\n' )
1958 terminated = 1;
1959 }
1960 else
1961 {
1962 int extra_len, ori_len;
1963 unsigned char *larger_buf;
1964
1965 ori_len = ret;
1966 extra_len = ssl_get_bytes_avail( &ssl );
1967
1968 larger_buf = polarssl_malloc( ori_len + extra_len + 1 );
1969 if( larger_buf == NULL )
1970 {
1971 printf( " ! memory allocation failed\n" );
1972 ret = 1;
1973 goto reset;
1974 }
1975
1976 memset( larger_buf, 0, ori_len + extra_len );
1977 memcpy( larger_buf, buf, ori_len );
1978
1979 /* This read should never fail and get the whole cached data */
1980 ret = ssl_read( &ssl, larger_buf + ori_len, extra_len );
1981 if( ret != extra_len ||
1982 ssl_get_bytes_avail( &ssl ) != 0 )
1983 {
1984 printf( " ! ssl_read failed on cached data\n" );
1985 ret = 1;
1986 goto reset;
1987 }
1988
1989 larger_buf[ori_len + extra_len] = '\0';
1990 printf( " %u bytes read (%u + %u)\n\n%s\n",
1991 ori_len + extra_len, ori_len, extra_len,
1992 (char *) larger_buf );
1993
1994 /* End of message should be detected according to the syntax of the
1995 * application protocol (eg HTTP), just use a dummy test here. */
1996 if( larger_buf[ori_len + extra_len - 1] == '\n' )
1997 terminated = 1;
1998
1999 polarssl_free( larger_buf );
2000 }
2001
2002 if( terminated )
2003 {
2004 ret = 0;
2005 break;
2006 }
2007 }
2008 while( 1 );
2009 }
2010 else /* Not stream, so datagram */
2011 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002012 len = sizeof( buf ) - 1;
2013 memset( buf, 0, sizeof( buf ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002014
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002015 do ret = ssl_read( &ssl, buf, len );
2016 while( ret == POLARSSL_ERR_NET_WANT_READ ||
2017 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002018
2019 if( ret <= 0 )
2020 {
2021 switch( ret )
2022 {
2023 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
2024 printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002025 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002026 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002027
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002028 default:
2029 printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002030 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002031 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002032 }
2033
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002034 len = ret;
2035 buf[len] = '\0';
2036 printf( " %d bytes read\n\n%s", len, (char *) buf );
2037 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002038 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002039
2040 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002041 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02002042 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002043 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002044#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02002045 if( opt.renegotiate && exchanges == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002046 {
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002047 printf( " . Requestion renegotiation..." );
2048 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002049
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002050 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
2051 {
2052 if( ret != POLARSSL_ERR_NET_WANT_READ &&
2053 ret != POLARSSL_ERR_NET_WANT_WRITE )
2054 {
2055 printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
2056 goto reset;
2057 }
2058 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002059
2060 printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002061 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002062#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002063
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002064 /*
2065 * 7. Write the 200 Response
2066 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002067 printf( " > Write to client:" );
2068 fflush( stdout );
2069
2070 len = sprintf( (char *) buf, HTTP_RESPONSE,
2071 ssl_get_ciphersuite( &ssl ) );
2072
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002073 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002074 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002075 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002076 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002077 while( ( ret = ssl_write( &ssl, buf + written, len - written ) )
2078 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002079 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002080 if( ret == POLARSSL_ERR_NET_CONN_RESET )
2081 {
2082 printf( " failed\n ! peer closed the connection\n\n" );
2083 goto reset;
2084 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002085
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002086 if( ret != POLARSSL_ERR_NET_WANT_READ &&
2087 ret != POLARSSL_ERR_NET_WANT_WRITE )
2088 {
2089 printf( " failed\n ! ssl_write returned %d\n\n", ret );
2090 goto reset;
2091 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002092 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002093 }
2094 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002095 else /* Not stream, so datagram */
2096 {
2097 do ret = ssl_write( &ssl, buf, len );
2098 while( ret == POLARSSL_ERR_NET_WANT_READ ||
2099 ret == POLARSSL_ERR_NET_WANT_WRITE );
2100
2101 if( ret < 0 )
2102 {
2103 printf( " failed\n ! ssl_write returned %d\n\n", ret );
2104 goto reset;
2105 }
2106
2107 frags = 1;
2108 written = ret;
2109 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002110
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002111 buf[written] = '\0';
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002112 printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Manuel Pégourié-Gonnarda92ed482015-01-14 10:46:08 +01002113 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002114
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002115 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002116 * 7b. Continue doing data exchanges?
2117 */
2118 if( --exchanges > 0 )
2119 goto data_exchange;
2120
2121 /*
2122 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002123 */
2124close_notify:
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002125 printf( " . Closing the connection..." );
2126
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002127 /* No error checking, the connection might be closed already */
2128 do
2129 ret = ssl_close_notify( &ssl );
2130 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
2131 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002132
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002133 printf( " done\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002134 goto reset;
2135
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002136 /*
2137 * Cleanup and exit
2138 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002139exit:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002140#ifdef POLARSSL_ERROR_C
2141 if( ret != 0 )
2142 {
2143 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02002144 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002145 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
2146 }
2147#endif
2148
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01002149 printf( " . Cleaning up..." );
2150 fflush( stdout );
2151
Paul Bakker0c226102014-04-17 16:02:36 +02002152 if( client_fd != -1 )
2153 net_close( client_fd );
2154
Paul Bakkera317a982014-06-18 16:44:11 +02002155#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
2156 dhm_free( &dhm );
2157#endif
Paul Bakker36713e82013-09-17 13:25:29 +02002158#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker36713e82013-09-17 13:25:29 +02002159 x509_crt_free( &cacert );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002160 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02002161 pk_free( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002162 x509_crt_free( &srvcert2 );
2163 pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02002164#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002165#if defined(POLARSSL_SNI)
2166 sni_free( sni_info );
2167#endif
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02002168#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
2169 psk_free( psk_info );
2170#endif
2171#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
2172 dhm_free( &dhm );
2173#endif
Paul Bakkered27a042013-04-18 22:46:23 +02002174
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002175 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02002176 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02002177 entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002178
Paul Bakker0a597072012-09-25 21:55:46 +00002179#if defined(POLARSSL_SSL_CACHE_C)
2180 ssl_cache_free( &cache );
2181#endif
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +02002182#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002183 ssl_cookie_free( &cookie_ctx );
2184#endif
Paul Bakker0a597072012-09-25 21:55:46 +00002185
Paul Bakker1337aff2013-09-29 14:45:34 +02002186#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
2187#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker82024bf2013-07-04 11:52:32 +02002188 memory_buffer_alloc_status();
2189#endif
Paul Bakker1337aff2013-09-29 14:45:34 +02002190 memory_buffer_alloc_free();
2191#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02002192
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01002193 printf( " done.\n" );
2194
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002195#if defined(_WIN32)
2196 printf( " + Press Enter to exit this program.\n" );
2197 fflush( stdout ); getchar();
2198#endif
2199
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002200 // Shell can not handle large exit numbers -> 1 for errors
2201 if( ret < 0 )
2202 ret = 1;
2203
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002204 return( ret );
2205}
2206#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
2207 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
2208 POLARSSL_CTR_DRBG_C */