blob: be9d07b287256bdf62c7058dadaf08866639924d [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
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000129
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200130#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
131 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
132 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
133 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
134 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
135 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
136 "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 +0200137
Paul Bakker8e714d72013-07-18 11:05:13 +0200138/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
139 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000140#define HTTP_RESPONSE \
141 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
142 "<h2>PolarSSL Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200143 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000144
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200145/*
146 * Size of the basic I/O buffer. Able to hold our default response.
147 *
148 * You will need to adapt the ssl_get_bytes_avail() test in ssl-opt.sh
149 * if you change this value to something outside the range <= 100 or > 500
150 */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200151#define IO_BUF_LEN 200
152
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000153/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000154 * global options
155 */
156struct options
157{
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100158 const char *server_addr; /* address on which the ssl service runs */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000159 int server_port; /* port on which the ssl service runs */
160 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100161 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200162 uint32_t read_timeout; /* timeout on ssl_read() in milliseconds */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200163 const char *ca_file; /* the file with the CA certificate(s) */
164 const char *ca_path; /* the path with the CA certificate(s) reside */
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200165 const char *crt_file; /* the file with the server certificate */
166 const char *key_file; /* the file with the server key */
167 const char *crt_file2; /* the file with the 2nd server certificate */
168 const char *key_file2; /* the file with the 2nd server key */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200169 const char *psk; /* the pre-shared key */
170 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200171 char *psk_list; /* list of PSK id/key pairs for callback */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000172 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200173 const char *version_suites; /* per-version ciphersuites */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000174 int renegotiation; /* enable / disable renegotiation */
175 int allow_legacy; /* allow legacy renegotiation */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100176 int renegotiate; /* attempt renegotiation? */
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200177 int renego_delay; /* delay before enforcing renegotiation */
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200178 int exchanges; /* number of data exchanges */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000179 int min_version; /* minimum protocol version accepted */
Paul Bakkerc1516be2013-06-29 16:01:32 +0200180 int max_version; /* maximum protocol version accepted */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100181 int auth_mode; /* verify mode for connection */
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200182 unsigned char mfl_code; /* code for maximum fragment length */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200183 int tickets; /* enable / disable session tickets */
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100184 int ticket_timeout; /* session ticket lifetime */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100185 int cache_max; /* max number of session cache entries */
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100186 int cache_timeout; /* expiration delay of session cache entries */
Paul Bakker1ebc0c52014-05-22 15:47:58 +0200187 char *sni; /* string describing sni information */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200188 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200189 const char *dhm_file; /* the file with the DH parameters */
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100190 int transport; /* TLS or DTLS? */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200191 int cookies; /* Use cookies for DTLS? -1 to break them */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200192 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200193 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
194 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000195} opt;
196
Paul Bakker3c5ef712013-06-25 16:37:45 +0200197static void my_debug( void *ctx, int level, const char *str )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000198{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200199 ((void) level);
200
201 fprintf( (FILE *) ctx, "%s", str );
202 fflush( (FILE *) ctx );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000203}
204
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100205/*
206 * Test recv/send functions that make sure each try returns
207 * WANT_READ/WANT_WRITE at least once before sucesseding
208 */
209static int my_recv( void *ctx, unsigned char *buf, size_t len )
210{
211 static int first_try = 1;
212 int ret;
213
214 if( first_try )
215 {
216 first_try = 0;
217 return( POLARSSL_ERR_NET_WANT_READ );
218 }
219
220 ret = net_recv( ctx, buf, len );
221 if( ret != POLARSSL_ERR_NET_WANT_READ )
222 first_try = 1; /* Next call will be a new operation */
223 return( ret );
224}
225
226static int my_send( void *ctx, const unsigned char *buf, size_t len )
227{
228 static int first_try = 1;
229 int ret;
230
231 if( first_try )
232 {
233 first_try = 0;
234 return( POLARSSL_ERR_NET_WANT_WRITE );
235 }
236
237 ret = net_send( ctx, buf, len );
238 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
239 first_try = 1; /* Next call will be a new operation */
240 return( ret );
241}
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200242
243#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000244#if defined(POLARSSL_FS_IO)
245#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100246 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
247 " default: \"\" (pre-loaded)\n" \
248 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
249 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
250 " 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 +0200251 " default: see note after key_file2\n" \
252 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200253 " 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 +0200254 " default: see note after key_file2\n" \
255 " key_file2=%%s default: see note below\n" \
256 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200257 " preloaded certificate(s) and key(s) are used if available\n" \
258 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
259 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000260#else
261#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200262 "\n" \
263 " No file operations available (POLARSSL_FS_IO not defined)\n" \
264 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000265#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200266#else
267#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200268#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200269
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200270#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200271#define USAGE_PSK \
272 " psk=%%s default: \"\" (in hex, without 0x)\n" \
273 " psk_identity=%%s default: \"Client_identity\"\n"
274#else
275#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200276#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000277
Paul Bakkera503a632013-08-14 13:48:06 +0200278#if defined(POLARSSL_SSL_SESSION_TICKETS)
279#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100280 " tickets=%%d default: 1 (enabled)\n" \
281 " ticket_timeout=%%d default: ticket default (1d)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200282#else
283#define USAGE_TICKETS ""
284#endif /* POLARSSL_SSL_SESSION_TICKETS */
285
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100286#if defined(POLARSSL_SSL_CACHE_C)
287#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100288 " cache_max=%%d default: cache default (50)\n" \
289 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100290#else
291#define USAGE_CACHE ""
292#endif /* POLARSSL_SSL_CACHE_C */
293
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100294#if defined(POLARSSL_SNI)
295#define USAGE_SNI \
296 " sni=%%s name1,cert1,key1[,name2,cert2,key2[,...]]\n" \
297 " default: disabled\n"
298#else
299#define USAGE_SNI ""
300#endif /* POLARSSL_SNI */
301
Paul Bakker05decb22013-08-15 13:33:48 +0200302#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
303#define USAGE_MAX_FRAG_LEN \
304 " max_frag_len=%%d default: 16384 (tls default)\n" \
305 " options: 512, 1024, 2048, 4096\n"
306#else
307#define USAGE_MAX_FRAG_LEN ""
308#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
309
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200310#if defined(POLARSSL_SSL_ALPN)
311#define USAGE_ALPN \
312 " alpn=%%s default: \"\" (disabled)\n" \
313 " example: spdy/1,http/1.1\n"
314#else
315#define USAGE_ALPN ""
316#endif /* POLARSSL_SSL_ALPN */
317
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200318#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
319#define USAGE_COOKIES \
320 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200321 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200322#else
323#define USAGE_COOKIES ""
324#endif
325
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200326#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
327#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200328 " anti_replay=0/1 default: (library default = enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200329#else
330#define USAGE_ANTI_REPLAY ""
331#endif
332
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200333#if defined(POLARSSL_SSL_PROTO_DTLS)
334#define USAGE_DTLS \
335 " dtls=%%d default: 0 (TLS)\n" \
336 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
337 " range of DTLS handshake timeouts in millisecs\n"
338#else
339#define USAGE_DTLS ""
340#endif
341
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000342#define USAGE \
343 "\n usage: ssl_server2 param=<>...\n" \
344 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100345 " server_addr=%%d default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000346 " server_port=%%d default: 4433\n" \
347 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100348 " nbio=%%d default: 0 (blocking I/O)\n" \
349 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200350 " read_timeout=%%d default: 0 (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100351 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200352 USAGE_DTLS \
353 USAGE_COOKIES \
354 USAGE_ANTI_REPLAY \
355 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100356 " auth_mode=%%s default: \"optional\"\n" \
357 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000358 USAGE_IO \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100359 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100360 "\n" \
361 USAGE_PSK \
362 "\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000363 " renegotiation=%%d default: 1 (enabled)\n" \
364 " allow_legacy=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100365 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200366 " renego_delay=%%d default: -2 (library default)\n" \
367 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200368 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100369 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100370 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100371 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200372 USAGE_ALPN \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100373 "\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000374 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200375 " max_version=%%s default: \"tls1_2\"\n" \
376 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100377 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200378 "\n" \
379 " version_suites=a,b,c,d per-version ciphersuites\n" \
380 " in order from ssl3 to tls1_2\n" \
381 " default: all enabled\n" \
382 " force_ciphersuite=<name> default: all enabled\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000383 " acceptable ciphersuite names:\n"
384
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200385/*
386 * Used by sni_parse and psk_parse to handle coma-separated lists
387 */
388#define GET_ITEM( dst ) \
389 dst = p; \
390 while( *p != ',' ) \
391 if( ++p > end ) \
392 return( NULL ); \
393 *p++ = '\0';
394
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100395#if defined(POLARSSL_SNI)
396typedef struct _sni_entry sni_entry;
397
398struct _sni_entry {
399 const char *name;
400 x509_crt *cert;
401 pk_context *key;
402 sni_entry *next;
403};
404
405/*
406 * Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]]
407 * into a usable sni_entry list.
408 *
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200409 * Modifies the input string! This is not production quality!
410 * (leaks memory if parsing fails, no error reporting, ...)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100411 */
412sni_entry *sni_parse( char *sni_string )
413{
414 sni_entry *cur = NULL, *new = NULL;
415 char *p = sni_string;
416 char *end = p;
417 char *crt_file, *key_file;
418
419 while( *end != '\0' )
420 ++end;
421 *end = ',';
422
423 while( p <= end )
424 {
425 if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL )
426 return( NULL );
427
428 memset( new, 0, sizeof( sni_entry ) );
429
430 if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ||
431 ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200432 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100433
434 x509_crt_init( new->cert );
435 pk_init( new->key );
436
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200437 GET_ITEM( new->name );
438 GET_ITEM( crt_file );
439 GET_ITEM( key_file );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100440
441 if( x509_crt_parse_file( new->cert, crt_file ) != 0 ||
442 pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200443 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100444
445 new->next = cur;
446 cur = new;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100447 }
448
449 return( cur );
450}
451
452void sni_free( sni_entry *head )
453{
454 sni_entry *cur = head, *next;
455
456 while( cur != NULL )
457 {
458 x509_crt_free( cur->cert );
459 polarssl_free( cur->cert );
460
461 pk_free( cur->key );
462 polarssl_free( cur->key );
463
464 next = cur->next;
465 polarssl_free( cur );
466 cur = next;
467 }
468}
469
470/*
471 * SNI callback.
472 */
473int sni_callback( void *p_info, ssl_context *ssl,
474 const unsigned char *name, size_t name_len )
475{
476 sni_entry *cur = (sni_entry *) p_info;
477
478 while( cur != NULL )
479 {
480 if( name_len == strlen( cur->name ) &&
481 memcmp( name, cur->name, name_len ) == 0 )
482 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200483 return( ssl_set_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100484 }
485
486 cur = cur->next;
487 }
488
489 return( -1 );
490}
491
492#endif /* POLARSSL_SNI */
493
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200494#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
495
496#define HEX2NUM( c ) \
497 if( c >= '0' && c <= '9' ) \
498 c -= '0'; \
499 else if( c >= 'a' && c <= 'f' ) \
500 c -= 'a' - 10; \
501 else if( c >= 'A' && c <= 'F' ) \
502 c -= 'A' - 10; \
503 else \
504 return( -1 );
505
506/*
507 * Convert a hex string to bytes.
508 * Return 0 on success, -1 on error.
509 */
510int unhexify( unsigned char *output, const char *input, size_t *olen )
511{
512 unsigned char c;
513 size_t j;
514
515 *olen = strlen( input );
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200516 if( *olen % 2 != 0 || *olen / 2 > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200517 return( -1 );
518 *olen /= 2;
519
520 for( j = 0; j < *olen * 2; j += 2 )
521 {
522 c = input[j];
523 HEX2NUM( c );
524 output[ j / 2 ] = c << 4;
525
526 c = input[j + 1];
527 HEX2NUM( c );
528 output[ j / 2 ] |= c;
529 }
530
531 return( 0 );
532}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200533
534typedef struct _psk_entry psk_entry;
535
536struct _psk_entry
537{
538 const char *name;
539 size_t key_len;
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200540 unsigned char key[POLARSSL_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200541 psk_entry *next;
542};
543
544/*
545 * Parse a string of pairs name1,key1[,name2,key2[,...]]
546 * into a usable psk_entry list.
547 *
548 * Modifies the input string! This is not production quality!
549 * (leaks memory if parsing fails, no error reporting, ...)
550 */
551psk_entry *psk_parse( char *psk_string )
552{
553 psk_entry *cur = NULL, *new = NULL;
554 char *p = psk_string;
555 char *end = p;
556 char *key_hex;
557
558 while( *end != '\0' )
559 ++end;
560 *end = ',';
561
562 while( p <= end )
563 {
564 if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL )
565 return( NULL );
566
567 memset( new, 0, sizeof( psk_entry ) );
568
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200569 GET_ITEM( new->name );
570 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200571
572 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
573 return( NULL );
574
575 new->next = cur;
576 cur = new;
577 }
578
579 return( cur );
580}
581
582/*
583 * Free a list of psk_entry's
584 */
585void psk_free( psk_entry *head )
586{
587 psk_entry *next;
588
589 while( head != NULL )
590 {
591 next = head->next;
592 polarssl_free( head );
593 head = next;
594 }
595}
596
597/*
598 * PSK callback
599 */
600int psk_callback( void *p_info, ssl_context *ssl,
601 const unsigned char *name, size_t name_len )
602{
603 psk_entry *cur = (psk_entry *) p_info;
604
605 while( cur != NULL )
606 {
607 if( name_len == strlen( cur->name ) &&
608 memcmp( name, cur->name, name_len ) == 0 )
609 {
610 return( ssl_set_psk( ssl, cur->key, cur->key_len,
611 name, name_len ) );
612 }
613
614 cur = cur->next;
615 }
616
617 return( -1 );
618}
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200619#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
620
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200621static int listen_fd;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200622
623/* Interruption handler to ensure clean exit (for valgrind testing) */
624#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200625static int received_sigterm = 0;
626void term_handler( int sig )
627{
628 ((void) sig);
629 received_sigterm = 1;
630 net_close( listen_fd ); /* causes net_accept() to abort */
631}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200632#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200633
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000634int main( int argc, char *argv[] )
635{
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200636 int ret = 0, len, written, frags, exchanges;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000637 int client_fd = -1;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200638 int version_suites[4][2];
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200639 unsigned char buf[IO_BUF_LEN];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200640#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200641 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +0200642 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +0200643 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +0200644#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200645 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +0200646 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +0200647#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200648 ssl_cookie_ctx cookie_ctx;
649#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000650
651 entropy_context entropy;
652 ctr_drbg_context ctr_drbg;
653 ssl_context ssl;
Paul Bakker36713e82013-09-17 13:25:29 +0200654#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200655 x509_crt cacert;
656 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200657 pk_context pkey;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200658 x509_crt srvcert2;
659 pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200660 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200661#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200662#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
663 dhm_context dhm;
664#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000665#if defined(POLARSSL_SSL_CACHE_C)
666 ssl_cache_context cache;
667#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100668#if defined(POLARSSL_SNI)
669 sni_entry *sni_info = NULL;
670#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200671#if defined(POLARSSL_SSL_ALPN)
672 const char *alpn_list[10];
673#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200674#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
675 unsigned char alloc_buf[100000];
676#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000677
678 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000679 char *p, *q;
680 const int *list;
681
Paul Bakker82024bf2013-07-04 11:52:32 +0200682#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
683 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
684#endif
685
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000686 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200687 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000688 */
689 listen_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200690 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200691#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200692 x509_crt_init( &cacert );
693 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200694 pk_init( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200695 x509_crt_init( &srvcert2 );
696 pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200697#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200698#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
Paul Bakkera317a982014-06-18 16:44:11 +0200699 dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200700#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000701#if defined(POLARSSL_SSL_CACHE_C)
702 ssl_cache_init( &cache );
703#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200704#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200705 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200706#endif
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +0200707#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200708 ssl_cookie_init( &cookie_ctx );
709#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000710
Paul Bakkerc1283d32014-08-18 11:05:51 +0200711#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200712 /* Abort cleanly on SIGTERM */
713 signal( SIGTERM, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +0200714#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200715
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000716 if( argc == 0 )
717 {
718 usage:
719 if( ret == 0 )
720 ret = 1;
721
722 printf( USAGE );
723
724 list = ssl_list_ciphersuites();
725 while( *list )
726 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200727 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200728 list++;
729 if( !*list )
730 break;
731 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000732 list++;
733 }
734 printf("\n");
735 goto exit;
736 }
737
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100738 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000739 opt.server_port = DFL_SERVER_PORT;
740 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100741 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200742 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000743 opt.ca_file = DFL_CA_FILE;
744 opt.ca_path = DFL_CA_PATH;
745 opt.crt_file = DFL_CRT_FILE;
746 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200747 opt.crt_file2 = DFL_CRT_FILE2;
748 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200749 opt.psk = DFL_PSK;
750 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200751 opt.psk_list = DFL_PSK_LIST;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000752 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200753 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000754 opt.renegotiation = DFL_RENEGOTIATION;
755 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100756 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200757 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200758 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000759 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200760 opt.max_version = DFL_MAX_VERSION;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100761 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200762 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200763 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100764 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100765 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100766 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100767 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200768 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200769 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100770 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200771 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200772 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200773 opt.hs_to_min = DFL_HS_TO_MIN;
774 opt.hs_to_max = DFL_HS_TO_MAX;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000775
776 for( i = 1; i < argc; i++ )
777 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000778 p = argv[i];
779 if( ( q = strchr( p, '=' ) ) == NULL )
780 goto usage;
781 *q++ = '\0';
782
783 if( strcmp( p, "server_port" ) == 0 )
784 {
785 opt.server_port = atoi( q );
786 if( opt.server_port < 1 || opt.server_port > 65535 )
787 goto usage;
788 }
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100789 else if( strcmp( p, "server_addr" ) == 0 )
790 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100791 else if( strcmp( p, "dtls" ) == 0 )
792 {
793 int t = atoi( q );
794 if( t == 0 )
795 opt.transport = SSL_TRANSPORT_STREAM;
796 else if( t == 1 )
797 opt.transport = SSL_TRANSPORT_DATAGRAM;
798 else
799 goto usage;
800 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000801 else if( strcmp( p, "debug_level" ) == 0 )
802 {
803 opt.debug_level = atoi( q );
804 if( opt.debug_level < 0 || opt.debug_level > 65535 )
805 goto usage;
806 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100807 else if( strcmp( p, "nbio" ) == 0 )
808 {
809 opt.nbio = atoi( q );
810 if( opt.nbio < 0 || opt.nbio > 2 )
811 goto usage;
812 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200813 else if( strcmp( p, "read_timeout" ) == 0 )
814 opt.read_timeout = atoi( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000815 else if( strcmp( p, "ca_file" ) == 0 )
816 opt.ca_file = q;
817 else if( strcmp( p, "ca_path" ) == 0 )
818 opt.ca_path = q;
819 else if( strcmp( p, "crt_file" ) == 0 )
820 opt.crt_file = q;
821 else if( strcmp( p, "key_file" ) == 0 )
822 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200823 else if( strcmp( p, "crt_file2" ) == 0 )
824 opt.crt_file2 = q;
825 else if( strcmp( p, "key_file2" ) == 0 )
826 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200827 else if( strcmp( p, "dhm_file" ) == 0 )
828 opt.dhm_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200829 else if( strcmp( p, "psk" ) == 0 )
830 opt.psk = q;
831 else if( strcmp( p, "psk_identity" ) == 0 )
832 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200833 else if( strcmp( p, "psk_list" ) == 0 )
834 opt.psk_list = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000835 else if( strcmp( p, "force_ciphersuite" ) == 0 )
836 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000837 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
838
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200839 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000840 {
841 ret = 2;
842 goto usage;
843 }
844 opt.force_ciphersuite[1] = 0;
845 }
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200846 else if( strcmp( p, "version_suites" ) == 0 )
847 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000848 else if( strcmp( p, "renegotiation" ) == 0 )
849 {
850 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
851 SSL_RENEGOTIATION_DISABLED;
852 }
853 else if( strcmp( p, "allow_legacy" ) == 0 )
854 {
855 opt.allow_legacy = atoi( q );
856 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
857 goto usage;
858 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100859 else if( strcmp( p, "renegotiate" ) == 0 )
860 {
861 opt.renegotiate = atoi( q );
862 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
863 goto usage;
864 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200865 else if( strcmp( p, "renego_delay" ) == 0 )
866 {
867 opt.renego_delay = atoi( q );
868 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200869 else if( strcmp( p, "exchanges" ) == 0 )
870 {
871 opt.exchanges = atoi( q );
872 if( opt.exchanges < 1 )
873 goto usage;
874 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000875 else if( strcmp( p, "min_version" ) == 0 )
876 {
877 if( strcmp( q, "ssl3" ) == 0 )
878 opt.min_version = SSL_MINOR_VERSION_0;
879 else if( strcmp( q, "tls1" ) == 0 )
880 opt.min_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100881 else if( strcmp( q, "tls1_1" ) == 0 ||
882 strcmp( q, "dtls1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000883 opt.min_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100884 else if( strcmp( q, "tls1_2" ) == 0 ||
885 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000886 opt.min_version = SSL_MINOR_VERSION_3;
887 else
888 goto usage;
889 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200890 else if( strcmp( p, "max_version" ) == 0 )
891 {
892 if( strcmp( q, "ssl3" ) == 0 )
893 opt.max_version = SSL_MINOR_VERSION_0;
894 else if( strcmp( q, "tls1" ) == 0 )
895 opt.max_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100896 else if( strcmp( q, "tls1_1" ) == 0 ||
897 strcmp( q, "dtls1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200898 opt.max_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100899 else if( strcmp( q, "tls1_2" ) == 0 ||
900 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200901 opt.max_version = SSL_MINOR_VERSION_3;
902 else
903 goto usage;
904 }
905 else if( strcmp( p, "force_version" ) == 0 )
906 {
907 if( strcmp( q, "ssl3" ) == 0 )
908 {
909 opt.min_version = SSL_MINOR_VERSION_0;
910 opt.max_version = SSL_MINOR_VERSION_0;
911 }
912 else if( strcmp( q, "tls1" ) == 0 )
913 {
914 opt.min_version = SSL_MINOR_VERSION_1;
915 opt.max_version = SSL_MINOR_VERSION_1;
916 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100917 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200918 {
919 opt.min_version = SSL_MINOR_VERSION_2;
920 opt.max_version = SSL_MINOR_VERSION_2;
921 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100922 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200923 {
924 opt.min_version = SSL_MINOR_VERSION_3;
925 opt.max_version = SSL_MINOR_VERSION_3;
926 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100927 else if( strcmp( q, "dtls1" ) == 0 )
928 {
929 opt.min_version = SSL_MINOR_VERSION_2;
930 opt.max_version = SSL_MINOR_VERSION_2;
931 opt.transport = SSL_TRANSPORT_DATAGRAM;
932 }
933 else if( strcmp( q, "dtls1_2" ) == 0 )
934 {
935 opt.min_version = SSL_MINOR_VERSION_3;
936 opt.max_version = SSL_MINOR_VERSION_3;
937 opt.transport = SSL_TRANSPORT_DATAGRAM;
938 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200939 else
940 goto usage;
941 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100942 else if( strcmp( p, "auth_mode" ) == 0 )
943 {
944 if( strcmp( q, "none" ) == 0 )
945 opt.auth_mode = SSL_VERIFY_NONE;
946 else if( strcmp( q, "optional" ) == 0 )
947 opt.auth_mode = SSL_VERIFY_OPTIONAL;
948 else if( strcmp( q, "required" ) == 0 )
949 opt.auth_mode = SSL_VERIFY_REQUIRED;
950 else
951 goto usage;
952 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200953 else if( strcmp( p, "max_frag_len" ) == 0 )
954 {
955 if( strcmp( q, "512" ) == 0 )
956 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
957 else if( strcmp( q, "1024" ) == 0 )
958 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
959 else if( strcmp( q, "2048" ) == 0 )
960 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
961 else if( strcmp( q, "4096" ) == 0 )
962 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
963 else
964 goto usage;
965 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200966 else if( strcmp( p, "alpn" ) == 0 )
967 {
968 opt.alpn_string = q;
969 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200970 else if( strcmp( p, "tickets" ) == 0 )
971 {
972 opt.tickets = atoi( q );
973 if( opt.tickets < 0 || opt.tickets > 1 )
974 goto usage;
975 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100976 else if( strcmp( p, "ticket_timeout" ) == 0 )
977 {
978 opt.ticket_timeout = atoi( q );
979 if( opt.ticket_timeout < 0 )
980 goto usage;
981 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100982 else if( strcmp( p, "cache_max" ) == 0 )
983 {
984 opt.cache_max = atoi( q );
985 if( opt.cache_max < 0 )
986 goto usage;
987 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100988 else if( strcmp( p, "cache_timeout" ) == 0 )
989 {
990 opt.cache_timeout = atoi( q );
991 if( opt.cache_timeout < 0 )
992 goto usage;
993 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200994 else if( strcmp( p, "cookies" ) == 0 )
995 {
996 opt.cookies = atoi( q );
997 if( opt.cookies < -1 || opt.cookies > 1)
998 goto usage;
999 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001000 else if( strcmp( p, "anti_replay" ) == 0 )
1001 {
1002 opt.anti_replay = atoi( q );
1003 if( opt.anti_replay < 0 || opt.anti_replay > 1)
1004 goto usage;
1005 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001006 else if( strcmp( p, "hs_timeout" ) == 0 )
1007 {
1008 if( ( p = strchr( q, '-' ) ) == NULL )
1009 goto usage;
1010 *p++ = '\0';
1011 opt.hs_to_min = atoi( q );
1012 opt.hs_to_max = atoi( p );
1013 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1014 goto usage;
1015 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001016 else if( strcmp( p, "sni" ) == 0 )
1017 {
1018 opt.sni = q;
1019 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001020 else
1021 goto usage;
1022 }
1023
Paul Bakkerc73079a2014-04-25 16:34:30 +02001024#if defined(POLARSSL_DEBUG_C)
1025 debug_set_threshold( opt.debug_level );
1026#endif
1027
Paul Bakkerc1516be2013-06-29 16:01:32 +02001028 if( opt.force_ciphersuite[0] > 0 )
1029 {
1030 const ssl_ciphersuite_t *ciphersuite_info;
1031 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1032
Paul Bakker5b55b792013-07-19 13:43:43 +02001033 if( opt.max_version != -1 &&
1034 ciphersuite_info->min_minor_ver > opt.max_version )
1035 {
1036 printf("forced ciphersuite not allowed with this protocol version\n");
1037 ret = 2;
1038 goto usage;
1039 }
1040 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001041 ciphersuite_info->max_minor_ver < opt.min_version )
1042 {
1043 printf("forced ciphersuite not allowed with this protocol version\n");
1044 ret = 2;
1045 goto usage;
1046 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001047
1048 /* If we select a version that's not supported by
1049 * this suite, then there will be no common ciphersuite... */
1050 if( opt.max_version == -1 ||
1051 opt.max_version > ciphersuite_info->max_minor_ver )
1052 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001053 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001054 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001055 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001056 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001057 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001058 /* DTLS starts with TLS 1.1 */
1059 if( opt.transport == SSL_TRANSPORT_DATAGRAM &&
1060 opt.min_version < SSL_MINOR_VERSION_2 )
1061 opt.min_version = SSL_MINOR_VERSION_2;
1062 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001063 }
1064
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001065 if( opt.version_suites != NULL )
1066 {
1067 const char *name[4] = { 0 };
1068
1069 /* Parse 4-element coma-separated list */
1070 for( i = 0, p = (char *) opt.version_suites;
1071 i < 4 && *p != '\0';
1072 i++ )
1073 {
1074 name[i] = p;
1075
1076 /* Terminate the current string and move on to next one */
1077 while( *p != ',' && *p != '\0' )
1078 p++;
1079 if( *p == ',' )
1080 *p++ = '\0';
1081 }
1082
1083 if( i != 4 )
1084 {
1085 printf( "too few values for version_suites\n" );
1086 ret = 1;
1087 goto exit;
1088 }
1089
1090 memset( version_suites, 0, sizeof( version_suites ) );
1091
1092 /* Get the suites identifiers from their name */
1093 for( i = 0; i < 4; i++ )
1094 {
1095 version_suites[i][0] = ssl_get_ciphersuite_id( name[i] );
1096
1097 if( version_suites[i][0] == 0 )
1098 {
1099 printf( "unknown ciphersuite: '%s'\n", name[i] );
1100 ret = 2;
1101 goto usage;
1102 }
1103 }
1104 }
1105
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001106#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001107 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001108 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001109 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001110 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001111 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001112 printf( "pre-shared key not valid hex\n" );
1113 goto exit;
1114 }
1115
1116 if( opt.psk_list != NULL )
1117 {
1118 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001119 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001120 printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001121 goto exit;
1122 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001123 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001124#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001125
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001126#if defined(POLARSSL_SSL_ALPN)
1127 if( opt.alpn_string != NULL )
1128 {
1129 p = (char *) opt.alpn_string;
1130 i = 0;
1131
1132 /* Leave room for a final NULL in alpn_list */
1133 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
1134 {
1135 alpn_list[i++] = p;
1136
1137 /* Terminate the current string and move on to next one */
1138 while( *p != ',' && *p != '\0' )
1139 p++;
1140 if( *p == ',' )
1141 *p++ = '\0';
1142 }
1143 }
1144#endif /* POLARSSL_SSL_ALPN */
1145
Paul Bakkerfbb17802013-04-17 19:10:21 +02001146 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001147 * 0. Initialize the RNG and the session data
1148 */
1149 printf( "\n . Seeding the random number generator..." );
1150 fflush( stdout );
1151
1152 entropy_init( &entropy );
1153 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001154 (const unsigned char *) pers,
1155 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001156 {
1157 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
1158 goto exit;
1159 }
1160
1161 printf( " ok\n" );
1162
Paul Bakker36713e82013-09-17 13:25:29 +02001163#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001164 /*
1165 * 1.1. Load the trusted CA
1166 */
1167 printf( " . Loading the CA root certificate ..." );
1168 fflush( stdout );
1169
1170#if defined(POLARSSL_FS_IO)
1171 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001172 if( strcmp( opt.ca_path, "none" ) == 0 )
1173 ret = 0;
1174 else
1175 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001176 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001177 if( strcmp( opt.ca_file, "none" ) == 0 )
1178 ret = 0;
1179 else
1180 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001181 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001182#endif
1183#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +02001184 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
1185 strlen( test_ca_list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001186#else
1187 {
1188 ret = 1;
1189 printf("POLARSSL_CERTS_C not defined.");
1190 }
1191#endif
1192 if( ret < 0 )
1193 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001194 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001195 goto exit;
1196 }
1197
1198 printf( " ok (%d skipped)\n", ret );
1199
1200 /*
1201 * 1.2. Load own certificate and private key
1202 */
1203 printf( " . Loading the server cert. and key..." );
1204 fflush( stdout );
1205
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001206#if defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001207 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001208 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001209 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001210 if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
1211 {
1212 printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n",
1213 -ret );
1214 goto exit;
1215 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001216 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001217 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001218 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001219 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001220 if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
1221 {
1222 printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret );
1223 goto exit;
1224 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001225 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001226 if( key_cert_init == 1 )
1227 {
1228 printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
1229 goto exit;
1230 }
1231
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001232 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001233 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001234 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001235 if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
1236 {
1237 printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n",
1238 -ret );
1239 goto exit;
1240 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001241 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001242 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001243 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001244 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001245 if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
1246 {
1247 printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n",
1248 -ret );
1249 goto exit;
1250 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001251 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001252 if( key_cert_init2 == 1 )
1253 {
1254 printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
1255 goto exit;
1256 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001257#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001258 if( key_cert_init == 0 &&
1259 strcmp( opt.crt_file, "none" ) != 0 &&
1260 strcmp( opt.key_file, "none" ) != 0 &&
1261 key_cert_init2 == 0 &&
1262 strcmp( opt.crt_file2, "none" ) != 0 &&
1263 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001264 {
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001265#if !defined(POLARSSL_CERTS_C)
1266 printf( "Not certificated or key provided, and \n"
1267 "POLARSSL_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001268 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001269#else
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001270#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001271 if( ( ret = x509_crt_parse( &srvcert,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001272 (const unsigned char *) test_srv_crt_rsa,
1273 strlen( test_srv_crt_rsa ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001274 {
1275 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
1276 goto exit;
1277 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001278 if( ( ret = pk_parse_key( &pkey,
1279 (const unsigned char *) test_srv_key_rsa,
1280 strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001281 {
1282 printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
1283 goto exit;
1284 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001285 key_cert_init = 2;
1286#endif /* POLARSSL_RSA_C */
1287#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001288 if( ( ret = x509_crt_parse( &srvcert2,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001289 (const unsigned char *) test_srv_crt_ec,
1290 strlen( test_srv_crt_ec ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001291 {
1292 printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
1293 goto exit;
1294 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001295 if( ( ret = pk_parse_key( &pkey2,
1296 (const unsigned char *) test_srv_key_ec,
1297 strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001298 {
1299 printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
1300 goto exit;
1301 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001302 key_cert_init2 = 2;
1303#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001304#endif /* POLARSSL_CERTS_C */
1305 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001306
1307 printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001308#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001309
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001310#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1311 if( opt.dhm_file != NULL )
1312 {
1313 printf( " . Loading DHM parameters..." );
1314 fflush( stdout );
1315
1316 if( ( ret = dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
1317 {
1318 printf( " failed\n ! dhm_parse_dhmfile returned -0x%04X\n\n",
1319 -ret );
1320 goto exit;
1321 }
1322
1323 printf( " ok\n" );
1324 }
1325#endif
1326
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001327#if defined(POLARSSL_SNI)
1328 if( opt.sni != NULL )
1329 {
1330 printf( " . Setting up SNI information..." );
1331 fflush( stdout );
1332
1333 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
1334 {
1335 printf( " failed\n" );
1336 goto exit;
1337 }
1338
1339 printf( " ok\n" );
1340 }
1341#endif /* POLARSSL_SNI */
1342
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001343 /*
1344 * 2. Setup the listening TCP socket
1345 */
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001346 printf( " . Bind on %s://%s:%-4d/ ...",
1347 opt.transport == SSL_TRANSPORT_STREAM ? "tcp" : "udp",
1348 opt.server_addr ? opt.server_addr : "*",
1349 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001350 fflush( stdout );
1351
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001352 if( ( ret = net_bind( &listen_fd, opt.server_addr, opt.server_port,
1353 opt.transport == SSL_TRANSPORT_STREAM ?
1354 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001355 {
1356 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1357 goto exit;
1358 }
1359
1360 printf( " ok\n" );
1361
1362 /*
1363 * 3. Setup stuff
1364 */
1365 printf( " . Setting up the SSL/TLS structure..." );
1366 fflush( stdout );
1367
1368 if( ( ret = ssl_init( &ssl ) ) != 0 )
1369 {
1370 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
1371 goto exit;
1372 }
1373
1374 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001375 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001376
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001377#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001378 if( ( ret = ssl_set_transport( &ssl, opt.transport ) ) != 0 )
1379 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001380 printf( " failed\n ! selected transport is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001381 goto exit;
1382 }
1383
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001384 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
1385 ssl_set_handshake_timeout( &ssl, opt.hs_to_min, opt.hs_to_max );
1386#endif /* POLARSSL_SSL_PROTO_DTLS */
1387
Paul Bakker05decb22013-08-15 13:33:48 +02001388#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001389 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1390 {
1391 printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
1392 goto exit;
1393 };
Paul Bakker05decb22013-08-15 13:33:48 +02001394#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001395
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001396#if defined(POLARSSL_SSL_ALPN)
1397 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001398 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1399 {
1400 printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
1401 goto exit;
1402 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001403#endif
1404
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001405 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
1406 ssl_set_dbg( &ssl, my_debug, stdout );
1407
Paul Bakker0a597072012-09-25 21:55:46 +00001408#if defined(POLARSSL_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001409 if( opt.cache_max != -1 )
1410 ssl_cache_set_max_entries( &cache, opt.cache_max );
1411
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001412 if( opt.cache_timeout != -1 )
1413 ssl_cache_set_timeout( &cache, opt.cache_timeout );
1414
Paul Bakker0a597072012-09-25 21:55:46 +00001415 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
1416 ssl_cache_set, &cache );
1417#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001418
Paul Bakkera503a632013-08-14 13:48:06 +02001419#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001420 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1421 {
1422 printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
1423 goto exit;
1424 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001425
1426 if( opt.ticket_timeout != -1 )
1427 ssl_set_session_ticket_lifetime( &ssl, opt.ticket_timeout );
Paul Bakkera503a632013-08-14 13:48:06 +02001428#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001429
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001430#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001431 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001432 {
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001433#if defined(POLARSSL_SSL_COOKIE_C)
1434 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001435 {
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001436 if( ( ret = ssl_cookie_setup( &cookie_ctx,
1437 ctr_drbg_random, &ctr_drbg ) ) != 0 )
1438 {
1439 printf( " failed\n ! ssl_setup_hvr_key returned %d\n\n", ret );
1440 goto exit;
1441 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001442
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001443 ssl_set_dtls_cookies( &ssl, ssl_cookie_write, ssl_cookie_check,
1444 &cookie_ctx );
1445 }
1446 else
1447#endif /* POLARSSL_SSL_COOKIE_C */
1448#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
1449 if( opt.cookies == 0 )
1450 {
1451 ssl_set_dtls_cookies( &ssl, NULL, NULL, NULL );
1452 }
1453 else
1454#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
1455 {
1456 ; /* Nothing to do */
1457 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001458
1459#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1460 if( opt.anti_replay != DFL_ANTI_REPLAY )
1461 {
1462 ssl_set_dtls_anti_replay( &ssl, opt.anti_replay );
1463 }
1464#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001465 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001466#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001467
Paul Bakker41c83d32013-03-20 14:39:14 +01001468 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001469 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
1470
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001471 if( opt.version_suites != NULL )
1472 {
1473 ssl_set_ciphersuites_for_version( &ssl, version_suites[0],
1474 SSL_MAJOR_VERSION_3,
1475 SSL_MINOR_VERSION_0 );
1476 ssl_set_ciphersuites_for_version( &ssl, version_suites[1],
1477 SSL_MAJOR_VERSION_3,
1478 SSL_MINOR_VERSION_1 );
1479 ssl_set_ciphersuites_for_version( &ssl, version_suites[2],
1480 SSL_MAJOR_VERSION_3,
1481 SSL_MINOR_VERSION_2 );
1482 ssl_set_ciphersuites_for_version( &ssl, version_suites[3],
1483 SSL_MAJOR_VERSION_3,
1484 SSL_MINOR_VERSION_3 );
1485 }
1486
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001487 ssl_set_renegotiation( &ssl, opt.renegotiation );
1488 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001489 if( opt.renego_delay != DFL_RENEGO_DELAY )
1490 ssl_set_renegotiation_enforced( &ssl, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001491
Paul Bakker36713e82013-09-17 13:25:29 +02001492#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001493 if( strcmp( opt.ca_path, "none" ) != 0 &&
1494 strcmp( opt.ca_file, "none" ) != 0 )
1495 {
1496 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
1497 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001498 if( key_cert_init )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001499 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
1500 {
1501 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1502 goto exit;
1503 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001504 if( key_cert_init2 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001505 if( ( ret = ssl_set_own_cert( &ssl, &srvcert2, &pkey2 ) ) != 0 )
1506 {
1507 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1508 goto exit;
1509 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001510#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001511
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001512#if defined(POLARSSL_SNI)
1513 if( opt.sni != NULL )
1514 ssl_set_sni( &ssl, sni_callback, sni_info );
1515#endif
1516
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001517#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001518 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
1519 {
1520 ret = ssl_set_psk( &ssl, psk, psk_len,
1521 (const unsigned char *) opt.psk_identity,
1522 strlen( opt.psk_identity ) );
1523 if( ret != 0 )
1524 {
1525 printf( " failed\n ssl_set_psk returned -0x%04X\n\n", - ret );
1526 goto exit;
1527 }
1528 }
1529
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001530 if( opt.psk_list != NULL )
1531 ssl_set_psk_cb( &ssl, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02001532#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001533
1534#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00001535 /*
1536 * Use different group than default DHM group
1537 */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001538#if defined(POLARSSL_FS_IO)
1539 if( opt.dhm_file != NULL )
1540 ret = ssl_set_dh_param_ctx( &ssl, &dhm );
1541 else
1542#endif
1543 ret = ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
1544 POLARSSL_DHM_RFC5114_MODP_2048_G );
1545
1546 if( ret != 0 )
1547 {
1548 printf( " failed\n ssl_set_dh_param returned -0x%04X\n\n", - ret );
1549 goto exit;
1550 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001551#endif
1552
Paul Bakker1d29fb52012-09-28 13:28:45 +00001553 if( opt.min_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001554 {
1555 ret = ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1556 if( ret != 0 )
1557 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001558 printf( " failed\n ! selected min_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001559 goto exit;
1560 }
1561 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001562
Paul Bakkerc1516be2013-06-29 16:01:32 +02001563 if( opt.max_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001564 {
1565 ret = ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1566 if( ret != 0 )
1567 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001568 printf( " failed\n ! selected max_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001569 goto exit;
1570 }
1571 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001572
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001573 printf( " ok\n" );
1574
1575reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02001576#if !defined(_WIN32)
1577 if( received_sigterm )
1578 {
1579 printf( " interrupted by SIGTERM\n" );
1580 ret = 0;
1581 goto exit;
1582 }
1583#endif
1584
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001585#ifdef POLARSSL_ERROR_C
1586 if( ret != 0 )
1587 {
1588 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001589 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001590 printf("Last error was: %d - %s\n\n", ret, error_buf );
1591 }
1592#endif
1593
1594 if( client_fd != -1 )
Manuel Pégourié-Gonnard4ba6ab62014-08-07 17:21:47 +02001595 net_close( client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001596
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001597 ssl_session_reset( &ssl );
1598
1599 /*
1600 * 3. Wait until a client connects
1601 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001602 client_fd = -1;
1603
1604 printf( " . Waiting for a remote connection ..." );
1605 fflush( stdout );
1606
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001607 if( ( ret = net_accept( listen_fd, &client_fd, client_ip ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001608 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02001609#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001610 if( received_sigterm )
1611 {
1612 printf( " interrupted by SIGTERM\n" );
1613 ret = 0;
1614 goto exit;
1615 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02001616#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001617
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001618 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
1619 goto exit;
1620 }
1621
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001622 if( opt.nbio > 0 )
1623 ret = net_set_nonblock( client_fd );
1624 else
1625 ret = net_set_block( client_fd );
1626 if( ret != 0 )
1627 {
1628 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
1629 goto exit;
1630 }
1631
1632 if( opt.nbio == 2 )
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001633 ssl_set_bio_timeout( &ssl, &client_fd, my_send, my_recv, NULL, 0 );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001634 else
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001635 ssl_set_bio_timeout( &ssl, &client_fd, net_send, net_recv,
1636#if defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnardf0365122014-09-29 16:11:47 +02001637 opt.nbio == 0 ? net_recv_timeout : NULL,
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001638#else
1639 NULL,
1640#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001641 opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001642
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001643#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001644 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
1645 {
1646 if( ( ret = ssl_set_client_transport_id( &ssl, client_ip,
1647 sizeof( client_ip ) ) ) != 0 )
1648 {
1649 printf( " failed\n ! "
1650 "ssl_set_client_tranport_id() returned -0x%x\n\n", -ret );
1651 goto exit;
1652 }
1653 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001654#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001655
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001656 printf( " ok\n" );
1657
1658 /*
Manuel Pégourié-Gonnardbd97fdb2014-09-26 16:46:36 +02001659 * With UDP, bind_fd is hijacked by client_fd, so bind a new one
1660 */
1661#if defined(POLARSSL_SSL_PROTO_DTLS)
1662 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
1663 {
1664 printf( " . Re-bind on udp://%s:%-4d/ ...",
1665 opt.server_addr ? opt.server_addr : "*",
1666 opt.server_port );
1667 fflush( stdout );
1668
1669 if( ( ret = net_bind( &listen_fd, opt.server_addr,
1670 opt.server_port, NET_PROTO_UDP ) ) != 0 )
1671 {
1672 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1673 goto exit;
1674 }
1675
1676 printf( " ok\n" );
1677 }
1678#endif /* POLARSSL_SSL_PROTO_DTLS */
1679
1680 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001681 * 4. Handshake
1682 */
1683 printf( " . Performing the SSL/TLS handshake..." );
1684 fflush( stdout );
1685
1686 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1687 {
1688 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1689 {
1690 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001691 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001692 }
1693 }
1694
Manuel Pégourié-Gonnardc580a002014-02-12 10:15:30 +01001695 printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1696 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001697
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001698#if defined(POLARSSL_SSL_ALPN)
1699 if( opt.alpn_string != NULL )
1700 {
1701 const char *alp = ssl_get_alpn_protocol( &ssl );
1702 printf( " [ Application Layer Protocol is %s ]\n",
1703 alp ? alp : "(none)" );
1704 }
1705#endif
1706
Paul Bakker36713e82013-09-17 13:25:29 +02001707#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001708 /*
1709 * 5. Verify the server certificate
1710 */
1711 printf( " . Verifying peer X.509 certificate..." );
1712
1713 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1714 {
1715 printf( " failed\n" );
1716
Paul Bakkerb0550d92012-10-30 07:51:03 +00001717 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001718 printf( " ! no client certificate sent\n" );
1719
1720 if( ( ret & BADCERT_EXPIRED ) != 0 )
1721 printf( " ! client certificate has expired\n" );
1722
1723 if( ( ret & BADCERT_REVOKED ) != 0 )
1724 printf( " ! client certificate has been revoked\n" );
1725
1726 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
1727 printf( " ! self-signed or not signed by a trusted CA\n" );
1728
1729 printf( "\n" );
1730 }
1731 else
1732 printf( " ok\n" );
1733
Paul Bakkerb0550d92012-10-30 07:51:03 +00001734 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001735 {
1736 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001737 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1738 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001739 printf( "%s\n", buf );
1740 }
Paul Bakker36713e82013-09-17 13:25:29 +02001741#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001742
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001743 exchanges = opt.exchanges;
1744data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001745 /*
1746 * 6. Read the HTTP Request
1747 */
1748 printf( " < Read from client:" );
1749 fflush( stdout );
1750
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001751 /*
1752 * TLS and DTLS need different reading styles (stream vs datagram)
1753 */
1754 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001755 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001756 do
1757 {
1758 int terminated = 0;
1759 len = sizeof( buf ) - 1;
1760 memset( buf, 0, sizeof( buf ) );
1761 ret = ssl_read( &ssl, buf, len );
1762
1763 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1764 ret == POLARSSL_ERR_NET_WANT_WRITE )
1765 continue;
1766
1767 if( ret <= 0 )
1768 {
1769 switch( ret )
1770 {
1771 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1772 printf( " connection was closed gracefully\n" );
1773 goto close_notify;
1774
1775 case 0:
1776 case POLARSSL_ERR_NET_CONN_RESET:
1777 printf( " connection was reset by peer\n" );
1778 ret = POLARSSL_ERR_NET_CONN_RESET;
1779 goto reset;
1780
1781 default:
1782 printf( " ssl_read returned -0x%x\n", -ret );
1783 goto reset;
1784 }
1785 }
1786
1787 if( ssl_get_bytes_avail( &ssl ) == 0 )
1788 {
1789 len = ret;
1790 buf[len] = '\0';
1791 printf( " %d bytes read\n\n%s\n", len, (char *) buf );
1792
1793 /* End of message should be detected according to the syntax of the
1794 * application protocol (eg HTTP), just use a dummy test here. */
1795 if( buf[len - 1] == '\n' )
1796 terminated = 1;
1797 }
1798 else
1799 {
1800 int extra_len, ori_len;
1801 unsigned char *larger_buf;
1802
1803 ori_len = ret;
1804 extra_len = ssl_get_bytes_avail( &ssl );
1805
1806 larger_buf = polarssl_malloc( ori_len + extra_len + 1 );
1807 if( larger_buf == NULL )
1808 {
1809 printf( " ! memory allocation failed\n" );
1810 ret = 1;
1811 goto reset;
1812 }
1813
1814 memset( larger_buf, 0, ori_len + extra_len );
1815 memcpy( larger_buf, buf, ori_len );
1816
1817 /* This read should never fail and get the whole cached data */
1818 ret = ssl_read( &ssl, larger_buf + ori_len, extra_len );
1819 if( ret != extra_len ||
1820 ssl_get_bytes_avail( &ssl ) != 0 )
1821 {
1822 printf( " ! ssl_read failed on cached data\n" );
1823 ret = 1;
1824 goto reset;
1825 }
1826
1827 larger_buf[ori_len + extra_len] = '\0';
1828 printf( " %u bytes read (%u + %u)\n\n%s\n",
1829 ori_len + extra_len, ori_len, extra_len,
1830 (char *) larger_buf );
1831
1832 /* End of message should be detected according to the syntax of the
1833 * application protocol (eg HTTP), just use a dummy test here. */
1834 if( larger_buf[ori_len + extra_len - 1] == '\n' )
1835 terminated = 1;
1836
1837 polarssl_free( larger_buf );
1838 }
1839
1840 if( terminated )
1841 {
1842 ret = 0;
1843 break;
1844 }
1845 }
1846 while( 1 );
1847 }
1848 else /* Not stream, so datagram */
1849 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001850 len = sizeof( buf ) - 1;
1851 memset( buf, 0, sizeof( buf ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001852
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001853 do ret = ssl_read( &ssl, buf, len );
1854 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1855 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001856
1857 if( ret <= 0 )
1858 {
1859 switch( ret )
1860 {
1861 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1862 printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001863 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001864 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001865
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001866 case 0:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001867 case POLARSSL_ERR_NET_CONN_RESET:
1868 printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001869 ret = POLARSSL_ERR_NET_CONN_RESET;
1870 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001871
1872 default:
1873 printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001874 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001875 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001876 }
1877
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001878 len = ret;
1879 buf[len] = '\0';
1880 printf( " %d bytes read\n\n%s", len, (char *) buf );
1881 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001882 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001883
1884 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001885 * 7a. Request renegotiation while client is waiting for input from us.
1886 * (only if we're going to exhange more data afterwards)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001887 */
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001888 if( opt.renegotiate && exchanges > 1 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001889 {
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001890 printf( " . Requestion renegotiation..." );
1891 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001892
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001893 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
1894 {
1895 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1896 ret != POLARSSL_ERR_NET_WANT_WRITE )
1897 {
1898 printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
1899 goto reset;
1900 }
1901 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001902
1903 printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001904 }
1905
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001906 /*
1907 * 7. Write the 200 Response
1908 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001909 printf( " > Write to client:" );
1910 fflush( stdout );
1911
1912 len = sprintf( (char *) buf, HTTP_RESPONSE,
1913 ssl_get_ciphersuite( &ssl ) );
1914
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001915 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001916 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001917 while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001918 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001919 if( ret == POLARSSL_ERR_NET_CONN_RESET )
1920 {
1921 printf( " failed\n ! peer closed the connection\n\n" );
1922 goto reset;
1923 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001924
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001925 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1926 {
1927 printf( " failed\n ! ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001928 goto reset;
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001929 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001930 }
1931 }
1932
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001933 buf[written] = '\0';
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001934 printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001935
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001936
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001937 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001938 * 7b. Continue doing data exchanges?
1939 */
1940 if( --exchanges > 0 )
1941 goto data_exchange;
1942
1943 /*
1944 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001945 */
1946close_notify:
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001947 printf( " . Closing the connection..." );
1948
1949 while( ( ret = ssl_close_notify( &ssl ) ) < 0 )
1950 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001951 if( ret == POLARSSL_ERR_NET_CONN_RESET )
1952 {
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001953 printf( " ok (already closed by peer)\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001954 ret = 0;
1955 goto reset;
1956 }
1957
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001958 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1959 ret != POLARSSL_ERR_NET_WANT_WRITE )
1960 {
1961 printf( " failed\n ! ssl_close_notify returned %d\n\n", ret );
1962 goto reset;
1963 }
1964 }
1965
1966 printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001967 goto reset;
1968
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001969 /*
1970 * Cleanup and exit
1971 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001972exit:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001973#ifdef POLARSSL_ERROR_C
1974 if( ret != 0 )
1975 {
1976 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001977 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001978 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
1979 }
1980#endif
1981
Paul Bakker0c226102014-04-17 16:02:36 +02001982 if( client_fd != -1 )
1983 net_close( client_fd );
1984
Paul Bakkera317a982014-06-18 16:44:11 +02001985#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1986 dhm_free( &dhm );
1987#endif
Paul Bakker36713e82013-09-17 13:25:29 +02001988#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker36713e82013-09-17 13:25:29 +02001989 x509_crt_free( &cacert );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001990 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02001991 pk_free( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001992 x509_crt_free( &srvcert2 );
1993 pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02001994#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001995#if defined(POLARSSL_SNI)
1996 sni_free( sni_info );
1997#endif
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02001998#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1999 psk_free( psk_info );
2000#endif
2001#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
2002 dhm_free( &dhm );
2003#endif
Paul Bakkered27a042013-04-18 22:46:23 +02002004
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002005 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02002006 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02002007 entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002008
Paul Bakker0a597072012-09-25 21:55:46 +00002009#if defined(POLARSSL_SSL_CACHE_C)
2010 ssl_cache_free( &cache );
2011#endif
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +02002012#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002013 ssl_cookie_free( &cookie_ctx );
2014#endif
Paul Bakker0a597072012-09-25 21:55:46 +00002015
Paul Bakker1337aff2013-09-29 14:45:34 +02002016#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
2017#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker82024bf2013-07-04 11:52:32 +02002018 memory_buffer_alloc_status();
2019#endif
Paul Bakker1337aff2013-09-29 14:45:34 +02002020 memory_buffer_alloc_free();
2021#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02002022
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002023#if defined(_WIN32)
2024 printf( " + Press Enter to exit this program.\n" );
2025 fflush( stdout ); getchar();
2026#endif
2027
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002028 // Shell can not handle large exit numbers -> 1 for errors
2029 if( ret < 0 )
2030 ret = 1;
2031
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002032 return( ret );
2033}
2034#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
2035 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
2036 POLARSSL_CTR_DRBG_C */