blob: a4ae359c8b10eb9d1b9588dc2dbd825682241424 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client with certificate authentication
3 *
Paul Bakker3c5ef712013-06-25 16:37:45 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * 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 Bakker5121ce52009-01-03 21:22:43 +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_CLI_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_CLI_C and/or "
43 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
44 return( 0 );
45}
46#else
47
Paul Bakker5121ce52009-01-03 21:22:43 +000048#include <string.h>
Paul Bakker9caf2d22010-02-18 19:37:19 +000049#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000050#include <stdio.h>
51
Paul Bakker40e46942009-01-03 21:51:57 +000052#include "polarssl/net.h"
53#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000054#include "polarssl/entropy.h"
55#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000056#include "polarssl/certs.h"
57#include "polarssl/x509.h"
Paul Bakkera3d195c2011-11-27 21:07:34 +000058#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020059#include "polarssl/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010061#if defined(POLARSSL_TIMING_C)
62#include "polarssl/timing.h"
63#endif
64
Paul Bakker93c32b22014-04-25 13:40:05 +020065#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
66#if !defined snprintf
67#define snprintf _snprintf
68#endif
69#endif
70
Paul Bakker9caf2d22010-02-18 19:37:19 +000071#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010072#define DFL_SERVER_ADDR NULL
Paul Bakker9caf2d22010-02-18 19:37:19 +000073#define DFL_SERVER_PORT 4433
74#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +020075#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +000076#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010077#define DFL_NBIO 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020078#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +020079#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +000080#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000081#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000082#define DFL_CRT_FILE ""
83#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020084#define DFL_PSK ""
85#define DFL_PSK_IDENTITY "Client_identity"
Paul Bakker51936882011-02-20 16:05:58 +000086#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +020087#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010088#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010089#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +020090#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +010091#define DFL_MIN_VERSION SSL_MINOR_VERSION_1
Paul Bakker1d29fb52012-09-28 13:28:45 +000092#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +010093#define DFL_ARC4 SSL_ARC4_DISABLED
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +010094#define DFL_AUTH_MODE SSL_VERIFY_REQUIRED
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +020095#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +010096#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +010097#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +020098#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010099#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200100#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200101#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100102#define DFL_TRANSPORT SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200103#define DFL_HS_TO_MIN 0
104#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200105#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200106#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100107#define DFL_ETM -1
Paul Bakker0e6975b2009-02-10 22:19:10 +0000108
Paul Bakker93c32b22014-04-25 13:40:05 +0200109#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
110#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Paul Bakker9caf2d22010-02-18 19:37:19 +0000112/*
113 * global options
114 */
115struct options
116{
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200117 const char *server_name; /* hostname of the server (client only) */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100118 const char *server_addr; /* address of the server (client only) */
Paul Bakker67968392010-07-18 08:28:20 +0000119 int server_port; /* port on which the ssl service runs */
120 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100121 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200122 uint32_t read_timeout; /* timeout on ssl_read() in milliseconds */
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200123 int max_resend; /* DTLS times to resend on read timeout */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200124 const char *request_page; /* page on server to request */
Paul Bakker93c32b22014-04-25 13:40:05 +0200125 int request_size; /* pad request with header to requested size */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200126 const char *ca_file; /* the file with the CA certificate(s) */
127 const char *ca_path; /* the path with the CA certificate(s) reside */
128 const char *crt_file; /* the file with the client certificate */
129 const char *key_file; /* the file with the client key */
130 const char *psk; /* the pre-shared key */
131 const char *psk_identity; /* the pre-shared key identity */
Paul Bakker51936882011-02-20 16:05:58 +0000132 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Paul Bakker48916f92012-09-16 19:57:18 +0000133 int renegotiation; /* enable / disable renegotiation */
134 int allow_legacy; /* allow legacy renegotiation */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100135 int renegotiate; /* attempt renegotiation? */
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200136 int renego_delay; /* delay before enforcing renegotiation */
137 int exchanges; /* number of data exchanges */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000138 int min_version; /* minimum protocol version accepted */
139 int max_version; /* maximum protocol version accepted */
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100140 int arc4; /* flag for arc4 suites support */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100141 int auth_mode; /* verify mode for connection */
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200142 unsigned char mfl_code; /* code for maximum fragment length */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200143 int trunc_hmac; /* negotiate truncated hmac or not */
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100144 int recsplit; /* enable record splitting? */
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200145 int reconnect; /* attempt to resume session */
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100146 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200147 int tickets; /* enable / disable session tickets */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200148 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100149 int transport; /* TLS or DTLS? */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200150 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
151 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200152 int fallback; /* is this a fallback connection? */
Manuel Pégourié-Gonnard98286562015-01-12 19:17:05 +0100153 int extended_ms; /* negotiate extended master secret? */
Paul Bakker8b9bcec2015-01-13 15:59:55 +0100154 int etm; /* negotiate encrypt then mac? */
Paul Bakker9caf2d22010-02-18 19:37:19 +0000155} opt;
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Paul Bakker3c5ef712013-06-25 16:37:45 +0200157static void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +0000158{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200159 ((void) level);
160
161 fprintf( (FILE *) ctx, "%s", str );
162 fflush( (FILE *) ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163}
164
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100165/*
166 * Test recv/send functions that make sure each try returns
167 * WANT_READ/WANT_WRITE at least once before sucesseding
168 */
169static int my_recv( void *ctx, unsigned char *buf, size_t len )
170{
171 static int first_try = 1;
172 int ret;
173
174 if( first_try )
175 {
176 first_try = 0;
177 return( POLARSSL_ERR_NET_WANT_READ );
178 }
179
180 ret = net_recv( ctx, buf, len );
181 if( ret != POLARSSL_ERR_NET_WANT_READ )
182 first_try = 1; /* Next call will be a new operation */
183 return( ret );
184}
185
186static int my_send( void *ctx, const unsigned char *buf, size_t len )
187{
188 static int first_try = 1;
189 int ret;
190
191 if( first_try )
192 {
193 first_try = 0;
194 return( POLARSSL_ERR_NET_WANT_WRITE );
195 }
196
197 ret = net_send( ctx, buf, len );
198 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
199 first_try = 1; /* Next call will be a new operation */
200 return( ret );
201}
202
Paul Bakker36713e82013-09-17 13:25:29 +0200203#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker915275b2012-09-28 07:10:55 +0000204/*
205 * Enabled if debug_level > 1 in code below
206 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200207static int my_verify( void *data, x509_crt *crt, int depth, int *flags )
Paul Bakker915275b2012-09-28 07:10:55 +0000208{
209 char buf[1024];
210 ((void) data);
211
212 printf( "\nVerify requested for (Depth %d):\n", depth );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200213 x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Paul Bakker915275b2012-09-28 07:10:55 +0000214 printf( "%s", buf );
215
216 if( ( (*flags) & BADCERT_EXPIRED ) != 0 )
217 printf( " ! server certificate has expired\n" );
218
219 if( ( (*flags) & BADCERT_REVOKED ) != 0 )
220 printf( " ! server certificate has been revoked\n" );
221
222 if( ( (*flags) & BADCERT_CN_MISMATCH ) != 0 )
223 printf( " ! CN mismatch\n" );
224
225 if( ( (*flags) & BADCERT_NOT_TRUSTED ) != 0 )
226 printf( " ! self-signed or not signed by a trusted CA\n" );
227
228 if( ( (*flags) & BADCRL_NOT_TRUSTED ) != 0 )
229 printf( " ! CRL not trusted\n" );
230
231 if( ( (*flags) & BADCRL_EXPIRED ) != 0 )
232 printf( " ! CRL expired\n" );
233
234 if( ( (*flags) & BADCERT_OTHER ) != 0 )
235 printf( " ! other (unknown) flag\n" );
236
237 if ( ( *flags ) == 0 )
238 printf( " This certificate has no flags\n" );
239
240 return( 0 );
241}
Paul Bakker36713e82013-09-17 13:25:29 +0200242#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +0000243
Paul Bakker36713e82013-09-17 13:25:29 +0200244#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5690efc2011-05-26 13:16:06 +0000245#if defined(POLARSSL_FS_IO)
246#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100247 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
248 " default: \"\" (pre-loaded)\n" \
249 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
250 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
251 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
252 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000253 " key_file=%%s default: \"\" (pre-loaded)\n"
254#else
255#define USAGE_IO \
256 " No file operations available (POLARSSL_FS_IO not defined)\n"
257#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200258#else
259#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200260#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200261
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200262#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200263#define USAGE_PSK \
264 " psk=%%s default: \"\" (in hex, without 0x)\n" \
265 " psk_identity=%%s default: \"Client_identity\"\n"
266#else
267#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200268#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000269
Paul Bakkera503a632013-08-14 13:48:06 +0200270#if defined(POLARSSL_SSL_SESSION_TICKETS)
271#define USAGE_TICKETS \
272 " tickets=%%d default: 1 (enabled)\n"
273#else
274#define USAGE_TICKETS ""
275#endif /* POLARSSL_SSL_SESSION_TICKETS */
276
Paul Bakker1f2bc622013-08-15 13:45:55 +0200277#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
278#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100279 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200280#else
281#define USAGE_TRUNC_HMAC ""
282#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
283
Paul Bakker05decb22013-08-15 13:33:48 +0200284#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
285#define USAGE_MAX_FRAG_LEN \
286 " max_frag_len=%%d default: 16384 (tls default)\n" \
287 " options: 512, 1024, 2048, 4096\n"
288#else
289#define USAGE_MAX_FRAG_LEN ""
290#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
291
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100292#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
293#define USAGE_RECSPLIT \
294 " recplit=%%d default: (library default)\n"
295#else
296#define USAGE_RECSPLIT
297#endif
298
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100299#if defined(POLARSSL_TIMING_C)
300#define USAGE_TIME \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200301 " reco_delay=%%d default: 0 seconds\n"
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100302#else
303#define USAGE_TIME ""
304#endif /* POLARSSL_TIMING_C */
305
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200306#if defined(POLARSSL_SSL_ALPN)
307#define USAGE_ALPN \
308 " alpn=%%s default: \"\" (disabled)\n" \
309 " example: spdy/1,http/1.1\n"
310#else
311#define USAGE_ALPN ""
312#endif /* POLARSSL_SSL_ALPN */
313
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200314#if defined(POLARSSL_SSL_PROTO_DTLS)
315#define USAGE_DTLS \
316 " dtls=%%d default: 0 (TLS)\n" \
317 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
318 " range of DTLS handshake timeouts in millisecs\n"
319#else
320#define USAGE_DTLS ""
321#endif
322
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200323#if defined(POLARSSL_SSL_FALLBACK_SCSV)
324#define USAGE_FALLBACK \
325 " fallback=0/1 default: (library default: off)\n"
326#else
327#define USAGE_FALLBACK ""
328#endif
329
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200330#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
331#define USAGE_EMS \
332 " extended_ms=0/1 default: (library default: on)\n"
333#else
334#define USAGE_EMS ""
335#endif
336
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100337#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
338#define USAGE_ETM \
339 " etm=0/1 default: (library default: on)\n"
340#else
341#define USAGE_ETM ""
342#endif
343
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100344#if defined(POLARSSL_SSL_RENEGOTIATION)
345#define USAGE_RENEGO \
346 " renegotiation=%%d default: 0 (disabled)\n" \
347 " renegotiate=%%d default: 0 (disabled)\n"
348#else
349#define USAGE_RENEGO ""
350#endif
351
Paul Bakker9caf2d22010-02-18 19:37:19 +0000352#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000353 "\n usage: ssl_client2 param=<>...\n" \
354 "\n acceptable parameters:\n" \
355 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100356 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000357 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000358 " request_page=%%s default: \".\"\n" \
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +0200359 " request_size=%%d default: about 34 (basic request)\n" \
360 " (minimum: 0, max: 16384)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100361 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100362 " nbio=%%d default: 0 (blocking I/O)\n" \
363 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200364 " read_timeout=%%d default: 0 (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200365 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100366 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200367 USAGE_DTLS \
368 "\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100369 " auth_mode=%%s default: \"optional\"\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100370 " options: none, optional, required\n" \
371 USAGE_IO \
372 "\n" \
373 USAGE_PSK \
374 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100375 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100376 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200377 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200378 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100379 USAGE_TIME \
Paul Bakkera503a632013-08-14 13:48:06 +0200380 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100381 USAGE_MAX_FRAG_LEN \
382 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200383 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200384 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200385 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100386 USAGE_ETM \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100387 USAGE_RECSPLIT \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000388 "\n" \
389 " min_version=%%s default: \"\" (ssl3)\n" \
390 " max_version=%%s default: \"\" (tls1_2)\n" \
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100391 " arc4=%%d default: 0 (disabled)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000392 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100393 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100394 " auth_mode=%%s default: \"required\"\n" \
Paul Bakker91ebfb52012-11-23 14:04:08 +0100395 " options: none, optional, required\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000396 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000397 " force_ciphersuite=<name> default: all enabled\n"\
398 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000399
400int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000401{
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200402 int ret = 0, len, tail_len, server_fd, i, written, frags, retry_left;
Paul Bakker93c32b22014-04-25 13:40:05 +0200403 unsigned char buf[SSL_MAX_CONTENT_LEN + 1];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200404#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200405 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200406 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200407#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200408#if defined(POLARSSL_SSL_ALPN)
409 const char *alpn_list[10];
410#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200411 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000412
413 entropy_context entropy;
414 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 ssl_context ssl;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200416 ssl_session saved_session;
Paul Bakker36713e82013-09-17 13:25:29 +0200417#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200418 x509_crt cacert;
419 x509_crt clicert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200420 pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200421#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000422 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000423 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000424
Paul Bakker1a207ec2011-02-06 13:22:40 +0000425 /*
426 * Make sure memory references are valid.
427 */
428 server_fd = 0;
Paul Bakker1a207ec2011-02-06 13:22:40 +0000429 memset( &ssl, 0, sizeof( ssl_context ) );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200430 memset( &saved_session, 0, sizeof( ssl_session ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200431#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200432 x509_crt_init( &cacert );
433 x509_crt_init( &clicert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200434 pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200435#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200436#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200437 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200438#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000439
Paul Bakker9caf2d22010-02-18 19:37:19 +0000440 if( argc == 0 )
441 {
442 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000443 if( ret == 0 )
444 ret = 1;
445
Paul Bakker9caf2d22010-02-18 19:37:19 +0000446 printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000447
448 list = ssl_list_ciphersuites();
449 while( *list )
450 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200451 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200452 list++;
453 if( !*list )
454 break;
455 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000456 list++;
457 }
458 printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000459 goto exit;
460 }
461
462 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100463 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000464 opt.server_port = DFL_SERVER_PORT;
465 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100466 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200467 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200468 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000469 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200470 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000471 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000472 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000473 opt.crt_file = DFL_CRT_FILE;
474 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200475 opt.psk = DFL_PSK;
476 opt.psk_identity = DFL_PSK_IDENTITY;
Paul Bakker51936882011-02-20 16:05:58 +0000477 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000478 opt.renegotiation = DFL_RENEGOTIATION;
479 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100480 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200481 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000482 opt.min_version = DFL_MIN_VERSION;
483 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100484 opt.arc4 = DFL_ARC4;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100485 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200486 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200487 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100488 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200489 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100490 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200491 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200492 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200493 opt.transport = DFL_TRANSPORT;
494 opt.hs_to_min = DFL_HS_TO_MIN;
495 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200496 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200497 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100498 opt.etm = DFL_ETM;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000499
500 for( i = 1; i < argc; i++ )
501 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000502 p = argv[i];
503 if( ( q = strchr( p, '=' ) ) == NULL )
504 goto usage;
505 *q++ = '\0';
506
507 if( strcmp( p, "server_name" ) == 0 )
508 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100509 else if( strcmp( p, "server_addr" ) == 0 )
510 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000511 else if( strcmp( p, "server_port" ) == 0 )
512 {
513 opt.server_port = atoi( q );
514 if( opt.server_port < 1 || opt.server_port > 65535 )
515 goto usage;
516 }
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100517 else if( strcmp( p, "dtls" ) == 0 )
518 {
519 int t = atoi( q );
520 if( t == 0 )
521 opt.transport = SSL_TRANSPORT_STREAM;
522 else if( t == 1 )
523 opt.transport = SSL_TRANSPORT_DATAGRAM;
524 else
525 goto usage;
526 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000527 else if( strcmp( p, "debug_level" ) == 0 )
528 {
529 opt.debug_level = atoi( q );
530 if( opt.debug_level < 0 || opt.debug_level > 65535 )
531 goto usage;
532 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100533 else if( strcmp( p, "nbio" ) == 0 )
534 {
535 opt.nbio = atoi( q );
536 if( opt.nbio < 0 || opt.nbio > 2 )
537 goto usage;
538 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200539 else if( strcmp( p, "read_timeout" ) == 0 )
540 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200541 else if( strcmp( p, "max_resend" ) == 0 )
542 {
543 opt.max_resend = atoi( q );
544 if( opt.max_resend < 0 )
545 goto usage;
546 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000547 else if( strcmp( p, "request_page" ) == 0 )
548 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200549 else if( strcmp( p, "request_size" ) == 0 )
550 {
551 opt.request_size = atoi( q );
552 if( opt.request_size < 0 || opt.request_size > SSL_MAX_CONTENT_LEN )
553 goto usage;
554 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000555 else if( strcmp( p, "ca_file" ) == 0 )
556 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000557 else if( strcmp( p, "ca_path" ) == 0 )
558 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000559 else if( strcmp( p, "crt_file" ) == 0 )
560 opt.crt_file = q;
561 else if( strcmp( p, "key_file" ) == 0 )
562 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200563 else if( strcmp( p, "psk" ) == 0 )
564 opt.psk = q;
565 else if( strcmp( p, "psk_identity" ) == 0 )
566 opt.psk_identity = q;
Paul Bakker51936882011-02-20 16:05:58 +0000567 else if( strcmp( p, "force_ciphersuite" ) == 0 )
568 {
Paul Bakker51936882011-02-20 16:05:58 +0000569 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
570
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200571 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000572 {
573 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000574 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000575 }
Paul Bakker51936882011-02-20 16:05:58 +0000576 opt.force_ciphersuite[1] = 0;
577 }
Paul Bakker48916f92012-09-16 19:57:18 +0000578 else if( strcmp( p, "renegotiation" ) == 0 )
579 {
580 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
581 SSL_RENEGOTIATION_DISABLED;
582 }
583 else if( strcmp( p, "allow_legacy" ) == 0 )
584 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100585 switch( atoi( q ) )
586 {
587 case -1: opt.allow_legacy = SSL_LEGACY_BREAK_HANDSHAKE; break;
588 case 0: opt.allow_legacy = SSL_LEGACY_NO_RENEGOTIATION; break;
589 case 1: opt.allow_legacy = SSL_LEGACY_ALLOW_RENEGOTIATION; break;
590 default: goto usage;
591 }
Paul Bakker48916f92012-09-16 19:57:18 +0000592 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100593 else if( strcmp( p, "renegotiate" ) == 0 )
594 {
595 opt.renegotiate = atoi( q );
596 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
597 goto usage;
598 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200599 else if( strcmp( p, "exchanges" ) == 0 )
600 {
601 opt.exchanges = atoi( q );
602 if( opt.exchanges < 1 )
603 goto usage;
604 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200605 else if( strcmp( p, "reconnect" ) == 0 )
606 {
607 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200608 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200609 goto usage;
610 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100611 else if( strcmp( p, "reco_delay" ) == 0 )
612 {
613 opt.reco_delay = atoi( q );
614 if( opt.reco_delay < 0 )
615 goto usage;
616 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200617 else if( strcmp( p, "tickets" ) == 0 )
618 {
619 opt.tickets = atoi( q );
620 if( opt.tickets < 0 || opt.tickets > 2 )
621 goto usage;
622 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200623 else if( strcmp( p, "alpn" ) == 0 )
624 {
625 opt.alpn_string = q;
626 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200627 else if( strcmp( p, "fallback" ) == 0 )
628 {
629 switch( atoi( q ) )
630 {
631 case 0: opt.fallback = SSL_IS_NOT_FALLBACK; break;
632 case 1: opt.fallback = SSL_IS_FALLBACK; break;
633 default: goto usage;
634 }
635 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200636 else if( strcmp( p, "extended_ms" ) == 0 )
637 {
638 switch( atoi( q ) )
639 {
640 case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break;
641 case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break;
642 default: goto usage;
643 }
644 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100645 else if( strcmp( p, "etm" ) == 0 )
646 {
647 switch( atoi( q ) )
648 {
649 case 0: opt.etm = SSL_ETM_DISABLED; break;
650 case 1: opt.etm = SSL_ETM_ENABLED; break;
651 default: goto usage;
652 }
653 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000654 else if( strcmp( p, "min_version" ) == 0 )
655 {
656 if( strcmp( q, "ssl3" ) == 0 )
657 opt.min_version = SSL_MINOR_VERSION_0;
658 else if( strcmp( q, "tls1" ) == 0 )
659 opt.min_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100660 else if( strcmp( q, "tls1_1" ) == 0 ||
661 strcmp( q, "dtls1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000662 opt.min_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100663 else if( strcmp( q, "tls1_2" ) == 0 ||
664 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000665 opt.min_version = SSL_MINOR_VERSION_3;
666 else
667 goto usage;
668 }
669 else if( strcmp( p, "max_version" ) == 0 )
670 {
671 if( strcmp( q, "ssl3" ) == 0 )
672 opt.max_version = SSL_MINOR_VERSION_0;
673 else if( strcmp( q, "tls1" ) == 0 )
674 opt.max_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100675 else if( strcmp( q, "tls1_1" ) == 0 ||
676 strcmp( q, "dtls1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000677 opt.max_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100678 else if( strcmp( q, "tls1_2" ) == 0 ||
679 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000680 opt.max_version = SSL_MINOR_VERSION_3;
681 else
682 goto usage;
683 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100684 else if( strcmp( p, "arc4" ) == 0 )
685 {
686 switch( atoi( q ) )
687 {
688 case 0: opt.arc4 = SSL_ARC4_DISABLED; break;
689 case 1: opt.arc4 = SSL_ARC4_ENABLED; break;
690 default: goto usage;
691 }
692 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000693 else if( strcmp( p, "force_version" ) == 0 )
694 {
695 if( strcmp( q, "ssl3" ) == 0 )
696 {
697 opt.min_version = SSL_MINOR_VERSION_0;
698 opt.max_version = SSL_MINOR_VERSION_0;
699 }
700 else if( strcmp( q, "tls1" ) == 0 )
701 {
702 opt.min_version = SSL_MINOR_VERSION_1;
703 opt.max_version = SSL_MINOR_VERSION_1;
704 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100705 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000706 {
707 opt.min_version = SSL_MINOR_VERSION_2;
708 opt.max_version = SSL_MINOR_VERSION_2;
709 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100710 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000711 {
712 opt.min_version = SSL_MINOR_VERSION_3;
713 opt.max_version = SSL_MINOR_VERSION_3;
714 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100715 else if( strcmp( q, "dtls1" ) == 0 )
716 {
717 opt.min_version = SSL_MINOR_VERSION_2;
718 opt.max_version = SSL_MINOR_VERSION_2;
719 opt.transport = SSL_TRANSPORT_DATAGRAM;
720 }
721 else if( strcmp( q, "dtls1_2" ) == 0 )
722 {
723 opt.min_version = SSL_MINOR_VERSION_3;
724 opt.max_version = SSL_MINOR_VERSION_3;
725 opt.transport = SSL_TRANSPORT_DATAGRAM;
726 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000727 else
728 goto usage;
729 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100730 else if( strcmp( p, "auth_mode" ) == 0 )
731 {
732 if( strcmp( q, "none" ) == 0 )
733 opt.auth_mode = SSL_VERIFY_NONE;
734 else if( strcmp( q, "optional" ) == 0 )
735 opt.auth_mode = SSL_VERIFY_OPTIONAL;
736 else if( strcmp( q, "required" ) == 0 )
737 opt.auth_mode = SSL_VERIFY_REQUIRED;
738 else
739 goto usage;
740 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200741 else if( strcmp( p, "max_frag_len" ) == 0 )
742 {
743 if( strcmp( q, "512" ) == 0 )
744 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
745 else if( strcmp( q, "1024" ) == 0 )
746 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
747 else if( strcmp( q, "2048" ) == 0 )
748 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
749 else if( strcmp( q, "4096" ) == 0 )
750 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
751 else
752 goto usage;
753 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200754 else if( strcmp( p, "trunc_hmac" ) == 0 )
755 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100756 switch( atoi( q ) )
757 {
758 case 0: opt.trunc_hmac = SSL_TRUNC_HMAC_DISABLED; break;
759 case 1: opt.trunc_hmac = SSL_TRUNC_HMAC_ENABLED; break;
760 default: goto usage;
761 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200762 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200763 else if( strcmp( p, "hs_timeout" ) == 0 )
764 {
765 if( ( p = strchr( q, '-' ) ) == NULL )
766 goto usage;
767 *p++ = '\0';
768 opt.hs_to_min = atoi( q );
769 opt.hs_to_max = atoi( p );
770 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
771 goto usage;
772 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100773 else if( strcmp( p, "recsplit" ) == 0 )
774 {
775 opt.recsplit = atoi( q );
776 if( opt.recsplit < 0 || opt.recsplit > 1 )
777 goto usage;
778 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000779 else
780 goto usage;
781 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000782
Paul Bakkerc73079a2014-04-25 16:34:30 +0200783#if defined(POLARSSL_DEBUG_C)
784 debug_set_threshold( opt.debug_level );
785#endif
786
Paul Bakkerc1516be2013-06-29 16:01:32 +0200787 if( opt.force_ciphersuite[0] > 0 )
788 {
789 const ssl_ciphersuite_t *ciphersuite_info;
790 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
791
Paul Bakker66c48102013-07-26 14:05:32 +0200792 if( opt.max_version != -1 &&
793 ciphersuite_info->min_minor_ver > opt.max_version )
794 {
795 printf("forced ciphersuite not allowed with this protocol version\n");
796 ret = 2;
797 goto usage;
798 }
799 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200800 ciphersuite_info->max_minor_ver < opt.min_version )
801 {
802 printf("forced ciphersuite not allowed with this protocol version\n");
803 ret = 2;
804 goto usage;
805 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100806
807 /* If the server selects a version that's not supported by
808 * this suite, then there will be no common ciphersuite... */
809 if( opt.max_version == -1 ||
810 opt.max_version > ciphersuite_info->max_minor_ver )
811 {
Paul Bakker66c48102013-07-26 14:05:32 +0200812 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100813 }
Paul Bakker66c48102013-07-26 14:05:32 +0200814 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100815 {
Paul Bakker66c48102013-07-26 14:05:32 +0200816 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100817 /* DTLS starts with TLS 1.1 */
818 if( opt.transport == SSL_TRANSPORT_DATAGRAM &&
819 opt.min_version < SSL_MINOR_VERSION_2 )
820 opt.min_version = SSL_MINOR_VERSION_2;
821 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200822 }
823
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200824#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000825 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200826 * Unhexify the pre-shared key if any is given
827 */
828 if( strlen( opt.psk ) )
829 {
830 unsigned char c;
831 size_t j;
832
833 if( strlen( opt.psk ) % 2 != 0 )
834 {
835 printf("pre-shared key not valid hex\n");
836 goto exit;
837 }
838
839 psk_len = strlen( opt.psk ) / 2;
840
841 for( j = 0; j < strlen( opt.psk ); j += 2 )
842 {
843 c = opt.psk[j];
844 if( c >= '0' && c <= '9' )
845 c -= '0';
846 else if( c >= 'a' && c <= 'f' )
847 c -= 'a' - 10;
848 else if( c >= 'A' && c <= 'F' )
849 c -= 'A' - 10;
850 else
851 {
852 printf("pre-shared key not valid hex\n");
853 goto exit;
854 }
855 psk[ j / 2 ] = c << 4;
856
857 c = opt.psk[j + 1];
858 if( c >= '0' && c <= '9' )
859 c -= '0';
860 else if( c >= 'a' && c <= 'f' )
861 c -= 'a' - 10;
862 else if( c >= 'A' && c <= 'F' )
863 c -= 'A' - 10;
864 else
865 {
866 printf("pre-shared key not valid hex\n");
867 goto exit;
868 }
869 psk[ j / 2 ] |= c;
870 }
871 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200872#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200873
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200874#if defined(POLARSSL_SSL_ALPN)
875 if( opt.alpn_string != NULL )
876 {
877 p = (char *) opt.alpn_string;
878 i = 0;
879
880 /* Leave room for a final NULL in alpn_list */
881 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
882 {
883 alpn_list[i++] = p;
884
885 /* Terminate the current string and move on to next one */
886 while( *p != ',' && *p != '\0' )
887 p++;
888 if( *p == ',' )
889 *p++ = '\0';
890 }
891 }
892#endif /* POLARSSL_SSL_ALPN */
893
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200894 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000895 * 0. Initialize the RNG and the session data
896 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000897 printf( "\n . Seeding the random number generator..." );
898 fflush( stdout );
899
900 entropy_init( &entropy );
901 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200902 (const unsigned char *) pers,
903 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000904 {
Paul Bakker0b22e3e2012-04-18 14:23:29 +0000905 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000906 goto exit;
907 }
908
909 printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000910
Paul Bakker36713e82013-09-17 13:25:29 +0200911#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000912 /*
913 * 1.1. Load the trusted CA
914 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000915 printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000916 fflush( stdout );
917
Paul Bakker5690efc2011-05-26 13:16:06 +0000918#if defined(POLARSSL_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +0000919 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100920 if( strcmp( opt.ca_path, "none" ) == 0 )
921 ret = 0;
922 else
923 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +0000924 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100925 if( strcmp( opt.ca_file, "none" ) == 0 )
926 ret = 0;
927 else
928 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +0200929 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000930#endif
931#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200932 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
933 strlen( test_ca_list ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000934#else
935 {
936 ret = 1;
937 printf("POLARSSL_CERTS_C not defined.");
938 }
939#endif
Paul Bakker42488232012-05-16 08:21:05 +0000940 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000941 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200942 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000943 goto exit;
944 }
945
Paul Bakker42488232012-05-16 08:21:05 +0000946 printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000947
948 /*
949 * 1.2. Load own certificate and private key
950 *
951 * (can be skipped if client authentication is not required)
952 */
953 printf( " . Loading the client cert. and key..." );
954 fflush( stdout );
955
Paul Bakker5690efc2011-05-26 13:16:06 +0000956#if defined(POLARSSL_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +0000957 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100958 if( strcmp( opt.crt_file, "none" ) == 0 )
959 ret = 0;
960 else
961 ret = x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +0200962 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000963#endif
964#if defined(POLARSSL_CERTS_C)
Paul Bakkerddf26b42013-09-18 13:46:23 +0200965 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000966 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000967#else
968 {
969 ret = 1;
970 printf("POLARSSL_CERTS_C not defined.");
971 }
972#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 if( ret != 0 )
974 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200975 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000976 goto exit;
977 }
978
Paul Bakker5690efc2011-05-26 13:16:06 +0000979#if defined(POLARSSL_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +0000980 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100981 if( strcmp( opt.key_file, "none" ) == 0 )
982 ret = 0;
983 else
984 ret = pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +0000985 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000986#endif
987#if defined(POLARSSL_CERTS_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200988 ret = pk_parse_key( &pkey, (const unsigned char *) test_cli_key,
Paul Bakker67968392010-07-18 08:28:20 +0000989 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000990#else
991 {
992 ret = 1;
993 printf("POLARSSL_CERTS_C not defined.");
994 }
995#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000996 if( ret != 0 )
997 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200998 printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 goto exit;
1000 }
1001
1002 printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001003#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001004
1005 /*
1006 * 2. Start the connection
1007 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +01001008 if( opt.server_addr == NULL)
1009 opt.server_addr = opt.server_name;
1010
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001011 printf( " . Connecting to %s/%s/%-4d...",
1012 opt.transport == SSL_TRANSPORT_STREAM ? "tcp" : "udp",
1013 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 fflush( stdout );
1015
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001016 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
1017 opt.transport == SSL_TRANSPORT_STREAM ?
1018 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001019 {
Paul Bakker0b22e3e2012-04-18 14:23:29 +00001020 printf( " failed\n ! net_connect returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001021 goto exit;
1022 }
1023
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001024 if( opt.nbio > 0 )
1025 ret = net_set_nonblock( server_fd );
1026 else
1027 ret = net_set_block( server_fd );
1028 if( ret != 0 )
1029 {
1030 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
1031 goto exit;
1032 }
1033
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 printf( " ok\n" );
1035
1036 /*
1037 * 3. Setup stuff
1038 */
1039 printf( " . Setting up the SSL/TLS structure..." );
1040 fflush( stdout );
1041
Paul Bakker5121ce52009-01-03 21:22:43 +00001042 if( ( ret = ssl_init( &ssl ) ) != 0 )
1043 {
Paul Bakker0b22e3e2012-04-18 14:23:29 +00001044 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001045 goto exit;
1046 }
1047
Paul Bakker36713e82013-09-17 13:25:29 +02001048#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker915275b2012-09-28 07:10:55 +00001049 if( opt.debug_level > 0 )
1050 ssl_set_verify( &ssl, my_verify, NULL );
Paul Bakkered27a042013-04-18 22:46:23 +02001051#endif
Paul Bakker915275b2012-09-28 07:10:55 +00001052
Paul Bakker5121ce52009-01-03 21:22:43 +00001053 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001054 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001055
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001056#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001057 if( ( ret = ssl_set_transport( &ssl, opt.transport ) ) != 0 )
1058 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001059 printf( " failed\n ! selected transport is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001060 goto exit;
1061 }
1062
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001063 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
1064 ssl_set_handshake_timeout( &ssl, opt.hs_to_min, opt.hs_to_max );
1065#endif /* POLARSSL_SSL_PROTO_DTLS */
1066
Paul Bakker05decb22013-08-15 13:33:48 +02001067#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001068 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1069 {
1070 printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
1071 goto exit;
1072 }
Paul Bakker05decb22013-08-15 13:33:48 +02001073#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001074
Paul Bakker1f2bc622013-08-15 13:45:55 +02001075#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +01001076 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
1077 ssl_set_truncated_hmac( &ssl, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +02001078#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001079
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001080#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1081 if( opt.extended_ms != DFL_EXTENDED_MS )
1082 ssl_set_extended_master_secret( &ssl, opt.extended_ms );
1083#endif
1084
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001085#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1086 if( opt.etm != DFL_ETM )
1087 ssl_set_encrypt_then_mac( &ssl, opt.etm );
1088#endif
1089
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001090#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
1091 if( opt.recsplit != DFL_RECSPLIT )
1092 ssl_set_cbc_record_splitting( &ssl, opt.recsplit
1093 ? SSL_CBC_RECORD_SPLITTING_ENABLED
1094 : SSL_CBC_RECORD_SPLITTING_DISABLED );
1095#endif
1096
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001097#if defined(POLARSSL_SSL_ALPN)
1098 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001099 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1100 {
1101 printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
1102 goto exit;
1103 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001104#endif
1105
Paul Bakker508ad5a2011-12-04 17:09:26 +00001106 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker9caf2d22010-02-18 19:37:19 +00001107 ssl_set_dbg( &ssl, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001108
1109 if( opt.nbio == 2 )
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001110 ssl_set_bio_timeout( &ssl, &server_fd, my_send, my_recv, NULL,
1111 opt.read_timeout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001112 else
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001113 ssl_set_bio_timeout( &ssl, &server_fd, net_send, net_recv,
1114#if defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnardf0365122014-09-29 16:11:47 +02001115 opt.nbio == 0 ? net_recv_timeout : NULL,
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001116#else
1117 NULL,
1118#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001119 opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001120
Paul Bakkera503a632013-08-14 13:48:06 +02001121#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001122 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1123 {
1124 printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
1125 goto exit;
1126 }
Paul Bakkera503a632013-08-14 13:48:06 +02001127#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001128
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001129 /* RC4 setting is redundant if we use only one ciphersuite */
Paul Bakker645ce3a2012-10-31 12:32:41 +00001130 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker51936882011-02-20 16:05:58 +00001131 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001132 else
1133 ssl_set_arc4_support( &ssl, opt.arc4 );
Paul Bakker51936882011-02-20 16:05:58 +00001134
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001135 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
1136 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001137#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001138 ssl_set_renegotiation( &ssl, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001139#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001140
Paul Bakker36713e82013-09-17 13:25:29 +02001141#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001142 if( strcmp( opt.ca_path, "none" ) != 0 &&
1143 strcmp( opt.ca_file, "none" ) != 0 )
1144 {
1145 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
1146 }
1147 if( strcmp( opt.crt_file, "none" ) != 0 &&
1148 strcmp( opt.key_file, "none" ) != 0 )
1149 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001150 if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 )
1151 {
1152 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1153 goto exit;
1154 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001155 }
Paul Bakkered27a042013-04-18 22:46:23 +02001156#endif
1157
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001158#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001159 if( ( ret = ssl_set_psk( &ssl, psk, psk_len,
1160 (const unsigned char *) opt.psk_identity,
1161 strlen( opt.psk_identity ) ) ) != 0 )
1162 {
1163 printf( " failed\n ! ssl_set_psk returned %d\n\n", ret );
1164 goto exit;
1165 }
Paul Bakkered27a042013-04-18 22:46:23 +02001166#endif
1167
Paul Bakker0be444a2013-08-27 21:55:01 +02001168#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001169 if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1170 {
1171 printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret );
1172 goto exit;
1173 }
Paul Bakker0be444a2013-08-27 21:55:01 +02001174#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001175
Paul Bakker1d29fb52012-09-28 13:28:45 +00001176 if( opt.min_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001177 {
1178 ret = ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1179 if( ret != 0 )
1180 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001181 printf( " failed\n ! selected min_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001182 goto exit;
1183 }
1184 }
1185
Paul Bakker1d29fb52012-09-28 13:28:45 +00001186 if( opt.max_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001187 {
1188 ret = ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1189 if( ret != 0 )
1190 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001191 printf( " failed\n ! selected max_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001192 goto exit;
1193 }
1194 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001195
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001196#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1197 if( opt.fallback != DFL_FALLBACK )
1198 ssl_set_fallback( &ssl, opt.fallback );
1199#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001200
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001201 printf( " ok\n" );
1202
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 /*
1204 * 4. Handshake
1205 */
1206 printf( " . Performing the SSL/TLS handshake..." );
1207 fflush( stdout );
1208
1209 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1210 {
1211 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker40e46942009-01-03 21:51:57 +00001212 {
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001213 printf( " failed\n ! ssl_handshake returned -0x%x\n", -ret );
1214 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
1215 printf(
1216 " Unable to verify the server's certificate. "
1217 "Either it is invalid,\n"
1218 " or you didn't set ca_file or ca_path "
1219 "to an appropriate value.\n"
1220 " Alternatively, you may want to use "
1221 "auth_mode=optional for testing purposes.\n" );
1222 printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00001224 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001225 }
1226
Manuel Pégourié-Gonnardc580a002014-02-12 10:15:30 +01001227 printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1228 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001229
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001230 if( ( ret = ssl_get_record_expansion( &ssl ) ) >= 0 )
1231 printf( " [ Record expansion is %d ]\n", ret );
1232 else
1233 printf( " [ Record expansion is unknown (compression) ]\n" );
1234
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001235#if defined(POLARSSL_SSL_ALPN)
1236 if( opt.alpn_string != NULL )
1237 {
1238 const char *alp = ssl_get_alpn_protocol( &ssl );
1239 printf( " [ Application Layer Protocol is %s ]\n",
1240 alp ? alp : "(none)" );
1241 }
1242#endif
1243
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001244 if( opt.reconnect != 0 )
1245 {
1246 printf(" . Saving session for reuse..." );
1247 fflush( stdout );
1248
1249 if( ( ret = ssl_get_session( &ssl, &saved_session ) ) != 0 )
1250 {
1251 printf( " failed\n ! ssl_get_session returned -0x%x\n\n", -ret );
1252 goto exit;
1253 }
1254
1255 printf( " ok\n" );
1256 }
1257
Paul Bakker36713e82013-09-17 13:25:29 +02001258#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 /*
1260 * 5. Verify the server certificate
1261 */
1262 printf( " . Verifying peer X.509 certificate..." );
1263
1264 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1265 {
1266 printf( " failed\n" );
1267
1268 if( ( ret & BADCERT_EXPIRED ) != 0 )
1269 printf( " ! server certificate has expired\n" );
1270
1271 if( ( ret & BADCERT_REVOKED ) != 0 )
1272 printf( " ! server certificate has been revoked\n" );
1273
1274 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
1275 printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
1276
1277 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
1278 printf( " ! self-signed or not signed by a trusted CA\n" );
1279
1280 printf( "\n" );
1281 }
1282 else
1283 printf( " ok\n" );
1284
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001285 if( ssl_get_peer_cert( &ssl ) != NULL )
1286 {
1287 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001288 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1289 ssl_get_peer_cert( &ssl ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001290 printf( "%s\n", buf );
1291 }
Paul Bakker36713e82013-09-17 13:25:29 +02001292#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001294#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001295 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001296 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001297 /*
1298 * Perform renegotiation (this must be done when the server is waiting
1299 * for input from our side).
1300 */
1301 printf( " . Performing renegotiation..." );
1302 fflush( stdout );
1303 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001304 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001305 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1306 ret != POLARSSL_ERR_NET_WANT_WRITE )
1307 {
1308 printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
1309 goto exit;
1310 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001311 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001312 printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001313 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001314#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001315
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 /*
1317 * 6. Write the GET request
1318 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001319 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001320send_request:
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 printf( " > Write to server:" );
1322 fflush( stdout );
1323
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001324 len = snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST,
1325 opt.request_page );
1326 tail_len = strlen( GET_REQUEST_END );
1327
1328 /* Add padding to GET request to reach opt.request_size in length */
1329 if( opt.request_size != DFL_REQUEST_SIZE &&
1330 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001331 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001332 memset( buf + len, 'A', opt.request_size - len - tail_len );
1333 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001334 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001336 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof(buf) - len - 1 );
1337 len += tail_len;
1338
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001339 /* Truncate if request size is smaller than the "natural" size */
1340 if( opt.request_size != DFL_REQUEST_SIZE &&
1341 len > opt.request_size )
1342 {
1343 len = opt.request_size;
1344
1345 /* Still end with \r\n unless that's really not possible */
1346 if( len >= 2 ) buf[len - 2] = '\r';
1347 if( len >= 1 ) buf[len - 1] = '\n';
1348 }
1349
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001350 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001351 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001352 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001353 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001354 while( ( ret = ssl_write( &ssl, buf + written, len - written ) )
1355 <= 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001356 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001357 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1358 ret != POLARSSL_ERR_NET_WANT_WRITE )
1359 {
1360 printf( " failed\n ! ssl_write returned -0x%x\n\n", -ret );
1361 goto exit;
1362 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001363 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 }
1365 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001366 else /* Not stream, so datagram */
1367 {
1368 do ret = ssl_write( &ssl, buf, len );
1369 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1370 ret == POLARSSL_ERR_NET_WANT_WRITE );
1371
1372 if( ret < 0 )
1373 {
1374 printf( " failed\n ! ssl_write returned %d\n\n", ret );
1375 goto exit;
1376 }
1377
1378 frags = 1;
1379 written = ret;
1380 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001382 buf[written] = '\0';
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001383 printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
1385 /*
1386 * 7. Read the HTTP response
1387 */
1388 printf( " < Read from server:" );
1389 fflush( stdout );
1390
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001391 /*
1392 * TLS and DTLS need different reading styles (stream vs datagram)
1393 */
1394 if( opt.transport == SSL_TRANSPORT_STREAM )
1395 {
1396 do
1397 {
1398 len = sizeof( buf ) - 1;
1399 memset( buf, 0, sizeof( buf ) );
1400 ret = ssl_read( &ssl, buf, len );
1401
1402 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1403 ret == POLARSSL_ERR_NET_WANT_WRITE )
1404 continue;
1405
1406 if( ret <= 0 )
1407 {
1408 switch( ret )
1409 {
1410 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1411 printf( " connection was closed gracefully\n" );
1412 ret = 0;
1413 goto close_notify;
1414
1415 case 0:
1416 case POLARSSL_ERR_NET_CONN_RESET:
1417 printf( " connection was reset by peer\n" );
1418 ret = 0;
1419 goto reconnect;
1420
1421 default:
1422 printf( " ssl_read returned -0x%x\n", -ret );
1423 goto exit;
1424 }
1425 }
1426
1427 len = ret;
1428 buf[len] = '\0';
1429 printf( " %d bytes read\n\n%s", len, (char *) buf );
1430
1431 /* End of message should be detected according to the syntax of the
1432 * application protocol (eg HTTP), just use a dummy test here. */
1433 if( ret > 0 && buf[len-1] == '\n' )
1434 {
1435 ret = 0;
1436 break;
1437 }
1438 }
1439 while( 1 );
1440 }
1441 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 {
1443 len = sizeof( buf ) - 1;
1444 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001445
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001446 do ret = ssl_read( &ssl, buf, len );
1447 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1448 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001449
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001450 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001451 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001452 switch( ret )
1453 {
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001454 case POLARSSL_ERR_NET_TIMEOUT:
1455 printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001456 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001457 goto send_request;
1458 goto exit;
1459
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001460 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1461 printf( " connection was closed gracefully\n" );
1462 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001463 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00001464
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001465 default:
1466 printf( " ssl_read returned -0x%x\n", -ret );
1467 goto exit;
1468 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001469 }
1470
Paul Bakker5121ce52009-01-03 21:22:43 +00001471 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02001472 buf[len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00001473 printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001474 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001475 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001476
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001477 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001478 * 7b. Continue doing data exchanges?
1479 */
1480 if( --opt.exchanges > 0 )
1481 goto send_request;
1482
1483 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001484 * 8. Done, cleanly close the connection
1485 */
1486close_notify:
1487 printf( " . Closing the connection..." );
1488
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001489 /* No error checking, the connection might be closed already */
1490 do
1491 ret = ssl_close_notify( &ssl );
1492 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
1493 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001494
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001495 printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001496
1497 /*
1498 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001499 */
1500reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001501 if( opt.reconnect != 0 )
1502 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001503 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001504
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001505 net_close( server_fd );
1506
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001507#if defined(POLARSSL_TIMING_C)
1508 if( opt.reco_delay > 0 )
1509 m_sleep( 1000 * opt.reco_delay );
1510#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02001511
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001512 printf( " . Reconnecting with saved session..." );
1513 fflush( stdout );
1514
1515 if( ( ret = ssl_session_reset( &ssl ) ) != 0 )
1516 {
1517 printf( " failed\n ! ssl_session_reset returned -0x%x\n\n", -ret );
1518 goto exit;
1519 }
1520
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001521 if( ( ret = ssl_set_session( &ssl, &saved_session ) ) != 0 )
1522 {
1523 printf( " failed\n ! ssl_set_session returned %d\n\n", ret );
1524 goto exit;
1525 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001526
Manuel Pégourié-Gonnard484b8f92014-09-23 12:36:12 +02001527 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001528 opt.transport == SSL_TRANSPORT_STREAM ?
1529 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001530 {
1531 printf( " failed\n ! net_connect returned -0x%x\n\n", -ret );
1532 goto exit;
1533 }
1534
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001535 if( opt.nbio > 0 )
1536 ret = net_set_nonblock( server_fd );
1537 else
1538 ret = net_set_block( server_fd );
1539 if( ret != 0 )
1540 {
1541 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
1542 -ret );
1543 goto exit;
1544 }
1545
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001546 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1547 {
1548 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1549 ret != POLARSSL_ERR_NET_WANT_WRITE )
1550 {
1551 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
1552 goto exit;
1553 }
1554 }
1555
1556 printf( " ok\n" );
1557
1558 goto send_request;
1559 }
1560
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001561 /*
1562 * Cleanup and exit
1563 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001564exit:
Paul Bakkera3d195c2011-11-27 21:07:34 +00001565#ifdef POLARSSL_ERROR_C
1566 if( ret != 0 )
1567 {
1568 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001569 polarssl_strerror( ret, error_buf, 100 );
Paul Bakker570267f2012-04-10 08:22:46 +00001570 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001571 }
1572#endif
1573
Paul Bakker1a207ec2011-02-06 13:22:40 +00001574 if( server_fd )
1575 net_close( server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001576
Paul Bakker36713e82013-09-17 13:25:29 +02001577#if defined(POLARSSL_X509_CRT_PARSE_C)
1578 x509_crt_free( &clicert );
1579 x509_crt_free( &cacert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02001580 pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001581#endif
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001582 ssl_session_free( &saved_session );
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02001584 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02001585 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00001586
Paul Bakkercce9d772011-11-18 14:26:47 +00001587#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 printf( " + Press Enter to exit this program.\n" );
1589 fflush( stdout ); getchar();
1590#endif
1591
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001592 // Shell can not handle large exit numbers -> 1 for errors
1593 if( ret < 0 )
1594 ret = 1;
1595
Paul Bakker5121ce52009-01-03 21:22:43 +00001596 return( ret );
1597}
Paul Bakker508ad5a2011-12-04 17:09:26 +00001598#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
1599 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
1600 POLARSSL_CTR_DRBG_C */