blob: f0a2b4854a6be782e77305c194c820aa1a47f3dd [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
Paul Bakker5690efc2011-05-26 13:16:06 +000079#define DFL_CA_FILE ""
Paul Bakker8d914582012-06-04 12:46:42 +000080#define DFL_CA_PATH ""
Paul Bakker67968392010-07-18 08:28:20 +000081#define DFL_CRT_FILE ""
82#define DFL_KEY_FILE ""
Paul Bakkerd4a56ec2013-04-16 18:05:29 +020083#define DFL_PSK ""
84#define DFL_PSK_IDENTITY "Client_identity"
Paul Bakker51936882011-02-20 16:05:58 +000085#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +020086#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000087#define DFL_ALLOW_LEGACY SSL_LEGACY_NO_RENEGOTIATION
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010088#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +020089#define DFL_EXCHANGES 1
Paul Bakker1d29fb52012-09-28 13:28:45 +000090#define DFL_MIN_VERSION -1
91#define DFL_MAX_VERSION -1
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +010092#define DFL_AUTH_MODE SSL_VERIFY_REQUIRED
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +020093#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +020094#define DFL_TRUNC_HMAC 0
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +020095#define DFL_RECONNECT 0
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010096#define DFL_RECO_DELAY 0
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +020097#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +020098#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +010099#define DFL_TRANSPORT SSL_TRANSPORT_STREAM
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200100#define DFL_HS_TO_MIN 0
101#define DFL_HS_TO_MAX 0
Paul Bakker0e6975b2009-02-10 22:19:10 +0000102
Paul Bakker93c32b22014-04-25 13:40:05 +0200103#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
104#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Paul Bakker9caf2d22010-02-18 19:37:19 +0000106/*
107 * global options
108 */
109struct options
110{
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200111 const char *server_name; /* hostname of the server (client only) */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100112 const char *server_addr; /* address of the server (client only) */
Paul Bakker67968392010-07-18 08:28:20 +0000113 int server_port; /* port on which the ssl service runs */
114 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100115 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200116 uint32_t read_timeout; /* timeout on ssl_read() in milliseconds */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200117 const char *request_page; /* page on server to request */
Paul Bakker93c32b22014-04-25 13:40:05 +0200118 int request_size; /* pad request with header to requested size */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200119 const char *ca_file; /* the file with the CA certificate(s) */
120 const char *ca_path; /* the path with the CA certificate(s) reside */
121 const char *crt_file; /* the file with the client certificate */
122 const char *key_file; /* the file with the client key */
123 const char *psk; /* the pre-shared key */
124 const char *psk_identity; /* the pre-shared key identity */
Paul Bakker51936882011-02-20 16:05:58 +0000125 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Paul Bakker48916f92012-09-16 19:57:18 +0000126 int renegotiation; /* enable / disable renegotiation */
127 int allow_legacy; /* allow legacy renegotiation */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100128 int renegotiate; /* attempt renegotiation? */
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200129 int renego_delay; /* delay before enforcing renegotiation */
130 int exchanges; /* number of data exchanges */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000131 int min_version; /* minimum protocol version accepted */
132 int max_version; /* maximum protocol version accepted */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100133 int auth_mode; /* verify mode for connection */
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200134 unsigned char mfl_code; /* code for maximum fragment length */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200135 int trunc_hmac; /* negotiate truncated hmac or not */
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200136 int reconnect; /* attempt to resume session */
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100137 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200138 int tickets; /* enable / disable session tickets */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200139 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100140 int transport; /* TLS or DTLS? */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200141 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
142 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Paul Bakker9caf2d22010-02-18 19:37:19 +0000143} opt;
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
Paul Bakker3c5ef712013-06-25 16:37:45 +0200145static void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +0000146{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200147 ((void) level);
148
149 fprintf( (FILE *) ctx, "%s", str );
150 fflush( (FILE *) ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151}
152
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100153/*
154 * Test recv/send functions that make sure each try returns
155 * WANT_READ/WANT_WRITE at least once before sucesseding
156 */
157static int my_recv( void *ctx, unsigned char *buf, size_t len )
158{
159 static int first_try = 1;
160 int ret;
161
162 if( first_try )
163 {
164 first_try = 0;
165 return( POLARSSL_ERR_NET_WANT_READ );
166 }
167
168 ret = net_recv( ctx, buf, len );
169 if( ret != POLARSSL_ERR_NET_WANT_READ )
170 first_try = 1; /* Next call will be a new operation */
171 return( ret );
172}
173
174static int my_send( void *ctx, const unsigned char *buf, size_t len )
175{
176 static int first_try = 1;
177 int ret;
178
179 if( first_try )
180 {
181 first_try = 0;
182 return( POLARSSL_ERR_NET_WANT_WRITE );
183 }
184
185 ret = net_send( ctx, buf, len );
186 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
187 first_try = 1; /* Next call will be a new operation */
188 return( ret );
189}
190
Paul Bakker36713e82013-09-17 13:25:29 +0200191#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker915275b2012-09-28 07:10:55 +0000192/*
193 * Enabled if debug_level > 1 in code below
194 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200195static int my_verify( void *data, x509_crt *crt, int depth, int *flags )
Paul Bakker915275b2012-09-28 07:10:55 +0000196{
197 char buf[1024];
198 ((void) data);
199
200 printf( "\nVerify requested for (Depth %d):\n", depth );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200201 x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Paul Bakker915275b2012-09-28 07:10:55 +0000202 printf( "%s", buf );
203
204 if( ( (*flags) & BADCERT_EXPIRED ) != 0 )
205 printf( " ! server certificate has expired\n" );
206
207 if( ( (*flags) & BADCERT_REVOKED ) != 0 )
208 printf( " ! server certificate has been revoked\n" );
209
210 if( ( (*flags) & BADCERT_CN_MISMATCH ) != 0 )
211 printf( " ! CN mismatch\n" );
212
213 if( ( (*flags) & BADCERT_NOT_TRUSTED ) != 0 )
214 printf( " ! self-signed or not signed by a trusted CA\n" );
215
216 if( ( (*flags) & BADCRL_NOT_TRUSTED ) != 0 )
217 printf( " ! CRL not trusted\n" );
218
219 if( ( (*flags) & BADCRL_EXPIRED ) != 0 )
220 printf( " ! CRL expired\n" );
221
222 if( ( (*flags) & BADCERT_OTHER ) != 0 )
223 printf( " ! other (unknown) flag\n" );
224
225 if ( ( *flags ) == 0 )
226 printf( " This certificate has no flags\n" );
227
228 return( 0 );
229}
Paul Bakker36713e82013-09-17 13:25:29 +0200230#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +0000231
Paul Bakker36713e82013-09-17 13:25:29 +0200232#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5690efc2011-05-26 13:16:06 +0000233#if defined(POLARSSL_FS_IO)
234#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100235 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
236 " default: \"\" (pre-loaded)\n" \
237 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
238 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
239 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
240 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000241 " key_file=%%s default: \"\" (pre-loaded)\n"
242#else
243#define USAGE_IO \
244 " No file operations available (POLARSSL_FS_IO not defined)\n"
245#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200246#else
247#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200248#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200249
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200250#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200251#define USAGE_PSK \
252 " psk=%%s default: \"\" (in hex, without 0x)\n" \
253 " psk_identity=%%s default: \"Client_identity\"\n"
254#else
255#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200256#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000257
Paul Bakkera503a632013-08-14 13:48:06 +0200258#if defined(POLARSSL_SSL_SESSION_TICKETS)
259#define USAGE_TICKETS \
260 " tickets=%%d default: 1 (enabled)\n"
261#else
262#define USAGE_TICKETS ""
263#endif /* POLARSSL_SSL_SESSION_TICKETS */
264
Paul Bakker1f2bc622013-08-15 13:45:55 +0200265#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
266#define USAGE_TRUNC_HMAC \
267 " trunc_hmac=%%d default: 0 (disabled)\n"
268#else
269#define USAGE_TRUNC_HMAC ""
270#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
271
Paul Bakker05decb22013-08-15 13:33:48 +0200272#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
273#define USAGE_MAX_FRAG_LEN \
274 " max_frag_len=%%d default: 16384 (tls default)\n" \
275 " options: 512, 1024, 2048, 4096\n"
276#else
277#define USAGE_MAX_FRAG_LEN ""
278#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
279
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100280#if defined(POLARSSL_TIMING_C)
281#define USAGE_TIME \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200282 " reco_delay=%%d default: 0 seconds\n"
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100283#else
284#define USAGE_TIME ""
285#endif /* POLARSSL_TIMING_C */
286
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200287#if defined(POLARSSL_SSL_ALPN)
288#define USAGE_ALPN \
289 " alpn=%%s default: \"\" (disabled)\n" \
290 " example: spdy/1,http/1.1\n"
291#else
292#define USAGE_ALPN ""
293#endif /* POLARSSL_SSL_ALPN */
294
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200295#if defined(POLARSSL_SSL_PROTO_DTLS)
296#define USAGE_DTLS \
297 " dtls=%%d default: 0 (TLS)\n" \
298 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
299 " range of DTLS handshake timeouts in millisecs\n"
300#else
301#define USAGE_DTLS ""
302#endif
303
Paul Bakker9caf2d22010-02-18 19:37:19 +0000304#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000305 "\n usage: ssl_client2 param=<>...\n" \
306 "\n acceptable parameters:\n" \
307 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100308 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000309 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000310 " request_page=%%s default: \".\"\n" \
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +0200311 " request_size=%%d default: about 34 (basic request)\n" \
312 " (minimum: 0, max: 16384)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100313 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100314 " nbio=%%d default: 0 (blocking I/O)\n" \
315 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200316 " read_timeout=%%d default: 0 (no timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100317 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200318 USAGE_DTLS \
319 "\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100320 " auth_mode=%%s default: \"optional\"\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100321 " options: none, optional, required\n" \
322 USAGE_IO \
323 "\n" \
324 USAGE_PSK \
325 "\n" \
Paul Bakker48916f92012-09-16 19:57:18 +0000326 " renegotiation=%%d default: 1 (enabled)\n" \
327 " allow_legacy=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100328 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200329 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200330 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100331 USAGE_TIME \
Paul Bakkera503a632013-08-14 13:48:06 +0200332 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100333 USAGE_MAX_FRAG_LEN \
334 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200335 USAGE_ALPN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000336 "\n" \
337 " min_version=%%s default: \"\" (ssl3)\n" \
338 " max_version=%%s default: \"\" (tls1_2)\n" \
339 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100340 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100341 " auth_mode=%%s default: \"required\"\n" \
Paul Bakker91ebfb52012-11-23 14:04:08 +0100342 " options: none, optional, required\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000343 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000344 " force_ciphersuite=<name> default: all enabled\n"\
345 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000346
347int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000348{
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +0200349 int ret = 0, len, tail_len, server_fd, i, written, frags;
Paul Bakker93c32b22014-04-25 13:40:05 +0200350 unsigned char buf[SSL_MAX_CONTENT_LEN + 1];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200351#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200352 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200353 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200354#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200355#if defined(POLARSSL_SSL_ALPN)
356 const char *alpn_list[10];
357#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200358 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000359
360 entropy_context entropy;
361 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 ssl_context ssl;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200363 ssl_session saved_session;
Paul Bakker36713e82013-09-17 13:25:29 +0200364#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200365 x509_crt cacert;
366 x509_crt clicert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200367 pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200368#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000369 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000370 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000371
Paul Bakker1a207ec2011-02-06 13:22:40 +0000372 /*
373 * Make sure memory references are valid.
374 */
375 server_fd = 0;
Paul Bakker1a207ec2011-02-06 13:22:40 +0000376 memset( &ssl, 0, sizeof( ssl_context ) );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200377 memset( &saved_session, 0, sizeof( ssl_session ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200378#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200379 x509_crt_init( &cacert );
380 x509_crt_init( &clicert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200381 pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200382#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200383#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200384 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200385#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000386
Paul Bakker9caf2d22010-02-18 19:37:19 +0000387 if( argc == 0 )
388 {
389 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000390 if( ret == 0 )
391 ret = 1;
392
Paul Bakker9caf2d22010-02-18 19:37:19 +0000393 printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000394
395 list = ssl_list_ciphersuites();
396 while( *list )
397 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200398 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200399 list++;
400 if( !*list )
401 break;
402 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000403 list++;
404 }
405 printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000406 goto exit;
407 }
408
409 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100410 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000411 opt.server_port = DFL_SERVER_PORT;
412 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100413 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200414 opt.read_timeout = DFL_READ_TIMEOUT;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000415 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200416 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000417 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000418 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000419 opt.crt_file = DFL_CRT_FILE;
420 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200421 opt.psk = DFL_PSK;
422 opt.psk_identity = DFL_PSK_IDENTITY;
Paul Bakker51936882011-02-20 16:05:58 +0000423 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000424 opt.renegotiation = DFL_RENEGOTIATION;
425 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100426 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200427 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000428 opt.min_version = DFL_MIN_VERSION;
429 opt.max_version = DFL_MAX_VERSION;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100430 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200431 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200432 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200433 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100434 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200435 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200436 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200437 opt.transport = DFL_TRANSPORT;
438 opt.hs_to_min = DFL_HS_TO_MIN;
439 opt.hs_to_max = DFL_HS_TO_MAX;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000440
441 for( i = 1; i < argc; i++ )
442 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000443 p = argv[i];
444 if( ( q = strchr( p, '=' ) ) == NULL )
445 goto usage;
446 *q++ = '\0';
447
448 if( strcmp( p, "server_name" ) == 0 )
449 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100450 else if( strcmp( p, "server_addr" ) == 0 )
451 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000452 else if( strcmp( p, "server_port" ) == 0 )
453 {
454 opt.server_port = atoi( q );
455 if( opt.server_port < 1 || opt.server_port > 65535 )
456 goto usage;
457 }
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100458 else if( strcmp( p, "dtls" ) == 0 )
459 {
460 int t = atoi( q );
461 if( t == 0 )
462 opt.transport = SSL_TRANSPORT_STREAM;
463 else if( t == 1 )
464 opt.transport = SSL_TRANSPORT_DATAGRAM;
465 else
466 goto usage;
467 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000468 else if( strcmp( p, "debug_level" ) == 0 )
469 {
470 opt.debug_level = atoi( q );
471 if( opt.debug_level < 0 || opt.debug_level > 65535 )
472 goto usage;
473 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100474 else if( strcmp( p, "nbio" ) == 0 )
475 {
476 opt.nbio = atoi( q );
477 if( opt.nbio < 0 || opt.nbio > 2 )
478 goto usage;
479 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200480 else if( strcmp( p, "read_timeout" ) == 0 )
481 opt.read_timeout = atoi( q );
Paul Bakker9caf2d22010-02-18 19:37:19 +0000482 else if( strcmp( p, "request_page" ) == 0 )
483 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200484 else if( strcmp( p, "request_size" ) == 0 )
485 {
486 opt.request_size = atoi( q );
487 if( opt.request_size < 0 || opt.request_size > SSL_MAX_CONTENT_LEN )
488 goto usage;
489 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000490 else if( strcmp( p, "ca_file" ) == 0 )
491 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000492 else if( strcmp( p, "ca_path" ) == 0 )
493 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000494 else if( strcmp( p, "crt_file" ) == 0 )
495 opt.crt_file = q;
496 else if( strcmp( p, "key_file" ) == 0 )
497 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200498 else if( strcmp( p, "psk" ) == 0 )
499 opt.psk = q;
500 else if( strcmp( p, "psk_identity" ) == 0 )
501 opt.psk_identity = q;
Paul Bakker51936882011-02-20 16:05:58 +0000502 else if( strcmp( p, "force_ciphersuite" ) == 0 )
503 {
Paul Bakker51936882011-02-20 16:05:58 +0000504 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
505
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200506 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000507 {
508 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000509 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000510 }
Paul Bakker51936882011-02-20 16:05:58 +0000511 opt.force_ciphersuite[1] = 0;
512 }
Paul Bakker48916f92012-09-16 19:57:18 +0000513 else if( strcmp( p, "renegotiation" ) == 0 )
514 {
515 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
516 SSL_RENEGOTIATION_DISABLED;
517 }
518 else if( strcmp( p, "allow_legacy" ) == 0 )
519 {
520 opt.allow_legacy = atoi( q );
521 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
522 goto usage;
523 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100524 else if( strcmp( p, "renegotiate" ) == 0 )
525 {
526 opt.renegotiate = atoi( q );
527 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
528 goto usage;
529 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200530 else if( strcmp( p, "exchanges" ) == 0 )
531 {
532 opt.exchanges = atoi( q );
533 if( opt.exchanges < 1 )
534 goto usage;
535 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200536 else if( strcmp( p, "reconnect" ) == 0 )
537 {
538 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200539 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200540 goto usage;
541 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100542 else if( strcmp( p, "reco_delay" ) == 0 )
543 {
544 opt.reco_delay = atoi( q );
545 if( opt.reco_delay < 0 )
546 goto usage;
547 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200548 else if( strcmp( p, "tickets" ) == 0 )
549 {
550 opt.tickets = atoi( q );
551 if( opt.tickets < 0 || opt.tickets > 2 )
552 goto usage;
553 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200554 else if( strcmp( p, "alpn" ) == 0 )
555 {
556 opt.alpn_string = q;
557 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000558 else if( strcmp( p, "min_version" ) == 0 )
559 {
560 if( strcmp( q, "ssl3" ) == 0 )
561 opt.min_version = SSL_MINOR_VERSION_0;
562 else if( strcmp( q, "tls1" ) == 0 )
563 opt.min_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100564 else if( strcmp( q, "tls1_1" ) == 0 ||
565 strcmp( q, "dtls1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000566 opt.min_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100567 else if( strcmp( q, "tls1_2" ) == 0 ||
568 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000569 opt.min_version = SSL_MINOR_VERSION_3;
570 else
571 goto usage;
572 }
573 else if( strcmp( p, "max_version" ) == 0 )
574 {
575 if( strcmp( q, "ssl3" ) == 0 )
576 opt.max_version = SSL_MINOR_VERSION_0;
577 else if( strcmp( q, "tls1" ) == 0 )
578 opt.max_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100579 else if( strcmp( q, "tls1_1" ) == 0 ||
580 strcmp( q, "dtls1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000581 opt.max_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100582 else if( strcmp( q, "tls1_2" ) == 0 ||
583 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000584 opt.max_version = SSL_MINOR_VERSION_3;
585 else
586 goto usage;
587 }
588 else if( strcmp( p, "force_version" ) == 0 )
589 {
590 if( strcmp( q, "ssl3" ) == 0 )
591 {
592 opt.min_version = SSL_MINOR_VERSION_0;
593 opt.max_version = SSL_MINOR_VERSION_0;
594 }
595 else if( strcmp( q, "tls1" ) == 0 )
596 {
597 opt.min_version = SSL_MINOR_VERSION_1;
598 opt.max_version = SSL_MINOR_VERSION_1;
599 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100600 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000601 {
602 opt.min_version = SSL_MINOR_VERSION_2;
603 opt.max_version = SSL_MINOR_VERSION_2;
604 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100605 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000606 {
607 opt.min_version = SSL_MINOR_VERSION_3;
608 opt.max_version = SSL_MINOR_VERSION_3;
609 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100610 else if( strcmp( q, "dtls1" ) == 0 )
611 {
612 opt.min_version = SSL_MINOR_VERSION_2;
613 opt.max_version = SSL_MINOR_VERSION_2;
614 opt.transport = SSL_TRANSPORT_DATAGRAM;
615 }
616 else if( strcmp( q, "dtls1_2" ) == 0 )
617 {
618 opt.min_version = SSL_MINOR_VERSION_3;
619 opt.max_version = SSL_MINOR_VERSION_3;
620 opt.transport = SSL_TRANSPORT_DATAGRAM;
621 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000622 else
623 goto usage;
624 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100625 else if( strcmp( p, "auth_mode" ) == 0 )
626 {
627 if( strcmp( q, "none" ) == 0 )
628 opt.auth_mode = SSL_VERIFY_NONE;
629 else if( strcmp( q, "optional" ) == 0 )
630 opt.auth_mode = SSL_VERIFY_OPTIONAL;
631 else if( strcmp( q, "required" ) == 0 )
632 opt.auth_mode = SSL_VERIFY_REQUIRED;
633 else
634 goto usage;
635 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200636 else if( strcmp( p, "max_frag_len" ) == 0 )
637 {
638 if( strcmp( q, "512" ) == 0 )
639 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
640 else if( strcmp( q, "1024" ) == 0 )
641 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
642 else if( strcmp( q, "2048" ) == 0 )
643 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
644 else if( strcmp( q, "4096" ) == 0 )
645 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
646 else
647 goto usage;
648 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200649 else if( strcmp( p, "trunc_hmac" ) == 0 )
650 {
651 opt.trunc_hmac = atoi( q );
652 if( opt.trunc_hmac < 0 || opt.trunc_hmac > 1 )
653 goto usage;
654 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200655 else if( strcmp( p, "hs_timeout" ) == 0 )
656 {
657 if( ( p = strchr( q, '-' ) ) == NULL )
658 goto usage;
659 *p++ = '\0';
660 opt.hs_to_min = atoi( q );
661 opt.hs_to_max = atoi( p );
662 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
663 goto usage;
664 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000665 else
666 goto usage;
667 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000668
Paul Bakkerc73079a2014-04-25 16:34:30 +0200669#if defined(POLARSSL_DEBUG_C)
670 debug_set_threshold( opt.debug_level );
671#endif
672
Paul Bakkerc1516be2013-06-29 16:01:32 +0200673 if( opt.force_ciphersuite[0] > 0 )
674 {
675 const ssl_ciphersuite_t *ciphersuite_info;
676 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
677
Paul Bakker66c48102013-07-26 14:05:32 +0200678 if( opt.max_version != -1 &&
679 ciphersuite_info->min_minor_ver > opt.max_version )
680 {
681 printf("forced ciphersuite not allowed with this protocol version\n");
682 ret = 2;
683 goto usage;
684 }
685 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200686 ciphersuite_info->max_minor_ver < opt.min_version )
687 {
688 printf("forced ciphersuite not allowed with this protocol version\n");
689 ret = 2;
690 goto usage;
691 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100692
693 /* If the server selects a version that's not supported by
694 * this suite, then there will be no common ciphersuite... */
695 if( opt.max_version == -1 ||
696 opt.max_version > ciphersuite_info->max_minor_ver )
697 {
Paul Bakker66c48102013-07-26 14:05:32 +0200698 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100699 }
Paul Bakker66c48102013-07-26 14:05:32 +0200700 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100701 {
Paul Bakker66c48102013-07-26 14:05:32 +0200702 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100703 /* DTLS starts with TLS 1.1 */
704 if( opt.transport == SSL_TRANSPORT_DATAGRAM &&
705 opt.min_version < SSL_MINOR_VERSION_2 )
706 opt.min_version = SSL_MINOR_VERSION_2;
707 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200708 }
709
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200710#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000711 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200712 * Unhexify the pre-shared key if any is given
713 */
714 if( strlen( opt.psk ) )
715 {
716 unsigned char c;
717 size_t j;
718
719 if( strlen( opt.psk ) % 2 != 0 )
720 {
721 printf("pre-shared key not valid hex\n");
722 goto exit;
723 }
724
725 psk_len = strlen( opt.psk ) / 2;
726
727 for( j = 0; j < strlen( opt.psk ); j += 2 )
728 {
729 c = opt.psk[j];
730 if( c >= '0' && c <= '9' )
731 c -= '0';
732 else if( c >= 'a' && c <= 'f' )
733 c -= 'a' - 10;
734 else if( c >= 'A' && c <= 'F' )
735 c -= 'A' - 10;
736 else
737 {
738 printf("pre-shared key not valid hex\n");
739 goto exit;
740 }
741 psk[ j / 2 ] = c << 4;
742
743 c = opt.psk[j + 1];
744 if( c >= '0' && c <= '9' )
745 c -= '0';
746 else if( c >= 'a' && c <= 'f' )
747 c -= 'a' - 10;
748 else if( c >= 'A' && c <= 'F' )
749 c -= 'A' - 10;
750 else
751 {
752 printf("pre-shared key not valid hex\n");
753 goto exit;
754 }
755 psk[ j / 2 ] |= c;
756 }
757 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200758#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200759
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200760#if defined(POLARSSL_SSL_ALPN)
761 if( opt.alpn_string != NULL )
762 {
763 p = (char *) opt.alpn_string;
764 i = 0;
765
766 /* Leave room for a final NULL in alpn_list */
767 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
768 {
769 alpn_list[i++] = p;
770
771 /* Terminate the current string and move on to next one */
772 while( *p != ',' && *p != '\0' )
773 p++;
774 if( *p == ',' )
775 *p++ = '\0';
776 }
777 }
778#endif /* POLARSSL_SSL_ALPN */
779
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200780 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000781 * 0. Initialize the RNG and the session data
782 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000783 printf( "\n . Seeding the random number generator..." );
784 fflush( stdout );
785
786 entropy_init( &entropy );
787 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200788 (const unsigned char *) pers,
789 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000790 {
Paul Bakker0b22e3e2012-04-18 14:23:29 +0000791 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000792 goto exit;
793 }
794
795 printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000796
Paul Bakker36713e82013-09-17 13:25:29 +0200797#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000798 /*
799 * 1.1. Load the trusted CA
800 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000801 printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000802 fflush( stdout );
803
Paul Bakker5690efc2011-05-26 13:16:06 +0000804#if defined(POLARSSL_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +0000805 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100806 if( strcmp( opt.ca_path, "none" ) == 0 )
807 ret = 0;
808 else
809 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +0000810 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100811 if( strcmp( opt.ca_file, "none" ) == 0 )
812 ret = 0;
813 else
814 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +0200815 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000816#endif
817#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200818 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
819 strlen( test_ca_list ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000820#else
821 {
822 ret = 1;
823 printf("POLARSSL_CERTS_C not defined.");
824 }
825#endif
Paul Bakker42488232012-05-16 08:21:05 +0000826 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000827 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200828 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000829 goto exit;
830 }
831
Paul Bakker42488232012-05-16 08:21:05 +0000832 printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000833
834 /*
835 * 1.2. Load own certificate and private key
836 *
837 * (can be skipped if client authentication is not required)
838 */
839 printf( " . Loading the client cert. and key..." );
840 fflush( stdout );
841
Paul Bakker5690efc2011-05-26 13:16:06 +0000842#if defined(POLARSSL_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +0000843 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100844 if( strcmp( opt.crt_file, "none" ) == 0 )
845 ret = 0;
846 else
847 ret = x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +0200848 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000849#endif
850#if defined(POLARSSL_CERTS_C)
Paul Bakkerddf26b42013-09-18 13:46:23 +0200851 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000852 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000853#else
854 {
855 ret = 1;
856 printf("POLARSSL_CERTS_C not defined.");
857 }
858#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000859 if( ret != 0 )
860 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200861 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000862 goto exit;
863 }
864
Paul Bakker5690efc2011-05-26 13:16:06 +0000865#if defined(POLARSSL_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +0000866 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100867 if( strcmp( opt.key_file, "none" ) == 0 )
868 ret = 0;
869 else
870 ret = pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +0000871 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000872#endif
873#if defined(POLARSSL_CERTS_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200874 ret = pk_parse_key( &pkey, (const unsigned char *) test_cli_key,
Paul Bakker67968392010-07-18 08:28:20 +0000875 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000876#else
877 {
878 ret = 1;
879 printf("POLARSSL_CERTS_C not defined.");
880 }
881#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000882 if( ret != 0 )
883 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200884 printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000885 goto exit;
886 }
887
888 printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +0200889#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000890
891 /*
892 * 2. Start the connection
893 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100894 if( opt.server_addr == NULL)
895 opt.server_addr = opt.server_name;
896
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +0100897 printf( " . Connecting to %s/%s/%-4d...",
898 opt.transport == SSL_TRANSPORT_STREAM ? "tcp" : "udp",
899 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +0000900 fflush( stdout );
901
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +0100902 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
903 opt.transport == SSL_TRANSPORT_STREAM ?
904 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000905 {
Paul Bakker0b22e3e2012-04-18 14:23:29 +0000906 printf( " failed\n ! net_connect returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000907 goto exit;
908 }
909
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100910 if( opt.nbio > 0 )
911 ret = net_set_nonblock( server_fd );
912 else
913 ret = net_set_block( server_fd );
914 if( ret != 0 )
915 {
916 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
917 goto exit;
918 }
919
Paul Bakker5121ce52009-01-03 21:22:43 +0000920 printf( " ok\n" );
921
922 /*
923 * 3. Setup stuff
924 */
925 printf( " . Setting up the SSL/TLS structure..." );
926 fflush( stdout );
927
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 if( ( ret = ssl_init( &ssl ) ) != 0 )
929 {
Paul Bakker0b22e3e2012-04-18 14:23:29 +0000930 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000931 goto exit;
932 }
933
Paul Bakker36713e82013-09-17 13:25:29 +0200934#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker915275b2012-09-28 07:10:55 +0000935 if( opt.debug_level > 0 )
936 ssl_set_verify( &ssl, my_verify, NULL );
Paul Bakkered27a042013-04-18 22:46:23 +0200937#endif
Paul Bakker915275b2012-09-28 07:10:55 +0000938
Paul Bakker5121ce52009-01-03 21:22:43 +0000939 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Paul Bakker91ebfb52012-11-23 14:04:08 +0100940 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +0000941
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200942#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +0100943 if( ( ret = ssl_set_transport( &ssl, opt.transport ) ) != 0 )
944 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100945 printf( " failed\n ! selected transport is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +0100946 goto exit;
947 }
948
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200949 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
950 ssl_set_handshake_timeout( &ssl, opt.hs_to_min, opt.hs_to_max );
951#endif /* POLARSSL_SSL_PROTO_DTLS */
952
Paul Bakker05decb22013-08-15 13:33:48 +0200953#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200954 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
955 {
956 printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
957 goto exit;
958 }
Paul Bakker05decb22013-08-15 13:33:48 +0200959#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200960
Paul Bakker1f2bc622013-08-15 13:45:55 +0200961#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200962 if( opt.trunc_hmac != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200963 if( ( ret = ssl_set_truncated_hmac( &ssl, SSL_TRUNC_HMAC_ENABLED ) ) != 0 )
964 {
965 printf( " failed\n ! ssl_set_truncated_hmac returned %d\n\n", ret );
966 goto exit;
967 }
Paul Bakker1f2bc622013-08-15 13:45:55 +0200968#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200969
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200970#if defined(POLARSSL_SSL_ALPN)
971 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200972 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
973 {
974 printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
975 goto exit;
976 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200977#endif
978
Paul Bakker508ad5a2011-12-04 17:09:26 +0000979 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker9caf2d22010-02-18 19:37:19 +0000980 ssl_set_dbg( &ssl, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100981
982 if( opt.nbio == 2 )
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +0200983 ssl_set_bio_timeout( &ssl, &server_fd, my_send, my_recv, NULL, 0 );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100984 else
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +0200985 ssl_set_bio_timeout( &ssl, &server_fd, net_send, net_recv,
986#if defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnardf0365122014-09-29 16:11:47 +0200987 opt.nbio == 0 ? net_recv_timeout : NULL,
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +0200988#else
989 NULL,
990#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200991 opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000992
Paul Bakkera503a632013-08-14 13:48:06 +0200993#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200994 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
995 {
996 printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
997 goto exit;
998 }
Paul Bakkera503a632013-08-14 13:48:06 +0200999#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001000
Paul Bakker645ce3a2012-10-31 12:32:41 +00001001 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker51936882011-02-20 16:05:58 +00001002 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
1003
Paul Bakker48916f92012-09-16 19:57:18 +00001004 ssl_set_renegotiation( &ssl, opt.renegotiation );
1005 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
1006
Paul Bakker36713e82013-09-17 13:25:29 +02001007#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001008 if( strcmp( opt.ca_path, "none" ) != 0 &&
1009 strcmp( opt.ca_file, "none" ) != 0 )
1010 {
1011 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
1012 }
1013 if( strcmp( opt.crt_file, "none" ) != 0 &&
1014 strcmp( opt.key_file, "none" ) != 0 )
1015 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001016 if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 )
1017 {
1018 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1019 goto exit;
1020 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001021 }
Paul Bakkered27a042013-04-18 22:46:23 +02001022#endif
1023
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001024#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001025 if( ( ret = ssl_set_psk( &ssl, psk, psk_len,
1026 (const unsigned char *) opt.psk_identity,
1027 strlen( opt.psk_identity ) ) ) != 0 )
1028 {
1029 printf( " failed\n ! ssl_set_psk returned %d\n\n", ret );
1030 goto exit;
1031 }
Paul Bakkered27a042013-04-18 22:46:23 +02001032#endif
1033
Paul Bakker0be444a2013-08-27 21:55:01 +02001034#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001035 if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1036 {
1037 printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret );
1038 goto exit;
1039 }
Paul Bakker0be444a2013-08-27 21:55:01 +02001040#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Paul Bakker1d29fb52012-09-28 13:28:45 +00001042 if( opt.min_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001043 {
1044 ret = ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1045 if( ret != 0 )
1046 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001047 printf( " failed\n ! selected min_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001048 goto exit;
1049 }
1050 }
1051
Paul Bakker1d29fb52012-09-28 13:28:45 +00001052 if( opt.max_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001053 {
1054 ret = ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1055 if( ret != 0 )
1056 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001057 printf( " failed\n ! selected max_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001058 goto exit;
1059 }
1060 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001061
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001062 printf( " ok\n" );
1063
Paul Bakker5121ce52009-01-03 21:22:43 +00001064 /*
1065 * 4. Handshake
1066 */
1067 printf( " . Performing the SSL/TLS handshake..." );
1068 fflush( stdout );
1069
1070 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1071 {
Paul Bakker831a7552011-05-18 13:32:51 +00001072 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001073 {
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001074 printf( " failed\n ! ssl_handshake returned -0x%x\n", -ret );
1075 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
1076 printf(
1077 " Unable to verify the server's certificate. "
1078 "Either it is invalid,\n"
1079 " or you didn't set ca_file or ca_path "
1080 "to an appropriate value.\n"
1081 " Alternatively, you may want to use "
1082 "auth_mode=optional for testing purposes.\n" );
1083 printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 goto exit;
1085 }
1086 }
1087
Manuel Pégourié-Gonnardc580a002014-02-12 10:15:30 +01001088 printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1089 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001091#if defined(POLARSSL_SSL_ALPN)
1092 if( opt.alpn_string != NULL )
1093 {
1094 const char *alp = ssl_get_alpn_protocol( &ssl );
1095 printf( " [ Application Layer Protocol is %s ]\n",
1096 alp ? alp : "(none)" );
1097 }
1098#endif
1099
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001100 if( opt.reconnect != 0 )
1101 {
1102 printf(" . Saving session for reuse..." );
1103 fflush( stdout );
1104
1105 if( ( ret = ssl_get_session( &ssl, &saved_session ) ) != 0 )
1106 {
1107 printf( " failed\n ! ssl_get_session returned -0x%x\n\n", -ret );
1108 goto exit;
1109 }
1110
1111 printf( " ok\n" );
1112 }
1113
Paul Bakker36713e82013-09-17 13:25:29 +02001114#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001115 /*
1116 * 5. Verify the server certificate
1117 */
1118 printf( " . Verifying peer X.509 certificate..." );
1119
1120 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1121 {
1122 printf( " failed\n" );
1123
1124 if( ( ret & BADCERT_EXPIRED ) != 0 )
1125 printf( " ! server certificate has expired\n" );
1126
1127 if( ( ret & BADCERT_REVOKED ) != 0 )
1128 printf( " ! server certificate has been revoked\n" );
1129
1130 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Paul Bakker9caf2d22010-02-18 19:37:19 +00001131 printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00001132
1133 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
1134 printf( " ! self-signed or not signed by a trusted CA\n" );
1135
1136 printf( "\n" );
1137 }
1138 else
1139 printf( " ok\n" );
1140
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001141 if( ssl_get_peer_cert( &ssl ) != NULL )
1142 {
1143 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001144 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1145 ssl_get_peer_cert( &ssl ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001146 printf( "%s\n", buf );
1147 }
Paul Bakker36713e82013-09-17 13:25:29 +02001148#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001149
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001150 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001151 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001152 /*
1153 * Perform renegotiation (this must be done when the server is waiting
1154 * for input from our side).
1155 */
1156 printf( " . Performing renegotiation..." );
1157 fflush( stdout );
1158 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001159 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001160 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1161 ret != POLARSSL_ERR_NET_WANT_WRITE )
1162 {
1163 printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
1164 goto exit;
1165 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001166 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001167 printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001168 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001169
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 /*
1171 * 6. Write the GET request
1172 */
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001173send_request:
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 printf( " > Write to server:" );
1175 fflush( stdout );
1176
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001177 len = snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST,
1178 opt.request_page );
1179 tail_len = strlen( GET_REQUEST_END );
1180
1181 /* Add padding to GET request to reach opt.request_size in length */
1182 if( opt.request_size != DFL_REQUEST_SIZE &&
1183 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001184 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001185 memset( buf + len, 'A', opt.request_size - len - tail_len );
1186 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001187 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001188
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001189 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof(buf) - len - 1 );
1190 len += tail_len;
1191
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001192 /* Truncate if request size is smaller than the "natural" size */
1193 if( opt.request_size != DFL_REQUEST_SIZE &&
1194 len > opt.request_size )
1195 {
1196 len = opt.request_size;
1197
1198 /* Still end with \r\n unless that's really not possible */
1199 if( len >= 2 ) buf[len - 2] = '\r';
1200 if( len >= 1 ) buf[len - 1] = '\n';
1201 }
1202
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001203 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001204 {
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001205 while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 {
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001207 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1208 {
1209 printf( " failed\n ! ssl_write returned -0x%x\n\n", -ret );
1210 goto exit;
1211 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001212 }
1213 }
1214
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001215 buf[written] = '\0';
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001216 printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001217
1218 /*
1219 * 7. Read the HTTP response
1220 */
1221 printf( " < Read from server:" );
1222 fflush( stdout );
1223
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001224 /*
1225 * TLS and DTLS need different reading styles (stream vs datagram)
1226 */
1227 if( opt.transport == SSL_TRANSPORT_STREAM )
1228 {
1229 do
1230 {
1231 len = sizeof( buf ) - 1;
1232 memset( buf, 0, sizeof( buf ) );
1233 ret = ssl_read( &ssl, buf, len );
1234
1235 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1236 ret == POLARSSL_ERR_NET_WANT_WRITE )
1237 continue;
1238
1239 if( ret <= 0 )
1240 {
1241 switch( ret )
1242 {
1243 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1244 printf( " connection was closed gracefully\n" );
1245 ret = 0;
1246 goto close_notify;
1247
1248 case 0:
1249 case POLARSSL_ERR_NET_CONN_RESET:
1250 printf( " connection was reset by peer\n" );
1251 ret = 0;
1252 goto reconnect;
1253
1254 default:
1255 printf( " ssl_read returned -0x%x\n", -ret );
1256 goto exit;
1257 }
1258 }
1259
1260 len = ret;
1261 buf[len] = '\0';
1262 printf( " %d bytes read\n\n%s", len, (char *) buf );
1263
1264 /* End of message should be detected according to the syntax of the
1265 * application protocol (eg HTTP), just use a dummy test here. */
1266 if( ret > 0 && buf[len-1] == '\n' )
1267 {
1268 ret = 0;
1269 break;
1270 }
1271 }
1272 while( 1 );
1273 }
1274 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001275 {
1276 len = sizeof( buf ) - 1;
1277 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001278
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001279 do ret = ssl_read( &ssl, buf, len );
1280 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1281 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001282
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001283 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001285 switch( ret )
1286 {
1287 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1288 printf( " connection was closed gracefully\n" );
1289 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001290 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001292 case 0:
1293 case POLARSSL_ERR_NET_CONN_RESET:
1294 printf( " connection was reset by peer\n" );
1295 ret = 0;
1296 goto reconnect;
1297
1298 default:
1299 printf( " ssl_read returned -0x%x\n", -ret );
1300 goto exit;
1301 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001302 }
1303
Paul Bakker5121ce52009-01-03 21:22:43 +00001304 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02001305 buf[len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001307 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001308 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001309
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001310 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001311 * 7b. Continue doing data exchanges?
1312 */
1313 if( --opt.exchanges > 0 )
1314 goto send_request;
1315
1316 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001317 * 8. Done, cleanly close the connection
1318 */
1319close_notify:
1320 printf( " . Closing the connection..." );
1321
1322 while( ( ret = ssl_close_notify( &ssl ) ) < 0 )
1323 {
1324 if( ret == POLARSSL_ERR_NET_CONN_RESET )
1325 {
1326 printf( " ok (already closed by peer)\n" );
1327 ret = 0;
1328 goto reconnect;
1329 }
1330
1331 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1332 ret != POLARSSL_ERR_NET_WANT_WRITE )
1333 {
1334 printf( " failed\n ! ssl_close_notify returned %d\n\n", ret );
1335 goto reconnect;
1336 }
1337 }
1338
1339 printf( " ok\n" );
1340
1341 /*
1342 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001343 */
1344reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001345 if( opt.reconnect != 0 )
1346 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001347 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001348
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001349 net_close( server_fd );
1350
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001351#if defined(POLARSSL_TIMING_C)
1352 if( opt.reco_delay > 0 )
1353 m_sleep( 1000 * opt.reco_delay );
1354#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02001355
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001356 printf( " . Reconnecting with saved session..." );
1357 fflush( stdout );
1358
1359 if( ( ret = ssl_session_reset( &ssl ) ) != 0 )
1360 {
1361 printf( " failed\n ! ssl_session_reset returned -0x%x\n\n", -ret );
1362 goto exit;
1363 }
1364
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001365 if( ( ret = ssl_set_session( &ssl, &saved_session ) ) != 0 )
1366 {
1367 printf( " failed\n ! ssl_set_session returned %d\n\n", ret );
1368 goto exit;
1369 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001370
Manuel Pégourié-Gonnard484b8f92014-09-23 12:36:12 +02001371 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001372 opt.transport == SSL_TRANSPORT_STREAM ?
1373 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001374 {
1375 printf( " failed\n ! net_connect returned -0x%x\n\n", -ret );
1376 goto exit;
1377 }
1378
1379 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1380 {
1381 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1382 ret != POLARSSL_ERR_NET_WANT_WRITE )
1383 {
1384 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
1385 goto exit;
1386 }
1387 }
1388
1389 printf( " ok\n" );
1390
1391 goto send_request;
1392 }
1393
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001394 /*
1395 * Cleanup and exit
1396 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001397exit:
Paul Bakkera3d195c2011-11-27 21:07:34 +00001398#ifdef POLARSSL_ERROR_C
1399 if( ret != 0 )
1400 {
1401 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001402 polarssl_strerror( ret, error_buf, 100 );
Paul Bakker570267f2012-04-10 08:22:46 +00001403 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001404 }
1405#endif
1406
Paul Bakker1a207ec2011-02-06 13:22:40 +00001407 if( server_fd )
1408 net_close( server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001409
Paul Bakker36713e82013-09-17 13:25:29 +02001410#if defined(POLARSSL_X509_CRT_PARSE_C)
1411 x509_crt_free( &clicert );
1412 x509_crt_free( &cacert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02001413 pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001414#endif
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001415 ssl_session_free( &saved_session );
Paul Bakker5121ce52009-01-03 21:22:43 +00001416 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02001417 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02001418 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00001419
Paul Bakkercce9d772011-11-18 14:26:47 +00001420#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 printf( " + Press Enter to exit this program.\n" );
1422 fflush( stdout ); getchar();
1423#endif
1424
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001425 // Shell can not handle large exit numbers -> 1 for errors
1426 if( ret < 0 )
1427 ret = 1;
1428
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 return( ret );
1430}
Paul Bakker508ad5a2011-12-04 17:09:26 +00001431#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
1432 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
1433 POLARSSL_CTR_DRBG_C */