blob: 54953c7a2cc2edf045a3c29425b499a300bc060a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client with certificate authentication
3 *
Paul Bakker3c5ef712013-06-25 16:37:45 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Manuel Pégourié-Gonnard8a4d5712014-06-24 14:19:59 +020032#if !defined(POLARSSL_ENTROPY_C) || \
33 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
34 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
35#include <stdio.h>
36int main( int argc, char *argv[] )
37{
38 ((void) argc);
39 ((void) argv);
40
41 printf("POLARSSL_ENTROPY_C and/or "
42 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
43 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
44 return( 0 );
45}
46#else
47
Paul Bakker5121ce52009-01-03 21:22:43 +000048#include <string.h>
Paul Bakker9caf2d22010-02-18 19:37:19 +000049#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000050#include <stdio.h>
51
Paul Bakker40e46942009-01-03 21:51:57 +000052#include "polarssl/net.h"
53#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000054#include "polarssl/entropy.h"
55#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000056#include "polarssl/certs.h"
57#include "polarssl/x509.h"
Paul Bakkera3d195c2011-11-27 21:07:34 +000058#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020059#include "polarssl/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010061#if defined(POLARSSL_TIMING_C)
62#include "polarssl/timing.h"
63#endif
64
Paul Bakker93c32b22014-04-25 13:40:05 +020065#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
66#if !defined snprintf
67#define snprintf _snprintf
68#endif
69#endif
70
Paul Bakker9caf2d22010-02-18 19:37:19 +000071#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +010072#define DFL_SERVER_ADDR NULL
Paul Bakker9caf2d22010-02-18 19:37:19 +000073#define DFL_SERVER_PORT 4433
74#define DFL_REQUEST_PAGE "/"
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +020075#define DFL_REQUEST_SIZE -1
Paul Bakker9caf2d22010-02-18 19:37:19 +000076#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010077#define DFL_NBIO 0
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020078#define DFL_READ_TIMEOUT 0
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +020079#define DFL_MAX_RESEND 0
Paul Bakker5690efc2011-05-26 13:16:06 +000080#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000081#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000082#define DFL_CRT_FILE ""
83#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020084#define DFL_PSK ""
85#define DFL_PSK_IDENTITY "Client_identity"
Paul Bakker51936882011-02-20 16:05:58 +000086#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +020087#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000088#define DFL_ALLOW_LEGACY SSL_LEGACY_NO_RENEGOTIATION
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010089#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +020090#define DFL_EXCHANGES 1
Paul Bakker1d29fb52012-09-28 13:28:45 +000091#define DFL_MIN_VERSION -1
92#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +010093#define DFL_AUTH_MODE SSL_VERIFY_REQUIRED
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +020094#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +020095#define DFL_TRUNC_HMAC 0
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +020096#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010097#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +020098#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +020099#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100100#define DFL_TRANSPORT SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200101#define DFL_HS_TO_MIN 0
102#define DFL_HS_TO_MAX 0
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200103#define DFL_FALLBACK -1
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200104#define DFL_EXTENDED_MS -1
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100105#define DFL_ETM -1
Paul Bakker0e6975b2009-02-10 22:19:10 +0000106
Paul Bakker93c32b22014-04-25 13:40:05 +0200107#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
108#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Paul Bakker9caf2d22010-02-18 19:37:19 +0000110/*
111 * global options
112 */
113struct options
114{
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200115 const char *server_name; /* hostname of the server (client only) */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100116 const char *server_addr; /* address of the server (client only) */
Paul Bakker67968392010-07-18 08:28:20 +0000117 int server_port; /* port on which the ssl service runs */
118 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100119 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200120 uint32_t read_timeout; /* timeout on ssl_read() in milliseconds */
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200121 int max_resend; /* DTLS times to resend on read timeout */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200122 const char *request_page; /* page on server to request */
Paul Bakker93c32b22014-04-25 13:40:05 +0200123 int request_size; /* pad request with header to requested size */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200124 const char *ca_file; /* the file with the CA certificate(s) */
125 const char *ca_path; /* the path with the CA certificate(s) reside */
126 const char *crt_file; /* the file with the client certificate */
127 const char *key_file; /* the file with the client key */
128 const char *psk; /* the pre-shared key */
129 const char *psk_identity; /* the pre-shared key identity */
Paul Bakker51936882011-02-20 16:05:58 +0000130 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Paul Bakker48916f92012-09-16 19:57:18 +0000131 int renegotiation; /* enable / disable renegotiation */
132 int allow_legacy; /* allow legacy renegotiation */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100133 int renegotiate; /* attempt renegotiation? */
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200134 int renego_delay; /* delay before enforcing renegotiation */
135 int exchanges; /* number of data exchanges */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000136 int min_version; /* minimum protocol version accepted */
137 int max_version; /* maximum protocol version accepted */
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é-Gonnardaaa1eab2013-07-30 13:43:43 +0200141 int reconnect; /* attempt to resume session */
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100142 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200143 int tickets; /* enable / disable session tickets */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200144 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100145 int transport; /* TLS or DTLS? */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200146 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
147 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200148 int fallback; /* is this a fallback connection? */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200149 char extended_ms; /* negotiate extended master secret? */
Manuel Pégourié-Gonnardbe6ce832014-11-17 14:29:36 +0100150 char etm; /* negotiate encrypt then mac? ? */
Paul Bakker9caf2d22010-02-18 19:37:19 +0000151} opt;
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Paul Bakker3c5ef712013-06-25 16:37:45 +0200153static void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +0000154{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200155 ((void) level);
156
157 fprintf( (FILE *) ctx, "%s", str );
158 fflush( (FILE *) ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159}
160
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100161/*
162 * Test recv/send functions that make sure each try returns
163 * WANT_READ/WANT_WRITE at least once before sucesseding
164 */
165static int my_recv( void *ctx, unsigned char *buf, size_t len )
166{
167 static int first_try = 1;
168 int ret;
169
170 if( first_try )
171 {
172 first_try = 0;
173 return( POLARSSL_ERR_NET_WANT_READ );
174 }
175
176 ret = net_recv( ctx, buf, len );
177 if( ret != POLARSSL_ERR_NET_WANT_READ )
178 first_try = 1; /* Next call will be a new operation */
179 return( ret );
180}
181
182static int my_send( void *ctx, const unsigned char *buf, size_t len )
183{
184 static int first_try = 1;
185 int ret;
186
187 if( first_try )
188 {
189 first_try = 0;
190 return( POLARSSL_ERR_NET_WANT_WRITE );
191 }
192
193 ret = net_send( ctx, buf, len );
194 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
195 first_try = 1; /* Next call will be a new operation */
196 return( ret );
197}
198
Paul Bakker36713e82013-09-17 13:25:29 +0200199#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker915275b2012-09-28 07:10:55 +0000200/*
201 * Enabled if debug_level > 1 in code below
202 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200203static int my_verify( void *data, x509_crt *crt, int depth, int *flags )
Paul Bakker915275b2012-09-28 07:10:55 +0000204{
205 char buf[1024];
206 ((void) data);
207
208 printf( "\nVerify requested for (Depth %d):\n", depth );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200209 x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Paul Bakker915275b2012-09-28 07:10:55 +0000210 printf( "%s", buf );
211
212 if( ( (*flags) & BADCERT_EXPIRED ) != 0 )
213 printf( " ! server certificate has expired\n" );
214
215 if( ( (*flags) & BADCERT_REVOKED ) != 0 )
216 printf( " ! server certificate has been revoked\n" );
217
218 if( ( (*flags) & BADCERT_CN_MISMATCH ) != 0 )
219 printf( " ! CN mismatch\n" );
220
221 if( ( (*flags) & BADCERT_NOT_TRUSTED ) != 0 )
222 printf( " ! self-signed or not signed by a trusted CA\n" );
223
224 if( ( (*flags) & BADCRL_NOT_TRUSTED ) != 0 )
225 printf( " ! CRL not trusted\n" );
226
227 if( ( (*flags) & BADCRL_EXPIRED ) != 0 )
228 printf( " ! CRL expired\n" );
229
230 if( ( (*flags) & BADCERT_OTHER ) != 0 )
231 printf( " ! other (unknown) flag\n" );
232
233 if ( ( *flags ) == 0 )
234 printf( " This certificate has no flags\n" );
235
236 return( 0 );
237}
Paul Bakker36713e82013-09-17 13:25:29 +0200238#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +0000239
Paul Bakker36713e82013-09-17 13:25:29 +0200240#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5690efc2011-05-26 13:16:06 +0000241#if defined(POLARSSL_FS_IO)
242#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100243 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
244 " default: \"\" (pre-loaded)\n" \
245 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
246 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
247 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
248 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000249 " key_file=%%s default: \"\" (pre-loaded)\n"
250#else
251#define USAGE_IO \
252 " No file operations available (POLARSSL_FS_IO not defined)\n"
253#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200254#else
255#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200256#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200257
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200258#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200259#define USAGE_PSK \
260 " psk=%%s default: \"\" (in hex, without 0x)\n" \
261 " psk_identity=%%s default: \"Client_identity\"\n"
262#else
263#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200264#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000265
Paul Bakkera503a632013-08-14 13:48:06 +0200266#if defined(POLARSSL_SSL_SESSION_TICKETS)
267#define USAGE_TICKETS \
268 " tickets=%%d default: 1 (enabled)\n"
269#else
270#define USAGE_TICKETS ""
271#endif /* POLARSSL_SSL_SESSION_TICKETS */
272
Paul Bakker1f2bc622013-08-15 13:45:55 +0200273#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
274#define USAGE_TRUNC_HMAC \
275 " trunc_hmac=%%d default: 0 (disabled)\n"
276#else
277#define USAGE_TRUNC_HMAC ""
278#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
279
Paul Bakker05decb22013-08-15 13:33:48 +0200280#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
281#define USAGE_MAX_FRAG_LEN \
282 " max_frag_len=%%d default: 16384 (tls default)\n" \
283 " options: 512, 1024, 2048, 4096\n"
284#else
285#define USAGE_MAX_FRAG_LEN ""
286#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
287
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100288#if defined(POLARSSL_TIMING_C)
289#define USAGE_TIME \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200290 " reco_delay=%%d default: 0 seconds\n"
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100291#else
292#define USAGE_TIME ""
293#endif /* POLARSSL_TIMING_C */
294
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200295#if defined(POLARSSL_SSL_ALPN)
296#define USAGE_ALPN \
297 " alpn=%%s default: \"\" (disabled)\n" \
298 " example: spdy/1,http/1.1\n"
299#else
300#define USAGE_ALPN ""
301#endif /* POLARSSL_SSL_ALPN */
302
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200303#if defined(POLARSSL_SSL_PROTO_DTLS)
304#define USAGE_DTLS \
305 " dtls=%%d default: 0 (TLS)\n" \
306 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
307 " range of DTLS handshake timeouts in millisecs\n"
308#else
309#define USAGE_DTLS ""
310#endif
311
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200312#if defined(POLARSSL_SSL_FALLBACK_SCSV)
313#define USAGE_FALLBACK \
314 " fallback=0/1 default: (library default: off)\n"
315#else
316#define USAGE_FALLBACK ""
317#endif
318
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200319#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
320#define USAGE_EMS \
321 " extended_ms=0/1 default: (library default: on)\n"
322#else
323#define USAGE_EMS ""
324#endif
325
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100326#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
327#define USAGE_ETM \
328 " etm=0/1 default: (library default: on)\n"
329#else
330#define USAGE_ETM ""
331#endif
332
Paul Bakker9caf2d22010-02-18 19:37:19 +0000333#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000334 "\n usage: ssl_client2 param=<>...\n" \
335 "\n acceptable parameters:\n" \
336 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100337 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000338 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000339 " request_page=%%s default: \".\"\n" \
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +0200340 " request_size=%%d default: about 34 (basic request)\n" \
341 " (minimum: 0, max: 16384)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100342 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100343 " nbio=%%d default: 0 (blocking I/O)\n" \
344 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200345 " read_timeout=%%d default: 0 (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200346 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100347 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200348 USAGE_DTLS \
349 "\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100350 " auth_mode=%%s default: \"optional\"\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" \
Paul Bakker48916f92012-09-16 19:57:18 +0000356 " renegotiation=%%d default: 1 (enabled)\n" \
357 " allow_legacy=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100358 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200359 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200360 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100361 USAGE_TIME \
Paul Bakkera503a632013-08-14 13:48:06 +0200362 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100363 USAGE_MAX_FRAG_LEN \
364 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200365 USAGE_ALPN \
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200366 USAGE_FALLBACK \
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200367 USAGE_EMS \
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100368 USAGE_ETM \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000369 "\n" \
370 " min_version=%%s default: \"\" (ssl3)\n" \
371 " max_version=%%s default: \"\" (tls1_2)\n" \
372 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100373 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100374 " auth_mode=%%s default: \"required\"\n" \
Paul Bakker91ebfb52012-11-23 14:04:08 +0100375 " options: none, optional, required\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000376 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000377 " force_ciphersuite=<name> default: all enabled\n"\
378 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000379
380int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000381{
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200382 int ret = 0, len, tail_len, server_fd, i, written, frags, retry_left;
Paul Bakker93c32b22014-04-25 13:40:05 +0200383 unsigned char buf[SSL_MAX_CONTENT_LEN + 1];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200384#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200385 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200386 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200387#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200388#if defined(POLARSSL_SSL_ALPN)
389 const char *alpn_list[10];
390#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200391 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000392
393 entropy_context entropy;
394 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 ssl_context ssl;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200396 ssl_session saved_session;
Paul Bakker36713e82013-09-17 13:25:29 +0200397#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200398 x509_crt cacert;
399 x509_crt clicert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200400 pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200401#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000402 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000403 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000404
Paul Bakker1a207ec2011-02-06 13:22:40 +0000405 /*
406 * Make sure memory references are valid.
407 */
408 server_fd = 0;
Paul Bakker1a207ec2011-02-06 13:22:40 +0000409 memset( &ssl, 0, sizeof( ssl_context ) );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200410 memset( &saved_session, 0, sizeof( ssl_session ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200411#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200412 x509_crt_init( &cacert );
413 x509_crt_init( &clicert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200414 pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200415#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200416#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200417 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200418#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000419
Paul Bakker9caf2d22010-02-18 19:37:19 +0000420 if( argc == 0 )
421 {
422 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000423 if( ret == 0 )
424 ret = 1;
425
Paul Bakker9caf2d22010-02-18 19:37:19 +0000426 printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000427
428 list = ssl_list_ciphersuites();
429 while( *list )
430 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200431 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200432 list++;
433 if( !*list )
434 break;
435 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000436 list++;
437 }
438 printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000439 goto exit;
440 }
441
442 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100443 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000444 opt.server_port = DFL_SERVER_PORT;
445 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100446 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200447 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200448 opt.max_resend = DFL_MAX_RESEND;
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;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100464 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200465 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200466 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200467 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100468 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200469 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200470 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200471 opt.transport = DFL_TRANSPORT;
472 opt.hs_to_min = DFL_HS_TO_MIN;
473 opt.hs_to_max = DFL_HS_TO_MAX;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200474 opt.fallback = DFL_FALLBACK;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200475 opt.extended_ms = DFL_EXTENDED_MS;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100476 opt.etm = DFL_ETM;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000477
478 for( i = 1; i < argc; i++ )
479 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000480 p = argv[i];
481 if( ( q = strchr( p, '=' ) ) == NULL )
482 goto usage;
483 *q++ = '\0';
484
485 if( strcmp( p, "server_name" ) == 0 )
486 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100487 else if( strcmp( p, "server_addr" ) == 0 )
488 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000489 else if( strcmp( p, "server_port" ) == 0 )
490 {
491 opt.server_port = atoi( q );
492 if( opt.server_port < 1 || opt.server_port > 65535 )
493 goto usage;
494 }
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100495 else if( strcmp( p, "dtls" ) == 0 )
496 {
497 int t = atoi( q );
498 if( t == 0 )
499 opt.transport = SSL_TRANSPORT_STREAM;
500 else if( t == 1 )
501 opt.transport = SSL_TRANSPORT_DATAGRAM;
502 else
503 goto usage;
504 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000505 else if( strcmp( p, "debug_level" ) == 0 )
506 {
507 opt.debug_level = atoi( q );
508 if( opt.debug_level < 0 || opt.debug_level > 65535 )
509 goto usage;
510 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100511 else if( strcmp( p, "nbio" ) == 0 )
512 {
513 opt.nbio = atoi( q );
514 if( opt.nbio < 0 || opt.nbio > 2 )
515 goto usage;
516 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200517 else if( strcmp( p, "read_timeout" ) == 0 )
518 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200519 else if( strcmp( p, "max_resend" ) == 0 )
520 {
521 opt.max_resend = atoi( q );
522 if( opt.max_resend < 0 )
523 goto usage;
524 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000525 else if( strcmp( p, "request_page" ) == 0 )
526 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200527 else if( strcmp( p, "request_size" ) == 0 )
528 {
529 opt.request_size = atoi( q );
530 if( opt.request_size < 0 || opt.request_size > SSL_MAX_CONTENT_LEN )
531 goto usage;
532 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000533 else if( strcmp( p, "ca_file" ) == 0 )
534 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000535 else if( strcmp( p, "ca_path" ) == 0 )
536 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000537 else if( strcmp( p, "crt_file" ) == 0 )
538 opt.crt_file = q;
539 else if( strcmp( p, "key_file" ) == 0 )
540 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200541 else if( strcmp( p, "psk" ) == 0 )
542 opt.psk = q;
543 else if( strcmp( p, "psk_identity" ) == 0 )
544 opt.psk_identity = q;
Paul Bakker51936882011-02-20 16:05:58 +0000545 else if( strcmp( p, "force_ciphersuite" ) == 0 )
546 {
Paul Bakker51936882011-02-20 16:05:58 +0000547 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
548
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200549 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000550 {
551 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000552 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000553 }
Paul Bakker51936882011-02-20 16:05:58 +0000554 opt.force_ciphersuite[1] = 0;
555 }
Paul Bakker48916f92012-09-16 19:57:18 +0000556 else if( strcmp( p, "renegotiation" ) == 0 )
557 {
558 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
559 SSL_RENEGOTIATION_DISABLED;
560 }
561 else if( strcmp( p, "allow_legacy" ) == 0 )
562 {
563 opt.allow_legacy = atoi( q );
564 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
565 goto usage;
566 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100567 else if( strcmp( p, "renegotiate" ) == 0 )
568 {
569 opt.renegotiate = atoi( q );
570 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
571 goto usage;
572 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200573 else if( strcmp( p, "exchanges" ) == 0 )
574 {
575 opt.exchanges = atoi( q );
576 if( opt.exchanges < 1 )
577 goto usage;
578 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200579 else if( strcmp( p, "reconnect" ) == 0 )
580 {
581 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200582 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200583 goto usage;
584 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100585 else if( strcmp( p, "reco_delay" ) == 0 )
586 {
587 opt.reco_delay = atoi( q );
588 if( opt.reco_delay < 0 )
589 goto usage;
590 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200591 else if( strcmp( p, "tickets" ) == 0 )
592 {
593 opt.tickets = atoi( q );
594 if( opt.tickets < 0 || opt.tickets > 2 )
595 goto usage;
596 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200597 else if( strcmp( p, "alpn" ) == 0 )
598 {
599 opt.alpn_string = q;
600 }
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200601 else if( strcmp( p, "fallback" ) == 0 )
602 {
603 switch( atoi( q ) )
604 {
605 case 0: opt.fallback = SSL_IS_NOT_FALLBACK; break;
606 case 1: opt.fallback = SSL_IS_FALLBACK; break;
607 default: goto usage;
608 }
609 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200610 else if( strcmp( p, "extended_ms" ) == 0 )
611 {
612 switch( atoi( q ) )
613 {
614 case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break;
615 case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break;
616 default: goto usage;
617 }
618 }
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100619 else if( strcmp( p, "etm" ) == 0 )
620 {
621 switch( atoi( q ) )
622 {
623 case 0: opt.etm = SSL_ETM_DISABLED; break;
624 case 1: opt.etm = SSL_ETM_ENABLED; break;
625 default: goto usage;
626 }
627 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000628 else if( strcmp( p, "min_version" ) == 0 )
629 {
630 if( strcmp( q, "ssl3" ) == 0 )
631 opt.min_version = SSL_MINOR_VERSION_0;
632 else if( strcmp( q, "tls1" ) == 0 )
633 opt.min_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100634 else if( strcmp( q, "tls1_1" ) == 0 ||
635 strcmp( q, "dtls1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000636 opt.min_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100637 else if( strcmp( q, "tls1_2" ) == 0 ||
638 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000639 opt.min_version = SSL_MINOR_VERSION_3;
640 else
641 goto usage;
642 }
643 else if( strcmp( p, "max_version" ) == 0 )
644 {
645 if( strcmp( q, "ssl3" ) == 0 )
646 opt.max_version = SSL_MINOR_VERSION_0;
647 else if( strcmp( q, "tls1" ) == 0 )
648 opt.max_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100649 else if( strcmp( q, "tls1_1" ) == 0 ||
650 strcmp( q, "dtls1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000651 opt.max_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100652 else if( strcmp( q, "tls1_2" ) == 0 ||
653 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000654 opt.max_version = SSL_MINOR_VERSION_3;
655 else
656 goto usage;
657 }
658 else if( strcmp( p, "force_version" ) == 0 )
659 {
660 if( strcmp( q, "ssl3" ) == 0 )
661 {
662 opt.min_version = SSL_MINOR_VERSION_0;
663 opt.max_version = SSL_MINOR_VERSION_0;
664 }
665 else if( strcmp( q, "tls1" ) == 0 )
666 {
667 opt.min_version = SSL_MINOR_VERSION_1;
668 opt.max_version = SSL_MINOR_VERSION_1;
669 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100670 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000671 {
672 opt.min_version = SSL_MINOR_VERSION_2;
673 opt.max_version = SSL_MINOR_VERSION_2;
674 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100675 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000676 {
677 opt.min_version = SSL_MINOR_VERSION_3;
678 opt.max_version = SSL_MINOR_VERSION_3;
679 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100680 else if( strcmp( q, "dtls1" ) == 0 )
681 {
682 opt.min_version = SSL_MINOR_VERSION_2;
683 opt.max_version = SSL_MINOR_VERSION_2;
684 opt.transport = SSL_TRANSPORT_DATAGRAM;
685 }
686 else if( strcmp( q, "dtls1_2" ) == 0 )
687 {
688 opt.min_version = SSL_MINOR_VERSION_3;
689 opt.max_version = SSL_MINOR_VERSION_3;
690 opt.transport = SSL_TRANSPORT_DATAGRAM;
691 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000692 else
693 goto usage;
694 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100695 else if( strcmp( p, "auth_mode" ) == 0 )
696 {
697 if( strcmp( q, "none" ) == 0 )
698 opt.auth_mode = SSL_VERIFY_NONE;
699 else if( strcmp( q, "optional" ) == 0 )
700 opt.auth_mode = SSL_VERIFY_OPTIONAL;
701 else if( strcmp( q, "required" ) == 0 )
702 opt.auth_mode = SSL_VERIFY_REQUIRED;
703 else
704 goto usage;
705 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200706 else if( strcmp( p, "max_frag_len" ) == 0 )
707 {
708 if( strcmp( q, "512" ) == 0 )
709 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
710 else if( strcmp( q, "1024" ) == 0 )
711 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
712 else if( strcmp( q, "2048" ) == 0 )
713 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
714 else if( strcmp( q, "4096" ) == 0 )
715 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
716 else
717 goto usage;
718 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200719 else if( strcmp( p, "trunc_hmac" ) == 0 )
720 {
721 opt.trunc_hmac = atoi( q );
722 if( opt.trunc_hmac < 0 || opt.trunc_hmac > 1 )
723 goto usage;
724 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200725 else if( strcmp( p, "hs_timeout" ) == 0 )
726 {
727 if( ( p = strchr( q, '-' ) ) == NULL )
728 goto usage;
729 *p++ = '\0';
730 opt.hs_to_min = atoi( q );
731 opt.hs_to_max = atoi( p );
732 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
733 goto usage;
734 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000735 else
736 goto usage;
737 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000738
Paul Bakkerc73079a2014-04-25 16:34:30 +0200739#if defined(POLARSSL_DEBUG_C)
740 debug_set_threshold( opt.debug_level );
741#endif
742
Paul Bakkerc1516be2013-06-29 16:01:32 +0200743 if( opt.force_ciphersuite[0] > 0 )
744 {
745 const ssl_ciphersuite_t *ciphersuite_info;
746 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
747
Paul Bakker66c48102013-07-26 14:05:32 +0200748 if( opt.max_version != -1 &&
749 ciphersuite_info->min_minor_ver > opt.max_version )
750 {
751 printf("forced ciphersuite not allowed with this protocol version\n");
752 ret = 2;
753 goto usage;
754 }
755 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200756 ciphersuite_info->max_minor_ver < opt.min_version )
757 {
758 printf("forced ciphersuite not allowed with this protocol version\n");
759 ret = 2;
760 goto usage;
761 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100762
763 /* If the server selects a version that's not supported by
764 * this suite, then there will be no common ciphersuite... */
765 if( opt.max_version == -1 ||
766 opt.max_version > ciphersuite_info->max_minor_ver )
767 {
Paul Bakker66c48102013-07-26 14:05:32 +0200768 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100769 }
Paul Bakker66c48102013-07-26 14:05:32 +0200770 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100771 {
Paul Bakker66c48102013-07-26 14:05:32 +0200772 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100773 /* DTLS starts with TLS 1.1 */
774 if( opt.transport == SSL_TRANSPORT_DATAGRAM &&
775 opt.min_version < SSL_MINOR_VERSION_2 )
776 opt.min_version = SSL_MINOR_VERSION_2;
777 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200778 }
779
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200780#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000781 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200782 * Unhexify the pre-shared key if any is given
783 */
784 if( strlen( opt.psk ) )
785 {
786 unsigned char c;
787 size_t j;
788
789 if( strlen( opt.psk ) % 2 != 0 )
790 {
791 printf("pre-shared key not valid hex\n");
792 goto exit;
793 }
794
795 psk_len = strlen( opt.psk ) / 2;
796
797 for( j = 0; j < strlen( opt.psk ); j += 2 )
798 {
799 c = opt.psk[j];
800 if( c >= '0' && c <= '9' )
801 c -= '0';
802 else if( c >= 'a' && c <= 'f' )
803 c -= 'a' - 10;
804 else if( c >= 'A' && c <= 'F' )
805 c -= 'A' - 10;
806 else
807 {
808 printf("pre-shared key not valid hex\n");
809 goto exit;
810 }
811 psk[ j / 2 ] = c << 4;
812
813 c = opt.psk[j + 1];
814 if( c >= '0' && c <= '9' )
815 c -= '0';
816 else if( c >= 'a' && c <= 'f' )
817 c -= 'a' - 10;
818 else if( c >= 'A' && c <= 'F' )
819 c -= 'A' - 10;
820 else
821 {
822 printf("pre-shared key not valid hex\n");
823 goto exit;
824 }
825 psk[ j / 2 ] |= c;
826 }
827 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200828#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200829
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200830#if defined(POLARSSL_SSL_ALPN)
831 if( opt.alpn_string != NULL )
832 {
833 p = (char *) opt.alpn_string;
834 i = 0;
835
836 /* Leave room for a final NULL in alpn_list */
837 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
838 {
839 alpn_list[i++] = p;
840
841 /* Terminate the current string and move on to next one */
842 while( *p != ',' && *p != '\0' )
843 p++;
844 if( *p == ',' )
845 *p++ = '\0';
846 }
847 }
848#endif /* POLARSSL_SSL_ALPN */
849
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200850 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000851 * 0. Initialize the RNG and the session data
852 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000853 printf( "\n . Seeding the random number generator..." );
854 fflush( stdout );
855
856 entropy_init( &entropy );
857 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200858 (const unsigned char *) pers,
859 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000860 {
Paul Bakker0b22e3e2012-04-18 14:23:29 +0000861 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000862 goto exit;
863 }
864
865 printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000866
Paul Bakker36713e82013-09-17 13:25:29 +0200867#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000868 /*
869 * 1.1. Load the trusted CA
870 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000871 printf( " . Loading the CA root certificate ..." );
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 Bakker8d914582012-06-04 12:46:42 +0000875 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100876 if( strcmp( opt.ca_path, "none" ) == 0 )
877 ret = 0;
878 else
879 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +0000880 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100881 if( strcmp( opt.ca_file, "none" ) == 0 )
882 ret = 0;
883 else
884 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +0200885 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000886#endif
887#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200888 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
889 strlen( test_ca_list ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000890#else
891 {
892 ret = 1;
893 printf("POLARSSL_CERTS_C not defined.");
894 }
895#endif
Paul Bakker42488232012-05-16 08:21:05 +0000896 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000897 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200898 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000899 goto exit;
900 }
901
Paul Bakker42488232012-05-16 08:21:05 +0000902 printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000903
904 /*
905 * 1.2. Load own certificate and private key
906 *
907 * (can be skipped if client authentication is not required)
908 */
909 printf( " . Loading the client cert. and key..." );
910 fflush( stdout );
911
Paul Bakker5690efc2011-05-26 13:16:06 +0000912#if defined(POLARSSL_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +0000913 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100914 if( strcmp( opt.crt_file, "none" ) == 0 )
915 ret = 0;
916 else
917 ret = x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +0200918 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000919#endif
920#if defined(POLARSSL_CERTS_C)
Paul Bakkerddf26b42013-09-18 13:46:23 +0200921 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000922 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000923#else
924 {
925 ret = 1;
926 printf("POLARSSL_CERTS_C not defined.");
927 }
928#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 if( ret != 0 )
930 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200931 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000932 goto exit;
933 }
934
Paul Bakker5690efc2011-05-26 13:16:06 +0000935#if defined(POLARSSL_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +0000936 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100937 if( strcmp( opt.key_file, "none" ) == 0 )
938 ret = 0;
939 else
940 ret = pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +0000941 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000942#endif
943#if defined(POLARSSL_CERTS_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200944 ret = pk_parse_key( &pkey, (const unsigned char *) test_cli_key,
Paul Bakker67968392010-07-18 08:28:20 +0000945 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000946#else
947 {
948 ret = 1;
949 printf("POLARSSL_CERTS_C not defined.");
950 }
951#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000952 if( ret != 0 )
953 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200954 printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 goto exit;
956 }
957
958 printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +0200959#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
961 /*
962 * 2. Start the connection
963 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100964 if( opt.server_addr == NULL)
965 opt.server_addr = opt.server_name;
966
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +0100967 printf( " . Connecting to %s/%s/%-4d...",
968 opt.transport == SSL_TRANSPORT_STREAM ? "tcp" : "udp",
969 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +0000970 fflush( stdout );
971
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +0100972 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
973 opt.transport == SSL_TRANSPORT_STREAM ?
974 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000975 {
Paul Bakker0b22e3e2012-04-18 14:23:29 +0000976 printf( " failed\n ! net_connect returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000977 goto exit;
978 }
979
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100980 if( opt.nbio > 0 )
981 ret = net_set_nonblock( server_fd );
982 else
983 ret = net_set_block( server_fd );
984 if( ret != 0 )
985 {
986 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
987 goto exit;
988 }
989
Paul Bakker5121ce52009-01-03 21:22:43 +0000990 printf( " ok\n" );
991
992 /*
993 * 3. Setup stuff
994 */
995 printf( " . Setting up the SSL/TLS structure..." );
996 fflush( stdout );
997
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 if( ( ret = ssl_init( &ssl ) ) != 0 )
999 {
Paul Bakker0b22e3e2012-04-18 14:23:29 +00001000 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001001 goto exit;
1002 }
1003
Paul Bakker36713e82013-09-17 13:25:29 +02001004#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker915275b2012-09-28 07:10:55 +00001005 if( opt.debug_level > 0 )
1006 ssl_set_verify( &ssl, my_verify, NULL );
Paul Bakkered27a042013-04-18 22:46:23 +02001007#endif
Paul Bakker915275b2012-09-28 07:10:55 +00001008
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001010 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +00001011
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001012#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001013 if( ( ret = ssl_set_transport( &ssl, opt.transport ) ) != 0 )
1014 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001015 printf( " failed\n ! selected transport is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001016 goto exit;
1017 }
1018
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +02001019 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
1020 ssl_set_handshake_timeout( &ssl, opt.hs_to_min, opt.hs_to_max );
1021#endif /* POLARSSL_SSL_PROTO_DTLS */
1022
Paul Bakker05decb22013-08-15 13:33:48 +02001023#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001024 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1025 {
1026 printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
1027 goto exit;
1028 }
Paul Bakker05decb22013-08-15 13:33:48 +02001029#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +02001030
Paul Bakker1f2bc622013-08-15 13:45:55 +02001031#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001032 if( opt.trunc_hmac != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001033 if( ( ret = ssl_set_truncated_hmac( &ssl, SSL_TRUNC_HMAC_ENABLED ) ) != 0 )
1034 {
1035 printf( " failed\n ! ssl_set_truncated_hmac returned %d\n\n", ret );
1036 goto exit;
1037 }
Paul Bakker1f2bc622013-08-15 13:45:55 +02001038#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02001039
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001040#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1041 if( opt.extended_ms != DFL_EXTENDED_MS )
1042 ssl_set_extended_master_secret( &ssl, opt.extended_ms );
1043#endif
1044
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001045#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1046 if( opt.etm != DFL_ETM )
1047 ssl_set_encrypt_then_mac( &ssl, opt.etm );
1048#endif
1049
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001050#if defined(POLARSSL_SSL_ALPN)
1051 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001052 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1053 {
1054 printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
1055 goto exit;
1056 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001057#endif
1058
Paul Bakker508ad5a2011-12-04 17:09:26 +00001059 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker9caf2d22010-02-18 19:37:19 +00001060 ssl_set_dbg( &ssl, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001061
1062 if( opt.nbio == 2 )
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001063 ssl_set_bio_timeout( &ssl, &server_fd, my_send, my_recv, NULL,
1064 opt.read_timeout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001065 else
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001066 ssl_set_bio_timeout( &ssl, &server_fd, net_send, net_recv,
1067#if defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnardf0365122014-09-29 16:11:47 +02001068 opt.nbio == 0 ? net_recv_timeout : NULL,
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +02001069#else
1070 NULL,
1071#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001072 opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001073
Paul Bakkera503a632013-08-14 13:48:06 +02001074#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001075 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1076 {
1077 printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
1078 goto exit;
1079 }
Paul Bakkera503a632013-08-14 13:48:06 +02001080#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001081
Paul Bakker645ce3a2012-10-31 12:32:41 +00001082 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker51936882011-02-20 16:05:58 +00001083 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
1084
Paul Bakker48916f92012-09-16 19:57:18 +00001085 ssl_set_renegotiation( &ssl, opt.renegotiation );
1086 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
1087
Paul Bakker36713e82013-09-17 13:25:29 +02001088#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001089 if( strcmp( opt.ca_path, "none" ) != 0 &&
1090 strcmp( opt.ca_file, "none" ) != 0 )
1091 {
1092 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
1093 }
1094 if( strcmp( opt.crt_file, "none" ) != 0 &&
1095 strcmp( opt.key_file, "none" ) != 0 )
1096 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001097 if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 )
1098 {
1099 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1100 goto exit;
1101 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001102 }
Paul Bakkered27a042013-04-18 22:46:23 +02001103#endif
1104
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001105#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001106 if( ( ret = ssl_set_psk( &ssl, psk, psk_len,
1107 (const unsigned char *) opt.psk_identity,
1108 strlen( opt.psk_identity ) ) ) != 0 )
1109 {
1110 printf( " failed\n ! ssl_set_psk returned %d\n\n", ret );
1111 goto exit;
1112 }
Paul Bakkered27a042013-04-18 22:46:23 +02001113#endif
1114
Paul Bakker0be444a2013-08-27 21:55:01 +02001115#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001116 if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1117 {
1118 printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret );
1119 goto exit;
1120 }
Paul Bakker0be444a2013-08-27 21:55:01 +02001121#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001122
Paul Bakker1d29fb52012-09-28 13:28:45 +00001123 if( opt.min_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001124 {
1125 ret = ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1126 if( ret != 0 )
1127 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001128 printf( " failed\n ! selected min_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001129 goto exit;
1130 }
1131 }
1132
Paul Bakker1d29fb52012-09-28 13:28:45 +00001133 if( opt.max_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001134 {
1135 ret = ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1136 if( ret != 0 )
1137 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001138 printf( " failed\n ! selected max_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001139 goto exit;
1140 }
1141 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001142
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02001143#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1144 if( opt.fallback != DFL_FALLBACK )
1145 ssl_set_fallback( &ssl, opt.fallback );
1146#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001147
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001148 printf( " ok\n" );
1149
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 /*
1151 * 4. Handshake
1152 */
1153 printf( " . Performing the SSL/TLS handshake..." );
1154 fflush( stdout );
1155
1156 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1157 {
1158 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker40e46942009-01-03 21:51:57 +00001159 {
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001160 printf( " failed\n ! ssl_handshake returned -0x%x\n", -ret );
1161 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
1162 printf(
1163 " Unable to verify the server's certificate. "
1164 "Either it is invalid,\n"
1165 " or you didn't set ca_file or ca_path "
1166 "to an appropriate value.\n"
1167 " Alternatively, you may want to use "
1168 "auth_mode=optional for testing purposes.\n" );
1169 printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 goto exit;
Paul Bakker40e46942009-01-03 21:51:57 +00001171 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001172 }
1173
Manuel Pégourié-Gonnardc580a002014-02-12 10:15:30 +01001174 printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1175 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001176
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02001177 if( ( ret = ssl_get_record_expansion( &ssl ) ) >= 0 )
1178 printf( " [ Record expansion is %d ]\n", ret );
1179 else
1180 printf( " [ Record expansion is unknown (compression) ]\n" );
1181
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001182#if defined(POLARSSL_SSL_ALPN)
1183 if( opt.alpn_string != NULL )
1184 {
1185 const char *alp = ssl_get_alpn_protocol( &ssl );
1186 printf( " [ Application Layer Protocol is %s ]\n",
1187 alp ? alp : "(none)" );
1188 }
1189#endif
1190
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001191 if( opt.reconnect != 0 )
1192 {
1193 printf(" . Saving session for reuse..." );
1194 fflush( stdout );
1195
1196 if( ( ret = ssl_get_session( &ssl, &saved_session ) ) != 0 )
1197 {
1198 printf( " failed\n ! ssl_get_session returned -0x%x\n\n", -ret );
1199 goto exit;
1200 }
1201
1202 printf( " ok\n" );
1203 }
1204
Paul Bakker36713e82013-09-17 13:25:29 +02001205#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 /*
1207 * 5. Verify the server certificate
1208 */
1209 printf( " . Verifying peer X.509 certificate..." );
1210
1211 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1212 {
1213 printf( " failed\n" );
1214
1215 if( ( ret & BADCERT_EXPIRED ) != 0 )
1216 printf( " ! server certificate has expired\n" );
1217
1218 if( ( ret & BADCERT_REVOKED ) != 0 )
1219 printf( " ! server certificate has been revoked\n" );
1220
1221 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
1222 printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
1223
1224 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
1225 printf( " ! self-signed or not signed by a trusted CA\n" );
1226
1227 printf( "\n" );
1228 }
1229 else
1230 printf( " ok\n" );
1231
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001232 if( ssl_get_peer_cert( &ssl ) != NULL )
1233 {
1234 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001235 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1236 ssl_get_peer_cert( &ssl ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001237 printf( "%s\n", buf );
1238 }
Paul Bakker36713e82013-09-17 13:25:29 +02001239#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001240
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001241 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001242 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001243 /*
1244 * Perform renegotiation (this must be done when the server is waiting
1245 * for input from our side).
1246 */
1247 printf( " . Performing renegotiation..." );
1248 fflush( stdout );
1249 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001250 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001251 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1252 ret != POLARSSL_ERR_NET_WANT_WRITE )
1253 {
1254 printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
1255 goto exit;
1256 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001257 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001258 printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001259 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001260
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 /*
1262 * 6. Write the GET request
1263 */
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001264 retry_left = opt.max_resend;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001265send_request:
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 printf( " > Write to server:" );
1267 fflush( stdout );
1268
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001269 len = snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST,
1270 opt.request_page );
1271 tail_len = strlen( GET_REQUEST_END );
1272
1273 /* Add padding to GET request to reach opt.request_size in length */
1274 if( opt.request_size != DFL_REQUEST_SIZE &&
1275 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001276 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001277 memset( buf + len, 'A', opt.request_size - len - tail_len );
1278 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001279 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001280
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001281 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof(buf) - len - 1 );
1282 len += tail_len;
1283
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001284 /* Truncate if request size is smaller than the "natural" size */
1285 if( opt.request_size != DFL_REQUEST_SIZE &&
1286 len > opt.request_size )
1287 {
1288 len = opt.request_size;
1289
1290 /* Still end with \r\n unless that's really not possible */
1291 if( len >= 2 ) buf[len - 2] = '\r';
1292 if( len >= 1 ) buf[len - 1] = '\n';
1293 }
1294
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001295 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001297 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001299 while( ( ret = ssl_write( &ssl, buf + written, len - written ) )
1300 <= 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001301 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001302 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1303 ret != POLARSSL_ERR_NET_WANT_WRITE )
1304 {
1305 printf( " failed\n ! ssl_write returned -0x%x\n\n", -ret );
1306 goto exit;
1307 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001308 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 }
1310 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001311 else /* Not stream, so datagram */
1312 {
1313 do ret = ssl_write( &ssl, buf, len );
1314 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1315 ret == POLARSSL_ERR_NET_WANT_WRITE );
1316
1317 if( ret < 0 )
1318 {
1319 printf( " failed\n ! ssl_write returned %d\n\n", ret );
1320 goto exit;
1321 }
1322
1323 frags = 1;
1324 written = ret;
1325 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001327 buf[written] = '\0';
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001328 printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001329
1330 /*
1331 * 7. Read the HTTP response
1332 */
1333 printf( " < Read from server:" );
1334 fflush( stdout );
1335
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001336 /*
1337 * TLS and DTLS need different reading styles (stream vs datagram)
1338 */
1339 if( opt.transport == SSL_TRANSPORT_STREAM )
1340 {
1341 do
1342 {
1343 len = sizeof( buf ) - 1;
1344 memset( buf, 0, sizeof( buf ) );
1345 ret = ssl_read( &ssl, buf, len );
1346
1347 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1348 ret == POLARSSL_ERR_NET_WANT_WRITE )
1349 continue;
1350
1351 if( ret <= 0 )
1352 {
1353 switch( ret )
1354 {
1355 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1356 printf( " connection was closed gracefully\n" );
1357 ret = 0;
1358 goto close_notify;
1359
1360 case 0:
1361 case POLARSSL_ERR_NET_CONN_RESET:
1362 printf( " connection was reset by peer\n" );
1363 ret = 0;
1364 goto reconnect;
1365
1366 default:
1367 printf( " ssl_read returned -0x%x\n", -ret );
1368 goto exit;
1369 }
1370 }
1371
1372 len = ret;
1373 buf[len] = '\0';
1374 printf( " %d bytes read\n\n%s", len, (char *) buf );
1375
1376 /* End of message should be detected according to the syntax of the
1377 * application protocol (eg HTTP), just use a dummy test here. */
1378 if( ret > 0 && buf[len-1] == '\n' )
1379 {
1380 ret = 0;
1381 break;
1382 }
1383 }
1384 while( 1 );
1385 }
1386 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 {
1388 len = sizeof( buf ) - 1;
1389 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001391 do ret = ssl_read( &ssl, buf, len );
1392 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1393 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001394
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001395 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001397 switch( ret )
1398 {
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001399 case POLARSSL_ERR_NET_TIMEOUT:
1400 printf( " timeout\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001401 if( retry_left-- > 0 )
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001402 goto send_request;
1403 goto exit;
1404
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001405 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1406 printf( " connection was closed gracefully\n" );
1407 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001408 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00001409
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001410 default:
1411 printf( " ssl_read returned -0x%x\n", -ret );
1412 goto exit;
1413 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001414 }
1415
Paul Bakker5121ce52009-01-03 21:22:43 +00001416 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02001417 buf[len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00001418 printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001419 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001420 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001422 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001423 * 7b. Continue doing data exchanges?
1424 */
1425 if( --opt.exchanges > 0 )
1426 goto send_request;
1427
1428 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001429 * 8. Done, cleanly close the connection
1430 */
1431close_notify:
1432 printf( " . Closing the connection..." );
1433
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001434 /* No error checking, the connection might be closed already */
1435 do
1436 ret = ssl_close_notify( &ssl );
1437 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
1438 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001439
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001440 printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001441
1442 /*
1443 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001444 */
1445reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001446 if( opt.reconnect != 0 )
1447 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001448 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001449
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001450 net_close( server_fd );
1451
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001452#if defined(POLARSSL_TIMING_C)
1453 if( opt.reco_delay > 0 )
1454 m_sleep( 1000 * opt.reco_delay );
1455#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02001456
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001457 printf( " . Reconnecting with saved session..." );
1458 fflush( stdout );
1459
1460 if( ( ret = ssl_session_reset( &ssl ) ) != 0 )
1461 {
1462 printf( " failed\n ! ssl_session_reset returned -0x%x\n\n", -ret );
1463 goto exit;
1464 }
1465
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001466 if( ( ret = ssl_set_session( &ssl, &saved_session ) ) != 0 )
1467 {
1468 printf( " failed\n ! ssl_set_session returned %d\n\n", ret );
1469 goto exit;
1470 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001471
Manuel Pégourié-Gonnard484b8f92014-09-23 12:36:12 +02001472 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001473 opt.transport == SSL_TRANSPORT_STREAM ?
1474 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001475 {
1476 printf( " failed\n ! net_connect returned -0x%x\n\n", -ret );
1477 goto exit;
1478 }
1479
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001480 if( opt.nbio > 0 )
1481 ret = net_set_nonblock( server_fd );
1482 else
1483 ret = net_set_block( server_fd );
1484 if( ret != 0 )
1485 {
1486 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
1487 -ret );
1488 goto exit;
1489 }
1490
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001491 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1492 {
1493 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1494 ret != POLARSSL_ERR_NET_WANT_WRITE )
1495 {
1496 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
1497 goto exit;
1498 }
1499 }
1500
1501 printf( " ok\n" );
1502
1503 goto send_request;
1504 }
1505
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001506 /*
1507 * Cleanup and exit
1508 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001509exit:
Paul Bakkera3d195c2011-11-27 21:07:34 +00001510#ifdef POLARSSL_ERROR_C
1511 if( ret != 0 )
1512 {
1513 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001514 polarssl_strerror( ret, error_buf, 100 );
Paul Bakker570267f2012-04-10 08:22:46 +00001515 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001516 }
1517#endif
1518
Paul Bakker1a207ec2011-02-06 13:22:40 +00001519 if( server_fd )
1520 net_close( server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001521
Paul Bakker36713e82013-09-17 13:25:29 +02001522#if defined(POLARSSL_X509_CRT_PARSE_C)
1523 x509_crt_free( &clicert );
1524 x509_crt_free( &cacert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02001525 pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001526#endif
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001527 ssl_session_free( &saved_session );
Paul Bakker5121ce52009-01-03 21:22:43 +00001528 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02001529 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02001530 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00001531
Paul Bakkercce9d772011-11-18 14:26:47 +00001532#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 printf( " + Press Enter to exit this program.\n" );
1534 fflush( stdout ); getchar();
1535#endif
1536
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001537 // Shell can not handle large exit numbers -> 1 for errors
1538 if( ret < 0 )
1539 ret = 1;
1540
Paul Bakker5121ce52009-01-03 21:22:43 +00001541 return( ret );
1542}
Paul Bakker508ad5a2011-12-04 17:09:26 +00001543#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
1544 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
1545 POLARSSL_CTR_DRBG_C */