blob: 2a046a77fee9294ad2f58b07688315a30a4097ae [file] [log] [blame]
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001/*
2 * SSL client with options
3 *
Paul Bakker3c5ef712013-06-25 16:37:45 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * 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é-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000027
28#if defined(_WIN32)
29#include <windows.h>
30#endif
31
32#include <string.h>
33#include <stdlib.h>
34#include <stdio.h>
35
Paul Bakkerb60b95f2012-09-25 09:05:17 +000036#include "polarssl/net.h"
37#include "polarssl/ssl.h"
38#include "polarssl/entropy.h"
39#include "polarssl/ctr_drbg.h"
40#include "polarssl/certs.h"
41#include "polarssl/x509.h"
42#include "polarssl/error.h"
43
Paul Bakker0a597072012-09-25 21:55:46 +000044#if defined(POLARSSL_SSL_CACHE_C)
45#include "polarssl/ssl_cache.h"
46#endif
47
Paul Bakker82024bf2013-07-04 11:52:32 +020048#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
49#include "polarssl/memory.h"
50#endif
51
Paul Bakkerb60b95f2012-09-25 09:05:17 +000052#define DFL_SERVER_PORT 4433
Paul Bakkerb60b95f2012-09-25 09:05:17 +000053#define DFL_DEBUG_LEVEL 0
54#define DFL_CA_FILE ""
55#define DFL_CA_PATH ""
56#define DFL_CRT_FILE ""
57#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +020058#define DFL_CRT_FILE2 ""
59#define DFL_KEY_FILE2 ""
Paul Bakkerfbb17802013-04-17 19:10:21 +020060#define DFL_PSK ""
61#define DFL_PSK_IDENTITY "Client_identity"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000062#define DFL_FORCE_CIPHER 0
63#define DFL_RENEGOTIATION SSL_RENEGOTIATION_ENABLED
64#define DFL_ALLOW_LEGACY SSL_LEGACY_NO_RENEGOTIATION
Paul Bakker1d29fb52012-09-28 13:28:45 +000065#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +020066#define DFL_MAX_VERSION -1
Paul Bakker91ebfb52012-11-23 14:04:08 +010067#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +020068#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +020069#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Paul Bakkerb60b95f2012-09-25 09:05:17 +000070
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +020071#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
72 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
73 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
74 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
75 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
76 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
77 "07-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah</p>\r\n"
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +020078
Paul Bakker8e714d72013-07-18 11:05:13 +020079/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
80 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +000081#define HTTP_RESPONSE \
82 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
83 "<h2>PolarSSL Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +020084 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +000085
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010086/* Uncomment to test server-initiated renegotiation */
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +010087// #define TEST_RENEGO
88
Paul Bakkerb60b95f2012-09-25 09:05:17 +000089/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +000090 * global options
91 */
92struct options
93{
94 int server_port; /* port on which the ssl service runs */
95 int debug_level; /* level of debugging */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020096 const char *ca_file; /* the file with the CA certificate(s) */
97 const char *ca_path; /* the path with the CA certificate(s) reside */
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +020098 const char *crt_file; /* the file with the server certificate */
99 const char *key_file; /* the file with the server key */
100 const char *crt_file2; /* the file with the 2nd server certificate */
101 const char *key_file2; /* the file with the 2nd server key */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200102 const char *psk; /* the pre-shared key */
103 const char *psk_identity; /* the pre-shared key identity */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000104 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
105 int renegotiation; /* enable / disable renegotiation */
106 int allow_legacy; /* allow legacy renegotiation */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000107 int min_version; /* minimum protocol version accepted */
Paul Bakkerc1516be2013-06-29 16:01:32 +0200108 int max_version; /* maximum protocol version accepted */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100109 int auth_mode; /* verify mode for connection */
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200110 unsigned char mfl_code; /* code for maximum fragment length */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200111 int tickets; /* enable / disable session tickets */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000112} opt;
113
Paul Bakker3c5ef712013-06-25 16:37:45 +0200114static void my_debug( void *ctx, int level, const char *str )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000115{
116 if( level < opt.debug_level )
117 {
118 fprintf( (FILE *) ctx, "%s", str );
119 fflush( (FILE *) ctx );
120 }
121}
122
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200123
124#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000125#if defined(POLARSSL_FS_IO)
126#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100127 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
128 " default: \"\" (pre-loaded)\n" \
129 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
130 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
131 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200132 " default: see note after key_file2\n" \
133 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200134 " crt_file2=%%s Your second cert and chain (in bottom to top order, top may be omitted)\n" \
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200135 " default: see note after key_file2\n" \
136 " key_file2=%%s default: see note below\n" \
137 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
138 " preloaded certificate(s) and key(s) are used if available\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000139#else
140#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200141 "\n" \
142 " No file operations available (POLARSSL_FS_IO not defined)\n" \
143 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000144#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200145#else
146#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200147#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200148
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200149#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200150#define USAGE_PSK \
151 " psk=%%s default: \"\" (in hex, without 0x)\n" \
152 " psk_identity=%%s default: \"Client_identity\"\n"
153#else
154#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200155#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000156
Paul Bakkera503a632013-08-14 13:48:06 +0200157#if defined(POLARSSL_SSL_SESSION_TICKETS)
158#define USAGE_TICKETS \
159 " tickets=%%d default: 1 (enabled)\n"
160#else
161#define USAGE_TICKETS ""
162#endif /* POLARSSL_SSL_SESSION_TICKETS */
163
Paul Bakker05decb22013-08-15 13:33:48 +0200164#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
165#define USAGE_MAX_FRAG_LEN \
166 " max_frag_len=%%d default: 16384 (tls default)\n" \
167 " options: 512, 1024, 2048, 4096\n"
168#else
169#define USAGE_MAX_FRAG_LEN ""
170#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
171
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000172#define USAGE \
173 "\n usage: ssl_server2 param=<>...\n" \
174 "\n acceptable parameters:\n" \
175 " server_port=%%d default: 4433\n" \
176 " debug_level=%%d default: 0 (disabled)\n" \
177 USAGE_IO \
178 " request_page=%%s default: \".\"\n" \
179 " renegotiation=%%d default: 1 (enabled)\n" \
Paul Bakkera503a632013-08-14 13:48:06 +0200180 USAGE_TICKETS \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000181 " allow_legacy=%%d default: 0 (disabled)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000182 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200183 " max_version=%%s default: \"tls1_2\"\n" \
184 " force_version=%%s default: \"\" (none)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000185 " options: ssl3, tls1, tls1_1, tls1_2\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200186 " auth_mode=%%s default: \"optional\"\n" \
Paul Bakker91ebfb52012-11-23 14:04:08 +0100187 " options: none, optional, required\n" \
Paul Bakker05decb22013-08-15 13:33:48 +0200188 USAGE_MAX_FRAG_LEN \
Paul Bakkered27a042013-04-18 22:46:23 +0200189 USAGE_PSK \
Paul Bakkerfbb17802013-04-17 19:10:21 +0200190 "\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000191 " force_ciphersuite=<name> default: all enabled\n"\
192 " acceptable ciphersuite names:\n"
193
Paul Bakkered27a042013-04-18 22:46:23 +0200194#if !defined(POLARSSL_ENTROPY_C) || \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000195 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
Paul Bakkered27a042013-04-18 22:46:23 +0200196 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000197int main( int argc, char *argv[] )
198{
199 ((void) argc);
200 ((void) argv);
201
Paul Bakkered27a042013-04-18 22:46:23 +0200202 printf("POLARSSL_ENTROPY_C and/or "
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000203 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
Paul Bakkered27a042013-04-18 22:46:23 +0200204 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000205 return( 0 );
206}
207#else
208int main( int argc, char *argv[] )
209{
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200210 int ret = 0, len, written, frags;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000211 int listen_fd;
212 int client_fd = -1;
213 unsigned char buf[1024];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200214#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerfbb17802013-04-17 19:10:21 +0200215 unsigned char psk[256];
216 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200217#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200218 const char *pers = "ssl_server2";
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000219
220 entropy_context entropy;
221 ctr_drbg_context ctr_drbg;
222 ssl_context ssl;
Paul Bakker36713e82013-09-17 13:25:29 +0200223#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200224 x509_crt cacert;
225 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200226 pk_context pkey;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200227 x509_crt srvcert2;
228 pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200229 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200230#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000231#if defined(POLARSSL_SSL_CACHE_C)
232 ssl_cache_context cache;
233#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200234#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
235 unsigned char alloc_buf[100000];
236#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000237
238 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000239 char *p, *q;
240 const int *list;
241
Paul Bakker82024bf2013-07-04 11:52:32 +0200242#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
243 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
244#endif
245
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000246 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200247 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000248 */
249 listen_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200250 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200251#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200252 x509_crt_init( &cacert );
253 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200254 pk_init( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200255 x509_crt_init( &srvcert2 );
256 pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200257#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000258#if defined(POLARSSL_SSL_CACHE_C)
259 ssl_cache_init( &cache );
260#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000261
262 if( argc == 0 )
263 {
264 usage:
265 if( ret == 0 )
266 ret = 1;
267
268 printf( USAGE );
269
270 list = ssl_list_ciphersuites();
271 while( *list )
272 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200273 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200274 list++;
275 if( !*list )
276 break;
277 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000278 list++;
279 }
280 printf("\n");
281 goto exit;
282 }
283
284 opt.server_port = DFL_SERVER_PORT;
285 opt.debug_level = DFL_DEBUG_LEVEL;
286 opt.ca_file = DFL_CA_FILE;
287 opt.ca_path = DFL_CA_PATH;
288 opt.crt_file = DFL_CRT_FILE;
289 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200290 opt.crt_file2 = DFL_CRT_FILE2;
291 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200292 opt.psk = DFL_PSK;
293 opt.psk_identity = DFL_PSK_IDENTITY;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000294 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
295 opt.renegotiation = DFL_RENEGOTIATION;
296 opt.allow_legacy = DFL_ALLOW_LEGACY;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000297 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200298 opt.max_version = DFL_MAX_VERSION;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100299 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200300 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200301 opt.tickets = DFL_TICKETS;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000302
303 for( i = 1; i < argc; i++ )
304 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000305 p = argv[i];
306 if( ( q = strchr( p, '=' ) ) == NULL )
307 goto usage;
308 *q++ = '\0';
309
310 if( strcmp( p, "server_port" ) == 0 )
311 {
312 opt.server_port = atoi( q );
313 if( opt.server_port < 1 || opt.server_port > 65535 )
314 goto usage;
315 }
316 else if( strcmp( p, "debug_level" ) == 0 )
317 {
318 opt.debug_level = atoi( q );
319 if( opt.debug_level < 0 || opt.debug_level > 65535 )
320 goto usage;
321 }
322 else if( strcmp( p, "ca_file" ) == 0 )
323 opt.ca_file = q;
324 else if( strcmp( p, "ca_path" ) == 0 )
325 opt.ca_path = q;
326 else if( strcmp( p, "crt_file" ) == 0 )
327 opt.crt_file = q;
328 else if( strcmp( p, "key_file" ) == 0 )
329 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200330 else if( strcmp( p, "crt_file2" ) == 0 )
331 opt.crt_file2 = q;
332 else if( strcmp( p, "key_file2" ) == 0 )
333 opt.key_file2 = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200334 else if( strcmp( p, "psk" ) == 0 )
335 opt.psk = q;
336 else if( strcmp( p, "psk_identity" ) == 0 )
337 opt.psk_identity = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000338 else if( strcmp( p, "force_ciphersuite" ) == 0 )
339 {
340 opt.force_ciphersuite[0] = -1;
341
342 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
343
344 if( opt.force_ciphersuite[0] <= 0 )
345 {
346 ret = 2;
347 goto usage;
348 }
349 opt.force_ciphersuite[1] = 0;
350 }
351 else if( strcmp( p, "renegotiation" ) == 0 )
352 {
353 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
354 SSL_RENEGOTIATION_DISABLED;
355 }
356 else if( strcmp( p, "allow_legacy" ) == 0 )
357 {
358 opt.allow_legacy = atoi( q );
359 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
360 goto usage;
361 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000362 else if( strcmp( p, "min_version" ) == 0 )
363 {
364 if( strcmp( q, "ssl3" ) == 0 )
365 opt.min_version = SSL_MINOR_VERSION_0;
366 else if( strcmp( q, "tls1" ) == 0 )
367 opt.min_version = SSL_MINOR_VERSION_1;
368 else if( strcmp( q, "tls1_1" ) == 0 )
369 opt.min_version = SSL_MINOR_VERSION_2;
370 else if( strcmp( q, "tls1_2" ) == 0 )
371 opt.min_version = SSL_MINOR_VERSION_3;
372 else
373 goto usage;
374 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200375 else if( strcmp( p, "max_version" ) == 0 )
376 {
377 if( strcmp( q, "ssl3" ) == 0 )
378 opt.max_version = SSL_MINOR_VERSION_0;
379 else if( strcmp( q, "tls1" ) == 0 )
380 opt.max_version = SSL_MINOR_VERSION_1;
381 else if( strcmp( q, "tls1_1" ) == 0 )
382 opt.max_version = SSL_MINOR_VERSION_2;
383 else if( strcmp( q, "tls1_2" ) == 0 )
384 opt.max_version = SSL_MINOR_VERSION_3;
385 else
386 goto usage;
387 }
388 else if( strcmp( p, "force_version" ) == 0 )
389 {
390 if( strcmp( q, "ssl3" ) == 0 )
391 {
392 opt.min_version = SSL_MINOR_VERSION_0;
393 opt.max_version = SSL_MINOR_VERSION_0;
394 }
395 else if( strcmp( q, "tls1" ) == 0 )
396 {
397 opt.min_version = SSL_MINOR_VERSION_1;
398 opt.max_version = SSL_MINOR_VERSION_1;
399 }
400 else if( strcmp( q, "tls1_1" ) == 0 )
401 {
402 opt.min_version = SSL_MINOR_VERSION_2;
403 opt.max_version = SSL_MINOR_VERSION_2;
404 }
405 else if( strcmp( q, "tls1_2" ) == 0 )
406 {
407 opt.min_version = SSL_MINOR_VERSION_3;
408 opt.max_version = SSL_MINOR_VERSION_3;
409 }
410 else
411 goto usage;
412 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100413 else if( strcmp( p, "auth_mode" ) == 0 )
414 {
415 if( strcmp( q, "none" ) == 0 )
416 opt.auth_mode = SSL_VERIFY_NONE;
417 else if( strcmp( q, "optional" ) == 0 )
418 opt.auth_mode = SSL_VERIFY_OPTIONAL;
419 else if( strcmp( q, "required" ) == 0 )
420 opt.auth_mode = SSL_VERIFY_REQUIRED;
421 else
422 goto usage;
423 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200424 else if( strcmp( p, "max_frag_len" ) == 0 )
425 {
426 if( strcmp( q, "512" ) == 0 )
427 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
428 else if( strcmp( q, "1024" ) == 0 )
429 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
430 else if( strcmp( q, "2048" ) == 0 )
431 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
432 else if( strcmp( q, "4096" ) == 0 )
433 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
434 else
435 goto usage;
436 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200437 else if( strcmp( p, "tickets" ) == 0 )
438 {
439 opt.tickets = atoi( q );
440 if( opt.tickets < 0 || opt.tickets > 1 )
441 goto usage;
442 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000443 else
444 goto usage;
445 }
446
Paul Bakkerc1516be2013-06-29 16:01:32 +0200447 if( opt.force_ciphersuite[0] > 0 )
448 {
449 const ssl_ciphersuite_t *ciphersuite_info;
450 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
451
Paul Bakker5b55b792013-07-19 13:43:43 +0200452 if( opt.max_version != -1 &&
453 ciphersuite_info->min_minor_ver > opt.max_version )
454 {
455 printf("forced ciphersuite not allowed with this protocol version\n");
456 ret = 2;
457 goto usage;
458 }
459 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200460 ciphersuite_info->max_minor_ver < opt.min_version )
461 {
462 printf("forced ciphersuite not allowed with this protocol version\n");
463 ret = 2;
464 goto usage;
465 }
Paul Bakker5b55b792013-07-19 13:43:43 +0200466 if( opt.max_version > ciphersuite_info->max_minor_ver )
467 opt.max_version = ciphersuite_info->max_minor_ver;
468 if( opt.min_version < ciphersuite_info->min_minor_ver )
469 opt.min_version = ciphersuite_info->min_minor_ver;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200470 }
471
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200472#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000473 /*
Paul Bakkerfbb17802013-04-17 19:10:21 +0200474 * Unhexify the pre-shared key if any is given
475 */
476 if( strlen( opt.psk ) )
477 {
478 unsigned char c;
479 size_t j;
480
481 if( strlen( opt.psk ) % 2 != 0 )
482 {
483 printf("pre-shared key not valid hex\n");
484 goto exit;
485 }
486
487 psk_len = strlen( opt.psk ) / 2;
488
489 for( j = 0; j < strlen( opt.psk ); j += 2 )
490 {
491 c = opt.psk[j];
492 if( c >= '0' && c <= '9' )
493 c -= '0';
494 else if( c >= 'a' && c <= 'f' )
495 c -= 'a' - 10;
496 else if( c >= 'A' && c <= 'F' )
497 c -= 'A' - 10;
498 else
499 {
500 printf("pre-shared key not valid hex\n");
501 goto exit;
502 }
503 psk[ j / 2 ] = c << 4;
504
505 c = opt.psk[j + 1];
506 if( c >= '0' && c <= '9' )
507 c -= '0';
508 else if( c >= 'a' && c <= 'f' )
509 c -= 'a' - 10;
510 else if( c >= 'A' && c <= 'F' )
511 c -= 'A' - 10;
512 else
513 {
514 printf("pre-shared key not valid hex\n");
515 goto exit;
516 }
517 psk[ j / 2 ] |= c;
518 }
519 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200520#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +0200521
522 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000523 * 0. Initialize the RNG and the session data
524 */
525 printf( "\n . Seeding the random number generator..." );
526 fflush( stdout );
527
528 entropy_init( &entropy );
529 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200530 (const unsigned char *) pers,
531 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000532 {
533 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
534 goto exit;
535 }
536
537 printf( " ok\n" );
538
Paul Bakker36713e82013-09-17 13:25:29 +0200539#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000540 /*
541 * 1.1. Load the trusted CA
542 */
543 printf( " . Loading the CA root certificate ..." );
544 fflush( stdout );
545
546#if defined(POLARSSL_FS_IO)
547 if( strlen( opt.ca_path ) )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200548 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000549 else if( strlen( opt.ca_file ) )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200550 ret = x509_crt_parse_file( &cacert, opt.ca_file );
551 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000552#endif
553#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200554 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
555 strlen( test_ca_list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000556#else
557 {
558 ret = 1;
559 printf("POLARSSL_CERTS_C not defined.");
560 }
561#endif
562 if( ret < 0 )
563 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200564 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000565 goto exit;
566 }
567
568 printf( " ok (%d skipped)\n", ret );
569
570 /*
571 * 1.2. Load own certificate and private key
572 */
573 printf( " . Loading the server cert. and key..." );
574 fflush( stdout );
575
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +0200576#if defined(POLARSSL_FS_IO)
577 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +0200578 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200579 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200580 if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
581 {
582 printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n",
583 -ret );
584 goto exit;
585 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +0200586 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +0200587 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +0200588 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200589 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200590 if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
591 {
592 printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret );
593 goto exit;
594 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +0200595 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200596 if( key_cert_init == 1 )
597 {
598 printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
599 goto exit;
600 }
601
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +0200602 if( strlen( opt.crt_file2 ) )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +0200603 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200604 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200605 if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
606 {
607 printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n",
608 -ret );
609 goto exit;
610 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +0200611 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +0200612 if( strlen( opt.key_file2 ) )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +0200613 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200614 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200615 if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
616 {
617 printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n",
618 -ret );
619 goto exit;
620 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +0200621 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200622 if( key_cert_init2 == 1 )
623 {
624 printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
625 goto exit;
626 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +0200627#endif
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200628 if( key_cert_init == 0 && key_cert_init2 == 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +0200629 {
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200630#if !defined(POLARSSL_CERTS_C)
631 printf( "Not certificated or key provided, and \n"
632 "POLARSSL_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +0200633 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200634#else
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200635#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200636 if( ( ret = x509_crt_parse( &srvcert,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200637 (const unsigned char *) test_srv_crt_rsa,
638 strlen( test_srv_crt_rsa ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200639 {
640 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
641 goto exit;
642 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200643 if( ( ret = pk_parse_key( &pkey,
644 (const unsigned char *) test_srv_key_rsa,
645 strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200646 {
647 printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
648 goto exit;
649 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200650 key_cert_init = 2;
651#endif /* POLARSSL_RSA_C */
652#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200653 if( ( ret = x509_crt_parse( &srvcert2,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200654 (const unsigned char *) test_srv_crt_ec,
655 strlen( test_srv_crt_ec ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200656 {
657 printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
658 goto exit;
659 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200660 if( ( ret = pk_parse_key( &pkey2,
661 (const unsigned char *) test_srv_key_ec,
662 strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200663 {
664 printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
665 goto exit;
666 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200667 key_cert_init2 = 2;
668#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200669#endif /* POLARSSL_CERTS_C */
670 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000671
672 printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +0200673#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000674
675 /*
676 * 2. Setup the listening TCP socket
677 */
678 printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
679 fflush( stdout );
680
681 if( ( ret = net_bind( &listen_fd, NULL, opt.server_port ) ) != 0 )
682 {
683 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
684 goto exit;
685 }
686
687 printf( " ok\n" );
688
689 /*
690 * 3. Setup stuff
691 */
692 printf( " . Setting up the SSL/TLS structure..." );
693 fflush( stdout );
694
695 if( ( ret = ssl_init( &ssl ) ) != 0 )
696 {
697 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
698 goto exit;
699 }
700
701 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +0100702 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000703
Paul Bakker05decb22013-08-15 13:33:48 +0200704#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200705 ssl_set_max_frag_len( &ssl, opt.mfl_code );
Paul Bakker05decb22013-08-15 13:33:48 +0200706#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200707
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000708 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
709 ssl_set_dbg( &ssl, my_debug, stdout );
710
Paul Bakker0a597072012-09-25 21:55:46 +0000711#if defined(POLARSSL_SSL_CACHE_C)
712 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
713 ssl_cache_set, &cache );
714#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000715
Paul Bakkera503a632013-08-14 13:48:06 +0200716#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200717 ssl_set_session_tickets( &ssl, opt.tickets );
Paul Bakkera503a632013-08-14 13:48:06 +0200718#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200719
Paul Bakker41c83d32013-03-20 14:39:14 +0100720 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000721 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
722
723 ssl_set_renegotiation( &ssl, opt.renegotiation );
724 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
725
Paul Bakker36713e82013-09-17 13:25:29 +0200726#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000727 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200728 if( key_cert_init )
729 ssl_set_own_cert( &ssl, &srvcert, &pkey );
730 if( key_cert_init2 )
731 ssl_set_own_cert( &ssl, &srvcert2, &pkey2 );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +0200732#endif
Paul Bakkered27a042013-04-18 22:46:23 +0200733
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200734#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker3c5ef712013-06-25 16:37:45 +0200735 ssl_set_psk( &ssl, psk, psk_len, (const unsigned char *) opt.psk_identity,
Paul Bakkerfbb17802013-04-17 19:10:21 +0200736 strlen( opt.psk_identity ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200737#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000738
739#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +0000740 /*
741 * Use different group than default DHM group
742 */
Paul Bakkerd4324102012-09-26 08:29:38 +0000743 ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
744 POLARSSL_DHM_RFC5114_MODP_2048_G );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000745#endif
746
Paul Bakker1d29fb52012-09-28 13:28:45 +0000747 if( opt.min_version != -1 )
748 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
749
Paul Bakkerc1516be2013-06-29 16:01:32 +0200750 if( opt.max_version != -1 )
751 ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
752
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000753 printf( " ok\n" );
754
755reset:
756#ifdef POLARSSL_ERROR_C
757 if( ret != 0 )
758 {
759 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200760 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000761 printf("Last error was: %d - %s\n\n", ret, error_buf );
762 }
763#endif
764
765 if( client_fd != -1 )
766 net_close( client_fd );
767
768 ssl_session_reset( &ssl );
769
770 /*
771 * 3. Wait until a client connects
772 */
773#if defined(_WIN32_WCE)
774 {
775 SHELLEXECUTEINFO sei;
776
777 ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) );
778
779 sei.cbSize = sizeof( SHELLEXECUTEINFO );
780 sei.fMask = 0;
781 sei.hwnd = 0;
782 sei.lpVerb = _T( "open" );
783 sei.lpFile = _T( "https://localhost:4433/" );
784 sei.lpParameters = NULL;
785 sei.lpDirectory = NULL;
786 sei.nShow = SW_SHOWNORMAL;
787
788 ShellExecuteEx( &sei );
789 }
790#elif defined(_WIN32)
791 ShellExecute( NULL, "open", "https://localhost:4433/",
792 NULL, NULL, SW_SHOWNORMAL );
793#endif
794
795 client_fd = -1;
796
797 printf( " . Waiting for a remote connection ..." );
798 fflush( stdout );
799
800 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
801 {
802 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
803 goto exit;
804 }
805
806 ssl_set_bio( &ssl, net_recv, &client_fd,
807 net_send, &client_fd );
808
809 printf( " ok\n" );
810
811 /*
812 * 4. Handshake
813 */
814 printf( " . Performing the SSL/TLS handshake..." );
815 fflush( stdout );
816
817 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
818 {
819 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
820 {
821 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000822 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000823 }
824 }
825
826 printf( " ok\n [ Ciphersuite is %s ]\n",
827 ssl_get_ciphersuite( &ssl ) );
828
Paul Bakker36713e82013-09-17 13:25:29 +0200829#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000830 /*
831 * 5. Verify the server certificate
832 */
833 printf( " . Verifying peer X.509 certificate..." );
834
835 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
836 {
837 printf( " failed\n" );
838
Paul Bakkerb0550d92012-10-30 07:51:03 +0000839 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000840 printf( " ! no client certificate sent\n" );
841
842 if( ( ret & BADCERT_EXPIRED ) != 0 )
843 printf( " ! client certificate has expired\n" );
844
845 if( ( ret & BADCERT_REVOKED ) != 0 )
846 printf( " ! client certificate has been revoked\n" );
847
848 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
849 printf( " ! self-signed or not signed by a trusted CA\n" );
850
851 printf( "\n" );
852 }
853 else
854 printf( " ok\n" );
855
Paul Bakkerb0550d92012-10-30 07:51:03 +0000856 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000857 {
858 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200859 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
860 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000861 printf( "%s\n", buf );
862 }
Paul Bakker36713e82013-09-17 13:25:29 +0200863#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000864
865 /*
866 * 6. Read the HTTP Request
867 */
868 printf( " < Read from client:" );
869 fflush( stdout );
870
871 do
872 {
873 len = sizeof( buf ) - 1;
874 memset( buf, 0, sizeof( buf ) );
875 ret = ssl_read( &ssl, buf, len );
876
877 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
878 continue;
879
880 if( ret <= 0 )
881 {
882 switch( ret )
883 {
884 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
885 printf( " connection was closed gracefully\n" );
886 break;
887
888 case POLARSSL_ERR_NET_CONN_RESET:
889 printf( " connection was reset by peer\n" );
890 break;
891
892 default:
893 printf( " ssl_read returned -0x%x\n", -ret );
894 break;
895 }
896
897 break;
898 }
899
900 len = ret;
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200901 printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000902
Paul Bakker82024bf2013-07-04 11:52:32 +0200903 if( memcmp( buf, "SERVERQUIT", 10 ) == 0 )
Manuel Pégourié-Gonnarde8ea0c02013-09-07 17:09:14 +0200904 {
905 ret = 0;
Paul Bakker82024bf2013-07-04 11:52:32 +0200906 goto exit;
Manuel Pégourié-Gonnarde8ea0c02013-09-07 17:09:14 +0200907 }
Paul Bakker82024bf2013-07-04 11:52:32 +0200908
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000909 if( ret > 0 )
910 break;
911 }
912 while( 1 );
913
914 /*
915 * 7. Write the 200 Response
916 */
917 printf( " > Write to client:" );
918 fflush( stdout );
919
920 len = sprintf( (char *) buf, HTTP_RESPONSE,
921 ssl_get_ciphersuite( &ssl ) );
922
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200923 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000924 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200925 while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000926 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200927 if( ret == POLARSSL_ERR_NET_CONN_RESET )
928 {
929 printf( " failed\n ! peer closed the connection\n\n" );
930 goto reset;
931 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000932
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200933 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
934 {
935 printf( " failed\n ! ssl_write returned %d\n\n", ret );
936 goto exit;
937 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000938 }
939 }
940
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200941 buf[written] = '\0';
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200942 printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000943
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +0100944#ifdef TEST_RENEGO
945 /*
946 * Request renegotiation (this must be done when the client is still
947 * waiting for input from our side).
948 */
949 printf( " . Requestion renegotiation..." );
950 fflush( stdout );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +0100951 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +0100952 {
953 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
954 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +0100955 printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +0100956 goto exit;
957 }
958 }
959
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +0100960 /*
961 * Should be a while loop, not an if, but here we're not actually
962 * expecting data from the client, and since we're running tests locally,
963 * we can just hope the handshake will finish the during the first call.
964 */
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +0100965 if( ( ret = ssl_read( &ssl, buf, 0 ) ) != 0 )
966 {
967 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
968 {
969 printf( " failed\n ! ssl_read returned %d\n\n", ret );
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +0100970
971 /* Unexpected message probably means client didn't renegotiate */
972 if( ret == POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE )
973 goto reset;
974 else
975 goto exit;
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +0100976 }
977 }
978
979 printf( " ok\n" );
980#endif
981
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000982 ret = 0;
983 goto reset;
984
985exit:
986
987#ifdef POLARSSL_ERROR_C
988 if( ret != 0 )
989 {
990 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200991 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000992 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
993 }
994#endif
995
996 net_close( client_fd );
Paul Bakker36713e82013-09-17 13:25:29 +0200997#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker36713e82013-09-17 13:25:29 +0200998 x509_crt_free( &cacert );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200999 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02001000 pk_free( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001001 x509_crt_free( &srvcert2 );
1002 pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02001003#endif
1004
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001005 ssl_free( &ssl );
Paul Bakker1ffefac2013-09-28 15:23:03 +02001006 entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001007
Paul Bakker0a597072012-09-25 21:55:46 +00001008#if defined(POLARSSL_SSL_CACHE_C)
1009 ssl_cache_free( &cache );
1010#endif
1011
Paul Bakker1337aff2013-09-29 14:45:34 +02001012#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1013#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker82024bf2013-07-04 11:52:32 +02001014 memory_buffer_alloc_status();
1015#endif
Paul Bakker1337aff2013-09-29 14:45:34 +02001016 memory_buffer_alloc_free();
1017#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02001018
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001019#if defined(_WIN32)
1020 printf( " + Press Enter to exit this program.\n" );
1021 fflush( stdout ); getchar();
1022#endif
1023
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001024 // Shell can not handle large exit numbers -> 1 for errors
1025 if( ret < 0 )
1026 ret = 1;
1027
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001028 return( ret );
1029}
1030#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
1031 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
1032 POLARSSL_CTR_DRBG_C */