blob: c76124bf40df49253f3bec6bb278848b4dfeec0a [file] [log] [blame]
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001/*
2 * SSL client with options
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerb60b95f2012-09-25 09:05:17 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
34#define polarssl_malloc malloc
35#define polarssl_free free
36#endif
37
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020038#if !defined(POLARSSL_ENTROPY_C) || \
39 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
40 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
41#include <stdio.h>
42int main( int argc, char *argv[] )
43{
44 ((void) argc);
45 ((void) argv);
46
Rich Evansf90016a2015-01-19 14:26:37 +000047 polarssl_printf("POLARSSL_ENTROPY_C and/or "
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020048 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
49 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
50 return( 0 );
51}
52#else
53
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010054#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) && defined(POLARSSL_FS_IO)
55#define POLARSSL_SNI
56#endif
57
Paul Bakkerb60b95f2012-09-25 09:05:17 +000058#if defined(_WIN32)
59#include <windows.h>
60#endif
61
62#include <string.h>
63#include <stdlib.h>
64#include <stdio.h>
Paul Bakkerc1283d32014-08-18 11:05:51 +020065
66#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +020067#include <signal.h>
Paul Bakkerc1283d32014-08-18 11:05:51 +020068#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000069
Paul Bakkerb60b95f2012-09-25 09:05:17 +000070#include "polarssl/net.h"
71#include "polarssl/ssl.h"
72#include "polarssl/entropy.h"
73#include "polarssl/ctr_drbg.h"
74#include "polarssl/certs.h"
75#include "polarssl/x509.h"
76#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020077#include "polarssl/debug.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000078
Paul Bakker0a597072012-09-25 21:55:46 +000079#if defined(POLARSSL_SSL_CACHE_C)
80#include "polarssl/ssl_cache.h"
81#endif
82
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +020083#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020084#include "polarssl/ssl_cookie.h"
85#endif
86
Paul Bakker82024bf2013-07-04 11:52:32 +020087#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardd43ccb62015-01-23 17:38:09 +000088#include "polarssl/memory_buffer_alloc.h"
Paul Bakker82024bf2013-07-04 11:52:32 +020089#endif
90
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +010091#define DFL_SERVER_ADDR NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000092#define DFL_SERVER_PORT 4433
Paul Bakkerb60b95f2012-09-25 09:05:17 +000093#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010094#define DFL_NBIO 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020095#define DFL_READ_TIMEOUT 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +000096#define DFL_CA_FILE ""
97#define DFL_CA_PATH ""
98#define DFL_CRT_FILE ""
99#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200100#define DFL_CRT_FILE2 ""
101#define DFL_KEY_FILE2 ""
Paul Bakkerfbb17802013-04-17 19:10:21 +0200102#define DFL_PSK ""
103#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200104#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000105#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200106#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +0200107#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100108#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100109#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200110#define DFL_RENEGO_DELAY -2
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100111#define DFL_RENEGO_PERIOD -1
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200112#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100113#define DFL_MIN_VERSION SSL_MINOR_VERSION_1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200114#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100115#define DFL_ARC4 SSL_ARC4_DISABLED
Paul Bakker91ebfb52012-11-23 14:04:08 +0100116#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200117#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100118#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200119#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100120#define DFL_TICKET_TIMEOUT -1
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100121#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100122#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100123#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200124#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200125#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100126#define DFL_TRANSPORT SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200127#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200128#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200129#define DFL_HS_TO_MIN 0
130#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200131#define DFL_BADMAC_LIMIT -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200132#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100133#define DFL_ETM -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000134
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200135#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
136 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
137 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
138 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
139 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
140 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
141 "07-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah</p>\r\n"
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200142
Paul Bakker8e714d72013-07-18 11:05:13 +0200143/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
144 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000145#define HTTP_RESPONSE \
146 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000147 "<h2>mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200148 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000149
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200150/*
151 * Size of the basic I/O buffer. Able to hold our default response.
152 *
153 * You will need to adapt the ssl_get_bytes_avail() test in ssl-opt.sh
154 * if you change this value to something outside the range <= 100 or > 500
155 */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200156#define IO_BUF_LEN 200
157
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000158/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000159 * global options
160 */
161struct options
162{
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100163 const char *server_addr; /* address on which the ssl service runs */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000164 int server_port; /* port on which the ssl service runs */
165 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100166 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200167 uint32_t read_timeout; /* timeout on ssl_read() in milliseconds */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200168 const char *ca_file; /* the file with the CA certificate(s) */
169 const char *ca_path; /* the path with the CA certificate(s) reside */
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200170 const char *crt_file; /* the file with the server certificate */
171 const char *key_file; /* the file with the server key */
172 const char *crt_file2; /* the file with the 2nd server certificate */
173 const char *key_file2; /* the file with the 2nd server key */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200174 const char *psk; /* the pre-shared key */
175 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200176 char *psk_list; /* list of PSK id/key pairs for callback */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000177 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200178 const char *version_suites; /* per-version ciphersuites */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000179 int renegotiation; /* enable / disable renegotiation */
180 int allow_legacy; /* allow legacy renegotiation */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100181 int renegotiate; /* attempt renegotiation? */
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200182 int renego_delay; /* delay before enforcing renegotiation */
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100183 int renego_period; /* period for automatic renegotiation */
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200184 int exchanges; /* number of data exchanges */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000185 int min_version; /* minimum protocol version accepted */
Paul Bakkerc1516be2013-06-29 16:01:32 +0200186 int max_version; /* maximum protocol version accepted */
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100187 int arc4; /* flag for arc4 suites support */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100188 int auth_mode; /* verify mode for connection */
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200189 unsigned char mfl_code; /* code for maximum fragment length */
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100190 int trunc_hmac; /* accept truncated hmac? */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200191 int tickets; /* enable / disable session tickets */
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100192 int ticket_timeout; /* session ticket lifetime */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100193 int cache_max; /* max number of session cache entries */
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100194 int cache_timeout; /* expiration delay of session cache entries */
Paul Bakker1ebc0c52014-05-22 15:47:58 +0200195 char *sni; /* string describing sni information */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200196 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200197 const char *dhm_file; /* the file with the DH parameters */
Paul Bakkerb2eaac12015-01-13 17:15:31 +0100198 int extended_ms; /* allow negotiation of extended MS? */
199 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100200 int transport; /* TLS or DTLS? */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200201 int cookies; /* Use cookies for DTLS? -1 to break them */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200202 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200203 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
204 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200205 int badmac_limit; /* Limit of records with bad MAC */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000206} opt;
207
Paul Bakker3c5ef712013-06-25 16:37:45 +0200208static void my_debug( void *ctx, int level, const char *str )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000209{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200210 ((void) level);
211
Rich Evansf90016a2015-01-19 14:26:37 +0000212 polarssl_fprintf( (FILE *) ctx, "%s", str );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200213 fflush( (FILE *) ctx );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000214}
215
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100216/*
217 * Test recv/send functions that make sure each try returns
218 * WANT_READ/WANT_WRITE at least once before sucesseding
219 */
220static int my_recv( void *ctx, unsigned char *buf, size_t len )
221{
222 static int first_try = 1;
223 int ret;
224
225 if( first_try )
226 {
227 first_try = 0;
228 return( POLARSSL_ERR_NET_WANT_READ );
229 }
230
231 ret = net_recv( ctx, buf, len );
232 if( ret != POLARSSL_ERR_NET_WANT_READ )
233 first_try = 1; /* Next call will be a new operation */
234 return( ret );
235}
236
237static int my_send( void *ctx, const unsigned char *buf, size_t len )
238{
239 static int first_try = 1;
240 int ret;
241
242 if( first_try )
243 {
244 first_try = 0;
245 return( POLARSSL_ERR_NET_WANT_WRITE );
246 }
247
248 ret = net_send( ctx, buf, len );
249 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
250 first_try = 1; /* Next call will be a new operation */
251 return( ret );
252}
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200253
254#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000255#if defined(POLARSSL_FS_IO)
256#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100257 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
258 " default: \"\" (pre-loaded)\n" \
259 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
260 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
261 " 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 +0200262 " default: see note after key_file2\n" \
263 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200264 " 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 +0200265 " default: see note after key_file2\n" \
266 " key_file2=%%s default: see note below\n" \
267 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200268 " preloaded certificate(s) and key(s) are used if available\n" \
269 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
270 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000271#else
272#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200273 "\n" \
274 " No file operations available (POLARSSL_FS_IO not defined)\n" \
275 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000276#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200277#else
278#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200279#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200280
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200281#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200282#define USAGE_PSK \
283 " psk=%%s default: \"\" (in hex, without 0x)\n" \
284 " psk_identity=%%s default: \"Client_identity\"\n"
285#else
286#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200287#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000288
Paul Bakkera503a632013-08-14 13:48:06 +0200289#if defined(POLARSSL_SSL_SESSION_TICKETS)
290#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100291 " tickets=%%d default: 1 (enabled)\n" \
292 " ticket_timeout=%%d default: ticket default (1d)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200293#else
294#define USAGE_TICKETS ""
295#endif /* POLARSSL_SSL_SESSION_TICKETS */
296
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100297#if defined(POLARSSL_SSL_CACHE_C)
298#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100299 " cache_max=%%d default: cache default (50)\n" \
300 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100301#else
302#define USAGE_CACHE ""
303#endif /* POLARSSL_SSL_CACHE_C */
304
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100305#if defined(POLARSSL_SNI)
306#define USAGE_SNI \
307 " sni=%%s name1,cert1,key1[,name2,cert2,key2[,...]]\n" \
308 " default: disabled\n"
309#else
310#define USAGE_SNI ""
311#endif /* POLARSSL_SNI */
312
Paul Bakker05decb22013-08-15 13:33:48 +0200313#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
314#define USAGE_MAX_FRAG_LEN \
315 " max_frag_len=%%d default: 16384 (tls default)\n" \
316 " options: 512, 1024, 2048, 4096\n"
317#else
318#define USAGE_MAX_FRAG_LEN ""
319#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
320
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100321#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
322#define USAGE_TRUNC_HMAC \
323 " trunc_hmac=%%d default: library default\n"
324#else
325#define USAGE_TRUNC_HMAC ""
326#endif
327
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200328#if defined(POLARSSL_SSL_ALPN)
329#define USAGE_ALPN \
330 " alpn=%%s default: \"\" (disabled)\n" \
331 " example: spdy/1,http/1.1\n"
332#else
333#define USAGE_ALPN ""
334#endif /* POLARSSL_SSL_ALPN */
335
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200336#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
337#define USAGE_COOKIES \
338 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200339 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200340#else
341#define USAGE_COOKIES ""
342#endif
343
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200344#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
345#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200346 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200347#else
348#define USAGE_ANTI_REPLAY ""
349#endif
350
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200351#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
352#define USAGE_BADMAC_LIMIT \
353 " badmac_limit=%%d default: (library default: disabled)\n"
354#else
355#define USAGE_BADMAC_LIMIT ""
356#endif
357
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200358#if defined(POLARSSL_SSL_PROTO_DTLS)
359#define USAGE_DTLS \
360 " dtls=%%d default: 0 (TLS)\n" \
361 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
362 " range of DTLS handshake timeouts in millisecs\n"
363#else
364#define USAGE_DTLS ""
365#endif
366
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200367#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
368#define USAGE_EMS \
369 " extended_ms=0/1 default: (library default: on)\n"
370#else
371#define USAGE_EMS ""
372#endif
373
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100374#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
375#define USAGE_ETM \
376 " etm=0/1 default: (library default: on)\n"
377#else
378#define USAGE_ETM ""
379#endif
380
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100381#if defined(POLARSSL_SSL_RENEGOTIATION)
382#define USAGE_RENEGO \
383 " renegotiation=%%d default: 0 (disabled)\n" \
384 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100385 " renego_delay=%%d default: -2 (library default)\n" \
386 " renego_period=%%d default: (library default)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100387#else
388#define USAGE_RENEGO ""
389#endif
390
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000391#define USAGE \
392 "\n usage: ssl_server2 param=<>...\n" \
393 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100394 " server_addr=%%d default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000395 " server_port=%%d default: 4433\n" \
396 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100397 " nbio=%%d default: 0 (blocking I/O)\n" \
398 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200399 " read_timeout=%%d default: 0 (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100400 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200401 USAGE_DTLS \
402 USAGE_COOKIES \
403 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200404 USAGE_BADMAC_LIMIT \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200405 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100406 " auth_mode=%%s default: \"optional\"\n" \
407 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000408 USAGE_IO \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100409 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100410 "\n" \
411 USAGE_PSK \
412 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100413 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100414 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200415 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200416 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100417 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100418 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100419 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100420 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200421 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200422 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100423 USAGE_ETM \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100424 "\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000425 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200426 " max_version=%%s default: \"tls1_2\"\n" \
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100427 " arc4=%%d default: 0 (disabled)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200428 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100429 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200430 "\n" \
431 " version_suites=a,b,c,d per-version ciphersuites\n" \
432 " in order from ssl3 to tls1_2\n" \
433 " default: all enabled\n" \
434 " force_ciphersuite=<name> default: all enabled\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000435 " acceptable ciphersuite names:\n"
436
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200437/*
438 * Used by sni_parse and psk_parse to handle coma-separated lists
439 */
440#define GET_ITEM( dst ) \
441 dst = p; \
442 while( *p != ',' ) \
443 if( ++p > end ) \
444 return( NULL ); \
445 *p++ = '\0';
446
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100447#if defined(POLARSSL_SNI)
448typedef struct _sni_entry sni_entry;
449
450struct _sni_entry {
451 const char *name;
452 x509_crt *cert;
453 pk_context *key;
454 sni_entry *next;
455};
456
457/*
458 * Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]]
459 * into a usable sni_entry list.
460 *
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200461 * Modifies the input string! This is not production quality!
462 * (leaks memory if parsing fails, no error reporting, ...)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100463 */
464sni_entry *sni_parse( char *sni_string )
465{
466 sni_entry *cur = NULL, *new = NULL;
467 char *p = sni_string;
468 char *end = p;
469 char *crt_file, *key_file;
470
471 while( *end != '\0' )
472 ++end;
473 *end = ',';
474
475 while( p <= end )
476 {
477 if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL )
478 return( NULL );
479
480 memset( new, 0, sizeof( sni_entry ) );
481
482 if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ||
483 ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200484 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100485
486 x509_crt_init( new->cert );
487 pk_init( new->key );
488
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200489 GET_ITEM( new->name );
490 GET_ITEM( crt_file );
491 GET_ITEM( key_file );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100492
493 if( x509_crt_parse_file( new->cert, crt_file ) != 0 ||
494 pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200495 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100496
497 new->next = cur;
498 cur = new;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100499 }
500
501 return( cur );
502}
503
504void sni_free( sni_entry *head )
505{
506 sni_entry *cur = head, *next;
507
508 while( cur != NULL )
509 {
510 x509_crt_free( cur->cert );
511 polarssl_free( cur->cert );
512
513 pk_free( cur->key );
514 polarssl_free( cur->key );
515
516 next = cur->next;
517 polarssl_free( cur );
518 cur = next;
519 }
520}
521
522/*
523 * SNI callback.
524 */
525int sni_callback( void *p_info, ssl_context *ssl,
526 const unsigned char *name, size_t name_len )
527{
528 sni_entry *cur = (sni_entry *) p_info;
529
530 while( cur != NULL )
531 {
532 if( name_len == strlen( cur->name ) &&
533 memcmp( name, cur->name, name_len ) == 0 )
534 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200535 return( ssl_set_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100536 }
537
538 cur = cur->next;
539 }
540
541 return( -1 );
542}
543
544#endif /* POLARSSL_SNI */
545
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200546#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
547
548#define HEX2NUM( c ) \
549 if( c >= '0' && c <= '9' ) \
550 c -= '0'; \
551 else if( c >= 'a' && c <= 'f' ) \
552 c -= 'a' - 10; \
553 else if( c >= 'A' && c <= 'F' ) \
554 c -= 'A' - 10; \
555 else \
556 return( -1 );
557
558/*
559 * Convert a hex string to bytes.
560 * Return 0 on success, -1 on error.
561 */
562int unhexify( unsigned char *output, const char *input, size_t *olen )
563{
564 unsigned char c;
565 size_t j;
566
567 *olen = strlen( input );
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200568 if( *olen % 2 != 0 || *olen / 2 > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200569 return( -1 );
570 *olen /= 2;
571
572 for( j = 0; j < *olen * 2; j += 2 )
573 {
574 c = input[j];
575 HEX2NUM( c );
576 output[ j / 2 ] = c << 4;
577
578 c = input[j + 1];
579 HEX2NUM( c );
580 output[ j / 2 ] |= c;
581 }
582
583 return( 0 );
584}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200585
586typedef struct _psk_entry psk_entry;
587
588struct _psk_entry
589{
590 const char *name;
591 size_t key_len;
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200592 unsigned char key[POLARSSL_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200593 psk_entry *next;
594};
595
596/*
597 * Parse a string of pairs name1,key1[,name2,key2[,...]]
598 * into a usable psk_entry list.
599 *
600 * Modifies the input string! This is not production quality!
601 * (leaks memory if parsing fails, no error reporting, ...)
602 */
603psk_entry *psk_parse( char *psk_string )
604{
605 psk_entry *cur = NULL, *new = NULL;
606 char *p = psk_string;
607 char *end = p;
608 char *key_hex;
609
610 while( *end != '\0' )
611 ++end;
612 *end = ',';
613
614 while( p <= end )
615 {
616 if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL )
617 return( NULL );
618
619 memset( new, 0, sizeof( psk_entry ) );
620
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200621 GET_ITEM( new->name );
622 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200623
624 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
625 return( NULL );
626
627 new->next = cur;
628 cur = new;
629 }
630
631 return( cur );
632}
633
634/*
635 * Free a list of psk_entry's
636 */
637void psk_free( psk_entry *head )
638{
639 psk_entry *next;
640
641 while( head != NULL )
642 {
643 next = head->next;
644 polarssl_free( head );
645 head = next;
646 }
647}
648
649/*
650 * PSK callback
651 */
652int psk_callback( void *p_info, ssl_context *ssl,
653 const unsigned char *name, size_t name_len )
654{
655 psk_entry *cur = (psk_entry *) p_info;
656
657 while( cur != NULL )
658 {
659 if( name_len == strlen( cur->name ) &&
660 memcmp( name, cur->name, name_len ) == 0 )
661 {
662 return( ssl_set_psk( ssl, cur->key, cur->key_len,
663 name, name_len ) );
664 }
665
666 cur = cur->next;
667 }
668
669 return( -1 );
670}
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200671#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
672
Manuel Pégourié-Gonnarda9d7d032014-10-09 16:07:08 +0200673static int listen_fd, client_fd = -1;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200674
675/* Interruption handler to ensure clean exit (for valgrind testing) */
676#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200677static int received_sigterm = 0;
678void term_handler( int sig )
679{
680 ((void) sig);
681 received_sigterm = 1;
682 net_close( listen_fd ); /* causes net_accept() to abort */
Manuel Pégourié-Gonnarda9d7d032014-10-09 16:07:08 +0200683 net_close( client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200684}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200685#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200686
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000687int main( int argc, char *argv[] )
688{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +0000689 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200690 int version_suites[4][2];
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200691 unsigned char buf[IO_BUF_LEN];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200692#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200693 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +0200694 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +0200695 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +0200696#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200697 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +0200698 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +0200699#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200700 ssl_cookie_ctx cookie_ctx;
701#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000702
703 entropy_context entropy;
704 ctr_drbg_context ctr_drbg;
705 ssl_context ssl;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100706#if defined(POLARSSL_SSL_RENEGOTIATION)
707 unsigned char renego_period[8] = { 0 };
708#endif
Paul Bakker36713e82013-09-17 13:25:29 +0200709#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200710 x509_crt cacert;
711 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200712 pk_context pkey;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200713 x509_crt srvcert2;
714 pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200715 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200716#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200717#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
718 dhm_context dhm;
719#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000720#if defined(POLARSSL_SSL_CACHE_C)
721 ssl_cache_context cache;
722#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100723#if defined(POLARSSL_SNI)
724 sni_entry *sni_info = NULL;
725#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200726#if defined(POLARSSL_SSL_ALPN)
727 const char *alpn_list[10];
728#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200729#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
730 unsigned char alloc_buf[100000];
731#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000732
733 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000734 char *p, *q;
735 const int *list;
736
Paul Bakker82024bf2013-07-04 11:52:32 +0200737#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
738 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
739#endif
740
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000741 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200742 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000743 */
744 listen_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200745 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200746#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200747 x509_crt_init( &cacert );
748 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200749 pk_init( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200750 x509_crt_init( &srvcert2 );
751 pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200752#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200753#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
Paul Bakkera317a982014-06-18 16:44:11 +0200754 dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200755#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000756#if defined(POLARSSL_SSL_CACHE_C)
757 ssl_cache_init( &cache );
758#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200759#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200760 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200761#endif
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +0200762#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200763 ssl_cookie_init( &cookie_ctx );
764#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000765
Paul Bakkerc1283d32014-08-18 11:05:51 +0200766#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100767 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200768 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100769 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +0200770#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200771
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000772 if( argc == 0 )
773 {
774 usage:
775 if( ret == 0 )
776 ret = 1;
777
Rich Evansf90016a2015-01-19 14:26:37 +0000778 polarssl_printf( USAGE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000779
780 list = ssl_list_ciphersuites();
781 while( *list )
782 {
Rich Evansf90016a2015-01-19 14:26:37 +0000783 polarssl_printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200784 list++;
785 if( !*list )
786 break;
Rich Evansf90016a2015-01-19 14:26:37 +0000787 polarssl_printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000788 list++;
789 }
Rich Evansf90016a2015-01-19 14:26:37 +0000790 polarssl_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000791 goto exit;
792 }
793
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100794 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000795 opt.server_port = DFL_SERVER_PORT;
796 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100797 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200798 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000799 opt.ca_file = DFL_CA_FILE;
800 opt.ca_path = DFL_CA_PATH;
801 opt.crt_file = DFL_CRT_FILE;
802 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200803 opt.crt_file2 = DFL_CRT_FILE2;
804 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200805 opt.psk = DFL_PSK;
806 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200807 opt.psk_list = DFL_PSK_LIST;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000808 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200809 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000810 opt.renegotiation = DFL_RENEGOTIATION;
811 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100812 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200813 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100814 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200815 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000816 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200817 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100818 opt.arc4 = DFL_ARC4;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100819 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200820 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100821 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200822 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100823 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100824 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100825 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100826 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200827 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200828 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100829 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200830 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200831 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200832 opt.hs_to_min = DFL_HS_TO_MIN;
833 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200834 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200835 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100836 opt.etm = DFL_ETM;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000837
838 for( i = 1; i < argc; i++ )
839 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000840 p = argv[i];
841 if( ( q = strchr( p, '=' ) ) == NULL )
842 goto usage;
843 *q++ = '\0';
844
845 if( strcmp( p, "server_port" ) == 0 )
846 {
847 opt.server_port = atoi( q );
848 if( opt.server_port < 1 || opt.server_port > 65535 )
849 goto usage;
850 }
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100851 else if( strcmp( p, "server_addr" ) == 0 )
852 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100853 else if( strcmp( p, "dtls" ) == 0 )
854 {
855 int t = atoi( q );
856 if( t == 0 )
857 opt.transport = SSL_TRANSPORT_STREAM;
858 else if( t == 1 )
859 opt.transport = SSL_TRANSPORT_DATAGRAM;
860 else
861 goto usage;
862 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000863 else if( strcmp( p, "debug_level" ) == 0 )
864 {
865 opt.debug_level = atoi( q );
866 if( opt.debug_level < 0 || opt.debug_level > 65535 )
867 goto usage;
868 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100869 else if( strcmp( p, "nbio" ) == 0 )
870 {
871 opt.nbio = atoi( q );
872 if( opt.nbio < 0 || opt.nbio > 2 )
873 goto usage;
874 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200875 else if( strcmp( p, "read_timeout" ) == 0 )
876 opt.read_timeout = atoi( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000877 else if( strcmp( p, "ca_file" ) == 0 )
878 opt.ca_file = q;
879 else if( strcmp( p, "ca_path" ) == 0 )
880 opt.ca_path = q;
881 else if( strcmp( p, "crt_file" ) == 0 )
882 opt.crt_file = q;
883 else if( strcmp( p, "key_file" ) == 0 )
884 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200885 else if( strcmp( p, "crt_file2" ) == 0 )
886 opt.crt_file2 = q;
887 else if( strcmp( p, "key_file2" ) == 0 )
888 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200889 else if( strcmp( p, "dhm_file" ) == 0 )
890 opt.dhm_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200891 else if( strcmp( p, "psk" ) == 0 )
892 opt.psk = q;
893 else if( strcmp( p, "psk_identity" ) == 0 )
894 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200895 else if( strcmp( p, "psk_list" ) == 0 )
896 opt.psk_list = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000897 else if( strcmp( p, "force_ciphersuite" ) == 0 )
898 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000899 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
900
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200901 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000902 {
903 ret = 2;
904 goto usage;
905 }
906 opt.force_ciphersuite[1] = 0;
907 }
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200908 else if( strcmp( p, "version_suites" ) == 0 )
909 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000910 else if( strcmp( p, "renegotiation" ) == 0 )
911 {
912 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
913 SSL_RENEGOTIATION_DISABLED;
914 }
915 else if( strcmp( p, "allow_legacy" ) == 0 )
916 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100917 switch( atoi( q ) )
918 {
919 case -1: opt.allow_legacy = SSL_LEGACY_BREAK_HANDSHAKE; break;
920 case 0: opt.allow_legacy = SSL_LEGACY_NO_RENEGOTIATION; break;
921 case 1: opt.allow_legacy = SSL_LEGACY_ALLOW_RENEGOTIATION; break;
922 default: goto usage;
923 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000924 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100925 else if( strcmp( p, "renegotiate" ) == 0 )
926 {
927 opt.renegotiate = atoi( q );
928 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
929 goto usage;
930 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200931 else if( strcmp( p, "renego_delay" ) == 0 )
932 {
933 opt.renego_delay = atoi( q );
934 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100935 else if( strcmp( p, "renego_period" ) == 0 )
936 {
937 opt.renego_period = atoi( q );
938 if( opt.renego_period < 2 || opt.renego_period > 255 )
939 goto usage;
940 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200941 else if( strcmp( p, "exchanges" ) == 0 )
942 {
943 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +0200944 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200945 goto usage;
946 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000947 else if( strcmp( p, "min_version" ) == 0 )
948 {
949 if( strcmp( q, "ssl3" ) == 0 )
950 opt.min_version = SSL_MINOR_VERSION_0;
951 else if( strcmp( q, "tls1" ) == 0 )
952 opt.min_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100953 else if( strcmp( q, "tls1_1" ) == 0 ||
954 strcmp( q, "dtls1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000955 opt.min_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100956 else if( strcmp( q, "tls1_2" ) == 0 ||
957 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000958 opt.min_version = SSL_MINOR_VERSION_3;
959 else
960 goto usage;
961 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200962 else if( strcmp( p, "max_version" ) == 0 )
963 {
964 if( strcmp( q, "ssl3" ) == 0 )
965 opt.max_version = SSL_MINOR_VERSION_0;
966 else if( strcmp( q, "tls1" ) == 0 )
967 opt.max_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100968 else if( strcmp( q, "tls1_1" ) == 0 ||
969 strcmp( q, "dtls1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200970 opt.max_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100971 else if( strcmp( q, "tls1_2" ) == 0 ||
972 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200973 opt.max_version = SSL_MINOR_VERSION_3;
974 else
975 goto usage;
976 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100977 else if( strcmp( p, "arc4" ) == 0 )
978 {
979 switch( atoi( q ) )
980 {
981 case 0: opt.arc4 = SSL_ARC4_DISABLED; break;
982 case 1: opt.arc4 = SSL_ARC4_ENABLED; break;
983 default: goto usage;
984 }
985 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200986 else if( strcmp( p, "force_version" ) == 0 )
987 {
988 if( strcmp( q, "ssl3" ) == 0 )
989 {
990 opt.min_version = SSL_MINOR_VERSION_0;
991 opt.max_version = SSL_MINOR_VERSION_0;
992 }
993 else if( strcmp( q, "tls1" ) == 0 )
994 {
995 opt.min_version = SSL_MINOR_VERSION_1;
996 opt.max_version = SSL_MINOR_VERSION_1;
997 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100998 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200999 {
1000 opt.min_version = SSL_MINOR_VERSION_2;
1001 opt.max_version = SSL_MINOR_VERSION_2;
1002 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001003 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001004 {
1005 opt.min_version = SSL_MINOR_VERSION_3;
1006 opt.max_version = SSL_MINOR_VERSION_3;
1007 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001008 else if( strcmp( q, "dtls1" ) == 0 )
1009 {
1010 opt.min_version = SSL_MINOR_VERSION_2;
1011 opt.max_version = SSL_MINOR_VERSION_2;
1012 opt.transport = SSL_TRANSPORT_DATAGRAM;
1013 }
1014 else if( strcmp( q, "dtls1_2" ) == 0 )
1015 {
1016 opt.min_version = SSL_MINOR_VERSION_3;
1017 opt.max_version = SSL_MINOR_VERSION_3;
1018 opt.transport = SSL_TRANSPORT_DATAGRAM;
1019 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001020 else
1021 goto usage;
1022 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001023 else if( strcmp( p, "auth_mode" ) == 0 )
1024 {
1025 if( strcmp( q, "none" ) == 0 )
1026 opt.auth_mode = SSL_VERIFY_NONE;
1027 else if( strcmp( q, "optional" ) == 0 )
1028 opt.auth_mode = SSL_VERIFY_OPTIONAL;
1029 else if( strcmp( q, "required" ) == 0 )
1030 opt.auth_mode = SSL_VERIFY_REQUIRED;
1031 else
1032 goto usage;
1033 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001034 else if( strcmp( p, "max_frag_len" ) == 0 )
1035 {
1036 if( strcmp( q, "512" ) == 0 )
1037 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
1038 else if( strcmp( q, "1024" ) == 0 )
1039 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
1040 else if( strcmp( q, "2048" ) == 0 )
1041 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
1042 else if( strcmp( q, "4096" ) == 0 )
1043 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
1044 else
1045 goto usage;
1046 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001047 else if( strcmp( p, "alpn" ) == 0 )
1048 {
1049 opt.alpn_string = q;
1050 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001051 else if( strcmp( p, "trunc_hmac" ) == 0 )
1052 {
1053 switch( atoi( q ) )
1054 {
1055 case 0: opt.trunc_hmac = SSL_TRUNC_HMAC_DISABLED; break;
1056 case 1: opt.trunc_hmac = SSL_TRUNC_HMAC_ENABLED; break;
1057 default: goto usage;
1058 }
1059 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001060 else if( strcmp( p, "extended_ms" ) == 0 )
1061 {
1062 switch( atoi( q ) )
1063 {
1064 case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break;
1065 case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break;
1066 default: goto usage;
1067 }
1068 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001069 else if( strcmp( p, "etm" ) == 0 )
1070 {
1071 switch( atoi( q ) )
1072 {
1073 case 0: opt.etm = SSL_ETM_DISABLED; break;
1074 case 1: opt.etm = SSL_ETM_ENABLED; break;
1075 default: goto usage;
1076 }
1077 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001078 else if( strcmp( p, "tickets" ) == 0 )
1079 {
1080 opt.tickets = atoi( q );
1081 if( opt.tickets < 0 || opt.tickets > 1 )
1082 goto usage;
1083 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001084 else if( strcmp( p, "ticket_timeout" ) == 0 )
1085 {
1086 opt.ticket_timeout = atoi( q );
1087 if( opt.ticket_timeout < 0 )
1088 goto usage;
1089 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001090 else if( strcmp( p, "cache_max" ) == 0 )
1091 {
1092 opt.cache_max = atoi( q );
1093 if( opt.cache_max < 0 )
1094 goto usage;
1095 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001096 else if( strcmp( p, "cache_timeout" ) == 0 )
1097 {
1098 opt.cache_timeout = atoi( q );
1099 if( opt.cache_timeout < 0 )
1100 goto usage;
1101 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001102 else if( strcmp( p, "cookies" ) == 0 )
1103 {
1104 opt.cookies = atoi( q );
1105 if( opt.cookies < -1 || opt.cookies > 1)
1106 goto usage;
1107 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001108 else if( strcmp( p, "anti_replay" ) == 0 )
1109 {
1110 opt.anti_replay = atoi( q );
1111 if( opt.anti_replay < 0 || opt.anti_replay > 1)
1112 goto usage;
1113 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001114 else if( strcmp( p, "badmac_limit" ) == 0 )
1115 {
1116 opt.badmac_limit = atoi( q );
1117 if( opt.badmac_limit < 0 )
1118 goto usage;
1119 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001120 else if( strcmp( p, "hs_timeout" ) == 0 )
1121 {
1122 if( ( p = strchr( q, '-' ) ) == NULL )
1123 goto usage;
1124 *p++ = '\0';
1125 opt.hs_to_min = atoi( q );
1126 opt.hs_to_max = atoi( p );
1127 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1128 goto usage;
1129 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001130 else if( strcmp( p, "sni" ) == 0 )
1131 {
1132 opt.sni = q;
1133 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001134 else
1135 goto usage;
1136 }
1137
Paul Bakkerc73079a2014-04-25 16:34:30 +02001138#if defined(POLARSSL_DEBUG_C)
1139 debug_set_threshold( opt.debug_level );
1140#endif
1141
Paul Bakkerc1516be2013-06-29 16:01:32 +02001142 if( opt.force_ciphersuite[0] > 0 )
1143 {
1144 const ssl_ciphersuite_t *ciphersuite_info;
1145 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1146
Paul Bakker5b55b792013-07-19 13:43:43 +02001147 if( opt.max_version != -1 &&
1148 ciphersuite_info->min_minor_ver > opt.max_version )
1149 {
Rich Evansf90016a2015-01-19 14:26:37 +00001150 polarssl_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakker5b55b792013-07-19 13:43:43 +02001151 ret = 2;
1152 goto usage;
1153 }
1154 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001155 ciphersuite_info->max_minor_ver < opt.min_version )
1156 {
Rich Evansf90016a2015-01-19 14:26:37 +00001157 polarssl_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +02001158 ret = 2;
1159 goto usage;
1160 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001161
1162 /* If we select a version that's not supported by
1163 * this suite, then there will be no common ciphersuite... */
1164 if( opt.max_version == -1 ||
1165 opt.max_version > ciphersuite_info->max_minor_ver )
1166 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001167 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001168 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001169 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001170 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001171 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001172 /* DTLS starts with TLS 1.1 */
1173 if( opt.transport == SSL_TRANSPORT_DATAGRAM &&
1174 opt.min_version < SSL_MINOR_VERSION_2 )
1175 opt.min_version = SSL_MINOR_VERSION_2;
1176 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001177 }
1178
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001179 if( opt.version_suites != NULL )
1180 {
1181 const char *name[4] = { 0 };
1182
1183 /* Parse 4-element coma-separated list */
1184 for( i = 0, p = (char *) opt.version_suites;
1185 i < 4 && *p != '\0';
1186 i++ )
1187 {
1188 name[i] = p;
1189
1190 /* Terminate the current string and move on to next one */
1191 while( *p != ',' && *p != '\0' )
1192 p++;
1193 if( *p == ',' )
1194 *p++ = '\0';
1195 }
1196
1197 if( i != 4 )
1198 {
Rich Evansf90016a2015-01-19 14:26:37 +00001199 polarssl_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001200 ret = 1;
1201 goto exit;
1202 }
1203
1204 memset( version_suites, 0, sizeof( version_suites ) );
1205
1206 /* Get the suites identifiers from their name */
1207 for( i = 0; i < 4; i++ )
1208 {
1209 version_suites[i][0] = ssl_get_ciphersuite_id( name[i] );
1210
1211 if( version_suites[i][0] == 0 )
1212 {
Rich Evansf90016a2015-01-19 14:26:37 +00001213 polarssl_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001214 ret = 2;
1215 goto usage;
1216 }
1217 }
1218 }
1219
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001220#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001221 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001222 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001223 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001224 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001225 {
Rich Evansf90016a2015-01-19 14:26:37 +00001226 polarssl_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001227 goto exit;
1228 }
1229
1230 if( opt.psk_list != NULL )
1231 {
1232 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001233 {
Rich Evansf90016a2015-01-19 14:26:37 +00001234 polarssl_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001235 goto exit;
1236 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001237 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001238#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001239
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001240#if defined(POLARSSL_SSL_ALPN)
1241 if( opt.alpn_string != NULL )
1242 {
1243 p = (char *) opt.alpn_string;
1244 i = 0;
1245
1246 /* Leave room for a final NULL in alpn_list */
1247 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
1248 {
1249 alpn_list[i++] = p;
1250
1251 /* Terminate the current string and move on to next one */
1252 while( *p != ',' && *p != '\0' )
1253 p++;
1254 if( *p == ',' )
1255 *p++ = '\0';
1256 }
1257 }
1258#endif /* POLARSSL_SSL_ALPN */
1259
Paul Bakkerfbb17802013-04-17 19:10:21 +02001260 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001261 * 0. Initialize the RNG and the session data
1262 */
Rich Evansf90016a2015-01-19 14:26:37 +00001263 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001264 fflush( stdout );
1265
1266 entropy_init( &entropy );
1267 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001268 (const unsigned char *) pers,
1269 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001270 {
Rich Evansf90016a2015-01-19 14:26:37 +00001271 polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001272 goto exit;
1273 }
1274
Rich Evansf90016a2015-01-19 14:26:37 +00001275 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001276
Paul Bakker36713e82013-09-17 13:25:29 +02001277#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001278 /*
1279 * 1.1. Load the trusted CA
1280 */
Rich Evansf90016a2015-01-19 14:26:37 +00001281 polarssl_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001282 fflush( stdout );
1283
1284#if defined(POLARSSL_FS_IO)
1285 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001286 if( strcmp( opt.ca_path, "none" ) == 0 )
1287 ret = 0;
1288 else
1289 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001290 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001291 if( strcmp( opt.ca_file, "none" ) == 0 )
1292 ret = 0;
1293 else
1294 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001295 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001296#endif
1297#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +02001298 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
1299 strlen( test_ca_list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001300#else
1301 {
1302 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +00001303 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001304 }
1305#endif
1306 if( ret < 0 )
1307 {
Rich Evansf90016a2015-01-19 14:26:37 +00001308 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001309 goto exit;
1310 }
1311
Rich Evansf90016a2015-01-19 14:26:37 +00001312 polarssl_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001313
1314 /*
1315 * 1.2. Load own certificate and private key
1316 */
Rich Evansf90016a2015-01-19 14:26:37 +00001317 polarssl_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001318 fflush( stdout );
1319
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001320#if defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001321 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001322 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001323 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001324 if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
1325 {
Rich Evansf90016a2015-01-19 14:26:37 +00001326 polarssl_printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001327 -ret );
1328 goto exit;
1329 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001330 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001331 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001332 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001333 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001334 if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
1335 {
Rich Evansf90016a2015-01-19 14:26:37 +00001336 polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001337 goto exit;
1338 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001339 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001340 if( key_cert_init == 1 )
1341 {
Rich Evansf90016a2015-01-19 14:26:37 +00001342 polarssl_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001343 goto exit;
1344 }
1345
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001346 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001347 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001348 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001349 if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
1350 {
Rich Evansf90016a2015-01-19 14:26:37 +00001351 polarssl_printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001352 -ret );
1353 goto exit;
1354 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001355 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001356 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001357 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001358 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001359 if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
1360 {
Rich Evansf90016a2015-01-19 14:26:37 +00001361 polarssl_printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001362 -ret );
1363 goto exit;
1364 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001365 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001366 if( key_cert_init2 == 1 )
1367 {
Rich Evansf90016a2015-01-19 14:26:37 +00001368 polarssl_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001369 goto exit;
1370 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001371#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001372 if( key_cert_init == 0 &&
1373 strcmp( opt.crt_file, "none" ) != 0 &&
1374 strcmp( opt.key_file, "none" ) != 0 &&
1375 key_cert_init2 == 0 &&
1376 strcmp( opt.crt_file2, "none" ) != 0 &&
1377 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001378 {
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001379#if !defined(POLARSSL_CERTS_C)
Rich Evansf90016a2015-01-19 14:26:37 +00001380 polarssl_printf( "Not certificated or key provided, and \n"
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001381 "POLARSSL_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001382 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001383#else
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001384#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001385 if( ( ret = x509_crt_parse( &srvcert,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001386 (const unsigned char *) test_srv_crt_rsa,
1387 strlen( test_srv_crt_rsa ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001388 {
Rich Evansf90016a2015-01-19 14:26:37 +00001389 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001390 goto exit;
1391 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001392 if( ( ret = pk_parse_key( &pkey,
1393 (const unsigned char *) test_srv_key_rsa,
1394 strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001395 {
Rich Evansf90016a2015-01-19 14:26:37 +00001396 polarssl_printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001397 goto exit;
1398 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001399 key_cert_init = 2;
1400#endif /* POLARSSL_RSA_C */
1401#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001402 if( ( ret = x509_crt_parse( &srvcert2,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001403 (const unsigned char *) test_srv_crt_ec,
1404 strlen( test_srv_crt_ec ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001405 {
Rich Evansf90016a2015-01-19 14:26:37 +00001406 polarssl_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001407 goto exit;
1408 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001409 if( ( ret = pk_parse_key( &pkey2,
1410 (const unsigned char *) test_srv_key_ec,
1411 strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001412 {
Rich Evansf90016a2015-01-19 14:26:37 +00001413 polarssl_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001414 goto exit;
1415 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001416 key_cert_init2 = 2;
1417#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001418#endif /* POLARSSL_CERTS_C */
1419 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001420
Rich Evansf90016a2015-01-19 14:26:37 +00001421 polarssl_printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001422#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001423
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001424#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1425 if( opt.dhm_file != NULL )
1426 {
Rich Evansf90016a2015-01-19 14:26:37 +00001427 polarssl_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001428 fflush( stdout );
1429
1430 if( ( ret = dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
1431 {
Rich Evansf90016a2015-01-19 14:26:37 +00001432 polarssl_printf( " failed\n ! dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001433 -ret );
1434 goto exit;
1435 }
1436
Rich Evansf90016a2015-01-19 14:26:37 +00001437 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001438 }
1439#endif
1440
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001441#if defined(POLARSSL_SNI)
1442 if( opt.sni != NULL )
1443 {
Rich Evansf90016a2015-01-19 14:26:37 +00001444 polarssl_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001445 fflush( stdout );
1446
1447 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
1448 {
Rich Evansf90016a2015-01-19 14:26:37 +00001449 polarssl_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001450 goto exit;
1451 }
1452
Rich Evansf90016a2015-01-19 14:26:37 +00001453 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001454 }
1455#endif /* POLARSSL_SNI */
1456
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001457 /*
1458 * 2. Setup the listening TCP socket
1459 */
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00001460 polarssl_printf( " . Bind on %s://%s:%-4d/ ...",
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001461 opt.transport == SSL_TRANSPORT_STREAM ? "tcp" : "udp",
1462 opt.server_addr ? opt.server_addr : "*",
1463 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001464 fflush( stdout );
1465
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001466 if( ( ret = net_bind( &listen_fd, opt.server_addr, opt.server_port,
1467 opt.transport == SSL_TRANSPORT_STREAM ?
1468 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001469 {
Rich Evansf90016a2015-01-19 14:26:37 +00001470 polarssl_printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001471 goto exit;
1472 }
1473
Rich Evansf90016a2015-01-19 14:26:37 +00001474 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001475
1476 /*
1477 * 3. Setup stuff
1478 */
Rich Evansf90016a2015-01-19 14:26:37 +00001479 polarssl_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001480 fflush( stdout );
1481
1482 if( ( ret = ssl_init( &ssl ) ) != 0 )
1483 {
Rich Evansf90016a2015-01-19 14:26:37 +00001484 polarssl_printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001485 goto exit;
1486 }
1487
1488 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001489 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001490
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001491#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001492 if( ( ret = ssl_set_transport( &ssl, opt.transport ) ) != 0 )
1493 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001494 printf( " failed\n ! selected transport is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001495 goto exit;
1496 }
1497
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001498 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
1499 ssl_set_handshake_timeout( &ssl, opt.hs_to_min, opt.hs_to_max );
1500#endif /* POLARSSL_SSL_PROTO_DTLS */
1501
Paul Bakker05decb22013-08-15 13:33:48 +02001502#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001503 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1504 {
Rich Evansf90016a2015-01-19 14:26:37 +00001505 polarssl_printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001506 goto exit;
1507 };
Paul Bakker05decb22013-08-15 13:33:48 +02001508#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001509
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001510#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1511 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
1512 ssl_set_truncated_hmac( &ssl, opt.trunc_hmac );
1513#endif
1514
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001515#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1516 if( opt.extended_ms != DFL_EXTENDED_MS )
1517 ssl_set_extended_master_secret( &ssl, opt.extended_ms );
1518#endif
1519
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001520#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1521 if( opt.etm != DFL_ETM )
1522 ssl_set_encrypt_then_mac( &ssl, opt.etm );
1523#endif
1524
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001525#if defined(POLARSSL_SSL_ALPN)
1526 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001527 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1528 {
Rich Evansf90016a2015-01-19 14:26:37 +00001529 polarssl_printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001530 goto exit;
1531 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001532#endif
1533
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001534 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
1535 ssl_set_dbg( &ssl, my_debug, stdout );
1536
Paul Bakker0a597072012-09-25 21:55:46 +00001537#if defined(POLARSSL_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001538 if( opt.cache_max != -1 )
1539 ssl_cache_set_max_entries( &cache, opt.cache_max );
1540
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001541 if( opt.cache_timeout != -1 )
1542 ssl_cache_set_timeout( &cache, opt.cache_timeout );
1543
Paul Bakker0a597072012-09-25 21:55:46 +00001544 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
1545 ssl_cache_set, &cache );
1546#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001547
Paul Bakkera503a632013-08-14 13:48:06 +02001548#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001549 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1550 {
Rich Evansf90016a2015-01-19 14:26:37 +00001551 polarssl_printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001552 goto exit;
1553 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001554
1555 if( opt.ticket_timeout != -1 )
1556 ssl_set_session_ticket_lifetime( &ssl, opt.ticket_timeout );
Paul Bakkera503a632013-08-14 13:48:06 +02001557#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001558
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001559#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001560 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001561 {
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001562#if defined(POLARSSL_SSL_COOKIE_C)
1563 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001564 {
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001565 if( ( ret = ssl_cookie_setup( &cookie_ctx,
1566 ctr_drbg_random, &ctr_drbg ) ) != 0 )
1567 {
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001568 printf( " failed\n ! ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001569 goto exit;
1570 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001571
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001572 ssl_set_dtls_cookies( &ssl, ssl_cookie_write, ssl_cookie_check,
1573 &cookie_ctx );
1574 }
1575 else
1576#endif /* POLARSSL_SSL_COOKIE_C */
1577#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
1578 if( opt.cookies == 0 )
1579 {
1580 ssl_set_dtls_cookies( &ssl, NULL, NULL, NULL );
1581 }
1582 else
1583#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
1584 {
1585 ; /* Nothing to do */
1586 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001587
1588#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1589 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001590 ssl_set_dtls_anti_replay( &ssl, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001591#endif
1592
1593#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
1594 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
1595 ssl_set_dtls_badmac_limit( &ssl, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001596#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001597 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001598#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001599
Paul Bakker41c83d32013-03-20 14:39:14 +01001600 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001601 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001602 else
1603 ssl_set_arc4_support( &ssl, opt.arc4 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001604
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001605 if( opt.version_suites != NULL )
1606 {
1607 ssl_set_ciphersuites_for_version( &ssl, version_suites[0],
1608 SSL_MAJOR_VERSION_3,
1609 SSL_MINOR_VERSION_0 );
1610 ssl_set_ciphersuites_for_version( &ssl, version_suites[1],
1611 SSL_MAJOR_VERSION_3,
1612 SSL_MINOR_VERSION_1 );
1613 ssl_set_ciphersuites_for_version( &ssl, version_suites[2],
1614 SSL_MAJOR_VERSION_3,
1615 SSL_MINOR_VERSION_2 );
1616 ssl_set_ciphersuites_for_version( &ssl, version_suites[3],
1617 SSL_MAJOR_VERSION_3,
1618 SSL_MINOR_VERSION_3 );
1619 }
1620
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001621 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
1622 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001623#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001624 ssl_set_renegotiation( &ssl, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001625
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001626 if( opt.renego_delay != DFL_RENEGO_DELAY )
1627 ssl_set_renegotiation_enforced( &ssl, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001628
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001629 if( opt.renego_period != DFL_RENEGO_PERIOD )
1630 {
1631 renego_period[7] = opt.renego_period;
1632 ssl_set_renegotiation_period( &ssl, renego_period );
1633 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001634#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001635
Paul Bakker36713e82013-09-17 13:25:29 +02001636#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001637 if( strcmp( opt.ca_path, "none" ) != 0 &&
1638 strcmp( opt.ca_file, "none" ) != 0 )
1639 {
1640 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
1641 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001642 if( key_cert_init )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001643 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
1644 {
Rich Evansf90016a2015-01-19 14:26:37 +00001645 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001646 goto exit;
1647 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001648 if( key_cert_init2 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001649 if( ( ret = ssl_set_own_cert( &ssl, &srvcert2, &pkey2 ) ) != 0 )
1650 {
Rich Evansf90016a2015-01-19 14:26:37 +00001651 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001652 goto exit;
1653 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001654#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001655
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001656#if defined(POLARSSL_SNI)
1657 if( opt.sni != NULL )
1658 ssl_set_sni( &ssl, sni_callback, sni_info );
1659#endif
1660
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001661#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001662 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
1663 {
1664 ret = ssl_set_psk( &ssl, psk, psk_len,
1665 (const unsigned char *) opt.psk_identity,
1666 strlen( opt.psk_identity ) );
1667 if( ret != 0 )
1668 {
Rich Evansf90016a2015-01-19 14:26:37 +00001669 polarssl_printf( " failed\n ssl_set_psk returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001670 goto exit;
1671 }
1672 }
1673
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001674 if( opt.psk_list != NULL )
1675 ssl_set_psk_cb( &ssl, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02001676#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001677
1678#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00001679 /*
1680 * Use different group than default DHM group
1681 */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001682#if defined(POLARSSL_FS_IO)
1683 if( opt.dhm_file != NULL )
1684 ret = ssl_set_dh_param_ctx( &ssl, &dhm );
1685 else
1686#endif
1687 ret = ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
1688 POLARSSL_DHM_RFC5114_MODP_2048_G );
1689
1690 if( ret != 0 )
1691 {
Rich Evansf90016a2015-01-19 14:26:37 +00001692 polarssl_printf( " failed\n ssl_set_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001693 goto exit;
1694 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001695#endif
1696
Paul Bakker1d29fb52012-09-28 13:28:45 +00001697 if( opt.min_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001698 {
1699 ret = ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
Manuel Pégourié-Gonnard23eb74d2015-01-21 14:37:13 +00001700 if( ret != 0 && opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001701 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001702 printf( " failed\n ! selected min_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001703 goto exit;
1704 }
1705 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001706
Paul Bakkerc1516be2013-06-29 16:01:32 +02001707 if( opt.max_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001708 {
1709 ret = ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1710 if( ret != 0 )
1711 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001712 printf( " failed\n ! selected max_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001713 goto exit;
1714 }
1715 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001716
Rich Evansf90016a2015-01-19 14:26:37 +00001717 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001718
1719reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02001720#if !defined(_WIN32)
1721 if( received_sigterm )
1722 {
Manuel Pégourié-Gonnard7e81e702015-01-29 11:47:41 +00001723 polarssl_printf( " interrupted by SIGTERM\n" );
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02001724 ret = 0;
1725 goto exit;
1726 }
1727#endif
1728
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001729#ifdef POLARSSL_ERROR_C
1730 if( ret != 0 )
1731 {
1732 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001733 polarssl_strerror( ret, error_buf, 100 );
Rich Evansf90016a2015-01-19 14:26:37 +00001734 polarssl_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001735 }
1736#endif
1737
1738 if( client_fd != -1 )
Manuel Pégourié-Gonnard4ba6ab62014-08-07 17:21:47 +02001739 net_close( client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001740
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001741 ssl_session_reset( &ssl );
1742
1743 /*
1744 * 3. Wait until a client connects
1745 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001746 client_fd = -1;
1747
Rich Evansf90016a2015-01-19 14:26:37 +00001748 polarssl_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001749 fflush( stdout );
1750
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001751 if( ( ret = net_accept( listen_fd, &client_fd, client_ip ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001752 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02001753#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001754 if( received_sigterm )
1755 {
Rich Evansf90016a2015-01-19 14:26:37 +00001756 polarssl_printf( " interrupted by signal\n" );
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001757 ret = 0;
1758 goto exit;
1759 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02001760#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001761
Rich Evansf90016a2015-01-19 14:26:37 +00001762 polarssl_printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001763 goto exit;
1764 }
1765
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001766 if( opt.nbio > 0 )
1767 ret = net_set_nonblock( client_fd );
1768 else
1769 ret = net_set_block( client_fd );
1770 if( ret != 0 )
1771 {
Rich Evansf90016a2015-01-19 14:26:37 +00001772 polarssl_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001773 goto exit;
1774 }
1775
1776 if( opt.nbio == 2 )
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001777 ssl_set_bio_timeout( &ssl, &client_fd, my_send, my_recv, NULL, 0 );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001778 else
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001779 ssl_set_bio_timeout( &ssl, &client_fd, net_send, net_recv,
1780#if defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnardf0365122014-09-29 16:11:47 +02001781 opt.nbio == 0 ? net_recv_timeout : NULL,
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001782#else
1783 NULL,
1784#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001785 opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001786
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001787#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001788 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
1789 {
1790 if( ( ret = ssl_set_client_transport_id( &ssl, client_ip,
1791 sizeof( client_ip ) ) ) != 0 )
1792 {
1793 printf( " failed\n ! "
1794 "ssl_set_client_tranport_id() returned -0x%x\n\n", -ret );
1795 goto exit;
1796 }
1797 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001798#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001799
Rich Evansf90016a2015-01-19 14:26:37 +00001800 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001801
1802 /*
Manuel Pégourié-Gonnardbd97fdb2014-09-26 16:46:36 +02001803 * With UDP, bind_fd is hijacked by client_fd, so bind a new one
1804 */
1805#if defined(POLARSSL_SSL_PROTO_DTLS)
1806 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
1807 {
1808 printf( " . Re-bind on udp://%s:%-4d/ ...",
1809 opt.server_addr ? opt.server_addr : "*",
1810 opt.server_port );
1811 fflush( stdout );
1812
1813 if( ( ret = net_bind( &listen_fd, opt.server_addr,
1814 opt.server_port, NET_PROTO_UDP ) ) != 0 )
1815 {
1816 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1817 goto exit;
1818 }
1819
1820 printf( " ok\n" );
1821 }
1822#endif /* POLARSSL_SSL_PROTO_DTLS */
1823
1824 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001825 * 4. Handshake
1826 */
Rich Evansf90016a2015-01-19 14:26:37 +00001827 polarssl_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001828 fflush( stdout );
1829
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001830 do ret = ssl_handshake( &ssl );
1831 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1832 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001833
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001834 if( ret == POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED )
1835 {
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00001836 polarssl_printf( " hello verification requested\n" );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001837 ret = 0;
1838 goto reset;
1839 }
1840 else if( ret != 0 )
1841 {
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00001842 polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001843 goto reset;
1844 }
1845 else /* ret == 0 */
1846 {
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00001847 polarssl_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001848 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
1849 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001850
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001851 if( ( ret = ssl_get_record_expansion( &ssl ) ) >= 0 )
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00001852 polarssl_printf( " [ Record expansion is %d ]\n", ret );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001853 else
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00001854 polarssl_printf( " [ Record expansion is unknown (compression) ]\n" );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001855
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001856#if defined(POLARSSL_SSL_ALPN)
1857 if( opt.alpn_string != NULL )
1858 {
1859 const char *alp = ssl_get_alpn_protocol( &ssl );
Rich Evansf90016a2015-01-19 14:26:37 +00001860 polarssl_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001861 alp ? alp : "(none)" );
1862 }
1863#endif
1864
Paul Bakker36713e82013-09-17 13:25:29 +02001865#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001866 /*
1867 * 5. Verify the server certificate
1868 */
Rich Evansf90016a2015-01-19 14:26:37 +00001869 polarssl_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001870
1871 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1872 {
Rich Evansf90016a2015-01-19 14:26:37 +00001873 polarssl_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001874
Paul Bakkerb0550d92012-10-30 07:51:03 +00001875 if( !ssl_get_peer_cert( &ssl ) )
Rich Evansf90016a2015-01-19 14:26:37 +00001876 polarssl_printf( " ! no client certificate sent\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001877
1878 if( ( ret & BADCERT_EXPIRED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001879 polarssl_printf( " ! client certificate has expired\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001880
1881 if( ( ret & BADCERT_REVOKED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001882 polarssl_printf( " ! client certificate has been revoked\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001883
1884 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001885 polarssl_printf( " ! self-signed or not signed by a trusted CA\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001886
Rich Evansf90016a2015-01-19 14:26:37 +00001887 polarssl_printf( "\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001888 }
1889 else
Rich Evansf90016a2015-01-19 14:26:37 +00001890 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001891
Paul Bakkerb0550d92012-10-30 07:51:03 +00001892 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001893 {
Rich Evansf90016a2015-01-19 14:26:37 +00001894 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001895 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1896 ssl_get_peer_cert( &ssl ) );
Rich Evansf90016a2015-01-19 14:26:37 +00001897 polarssl_printf( "%s\n", buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001898 }
Paul Bakker36713e82013-09-17 13:25:29 +02001899#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001900
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001901 if( opt.exchanges == 0 )
1902 goto close_notify;
1903
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001904 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001905data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001906 /*
1907 * 6. Read the HTTP Request
1908 */
Rich Evansf90016a2015-01-19 14:26:37 +00001909 polarssl_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001910 fflush( stdout );
1911
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001912 /*
1913 * TLS and DTLS need different reading styles (stream vs datagram)
1914 */
1915 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001916 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001917 do
1918 {
1919 int terminated = 0;
1920 len = sizeof( buf ) - 1;
1921 memset( buf, 0, sizeof( buf ) );
1922 ret = ssl_read( &ssl, buf, len );
1923
1924 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1925 ret == POLARSSL_ERR_NET_WANT_WRITE )
1926 continue;
1927
1928 if( ret <= 0 )
1929 {
1930 switch( ret )
1931 {
1932 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00001933 polarssl_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001934 goto close_notify;
1935
1936 case 0:
1937 case POLARSSL_ERR_NET_CONN_RESET:
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00001938 polarssl_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001939 ret = POLARSSL_ERR_NET_CONN_RESET;
1940 goto reset;
1941
1942 default:
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00001943 polarssl_printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001944 goto reset;
1945 }
1946 }
1947
1948 if( ssl_get_bytes_avail( &ssl ) == 0 )
1949 {
1950 len = ret;
1951 buf[len] = '\0';
1952 printf( " %d bytes read\n\n%s\n", len, (char *) buf );
1953
1954 /* End of message should be detected according to the syntax of the
1955 * application protocol (eg HTTP), just use a dummy test here. */
1956 if( buf[len - 1] == '\n' )
1957 terminated = 1;
1958 }
1959 else
1960 {
1961 int extra_len, ori_len;
1962 unsigned char *larger_buf;
1963
1964 ori_len = ret;
1965 extra_len = ssl_get_bytes_avail( &ssl );
1966
1967 larger_buf = polarssl_malloc( ori_len + extra_len + 1 );
1968 if( larger_buf == NULL )
1969 {
1970 printf( " ! memory allocation failed\n" );
1971 ret = 1;
1972 goto reset;
1973 }
1974
1975 memset( larger_buf, 0, ori_len + extra_len );
1976 memcpy( larger_buf, buf, ori_len );
1977
1978 /* This read should never fail and get the whole cached data */
1979 ret = ssl_read( &ssl, larger_buf + ori_len, extra_len );
1980 if( ret != extra_len ||
1981 ssl_get_bytes_avail( &ssl ) != 0 )
1982 {
1983 printf( " ! ssl_read failed on cached data\n" );
1984 ret = 1;
1985 goto reset;
1986 }
1987
1988 larger_buf[ori_len + extra_len] = '\0';
1989 printf( " %u bytes read (%u + %u)\n\n%s\n",
1990 ori_len + extra_len, ori_len, extra_len,
1991 (char *) larger_buf );
1992
1993 /* End of message should be detected according to the syntax of the
1994 * application protocol (eg HTTP), just use a dummy test here. */
1995 if( larger_buf[ori_len + extra_len - 1] == '\n' )
1996 terminated = 1;
1997
1998 polarssl_free( larger_buf );
1999 }
2000
2001 if( terminated )
2002 {
2003 ret = 0;
2004 break;
2005 }
2006 }
2007 while( 1 );
2008 }
2009 else /* Not stream, so datagram */
2010 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002011 len = sizeof( buf ) - 1;
2012 memset( buf, 0, sizeof( buf ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002013
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002014 do ret = ssl_read( &ssl, buf, len );
2015 while( ret == POLARSSL_ERR_NET_WANT_READ ||
2016 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002017
2018 if( ret <= 0 )
2019 {
2020 switch( ret )
2021 {
2022 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
2023 printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002024 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002025 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002026
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002027 default:
2028 printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002029 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002030 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002031 }
2032
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002033 len = ret;
2034 buf[len] = '\0';
2035 printf( " %d bytes read\n\n%s", len, (char *) buf );
2036 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002037 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002038
2039 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002040 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02002041 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002042 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002043#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00002044 if( opt.renegotiate && exchanges_left == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002045 {
Rich Evansf90016a2015-01-19 14:26:37 +00002046 polarssl_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002047 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002048
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002049 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
2050 {
2051 if( ret != POLARSSL_ERR_NET_WANT_READ &&
2052 ret != POLARSSL_ERR_NET_WANT_WRITE )
2053 {
Rich Evansf90016a2015-01-19 14:26:37 +00002054 polarssl_printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002055 goto reset;
2056 }
2057 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002058
Rich Evansf90016a2015-01-19 14:26:37 +00002059 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002060 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002061#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002062
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002063 /*
2064 * 7. Write the 200 Response
2065 */
Rich Evansf90016a2015-01-19 14:26:37 +00002066 polarssl_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002067 fflush( stdout );
2068
2069 len = sprintf( (char *) buf, HTTP_RESPONSE,
2070 ssl_get_ciphersuite( &ssl ) );
2071
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002072 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002073 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002074 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002075 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002076 while( ( ret = ssl_write( &ssl, buf + written, len - written ) )
2077 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002078 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002079 if( ret == POLARSSL_ERR_NET_CONN_RESET )
2080 {
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00002081 polarssl_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002082 goto reset;
2083 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002084
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002085 if( ret != POLARSSL_ERR_NET_WANT_READ &&
2086 ret != POLARSSL_ERR_NET_WANT_WRITE )
2087 {
Manuel Pégourié-Gonnard2a0718d2015-01-29 11:29:12 +00002088 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002089 goto reset;
2090 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002091 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002092 }
2093 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002094 else /* Not stream, so datagram */
2095 {
2096 do ret = ssl_write( &ssl, buf, len );
2097 while( ret == POLARSSL_ERR_NET_WANT_READ ||
2098 ret == POLARSSL_ERR_NET_WANT_WRITE );
2099
2100 if( ret < 0 )
2101 {
2102 printf( " failed\n ! ssl_write returned %d\n\n", ret );
2103 goto reset;
2104 }
2105
2106 frags = 1;
2107 written = ret;
2108 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002109
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002110 buf[written] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +00002111 polarssl_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Manuel Pégourié-Gonnarda92ed482015-01-14 10:46:08 +01002112 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002113
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002114 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002115 * 7b. Continue doing data exchanges?
2116 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00002117 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002118 goto data_exchange;
2119
2120 /*
2121 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002122 */
2123close_notify:
Rich Evansf90016a2015-01-19 14:26:37 +00002124 polarssl_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002125
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002126 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard34377b12015-01-22 10:46:46 +00002127 do ret = ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002128 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
2129 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002130
Rich Evansf90016a2015-01-19 14:26:37 +00002131 polarssl_printf( " done\n" );
2132
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002133 goto reset;
2134
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002135 /*
2136 * Cleanup and exit
2137 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002138exit:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002139#ifdef POLARSSL_ERROR_C
2140 if( ret != 0 )
2141 {
2142 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02002143 polarssl_strerror( ret, error_buf, 100 );
Rich Evansf90016a2015-01-19 14:26:37 +00002144 polarssl_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002145 }
2146#endif
2147
Manuel Pégourié-Gonnard7e81e702015-01-29 11:47:41 +00002148 polarssl_printf( " . Cleaning up..." );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01002149 fflush( stdout );
2150
Paul Bakker0c226102014-04-17 16:02:36 +02002151 if( client_fd != -1 )
2152 net_close( client_fd );
2153
Paul Bakkera317a982014-06-18 16:44:11 +02002154#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
2155 dhm_free( &dhm );
2156#endif
Paul Bakker36713e82013-09-17 13:25:29 +02002157#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker36713e82013-09-17 13:25:29 +02002158 x509_crt_free( &cacert );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002159 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02002160 pk_free( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002161 x509_crt_free( &srvcert2 );
2162 pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02002163#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002164#if defined(POLARSSL_SNI)
2165 sni_free( sni_info );
2166#endif
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02002167#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
2168 psk_free( psk_info );
2169#endif
2170#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
2171 dhm_free( &dhm );
2172#endif
Paul Bakkered27a042013-04-18 22:46:23 +02002173
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002174 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02002175 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02002176 entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002177
Paul Bakker0a597072012-09-25 21:55:46 +00002178#if defined(POLARSSL_SSL_CACHE_C)
2179 ssl_cache_free( &cache );
2180#endif
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +02002181#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002182 ssl_cookie_free( &cookie_ctx );
2183#endif
Paul Bakker0a597072012-09-25 21:55:46 +00002184
Paul Bakker1337aff2013-09-29 14:45:34 +02002185#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
2186#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker82024bf2013-07-04 11:52:32 +02002187 memory_buffer_alloc_status();
2188#endif
Paul Bakker1337aff2013-09-29 14:45:34 +02002189 memory_buffer_alloc_free();
2190#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02002191
Manuel Pégourié-Gonnard7e81e702015-01-29 11:47:41 +00002192 polarssl_printf( " done.\n" );
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01002193
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002194#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +00002195 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002196 fflush( stdout ); getchar();
2197#endif
2198
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002199 // Shell can not handle large exit numbers -> 1 for errors
2200 if( ret < 0 )
2201 ret = 1;
2202
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002203 return( ret );
2204}
2205#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
2206 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
2207 POLARSSL_CTR_DRBG_C */