blob: 39e3a41a8a290ac062776d62af43f07741d20467 [file] [log] [blame]
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001/*
2 * SSL client with options
3 *
Paul Bakker3c5ef712013-06-25 16:37:45 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000031
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020032#if !defined(POLARSSL_ENTROPY_C) || \
33 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
34 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
35#include <stdio.h>
36int main( int argc, char *argv[] )
37{
38 ((void) argc);
39 ((void) argv);
40
41 printf("POLARSSL_ENTROPY_C and/or "
42 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
43 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
44 return( 0 );
45}
46#else
47
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010048#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) && defined(POLARSSL_FS_IO)
49#define POLARSSL_SNI
50#endif
51
52#if defined(POLARSSL_PLATFORM_C)
53#include "polarssl/platform.h"
54#else
55#define polarssl_malloc malloc
56#define polarssl_free free
57#endif
58
Paul Bakkerb60b95f2012-09-25 09:05:17 +000059#if defined(_WIN32)
60#include <windows.h>
61#endif
62
63#include <string.h>
64#include <stdlib.h>
65#include <stdio.h>
Paul Bakkerc1283d32014-08-18 11:05:51 +020066
67#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +020068#include <signal.h>
Paul Bakkerc1283d32014-08-18 11:05:51 +020069#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000070
Paul Bakkerb60b95f2012-09-25 09:05:17 +000071#include "polarssl/net.h"
72#include "polarssl/ssl.h"
73#include "polarssl/entropy.h"
74#include "polarssl/ctr_drbg.h"
75#include "polarssl/certs.h"
76#include "polarssl/x509.h"
77#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020078#include "polarssl/debug.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000079
Paul Bakker0a597072012-09-25 21:55:46 +000080#if defined(POLARSSL_SSL_CACHE_C)
81#include "polarssl/ssl_cache.h"
82#endif
83
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +020084#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020085#include "polarssl/ssl_cookie.h"
86#endif
87
Paul Bakker82024bf2013-07-04 11:52:32 +020088#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
89#include "polarssl/memory.h"
90#endif
91
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +010092#define DFL_SERVER_ADDR NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000093#define DFL_SERVER_PORT 4433
Paul Bakkerb60b95f2012-09-25 09:05:17 +000094#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010095#define DFL_NBIO 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020096#define DFL_READ_TIMEOUT 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +000097#define DFL_CA_FILE ""
98#define DFL_CA_PATH ""
99#define DFL_CRT_FILE ""
100#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200101#define DFL_CRT_FILE2 ""
102#define DFL_KEY_FILE2 ""
Paul Bakkerfbb17802013-04-17 19:10:21 +0200103#define DFL_PSK ""
104#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200105#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000106#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200107#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +0200108#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000109#define DFL_ALLOW_LEGACY SSL_LEGACY_NO_RENEGOTIATION
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100110#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200111#define DFL_RENEGO_DELAY -2
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200112#define DFL_EXCHANGES 1
Paul Bakker1d29fb52012-09-28 13:28:45 +0000113#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200114#define DFL_MAX_VERSION -1
Paul Bakker91ebfb52012-11-23 14:04:08 +0100115#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200116#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200117#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100118#define DFL_TICKET_TIMEOUT -1
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100119#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100120#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100121#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200122#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200123#define DFL_DHM_FILE NULL
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100124#define DFL_TRANSPORT SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200125#define DFL_COOKIES 1
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200126#define DFL_ANTI_REPLAY -1
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200127#define DFL_HS_TO_MIN 0
128#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200129#define DFL_BADMAC_LIMIT -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000130
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200131#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
132 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
133 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
134 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
135 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
136 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
137 "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 +0200138
Paul Bakker8e714d72013-07-18 11:05:13 +0200139/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
140 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000141#define HTTP_RESPONSE \
142 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
143 "<h2>PolarSSL Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200144 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000145
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200146/*
147 * Size of the basic I/O buffer. Able to hold our default response.
148 *
149 * You will need to adapt the ssl_get_bytes_avail() test in ssl-opt.sh
150 * if you change this value to something outside the range <= 100 or > 500
151 */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200152#define IO_BUF_LEN 200
153
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000154/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000155 * global options
156 */
157struct options
158{
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100159 const char *server_addr; /* address on which the ssl service runs */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000160 int server_port; /* port on which the ssl service runs */
161 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100162 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200163 uint32_t read_timeout; /* timeout on ssl_read() in milliseconds */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200164 const char *ca_file; /* the file with the CA certificate(s) */
165 const char *ca_path; /* the path with the CA certificate(s) reside */
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200166 const char *crt_file; /* the file with the server certificate */
167 const char *key_file; /* the file with the server key */
168 const char *crt_file2; /* the file with the 2nd server certificate */
169 const char *key_file2; /* the file with the 2nd server key */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200170 const char *psk; /* the pre-shared key */
171 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200172 char *psk_list; /* list of PSK id/key pairs for callback */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000173 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200174 const char *version_suites; /* per-version ciphersuites */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000175 int renegotiation; /* enable / disable renegotiation */
176 int allow_legacy; /* allow legacy renegotiation */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100177 int renegotiate; /* attempt renegotiation? */
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200178 int renego_delay; /* delay before enforcing renegotiation */
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200179 int exchanges; /* number of data exchanges */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000180 int min_version; /* minimum protocol version accepted */
Paul Bakkerc1516be2013-06-29 16:01:32 +0200181 int max_version; /* maximum protocol version accepted */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100182 int auth_mode; /* verify mode for connection */
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200183 unsigned char mfl_code; /* code for maximum fragment length */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200184 int tickets; /* enable / disable session tickets */
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100185 int ticket_timeout; /* session ticket lifetime */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100186 int cache_max; /* max number of session cache entries */
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100187 int cache_timeout; /* expiration delay of session cache entries */
Paul Bakker1ebc0c52014-05-22 15:47:58 +0200188 char *sni; /* string describing sni information */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200189 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200190 const char *dhm_file; /* the file with the DH parameters */
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100191 int transport; /* TLS or DTLS? */
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200192 int cookies; /* Use cookies for DTLS? -1 to break them */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200193 int anti_replay; /* Use anti-replay for DTLS? -1 for default */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200194 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
195 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200196 int badmac_limit; /* Limit of records with bad MAC */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000197} opt;
198
Paul Bakker3c5ef712013-06-25 16:37:45 +0200199static void my_debug( void *ctx, int level, const char *str )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000200{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200201 ((void) level);
202
203 fprintf( (FILE *) ctx, "%s", str );
204 fflush( (FILE *) ctx );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000205}
206
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100207/*
208 * Test recv/send functions that make sure each try returns
209 * WANT_READ/WANT_WRITE at least once before sucesseding
210 */
211static int my_recv( void *ctx, unsigned char *buf, size_t len )
212{
213 static int first_try = 1;
214 int ret;
215
216 if( first_try )
217 {
218 first_try = 0;
219 return( POLARSSL_ERR_NET_WANT_READ );
220 }
221
222 ret = net_recv( ctx, buf, len );
223 if( ret != POLARSSL_ERR_NET_WANT_READ )
224 first_try = 1; /* Next call will be a new operation */
225 return( ret );
226}
227
228static int my_send( void *ctx, const unsigned char *buf, size_t len )
229{
230 static int first_try = 1;
231 int ret;
232
233 if( first_try )
234 {
235 first_try = 0;
236 return( POLARSSL_ERR_NET_WANT_WRITE );
237 }
238
239 ret = net_send( ctx, buf, len );
240 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
241 first_try = 1; /* Next call will be a new operation */
242 return( ret );
243}
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200244
245#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000246#if defined(POLARSSL_FS_IO)
247#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100248 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
249 " default: \"\" (pre-loaded)\n" \
250 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
251 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
252 " 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 +0200253 " default: see note after key_file2\n" \
254 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200255 " 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 +0200256 " default: see note after key_file2\n" \
257 " key_file2=%%s default: see note below\n" \
258 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200259 " preloaded certificate(s) and key(s) are used if available\n" \
260 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
261 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000262#else
263#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200264 "\n" \
265 " No file operations available (POLARSSL_FS_IO not defined)\n" \
266 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000267#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200268#else
269#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200270#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200271
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200272#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200273#define USAGE_PSK \
274 " psk=%%s default: \"\" (in hex, without 0x)\n" \
275 " psk_identity=%%s default: \"Client_identity\"\n"
276#else
277#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200278#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000279
Paul Bakkera503a632013-08-14 13:48:06 +0200280#if defined(POLARSSL_SSL_SESSION_TICKETS)
281#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100282 " tickets=%%d default: 1 (enabled)\n" \
283 " ticket_timeout=%%d default: ticket default (1d)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200284#else
285#define USAGE_TICKETS ""
286#endif /* POLARSSL_SSL_SESSION_TICKETS */
287
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100288#if defined(POLARSSL_SSL_CACHE_C)
289#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100290 " cache_max=%%d default: cache default (50)\n" \
291 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100292#else
293#define USAGE_CACHE ""
294#endif /* POLARSSL_SSL_CACHE_C */
295
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100296#if defined(POLARSSL_SNI)
297#define USAGE_SNI \
298 " sni=%%s name1,cert1,key1[,name2,cert2,key2[,...]]\n" \
299 " default: disabled\n"
300#else
301#define USAGE_SNI ""
302#endif /* POLARSSL_SNI */
303
Paul Bakker05decb22013-08-15 13:33:48 +0200304#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
305#define USAGE_MAX_FRAG_LEN \
306 " max_frag_len=%%d default: 16384 (tls default)\n" \
307 " options: 512, 1024, 2048, 4096\n"
308#else
309#define USAGE_MAX_FRAG_LEN ""
310#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
311
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200312#if defined(POLARSSL_SSL_ALPN)
313#define USAGE_ALPN \
314 " alpn=%%s default: \"\" (disabled)\n" \
315 " example: spdy/1,http/1.1\n"
316#else
317#define USAGE_ALPN ""
318#endif /* POLARSSL_SSL_ALPN */
319
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200320#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
321#define USAGE_COOKIES \
322 " cookies=0/1/-1 default: 1 (enabled)\n" \
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200323 " 0: disabled, -1: library default (broken)\n"
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200324#else
325#define USAGE_COOKIES ""
326#endif
327
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200328#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
329#define USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200330 " anti_replay=0/1 default: (library default: enabled)\n"
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200331#else
332#define USAGE_ANTI_REPLAY ""
333#endif
334
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200335#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
336#define USAGE_BADMAC_LIMIT \
337 " badmac_limit=%%d default: (library default: disabled)\n"
338#else
339#define USAGE_BADMAC_LIMIT ""
340#endif
341
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200342#if defined(POLARSSL_SSL_PROTO_DTLS)
343#define USAGE_DTLS \
344 " dtls=%%d default: 0 (TLS)\n" \
345 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
346 " range of DTLS handshake timeouts in millisecs\n"
347#else
348#define USAGE_DTLS ""
349#endif
350
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000351#define USAGE \
352 "\n usage: ssl_server2 param=<>...\n" \
353 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100354 " server_addr=%%d default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000355 " server_port=%%d default: 4433\n" \
356 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100357 " nbio=%%d default: 0 (blocking I/O)\n" \
358 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200359 " read_timeout=%%d default: 0 (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100360 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200361 USAGE_DTLS \
362 USAGE_COOKIES \
363 USAGE_ANTI_REPLAY \
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200364 USAGE_BADMAC_LIMIT \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200365 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100366 " auth_mode=%%s default: \"optional\"\n" \
367 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000368 USAGE_IO \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100369 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100370 "\n" \
371 USAGE_PSK \
372 "\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000373 " renegotiation=%%d default: 1 (enabled)\n" \
374 " allow_legacy=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100375 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200376 " renego_delay=%%d default: -2 (library default)\n" \
377 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200378 "\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100379 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100380 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100381 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200382 USAGE_ALPN \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100383 "\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000384 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200385 " max_version=%%s default: \"tls1_2\"\n" \
386 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100387 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200388 "\n" \
389 " version_suites=a,b,c,d per-version ciphersuites\n" \
390 " in order from ssl3 to tls1_2\n" \
391 " default: all enabled\n" \
392 " force_ciphersuite=<name> default: all enabled\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000393 " acceptable ciphersuite names:\n"
394
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200395/*
396 * Used by sni_parse and psk_parse to handle coma-separated lists
397 */
398#define GET_ITEM( dst ) \
399 dst = p; \
400 while( *p != ',' ) \
401 if( ++p > end ) \
402 return( NULL ); \
403 *p++ = '\0';
404
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100405#if defined(POLARSSL_SNI)
406typedef struct _sni_entry sni_entry;
407
408struct _sni_entry {
409 const char *name;
410 x509_crt *cert;
411 pk_context *key;
412 sni_entry *next;
413};
414
415/*
416 * Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]]
417 * into a usable sni_entry list.
418 *
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200419 * Modifies the input string! This is not production quality!
420 * (leaks memory if parsing fails, no error reporting, ...)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100421 */
422sni_entry *sni_parse( char *sni_string )
423{
424 sni_entry *cur = NULL, *new = NULL;
425 char *p = sni_string;
426 char *end = p;
427 char *crt_file, *key_file;
428
429 while( *end != '\0' )
430 ++end;
431 *end = ',';
432
433 while( p <= end )
434 {
435 if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL )
436 return( NULL );
437
438 memset( new, 0, sizeof( sni_entry ) );
439
440 if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ||
441 ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200442 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100443
444 x509_crt_init( new->cert );
445 pk_init( new->key );
446
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200447 GET_ITEM( new->name );
448 GET_ITEM( crt_file );
449 GET_ITEM( key_file );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100450
451 if( x509_crt_parse_file( new->cert, crt_file ) != 0 ||
452 pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200453 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100454
455 new->next = cur;
456 cur = new;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100457 }
458
459 return( cur );
460}
461
462void sni_free( sni_entry *head )
463{
464 sni_entry *cur = head, *next;
465
466 while( cur != NULL )
467 {
468 x509_crt_free( cur->cert );
469 polarssl_free( cur->cert );
470
471 pk_free( cur->key );
472 polarssl_free( cur->key );
473
474 next = cur->next;
475 polarssl_free( cur );
476 cur = next;
477 }
478}
479
480/*
481 * SNI callback.
482 */
483int sni_callback( void *p_info, ssl_context *ssl,
484 const unsigned char *name, size_t name_len )
485{
486 sni_entry *cur = (sni_entry *) p_info;
487
488 while( cur != NULL )
489 {
490 if( name_len == strlen( cur->name ) &&
491 memcmp( name, cur->name, name_len ) == 0 )
492 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200493 return( ssl_set_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100494 }
495
496 cur = cur->next;
497 }
498
499 return( -1 );
500}
501
502#endif /* POLARSSL_SNI */
503
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200504#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
505
506#define HEX2NUM( c ) \
507 if( c >= '0' && c <= '9' ) \
508 c -= '0'; \
509 else if( c >= 'a' && c <= 'f' ) \
510 c -= 'a' - 10; \
511 else if( c >= 'A' && c <= 'F' ) \
512 c -= 'A' - 10; \
513 else \
514 return( -1 );
515
516/*
517 * Convert a hex string to bytes.
518 * Return 0 on success, -1 on error.
519 */
520int unhexify( unsigned char *output, const char *input, size_t *olen )
521{
522 unsigned char c;
523 size_t j;
524
525 *olen = strlen( input );
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200526 if( *olen % 2 != 0 || *olen / 2 > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200527 return( -1 );
528 *olen /= 2;
529
530 for( j = 0; j < *olen * 2; j += 2 )
531 {
532 c = input[j];
533 HEX2NUM( c );
534 output[ j / 2 ] = c << 4;
535
536 c = input[j + 1];
537 HEX2NUM( c );
538 output[ j / 2 ] |= c;
539 }
540
541 return( 0 );
542}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200543
544typedef struct _psk_entry psk_entry;
545
546struct _psk_entry
547{
548 const char *name;
549 size_t key_len;
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200550 unsigned char key[POLARSSL_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200551 psk_entry *next;
552};
553
554/*
555 * Parse a string of pairs name1,key1[,name2,key2[,...]]
556 * into a usable psk_entry list.
557 *
558 * Modifies the input string! This is not production quality!
559 * (leaks memory if parsing fails, no error reporting, ...)
560 */
561psk_entry *psk_parse( char *psk_string )
562{
563 psk_entry *cur = NULL, *new = NULL;
564 char *p = psk_string;
565 char *end = p;
566 char *key_hex;
567
568 while( *end != '\0' )
569 ++end;
570 *end = ',';
571
572 while( p <= end )
573 {
574 if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL )
575 return( NULL );
576
577 memset( new, 0, sizeof( psk_entry ) );
578
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200579 GET_ITEM( new->name );
580 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200581
582 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
583 return( NULL );
584
585 new->next = cur;
586 cur = new;
587 }
588
589 return( cur );
590}
591
592/*
593 * Free a list of psk_entry's
594 */
595void psk_free( psk_entry *head )
596{
597 psk_entry *next;
598
599 while( head != NULL )
600 {
601 next = head->next;
602 polarssl_free( head );
603 head = next;
604 }
605}
606
607/*
608 * PSK callback
609 */
610int psk_callback( void *p_info, ssl_context *ssl,
611 const unsigned char *name, size_t name_len )
612{
613 psk_entry *cur = (psk_entry *) p_info;
614
615 while( cur != NULL )
616 {
617 if( name_len == strlen( cur->name ) &&
618 memcmp( name, cur->name, name_len ) == 0 )
619 {
620 return( ssl_set_psk( ssl, cur->key, cur->key_len,
621 name, name_len ) );
622 }
623
624 cur = cur->next;
625 }
626
627 return( -1 );
628}
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200629#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
630
Manuel Pégourié-Gonnarda9d7d032014-10-09 16:07:08 +0200631static int listen_fd, client_fd = -1;
Paul Bakkerbc3e54c2014-08-18 14:36:17 +0200632
633/* Interruption handler to ensure clean exit (for valgrind testing) */
634#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200635static int received_sigterm = 0;
636void term_handler( int sig )
637{
638 ((void) sig);
639 received_sigterm = 1;
640 net_close( listen_fd ); /* causes net_accept() to abort */
Manuel Pégourié-Gonnarda9d7d032014-10-09 16:07:08 +0200641 net_close( client_fd ); /* causes net_read() to abort */
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200642}
Paul Bakkerc1283d32014-08-18 11:05:51 +0200643#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200644
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000645int main( int argc, char *argv[] )
646{
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200647 int ret = 0, len, written, frags, exchanges;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200648 int version_suites[4][2];
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200649 unsigned char buf[IO_BUF_LEN];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200650#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200651 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +0200652 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +0200653 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +0200654#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200655 const char *pers = "ssl_server2";
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +0200656 unsigned char client_ip[16] = { 0 };
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +0200657#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200658 ssl_cookie_ctx cookie_ctx;
659#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000660
661 entropy_context entropy;
662 ctr_drbg_context ctr_drbg;
663 ssl_context ssl;
Paul Bakker36713e82013-09-17 13:25:29 +0200664#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200665 x509_crt cacert;
666 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200667 pk_context pkey;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200668 x509_crt srvcert2;
669 pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200670 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200671#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200672#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
673 dhm_context dhm;
674#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000675#if defined(POLARSSL_SSL_CACHE_C)
676 ssl_cache_context cache;
677#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100678#if defined(POLARSSL_SNI)
679 sni_entry *sni_info = NULL;
680#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200681#if defined(POLARSSL_SSL_ALPN)
682 const char *alpn_list[10];
683#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200684#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
685 unsigned char alloc_buf[100000];
686#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000687
688 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000689 char *p, *q;
690 const int *list;
691
Paul Bakker82024bf2013-07-04 11:52:32 +0200692#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
693 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
694#endif
695
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000696 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200697 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000698 */
699 listen_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200700 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200701#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200702 x509_crt_init( &cacert );
703 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200704 pk_init( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200705 x509_crt_init( &srvcert2 );
706 pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200707#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200708#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
Paul Bakkera317a982014-06-18 16:44:11 +0200709 dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200710#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000711#if defined(POLARSSL_SSL_CACHE_C)
712 ssl_cache_init( &cache );
713#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200714#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200715 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200716#endif
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +0200717#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200718 ssl_cookie_init( &cookie_ctx );
719#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000720
Paul Bakkerc1283d32014-08-18 11:05:51 +0200721#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200722 /* Abort cleanly on SIGTERM */
723 signal( SIGTERM, term_handler );
Paul Bakkerc1283d32014-08-18 11:05:51 +0200724#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200725
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000726 if( argc == 0 )
727 {
728 usage:
729 if( ret == 0 )
730 ret = 1;
731
732 printf( USAGE );
733
734 list = ssl_list_ciphersuites();
735 while( *list )
736 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200737 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200738 list++;
739 if( !*list )
740 break;
741 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000742 list++;
743 }
744 printf("\n");
745 goto exit;
746 }
747
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100748 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000749 opt.server_port = DFL_SERVER_PORT;
750 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100751 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200752 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000753 opt.ca_file = DFL_CA_FILE;
754 opt.ca_path = DFL_CA_PATH;
755 opt.crt_file = DFL_CRT_FILE;
756 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200757 opt.crt_file2 = DFL_CRT_FILE2;
758 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200759 opt.psk = DFL_PSK;
760 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200761 opt.psk_list = DFL_PSK_LIST;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000762 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200763 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000764 opt.renegotiation = DFL_RENEGOTIATION;
765 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100766 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200767 opt.renego_delay = DFL_RENEGO_DELAY;
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200768 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000769 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200770 opt.max_version = DFL_MAX_VERSION;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100771 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200772 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200773 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100774 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100775 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100776 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100777 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200778 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200779 opt.dhm_file = DFL_DHM_FILE;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100780 opt.transport = DFL_TRANSPORT;
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +0200781 opt.cookies = DFL_COOKIES;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +0200782 opt.anti_replay = DFL_ANTI_REPLAY;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200783 opt.hs_to_min = DFL_HS_TO_MIN;
784 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +0200785 opt.badmac_limit = DFL_BADMAC_LIMIT;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000786
787 for( i = 1; i < argc; i++ )
788 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000789 p = argv[i];
790 if( ( q = strchr( p, '=' ) ) == NULL )
791 goto usage;
792 *q++ = '\0';
793
794 if( strcmp( p, "server_port" ) == 0 )
795 {
796 opt.server_port = atoi( q );
797 if( opt.server_port < 1 || opt.server_port > 65535 )
798 goto usage;
799 }
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100800 else if( strcmp( p, "server_addr" ) == 0 )
801 opt.server_addr = q;
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100802 else if( strcmp( p, "dtls" ) == 0 )
803 {
804 int t = atoi( q );
805 if( t == 0 )
806 opt.transport = SSL_TRANSPORT_STREAM;
807 else if( t == 1 )
808 opt.transport = SSL_TRANSPORT_DATAGRAM;
809 else
810 goto usage;
811 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000812 else if( strcmp( p, "debug_level" ) == 0 )
813 {
814 opt.debug_level = atoi( q );
815 if( opt.debug_level < 0 || opt.debug_level > 65535 )
816 goto usage;
817 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100818 else if( strcmp( p, "nbio" ) == 0 )
819 {
820 opt.nbio = atoi( q );
821 if( opt.nbio < 0 || opt.nbio > 2 )
822 goto usage;
823 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200824 else if( strcmp( p, "read_timeout" ) == 0 )
825 opt.read_timeout = atoi( q );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000826 else if( strcmp( p, "ca_file" ) == 0 )
827 opt.ca_file = q;
828 else if( strcmp( p, "ca_path" ) == 0 )
829 opt.ca_path = q;
830 else if( strcmp( p, "crt_file" ) == 0 )
831 opt.crt_file = q;
832 else if( strcmp( p, "key_file" ) == 0 )
833 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200834 else if( strcmp( p, "crt_file2" ) == 0 )
835 opt.crt_file2 = q;
836 else if( strcmp( p, "key_file2" ) == 0 )
837 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200838 else if( strcmp( p, "dhm_file" ) == 0 )
839 opt.dhm_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200840 else if( strcmp( p, "psk" ) == 0 )
841 opt.psk = q;
842 else if( strcmp( p, "psk_identity" ) == 0 )
843 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200844 else if( strcmp( p, "psk_list" ) == 0 )
845 opt.psk_list = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000846 else if( strcmp( p, "force_ciphersuite" ) == 0 )
847 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000848 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
849
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200850 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000851 {
852 ret = 2;
853 goto usage;
854 }
855 opt.force_ciphersuite[1] = 0;
856 }
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200857 else if( strcmp( p, "version_suites" ) == 0 )
858 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000859 else if( strcmp( p, "renegotiation" ) == 0 )
860 {
861 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
862 SSL_RENEGOTIATION_DISABLED;
863 }
864 else if( strcmp( p, "allow_legacy" ) == 0 )
865 {
866 opt.allow_legacy = atoi( q );
867 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
868 goto usage;
869 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100870 else if( strcmp( p, "renegotiate" ) == 0 )
871 {
872 opt.renegotiate = atoi( q );
873 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
874 goto usage;
875 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200876 else if( strcmp( p, "renego_delay" ) == 0 )
877 {
878 opt.renego_delay = atoi( q );
879 }
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200880 else if( strcmp( p, "exchanges" ) == 0 )
881 {
882 opt.exchanges = atoi( q );
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +0200883 if( opt.exchanges < 0 )
Manuel Pégourié-Gonnard67686c42014-08-15 11:17:27 +0200884 goto usage;
885 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000886 else if( strcmp( p, "min_version" ) == 0 )
887 {
888 if( strcmp( q, "ssl3" ) == 0 )
889 opt.min_version = SSL_MINOR_VERSION_0;
890 else if( strcmp( q, "tls1" ) == 0 )
891 opt.min_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100892 else if( strcmp( q, "tls1_1" ) == 0 ||
893 strcmp( q, "dtls1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000894 opt.min_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100895 else if( strcmp( q, "tls1_2" ) == 0 ||
896 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000897 opt.min_version = SSL_MINOR_VERSION_3;
898 else
899 goto usage;
900 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200901 else if( strcmp( p, "max_version" ) == 0 )
902 {
903 if( strcmp( q, "ssl3" ) == 0 )
904 opt.max_version = SSL_MINOR_VERSION_0;
905 else if( strcmp( q, "tls1" ) == 0 )
906 opt.max_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100907 else if( strcmp( q, "tls1_1" ) == 0 ||
908 strcmp( q, "dtls1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200909 opt.max_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100910 else if( strcmp( q, "tls1_2" ) == 0 ||
911 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200912 opt.max_version = SSL_MINOR_VERSION_3;
913 else
914 goto usage;
915 }
916 else if( strcmp( p, "force_version" ) == 0 )
917 {
918 if( strcmp( q, "ssl3" ) == 0 )
919 {
920 opt.min_version = SSL_MINOR_VERSION_0;
921 opt.max_version = SSL_MINOR_VERSION_0;
922 }
923 else if( strcmp( q, "tls1" ) == 0 )
924 {
925 opt.min_version = SSL_MINOR_VERSION_1;
926 opt.max_version = SSL_MINOR_VERSION_1;
927 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100928 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200929 {
930 opt.min_version = SSL_MINOR_VERSION_2;
931 opt.max_version = SSL_MINOR_VERSION_2;
932 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100933 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakkerc1516be2013-06-29 16:01:32 +0200934 {
935 opt.min_version = SSL_MINOR_VERSION_3;
936 opt.max_version = SSL_MINOR_VERSION_3;
937 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100938 else if( strcmp( q, "dtls1" ) == 0 )
939 {
940 opt.min_version = SSL_MINOR_VERSION_2;
941 opt.max_version = SSL_MINOR_VERSION_2;
942 opt.transport = SSL_TRANSPORT_DATAGRAM;
943 }
944 else if( strcmp( q, "dtls1_2" ) == 0 )
945 {
946 opt.min_version = SSL_MINOR_VERSION_3;
947 opt.max_version = SSL_MINOR_VERSION_3;
948 opt.transport = SSL_TRANSPORT_DATAGRAM;
949 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200950 else
951 goto usage;
952 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100953 else if( strcmp( p, "auth_mode" ) == 0 )
954 {
955 if( strcmp( q, "none" ) == 0 )
956 opt.auth_mode = SSL_VERIFY_NONE;
957 else if( strcmp( q, "optional" ) == 0 )
958 opt.auth_mode = SSL_VERIFY_OPTIONAL;
959 else if( strcmp( q, "required" ) == 0 )
960 opt.auth_mode = SSL_VERIFY_REQUIRED;
961 else
962 goto usage;
963 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200964 else if( strcmp( p, "max_frag_len" ) == 0 )
965 {
966 if( strcmp( q, "512" ) == 0 )
967 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
968 else if( strcmp( q, "1024" ) == 0 )
969 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
970 else if( strcmp( q, "2048" ) == 0 )
971 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
972 else if( strcmp( q, "4096" ) == 0 )
973 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
974 else
975 goto usage;
976 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200977 else if( strcmp( p, "alpn" ) == 0 )
978 {
979 opt.alpn_string = q;
980 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200981 else if( strcmp( p, "tickets" ) == 0 )
982 {
983 opt.tickets = atoi( q );
984 if( opt.tickets < 0 || opt.tickets > 1 )
985 goto usage;
986 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100987 else if( strcmp( p, "ticket_timeout" ) == 0 )
988 {
989 opt.ticket_timeout = atoi( q );
990 if( opt.ticket_timeout < 0 )
991 goto usage;
992 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100993 else if( strcmp( p, "cache_max" ) == 0 )
994 {
995 opt.cache_max = atoi( q );
996 if( opt.cache_max < 0 )
997 goto usage;
998 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100999 else if( strcmp( p, "cache_timeout" ) == 0 )
1000 {
1001 opt.cache_timeout = atoi( q );
1002 if( opt.cache_timeout < 0 )
1003 goto usage;
1004 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001005 else if( strcmp( p, "cookies" ) == 0 )
1006 {
1007 opt.cookies = atoi( q );
1008 if( opt.cookies < -1 || opt.cookies > 1)
1009 goto usage;
1010 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001011 else if( strcmp( p, "anti_replay" ) == 0 )
1012 {
1013 opt.anti_replay = atoi( q );
1014 if( opt.anti_replay < 0 || opt.anti_replay > 1)
1015 goto usage;
1016 }
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001017 else if( strcmp( p, "badmac_limit" ) == 0 )
1018 {
1019 opt.badmac_limit = atoi( q );
1020 if( opt.badmac_limit < 0 )
1021 goto usage;
1022 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001023 else if( strcmp( p, "hs_timeout" ) == 0 )
1024 {
1025 if( ( p = strchr( q, '-' ) ) == NULL )
1026 goto usage;
1027 *p++ = '\0';
1028 opt.hs_to_min = atoi( q );
1029 opt.hs_to_max = atoi( p );
1030 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
1031 goto usage;
1032 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001033 else if( strcmp( p, "sni" ) == 0 )
1034 {
1035 opt.sni = q;
1036 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001037 else
1038 goto usage;
1039 }
1040
Paul Bakkerc73079a2014-04-25 16:34:30 +02001041#if defined(POLARSSL_DEBUG_C)
1042 debug_set_threshold( opt.debug_level );
1043#endif
1044
Paul Bakkerc1516be2013-06-29 16:01:32 +02001045 if( opt.force_ciphersuite[0] > 0 )
1046 {
1047 const ssl_ciphersuite_t *ciphersuite_info;
1048 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
1049
Paul Bakker5b55b792013-07-19 13:43:43 +02001050 if( opt.max_version != -1 &&
1051 ciphersuite_info->min_minor_ver > opt.max_version )
1052 {
1053 printf("forced ciphersuite not allowed with this protocol version\n");
1054 ret = 2;
1055 goto usage;
1056 }
1057 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +02001058 ciphersuite_info->max_minor_ver < opt.min_version )
1059 {
1060 printf("forced ciphersuite not allowed with this protocol version\n");
1061 ret = 2;
1062 goto usage;
1063 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001064
1065 /* If we select a version that's not supported by
1066 * this suite, then there will be no common ciphersuite... */
1067 if( opt.max_version == -1 ||
1068 opt.max_version > ciphersuite_info->max_minor_ver )
1069 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001070 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001071 }
Paul Bakker5b55b792013-07-19 13:43:43 +02001072 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001073 {
Paul Bakker5b55b792013-07-19 13:43:43 +02001074 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001075 /* DTLS starts with TLS 1.1 */
1076 if( opt.transport == SSL_TRANSPORT_DATAGRAM &&
1077 opt.min_version < SSL_MINOR_VERSION_2 )
1078 opt.min_version = SSL_MINOR_VERSION_2;
1079 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001080 }
1081
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001082 if( opt.version_suites != NULL )
1083 {
1084 const char *name[4] = { 0 };
1085
1086 /* Parse 4-element coma-separated list */
1087 for( i = 0, p = (char *) opt.version_suites;
1088 i < 4 && *p != '\0';
1089 i++ )
1090 {
1091 name[i] = p;
1092
1093 /* Terminate the current string and move on to next one */
1094 while( *p != ',' && *p != '\0' )
1095 p++;
1096 if( *p == ',' )
1097 *p++ = '\0';
1098 }
1099
1100 if( i != 4 )
1101 {
1102 printf( "too few values for version_suites\n" );
1103 ret = 1;
1104 goto exit;
1105 }
1106
1107 memset( version_suites, 0, sizeof( version_suites ) );
1108
1109 /* Get the suites identifiers from their name */
1110 for( i = 0; i < 4; i++ )
1111 {
1112 version_suites[i][0] = ssl_get_ciphersuite_id( name[i] );
1113
1114 if( version_suites[i][0] == 0 )
1115 {
1116 printf( "unknown ciphersuite: '%s'\n", name[i] );
1117 ret = 2;
1118 goto usage;
1119 }
1120 }
1121 }
1122
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001123#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001124 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001125 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +02001126 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001127 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001128 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001129 printf( "pre-shared key not valid hex\n" );
1130 goto exit;
1131 }
1132
1133 if( opt.psk_list != NULL )
1134 {
1135 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +02001136 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001137 printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +02001138 goto exit;
1139 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02001140 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001141#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02001142
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001143#if defined(POLARSSL_SSL_ALPN)
1144 if( opt.alpn_string != NULL )
1145 {
1146 p = (char *) opt.alpn_string;
1147 i = 0;
1148
1149 /* Leave room for a final NULL in alpn_list */
1150 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
1151 {
1152 alpn_list[i++] = p;
1153
1154 /* Terminate the current string and move on to next one */
1155 while( *p != ',' && *p != '\0' )
1156 p++;
1157 if( *p == ',' )
1158 *p++ = '\0';
1159 }
1160 }
1161#endif /* POLARSSL_SSL_ALPN */
1162
Paul Bakkerfbb17802013-04-17 19:10:21 +02001163 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001164 * 0. Initialize the RNG and the session data
1165 */
1166 printf( "\n . Seeding the random number generator..." );
1167 fflush( stdout );
1168
1169 entropy_init( &entropy );
1170 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001171 (const unsigned char *) pers,
1172 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001173 {
1174 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
1175 goto exit;
1176 }
1177
1178 printf( " ok\n" );
1179
Paul Bakker36713e82013-09-17 13:25:29 +02001180#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001181 /*
1182 * 1.1. Load the trusted CA
1183 */
1184 printf( " . Loading the CA root certificate ..." );
1185 fflush( stdout );
1186
1187#if defined(POLARSSL_FS_IO)
1188 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001189 if( strcmp( opt.ca_path, "none" ) == 0 )
1190 ret = 0;
1191 else
1192 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001193 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001194 if( strcmp( opt.ca_file, "none" ) == 0 )
1195 ret = 0;
1196 else
1197 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001198 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001199#endif
1200#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +02001201 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
1202 strlen( test_ca_list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001203#else
1204 {
1205 ret = 1;
1206 printf("POLARSSL_CERTS_C not defined.");
1207 }
1208#endif
1209 if( ret < 0 )
1210 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001211 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001212 goto exit;
1213 }
1214
1215 printf( " ok (%d skipped)\n", ret );
1216
1217 /*
1218 * 1.2. Load own certificate and private key
1219 */
1220 printf( " . Loading the server cert. and key..." );
1221 fflush( stdout );
1222
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001223#if defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001224 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001225 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001226 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001227 if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
1228 {
1229 printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n",
1230 -ret );
1231 goto exit;
1232 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001233 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001234 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001235 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001236 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001237 if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
1238 {
1239 printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret );
1240 goto exit;
1241 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001242 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001243 if( key_cert_init == 1 )
1244 {
1245 printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
1246 goto exit;
1247 }
1248
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001249 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001250 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001251 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001252 if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
1253 {
1254 printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n",
1255 -ret );
1256 goto exit;
1257 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001258 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001259 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001260 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001261 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001262 if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
1263 {
1264 printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n",
1265 -ret );
1266 goto exit;
1267 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001268 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001269 if( key_cert_init2 == 1 )
1270 {
1271 printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
1272 goto exit;
1273 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001274#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001275 if( key_cert_init == 0 &&
1276 strcmp( opt.crt_file, "none" ) != 0 &&
1277 strcmp( opt.key_file, "none" ) != 0 &&
1278 key_cert_init2 == 0 &&
1279 strcmp( opt.crt_file2, "none" ) != 0 &&
1280 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001281 {
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001282#if !defined(POLARSSL_CERTS_C)
1283 printf( "Not certificated or key provided, and \n"
1284 "POLARSSL_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001285 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001286#else
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001287#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001288 if( ( ret = x509_crt_parse( &srvcert,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001289 (const unsigned char *) test_srv_crt_rsa,
1290 strlen( test_srv_crt_rsa ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001291 {
1292 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
1293 goto exit;
1294 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001295 if( ( ret = pk_parse_key( &pkey,
1296 (const unsigned char *) test_srv_key_rsa,
1297 strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001298 {
1299 printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
1300 goto exit;
1301 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001302 key_cert_init = 2;
1303#endif /* POLARSSL_RSA_C */
1304#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001305 if( ( ret = x509_crt_parse( &srvcert2,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001306 (const unsigned char *) test_srv_crt_ec,
1307 strlen( test_srv_crt_ec ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001308 {
1309 printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
1310 goto exit;
1311 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001312 if( ( ret = pk_parse_key( &pkey2,
1313 (const unsigned char *) test_srv_key_ec,
1314 strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001315 {
1316 printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
1317 goto exit;
1318 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001319 key_cert_init2 = 2;
1320#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001321#endif /* POLARSSL_CERTS_C */
1322 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001323
1324 printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001325#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001326
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001327#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1328 if( opt.dhm_file != NULL )
1329 {
1330 printf( " . Loading DHM parameters..." );
1331 fflush( stdout );
1332
1333 if( ( ret = dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
1334 {
1335 printf( " failed\n ! dhm_parse_dhmfile returned -0x%04X\n\n",
1336 -ret );
1337 goto exit;
1338 }
1339
1340 printf( " ok\n" );
1341 }
1342#endif
1343
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001344#if defined(POLARSSL_SNI)
1345 if( opt.sni != NULL )
1346 {
1347 printf( " . Setting up SNI information..." );
1348 fflush( stdout );
1349
1350 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
1351 {
1352 printf( " failed\n" );
1353 goto exit;
1354 }
1355
1356 printf( " ok\n" );
1357 }
1358#endif /* POLARSSL_SNI */
1359
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001360 /*
1361 * 2. Setup the listening TCP socket
1362 */
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001363 printf( " . Bind on %s://%s:%-4d/ ...",
1364 opt.transport == SSL_TRANSPORT_STREAM ? "tcp" : "udp",
1365 opt.server_addr ? opt.server_addr : "*",
1366 opt.server_port );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001367 fflush( stdout );
1368
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001369 if( ( ret = net_bind( &listen_fd, opt.server_addr, opt.server_port,
1370 opt.transport == SSL_TRANSPORT_STREAM ?
1371 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001372 {
1373 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1374 goto exit;
1375 }
1376
1377 printf( " ok\n" );
1378
1379 /*
1380 * 3. Setup stuff
1381 */
1382 printf( " . Setting up the SSL/TLS structure..." );
1383 fflush( stdout );
1384
1385 if( ( ret = ssl_init( &ssl ) ) != 0 )
1386 {
1387 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
1388 goto exit;
1389 }
1390
1391 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001392 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001393
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001394#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001395 if( ( ret = ssl_set_transport( &ssl, opt.transport ) ) != 0 )
1396 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001397 printf( " failed\n ! selected transport is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001398 goto exit;
1399 }
1400
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001401 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
1402 ssl_set_handshake_timeout( &ssl, opt.hs_to_min, opt.hs_to_max );
1403#endif /* POLARSSL_SSL_PROTO_DTLS */
1404
Paul Bakker05decb22013-08-15 13:33:48 +02001405#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001406 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1407 {
1408 printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
1409 goto exit;
1410 };
Paul Bakker05decb22013-08-15 13:33:48 +02001411#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001412
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001413#if defined(POLARSSL_SSL_ALPN)
1414 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001415 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1416 {
1417 printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
1418 goto exit;
1419 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001420#endif
1421
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001422 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
1423 ssl_set_dbg( &ssl, my_debug, stdout );
1424
Paul Bakker0a597072012-09-25 21:55:46 +00001425#if defined(POLARSSL_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001426 if( opt.cache_max != -1 )
1427 ssl_cache_set_max_entries( &cache, opt.cache_max );
1428
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001429 if( opt.cache_timeout != -1 )
1430 ssl_cache_set_timeout( &cache, opt.cache_timeout );
1431
Paul Bakker0a597072012-09-25 21:55:46 +00001432 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
1433 ssl_cache_set, &cache );
1434#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001435
Paul Bakkera503a632013-08-14 13:48:06 +02001436#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001437 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1438 {
1439 printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
1440 goto exit;
1441 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001442
1443 if( opt.ticket_timeout != -1 )
1444 ssl_set_session_ticket_lifetime( &ssl, opt.ticket_timeout );
Paul Bakkera503a632013-08-14 13:48:06 +02001445#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001446
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001447#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001448 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001449 {
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001450#if defined(POLARSSL_SSL_COOKIE_C)
1451 if( opt.cookies > 0 )
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001452 {
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001453 if( ( ret = ssl_cookie_setup( &cookie_ctx,
1454 ctr_drbg_random, &ctr_drbg ) ) != 0 )
1455 {
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001456 printf( " failed\n ! ssl_cookie_setup returned %d\n\n", ret );
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001457 goto exit;
1458 }
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001459
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001460 ssl_set_dtls_cookies( &ssl, ssl_cookie_write, ssl_cookie_check,
1461 &cookie_ctx );
1462 }
1463 else
1464#endif /* POLARSSL_SSL_COOKIE_C */
1465#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
1466 if( opt.cookies == 0 )
1467 {
1468 ssl_set_dtls_cookies( &ssl, NULL, NULL, NULL );
1469 }
1470 else
1471#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
1472 {
1473 ; /* Nothing to do */
1474 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001475
1476#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1477 if( opt.anti_replay != DFL_ANTI_REPLAY )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001478 ssl_set_dtls_anti_replay( &ssl, opt.anti_replay );
Manuel Pégourié-Gonnarde698f592014-10-14 19:36:36 +02001479#endif
1480
1481#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
1482 if( opt.badmac_limit != DFL_BADMAC_LIMIT )
1483 ssl_set_dtls_badmac_limit( &ssl, opt.badmac_limit );
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001484#endif
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001485 }
Manuel Pégourié-Gonnard26820e32014-07-23 19:34:59 +02001486#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001487
Paul Bakker41c83d32013-03-20 14:39:14 +01001488 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001489 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
1490
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001491 if( opt.version_suites != NULL )
1492 {
1493 ssl_set_ciphersuites_for_version( &ssl, version_suites[0],
1494 SSL_MAJOR_VERSION_3,
1495 SSL_MINOR_VERSION_0 );
1496 ssl_set_ciphersuites_for_version( &ssl, version_suites[1],
1497 SSL_MAJOR_VERSION_3,
1498 SSL_MINOR_VERSION_1 );
1499 ssl_set_ciphersuites_for_version( &ssl, version_suites[2],
1500 SSL_MAJOR_VERSION_3,
1501 SSL_MINOR_VERSION_2 );
1502 ssl_set_ciphersuites_for_version( &ssl, version_suites[3],
1503 SSL_MAJOR_VERSION_3,
1504 SSL_MINOR_VERSION_3 );
1505 }
1506
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001507 ssl_set_renegotiation( &ssl, opt.renegotiation );
1508 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001509 if( opt.renego_delay != DFL_RENEGO_DELAY )
1510 ssl_set_renegotiation_enforced( &ssl, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001511
Paul Bakker36713e82013-09-17 13:25:29 +02001512#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001513 if( strcmp( opt.ca_path, "none" ) != 0 &&
1514 strcmp( opt.ca_file, "none" ) != 0 )
1515 {
1516 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
1517 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001518 if( key_cert_init )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001519 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
1520 {
1521 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1522 goto exit;
1523 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001524 if( key_cert_init2 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001525 if( ( ret = ssl_set_own_cert( &ssl, &srvcert2, &pkey2 ) ) != 0 )
1526 {
1527 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1528 goto exit;
1529 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001530#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001531
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001532#if defined(POLARSSL_SNI)
1533 if( opt.sni != NULL )
1534 ssl_set_sni( &ssl, sni_callback, sni_info );
1535#endif
1536
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001537#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001538 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
1539 {
1540 ret = ssl_set_psk( &ssl, psk, psk_len,
1541 (const unsigned char *) opt.psk_identity,
1542 strlen( opt.psk_identity ) );
1543 if( ret != 0 )
1544 {
1545 printf( " failed\n ssl_set_psk returned -0x%04X\n\n", - ret );
1546 goto exit;
1547 }
1548 }
1549
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001550 if( opt.psk_list != NULL )
1551 ssl_set_psk_cb( &ssl, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02001552#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001553
1554#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00001555 /*
1556 * Use different group than default DHM group
1557 */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001558#if defined(POLARSSL_FS_IO)
1559 if( opt.dhm_file != NULL )
1560 ret = ssl_set_dh_param_ctx( &ssl, &dhm );
1561 else
1562#endif
1563 ret = ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
1564 POLARSSL_DHM_RFC5114_MODP_2048_G );
1565
1566 if( ret != 0 )
1567 {
1568 printf( " failed\n ssl_set_dh_param returned -0x%04X\n\n", - ret );
1569 goto exit;
1570 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001571#endif
1572
Paul Bakker1d29fb52012-09-28 13:28:45 +00001573 if( opt.min_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001574 {
1575 ret = ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1576 if( ret != 0 )
1577 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001578 printf( " failed\n ! selected min_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001579 goto exit;
1580 }
1581 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001582
Paul Bakkerc1516be2013-06-29 16:01:32 +02001583 if( opt.max_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001584 {
1585 ret = ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1586 if( ret != 0 )
1587 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001588 printf( " failed\n ! selected max_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001589 goto exit;
1590 }
1591 }
Paul Bakkerc1516be2013-06-29 16:01:32 +02001592
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001593 printf( " ok\n" );
1594
1595reset:
Manuel Pégourié-Gonnardb6440a42014-09-20 12:03:00 +02001596#if !defined(_WIN32)
1597 if( received_sigterm )
1598 {
1599 printf( " interrupted by SIGTERM\n" );
1600 ret = 0;
1601 goto exit;
1602 }
1603#endif
1604
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001605#ifdef POLARSSL_ERROR_C
1606 if( ret != 0 )
1607 {
1608 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001609 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001610 printf("Last error was: %d - %s\n\n", ret, error_buf );
1611 }
1612#endif
1613
1614 if( client_fd != -1 )
Manuel Pégourié-Gonnard4ba6ab62014-08-07 17:21:47 +02001615 net_close( client_fd );
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001616
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001617 ssl_session_reset( &ssl );
1618
1619 /*
1620 * 3. Wait until a client connects
1621 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001622 client_fd = -1;
1623
1624 printf( " . Waiting for a remote connection ..." );
1625 fflush( stdout );
1626
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001627 if( ( ret = net_accept( listen_fd, &client_fd, client_ip ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001628 {
Paul Bakkerc1283d32014-08-18 11:05:51 +02001629#if !defined(_WIN32)
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001630 if( received_sigterm )
1631 {
1632 printf( " interrupted by SIGTERM\n" );
1633 ret = 0;
1634 goto exit;
1635 }
Paul Bakkerc1283d32014-08-18 11:05:51 +02001636#endif
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001637
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001638 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
1639 goto exit;
1640 }
1641
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001642 if( opt.nbio > 0 )
1643 ret = net_set_nonblock( client_fd );
1644 else
1645 ret = net_set_block( client_fd );
1646 if( ret != 0 )
1647 {
1648 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
1649 goto exit;
1650 }
1651
1652 if( opt.nbio == 2 )
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001653 ssl_set_bio_timeout( &ssl, &client_fd, my_send, my_recv, NULL, 0 );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001654 else
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001655 ssl_set_bio_timeout( &ssl, &client_fd, net_send, net_recv,
1656#if defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnardf0365122014-09-29 16:11:47 +02001657 opt.nbio == 0 ? net_recv_timeout : NULL,
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001658#else
1659 NULL,
1660#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001661 opt.read_timeout );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001662
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001663#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001664 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
1665 {
1666 if( ( ret = ssl_set_client_transport_id( &ssl, client_ip,
1667 sizeof( client_ip ) ) ) != 0 )
1668 {
1669 printf( " failed\n ! "
1670 "ssl_set_client_tranport_id() returned -0x%x\n\n", -ret );
1671 goto exit;
1672 }
1673 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001674#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard336b8242014-07-22 17:57:43 +02001675
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001676 printf( " ok\n" );
1677
1678 /*
Manuel Pégourié-Gonnardbd97fdb2014-09-26 16:46:36 +02001679 * With UDP, bind_fd is hijacked by client_fd, so bind a new one
1680 */
1681#if defined(POLARSSL_SSL_PROTO_DTLS)
1682 if( opt.transport == SSL_TRANSPORT_DATAGRAM )
1683 {
1684 printf( " . Re-bind on udp://%s:%-4d/ ...",
1685 opt.server_addr ? opt.server_addr : "*",
1686 opt.server_port );
1687 fflush( stdout );
1688
1689 if( ( ret = net_bind( &listen_fd, opt.server_addr,
1690 opt.server_port, NET_PROTO_UDP ) ) != 0 )
1691 {
1692 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1693 goto exit;
1694 }
1695
1696 printf( " ok\n" );
1697 }
1698#endif /* POLARSSL_SSL_PROTO_DTLS */
1699
1700 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001701 * 4. Handshake
1702 */
1703 printf( " . Performing the SSL/TLS handshake..." );
1704 fflush( stdout );
1705
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001706 do ret = ssl_handshake( &ssl );
1707 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1708 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001709
Manuel Pégourié-Gonnardcaecdae2014-10-13 19:04:37 +02001710 if( ret == POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED )
1711 {
1712 printf( " hello verification requested\n" );
1713 ret = 0;
1714 goto reset;
1715 }
1716 else if( ret != 0 )
1717 {
1718 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
1719 goto reset;
1720 }
1721 else /* ret == 0 */
1722 {
1723 printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1724 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
1725 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001726
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001727 if( ( ret = ssl_get_record_expansion( &ssl ) ) >= 0 )
1728 printf( " [ Record expansion is %d ]\n", ret );
1729 else
1730 printf( " [ Record expansion is unknown (compression) ]\n" );
1731
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001732#if defined(POLARSSL_SSL_ALPN)
1733 if( opt.alpn_string != NULL )
1734 {
1735 const char *alp = ssl_get_alpn_protocol( &ssl );
1736 printf( " [ Application Layer Protocol is %s ]\n",
1737 alp ? alp : "(none)" );
1738 }
1739#endif
1740
Paul Bakker36713e82013-09-17 13:25:29 +02001741#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001742 /*
1743 * 5. Verify the server certificate
1744 */
1745 printf( " . Verifying peer X.509 certificate..." );
1746
1747 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1748 {
1749 printf( " failed\n" );
1750
Paul Bakkerb0550d92012-10-30 07:51:03 +00001751 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001752 printf( " ! no client certificate sent\n" );
1753
1754 if( ( ret & BADCERT_EXPIRED ) != 0 )
1755 printf( " ! client certificate has expired\n" );
1756
1757 if( ( ret & BADCERT_REVOKED ) != 0 )
1758 printf( " ! client certificate has been revoked\n" );
1759
1760 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
1761 printf( " ! self-signed or not signed by a trusted CA\n" );
1762
1763 printf( "\n" );
1764 }
1765 else
1766 printf( " ok\n" );
1767
Paul Bakkerb0550d92012-10-30 07:51:03 +00001768 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001769 {
1770 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001771 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1772 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001773 printf( "%s\n", buf );
1774 }
Paul Bakker36713e82013-09-17 13:25:29 +02001775#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001776
Manuel Pégourié-Gonnard6a2bc232014-10-09 15:33:13 +02001777 if( opt.exchanges == 0 )
1778 goto close_notify;
1779
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001780 exchanges = opt.exchanges;
1781data_exchange:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001782 /*
1783 * 6. Read the HTTP Request
1784 */
1785 printf( " < Read from client:" );
1786 fflush( stdout );
1787
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001788 /*
1789 * TLS and DTLS need different reading styles (stream vs datagram)
1790 */
1791 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001792 {
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001793 do
1794 {
1795 int terminated = 0;
1796 len = sizeof( buf ) - 1;
1797 memset( buf, 0, sizeof( buf ) );
1798 ret = ssl_read( &ssl, buf, len );
1799
1800 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1801 ret == POLARSSL_ERR_NET_WANT_WRITE )
1802 continue;
1803
1804 if( ret <= 0 )
1805 {
1806 switch( ret )
1807 {
1808 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1809 printf( " connection was closed gracefully\n" );
1810 goto close_notify;
1811
1812 case 0:
1813 case POLARSSL_ERR_NET_CONN_RESET:
1814 printf( " connection was reset by peer\n" );
1815 ret = POLARSSL_ERR_NET_CONN_RESET;
1816 goto reset;
1817
1818 default:
1819 printf( " ssl_read returned -0x%x\n", -ret );
1820 goto reset;
1821 }
1822 }
1823
1824 if( ssl_get_bytes_avail( &ssl ) == 0 )
1825 {
1826 len = ret;
1827 buf[len] = '\0';
1828 printf( " %d bytes read\n\n%s\n", len, (char *) buf );
1829
1830 /* End of message should be detected according to the syntax of the
1831 * application protocol (eg HTTP), just use a dummy test here. */
1832 if( buf[len - 1] == '\n' )
1833 terminated = 1;
1834 }
1835 else
1836 {
1837 int extra_len, ori_len;
1838 unsigned char *larger_buf;
1839
1840 ori_len = ret;
1841 extra_len = ssl_get_bytes_avail( &ssl );
1842
1843 larger_buf = polarssl_malloc( ori_len + extra_len + 1 );
1844 if( larger_buf == NULL )
1845 {
1846 printf( " ! memory allocation failed\n" );
1847 ret = 1;
1848 goto reset;
1849 }
1850
1851 memset( larger_buf, 0, ori_len + extra_len );
1852 memcpy( larger_buf, buf, ori_len );
1853
1854 /* This read should never fail and get the whole cached data */
1855 ret = ssl_read( &ssl, larger_buf + ori_len, extra_len );
1856 if( ret != extra_len ||
1857 ssl_get_bytes_avail( &ssl ) != 0 )
1858 {
1859 printf( " ! ssl_read failed on cached data\n" );
1860 ret = 1;
1861 goto reset;
1862 }
1863
1864 larger_buf[ori_len + extra_len] = '\0';
1865 printf( " %u bytes read (%u + %u)\n\n%s\n",
1866 ori_len + extra_len, ori_len, extra_len,
1867 (char *) larger_buf );
1868
1869 /* End of message should be detected according to the syntax of the
1870 * application protocol (eg HTTP), just use a dummy test here. */
1871 if( larger_buf[ori_len + extra_len - 1] == '\n' )
1872 terminated = 1;
1873
1874 polarssl_free( larger_buf );
1875 }
1876
1877 if( terminated )
1878 {
1879 ret = 0;
1880 break;
1881 }
1882 }
1883 while( 1 );
1884 }
1885 else /* Not stream, so datagram */
1886 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001887 len = sizeof( buf ) - 1;
1888 memset( buf, 0, sizeof( buf ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001889
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001890 do ret = ssl_read( &ssl, buf, len );
1891 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1892 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001893
1894 if( ret <= 0 )
1895 {
1896 switch( ret )
1897 {
1898 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1899 printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001900 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001901 goto close_notify;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001902
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001903 default:
1904 printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001905 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001906 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001907 }
1908
Manuel Pégourié-Gonnardcce220d2014-10-06 18:11:43 +02001909 len = ret;
1910 buf[len] = '\0';
1911 printf( " %d bytes read\n\n%s", len, (char *) buf );
1912 ret = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001913 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001914
1915 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001916 * 7a. Request renegotiation while client is waiting for input from us.
1917 * (only if we're going to exhange more data afterwards)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001918 */
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001919 if( opt.renegotiate && exchanges > 1 )
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001920 {
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001921 printf( " . Requestion renegotiation..." );
1922 fflush( stdout );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001923
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001924 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
1925 {
1926 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1927 ret != POLARSSL_ERR_NET_WANT_WRITE )
1928 {
1929 printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
1930 goto reset;
1931 }
1932 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001933
1934 printf( " ok\n" );
Manuel Pégourié-Gonnard296e3b12014-08-19 12:59:03 +02001935 }
1936
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001937 /*
1938 * 7. Write the 200 Response
1939 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001940 printf( " > Write to client:" );
1941 fflush( stdout );
1942
1943 len = sprintf( (char *) buf, HTTP_RESPONSE,
1944 ssl_get_ciphersuite( &ssl ) );
1945
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001946 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001947 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001948 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001949 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001950 while( ( ret = ssl_write( &ssl, buf + written, len - written ) )
1951 <= 0 )
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001952 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001953 if( ret == POLARSSL_ERR_NET_CONN_RESET )
1954 {
1955 printf( " failed\n ! peer closed the connection\n\n" );
1956 goto reset;
1957 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001958
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001959 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1960 ret != POLARSSL_ERR_NET_WANT_WRITE )
1961 {
1962 printf( " failed\n ! ssl_write returned %d\n\n", ret );
1963 goto reset;
1964 }
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001965 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001966 }
1967 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001968 else /* Not stream, so datagram */
1969 {
1970 do ret = ssl_write( &ssl, buf, len );
1971 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1972 ret == POLARSSL_ERR_NET_WANT_WRITE );
1973
1974 if( ret < 0 )
1975 {
1976 printf( " failed\n ! ssl_write returned %d\n\n", ret );
1977 goto reset;
1978 }
1979
1980 frags = 1;
1981 written = ret;
1982 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001983
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001984 buf[written] = '\0';
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001985 printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001986
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001987
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001988 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001989 * 7b. Continue doing data exchanges?
1990 */
1991 if( --exchanges > 0 )
1992 goto data_exchange;
1993
1994 /*
1995 * 8. Done, cleanly close the connection
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001996 */
1997close_notify:
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001998 printf( " . Closing the connection..." );
1999
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002000 /* No error checking, the connection might be closed already */
2001 do
2002 ret = ssl_close_notify( &ssl );
2003 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
2004 ret = 0;
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002005
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02002006 printf( " done\n" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002007 goto reset;
2008
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02002009 /*
2010 * Cleanup and exit
2011 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002012exit:
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002013#ifdef POLARSSL_ERROR_C
2014 if( ret != 0 )
2015 {
2016 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02002017 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002018 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
2019 }
2020#endif
2021
Paul Bakker0c226102014-04-17 16:02:36 +02002022 if( client_fd != -1 )
2023 net_close( client_fd );
2024
Paul Bakkera317a982014-06-18 16:44:11 +02002025#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
2026 dhm_free( &dhm );
2027#endif
Paul Bakker36713e82013-09-17 13:25:29 +02002028#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker36713e82013-09-17 13:25:29 +02002029 x509_crt_free( &cacert );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002030 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02002031 pk_free( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002032 x509_crt_free( &srvcert2 );
2033 pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02002034#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01002035#if defined(POLARSSL_SNI)
2036 sni_free( sni_info );
2037#endif
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02002038#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
2039 psk_free( psk_info );
2040#endif
2041#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
2042 dhm_free( &dhm );
2043#endif
Paul Bakkered27a042013-04-18 22:46:23 +02002044
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002045 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02002046 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02002047 entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002048
Paul Bakker0a597072012-09-25 21:55:46 +00002049#if defined(POLARSSL_SSL_CACHE_C)
2050 ssl_cache_free( &cache );
2051#endif
Manuel Pégourié-Gonnarda64acd42014-07-23 18:30:45 +02002052#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002053 ssl_cookie_free( &cookie_ctx );
2054#endif
Paul Bakker0a597072012-09-25 21:55:46 +00002055
Paul Bakker1337aff2013-09-29 14:45:34 +02002056#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
2057#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker82024bf2013-07-04 11:52:32 +02002058 memory_buffer_alloc_status();
2059#endif
Paul Bakker1337aff2013-09-29 14:45:34 +02002060 memory_buffer_alloc_free();
2061#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02002062
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002063#if defined(_WIN32)
2064 printf( " + Press Enter to exit this program.\n" );
2065 fflush( stdout ); getchar();
2066#endif
2067
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02002068 // Shell can not handle large exit numbers -> 1 for errors
2069 if( ret < 0 )
2070 ret = 1;
2071
Paul Bakkerb60b95f2012-09-25 09:05:17 +00002072 return( ret );
2073}
2074#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
2075 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
2076 POLARSSL_CTR_DRBG_C */