blob: 6f529fa6b4ff5c87b22a50886e3960431f573405 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client with certificate authentication
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +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 Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020029#if !defined(POLARSSL_ENTROPY_C) || \
30 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
31 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
32#include <stdio.h>
33int main( int argc, char *argv[] )
34{
35 ((void) argc);
36 ((void) argv);
37
38 printf("POLARSSL_ENTROPY_C and/or "
39 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
40 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
41 return( 0 );
42}
43#else
44
Paul Bakker5121ce52009-01-03 21:22:43 +000045#include <string.h>
Paul Bakker9caf2d22010-02-18 19:37:19 +000046#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000047#include <stdio.h>
48
Paul Bakker40e46942009-01-03 21:51:57 +000049#include "polarssl/net.h"
50#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000051#include "polarssl/entropy.h"
52#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000053#include "polarssl/certs.h"
54#include "polarssl/x509.h"
Paul Bakkera3d195c2011-11-27 21:07:34 +000055#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020056#include "polarssl/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010058#if defined(POLARSSL_TIMING_C)
59#include "polarssl/timing.h"
60#endif
61
Paul Bakker93c32b22014-04-25 13:40:05 +020062#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
63#if !defined snprintf
64#define snprintf _snprintf
65#endif
66#endif
67
Paul Bakker9caf2d22010-02-18 19:37:19 +000068#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010069#define DFL_SERVER_ADDR NULL
Paul Bakker9caf2d22010-02-18 19:37:19 +000070#define DFL_SERVER_PORT 4433
71#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +020072#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +000073#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010074#define DFL_NBIO 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020075#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +020076#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +000077#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000078#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000079#define DFL_CRT_FILE ""
80#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020081#define DFL_PSK ""
82#define DFL_PSK_IDENTITY "Client_identity"
Paul Bakker51936882011-02-20 16:05:58 +000083#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +020084#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010085#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010086#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +020087#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +010088#define DFL_MIN_VERSION SSL_MINOR_VERSION_1
Paul Bakker1d29fb52012-09-28 13:28:45 +000089#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +010090#define DFL_ARC4 SSL_ARC4_DISABLED
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +010091#define DFL_AUTH_MODE SSL_VERIFY_REQUIRED
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +020092#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +010093#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +010094#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +020095#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010096#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +020097#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +020098#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +010099#define DFL_TRANSPORT SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200100#define DFL_HS_TO_MIN 0
101#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200102#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200103#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100104#define DFL_ETM -1
Paul Bakker0e6975b2009-02-10 22:19:10 +0000105
Paul Bakker93c32b22014-04-25 13:40:05 +0200106#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
107#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
Paul Bakker9caf2d22010-02-18 19:37:19 +0000109/*
110 * global options
111 */
112struct options
113{
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200114 const char *server_name; /* hostname of the server (client only) */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100115 const char *server_addr; /* address of the server (client only) */
Paul Bakker67968392010-07-18 08:28:20 +0000116 int server_port; /* port on which the ssl service runs */
117 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100118 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200119 uint32_t read_timeout; /* timeout on ssl_read() in milliseconds */
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200120 int max_resend; /* DTLS times to resend on read timeout */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200121 const char *request_page; /* page on server to request */
Paul Bakker93c32b22014-04-25 13:40:05 +0200122 int request_size; /* pad request with header to requested size */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200123 const char *ca_file; /* the file with the CA certificate(s) */
124 const char *ca_path; /* the path with the CA certificate(s) reside */
125 const char *crt_file; /* the file with the client certificate */
126 const char *key_file; /* the file with the client key */
127 const char *psk; /* the pre-shared key */
128 const char *psk_identity; /* the pre-shared key identity */
Paul Bakker51936882011-02-20 16:05:58 +0000129 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Paul Bakker48916f92012-09-16 19:57:18 +0000130 int renegotiation; /* enable / disable renegotiation */
131 int allow_legacy; /* allow legacy renegotiation */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100132 int renegotiate; /* attempt renegotiation? */
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200133 int renego_delay; /* delay before enforcing renegotiation */
134 int exchanges; /* number of data exchanges */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000135 int min_version; /* minimum protocol version accepted */
136 int max_version; /* maximum protocol version accepted */
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100137 int arc4; /* flag for arc4 suites support */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100138 int auth_mode; /* verify mode for connection */
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200139 unsigned char mfl_code; /* code for maximum fragment length */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200140 int trunc_hmac; /* negotiate truncated hmac or not */
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100141 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200142 int reconnect; /* attempt to resume session */
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100143 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200144 int tickets; /* enable / disable session tickets */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200145 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100146 int transport; /* TLS or DTLS? */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200147 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
148 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200149 int fallback; /* is this a fallback connection? */
Manuel Pégourié-Gonnard98286562015-01-12 19:17:05 +0100150 int extended_ms; /* negotiate extended master secret? */
Paul Bakker8b9bcec2015-01-13 15:59:55 +0100151 int etm; /* negotiate encrypt then mac? */
Paul Bakker9caf2d22010-02-18 19:37:19 +0000152} opt;
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Paul Bakker3c5ef712013-06-25 16:37:45 +0200154static void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +0000155{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200156 ((void) level);
157
158 fprintf( (FILE *) ctx, "%s", str );
159 fflush( (FILE *) ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160}
161
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100162/*
163 * Test recv/send functions that make sure each try returns
164 * WANT_READ/WANT_WRITE at least once before sucesseding
165 */
166static int my_recv( void *ctx, unsigned char *buf, size_t len )
167{
168 static int first_try = 1;
169 int ret;
170
171 if( first_try )
172 {
173 first_try = 0;
174 return( POLARSSL_ERR_NET_WANT_READ );
175 }
176
177 ret = net_recv( ctx, buf, len );
178 if( ret != POLARSSL_ERR_NET_WANT_READ )
179 first_try = 1; /* Next call will be a new operation */
180 return( ret );
181}
182
183static int my_send( void *ctx, const unsigned char *buf, size_t len )
184{
185 static int first_try = 1;
186 int ret;
187
188 if( first_try )
189 {
190 first_try = 0;
191 return( POLARSSL_ERR_NET_WANT_WRITE );
192 }
193
194 ret = net_send( ctx, buf, len );
195 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
196 first_try = 1; /* Next call will be a new operation */
197 return( ret );
198}
199
Paul Bakker36713e82013-09-17 13:25:29 +0200200#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker915275b2012-09-28 07:10:55 +0000201/*
202 * Enabled if debug_level > 1 in code below
203 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200204static int my_verify( void *data, x509_crt *crt, int depth, int *flags )
Paul Bakker915275b2012-09-28 07:10:55 +0000205{
206 char buf[1024];
207 ((void) data);
208
209 printf( "\nVerify requested for (Depth %d):\n", depth );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200210 x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Paul Bakker915275b2012-09-28 07:10:55 +0000211 printf( "%s", buf );
212
213 if( ( (*flags) & BADCERT_EXPIRED ) != 0 )
214 printf( " ! server certificate has expired\n" );
215
216 if( ( (*flags) & BADCERT_REVOKED ) != 0 )
217 printf( " ! server certificate has been revoked\n" );
218
219 if( ( (*flags) & BADCERT_CN_MISMATCH ) != 0 )
220 printf( " ! CN mismatch\n" );
221
222 if( ( (*flags) & BADCERT_NOT_TRUSTED ) != 0 )
223 printf( " ! self-signed or not signed by a trusted CA\n" );
224
225 if( ( (*flags) & BADCRL_NOT_TRUSTED ) != 0 )
226 printf( " ! CRL not trusted\n" );
227
228 if( ( (*flags) & BADCRL_EXPIRED ) != 0 )
229 printf( " ! CRL expired\n" );
230
231 if( ( (*flags) & BADCERT_OTHER ) != 0 )
232 printf( " ! other (unknown) flag\n" );
233
234 if ( ( *flags ) == 0 )
235 printf( " This certificate has no flags\n" );
236
237 return( 0 );
238}
Paul Bakker36713e82013-09-17 13:25:29 +0200239#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +0000240
Paul Bakker36713e82013-09-17 13:25:29 +0200241#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5690efc2011-05-26 13:16:06 +0000242#if defined(POLARSSL_FS_IO)
243#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100244 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
245 " default: \"\" (pre-loaded)\n" \
246 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
247 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
248 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
249 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000250 " key_file=%%s default: \"\" (pre-loaded)\n"
251#else
252#define USAGE_IO \
253 " No file operations available (POLARSSL_FS_IO not defined)\n"
254#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200255#else
256#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200257#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200258
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200259#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200260#define USAGE_PSK \
261 " psk=%%s default: \"\" (in hex, without 0x)\n" \
262 " psk_identity=%%s default: \"Client_identity\"\n"
263#else
264#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200265#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000266
Paul Bakkera503a632013-08-14 13:48:06 +0200267#if defined(POLARSSL_SSL_SESSION_TICKETS)
268#define USAGE_TICKETS \
269 " tickets=%%d default: 1 (enabled)\n"
270#else
271#define USAGE_TICKETS ""
272#endif /* POLARSSL_SSL_SESSION_TICKETS */
273
Paul Bakker1f2bc622013-08-15 13:45:55 +0200274#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
275#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100276 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200277#else
278#define USAGE_TRUNC_HMAC ""
279#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
280
Paul Bakker05decb22013-08-15 13:33:48 +0200281#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
282#define USAGE_MAX_FRAG_LEN \
283 " max_frag_len=%%d default: 16384 (tls default)\n" \
284 " options: 512, 1024, 2048, 4096\n"
285#else
286#define USAGE_MAX_FRAG_LEN ""
287#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
288
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100289#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
290#define USAGE_RECSPLIT \
291 " recplit=%%d default: (library default)\n"
292#else
293#define USAGE_RECSPLIT
294#endif
295
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100296#if defined(POLARSSL_TIMING_C)
297#define USAGE_TIME \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200298 " reco_delay=%%d default: 0 seconds\n"
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100299#else
300#define USAGE_TIME ""
301#endif /* POLARSSL_TIMING_C */
302
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200303#if defined(POLARSSL_SSL_ALPN)
304#define USAGE_ALPN \
305 " alpn=%%s default: \"\" (disabled)\n" \
306 " example: spdy/1,http/1.1\n"
307#else
308#define USAGE_ALPN ""
309#endif /* POLARSSL_SSL_ALPN */
310
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200311#if defined(POLARSSL_SSL_PROTO_DTLS)
312#define USAGE_DTLS \
313 " dtls=%%d default: 0 (TLS)\n" \
314 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
315 " range of DTLS handshake timeouts in millisecs\n"
316#else
317#define USAGE_DTLS ""
318#endif
319
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200320#if defined(POLARSSL_SSL_FALLBACK_SCSV)
321#define USAGE_FALLBACK \
322 " fallback=0/1 default: (library default: off)\n"
323#else
324#define USAGE_FALLBACK ""
325#endif
326
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200327#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
328#define USAGE_EMS \
329 " extended_ms=0/1 default: (library default: on)\n"
330#else
331#define USAGE_EMS ""
332#endif
333
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100334#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
335#define USAGE_ETM \
336 " etm=0/1 default: (library default: on)\n"
337#else
338#define USAGE_ETM ""
339#endif
340
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100341#if defined(POLARSSL_SSL_RENEGOTIATION)
342#define USAGE_RENEGO \
343 " renegotiation=%%d default: 0 (disabled)\n" \
344 " renegotiate=%%d default: 0 (disabled)\n"
345#else
346#define USAGE_RENEGO ""
347#endif
348
Paul Bakker9caf2d22010-02-18 19:37:19 +0000349#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000350 "\n usage: ssl_client2 param=<>...\n" \
351 "\n acceptable parameters:\n" \
352 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100353 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000354 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000355 " request_page=%%s default: \".\"\n" \
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +0200356 " request_size=%%d default: about 34 (basic request)\n" \
357 " (minimum: 0, max: 16384)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100358 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100359 " nbio=%%d default: 0 (blocking I/O)\n" \
360 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200361 " read_timeout=%%d default: 0 (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200362 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100363 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200364 USAGE_DTLS \
365 "\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100366 " auth_mode=%%s default: \"optional\"\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100367 " options: none, optional, required\n" \
368 USAGE_IO \
369 "\n" \
370 USAGE_PSK \
371 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100372 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100373 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200374 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200375 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100376 USAGE_TIME \
Paul Bakkera503a632013-08-14 13:48:06 +0200377 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100378 USAGE_MAX_FRAG_LEN \
379 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200380 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200381 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200382 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100383 USAGE_ETM \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100384 USAGE_RECSPLIT \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000385 "\n" \
386 " min_version=%%s default: \"\" (ssl3)\n" \
387 " max_version=%%s default: \"\" (tls1_2)\n" \
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100388 " arc4=%%d default: 0 (disabled)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000389 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100390 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100391 " auth_mode=%%s default: \"required\"\n" \
Paul Bakker91ebfb52012-11-23 14:04:08 +0100392 " options: none, optional, required\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000393 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000394 " force_ciphersuite=<name> default: all enabled\n"\
395 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000396
397int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000398{
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200399 int ret = 0, len, tail_len, server_fd, i, written, frags, retry_left;
Paul Bakker93c32b22014-04-25 13:40:05 +0200400 unsigned char buf[SSL_MAX_CONTENT_LEN + 1];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200401#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200402 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200403 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200404#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200405#if defined(POLARSSL_SSL_ALPN)
406 const char *alpn_list[10];
407#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200408 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000409
410 entropy_context entropy;
411 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 ssl_context ssl;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200413 ssl_session saved_session;
Paul Bakker36713e82013-09-17 13:25:29 +0200414#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200415 x509_crt cacert;
416 x509_crt clicert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200417 pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200418#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000419 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000420 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000421
Paul Bakker1a207ec2011-02-06 13:22:40 +0000422 /*
423 * Make sure memory references are valid.
424 */
425 server_fd = 0;
Paul Bakker1a207ec2011-02-06 13:22:40 +0000426 memset( &ssl, 0, sizeof( ssl_context ) );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200427 memset( &saved_session, 0, sizeof( ssl_session ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200428#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200429 x509_crt_init( &cacert );
430 x509_crt_init( &clicert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200431 pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200432#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200433#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200434 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200435#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000436
Paul Bakker9caf2d22010-02-18 19:37:19 +0000437 if( argc == 0 )
438 {
439 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000440 if( ret == 0 )
441 ret = 1;
442
Paul Bakker9caf2d22010-02-18 19:37:19 +0000443 printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000444
445 list = ssl_list_ciphersuites();
446 while( *list )
447 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200448 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200449 list++;
450 if( !*list )
451 break;
452 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000453 list++;
454 }
455 printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000456 goto exit;
457 }
458
459 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100460 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000461 opt.server_port = DFL_SERVER_PORT;
462 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100463 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200464 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200465 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000466 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200467 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000468 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000469 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000470 opt.crt_file = DFL_CRT_FILE;
471 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200472 opt.psk = DFL_PSK;
473 opt.psk_identity = DFL_PSK_IDENTITY;
Paul Bakker51936882011-02-20 16:05:58 +0000474 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000475 opt.renegotiation = DFL_RENEGOTIATION;
476 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100477 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200478 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000479 opt.min_version = DFL_MIN_VERSION;
480 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100481 opt.arc4 = DFL_ARC4;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100482 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200483 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200484 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100485 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200486 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100487 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200488 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200489 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200490 opt.transport = DFL_TRANSPORT;
491 opt.hs_to_min = DFL_HS_TO_MIN;
492 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200493 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200494 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100495 opt.etm = DFL_ETM;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000496
497 for( i = 1; i < argc; i++ )
498 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000499 p = argv[i];
500 if( ( q = strchr( p, '=' ) ) == NULL )
501 goto usage;
502 *q++ = '\0';
503
504 if( strcmp( p, "server_name" ) == 0 )
505 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100506 else if( strcmp( p, "server_addr" ) == 0 )
507 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000508 else if( strcmp( p, "server_port" ) == 0 )
509 {
510 opt.server_port = atoi( q );
511 if( opt.server_port < 1 || opt.server_port > 65535 )
512 goto usage;
513 }
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100514 else if( strcmp( p, "dtls" ) == 0 )
515 {
516 int t = atoi( q );
517 if( t == 0 )
518 opt.transport = SSL_TRANSPORT_STREAM;
519 else if( t == 1 )
520 opt.transport = SSL_TRANSPORT_DATAGRAM;
521 else
522 goto usage;
523 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000524 else if( strcmp( p, "debug_level" ) == 0 )
525 {
526 opt.debug_level = atoi( q );
527 if( opt.debug_level < 0 || opt.debug_level > 65535 )
528 goto usage;
529 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100530 else if( strcmp( p, "nbio" ) == 0 )
531 {
532 opt.nbio = atoi( q );
533 if( opt.nbio < 0 || opt.nbio > 2 )
534 goto usage;
535 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200536 else if( strcmp( p, "read_timeout" ) == 0 )
537 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200538 else if( strcmp( p, "max_resend" ) == 0 )
539 {
540 opt.max_resend = atoi( q );
541 if( opt.max_resend < 0 )
542 goto usage;
543 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000544 else if( strcmp( p, "request_page" ) == 0 )
545 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200546 else if( strcmp( p, "request_size" ) == 0 )
547 {
548 opt.request_size = atoi( q );
549 if( opt.request_size < 0 || opt.request_size > SSL_MAX_CONTENT_LEN )
550 goto usage;
551 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000552 else if( strcmp( p, "ca_file" ) == 0 )
553 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000554 else if( strcmp( p, "ca_path" ) == 0 )
555 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000556 else if( strcmp( p, "crt_file" ) == 0 )
557 opt.crt_file = q;
558 else if( strcmp( p, "key_file" ) == 0 )
559 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200560 else if( strcmp( p, "psk" ) == 0 )
561 opt.psk = q;
562 else if( strcmp( p, "psk_identity" ) == 0 )
563 opt.psk_identity = q;
Paul Bakker51936882011-02-20 16:05:58 +0000564 else if( strcmp( p, "force_ciphersuite" ) == 0 )
565 {
Paul Bakker51936882011-02-20 16:05:58 +0000566 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
567
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200568 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000569 {
570 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000571 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000572 }
Paul Bakker51936882011-02-20 16:05:58 +0000573 opt.force_ciphersuite[1] = 0;
574 }
Paul Bakker48916f92012-09-16 19:57:18 +0000575 else if( strcmp( p, "renegotiation" ) == 0 )
576 {
577 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
578 SSL_RENEGOTIATION_DISABLED;
579 }
580 else if( strcmp( p, "allow_legacy" ) == 0 )
581 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100582 switch( atoi( q ) )
583 {
584 case -1: opt.allow_legacy = SSL_LEGACY_BREAK_HANDSHAKE; break;
585 case 0: opt.allow_legacy = SSL_LEGACY_NO_RENEGOTIATION; break;
586 case 1: opt.allow_legacy = SSL_LEGACY_ALLOW_RENEGOTIATION; break;
587 default: goto usage;
588 }
Paul Bakker48916f92012-09-16 19:57:18 +0000589 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100590 else if( strcmp( p, "renegotiate" ) == 0 )
591 {
592 opt.renegotiate = atoi( q );
593 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
594 goto usage;
595 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200596 else if( strcmp( p, "exchanges" ) == 0 )
597 {
598 opt.exchanges = atoi( q );
599 if( opt.exchanges < 1 )
600 goto usage;
601 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200602 else if( strcmp( p, "reconnect" ) == 0 )
603 {
604 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200605 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200606 goto usage;
607 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100608 else if( strcmp( p, "reco_delay" ) == 0 )
609 {
610 opt.reco_delay = atoi( q );
611 if( opt.reco_delay < 0 )
612 goto usage;
613 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200614 else if( strcmp( p, "tickets" ) == 0 )
615 {
616 opt.tickets = atoi( q );
617 if( opt.tickets < 0 || opt.tickets > 2 )
618 goto usage;
619 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200620 else if( strcmp( p, "alpn" ) == 0 )
621 {
622 opt.alpn_string = q;
623 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200624 else if( strcmp( p, "fallback" ) == 0 )
625 {
626 switch( atoi( q ) )
627 {
628 case 0: opt.fallback = SSL_IS_NOT_FALLBACK; break;
629 case 1: opt.fallback = SSL_IS_FALLBACK; break;
630 default: goto usage;
631 }
632 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200633 else if( strcmp( p, "extended_ms" ) == 0 )
634 {
635 switch( atoi( q ) )
636 {
637 case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break;
638 case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break;
639 default: goto usage;
640 }
641 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100642 else if( strcmp( p, "etm" ) == 0 )
643 {
644 switch( atoi( q ) )
645 {
646 case 0: opt.etm = SSL_ETM_DISABLED; break;
647 case 1: opt.etm = SSL_ETM_ENABLED; break;
648 default: goto usage;
649 }
650 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000651 else if( strcmp( p, "min_version" ) == 0 )
652 {
653 if( strcmp( q, "ssl3" ) == 0 )
654 opt.min_version = SSL_MINOR_VERSION_0;
655 else if( strcmp( q, "tls1" ) == 0 )
656 opt.min_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100657 else if( strcmp( q, "tls1_1" ) == 0 ||
658 strcmp( q, "dtls1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000659 opt.min_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100660 else if( strcmp( q, "tls1_2" ) == 0 ||
661 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000662 opt.min_version = SSL_MINOR_VERSION_3;
663 else
664 goto usage;
665 }
666 else if( strcmp( p, "max_version" ) == 0 )
667 {
668 if( strcmp( q, "ssl3" ) == 0 )
669 opt.max_version = SSL_MINOR_VERSION_0;
670 else if( strcmp( q, "tls1" ) == 0 )
671 opt.max_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100672 else if( strcmp( q, "tls1_1" ) == 0 ||
673 strcmp( q, "dtls1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000674 opt.max_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100675 else if( strcmp( q, "tls1_2" ) == 0 ||
676 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000677 opt.max_version = SSL_MINOR_VERSION_3;
678 else
679 goto usage;
680 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100681 else if( strcmp( p, "arc4" ) == 0 )
682 {
683 switch( atoi( q ) )
684 {
685 case 0: opt.arc4 = SSL_ARC4_DISABLED; break;
686 case 1: opt.arc4 = SSL_ARC4_ENABLED; break;
687 default: goto usage;
688 }
689 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000690 else if( strcmp( p, "force_version" ) == 0 )
691 {
692 if( strcmp( q, "ssl3" ) == 0 )
693 {
694 opt.min_version = SSL_MINOR_VERSION_0;
695 opt.max_version = SSL_MINOR_VERSION_0;
696 }
697 else if( strcmp( q, "tls1" ) == 0 )
698 {
699 opt.min_version = SSL_MINOR_VERSION_1;
700 opt.max_version = SSL_MINOR_VERSION_1;
701 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100702 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000703 {
704 opt.min_version = SSL_MINOR_VERSION_2;
705 opt.max_version = SSL_MINOR_VERSION_2;
706 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100707 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000708 {
709 opt.min_version = SSL_MINOR_VERSION_3;
710 opt.max_version = SSL_MINOR_VERSION_3;
711 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100712 else if( strcmp( q, "dtls1" ) == 0 )
713 {
714 opt.min_version = SSL_MINOR_VERSION_2;
715 opt.max_version = SSL_MINOR_VERSION_2;
716 opt.transport = SSL_TRANSPORT_DATAGRAM;
717 }
718 else if( strcmp( q, "dtls1_2" ) == 0 )
719 {
720 opt.min_version = SSL_MINOR_VERSION_3;
721 opt.max_version = SSL_MINOR_VERSION_3;
722 opt.transport = SSL_TRANSPORT_DATAGRAM;
723 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000724 else
725 goto usage;
726 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100727 else if( strcmp( p, "auth_mode" ) == 0 )
728 {
729 if( strcmp( q, "none" ) == 0 )
730 opt.auth_mode = SSL_VERIFY_NONE;
731 else if( strcmp( q, "optional" ) == 0 )
732 opt.auth_mode = SSL_VERIFY_OPTIONAL;
733 else if( strcmp( q, "required" ) == 0 )
734 opt.auth_mode = SSL_VERIFY_REQUIRED;
735 else
736 goto usage;
737 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200738 else if( strcmp( p, "max_frag_len" ) == 0 )
739 {
740 if( strcmp( q, "512" ) == 0 )
741 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
742 else if( strcmp( q, "1024" ) == 0 )
743 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
744 else if( strcmp( q, "2048" ) == 0 )
745 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
746 else if( strcmp( q, "4096" ) == 0 )
747 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
748 else
749 goto usage;
750 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200751 else if( strcmp( p, "trunc_hmac" ) == 0 )
752 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100753 switch( atoi( q ) )
754 {
755 case 0: opt.trunc_hmac = SSL_TRUNC_HMAC_DISABLED; break;
756 case 1: opt.trunc_hmac = SSL_TRUNC_HMAC_ENABLED; break;
757 default: goto usage;
758 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200759 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200760 else if( strcmp( p, "hs_timeout" ) == 0 )
761 {
762 if( ( p = strchr( q, '-' ) ) == NULL )
763 goto usage;
764 *p++ = '\0';
765 opt.hs_to_min = atoi( q );
766 opt.hs_to_max = atoi( p );
767 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
768 goto usage;
769 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100770 else if( strcmp( p, "recsplit" ) == 0 )
771 {
772 opt.recsplit = atoi( q );
773 if( opt.recsplit < 0 || opt.recsplit > 1 )
774 goto usage;
775 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000776 else
777 goto usage;
778 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000779
Paul Bakkerc73079a2014-04-25 16:34:30 +0200780#if defined(POLARSSL_DEBUG_C)
781 debug_set_threshold( opt.debug_level );
782#endif
783
Paul Bakkerc1516be2013-06-29 16:01:32 +0200784 if( opt.force_ciphersuite[0] > 0 )
785 {
786 const ssl_ciphersuite_t *ciphersuite_info;
787 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
788
Paul Bakker66c48102013-07-26 14:05:32 +0200789 if( opt.max_version != -1 &&
790 ciphersuite_info->min_minor_ver > opt.max_version )
791 {
792 printf("forced ciphersuite not allowed with this protocol version\n");
793 ret = 2;
794 goto usage;
795 }
796 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200797 ciphersuite_info->max_minor_ver < opt.min_version )
798 {
799 printf("forced ciphersuite not allowed with this protocol version\n");
800 ret = 2;
801 goto usage;
802 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100803
804 /* If the server selects a version that's not supported by
805 * this suite, then there will be no common ciphersuite... */
806 if( opt.max_version == -1 ||
807 opt.max_version > ciphersuite_info->max_minor_ver )
808 {
Paul Bakker66c48102013-07-26 14:05:32 +0200809 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100810 }
Paul Bakker66c48102013-07-26 14:05:32 +0200811 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100812 {
Paul Bakker66c48102013-07-26 14:05:32 +0200813 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100814 /* DTLS starts with TLS 1.1 */
815 if( opt.transport == SSL_TRANSPORT_DATAGRAM &&
816 opt.min_version < SSL_MINOR_VERSION_2 )
817 opt.min_version = SSL_MINOR_VERSION_2;
818 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200819 }
820
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200821#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000822 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200823 * Unhexify the pre-shared key if any is given
824 */
825 if( strlen( opt.psk ) )
826 {
827 unsigned char c;
828 size_t j;
829
830 if( strlen( opt.psk ) % 2 != 0 )
831 {
832 printf("pre-shared key not valid hex\n");
833 goto exit;
834 }
835
836 psk_len = strlen( opt.psk ) / 2;
837
838 for( j = 0; j < strlen( opt.psk ); j += 2 )
839 {
840 c = opt.psk[j];
841 if( c >= '0' && c <= '9' )
842 c -= '0';
843 else if( c >= 'a' && c <= 'f' )
844 c -= 'a' - 10;
845 else if( c >= 'A' && c <= 'F' )
846 c -= 'A' - 10;
847 else
848 {
849 printf("pre-shared key not valid hex\n");
850 goto exit;
851 }
852 psk[ j / 2 ] = c << 4;
853
854 c = opt.psk[j + 1];
855 if( c >= '0' && c <= '9' )
856 c -= '0';
857 else if( c >= 'a' && c <= 'f' )
858 c -= 'a' - 10;
859 else if( c >= 'A' && c <= 'F' )
860 c -= 'A' - 10;
861 else
862 {
863 printf("pre-shared key not valid hex\n");
864 goto exit;
865 }
866 psk[ j / 2 ] |= c;
867 }
868 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200869#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200870
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200871#if defined(POLARSSL_SSL_ALPN)
872 if( opt.alpn_string != NULL )
873 {
874 p = (char *) opt.alpn_string;
875 i = 0;
876
877 /* Leave room for a final NULL in alpn_list */
878 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
879 {
880 alpn_list[i++] = p;
881
882 /* Terminate the current string and move on to next one */
883 while( *p != ',' && *p != '\0' )
884 p++;
885 if( *p == ',' )
886 *p++ = '\0';
887 }
888 }
889#endif /* POLARSSL_SSL_ALPN */
890
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200891 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000892 * 0. Initialize the RNG and the session data
893 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000894 printf( "\n . Seeding the random number generator..." );
895 fflush( stdout );
896
897 entropy_init( &entropy );
898 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200899 (const unsigned char *) pers,
900 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000901 {
Paul Bakker0b22e3e2012-04-18 14:23:29 +0000902 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000903 goto exit;
904 }
905
906 printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000907
Paul Bakker36713e82013-09-17 13:25:29 +0200908#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000909 /*
910 * 1.1. Load the trusted CA
911 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000912 printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000913 fflush( stdout );
914
Paul Bakker5690efc2011-05-26 13:16:06 +0000915#if defined(POLARSSL_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +0000916 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100917 if( strcmp( opt.ca_path, "none" ) == 0 )
918 ret = 0;
919 else
920 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +0000921 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100922 if( strcmp( opt.ca_file, "none" ) == 0 )
923 ret = 0;
924 else
925 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +0200926 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000927#endif
928#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200929 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
930 strlen( test_ca_list ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000931#else
932 {
933 ret = 1;
934 printf("POLARSSL_CERTS_C not defined.");
935 }
936#endif
Paul Bakker42488232012-05-16 08:21:05 +0000937 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000938 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200939 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000940 goto exit;
941 }
942
Paul Bakker42488232012-05-16 08:21:05 +0000943 printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000944
945 /*
946 * 1.2. Load own certificate and private key
947 *
948 * (can be skipped if client authentication is not required)
949 */
950 printf( " . Loading the client cert. and key..." );
951 fflush( stdout );
952
Paul Bakker5690efc2011-05-26 13:16:06 +0000953#if defined(POLARSSL_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +0000954 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100955 if( strcmp( opt.crt_file, "none" ) == 0 )
956 ret = 0;
957 else
958 ret = x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +0200959 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000960#endif
961#if defined(POLARSSL_CERTS_C)
Paul Bakkerddf26b42013-09-18 13:46:23 +0200962 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000963 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000964#else
965 {
966 ret = 1;
967 printf("POLARSSL_CERTS_C not defined.");
968 }
969#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000970 if( ret != 0 )
971 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200972 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 goto exit;
974 }
975
Paul Bakker5690efc2011-05-26 13:16:06 +0000976#if defined(POLARSSL_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +0000977 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100978 if( strcmp( opt.key_file, "none" ) == 0 )
979 ret = 0;
980 else
981 ret = pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +0000982 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000983#endif
984#if defined(POLARSSL_CERTS_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200985 ret = pk_parse_key( &pkey, (const unsigned char *) test_cli_key,
Paul Bakker67968392010-07-18 08:28:20 +0000986 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000987#else
988 {
989 ret = 1;
990 printf("POLARSSL_CERTS_C not defined.");
991 }
992#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000993 if( ret != 0 )
994 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200995 printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000996 goto exit;
997 }
998
999 printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001000#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
1002 /*
1003 * 2. Start the connection
1004 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001005 if( opt.server_addr == NULL)
1006 opt.server_addr = opt.server_name;
1007
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001008 printf( " . Connecting to %s/%s/%-4d...",
1009 opt.transport == SSL_TRANSPORT_STREAM ? "tcp" : "udp",
1010 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001011 fflush( stdout );
1012
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001013 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
1014 opt.transport == SSL_TRANSPORT_STREAM ?
1015 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001016 {
Paul Bakker0b22e3e2012-04-18 14:23:29 +00001017 printf( " failed\n ! net_connect returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 goto exit;
1019 }
1020
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001021 if( opt.nbio > 0 )
1022 ret = net_set_nonblock( server_fd );
1023 else
1024 ret = net_set_block( server_fd );
1025 if( ret != 0 )
1026 {
1027 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
1028 goto exit;
1029 }
1030
Paul Bakker5121ce52009-01-03 21:22:43 +00001031 printf( " ok\n" );
1032
1033 /*
1034 * 3. Setup stuff
1035 */
1036 printf( " . Setting up the SSL/TLS structure..." );
1037 fflush( stdout );
1038
Paul Bakker5121ce52009-01-03 21:22:43 +00001039 if( ( ret = ssl_init( &ssl ) ) != 0 )
1040 {
Paul Bakker0b22e3e2012-04-18 14:23:29 +00001041 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001042 goto exit;
1043 }
1044
Paul Bakker36713e82013-09-17 13:25:29 +02001045#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker915275b2012-09-28 07:10:55 +00001046 if( opt.debug_level > 0 )
1047 ssl_set_verify( &ssl, my_verify, NULL );
Paul Bakkered27a042013-04-18 22:46:23 +02001048#endif
Paul Bakker915275b2012-09-28 07:10:55 +00001049
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001051 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001052
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001053#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001054 if( ( ret = ssl_set_transport( &ssl, opt.transport ) ) != 0 )
1055 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001056 printf( " failed\n ! selected transport is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001057 goto exit;
1058 }
1059
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001060 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
1061 ssl_set_handshake_timeout( &ssl, opt.hs_to_min, opt.hs_to_max );
1062#endif /* POLARSSL_SSL_PROTO_DTLS */
1063
Paul Bakker05decb22013-08-15 13:33:48 +02001064#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001065 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1066 {
1067 printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
1068 goto exit;
1069 }
Paul Bakker05decb22013-08-15 13:33:48 +02001070#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001071
Paul Bakker1f2bc622013-08-15 13:45:55 +02001072#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001073 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
1074 ssl_set_truncated_hmac( &ssl, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001075#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001076
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001077#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1078 if( opt.extended_ms != DFL_EXTENDED_MS )
1079 ssl_set_extended_master_secret( &ssl, opt.extended_ms );
1080#endif
1081
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001082#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1083 if( opt.etm != DFL_ETM )
1084 ssl_set_encrypt_then_mac( &ssl, opt.etm );
1085#endif
1086
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001087#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
1088 if( opt.recsplit != DFL_RECSPLIT )
1089 ssl_set_cbc_record_splitting( &ssl, opt.recsplit
1090 ? SSL_CBC_RECORD_SPLITTING_ENABLED
1091 : SSL_CBC_RECORD_SPLITTING_DISABLED );
1092#endif
1093
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001094#if defined(POLARSSL_SSL_ALPN)
1095 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001096 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1097 {
1098 printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
1099 goto exit;
1100 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001101#endif
1102
Paul Bakker508ad5a2011-12-04 17:09:26 +00001103 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker9caf2d22010-02-18 19:37:19 +00001104 ssl_set_dbg( &ssl, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001105
1106 if( opt.nbio == 2 )
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001107 ssl_set_bio_timeout( &ssl, &server_fd, my_send, my_recv, NULL,
1108 opt.read_timeout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001109 else
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001110 ssl_set_bio_timeout( &ssl, &server_fd, net_send, net_recv,
1111#if defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnardf0365122014-09-29 16:11:47 +02001112 opt.nbio == 0 ? net_recv_timeout : NULL,
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001113#else
1114 NULL,
1115#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001116 opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001117
Paul Bakkera503a632013-08-14 13:48:06 +02001118#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001119 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1120 {
1121 printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
1122 goto exit;
1123 }
Paul Bakkera503a632013-08-14 13:48:06 +02001124#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001125
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001126 /* RC4 setting is redundant if we use only one ciphersuite */
Paul Bakker645ce3a2012-10-31 12:32:41 +00001127 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker51936882011-02-20 16:05:58 +00001128 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001129 else
1130 ssl_set_arc4_support( &ssl, opt.arc4 );
Paul Bakker51936882011-02-20 16:05:58 +00001131
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001132 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
1133 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001134#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001135 ssl_set_renegotiation( &ssl, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001136#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001137
Paul Bakker36713e82013-09-17 13:25:29 +02001138#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001139 if( strcmp( opt.ca_path, "none" ) != 0 &&
1140 strcmp( opt.ca_file, "none" ) != 0 )
1141 {
1142 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
1143 }
1144 if( strcmp( opt.crt_file, "none" ) != 0 &&
1145 strcmp( opt.key_file, "none" ) != 0 )
1146 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001147 if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 )
1148 {
1149 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1150 goto exit;
1151 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001152 }
Paul Bakkered27a042013-04-18 22:46:23 +02001153#endif
1154
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001155#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001156 if( ( ret = ssl_set_psk( &ssl, psk, psk_len,
1157 (const unsigned char *) opt.psk_identity,
1158 strlen( opt.psk_identity ) ) ) != 0 )
1159 {
1160 printf( " failed\n ! ssl_set_psk returned %d\n\n", ret );
1161 goto exit;
1162 }
Paul Bakkered27a042013-04-18 22:46:23 +02001163#endif
1164
Paul Bakker0be444a2013-08-27 21:55:01 +02001165#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001166 if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1167 {
1168 printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret );
1169 goto exit;
1170 }
Paul Bakker0be444a2013-08-27 21:55:01 +02001171#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001172
Paul Bakker1d29fb52012-09-28 13:28:45 +00001173 if( opt.min_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001174 {
1175 ret = ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
Manuel Pégourié-Gonnard23eb74d2015-01-21 14:37:13 +00001176 if( ret != 0 && opt.min_version != DFL_MIN_VERSION )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001177 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001178 printf( " failed\n ! selected min_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001179 goto exit;
1180 }
1181 }
1182
Paul Bakker1d29fb52012-09-28 13:28:45 +00001183 if( opt.max_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001184 {
1185 ret = ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1186 if( ret != 0 )
1187 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001188 printf( " failed\n ! selected max_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001189 goto exit;
1190 }
1191 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001192
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001193#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1194 if( opt.fallback != DFL_FALLBACK )
1195 ssl_set_fallback( &ssl, opt.fallback );
1196#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001197
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001198 printf( " ok\n" );
1199
Paul Bakker5121ce52009-01-03 21:22:43 +00001200 /*
1201 * 4. Handshake
1202 */
1203 printf( " . Performing the SSL/TLS handshake..." );
1204 fflush( stdout );
1205
1206 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1207 {
1208 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker40e46942009-01-03 21:51:57 +00001209 {
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001210 printf( " failed\n ! ssl_handshake returned -0x%x\n", -ret );
1211 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
1212 printf(
1213 " Unable to verify the server's certificate. "
1214 "Either it is invalid,\n"
1215 " or you didn't set ca_file or ca_path "
1216 "to an appropriate value.\n"
1217 " Alternatively, you may want to use "
1218 "auth_mode=optional for testing purposes.\n" );
1219 printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00001221 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001222 }
1223
Manuel Pégourié-Gonnardc580a002014-02-12 10:15:30 +01001224 printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1225 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001226
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001227 if( ( ret = ssl_get_record_expansion( &ssl ) ) >= 0 )
1228 printf( " [ Record expansion is %d ]\n", ret );
1229 else
1230 printf( " [ Record expansion is unknown (compression) ]\n" );
1231
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001232#if defined(POLARSSL_SSL_ALPN)
1233 if( opt.alpn_string != NULL )
1234 {
1235 const char *alp = ssl_get_alpn_protocol( &ssl );
1236 printf( " [ Application Layer Protocol is %s ]\n",
1237 alp ? alp : "(none)" );
1238 }
1239#endif
1240
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001241 if( opt.reconnect != 0 )
1242 {
1243 printf(" . Saving session for reuse..." );
1244 fflush( stdout );
1245
1246 if( ( ret = ssl_get_session( &ssl, &saved_session ) ) != 0 )
1247 {
1248 printf( " failed\n ! ssl_get_session returned -0x%x\n\n", -ret );
1249 goto exit;
1250 }
1251
1252 printf( " ok\n" );
1253 }
1254
Paul Bakker36713e82013-09-17 13:25:29 +02001255#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 /*
1257 * 5. Verify the server certificate
1258 */
1259 printf( " . Verifying peer X.509 certificate..." );
1260
1261 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1262 {
1263 printf( " failed\n" );
1264
1265 if( ( ret & BADCERT_EXPIRED ) != 0 )
1266 printf( " ! server certificate has expired\n" );
1267
1268 if( ( ret & BADCERT_REVOKED ) != 0 )
1269 printf( " ! server certificate has been revoked\n" );
1270
1271 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
1272 printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
1273
1274 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
1275 printf( " ! self-signed or not signed by a trusted CA\n" );
1276
1277 printf( "\n" );
1278 }
1279 else
1280 printf( " ok\n" );
1281
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001282 if( ssl_get_peer_cert( &ssl ) != NULL )
1283 {
1284 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001285 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1286 ssl_get_peer_cert( &ssl ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001287 printf( "%s\n", buf );
1288 }
Paul Bakker36713e82013-09-17 13:25:29 +02001289#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001290
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001291#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001292 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001293 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001294 /*
1295 * Perform renegotiation (this must be done when the server is waiting
1296 * for input from our side).
1297 */
1298 printf( " . Performing renegotiation..." );
1299 fflush( stdout );
1300 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001301 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001302 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1303 ret != POLARSSL_ERR_NET_WANT_WRITE )
1304 {
1305 printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
1306 goto exit;
1307 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001308 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001309 printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001310 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001311#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001312
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 /*
1314 * 6. Write the GET request
1315 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001316 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001317send_request:
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 printf( " > Write to server:" );
1319 fflush( stdout );
1320
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001321 len = snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST,
1322 opt.request_page );
1323 tail_len = strlen( GET_REQUEST_END );
1324
1325 /* Add padding to GET request to reach opt.request_size in length */
1326 if( opt.request_size != DFL_REQUEST_SIZE &&
1327 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001328 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001329 memset( buf + len, 'A', opt.request_size - len - tail_len );
1330 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001331 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001333 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof(buf) - len - 1 );
1334 len += tail_len;
1335
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001336 /* Truncate if request size is smaller than the "natural" size */
1337 if( opt.request_size != DFL_REQUEST_SIZE &&
1338 len > opt.request_size )
1339 {
1340 len = opt.request_size;
1341
1342 /* Still end with \r\n unless that's really not possible */
1343 if( len >= 2 ) buf[len - 2] = '\r';
1344 if( len >= 1 ) buf[len - 1] = '\n';
1345 }
1346
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001347 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001348 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001349 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001351 while( ( ret = ssl_write( &ssl, buf + written, len - written ) )
1352 <= 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001353 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001354 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1355 ret != POLARSSL_ERR_NET_WANT_WRITE )
1356 {
1357 printf( " failed\n ! ssl_write returned -0x%x\n\n", -ret );
1358 goto exit;
1359 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001360 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 }
1362 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001363 else /* Not stream, so datagram */
1364 {
1365 do ret = ssl_write( &ssl, buf, len );
1366 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1367 ret == POLARSSL_ERR_NET_WANT_WRITE );
1368
1369 if( ret < 0 )
1370 {
1371 printf( " failed\n ! ssl_write returned %d\n\n", ret );
1372 goto exit;
1373 }
1374
1375 frags = 1;
1376 written = ret;
1377 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001378
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001379 buf[written] = '\0';
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001380 printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
1382 /*
1383 * 7. Read the HTTP response
1384 */
1385 printf( " < Read from server:" );
1386 fflush( stdout );
1387
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001388 /*
1389 * TLS and DTLS need different reading styles (stream vs datagram)
1390 */
1391 if( opt.transport == SSL_TRANSPORT_STREAM )
1392 {
1393 do
1394 {
1395 len = sizeof( buf ) - 1;
1396 memset( buf, 0, sizeof( buf ) );
1397 ret = ssl_read( &ssl, buf, len );
1398
1399 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1400 ret == POLARSSL_ERR_NET_WANT_WRITE )
1401 continue;
1402
1403 if( ret <= 0 )
1404 {
1405 switch( ret )
1406 {
1407 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1408 printf( " connection was closed gracefully\n" );
1409 ret = 0;
1410 goto close_notify;
1411
1412 case 0:
1413 case POLARSSL_ERR_NET_CONN_RESET:
1414 printf( " connection was reset by peer\n" );
1415 ret = 0;
1416 goto reconnect;
1417
1418 default:
1419 printf( " ssl_read returned -0x%x\n", -ret );
1420 goto exit;
1421 }
1422 }
1423
1424 len = ret;
1425 buf[len] = '\0';
1426 printf( " %d bytes read\n\n%s", len, (char *) buf );
1427
1428 /* End of message should be detected according to the syntax of the
1429 * application protocol (eg HTTP), just use a dummy test here. */
1430 if( ret > 0 && buf[len-1] == '\n' )
1431 {
1432 ret = 0;
1433 break;
1434 }
1435 }
1436 while( 1 );
1437 }
1438 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 {
1440 len = sizeof( buf ) - 1;
1441 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001443 do ret = ssl_read( &ssl, buf, len );
1444 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1445 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001446
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001447 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001448 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001449 switch( ret )
1450 {
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001451 case POLARSSL_ERR_NET_TIMEOUT:
1452 printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001453 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001454 goto send_request;
1455 goto exit;
1456
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001457 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1458 printf( " connection was closed gracefully\n" );
1459 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001460 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00001461
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001462 default:
1463 printf( " ssl_read returned -0x%x\n", -ret );
1464 goto exit;
1465 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001466 }
1467
Paul Bakker5121ce52009-01-03 21:22:43 +00001468 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02001469 buf[len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00001470 printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001471 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001472 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001473
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001474 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001475 * 7b. Continue doing data exchanges?
1476 */
1477 if( --opt.exchanges > 0 )
1478 goto send_request;
1479
1480 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001481 * 8. Done, cleanly close the connection
1482 */
1483close_notify:
1484 printf( " . Closing the connection..." );
1485
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001486 /* No error checking, the connection might be closed already */
Manuel Pégourié-Gonnard34377b12015-01-22 10:46:46 +00001487 do ret = ssl_close_notify( &ssl );
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001488 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
1489 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001490
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001491 printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001492
1493 /*
1494 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001495 */
1496reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001497 if( opt.reconnect != 0 )
1498 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001499 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001500
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001501 net_close( server_fd );
1502
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001503#if defined(POLARSSL_TIMING_C)
1504 if( opt.reco_delay > 0 )
1505 m_sleep( 1000 * opt.reco_delay );
1506#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02001507
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001508 printf( " . Reconnecting with saved session..." );
1509 fflush( stdout );
1510
1511 if( ( ret = ssl_session_reset( &ssl ) ) != 0 )
1512 {
1513 printf( " failed\n ! ssl_session_reset returned -0x%x\n\n", -ret );
1514 goto exit;
1515 }
1516
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001517 if( ( ret = ssl_set_session( &ssl, &saved_session ) ) != 0 )
1518 {
1519 printf( " failed\n ! ssl_set_session returned %d\n\n", ret );
1520 goto exit;
1521 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001522
Manuel Pégourié-Gonnard484b8f92014-09-23 12:36:12 +02001523 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001524 opt.transport == SSL_TRANSPORT_STREAM ?
1525 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001526 {
1527 printf( " failed\n ! net_connect returned -0x%x\n\n", -ret );
1528 goto exit;
1529 }
1530
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001531 if( opt.nbio > 0 )
1532 ret = net_set_nonblock( server_fd );
1533 else
1534 ret = net_set_block( server_fd );
1535 if( ret != 0 )
1536 {
1537 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
1538 -ret );
1539 goto exit;
1540 }
1541
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001542 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1543 {
1544 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1545 ret != POLARSSL_ERR_NET_WANT_WRITE )
1546 {
1547 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
1548 goto exit;
1549 }
1550 }
1551
1552 printf( " ok\n" );
1553
1554 goto send_request;
1555 }
1556
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001557 /*
1558 * Cleanup and exit
1559 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001560exit:
Paul Bakkera3d195c2011-11-27 21:07:34 +00001561#ifdef POLARSSL_ERROR_C
1562 if( ret != 0 )
1563 {
1564 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001565 polarssl_strerror( ret, error_buf, 100 );
Paul Bakker570267f2012-04-10 08:22:46 +00001566 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001567 }
1568#endif
1569
Paul Bakker1a207ec2011-02-06 13:22:40 +00001570 if( server_fd )
1571 net_close( server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001572
Paul Bakker36713e82013-09-17 13:25:29 +02001573#if defined(POLARSSL_X509_CRT_PARSE_C)
1574 x509_crt_free( &clicert );
1575 x509_crt_free( &cacert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02001576 pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001577#endif
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001578 ssl_session_free( &saved_session );
Paul Bakker5121ce52009-01-03 21:22:43 +00001579 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02001580 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02001581 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00001582
Paul Bakkercce9d772011-11-18 14:26:47 +00001583#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +00001584 printf( " + Press Enter to exit this program.\n" );
1585 fflush( stdout ); getchar();
1586#endif
1587
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001588 // Shell can not handle large exit numbers -> 1 for errors
1589 if( ret < 0 )
1590 ret = 1;
1591
Paul Bakker5121ce52009-01-03 21:22:43 +00001592 return( ret );
1593}
Paul Bakker508ad5a2011-12-04 17:09:26 +00001594#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
1595 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
1596 POLARSSL_CTR_DRBG_C */