blob: 85189bd11e34fffa736895cec43dde4334e2b8b0 [file] [log] [blame]
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001/*
2 * SSL client with options
3 *
Paul Bakker3c5ef712013-06-25 16:37:45 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000031
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020032#if !defined(POLARSSL_ENTROPY_C) || \
33 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
34 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
35#include <stdio.h>
36int main( int argc, char *argv[] )
37{
38 ((void) argc);
39 ((void) argv);
40
41 printf("POLARSSL_ENTROPY_C and/or "
42 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
43 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
44 return( 0 );
45}
46#else
47
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010048#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) && defined(POLARSSL_FS_IO)
49#define POLARSSL_SNI
50#endif
51
52#if defined(POLARSSL_PLATFORM_C)
53#include "polarssl/platform.h"
54#else
55#define polarssl_malloc malloc
56#define polarssl_free free
57#endif
58
Paul Bakkerb60b95f2012-09-25 09:05:17 +000059#if defined(_WIN32)
60#include <windows.h>
61#endif
62
63#include <string.h>
64#include <stdlib.h>
65#include <stdio.h>
Paul Bakkerc1283d32014-08-18 11:05:51 +020066
67#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +020068#include <signal.h>
Paul Bakkerc1283d32014-08-18 11:05:51 +020069#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000070
Paul Bakkerb60b95f2012-09-25 09:05:17 +000071#include "polarssl/net.h"
72#include "polarssl/ssl.h"
73#include "polarssl/entropy.h"
74#include "polarssl/ctr_drbg.h"
75#include "polarssl/certs.h"
76#include "polarssl/x509.h"
77#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020078#include "polarssl/debug.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000079
Paul Bakker0a597072012-09-25 21:55:46 +000080#if defined(POLARSSL_SSL_CACHE_C)
81#include "polarssl/ssl_cache.h"
82#endif
83
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +020084#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020085#include "polarssl/ssl_cookie.h"
86#endif
87
Paul Bakker82024bf2013-07-04 11:52:32 +020088#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
89#include "polarssl/memory.h"
90#endif
91
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +010092#define DFL_SERVER_ADDR NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000093#define DFL_SERVER_PORT 4433
Paul Bakkerb60b95f2012-09-25 09:05:17 +000094#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010095#define DFL_NBIO 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020096#define DFL_READ_TIMEOUT 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +000097#define DFL_CA_FILE ""
98#define DFL_CA_PATH ""
99#define DFL_CRT_FILE ""
100#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200101#define DFL_CRT_FILE2 ""
102#define DFL_KEY_FILE2 ""
Paul Bakkerfbb17802013-04-17 19:10:21 +0200103#define DFL_PSK ""
104#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200105#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000106#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200107#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +0200108#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100109#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100110#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200111#define DFL_RENEGO_DELAY -2
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100112#define DFL_RENEGO_PERIOD -1
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200113#define DFL_EXCHANGES 1
Paul Bakker1d29fb52012-09-28 13:28:45 +0000114#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200115#define DFL_MAX_VERSION -1
Paul Bakker91ebfb52012-11-23 14:04:08 +0100116#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200117#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100118#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200119#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100120#define DFL_TICKET_TIMEOUT -1
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100121#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100122#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100123#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200124#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200125#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100126#define DFL_TRANSPORT SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200127#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200128#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200129#define DFL_HS_TO_MIN 0
130#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200131#define DFL_BADMAC_LIMIT -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200132#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100133#define DFL_ETM -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000134
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200135#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
136 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
137 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
138 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
139 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
140 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
141 "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 +0200142
Paul Bakker8e714d72013-07-18 11:05:13 +0200143/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
144 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000145#define HTTP_RESPONSE \
146 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
147 "<h2>PolarSSL Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200148 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000149
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200150/*
151 * Size of the basic I/O buffer. Able to hold our default response.
152 *
153 * You will need to adapt the ssl_get_bytes_avail() test in ssl-opt.sh
154 * if you change this value to something outside the range <= 100 or > 500
155 */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200156#define IO_BUF_LEN 200
157
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000158/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000159 * global options
160 */
161struct options
162{
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100163 const char *server_addr; /* address on which the ssl service runs */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000164 int server_port; /* port on which the ssl service runs */
165 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100166 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200167 uint32_t read_timeout; /* timeout on ssl_read() in milliseconds */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200168 const char *ca_file; /* the file with the CA certificate(s) */
169 const char *ca_path; /* the path with the CA certificate(s) reside */
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200170 const char *crt_file; /* the file with the server certificate */
171 const char *key_file; /* the file with the server key */
172 const char *crt_file2; /* the file with the 2nd server certificate */
173 const char *key_file2; /* the file with the 2nd server key */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200174 const char *psk; /* the pre-shared key */
175 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200176 char *psk_list; /* list of PSK id/key pairs for callback */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000177 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200178 const char *version_suites; /* per-version ciphersuites */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000179 int renegotiation; /* enable / disable renegotiation */
180 int allow_legacy; /* allow legacy renegotiation */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100181 int renegotiate; /* attempt renegotiation? */
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200182 int renego_delay; /* delay before enforcing renegotiation */
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100183 int renego_period; /* period for automatic renegotiation */
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200184 int exchanges; /* number of data exchanges */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000185 int min_version; /* minimum protocol version accepted */
Paul Bakkerc1516be2013-06-29 16:01:32 +0200186 int max_version; /* maximum protocol version accepted */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100187 int auth_mode; /* verify mode for connection */
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200188 unsigned char mfl_code; /* code for maximum fragment length */
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100189 int trunc_hmac; /* accept truncated hmac? */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200190 int tickets; /* enable / disable session tickets */
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100191 int ticket_timeout; /* session ticket lifetime */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100192 int cache_max; /* max number of session cache entries */
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100193 int cache_timeout; /* expiration delay of session cache entries */
Paul Bakker1ebc0c52014-05-22 15:47:58 +0200194 char *sni; /* string describing sni information */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200195 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200196 const char *dhm_file; /* the file with the DH parameters */
Paul Bakkerb2eaac12015-01-13 17:15:31 +0100197 int extended_ms; /* allow negotiation of extended MS? */
198 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100199 int transport; /* TLS or DTLS? */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200200 int cookies; /* Use cookies for DTLS? -1 to break them */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200201 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200202 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
203 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200204 int badmac_limit; /* Limit of records with bad MAC */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000205} opt;
206
Paul Bakker3c5ef712013-06-25 16:37:45 +0200207static void my_debug( void *ctx, int level, const char *str )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000208{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200209 ((void) level);
210
211 fprintf( (FILE *) ctx, "%s", str );
212 fflush( (FILE *) ctx );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000213}
214
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100215/*
216 * Test recv/send functions that make sure each try returns
217 * WANT_READ/WANT_WRITE at least once before sucesseding
218 */
219static int my_recv( void *ctx, unsigned char *buf, size_t len )
220{
221 static int first_try = 1;
222 int ret;
223
224 if( first_try )
225 {
226 first_try = 0;
227 return( POLARSSL_ERR_NET_WANT_READ );
228 }
229
230 ret = net_recv( ctx, buf, len );
231 if( ret != POLARSSL_ERR_NET_WANT_READ )
232 first_try = 1; /* Next call will be a new operation */
233 return( ret );
234}
235
236static int my_send( void *ctx, const unsigned char *buf, size_t len )
237{
238 static int first_try = 1;
239 int ret;
240
241 if( first_try )
242 {
243 first_try = 0;
244 return( POLARSSL_ERR_NET_WANT_WRITE );
245 }
246
247 ret = net_send( ctx, buf, len );
248 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
249 first_try = 1; /* Next call will be a new operation */
250 return( ret );
251}
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200252
253#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000254#if defined(POLARSSL_FS_IO)
255#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100256 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
257 " default: \"\" (pre-loaded)\n" \
258 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
259 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
260 " 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 +0200261 " default: see note after key_file2\n" \
262 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200263 " 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 +0200264 " default: see note after key_file2\n" \
265 " key_file2=%%s default: see note below\n" \
266 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200267 " preloaded certificate(s) and key(s) are used if available\n" \
268 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
269 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000270#else
271#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200272 "\n" \
273 " No file operations available (POLARSSL_FS_IO not defined)\n" \
274 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000275#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200276#else
277#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200278#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200279
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200280#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200281#define USAGE_PSK \
282 " psk=%%s default: \"\" (in hex, without 0x)\n" \
283 " psk_identity=%%s default: \"Client_identity\"\n"
284#else
285#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200286#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000287
Paul Bakkera503a632013-08-14 13:48:06 +0200288#if defined(POLARSSL_SSL_SESSION_TICKETS)
289#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100290 " tickets=%%d default: 1 (enabled)\n" \
291 " ticket_timeout=%%d default: ticket default (1d)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200292#else
293#define USAGE_TICKETS ""
294#endif /* POLARSSL_SSL_SESSION_TICKETS */
295
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100296#if defined(POLARSSL_SSL_CACHE_C)
297#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100298 " cache_max=%%d default: cache default (50)\n" \
299 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100300#else
301#define USAGE_CACHE ""
302#endif /* POLARSSL_SSL_CACHE_C */
303
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100304#if defined(POLARSSL_SNI)
305#define USAGE_SNI \
306 " sni=%%s name1,cert1,key1[,name2,cert2,key2[,...]]\n" \
307 " default: disabled\n"
308#else
309#define USAGE_SNI ""
310#endif /* POLARSSL_SNI */
311
Paul Bakker05decb22013-08-15 13:33:48 +0200312#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
313#define USAGE_MAX_FRAG_LEN \
314 " max_frag_len=%%d default: 16384 (tls default)\n" \
315 " options: 512, 1024, 2048, 4096\n"
316#else
317#define USAGE_MAX_FRAG_LEN ""
318#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
319
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100320#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
321#define USAGE_TRUNC_HMAC \
322 " trunc_hmac=%%d default: library default\n"
323#else
324#define USAGE_TRUNC_HMAC ""
325#endif
326
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200327#if defined(POLARSSL_SSL_ALPN)
328#define USAGE_ALPN \
329 " alpn=%%s default: \"\" (disabled)\n" \
330 " example: spdy/1,http/1.1\n"
331#else
332#define USAGE_ALPN ""
333#endif /* POLARSSL_SSL_ALPN */
334
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200335#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
336#define USAGE_COOKIES \
337 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200338 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200339#else
340#define USAGE_COOKIES ""
341#endif
342
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200343#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
344#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200345 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200346#else
347#define USAGE_ANTI_REPLAY ""
348#endif
349
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200350#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
351#define USAGE_BADMAC_LIMIT \
352 " badmac_limit=%%d default: (library default: disabled)\n"
353#else
354#define USAGE_BADMAC_LIMIT ""
355#endif
356
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200357#if defined(POLARSSL_SSL_PROTO_DTLS)
358#define USAGE_DTLS \
359 " dtls=%%d default: 0 (TLS)\n" \
360 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
361 " range of DTLS handshake timeouts in millisecs\n"
362#else
363#define USAGE_DTLS ""
364#endif
365
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200366#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
367#define USAGE_EMS \
368 " extended_ms=0/1 default: (library default: on)\n"
369#else
370#define USAGE_EMS ""
371#endif
372
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100373#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
374#define USAGE_ETM \
375 " etm=0/1 default: (library default: on)\n"
376#else
377#define USAGE_ETM ""
378#endif
379
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100380#if defined(POLARSSL_SSL_RENEGOTIATION)
381#define USAGE_RENEGO \
382 " renegotiation=%%d default: 0 (disabled)\n" \
383 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100384 " renego_delay=%%d default: -2 (library default)\n" \
385 " renego_period=%%d default: (library default)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100386#else
387#define USAGE_RENEGO ""
388#endif
389
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000390#define USAGE \
391 "\n usage: ssl_server2 param=<>...\n" \
392 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100393 " server_addr=%%d default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000394 " server_port=%%d default: 4433\n" \
395 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100396 " nbio=%%d default: 0 (blocking I/O)\n" \
397 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200398 " read_timeout=%%d default: 0 (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100399 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200400 USAGE_DTLS \
401 USAGE_COOKIES \
402 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200403 USAGE_BADMAC_LIMIT \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200404 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100405 " auth_mode=%%s default: \"optional\"\n" \
406 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000407 USAGE_IO \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100408 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100409 "\n" \
410 USAGE_PSK \
411 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100412 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100413 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200414 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200415 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100416 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100417 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100418 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100419 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200420 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200421 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100422 USAGE_ETM \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100423 "\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000424 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200425 " max_version=%%s default: \"tls1_2\"\n" \
426 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100427 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200428 "\n" \
429 " version_suites=a,b,c,d per-version ciphersuites\n" \
430 " in order from ssl3 to tls1_2\n" \
431 " default: all enabled\n" \
432 " force_ciphersuite=<name> default: all enabled\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000433 " acceptable ciphersuite names:\n"
434
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200435/*
436 * Used by sni_parse and psk_parse to handle coma-separated lists
437 */
438#define GET_ITEM( dst ) \
439 dst = p; \
440 while( *p != ',' ) \
441 if( ++p > end ) \
442 return( NULL ); \
443 *p++ = '\0';
444
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100445#if defined(POLARSSL_SNI)
446typedef struct _sni_entry sni_entry;
447
448struct _sni_entry {
449 const char *name;
450 x509_crt *cert;
451 pk_context *key;
452 sni_entry *next;
453};
454
455/*
456 * Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]]
457 * into a usable sni_entry list.
458 *
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200459 * Modifies the input string! This is not production quality!
460 * (leaks memory if parsing fails, no error reporting, ...)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100461 */
462sni_entry *sni_parse( char *sni_string )
463{
464 sni_entry *cur = NULL, *new = NULL;
465 char *p = sni_string;
466 char *end = p;
467 char *crt_file, *key_file;
468
469 while( *end != '\0' )
470 ++end;
471 *end = ',';
472
473 while( p <= end )
474 {
475 if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL )
476 return( NULL );
477
478 memset( new, 0, sizeof( sni_entry ) );
479
480 if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ||
481 ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200482 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100483
484 x509_crt_init( new->cert );
485 pk_init( new->key );
486
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200487 GET_ITEM( new->name );
488 GET_ITEM( crt_file );
489 GET_ITEM( key_file );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100490
491 if( x509_crt_parse_file( new->cert, crt_file ) != 0 ||
492 pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200493 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100494
495 new->next = cur;
496 cur = new;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100497 }
498
499 return( cur );
500}
501
502void sni_free( sni_entry *head )
503{
504 sni_entry *cur = head, *next;
505
506 while( cur != NULL )
507 {
508 x509_crt_free( cur->cert );
509 polarssl_free( cur->cert );
510
511 pk_free( cur->key );
512 polarssl_free( cur->key );
513
514 next = cur->next;
515 polarssl_free( cur );
516 cur = next;
517 }
518}
519
520/*
521 * SNI callback.
522 */
523int sni_callback( void *p_info, ssl_context *ssl,
524 const unsigned char *name, size_t name_len )
525{
526 sni_entry *cur = (sni_entry *) p_info;
527
528 while( cur != NULL )
529 {
530 if( name_len == strlen( cur->name ) &&
531 memcmp( name, cur->name, name_len ) == 0 )
532 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200533 return( ssl_set_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100534 }
535
536 cur = cur->next;
537 }
538
539 return( -1 );
540}
541
542#endif /* POLARSSL_SNI */
543
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200544#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
545
546#define HEX2NUM( c ) \
547 if( c >= '0' && c <= '9' ) \
548 c -= '0'; \
549 else if( c >= 'a' && c <= 'f' ) \
550 c -= 'a' - 10; \
551 else if( c >= 'A' && c <= 'F' ) \
552 c -= 'A' - 10; \
553 else \
554 return( -1 );
555
556/*
557 * Convert a hex string to bytes.
558 * Return 0 on success, -1 on error.
559 */
560int unhexify( unsigned char *output, const char *input, size_t *olen )
561{
562 unsigned char c;
563 size_t j;
564
565 *olen = strlen( input );
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200566 if( *olen % 2 != 0 || *olen / 2 > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200567 return( -1 );
568 *olen /= 2;
569
570 for( j = 0; j < *olen * 2; j += 2 )
571 {
572 c = input[j];
573 HEX2NUM( c );
574 output[ j / 2 ] = c << 4;
575
576 c = input[j + 1];
577 HEX2NUM( c );
578 output[ j / 2 ] |= c;
579 }
580
581 return( 0 );
582}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200583
584typedef struct _psk_entry psk_entry;
585
586struct _psk_entry
587{
588 const char *name;
589 size_t key_len;
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200590 unsigned char key[POLARSSL_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200591 psk_entry *next;
592};
593
594/*
595 * Parse a string of pairs name1,key1[,name2,key2[,...]]
596 * into a usable psk_entry list.
597 *
598 * Modifies the input string! This is not production quality!
599 * (leaks memory if parsing fails, no error reporting, ...)
600 */
601psk_entry *psk_parse( char *psk_string )
602{
603 psk_entry *cur = NULL, *new = NULL;
604 char *p = psk_string;
605 char *end = p;
606 char *key_hex;
607
608 while( *end != '\0' )
609 ++end;
610 *end = ',';
611
612 while( p <= end )
613 {
614 if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL )
615 return( NULL );
616
617 memset( new, 0, sizeof( psk_entry ) );
618
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200619 GET_ITEM( new->name );
620 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200621
622 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
623 return( NULL );
624
625 new->next = cur;
626 cur = new;
627 }
628
629 return( cur );
630}
631
632/*
633 * Free a list of psk_entry's
634 */
635void psk_free( psk_entry *head )
636{
637 psk_entry *next;
638
639 while( head != NULL )
640 {
641 next = head->next;
642 polarssl_free( head );
643 head = next;
644 }
645}
646
647/*
648 * PSK callback
649 */
650int psk_callback( void *p_info, ssl_context *ssl,
651 const unsigned char *name, size_t name_len )
652{
653 psk_entry *cur = (psk_entry *) p_info;
654
655 while( cur != NULL )
656 {
657 if( name_len == strlen( cur->name ) &&
658 memcmp( name, cur->name, name_len ) == 0 )
659 {
660 return( ssl_set_psk( ssl, cur->key, cur->key_len,
661 name, name_len ) );
662 }
663
664 cur = cur->next;
665 }
666
667 return( -1 );
668}
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200669#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
670
Manuel Pégourié-Gonnarda9d7d032014-10-09 16:07:08 +0200671static int listen_fd, client_fd = -1;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200672
673/* Interruption handler to ensure clean exit (for valgrind testing) */
674#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200675static int received_sigterm = 0;
676void term_handler( int sig )
677{
678 ((void) sig);
679 received_sigterm = 1;
680 net_close( listen_fd ); /* causes net_accept() to abort */
Manuel Pégourié-Gonnarda9d7d032014-10-09 16:07:08 +0200681 net_close( client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200682}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200683#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200684
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000685int main( int argc, char *argv[] )
686{
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200687 int ret = 0, len, written, frags, exchanges;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200688 int version_suites[4][2];
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200689 unsigned char buf[IO_BUF_LEN];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200690#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200691 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +0200692 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +0200693 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +0200694#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200695 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +0200696 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +0200697#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200698 ssl_cookie_ctx cookie_ctx;
699#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000700
701 entropy_context entropy;
702 ctr_drbg_context ctr_drbg;
703 ssl_context ssl;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100704#if defined(POLARSSL_SSL_RENEGOTIATION)
705 unsigned char renego_period[8] = { 0 };
706#endif
Paul Bakker36713e82013-09-17 13:25:29 +0200707#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200708 x509_crt cacert;
709 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200710 pk_context pkey;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200711 x509_crt srvcert2;
712 pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200713 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200714#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200715#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
716 dhm_context dhm;
717#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000718#if defined(POLARSSL_SSL_CACHE_C)
719 ssl_cache_context cache;
720#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100721#if defined(POLARSSL_SNI)
722 sni_entry *sni_info = NULL;
723#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200724#if defined(POLARSSL_SSL_ALPN)
725 const char *alpn_list[10];
726#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200727#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
728 unsigned char alloc_buf[100000];
729#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000730
731 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000732 char *p, *q;
733 const int *list;
734
Paul Bakker82024bf2013-07-04 11:52:32 +0200735#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
736 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
737#endif
738
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000739 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200740 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000741 */
742 listen_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200743 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200744#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200745 x509_crt_init( &cacert );
746 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200747 pk_init( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200748 x509_crt_init( &srvcert2 );
749 pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200750#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200751#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
Paul Bakkera317a982014-06-18 16:44:11 +0200752 dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200753#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000754#if defined(POLARSSL_SSL_CACHE_C)
755 ssl_cache_init( &cache );
756#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200757#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200758 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200759#endif
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +0200760#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200761 ssl_cookie_init( &cookie_ctx );
762#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000763
Paul Bakkerc1283d32014-08-18 11:05:51 +0200764#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100765 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200766 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100767 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +0200768#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200769
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000770 if( argc == 0 )
771 {
772 usage:
773 if( ret == 0 )
774 ret = 1;
775
776 printf( USAGE );
777
778 list = ssl_list_ciphersuites();
779 while( *list )
780 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200781 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200782 list++;
783 if( !*list )
784 break;
785 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000786 list++;
787 }
788 printf("\n");
789 goto exit;
790 }
791
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100792 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000793 opt.server_port = DFL_SERVER_PORT;
794 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100795 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200796 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000797 opt.ca_file = DFL_CA_FILE;
798 opt.ca_path = DFL_CA_PATH;
799 opt.crt_file = DFL_CRT_FILE;
800 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200801 opt.crt_file2 = DFL_CRT_FILE2;
802 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200803 opt.psk = DFL_PSK;
804 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200805 opt.psk_list = DFL_PSK_LIST;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000806 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200807 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000808 opt.renegotiation = DFL_RENEGOTIATION;
809 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100810 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200811 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100812 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200813 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000814 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200815 opt.max_version = DFL_MAX_VERSION;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100816 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200817 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100818 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200819 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100820 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100821 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100822 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100823 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200824 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200825 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100826 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200827 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200828 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200829 opt.hs_to_min = DFL_HS_TO_MIN;
830 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200831 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200832 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100833 opt.etm = DFL_ETM;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000834
835 for( i = 1; i < argc; i++ )
836 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000837 p = argv[i];
838 if( ( q = strchr( p, '=' ) ) == NULL )
839 goto usage;
840 *q++ = '\0';
841
842 if( strcmp( p, "server_port" ) == 0 )
843 {
844 opt.server_port = atoi( q );
845 if( opt.server_port < 1 || opt.server_port > 65535 )
846 goto usage;
847 }
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100848 else if( strcmp( p, "server_addr" ) == 0 )
849 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100850 else if( strcmp( p, "dtls" ) == 0 )
851 {
852 int t = atoi( q );
853 if( t == 0 )
854 opt.transport = SSL_TRANSPORT_STREAM;
855 else if( t == 1 )
856 opt.transport = SSL_TRANSPORT_DATAGRAM;
857 else
858 goto usage;
859 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000860 else if( strcmp( p, "debug_level" ) == 0 )
861 {
862 opt.debug_level = atoi( q );
863 if( opt.debug_level < 0 || opt.debug_level > 65535 )
864 goto usage;
865 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100866 else if( strcmp( p, "nbio" ) == 0 )
867 {
868 opt.nbio = atoi( q );
869 if( opt.nbio < 0 || opt.nbio > 2 )
870 goto usage;
871 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200872 else if( strcmp( p, "read_timeout" ) == 0 )
873 opt.read_timeout = atoi( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000874 else if( strcmp( p, "ca_file" ) == 0 )
875 opt.ca_file = q;
876 else if( strcmp( p, "ca_path" ) == 0 )
877 opt.ca_path = q;
878 else if( strcmp( p, "crt_file" ) == 0 )
879 opt.crt_file = q;
880 else if( strcmp( p, "key_file" ) == 0 )
881 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200882 else if( strcmp( p, "crt_file2" ) == 0 )
883 opt.crt_file2 = q;
884 else if( strcmp( p, "key_file2" ) == 0 )
885 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200886 else if( strcmp( p, "dhm_file" ) == 0 )
887 opt.dhm_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200888 else if( strcmp( p, "psk" ) == 0 )
889 opt.psk = q;
890 else if( strcmp( p, "psk_identity" ) == 0 )
891 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200892 else if( strcmp( p, "psk_list" ) == 0 )
893 opt.psk_list = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000894 else if( strcmp( p, "force_ciphersuite" ) == 0 )
895 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000896 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
897
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200898 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000899 {
900 ret = 2;
901 goto usage;
902 }
903 opt.force_ciphersuite[1] = 0;
904 }
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200905 else if( strcmp( p, "version_suites" ) == 0 )
906 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000907 else if( strcmp( p, "renegotiation" ) == 0 )
908 {
909 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
910 SSL_RENEGOTIATION_DISABLED;
911 }
912 else if( strcmp( p, "allow_legacy" ) == 0 )
913 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100914 switch( atoi( q ) )
915 {
916 case -1: opt.allow_legacy = SSL_LEGACY_BREAK_HANDSHAKE; break;
917 case 0: opt.allow_legacy = SSL_LEGACY_NO_RENEGOTIATION; break;
918 case 1: opt.allow_legacy = SSL_LEGACY_ALLOW_RENEGOTIATION; break;
919 default: goto usage;
920 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000921 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100922 else if( strcmp( p, "renegotiate" ) == 0 )
923 {
924 opt.renegotiate = atoi( q );
925 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
926 goto usage;
927 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200928 else if( strcmp( p, "renego_delay" ) == 0 )
929 {
930 opt.renego_delay = atoi( q );
931 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100932 else if( strcmp( p, "renego_period" ) == 0 )
933 {
934 opt.renego_period = atoi( q );
935 if( opt.renego_period < 2 || opt.renego_period > 255 )
936 goto usage;
937 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200938 else if( strcmp( p, "exchanges" ) == 0 )
939 {
940 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +0200941 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200942 goto usage;
943 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000944 else if( strcmp( p, "min_version" ) == 0 )
945 {
946 if( strcmp( q, "ssl3" ) == 0 )
947 opt.min_version = SSL_MINOR_VERSION_0;
948 else if( strcmp( q, "tls1" ) == 0 )
949 opt.min_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100950 else if( strcmp( q, "tls1_1" ) == 0 ||
951 strcmp( q, "dtls1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000952 opt.min_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100953 else if( strcmp( q, "tls1_2" ) == 0 ||
954 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000955 opt.min_version = SSL_MINOR_VERSION_3;
956 else
957 goto usage;
958 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200959 else if( strcmp( p, "max_version" ) == 0 )
960 {
961 if( strcmp( q, "ssl3" ) == 0 )
962 opt.max_version = SSL_MINOR_VERSION_0;
963 else if( strcmp( q, "tls1" ) == 0 )
964 opt.max_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100965 else if( strcmp( q, "tls1_1" ) == 0 ||
966 strcmp( q, "dtls1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200967 opt.max_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100968 else if( strcmp( q, "tls1_2" ) == 0 ||
969 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200970 opt.max_version = SSL_MINOR_VERSION_3;
971 else
972 goto usage;
973 }
974 else if( strcmp( p, "force_version" ) == 0 )
975 {
976 if( strcmp( q, "ssl3" ) == 0 )
977 {
978 opt.min_version = SSL_MINOR_VERSION_0;
979 opt.max_version = SSL_MINOR_VERSION_0;
980 }
981 else if( strcmp( q, "tls1" ) == 0 )
982 {
983 opt.min_version = SSL_MINOR_VERSION_1;
984 opt.max_version = SSL_MINOR_VERSION_1;
985 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100986 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200987 {
988 opt.min_version = SSL_MINOR_VERSION_2;
989 opt.max_version = SSL_MINOR_VERSION_2;
990 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100991 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200992 {
993 opt.min_version = SSL_MINOR_VERSION_3;
994 opt.max_version = SSL_MINOR_VERSION_3;
995 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100996 else if( strcmp( q, "dtls1" ) == 0 )
997 {
998 opt.min_version = SSL_MINOR_VERSION_2;
999 opt.max_version = SSL_MINOR_VERSION_2;
1000 opt.transport = SSL_TRANSPORT_DATAGRAM;
1001 }
1002 else if( strcmp( q, "dtls1_2" ) == 0 )
1003 {
1004 opt.min_version = SSL_MINOR_VERSION_3;
1005 opt.max_version = SSL_MINOR_VERSION_3;
1006 opt.transport = SSL_TRANSPORT_DATAGRAM;
1007 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001008 else
1009 goto usage;
1010 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001011 else if( strcmp( p, "auth_mode" ) == 0 )
1012 {
1013 if( strcmp( q, "none" ) == 0 )
1014 opt.auth_mode = SSL_VERIFY_NONE;
1015 else if( strcmp( q, "optional" ) == 0 )
1016 opt.auth_mode = SSL_VERIFY_OPTIONAL;
1017 else if( strcmp( q, "required" ) == 0 )
1018 opt.auth_mode = SSL_VERIFY_REQUIRED;
1019 else
1020 goto usage;
1021 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001022 else if( strcmp( p, "max_frag_len" ) == 0 )
1023 {
1024 if( strcmp( q, "512" ) == 0 )
1025 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
1026 else if( strcmp( q, "1024" ) == 0 )
1027 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
1028 else if( strcmp( q, "2048" ) == 0 )
1029 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
1030 else if( strcmp( q, "4096" ) == 0 )
1031 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
1032 else
1033 goto usage;
1034 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001035 else if( strcmp( p, "alpn" ) == 0 )
1036 {
1037 opt.alpn_string = q;
1038 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001039 else if( strcmp( p, "trunc_hmac" ) == 0 )
1040 {
1041 switch( atoi( q ) )
1042 {
1043 case 0: opt.trunc_hmac = SSL_TRUNC_HMAC_DISABLED; break;
1044 case 1: opt.trunc_hmac = SSL_TRUNC_HMAC_ENABLED; break;
1045 default: goto usage;
1046 }
1047 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001048 else if( strcmp( p, "extended_ms" ) == 0 )
1049 {
1050 switch( atoi( q ) )
1051 {
1052 case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break;
1053 case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break;
1054 default: goto usage;
1055 }
1056 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001057 else if( strcmp( p, "etm" ) == 0 )
1058 {
1059 switch( atoi( q ) )
1060 {
1061 case 0: opt.etm = SSL_ETM_DISABLED; break;
1062 case 1: opt.etm = SSL_ETM_ENABLED; break;
1063 default: goto usage;
1064 }
1065 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001066 else if( strcmp( p, "tickets" ) == 0 )
1067 {
1068 opt.tickets = atoi( q );
1069 if( opt.tickets < 0 || opt.tickets > 1 )
1070 goto usage;
1071 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001072 else if( strcmp( p, "ticket_timeout" ) == 0 )
1073 {
1074 opt.ticket_timeout = atoi( q );
1075 if( opt.ticket_timeout < 0 )
1076 goto usage;
1077 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001078 else if( strcmp( p, "cache_max" ) == 0 )
1079 {
1080 opt.cache_max = atoi( q );
1081 if( opt.cache_max < 0 )
1082 goto usage;
1083 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001084 else if( strcmp( p, "cache_timeout" ) == 0 )
1085 {
1086 opt.cache_timeout = atoi( q );
1087 if( opt.cache_timeout < 0 )
1088 goto usage;
1089 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001090 else if( strcmp( p, "cookies" ) == 0 )
1091 {
1092 opt.cookies = atoi( q );
1093 if( opt.cookies < -1 || opt.cookies > 1)
1094 goto usage;
1095 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001096 else if( strcmp( p, "anti_replay" ) == 0 )
1097 {
1098 opt.anti_replay = atoi( q );
1099 if( opt.anti_replay < 0 || opt.anti_replay > 1)
1100 goto usage;
1101 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001102 else if( strcmp( p, "badmac_limit" ) == 0 )
1103 {
1104 opt.badmac_limit = atoi( q );
1105 if( opt.badmac_limit < 0 )
1106 goto usage;
1107 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001108 else if( strcmp( p, "hs_timeout" ) == 0 )
1109 {
1110 if( ( p = strchr( q, '-' ) ) == NULL )
1111 goto usage;
1112 *p++ = '\0';
1113 opt.hs_to_min = atoi( q );
1114 opt.hs_to_max = atoi( p );
1115 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1116 goto usage;
1117 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001118 else if( strcmp( p, "sni" ) == 0 )
1119 {
1120 opt.sni = q;
1121 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001122 else
1123 goto usage;
1124 }
1125
Paul Bakkerc73079a2014-04-25 16:34:30 +02001126#if defined(POLARSSL_DEBUG_C)
1127 debug_set_threshold( opt.debug_level );
1128#endif
1129
Paul Bakkerc1516be2013-06-29 16:01:32 +02001130 if( opt.force_ciphersuite[0] > 0 )
1131 {
1132 const ssl_ciphersuite_t *ciphersuite_info;
1133 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1134
Paul Bakker5b55b792013-07-19 13:43:43 +02001135 if( opt.max_version != -1 &&
1136 ciphersuite_info->min_minor_ver > opt.max_version )
1137 {
1138 printf("forced ciphersuite not allowed with this protocol version\n");
1139 ret = 2;
1140 goto usage;
1141 }
1142 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001143 ciphersuite_info->max_minor_ver < opt.min_version )
1144 {
1145 printf("forced ciphersuite not allowed with this protocol version\n");
1146 ret = 2;
1147 goto usage;
1148 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001149
1150 /* If we select a version that's not supported by
1151 * this suite, then there will be no common ciphersuite... */
1152 if( opt.max_version == -1 ||
1153 opt.max_version > ciphersuite_info->max_minor_ver )
1154 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001155 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001156 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001157 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001158 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001159 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001160 /* DTLS starts with TLS 1.1 */
1161 if( opt.transport == SSL_TRANSPORT_DATAGRAM &&
1162 opt.min_version < SSL_MINOR_VERSION_2 )
1163 opt.min_version = SSL_MINOR_VERSION_2;
1164 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001165 }
1166
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001167 if( opt.version_suites != NULL )
1168 {
1169 const char *name[4] = { 0 };
1170
1171 /* Parse 4-element coma-separated list */
1172 for( i = 0, p = (char *) opt.version_suites;
1173 i < 4 && *p != '\0';
1174 i++ )
1175 {
1176 name[i] = p;
1177
1178 /* Terminate the current string and move on to next one */
1179 while( *p != ',' && *p != '\0' )
1180 p++;
1181 if( *p == ',' )
1182 *p++ = '\0';
1183 }
1184
1185 if( i != 4 )
1186 {
1187 printf( "too few values for version_suites\n" );
1188 ret = 1;
1189 goto exit;
1190 }
1191
1192 memset( version_suites, 0, sizeof( version_suites ) );
1193
1194 /* Get the suites identifiers from their name */
1195 for( i = 0; i < 4; i++ )
1196 {
1197 version_suites[i][0] = ssl_get_ciphersuite_id( name[i] );
1198
1199 if( version_suites[i][0] == 0 )
1200 {
1201 printf( "unknown ciphersuite: '%s'\n", name[i] );
1202 ret = 2;
1203 goto usage;
1204 }
1205 }
1206 }
1207
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001208#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001209 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001210 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001211 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001212 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001213 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001214 printf( "pre-shared key not valid hex\n" );
1215 goto exit;
1216 }
1217
1218 if( opt.psk_list != NULL )
1219 {
1220 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001221 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001222 printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001223 goto exit;
1224 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001225 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001226#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001227
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001228#if defined(POLARSSL_SSL_ALPN)
1229 if( opt.alpn_string != NULL )
1230 {
1231 p = (char *) opt.alpn_string;
1232 i = 0;
1233
1234 /* Leave room for a final NULL in alpn_list */
1235 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
1236 {
1237 alpn_list[i++] = p;
1238
1239 /* Terminate the current string and move on to next one */
1240 while( *p != ',' && *p != '\0' )
1241 p++;
1242 if( *p == ',' )
1243 *p++ = '\0';
1244 }
1245 }
1246#endif /* POLARSSL_SSL_ALPN */
1247
Paul Bakkerfbb17802013-04-17 19:10:21 +02001248 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001249 * 0. Initialize the RNG and the session data
1250 */
1251 printf( "\n . Seeding the random number generator..." );
1252 fflush( stdout );
1253
1254 entropy_init( &entropy );
1255 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001256 (const unsigned char *) pers,
1257 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001258 {
1259 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
1260 goto exit;
1261 }
1262
1263 printf( " ok\n" );
1264
Paul Bakker36713e82013-09-17 13:25:29 +02001265#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001266 /*
1267 * 1.1. Load the trusted CA
1268 */
1269 printf( " . Loading the CA root certificate ..." );
1270 fflush( stdout );
1271
1272#if defined(POLARSSL_FS_IO)
1273 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001274 if( strcmp( opt.ca_path, "none" ) == 0 )
1275 ret = 0;
1276 else
1277 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001278 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001279 if( strcmp( opt.ca_file, "none" ) == 0 )
1280 ret = 0;
1281 else
1282 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001283 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001284#endif
1285#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +02001286 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
1287 strlen( test_ca_list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001288#else
1289 {
1290 ret = 1;
1291 printf("POLARSSL_CERTS_C not defined.");
1292 }
1293#endif
1294 if( ret < 0 )
1295 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001296 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001297 goto exit;
1298 }
1299
1300 printf( " ok (%d skipped)\n", ret );
1301
1302 /*
1303 * 1.2. Load own certificate and private key
1304 */
1305 printf( " . Loading the server cert. and key..." );
1306 fflush( stdout );
1307
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001308#if defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001309 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001310 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001311 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001312 if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
1313 {
1314 printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n",
1315 -ret );
1316 goto exit;
1317 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001318 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001319 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001320 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001321 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001322 if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
1323 {
1324 printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret );
1325 goto exit;
1326 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001327 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001328 if( key_cert_init == 1 )
1329 {
1330 printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
1331 goto exit;
1332 }
1333
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001334 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001335 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001336 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001337 if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
1338 {
1339 printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n",
1340 -ret );
1341 goto exit;
1342 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001343 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001344 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001345 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001346 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001347 if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
1348 {
1349 printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n",
1350 -ret );
1351 goto exit;
1352 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001353 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001354 if( key_cert_init2 == 1 )
1355 {
1356 printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
1357 goto exit;
1358 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001359#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001360 if( key_cert_init == 0 &&
1361 strcmp( opt.crt_file, "none" ) != 0 &&
1362 strcmp( opt.key_file, "none" ) != 0 &&
1363 key_cert_init2 == 0 &&
1364 strcmp( opt.crt_file2, "none" ) != 0 &&
1365 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001366 {
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001367#if !defined(POLARSSL_CERTS_C)
1368 printf( "Not certificated or key provided, and \n"
1369 "POLARSSL_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001370 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001371#else
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001372#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001373 if( ( ret = x509_crt_parse( &srvcert,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001374 (const unsigned char *) test_srv_crt_rsa,
1375 strlen( test_srv_crt_rsa ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001376 {
1377 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
1378 goto exit;
1379 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001380 if( ( ret = pk_parse_key( &pkey,
1381 (const unsigned char *) test_srv_key_rsa,
1382 strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001383 {
1384 printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
1385 goto exit;
1386 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001387 key_cert_init = 2;
1388#endif /* POLARSSL_RSA_C */
1389#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001390 if( ( ret = x509_crt_parse( &srvcert2,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001391 (const unsigned char *) test_srv_crt_ec,
1392 strlen( test_srv_crt_ec ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001393 {
1394 printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
1395 goto exit;
1396 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001397 if( ( ret = pk_parse_key( &pkey2,
1398 (const unsigned char *) test_srv_key_ec,
1399 strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001400 {
1401 printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
1402 goto exit;
1403 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001404 key_cert_init2 = 2;
1405#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001406#endif /* POLARSSL_CERTS_C */
1407 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001408
1409 printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001410#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001411
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001412#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1413 if( opt.dhm_file != NULL )
1414 {
1415 printf( " . Loading DHM parameters..." );
1416 fflush( stdout );
1417
1418 if( ( ret = dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
1419 {
1420 printf( " failed\n ! dhm_parse_dhmfile returned -0x%04X\n\n",
1421 -ret );
1422 goto exit;
1423 }
1424
1425 printf( " ok\n" );
1426 }
1427#endif
1428
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001429#if defined(POLARSSL_SNI)
1430 if( opt.sni != NULL )
1431 {
1432 printf( " . Setting up SNI information..." );
1433 fflush( stdout );
1434
1435 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
1436 {
1437 printf( " failed\n" );
1438 goto exit;
1439 }
1440
1441 printf( " ok\n" );
1442 }
1443#endif /* POLARSSL_SNI */
1444
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001445 /*
1446 * 2. Setup the listening TCP socket
1447 */
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001448 printf( " . Bind on %s://%s:%-4d/ ...",
1449 opt.transport == SSL_TRANSPORT_STREAM ? "tcp" : "udp",
1450 opt.server_addr ? opt.server_addr : "*",
1451 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001452 fflush( stdout );
1453
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001454 if( ( ret = net_bind( &listen_fd, opt.server_addr, opt.server_port,
1455 opt.transport == SSL_TRANSPORT_STREAM ?
1456 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001457 {
1458 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1459 goto exit;
1460 }
1461
1462 printf( " ok\n" );
1463
1464 /*
1465 * 3. Setup stuff
1466 */
1467 printf( " . Setting up the SSL/TLS structure..." );
1468 fflush( stdout );
1469
1470 if( ( ret = ssl_init( &ssl ) ) != 0 )
1471 {
1472 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
1473 goto exit;
1474 }
1475
1476 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001477 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001478
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001479#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001480 if( ( ret = ssl_set_transport( &ssl, opt.transport ) ) != 0 )
1481 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001482 printf( " failed\n ! selected transport is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001483 goto exit;
1484 }
1485
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001486 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
1487 ssl_set_handshake_timeout( &ssl, opt.hs_to_min, opt.hs_to_max );
1488#endif /* POLARSSL_SSL_PROTO_DTLS */
1489
Paul Bakker05decb22013-08-15 13:33:48 +02001490#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001491 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1492 {
1493 printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
1494 goto exit;
1495 };
Paul Bakker05decb22013-08-15 13:33:48 +02001496#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001497
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001498#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1499 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
1500 ssl_set_truncated_hmac( &ssl, opt.trunc_hmac );
1501#endif
1502
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001503#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1504 if( opt.extended_ms != DFL_EXTENDED_MS )
1505 ssl_set_extended_master_secret( &ssl, opt.extended_ms );
1506#endif
1507
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001508#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1509 if( opt.etm != DFL_ETM )
1510 ssl_set_encrypt_then_mac( &ssl, opt.etm );
1511#endif
1512
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001513#if defined(POLARSSL_SSL_ALPN)
1514 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001515 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1516 {
1517 printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
1518 goto exit;
1519 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001520#endif
1521
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001522 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
1523 ssl_set_dbg( &ssl, my_debug, stdout );
1524
Paul Bakker0a597072012-09-25 21:55:46 +00001525#if defined(POLARSSL_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001526 if( opt.cache_max != -1 )
1527 ssl_cache_set_max_entries( &cache, opt.cache_max );
1528
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001529 if( opt.cache_timeout != -1 )
1530 ssl_cache_set_timeout( &cache, opt.cache_timeout );
1531
Paul Bakker0a597072012-09-25 21:55:46 +00001532 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
1533 ssl_cache_set, &cache );
1534#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001535
Paul Bakkera503a632013-08-14 13:48:06 +02001536#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001537 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1538 {
1539 printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
1540 goto exit;
1541 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001542
1543 if( opt.ticket_timeout != -1 )
1544 ssl_set_session_ticket_lifetime( &ssl, opt.ticket_timeout );
Paul Bakkera503a632013-08-14 13:48:06 +02001545#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001546
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001547#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001548 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001549 {
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001550#if defined(POLARSSL_SSL_COOKIE_C)
1551 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001552 {
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001553 if( ( ret = ssl_cookie_setup( &cookie_ctx,
1554 ctr_drbg_random, &ctr_drbg ) ) != 0 )
1555 {
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001556 printf( " failed\n ! ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001557 goto exit;
1558 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001559
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001560 ssl_set_dtls_cookies( &ssl, ssl_cookie_write, ssl_cookie_check,
1561 &cookie_ctx );
1562 }
1563 else
1564#endif /* POLARSSL_SSL_COOKIE_C */
1565#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
1566 if( opt.cookies == 0 )
1567 {
1568 ssl_set_dtls_cookies( &ssl, NULL, NULL, NULL );
1569 }
1570 else
1571#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
1572 {
1573 ; /* Nothing to do */
1574 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001575
1576#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1577 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001578 ssl_set_dtls_anti_replay( &ssl, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001579#endif
1580
1581#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
1582 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
1583 ssl_set_dtls_badmac_limit( &ssl, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001584#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001585 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001586#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001587
Paul Bakker41c83d32013-03-20 14:39:14 +01001588 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001589 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
1590
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001591 if( opt.version_suites != NULL )
1592 {
1593 ssl_set_ciphersuites_for_version( &ssl, version_suites[0],
1594 SSL_MAJOR_VERSION_3,
1595 SSL_MINOR_VERSION_0 );
1596 ssl_set_ciphersuites_for_version( &ssl, version_suites[1],
1597 SSL_MAJOR_VERSION_3,
1598 SSL_MINOR_VERSION_1 );
1599 ssl_set_ciphersuites_for_version( &ssl, version_suites[2],
1600 SSL_MAJOR_VERSION_3,
1601 SSL_MINOR_VERSION_2 );
1602 ssl_set_ciphersuites_for_version( &ssl, version_suites[3],
1603 SSL_MAJOR_VERSION_3,
1604 SSL_MINOR_VERSION_3 );
1605 }
1606
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001607 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
1608 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001609#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001610 ssl_set_renegotiation( &ssl, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001611
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001612 if( opt.renego_delay != DFL_RENEGO_DELAY )
1613 ssl_set_renegotiation_enforced( &ssl, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001614
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001615 if( opt.renego_period != DFL_RENEGO_PERIOD )
1616 {
1617 renego_period[7] = opt.renego_period;
1618 ssl_set_renegotiation_period( &ssl, renego_period );
1619 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001620#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001621
Paul Bakker36713e82013-09-17 13:25:29 +02001622#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001623 if( strcmp( opt.ca_path, "none" ) != 0 &&
1624 strcmp( opt.ca_file, "none" ) != 0 )
1625 {
1626 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
1627 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001628 if( key_cert_init )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001629 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
1630 {
1631 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1632 goto exit;
1633 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001634 if( key_cert_init2 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001635 if( ( ret = ssl_set_own_cert( &ssl, &srvcert2, &pkey2 ) ) != 0 )
1636 {
1637 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1638 goto exit;
1639 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001640#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001641
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001642#if defined(POLARSSL_SNI)
1643 if( opt.sni != NULL )
1644 ssl_set_sni( &ssl, sni_callback, sni_info );
1645#endif
1646
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001647#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001648 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
1649 {
1650 ret = ssl_set_psk( &ssl, psk, psk_len,
1651 (const unsigned char *) opt.psk_identity,
1652 strlen( opt.psk_identity ) );
1653 if( ret != 0 )
1654 {
1655 printf( " failed\n ssl_set_psk returned -0x%04X\n\n", - ret );
1656 goto exit;
1657 }
1658 }
1659
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001660 if( opt.psk_list != NULL )
1661 ssl_set_psk_cb( &ssl, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02001662#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001663
1664#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00001665 /*
1666 * Use different group than default DHM group
1667 */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001668#if defined(POLARSSL_FS_IO)
1669 if( opt.dhm_file != NULL )
1670 ret = ssl_set_dh_param_ctx( &ssl, &dhm );
1671 else
1672#endif
1673 ret = ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
1674 POLARSSL_DHM_RFC5114_MODP_2048_G );
1675
1676 if( ret != 0 )
1677 {
1678 printf( " failed\n ssl_set_dh_param returned -0x%04X\n\n", - ret );
1679 goto exit;
1680 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001681#endif
1682
Paul Bakker1d29fb52012-09-28 13:28:45 +00001683 if( opt.min_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001684 {
1685 ret = ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1686 if( ret != 0 )
1687 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001688 printf( " failed\n ! selected min_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001689 goto exit;
1690 }
1691 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001692
Paul Bakkerc1516be2013-06-29 16:01:32 +02001693 if( opt.max_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001694 {
1695 ret = ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1696 if( ret != 0 )
1697 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001698 printf( " failed\n ! selected max_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001699 goto exit;
1700 }
1701 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001702
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001703 printf( " ok\n" );
1704
1705reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02001706#if !defined(_WIN32)
1707 if( received_sigterm )
1708 {
1709 printf( " interrupted by SIGTERM\n" );
1710 ret = 0;
1711 goto exit;
1712 }
1713#endif
1714
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001715#ifdef POLARSSL_ERROR_C
1716 if( ret != 0 )
1717 {
1718 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001719 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001720 printf("Last error was: %d - %s\n\n", ret, error_buf );
1721 }
1722#endif
1723
1724 if( client_fd != -1 )
Manuel Pégourié-Gonnard4ba6ab62014-08-07 17:21:47 +02001725 net_close( client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001726
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001727 ssl_session_reset( &ssl );
1728
1729 /*
1730 * 3. Wait until a client connects
1731 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001732 client_fd = -1;
1733
1734 printf( " . Waiting for a remote connection ..." );
1735 fflush( stdout );
1736
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001737 if( ( ret = net_accept( listen_fd, &client_fd, client_ip ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001738 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02001739#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001740 if( received_sigterm )
1741 {
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001742 printf( " interrupted by signal\n" );
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001743 ret = 0;
1744 goto exit;
1745 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02001746#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001747
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001748 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
1749 goto exit;
1750 }
1751
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001752 if( opt.nbio > 0 )
1753 ret = net_set_nonblock( client_fd );
1754 else
1755 ret = net_set_block( client_fd );
1756 if( ret != 0 )
1757 {
1758 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
1759 goto exit;
1760 }
1761
1762 if( opt.nbio == 2 )
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001763 ssl_set_bio_timeout( &ssl, &client_fd, my_send, my_recv, NULL, 0 );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001764 else
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001765 ssl_set_bio_timeout( &ssl, &client_fd, net_send, net_recv,
1766#if defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnardf0365122014-09-29 16:11:47 +02001767 opt.nbio == 0 ? net_recv_timeout : NULL,
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001768#else
1769 NULL,
1770#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001771 opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001772
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001773#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001774 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
1775 {
1776 if( ( ret = ssl_set_client_transport_id( &ssl, client_ip,
1777 sizeof( client_ip ) ) ) != 0 )
1778 {
1779 printf( " failed\n ! "
1780 "ssl_set_client_tranport_id() returned -0x%x\n\n", -ret );
1781 goto exit;
1782 }
1783 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001784#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001785
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001786 printf( " ok\n" );
1787
1788 /*
Manuel Pégourié-Gonnardbd97fdb2014-09-26 16:46:36 +02001789 * With UDP, bind_fd is hijacked by client_fd, so bind a new one
1790 */
1791#if defined(POLARSSL_SSL_PROTO_DTLS)
1792 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
1793 {
1794 printf( " . Re-bind on udp://%s:%-4d/ ...",
1795 opt.server_addr ? opt.server_addr : "*",
1796 opt.server_port );
1797 fflush( stdout );
1798
1799 if( ( ret = net_bind( &listen_fd, opt.server_addr,
1800 opt.server_port, NET_PROTO_UDP ) ) != 0 )
1801 {
1802 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1803 goto exit;
1804 }
1805
1806 printf( " ok\n" );
1807 }
1808#endif /* POLARSSL_SSL_PROTO_DTLS */
1809
1810 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001811 * 4. Handshake
1812 */
1813 printf( " . Performing the SSL/TLS handshake..." );
1814 fflush( stdout );
1815
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001816 do ret = ssl_handshake( &ssl );
1817 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1818 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001819
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001820 if( ret == POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED )
1821 {
1822 printf( " hello verification requested\n" );
1823 ret = 0;
1824 goto reset;
1825 }
1826 else if( ret != 0 )
1827 {
1828 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
1829 goto reset;
1830 }
1831 else /* ret == 0 */
1832 {
1833 printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1834 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
1835 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001836
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001837 if( ( ret = ssl_get_record_expansion( &ssl ) ) >= 0 )
1838 printf( " [ Record expansion is %d ]\n", ret );
1839 else
1840 printf( " [ Record expansion is unknown (compression) ]\n" );
1841
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001842#if defined(POLARSSL_SSL_ALPN)
1843 if( opt.alpn_string != NULL )
1844 {
1845 const char *alp = ssl_get_alpn_protocol( &ssl );
1846 printf( " [ Application Layer Protocol is %s ]\n",
1847 alp ? alp : "(none)" );
1848 }
1849#endif
1850
Paul Bakker36713e82013-09-17 13:25:29 +02001851#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001852 /*
1853 * 5. Verify the server certificate
1854 */
1855 printf( " . Verifying peer X.509 certificate..." );
1856
1857 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1858 {
1859 printf( " failed\n" );
1860
Paul Bakkerb0550d92012-10-30 07:51:03 +00001861 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001862 printf( " ! no client certificate sent\n" );
1863
1864 if( ( ret & BADCERT_EXPIRED ) != 0 )
1865 printf( " ! client certificate has expired\n" );
1866
1867 if( ( ret & BADCERT_REVOKED ) != 0 )
1868 printf( " ! client certificate has been revoked\n" );
1869
1870 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
1871 printf( " ! self-signed or not signed by a trusted CA\n" );
1872
1873 printf( "\n" );
1874 }
1875 else
1876 printf( " ok\n" );
1877
Paul Bakkerb0550d92012-10-30 07:51:03 +00001878 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001879 {
1880 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001881 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1882 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001883 printf( "%s\n", buf );
1884 }
Paul Bakker36713e82013-09-17 13:25:29 +02001885#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001886
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001887 if( opt.exchanges == 0 )
1888 goto close_notify;
1889
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001890 exchanges = opt.exchanges;
1891data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001892 /*
1893 * 6. Read the HTTP Request
1894 */
1895 printf( " < Read from client:" );
1896 fflush( stdout );
1897
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001898 /*
1899 * TLS and DTLS need different reading styles (stream vs datagram)
1900 */
1901 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001902 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001903 do
1904 {
1905 int terminated = 0;
1906 len = sizeof( buf ) - 1;
1907 memset( buf, 0, sizeof( buf ) );
1908 ret = ssl_read( &ssl, buf, len );
1909
1910 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1911 ret == POLARSSL_ERR_NET_WANT_WRITE )
1912 continue;
1913
1914 if( ret <= 0 )
1915 {
1916 switch( ret )
1917 {
1918 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1919 printf( " connection was closed gracefully\n" );
1920 goto close_notify;
1921
1922 case 0:
1923 case POLARSSL_ERR_NET_CONN_RESET:
1924 printf( " connection was reset by peer\n" );
1925 ret = POLARSSL_ERR_NET_CONN_RESET;
1926 goto reset;
1927
1928 default:
1929 printf( " ssl_read returned -0x%x\n", -ret );
1930 goto reset;
1931 }
1932 }
1933
1934 if( ssl_get_bytes_avail( &ssl ) == 0 )
1935 {
1936 len = ret;
1937 buf[len] = '\0';
1938 printf( " %d bytes read\n\n%s\n", len, (char *) buf );
1939
1940 /* End of message should be detected according to the syntax of the
1941 * application protocol (eg HTTP), just use a dummy test here. */
1942 if( buf[len - 1] == '\n' )
1943 terminated = 1;
1944 }
1945 else
1946 {
1947 int extra_len, ori_len;
1948 unsigned char *larger_buf;
1949
1950 ori_len = ret;
1951 extra_len = ssl_get_bytes_avail( &ssl );
1952
1953 larger_buf = polarssl_malloc( ori_len + extra_len + 1 );
1954 if( larger_buf == NULL )
1955 {
1956 printf( " ! memory allocation failed\n" );
1957 ret = 1;
1958 goto reset;
1959 }
1960
1961 memset( larger_buf, 0, ori_len + extra_len );
1962 memcpy( larger_buf, buf, ori_len );
1963
1964 /* This read should never fail and get the whole cached data */
1965 ret = ssl_read( &ssl, larger_buf + ori_len, extra_len );
1966 if( ret != extra_len ||
1967 ssl_get_bytes_avail( &ssl ) != 0 )
1968 {
1969 printf( " ! ssl_read failed on cached data\n" );
1970 ret = 1;
1971 goto reset;
1972 }
1973
1974 larger_buf[ori_len + extra_len] = '\0';
1975 printf( " %u bytes read (%u + %u)\n\n%s\n",
1976 ori_len + extra_len, ori_len, extra_len,
1977 (char *) larger_buf );
1978
1979 /* End of message should be detected according to the syntax of the
1980 * application protocol (eg HTTP), just use a dummy test here. */
1981 if( larger_buf[ori_len + extra_len - 1] == '\n' )
1982 terminated = 1;
1983
1984 polarssl_free( larger_buf );
1985 }
1986
1987 if( terminated )
1988 {
1989 ret = 0;
1990 break;
1991 }
1992 }
1993 while( 1 );
1994 }
1995 else /* Not stream, so datagram */
1996 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001997 len = sizeof( buf ) - 1;
1998 memset( buf, 0, sizeof( buf ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001999
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002000 do ret = ssl_read( &ssl, buf, len );
2001 while( ret == POLARSSL_ERR_NET_WANT_READ ||
2002 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002003
2004 if( ret <= 0 )
2005 {
2006 switch( ret )
2007 {
2008 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
2009 printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002010 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002011 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002012
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002013 default:
2014 printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002015 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002016 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002017 }
2018
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002019 len = ret;
2020 buf[len] = '\0';
2021 printf( " %d bytes read\n\n%s", len, (char *) buf );
2022 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002023 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002024
2025 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002026 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02002027 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002028 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002029#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02002030 if( opt.renegotiate && exchanges == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002031 {
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002032 printf( " . Requestion renegotiation..." );
2033 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002034
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002035 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
2036 {
2037 if( ret != POLARSSL_ERR_NET_WANT_READ &&
2038 ret != POLARSSL_ERR_NET_WANT_WRITE )
2039 {
2040 printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
2041 goto reset;
2042 }
2043 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002044
2045 printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002046 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002047#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002048
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002049 /*
2050 * 7. Write the 200 Response
2051 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002052 printf( " > Write to client:" );
2053 fflush( stdout );
2054
2055 len = sprintf( (char *) buf, HTTP_RESPONSE,
2056 ssl_get_ciphersuite( &ssl ) );
2057
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002058 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002059 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002060 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002061 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002062 while( ( ret = ssl_write( &ssl, buf + written, len - written ) )
2063 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002064 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002065 if( ret == POLARSSL_ERR_NET_CONN_RESET )
2066 {
2067 printf( " failed\n ! peer closed the connection\n\n" );
2068 goto reset;
2069 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002070
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002071 if( ret != POLARSSL_ERR_NET_WANT_READ &&
2072 ret != POLARSSL_ERR_NET_WANT_WRITE )
2073 {
2074 printf( " failed\n ! ssl_write returned %d\n\n", ret );
2075 goto reset;
2076 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002077 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002078 }
2079 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002080 else /* Not stream, so datagram */
2081 {
2082 do ret = ssl_write( &ssl, buf, len );
2083 while( ret == POLARSSL_ERR_NET_WANT_READ ||
2084 ret == POLARSSL_ERR_NET_WANT_WRITE );
2085
2086 if( ret < 0 )
2087 {
2088 printf( " failed\n ! ssl_write returned %d\n\n", ret );
2089 goto reset;
2090 }
2091
2092 frags = 1;
2093 written = ret;
2094 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002095
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002096 buf[written] = '\0';
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002097 printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Manuel Pégourié-Gonnarda92ed482015-01-14 10:46:08 +01002098 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002099
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002100 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002101 * 7b. Continue doing data exchanges?
2102 */
2103 if( --exchanges > 0 )
2104 goto data_exchange;
2105
2106 /*
2107 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002108 */
2109close_notify:
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002110 printf( " . Closing the connection..." );
2111
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002112 /* No error checking, the connection might be closed already */
2113 do
2114 ret = ssl_close_notify( &ssl );
2115 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
2116 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002117
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002118 printf( " done\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002119 goto reset;
2120
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002121 /*
2122 * Cleanup and exit
2123 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002124exit:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002125#ifdef POLARSSL_ERROR_C
2126 if( ret != 0 )
2127 {
2128 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02002129 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002130 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
2131 }
2132#endif
2133
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01002134 printf( " . Cleaning up..." );
2135 fflush( stdout );
2136
Paul Bakker0c226102014-04-17 16:02:36 +02002137 if( client_fd != -1 )
2138 net_close( client_fd );
2139
Paul Bakkera317a982014-06-18 16:44:11 +02002140#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
2141 dhm_free( &dhm );
2142#endif
Paul Bakker36713e82013-09-17 13:25:29 +02002143#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker36713e82013-09-17 13:25:29 +02002144 x509_crt_free( &cacert );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002145 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02002146 pk_free( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002147 x509_crt_free( &srvcert2 );
2148 pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02002149#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002150#if defined(POLARSSL_SNI)
2151 sni_free( sni_info );
2152#endif
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02002153#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
2154 psk_free( psk_info );
2155#endif
2156#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
2157 dhm_free( &dhm );
2158#endif
Paul Bakkered27a042013-04-18 22:46:23 +02002159
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002160 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02002161 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02002162 entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002163
Paul Bakker0a597072012-09-25 21:55:46 +00002164#if defined(POLARSSL_SSL_CACHE_C)
2165 ssl_cache_free( &cache );
2166#endif
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +02002167#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002168 ssl_cookie_free( &cookie_ctx );
2169#endif
Paul Bakker0a597072012-09-25 21:55:46 +00002170
Paul Bakker1337aff2013-09-29 14:45:34 +02002171#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
2172#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker82024bf2013-07-04 11:52:32 +02002173 memory_buffer_alloc_status();
2174#endif
Paul Bakker1337aff2013-09-29 14:45:34 +02002175 memory_buffer_alloc_free();
2176#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02002177
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01002178 printf( " done.\n" );
2179
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002180#if defined(_WIN32)
2181 printf( " + Press Enter to exit this program.\n" );
2182 fflush( stdout ); getchar();
2183#endif
2184
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002185 // Shell can not handle large exit numbers -> 1 for errors
2186 if( ret < 0 )
2187 ret = 1;
2188
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002189 return( ret );
2190}
2191#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
2192 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
2193 POLARSSL_CTR_DRBG_C */