blob: 5cb086a770cca8845c31ed9f47b7194bb27fc51c [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
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000109#define DFL_ALLOW_LEGACY SSL_LEGACY_NO_RENEGOTIATION
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é-Gonnard67686c42014-08-15 11:17:27 +0200112#define DFL_EXCHANGES 1
Paul Bakker1d29fb52012-09-28 13:28:45 +0000113#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200114#define DFL_MAX_VERSION -1
Paul Bakker91ebfb52012-11-23 14:04:08 +0100115#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200116#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200117#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100118#define DFL_TICKET_TIMEOUT -1
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100119#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100120#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100121#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200122#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200123#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100124#define DFL_TRANSPORT SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200125#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200126#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200127#define DFL_HS_TO_MIN 0
128#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200129#define DFL_BADMAC_LIMIT -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200130#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100131#define DFL_ETM -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000132
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200133#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
134 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
135 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
136 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
137 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
138 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
139 "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 +0200140
Paul Bakker8e714d72013-07-18 11:05:13 +0200141/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
142 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000143#define HTTP_RESPONSE \
144 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
145 "<h2>PolarSSL Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200146 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000147
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200148/*
149 * Size of the basic I/O buffer. Able to hold our default response.
150 *
151 * You will need to adapt the ssl_get_bytes_avail() test in ssl-opt.sh
152 * if you change this value to something outside the range <= 100 or > 500
153 */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200154#define IO_BUF_LEN 200
155
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000156/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000157 * global options
158 */
159struct options
160{
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100161 const char *server_addr; /* address on which the ssl service runs */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000162 int server_port; /* port on which the ssl service runs */
163 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100164 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200165 uint32_t read_timeout; /* timeout on ssl_read() in milliseconds */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200166 const char *ca_file; /* the file with the CA certificate(s) */
167 const char *ca_path; /* the path with the CA certificate(s) reside */
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200168 const char *crt_file; /* the file with the server certificate */
169 const char *key_file; /* the file with the server key */
170 const char *crt_file2; /* the file with the 2nd server certificate */
171 const char *key_file2; /* the file with the 2nd server key */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200172 const char *psk; /* the pre-shared key */
173 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200174 char *psk_list; /* list of PSK id/key pairs for callback */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000175 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200176 const char *version_suites; /* per-version ciphersuites */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000177 int renegotiation; /* enable / disable renegotiation */
178 int allow_legacy; /* allow legacy renegotiation */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100179 int renegotiate; /* attempt renegotiation? */
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200180 int renego_delay; /* delay before enforcing renegotiation */
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200181 int exchanges; /* number of data exchanges */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000182 int min_version; /* minimum protocol version accepted */
Paul Bakkerc1516be2013-06-29 16:01:32 +0200183 int max_version; /* maximum protocol version accepted */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100184 int auth_mode; /* verify mode for connection */
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200185 unsigned char mfl_code; /* code for maximum fragment length */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200186 int tickets; /* enable / disable session tickets */
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100187 int ticket_timeout; /* session ticket lifetime */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100188 int cache_max; /* max number of session cache entries */
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100189 int cache_timeout; /* expiration delay of session cache entries */
Paul Bakker1ebc0c52014-05-22 15:47:58 +0200190 char *sni; /* string describing sni information */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200191 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200192 const char *dhm_file; /* the file with the DH parameters */
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100193 int transport; /* TLS or DTLS? */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200194 int cookies; /* Use cookies for DTLS? -1 to break them */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200195 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200196 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
197 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200198 int badmac_limit; /* Limit of records with bad MAC */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200199 char extended_ms; /* allow negotiation of extended MS? */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100200 char etm; /* allow negotiation of encrypt-then-MAC? */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000201} opt;
202
Paul Bakker3c5ef712013-06-25 16:37:45 +0200203static void my_debug( void *ctx, int level, const char *str )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000204{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200205 ((void) level);
206
207 fprintf( (FILE *) ctx, "%s", str );
208 fflush( (FILE *) ctx );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000209}
210
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100211/*
212 * Test recv/send functions that make sure each try returns
213 * WANT_READ/WANT_WRITE at least once before sucesseding
214 */
215static int my_recv( void *ctx, unsigned char *buf, size_t len )
216{
217 static int first_try = 1;
218 int ret;
219
220 if( first_try )
221 {
222 first_try = 0;
223 return( POLARSSL_ERR_NET_WANT_READ );
224 }
225
226 ret = net_recv( ctx, buf, len );
227 if( ret != POLARSSL_ERR_NET_WANT_READ )
228 first_try = 1; /* Next call will be a new operation */
229 return( ret );
230}
231
232static int my_send( void *ctx, const unsigned char *buf, size_t len )
233{
234 static int first_try = 1;
235 int ret;
236
237 if( first_try )
238 {
239 first_try = 0;
240 return( POLARSSL_ERR_NET_WANT_WRITE );
241 }
242
243 ret = net_send( ctx, buf, len );
244 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
245 first_try = 1; /* Next call will be a new operation */
246 return( ret );
247}
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200248
249#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000250#if defined(POLARSSL_FS_IO)
251#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100252 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
253 " default: \"\" (pre-loaded)\n" \
254 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
255 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
256 " 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 +0200257 " default: see note after key_file2\n" \
258 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200259 " 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 +0200260 " default: see note after key_file2\n" \
261 " key_file2=%%s default: see note below\n" \
262 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200263 " preloaded certificate(s) and key(s) are used if available\n" \
264 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
265 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000266#else
267#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200268 "\n" \
269 " No file operations available (POLARSSL_FS_IO not defined)\n" \
270 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000271#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200272#else
273#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200274#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200275
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200276#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200277#define USAGE_PSK \
278 " psk=%%s default: \"\" (in hex, without 0x)\n" \
279 " psk_identity=%%s default: \"Client_identity\"\n"
280#else
281#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200282#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000283
Paul Bakkera503a632013-08-14 13:48:06 +0200284#if defined(POLARSSL_SSL_SESSION_TICKETS)
285#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100286 " tickets=%%d default: 1 (enabled)\n" \
287 " ticket_timeout=%%d default: ticket default (1d)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200288#else
289#define USAGE_TICKETS ""
290#endif /* POLARSSL_SSL_SESSION_TICKETS */
291
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100292#if defined(POLARSSL_SSL_CACHE_C)
293#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100294 " cache_max=%%d default: cache default (50)\n" \
295 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100296#else
297#define USAGE_CACHE ""
298#endif /* POLARSSL_SSL_CACHE_C */
299
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100300#if defined(POLARSSL_SNI)
301#define USAGE_SNI \
302 " sni=%%s name1,cert1,key1[,name2,cert2,key2[,...]]\n" \
303 " default: disabled\n"
304#else
305#define USAGE_SNI ""
306#endif /* POLARSSL_SNI */
307
Paul Bakker05decb22013-08-15 13:33:48 +0200308#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
309#define USAGE_MAX_FRAG_LEN \
310 " max_frag_len=%%d default: 16384 (tls default)\n" \
311 " options: 512, 1024, 2048, 4096\n"
312#else
313#define USAGE_MAX_FRAG_LEN ""
314#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
315
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200316#if defined(POLARSSL_SSL_ALPN)
317#define USAGE_ALPN \
318 " alpn=%%s default: \"\" (disabled)\n" \
319 " example: spdy/1,http/1.1\n"
320#else
321#define USAGE_ALPN ""
322#endif /* POLARSSL_SSL_ALPN */
323
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200324#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
325#define USAGE_COOKIES \
326 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200327 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200328#else
329#define USAGE_COOKIES ""
330#endif
331
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200332#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
333#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200334 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200335#else
336#define USAGE_ANTI_REPLAY ""
337#endif
338
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200339#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
340#define USAGE_BADMAC_LIMIT \
341 " badmac_limit=%%d default: (library default: disabled)\n"
342#else
343#define USAGE_BADMAC_LIMIT ""
344#endif
345
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200346#if defined(POLARSSL_SSL_PROTO_DTLS)
347#define USAGE_DTLS \
348 " dtls=%%d default: 0 (TLS)\n" \
349 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
350 " range of DTLS handshake timeouts in millisecs\n"
351#else
352#define USAGE_DTLS ""
353#endif
354
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200355#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
356#define USAGE_EMS \
357 " extended_ms=0/1 default: (library default: on)\n"
358#else
359#define USAGE_EMS ""
360#endif
361
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100362#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
363#define USAGE_ETM \
364 " etm=0/1 default: (library default: on)\n"
365#else
366#define USAGE_ETM ""
367#endif
368
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000369#define USAGE \
370 "\n usage: ssl_server2 param=<>...\n" \
371 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100372 " server_addr=%%d default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000373 " server_port=%%d default: 4433\n" \
374 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100375 " nbio=%%d default: 0 (blocking I/O)\n" \
376 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200377 " read_timeout=%%d default: 0 (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100378 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200379 USAGE_DTLS \
380 USAGE_COOKIES \
381 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200382 USAGE_BADMAC_LIMIT \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200383 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100384 " auth_mode=%%s default: \"optional\"\n" \
385 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000386 USAGE_IO \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100387 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100388 "\n" \
389 USAGE_PSK \
390 "\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000391 " renegotiation=%%d default: 1 (enabled)\n" \
392 " allow_legacy=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100393 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200394 " renego_delay=%%d default: -2 (library default)\n" \
395 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200396 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100397 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100398 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100399 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200400 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200401 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100402 USAGE_ETM \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100403 "\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000404 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200405 " max_version=%%s default: \"tls1_2\"\n" \
406 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100407 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200408 "\n" \
409 " version_suites=a,b,c,d per-version ciphersuites\n" \
410 " in order from ssl3 to tls1_2\n" \
411 " default: all enabled\n" \
412 " force_ciphersuite=<name> default: all enabled\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000413 " acceptable ciphersuite names:\n"
414
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200415/*
416 * Used by sni_parse and psk_parse to handle coma-separated lists
417 */
418#define GET_ITEM( dst ) \
419 dst = p; \
420 while( *p != ',' ) \
421 if( ++p > end ) \
422 return( NULL ); \
423 *p++ = '\0';
424
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100425#if defined(POLARSSL_SNI)
426typedef struct _sni_entry sni_entry;
427
428struct _sni_entry {
429 const char *name;
430 x509_crt *cert;
431 pk_context *key;
432 sni_entry *next;
433};
434
435/*
436 * Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]]
437 * into a usable sni_entry list.
438 *
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200439 * Modifies the input string! This is not production quality!
440 * (leaks memory if parsing fails, no error reporting, ...)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100441 */
442sni_entry *sni_parse( char *sni_string )
443{
444 sni_entry *cur = NULL, *new = NULL;
445 char *p = sni_string;
446 char *end = p;
447 char *crt_file, *key_file;
448
449 while( *end != '\0' )
450 ++end;
451 *end = ',';
452
453 while( p <= end )
454 {
455 if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL )
456 return( NULL );
457
458 memset( new, 0, sizeof( sni_entry ) );
459
460 if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ||
461 ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200462 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100463
464 x509_crt_init( new->cert );
465 pk_init( new->key );
466
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200467 GET_ITEM( new->name );
468 GET_ITEM( crt_file );
469 GET_ITEM( key_file );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100470
471 if( x509_crt_parse_file( new->cert, crt_file ) != 0 ||
472 pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200473 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100474
475 new->next = cur;
476 cur = new;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100477 }
478
479 return( cur );
480}
481
482void sni_free( sni_entry *head )
483{
484 sni_entry *cur = head, *next;
485
486 while( cur != NULL )
487 {
488 x509_crt_free( cur->cert );
489 polarssl_free( cur->cert );
490
491 pk_free( cur->key );
492 polarssl_free( cur->key );
493
494 next = cur->next;
495 polarssl_free( cur );
496 cur = next;
497 }
498}
499
500/*
501 * SNI callback.
502 */
503int sni_callback( void *p_info, ssl_context *ssl,
504 const unsigned char *name, size_t name_len )
505{
506 sni_entry *cur = (sni_entry *) p_info;
507
508 while( cur != NULL )
509 {
510 if( name_len == strlen( cur->name ) &&
511 memcmp( name, cur->name, name_len ) == 0 )
512 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200513 return( ssl_set_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100514 }
515
516 cur = cur->next;
517 }
518
519 return( -1 );
520}
521
522#endif /* POLARSSL_SNI */
523
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200524#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
525
526#define HEX2NUM( c ) \
527 if( c >= '0' && c <= '9' ) \
528 c -= '0'; \
529 else if( c >= 'a' && c <= 'f' ) \
530 c -= 'a' - 10; \
531 else if( c >= 'A' && c <= 'F' ) \
532 c -= 'A' - 10; \
533 else \
534 return( -1 );
535
536/*
537 * Convert a hex string to bytes.
538 * Return 0 on success, -1 on error.
539 */
540int unhexify( unsigned char *output, const char *input, size_t *olen )
541{
542 unsigned char c;
543 size_t j;
544
545 *olen = strlen( input );
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200546 if( *olen % 2 != 0 || *olen / 2 > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200547 return( -1 );
548 *olen /= 2;
549
550 for( j = 0; j < *olen * 2; j += 2 )
551 {
552 c = input[j];
553 HEX2NUM( c );
554 output[ j / 2 ] = c << 4;
555
556 c = input[j + 1];
557 HEX2NUM( c );
558 output[ j / 2 ] |= c;
559 }
560
561 return( 0 );
562}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200563
564typedef struct _psk_entry psk_entry;
565
566struct _psk_entry
567{
568 const char *name;
569 size_t key_len;
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200570 unsigned char key[POLARSSL_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200571 psk_entry *next;
572};
573
574/*
575 * Parse a string of pairs name1,key1[,name2,key2[,...]]
576 * into a usable psk_entry list.
577 *
578 * Modifies the input string! This is not production quality!
579 * (leaks memory if parsing fails, no error reporting, ...)
580 */
581psk_entry *psk_parse( char *psk_string )
582{
583 psk_entry *cur = NULL, *new = NULL;
584 char *p = psk_string;
585 char *end = p;
586 char *key_hex;
587
588 while( *end != '\0' )
589 ++end;
590 *end = ',';
591
592 while( p <= end )
593 {
594 if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL )
595 return( NULL );
596
597 memset( new, 0, sizeof( psk_entry ) );
598
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200599 GET_ITEM( new->name );
600 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200601
602 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
603 return( NULL );
604
605 new->next = cur;
606 cur = new;
607 }
608
609 return( cur );
610}
611
612/*
613 * Free a list of psk_entry's
614 */
615void psk_free( psk_entry *head )
616{
617 psk_entry *next;
618
619 while( head != NULL )
620 {
621 next = head->next;
622 polarssl_free( head );
623 head = next;
624 }
625}
626
627/*
628 * PSK callback
629 */
630int psk_callback( void *p_info, ssl_context *ssl,
631 const unsigned char *name, size_t name_len )
632{
633 psk_entry *cur = (psk_entry *) p_info;
634
635 while( cur != NULL )
636 {
637 if( name_len == strlen( cur->name ) &&
638 memcmp( name, cur->name, name_len ) == 0 )
639 {
640 return( ssl_set_psk( ssl, cur->key, cur->key_len,
641 name, name_len ) );
642 }
643
644 cur = cur->next;
645 }
646
647 return( -1 );
648}
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200649#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
650
Manuel Pégourié-Gonnarda9d7d032014-10-09 16:07:08 +0200651static int listen_fd, client_fd = -1;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200652
653/* Interruption handler to ensure clean exit (for valgrind testing) */
654#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200655static int received_sigterm = 0;
656void term_handler( int sig )
657{
658 ((void) sig);
659 received_sigterm = 1;
660 net_close( listen_fd ); /* causes net_accept() to abort */
Manuel Pégourié-Gonnarda9d7d032014-10-09 16:07:08 +0200661 net_close( client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200662}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200663#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200664
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000665int main( int argc, char *argv[] )
666{
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200667 int ret = 0, len, written, frags, exchanges;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200668 int version_suites[4][2];
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200669 unsigned char buf[IO_BUF_LEN];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200670#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200671 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +0200672 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +0200673 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +0200674#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200675 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +0200676 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +0200677#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200678 ssl_cookie_ctx cookie_ctx;
679#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000680
681 entropy_context entropy;
682 ctr_drbg_context ctr_drbg;
683 ssl_context ssl;
Paul Bakker36713e82013-09-17 13:25:29 +0200684#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200685 x509_crt cacert;
686 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200687 pk_context pkey;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200688 x509_crt srvcert2;
689 pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200690 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200691#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200692#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
693 dhm_context dhm;
694#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000695#if defined(POLARSSL_SSL_CACHE_C)
696 ssl_cache_context cache;
697#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100698#if defined(POLARSSL_SNI)
699 sni_entry *sni_info = NULL;
700#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200701#if defined(POLARSSL_SSL_ALPN)
702 const char *alpn_list[10];
703#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200704#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
705 unsigned char alloc_buf[100000];
706#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000707
708 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000709 char *p, *q;
710 const int *list;
711
Paul Bakker82024bf2013-07-04 11:52:32 +0200712#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
713 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
714#endif
715
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000716 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200717 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000718 */
719 listen_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200720 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200721#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200722 x509_crt_init( &cacert );
723 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200724 pk_init( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200725 x509_crt_init( &srvcert2 );
726 pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200727#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200728#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
Paul Bakkera317a982014-06-18 16:44:11 +0200729 dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200730#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000731#if defined(POLARSSL_SSL_CACHE_C)
732 ssl_cache_init( &cache );
733#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200734#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200735 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200736#endif
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +0200737#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200738 ssl_cookie_init( &cookie_ctx );
739#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000740
Paul Bakkerc1283d32014-08-18 11:05:51 +0200741#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200742 /* Abort cleanly on SIGTERM */
743 signal( SIGTERM, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +0200744#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200745
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000746 if( argc == 0 )
747 {
748 usage:
749 if( ret == 0 )
750 ret = 1;
751
752 printf( USAGE );
753
754 list = ssl_list_ciphersuites();
755 while( *list )
756 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200757 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200758 list++;
759 if( !*list )
760 break;
761 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000762 list++;
763 }
764 printf("\n");
765 goto exit;
766 }
767
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100768 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000769 opt.server_port = DFL_SERVER_PORT;
770 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100771 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200772 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000773 opt.ca_file = DFL_CA_FILE;
774 opt.ca_path = DFL_CA_PATH;
775 opt.crt_file = DFL_CRT_FILE;
776 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200777 opt.crt_file2 = DFL_CRT_FILE2;
778 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200779 opt.psk = DFL_PSK;
780 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200781 opt.psk_list = DFL_PSK_LIST;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000782 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200783 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000784 opt.renegotiation = DFL_RENEGOTIATION;
785 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100786 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200787 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200788 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000789 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200790 opt.max_version = DFL_MAX_VERSION;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100791 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200792 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200793 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100794 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100795 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100796 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100797 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200798 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200799 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100800 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200801 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200802 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200803 opt.hs_to_min = DFL_HS_TO_MIN;
804 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200805 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200806 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100807 opt.etm = DFL_ETM;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000808
809 for( i = 1; i < argc; i++ )
810 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000811 p = argv[i];
812 if( ( q = strchr( p, '=' ) ) == NULL )
813 goto usage;
814 *q++ = '\0';
815
816 if( strcmp( p, "server_port" ) == 0 )
817 {
818 opt.server_port = atoi( q );
819 if( opt.server_port < 1 || opt.server_port > 65535 )
820 goto usage;
821 }
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100822 else if( strcmp( p, "server_addr" ) == 0 )
823 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100824 else if( strcmp( p, "dtls" ) == 0 )
825 {
826 int t = atoi( q );
827 if( t == 0 )
828 opt.transport = SSL_TRANSPORT_STREAM;
829 else if( t == 1 )
830 opt.transport = SSL_TRANSPORT_DATAGRAM;
831 else
832 goto usage;
833 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000834 else if( strcmp( p, "debug_level" ) == 0 )
835 {
836 opt.debug_level = atoi( q );
837 if( opt.debug_level < 0 || opt.debug_level > 65535 )
838 goto usage;
839 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100840 else if( strcmp( p, "nbio" ) == 0 )
841 {
842 opt.nbio = atoi( q );
843 if( opt.nbio < 0 || opt.nbio > 2 )
844 goto usage;
845 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200846 else if( strcmp( p, "read_timeout" ) == 0 )
847 opt.read_timeout = atoi( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000848 else if( strcmp( p, "ca_file" ) == 0 )
849 opt.ca_file = q;
850 else if( strcmp( p, "ca_path" ) == 0 )
851 opt.ca_path = q;
852 else if( strcmp( p, "crt_file" ) == 0 )
853 opt.crt_file = q;
854 else if( strcmp( p, "key_file" ) == 0 )
855 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200856 else if( strcmp( p, "crt_file2" ) == 0 )
857 opt.crt_file2 = q;
858 else if( strcmp( p, "key_file2" ) == 0 )
859 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200860 else if( strcmp( p, "dhm_file" ) == 0 )
861 opt.dhm_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200862 else if( strcmp( p, "psk" ) == 0 )
863 opt.psk = q;
864 else if( strcmp( p, "psk_identity" ) == 0 )
865 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200866 else if( strcmp( p, "psk_list" ) == 0 )
867 opt.psk_list = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000868 else if( strcmp( p, "force_ciphersuite" ) == 0 )
869 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000870 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
871
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200872 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000873 {
874 ret = 2;
875 goto usage;
876 }
877 opt.force_ciphersuite[1] = 0;
878 }
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200879 else if( strcmp( p, "version_suites" ) == 0 )
880 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000881 else if( strcmp( p, "renegotiation" ) == 0 )
882 {
883 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
884 SSL_RENEGOTIATION_DISABLED;
885 }
886 else if( strcmp( p, "allow_legacy" ) == 0 )
887 {
888 opt.allow_legacy = atoi( q );
889 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
890 goto usage;
891 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100892 else if( strcmp( p, "renegotiate" ) == 0 )
893 {
894 opt.renegotiate = atoi( q );
895 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
896 goto usage;
897 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200898 else if( strcmp( p, "renego_delay" ) == 0 )
899 {
900 opt.renego_delay = atoi( q );
901 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200902 else if( strcmp( p, "exchanges" ) == 0 )
903 {
904 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +0200905 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200906 goto usage;
907 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000908 else if( strcmp( p, "min_version" ) == 0 )
909 {
910 if( strcmp( q, "ssl3" ) == 0 )
911 opt.min_version = SSL_MINOR_VERSION_0;
912 else if( strcmp( q, "tls1" ) == 0 )
913 opt.min_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100914 else if( strcmp( q, "tls1_1" ) == 0 ||
915 strcmp( q, "dtls1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000916 opt.min_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100917 else if( strcmp( q, "tls1_2" ) == 0 ||
918 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000919 opt.min_version = SSL_MINOR_VERSION_3;
920 else
921 goto usage;
922 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200923 else if( strcmp( p, "max_version" ) == 0 )
924 {
925 if( strcmp( q, "ssl3" ) == 0 )
926 opt.max_version = SSL_MINOR_VERSION_0;
927 else if( strcmp( q, "tls1" ) == 0 )
928 opt.max_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100929 else if( strcmp( q, "tls1_1" ) == 0 ||
930 strcmp( q, "dtls1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200931 opt.max_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100932 else if( strcmp( q, "tls1_2" ) == 0 ||
933 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200934 opt.max_version = SSL_MINOR_VERSION_3;
935 else
936 goto usage;
937 }
938 else if( strcmp( p, "force_version" ) == 0 )
939 {
940 if( strcmp( q, "ssl3" ) == 0 )
941 {
942 opt.min_version = SSL_MINOR_VERSION_0;
943 opt.max_version = SSL_MINOR_VERSION_0;
944 }
945 else if( strcmp( q, "tls1" ) == 0 )
946 {
947 opt.min_version = SSL_MINOR_VERSION_1;
948 opt.max_version = SSL_MINOR_VERSION_1;
949 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100950 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200951 {
952 opt.min_version = SSL_MINOR_VERSION_2;
953 opt.max_version = SSL_MINOR_VERSION_2;
954 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100955 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200956 {
957 opt.min_version = SSL_MINOR_VERSION_3;
958 opt.max_version = SSL_MINOR_VERSION_3;
959 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100960 else if( strcmp( q, "dtls1" ) == 0 )
961 {
962 opt.min_version = SSL_MINOR_VERSION_2;
963 opt.max_version = SSL_MINOR_VERSION_2;
964 opt.transport = SSL_TRANSPORT_DATAGRAM;
965 }
966 else if( strcmp( q, "dtls1_2" ) == 0 )
967 {
968 opt.min_version = SSL_MINOR_VERSION_3;
969 opt.max_version = SSL_MINOR_VERSION_3;
970 opt.transport = SSL_TRANSPORT_DATAGRAM;
971 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200972 else
973 goto usage;
974 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100975 else if( strcmp( p, "auth_mode" ) == 0 )
976 {
977 if( strcmp( q, "none" ) == 0 )
978 opt.auth_mode = SSL_VERIFY_NONE;
979 else if( strcmp( q, "optional" ) == 0 )
980 opt.auth_mode = SSL_VERIFY_OPTIONAL;
981 else if( strcmp( q, "required" ) == 0 )
982 opt.auth_mode = SSL_VERIFY_REQUIRED;
983 else
984 goto usage;
985 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200986 else if( strcmp( p, "max_frag_len" ) == 0 )
987 {
988 if( strcmp( q, "512" ) == 0 )
989 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
990 else if( strcmp( q, "1024" ) == 0 )
991 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
992 else if( strcmp( q, "2048" ) == 0 )
993 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
994 else if( strcmp( q, "4096" ) == 0 )
995 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
996 else
997 goto usage;
998 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200999 else if( strcmp( p, "alpn" ) == 0 )
1000 {
1001 opt.alpn_string = q;
1002 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001003 else if( strcmp( p, "extended_ms" ) == 0 )
1004 {
1005 switch( atoi( q ) )
1006 {
1007 case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break;
1008 case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break;
1009 default: goto usage;
1010 }
1011 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001012 else if( strcmp( p, "etm" ) == 0 )
1013 {
1014 switch( atoi( q ) )
1015 {
1016 case 0: opt.etm = SSL_ETM_DISABLED; break;
1017 case 1: opt.etm = SSL_ETM_ENABLED; break;
1018 default: goto usage;
1019 }
1020 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001021 else if( strcmp( p, "tickets" ) == 0 )
1022 {
1023 opt.tickets = atoi( q );
1024 if( opt.tickets < 0 || opt.tickets > 1 )
1025 goto usage;
1026 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001027 else if( strcmp( p, "ticket_timeout" ) == 0 )
1028 {
1029 opt.ticket_timeout = atoi( q );
1030 if( opt.ticket_timeout < 0 )
1031 goto usage;
1032 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001033 else if( strcmp( p, "cache_max" ) == 0 )
1034 {
1035 opt.cache_max = atoi( q );
1036 if( opt.cache_max < 0 )
1037 goto usage;
1038 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001039 else if( strcmp( p, "cache_timeout" ) == 0 )
1040 {
1041 opt.cache_timeout = atoi( q );
1042 if( opt.cache_timeout < 0 )
1043 goto usage;
1044 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001045 else if( strcmp( p, "cookies" ) == 0 )
1046 {
1047 opt.cookies = atoi( q );
1048 if( opt.cookies < -1 || opt.cookies > 1)
1049 goto usage;
1050 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001051 else if( strcmp( p, "anti_replay" ) == 0 )
1052 {
1053 opt.anti_replay = atoi( q );
1054 if( opt.anti_replay < 0 || opt.anti_replay > 1)
1055 goto usage;
1056 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001057 else if( strcmp( p, "badmac_limit" ) == 0 )
1058 {
1059 opt.badmac_limit = atoi( q );
1060 if( opt.badmac_limit < 0 )
1061 goto usage;
1062 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001063 else if( strcmp( p, "hs_timeout" ) == 0 )
1064 {
1065 if( ( p = strchr( q, '-' ) ) == NULL )
1066 goto usage;
1067 *p++ = '\0';
1068 opt.hs_to_min = atoi( q );
1069 opt.hs_to_max = atoi( p );
1070 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1071 goto usage;
1072 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001073 else if( strcmp( p, "sni" ) == 0 )
1074 {
1075 opt.sni = q;
1076 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001077 else
1078 goto usage;
1079 }
1080
Paul Bakkerc73079a2014-04-25 16:34:30 +02001081#if defined(POLARSSL_DEBUG_C)
1082 debug_set_threshold( opt.debug_level );
1083#endif
1084
Paul Bakkerc1516be2013-06-29 16:01:32 +02001085 if( opt.force_ciphersuite[0] > 0 )
1086 {
1087 const ssl_ciphersuite_t *ciphersuite_info;
1088 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1089
Paul Bakker5b55b792013-07-19 13:43:43 +02001090 if( opt.max_version != -1 &&
1091 ciphersuite_info->min_minor_ver > opt.max_version )
1092 {
1093 printf("forced ciphersuite not allowed with this protocol version\n");
1094 ret = 2;
1095 goto usage;
1096 }
1097 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001098 ciphersuite_info->max_minor_ver < opt.min_version )
1099 {
1100 printf("forced ciphersuite not allowed with this protocol version\n");
1101 ret = 2;
1102 goto usage;
1103 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001104
1105 /* If we select a version that's not supported by
1106 * this suite, then there will be no common ciphersuite... */
1107 if( opt.max_version == -1 ||
1108 opt.max_version > ciphersuite_info->max_minor_ver )
1109 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001110 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001111 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001112 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001113 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001114 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001115 /* DTLS starts with TLS 1.1 */
1116 if( opt.transport == SSL_TRANSPORT_DATAGRAM &&
1117 opt.min_version < SSL_MINOR_VERSION_2 )
1118 opt.min_version = SSL_MINOR_VERSION_2;
1119 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001120 }
1121
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001122 if( opt.version_suites != NULL )
1123 {
1124 const char *name[4] = { 0 };
1125
1126 /* Parse 4-element coma-separated list */
1127 for( i = 0, p = (char *) opt.version_suites;
1128 i < 4 && *p != '\0';
1129 i++ )
1130 {
1131 name[i] = p;
1132
1133 /* Terminate the current string and move on to next one */
1134 while( *p != ',' && *p != '\0' )
1135 p++;
1136 if( *p == ',' )
1137 *p++ = '\0';
1138 }
1139
1140 if( i != 4 )
1141 {
1142 printf( "too few values for version_suites\n" );
1143 ret = 1;
1144 goto exit;
1145 }
1146
1147 memset( version_suites, 0, sizeof( version_suites ) );
1148
1149 /* Get the suites identifiers from their name */
1150 for( i = 0; i < 4; i++ )
1151 {
1152 version_suites[i][0] = ssl_get_ciphersuite_id( name[i] );
1153
1154 if( version_suites[i][0] == 0 )
1155 {
1156 printf( "unknown ciphersuite: '%s'\n", name[i] );
1157 ret = 2;
1158 goto usage;
1159 }
1160 }
1161 }
1162
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001163#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001164 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001165 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001166 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001167 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001168 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001169 printf( "pre-shared key not valid hex\n" );
1170 goto exit;
1171 }
1172
1173 if( opt.psk_list != NULL )
1174 {
1175 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001176 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001177 printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001178 goto exit;
1179 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001180 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001181#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001182
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001183#if defined(POLARSSL_SSL_ALPN)
1184 if( opt.alpn_string != NULL )
1185 {
1186 p = (char *) opt.alpn_string;
1187 i = 0;
1188
1189 /* Leave room for a final NULL in alpn_list */
1190 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
1191 {
1192 alpn_list[i++] = p;
1193
1194 /* Terminate the current string and move on to next one */
1195 while( *p != ',' && *p != '\0' )
1196 p++;
1197 if( *p == ',' )
1198 *p++ = '\0';
1199 }
1200 }
1201#endif /* POLARSSL_SSL_ALPN */
1202
Paul Bakkerfbb17802013-04-17 19:10:21 +02001203 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001204 * 0. Initialize the RNG and the session data
1205 */
1206 printf( "\n . Seeding the random number generator..." );
1207 fflush( stdout );
1208
1209 entropy_init( &entropy );
1210 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001211 (const unsigned char *) pers,
1212 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001213 {
1214 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
1215 goto exit;
1216 }
1217
1218 printf( " ok\n" );
1219
Paul Bakker36713e82013-09-17 13:25:29 +02001220#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001221 /*
1222 * 1.1. Load the trusted CA
1223 */
1224 printf( " . Loading the CA root certificate ..." );
1225 fflush( stdout );
1226
1227#if defined(POLARSSL_FS_IO)
1228 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001229 if( strcmp( opt.ca_path, "none" ) == 0 )
1230 ret = 0;
1231 else
1232 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001233 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001234 if( strcmp( opt.ca_file, "none" ) == 0 )
1235 ret = 0;
1236 else
1237 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001238 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001239#endif
1240#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +02001241 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
1242 strlen( test_ca_list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001243#else
1244 {
1245 ret = 1;
1246 printf("POLARSSL_CERTS_C not defined.");
1247 }
1248#endif
1249 if( ret < 0 )
1250 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001251 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001252 goto exit;
1253 }
1254
1255 printf( " ok (%d skipped)\n", ret );
1256
1257 /*
1258 * 1.2. Load own certificate and private key
1259 */
1260 printf( " . Loading the server cert. and key..." );
1261 fflush( stdout );
1262
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001263#if defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001264 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001265 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001266 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001267 if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
1268 {
1269 printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n",
1270 -ret );
1271 goto exit;
1272 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001273 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001274 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001275 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001276 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001277 if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
1278 {
1279 printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret );
1280 goto exit;
1281 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001282 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001283 if( key_cert_init == 1 )
1284 {
1285 printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
1286 goto exit;
1287 }
1288
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001289 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001290 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001291 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001292 if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
1293 {
1294 printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n",
1295 -ret );
1296 goto exit;
1297 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001298 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001299 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001300 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001301 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001302 if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
1303 {
1304 printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n",
1305 -ret );
1306 goto exit;
1307 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001308 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001309 if( key_cert_init2 == 1 )
1310 {
1311 printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
1312 goto exit;
1313 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001314#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001315 if( key_cert_init == 0 &&
1316 strcmp( opt.crt_file, "none" ) != 0 &&
1317 strcmp( opt.key_file, "none" ) != 0 &&
1318 key_cert_init2 == 0 &&
1319 strcmp( opt.crt_file2, "none" ) != 0 &&
1320 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001321 {
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001322#if !defined(POLARSSL_CERTS_C)
1323 printf( "Not certificated or key provided, and \n"
1324 "POLARSSL_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001325 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001326#else
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001327#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001328 if( ( ret = x509_crt_parse( &srvcert,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001329 (const unsigned char *) test_srv_crt_rsa,
1330 strlen( test_srv_crt_rsa ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001331 {
1332 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
1333 goto exit;
1334 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001335 if( ( ret = pk_parse_key( &pkey,
1336 (const unsigned char *) test_srv_key_rsa,
1337 strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001338 {
1339 printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
1340 goto exit;
1341 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001342 key_cert_init = 2;
1343#endif /* POLARSSL_RSA_C */
1344#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001345 if( ( ret = x509_crt_parse( &srvcert2,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001346 (const unsigned char *) test_srv_crt_ec,
1347 strlen( test_srv_crt_ec ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001348 {
1349 printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
1350 goto exit;
1351 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001352 if( ( ret = pk_parse_key( &pkey2,
1353 (const unsigned char *) test_srv_key_ec,
1354 strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001355 {
1356 printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
1357 goto exit;
1358 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001359 key_cert_init2 = 2;
1360#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001361#endif /* POLARSSL_CERTS_C */
1362 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001363
1364 printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001365#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001366
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001367#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1368 if( opt.dhm_file != NULL )
1369 {
1370 printf( " . Loading DHM parameters..." );
1371 fflush( stdout );
1372
1373 if( ( ret = dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
1374 {
1375 printf( " failed\n ! dhm_parse_dhmfile returned -0x%04X\n\n",
1376 -ret );
1377 goto exit;
1378 }
1379
1380 printf( " ok\n" );
1381 }
1382#endif
1383
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001384#if defined(POLARSSL_SNI)
1385 if( opt.sni != NULL )
1386 {
1387 printf( " . Setting up SNI information..." );
1388 fflush( stdout );
1389
1390 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
1391 {
1392 printf( " failed\n" );
1393 goto exit;
1394 }
1395
1396 printf( " ok\n" );
1397 }
1398#endif /* POLARSSL_SNI */
1399
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001400 /*
1401 * 2. Setup the listening TCP socket
1402 */
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001403 printf( " . Bind on %s://%s:%-4d/ ...",
1404 opt.transport == SSL_TRANSPORT_STREAM ? "tcp" : "udp",
1405 opt.server_addr ? opt.server_addr : "*",
1406 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001407 fflush( stdout );
1408
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001409 if( ( ret = net_bind( &listen_fd, opt.server_addr, opt.server_port,
1410 opt.transport == SSL_TRANSPORT_STREAM ?
1411 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001412 {
1413 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1414 goto exit;
1415 }
1416
1417 printf( " ok\n" );
1418
1419 /*
1420 * 3. Setup stuff
1421 */
1422 printf( " . Setting up the SSL/TLS structure..." );
1423 fflush( stdout );
1424
1425 if( ( ret = ssl_init( &ssl ) ) != 0 )
1426 {
1427 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
1428 goto exit;
1429 }
1430
1431 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001432 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001433
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001434#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001435 if( ( ret = ssl_set_transport( &ssl, opt.transport ) ) != 0 )
1436 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001437 printf( " failed\n ! selected transport is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001438 goto exit;
1439 }
1440
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001441 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
1442 ssl_set_handshake_timeout( &ssl, opt.hs_to_min, opt.hs_to_max );
1443#endif /* POLARSSL_SSL_PROTO_DTLS */
1444
Paul Bakker05decb22013-08-15 13:33:48 +02001445#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001446 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1447 {
1448 printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
1449 goto exit;
1450 };
Paul Bakker05decb22013-08-15 13:33:48 +02001451#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001452
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001453#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1454 if( opt.extended_ms != DFL_EXTENDED_MS )
1455 ssl_set_extended_master_secret( &ssl, opt.extended_ms );
1456#endif
1457
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001458#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1459 if( opt.etm != DFL_ETM )
1460 ssl_set_encrypt_then_mac( &ssl, opt.etm );
1461#endif
1462
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001463#if defined(POLARSSL_SSL_ALPN)
1464 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001465 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1466 {
1467 printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
1468 goto exit;
1469 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001470#endif
1471
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001472 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
1473 ssl_set_dbg( &ssl, my_debug, stdout );
1474
Paul Bakker0a597072012-09-25 21:55:46 +00001475#if defined(POLARSSL_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001476 if( opt.cache_max != -1 )
1477 ssl_cache_set_max_entries( &cache, opt.cache_max );
1478
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001479 if( opt.cache_timeout != -1 )
1480 ssl_cache_set_timeout( &cache, opt.cache_timeout );
1481
Paul Bakker0a597072012-09-25 21:55:46 +00001482 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
1483 ssl_cache_set, &cache );
1484#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001485
Paul Bakkera503a632013-08-14 13:48:06 +02001486#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001487 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1488 {
1489 printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
1490 goto exit;
1491 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001492
1493 if( opt.ticket_timeout != -1 )
1494 ssl_set_session_ticket_lifetime( &ssl, opt.ticket_timeout );
Paul Bakkera503a632013-08-14 13:48:06 +02001495#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001496
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001497#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001498 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001499 {
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001500#if defined(POLARSSL_SSL_COOKIE_C)
1501 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001502 {
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001503 if( ( ret = ssl_cookie_setup( &cookie_ctx,
1504 ctr_drbg_random, &ctr_drbg ) ) != 0 )
1505 {
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001506 printf( " failed\n ! ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001507 goto exit;
1508 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001509
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001510 ssl_set_dtls_cookies( &ssl, ssl_cookie_write, ssl_cookie_check,
1511 &cookie_ctx );
1512 }
1513 else
1514#endif /* POLARSSL_SSL_COOKIE_C */
1515#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
1516 if( opt.cookies == 0 )
1517 {
1518 ssl_set_dtls_cookies( &ssl, NULL, NULL, NULL );
1519 }
1520 else
1521#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
1522 {
1523 ; /* Nothing to do */
1524 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001525
1526#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1527 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001528 ssl_set_dtls_anti_replay( &ssl, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001529#endif
1530
1531#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
1532 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
1533 ssl_set_dtls_badmac_limit( &ssl, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001534#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001535 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001536#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001537
Paul Bakker41c83d32013-03-20 14:39:14 +01001538 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001539 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
1540
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001541 if( opt.version_suites != NULL )
1542 {
1543 ssl_set_ciphersuites_for_version( &ssl, version_suites[0],
1544 SSL_MAJOR_VERSION_3,
1545 SSL_MINOR_VERSION_0 );
1546 ssl_set_ciphersuites_for_version( &ssl, version_suites[1],
1547 SSL_MAJOR_VERSION_3,
1548 SSL_MINOR_VERSION_1 );
1549 ssl_set_ciphersuites_for_version( &ssl, version_suites[2],
1550 SSL_MAJOR_VERSION_3,
1551 SSL_MINOR_VERSION_2 );
1552 ssl_set_ciphersuites_for_version( &ssl, version_suites[3],
1553 SSL_MAJOR_VERSION_3,
1554 SSL_MINOR_VERSION_3 );
1555 }
1556
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001557 ssl_set_renegotiation( &ssl, opt.renegotiation );
1558 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001559 if( opt.renego_delay != DFL_RENEGO_DELAY )
1560 ssl_set_renegotiation_enforced( &ssl, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001561
Paul Bakker36713e82013-09-17 13:25:29 +02001562#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001563 if( strcmp( opt.ca_path, "none" ) != 0 &&
1564 strcmp( opt.ca_file, "none" ) != 0 )
1565 {
1566 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
1567 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001568 if( key_cert_init )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001569 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
1570 {
1571 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1572 goto exit;
1573 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001574 if( key_cert_init2 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001575 if( ( ret = ssl_set_own_cert( &ssl, &srvcert2, &pkey2 ) ) != 0 )
1576 {
1577 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1578 goto exit;
1579 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001580#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001581
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001582#if defined(POLARSSL_SNI)
1583 if( opt.sni != NULL )
1584 ssl_set_sni( &ssl, sni_callback, sni_info );
1585#endif
1586
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001587#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001588 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
1589 {
1590 ret = ssl_set_psk( &ssl, psk, psk_len,
1591 (const unsigned char *) opt.psk_identity,
1592 strlen( opt.psk_identity ) );
1593 if( ret != 0 )
1594 {
1595 printf( " failed\n ssl_set_psk returned -0x%04X\n\n", - ret );
1596 goto exit;
1597 }
1598 }
1599
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001600 if( opt.psk_list != NULL )
1601 ssl_set_psk_cb( &ssl, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02001602#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001603
1604#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00001605 /*
1606 * Use different group than default DHM group
1607 */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001608#if defined(POLARSSL_FS_IO)
1609 if( opt.dhm_file != NULL )
1610 ret = ssl_set_dh_param_ctx( &ssl, &dhm );
1611 else
1612#endif
1613 ret = ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
1614 POLARSSL_DHM_RFC5114_MODP_2048_G );
1615
1616 if( ret != 0 )
1617 {
1618 printf( " failed\n ssl_set_dh_param returned -0x%04X\n\n", - ret );
1619 goto exit;
1620 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001621#endif
1622
Paul Bakker1d29fb52012-09-28 13:28:45 +00001623 if( opt.min_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001624 {
1625 ret = ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1626 if( ret != 0 )
1627 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001628 printf( " failed\n ! selected min_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001629 goto exit;
1630 }
1631 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001632
Paul Bakkerc1516be2013-06-29 16:01:32 +02001633 if( opt.max_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001634 {
1635 ret = ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1636 if( ret != 0 )
1637 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001638 printf( " failed\n ! selected max_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001639 goto exit;
1640 }
1641 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001642
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001643 printf( " ok\n" );
1644
1645reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02001646#if !defined(_WIN32)
1647 if( received_sigterm )
1648 {
1649 printf( " interrupted by SIGTERM\n" );
1650 ret = 0;
1651 goto exit;
1652 }
1653#endif
1654
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001655#ifdef POLARSSL_ERROR_C
1656 if( ret != 0 )
1657 {
1658 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001659 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001660 printf("Last error was: %d - %s\n\n", ret, error_buf );
1661 }
1662#endif
1663
1664 if( client_fd != -1 )
Manuel Pégourié-Gonnard4ba6ab62014-08-07 17:21:47 +02001665 net_close( client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001666
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001667 ssl_session_reset( &ssl );
1668
1669 /*
1670 * 3. Wait until a client connects
1671 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001672 client_fd = -1;
1673
1674 printf( " . Waiting for a remote connection ..." );
1675 fflush( stdout );
1676
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001677 if( ( ret = net_accept( listen_fd, &client_fd, client_ip ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001678 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02001679#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001680 if( received_sigterm )
1681 {
1682 printf( " interrupted by SIGTERM\n" );
1683 ret = 0;
1684 goto exit;
1685 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02001686#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001687
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001688 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
1689 goto exit;
1690 }
1691
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001692 if( opt.nbio > 0 )
1693 ret = net_set_nonblock( client_fd );
1694 else
1695 ret = net_set_block( client_fd );
1696 if( ret != 0 )
1697 {
1698 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
1699 goto exit;
1700 }
1701
1702 if( opt.nbio == 2 )
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001703 ssl_set_bio_timeout( &ssl, &client_fd, my_send, my_recv, NULL, 0 );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001704 else
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001705 ssl_set_bio_timeout( &ssl, &client_fd, net_send, net_recv,
1706#if defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnardf0365122014-09-29 16:11:47 +02001707 opt.nbio == 0 ? net_recv_timeout : NULL,
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001708#else
1709 NULL,
1710#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001711 opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001712
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001713#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001714 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
1715 {
1716 if( ( ret = ssl_set_client_transport_id( &ssl, client_ip,
1717 sizeof( client_ip ) ) ) != 0 )
1718 {
1719 printf( " failed\n ! "
1720 "ssl_set_client_tranport_id() returned -0x%x\n\n", -ret );
1721 goto exit;
1722 }
1723 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001724#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001725
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001726 printf( " ok\n" );
1727
1728 /*
Manuel Pégourié-Gonnardbd97fdb2014-09-26 16:46:36 +02001729 * With UDP, bind_fd is hijacked by client_fd, so bind a new one
1730 */
1731#if defined(POLARSSL_SSL_PROTO_DTLS)
1732 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
1733 {
1734 printf( " . Re-bind on udp://%s:%-4d/ ...",
1735 opt.server_addr ? opt.server_addr : "*",
1736 opt.server_port );
1737 fflush( stdout );
1738
1739 if( ( ret = net_bind( &listen_fd, opt.server_addr,
1740 opt.server_port, NET_PROTO_UDP ) ) != 0 )
1741 {
1742 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1743 goto exit;
1744 }
1745
1746 printf( " ok\n" );
1747 }
1748#endif /* POLARSSL_SSL_PROTO_DTLS */
1749
1750 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001751 * 4. Handshake
1752 */
1753 printf( " . Performing the SSL/TLS handshake..." );
1754 fflush( stdout );
1755
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001756 do ret = ssl_handshake( &ssl );
1757 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1758 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001759
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001760 if( ret == POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED )
1761 {
1762 printf( " hello verification requested\n" );
1763 ret = 0;
1764 goto reset;
1765 }
1766 else if( ret != 0 )
1767 {
1768 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
1769 goto reset;
1770 }
1771 else /* ret == 0 */
1772 {
1773 printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1774 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
1775 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001776
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001777 if( ( ret = ssl_get_record_expansion( &ssl ) ) >= 0 )
1778 printf( " [ Record expansion is %d ]\n", ret );
1779 else
1780 printf( " [ Record expansion is unknown (compression) ]\n" );
1781
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001782#if defined(POLARSSL_SSL_ALPN)
1783 if( opt.alpn_string != NULL )
1784 {
1785 const char *alp = ssl_get_alpn_protocol( &ssl );
1786 printf( " [ Application Layer Protocol is %s ]\n",
1787 alp ? alp : "(none)" );
1788 }
1789#endif
1790
Paul Bakker36713e82013-09-17 13:25:29 +02001791#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001792 /*
1793 * 5. Verify the server certificate
1794 */
1795 printf( " . Verifying peer X.509 certificate..." );
1796
1797 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1798 {
1799 printf( " failed\n" );
1800
Paul Bakkerb0550d92012-10-30 07:51:03 +00001801 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001802 printf( " ! no client certificate sent\n" );
1803
1804 if( ( ret & BADCERT_EXPIRED ) != 0 )
1805 printf( " ! client certificate has expired\n" );
1806
1807 if( ( ret & BADCERT_REVOKED ) != 0 )
1808 printf( " ! client certificate has been revoked\n" );
1809
1810 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
1811 printf( " ! self-signed or not signed by a trusted CA\n" );
1812
1813 printf( "\n" );
1814 }
1815 else
1816 printf( " ok\n" );
1817
Paul Bakkerb0550d92012-10-30 07:51:03 +00001818 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001819 {
1820 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001821 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1822 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001823 printf( "%s\n", buf );
1824 }
Paul Bakker36713e82013-09-17 13:25:29 +02001825#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001826
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001827 if( opt.exchanges == 0 )
1828 goto close_notify;
1829
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001830 exchanges = opt.exchanges;
1831data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001832 /*
1833 * 6. Read the HTTP Request
1834 */
1835 printf( " < Read from client:" );
1836 fflush( stdout );
1837
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001838 /*
1839 * TLS and DTLS need different reading styles (stream vs datagram)
1840 */
1841 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001842 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001843 do
1844 {
1845 int terminated = 0;
1846 len = sizeof( buf ) - 1;
1847 memset( buf, 0, sizeof( buf ) );
1848 ret = ssl_read( &ssl, buf, len );
1849
1850 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1851 ret == POLARSSL_ERR_NET_WANT_WRITE )
1852 continue;
1853
1854 if( ret <= 0 )
1855 {
1856 switch( ret )
1857 {
1858 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1859 printf( " connection was closed gracefully\n" );
1860 goto close_notify;
1861
1862 case 0:
1863 case POLARSSL_ERR_NET_CONN_RESET:
1864 printf( " connection was reset by peer\n" );
1865 ret = POLARSSL_ERR_NET_CONN_RESET;
1866 goto reset;
1867
1868 default:
1869 printf( " ssl_read returned -0x%x\n", -ret );
1870 goto reset;
1871 }
1872 }
1873
1874 if( ssl_get_bytes_avail( &ssl ) == 0 )
1875 {
1876 len = ret;
1877 buf[len] = '\0';
1878 printf( " %d bytes read\n\n%s\n", len, (char *) buf );
1879
1880 /* End of message should be detected according to the syntax of the
1881 * application protocol (eg HTTP), just use a dummy test here. */
1882 if( buf[len - 1] == '\n' )
1883 terminated = 1;
1884 }
1885 else
1886 {
1887 int extra_len, ori_len;
1888 unsigned char *larger_buf;
1889
1890 ori_len = ret;
1891 extra_len = ssl_get_bytes_avail( &ssl );
1892
1893 larger_buf = polarssl_malloc( ori_len + extra_len + 1 );
1894 if( larger_buf == NULL )
1895 {
1896 printf( " ! memory allocation failed\n" );
1897 ret = 1;
1898 goto reset;
1899 }
1900
1901 memset( larger_buf, 0, ori_len + extra_len );
1902 memcpy( larger_buf, buf, ori_len );
1903
1904 /* This read should never fail and get the whole cached data */
1905 ret = ssl_read( &ssl, larger_buf + ori_len, extra_len );
1906 if( ret != extra_len ||
1907 ssl_get_bytes_avail( &ssl ) != 0 )
1908 {
1909 printf( " ! ssl_read failed on cached data\n" );
1910 ret = 1;
1911 goto reset;
1912 }
1913
1914 larger_buf[ori_len + extra_len] = '\0';
1915 printf( " %u bytes read (%u + %u)\n\n%s\n",
1916 ori_len + extra_len, ori_len, extra_len,
1917 (char *) larger_buf );
1918
1919 /* End of message should be detected according to the syntax of the
1920 * application protocol (eg HTTP), just use a dummy test here. */
1921 if( larger_buf[ori_len + extra_len - 1] == '\n' )
1922 terminated = 1;
1923
1924 polarssl_free( larger_buf );
1925 }
1926
1927 if( terminated )
1928 {
1929 ret = 0;
1930 break;
1931 }
1932 }
1933 while( 1 );
1934 }
1935 else /* Not stream, so datagram */
1936 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001937 len = sizeof( buf ) - 1;
1938 memset( buf, 0, sizeof( buf ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001939
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001940 do ret = ssl_read( &ssl, buf, len );
1941 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1942 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001943
1944 if( ret <= 0 )
1945 {
1946 switch( ret )
1947 {
1948 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1949 printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001950 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001951 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001952
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001953 default:
1954 printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001955 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001956 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001957 }
1958
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001959 len = ret;
1960 buf[len] = '\0';
1961 printf( " %d bytes read\n\n%s", len, (char *) buf );
1962 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001963 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001964
1965 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001966 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02001967 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001968 */
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02001969 if( opt.renegotiate && exchanges == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001970 {
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001971 printf( " . Requestion renegotiation..." );
1972 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001973
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001974 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
1975 {
1976 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1977 ret != POLARSSL_ERR_NET_WANT_WRITE )
1978 {
1979 printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
1980 goto reset;
1981 }
1982 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001983
1984 printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001985 }
1986
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001987 /*
1988 * 7. Write the 200 Response
1989 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001990 printf( " > Write to client:" );
1991 fflush( stdout );
1992
1993 len = sprintf( (char *) buf, HTTP_RESPONSE,
1994 ssl_get_ciphersuite( &ssl ) );
1995
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001996 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001997 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001998 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001999 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002000 while( ( ret = ssl_write( &ssl, buf + written, len - written ) )
2001 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002002 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002003 if( ret == POLARSSL_ERR_NET_CONN_RESET )
2004 {
2005 printf( " failed\n ! peer closed the connection\n\n" );
2006 goto reset;
2007 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002008
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002009 if( ret != POLARSSL_ERR_NET_WANT_READ &&
2010 ret != POLARSSL_ERR_NET_WANT_WRITE )
2011 {
2012 printf( " failed\n ! ssl_write returned %d\n\n", ret );
2013 goto reset;
2014 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002015 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002016 }
2017 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002018 else /* Not stream, so datagram */
2019 {
2020 do ret = ssl_write( &ssl, buf, len );
2021 while( ret == POLARSSL_ERR_NET_WANT_READ ||
2022 ret == POLARSSL_ERR_NET_WANT_WRITE );
2023
2024 if( ret < 0 )
2025 {
2026 printf( " failed\n ! ssl_write returned %d\n\n", ret );
2027 goto reset;
2028 }
2029
2030 frags = 1;
2031 written = ret;
2032 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002033
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002034 buf[written] = '\0';
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002035 printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002036
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002037
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002038 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002039 * 7b. Continue doing data exchanges?
2040 */
2041 if( --exchanges > 0 )
2042 goto data_exchange;
2043
2044 /*
2045 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002046 */
2047close_notify:
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002048 printf( " . Closing the connection..." );
2049
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002050 /* No error checking, the connection might be closed already */
2051 do
2052 ret = ssl_close_notify( &ssl );
2053 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
2054 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002055
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002056 printf( " done\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002057 goto reset;
2058
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002059 /*
2060 * Cleanup and exit
2061 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002062exit:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002063#ifdef POLARSSL_ERROR_C
2064 if( ret != 0 )
2065 {
2066 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02002067 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002068 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
2069 }
2070#endif
2071
Paul Bakker0c226102014-04-17 16:02:36 +02002072 if( client_fd != -1 )
2073 net_close( client_fd );
2074
Paul Bakkera317a982014-06-18 16:44:11 +02002075#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
2076 dhm_free( &dhm );
2077#endif
Paul Bakker36713e82013-09-17 13:25:29 +02002078#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker36713e82013-09-17 13:25:29 +02002079 x509_crt_free( &cacert );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002080 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02002081 pk_free( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002082 x509_crt_free( &srvcert2 );
2083 pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02002084#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002085#if defined(POLARSSL_SNI)
2086 sni_free( sni_info );
2087#endif
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02002088#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
2089 psk_free( psk_info );
2090#endif
2091#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
2092 dhm_free( &dhm );
2093#endif
Paul Bakkered27a042013-04-18 22:46:23 +02002094
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002095 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02002096 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02002097 entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002098
Paul Bakker0a597072012-09-25 21:55:46 +00002099#if defined(POLARSSL_SSL_CACHE_C)
2100 ssl_cache_free( &cache );
2101#endif
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +02002102#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002103 ssl_cookie_free( &cookie_ctx );
2104#endif
Paul Bakker0a597072012-09-25 21:55:46 +00002105
Paul Bakker1337aff2013-09-29 14:45:34 +02002106#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
2107#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker82024bf2013-07-04 11:52:32 +02002108 memory_buffer_alloc_status();
2109#endif
Paul Bakker1337aff2013-09-29 14:45:34 +02002110 memory_buffer_alloc_free();
2111#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02002112
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002113#if defined(_WIN32)
2114 printf( " + Press Enter to exit this program.\n" );
2115 fflush( stdout ); getchar();
2116#endif
2117
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002118 // Shell can not handle large exit numbers -> 1 for errors
2119 if( ret < 0 )
2120 ret = 1;
2121
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002122 return( ret );
2123}
2124#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
2125 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
2126 POLARSSL_CTR_DRBG_C */