blob: bd01c1bff5af7006a851c6e5025c97c0ee563cc7 [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020029#if !defined(POLARSSL_ENTROPY_C) || \
30 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
31 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
32#include <stdio.h>
33int main( int argc, char *argv[] )
34{
35 ((void) argc);
36 ((void) argv);
37
38 printf("POLARSSL_ENTROPY_C and/or "
39 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
40 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
41 return( 0 );
42}
43#else
44
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010045#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) && defined(POLARSSL_FS_IO)
46#define POLARSSL_SNI
47#endif
48
49#if defined(POLARSSL_PLATFORM_C)
50#include "polarssl/platform.h"
51#else
52#define polarssl_malloc malloc
53#define polarssl_free free
54#endif
55
Paul Bakkerb60b95f2012-09-25 09:05:17 +000056#if defined(_WIN32)
57#include <windows.h>
58#endif
59
60#include <string.h>
61#include <stdlib.h>
62#include <stdio.h>
Paul Bakkerc1283d32014-08-18 11:05:51 +020063
64#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +020065#include <signal.h>
Paul Bakkerc1283d32014-08-18 11:05:51 +020066#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000067
Paul Bakkerb60b95f2012-09-25 09:05:17 +000068#include "polarssl/net.h"
69#include "polarssl/ssl.h"
70#include "polarssl/entropy.h"
71#include "polarssl/ctr_drbg.h"
72#include "polarssl/certs.h"
73#include "polarssl/x509.h"
74#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020075#include "polarssl/debug.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000076
Paul Bakker0a597072012-09-25 21:55:46 +000077#if defined(POLARSSL_SSL_CACHE_C)
78#include "polarssl/ssl_cache.h"
79#endif
80
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +020081#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020082#include "polarssl/ssl_cookie.h"
83#endif
84
Paul Bakker82024bf2013-07-04 11:52:32 +020085#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
86#include "polarssl/memory.h"
87#endif
88
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +010089#define DFL_SERVER_ADDR NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000090#define DFL_SERVER_PORT 4433
Paul Bakkerb60b95f2012-09-25 09:05:17 +000091#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010092#define DFL_NBIO 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020093#define DFL_READ_TIMEOUT 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +000094#define DFL_CA_FILE ""
95#define DFL_CA_PATH ""
96#define DFL_CRT_FILE ""
97#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +020098#define DFL_CRT_FILE2 ""
99#define DFL_KEY_FILE2 ""
Paul Bakkerfbb17802013-04-17 19:10:21 +0200100#define DFL_PSK ""
101#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200102#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000103#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200104#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +0200105#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100106#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100107#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200108#define DFL_RENEGO_DELAY -2
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100109#define DFL_RENEGO_PERIOD -1
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200110#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100111#define DFL_MIN_VERSION SSL_MINOR_VERSION_1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200112#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100113#define DFL_ARC4 SSL_ARC4_DISABLED
Paul Bakker91ebfb52012-11-23 14:04:08 +0100114#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200115#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100116#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200117#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100118#define DFL_TICKET_TIMEOUT -1
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100119#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100120#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100121#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200122#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200123#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100124#define DFL_TRANSPORT SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200125#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200126#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200127#define DFL_HS_TO_MIN 0
128#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200129#define DFL_BADMAC_LIMIT -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200130#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100131#define DFL_ETM -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000132
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200133#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
134 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
135 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
136 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
137 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
138 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
139 "07-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah</p>\r\n"
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200140
Paul Bakker8e714d72013-07-18 11:05:13 +0200141/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
142 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000143#define HTTP_RESPONSE \
144 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000145 "<h2>mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200146 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000147
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200148/*
149 * Size of the basic I/O buffer. Able to hold our default response.
150 *
151 * You will need to adapt the ssl_get_bytes_avail() test in ssl-opt.sh
152 * if you change this value to something outside the range <= 100 or > 500
153 */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200154#define IO_BUF_LEN 200
155
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000156/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000157 * global options
158 */
159struct options
160{
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100161 const char *server_addr; /* address on which the ssl service runs */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000162 int server_port; /* port on which the ssl service runs */
163 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100164 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200165 uint32_t read_timeout; /* timeout on ssl_read() in milliseconds */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200166 const char *ca_file; /* the file with the CA certificate(s) */
167 const char *ca_path; /* the path with the CA certificate(s) reside */
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200168 const char *crt_file; /* the file with the server certificate */
169 const char *key_file; /* the file with the server key */
170 const char *crt_file2; /* the file with the 2nd server certificate */
171 const char *key_file2; /* the file with the 2nd server key */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200172 const char *psk; /* the pre-shared key */
173 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200174 char *psk_list; /* list of PSK id/key pairs for callback */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000175 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200176 const char *version_suites; /* per-version ciphersuites */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000177 int renegotiation; /* enable / disable renegotiation */
178 int allow_legacy; /* allow legacy renegotiation */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100179 int renegotiate; /* attempt renegotiation? */
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200180 int renego_delay; /* delay before enforcing renegotiation */
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100181 int renego_period; /* period for automatic renegotiation */
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200182 int exchanges; /* number of data exchanges */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000183 int min_version; /* minimum protocol version accepted */
Paul Bakkerc1516be2013-06-29 16:01:32 +0200184 int max_version; /* maximum protocol version accepted */
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100185 int arc4; /* flag for arc4 suites support */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100186 int auth_mode; /* verify mode for connection */
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200187 unsigned char mfl_code; /* code for maximum fragment length */
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100188 int trunc_hmac; /* accept truncated hmac? */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200189 int tickets; /* enable / disable session tickets */
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100190 int ticket_timeout; /* session ticket lifetime */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100191 int cache_max; /* max number of session cache entries */
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100192 int cache_timeout; /* expiration delay of session cache entries */
Paul Bakker1ebc0c52014-05-22 15:47:58 +0200193 char *sni; /* string describing sni information */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200194 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200195 const char *dhm_file; /* the file with the DH parameters */
Paul Bakkerb2eaac12015-01-13 17:15:31 +0100196 int extended_ms; /* allow negotiation of extended MS? */
197 int etm; /* allow negotiation of encrypt-then-MAC? */
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100198 int transport; /* TLS or DTLS? */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200199 int cookies; /* Use cookies for DTLS? -1 to break them */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200200 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200201 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
202 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200203 int badmac_limit; /* Limit of records with bad MAC */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000204} opt;
205
Paul Bakker3c5ef712013-06-25 16:37:45 +0200206static void my_debug( void *ctx, int level, const char *str )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000207{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200208 ((void) level);
209
210 fprintf( (FILE *) ctx, "%s", str );
211 fflush( (FILE *) ctx );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000212}
213
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100214/*
215 * Test recv/send functions that make sure each try returns
216 * WANT_READ/WANT_WRITE at least once before sucesseding
217 */
218static int my_recv( void *ctx, unsigned char *buf, size_t len )
219{
220 static int first_try = 1;
221 int ret;
222
223 if( first_try )
224 {
225 first_try = 0;
226 return( POLARSSL_ERR_NET_WANT_READ );
227 }
228
229 ret = net_recv( ctx, buf, len );
230 if( ret != POLARSSL_ERR_NET_WANT_READ )
231 first_try = 1; /* Next call will be a new operation */
232 return( ret );
233}
234
235static int my_send( void *ctx, const unsigned char *buf, size_t len )
236{
237 static int first_try = 1;
238 int ret;
239
240 if( first_try )
241 {
242 first_try = 0;
243 return( POLARSSL_ERR_NET_WANT_WRITE );
244 }
245
246 ret = net_send( ctx, buf, len );
247 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
248 first_try = 1; /* Next call will be a new operation */
249 return( ret );
250}
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200251
252#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000253#if defined(POLARSSL_FS_IO)
254#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100255 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
256 " default: \"\" (pre-loaded)\n" \
257 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
258 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
259 " 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 +0200260 " default: see note after key_file2\n" \
261 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200262 " 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 +0200263 " default: see note after key_file2\n" \
264 " key_file2=%%s default: see note below\n" \
265 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200266 " preloaded certificate(s) and key(s) are used if available\n" \
267 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
268 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000269#else
270#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200271 "\n" \
272 " No file operations available (POLARSSL_FS_IO not defined)\n" \
273 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000274#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200275#else
276#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200277#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200278
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200279#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200280#define USAGE_PSK \
281 " psk=%%s default: \"\" (in hex, without 0x)\n" \
282 " psk_identity=%%s default: \"Client_identity\"\n"
283#else
284#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200285#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000286
Paul Bakkera503a632013-08-14 13:48:06 +0200287#if defined(POLARSSL_SSL_SESSION_TICKETS)
288#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100289 " tickets=%%d default: 1 (enabled)\n" \
290 " ticket_timeout=%%d default: ticket default (1d)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200291#else
292#define USAGE_TICKETS ""
293#endif /* POLARSSL_SSL_SESSION_TICKETS */
294
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100295#if defined(POLARSSL_SSL_CACHE_C)
296#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100297 " cache_max=%%d default: cache default (50)\n" \
298 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100299#else
300#define USAGE_CACHE ""
301#endif /* POLARSSL_SSL_CACHE_C */
302
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100303#if defined(POLARSSL_SNI)
304#define USAGE_SNI \
305 " sni=%%s name1,cert1,key1[,name2,cert2,key2[,...]]\n" \
306 " default: disabled\n"
307#else
308#define USAGE_SNI ""
309#endif /* POLARSSL_SNI */
310
Paul Bakker05decb22013-08-15 13:33:48 +0200311#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
312#define USAGE_MAX_FRAG_LEN \
313 " max_frag_len=%%d default: 16384 (tls default)\n" \
314 " options: 512, 1024, 2048, 4096\n"
315#else
316#define USAGE_MAX_FRAG_LEN ""
317#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
318
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100319#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
320#define USAGE_TRUNC_HMAC \
321 " trunc_hmac=%%d default: library default\n"
322#else
323#define USAGE_TRUNC_HMAC ""
324#endif
325
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200326#if defined(POLARSSL_SSL_ALPN)
327#define USAGE_ALPN \
328 " alpn=%%s default: \"\" (disabled)\n" \
329 " example: spdy/1,http/1.1\n"
330#else
331#define USAGE_ALPN ""
332#endif /* POLARSSL_SSL_ALPN */
333
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200334#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
335#define USAGE_COOKIES \
336 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200337 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200338#else
339#define USAGE_COOKIES ""
340#endif
341
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200342#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
343#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200344 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200345#else
346#define USAGE_ANTI_REPLAY ""
347#endif
348
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200349#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
350#define USAGE_BADMAC_LIMIT \
351 " badmac_limit=%%d default: (library default: disabled)\n"
352#else
353#define USAGE_BADMAC_LIMIT ""
354#endif
355
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200356#if defined(POLARSSL_SSL_PROTO_DTLS)
357#define USAGE_DTLS \
358 " dtls=%%d default: 0 (TLS)\n" \
359 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
360 " range of DTLS handshake timeouts in millisecs\n"
361#else
362#define USAGE_DTLS ""
363#endif
364
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200365#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
366#define USAGE_EMS \
367 " extended_ms=0/1 default: (library default: on)\n"
368#else
369#define USAGE_EMS ""
370#endif
371
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100372#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
373#define USAGE_ETM \
374 " etm=0/1 default: (library default: on)\n"
375#else
376#define USAGE_ETM ""
377#endif
378
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100379#if defined(POLARSSL_SSL_RENEGOTIATION)
380#define USAGE_RENEGO \
381 " renegotiation=%%d default: 0 (disabled)\n" \
382 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100383 " renego_delay=%%d default: -2 (library default)\n" \
384 " renego_period=%%d default: (library default)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100385#else
386#define USAGE_RENEGO ""
387#endif
388
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000389#define USAGE \
390 "\n usage: ssl_server2 param=<>...\n" \
391 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100392 " server_addr=%%d default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000393 " server_port=%%d default: 4433\n" \
394 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100395 " nbio=%%d default: 0 (blocking I/O)\n" \
396 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200397 " read_timeout=%%d default: 0 (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100398 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200399 USAGE_DTLS \
400 USAGE_COOKIES \
401 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200402 USAGE_BADMAC_LIMIT \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200403 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100404 " auth_mode=%%s default: \"optional\"\n" \
405 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000406 USAGE_IO \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100407 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100408 "\n" \
409 USAGE_PSK \
410 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100411 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100412 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200413 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200414 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100415 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100416 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100417 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100418 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200419 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200420 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100421 USAGE_ETM \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100422 "\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000423 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200424 " max_version=%%s default: \"tls1_2\"\n" \
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100425 " arc4=%%d default: 0 (disabled)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200426 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100427 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200428 "\n" \
429 " version_suites=a,b,c,d per-version ciphersuites\n" \
430 " in order from ssl3 to tls1_2\n" \
431 " default: all enabled\n" \
432 " force_ciphersuite=<name> default: all enabled\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000433 " acceptable ciphersuite names:\n"
434
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200435/*
436 * Used by sni_parse and psk_parse to handle coma-separated lists
437 */
438#define GET_ITEM( dst ) \
439 dst = p; \
440 while( *p != ',' ) \
441 if( ++p > end ) \
442 return( NULL ); \
443 *p++ = '\0';
444
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100445#if defined(POLARSSL_SNI)
446typedef struct _sni_entry sni_entry;
447
448struct _sni_entry {
449 const char *name;
450 x509_crt *cert;
451 pk_context *key;
452 sni_entry *next;
453};
454
455/*
456 * Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]]
457 * into a usable sni_entry list.
458 *
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200459 * Modifies the input string! This is not production quality!
460 * (leaks memory if parsing fails, no error reporting, ...)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100461 */
462sni_entry *sni_parse( char *sni_string )
463{
464 sni_entry *cur = NULL, *new = NULL;
465 char *p = sni_string;
466 char *end = p;
467 char *crt_file, *key_file;
468
469 while( *end != '\0' )
470 ++end;
471 *end = ',';
472
473 while( p <= end )
474 {
475 if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL )
476 return( NULL );
477
478 memset( new, 0, sizeof( sni_entry ) );
479
480 if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ||
481 ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200482 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100483
484 x509_crt_init( new->cert );
485 pk_init( new->key );
486
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200487 GET_ITEM( new->name );
488 GET_ITEM( crt_file );
489 GET_ITEM( key_file );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100490
491 if( x509_crt_parse_file( new->cert, crt_file ) != 0 ||
492 pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200493 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100494
495 new->next = cur;
496 cur = new;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100497 }
498
499 return( cur );
500}
501
502void sni_free( sni_entry *head )
503{
504 sni_entry *cur = head, *next;
505
506 while( cur != NULL )
507 {
508 x509_crt_free( cur->cert );
509 polarssl_free( cur->cert );
510
511 pk_free( cur->key );
512 polarssl_free( cur->key );
513
514 next = cur->next;
515 polarssl_free( cur );
516 cur = next;
517 }
518}
519
520/*
521 * SNI callback.
522 */
523int sni_callback( void *p_info, ssl_context *ssl,
524 const unsigned char *name, size_t name_len )
525{
526 sni_entry *cur = (sni_entry *) p_info;
527
528 while( cur != NULL )
529 {
530 if( name_len == strlen( cur->name ) &&
531 memcmp( name, cur->name, name_len ) == 0 )
532 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200533 return( ssl_set_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100534 }
535
536 cur = cur->next;
537 }
538
539 return( -1 );
540}
541
542#endif /* POLARSSL_SNI */
543
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200544#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
545
546#define HEX2NUM( c ) \
547 if( c >= '0' && c <= '9' ) \
548 c -= '0'; \
549 else if( c >= 'a' && c <= 'f' ) \
550 c -= 'a' - 10; \
551 else if( c >= 'A' && c <= 'F' ) \
552 c -= 'A' - 10; \
553 else \
554 return( -1 );
555
556/*
557 * Convert a hex string to bytes.
558 * Return 0 on success, -1 on error.
559 */
560int unhexify( unsigned char *output, const char *input, size_t *olen )
561{
562 unsigned char c;
563 size_t j;
564
565 *olen = strlen( input );
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200566 if( *olen % 2 != 0 || *olen / 2 > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200567 return( -1 );
568 *olen /= 2;
569
570 for( j = 0; j < *olen * 2; j += 2 )
571 {
572 c = input[j];
573 HEX2NUM( c );
574 output[ j / 2 ] = c << 4;
575
576 c = input[j + 1];
577 HEX2NUM( c );
578 output[ j / 2 ] |= c;
579 }
580
581 return( 0 );
582}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200583
584typedef struct _psk_entry psk_entry;
585
586struct _psk_entry
587{
588 const char *name;
589 size_t key_len;
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200590 unsigned char key[POLARSSL_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200591 psk_entry *next;
592};
593
594/*
595 * Parse a string of pairs name1,key1[,name2,key2[,...]]
596 * into a usable psk_entry list.
597 *
598 * Modifies the input string! This is not production quality!
599 * (leaks memory if parsing fails, no error reporting, ...)
600 */
601psk_entry *psk_parse( char *psk_string )
602{
603 psk_entry *cur = NULL, *new = NULL;
604 char *p = psk_string;
605 char *end = p;
606 char *key_hex;
607
608 while( *end != '\0' )
609 ++end;
610 *end = ',';
611
612 while( p <= end )
613 {
614 if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL )
615 return( NULL );
616
617 memset( new, 0, sizeof( psk_entry ) );
618
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200619 GET_ITEM( new->name );
620 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200621
622 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
623 return( NULL );
624
625 new->next = cur;
626 cur = new;
627 }
628
629 return( cur );
630}
631
632/*
633 * Free a list of psk_entry's
634 */
635void psk_free( psk_entry *head )
636{
637 psk_entry *next;
638
639 while( head != NULL )
640 {
641 next = head->next;
642 polarssl_free( head );
643 head = next;
644 }
645}
646
647/*
648 * PSK callback
649 */
650int psk_callback( void *p_info, ssl_context *ssl,
651 const unsigned char *name, size_t name_len )
652{
653 psk_entry *cur = (psk_entry *) p_info;
654
655 while( cur != NULL )
656 {
657 if( name_len == strlen( cur->name ) &&
658 memcmp( name, cur->name, name_len ) == 0 )
659 {
660 return( ssl_set_psk( ssl, cur->key, cur->key_len,
661 name, name_len ) );
662 }
663
664 cur = cur->next;
665 }
666
667 return( -1 );
668}
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200669#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
670
Manuel Pégourié-Gonnarda9d7d032014-10-09 16:07:08 +0200671static int listen_fd, client_fd = -1;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200672
673/* Interruption handler to ensure clean exit (for valgrind testing) */
674#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200675static int received_sigterm = 0;
676void term_handler( int sig )
677{
678 ((void) sig);
679 received_sigterm = 1;
680 net_close( listen_fd ); /* causes net_accept() to abort */
Manuel Pégourié-Gonnarda9d7d032014-10-09 16:07:08 +0200681 net_close( client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200682}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200683#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200684
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000685int main( int argc, char *argv[] )
686{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +0000687 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200688 int version_suites[4][2];
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200689 unsigned char buf[IO_BUF_LEN];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200690#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200691 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +0200692 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +0200693 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +0200694#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200695 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +0200696 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +0200697#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200698 ssl_cookie_ctx cookie_ctx;
699#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000700
701 entropy_context entropy;
702 ctr_drbg_context ctr_drbg;
703 ssl_context ssl;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100704#if defined(POLARSSL_SSL_RENEGOTIATION)
705 unsigned char renego_period[8] = { 0 };
706#endif
Paul Bakker36713e82013-09-17 13:25:29 +0200707#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200708 x509_crt cacert;
709 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200710 pk_context pkey;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200711 x509_crt srvcert2;
712 pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200713 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200714#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200715#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
716 dhm_context dhm;
717#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000718#if defined(POLARSSL_SSL_CACHE_C)
719 ssl_cache_context cache;
720#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100721#if defined(POLARSSL_SNI)
722 sni_entry *sni_info = NULL;
723#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200724#if defined(POLARSSL_SSL_ALPN)
725 const char *alpn_list[10];
726#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200727#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
728 unsigned char alloc_buf[100000];
729#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000730
731 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000732 char *p, *q;
733 const int *list;
734
Paul Bakker82024bf2013-07-04 11:52:32 +0200735#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
736 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
737#endif
738
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000739 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200740 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000741 */
742 listen_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200743 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200744#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200745 x509_crt_init( &cacert );
746 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200747 pk_init( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200748 x509_crt_init( &srvcert2 );
749 pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200750#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200751#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
Paul Bakkera317a982014-06-18 16:44:11 +0200752 dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200753#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000754#if defined(POLARSSL_SSL_CACHE_C)
755 ssl_cache_init( &cache );
756#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200757#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200758 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200759#endif
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +0200760#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200761 ssl_cookie_init( &cookie_ctx );
762#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000763
Paul Bakkerc1283d32014-08-18 11:05:51 +0200764#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100765 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200766 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100767 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +0200768#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200769
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000770 if( argc == 0 )
771 {
772 usage:
773 if( ret == 0 )
774 ret = 1;
775
776 printf( USAGE );
777
778 list = ssl_list_ciphersuites();
779 while( *list )
780 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200781 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200782 list++;
783 if( !*list )
784 break;
785 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000786 list++;
787 }
788 printf("\n");
789 goto exit;
790 }
791
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100792 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000793 opt.server_port = DFL_SERVER_PORT;
794 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100795 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200796 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000797 opt.ca_file = DFL_CA_FILE;
798 opt.ca_path = DFL_CA_PATH;
799 opt.crt_file = DFL_CRT_FILE;
800 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200801 opt.crt_file2 = DFL_CRT_FILE2;
802 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200803 opt.psk = DFL_PSK;
804 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200805 opt.psk_list = DFL_PSK_LIST;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000806 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200807 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000808 opt.renegotiation = DFL_RENEGOTIATION;
809 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100810 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200811 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100812 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200813 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000814 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200815 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100816 opt.arc4 = DFL_ARC4;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100817 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200818 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100819 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200820 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100821 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100822 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100823 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100824 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200825 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200826 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100827 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200828 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200829 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200830 opt.hs_to_min = DFL_HS_TO_MIN;
831 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200832 opt.badmac_limit = DFL_BADMAC_LIMIT;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200833 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100834 opt.etm = DFL_ETM;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000835
836 for( i = 1; i < argc; i++ )
837 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000838 p = argv[i];
839 if( ( q = strchr( p, '=' ) ) == NULL )
840 goto usage;
841 *q++ = '\0';
842
843 if( strcmp( p, "server_port" ) == 0 )
844 {
845 opt.server_port = atoi( q );
846 if( opt.server_port < 1 || opt.server_port > 65535 )
847 goto usage;
848 }
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100849 else if( strcmp( p, "server_addr" ) == 0 )
850 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100851 else if( strcmp( p, "dtls" ) == 0 )
852 {
853 int t = atoi( q );
854 if( t == 0 )
855 opt.transport = SSL_TRANSPORT_STREAM;
856 else if( t == 1 )
857 opt.transport = SSL_TRANSPORT_DATAGRAM;
858 else
859 goto usage;
860 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000861 else if( strcmp( p, "debug_level" ) == 0 )
862 {
863 opt.debug_level = atoi( q );
864 if( opt.debug_level < 0 || opt.debug_level > 65535 )
865 goto usage;
866 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100867 else if( strcmp( p, "nbio" ) == 0 )
868 {
869 opt.nbio = atoi( q );
870 if( opt.nbio < 0 || opt.nbio > 2 )
871 goto usage;
872 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200873 else if( strcmp( p, "read_timeout" ) == 0 )
874 opt.read_timeout = atoi( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000875 else if( strcmp( p, "ca_file" ) == 0 )
876 opt.ca_file = q;
877 else if( strcmp( p, "ca_path" ) == 0 )
878 opt.ca_path = q;
879 else if( strcmp( p, "crt_file" ) == 0 )
880 opt.crt_file = q;
881 else if( strcmp( p, "key_file" ) == 0 )
882 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200883 else if( strcmp( p, "crt_file2" ) == 0 )
884 opt.crt_file2 = q;
885 else if( strcmp( p, "key_file2" ) == 0 )
886 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200887 else if( strcmp( p, "dhm_file" ) == 0 )
888 opt.dhm_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200889 else if( strcmp( p, "psk" ) == 0 )
890 opt.psk = q;
891 else if( strcmp( p, "psk_identity" ) == 0 )
892 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200893 else if( strcmp( p, "psk_list" ) == 0 )
894 opt.psk_list = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000895 else if( strcmp( p, "force_ciphersuite" ) == 0 )
896 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000897 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
898
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200899 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000900 {
901 ret = 2;
902 goto usage;
903 }
904 opt.force_ciphersuite[1] = 0;
905 }
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200906 else if( strcmp( p, "version_suites" ) == 0 )
907 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000908 else if( strcmp( p, "renegotiation" ) == 0 )
909 {
910 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
911 SSL_RENEGOTIATION_DISABLED;
912 }
913 else if( strcmp( p, "allow_legacy" ) == 0 )
914 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100915 switch( atoi( q ) )
916 {
917 case -1: opt.allow_legacy = SSL_LEGACY_BREAK_HANDSHAKE; break;
918 case 0: opt.allow_legacy = SSL_LEGACY_NO_RENEGOTIATION; break;
919 case 1: opt.allow_legacy = SSL_LEGACY_ALLOW_RENEGOTIATION; break;
920 default: goto usage;
921 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000922 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100923 else if( strcmp( p, "renegotiate" ) == 0 )
924 {
925 opt.renegotiate = atoi( q );
926 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
927 goto usage;
928 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200929 else if( strcmp( p, "renego_delay" ) == 0 )
930 {
931 opt.renego_delay = atoi( q );
932 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100933 else if( strcmp( p, "renego_period" ) == 0 )
934 {
935 opt.renego_period = atoi( q );
936 if( opt.renego_period < 2 || opt.renego_period > 255 )
937 goto usage;
938 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200939 else if( strcmp( p, "exchanges" ) == 0 )
940 {
941 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +0200942 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200943 goto usage;
944 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000945 else if( strcmp( p, "min_version" ) == 0 )
946 {
947 if( strcmp( q, "ssl3" ) == 0 )
948 opt.min_version = SSL_MINOR_VERSION_0;
949 else if( strcmp( q, "tls1" ) == 0 )
950 opt.min_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100951 else if( strcmp( q, "tls1_1" ) == 0 ||
952 strcmp( q, "dtls1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000953 opt.min_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100954 else if( strcmp( q, "tls1_2" ) == 0 ||
955 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000956 opt.min_version = SSL_MINOR_VERSION_3;
957 else
958 goto usage;
959 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200960 else if( strcmp( p, "max_version" ) == 0 )
961 {
962 if( strcmp( q, "ssl3" ) == 0 )
963 opt.max_version = SSL_MINOR_VERSION_0;
964 else if( strcmp( q, "tls1" ) == 0 )
965 opt.max_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100966 else if( strcmp( q, "tls1_1" ) == 0 ||
967 strcmp( q, "dtls1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200968 opt.max_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100969 else if( strcmp( q, "tls1_2" ) == 0 ||
970 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200971 opt.max_version = SSL_MINOR_VERSION_3;
972 else
973 goto usage;
974 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100975 else if( strcmp( p, "arc4" ) == 0 )
976 {
977 switch( atoi( q ) )
978 {
979 case 0: opt.arc4 = SSL_ARC4_DISABLED; break;
980 case 1: opt.arc4 = SSL_ARC4_ENABLED; break;
981 default: goto usage;
982 }
983 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200984 else if( strcmp( p, "force_version" ) == 0 )
985 {
986 if( strcmp( q, "ssl3" ) == 0 )
987 {
988 opt.min_version = SSL_MINOR_VERSION_0;
989 opt.max_version = SSL_MINOR_VERSION_0;
990 }
991 else if( strcmp( q, "tls1" ) == 0 )
992 {
993 opt.min_version = SSL_MINOR_VERSION_1;
994 opt.max_version = SSL_MINOR_VERSION_1;
995 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100996 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200997 {
998 opt.min_version = SSL_MINOR_VERSION_2;
999 opt.max_version = SSL_MINOR_VERSION_2;
1000 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001001 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +02001002 {
1003 opt.min_version = SSL_MINOR_VERSION_3;
1004 opt.max_version = SSL_MINOR_VERSION_3;
1005 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +01001006 else if( strcmp( q, "dtls1" ) == 0 )
1007 {
1008 opt.min_version = SSL_MINOR_VERSION_2;
1009 opt.max_version = SSL_MINOR_VERSION_2;
1010 opt.transport = SSL_TRANSPORT_DATAGRAM;
1011 }
1012 else if( strcmp( q, "dtls1_2" ) == 0 )
1013 {
1014 opt.min_version = SSL_MINOR_VERSION_3;
1015 opt.max_version = SSL_MINOR_VERSION_3;
1016 opt.transport = SSL_TRANSPORT_DATAGRAM;
1017 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001018 else
1019 goto usage;
1020 }
Paul Bakker91ebfb52012-11-23 14:04:08 +01001021 else if( strcmp( p, "auth_mode" ) == 0 )
1022 {
1023 if( strcmp( q, "none" ) == 0 )
1024 opt.auth_mode = SSL_VERIFY_NONE;
1025 else if( strcmp( q, "optional" ) == 0 )
1026 opt.auth_mode = SSL_VERIFY_OPTIONAL;
1027 else if( strcmp( q, "required" ) == 0 )
1028 opt.auth_mode = SSL_VERIFY_REQUIRED;
1029 else
1030 goto usage;
1031 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001032 else if( strcmp( p, "max_frag_len" ) == 0 )
1033 {
1034 if( strcmp( q, "512" ) == 0 )
1035 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
1036 else if( strcmp( q, "1024" ) == 0 )
1037 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
1038 else if( strcmp( q, "2048" ) == 0 )
1039 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
1040 else if( strcmp( q, "4096" ) == 0 )
1041 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
1042 else
1043 goto usage;
1044 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001045 else if( strcmp( p, "alpn" ) == 0 )
1046 {
1047 opt.alpn_string = q;
1048 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001049 else if( strcmp( p, "trunc_hmac" ) == 0 )
1050 {
1051 switch( atoi( q ) )
1052 {
1053 case 0: opt.trunc_hmac = SSL_TRUNC_HMAC_DISABLED; break;
1054 case 1: opt.trunc_hmac = SSL_TRUNC_HMAC_ENABLED; break;
1055 default: goto usage;
1056 }
1057 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001058 else if( strcmp( p, "extended_ms" ) == 0 )
1059 {
1060 switch( atoi( q ) )
1061 {
1062 case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break;
1063 case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break;
1064 default: goto usage;
1065 }
1066 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001067 else if( strcmp( p, "etm" ) == 0 )
1068 {
1069 switch( atoi( q ) )
1070 {
1071 case 0: opt.etm = SSL_ETM_DISABLED; break;
1072 case 1: opt.etm = SSL_ETM_ENABLED; break;
1073 default: goto usage;
1074 }
1075 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001076 else if( strcmp( p, "tickets" ) == 0 )
1077 {
1078 opt.tickets = atoi( q );
1079 if( opt.tickets < 0 || opt.tickets > 1 )
1080 goto usage;
1081 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001082 else if( strcmp( p, "ticket_timeout" ) == 0 )
1083 {
1084 opt.ticket_timeout = atoi( q );
1085 if( opt.ticket_timeout < 0 )
1086 goto usage;
1087 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001088 else if( strcmp( p, "cache_max" ) == 0 )
1089 {
1090 opt.cache_max = atoi( q );
1091 if( opt.cache_max < 0 )
1092 goto usage;
1093 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001094 else if( strcmp( p, "cache_timeout" ) == 0 )
1095 {
1096 opt.cache_timeout = atoi( q );
1097 if( opt.cache_timeout < 0 )
1098 goto usage;
1099 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001100 else if( strcmp( p, "cookies" ) == 0 )
1101 {
1102 opt.cookies = atoi( q );
1103 if( opt.cookies < -1 || opt.cookies > 1)
1104 goto usage;
1105 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001106 else if( strcmp( p, "anti_replay" ) == 0 )
1107 {
1108 opt.anti_replay = atoi( q );
1109 if( opt.anti_replay < 0 || opt.anti_replay > 1)
1110 goto usage;
1111 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001112 else if( strcmp( p, "badmac_limit" ) == 0 )
1113 {
1114 opt.badmac_limit = atoi( q );
1115 if( opt.badmac_limit < 0 )
1116 goto usage;
1117 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001118 else if( strcmp( p, "hs_timeout" ) == 0 )
1119 {
1120 if( ( p = strchr( q, '-' ) ) == NULL )
1121 goto usage;
1122 *p++ = '\0';
1123 opt.hs_to_min = atoi( q );
1124 opt.hs_to_max = atoi( p );
1125 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1126 goto usage;
1127 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001128 else if( strcmp( p, "sni" ) == 0 )
1129 {
1130 opt.sni = q;
1131 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001132 else
1133 goto usage;
1134 }
1135
Paul Bakkerc73079a2014-04-25 16:34:30 +02001136#if defined(POLARSSL_DEBUG_C)
1137 debug_set_threshold( opt.debug_level );
1138#endif
1139
Paul Bakkerc1516be2013-06-29 16:01:32 +02001140 if( opt.force_ciphersuite[0] > 0 )
1141 {
1142 const ssl_ciphersuite_t *ciphersuite_info;
1143 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1144
Paul Bakker5b55b792013-07-19 13:43:43 +02001145 if( opt.max_version != -1 &&
1146 ciphersuite_info->min_minor_ver > opt.max_version )
1147 {
1148 printf("forced ciphersuite not allowed with this protocol version\n");
1149 ret = 2;
1150 goto usage;
1151 }
1152 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001153 ciphersuite_info->max_minor_ver < opt.min_version )
1154 {
1155 printf("forced ciphersuite not allowed with this protocol version\n");
1156 ret = 2;
1157 goto usage;
1158 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001159
1160 /* If we select a version that's not supported by
1161 * this suite, then there will be no common ciphersuite... */
1162 if( opt.max_version == -1 ||
1163 opt.max_version > ciphersuite_info->max_minor_ver )
1164 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001165 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001166 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001167 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001168 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001169 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001170 /* DTLS starts with TLS 1.1 */
1171 if( opt.transport == SSL_TRANSPORT_DATAGRAM &&
1172 opt.min_version < SSL_MINOR_VERSION_2 )
1173 opt.min_version = SSL_MINOR_VERSION_2;
1174 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001175 }
1176
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001177 if( opt.version_suites != NULL )
1178 {
1179 const char *name[4] = { 0 };
1180
1181 /* Parse 4-element coma-separated list */
1182 for( i = 0, p = (char *) opt.version_suites;
1183 i < 4 && *p != '\0';
1184 i++ )
1185 {
1186 name[i] = p;
1187
1188 /* Terminate the current string and move on to next one */
1189 while( *p != ',' && *p != '\0' )
1190 p++;
1191 if( *p == ',' )
1192 *p++ = '\0';
1193 }
1194
1195 if( i != 4 )
1196 {
1197 printf( "too few values for version_suites\n" );
1198 ret = 1;
1199 goto exit;
1200 }
1201
1202 memset( version_suites, 0, sizeof( version_suites ) );
1203
1204 /* Get the suites identifiers from their name */
1205 for( i = 0; i < 4; i++ )
1206 {
1207 version_suites[i][0] = ssl_get_ciphersuite_id( name[i] );
1208
1209 if( version_suites[i][0] == 0 )
1210 {
1211 printf( "unknown ciphersuite: '%s'\n", name[i] );
1212 ret = 2;
1213 goto usage;
1214 }
1215 }
1216 }
1217
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001218#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001219 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001220 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001221 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001222 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001223 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001224 printf( "pre-shared key not valid hex\n" );
1225 goto exit;
1226 }
1227
1228 if( opt.psk_list != NULL )
1229 {
1230 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001231 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001232 printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001233 goto exit;
1234 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001235 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001236#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001237
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001238#if defined(POLARSSL_SSL_ALPN)
1239 if( opt.alpn_string != NULL )
1240 {
1241 p = (char *) opt.alpn_string;
1242 i = 0;
1243
1244 /* Leave room for a final NULL in alpn_list */
1245 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
1246 {
1247 alpn_list[i++] = p;
1248
1249 /* Terminate the current string and move on to next one */
1250 while( *p != ',' && *p != '\0' )
1251 p++;
1252 if( *p == ',' )
1253 *p++ = '\0';
1254 }
1255 }
1256#endif /* POLARSSL_SSL_ALPN */
1257
Paul Bakkerfbb17802013-04-17 19:10:21 +02001258 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001259 * 0. Initialize the RNG and the session data
1260 */
1261 printf( "\n . Seeding the random number generator..." );
1262 fflush( stdout );
1263
1264 entropy_init( &entropy );
1265 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001266 (const unsigned char *) pers,
1267 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001268 {
1269 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
1270 goto exit;
1271 }
1272
1273 printf( " ok\n" );
1274
Paul Bakker36713e82013-09-17 13:25:29 +02001275#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001276 /*
1277 * 1.1. Load the trusted CA
1278 */
1279 printf( " . Loading the CA root certificate ..." );
1280 fflush( stdout );
1281
1282#if defined(POLARSSL_FS_IO)
1283 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001284 if( strcmp( opt.ca_path, "none" ) == 0 )
1285 ret = 0;
1286 else
1287 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001288 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001289 if( strcmp( opt.ca_file, "none" ) == 0 )
1290 ret = 0;
1291 else
1292 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001293 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001294#endif
1295#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +02001296 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
1297 strlen( test_ca_list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001298#else
1299 {
1300 ret = 1;
1301 printf("POLARSSL_CERTS_C not defined.");
1302 }
1303#endif
1304 if( ret < 0 )
1305 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001306 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001307 goto exit;
1308 }
1309
1310 printf( " ok (%d skipped)\n", ret );
1311
1312 /*
1313 * 1.2. Load own certificate and private key
1314 */
1315 printf( " . Loading the server cert. and key..." );
1316 fflush( stdout );
1317
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001318#if defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001319 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001320 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001321 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001322 if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
1323 {
1324 printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n",
1325 -ret );
1326 goto exit;
1327 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001328 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001329 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001330 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001331 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001332 if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
1333 {
1334 printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret );
1335 goto exit;
1336 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001337 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001338 if( key_cert_init == 1 )
1339 {
1340 printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
1341 goto exit;
1342 }
1343
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001344 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001345 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001346 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001347 if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
1348 {
1349 printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n",
1350 -ret );
1351 goto exit;
1352 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001353 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001354 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001355 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001356 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001357 if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
1358 {
1359 printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n",
1360 -ret );
1361 goto exit;
1362 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001363 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001364 if( key_cert_init2 == 1 )
1365 {
1366 printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
1367 goto exit;
1368 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001369#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001370 if( key_cert_init == 0 &&
1371 strcmp( opt.crt_file, "none" ) != 0 &&
1372 strcmp( opt.key_file, "none" ) != 0 &&
1373 key_cert_init2 == 0 &&
1374 strcmp( opt.crt_file2, "none" ) != 0 &&
1375 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001376 {
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001377#if !defined(POLARSSL_CERTS_C)
1378 printf( "Not certificated or key provided, and \n"
1379 "POLARSSL_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001380 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001381#else
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001382#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001383 if( ( ret = x509_crt_parse( &srvcert,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001384 (const unsigned char *) test_srv_crt_rsa,
1385 strlen( test_srv_crt_rsa ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001386 {
1387 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
1388 goto exit;
1389 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001390 if( ( ret = pk_parse_key( &pkey,
1391 (const unsigned char *) test_srv_key_rsa,
1392 strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001393 {
1394 printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
1395 goto exit;
1396 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001397 key_cert_init = 2;
1398#endif /* POLARSSL_RSA_C */
1399#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001400 if( ( ret = x509_crt_parse( &srvcert2,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001401 (const unsigned char *) test_srv_crt_ec,
1402 strlen( test_srv_crt_ec ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001403 {
1404 printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
1405 goto exit;
1406 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001407 if( ( ret = pk_parse_key( &pkey2,
1408 (const unsigned char *) test_srv_key_ec,
1409 strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001410 {
1411 printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
1412 goto exit;
1413 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001414 key_cert_init2 = 2;
1415#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001416#endif /* POLARSSL_CERTS_C */
1417 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001418
1419 printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001420#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001421
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001422#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1423 if( opt.dhm_file != NULL )
1424 {
1425 printf( " . Loading DHM parameters..." );
1426 fflush( stdout );
1427
1428 if( ( ret = dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
1429 {
1430 printf( " failed\n ! dhm_parse_dhmfile returned -0x%04X\n\n",
1431 -ret );
1432 goto exit;
1433 }
1434
1435 printf( " ok\n" );
1436 }
1437#endif
1438
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001439#if defined(POLARSSL_SNI)
1440 if( opt.sni != NULL )
1441 {
1442 printf( " . Setting up SNI information..." );
1443 fflush( stdout );
1444
1445 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
1446 {
1447 printf( " failed\n" );
1448 goto exit;
1449 }
1450
1451 printf( " ok\n" );
1452 }
1453#endif /* POLARSSL_SNI */
1454
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001455 /*
1456 * 2. Setup the listening TCP socket
1457 */
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001458 printf( " . Bind on %s://%s:%-4d/ ...",
1459 opt.transport == SSL_TRANSPORT_STREAM ? "tcp" : "udp",
1460 opt.server_addr ? opt.server_addr : "*",
1461 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001462 fflush( stdout );
1463
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001464 if( ( ret = net_bind( &listen_fd, opt.server_addr, opt.server_port,
1465 opt.transport == SSL_TRANSPORT_STREAM ?
1466 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001467 {
1468 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1469 goto exit;
1470 }
1471
1472 printf( " ok\n" );
1473
1474 /*
1475 * 3. Setup stuff
1476 */
1477 printf( " . Setting up the SSL/TLS structure..." );
1478 fflush( stdout );
1479
1480 if( ( ret = ssl_init( &ssl ) ) != 0 )
1481 {
1482 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
1483 goto exit;
1484 }
1485
1486 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001487 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001488
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001489#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001490 if( ( ret = ssl_set_transport( &ssl, opt.transport ) ) != 0 )
1491 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001492 printf( " failed\n ! selected transport is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001493 goto exit;
1494 }
1495
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001496 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
1497 ssl_set_handshake_timeout( &ssl, opt.hs_to_min, opt.hs_to_max );
1498#endif /* POLARSSL_SSL_PROTO_DTLS */
1499
Paul Bakker05decb22013-08-15 13:33:48 +02001500#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001501 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1502 {
1503 printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
1504 goto exit;
1505 };
Paul Bakker05decb22013-08-15 13:33:48 +02001506#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001507
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001508#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1509 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
1510 ssl_set_truncated_hmac( &ssl, opt.trunc_hmac );
1511#endif
1512
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001513#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1514 if( opt.extended_ms != DFL_EXTENDED_MS )
1515 ssl_set_extended_master_secret( &ssl, opt.extended_ms );
1516#endif
1517
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001518#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1519 if( opt.etm != DFL_ETM )
1520 ssl_set_encrypt_then_mac( &ssl, opt.etm );
1521#endif
1522
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001523#if defined(POLARSSL_SSL_ALPN)
1524 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001525 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1526 {
1527 printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
1528 goto exit;
1529 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001530#endif
1531
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001532 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
1533 ssl_set_dbg( &ssl, my_debug, stdout );
1534
Paul Bakker0a597072012-09-25 21:55:46 +00001535#if defined(POLARSSL_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001536 if( opt.cache_max != -1 )
1537 ssl_cache_set_max_entries( &cache, opt.cache_max );
1538
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001539 if( opt.cache_timeout != -1 )
1540 ssl_cache_set_timeout( &cache, opt.cache_timeout );
1541
Paul Bakker0a597072012-09-25 21:55:46 +00001542 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
1543 ssl_cache_set, &cache );
1544#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001545
Paul Bakkera503a632013-08-14 13:48:06 +02001546#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001547 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1548 {
1549 printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
1550 goto exit;
1551 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001552
1553 if( opt.ticket_timeout != -1 )
1554 ssl_set_session_ticket_lifetime( &ssl, opt.ticket_timeout );
Paul Bakkera503a632013-08-14 13:48:06 +02001555#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001556
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001557#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001558 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001559 {
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001560#if defined(POLARSSL_SSL_COOKIE_C)
1561 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001562 {
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001563 if( ( ret = ssl_cookie_setup( &cookie_ctx,
1564 ctr_drbg_random, &ctr_drbg ) ) != 0 )
1565 {
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001566 printf( " failed\n ! ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001567 goto exit;
1568 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001569
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001570 ssl_set_dtls_cookies( &ssl, ssl_cookie_write, ssl_cookie_check,
1571 &cookie_ctx );
1572 }
1573 else
1574#endif /* POLARSSL_SSL_COOKIE_C */
1575#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
1576 if( opt.cookies == 0 )
1577 {
1578 ssl_set_dtls_cookies( &ssl, NULL, NULL, NULL );
1579 }
1580 else
1581#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
1582 {
1583 ; /* Nothing to do */
1584 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001585
1586#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1587 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001588 ssl_set_dtls_anti_replay( &ssl, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001589#endif
1590
1591#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
1592 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
1593 ssl_set_dtls_badmac_limit( &ssl, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001594#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001595 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001596#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001597
Paul Bakker41c83d32013-03-20 14:39:14 +01001598 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001599 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001600 else
1601 ssl_set_arc4_support( &ssl, opt.arc4 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001602
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001603 if( opt.version_suites != NULL )
1604 {
1605 ssl_set_ciphersuites_for_version( &ssl, version_suites[0],
1606 SSL_MAJOR_VERSION_3,
1607 SSL_MINOR_VERSION_0 );
1608 ssl_set_ciphersuites_for_version( &ssl, version_suites[1],
1609 SSL_MAJOR_VERSION_3,
1610 SSL_MINOR_VERSION_1 );
1611 ssl_set_ciphersuites_for_version( &ssl, version_suites[2],
1612 SSL_MAJOR_VERSION_3,
1613 SSL_MINOR_VERSION_2 );
1614 ssl_set_ciphersuites_for_version( &ssl, version_suites[3],
1615 SSL_MAJOR_VERSION_3,
1616 SSL_MINOR_VERSION_3 );
1617 }
1618
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001619 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
1620 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001621#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001622 ssl_set_renegotiation( &ssl, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001623
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001624 if( opt.renego_delay != DFL_RENEGO_DELAY )
1625 ssl_set_renegotiation_enforced( &ssl, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001626
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001627 if( opt.renego_period != DFL_RENEGO_PERIOD )
1628 {
1629 renego_period[7] = opt.renego_period;
1630 ssl_set_renegotiation_period( &ssl, renego_period );
1631 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001632#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001633
Paul Bakker36713e82013-09-17 13:25:29 +02001634#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001635 if( strcmp( opt.ca_path, "none" ) != 0 &&
1636 strcmp( opt.ca_file, "none" ) != 0 )
1637 {
1638 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
1639 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001640 if( key_cert_init )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001641 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
1642 {
1643 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1644 goto exit;
1645 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001646 if( key_cert_init2 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001647 if( ( ret = ssl_set_own_cert( &ssl, &srvcert2, &pkey2 ) ) != 0 )
1648 {
1649 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1650 goto exit;
1651 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001652#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001653
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001654#if defined(POLARSSL_SNI)
1655 if( opt.sni != NULL )
1656 ssl_set_sni( &ssl, sni_callback, sni_info );
1657#endif
1658
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001659#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001660 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
1661 {
1662 ret = ssl_set_psk( &ssl, psk, psk_len,
1663 (const unsigned char *) opt.psk_identity,
1664 strlen( opt.psk_identity ) );
1665 if( ret != 0 )
1666 {
1667 printf( " failed\n ssl_set_psk returned -0x%04X\n\n", - ret );
1668 goto exit;
1669 }
1670 }
1671
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001672 if( opt.psk_list != NULL )
1673 ssl_set_psk_cb( &ssl, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02001674#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001675
1676#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00001677 /*
1678 * Use different group than default DHM group
1679 */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001680#if defined(POLARSSL_FS_IO)
1681 if( opt.dhm_file != NULL )
1682 ret = ssl_set_dh_param_ctx( &ssl, &dhm );
1683 else
1684#endif
1685 ret = ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
1686 POLARSSL_DHM_RFC5114_MODP_2048_G );
1687
1688 if( ret != 0 )
1689 {
1690 printf( " failed\n ssl_set_dh_param returned -0x%04X\n\n", - ret );
1691 goto exit;
1692 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001693#endif
1694
Paul Bakker1d29fb52012-09-28 13:28:45 +00001695 if( opt.min_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001696 {
1697 ret = ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
Manuel Pégourié-Gonnard23eb74d2015-01-21 14:37:13 +00001698 if( ret != 0 && opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001699 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001700 printf( " failed\n ! selected min_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001701 goto exit;
1702 }
1703 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001704
Paul Bakkerc1516be2013-06-29 16:01:32 +02001705 if( opt.max_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001706 {
1707 ret = ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1708 if( ret != 0 )
1709 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001710 printf( " failed\n ! selected max_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001711 goto exit;
1712 }
1713 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001714
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001715 printf( " ok\n" );
1716
1717reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02001718#if !defined(_WIN32)
1719 if( received_sigterm )
1720 {
1721 printf( " interrupted by SIGTERM\n" );
1722 ret = 0;
1723 goto exit;
1724 }
1725#endif
1726
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001727#ifdef POLARSSL_ERROR_C
1728 if( ret != 0 )
1729 {
1730 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001731 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001732 printf("Last error was: %d - %s\n\n", ret, error_buf );
1733 }
1734#endif
1735
1736 if( client_fd != -1 )
Manuel Pégourié-Gonnard4ba6ab62014-08-07 17:21:47 +02001737 net_close( client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001738
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001739 ssl_session_reset( &ssl );
1740
1741 /*
1742 * 3. Wait until a client connects
1743 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001744 client_fd = -1;
1745
1746 printf( " . Waiting for a remote connection ..." );
1747 fflush( stdout );
1748
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001749 if( ( ret = net_accept( listen_fd, &client_fd, client_ip ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001750 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02001751#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001752 if( received_sigterm )
1753 {
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001754 printf( " interrupted by signal\n" );
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001755 ret = 0;
1756 goto exit;
1757 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02001758#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001759
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001760 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
1761 goto exit;
1762 }
1763
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001764 if( opt.nbio > 0 )
1765 ret = net_set_nonblock( client_fd );
1766 else
1767 ret = net_set_block( client_fd );
1768 if( ret != 0 )
1769 {
1770 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
1771 goto exit;
1772 }
1773
1774 if( opt.nbio == 2 )
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001775 ssl_set_bio_timeout( &ssl, &client_fd, my_send, my_recv, NULL, 0 );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001776 else
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001777 ssl_set_bio_timeout( &ssl, &client_fd, net_send, net_recv,
1778#if defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnardf0365122014-09-29 16:11:47 +02001779 opt.nbio == 0 ? net_recv_timeout : NULL,
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001780#else
1781 NULL,
1782#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001783 opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001784
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001785#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001786 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
1787 {
1788 if( ( ret = ssl_set_client_transport_id( &ssl, client_ip,
1789 sizeof( client_ip ) ) ) != 0 )
1790 {
1791 printf( " failed\n ! "
1792 "ssl_set_client_tranport_id() returned -0x%x\n\n", -ret );
1793 goto exit;
1794 }
1795 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001796#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001797
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001798 printf( " ok\n" );
1799
1800 /*
Manuel Pégourié-Gonnardbd97fdb2014-09-26 16:46:36 +02001801 * With UDP, bind_fd is hijacked by client_fd, so bind a new one
1802 */
1803#if defined(POLARSSL_SSL_PROTO_DTLS)
1804 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
1805 {
1806 printf( " . Re-bind on udp://%s:%-4d/ ...",
1807 opt.server_addr ? opt.server_addr : "*",
1808 opt.server_port );
1809 fflush( stdout );
1810
1811 if( ( ret = net_bind( &listen_fd, opt.server_addr,
1812 opt.server_port, NET_PROTO_UDP ) ) != 0 )
1813 {
1814 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1815 goto exit;
1816 }
1817
1818 printf( " ok\n" );
1819 }
1820#endif /* POLARSSL_SSL_PROTO_DTLS */
1821
1822 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001823 * 4. Handshake
1824 */
1825 printf( " . Performing the SSL/TLS handshake..." );
1826 fflush( stdout );
1827
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001828 do ret = ssl_handshake( &ssl );
1829 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1830 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001831
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001832 if( ret == POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED )
1833 {
1834 printf( " hello verification requested\n" );
1835 ret = 0;
1836 goto reset;
1837 }
1838 else if( ret != 0 )
1839 {
1840 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
1841 goto reset;
1842 }
1843 else /* ret == 0 */
1844 {
1845 printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1846 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
1847 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001848
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001849 if( ( ret = ssl_get_record_expansion( &ssl ) ) >= 0 )
1850 printf( " [ Record expansion is %d ]\n", ret );
1851 else
1852 printf( " [ Record expansion is unknown (compression) ]\n" );
1853
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001854#if defined(POLARSSL_SSL_ALPN)
1855 if( opt.alpn_string != NULL )
1856 {
1857 const char *alp = ssl_get_alpn_protocol( &ssl );
1858 printf( " [ Application Layer Protocol is %s ]\n",
1859 alp ? alp : "(none)" );
1860 }
1861#endif
1862
Paul Bakker36713e82013-09-17 13:25:29 +02001863#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001864 /*
1865 * 5. Verify the server certificate
1866 */
1867 printf( " . Verifying peer X.509 certificate..." );
1868
1869 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1870 {
1871 printf( " failed\n" );
1872
Paul Bakkerb0550d92012-10-30 07:51:03 +00001873 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001874 printf( " ! no client certificate sent\n" );
1875
1876 if( ( ret & BADCERT_EXPIRED ) != 0 )
1877 printf( " ! client certificate has expired\n" );
1878
1879 if( ( ret & BADCERT_REVOKED ) != 0 )
1880 printf( " ! client certificate has been revoked\n" );
1881
1882 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
1883 printf( " ! self-signed or not signed by a trusted CA\n" );
1884
1885 printf( "\n" );
1886 }
1887 else
1888 printf( " ok\n" );
1889
Paul Bakkerb0550d92012-10-30 07:51:03 +00001890 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001891 {
1892 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001893 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1894 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001895 printf( "%s\n", buf );
1896 }
Paul Bakker36713e82013-09-17 13:25:29 +02001897#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001898
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001899 if( opt.exchanges == 0 )
1900 goto close_notify;
1901
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001902 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001903data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001904 /*
1905 * 6. Read the HTTP Request
1906 */
1907 printf( " < Read from client:" );
1908 fflush( stdout );
1909
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001910 /*
1911 * TLS and DTLS need different reading styles (stream vs datagram)
1912 */
1913 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001914 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001915 do
1916 {
1917 int terminated = 0;
1918 len = sizeof( buf ) - 1;
1919 memset( buf, 0, sizeof( buf ) );
1920 ret = ssl_read( &ssl, buf, len );
1921
1922 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1923 ret == POLARSSL_ERR_NET_WANT_WRITE )
1924 continue;
1925
1926 if( ret <= 0 )
1927 {
1928 switch( ret )
1929 {
1930 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1931 printf( " connection was closed gracefully\n" );
1932 goto close_notify;
1933
1934 case 0:
1935 case POLARSSL_ERR_NET_CONN_RESET:
1936 printf( " connection was reset by peer\n" );
1937 ret = POLARSSL_ERR_NET_CONN_RESET;
1938 goto reset;
1939
1940 default:
1941 printf( " ssl_read returned -0x%x\n", -ret );
1942 goto reset;
1943 }
1944 }
1945
1946 if( ssl_get_bytes_avail( &ssl ) == 0 )
1947 {
1948 len = ret;
1949 buf[len] = '\0';
1950 printf( " %d bytes read\n\n%s\n", len, (char *) buf );
1951
1952 /* End of message should be detected according to the syntax of the
1953 * application protocol (eg HTTP), just use a dummy test here. */
1954 if( buf[len - 1] == '\n' )
1955 terminated = 1;
1956 }
1957 else
1958 {
1959 int extra_len, ori_len;
1960 unsigned char *larger_buf;
1961
1962 ori_len = ret;
1963 extra_len = ssl_get_bytes_avail( &ssl );
1964
1965 larger_buf = polarssl_malloc( ori_len + extra_len + 1 );
1966 if( larger_buf == NULL )
1967 {
1968 printf( " ! memory allocation failed\n" );
1969 ret = 1;
1970 goto reset;
1971 }
1972
1973 memset( larger_buf, 0, ori_len + extra_len );
1974 memcpy( larger_buf, buf, ori_len );
1975
1976 /* This read should never fail and get the whole cached data */
1977 ret = ssl_read( &ssl, larger_buf + ori_len, extra_len );
1978 if( ret != extra_len ||
1979 ssl_get_bytes_avail( &ssl ) != 0 )
1980 {
1981 printf( " ! ssl_read failed on cached data\n" );
1982 ret = 1;
1983 goto reset;
1984 }
1985
1986 larger_buf[ori_len + extra_len] = '\0';
1987 printf( " %u bytes read (%u + %u)\n\n%s\n",
1988 ori_len + extra_len, ori_len, extra_len,
1989 (char *) larger_buf );
1990
1991 /* End of message should be detected according to the syntax of the
1992 * application protocol (eg HTTP), just use a dummy test here. */
1993 if( larger_buf[ori_len + extra_len - 1] == '\n' )
1994 terminated = 1;
1995
1996 polarssl_free( larger_buf );
1997 }
1998
1999 if( terminated )
2000 {
2001 ret = 0;
2002 break;
2003 }
2004 }
2005 while( 1 );
2006 }
2007 else /* Not stream, so datagram */
2008 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002009 len = sizeof( buf ) - 1;
2010 memset( buf, 0, sizeof( buf ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002011
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002012 do ret = ssl_read( &ssl, buf, len );
2013 while( ret == POLARSSL_ERR_NET_WANT_READ ||
2014 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002015
2016 if( ret <= 0 )
2017 {
2018 switch( ret )
2019 {
2020 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
2021 printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002022 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002023 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002024
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002025 default:
2026 printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002027 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002028 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002029 }
2030
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02002031 len = ret;
2032 buf[len] = '\0';
2033 printf( " %d bytes read\n\n%s", len, (char *) buf );
2034 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002035 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002036
2037 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002038 * 7a. Request renegotiation while client is waiting for input from us.
Manuel Pégourié-Gonnarda6ace042014-10-15 12:44:41 +02002039 * (only on the first exchange, to be able to test retransmission)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002040 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002041#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00002042 if( opt.renegotiate && exchanges_left == opt.exchanges )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002043 {
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002044 printf( " . Requestion renegotiation..." );
2045 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002046
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002047 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
2048 {
2049 if( ret != POLARSSL_ERR_NET_WANT_READ &&
2050 ret != POLARSSL_ERR_NET_WANT_WRITE )
2051 {
2052 printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
2053 goto reset;
2054 }
2055 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002056
2057 printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002058 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002059#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02002060
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002061 /*
2062 * 7. Write the 200 Response
2063 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002064 printf( " > Write to client:" );
2065 fflush( stdout );
2066
2067 len = sprintf( (char *) buf, HTTP_RESPONSE,
2068 ssl_get_ciphersuite( &ssl ) );
2069
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002070 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002071 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002072 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002073 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002074 while( ( ret = ssl_write( &ssl, buf + written, len - written ) )
2075 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002076 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002077 if( ret == POLARSSL_ERR_NET_CONN_RESET )
2078 {
2079 printf( " failed\n ! peer closed the connection\n\n" );
2080 goto reset;
2081 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002082
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002083 if( ret != POLARSSL_ERR_NET_WANT_READ &&
2084 ret != POLARSSL_ERR_NET_WANT_WRITE )
2085 {
2086 printf( " failed\n ! ssl_write returned %d\n\n", ret );
2087 goto reset;
2088 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002089 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002090 }
2091 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02002092 else /* Not stream, so datagram */
2093 {
2094 do ret = ssl_write( &ssl, buf, len );
2095 while( ret == POLARSSL_ERR_NET_WANT_READ ||
2096 ret == POLARSSL_ERR_NET_WANT_WRITE );
2097
2098 if( ret < 0 )
2099 {
2100 printf( " failed\n ! ssl_write returned %d\n\n", ret );
2101 goto reset;
2102 }
2103
2104 frags = 1;
2105 written = ret;
2106 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002107
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02002108 buf[written] = '\0';
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02002109 printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Manuel Pégourié-Gonnarda92ed482015-01-14 10:46:08 +01002110 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002111
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002112 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002113 * 7b. Continue doing data exchanges?
2114 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00002115 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02002116 goto data_exchange;
2117
2118 /*
2119 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002120 */
2121close_notify:
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01002122 printf( " . Closing the connection..." );
2123
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002124 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard34377b12015-01-22 10:46:46 +00002125 do ret = ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002126 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
2127 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002128
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002129 printf( " done\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002130 goto reset;
2131
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002132 /*
2133 * Cleanup and exit
2134 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002135exit:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002136#ifdef POLARSSL_ERROR_C
2137 if( ret != 0 )
2138 {
2139 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02002140 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002141 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
2142 }
2143#endif
2144
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01002145 printf( " . Cleaning up..." );
2146 fflush( stdout );
2147
Paul Bakker0c226102014-04-17 16:02:36 +02002148 if( client_fd != -1 )
2149 net_close( client_fd );
2150
Paul Bakkera317a982014-06-18 16:44:11 +02002151#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
2152 dhm_free( &dhm );
2153#endif
Paul Bakker36713e82013-09-17 13:25:29 +02002154#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker36713e82013-09-17 13:25:29 +02002155 x509_crt_free( &cacert );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002156 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02002157 pk_free( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002158 x509_crt_free( &srvcert2 );
2159 pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02002160#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002161#if defined(POLARSSL_SNI)
2162 sni_free( sni_info );
2163#endif
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02002164#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
2165 psk_free( psk_info );
2166#endif
2167#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
2168 dhm_free( &dhm );
2169#endif
Paul Bakkered27a042013-04-18 22:46:23 +02002170
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002171 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02002172 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02002173 entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002174
Paul Bakker0a597072012-09-25 21:55:46 +00002175#if defined(POLARSSL_SSL_CACHE_C)
2176 ssl_cache_free( &cache );
2177#endif
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +02002178#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002179 ssl_cookie_free( &cookie_ctx );
2180#endif
Paul Bakker0a597072012-09-25 21:55:46 +00002181
Paul Bakker1337aff2013-09-29 14:45:34 +02002182#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
2183#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker82024bf2013-07-04 11:52:32 +02002184 memory_buffer_alloc_status();
2185#endif
Paul Bakker1337aff2013-09-29 14:45:34 +02002186 memory_buffer_alloc_free();
2187#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02002188
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01002189 printf( " done.\n" );
2190
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002191#if defined(_WIN32)
2192 printf( " + Press Enter to exit this program.\n" );
2193 fflush( stdout ); getchar();
2194#endif
2195
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002196 // Shell can not handle large exit numbers -> 1 for errors
2197 if( ret < 0 )
2198 ret = 1;
2199
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002200 return( ret );
2201}
2202#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
2203 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
2204 POLARSSL_CTR_DRBG_C */