blob: 0d4a0f209228d2305c0556e62cd61382578573fa [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +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
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020036#if !defined(POLARSSL_ENTROPY_C) || \
37 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
38 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
39#include <stdio.h>
40int main( int argc, char *argv[] )
41{
42 ((void) argc);
43 ((void) argv);
44
Rich Evansf90016a2015-01-19 14:26:37 +000045 polarssl_printf("POLARSSL_ENTROPY_C and/or "
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020046 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
47 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
48 return( 0 );
49}
50#else
51
Paul Bakker5121ce52009-01-03 21:22:43 +000052#include <string.h>
Paul Bakker9caf2d22010-02-18 19:37:19 +000053#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000054#include <stdio.h>
55
Paul Bakker40e46942009-01-03 21:51:57 +000056#include "polarssl/net.h"
57#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000058#include "polarssl/entropy.h"
59#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000060#include "polarssl/certs.h"
61#include "polarssl/x509.h"
Paul Bakkera3d195c2011-11-27 21:07:34 +000062#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020063#include "polarssl/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010065#if defined(POLARSSL_TIMING_C)
66#include "polarssl/timing.h"
67#endif
68
Paul Bakker93c32b22014-04-25 13:40:05 +020069#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
70#if !defined snprintf
71#define snprintf _snprintf
72#endif
73#endif
74
Paul Bakker9caf2d22010-02-18 19:37:19 +000075#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010076#define DFL_SERVER_ADDR NULL
Paul Bakker9caf2d22010-02-18 19:37:19 +000077#define DFL_SERVER_PORT 4433
78#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +020079#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +000080#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010081#define DFL_NBIO 0
Paul Bakker5690efc2011-05-26 13:16:06 +000082#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000083#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000084#define DFL_CRT_FILE ""
85#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020086#define DFL_PSK ""
87#define DFL_PSK_IDENTITY "Client_identity"
Paul Bakker51936882011-02-20 16:05:58 +000088#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +020089#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010090#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010091#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +020092#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +010093#define DFL_MIN_VERSION SSL_MINOR_VERSION_1
Paul Bakker1d29fb52012-09-28 13:28:45 +000094#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +010095#define DFL_ARC4 SSL_ARC4_DISABLED
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +010096#define DFL_AUTH_MODE SSL_VERIFY_REQUIRED
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +020097#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +010098#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +010099#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200100#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100101#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200102#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200103#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200104#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200105#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100106#define DFL_ETM -1
Paul Bakker0e6975b2009-02-10 22:19:10 +0000107
Paul Bakker93c32b22014-04-25 13:40:05 +0200108#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
109#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Paul Bakker9caf2d22010-02-18 19:37:19 +0000111/*
112 * global options
113 */
114struct options
115{
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200116 const char *server_name; /* hostname of the server (client only) */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100117 const char *server_addr; /* address of the server (client only) */
Paul Bakker67968392010-07-18 08:28:20 +0000118 int server_port; /* port on which the ssl service runs */
119 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100120 int nbio; /* should I/O be blocking? */
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é-Gonnard1cbd39d2014-10-20 13:34:59 +0200146 int fallback; /* is this a fallback connection? */
Manuel Pégourié-Gonnard98286562015-01-12 19:17:05 +0100147 int extended_ms; /* negotiate extended master secret? */
Paul Bakker8b9bcec2015-01-13 15:59:55 +0100148 int etm; /* negotiate encrypt then mac? */
Paul Bakker9caf2d22010-02-18 19:37:19 +0000149} opt;
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
Paul Bakker3c5ef712013-06-25 16:37:45 +0200151static void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +0000152{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200153 ((void) level);
154
Rich Evansf90016a2015-01-19 14:26:37 +0000155 polarssl_fprintf( (FILE *) ctx, "%s", str );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200156 fflush( (FILE *) ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157}
158
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100159/*
160 * Test recv/send functions that make sure each try returns
161 * WANT_READ/WANT_WRITE at least once before sucesseding
162 */
163static int my_recv( void *ctx, unsigned char *buf, size_t len )
164{
165 static int first_try = 1;
166 int ret;
167
168 if( first_try )
169 {
170 first_try = 0;
171 return( POLARSSL_ERR_NET_WANT_READ );
172 }
173
174 ret = net_recv( ctx, buf, len );
175 if( ret != POLARSSL_ERR_NET_WANT_READ )
176 first_try = 1; /* Next call will be a new operation */
177 return( ret );
178}
179
180static int my_send( void *ctx, const unsigned char *buf, size_t len )
181{
182 static int first_try = 1;
183 int ret;
184
185 if( first_try )
186 {
187 first_try = 0;
188 return( POLARSSL_ERR_NET_WANT_WRITE );
189 }
190
191 ret = net_send( ctx, buf, len );
192 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
193 first_try = 1; /* Next call will be a new operation */
194 return( ret );
195}
196
Paul Bakker36713e82013-09-17 13:25:29 +0200197#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker915275b2012-09-28 07:10:55 +0000198/*
199 * Enabled if debug_level > 1 in code below
200 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200201static int my_verify( void *data, x509_crt *crt, int depth, int *flags )
Paul Bakker915275b2012-09-28 07:10:55 +0000202{
203 char buf[1024];
204 ((void) data);
205
Rich Evansf90016a2015-01-19 14:26:37 +0000206 polarssl_printf( "\nVerify requested for (Depth %d):\n", depth );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200207 x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Rich Evansf90016a2015-01-19 14:26:37 +0000208 polarssl_printf( "%s", buf );
Paul Bakker915275b2012-09-28 07:10:55 +0000209
210 if( ( (*flags) & BADCERT_EXPIRED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000211 polarssl_printf( " ! server certificate has expired\n" );
Paul Bakker915275b2012-09-28 07:10:55 +0000212
213 if( ( (*flags) & BADCERT_REVOKED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000214 polarssl_printf( " ! server certificate has been revoked\n" );
Paul Bakker915275b2012-09-28 07:10:55 +0000215
216 if( ( (*flags) & BADCERT_CN_MISMATCH ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000217 polarssl_printf( " ! CN mismatch\n" );
Paul Bakker915275b2012-09-28 07:10:55 +0000218
219 if( ( (*flags) & BADCERT_NOT_TRUSTED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000220 polarssl_printf( " ! self-signed or not signed by a trusted CA\n" );
Paul Bakker915275b2012-09-28 07:10:55 +0000221
222 if( ( (*flags) & BADCRL_NOT_TRUSTED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000223 polarssl_printf( " ! CRL not trusted\n" );
Paul Bakker915275b2012-09-28 07:10:55 +0000224
225 if( ( (*flags) & BADCRL_EXPIRED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000226 polarssl_printf( " ! CRL expired\n" );
Paul Bakker915275b2012-09-28 07:10:55 +0000227
228 if( ( (*flags) & BADCERT_OTHER ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000229 polarssl_printf( " ! other (unknown) flag\n" );
Paul Bakker915275b2012-09-28 07:10:55 +0000230
231 if ( ( *flags ) == 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000232 polarssl_printf( " This certificate has no flags\n" );
Paul Bakker915275b2012-09-28 07:10:55 +0000233
234 return( 0 );
235}
Paul Bakker36713e82013-09-17 13:25:29 +0200236#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +0000237
Paul Bakker36713e82013-09-17 13:25:29 +0200238#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5690efc2011-05-26 13:16:06 +0000239#if defined(POLARSSL_FS_IO)
240#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100241 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
242 " default: \"\" (pre-loaded)\n" \
243 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
244 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
245 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
246 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000247 " key_file=%%s default: \"\" (pre-loaded)\n"
248#else
249#define USAGE_IO \
250 " No file operations available (POLARSSL_FS_IO not defined)\n"
251#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200252#else
253#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200254#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200255
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200256#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200257#define USAGE_PSK \
258 " psk=%%s default: \"\" (in hex, without 0x)\n" \
259 " psk_identity=%%s default: \"Client_identity\"\n"
260#else
261#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200262#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000263
Paul Bakkera503a632013-08-14 13:48:06 +0200264#if defined(POLARSSL_SSL_SESSION_TICKETS)
265#define USAGE_TICKETS \
266 " tickets=%%d default: 1 (enabled)\n"
267#else
268#define USAGE_TICKETS ""
269#endif /* POLARSSL_SSL_SESSION_TICKETS */
270
Paul Bakker1f2bc622013-08-15 13:45:55 +0200271#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
272#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100273 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200274#else
275#define USAGE_TRUNC_HMAC ""
276#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
277
Paul Bakker05decb22013-08-15 13:33:48 +0200278#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
279#define USAGE_MAX_FRAG_LEN \
280 " max_frag_len=%%d default: 16384 (tls default)\n" \
281 " options: 512, 1024, 2048, 4096\n"
282#else
283#define USAGE_MAX_FRAG_LEN ""
284#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
285
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100286#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
287#define USAGE_RECSPLIT \
288 " recplit=%%d default: (library default)\n"
289#else
290#define USAGE_RECSPLIT
291#endif
292
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100293#if defined(POLARSSL_TIMING_C)
294#define USAGE_TIME \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200295 " reco_delay=%%d default: 0 seconds\n"
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100296#else
297#define USAGE_TIME ""
298#endif /* POLARSSL_TIMING_C */
299
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200300#if defined(POLARSSL_SSL_ALPN)
301#define USAGE_ALPN \
302 " alpn=%%s default: \"\" (disabled)\n" \
303 " example: spdy/1,http/1.1\n"
304#else
305#define USAGE_ALPN ""
306#endif /* POLARSSL_SSL_ALPN */
307
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200308#if defined(POLARSSL_SSL_FALLBACK_SCSV)
309#define USAGE_FALLBACK \
310 " fallback=0/1 default: (library default: off)\n"
311#else
312#define USAGE_FALLBACK ""
313#endif
314
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200315#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
316#define USAGE_EMS \
317 " extended_ms=0/1 default: (library default: on)\n"
318#else
319#define USAGE_EMS ""
320#endif
321
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100322#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
323#define USAGE_ETM \
324 " etm=0/1 default: (library default: on)\n"
325#else
326#define USAGE_ETM ""
327#endif
328
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100329#if defined(POLARSSL_SSL_RENEGOTIATION)
330#define USAGE_RENEGO \
331 " renegotiation=%%d default: 0 (disabled)\n" \
332 " renegotiate=%%d default: 0 (disabled)\n"
333#else
334#define USAGE_RENEGO ""
335#endif
336
Paul Bakker9caf2d22010-02-18 19:37:19 +0000337#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000338 "\n usage: ssl_client2 param=<>...\n" \
339 "\n acceptable parameters:\n" \
340 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100341 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000342 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000343 " request_page=%%s default: \".\"\n" \
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +0200344 " request_size=%%d default: about 34 (basic request)\n" \
345 " (minimum: 0, max: 16384)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100346 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100347 " nbio=%%d default: 0 (blocking I/O)\n" \
348 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100349 "\n" \
Manuel Pégourié-Gonnard478fac42015-01-19 16:40:56 +0000350 " auth_mode=%%s default: \"required\"\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100351 " options: none, optional, required\n" \
352 USAGE_IO \
353 "\n" \
354 USAGE_PSK \
355 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100356 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100357 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200358 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200359 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100360 USAGE_TIME \
Paul Bakkera503a632013-08-14 13:48:06 +0200361 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100362 USAGE_MAX_FRAG_LEN \
363 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200364 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200365 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200366 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100367 USAGE_ETM \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100368 USAGE_RECSPLIT \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000369 "\n" \
370 " min_version=%%s default: \"\" (ssl3)\n" \
371 " max_version=%%s default: \"\" (tls1_2)\n" \
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100372 " arc4=%%d default: 0 (disabled)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000373 " force_version=%%s default: \"\" (none)\n" \
374 " options: ssl3, tls1, tls1_1, tls1_2\n" \
375 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000376 " force_ciphersuite=<name> default: all enabled\n"\
377 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000378
379int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000380{
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +0200381 int ret = 0, len, tail_len, server_fd, i, written, frags;
Paul Bakker93c32b22014-04-25 13:40:05 +0200382 unsigned char buf[SSL_MAX_CONTENT_LEN + 1];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200383#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200384 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200385 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200386#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200387#if defined(POLARSSL_SSL_ALPN)
388 const char *alpn_list[10];
389#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200390 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000391
392 entropy_context entropy;
393 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 ssl_context ssl;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200395 ssl_session saved_session;
Paul Bakker36713e82013-09-17 13:25:29 +0200396#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200397 x509_crt cacert;
398 x509_crt clicert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200399 pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200400#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000401 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000402 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000403
Paul Bakker1a207ec2011-02-06 13:22:40 +0000404 /*
405 * Make sure memory references are valid.
406 */
407 server_fd = 0;
Paul Bakker1a207ec2011-02-06 13:22:40 +0000408 memset( &ssl, 0, sizeof( ssl_context ) );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200409 memset( &saved_session, 0, sizeof( ssl_session ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200410#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200411 x509_crt_init( &cacert );
412 x509_crt_init( &clicert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200413 pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200414#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200415#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200416 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200417#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000418
Paul Bakker9caf2d22010-02-18 19:37:19 +0000419 if( argc == 0 )
420 {
421 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000422 if( ret == 0 )
423 ret = 1;
424
Rich Evansf90016a2015-01-19 14:26:37 +0000425 polarssl_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000426
427 list = ssl_list_ciphersuites();
428 while( *list )
429 {
Rich Evansf90016a2015-01-19 14:26:37 +0000430 polarssl_printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200431 list++;
432 if( !*list )
433 break;
Rich Evansf90016a2015-01-19 14:26:37 +0000434 polarssl_printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000435 list++;
436 }
Rich Evansf90016a2015-01-19 14:26:37 +0000437 polarssl_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000438 goto exit;
439 }
440
441 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100442 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000443 opt.server_port = DFL_SERVER_PORT;
444 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100445 opt.nbio = DFL_NBIO;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000446 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200447 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000448 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000449 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000450 opt.crt_file = DFL_CRT_FILE;
451 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200452 opt.psk = DFL_PSK;
453 opt.psk_identity = DFL_PSK_IDENTITY;
Paul Bakker51936882011-02-20 16:05:58 +0000454 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000455 opt.renegotiation = DFL_RENEGOTIATION;
456 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100457 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200458 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000459 opt.min_version = DFL_MIN_VERSION;
460 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100461 opt.arc4 = DFL_ARC4;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100462 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200463 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200464 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100465 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200466 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100467 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200468 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200469 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200470 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200471 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100472 opt.etm = DFL_ETM;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000473
474 for( i = 1; i < argc; i++ )
475 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000476 p = argv[i];
477 if( ( q = strchr( p, '=' ) ) == NULL )
478 goto usage;
479 *q++ = '\0';
480
481 if( strcmp( p, "server_name" ) == 0 )
482 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100483 else if( strcmp( p, "server_addr" ) == 0 )
484 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000485 else if( strcmp( p, "server_port" ) == 0 )
486 {
487 opt.server_port = atoi( q );
488 if( opt.server_port < 1 || opt.server_port > 65535 )
489 goto usage;
490 }
491 else if( strcmp( p, "debug_level" ) == 0 )
492 {
493 opt.debug_level = atoi( q );
494 if( opt.debug_level < 0 || opt.debug_level > 65535 )
495 goto usage;
496 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100497 else if( strcmp( p, "nbio" ) == 0 )
498 {
499 opt.nbio = atoi( q );
500 if( opt.nbio < 0 || opt.nbio > 2 )
501 goto usage;
502 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000503 else if( strcmp( p, "request_page" ) == 0 )
504 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200505 else if( strcmp( p, "request_size" ) == 0 )
506 {
507 opt.request_size = atoi( q );
508 if( opt.request_size < 0 || opt.request_size > SSL_MAX_CONTENT_LEN )
509 goto usage;
510 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000511 else if( strcmp( p, "ca_file" ) == 0 )
512 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000513 else if( strcmp( p, "ca_path" ) == 0 )
514 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000515 else if( strcmp( p, "crt_file" ) == 0 )
516 opt.crt_file = q;
517 else if( strcmp( p, "key_file" ) == 0 )
518 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200519 else if( strcmp( p, "psk" ) == 0 )
520 opt.psk = q;
521 else if( strcmp( p, "psk_identity" ) == 0 )
522 opt.psk_identity = q;
Paul Bakker51936882011-02-20 16:05:58 +0000523 else if( strcmp( p, "force_ciphersuite" ) == 0 )
524 {
Paul Bakker51936882011-02-20 16:05:58 +0000525 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
526
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200527 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000528 {
529 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000530 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000531 }
Paul Bakker51936882011-02-20 16:05:58 +0000532 opt.force_ciphersuite[1] = 0;
533 }
Paul Bakker48916f92012-09-16 19:57:18 +0000534 else if( strcmp( p, "renegotiation" ) == 0 )
535 {
536 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
537 SSL_RENEGOTIATION_DISABLED;
538 }
539 else if( strcmp( p, "allow_legacy" ) == 0 )
540 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100541 switch( atoi( q ) )
542 {
543 case -1: opt.allow_legacy = SSL_LEGACY_BREAK_HANDSHAKE; break;
544 case 0: opt.allow_legacy = SSL_LEGACY_NO_RENEGOTIATION; break;
545 case 1: opt.allow_legacy = SSL_LEGACY_ALLOW_RENEGOTIATION; break;
546 default: goto usage;
547 }
Paul Bakker48916f92012-09-16 19:57:18 +0000548 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100549 else if( strcmp( p, "renegotiate" ) == 0 )
550 {
551 opt.renegotiate = atoi( q );
552 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
553 goto usage;
554 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200555 else if( strcmp( p, "exchanges" ) == 0 )
556 {
557 opt.exchanges = atoi( q );
558 if( opt.exchanges < 1 )
559 goto usage;
560 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200561 else if( strcmp( p, "reconnect" ) == 0 )
562 {
563 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200564 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200565 goto usage;
566 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100567 else if( strcmp( p, "reco_delay" ) == 0 )
568 {
569 opt.reco_delay = atoi( q );
570 if( opt.reco_delay < 0 )
571 goto usage;
572 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200573 else if( strcmp( p, "tickets" ) == 0 )
574 {
575 opt.tickets = atoi( q );
576 if( opt.tickets < 0 || opt.tickets > 2 )
577 goto usage;
578 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200579 else if( strcmp( p, "alpn" ) == 0 )
580 {
581 opt.alpn_string = q;
582 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200583 else if( strcmp( p, "fallback" ) == 0 )
584 {
585 switch( atoi( q ) )
586 {
587 case 0: opt.fallback = SSL_IS_NOT_FALLBACK; break;
588 case 1: opt.fallback = SSL_IS_FALLBACK; break;
589 default: goto usage;
590 }
591 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200592 else if( strcmp( p, "extended_ms" ) == 0 )
593 {
594 switch( atoi( q ) )
595 {
596 case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break;
597 case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break;
598 default: goto usage;
599 }
600 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100601 else if( strcmp( p, "etm" ) == 0 )
602 {
603 switch( atoi( q ) )
604 {
605 case 0: opt.etm = SSL_ETM_DISABLED; break;
606 case 1: opt.etm = SSL_ETM_ENABLED; break;
607 default: goto usage;
608 }
609 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000610 else if( strcmp( p, "min_version" ) == 0 )
611 {
612 if( strcmp( q, "ssl3" ) == 0 )
613 opt.min_version = SSL_MINOR_VERSION_0;
614 else if( strcmp( q, "tls1" ) == 0 )
615 opt.min_version = SSL_MINOR_VERSION_1;
616 else if( strcmp( q, "tls1_1" ) == 0 )
617 opt.min_version = SSL_MINOR_VERSION_2;
618 else if( strcmp( q, "tls1_2" ) == 0 )
619 opt.min_version = SSL_MINOR_VERSION_3;
620 else
621 goto usage;
622 }
623 else if( strcmp( p, "max_version" ) == 0 )
624 {
625 if( strcmp( q, "ssl3" ) == 0 )
626 opt.max_version = SSL_MINOR_VERSION_0;
627 else if( strcmp( q, "tls1" ) == 0 )
628 opt.max_version = SSL_MINOR_VERSION_1;
629 else if( strcmp( q, "tls1_1" ) == 0 )
630 opt.max_version = SSL_MINOR_VERSION_2;
631 else if( strcmp( q, "tls1_2" ) == 0 )
632 opt.max_version = SSL_MINOR_VERSION_3;
633 else
634 goto usage;
635 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100636 else if( strcmp( p, "arc4" ) == 0 )
637 {
638 switch( atoi( q ) )
639 {
640 case 0: opt.arc4 = SSL_ARC4_DISABLED; break;
641 case 1: opt.arc4 = SSL_ARC4_ENABLED; break;
642 default: goto usage;
643 }
644 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000645 else if( strcmp( p, "force_version" ) == 0 )
646 {
647 if( strcmp( q, "ssl3" ) == 0 )
648 {
649 opt.min_version = SSL_MINOR_VERSION_0;
650 opt.max_version = SSL_MINOR_VERSION_0;
651 }
652 else if( strcmp( q, "tls1" ) == 0 )
653 {
654 opt.min_version = SSL_MINOR_VERSION_1;
655 opt.max_version = SSL_MINOR_VERSION_1;
656 }
657 else if( strcmp( q, "tls1_1" ) == 0 )
658 {
659 opt.min_version = SSL_MINOR_VERSION_2;
660 opt.max_version = SSL_MINOR_VERSION_2;
661 }
662 else if( strcmp( q, "tls1_2" ) == 0 )
663 {
664 opt.min_version = SSL_MINOR_VERSION_3;
665 opt.max_version = SSL_MINOR_VERSION_3;
666 }
667 else
668 goto usage;
669 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100670 else if( strcmp( p, "auth_mode" ) == 0 )
671 {
672 if( strcmp( q, "none" ) == 0 )
673 opt.auth_mode = SSL_VERIFY_NONE;
674 else if( strcmp( q, "optional" ) == 0 )
675 opt.auth_mode = SSL_VERIFY_OPTIONAL;
676 else if( strcmp( q, "required" ) == 0 )
677 opt.auth_mode = SSL_VERIFY_REQUIRED;
678 else
679 goto usage;
680 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200681 else if( strcmp( p, "max_frag_len" ) == 0 )
682 {
683 if( strcmp( q, "512" ) == 0 )
684 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
685 else if( strcmp( q, "1024" ) == 0 )
686 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
687 else if( strcmp( q, "2048" ) == 0 )
688 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
689 else if( strcmp( q, "4096" ) == 0 )
690 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
691 else
692 goto usage;
693 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200694 else if( strcmp( p, "trunc_hmac" ) == 0 )
695 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100696 switch( atoi( q ) )
697 {
698 case 0: opt.trunc_hmac = SSL_TRUNC_HMAC_DISABLED; break;
699 case 1: opt.trunc_hmac = SSL_TRUNC_HMAC_ENABLED; break;
700 default: goto usage;
701 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200702 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100703 else if( strcmp( p, "recsplit" ) == 0 )
704 {
705 opt.recsplit = atoi( q );
706 if( opt.recsplit < 0 || opt.recsplit > 1 )
707 goto usage;
708 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000709 else
710 goto usage;
711 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000712
Paul Bakkerc73079a2014-04-25 16:34:30 +0200713#if defined(POLARSSL_DEBUG_C)
714 debug_set_threshold( opt.debug_level );
715#endif
716
Paul Bakkerc1516be2013-06-29 16:01:32 +0200717 if( opt.force_ciphersuite[0] > 0 )
718 {
719 const ssl_ciphersuite_t *ciphersuite_info;
720 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
721
Paul Bakker66c48102013-07-26 14:05:32 +0200722 if( opt.max_version != -1 &&
723 ciphersuite_info->min_minor_ver > opt.max_version )
724 {
Rich Evansf90016a2015-01-19 14:26:37 +0000725 polarssl_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakker66c48102013-07-26 14:05:32 +0200726 ret = 2;
727 goto usage;
728 }
729 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200730 ciphersuite_info->max_minor_ver < opt.min_version )
731 {
Rich Evansf90016a2015-01-19 14:26:37 +0000732 polarssl_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +0200733 ret = 2;
734 goto usage;
735 }
Paul Bakker66c48102013-07-26 14:05:32 +0200736 if( opt.max_version > ciphersuite_info->max_minor_ver )
737 opt.max_version = ciphersuite_info->max_minor_ver;
738 if( opt.min_version < ciphersuite_info->min_minor_ver )
739 opt.min_version = ciphersuite_info->min_minor_ver;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200740 }
741
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200742#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000743 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200744 * Unhexify the pre-shared key if any is given
745 */
746 if( strlen( opt.psk ) )
747 {
748 unsigned char c;
749 size_t j;
750
751 if( strlen( opt.psk ) % 2 != 0 )
752 {
Rich Evansf90016a2015-01-19 14:26:37 +0000753 polarssl_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200754 goto exit;
755 }
756
757 psk_len = strlen( opt.psk ) / 2;
758
759 for( j = 0; j < strlen( opt.psk ); j += 2 )
760 {
761 c = opt.psk[j];
762 if( c >= '0' && c <= '9' )
763 c -= '0';
764 else if( c >= 'a' && c <= 'f' )
765 c -= 'a' - 10;
766 else if( c >= 'A' && c <= 'F' )
767 c -= 'A' - 10;
768 else
769 {
Rich Evansf90016a2015-01-19 14:26:37 +0000770 polarssl_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200771 goto exit;
772 }
773 psk[ j / 2 ] = c << 4;
774
775 c = opt.psk[j + 1];
776 if( c >= '0' && c <= '9' )
777 c -= '0';
778 else if( c >= 'a' && c <= 'f' )
779 c -= 'a' - 10;
780 else if( c >= 'A' && c <= 'F' )
781 c -= 'A' - 10;
782 else
783 {
Rich Evansf90016a2015-01-19 14:26:37 +0000784 polarssl_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200785 goto exit;
786 }
787 psk[ j / 2 ] |= c;
788 }
789 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200790#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200791
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200792#if defined(POLARSSL_SSL_ALPN)
793 if( opt.alpn_string != NULL )
794 {
795 p = (char *) opt.alpn_string;
796 i = 0;
797
798 /* Leave room for a final NULL in alpn_list */
799 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
800 {
801 alpn_list[i++] = p;
802
803 /* Terminate the current string and move on to next one */
804 while( *p != ',' && *p != '\0' )
805 p++;
806 if( *p == ',' )
807 *p++ = '\0';
808 }
809 }
810#endif /* POLARSSL_SSL_ALPN */
811
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200812 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000813 * 0. Initialize the RNG and the session data
814 */
Rich Evansf90016a2015-01-19 14:26:37 +0000815 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000816 fflush( stdout );
817
818 entropy_init( &entropy );
819 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200820 (const unsigned char *) pers,
821 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000822 {
Rich Evansf90016a2015-01-19 14:26:37 +0000823 polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000824 goto exit;
825 }
826
Rich Evansf90016a2015-01-19 14:26:37 +0000827 polarssl_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000828
Paul Bakker36713e82013-09-17 13:25:29 +0200829#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000830 /*
831 * 1.1. Load the trusted CA
832 */
Rich Evansf90016a2015-01-19 14:26:37 +0000833 polarssl_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000834 fflush( stdout );
835
Paul Bakker5690efc2011-05-26 13:16:06 +0000836#if defined(POLARSSL_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +0000837 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100838 if( strcmp( opt.ca_path, "none" ) == 0 )
839 ret = 0;
840 else
841 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +0000842 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100843 if( strcmp( opt.ca_file, "none" ) == 0 )
844 ret = 0;
845 else
846 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +0200847 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000848#endif
849#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200850 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
851 strlen( test_ca_list ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000852#else
853 {
854 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000855 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000856 }
857#endif
Paul Bakker42488232012-05-16 08:21:05 +0000858 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000859 {
Rich Evansf90016a2015-01-19 14:26:37 +0000860 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 goto exit;
862 }
863
Rich Evansf90016a2015-01-19 14:26:37 +0000864 polarssl_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000865
866 /*
867 * 1.2. Load own certificate and private key
868 *
869 * (can be skipped if client authentication is not required)
870 */
Rich Evansf90016a2015-01-19 14:26:37 +0000871 polarssl_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000872 fflush( stdout );
873
Paul Bakker5690efc2011-05-26 13:16:06 +0000874#if defined(POLARSSL_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +0000875 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100876 if( strcmp( opt.crt_file, "none" ) == 0 )
877 ret = 0;
878 else
879 ret = x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +0200880 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000881#endif
882#if defined(POLARSSL_CERTS_C)
Paul Bakkerddf26b42013-09-18 13:46:23 +0200883 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000884 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000885#else
886 {
887 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000888 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000889 }
890#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000891 if( ret != 0 )
892 {
Rich Evansf90016a2015-01-19 14:26:37 +0000893 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000894 goto exit;
895 }
896
Paul Bakker5690efc2011-05-26 13:16:06 +0000897#if defined(POLARSSL_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +0000898 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100899 if( strcmp( opt.key_file, "none" ) == 0 )
900 ret = 0;
901 else
902 ret = pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +0000903 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000904#endif
905#if defined(POLARSSL_CERTS_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200906 ret = pk_parse_key( &pkey, (const unsigned char *) test_cli_key,
Paul Bakker67968392010-07-18 08:28:20 +0000907 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000908#else
909 {
910 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000911 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000912 }
913#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000914 if( ret != 0 )
915 {
Rich Evansf90016a2015-01-19 14:26:37 +0000916 polarssl_printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000917 goto exit;
918 }
919
Rich Evansf90016a2015-01-19 14:26:37 +0000920 polarssl_printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +0200921#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000922
923 /*
924 * 2. Start the connection
925 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100926 if( opt.server_addr == NULL)
927 opt.server_addr = opt.server_name;
928
Rich Evansf90016a2015-01-19 14:26:37 +0000929 polarssl_printf( " . Connecting to tcp/%s/%-4d...", opt.server_addr,
Paul Bakker9caf2d22010-02-18 19:37:19 +0000930 opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +0000931 fflush( stdout );
932
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100933 if( ( ret = net_connect( &server_fd, opt.server_addr,
Paul Bakker9caf2d22010-02-18 19:37:19 +0000934 opt.server_port ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000935 {
Rich Evansf90016a2015-01-19 14:26:37 +0000936 polarssl_printf( " failed\n ! net_connect returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000937 goto exit;
938 }
939
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100940 if( opt.nbio > 0 )
941 ret = net_set_nonblock( server_fd );
942 else
943 ret = net_set_block( server_fd );
944 if( ret != 0 )
945 {
Rich Evansf90016a2015-01-19 14:26:37 +0000946 polarssl_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100947 goto exit;
948 }
949
Rich Evansf90016a2015-01-19 14:26:37 +0000950 polarssl_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000951
952 /*
953 * 3. Setup stuff
954 */
Rich Evansf90016a2015-01-19 14:26:37 +0000955 polarssl_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000956 fflush( stdout );
957
Paul Bakker5121ce52009-01-03 21:22:43 +0000958 if( ( ret = ssl_init( &ssl ) ) != 0 )
959 {
Rich Evansf90016a2015-01-19 14:26:37 +0000960 polarssl_printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000961 goto exit;
962 }
963
Rich Evansf90016a2015-01-19 14:26:37 +0000964 polarssl_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
Paul Bakker36713e82013-09-17 13:25:29 +0200966#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker915275b2012-09-28 07:10:55 +0000967 if( opt.debug_level > 0 )
968 ssl_set_verify( &ssl, my_verify, NULL );
Paul Bakkered27a042013-04-18 22:46:23 +0200969#endif
Paul Bakker915275b2012-09-28 07:10:55 +0000970
Paul Bakker5121ce52009-01-03 21:22:43 +0000971 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Paul Bakker91ebfb52012-11-23 14:04:08 +0100972 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +0000973
Paul Bakker05decb22013-08-15 13:33:48 +0200974#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200975 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
976 {
Rich Evansf90016a2015-01-19 14:26:37 +0000977 polarssl_printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200978 goto exit;
979 }
Paul Bakker05decb22013-08-15 13:33:48 +0200980#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200981
Paul Bakker1f2bc622013-08-15 13:45:55 +0200982#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100983 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
984 ssl_set_truncated_hmac( &ssl, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +0200985#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200986
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200987#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
988 if( opt.extended_ms != DFL_EXTENDED_MS )
989 ssl_set_extended_master_secret( &ssl, opt.extended_ms );
990#endif
991
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100992#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
993 if( opt.etm != DFL_ETM )
994 ssl_set_encrypt_then_mac( &ssl, opt.etm );
995#endif
996
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100997#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
998 if( opt.recsplit != DFL_RECSPLIT )
999 ssl_set_cbc_record_splitting( &ssl, opt.recsplit
1000 ? SSL_CBC_RECORD_SPLITTING_ENABLED
1001 : SSL_CBC_RECORD_SPLITTING_DISABLED );
1002#endif
1003
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001004#if defined(POLARSSL_SSL_ALPN)
1005 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001006 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1007 {
Rich Evansf90016a2015-01-19 14:26:37 +00001008 polarssl_printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001009 goto exit;
1010 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001011#endif
1012
Paul Bakker508ad5a2011-12-04 17:09:26 +00001013 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker9caf2d22010-02-18 19:37:19 +00001014 ssl_set_dbg( &ssl, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001015
1016 if( opt.nbio == 2 )
1017 ssl_set_bio( &ssl, my_recv, &server_fd, my_send, &server_fd );
1018 else
1019 ssl_set_bio( &ssl, net_recv, &server_fd, net_send, &server_fd );
Paul Bakker5121ce52009-01-03 21:22:43 +00001020
Paul Bakkera503a632013-08-14 13:48:06 +02001021#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001022 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1023 {
Rich Evansf90016a2015-01-19 14:26:37 +00001024 polarssl_printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001025 goto exit;
1026 }
Paul Bakkera503a632013-08-14 13:48:06 +02001027#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001028
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001029 /* RC4 setting is redundant if we use only one ciphersuite */
Paul Bakker645ce3a2012-10-31 12:32:41 +00001030 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker51936882011-02-20 16:05:58 +00001031 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001032 else
1033 ssl_set_arc4_support( &ssl, opt.arc4 );
Paul Bakker51936882011-02-20 16:05:58 +00001034
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001035 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
1036 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001037#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001038 ssl_set_renegotiation( &ssl, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001039#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001040
Paul Bakker36713e82013-09-17 13:25:29 +02001041#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001042 if( strcmp( opt.ca_path, "none" ) != 0 &&
1043 strcmp( opt.ca_file, "none" ) != 0 )
1044 {
1045 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
1046 }
1047 if( strcmp( opt.crt_file, "none" ) != 0 &&
1048 strcmp( opt.key_file, "none" ) != 0 )
1049 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001050 if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 )
1051 {
Rich Evansf90016a2015-01-19 14:26:37 +00001052 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001053 goto exit;
1054 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001055 }
Paul Bakkered27a042013-04-18 22:46:23 +02001056#endif
1057
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001058#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001059 if( ( ret = ssl_set_psk( &ssl, psk, psk_len,
1060 (const unsigned char *) opt.psk_identity,
1061 strlen( opt.psk_identity ) ) ) != 0 )
1062 {
Rich Evansf90016a2015-01-19 14:26:37 +00001063 polarssl_printf( " failed\n ! ssl_set_psk returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001064 goto exit;
1065 }
Paul Bakkered27a042013-04-18 22:46:23 +02001066#endif
1067
Paul Bakker0be444a2013-08-27 21:55:01 +02001068#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001069 if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1070 {
Rich Evansf90016a2015-01-19 14:26:37 +00001071 polarssl_printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001072 goto exit;
1073 }
Paul Bakker0be444a2013-08-27 21:55:01 +02001074#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001075
Paul Bakker1d29fb52012-09-28 13:28:45 +00001076 if( opt.min_version != -1 )
1077 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1078 if( opt.max_version != -1 )
1079 ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001080#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1081 if( opt.fallback != DFL_FALLBACK )
1082 ssl_set_fallback( &ssl, opt.fallback );
1083#endif
Paul Bakker1d29fb52012-09-28 13:28:45 +00001084
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 /*
1086 * 4. Handshake
1087 */
Rich Evansf90016a2015-01-19 14:26:37 +00001088 polarssl_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001089 fflush( stdout );
1090
1091 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1092 {
Paul Bakker831a7552011-05-18 13:32:51 +00001093 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001094 {
Rich Evansf90016a2015-01-19 14:26:37 +00001095 polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001096 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
Rich Evansf90016a2015-01-19 14:26:37 +00001097 polarssl_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001098 " Unable to verify the server's certificate. "
1099 "Either it is invalid,\n"
1100 " or you didn't set ca_file or ca_path "
1101 "to an appropriate value.\n"
1102 " Alternatively, you may want to use "
1103 "auth_mode=optional for testing purposes.\n" );
Rich Evansf90016a2015-01-19 14:26:37 +00001104 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001105 goto exit;
1106 }
1107 }
1108
Rich Evansf90016a2015-01-19 14:26:37 +00001109 polarssl_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Manuel Pégourié-Gonnardc580a002014-02-12 10:15:30 +01001110 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001111
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001112#if defined(POLARSSL_SSL_ALPN)
1113 if( opt.alpn_string != NULL )
1114 {
1115 const char *alp = ssl_get_alpn_protocol( &ssl );
Rich Evansf90016a2015-01-19 14:26:37 +00001116 polarssl_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001117 alp ? alp : "(none)" );
1118 }
1119#endif
1120
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001121 if( opt.reconnect != 0 )
1122 {
Rich Evansf90016a2015-01-19 14:26:37 +00001123 polarssl_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001124 fflush( stdout );
1125
1126 if( ( ret = ssl_get_session( &ssl, &saved_session ) ) != 0 )
1127 {
Rich Evansf90016a2015-01-19 14:26:37 +00001128 polarssl_printf( " failed\n ! ssl_get_session returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001129 goto exit;
1130 }
1131
Rich Evansf90016a2015-01-19 14:26:37 +00001132 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001133 }
1134
Paul Bakker36713e82013-09-17 13:25:29 +02001135#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 /*
1137 * 5. Verify the server certificate
1138 */
Rich Evansf90016a2015-01-19 14:26:37 +00001139 polarssl_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
1141 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1142 {
Rich Evansf90016a2015-01-19 14:26:37 +00001143 polarssl_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001144
1145 if( ( ret & BADCERT_EXPIRED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001146 polarssl_printf( " ! server certificate has expired\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001147
1148 if( ( ret & BADCERT_REVOKED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001149 polarssl_printf( " ! server certificate has been revoked\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001150
1151 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001152 polarssl_printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
1154 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001155 polarssl_printf( " ! self-signed or not signed by a trusted CA\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Rich Evansf90016a2015-01-19 14:26:37 +00001157 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001158 }
1159 else
Rich Evansf90016a2015-01-19 14:26:37 +00001160 polarssl_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001161
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001162 if( ssl_get_peer_cert( &ssl ) != NULL )
1163 {
Rich Evansf90016a2015-01-19 14:26:37 +00001164 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001165 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1166 ssl_get_peer_cert( &ssl ) );
Rich Evansf90016a2015-01-19 14:26:37 +00001167 polarssl_printf( "%s\n", buf );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001168 }
Paul Bakker36713e82013-09-17 13:25:29 +02001169#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001170
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001171#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001172 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001173 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001174 /*
1175 * Perform renegotiation (this must be done when the server is waiting
1176 * for input from our side).
1177 */
Rich Evansf90016a2015-01-19 14:26:37 +00001178 polarssl_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001179 fflush( stdout );
1180 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001181 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001182 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1183 ret != POLARSSL_ERR_NET_WANT_WRITE )
1184 {
Rich Evansf90016a2015-01-19 14:26:37 +00001185 polarssl_printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001186 goto exit;
1187 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001188 }
Rich Evansf90016a2015-01-19 14:26:37 +00001189 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001190 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001191#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001192
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 /*
1194 * 6. Write the GET request
1195 */
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001196send_request:
Rich Evansf90016a2015-01-19 14:26:37 +00001197 polarssl_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 fflush( stdout );
1199
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001200 len = snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST,
1201 opt.request_page );
1202 tail_len = strlen( GET_REQUEST_END );
1203
1204 /* Add padding to GET request to reach opt.request_size in length */
1205 if( opt.request_size != DFL_REQUEST_SIZE &&
1206 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001207 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001208 memset( buf + len, 'A', opt.request_size - len - tail_len );
1209 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001210 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001211
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001212 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof(buf) - len - 1 );
1213 len += tail_len;
1214
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001215 /* Truncate if request size is smaller than the "natural" size */
1216 if( opt.request_size != DFL_REQUEST_SIZE &&
1217 len > opt.request_size )
1218 {
1219 len = opt.request_size;
1220
1221 /* Still end with \r\n unless that's really not possible */
1222 if( len >= 2 ) buf[len - 2] = '\r';
1223 if( len >= 1 ) buf[len - 1] = '\n';
1224 }
1225
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001226 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001227 {
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001228 while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 {
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001230 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1231 {
Rich Evansf90016a2015-01-19 14:26:37 +00001232 polarssl_printf( " failed\n ! ssl_write returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001233 goto exit;
1234 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001235 }
1236 }
1237
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001238 buf[written] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +00001239 polarssl_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001240
1241 /*
1242 * 7. Read the HTTP response
1243 */
Rich Evansf90016a2015-01-19 14:26:37 +00001244 polarssl_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001245 fflush( stdout );
1246
1247 do
1248 {
1249 len = sizeof( buf ) - 1;
1250 memset( buf, 0, sizeof( buf ) );
1251 ret = ssl_read( &ssl, buf, len );
1252
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001253 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1254 ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001255 continue;
1256
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001257 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001258 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001259 switch( ret )
1260 {
1261 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Rich Evansf90016a2015-01-19 14:26:37 +00001262 polarssl_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001263 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001264 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00001265
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001266 case 0:
1267 case POLARSSL_ERR_NET_CONN_RESET:
Rich Evansf90016a2015-01-19 14:26:37 +00001268 polarssl_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001269 ret = 0;
1270 goto reconnect;
1271
1272 default:
Rich Evansf90016a2015-01-19 14:26:37 +00001273 polarssl_printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001274 goto exit;
1275 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001276 }
1277
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02001279 buf[len] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +00001280 polarssl_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001281
1282 /* End of message should be detected according to the syntax of the
1283 * application protocol (eg HTTP), just use a dummy test here. */
1284 if( ret > 0 && buf[len-1] == '\n' )
1285 {
1286 ret = 0;
1287 break;
1288 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 }
Paul Bakkerf3571312011-05-20 12:32:35 +00001290 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001292 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001293 * 7b. Continue doing data exchanges?
1294 */
1295 if( --opt.exchanges > 0 )
1296 goto send_request;
1297
1298 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001299 * 8. Done, cleanly close the connection
1300 */
1301close_notify:
Rich Evansf90016a2015-01-19 14:26:37 +00001302 polarssl_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001303
Manuel Pégourié-Gonnard34377b12015-01-22 10:46:46 +00001304 /* No error checking, the connection might be closed already */
1305 do ret = ssl_close_notify( &ssl );
1306 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
1307 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001308
Rich Evansf90016a2015-01-19 14:26:37 +00001309 polarssl_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001310
1311 /*
1312 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001313 */
1314reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001315 if( opt.reconnect != 0 )
1316 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001317 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001318
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001319 net_close( server_fd );
1320
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001321#if defined(POLARSSL_TIMING_C)
1322 if( opt.reco_delay > 0 )
1323 m_sleep( 1000 * opt.reco_delay );
1324#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02001325
Rich Evansf90016a2015-01-19 14:26:37 +00001326 polarssl_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001327 fflush( stdout );
1328
1329 if( ( ret = ssl_session_reset( &ssl ) ) != 0 )
1330 {
Rich Evansf90016a2015-01-19 14:26:37 +00001331 polarssl_printf( " failed\n ! ssl_session_reset returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001332 goto exit;
1333 }
1334
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001335 if( ( ret = ssl_set_session( &ssl, &saved_session ) ) != 0 )
1336 {
Rich Evansf90016a2015-01-19 14:26:37 +00001337 polarssl_printf( " failed\n ! ssl_set_session returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001338 goto exit;
1339 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001340
Manuel Pégourié-Gonnardd3b90f72014-11-24 11:54:02 +01001341 if( ( ret = net_connect( &server_fd, opt.server_addr,
1342 opt.server_port ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001343 {
Rich Evansf90016a2015-01-19 14:26:37 +00001344 polarssl_printf( " failed\n ! net_connect returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001345 goto exit;
1346 }
1347
1348 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1349 {
1350 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1351 ret != POLARSSL_ERR_NET_WANT_WRITE )
1352 {
Rich Evansf90016a2015-01-19 14:26:37 +00001353 polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001354 goto exit;
1355 }
1356 }
1357
Rich Evansf90016a2015-01-19 14:26:37 +00001358 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001359
1360 goto send_request;
1361 }
1362
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001363 /*
1364 * Cleanup and exit
1365 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001366exit:
Paul Bakkera3d195c2011-11-27 21:07:34 +00001367#ifdef POLARSSL_ERROR_C
1368 if( ret != 0 )
1369 {
1370 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001371 polarssl_strerror( ret, error_buf, 100 );
Rich Evansf90016a2015-01-19 14:26:37 +00001372 polarssl_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001373 }
1374#endif
1375
Paul Bakker1a207ec2011-02-06 13:22:40 +00001376 if( server_fd )
1377 net_close( server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001378
Paul Bakker36713e82013-09-17 13:25:29 +02001379#if defined(POLARSSL_X509_CRT_PARSE_C)
1380 x509_crt_free( &clicert );
1381 x509_crt_free( &cacert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02001382 pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001383#endif
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001384 ssl_session_free( &saved_session );
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02001386 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02001387 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00001388
Paul Bakkercce9d772011-11-18 14:26:47 +00001389#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +00001390 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001391 fflush( stdout ); getchar();
1392#endif
1393
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001394 // Shell can not handle large exit numbers -> 1 for errors
1395 if( ret < 0 )
1396 ret = 1;
1397
Paul Bakker5121ce52009-01-03 21:22:43 +00001398 return( ret );
1399}
Paul Bakker508ad5a2011-12-04 17:09:26 +00001400#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
1401 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
1402 POLARSSL_CTR_DRBG_C */