blob: a574a2c12e95a4a72848ae69336a44cc0783e9ff [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
Paul Bakker0e6975b2009-02-10 22:19:10 +0000103
Paul Bakker93c32b22014-04-25 13:40:05 +0200104#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
105#define GET_REQUEST_END "\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Paul Bakker9caf2d22010-02-18 19:37:19 +0000107/*
108 * global options
109 */
110struct options
111{
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200112 const char *server_name; /* hostname of the server (client only) */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100113 const char *server_addr; /* address of the server (client only) */
Paul Bakker67968392010-07-18 08:28:20 +0000114 int server_port; /* port on which the ssl service runs */
115 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100116 int nbio; /* should I/O be blocking? */
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200117 uint32_t read_timeout; /* timeout on ssl_read() in milliseconds */
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200118 int max_resend; /* DTLS times to resend on read timeout */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200119 const char *request_page; /* page on server to request */
Paul Bakker93c32b22014-04-25 13:40:05 +0200120 int request_size; /* pad request with header to requested size */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200121 const char *ca_file; /* the file with the CA certificate(s) */
122 const char *ca_path; /* the path with the CA certificate(s) reside */
123 const char *crt_file; /* the file with the client certificate */
124 const char *key_file; /* the file with the client key */
125 const char *psk; /* the pre-shared key */
126 const char *psk_identity; /* the pre-shared key identity */
Paul Bakker51936882011-02-20 16:05:58 +0000127 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Paul Bakker48916f92012-09-16 19:57:18 +0000128 int renegotiation; /* enable / disable renegotiation */
129 int allow_legacy; /* allow legacy renegotiation */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100130 int renegotiate; /* attempt renegotiation? */
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200131 int renego_delay; /* delay before enforcing renegotiation */
132 int exchanges; /* number of data exchanges */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000133 int min_version; /* minimum protocol version accepted */
134 int max_version; /* maximum protocol version accepted */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100135 int auth_mode; /* verify mode for connection */
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200136 unsigned char mfl_code; /* code for maximum fragment length */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200137 int trunc_hmac; /* negotiate truncated hmac or not */
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200138 int reconnect; /* attempt to resume session */
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100139 int reco_delay; /* delay in seconds before resuming session */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200140 int tickets; /* enable / disable session tickets */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200141 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100142 int transport; /* TLS or DTLS? */
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200143 uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
144 uint32_t hs_to_max; /* Max value of DTLS handshake timer */
Paul Bakker9caf2d22010-02-18 19:37:19 +0000145} opt;
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
Paul Bakker3c5ef712013-06-25 16:37:45 +0200147static void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +0000148{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200149 ((void) level);
150
151 fprintf( (FILE *) ctx, "%s", str );
152 fflush( (FILE *) ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153}
154
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100155/*
156 * Test recv/send functions that make sure each try returns
157 * WANT_READ/WANT_WRITE at least once before sucesseding
158 */
159static int my_recv( void *ctx, unsigned char *buf, size_t len )
160{
161 static int first_try = 1;
162 int ret;
163
164 if( first_try )
165 {
166 first_try = 0;
167 return( POLARSSL_ERR_NET_WANT_READ );
168 }
169
170 ret = net_recv( ctx, buf, len );
171 if( ret != POLARSSL_ERR_NET_WANT_READ )
172 first_try = 1; /* Next call will be a new operation */
173 return( ret );
174}
175
176static int my_send( void *ctx, const unsigned char *buf, size_t len )
177{
178 static int first_try = 1;
179 int ret;
180
181 if( first_try )
182 {
183 first_try = 0;
184 return( POLARSSL_ERR_NET_WANT_WRITE );
185 }
186
187 ret = net_send( ctx, buf, len );
188 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
189 first_try = 1; /* Next call will be a new operation */
190 return( ret );
191}
192
Paul Bakker36713e82013-09-17 13:25:29 +0200193#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker915275b2012-09-28 07:10:55 +0000194/*
195 * Enabled if debug_level > 1 in code below
196 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200197static int my_verify( void *data, x509_crt *crt, int depth, int *flags )
Paul Bakker915275b2012-09-28 07:10:55 +0000198{
199 char buf[1024];
200 ((void) data);
201
202 printf( "\nVerify requested for (Depth %d):\n", depth );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200203 x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Paul Bakker915275b2012-09-28 07:10:55 +0000204 printf( "%s", buf );
205
206 if( ( (*flags) & BADCERT_EXPIRED ) != 0 )
207 printf( " ! server certificate has expired\n" );
208
209 if( ( (*flags) & BADCERT_REVOKED ) != 0 )
210 printf( " ! server certificate has been revoked\n" );
211
212 if( ( (*flags) & BADCERT_CN_MISMATCH ) != 0 )
213 printf( " ! CN mismatch\n" );
214
215 if( ( (*flags) & BADCERT_NOT_TRUSTED ) != 0 )
216 printf( " ! self-signed or not signed by a trusted CA\n" );
217
218 if( ( (*flags) & BADCRL_NOT_TRUSTED ) != 0 )
219 printf( " ! CRL not trusted\n" );
220
221 if( ( (*flags) & BADCRL_EXPIRED ) != 0 )
222 printf( " ! CRL expired\n" );
223
224 if( ( (*flags) & BADCERT_OTHER ) != 0 )
225 printf( " ! other (unknown) flag\n" );
226
227 if ( ( *flags ) == 0 )
228 printf( " This certificate has no flags\n" );
229
230 return( 0 );
231}
Paul Bakker36713e82013-09-17 13:25:29 +0200232#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker915275b2012-09-28 07:10:55 +0000233
Paul Bakker36713e82013-09-17 13:25:29 +0200234#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5690efc2011-05-26 13:16:06 +0000235#if defined(POLARSSL_FS_IO)
236#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100237 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
238 " default: \"\" (pre-loaded)\n" \
239 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
240 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
241 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
242 " default: \"\" (pre-loaded)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000243 " key_file=%%s default: \"\" (pre-loaded)\n"
244#else
245#define USAGE_IO \
246 " No file operations available (POLARSSL_FS_IO not defined)\n"
247#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200248#else
249#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200250#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200251
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200252#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200253#define USAGE_PSK \
254 " psk=%%s default: \"\" (in hex, without 0x)\n" \
255 " psk_identity=%%s default: \"Client_identity\"\n"
256#else
257#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200258#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5690efc2011-05-26 13:16:06 +0000259
Paul Bakkera503a632013-08-14 13:48:06 +0200260#if defined(POLARSSL_SSL_SESSION_TICKETS)
261#define USAGE_TICKETS \
262 " tickets=%%d default: 1 (enabled)\n"
263#else
264#define USAGE_TICKETS ""
265#endif /* POLARSSL_SSL_SESSION_TICKETS */
266
Paul Bakker1f2bc622013-08-15 13:45:55 +0200267#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
268#define USAGE_TRUNC_HMAC \
269 " trunc_hmac=%%d default: 0 (disabled)\n"
270#else
271#define USAGE_TRUNC_HMAC ""
272#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
273
Paul Bakker05decb22013-08-15 13:33:48 +0200274#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
275#define USAGE_MAX_FRAG_LEN \
276 " max_frag_len=%%d default: 16384 (tls default)\n" \
277 " options: 512, 1024, 2048, 4096\n"
278#else
279#define USAGE_MAX_FRAG_LEN ""
280#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
281
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100282#if defined(POLARSSL_TIMING_C)
283#define USAGE_TIME \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200284 " reco_delay=%%d default: 0 seconds\n"
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100285#else
286#define USAGE_TIME ""
287#endif /* POLARSSL_TIMING_C */
288
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200289#if defined(POLARSSL_SSL_ALPN)
290#define USAGE_ALPN \
291 " alpn=%%s default: \"\" (disabled)\n" \
292 " example: spdy/1,http/1.1\n"
293#else
294#define USAGE_ALPN ""
295#endif /* POLARSSL_SSL_ALPN */
296
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200297#if defined(POLARSSL_SSL_PROTO_DTLS)
298#define USAGE_DTLS \
299 " dtls=%%d default: 0 (TLS)\n" \
300 " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
301 " range of DTLS handshake timeouts in millisecs\n"
302#else
303#define USAGE_DTLS ""
304#endif
305
Paul Bakker9caf2d22010-02-18 19:37:19 +0000306#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +0000307 "\n usage: ssl_client2 param=<>...\n" \
308 "\n acceptable parameters:\n" \
309 " server_name=%%s default: localhost\n" \
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100310 " server_addr=%%s default: given by name\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000311 " server_port=%%d default: 4433\n" \
Paul Bakker67968392010-07-18 08:28:20 +0000312 " request_page=%%s default: \".\"\n" \
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +0200313 " request_size=%%d default: about 34 (basic request)\n" \
314 " (minimum: 0, max: 16384)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100315 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100316 " nbio=%%d default: 0 (blocking I/O)\n" \
317 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200318 " read_timeout=%%d default: 0 (no timeout)\n" \
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200319 " max_resend=%%d default: 0 (no resend on timeout)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100320 "\n" \
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200321 USAGE_DTLS \
322 "\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100323 " auth_mode=%%s default: \"optional\"\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100324 " options: none, optional, required\n" \
325 USAGE_IO \
326 "\n" \
327 USAGE_PSK \
328 "\n" \
Paul Bakker48916f92012-09-16 19:57:18 +0000329 " renegotiation=%%d default: 1 (enabled)\n" \
330 " allow_legacy=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100331 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200332 " exchanges=%%d default: 1\n" \
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200333 " reconnect=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100334 USAGE_TIME \
Paul Bakkera503a632013-08-14 13:48:06 +0200335 USAGE_TICKETS \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100336 USAGE_MAX_FRAG_LEN \
337 USAGE_TRUNC_HMAC \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200338 USAGE_ALPN \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000339 "\n" \
340 " min_version=%%s default: \"\" (ssl3)\n" \
341 " max_version=%%s default: \"\" (tls1_2)\n" \
342 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100343 " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100344 " auth_mode=%%s default: \"required\"\n" \
Paul Bakker91ebfb52012-11-23 14:04:08 +0100345 " options: none, optional, required\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000346 "\n" \
Paul Bakker51936882011-02-20 16:05:58 +0000347 " force_ciphersuite=<name> default: all enabled\n"\
348 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +0000349
350int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000351{
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +0200352 int ret = 0, len, tail_len, server_fd, i, written, frags;
Paul Bakker93c32b22014-04-25 13:40:05 +0200353 unsigned char buf[SSL_MAX_CONTENT_LEN + 1];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200354#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200355 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200356 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200357#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200358#if defined(POLARSSL_SSL_ALPN)
359 const char *alpn_list[10];
360#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200361 const char *pers = "ssl_client2";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000362
363 entropy_context entropy;
364 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 ssl_context ssl;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200366 ssl_session saved_session;
Paul Bakker36713e82013-09-17 13:25:29 +0200367#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200368 x509_crt cacert;
369 x509_crt clicert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200370 pk_context pkey;
Paul Bakkered27a042013-04-18 22:46:23 +0200371#endif
Paul Bakker9caf2d22010-02-18 19:37:19 +0000372 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +0000373 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000374
Paul Bakker1a207ec2011-02-06 13:22:40 +0000375 /*
376 * Make sure memory references are valid.
377 */
378 server_fd = 0;
Paul Bakker1a207ec2011-02-06 13:22:40 +0000379 memset( &ssl, 0, sizeof( ssl_context ) );
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200380 memset( &saved_session, 0, sizeof( ssl_session ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200381#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200382 x509_crt_init( &cacert );
383 x509_crt_init( &clicert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200384 pk_init( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +0200385#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200386#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200387 memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200388#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000389
Paul Bakker9caf2d22010-02-18 19:37:19 +0000390 if( argc == 0 )
391 {
392 usage:
Paul Bakkerfab5c822012-02-06 16:45:10 +0000393 if( ret == 0 )
394 ret = 1;
395
Paul Bakker9caf2d22010-02-18 19:37:19 +0000396 printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000397
398 list = ssl_list_ciphersuites();
399 while( *list )
400 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200401 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200402 list++;
403 if( !*list )
404 break;
405 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakker51936882011-02-20 16:05:58 +0000406 list++;
407 }
408 printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000409 goto exit;
410 }
411
412 opt.server_name = DFL_SERVER_NAME;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100413 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000414 opt.server_port = DFL_SERVER_PORT;
415 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100416 opt.nbio = DFL_NBIO;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200417 opt.read_timeout = DFL_READ_TIMEOUT;
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200418 opt.max_resend = DFL_MAX_RESEND;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000419 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker93c32b22014-04-25 13:40:05 +0200420 opt.request_size = DFL_REQUEST_SIZE;
Paul Bakker5690efc2011-05-26 13:16:06 +0000421 opt.ca_file = DFL_CA_FILE;
Paul Bakker8d914582012-06-04 12:46:42 +0000422 opt.ca_path = DFL_CA_PATH;
Paul Bakker67968392010-07-18 08:28:20 +0000423 opt.crt_file = DFL_CRT_FILE;
424 opt.key_file = DFL_KEY_FILE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200425 opt.psk = DFL_PSK;
426 opt.psk_identity = DFL_PSK_IDENTITY;
Paul Bakker51936882011-02-20 16:05:58 +0000427 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker48916f92012-09-16 19:57:18 +0000428 opt.renegotiation = DFL_RENEGOTIATION;
429 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100430 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200431 opt.exchanges = DFL_EXCHANGES;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000432 opt.min_version = DFL_MIN_VERSION;
433 opt.max_version = DFL_MAX_VERSION;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100434 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200435 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200436 opt.trunc_hmac = DFL_TRUNC_HMAC;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200437 opt.reconnect = DFL_RECONNECT;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100438 opt.reco_delay = DFL_RECO_DELAY;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200439 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200440 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200441 opt.transport = DFL_TRANSPORT;
442 opt.hs_to_min = DFL_HS_TO_MIN;
443 opt.hs_to_max = DFL_HS_TO_MAX;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000444
445 for( i = 1; i < argc; i++ )
446 {
Paul Bakker9caf2d22010-02-18 19:37:19 +0000447 p = argv[i];
448 if( ( q = strchr( p, '=' ) ) == NULL )
449 goto usage;
450 *q++ = '\0';
451
452 if( strcmp( p, "server_name" ) == 0 )
453 opt.server_name = q;
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100454 else if( strcmp( p, "server_addr" ) == 0 )
455 opt.server_addr = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000456 else if( strcmp( p, "server_port" ) == 0 )
457 {
458 opt.server_port = atoi( q );
459 if( opt.server_port < 1 || opt.server_port > 65535 )
460 goto usage;
461 }
Manuel Pégourié-Gonnarde29fd4b2014-02-06 14:02:55 +0100462 else if( strcmp( p, "dtls" ) == 0 )
463 {
464 int t = atoi( q );
465 if( t == 0 )
466 opt.transport = SSL_TRANSPORT_STREAM;
467 else if( t == 1 )
468 opt.transport = SSL_TRANSPORT_DATAGRAM;
469 else
470 goto usage;
471 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000472 else if( strcmp( p, "debug_level" ) == 0 )
473 {
474 opt.debug_level = atoi( q );
475 if( opt.debug_level < 0 || opt.debug_level > 65535 )
476 goto usage;
477 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100478 else if( strcmp( p, "nbio" ) == 0 )
479 {
480 opt.nbio = atoi( q );
481 if( opt.nbio < 0 || opt.nbio > 2 )
482 goto usage;
483 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +0200484 else if( strcmp( p, "read_timeout" ) == 0 )
485 opt.read_timeout = atoi( q );
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +0200486 else if( strcmp( p, "max_resend" ) == 0 )
487 {
488 opt.max_resend = atoi( q );
489 if( opt.max_resend < 0 )
490 goto usage;
491 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000492 else if( strcmp( p, "request_page" ) == 0 )
493 opt.request_page = q;
Paul Bakker93c32b22014-04-25 13:40:05 +0200494 else if( strcmp( p, "request_size" ) == 0 )
495 {
496 opt.request_size = atoi( q );
497 if( opt.request_size < 0 || opt.request_size > SSL_MAX_CONTENT_LEN )
498 goto usage;
499 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000500 else if( strcmp( p, "ca_file" ) == 0 )
501 opt.ca_file = q;
Paul Bakker8d914582012-06-04 12:46:42 +0000502 else if( strcmp( p, "ca_path" ) == 0 )
503 opt.ca_path = q;
Paul Bakker67968392010-07-18 08:28:20 +0000504 else if( strcmp( p, "crt_file" ) == 0 )
505 opt.crt_file = q;
506 else if( strcmp( p, "key_file" ) == 0 )
507 opt.key_file = q;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200508 else if( strcmp( p, "psk" ) == 0 )
509 opt.psk = q;
510 else if( strcmp( p, "psk_identity" ) == 0 )
511 opt.psk_identity = q;
Paul Bakker51936882011-02-20 16:05:58 +0000512 else if( strcmp( p, "force_ciphersuite" ) == 0 )
513 {
Paul Bakker51936882011-02-20 16:05:58 +0000514 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
515
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200516 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerfab5c822012-02-06 16:45:10 +0000517 {
518 ret = 2;
Paul Bakker51936882011-02-20 16:05:58 +0000519 goto usage;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000520 }
Paul Bakker51936882011-02-20 16:05:58 +0000521 opt.force_ciphersuite[1] = 0;
522 }
Paul Bakker48916f92012-09-16 19:57:18 +0000523 else if( strcmp( p, "renegotiation" ) == 0 )
524 {
525 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
526 SSL_RENEGOTIATION_DISABLED;
527 }
528 else if( strcmp( p, "allow_legacy" ) == 0 )
529 {
530 opt.allow_legacy = atoi( q );
531 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
532 goto usage;
533 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100534 else if( strcmp( p, "renegotiate" ) == 0 )
535 {
536 opt.renegotiate = atoi( q );
537 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
538 goto usage;
539 }
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +0200540 else if( strcmp( p, "exchanges" ) == 0 )
541 {
542 opt.exchanges = atoi( q );
543 if( opt.exchanges < 1 )
544 goto usage;
545 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200546 else if( strcmp( p, "reconnect" ) == 0 )
547 {
548 opt.reconnect = atoi( q );
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +0200549 if( opt.reconnect < 0 || opt.reconnect > 2 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +0200550 goto usage;
551 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100552 else if( strcmp( p, "reco_delay" ) == 0 )
553 {
554 opt.reco_delay = atoi( q );
555 if( opt.reco_delay < 0 )
556 goto usage;
557 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200558 else if( strcmp( p, "tickets" ) == 0 )
559 {
560 opt.tickets = atoi( q );
561 if( opt.tickets < 0 || opt.tickets > 2 )
562 goto usage;
563 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200564 else if( strcmp( p, "alpn" ) == 0 )
565 {
566 opt.alpn_string = q;
567 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000568 else if( strcmp( p, "min_version" ) == 0 )
569 {
570 if( strcmp( q, "ssl3" ) == 0 )
571 opt.min_version = SSL_MINOR_VERSION_0;
572 else if( strcmp( q, "tls1" ) == 0 )
573 opt.min_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100574 else if( strcmp( q, "tls1_1" ) == 0 ||
575 strcmp( q, "dtls1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000576 opt.min_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100577 else if( strcmp( q, "tls1_2" ) == 0 ||
578 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000579 opt.min_version = SSL_MINOR_VERSION_3;
580 else
581 goto usage;
582 }
583 else if( strcmp( p, "max_version" ) == 0 )
584 {
585 if( strcmp( q, "ssl3" ) == 0 )
586 opt.max_version = SSL_MINOR_VERSION_0;
587 else if( strcmp( q, "tls1" ) == 0 )
588 opt.max_version = SSL_MINOR_VERSION_1;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100589 else if( strcmp( q, "tls1_1" ) == 0 ||
590 strcmp( q, "dtls1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000591 opt.max_version = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnard83218f12014-02-12 11:11:12 +0100592 else if( strcmp( q, "tls1_2" ) == 0 ||
593 strcmp( q, "dtls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000594 opt.max_version = SSL_MINOR_VERSION_3;
595 else
596 goto usage;
597 }
598 else if( strcmp( p, "force_version" ) == 0 )
599 {
600 if( strcmp( q, "ssl3" ) == 0 )
601 {
602 opt.min_version = SSL_MINOR_VERSION_0;
603 opt.max_version = SSL_MINOR_VERSION_0;
604 }
605 else if( strcmp( q, "tls1" ) == 0 )
606 {
607 opt.min_version = SSL_MINOR_VERSION_1;
608 opt.max_version = SSL_MINOR_VERSION_1;
609 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100610 else if( strcmp( q, "tls1_1" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000611 {
612 opt.min_version = SSL_MINOR_VERSION_2;
613 opt.max_version = SSL_MINOR_VERSION_2;
614 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100615 else if( strcmp( q, "tls1_2" ) == 0 )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000616 {
617 opt.min_version = SSL_MINOR_VERSION_3;
618 opt.max_version = SSL_MINOR_VERSION_3;
619 }
Manuel Pégourié-Gonnardfe3f73b2014-03-26 12:16:44 +0100620 else if( strcmp( q, "dtls1" ) == 0 )
621 {
622 opt.min_version = SSL_MINOR_VERSION_2;
623 opt.max_version = SSL_MINOR_VERSION_2;
624 opt.transport = SSL_TRANSPORT_DATAGRAM;
625 }
626 else if( strcmp( q, "dtls1_2" ) == 0 )
627 {
628 opt.min_version = SSL_MINOR_VERSION_3;
629 opt.max_version = SSL_MINOR_VERSION_3;
630 opt.transport = SSL_TRANSPORT_DATAGRAM;
631 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000632 else
633 goto usage;
634 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100635 else if( strcmp( p, "auth_mode" ) == 0 )
636 {
637 if( strcmp( q, "none" ) == 0 )
638 opt.auth_mode = SSL_VERIFY_NONE;
639 else if( strcmp( q, "optional" ) == 0 )
640 opt.auth_mode = SSL_VERIFY_OPTIONAL;
641 else if( strcmp( q, "required" ) == 0 )
642 opt.auth_mode = SSL_VERIFY_REQUIRED;
643 else
644 goto usage;
645 }
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200646 else if( strcmp( p, "max_frag_len" ) == 0 )
647 {
648 if( strcmp( q, "512" ) == 0 )
649 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
650 else if( strcmp( q, "1024" ) == 0 )
651 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
652 else if( strcmp( q, "2048" ) == 0 )
653 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
654 else if( strcmp( q, "4096" ) == 0 )
655 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
656 else
657 goto usage;
658 }
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200659 else if( strcmp( p, "trunc_hmac" ) == 0 )
660 {
661 opt.trunc_hmac = atoi( q );
662 if( opt.trunc_hmac < 0 || opt.trunc_hmac > 1 )
663 goto usage;
664 }
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200665 else if( strcmp( p, "hs_timeout" ) == 0 )
666 {
667 if( ( p = strchr( q, '-' ) ) == NULL )
668 goto usage;
669 *p++ = '\0';
670 opt.hs_to_min = atoi( q );
671 opt.hs_to_max = atoi( p );
672 if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
673 goto usage;
674 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000675 else
676 goto usage;
677 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000678
Paul Bakkerc73079a2014-04-25 16:34:30 +0200679#if defined(POLARSSL_DEBUG_C)
680 debug_set_threshold( opt.debug_level );
681#endif
682
Paul Bakkerc1516be2013-06-29 16:01:32 +0200683 if( opt.force_ciphersuite[0] > 0 )
684 {
685 const ssl_ciphersuite_t *ciphersuite_info;
686 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
687
Paul Bakker66c48102013-07-26 14:05:32 +0200688 if( opt.max_version != -1 &&
689 ciphersuite_info->min_minor_ver > opt.max_version )
690 {
691 printf("forced ciphersuite not allowed with this protocol version\n");
692 ret = 2;
693 goto usage;
694 }
695 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200696 ciphersuite_info->max_minor_ver < opt.min_version )
697 {
698 printf("forced ciphersuite not allowed with this protocol version\n");
699 ret = 2;
700 goto usage;
701 }
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100702
703 /* If the server selects a version that's not supported by
704 * this suite, then there will be no common ciphersuite... */
705 if( opt.max_version == -1 ||
706 opt.max_version > ciphersuite_info->max_minor_ver )
707 {
Paul Bakker66c48102013-07-26 14:05:32 +0200708 opt.max_version = ciphersuite_info->max_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100709 }
Paul Bakker66c48102013-07-26 14:05:32 +0200710 if( opt.min_version < ciphersuite_info->min_minor_ver )
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100711 {
Paul Bakker66c48102013-07-26 14:05:32 +0200712 opt.min_version = ciphersuite_info->min_minor_ver;
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100713 /* DTLS starts with TLS 1.1 */
714 if( opt.transport == SSL_TRANSPORT_DATAGRAM &&
715 opt.min_version < SSL_MINOR_VERSION_2 )
716 opt.min_version = SSL_MINOR_VERSION_2;
717 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200718 }
719
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200720#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200722 * Unhexify the pre-shared key if any is given
723 */
724 if( strlen( opt.psk ) )
725 {
726 unsigned char c;
727 size_t j;
728
729 if( strlen( opt.psk ) % 2 != 0 )
730 {
731 printf("pre-shared key not valid hex\n");
732 goto exit;
733 }
734
735 psk_len = strlen( opt.psk ) / 2;
736
737 for( j = 0; j < strlen( opt.psk ); j += 2 )
738 {
739 c = opt.psk[j];
740 if( c >= '0' && c <= '9' )
741 c -= '0';
742 else if( c >= 'a' && c <= 'f' )
743 c -= 'a' - 10;
744 else if( c >= 'A' && c <= 'F' )
745 c -= 'A' - 10;
746 else
747 {
748 printf("pre-shared key not valid hex\n");
749 goto exit;
750 }
751 psk[ j / 2 ] = c << 4;
752
753 c = opt.psk[j + 1];
754 if( c >= '0' && c <= '9' )
755 c -= '0';
756 else if( c >= 'a' && c <= 'f' )
757 c -= 'a' - 10;
758 else if( c >= 'A' && c <= 'F' )
759 c -= 'A' - 10;
760 else
761 {
762 printf("pre-shared key not valid hex\n");
763 goto exit;
764 }
765 psk[ j / 2 ] |= c;
766 }
767 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200768#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200769
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200770#if defined(POLARSSL_SSL_ALPN)
771 if( opt.alpn_string != NULL )
772 {
773 p = (char *) opt.alpn_string;
774 i = 0;
775
776 /* Leave room for a final NULL in alpn_list */
777 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
778 {
779 alpn_list[i++] = p;
780
781 /* Terminate the current string and move on to next one */
782 while( *p != ',' && *p != '\0' )
783 p++;
784 if( *p == ',' )
785 *p++ = '\0';
786 }
787 }
788#endif /* POLARSSL_SSL_ALPN */
789
Paul Bakkerd4a56ec2013-04-16 18:05:29 +0200790 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000791 * 0. Initialize the RNG and the session data
792 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000793 printf( "\n . Seeding the random number generator..." );
794 fflush( stdout );
795
796 entropy_init( &entropy );
797 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200798 (const unsigned char *) pers,
799 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000800 {
Paul Bakker0b22e3e2012-04-18 14:23:29 +0000801 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000802 goto exit;
803 }
804
805 printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000806
Paul Bakker36713e82013-09-17 13:25:29 +0200807#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000808 /*
809 * 1.1. Load the trusted CA
810 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000811 printf( " . Loading the CA root certificate ..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000812 fflush( stdout );
813
Paul Bakker5690efc2011-05-26 13:16:06 +0000814#if defined(POLARSSL_FS_IO)
Paul Bakker8d914582012-06-04 12:46:42 +0000815 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100816 if( strcmp( opt.ca_path, "none" ) == 0 )
817 ret = 0;
818 else
819 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker8d914582012-06-04 12:46:42 +0000820 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100821 if( strcmp( opt.ca_file, "none" ) == 0 )
822 ret = 0;
823 else
824 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkered27a042013-04-18 22:46:23 +0200825 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000826#endif
827#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200828 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
829 strlen( test_ca_list ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000830#else
831 {
832 ret = 1;
833 printf("POLARSSL_CERTS_C not defined.");
834 }
835#endif
Paul Bakker42488232012-05-16 08:21:05 +0000836 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200838 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000839 goto exit;
840 }
841
Paul Bakker42488232012-05-16 08:21:05 +0000842 printf( " ok (%d skipped)\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000843
844 /*
845 * 1.2. Load own certificate and private key
846 *
847 * (can be skipped if client authentication is not required)
848 */
849 printf( " . Loading the client cert. and key..." );
850 fflush( stdout );
851
Paul Bakker5690efc2011-05-26 13:16:06 +0000852#if defined(POLARSSL_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +0000853 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100854 if( strcmp( opt.crt_file, "none" ) == 0 )
855 ret = 0;
856 else
857 ret = x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkered27a042013-04-18 22:46:23 +0200858 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000859#endif
860#if defined(POLARSSL_CERTS_C)
Paul Bakkerddf26b42013-09-18 13:46:23 +0200861 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000862 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000863#else
864 {
865 ret = 1;
866 printf("POLARSSL_CERTS_C not defined.");
867 }
868#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 if( ret != 0 )
870 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200871 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000872 goto exit;
873 }
874
Paul Bakker5690efc2011-05-26 13:16:06 +0000875#if defined(POLARSSL_FS_IO)
Paul Bakker67968392010-07-18 08:28:20 +0000876 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +0100877 if( strcmp( opt.key_file, "none" ) == 0 )
878 ret = 0;
879 else
880 ret = pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker67968392010-07-18 08:28:20 +0000881 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000882#endif
883#if defined(POLARSSL_CERTS_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200884 ret = pk_parse_key( &pkey, (const unsigned char *) test_cli_key,
Paul Bakker67968392010-07-18 08:28:20 +0000885 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000886#else
887 {
888 ret = 1;
889 printf("POLARSSL_CERTS_C not defined.");
890 }
891#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000892 if( ret != 0 )
893 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200894 printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000895 goto exit;
896 }
897
898 printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +0200899#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000900
901 /*
902 * 2. Start the connection
903 */
Manuel Pégourié-Gonnard0d8780b2014-02-25 11:11:26 +0100904 if( opt.server_addr == NULL)
905 opt.server_addr = opt.server_name;
906
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +0100907 printf( " . Connecting to %s/%s/%-4d...",
908 opt.transport == SSL_TRANSPORT_STREAM ? "tcp" : "udp",
909 opt.server_addr, opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +0000910 fflush( stdout );
911
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +0100912 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
913 opt.transport == SSL_TRANSPORT_STREAM ?
914 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000915 {
Paul Bakker0b22e3e2012-04-18 14:23:29 +0000916 printf( " failed\n ! net_connect returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000917 goto exit;
918 }
919
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100920 if( opt.nbio > 0 )
921 ret = net_set_nonblock( server_fd );
922 else
923 ret = net_set_block( server_fd );
924 if( ret != 0 )
925 {
926 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
927 goto exit;
928 }
929
Paul Bakker5121ce52009-01-03 21:22:43 +0000930 printf( " ok\n" );
931
932 /*
933 * 3. Setup stuff
934 */
935 printf( " . Setting up the SSL/TLS structure..." );
936 fflush( stdout );
937
Paul Bakker5121ce52009-01-03 21:22:43 +0000938 if( ( ret = ssl_init( &ssl ) ) != 0 )
939 {
Paul Bakker0b22e3e2012-04-18 14:23:29 +0000940 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000941 goto exit;
942 }
943
Paul Bakker36713e82013-09-17 13:25:29 +0200944#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker915275b2012-09-28 07:10:55 +0000945 if( opt.debug_level > 0 )
946 ssl_set_verify( &ssl, my_verify, NULL );
Paul Bakkered27a042013-04-18 22:46:23 +0200947#endif
Paul Bakker915275b2012-09-28 07:10:55 +0000948
Paul Bakker5121ce52009-01-03 21:22:43 +0000949 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Paul Bakker91ebfb52012-11-23 14:04:08 +0100950 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakker5121ce52009-01-03 21:22:43 +0000951
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200952#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +0100953 if( ( ret = ssl_set_transport( &ssl, opt.transport ) ) != 0 )
954 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +0100955 printf( " failed\n ! selected transport is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +0100956 goto exit;
957 }
958
Manuel Pégourié-Gonnardd823bd02014-10-01 14:40:56 +0200959 if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
960 ssl_set_handshake_timeout( &ssl, opt.hs_to_min, opt.hs_to_max );
961#endif /* POLARSSL_SSL_PROTO_DTLS */
962
Paul Bakker05decb22013-08-15 13:33:48 +0200963#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200964 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
965 {
966 printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
967 goto exit;
968 }
Paul Bakker05decb22013-08-15 13:33:48 +0200969#endif
Manuel Pégourié-Gonnard0df6b1f2013-07-16 13:39:57 +0200970
Paul Bakker1f2bc622013-08-15 13:45:55 +0200971#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200972 if( opt.trunc_hmac != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200973 if( ( ret = ssl_set_truncated_hmac( &ssl, SSL_TRUNC_HMAC_ENABLED ) ) != 0 )
974 {
975 printf( " failed\n ! ssl_set_truncated_hmac returned %d\n\n", ret );
976 goto exit;
977 }
Paul Bakker1f2bc622013-08-15 13:45:55 +0200978#endif
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +0200979
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200980#if defined(POLARSSL_SSL_ALPN)
981 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200982 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
983 {
984 printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
985 goto exit;
986 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200987#endif
988
Paul Bakker508ad5a2011-12-04 17:09:26 +0000989 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker9caf2d22010-02-18 19:37:19 +0000990 ssl_set_dbg( &ssl, my_debug, stdout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100991
992 if( opt.nbio == 2 )
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +0200993 ssl_set_bio_timeout( &ssl, &server_fd, my_send, my_recv, NULL,
994 opt.read_timeout );
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100995 else
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +0200996 ssl_set_bio_timeout( &ssl, &server_fd, net_send, net_recv,
997#if defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnardf0365122014-09-29 16:11:47 +0200998 opt.nbio == 0 ? net_recv_timeout : NULL,
Manuel Pégourié-Gonnarda0148292014-09-18 16:06:04 +0200999#else
1000 NULL,
1001#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001002 opt.read_timeout );
Paul Bakker5121ce52009-01-03 21:22:43 +00001003
Paul Bakkera503a632013-08-14 13:48:06 +02001004#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001005 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1006 {
1007 printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
1008 goto exit;
1009 }
Paul Bakkera503a632013-08-14 13:48:06 +02001010#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001011
Paul Bakker645ce3a2012-10-31 12:32:41 +00001012 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker51936882011-02-20 16:05:58 +00001013 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
1014
Paul Bakker48916f92012-09-16 19:57:18 +00001015 ssl_set_renegotiation( &ssl, opt.renegotiation );
1016 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
1017
Paul Bakker36713e82013-09-17 13:25:29 +02001018#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001019 if( strcmp( opt.ca_path, "none" ) != 0 &&
1020 strcmp( opt.ca_file, "none" ) != 0 )
1021 {
1022 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
1023 }
1024 if( strcmp( opt.crt_file, "none" ) != 0 &&
1025 strcmp( opt.key_file, "none" ) != 0 )
1026 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001027 if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 )
1028 {
1029 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1030 goto exit;
1031 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001032 }
Paul Bakkered27a042013-04-18 22:46:23 +02001033#endif
1034
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001035#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001036 if( ( ret = ssl_set_psk( &ssl, psk, psk_len,
1037 (const unsigned char *) opt.psk_identity,
1038 strlen( opt.psk_identity ) ) ) != 0 )
1039 {
1040 printf( " failed\n ! ssl_set_psk returned %d\n\n", ret );
1041 goto exit;
1042 }
Paul Bakkered27a042013-04-18 22:46:23 +02001043#endif
1044
Paul Bakker0be444a2013-08-27 21:55:01 +02001045#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001046 if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
1047 {
1048 printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret );
1049 goto exit;
1050 }
Paul Bakker0be444a2013-08-27 21:55:01 +02001051#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001052
Paul Bakker1d29fb52012-09-28 13:28:45 +00001053 if( opt.min_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001054 {
1055 ret = ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1056 if( ret != 0 )
1057 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001058 printf( " failed\n ! selected min_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001059 goto exit;
1060 }
1061 }
1062
Paul Bakker1d29fb52012-09-28 13:28:45 +00001063 if( opt.max_version != -1 )
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001064 {
1065 ret = ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1066 if( ret != 0 )
1067 {
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001068 printf( " failed\n ! selected max_version is not available\n" );
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01001069 goto exit;
1070 }
1071 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00001072
Manuel Pégourié-Gonnard798f15a2014-03-26 18:12:04 +01001073 printf( " ok\n" );
1074
Paul Bakker5121ce52009-01-03 21:22:43 +00001075 /*
1076 * 4. Handshake
1077 */
1078 printf( " . Performing the SSL/TLS handshake..." );
1079 fflush( stdout );
1080
1081 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1082 {
Paul Bakker831a7552011-05-18 13:32:51 +00001083 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 {
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +01001085 printf( " failed\n ! ssl_handshake returned -0x%x\n", -ret );
1086 if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
1087 printf(
1088 " Unable to verify the server's certificate. "
1089 "Either it is invalid,\n"
1090 " or you didn't set ca_file or ca_path "
1091 "to an appropriate value.\n"
1092 " Alternatively, you may want to use "
1093 "auth_mode=optional for testing purposes.\n" );
1094 printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001095 goto exit;
1096 }
1097 }
1098
Manuel Pégourié-Gonnardc580a002014-02-12 10:15:30 +01001099 printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1100 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001101
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001102#if defined(POLARSSL_SSL_ALPN)
1103 if( opt.alpn_string != NULL )
1104 {
1105 const char *alp = ssl_get_alpn_protocol( &ssl );
1106 printf( " [ Application Layer Protocol is %s ]\n",
1107 alp ? alp : "(none)" );
1108 }
1109#endif
1110
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001111 if( opt.reconnect != 0 )
1112 {
1113 printf(" . Saving session for reuse..." );
1114 fflush( stdout );
1115
1116 if( ( ret = ssl_get_session( &ssl, &saved_session ) ) != 0 )
1117 {
1118 printf( " failed\n ! ssl_get_session returned -0x%x\n\n", -ret );
1119 goto exit;
1120 }
1121
1122 printf( " ok\n" );
1123 }
1124
Paul Bakker36713e82013-09-17 13:25:29 +02001125#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001126 /*
1127 * 5. Verify the server certificate
1128 */
1129 printf( " . Verifying peer X.509 certificate..." );
1130
1131 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1132 {
1133 printf( " failed\n" );
1134
1135 if( ( ret & BADCERT_EXPIRED ) != 0 )
1136 printf( " ! server certificate has expired\n" );
1137
1138 if( ( ret & BADCERT_REVOKED ) != 0 )
1139 printf( " ! server certificate has been revoked\n" );
1140
1141 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Paul Bakker9caf2d22010-02-18 19:37:19 +00001142 printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00001143
1144 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
1145 printf( " ! self-signed or not signed by a trusted CA\n" );
1146
1147 printf( "\n" );
1148 }
1149 else
1150 printf( " ok\n" );
1151
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001152 if( ssl_get_peer_cert( &ssl ) != NULL )
1153 {
1154 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001155 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1156 ssl_get_peer_cert( &ssl ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001157 printf( "%s\n", buf );
1158 }
Paul Bakker36713e82013-09-17 13:25:29 +02001159#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001160
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001161 if( opt.renegotiate )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001162 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001163 /*
1164 * Perform renegotiation (this must be done when the server is waiting
1165 * for input from our side).
1166 */
1167 printf( " . Performing renegotiation..." );
1168 fflush( stdout );
1169 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001170 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001171 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1172 ret != POLARSSL_ERR_NET_WANT_WRITE )
1173 {
1174 printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
1175 goto exit;
1176 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001177 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001178 printf( " ok\n" );
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001179 }
Manuel Pégourié-Gonnard53b3e062013-10-29 18:16:38 +01001180
Paul Bakker5121ce52009-01-03 21:22:43 +00001181 /*
1182 * 6. Write the GET request
1183 */
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001184send_request:
Paul Bakker5121ce52009-01-03 21:22:43 +00001185 printf( " > Write to server:" );
1186 fflush( stdout );
1187
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001188 len = snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST,
1189 opt.request_page );
1190 tail_len = strlen( GET_REQUEST_END );
1191
1192 /* Add padding to GET request to reach opt.request_size in length */
1193 if( opt.request_size != DFL_REQUEST_SIZE &&
1194 len + tail_len < opt.request_size )
Paul Bakker93c32b22014-04-25 13:40:05 +02001195 {
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001196 memset( buf + len, 'A', opt.request_size - len - tail_len );
1197 len += opt.request_size - len - tail_len;
Paul Bakker93c32b22014-04-25 13:40:05 +02001198 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001199
Manuel Pégourié-Gonnarddcab2932014-08-14 17:47:17 +02001200 strncpy( (char *) buf + len, GET_REQUEST_END, sizeof(buf) - len - 1 );
1201 len += tail_len;
1202
Manuel Pégourié-Gonnarddea29c52014-06-18 13:07:56 +02001203 /* Truncate if request size is smaller than the "natural" size */
1204 if( opt.request_size != DFL_REQUEST_SIZE &&
1205 len > opt.request_size )
1206 {
1207 len = opt.request_size;
1208
1209 /* Still end with \r\n unless that's really not possible */
1210 if( len >= 2 ) buf[len - 2] = '\r';
1211 if( len >= 1 ) buf[len - 1] = '\n';
1212 }
1213
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001214 if( opt.transport == SSL_TRANSPORT_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001216 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001217 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001218 while( ( ret = ssl_write( &ssl, buf + written, len - written ) )
1219 <= 0 )
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001220 {
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001221 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1222 ret != POLARSSL_ERR_NET_WANT_WRITE )
1223 {
1224 printf( " failed\n ! ssl_write returned -0x%x\n\n", -ret );
1225 goto exit;
1226 }
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001227 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001228 }
1229 }
Manuel Pégourié-Gonnard2d87e412014-10-13 18:38:36 +02001230 else /* Not stream, so datagram */
1231 {
1232 do ret = ssl_write( &ssl, buf, len );
1233 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1234 ret == POLARSSL_ERR_NET_WANT_WRITE );
1235
1236 if( ret < 0 )
1237 {
1238 printf( " failed\n ! ssl_write returned %d\n\n", ret );
1239 goto exit;
1240 }
1241
1242 frags = 1;
1243 written = ret;
1244 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
Manuel Pégourié-Gonnard787b6582013-07-16 15:43:17 +02001246 buf[written] = '\0';
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001247 printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001248
1249 /*
1250 * 7. Read the HTTP response
1251 */
1252 printf( " < Read from server:" );
1253 fflush( stdout );
1254
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001255 /*
1256 * TLS and DTLS need different reading styles (stream vs datagram)
1257 */
1258 if( opt.transport == SSL_TRANSPORT_STREAM )
1259 {
1260 do
1261 {
1262 len = sizeof( buf ) - 1;
1263 memset( buf, 0, sizeof( buf ) );
1264 ret = ssl_read( &ssl, buf, len );
1265
1266 if( ret == POLARSSL_ERR_NET_WANT_READ ||
1267 ret == POLARSSL_ERR_NET_WANT_WRITE )
1268 continue;
1269
1270 if( ret <= 0 )
1271 {
1272 switch( ret )
1273 {
1274 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1275 printf( " connection was closed gracefully\n" );
1276 ret = 0;
1277 goto close_notify;
1278
1279 case 0:
1280 case POLARSSL_ERR_NET_CONN_RESET:
1281 printf( " connection was reset by peer\n" );
1282 ret = 0;
1283 goto reconnect;
1284
1285 default:
1286 printf( " ssl_read returned -0x%x\n", -ret );
1287 goto exit;
1288 }
1289 }
1290
1291 len = ret;
1292 buf[len] = '\0';
1293 printf( " %d bytes read\n\n%s", len, (char *) buf );
1294
1295 /* End of message should be detected according to the syntax of the
1296 * application protocol (eg HTTP), just use a dummy test here. */
1297 if( ret > 0 && buf[len-1] == '\n' )
1298 {
1299 ret = 0;
1300 break;
1301 }
1302 }
1303 while( 1 );
1304 }
1305 else /* Not stream, so datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 {
1307 len = sizeof( buf ) - 1;
1308 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001309
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001310 do ret = ssl_read( &ssl, buf, len );
1311 while( ret == POLARSSL_ERR_NET_WANT_READ ||
1312 ret == POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001313
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001314 if( ret <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 {
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001316 switch( ret )
1317 {
Manuel Pégourié-Gonnardf1e0df32014-10-02 14:02:32 +02001318 case POLARSSL_ERR_NET_TIMEOUT:
1319 printf( " timeout\n" );
1320 if( opt.max_resend-- > 0 )
1321 goto send_request;
1322 goto exit;
1323
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001324 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1325 printf( " connection was closed gracefully\n" );
1326 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001327 goto close_notify;
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001329 default:
1330 printf( " ssl_read returned -0x%x\n", -ret );
1331 goto exit;
1332 }
Paul Bakker5690efc2011-05-26 13:16:06 +00001333 }
1334
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 len = ret;
Paul Bakker93c32b22014-04-25 13:40:05 +02001336 buf[len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 printf( " %d bytes read\n\n%s", len, (char *) buf );
Manuel Pégourié-Gonnardae5050c2014-07-11 14:14:15 +02001338 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001340
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001341 /*
Manuel Pégourié-Gonnarda8c0a0d2014-08-15 12:07:38 +02001342 * 7b. Continue doing data exchanges?
1343 */
1344 if( --opt.exchanges > 0 )
1345 goto send_request;
1346
1347 /*
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001348 * 8. Done, cleanly close the connection
1349 */
1350close_notify:
1351 printf( " . Closing the connection..." );
1352
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001353 /* No error checking, the connection might be closed already */
1354 do
1355 ret = ssl_close_notify( &ssl );
1356 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
1357 ret = 0;
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001358
Manuel Pégourié-Gonnard994f8b52014-10-09 19:56:44 +02001359 printf( " done\n" );
Manuel Pégourié-Gonnardf1388742014-08-19 16:14:36 +02001360
1361 /*
1362 * 9. Reconnect?
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001363 */
1364reconnect:
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001365 if( opt.reconnect != 0 )
1366 {
Manuel Pégourié-Gonnardcf2e97e2013-08-02 15:04:36 +02001367 --opt.reconnect;
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001368
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001369 net_close( server_fd );
1370
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001371#if defined(POLARSSL_TIMING_C)
1372 if( opt.reco_delay > 0 )
1373 m_sleep( 1000 * opt.reco_delay );
1374#endif
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02001375
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001376 printf( " . Reconnecting with saved session..." );
1377 fflush( stdout );
1378
1379 if( ( ret = ssl_session_reset( &ssl ) ) != 0 )
1380 {
1381 printf( " failed\n ! ssl_session_reset returned -0x%x\n\n", -ret );
1382 goto exit;
1383 }
1384
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001385 if( ( ret = ssl_set_session( &ssl, &saved_session ) ) != 0 )
1386 {
1387 printf( " failed\n ! ssl_set_session returned %d\n\n", ret );
1388 goto exit;
1389 }
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001390
Manuel Pégourié-Gonnard484b8f92014-09-23 12:36:12 +02001391 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
Manuel Pégourié-Gonnard8a06d9c2014-03-23 18:23:41 +01001392 opt.transport == SSL_TRANSPORT_STREAM ?
1393 NET_PROTO_TCP : NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001394 {
1395 printf( " failed\n ! net_connect returned -0x%x\n\n", -ret );
1396 goto exit;
1397 }
1398
Manuel Pégourié-Gonnard85beb302014-10-02 17:59:19 +02001399 if( opt.nbio > 0 )
1400 ret = net_set_nonblock( server_fd );
1401 else
1402 ret = net_set_block( server_fd );
1403 if( ret != 0 )
1404 {
1405 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
1406 -ret );
1407 goto exit;
1408 }
1409
Manuel Pégourié-Gonnardaaa1eab2013-07-30 13:43:43 +02001410 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1411 {
1412 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1413 ret != POLARSSL_ERR_NET_WANT_WRITE )
1414 {
1415 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
1416 goto exit;
1417 }
1418 }
1419
1420 printf( " ok\n" );
1421
1422 goto send_request;
1423 }
1424
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001425 /*
1426 * Cleanup and exit
1427 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001428exit:
Paul Bakkera3d195c2011-11-27 21:07:34 +00001429#ifdef POLARSSL_ERROR_C
1430 if( ret != 0 )
1431 {
1432 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001433 polarssl_strerror( ret, error_buf, 100 );
Paul Bakker570267f2012-04-10 08:22:46 +00001434 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001435 }
1436#endif
1437
Paul Bakker1a207ec2011-02-06 13:22:40 +00001438 if( server_fd )
1439 net_close( server_fd );
Manuel Pégourié-Gonnarde08660e2014-08-16 11:28:40 +02001440
Paul Bakker36713e82013-09-17 13:25:29 +02001441#if defined(POLARSSL_X509_CRT_PARSE_C)
1442 x509_crt_free( &clicert );
1443 x509_crt_free( &cacert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02001444 pk_free( &pkey );
Paul Bakkered27a042013-04-18 22:46:23 +02001445#endif
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001446 ssl_session_free( &saved_session );
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02001448 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02001449 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +00001450
Paul Bakkercce9d772011-11-18 14:26:47 +00001451#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +00001452 printf( " + Press Enter to exit this program.\n" );
1453 fflush( stdout ); getchar();
1454#endif
1455
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001456 // Shell can not handle large exit numbers -> 1 for errors
1457 if( ret < 0 )
1458 ret = 1;
1459
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 return( ret );
1461}
Paul Bakker508ad5a2011-12-04 17:09:26 +00001462#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
1463 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
1464 POLARSSL_CTR_DRBG_C */