blob: b02b1765eec18710321b8abfd8fa507aefd27cc4 [file] [log] [blame]
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001/*
2 * SSL client with options
3 *
Paul Bakker3c5ef712013-06-25 16:37:45 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000031
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020032#if !defined(POLARSSL_ENTROPY_C) || \
33 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
34 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
35#include <stdio.h>
36int main( int argc, char *argv[] )
37{
38 ((void) argc);
39 ((void) argv);
40
41 printf("POLARSSL_ENTROPY_C and/or "
42 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
43 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
44 return( 0 );
45}
46#else
47
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010048#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) && defined(POLARSSL_FS_IO)
49#define POLARSSL_SNI
50#endif
51
52#if defined(POLARSSL_PLATFORM_C)
53#include "polarssl/platform.h"
54#else
55#define polarssl_malloc malloc
56#define polarssl_free free
57#endif
58
Paul Bakkerb60b95f2012-09-25 09:05:17 +000059#if defined(_WIN32)
60#include <windows.h>
61#endif
62
63#include <string.h>
64#include <stdlib.h>
65#include <stdio.h>
Paul Bakkerc1283d32014-08-18 11:05:51 +020066
67#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +020068#include <signal.h>
Paul Bakkerc1283d32014-08-18 11:05:51 +020069#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000070
Paul Bakkerb60b95f2012-09-25 09:05:17 +000071#include "polarssl/net.h"
72#include "polarssl/ssl.h"
73#include "polarssl/entropy.h"
74#include "polarssl/ctr_drbg.h"
75#include "polarssl/certs.h"
76#include "polarssl/x509.h"
77#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020078#include "polarssl/debug.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000079
Paul Bakker0a597072012-09-25 21:55:46 +000080#if defined(POLARSSL_SSL_CACHE_C)
81#include "polarssl/ssl_cache.h"
82#endif
83
Paul Bakker82024bf2013-07-04 11:52:32 +020084#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
85#include "polarssl/memory.h"
86#endif
87
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +010088#define DFL_SERVER_ADDR NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000089#define DFL_SERVER_PORT 4433
Paul Bakkerb60b95f2012-09-25 09:05:17 +000090#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010091#define DFL_NBIO 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +000092#define DFL_CA_FILE ""
93#define DFL_CA_PATH ""
94#define DFL_CRT_FILE ""
95#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +020096#define DFL_CRT_FILE2 ""
97#define DFL_KEY_FILE2 ""
Paul Bakkerfbb17802013-04-17 19:10:21 +020098#define DFL_PSK ""
99#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200100#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000101#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200102#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +0200103#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100104#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100105#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200106#define DFL_RENEGO_DELAY -2
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100107#define DFL_RENEGO_PERIOD -1
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200108#define DFL_EXCHANGES 1
Paul Bakker1d29fb52012-09-28 13:28:45 +0000109#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200110#define DFL_MAX_VERSION -1
Paul Bakker91ebfb52012-11-23 14:04:08 +0100111#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200112#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200113#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100114#define DFL_TICKET_TIMEOUT -1
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100115#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100116#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100117#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200118#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200119#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200120#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100121#define DFL_ETM -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000122
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200123#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
124 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
125 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
126 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
127 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
128 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
129 "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 +0200130
Paul Bakker8e714d72013-07-18 11:05:13 +0200131/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
132 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000133#define HTTP_RESPONSE \
134 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
135 "<h2>PolarSSL Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200136 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000137
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200138/*
139 * Size of the basic I/O buffer. Able to hold our default response.
140 *
141 * You will need to adapt the ssl_get_bytes_avail() test in ssl-opt.sh
142 * if you change this value to something outside the range <= 100 or > 500
143 */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200144#define IO_BUF_LEN 200
145
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000146/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000147 * global options
148 */
149struct options
150{
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100151 const char *server_addr; /* address on which the ssl service runs */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000152 int server_port; /* port on which the ssl service runs */
153 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100154 int nbio; /* should I/O be blocking? */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200155 const char *ca_file; /* the file with the CA certificate(s) */
156 const char *ca_path; /* the path with the CA certificate(s) reside */
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200157 const char *crt_file; /* the file with the server certificate */
158 const char *key_file; /* the file with the server key */
159 const char *crt_file2; /* the file with the 2nd server certificate */
160 const char *key_file2; /* the file with the 2nd server key */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200161 const char *psk; /* the pre-shared key */
162 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200163 char *psk_list; /* list of PSK id/key pairs for callback */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000164 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200165 const char *version_suites; /* per-version ciphersuites */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000166 int renegotiation; /* enable / disable renegotiation */
167 int allow_legacy; /* allow legacy renegotiation */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100168 int renegotiate; /* attempt renegotiation? */
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200169 int renego_delay; /* delay before enforcing renegotiation */
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100170 int renego_period; /* period for automatic renegotiation */
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200171 int exchanges; /* number of data exchanges */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000172 int min_version; /* minimum protocol version accepted */
Paul Bakkerc1516be2013-06-29 16:01:32 +0200173 int max_version; /* maximum protocol version accepted */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100174 int auth_mode; /* verify mode for connection */
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200175 unsigned char mfl_code; /* code for maximum fragment length */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200176 int tickets; /* enable / disable session tickets */
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100177 int ticket_timeout; /* session ticket lifetime */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100178 int cache_max; /* max number of session cache entries */
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100179 int cache_timeout; /* expiration delay of session cache entries */
Paul Bakker1ebc0c52014-05-22 15:47:58 +0200180 char *sni; /* string describing sni information */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200181 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200182 const char *dhm_file; /* the file with the DH parameters */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200183 char extended_ms; /* allow negotiation of extended MS? */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100184 char etm; /* allow negotiation of encrypt-then-MAC? */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000185} opt;
186
Paul Bakker3c5ef712013-06-25 16:37:45 +0200187static void my_debug( void *ctx, int level, const char *str )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000188{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200189 ((void) level);
190
191 fprintf( (FILE *) ctx, "%s", str );
192 fflush( (FILE *) ctx );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000193}
194
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100195/*
196 * Test recv/send functions that make sure each try returns
197 * WANT_READ/WANT_WRITE at least once before sucesseding
198 */
199static int my_recv( void *ctx, unsigned char *buf, size_t len )
200{
201 static int first_try = 1;
202 int ret;
203
204 if( first_try )
205 {
206 first_try = 0;
207 return( POLARSSL_ERR_NET_WANT_READ );
208 }
209
210 ret = net_recv( ctx, buf, len );
211 if( ret != POLARSSL_ERR_NET_WANT_READ )
212 first_try = 1; /* Next call will be a new operation */
213 return( ret );
214}
215
216static int my_send( void *ctx, const unsigned char *buf, size_t len )
217{
218 static int first_try = 1;
219 int ret;
220
221 if( first_try )
222 {
223 first_try = 0;
224 return( POLARSSL_ERR_NET_WANT_WRITE );
225 }
226
227 ret = net_send( ctx, buf, len );
228 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
229 first_try = 1; /* Next call will be a new operation */
230 return( ret );
231}
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200232
233#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000234#if defined(POLARSSL_FS_IO)
235#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100236 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
237 " default: \"\" (pre-loaded)\n" \
238 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
239 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
240 " 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 +0200241 " default: see note after key_file2\n" \
242 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200243 " 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 +0200244 " default: see note after key_file2\n" \
245 " key_file2=%%s default: see note below\n" \
246 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200247 " preloaded certificate(s) and key(s) are used if available\n" \
248 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
249 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000250#else
251#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200252 "\n" \
253 " No file operations available (POLARSSL_FS_IO not defined)\n" \
254 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000255#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200256#else
257#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200258#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200259
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200260#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200261#define USAGE_PSK \
262 " psk=%%s default: \"\" (in hex, without 0x)\n" \
263 " psk_identity=%%s default: \"Client_identity\"\n"
264#else
265#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200266#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000267
Paul Bakkera503a632013-08-14 13:48:06 +0200268#if defined(POLARSSL_SSL_SESSION_TICKETS)
269#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100270 " tickets=%%d default: 1 (enabled)\n" \
271 " ticket_timeout=%%d default: ticket default (1d)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200272#else
273#define USAGE_TICKETS ""
274#endif /* POLARSSL_SSL_SESSION_TICKETS */
275
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100276#if defined(POLARSSL_SSL_CACHE_C)
277#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100278 " cache_max=%%d default: cache default (50)\n" \
279 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100280#else
281#define USAGE_CACHE ""
282#endif /* POLARSSL_SSL_CACHE_C */
283
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100284#if defined(POLARSSL_SNI)
285#define USAGE_SNI \
286 " sni=%%s name1,cert1,key1[,name2,cert2,key2[,...]]\n" \
287 " default: disabled\n"
288#else
289#define USAGE_SNI ""
290#endif /* POLARSSL_SNI */
291
Paul Bakker05decb22013-08-15 13:33:48 +0200292#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
293#define USAGE_MAX_FRAG_LEN \
294 " max_frag_len=%%d default: 16384 (tls default)\n" \
295 " options: 512, 1024, 2048, 4096\n"
296#else
297#define USAGE_MAX_FRAG_LEN ""
298#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
299
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200300#if defined(POLARSSL_SSL_ALPN)
301#define USAGE_ALPN \
302 " alpn=%%s default: \"\" (disabled)\n" \
303 " example: spdy/1,http/1.1\n"
304#else
305#define USAGE_ALPN ""
306#endif /* POLARSSL_SSL_ALPN */
307
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200308#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
309#define USAGE_EMS \
310 " extended_ms=0/1 default: (library default: on)\n"
311#else
312#define USAGE_EMS ""
313#endif
314
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100315#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
316#define USAGE_ETM \
317 " etm=0/1 default: (library default: on)\n"
318#else
319#define USAGE_ETM ""
320#endif
321
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100322#if defined(POLARSSL_SSL_RENEGOTIATION)
323#define USAGE_RENEGO \
324 " renegotiation=%%d default: 0 (disabled)\n" \
325 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100326 " renego_delay=%%d default: -2 (library default)\n" \
327 " renego_period=%%d default: (library default)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100328#else
329#define USAGE_RENEGO ""
330#endif
331
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000332#define USAGE \
333 "\n usage: ssl_server2 param=<>...\n" \
334 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100335 " server_addr=%%d default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000336 " server_port=%%d default: 4433\n" \
337 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100338 " nbio=%%d default: 0 (blocking I/O)\n" \
339 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100340 "\n" \
341 " auth_mode=%%s default: \"optional\"\n" \
342 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000343 USAGE_IO \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100344 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100345 "\n" \
346 USAGE_PSK \
347 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100348 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100349 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200350 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100351 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100352 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100353 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200354 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200355 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100356 USAGE_ETM \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100357 "\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000358 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200359 " max_version=%%s default: \"tls1_2\"\n" \
360 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200361 " options: ssl3, tls1, tls1_1, tls1_2\n" \
362 "\n" \
363 " version_suites=a,b,c,d per-version ciphersuites\n" \
364 " in order from ssl3 to tls1_2\n" \
365 " default: all enabled\n" \
366 " force_ciphersuite=<name> default: all enabled\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000367 " acceptable ciphersuite names:\n"
368
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200369/*
370 * Used by sni_parse and psk_parse to handle coma-separated lists
371 */
372#define GET_ITEM( dst ) \
373 dst = p; \
374 while( *p != ',' ) \
375 if( ++p > end ) \
376 return( NULL ); \
377 *p++ = '\0';
378
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100379#if defined(POLARSSL_SNI)
380typedef struct _sni_entry sni_entry;
381
382struct _sni_entry {
383 const char *name;
384 x509_crt *cert;
385 pk_context *key;
386 sni_entry *next;
387};
388
389/*
390 * Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]]
391 * into a usable sni_entry list.
392 *
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200393 * Modifies the input string! This is not production quality!
394 * (leaks memory if parsing fails, no error reporting, ...)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100395 */
396sni_entry *sni_parse( char *sni_string )
397{
398 sni_entry *cur = NULL, *new = NULL;
399 char *p = sni_string;
400 char *end = p;
401 char *crt_file, *key_file;
402
403 while( *end != '\0' )
404 ++end;
405 *end = ',';
406
407 while( p <= end )
408 {
409 if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL )
410 return( NULL );
411
412 memset( new, 0, sizeof( sni_entry ) );
413
414 if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ||
415 ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200416 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100417
418 x509_crt_init( new->cert );
419 pk_init( new->key );
420
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200421 GET_ITEM( new->name );
422 GET_ITEM( crt_file );
423 GET_ITEM( key_file );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100424
425 if( x509_crt_parse_file( new->cert, crt_file ) != 0 ||
426 pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200427 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100428
429 new->next = cur;
430 cur = new;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100431 }
432
433 return( cur );
434}
435
436void sni_free( sni_entry *head )
437{
438 sni_entry *cur = head, *next;
439
440 while( cur != NULL )
441 {
442 x509_crt_free( cur->cert );
443 polarssl_free( cur->cert );
444
445 pk_free( cur->key );
446 polarssl_free( cur->key );
447
448 next = cur->next;
449 polarssl_free( cur );
450 cur = next;
451 }
452}
453
454/*
455 * SNI callback.
456 */
457int sni_callback( void *p_info, ssl_context *ssl,
458 const unsigned char *name, size_t name_len )
459{
460 sni_entry *cur = (sni_entry *) p_info;
461
462 while( cur != NULL )
463 {
464 if( name_len == strlen( cur->name ) &&
465 memcmp( name, cur->name, name_len ) == 0 )
466 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200467 return( ssl_set_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100468 }
469
470 cur = cur->next;
471 }
472
473 return( -1 );
474}
475
476#endif /* POLARSSL_SNI */
477
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200478#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
479
480#define HEX2NUM( c ) \
481 if( c >= '0' && c <= '9' ) \
482 c -= '0'; \
483 else if( c >= 'a' && c <= 'f' ) \
484 c -= 'a' - 10; \
485 else if( c >= 'A' && c <= 'F' ) \
486 c -= 'A' - 10; \
487 else \
488 return( -1 );
489
490/*
491 * Convert a hex string to bytes.
492 * Return 0 on success, -1 on error.
493 */
494int unhexify( unsigned char *output, const char *input, size_t *olen )
495{
496 unsigned char c;
497 size_t j;
498
499 *olen = strlen( input );
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200500 if( *olen % 2 != 0 || *olen / 2 > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200501 return( -1 );
502 *olen /= 2;
503
504 for( j = 0; j < *olen * 2; j += 2 )
505 {
506 c = input[j];
507 HEX2NUM( c );
508 output[ j / 2 ] = c << 4;
509
510 c = input[j + 1];
511 HEX2NUM( c );
512 output[ j / 2 ] |= c;
513 }
514
515 return( 0 );
516}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200517
518typedef struct _psk_entry psk_entry;
519
520struct _psk_entry
521{
522 const char *name;
523 size_t key_len;
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200524 unsigned char key[POLARSSL_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200525 psk_entry *next;
526};
527
528/*
529 * Parse a string of pairs name1,key1[,name2,key2[,...]]
530 * into a usable psk_entry list.
531 *
532 * Modifies the input string! This is not production quality!
533 * (leaks memory if parsing fails, no error reporting, ...)
534 */
535psk_entry *psk_parse( char *psk_string )
536{
537 psk_entry *cur = NULL, *new = NULL;
538 char *p = psk_string;
539 char *end = p;
540 char *key_hex;
541
542 while( *end != '\0' )
543 ++end;
544 *end = ',';
545
546 while( p <= end )
547 {
548 if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL )
549 return( NULL );
550
551 memset( new, 0, sizeof( psk_entry ) );
552
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200553 GET_ITEM( new->name );
554 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200555
556 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
557 return( NULL );
558
559 new->next = cur;
560 cur = new;
561 }
562
563 return( cur );
564}
565
566/*
567 * Free a list of psk_entry's
568 */
569void psk_free( psk_entry *head )
570{
571 psk_entry *next;
572
573 while( head != NULL )
574 {
575 next = head->next;
576 polarssl_free( head );
577 head = next;
578 }
579}
580
581/*
582 * PSK callback
583 */
584int psk_callback( void *p_info, ssl_context *ssl,
585 const unsigned char *name, size_t name_len )
586{
587 psk_entry *cur = (psk_entry *) p_info;
588
589 while( cur != NULL )
590 {
591 if( name_len == strlen( cur->name ) &&
592 memcmp( name, cur->name, name_len ) == 0 )
593 {
594 return( ssl_set_psk( ssl, cur->key, cur->key_len,
595 name, name_len ) );
596 }
597
598 cur = cur->next;
599 }
600
601 return( -1 );
602}
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200603#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
604
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +0200605static int listen_fd, client_fd = -1;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200606
607/* Interruption handler to ensure clean exit (for valgrind testing) */
608#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200609static int received_sigterm = 0;
610void term_handler( int sig )
611{
612 ((void) sig);
613 received_sigterm = 1;
614 net_close( listen_fd ); /* causes net_accept() to abort */
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +0200615 net_close( client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200616}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200617#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200618
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000619int main( int argc, char *argv[] )
620{
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200621 int ret = 0, len, written, frags, exchanges;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200622 int version_suites[4][2];
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200623 unsigned char buf[IO_BUF_LEN];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200624#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200625 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +0200626 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +0200627 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +0200628#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200629 const char *pers = "ssl_server2";
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000630
631 entropy_context entropy;
632 ctr_drbg_context ctr_drbg;
633 ssl_context ssl;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100634#if defined(POLARSSL_SSL_RENEGOTIATION)
635 unsigned char renego_period[8] = { 0 };
636#endif
Paul Bakker36713e82013-09-17 13:25:29 +0200637#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200638 x509_crt cacert;
639 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200640 pk_context pkey;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200641 x509_crt srvcert2;
642 pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200643 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200644#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200645#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
646 dhm_context dhm;
647#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000648#if defined(POLARSSL_SSL_CACHE_C)
649 ssl_cache_context cache;
650#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100651#if defined(POLARSSL_SNI)
652 sni_entry *sni_info = NULL;
653#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200654#if defined(POLARSSL_SSL_ALPN)
655 const char *alpn_list[10];
656#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200657#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
658 unsigned char alloc_buf[100000];
659#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000660
661 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000662 char *p, *q;
663 const int *list;
664
Paul Bakker82024bf2013-07-04 11:52:32 +0200665#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
666 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
667#endif
668
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000669 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200670 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000671 */
672 listen_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200673 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200674#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200675 x509_crt_init( &cacert );
676 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200677 pk_init( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200678 x509_crt_init( &srvcert2 );
679 pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200680#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200681#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
Paul Bakkera317a982014-06-18 16:44:11 +0200682 dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200683#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000684#if defined(POLARSSL_SSL_CACHE_C)
685 ssl_cache_init( &cache );
686#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200687#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200688 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200689#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000690
Paul Bakkerc1283d32014-08-18 11:05:51 +0200691#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100692 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200693 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100694 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +0200695#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200696
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000697 if( argc == 0 )
698 {
699 usage:
700 if( ret == 0 )
701 ret = 1;
702
703 printf( USAGE );
704
705 list = ssl_list_ciphersuites();
706 while( *list )
707 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200708 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200709 list++;
710 if( !*list )
711 break;
712 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000713 list++;
714 }
715 printf("\n");
716 goto exit;
717 }
718
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100719 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000720 opt.server_port = DFL_SERVER_PORT;
721 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100722 opt.nbio = DFL_NBIO;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000723 opt.ca_file = DFL_CA_FILE;
724 opt.ca_path = DFL_CA_PATH;
725 opt.crt_file = DFL_CRT_FILE;
726 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200727 opt.crt_file2 = DFL_CRT_FILE2;
728 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200729 opt.psk = DFL_PSK;
730 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200731 opt.psk_list = DFL_PSK_LIST;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000732 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200733 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000734 opt.renegotiation = DFL_RENEGOTIATION;
735 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100736 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200737 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100738 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200739 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000740 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200741 opt.max_version = DFL_MAX_VERSION;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100742 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200743 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200744 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100745 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100746 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100747 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100748 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200749 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200750 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200751 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100752 opt.etm = DFL_ETM;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000753
754 for( i = 1; i < argc; i++ )
755 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000756 p = argv[i];
757 if( ( q = strchr( p, '=' ) ) == NULL )
758 goto usage;
759 *q++ = '\0';
760
761 if( strcmp( p, "server_port" ) == 0 )
762 {
763 opt.server_port = atoi( q );
764 if( opt.server_port < 1 || opt.server_port > 65535 )
765 goto usage;
766 }
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100767 else if( strcmp( p, "server_addr" ) == 0 )
768 opt.server_addr = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000769 else if( strcmp( p, "debug_level" ) == 0 )
770 {
771 opt.debug_level = atoi( q );
772 if( opt.debug_level < 0 || opt.debug_level > 65535 )
773 goto usage;
774 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100775 else if( strcmp( p, "nbio" ) == 0 )
776 {
777 opt.nbio = atoi( q );
778 if( opt.nbio < 0 || opt.nbio > 2 )
779 goto usage;
780 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000781 else if( strcmp( p, "ca_file" ) == 0 )
782 opt.ca_file = q;
783 else if( strcmp( p, "ca_path" ) == 0 )
784 opt.ca_path = q;
785 else if( strcmp( p, "crt_file" ) == 0 )
786 opt.crt_file = q;
787 else if( strcmp( p, "key_file" ) == 0 )
788 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200789 else if( strcmp( p, "crt_file2" ) == 0 )
790 opt.crt_file2 = q;
791 else if( strcmp( p, "key_file2" ) == 0 )
792 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200793 else if( strcmp( p, "dhm_file" ) == 0 )
794 opt.dhm_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200795 else if( strcmp( p, "psk" ) == 0 )
796 opt.psk = q;
797 else if( strcmp( p, "psk_identity" ) == 0 )
798 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200799 else if( strcmp( p, "psk_list" ) == 0 )
800 opt.psk_list = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000801 else if( strcmp( p, "force_ciphersuite" ) == 0 )
802 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000803 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
804
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200805 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000806 {
807 ret = 2;
808 goto usage;
809 }
810 opt.force_ciphersuite[1] = 0;
811 }
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200812 else if( strcmp( p, "version_suites" ) == 0 )
813 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000814 else if( strcmp( p, "renegotiation" ) == 0 )
815 {
816 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
817 SSL_RENEGOTIATION_DISABLED;
818 }
819 else if( strcmp( p, "allow_legacy" ) == 0 )
820 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100821 switch( atoi( q ) )
822 {
823 case -1: opt.allow_legacy = SSL_LEGACY_BREAK_HANDSHAKE; break;
824 case 0: opt.allow_legacy = SSL_LEGACY_NO_RENEGOTIATION; break;
825 case 1: opt.allow_legacy = SSL_LEGACY_ALLOW_RENEGOTIATION; break;
826 default: goto usage;
827 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000828 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100829 else if( strcmp( p, "renegotiate" ) == 0 )
830 {
831 opt.renegotiate = atoi( q );
832 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
833 goto usage;
834 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200835 else if( strcmp( p, "renego_delay" ) == 0 )
836 {
837 opt.renego_delay = atoi( q );
838 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100839 else if( strcmp( p, "renego_period" ) == 0 )
840 {
841 opt.renego_period = atoi( q );
842 if( opt.renego_period < 2 || opt.renego_period > 255 )
843 goto usage;
844 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200845 else if( strcmp( p, "exchanges" ) == 0 )
846 {
847 opt.exchanges = atoi( q );
848 if( opt.exchanges < 1 )
849 goto usage;
850 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000851 else if( strcmp( p, "min_version" ) == 0 )
852 {
853 if( strcmp( q, "ssl3" ) == 0 )
854 opt.min_version = SSL_MINOR_VERSION_0;
855 else if( strcmp( q, "tls1" ) == 0 )
856 opt.min_version = SSL_MINOR_VERSION_1;
857 else if( strcmp( q, "tls1_1" ) == 0 )
858 opt.min_version = SSL_MINOR_VERSION_2;
859 else if( strcmp( q, "tls1_2" ) == 0 )
860 opt.min_version = SSL_MINOR_VERSION_3;
861 else
862 goto usage;
863 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200864 else if( strcmp( p, "max_version" ) == 0 )
865 {
866 if( strcmp( q, "ssl3" ) == 0 )
867 opt.max_version = SSL_MINOR_VERSION_0;
868 else if( strcmp( q, "tls1" ) == 0 )
869 opt.max_version = SSL_MINOR_VERSION_1;
870 else if( strcmp( q, "tls1_1" ) == 0 )
871 opt.max_version = SSL_MINOR_VERSION_2;
872 else if( strcmp( q, "tls1_2" ) == 0 )
873 opt.max_version = SSL_MINOR_VERSION_3;
874 else
875 goto usage;
876 }
877 else if( strcmp( p, "force_version" ) == 0 )
878 {
879 if( strcmp( q, "ssl3" ) == 0 )
880 {
881 opt.min_version = SSL_MINOR_VERSION_0;
882 opt.max_version = SSL_MINOR_VERSION_0;
883 }
884 else if( strcmp( q, "tls1" ) == 0 )
885 {
886 opt.min_version = SSL_MINOR_VERSION_1;
887 opt.max_version = SSL_MINOR_VERSION_1;
888 }
889 else if( strcmp( q, "tls1_1" ) == 0 )
890 {
891 opt.min_version = SSL_MINOR_VERSION_2;
892 opt.max_version = SSL_MINOR_VERSION_2;
893 }
894 else if( strcmp( q, "tls1_2" ) == 0 )
895 {
896 opt.min_version = SSL_MINOR_VERSION_3;
897 opt.max_version = SSL_MINOR_VERSION_3;
898 }
899 else
900 goto usage;
901 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100902 else if( strcmp( p, "auth_mode" ) == 0 )
903 {
904 if( strcmp( q, "none" ) == 0 )
905 opt.auth_mode = SSL_VERIFY_NONE;
906 else if( strcmp( q, "optional" ) == 0 )
907 opt.auth_mode = SSL_VERIFY_OPTIONAL;
908 else if( strcmp( q, "required" ) == 0 )
909 opt.auth_mode = SSL_VERIFY_REQUIRED;
910 else
911 goto usage;
912 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200913 else if( strcmp( p, "max_frag_len" ) == 0 )
914 {
915 if( strcmp( q, "512" ) == 0 )
916 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
917 else if( strcmp( q, "1024" ) == 0 )
918 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
919 else if( strcmp( q, "2048" ) == 0 )
920 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
921 else if( strcmp( q, "4096" ) == 0 )
922 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
923 else
924 goto usage;
925 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200926 else if( strcmp( p, "alpn" ) == 0 )
927 {
928 opt.alpn_string = q;
929 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200930 else if( strcmp( p, "extended_ms" ) == 0 )
931 {
932 switch( atoi( q ) )
933 {
934 case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break;
935 case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break;
936 default: goto usage;
937 }
938 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100939 else if( strcmp( p, "etm" ) == 0 )
940 {
941 switch( atoi( q ) )
942 {
943 case 0: opt.etm = SSL_ETM_DISABLED; break;
944 case 1: opt.etm = SSL_ETM_ENABLED; break;
945 default: goto usage;
946 }
947 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200948 else if( strcmp( p, "tickets" ) == 0 )
949 {
950 opt.tickets = atoi( q );
951 if( opt.tickets < 0 || opt.tickets > 1 )
952 goto usage;
953 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100954 else if( strcmp( p, "ticket_timeout" ) == 0 )
955 {
956 opt.ticket_timeout = atoi( q );
957 if( opt.ticket_timeout < 0 )
958 goto usage;
959 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100960 else if( strcmp( p, "cache_max" ) == 0 )
961 {
962 opt.cache_max = atoi( q );
963 if( opt.cache_max < 0 )
964 goto usage;
965 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100966 else if( strcmp( p, "cache_timeout" ) == 0 )
967 {
968 opt.cache_timeout = atoi( q );
969 if( opt.cache_timeout < 0 )
970 goto usage;
971 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100972 else if( strcmp( p, "sni" ) == 0 )
973 {
974 opt.sni = q;
975 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000976 else
977 goto usage;
978 }
979
Paul Bakkerc73079a2014-04-25 16:34:30 +0200980#if defined(POLARSSL_DEBUG_C)
981 debug_set_threshold( opt.debug_level );
982#endif
983
Paul Bakkerc1516be2013-06-29 16:01:32 +0200984 if( opt.force_ciphersuite[0] > 0 )
985 {
986 const ssl_ciphersuite_t *ciphersuite_info;
987 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
988
Paul Bakker5b55b792013-07-19 13:43:43 +0200989 if( opt.max_version != -1 &&
990 ciphersuite_info->min_minor_ver > opt.max_version )
991 {
992 printf("forced ciphersuite not allowed with this protocol version\n");
993 ret = 2;
994 goto usage;
995 }
996 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200997 ciphersuite_info->max_minor_ver < opt.min_version )
998 {
999 printf("forced ciphersuite not allowed with this protocol version\n");
1000 ret = 2;
1001 goto usage;
1002 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001003 if( opt.max_version > ciphersuite_info->max_minor_ver )
1004 opt.max_version = ciphersuite_info->max_minor_ver;
1005 if( opt.min_version < ciphersuite_info->min_minor_ver )
1006 opt.min_version = ciphersuite_info->min_minor_ver;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001007 }
1008
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001009 if( opt.version_suites != NULL )
1010 {
1011 const char *name[4] = { 0 };
1012
1013 /* Parse 4-element coma-separated list */
1014 for( i = 0, p = (char *) opt.version_suites;
1015 i < 4 && *p != '\0';
1016 i++ )
1017 {
1018 name[i] = p;
1019
1020 /* Terminate the current string and move on to next one */
1021 while( *p != ',' && *p != '\0' )
1022 p++;
1023 if( *p == ',' )
1024 *p++ = '\0';
1025 }
1026
1027 if( i != 4 )
1028 {
1029 printf( "too few values for version_suites\n" );
1030 ret = 1;
1031 goto exit;
1032 }
1033
1034 memset( version_suites, 0, sizeof( version_suites ) );
1035
1036 /* Get the suites identifiers from their name */
1037 for( i = 0; i < 4; i++ )
1038 {
1039 version_suites[i][0] = ssl_get_ciphersuite_id( name[i] );
1040
1041 if( version_suites[i][0] == 0 )
1042 {
1043 printf( "unknown ciphersuite: '%s'\n", name[i] );
1044 ret = 2;
1045 goto usage;
1046 }
1047 }
1048 }
1049
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001050#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001051 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001052 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001053 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001054 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001055 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001056 printf( "pre-shared key not valid hex\n" );
1057 goto exit;
1058 }
1059
1060 if( opt.psk_list != NULL )
1061 {
1062 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001063 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001064 printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001065 goto exit;
1066 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001067 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001068#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001069
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001070#if defined(POLARSSL_SSL_ALPN)
1071 if( opt.alpn_string != NULL )
1072 {
1073 p = (char *) opt.alpn_string;
1074 i = 0;
1075
1076 /* Leave room for a final NULL in alpn_list */
1077 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
1078 {
1079 alpn_list[i++] = p;
1080
1081 /* Terminate the current string and move on to next one */
1082 while( *p != ',' && *p != '\0' )
1083 p++;
1084 if( *p == ',' )
1085 *p++ = '\0';
1086 }
1087 }
1088#endif /* POLARSSL_SSL_ALPN */
1089
Paul Bakkerfbb17802013-04-17 19:10:21 +02001090 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001091 * 0. Initialize the RNG and the session data
1092 */
1093 printf( "\n . Seeding the random number generator..." );
1094 fflush( stdout );
1095
1096 entropy_init( &entropy );
1097 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001098 (const unsigned char *) pers,
1099 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001100 {
1101 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
1102 goto exit;
1103 }
1104
1105 printf( " ok\n" );
1106
Paul Bakker36713e82013-09-17 13:25:29 +02001107#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001108 /*
1109 * 1.1. Load the trusted CA
1110 */
1111 printf( " . Loading the CA root certificate ..." );
1112 fflush( stdout );
1113
1114#if defined(POLARSSL_FS_IO)
1115 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001116 if( strcmp( opt.ca_path, "none" ) == 0 )
1117 ret = 0;
1118 else
1119 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001120 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001121 if( strcmp( opt.ca_file, "none" ) == 0 )
1122 ret = 0;
1123 else
1124 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001125 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001126#endif
1127#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +02001128 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
1129 strlen( test_ca_list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001130#else
1131 {
1132 ret = 1;
1133 printf("POLARSSL_CERTS_C not defined.");
1134 }
1135#endif
1136 if( ret < 0 )
1137 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001138 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001139 goto exit;
1140 }
1141
1142 printf( " ok (%d skipped)\n", ret );
1143
1144 /*
1145 * 1.2. Load own certificate and private key
1146 */
1147 printf( " . Loading the server cert. and key..." );
1148 fflush( stdout );
1149
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001150#if defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001151 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001152 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001153 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001154 if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
1155 {
1156 printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n",
1157 -ret );
1158 goto exit;
1159 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001160 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001161 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001162 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001163 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001164 if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
1165 {
1166 printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret );
1167 goto exit;
1168 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001169 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001170 if( key_cert_init == 1 )
1171 {
1172 printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
1173 goto exit;
1174 }
1175
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001176 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001177 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001178 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001179 if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
1180 {
1181 printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n",
1182 -ret );
1183 goto exit;
1184 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001185 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001186 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001187 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001188 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001189 if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
1190 {
1191 printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n",
1192 -ret );
1193 goto exit;
1194 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001195 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001196 if( key_cert_init2 == 1 )
1197 {
1198 printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
1199 goto exit;
1200 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001201#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001202 if( key_cert_init == 0 &&
1203 strcmp( opt.crt_file, "none" ) != 0 &&
1204 strcmp( opt.key_file, "none" ) != 0 &&
1205 key_cert_init2 == 0 &&
1206 strcmp( opt.crt_file2, "none" ) != 0 &&
1207 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001208 {
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001209#if !defined(POLARSSL_CERTS_C)
1210 printf( "Not certificated or key provided, and \n"
1211 "POLARSSL_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001212 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001213#else
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001214#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001215 if( ( ret = x509_crt_parse( &srvcert,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001216 (const unsigned char *) test_srv_crt_rsa,
1217 strlen( test_srv_crt_rsa ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001218 {
1219 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
1220 goto exit;
1221 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001222 if( ( ret = pk_parse_key( &pkey,
1223 (const unsigned char *) test_srv_key_rsa,
1224 strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001225 {
1226 printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
1227 goto exit;
1228 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001229 key_cert_init = 2;
1230#endif /* POLARSSL_RSA_C */
1231#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001232 if( ( ret = x509_crt_parse( &srvcert2,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001233 (const unsigned char *) test_srv_crt_ec,
1234 strlen( test_srv_crt_ec ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001235 {
1236 printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
1237 goto exit;
1238 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001239 if( ( ret = pk_parse_key( &pkey2,
1240 (const unsigned char *) test_srv_key_ec,
1241 strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001242 {
1243 printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
1244 goto exit;
1245 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001246 key_cert_init2 = 2;
1247#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001248#endif /* POLARSSL_CERTS_C */
1249 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001250
1251 printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001252#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001253
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001254#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1255 if( opt.dhm_file != NULL )
1256 {
1257 printf( " . Loading DHM parameters..." );
1258 fflush( stdout );
1259
1260 if( ( ret = dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
1261 {
1262 printf( " failed\n ! dhm_parse_dhmfile returned -0x%04X\n\n",
1263 -ret );
1264 goto exit;
1265 }
1266
1267 printf( " ok\n" );
1268 }
1269#endif
1270
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001271#if defined(POLARSSL_SNI)
1272 if( opt.sni != NULL )
1273 {
1274 printf( " . Setting up SNI information..." );
1275 fflush( stdout );
1276
1277 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
1278 {
1279 printf( " failed\n" );
1280 goto exit;
1281 }
1282
1283 printf( " ok\n" );
1284 }
1285#endif /* POLARSSL_SNI */
1286
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001287 /*
1288 * 2. Setup the listening TCP socket
1289 */
1290 printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
1291 fflush( stdout );
1292
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001293 if( ( ret = net_bind( &listen_fd, opt.server_addr,
1294 opt.server_port ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001295 {
1296 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1297 goto exit;
1298 }
1299
1300 printf( " ok\n" );
1301
1302 /*
1303 * 3. Setup stuff
1304 */
1305 printf( " . Setting up the SSL/TLS structure..." );
1306 fflush( stdout );
1307
1308 if( ( ret = ssl_init( &ssl ) ) != 0 )
1309 {
1310 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
1311 goto exit;
1312 }
1313
1314 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001315 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001316
Paul Bakker05decb22013-08-15 13:33:48 +02001317#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001318 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1319 {
1320 printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
1321 goto exit;
1322 };
Paul Bakker05decb22013-08-15 13:33:48 +02001323#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001324
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001325#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1326 if( opt.extended_ms != DFL_EXTENDED_MS )
1327 ssl_set_extended_master_secret( &ssl, opt.extended_ms );
1328#endif
1329
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001330#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1331 if( opt.etm != DFL_ETM )
1332 ssl_set_encrypt_then_mac( &ssl, opt.etm );
1333#endif
1334
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001335#if defined(POLARSSL_SSL_ALPN)
1336 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001337 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1338 {
1339 printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
1340 goto exit;
1341 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001342#endif
1343
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001344 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
1345 ssl_set_dbg( &ssl, my_debug, stdout );
1346
Paul Bakker0a597072012-09-25 21:55:46 +00001347#if defined(POLARSSL_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001348 if( opt.cache_max != -1 )
1349 ssl_cache_set_max_entries( &cache, opt.cache_max );
1350
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001351 if( opt.cache_timeout != -1 )
1352 ssl_cache_set_timeout( &cache, opt.cache_timeout );
1353
Paul Bakker0a597072012-09-25 21:55:46 +00001354 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
1355 ssl_cache_set, &cache );
1356#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001357
Paul Bakkera503a632013-08-14 13:48:06 +02001358#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001359 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1360 {
1361 printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
1362 goto exit;
1363 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001364
1365 if( opt.ticket_timeout != -1 )
1366 ssl_set_session_ticket_lifetime( &ssl, opt.ticket_timeout );
Paul Bakkera503a632013-08-14 13:48:06 +02001367#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001368
Paul Bakker41c83d32013-03-20 14:39:14 +01001369 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001370 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
1371
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001372 if( opt.version_suites != NULL )
1373 {
1374 ssl_set_ciphersuites_for_version( &ssl, version_suites[0],
1375 SSL_MAJOR_VERSION_3,
1376 SSL_MINOR_VERSION_0 );
1377 ssl_set_ciphersuites_for_version( &ssl, version_suites[1],
1378 SSL_MAJOR_VERSION_3,
1379 SSL_MINOR_VERSION_1 );
1380 ssl_set_ciphersuites_for_version( &ssl, version_suites[2],
1381 SSL_MAJOR_VERSION_3,
1382 SSL_MINOR_VERSION_2 );
1383 ssl_set_ciphersuites_for_version( &ssl, version_suites[3],
1384 SSL_MAJOR_VERSION_3,
1385 SSL_MINOR_VERSION_3 );
1386 }
1387
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001388 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
1389 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001390#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001391 ssl_set_renegotiation( &ssl, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001392
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001393 if( opt.renego_delay != DFL_RENEGO_DELAY )
1394 ssl_set_renegotiation_enforced( &ssl, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001395
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001396 if( opt.renego_period != DFL_RENEGO_PERIOD )
1397 {
1398 renego_period[7] = opt.renego_period;
1399 ssl_set_renegotiation_period( &ssl, renego_period );
1400 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001401#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001402
Paul Bakker36713e82013-09-17 13:25:29 +02001403#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001404 if( strcmp( opt.ca_path, "none" ) != 0 &&
1405 strcmp( opt.ca_file, "none" ) != 0 )
1406 {
1407 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
1408 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001409 if( key_cert_init )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001410 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
1411 {
1412 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1413 goto exit;
1414 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001415 if( key_cert_init2 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001416 if( ( ret = ssl_set_own_cert( &ssl, &srvcert2, &pkey2 ) ) != 0 )
1417 {
1418 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1419 goto exit;
1420 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001421#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001422
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001423#if defined(POLARSSL_SNI)
1424 if( opt.sni != NULL )
1425 ssl_set_sni( &ssl, sni_callback, sni_info );
1426#endif
1427
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001428#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001429 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
1430 {
1431 ret = ssl_set_psk( &ssl, psk, psk_len,
1432 (const unsigned char *) opt.psk_identity,
1433 strlen( opt.psk_identity ) );
1434 if( ret != 0 )
1435 {
1436 printf( " failed\n ssl_set_psk returned -0x%04X\n\n", - ret );
1437 goto exit;
1438 }
1439 }
1440
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001441 if( opt.psk_list != NULL )
1442 ssl_set_psk_cb( &ssl, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02001443#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001444
1445#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00001446 /*
1447 * Use different group than default DHM group
1448 */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001449#if defined(POLARSSL_FS_IO)
1450 if( opt.dhm_file != NULL )
1451 ret = ssl_set_dh_param_ctx( &ssl, &dhm );
1452 else
1453#endif
1454 ret = ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
1455 POLARSSL_DHM_RFC5114_MODP_2048_G );
1456
1457 if( ret != 0 )
1458 {
1459 printf( " failed\n ssl_set_dh_param returned -0x%04X\n\n", - ret );
1460 goto exit;
1461 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001462#endif
1463
Paul Bakker1d29fb52012-09-28 13:28:45 +00001464 if( opt.min_version != -1 )
1465 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1466
Paul Bakkerc1516be2013-06-29 16:01:32 +02001467 if( opt.max_version != -1 )
1468 ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1469
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001470 printf( " ok\n" );
1471
1472reset:
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +02001473#if !defined(_WIN32)
1474 if( received_sigterm )
1475 {
1476 printf( " interrupted by SIGTERM\n" );
1477 ret = 0;
1478 goto exit;
1479 }
1480#endif
1481
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001482#ifdef POLARSSL_ERROR_C
1483 if( ret != 0 )
1484 {
1485 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001486 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001487 printf("Last error was: %d - %s\n\n", ret, error_buf );
1488 }
1489#endif
1490
1491 if( client_fd != -1 )
1492 net_close( client_fd );
1493
1494 ssl_session_reset( &ssl );
1495
1496 /*
1497 * 3. Wait until a client connects
1498 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001499 client_fd = -1;
1500
1501 printf( " . Waiting for a remote connection ..." );
1502 fflush( stdout );
1503
1504 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
1505 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02001506#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001507 if( received_sigterm )
1508 {
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +01001509 printf( " interrupted by signal\n" );
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001510 ret = 0;
1511 goto exit;
1512 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02001513#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001514
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001515 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
1516 goto exit;
1517 }
1518
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001519 if( opt.nbio > 0 )
1520 ret = net_set_nonblock( client_fd );
1521 else
1522 ret = net_set_block( client_fd );
1523 if( ret != 0 )
1524 {
1525 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
1526 goto exit;
1527 }
1528
1529 if( opt.nbio == 2 )
1530 ssl_set_bio( &ssl, my_recv, &client_fd, my_send, &client_fd );
1531 else
1532 ssl_set_bio( &ssl, net_recv, &client_fd, net_send, &client_fd );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001533
1534 printf( " ok\n" );
1535
1536 /*
1537 * 4. Handshake
1538 */
1539 printf( " . Performing the SSL/TLS handshake..." );
1540 fflush( stdout );
1541
1542 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1543 {
1544 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1545 {
1546 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001547 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001548 }
1549 }
1550
Manuel Pégourié-Gonnardc580a002014-02-12 10:15:30 +01001551 printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1552 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001553
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001554#if defined(POLARSSL_SSL_ALPN)
1555 if( opt.alpn_string != NULL )
1556 {
1557 const char *alp = ssl_get_alpn_protocol( &ssl );
1558 printf( " [ Application Layer Protocol is %s ]\n",
1559 alp ? alp : "(none)" );
1560 }
1561#endif
1562
Paul Bakker36713e82013-09-17 13:25:29 +02001563#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001564 /*
1565 * 5. Verify the server certificate
1566 */
1567 printf( " . Verifying peer X.509 certificate..." );
1568
1569 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1570 {
1571 printf( " failed\n" );
1572
Paul Bakkerb0550d92012-10-30 07:51:03 +00001573 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001574 printf( " ! no client certificate sent\n" );
1575
1576 if( ( ret & BADCERT_EXPIRED ) != 0 )
1577 printf( " ! client certificate has expired\n" );
1578
1579 if( ( ret & BADCERT_REVOKED ) != 0 )
1580 printf( " ! client certificate has been revoked\n" );
1581
1582 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
1583 printf( " ! self-signed or not signed by a trusted CA\n" );
1584
1585 printf( "\n" );
1586 }
1587 else
1588 printf( " ok\n" );
1589
Paul Bakkerb0550d92012-10-30 07:51:03 +00001590 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001591 {
1592 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001593 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1594 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001595 printf( "%s\n", buf );
1596 }
Paul Bakker36713e82013-09-17 13:25:29 +02001597#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001598
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001599 exchanges = opt.exchanges;
1600data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001601 /*
1602 * 6. Read the HTTP Request
1603 */
1604 printf( " < Read from client:" );
1605 fflush( stdout );
1606
1607 do
1608 {
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001609 int terminated = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001610 len = sizeof( buf ) - 1;
1611 memset( buf, 0, sizeof( buf ) );
1612 ret = ssl_read( &ssl, buf, len );
1613
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001614 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1615 ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001616 continue;
1617
1618 if( ret <= 0 )
1619 {
1620 switch( ret )
1621 {
1622 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1623 printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001624 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001625
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001626 case 0:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001627 case POLARSSL_ERR_NET_CONN_RESET:
1628 printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001629 ret = POLARSSL_ERR_NET_CONN_RESET;
1630 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001631
1632 default:
1633 printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001634 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001635 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001636 }
1637
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001638 if( ssl_get_bytes_avail( &ssl ) == 0 )
1639 {
1640 len = ret;
1641 buf[len] = '\0';
1642 printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001643
1644 /* End of message should be detected according to the syntax of the
1645 * application protocol (eg HTTP), just use a dummy test here. */
1646 if( buf[len - 1] == '\n' )
1647 terminated = 1;
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001648 }
1649 else
1650 {
1651 int extra_len, ori_len;
1652 unsigned char *larger_buf;
1653
1654 ori_len = ret;
1655 extra_len = ssl_get_bytes_avail( &ssl );
1656
1657 larger_buf = polarssl_malloc( ori_len + extra_len + 1 );
1658 if( larger_buf == NULL )
1659 {
1660 printf( " ! memory allocation failed\n" );
1661 ret = 1;
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001662 goto reset;
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001663 }
1664
1665 memset( larger_buf, 0, ori_len + extra_len );
1666 memcpy( larger_buf, buf, ori_len );
1667
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +02001668 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001669 ret = ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +02001670 if( ret != extra_len ||
1671 ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001672 {
1673 printf( " ! ssl_read failed on cached data\n" );
1674 ret = 1;
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001675 goto reset;
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001676 }
1677
1678 larger_buf[ori_len + extra_len] = '\0';
1679 printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnard0669f272014-06-18 13:07:20 +02001680 ori_len + extra_len, ori_len, extra_len,
1681 (char *) larger_buf );
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001682
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001683 /* End of message should be detected according to the syntax of the
1684 * application protocol (eg HTTP), just use a dummy test here. */
1685 if( larger_buf[ori_len + extra_len - 1] == '\n' )
1686 terminated = 1;
1687
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001688 polarssl_free( larger_buf );
1689 }
1690
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001691 if( terminated )
1692 {
1693 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001694 break;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001695 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001696 }
1697 while( 1 );
1698
1699 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001700 * 7a. Request renegotiation while client is waiting for input from us.
1701 * (only if we're going to exhange more data afterwards)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001702 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001703#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001704 if( opt.renegotiate && exchanges > 1 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001705 {
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001706 printf( " . Requestion renegotiation..." );
1707 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001708
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001709 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
1710 {
1711 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1712 ret != POLARSSL_ERR_NET_WANT_WRITE )
1713 {
1714 printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
1715 goto reset;
1716 }
1717 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001718
1719 printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001720 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001721#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001722
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001723 /*
1724 * 7. Write the 200 Response
1725 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001726 printf( " > Write to client:" );
1727 fflush( stdout );
1728
1729 len = sprintf( (char *) buf, HTTP_RESPONSE,
1730 ssl_get_ciphersuite( &ssl ) );
1731
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001732 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001733 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001734 while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001735 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001736 if( ret == POLARSSL_ERR_NET_CONN_RESET )
1737 {
1738 printf( " failed\n ! peer closed the connection\n\n" );
1739 goto reset;
1740 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001741
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001742 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1743 {
1744 printf( " failed\n ! ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001745 goto reset;
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001746 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001747 }
1748 }
1749
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001750 buf[written] = '\0';
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001751 printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001752
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001753
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001754 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001755 * 7b. Continue doing data exchanges?
1756 */
1757 if( --exchanges > 0 )
1758 goto data_exchange;
1759
1760 /*
1761 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001762 */
1763close_notify:
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001764 printf( " . Closing the connection..." );
1765
1766 while( ( ret = ssl_close_notify( &ssl ) ) < 0 )
1767 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001768 if( ret == POLARSSL_ERR_NET_CONN_RESET )
1769 {
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001770 printf( " ok (already closed by peer)\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001771 ret = 0;
1772 goto reset;
1773 }
1774
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001775 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1776 ret != POLARSSL_ERR_NET_WANT_WRITE )
1777 {
1778 printf( " failed\n ! ssl_close_notify returned %d\n\n", ret );
1779 goto reset;
1780 }
1781 }
1782
1783 printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001784 goto reset;
1785
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001786 /*
1787 * Cleanup and exit
1788 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001789exit:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001790#ifdef POLARSSL_ERROR_C
1791 if( ret != 0 )
1792 {
1793 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001794 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001795 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
1796 }
1797#endif
1798
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01001799 printf( " . Cleaning up..." );
1800 fflush( stdout );
1801
Paul Bakker0c226102014-04-17 16:02:36 +02001802 if( client_fd != -1 )
1803 net_close( client_fd );
1804
Paul Bakkera317a982014-06-18 16:44:11 +02001805#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1806 dhm_free( &dhm );
1807#endif
Paul Bakker36713e82013-09-17 13:25:29 +02001808#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker36713e82013-09-17 13:25:29 +02001809 x509_crt_free( &cacert );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001810 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02001811 pk_free( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001812 x509_crt_free( &srvcert2 );
1813 pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02001814#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001815#if defined(POLARSSL_SNI)
1816 sni_free( sni_info );
1817#endif
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02001818#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1819 psk_free( psk_info );
1820#endif
1821#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1822 dhm_free( &dhm );
1823#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001824
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001825 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02001826 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02001827 entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001828
Paul Bakker0a597072012-09-25 21:55:46 +00001829#if defined(POLARSSL_SSL_CACHE_C)
1830 ssl_cache_free( &cache );
1831#endif
1832
Paul Bakker1337aff2013-09-29 14:45:34 +02001833#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1834#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker82024bf2013-07-04 11:52:32 +02001835 memory_buffer_alloc_status();
1836#endif
Paul Bakker1337aff2013-09-29 14:45:34 +02001837 memory_buffer_alloc_free();
1838#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02001839
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01001840 printf( " done.\n" );
1841
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001842#if defined(_WIN32)
1843 printf( " + Press Enter to exit this program.\n" );
1844 fflush( stdout ); getchar();
1845#endif
1846
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001847 // Shell can not handle large exit numbers -> 1 for errors
1848 if( ret < 0 )
1849 ret = 1;
1850
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001851 return( ret );
1852}
1853#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
1854 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
1855 POLARSSL_CTR_DRBG_C */