blob: 4243ccafb9ef84b08939a7c80b35fbf06ecc8f9f [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
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_fprintf fprintf
Rich Evans18b78c72015-02-11 14:06:19 +000034#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Rich Evans18b78c72015-02-11 14:06:19 +000037#if defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_FS_IO) &&\
38 defined(POLARSSL_SSL_TLS_C) && defined(POLARSSL_SSL_CLI_C) &&\
39 defined(POLARSSL_NET_C) && defined(POLARSSL_CTR_DRBG_C)
Paul Bakker40e46942009-01-03 21:51:57 +000040#include "polarssl/net.h"
41#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000042#include "polarssl/entropy.h"
43#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000044#include "polarssl/certs.h"
45#include "polarssl/x509.h"
Paul Bakkera3d195c2011-11-27 21:07:34 +000046#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020047#include "polarssl/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Rich Evans18b78c72015-02-11 14:06:19 +000049#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#endif
53
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010054#if defined(POLARSSL_TIMING_C)
55#include "polarssl/timing.h"
56#endif
57
Paul Bakker93c32b22014-04-25 13:40:05 +020058#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
59#if !defined snprintf
60#define snprintf _snprintf
61#endif
62#endif
63
Paul Bakker9caf2d22010-02-18 19:37:19 +000064#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010065#define DFL_SERVER_ADDR NULL
Paul Bakker9caf2d22010-02-18 19:37:19 +000066#define DFL_SERVER_PORT 4433
67#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +020068#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +000069#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010070#define DFL_NBIO 0
Paul Bakker5690efc2011-05-26 13:16:06 +000071#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000072#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000073#define DFL_CRT_FILE ""
74#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020075#define DFL_PSK ""
76#define DFL_PSK_IDENTITY "Client_identity"
Paul Bakker51936882011-02-20 16:05:58 +000077#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +020078#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +010079#define DFL_ALLOW_LEGACY -2
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010080#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +020081#define DFL_EXCHANGES 1
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +010082#define DFL_MIN_VERSION SSL_MINOR_VERSION_1
Paul Bakker1d29fb52012-09-28 13:28:45 +000083#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +010084#define DFL_ARC4 SSL_ARC4_DISABLED
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +010085#define DFL_AUTH_MODE SSL_VERIFY_REQUIRED
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +020086#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +010087#define DFL_TRUNC_HMAC -1
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +010088#define DFL_RECSPLIT -1
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +020089#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010090#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +020091#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +020092#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +020093#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +020094#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +010095#define DFL_ETM -1
Paul Bakker0e6975b2009-02-10 22:19:10 +000096
Paul Bakker93c32b22014-04-25 13:40:05 +020097#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
98#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Rich Evans18b78c72015-02-11 14:06:19 +0000100#if !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_FS_IO) || \
101 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
102 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
103int main( int argc, char *argv[] )
104{
105 ((void) argc);
106 ((void) argv);
107
108 polarssl_printf("POLARSSL_ENTROPY_C and/or "
109 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
110 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
111 return( 0 );
112}
113#else
Paul Bakker9caf2d22010-02-18 19:37:19 +0000114/*
115 * global options
116 */
117struct options
118{
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200119 const char *server_name; /* hostname of the server (client only) */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100120 const char *server_addr; /* address of the server (client only) */
Paul Bakker67968392010-07-18 08:28:20 +0000121 int server_port; /* port on which the ssl service runs */
122 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100123 int nbio; /* should I/O be blocking? */
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é-Gonnard1cbd39d2014-10-20 13:34:59 +0200149 int fallback; /* is this a fallback connection? */
Manuel Pégourié-Gonnard98286562015-01-12 19:17:05 +0100150 int extended_ms; /* negotiate extended master secret? */
Paul Bakker8b9bcec2015-01-13 15:59:55 +0100151 int etm; /* negotiate encrypt then mac? */
Paul Bakker9caf2d22010-02-18 19:37:19 +0000152} opt;
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Paul Bakker3c5ef712013-06-25 16:37:45 +0200154static void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +0000155{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200156 ((void) level);
157
Rich Evansf90016a2015-01-19 14:26:37 +0000158 polarssl_fprintf( (FILE *) ctx, "%s", str );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200159 fflush( (FILE *) ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160}
161
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100162/*
163 * Test recv/send functions that make sure each try returns
164 * WANT_READ/WANT_WRITE at least once before sucesseding
165 */
166static int my_recv( void *ctx, unsigned char *buf, size_t len )
167{
168 static int first_try = 1;
169 int ret;
170
171 if( first_try )
172 {
173 first_try = 0;
174 return( POLARSSL_ERR_NET_WANT_READ );
175 }
176
177 ret = net_recv( ctx, buf, len );
178 if( ret != POLARSSL_ERR_NET_WANT_READ )
179 first_try = 1; /* Next call will be a new operation */
180 return( ret );
181}
182
183static int my_send( void *ctx, const unsigned char *buf, size_t len )
184{
185 static int first_try = 1;
186 int ret;
187
188 if( first_try )
189 {
190 first_try = 0;
191 return( POLARSSL_ERR_NET_WANT_WRITE );
192 }
193
194 ret = net_send( ctx, buf, len );
195 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
196 first_try = 1; /* Next call will be a new operation */
197 return( ret );
198}
199
Paul Bakker36713e82013-09-17 13:25:29 +0200200#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker915275b2012-09-28 07:10:55 +0000201/*
202 * Enabled if debug_level > 1 in code below
203 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200204static int my_verify( void *data, x509_crt *crt, int depth, int *flags )
Paul Bakker915275b2012-09-28 07:10:55 +0000205{
206 char buf[1024];
207 ((void) data);
208
Rich Evansf90016a2015-01-19 14:26:37 +0000209 polarssl_printf( "\nVerify requested for (Depth %d):\n", depth );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200210 x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Rich Evansf90016a2015-01-19 14:26:37 +0000211 polarssl_printf( "%s", buf );
Paul Bakker915275b2012-09-28 07:10:55 +0000212
213 if( ( (*flags) & BADCERT_EXPIRED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000214 polarssl_printf( " ! server certificate has expired\n" );
Paul Bakker915275b2012-09-28 07:10:55 +0000215
216 if( ( (*flags) & BADCERT_REVOKED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000217 polarssl_printf( " ! server certificate has been revoked\n" );
Paul Bakker915275b2012-09-28 07:10:55 +0000218
219 if( ( (*flags) & BADCERT_CN_MISMATCH ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000220 polarssl_printf( " ! CN mismatch\n" );
Paul Bakker915275b2012-09-28 07:10:55 +0000221
222 if( ( (*flags) & BADCERT_NOT_TRUSTED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000223 polarssl_printf( " ! self-signed or not signed by a trusted CA\n" );
Paul Bakker915275b2012-09-28 07:10:55 +0000224
225 if( ( (*flags) & BADCRL_NOT_TRUSTED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000226 polarssl_printf( " ! CRL not trusted\n" );
Paul Bakker915275b2012-09-28 07:10:55 +0000227
228 if( ( (*flags) & BADCRL_EXPIRED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000229 polarssl_printf( " ! CRL expired\n" );
Paul Bakker915275b2012-09-28 07:10:55 +0000230
231 if( ( (*flags) & BADCERT_OTHER ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000232 polarssl_printf( " ! other (unknown) flag\n" );
Paul Bakker915275b2012-09-28 07:10:55 +0000233
234 if ( ( *flags ) == 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000235 polarssl_printf( " This certificate has no flags\n" );
Paul Bakker915275b2012-09-28 07:10:55 +0000236
237 return( 0 );
238}
Paul Bakker36713e82013-09-17 13:25:29 +0200239#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +0000240
Paul Bakker36713e82013-09-17 13:25:29 +0200241#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5690efc2011-05-26 13:16:06 +0000242#if defined(POLARSSL_FS_IO)
243#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100244 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
245 " default: \"\" (pre-loaded)\n" \
246 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
247 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
248 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
249 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000250 " key_file=%%s default: \"\" (pre-loaded)\n"
251#else
252#define USAGE_IO \
253 " No file operations available (POLARSSL_FS_IO not defined)\n"
254#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200255#else
256#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200257#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200258
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200259#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200260#define USAGE_PSK \
261 " psk=%%s default: \"\" (in hex, without 0x)\n" \
262 " psk_identity=%%s default: \"Client_identity\"\n"
263#else
264#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200265#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000266
Paul Bakkera503a632013-08-14 13:48:06 +0200267#if defined(POLARSSL_SSL_SESSION_TICKETS)
268#define USAGE_TICKETS \
269 " tickets=%%d default: 1 (enabled)\n"
270#else
271#define USAGE_TICKETS ""
272#endif /* POLARSSL_SSL_SESSION_TICKETS */
273
Paul Bakker1f2bc622013-08-15 13:45:55 +0200274#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
275#define USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100276 " trunc_hmac=%%d default: library default\n"
Paul Bakker1f2bc622013-08-15 13:45:55 +0200277#else
278#define USAGE_TRUNC_HMAC ""
279#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
280
Paul Bakker05decb22013-08-15 13:33:48 +0200281#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
282#define USAGE_MAX_FRAG_LEN \
283 " max_frag_len=%%d default: 16384 (tls default)\n" \
284 " options: 512, 1024, 2048, 4096\n"
285#else
286#define USAGE_MAX_FRAG_LEN ""
287#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
288
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100289#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
290#define USAGE_RECSPLIT \
291 " recplit=%%d default: (library default)\n"
292#else
293#define USAGE_RECSPLIT
294#endif
295
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100296#if defined(POLARSSL_TIMING_C)
297#define USAGE_TIME \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200298 " reco_delay=%%d default: 0 seconds\n"
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100299#else
300#define USAGE_TIME ""
301#endif /* POLARSSL_TIMING_C */
302
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200303#if defined(POLARSSL_SSL_ALPN)
304#define USAGE_ALPN \
305 " alpn=%%s default: \"\" (disabled)\n" \
306 " example: spdy/1,http/1.1\n"
307#else
308#define USAGE_ALPN ""
309#endif /* POLARSSL_SSL_ALPN */
310
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200311#if defined(POLARSSL_SSL_FALLBACK_SCSV)
312#define USAGE_FALLBACK \
313 " fallback=0/1 default: (library default: off)\n"
314#else
315#define USAGE_FALLBACK ""
316#endif
317
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200318#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
319#define USAGE_EMS \
320 " extended_ms=0/1 default: (library default: on)\n"
321#else
322#define USAGE_EMS ""
323#endif
324
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100325#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
326#define USAGE_ETM \
327 " etm=0/1 default: (library default: on)\n"
328#else
329#define USAGE_ETM ""
330#endif
331
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100332#if defined(POLARSSL_SSL_RENEGOTIATION)
333#define USAGE_RENEGO \
334 " renegotiation=%%d default: 0 (disabled)\n" \
335 " renegotiate=%%d default: 0 (disabled)\n"
336#else
337#define USAGE_RENEGO ""
338#endif
339
Paul Bakker9caf2d22010-02-18 19:37:19 +0000340#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000341 "\n usage: ssl_client2 param=<>...\n" \
342 "\n acceptable parameters:\n" \
343 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100344 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000345 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000346 " request_page=%%s default: \".\"\n" \
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +0200347 " request_size=%%d default: about 34 (basic request)\n" \
348 " (minimum: 0, max: 16384)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100349 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100350 " nbio=%%d default: 0 (blocking I/O)\n" \
351 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100352 "\n" \
Manuel Pégourié-Gonnard478fac42015-01-19 16:40:56 +0000353 " auth_mode=%%s default: \"required\"\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100354 " options: none, optional, required\n" \
355 USAGE_IO \
356 "\n" \
357 USAGE_PSK \
358 "\n" \
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100359 " allow_legacy=%%d default: (library default: no)\n" \
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100360 USAGE_RENEGO \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200361 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200362 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100363 USAGE_TIME \
Paul Bakkera503a632013-08-14 13:48:06 +0200364 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100365 USAGE_MAX_FRAG_LEN \
366 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200367 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200368 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200369 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100370 USAGE_ETM \
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100371 USAGE_RECSPLIT \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000372 "\n" \
373 " min_version=%%s default: \"\" (ssl3)\n" \
374 " max_version=%%s default: \"\" (tls1_2)\n" \
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100375 " arc4=%%d default: 0 (disabled)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000376 " force_version=%%s default: \"\" (none)\n" \
377 " options: ssl3, tls1, tls1_1, tls1_2\n" \
378 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000379 " force_ciphersuite=<name> default: all enabled\n"\
380 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000381
382int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000383{
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +0200384 int ret = 0, len, tail_len, server_fd, i, written, frags;
Paul Bakker93c32b22014-04-25 13:40:05 +0200385 unsigned char buf[SSL_MAX_CONTENT_LEN + 1];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200386#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200387 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200388 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200389#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200390#if defined(POLARSSL_SSL_ALPN)
391 const char *alpn_list[10];
392#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200393 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000394
395 entropy_context entropy;
396 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 ssl_context ssl;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200398 ssl_session saved_session;
Paul Bakker36713e82013-09-17 13:25:29 +0200399#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200400 x509_crt cacert;
401 x509_crt clicert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200402 pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200403#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000404 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000405 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000406
Paul Bakker1a207ec2011-02-06 13:22:40 +0000407 /*
408 * Make sure memory references are valid.
409 */
410 server_fd = 0;
Paul Bakker1a207ec2011-02-06 13:22:40 +0000411 memset( &ssl, 0, sizeof( ssl_context ) );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200412 memset( &saved_session, 0, sizeof( ssl_session ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200413#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200414 x509_crt_init( &cacert );
415 x509_crt_init( &clicert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200416 pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200417#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200418#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200419 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200420#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000421
Paul Bakker9caf2d22010-02-18 19:37:19 +0000422 if( argc == 0 )
423 {
424 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000425 if( ret == 0 )
426 ret = 1;
427
Rich Evansf90016a2015-01-19 14:26:37 +0000428 polarssl_printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000429
430 list = ssl_list_ciphersuites();
431 while( *list )
432 {
Rich Evansf90016a2015-01-19 14:26:37 +0000433 polarssl_printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200434 list++;
435 if( !*list )
436 break;
Rich Evansf90016a2015-01-19 14:26:37 +0000437 polarssl_printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000438 list++;
439 }
Rich Evansf90016a2015-01-19 14:26:37 +0000440 polarssl_printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000441 goto exit;
442 }
443
444 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100445 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000446 opt.server_port = DFL_SERVER_PORT;
447 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100448 opt.nbio = DFL_NBIO;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000449 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200450 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000451 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000452 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000453 opt.crt_file = DFL_CRT_FILE;
454 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200455 opt.psk = DFL_PSK;
456 opt.psk_identity = DFL_PSK_IDENTITY;
Paul Bakker51936882011-02-20 16:05:58 +0000457 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000458 opt.renegotiation = DFL_RENEGOTIATION;
459 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100460 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200461 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000462 opt.min_version = DFL_MIN_VERSION;
463 opt.max_version = DFL_MAX_VERSION;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100464 opt.arc4 = DFL_ARC4;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100465 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200466 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200467 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100468 opt.recsplit = DFL_RECSPLIT;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200469 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100470 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200471 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200472 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200473 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200474 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100475 opt.etm = DFL_ETM;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000476
477 for( i = 1; i < argc; i++ )
478 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000479 p = argv[i];
480 if( ( q = strchr( p, '=' ) ) == NULL )
481 goto usage;
482 *q++ = '\0';
483
484 if( strcmp( p, "server_name" ) == 0 )
485 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100486 else if( strcmp( p, "server_addr" ) == 0 )
487 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000488 else if( strcmp( p, "server_port" ) == 0 )
489 {
490 opt.server_port = atoi( q );
491 if( opt.server_port < 1 || opt.server_port > 65535 )
492 goto usage;
493 }
494 else if( strcmp( p, "debug_level" ) == 0 )
495 {
496 opt.debug_level = atoi( q );
497 if( opt.debug_level < 0 || opt.debug_level > 65535 )
498 goto usage;
499 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100500 else if( strcmp( p, "nbio" ) == 0 )
501 {
502 opt.nbio = atoi( q );
503 if( opt.nbio < 0 || opt.nbio > 2 )
504 goto usage;
505 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000506 else if( strcmp( p, "request_page" ) == 0 )
507 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200508 else if( strcmp( p, "request_size" ) == 0 )
509 {
510 opt.request_size = atoi( q );
511 if( opt.request_size < 0 || opt.request_size > SSL_MAX_CONTENT_LEN )
512 goto usage;
513 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000514 else if( strcmp( p, "ca_file" ) == 0 )
515 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000516 else if( strcmp( p, "ca_path" ) == 0 )
517 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000518 else if( strcmp( p, "crt_file" ) == 0 )
519 opt.crt_file = q;
520 else if( strcmp( p, "key_file" ) == 0 )
521 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200522 else if( strcmp( p, "psk" ) == 0 )
523 opt.psk = q;
524 else if( strcmp( p, "psk_identity" ) == 0 )
525 opt.psk_identity = q;
Paul Bakker51936882011-02-20 16:05:58 +0000526 else if( strcmp( p, "force_ciphersuite" ) == 0 )
527 {
Paul Bakker51936882011-02-20 16:05:58 +0000528 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
529
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200530 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000531 {
532 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000533 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000534 }
Paul Bakker51936882011-02-20 16:05:58 +0000535 opt.force_ciphersuite[1] = 0;
536 }
Paul Bakker48916f92012-09-16 19:57:18 +0000537 else if( strcmp( p, "renegotiation" ) == 0 )
538 {
539 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
540 SSL_RENEGOTIATION_DISABLED;
541 }
542 else if( strcmp( p, "allow_legacy" ) == 0 )
543 {
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +0100544 switch( atoi( q ) )
545 {
546 case -1: opt.allow_legacy = SSL_LEGACY_BREAK_HANDSHAKE; break;
547 case 0: opt.allow_legacy = SSL_LEGACY_NO_RENEGOTIATION; break;
548 case 1: opt.allow_legacy = SSL_LEGACY_ALLOW_RENEGOTIATION; break;
549 default: goto usage;
550 }
Paul Bakker48916f92012-09-16 19:57:18 +0000551 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100552 else if( strcmp( p, "renegotiate" ) == 0 )
553 {
554 opt.renegotiate = atoi( q );
555 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
556 goto usage;
557 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200558 else if( strcmp( p, "exchanges" ) == 0 )
559 {
560 opt.exchanges = atoi( q );
561 if( opt.exchanges < 1 )
562 goto usage;
563 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200564 else if( strcmp( p, "reconnect" ) == 0 )
565 {
566 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200567 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200568 goto usage;
569 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100570 else if( strcmp( p, "reco_delay" ) == 0 )
571 {
572 opt.reco_delay = atoi( q );
573 if( opt.reco_delay < 0 )
574 goto usage;
575 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200576 else if( strcmp( p, "tickets" ) == 0 )
577 {
578 opt.tickets = atoi( q );
579 if( opt.tickets < 0 || opt.tickets > 2 )
580 goto usage;
581 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200582 else if( strcmp( p, "alpn" ) == 0 )
583 {
584 opt.alpn_string = q;
585 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200586 else if( strcmp( p, "fallback" ) == 0 )
587 {
588 switch( atoi( q ) )
589 {
590 case 0: opt.fallback = SSL_IS_NOT_FALLBACK; break;
591 case 1: opt.fallback = SSL_IS_FALLBACK; break;
592 default: goto usage;
593 }
594 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200595 else if( strcmp( p, "extended_ms" ) == 0 )
596 {
597 switch( atoi( q ) )
598 {
599 case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break;
600 case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break;
601 default: goto usage;
602 }
603 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100604 else if( strcmp( p, "etm" ) == 0 )
605 {
606 switch( atoi( q ) )
607 {
608 case 0: opt.etm = SSL_ETM_DISABLED; break;
609 case 1: opt.etm = SSL_ETM_ENABLED; break;
610 default: goto usage;
611 }
612 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000613 else if( strcmp( p, "min_version" ) == 0 )
614 {
615 if( strcmp( q, "ssl3" ) == 0 )
616 opt.min_version = SSL_MINOR_VERSION_0;
617 else if( strcmp( q, "tls1" ) == 0 )
618 opt.min_version = SSL_MINOR_VERSION_1;
619 else if( strcmp( q, "tls1_1" ) == 0 )
620 opt.min_version = SSL_MINOR_VERSION_2;
621 else if( strcmp( q, "tls1_2" ) == 0 )
622 opt.min_version = SSL_MINOR_VERSION_3;
623 else
624 goto usage;
625 }
626 else if( strcmp( p, "max_version" ) == 0 )
627 {
628 if( strcmp( q, "ssl3" ) == 0 )
629 opt.max_version = SSL_MINOR_VERSION_0;
630 else if( strcmp( q, "tls1" ) == 0 )
631 opt.max_version = SSL_MINOR_VERSION_1;
632 else if( strcmp( q, "tls1_1" ) == 0 )
633 opt.max_version = SSL_MINOR_VERSION_2;
634 else if( strcmp( q, "tls1_2" ) == 0 )
635 opt.max_version = SSL_MINOR_VERSION_3;
636 else
637 goto usage;
638 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100639 else if( strcmp( p, "arc4" ) == 0 )
640 {
641 switch( atoi( q ) )
642 {
643 case 0: opt.arc4 = SSL_ARC4_DISABLED; break;
644 case 1: opt.arc4 = SSL_ARC4_ENABLED; break;
645 default: goto usage;
646 }
647 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000648 else if( strcmp( p, "force_version" ) == 0 )
649 {
650 if( strcmp( q, "ssl3" ) == 0 )
651 {
652 opt.min_version = SSL_MINOR_VERSION_0;
653 opt.max_version = SSL_MINOR_VERSION_0;
654 }
655 else if( strcmp( q, "tls1" ) == 0 )
656 {
657 opt.min_version = SSL_MINOR_VERSION_1;
658 opt.max_version = SSL_MINOR_VERSION_1;
659 }
660 else if( strcmp( q, "tls1_1" ) == 0 )
661 {
662 opt.min_version = SSL_MINOR_VERSION_2;
663 opt.max_version = SSL_MINOR_VERSION_2;
664 }
665 else if( strcmp( q, "tls1_2" ) == 0 )
666 {
667 opt.min_version = SSL_MINOR_VERSION_3;
668 opt.max_version = SSL_MINOR_VERSION_3;
669 }
670 else
671 goto usage;
672 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100673 else if( strcmp( p, "auth_mode" ) == 0 )
674 {
675 if( strcmp( q, "none" ) == 0 )
676 opt.auth_mode = SSL_VERIFY_NONE;
677 else if( strcmp( q, "optional" ) == 0 )
678 opt.auth_mode = SSL_VERIFY_OPTIONAL;
679 else if( strcmp( q, "required" ) == 0 )
680 opt.auth_mode = SSL_VERIFY_REQUIRED;
681 else
682 goto usage;
683 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200684 else if( strcmp( p, "max_frag_len" ) == 0 )
685 {
686 if( strcmp( q, "512" ) == 0 )
687 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
688 else if( strcmp( q, "1024" ) == 0 )
689 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
690 else if( strcmp( q, "2048" ) == 0 )
691 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
692 else if( strcmp( q, "4096" ) == 0 )
693 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
694 else
695 goto usage;
696 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200697 else if( strcmp( p, "trunc_hmac" ) == 0 )
698 {
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100699 switch( atoi( q ) )
700 {
701 case 0: opt.trunc_hmac = SSL_TRUNC_HMAC_DISABLED; break;
702 case 1: opt.trunc_hmac = SSL_TRUNC_HMAC_ENABLED; break;
703 default: goto usage;
704 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200705 }
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +0100706 else if( strcmp( p, "recsplit" ) == 0 )
707 {
708 opt.recsplit = atoi( q );
709 if( opt.recsplit < 0 || opt.recsplit > 1 )
710 goto usage;
711 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000712 else
713 goto usage;
714 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000715
Paul Bakkerc73079a2014-04-25 16:34:30 +0200716#if defined(POLARSSL_DEBUG_C)
717 debug_set_threshold( opt.debug_level );
718#endif
719
Paul Bakkerc1516be2013-06-29 16:01:32 +0200720 if( opt.force_ciphersuite[0] > 0 )
721 {
722 const ssl_ciphersuite_t *ciphersuite_info;
723 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
724
Paul Bakker66c48102013-07-26 14:05:32 +0200725 if( opt.max_version != -1 &&
726 ciphersuite_info->min_minor_ver > opt.max_version )
727 {
Rich Evansf90016a2015-01-19 14:26:37 +0000728 polarssl_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakker66c48102013-07-26 14:05:32 +0200729 ret = 2;
730 goto usage;
731 }
732 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200733 ciphersuite_info->max_minor_ver < opt.min_version )
734 {
Rich Evansf90016a2015-01-19 14:26:37 +0000735 polarssl_printf("forced ciphersuite not allowed with this protocol version\n");
Paul Bakkerc1516be2013-06-29 16:01:32 +0200736 ret = 2;
737 goto usage;
738 }
Paul Bakker66c48102013-07-26 14:05:32 +0200739 if( opt.max_version > ciphersuite_info->max_minor_ver )
740 opt.max_version = ciphersuite_info->max_minor_ver;
741 if( opt.min_version < ciphersuite_info->min_minor_ver )
742 opt.min_version = ciphersuite_info->min_minor_ver;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200743 }
744
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200745#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000746 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200747 * Unhexify the pre-shared key if any is given
748 */
749 if( strlen( opt.psk ) )
750 {
751 unsigned char c;
752 size_t j;
753
754 if( strlen( opt.psk ) % 2 != 0 )
755 {
Rich Evansf90016a2015-01-19 14:26:37 +0000756 polarssl_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200757 goto exit;
758 }
759
760 psk_len = strlen( opt.psk ) / 2;
761
762 for( j = 0; j < strlen( opt.psk ); j += 2 )
763 {
764 c = opt.psk[j];
765 if( c >= '0' && c <= '9' )
766 c -= '0';
767 else if( c >= 'a' && c <= 'f' )
768 c -= 'a' - 10;
769 else if( c >= 'A' && c <= 'F' )
770 c -= 'A' - 10;
771 else
772 {
Rich Evansf90016a2015-01-19 14:26:37 +0000773 polarssl_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200774 goto exit;
775 }
776 psk[ j / 2 ] = c << 4;
777
778 c = opt.psk[j + 1];
779 if( c >= '0' && c <= '9' )
780 c -= '0';
781 else if( c >= 'a' && c <= 'f' )
782 c -= 'a' - 10;
783 else if( c >= 'A' && c <= 'F' )
784 c -= 'A' - 10;
785 else
786 {
Rich Evansf90016a2015-01-19 14:26:37 +0000787 polarssl_printf("pre-shared key not valid hex\n");
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200788 goto exit;
789 }
790 psk[ j / 2 ] |= c;
791 }
792 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200793#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200794
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200795#if defined(POLARSSL_SSL_ALPN)
796 if( opt.alpn_string != NULL )
797 {
798 p = (char *) opt.alpn_string;
799 i = 0;
800
801 /* Leave room for a final NULL in alpn_list */
802 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
803 {
804 alpn_list[i++] = p;
805
806 /* Terminate the current string and move on to next one */
807 while( *p != ',' && *p != '\0' )
808 p++;
809 if( *p == ',' )
810 *p++ = '\0';
811 }
812 }
813#endif /* POLARSSL_SSL_ALPN */
814
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200815 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000816 * 0. Initialize the RNG and the session data
817 */
Rich Evansf90016a2015-01-19 14:26:37 +0000818 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000819 fflush( stdout );
820
821 entropy_init( &entropy );
822 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200823 (const unsigned char *) pers,
824 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000825 {
Rich Evansf90016a2015-01-19 14:26:37 +0000826 polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000827 goto exit;
828 }
829
Rich Evansf90016a2015-01-19 14:26:37 +0000830 polarssl_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000831
Paul Bakker36713e82013-09-17 13:25:29 +0200832#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000833 /*
834 * 1.1. Load the trusted CA
835 */
Rich Evansf90016a2015-01-19 14:26:37 +0000836 polarssl_printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 fflush( stdout );
838
Paul Bakker5690efc2011-05-26 13:16:06 +0000839#if defined(POLARSSL_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +0000840 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100841 if( strcmp( opt.ca_path, "none" ) == 0 )
842 ret = 0;
843 else
844 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +0000845 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100846 if( strcmp( opt.ca_file, "none" ) == 0 )
847 ret = 0;
848 else
849 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +0200850 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000851#endif
852#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200853 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
854 strlen( test_ca_list ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000855#else
856 {
857 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000858 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000859 }
860#endif
Paul Bakker42488232012-05-16 08:21:05 +0000861 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000862 {
Rich Evansf90016a2015-01-19 14:26:37 +0000863 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000864 goto exit;
865 }
866
Rich Evansf90016a2015-01-19 14:26:37 +0000867 polarssl_printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000868
869 /*
870 * 1.2. Load own certificate and private key
871 *
872 * (can be skipped if client authentication is not required)
873 */
Rich Evansf90016a2015-01-19 14:26:37 +0000874 polarssl_printf( " . Loading the client cert. and key..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000875 fflush( stdout );
876
Paul Bakker5690efc2011-05-26 13:16:06 +0000877#if defined(POLARSSL_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +0000878 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100879 if( strcmp( opt.crt_file, "none" ) == 0 )
880 ret = 0;
881 else
882 ret = x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +0200883 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000884#endif
885#if defined(POLARSSL_CERTS_C)
Paul Bakkerddf26b42013-09-18 13:46:23 +0200886 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000887 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000888#else
889 {
890 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000891 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000892 }
893#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000894 if( ret != 0 )
895 {
Rich Evansf90016a2015-01-19 14:26:37 +0000896 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000897 goto exit;
898 }
899
Paul Bakker5690efc2011-05-26 13:16:06 +0000900#if defined(POLARSSL_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +0000901 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100902 if( strcmp( opt.key_file, "none" ) == 0 )
903 ret = 0;
904 else
905 ret = pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +0000906 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000907#endif
908#if defined(POLARSSL_CERTS_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200909 ret = pk_parse_key( &pkey, (const unsigned char *) test_cli_key,
Paul Bakker67968392010-07-18 08:28:20 +0000910 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000911#else
912 {
913 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000914 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000915 }
916#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000917 if( ret != 0 )
918 {
Rich Evansf90016a2015-01-19 14:26:37 +0000919 polarssl_printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000920 goto exit;
921 }
922
Rich Evansf90016a2015-01-19 14:26:37 +0000923 polarssl_printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +0200924#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000925
926 /*
927 * 2. Start the connection
928 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100929 if( opt.server_addr == NULL)
930 opt.server_addr = opt.server_name;
931
Rich Evansf90016a2015-01-19 14:26:37 +0000932 polarssl_printf( " . Connecting to tcp/%s/%-4d...", opt.server_addr,
Paul Bakker9caf2d22010-02-18 19:37:19 +0000933 opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +0000934 fflush( stdout );
935
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100936 if( ( ret = net_connect( &server_fd, opt.server_addr,
Paul Bakker9caf2d22010-02-18 19:37:19 +0000937 opt.server_port ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000938 {
Rich Evansf90016a2015-01-19 14:26:37 +0000939 polarssl_printf( " failed\n ! net_connect returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000940 goto exit;
941 }
942
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100943 if( opt.nbio > 0 )
944 ret = net_set_nonblock( server_fd );
945 else
946 ret = net_set_block( server_fd );
947 if( ret != 0 )
948 {
Rich Evansf90016a2015-01-19 14:26:37 +0000949 polarssl_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100950 goto exit;
951 }
952
Rich Evansf90016a2015-01-19 14:26:37 +0000953 polarssl_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000954
955 /*
956 * 3. Setup stuff
957 */
Rich Evansf90016a2015-01-19 14:26:37 +0000958 polarssl_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000959 fflush( stdout );
960
Paul Bakker5121ce52009-01-03 21:22:43 +0000961 if( ( ret = ssl_init( &ssl ) ) != 0 )
962 {
Rich Evansf90016a2015-01-19 14:26:37 +0000963 polarssl_printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000964 goto exit;
965 }
966
Rich Evansf90016a2015-01-19 14:26:37 +0000967 polarssl_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000968
Paul Bakker36713e82013-09-17 13:25:29 +0200969#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker915275b2012-09-28 07:10:55 +0000970 if( opt.debug_level > 0 )
971 ssl_set_verify( &ssl, my_verify, NULL );
Paul Bakkered27a042013-04-18 22:46:23 +0200972#endif
Paul Bakker915275b2012-09-28 07:10:55 +0000973
Paul Bakker5121ce52009-01-03 21:22:43 +0000974 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Paul Bakker91ebfb52012-11-23 14:04:08 +0100975 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +0000976
Paul Bakker05decb22013-08-15 13:33:48 +0200977#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200978 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
979 {
Rich Evansf90016a2015-01-19 14:26:37 +0000980 polarssl_printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200981 goto exit;
982 }
Paul Bakker05decb22013-08-15 13:33:48 +0200983#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200984
Paul Bakker1f2bc622013-08-15 13:45:55 +0200985#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard265fe992015-01-09 12:43:35 +0100986 if( opt.trunc_hmac != DFL_TRUNC_HMAC )
987 ssl_set_truncated_hmac( &ssl, opt.trunc_hmac );
Paul Bakker1f2bc622013-08-15 13:45:55 +0200988#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200989
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200990#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
991 if( opt.extended_ms != DFL_EXTENDED_MS )
992 ssl_set_extended_master_secret( &ssl, opt.extended_ms );
993#endif
994
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100995#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
996 if( opt.etm != DFL_ETM )
997 ssl_set_encrypt_then_mac( &ssl, opt.etm );
998#endif
999
Manuel Pégourié-Gonnardc82ee352015-01-07 16:35:25 +01001000#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
1001 if( opt.recsplit != DFL_RECSPLIT )
1002 ssl_set_cbc_record_splitting( &ssl, opt.recsplit
1003 ? SSL_CBC_RECORD_SPLITTING_ENABLED
1004 : SSL_CBC_RECORD_SPLITTING_DISABLED );
1005#endif
1006
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001007#if defined(POLARSSL_SSL_ALPN)
1008 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001009 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1010 {
Rich Evansf90016a2015-01-19 14:26:37 +00001011 polarssl_printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001012 goto exit;
1013 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001014#endif
1015
Paul Bakker508ad5a2011-12-04 17:09:26 +00001016 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker9caf2d22010-02-18 19:37:19 +00001017 ssl_set_dbg( &ssl, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001018
1019 if( opt.nbio == 2 )
1020 ssl_set_bio( &ssl, my_recv, &server_fd, my_send, &server_fd );
1021 else
1022 ssl_set_bio( &ssl, net_recv, &server_fd, net_send, &server_fd );
Paul Bakker5121ce52009-01-03 21:22:43 +00001023
Paul Bakkera503a632013-08-14 13:48:06 +02001024#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001025 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1026 {
Rich Evansf90016a2015-01-19 14:26:37 +00001027 polarssl_printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001028 goto exit;
1029 }
Paul Bakkera503a632013-08-14 13:48:06 +02001030#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001031
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001032 /* RC4 setting is redundant if we use only one ciphersuite */
Paul Bakker645ce3a2012-10-31 12:32:41 +00001033 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker51936882011-02-20 16:05:58 +00001034 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001035 else
1036 ssl_set_arc4_support( &ssl, opt.arc4 );
Paul Bakker51936882011-02-20 16:05:58 +00001037
Manuel Pégourié-Gonnard85d915b2014-11-03 20:10:36 +01001038 if( opt.allow_legacy != DFL_ALLOW_LEGACY )
1039 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001040#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001041 ssl_set_renegotiation( &ssl, opt.renegotiation );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001042#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001043
Paul Bakker36713e82013-09-17 13:25:29 +02001044#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001045 if( strcmp( opt.ca_path, "none" ) != 0 &&
1046 strcmp( opt.ca_file, "none" ) != 0 )
1047 {
1048 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
1049 }
1050 if( strcmp( opt.crt_file, "none" ) != 0 &&
1051 strcmp( opt.key_file, "none" ) != 0 )
1052 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001053 if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 )
1054 {
Rich Evansf90016a2015-01-19 14:26:37 +00001055 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001056 goto exit;
1057 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001058 }
Paul Bakkered27a042013-04-18 22:46:23 +02001059#endif
1060
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001061#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001062 if( ( ret = ssl_set_psk( &ssl, psk, psk_len,
1063 (const unsigned char *) opt.psk_identity,
1064 strlen( opt.psk_identity ) ) ) != 0 )
1065 {
Rich Evansf90016a2015-01-19 14:26:37 +00001066 polarssl_printf( " failed\n ! ssl_set_psk returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001067 goto exit;
1068 }
Paul Bakkered27a042013-04-18 22:46:23 +02001069#endif
1070
Paul Bakker0be444a2013-08-27 21:55:01 +02001071#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001072 if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1073 {
Rich Evansf90016a2015-01-19 14:26:37 +00001074 polarssl_printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001075 goto exit;
1076 }
Paul Bakker0be444a2013-08-27 21:55:01 +02001077#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001078
Paul Bakker1d29fb52012-09-28 13:28:45 +00001079 if( opt.min_version != -1 )
1080 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1081 if( opt.max_version != -1 )
1082 ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001083#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1084 if( opt.fallback != DFL_FALLBACK )
1085 ssl_set_fallback( &ssl, opt.fallback );
1086#endif
Paul Bakker1d29fb52012-09-28 13:28:45 +00001087
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 /*
1089 * 4. Handshake
1090 */
Rich Evansf90016a2015-01-19 14:26:37 +00001091 polarssl_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001092 fflush( stdout );
1093
1094 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1095 {
Paul Bakker831a7552011-05-18 13:32:51 +00001096 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001097 {
Rich Evansf90016a2015-01-19 14:26:37 +00001098 polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n", -ret );
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001099 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
Rich Evansf90016a2015-01-19 14:26:37 +00001100 polarssl_printf(
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001101 " Unable to verify the server's certificate. "
1102 "Either it is invalid,\n"
1103 " or you didn't set ca_file or ca_path "
1104 "to an appropriate value.\n"
1105 " Alternatively, you may want to use "
1106 "auth_mode=optional for testing purposes.\n" );
Rich Evansf90016a2015-01-19 14:26:37 +00001107 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001108 goto exit;
1109 }
1110 }
1111
Rich Evansf90016a2015-01-19 14:26:37 +00001112 polarssl_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
Manuel Pégourié-Gonnardc580a002014-02-12 10:15:30 +01001113 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001114
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001115#if defined(POLARSSL_SSL_ALPN)
1116 if( opt.alpn_string != NULL )
1117 {
1118 const char *alp = ssl_get_alpn_protocol( &ssl );
Rich Evansf90016a2015-01-19 14:26:37 +00001119 polarssl_printf( " [ Application Layer Protocol is %s ]\n",
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001120 alp ? alp : "(none)" );
1121 }
1122#endif
1123
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001124 if( opt.reconnect != 0 )
1125 {
Rich Evansf90016a2015-01-19 14:26:37 +00001126 polarssl_printf(" . Saving session for reuse..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001127 fflush( stdout );
1128
1129 if( ( ret = ssl_get_session( &ssl, &saved_session ) ) != 0 )
1130 {
Rich Evansf90016a2015-01-19 14:26:37 +00001131 polarssl_printf( " failed\n ! ssl_get_session returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001132 goto exit;
1133 }
1134
Rich Evansf90016a2015-01-19 14:26:37 +00001135 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001136 }
1137
Paul Bakker36713e82013-09-17 13:25:29 +02001138#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001139 /*
1140 * 5. Verify the server certificate
1141 */
Rich Evansf90016a2015-01-19 14:26:37 +00001142 polarssl_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +00001143
1144 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1145 {
Rich Evansf90016a2015-01-19 14:26:37 +00001146 polarssl_printf( " failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001147
1148 if( ( ret & BADCERT_EXPIRED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001149 polarssl_printf( " ! server certificate has expired\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001150
1151 if( ( ret & BADCERT_REVOKED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001152 polarssl_printf( " ! server certificate has been revoked\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
1154 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001155 polarssl_printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
1157 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +00001158 polarssl_printf( " ! self-signed or not signed by a trusted CA\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001159
Rich Evansf90016a2015-01-19 14:26:37 +00001160 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 }
1162 else
Rich Evansf90016a2015-01-19 14:26:37 +00001163 polarssl_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001164
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001165 if( ssl_get_peer_cert( &ssl ) != NULL )
1166 {
Rich Evansf90016a2015-01-19 14:26:37 +00001167 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001168 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1169 ssl_get_peer_cert( &ssl ) );
Rich Evansf90016a2015-01-19 14:26:37 +00001170 polarssl_printf( "%s\n", buf );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001171 }
Paul Bakker36713e82013-09-17 13:25:29 +02001172#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001173
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001174#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001175 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001176 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001177 /*
1178 * Perform renegotiation (this must be done when the server is waiting
1179 * for input from our side).
1180 */
Rich Evansf90016a2015-01-19 14:26:37 +00001181 polarssl_printf( " . Performing renegotiation..." );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001182 fflush( stdout );
1183 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001184 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001185 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1186 ret != POLARSSL_ERR_NET_WANT_WRITE )
1187 {
Rich Evansf90016a2015-01-19 14:26:37 +00001188 polarssl_printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001189 goto exit;
1190 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001191 }
Rich Evansf90016a2015-01-19 14:26:37 +00001192 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001193 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001194#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001195
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 /*
1197 * 6. Write the GET request
1198 */
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001199send_request:
Rich Evansf90016a2015-01-19 14:26:37 +00001200 polarssl_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 fflush( stdout );
1202
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001203 len = snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST,
1204 opt.request_page );
1205 tail_len = strlen( GET_REQUEST_END );
1206
1207 /* Add padding to GET request to reach opt.request_size in length */
1208 if( opt.request_size != DFL_REQUEST_SIZE &&
1209 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001210 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001211 memset( buf + len, 'A', opt.request_size - len - tail_len );
1212 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001213 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001214
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001215 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof(buf) - len - 1 );
1216 len += tail_len;
1217
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001218 /* Truncate if request size is smaller than the "natural" size */
1219 if( opt.request_size != DFL_REQUEST_SIZE &&
1220 len > opt.request_size )
1221 {
1222 len = opt.request_size;
1223
1224 /* Still end with \r\n unless that's really not possible */
1225 if( len >= 2 ) buf[len - 2] = '\r';
1226 if( len >= 1 ) buf[len - 1] = '\n';
1227 }
1228
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001229 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 {
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001231 while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 {
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001233 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1234 {
Rich Evansf90016a2015-01-19 14:26:37 +00001235 polarssl_printf( " failed\n ! ssl_write returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001236 goto exit;
1237 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 }
1239 }
1240
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001241 buf[written] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +00001242 polarssl_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
1244 /*
1245 * 7. Read the HTTP response
1246 */
Rich Evansf90016a2015-01-19 14:26:37 +00001247 polarssl_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001248 fflush( stdout );
1249
1250 do
1251 {
1252 len = sizeof( buf ) - 1;
1253 memset( buf, 0, sizeof( buf ) );
1254 ret = ssl_read( &ssl, buf, len );
1255
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001256 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1257 ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001258 continue;
1259
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001260 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001262 switch( ret )
1263 {
1264 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Rich Evansf90016a2015-01-19 14:26:37 +00001265 polarssl_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001266 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001267 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00001268
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001269 case 0:
1270 case POLARSSL_ERR_NET_CONN_RESET:
Rich Evansf90016a2015-01-19 14:26:37 +00001271 polarssl_printf( " connection was reset by peer\n" );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001272 ret = 0;
1273 goto reconnect;
1274
1275 default:
Rich Evansf90016a2015-01-19 14:26:37 +00001276 polarssl_printf( " ssl_read returned -0x%x\n", -ret );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001277 goto exit;
1278 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001279 }
1280
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02001282 buf[len] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +00001283 polarssl_printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001284
1285 /* End of message should be detected according to the syntax of the
1286 * application protocol (eg HTTP), just use a dummy test here. */
1287 if( ret > 0 && buf[len-1] == '\n' )
1288 {
1289 ret = 0;
1290 break;
1291 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001292 }
Paul Bakkerf3571312011-05-20 12:32:35 +00001293 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001294
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001295 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001296 * 7b. Continue doing data exchanges?
1297 */
1298 if( --opt.exchanges > 0 )
1299 goto send_request;
1300
1301 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001302 * 8. Done, cleanly close the connection
1303 */
1304close_notify:
Rich Evansf90016a2015-01-19 14:26:37 +00001305 polarssl_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001306
Manuel Pégourié-Gonnard34377b12015-01-22 10:46:46 +00001307 /* No error checking, the connection might be closed already */
1308 do ret = ssl_close_notify( &ssl );
1309 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
1310 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001311
Rich Evansf90016a2015-01-19 14:26:37 +00001312 polarssl_printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001313
1314 /*
1315 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001316 */
1317reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001318 if( opt.reconnect != 0 )
1319 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001320 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001321
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001322 net_close( server_fd );
1323
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001324#if defined(POLARSSL_TIMING_C)
1325 if( opt.reco_delay > 0 )
1326 m_sleep( 1000 * opt.reco_delay );
1327#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02001328
Rich Evansf90016a2015-01-19 14:26:37 +00001329 polarssl_printf( " . Reconnecting with saved session..." );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001330 fflush( stdout );
1331
1332 if( ( ret = ssl_session_reset( &ssl ) ) != 0 )
1333 {
Rich Evansf90016a2015-01-19 14:26:37 +00001334 polarssl_printf( " failed\n ! ssl_session_reset returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001335 goto exit;
1336 }
1337
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001338 if( ( ret = ssl_set_session( &ssl, &saved_session ) ) != 0 )
1339 {
Rich Evansf90016a2015-01-19 14:26:37 +00001340 polarssl_printf( " failed\n ! ssl_set_session returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001341 goto exit;
1342 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001343
Manuel Pégourié-Gonnardd3b90f72014-11-24 11:54:02 +01001344 if( ( ret = net_connect( &server_fd, opt.server_addr,
1345 opt.server_port ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001346 {
Rich Evansf90016a2015-01-19 14:26:37 +00001347 polarssl_printf( " failed\n ! net_connect returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001348 goto exit;
1349 }
1350
1351 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1352 {
1353 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1354 ret != POLARSSL_ERR_NET_WANT_WRITE )
1355 {
Rich Evansf90016a2015-01-19 14:26:37 +00001356 polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001357 goto exit;
1358 }
1359 }
1360
Rich Evansf90016a2015-01-19 14:26:37 +00001361 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001362
1363 goto send_request;
1364 }
1365
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001366 /*
1367 * Cleanup and exit
1368 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001369exit:
Paul Bakkera3d195c2011-11-27 21:07:34 +00001370#ifdef POLARSSL_ERROR_C
1371 if( ret != 0 )
1372 {
1373 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001374 polarssl_strerror( ret, error_buf, 100 );
Rich Evansf90016a2015-01-19 14:26:37 +00001375 polarssl_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001376 }
1377#endif
1378
Paul Bakker1a207ec2011-02-06 13:22:40 +00001379 if( server_fd )
1380 net_close( server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001381
Paul Bakker36713e82013-09-17 13:25:29 +02001382#if defined(POLARSSL_X509_CRT_PARSE_C)
1383 x509_crt_free( &clicert );
1384 x509_crt_free( &cacert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02001385 pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001386#endif
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001387 ssl_session_free( &saved_session );
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02001389 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02001390 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00001391
Paul Bakkercce9d772011-11-18 14:26:47 +00001392#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +00001393 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 fflush( stdout ); getchar();
1395#endif
1396
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001397 // Shell can not handle large exit numbers -> 1 for errors
1398 if( ret < 0 )
1399 ret = 1;
1400
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 return( ret );
1402}
Paul Bakker508ad5a2011-12-04 17:09:26 +00001403#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
1404 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
1405 POLARSSL_CTR_DRBG_C */