blob: 15166d22b91742d52fe328f8048e1a76c043f7b2 [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 *
Paul Bakkerb60b95f2012-09-25 09:05:17 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
34#define polarssl_malloc malloc
35#define polarssl_free free
36#endif
37
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020038#if !defined(POLARSSL_ENTROPY_C) || \
39 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
40 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
41#include <stdio.h>
42int main( int argc, char *argv[] )
43{
44 ((void) argc);
45 ((void) argv);
46
Rich Evansf90016a2015-01-19 14:26:37 +000047 polarssl_printf("POLARSSL_ENTROPY_C and/or "
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020048 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
49 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
50 return( 0 );
51}
52#else
53
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010054#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) && defined(POLARSSL_FS_IO)
55#define POLARSSL_SNI
56#endif
57
Paul Bakkerb60b95f2012-09-25 09:05:17 +000058#if defined(_WIN32)
59#include <windows.h>
60#endif
61
62#include <string.h>
63#include <stdlib.h>
64#include <stdio.h>
Paul Bakkerc1283d32014-08-18 11:05:51 +020065
66#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +020067#include <signal.h>
Paul Bakkerc1283d32014-08-18 11:05:51 +020068#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000069
Paul Bakkerb60b95f2012-09-25 09:05:17 +000070#include "polarssl/net.h"
71#include "polarssl/ssl.h"
72#include "polarssl/entropy.h"
73#include "polarssl/ctr_drbg.h"
74#include "polarssl/certs.h"
75#include "polarssl/x509.h"
76#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020077#include "polarssl/debug.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000078
Paul Bakker0a597072012-09-25 21:55:46 +000079#if defined(POLARSSL_SSL_CACHE_C)
80#include "polarssl/ssl_cache.h"
81#endif
82
Paul Bakker82024bf2013-07-04 11:52:32 +020083#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardd43ccb62015-01-23 17:38:09 +000084#include "polarssl/memory_buffer_alloc.h"
Paul Bakker82024bf2013-07-04 11:52:32 +020085#endif
86
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +010087#define DFL_SERVER_ADDR NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000088#define DFL_SERVER_PORT 4433
Paul Bakkerb60b95f2012-09-25 09:05:17 +000089#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010090#define DFL_NBIO 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +000091#define DFL_CA_FILE ""
92#define DFL_CA_PATH ""
93#define DFL_CRT_FILE ""
94#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +020095#define DFL_CRT_FILE2 ""
96#define DFL_KEY_FILE2 ""
Paul Bakkerfbb17802013-04-17 19:10:21 +020097#define DFL_PSK ""
98#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +020099#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000100#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200101#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +0200102#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100103#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100104#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200105#define DFL_RENEGO_DELAY -2
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100106#define DFL_RENEGO_PERIOD -1
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200107#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100108#define DFL_MIN_VERSION SSL_MINOR_VERSION_1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200109#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100110#define DFL_ARC4 SSL_ARC4_DISABLED
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é-Gonnarde117a8f2015-01-09 12:39:35 +0100113#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200114#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100115#define DFL_TICKET_TIMEOUT -1
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100116#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100117#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100118#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200119#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200120#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200121#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100122#define DFL_ETM -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000123
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200124#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
125 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
126 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
127 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
128 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
129 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
130 "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 +0200131
Paul Bakker8e714d72013-07-18 11:05:13 +0200132/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
133 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000134#define HTTP_RESPONSE \
135 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000136 "<h2>mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200137 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000138
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200139/*
140 * Size of the basic I/O buffer. Able to hold our default response.
141 *
142 * You will need to adapt the ssl_get_bytes_avail() test in ssl-opt.sh
143 * if you change this value to something outside the range <= 100 or > 500
144 */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200145#define IO_BUF_LEN 200
146
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000147/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000148 * global options
149 */
150struct options
151{
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100152 const char *server_addr; /* address on which the ssl service runs */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000153 int server_port; /* port on which the ssl service runs */
154 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100155 int nbio; /* should I/O be blocking? */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200156 const char *ca_file; /* the file with the CA certificate(s) */
157 const char *ca_path; /* the path with the CA certificate(s) reside */
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200158 const char *crt_file; /* the file with the server certificate */
159 const char *key_file; /* the file with the server key */
160 const char *crt_file2; /* the file with the 2nd server certificate */
161 const char *key_file2; /* the file with the 2nd server key */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200162 const char *psk; /* the pre-shared key */
163 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200164 char *psk_list; /* list of PSK id/key pairs for callback */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000165 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200166 const char *version_suites; /* per-version ciphersuites */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000167 int renegotiation; /* enable / disable renegotiation */
168 int allow_legacy; /* allow legacy renegotiation */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100169 int renegotiate; /* attempt renegotiation? */
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200170 int renego_delay; /* delay before enforcing renegotiation */
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100171 int renego_period; /* period for automatic renegotiation */
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200172 int exchanges; /* number of data exchanges */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000173 int min_version; /* minimum protocol version accepted */
Paul Bakkerc1516be2013-06-29 16:01:32 +0200174 int max_version; /* maximum protocol version accepted */
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100175 int arc4; /* flag for arc4 suites support */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100176 int auth_mode; /* verify mode for connection */
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200177 unsigned char mfl_code; /* code for maximum fragment length */
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100178 int trunc_hmac; /* accept truncated hmac? */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200179 int tickets; /* enable / disable session tickets */
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100180 int ticket_timeout; /* session ticket lifetime */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100181 int cache_max; /* max number of session cache entries */
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100182 int cache_timeout; /* expiration delay of session cache entries */
Paul Bakker1ebc0c52014-05-22 15:47:58 +0200183 char *sni; /* string describing sni information */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200184 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200185 const char *dhm_file; /* the file with the DH parameters */
Paul Bakkerb2eaac12015-01-13 17:15:31 +0100186 int extended_ms; /* allow negotiation of extended MS? */
187 int etm; /* allow negotiation of encrypt-then-MAC? */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000188} opt;
189
Paul Bakker3c5ef712013-06-25 16:37:45 +0200190static void my_debug( void *ctx, int level, const char *str )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000191{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200192 ((void) level);
193
Rich Evansf90016a2015-01-19 14:26:37 +0000194 polarssl_fprintf( (FILE *) ctx, "%s", str );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200195 fflush( (FILE *) ctx );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000196}
197
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100198/*
199 * Test recv/send functions that make sure each try returns
200 * WANT_READ/WANT_WRITE at least once before sucesseding
201 */
202static int my_recv( void *ctx, unsigned char *buf, size_t len )
203{
204 static int first_try = 1;
205 int ret;
206
207 if( first_try )
208 {
209 first_try = 0;
210 return( POLARSSL_ERR_NET_WANT_READ );
211 }
212
213 ret = net_recv( ctx, buf, len );
214 if( ret != POLARSSL_ERR_NET_WANT_READ )
215 first_try = 1; /* Next call will be a new operation */
216 return( ret );
217}
218
219static int my_send( void *ctx, const unsigned char *buf, size_t len )
220{
221 static int first_try = 1;
222 int ret;
223
224 if( first_try )
225 {
226 first_try = 0;
227 return( POLARSSL_ERR_NET_WANT_WRITE );
228 }
229
230 ret = net_send( ctx, buf, len );
231 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
232 first_try = 1; /* Next call will be a new operation */
233 return( ret );
234}
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200235
236#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000237#if defined(POLARSSL_FS_IO)
238#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100239 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
240 " default: \"\" (pre-loaded)\n" \
241 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
242 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
243 " 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 +0200244 " default: see note after key_file2\n" \
245 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200246 " 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 +0200247 " default: see note after key_file2\n" \
248 " key_file2=%%s default: see note below\n" \
249 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200250 " preloaded certificate(s) and key(s) are used if available\n" \
251 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
252 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000253#else
254#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200255 "\n" \
256 " No file operations available (POLARSSL_FS_IO not defined)\n" \
257 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000258#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200259#else
260#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200261#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200262
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200263#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200264#define USAGE_PSK \
265 " psk=%%s default: \"\" (in hex, without 0x)\n" \
266 " psk_identity=%%s default: \"Client_identity\"\n"
267#else
268#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200269#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000270
Paul Bakkera503a632013-08-14 13:48:06 +0200271#if defined(POLARSSL_SSL_SESSION_TICKETS)
272#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100273 " tickets=%%d default: 1 (enabled)\n" \
274 " ticket_timeout=%%d default: ticket default (1d)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200275#else
276#define USAGE_TICKETS ""
277#endif /* POLARSSL_SSL_SESSION_TICKETS */
278
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100279#if defined(POLARSSL_SSL_CACHE_C)
280#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100281 " cache_max=%%d default: cache default (50)\n" \
282 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100283#else
284#define USAGE_CACHE ""
285#endif /* POLARSSL_SSL_CACHE_C */
286
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100287#if defined(POLARSSL_SNI)
288#define USAGE_SNI \
289 " sni=%%s name1,cert1,key1[,name2,cert2,key2[,...]]\n" \
290 " default: disabled\n"
291#else
292#define USAGE_SNI ""
293#endif /* POLARSSL_SNI */
294
Paul Bakker05decb22013-08-15 13:33:48 +0200295#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
296#define USAGE_MAX_FRAG_LEN \
297 " max_frag_len=%%d default: 16384 (tls default)\n" \
298 " options: 512, 1024, 2048, 4096\n"
299#else
300#define USAGE_MAX_FRAG_LEN ""
301#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
302
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100303#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
304#define USAGE_TRUNC_HMAC \
305 " trunc_hmac=%%d default: library default\n"
306#else
307#define USAGE_TRUNC_HMAC ""
308#endif
309
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200310#if defined(POLARSSL_SSL_ALPN)
311#define USAGE_ALPN \
312 " alpn=%%s default: \"\" (disabled)\n" \
313 " example: spdy/1,http/1.1\n"
314#else
315#define USAGE_ALPN ""
316#endif /* POLARSSL_SSL_ALPN */
317
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200318#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
319#define USAGE_EMS \
320 " extended_ms=0/1 default: (library default: on)\n"
321#else
322#define USAGE_EMS ""
323#endif
324
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100325#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
326#define USAGE_ETM \
327 " etm=0/1 default: (library default: on)\n"
328#else
329#define USAGE_ETM ""
330#endif
331
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100332#if defined(POLARSSL_SSL_RENEGOTIATION)
333#define USAGE_RENEGO \
334 " renegotiation=%%d default: 0 (disabled)\n" \
335 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100336 " renego_delay=%%d default: -2 (library default)\n" \
337 " renego_period=%%d default: (library default)\n"
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100338#else
339#define USAGE_RENEGO ""
340#endif
341
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000342#define USAGE \
343 "\n usage: ssl_server2 param=<>...\n" \
344 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100345 " server_addr=%%d default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000346 " server_port=%%d default: 4433\n" \
347 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100348 " nbio=%%d default: 0 (blocking I/O)\n" \
349 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100350 "\n" \
351 " auth_mode=%%s default: \"optional\"\n" \
352 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000353 USAGE_IO \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100354 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100355 "\n" \
356 USAGE_PSK \
357 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100358 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100359 USAGE_RENEGO \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200360 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100361 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100362 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100363 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100364 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200365 USAGE_ALPN \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200366 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100367 USAGE_ETM \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100368 "\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000369 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200370 " max_version=%%s default: \"tls1_2\"\n" \
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100371 " arc4=%%d default: 0 (disabled)\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200372 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200373 " options: ssl3, tls1, tls1_1, tls1_2\n" \
374 "\n" \
375 " version_suites=a,b,c,d per-version ciphersuites\n" \
376 " in order from ssl3 to tls1_2\n" \
377 " default: all enabled\n" \
378 " force_ciphersuite=<name> default: all enabled\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000379 " acceptable ciphersuite names:\n"
380
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200381/*
382 * Used by sni_parse and psk_parse to handle coma-separated lists
383 */
384#define GET_ITEM( dst ) \
385 dst = p; \
386 while( *p != ',' ) \
387 if( ++p > end ) \
388 return( NULL ); \
389 *p++ = '\0';
390
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100391#if defined(POLARSSL_SNI)
392typedef struct _sni_entry sni_entry;
393
394struct _sni_entry {
395 const char *name;
396 x509_crt *cert;
397 pk_context *key;
398 sni_entry *next;
399};
400
401/*
402 * Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]]
403 * into a usable sni_entry list.
404 *
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200405 * Modifies the input string! This is not production quality!
406 * (leaks memory if parsing fails, no error reporting, ...)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100407 */
408sni_entry *sni_parse( char *sni_string )
409{
410 sni_entry *cur = NULL, *new = NULL;
411 char *p = sni_string;
412 char *end = p;
413 char *crt_file, *key_file;
414
415 while( *end != '\0' )
416 ++end;
417 *end = ',';
418
419 while( p <= end )
420 {
421 if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL )
422 return( NULL );
423
424 memset( new, 0, sizeof( sni_entry ) );
425
426 if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ||
427 ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200428 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100429
430 x509_crt_init( new->cert );
431 pk_init( new->key );
432
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200433 GET_ITEM( new->name );
434 GET_ITEM( crt_file );
435 GET_ITEM( key_file );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100436
437 if( x509_crt_parse_file( new->cert, crt_file ) != 0 ||
438 pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200439 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100440
441 new->next = cur;
442 cur = new;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100443 }
444
445 return( cur );
446}
447
448void sni_free( sni_entry *head )
449{
450 sni_entry *cur = head, *next;
451
452 while( cur != NULL )
453 {
454 x509_crt_free( cur->cert );
455 polarssl_free( cur->cert );
456
457 pk_free( cur->key );
458 polarssl_free( cur->key );
459
460 next = cur->next;
461 polarssl_free( cur );
462 cur = next;
463 }
464}
465
466/*
467 * SNI callback.
468 */
469int sni_callback( void *p_info, ssl_context *ssl,
470 const unsigned char *name, size_t name_len )
471{
472 sni_entry *cur = (sni_entry *) p_info;
473
474 while( cur != NULL )
475 {
476 if( name_len == strlen( cur->name ) &&
477 memcmp( name, cur->name, name_len ) == 0 )
478 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200479 return( ssl_set_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100480 }
481
482 cur = cur->next;
483 }
484
485 return( -1 );
486}
487
488#endif /* POLARSSL_SNI */
489
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200490#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
491
492#define HEX2NUM( c ) \
493 if( c >= '0' && c <= '9' ) \
494 c -= '0'; \
495 else if( c >= 'a' && c <= 'f' ) \
496 c -= 'a' - 10; \
497 else if( c >= 'A' && c <= 'F' ) \
498 c -= 'A' - 10; \
499 else \
500 return( -1 );
501
502/*
503 * Convert a hex string to bytes.
504 * Return 0 on success, -1 on error.
505 */
506int unhexify( unsigned char *output, const char *input, size_t *olen )
507{
508 unsigned char c;
509 size_t j;
510
511 *olen = strlen( input );
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200512 if( *olen % 2 != 0 || *olen / 2 > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200513 return( -1 );
514 *olen /= 2;
515
516 for( j = 0; j < *olen * 2; j += 2 )
517 {
518 c = input[j];
519 HEX2NUM( c );
520 output[ j / 2 ] = c << 4;
521
522 c = input[j + 1];
523 HEX2NUM( c );
524 output[ j / 2 ] |= c;
525 }
526
527 return( 0 );
528}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200529
530typedef struct _psk_entry psk_entry;
531
532struct _psk_entry
533{
534 const char *name;
535 size_t key_len;
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200536 unsigned char key[POLARSSL_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200537 psk_entry *next;
538};
539
540/*
541 * Parse a string of pairs name1,key1[,name2,key2[,...]]
542 * into a usable psk_entry list.
543 *
544 * Modifies the input string! This is not production quality!
545 * (leaks memory if parsing fails, no error reporting, ...)
546 */
547psk_entry *psk_parse( char *psk_string )
548{
549 psk_entry *cur = NULL, *new = NULL;
550 char *p = psk_string;
551 char *end = p;
552 char *key_hex;
553
554 while( *end != '\0' )
555 ++end;
556 *end = ',';
557
558 while( p <= end )
559 {
560 if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL )
561 return( NULL );
562
563 memset( new, 0, sizeof( psk_entry ) );
564
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200565 GET_ITEM( new->name );
566 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200567
568 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
569 return( NULL );
570
571 new->next = cur;
572 cur = new;
573 }
574
575 return( cur );
576}
577
578/*
579 * Free a list of psk_entry's
580 */
581void psk_free( psk_entry *head )
582{
583 psk_entry *next;
584
585 while( head != NULL )
586 {
587 next = head->next;
588 polarssl_free( head );
589 head = next;
590 }
591}
592
593/*
594 * PSK callback
595 */
596int psk_callback( void *p_info, ssl_context *ssl,
597 const unsigned char *name, size_t name_len )
598{
599 psk_entry *cur = (psk_entry *) p_info;
600
601 while( cur != NULL )
602 {
603 if( name_len == strlen( cur->name ) &&
604 memcmp( name, cur->name, name_len ) == 0 )
605 {
606 return( ssl_set_psk( ssl, cur->key, cur->key_len,
607 name, name_len ) );
608 }
609
610 cur = cur->next;
611 }
612
613 return( -1 );
614}
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200615#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
616
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +0200617static int listen_fd, client_fd = -1;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200618
619/* Interruption handler to ensure clean exit (for valgrind testing) */
620#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200621static int received_sigterm = 0;
622void term_handler( int sig )
623{
624 ((void) sig);
625 received_sigterm = 1;
626 net_close( listen_fd ); /* causes net_accept() to abort */
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +0200627 net_close( client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200628}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200629#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200630
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000631int main( int argc, char *argv[] )
632{
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +0000633 int ret = 0, len, written, frags, exchanges_left;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200634 int version_suites[4][2];
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200635 unsigned char buf[IO_BUF_LEN];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200636#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200637 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +0200638 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +0200639 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +0200640#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200641 const char *pers = "ssl_server2";
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000642
643 entropy_context entropy;
644 ctr_drbg_context ctr_drbg;
645 ssl_context ssl;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100646#if defined(POLARSSL_SSL_RENEGOTIATION)
647 unsigned char renego_period[8] = { 0 };
648#endif
Paul Bakker36713e82013-09-17 13:25:29 +0200649#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200650 x509_crt cacert;
651 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200652 pk_context pkey;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200653 x509_crt srvcert2;
654 pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200655 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200656#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200657#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
658 dhm_context dhm;
659#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000660#if defined(POLARSSL_SSL_CACHE_C)
661 ssl_cache_context cache;
662#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100663#if defined(POLARSSL_SNI)
664 sni_entry *sni_info = NULL;
665#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200666#if defined(POLARSSL_SSL_ALPN)
667 const char *alpn_list[10];
668#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200669#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
670 unsigned char alloc_buf[100000];
671#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000672
673 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000674 char *p, *q;
675 const int *list;
676
Paul Bakker82024bf2013-07-04 11:52:32 +0200677#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
678 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
679#endif
680
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000681 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200682 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000683 */
684 listen_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200685 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200686#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200687 x509_crt_init( &cacert );
688 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200689 pk_init( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200690 x509_crt_init( &srvcert2 );
691 pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200692#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200693#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
Paul Bakkera317a982014-06-18 16:44:11 +0200694 dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200695#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000696#if defined(POLARSSL_SSL_CACHE_C)
697 ssl_cache_init( &cache );
698#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200699#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200700 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200701#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000702
Paul Bakkerc1283d32014-08-18 11:05:51 +0200703#if !defined(_WIN32)
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100704 /* Abort cleanly on SIGTERM and SIGINT */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200705 signal( SIGTERM, term_handler );
Manuel Pégourié-Gonnard403a86f2014-11-17 12:46:49 +0100706 signal( SIGINT, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +0200707#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200708
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000709 if( argc == 0 )
710 {
711 usage:
712 if( ret == 0 )
713 ret = 1;
714
Rich Evansf90016a2015-01-19 14:26:37 +0000715 polarssl_printf( USAGE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000716
717 list = ssl_list_ciphersuites();
718 while( *list )
719 {
Rich Evansf90016a2015-01-19 14:26:37 +0000720 polarssl_printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200721 list++;
722 if( !*list )
723 break;
Rich Evansf90016a2015-01-19 14:26:37 +0000724 polarssl_printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000725 list++;
726 }
Rich Evansf90016a2015-01-19 14:26:37 +0000727 polarssl_printf("\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000728 goto exit;
729 }
730
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100731 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000732 opt.server_port = DFL_SERVER_PORT;
733 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100734 opt.nbio = DFL_NBIO;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000735 opt.ca_file = DFL_CA_FILE;
736 opt.ca_path = DFL_CA_PATH;
737 opt.crt_file = DFL_CRT_FILE;
738 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200739 opt.crt_file2 = DFL_CRT_FILE2;
740 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200741 opt.psk = DFL_PSK;
742 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200743 opt.psk_list = DFL_PSK_LIST;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000744 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200745 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000746 opt.renegotiation = DFL_RENEGOTIATION;
747 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100748 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200749 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100750 opt.renego_period = DFL_RENEGO_PERIOD;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200751 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000752 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200753 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100754 opt.arc4 = DFL_ARC4;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100755 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200756 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100757 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200758 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100759 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100760 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100761 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100762 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200763 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200764 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200765 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100766 opt.etm = DFL_ETM;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000767
768 for( i = 1; i < argc; i++ )
769 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000770 p = argv[i];
771 if( ( q = strchr( p, '=' ) ) == NULL )
772 goto usage;
773 *q++ = '\0';
774
775 if( strcmp( p, "server_port" ) == 0 )
776 {
777 opt.server_port = atoi( q );
778 if( opt.server_port < 1 || opt.server_port > 65535 )
779 goto usage;
780 }
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100781 else if( strcmp( p, "server_addr" ) == 0 )
782 opt.server_addr = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000783 else if( strcmp( p, "debug_level" ) == 0 )
784 {
785 opt.debug_level = atoi( q );
786 if( opt.debug_level < 0 || opt.debug_level > 65535 )
787 goto usage;
788 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100789 else if( strcmp( p, "nbio" ) == 0 )
790 {
791 opt.nbio = atoi( q );
792 if( opt.nbio < 0 || opt.nbio > 2 )
793 goto usage;
794 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000795 else if( strcmp( p, "ca_file" ) == 0 )
796 opt.ca_file = q;
797 else if( strcmp( p, "ca_path" ) == 0 )
798 opt.ca_path = q;
799 else if( strcmp( p, "crt_file" ) == 0 )
800 opt.crt_file = q;
801 else if( strcmp( p, "key_file" ) == 0 )
802 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200803 else if( strcmp( p, "crt_file2" ) == 0 )
804 opt.crt_file2 = q;
805 else if( strcmp( p, "key_file2" ) == 0 )
806 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200807 else if( strcmp( p, "dhm_file" ) == 0 )
808 opt.dhm_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200809 else if( strcmp( p, "psk" ) == 0 )
810 opt.psk = q;
811 else if( strcmp( p, "psk_identity" ) == 0 )
812 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200813 else if( strcmp( p, "psk_list" ) == 0 )
814 opt.psk_list = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000815 else if( strcmp( p, "force_ciphersuite" ) == 0 )
816 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000817 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
818
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200819 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000820 {
821 ret = 2;
822 goto usage;
823 }
824 opt.force_ciphersuite[1] = 0;
825 }
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200826 else if( strcmp( p, "version_suites" ) == 0 )
827 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000828 else if( strcmp( p, "renegotiation" ) == 0 )
829 {
830 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
831 SSL_RENEGOTIATION_DISABLED;
832 }
833 else if( strcmp( p, "allow_legacy" ) == 0 )
834 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100835 switch( atoi( q ) )
836 {
837 case -1: opt.allow_legacy = SSL_LEGACY_BREAK_HANDSHAKE; break;
838 case 0: opt.allow_legacy = SSL_LEGACY_NO_RENEGOTIATION; break;
839 case 1: opt.allow_legacy = SSL_LEGACY_ALLOW_RENEGOTIATION; break;
840 default: goto usage;
841 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000842 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100843 else if( strcmp( p, "renegotiate" ) == 0 )
844 {
845 opt.renegotiate = atoi( q );
846 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
847 goto usage;
848 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200849 else if( strcmp( p, "renego_delay" ) == 0 )
850 {
851 opt.renego_delay = atoi( q );
852 }
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +0100853 else if( strcmp( p, "renego_period" ) == 0 )
854 {
855 opt.renego_period = atoi( q );
856 if( opt.renego_period < 2 || opt.renego_period > 255 )
857 goto usage;
858 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200859 else if( strcmp( p, "exchanges" ) == 0 )
860 {
861 opt.exchanges = atoi( q );
862 if( opt.exchanges < 1 )
863 goto usage;
864 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000865 else if( strcmp( p, "min_version" ) == 0 )
866 {
867 if( strcmp( q, "ssl3" ) == 0 )
868 opt.min_version = SSL_MINOR_VERSION_0;
869 else if( strcmp( q, "tls1" ) == 0 )
870 opt.min_version = SSL_MINOR_VERSION_1;
871 else if( strcmp( q, "tls1_1" ) == 0 )
872 opt.min_version = SSL_MINOR_VERSION_2;
873 else if( strcmp( q, "tls1_2" ) == 0 )
874 opt.min_version = SSL_MINOR_VERSION_3;
875 else
876 goto usage;
877 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200878 else if( strcmp( p, "max_version" ) == 0 )
879 {
880 if( strcmp( q, "ssl3" ) == 0 )
881 opt.max_version = SSL_MINOR_VERSION_0;
882 else if( strcmp( q, "tls1" ) == 0 )
883 opt.max_version = SSL_MINOR_VERSION_1;
884 else if( strcmp( q, "tls1_1" ) == 0 )
885 opt.max_version = SSL_MINOR_VERSION_2;
886 else if( strcmp( q, "tls1_2" ) == 0 )
887 opt.max_version = SSL_MINOR_VERSION_3;
888 else
889 goto usage;
890 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100891 else if( strcmp( p, "arc4" ) == 0 )
892 {
893 switch( atoi( q ) )
894 {
895 case 0: opt.arc4 = SSL_ARC4_DISABLED; break;
896 case 1: opt.arc4 = SSL_ARC4_ENABLED; break;
897 default: goto usage;
898 }
899 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200900 else if( strcmp( p, "force_version" ) == 0 )
901 {
902 if( strcmp( q, "ssl3" ) == 0 )
903 {
904 opt.min_version = SSL_MINOR_VERSION_0;
905 opt.max_version = SSL_MINOR_VERSION_0;
906 }
907 else if( strcmp( q, "tls1" ) == 0 )
908 {
909 opt.min_version = SSL_MINOR_VERSION_1;
910 opt.max_version = SSL_MINOR_VERSION_1;
911 }
912 else if( strcmp( q, "tls1_1" ) == 0 )
913 {
914 opt.min_version = SSL_MINOR_VERSION_2;
915 opt.max_version = SSL_MINOR_VERSION_2;
916 }
917 else if( strcmp( q, "tls1_2" ) == 0 )
918 {
919 opt.min_version = SSL_MINOR_VERSION_3;
920 opt.max_version = SSL_MINOR_VERSION_3;
921 }
922 else
923 goto usage;
924 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100925 else if( strcmp( p, "auth_mode" ) == 0 )
926 {
927 if( strcmp( q, "none" ) == 0 )
928 opt.auth_mode = SSL_VERIFY_NONE;
929 else if( strcmp( q, "optional" ) == 0 )
930 opt.auth_mode = SSL_VERIFY_OPTIONAL;
931 else if( strcmp( q, "required" ) == 0 )
932 opt.auth_mode = SSL_VERIFY_REQUIRED;
933 else
934 goto usage;
935 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200936 else if( strcmp( p, "max_frag_len" ) == 0 )
937 {
938 if( strcmp( q, "512" ) == 0 )
939 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
940 else if( strcmp( q, "1024" ) == 0 )
941 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
942 else if( strcmp( q, "2048" ) == 0 )
943 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
944 else if( strcmp( q, "4096" ) == 0 )
945 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
946 else
947 goto usage;
948 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200949 else if( strcmp( p, "alpn" ) == 0 )
950 {
951 opt.alpn_string = q;
952 }
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100953 else if( strcmp( p, "trunc_hmac" ) == 0 )
954 {
955 switch( atoi( q ) )
956 {
957 case 0: opt.trunc_hmac = SSL_TRUNC_HMAC_DISABLED; break;
958 case 1: opt.trunc_hmac = SSL_TRUNC_HMAC_ENABLED; break;
959 default: goto usage;
960 }
961 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200962 else if( strcmp( p, "extended_ms" ) == 0 )
963 {
964 switch( atoi( q ) )
965 {
966 case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break;
967 case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break;
968 default: goto usage;
969 }
970 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100971 else if( strcmp( p, "etm" ) == 0 )
972 {
973 switch( atoi( q ) )
974 {
975 case 0: opt.etm = SSL_ETM_DISABLED; break;
976 case 1: opt.etm = SSL_ETM_ENABLED; break;
977 default: goto usage;
978 }
979 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200980 else if( strcmp( p, "tickets" ) == 0 )
981 {
982 opt.tickets = atoi( q );
983 if( opt.tickets < 0 || opt.tickets > 1 )
984 goto usage;
985 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100986 else if( strcmp( p, "ticket_timeout" ) == 0 )
987 {
988 opt.ticket_timeout = atoi( q );
989 if( opt.ticket_timeout < 0 )
990 goto usage;
991 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100992 else if( strcmp( p, "cache_max" ) == 0 )
993 {
994 opt.cache_max = atoi( q );
995 if( opt.cache_max < 0 )
996 goto usage;
997 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100998 else if( strcmp( p, "cache_timeout" ) == 0 )
999 {
1000 opt.cache_timeout = atoi( q );
1001 if( opt.cache_timeout < 0 )
1002 goto usage;
1003 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001004 else if( strcmp( p, "sni" ) == 0 )
1005 {
1006 opt.sni = q;
1007 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001008 else
1009 goto usage;
1010 }
1011
Paul Bakkerc73079a2014-04-25 16:34:30 +02001012#if defined(POLARSSL_DEBUG_C)
1013 debug_set_threshold( opt.debug_level );
1014#endif
1015
Paul Bakkerc1516be2013-06-29 16:01:32 +02001016 if( opt.force_ciphersuite[0] > 0 )
1017 {
1018 const ssl_ciphersuite_t *ciphersuite_info;
1019 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1020
Paul Bakker5b55b792013-07-19 13:43:43 +02001021 if( opt.max_version != -1 &&
1022 ciphersuite_info->min_minor_ver > opt.max_version )
1023 {
Rich Evansf90016a2015-01-19 14:26:37 +00001024 polarssl_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakker5b55b792013-07-19 13:43:43 +02001025 ret = 2;
1026 goto usage;
1027 }
1028 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001029 ciphersuite_info->max_minor_ver < opt.min_version )
1030 {
Rich Evansf90016a2015-01-19 14:26:37 +00001031 polarssl_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +02001032 ret = 2;
1033 goto usage;
1034 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001035 if( opt.max_version > ciphersuite_info->max_minor_ver )
1036 opt.max_version = ciphersuite_info->max_minor_ver;
1037 if( opt.min_version < ciphersuite_info->min_minor_ver )
1038 opt.min_version = ciphersuite_info->min_minor_ver;
Paul Bakkerc1516be2013-06-29 16:01:32 +02001039 }
1040
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001041 if( opt.version_suites != NULL )
1042 {
1043 const char *name[4] = { 0 };
1044
1045 /* Parse 4-element coma-separated list */
1046 for( i = 0, p = (char *) opt.version_suites;
1047 i < 4 && *p != '\0';
1048 i++ )
1049 {
1050 name[i] = p;
1051
1052 /* Terminate the current string and move on to next one */
1053 while( *p != ',' && *p != '\0' )
1054 p++;
1055 if( *p == ',' )
1056 *p++ = '\0';
1057 }
1058
1059 if( i != 4 )
1060 {
Rich Evansf90016a2015-01-19 14:26:37 +00001061 polarssl_printf( "too few values for version_suites\n" );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001062 ret = 1;
1063 goto exit;
1064 }
1065
1066 memset( version_suites, 0, sizeof( version_suites ) );
1067
1068 /* Get the suites identifiers from their name */
1069 for( i = 0; i < 4; i++ )
1070 {
1071 version_suites[i][0] = ssl_get_ciphersuite_id( name[i] );
1072
1073 if( version_suites[i][0] == 0 )
1074 {
Rich Evansf90016a2015-01-19 14:26:37 +00001075 polarssl_printf( "unknown ciphersuite: '%s'\n", name[i] );
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001076 ret = 2;
1077 goto usage;
1078 }
1079 }
1080 }
1081
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001082#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001083 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001084 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001085 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001086 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001087 {
Rich Evansf90016a2015-01-19 14:26:37 +00001088 polarssl_printf( "pre-shared key not valid hex\n" );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001089 goto exit;
1090 }
1091
1092 if( opt.psk_list != NULL )
1093 {
1094 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001095 {
Rich Evansf90016a2015-01-19 14:26:37 +00001096 polarssl_printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001097 goto exit;
1098 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001099 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001100#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001101
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001102#if defined(POLARSSL_SSL_ALPN)
1103 if( opt.alpn_string != NULL )
1104 {
1105 p = (char *) opt.alpn_string;
1106 i = 0;
1107
1108 /* Leave room for a final NULL in alpn_list */
1109 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
1110 {
1111 alpn_list[i++] = p;
1112
1113 /* Terminate the current string and move on to next one */
1114 while( *p != ',' && *p != '\0' )
1115 p++;
1116 if( *p == ',' )
1117 *p++ = '\0';
1118 }
1119 }
1120#endif /* POLARSSL_SSL_ALPN */
1121
Paul Bakkerfbb17802013-04-17 19:10:21 +02001122 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001123 * 0. Initialize the RNG and the session data
1124 */
Rich Evansf90016a2015-01-19 14:26:37 +00001125 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001126 fflush( stdout );
1127
1128 entropy_init( &entropy );
1129 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001130 (const unsigned char *) pers,
1131 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001132 {
Rich Evansf90016a2015-01-19 14:26:37 +00001133 polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001134 goto exit;
1135 }
1136
Rich Evansf90016a2015-01-19 14:26:37 +00001137 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001138
Paul Bakker36713e82013-09-17 13:25:29 +02001139#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001140 /*
1141 * 1.1. Load the trusted CA
1142 */
Rich Evansf90016a2015-01-19 14:26:37 +00001143 polarssl_printf( " . Loading the CA root certificate ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001144 fflush( stdout );
1145
1146#if defined(POLARSSL_FS_IO)
1147 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001148 if( strcmp( opt.ca_path, "none" ) == 0 )
1149 ret = 0;
1150 else
1151 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001152 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001153 if( strcmp( opt.ca_file, "none" ) == 0 )
1154 ret = 0;
1155 else
1156 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001157 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001158#endif
1159#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +02001160 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
1161 strlen( test_ca_list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001162#else
1163 {
1164 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +00001165 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001166 }
1167#endif
1168 if( ret < 0 )
1169 {
Rich Evansf90016a2015-01-19 14:26:37 +00001170 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001171 goto exit;
1172 }
1173
Rich Evansf90016a2015-01-19 14:26:37 +00001174 polarssl_printf( " ok (%d skipped)\n", ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001175
1176 /*
1177 * 1.2. Load own certificate and private key
1178 */
Rich Evansf90016a2015-01-19 14:26:37 +00001179 polarssl_printf( " . Loading the server cert. and key..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001180 fflush( stdout );
1181
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001182#if defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001183 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001184 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001185 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001186 if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
1187 {
Rich Evansf90016a2015-01-19 14:26:37 +00001188 polarssl_printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001189 -ret );
1190 goto exit;
1191 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001192 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001193 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001194 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001195 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001196 if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
1197 {
Rich Evansf90016a2015-01-19 14:26:37 +00001198 polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001199 goto exit;
1200 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001201 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001202 if( key_cert_init == 1 )
1203 {
Rich Evansf90016a2015-01-19 14:26:37 +00001204 polarssl_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001205 goto exit;
1206 }
1207
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001208 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001209 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001210 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001211 if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
1212 {
Rich Evansf90016a2015-01-19 14:26:37 +00001213 polarssl_printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001214 -ret );
1215 goto exit;
1216 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001217 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001218 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001219 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001220 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001221 if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
1222 {
Rich Evansf90016a2015-01-19 14:26:37 +00001223 polarssl_printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n",
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001224 -ret );
1225 goto exit;
1226 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001227 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001228 if( key_cert_init2 == 1 )
1229 {
Rich Evansf90016a2015-01-19 14:26:37 +00001230 polarssl_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001231 goto exit;
1232 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001233#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001234 if( key_cert_init == 0 &&
1235 strcmp( opt.crt_file, "none" ) != 0 &&
1236 strcmp( opt.key_file, "none" ) != 0 &&
1237 key_cert_init2 == 0 &&
1238 strcmp( opt.crt_file2, "none" ) != 0 &&
1239 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001240 {
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001241#if !defined(POLARSSL_CERTS_C)
Rich Evansf90016a2015-01-19 14:26:37 +00001242 polarssl_printf( "Not certificated or key provided, and \n"
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001243 "POLARSSL_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001244 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001245#else
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001246#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001247 if( ( ret = x509_crt_parse( &srvcert,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001248 (const unsigned char *) test_srv_crt_rsa,
1249 strlen( test_srv_crt_rsa ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001250 {
Rich Evansf90016a2015-01-19 14:26:37 +00001251 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001252 goto exit;
1253 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001254 if( ( ret = pk_parse_key( &pkey,
1255 (const unsigned char *) test_srv_key_rsa,
1256 strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001257 {
Rich Evansf90016a2015-01-19 14:26:37 +00001258 polarssl_printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001259 goto exit;
1260 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001261 key_cert_init = 2;
1262#endif /* POLARSSL_RSA_C */
1263#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001264 if( ( ret = x509_crt_parse( &srvcert2,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001265 (const unsigned char *) test_srv_crt_ec,
1266 strlen( test_srv_crt_ec ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001267 {
Rich Evansf90016a2015-01-19 14:26:37 +00001268 polarssl_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001269 goto exit;
1270 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001271 if( ( ret = pk_parse_key( &pkey2,
1272 (const unsigned char *) test_srv_key_ec,
1273 strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001274 {
Rich Evansf90016a2015-01-19 14:26:37 +00001275 polarssl_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001276 goto exit;
1277 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001278 key_cert_init2 = 2;
1279#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001280#endif /* POLARSSL_CERTS_C */
1281 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001282
Rich Evansf90016a2015-01-19 14:26:37 +00001283 polarssl_printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001284#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001285
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001286#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1287 if( opt.dhm_file != NULL )
1288 {
Rich Evansf90016a2015-01-19 14:26:37 +00001289 polarssl_printf( " . Loading DHM parameters..." );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001290 fflush( stdout );
1291
1292 if( ( ret = dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
1293 {
Rich Evansf90016a2015-01-19 14:26:37 +00001294 polarssl_printf( " failed\n ! dhm_parse_dhmfile returned -0x%04X\n\n",
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001295 -ret );
1296 goto exit;
1297 }
1298
Rich Evansf90016a2015-01-19 14:26:37 +00001299 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001300 }
1301#endif
1302
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001303#if defined(POLARSSL_SNI)
1304 if( opt.sni != NULL )
1305 {
Rich Evansf90016a2015-01-19 14:26:37 +00001306 polarssl_printf( " . Setting up SNI information..." );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001307 fflush( stdout );
1308
1309 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
1310 {
Rich Evansf90016a2015-01-19 14:26:37 +00001311 polarssl_printf( " failed\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001312 goto exit;
1313 }
1314
Rich Evansf90016a2015-01-19 14:26:37 +00001315 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001316 }
1317#endif /* POLARSSL_SNI */
1318
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001319 /*
1320 * 2. Setup the listening TCP socket
1321 */
Rich Evansf90016a2015-01-19 14:26:37 +00001322 polarssl_printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001323 fflush( stdout );
1324
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001325 if( ( ret = net_bind( &listen_fd, opt.server_addr,
1326 opt.server_port ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001327 {
Rich Evansf90016a2015-01-19 14:26:37 +00001328 polarssl_printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001329 goto exit;
1330 }
1331
Rich Evansf90016a2015-01-19 14:26:37 +00001332 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001333
1334 /*
1335 * 3. Setup stuff
1336 */
Rich Evansf90016a2015-01-19 14:26:37 +00001337 polarssl_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001338 fflush( stdout );
1339
1340 if( ( ret = ssl_init( &ssl ) ) != 0 )
1341 {
Rich Evansf90016a2015-01-19 14:26:37 +00001342 polarssl_printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001343 goto exit;
1344 }
1345
1346 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001347 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001348
Paul Bakker05decb22013-08-15 13:33:48 +02001349#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001350 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1351 {
Rich Evansf90016a2015-01-19 14:26:37 +00001352 polarssl_printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001353 goto exit;
1354 };
Paul Bakker05decb22013-08-15 13:33:48 +02001355#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001356
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01001357#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1358 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
1359 ssl_set_truncated_hmac( &ssl, opt.trunc_hmac );
1360#endif
1361
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001362#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1363 if( opt.extended_ms != DFL_EXTENDED_MS )
1364 ssl_set_extended_master_secret( &ssl, opt.extended_ms );
1365#endif
1366
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001367#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1368 if( opt.etm != DFL_ETM )
1369 ssl_set_encrypt_then_mac( &ssl, opt.etm );
1370#endif
1371
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001372#if defined(POLARSSL_SSL_ALPN)
1373 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001374 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1375 {
Rich Evansf90016a2015-01-19 14:26:37 +00001376 polarssl_printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001377 goto exit;
1378 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001379#endif
1380
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001381 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
1382 ssl_set_dbg( &ssl, my_debug, stdout );
1383
Paul Bakker0a597072012-09-25 21:55:46 +00001384#if defined(POLARSSL_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001385 if( opt.cache_max != -1 )
1386 ssl_cache_set_max_entries( &cache, opt.cache_max );
1387
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001388 if( opt.cache_timeout != -1 )
1389 ssl_cache_set_timeout( &cache, opt.cache_timeout );
1390
Paul Bakker0a597072012-09-25 21:55:46 +00001391 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
1392 ssl_cache_set, &cache );
1393#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001394
Paul Bakkera503a632013-08-14 13:48:06 +02001395#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001396 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1397 {
Rich Evansf90016a2015-01-19 14:26:37 +00001398 polarssl_printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001399 goto exit;
1400 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001401
1402 if( opt.ticket_timeout != -1 )
1403 ssl_set_session_ticket_lifetime( &ssl, opt.ticket_timeout );
Paul Bakkera503a632013-08-14 13:48:06 +02001404#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001405
Paul Bakker41c83d32013-03-20 14:39:14 +01001406 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001407 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001408 else
1409 ssl_set_arc4_support( &ssl, opt.arc4 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001410
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001411 if( opt.version_suites != NULL )
1412 {
1413 ssl_set_ciphersuites_for_version( &ssl, version_suites[0],
1414 SSL_MAJOR_VERSION_3,
1415 SSL_MINOR_VERSION_0 );
1416 ssl_set_ciphersuites_for_version( &ssl, version_suites[1],
1417 SSL_MAJOR_VERSION_3,
1418 SSL_MINOR_VERSION_1 );
1419 ssl_set_ciphersuites_for_version( &ssl, version_suites[2],
1420 SSL_MAJOR_VERSION_3,
1421 SSL_MINOR_VERSION_2 );
1422 ssl_set_ciphersuites_for_version( &ssl, version_suites[3],
1423 SSL_MAJOR_VERSION_3,
1424 SSL_MINOR_VERSION_3 );
1425 }
1426
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001427 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
1428 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001429#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001430 ssl_set_renegotiation( &ssl, opt.renegotiation );
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001431
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001432 if( opt.renego_delay != DFL_RENEGO_DELAY )
1433 ssl_set_renegotiation_enforced( &ssl, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001434
Manuel Pégourié-Gonnard590f4162014-11-05 14:23:03 +01001435 if( opt.renego_period != DFL_RENEGO_PERIOD )
1436 {
1437 renego_period[7] = opt.renego_period;
1438 ssl_set_renegotiation_period( &ssl, renego_period );
1439 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001440#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001441
Paul Bakker36713e82013-09-17 13:25:29 +02001442#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001443 if( strcmp( opt.ca_path, "none" ) != 0 &&
1444 strcmp( opt.ca_file, "none" ) != 0 )
1445 {
1446 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
1447 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001448 if( key_cert_init )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001449 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
1450 {
Rich Evansf90016a2015-01-19 14:26:37 +00001451 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001452 goto exit;
1453 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001454 if( key_cert_init2 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001455 if( ( ret = ssl_set_own_cert( &ssl, &srvcert2, &pkey2 ) ) != 0 )
1456 {
Rich Evansf90016a2015-01-19 14:26:37 +00001457 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001458 goto exit;
1459 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001460#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001461
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001462#if defined(POLARSSL_SNI)
1463 if( opt.sni != NULL )
1464 ssl_set_sni( &ssl, sni_callback, sni_info );
1465#endif
1466
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001467#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001468 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
1469 {
1470 ret = ssl_set_psk( &ssl, psk, psk_len,
1471 (const unsigned char *) opt.psk_identity,
1472 strlen( opt.psk_identity ) );
1473 if( ret != 0 )
1474 {
Rich Evansf90016a2015-01-19 14:26:37 +00001475 polarssl_printf( " failed\n ssl_set_psk returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001476 goto exit;
1477 }
1478 }
1479
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001480 if( opt.psk_list != NULL )
1481 ssl_set_psk_cb( &ssl, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02001482#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001483
1484#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00001485 /*
1486 * Use different group than default DHM group
1487 */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001488#if defined(POLARSSL_FS_IO)
1489 if( opt.dhm_file != NULL )
1490 ret = ssl_set_dh_param_ctx( &ssl, &dhm );
1491 else
1492#endif
1493 ret = ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
1494 POLARSSL_DHM_RFC5114_MODP_2048_G );
1495
1496 if( ret != 0 )
1497 {
Rich Evansf90016a2015-01-19 14:26:37 +00001498 polarssl_printf( " failed\n ssl_set_dh_param returned -0x%04X\n\n", - ret );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001499 goto exit;
1500 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001501#endif
1502
Paul Bakker1d29fb52012-09-28 13:28:45 +00001503 if( opt.min_version != -1 )
1504 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1505
Paul Bakkerc1516be2013-06-29 16:01:32 +02001506 if( opt.max_version != -1 )
1507 ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1508
Rich Evansf90016a2015-01-19 14:26:37 +00001509 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001510
1511reset:
Manuel Pégourié-Gonnard3a3066c2014-09-20 12:03:00 +02001512#if !defined(_WIN32)
1513 if( received_sigterm )
1514 {
1515 printf( " interrupted by SIGTERM\n" );
1516 ret = 0;
1517 goto exit;
1518 }
1519#endif
1520
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001521#ifdef POLARSSL_ERROR_C
1522 if( ret != 0 )
1523 {
1524 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001525 polarssl_strerror( ret, error_buf, 100 );
Rich Evansf90016a2015-01-19 14:26:37 +00001526 polarssl_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001527 }
1528#endif
1529
1530 if( client_fd != -1 )
1531 net_close( client_fd );
1532
1533 ssl_session_reset( &ssl );
1534
1535 /*
1536 * 3. Wait until a client connects
1537 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001538 client_fd = -1;
1539
Rich Evansf90016a2015-01-19 14:26:37 +00001540 polarssl_printf( " . Waiting for a remote connection ..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001541 fflush( stdout );
1542
1543 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
1544 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02001545#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001546 if( received_sigterm )
1547 {
Rich Evansf90016a2015-01-19 14:26:37 +00001548 polarssl_printf( " interrupted by signal\n" );
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001549 ret = 0;
1550 goto exit;
1551 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02001552#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001553
Rich Evansf90016a2015-01-19 14:26:37 +00001554 polarssl_printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001555 goto exit;
1556 }
1557
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001558 if( opt.nbio > 0 )
1559 ret = net_set_nonblock( client_fd );
1560 else
1561 ret = net_set_block( client_fd );
1562 if( ret != 0 )
1563 {
Rich Evansf90016a2015-01-19 14:26:37 +00001564 polarssl_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001565 goto exit;
1566 }
1567
1568 if( opt.nbio == 2 )
1569 ssl_set_bio( &ssl, my_recv, &client_fd, my_send, &client_fd );
1570 else
1571 ssl_set_bio( &ssl, net_recv, &client_fd, net_send, &client_fd );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001572
Rich Evansf90016a2015-01-19 14:26:37 +00001573 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001574
1575 /*
1576 * 4. Handshake
1577 */
Rich Evansf90016a2015-01-19 14:26:37 +00001578 polarssl_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001579 fflush( stdout );
1580
1581 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1582 {
1583 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1584 {
Rich Evansf90016a2015-01-19 14:26:37 +00001585 polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001586 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001587 }
1588 }
1589
Rich Evansf90016a2015-01-19 14:26:37 +00001590 polarssl_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Manuel Pégourié-Gonnardc580a002014-02-12 10:15:30 +01001591 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001592
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001593#if defined(POLARSSL_SSL_ALPN)
1594 if( opt.alpn_string != NULL )
1595 {
1596 const char *alp = ssl_get_alpn_protocol( &ssl );
Rich Evansf90016a2015-01-19 14:26:37 +00001597 polarssl_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001598 alp ? alp : "(none)" );
1599 }
1600#endif
1601
Paul Bakker36713e82013-09-17 13:25:29 +02001602#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001603 /*
1604 * 5. Verify the server certificate
1605 */
Rich Evansf90016a2015-01-19 14:26:37 +00001606 polarssl_printf( " . Verifying peer X.509 certificate..." );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001607
1608 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1609 {
Rich Evansf90016a2015-01-19 14:26:37 +00001610 polarssl_printf( " failed\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001611
Paul Bakkerb0550d92012-10-30 07:51:03 +00001612 if( !ssl_get_peer_cert( &ssl ) )
Rich Evansf90016a2015-01-19 14:26:37 +00001613 polarssl_printf( " ! no client certificate sent\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001614
1615 if( ( ret & BADCERT_EXPIRED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001616 polarssl_printf( " ! client certificate has expired\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001617
1618 if( ( ret & BADCERT_REVOKED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001619 polarssl_printf( " ! client certificate has been revoked\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001620
1621 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001622 polarssl_printf( " ! self-signed or not signed by a trusted CA\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001623
Rich Evansf90016a2015-01-19 14:26:37 +00001624 polarssl_printf( "\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001625 }
1626 else
Rich Evansf90016a2015-01-19 14:26:37 +00001627 polarssl_printf( " ok\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001628
Paul Bakkerb0550d92012-10-30 07:51:03 +00001629 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001630 {
Rich Evansf90016a2015-01-19 14:26:37 +00001631 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001632 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1633 ssl_get_peer_cert( &ssl ) );
Rich Evansf90016a2015-01-19 14:26:37 +00001634 polarssl_printf( "%s\n", buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001635 }
Paul Bakker36713e82013-09-17 13:25:29 +02001636#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001637
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001638 exchanges_left = opt.exchanges;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001639data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001640 /*
1641 * 6. Read the HTTP Request
1642 */
Rich Evansf90016a2015-01-19 14:26:37 +00001643 polarssl_printf( " < Read from client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001644 fflush( stdout );
1645
1646 do
1647 {
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001648 int terminated = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001649 len = sizeof( buf ) - 1;
1650 memset( buf, 0, sizeof( buf ) );
1651 ret = ssl_read( &ssl, buf, len );
1652
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001653 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1654 ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001655 continue;
1656
1657 if( ret <= 0 )
1658 {
1659 switch( ret )
1660 {
1661 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Rich Evansf90016a2015-01-19 14:26:37 +00001662 polarssl_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001663 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001664
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001665 case 0:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001666 case POLARSSL_ERR_NET_CONN_RESET:
Rich Evansf90016a2015-01-19 14:26:37 +00001667 polarssl_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001668 ret = POLARSSL_ERR_NET_CONN_RESET;
1669 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001670
1671 default:
Rich Evansf90016a2015-01-19 14:26:37 +00001672 polarssl_printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001673 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001674 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001675 }
1676
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001677 if( ssl_get_bytes_avail( &ssl ) == 0 )
1678 {
1679 len = ret;
1680 buf[len] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +00001681 polarssl_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001682
1683 /* 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( buf[len - 1] == '\n' )
1686 terminated = 1;
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001687 }
1688 else
1689 {
1690 int extra_len, ori_len;
1691 unsigned char *larger_buf;
1692
1693 ori_len = ret;
1694 extra_len = ssl_get_bytes_avail( &ssl );
1695
1696 larger_buf = polarssl_malloc( ori_len + extra_len + 1 );
1697 if( larger_buf == NULL )
1698 {
Rich Evansf90016a2015-01-19 14:26:37 +00001699 polarssl_printf( " ! memory allocation failed\n" );
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001700 ret = 1;
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001701 goto reset;
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001702 }
1703
1704 memset( larger_buf, 0, ori_len + extra_len );
1705 memcpy( larger_buf, buf, ori_len );
1706
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +02001707 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001708 ret = ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +02001709 if( ret != extra_len ||
1710 ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001711 {
Rich Evansf90016a2015-01-19 14:26:37 +00001712 polarssl_printf( " ! ssl_read failed on cached data\n" );
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001713 ret = 1;
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001714 goto reset;
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001715 }
1716
1717 larger_buf[ori_len + extra_len] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +00001718 polarssl_printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnard0669f272014-06-18 13:07:20 +02001719 ori_len + extra_len, ori_len, extra_len,
1720 (char *) larger_buf );
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001721
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001722 /* End of message should be detected according to the syntax of the
1723 * application protocol (eg HTTP), just use a dummy test here. */
1724 if( larger_buf[ori_len + extra_len - 1] == '\n' )
1725 terminated = 1;
1726
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001727 polarssl_free( larger_buf );
1728 }
1729
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001730 if( terminated )
1731 {
1732 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001733 break;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001734 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001735 }
1736 while( 1 );
1737
1738 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001739 * 7a. Request renegotiation while client is waiting for input from us.
1740 * (only if we're going to exhange more data afterwards)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001741 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001742#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001743 if( opt.renegotiate && exchanges_left > 1 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001744 {
Rich Evansf90016a2015-01-19 14:26:37 +00001745 polarssl_printf( " . Requestion renegotiation..." );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001746 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001747
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001748 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
1749 {
1750 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1751 ret != POLARSSL_ERR_NET_WANT_WRITE )
1752 {
Rich Evansf90016a2015-01-19 14:26:37 +00001753 polarssl_printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001754 goto reset;
1755 }
1756 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001757
Rich Evansf90016a2015-01-19 14:26:37 +00001758 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001759 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001760#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001761
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001762 /*
1763 * 7. Write the 200 Response
1764 */
Rich Evansf90016a2015-01-19 14:26:37 +00001765 polarssl_printf( " > Write to client:" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001766 fflush( stdout );
1767
1768 len = sprintf( (char *) buf, HTTP_RESPONSE,
1769 ssl_get_ciphersuite( &ssl ) );
1770
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001771 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001772 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001773 while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001774 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001775 if( ret == POLARSSL_ERR_NET_CONN_RESET )
1776 {
Rich Evansf90016a2015-01-19 14:26:37 +00001777 polarssl_printf( " failed\n ! peer closed the connection\n\n" );
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001778 goto reset;
1779 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001780
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001781 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1782 {
Rich Evansf90016a2015-01-19 14:26:37 +00001783 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnard250b1ca2014-08-15 10:59:03 +02001784 goto reset;
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001785 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001786 }
1787 }
1788
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001789 buf[written] = '\0';
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001790 printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Rich Evansf90016a2015-01-19 14:26:37 +00001791 polarssl_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Manuel Pégourié-Gonnarda92ed482015-01-14 10:46:08 +01001792 ret = 0;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001793
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001794 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001795 * 7b. Continue doing data exchanges?
1796 */
Manuel Pégourié-Gonnard6a0017b2015-01-22 10:33:29 +00001797 if( --exchanges_left > 0 )
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001798 goto data_exchange;
1799
1800 /*
1801 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001802 */
1803close_notify:
Rich Evansf90016a2015-01-19 14:26:37 +00001804 polarssl_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001805
Manuel Pégourié-Gonnard34377b12015-01-22 10:46:46 +00001806 /* No error checking, the connection might be closed already */
1807 do ret = ssl_close_notify( &ssl );
1808 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
1809 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001810
Rich Evansf90016a2015-01-19 14:26:37 +00001811 polarssl_printf( " done\n" );
1812
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001813 goto reset;
1814
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001815 /*
1816 * Cleanup and exit
1817 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001818exit:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001819#ifdef POLARSSL_ERROR_C
1820 if( ret != 0 )
1821 {
1822 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001823 polarssl_strerror( ret, error_buf, 100 );
Rich Evansf90016a2015-01-19 14:26:37 +00001824 polarssl_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001825 }
1826#endif
1827
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01001828 printf( " . Cleaning up..." );
1829 fflush( stdout );
1830
Paul Bakker0c226102014-04-17 16:02:36 +02001831 if( client_fd != -1 )
1832 net_close( client_fd );
1833
Paul Bakkera317a982014-06-18 16:44:11 +02001834#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1835 dhm_free( &dhm );
1836#endif
Paul Bakker36713e82013-09-17 13:25:29 +02001837#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker36713e82013-09-17 13:25:29 +02001838 x509_crt_free( &cacert );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001839 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02001840 pk_free( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001841 x509_crt_free( &srvcert2 );
1842 pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02001843#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001844#if defined(POLARSSL_SNI)
1845 sni_free( sni_info );
1846#endif
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02001847#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1848 psk_free( psk_info );
1849#endif
1850#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1851 dhm_free( &dhm );
1852#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001853
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001854 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02001855 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02001856 entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001857
Paul Bakker0a597072012-09-25 21:55:46 +00001858#if defined(POLARSSL_SSL_CACHE_C)
1859 ssl_cache_free( &cache );
1860#endif
1861
Paul Bakker1337aff2013-09-29 14:45:34 +02001862#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1863#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker82024bf2013-07-04 11:52:32 +02001864 memory_buffer_alloc_status();
1865#endif
Paul Bakker1337aff2013-09-29 14:45:34 +02001866 memory_buffer_alloc_free();
1867#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02001868
Manuel Pégourié-Gonnardf29e5de2014-11-21 11:54:41 +01001869 printf( " done.\n" );
1870
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001871#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +00001872 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001873 fflush( stdout ); getchar();
1874#endif
1875
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001876 // Shell can not handle large exit numbers -> 1 for errors
1877 if( ret < 0 )
1878 ret = 1;
1879
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001880 return( ret );
1881}
1882#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
1883 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
1884 POLARSSL_CTR_DRBG_C */