blob: ab2a87f7a529b7a200dc9fc0dee63ee1b2098869 [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
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#if defined(_WIN32)
31#include <windows.h>
32#endif
33
34#include <string.h>
35#include <stdlib.h>
36#include <stdio.h>
37
38#include "polarssl/config.h"
39
40#include "polarssl/net.h"
41#include "polarssl/ssl.h"
42#include "polarssl/entropy.h"
43#include "polarssl/ctr_drbg.h"
44#include "polarssl/certs.h"
45#include "polarssl/x509.h"
46#include "polarssl/error.h"
47
Paul Bakker0a597072012-09-25 21:55:46 +000048#if defined(POLARSSL_SSL_CACHE_C)
49#include "polarssl/ssl_cache.h"
50#endif
51
Paul Bakker82024bf2013-07-04 11:52:32 +020052#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
53#include "polarssl/memory.h"
54#endif
55
Paul Bakkerb60b95f2012-09-25 09:05:17 +000056#define DFL_SERVER_PORT 4433
57#define DFL_REQUEST_PAGE "/"
58#define DFL_DEBUG_LEVEL 0
59#define DFL_CA_FILE ""
60#define DFL_CA_PATH ""
61#define DFL_CRT_FILE ""
62#define DFL_KEY_FILE ""
Paul Bakkerfbb17802013-04-17 19:10:21 +020063#define DFL_PSK ""
64#define DFL_PSK_IDENTITY "Client_identity"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000065#define DFL_FORCE_CIPHER 0
66#define DFL_RENEGOTIATION SSL_RENEGOTIATION_ENABLED
67#define DFL_ALLOW_LEGACY SSL_LEGACY_NO_RENEGOTIATION
Paul Bakker1d29fb52012-09-28 13:28:45 +000068#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +020069#define DFL_MAX_VERSION -1
Paul Bakker91ebfb52012-11-23 14:04:08 +010070#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000071
Paul Bakker8e714d72013-07-18 11:05:13 +020072#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
73 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
74 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
75 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah</p>\r\n"
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +020076
Paul Bakker8e714d72013-07-18 11:05:13 +020077/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
78 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +000079#define HTTP_RESPONSE \
80 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
81 "<h2>PolarSSL Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +020082 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +000083
84/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +000085 * global options
86 */
87struct options
88{
89 int server_port; /* port on which the ssl service runs */
90 int debug_level; /* level of debugging */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020091 const char *ca_file; /* the file with the CA certificate(s) */
92 const char *ca_path; /* the path with the CA certificate(s) reside */
93 const char *crt_file; /* the file with the client certificate */
94 const char *key_file; /* the file with the client key */
95 const char *psk; /* the pre-shared key */
96 const char *psk_identity; /* the pre-shared key identity */
Paul Bakkerb60b95f2012-09-25 09:05:17 +000097 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
98 int renegotiation; /* enable / disable renegotiation */
99 int allow_legacy; /* allow legacy renegotiation */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000100 int min_version; /* minimum protocol version accepted */
Paul Bakkerc1516be2013-06-29 16:01:32 +0200101 int max_version; /* maximum protocol version accepted */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100102 int auth_mode; /* verify mode for connection */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000103} opt;
104
Paul Bakker3c5ef712013-06-25 16:37:45 +0200105static void my_debug( void *ctx, int level, const char *str )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000106{
107 if( level < opt.debug_level )
108 {
109 fprintf( (FILE *) ctx, "%s", str );
110 fflush( (FILE *) ctx );
111 }
112}
113
Paul Bakkered27a042013-04-18 22:46:23 +0200114#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000115#if defined(POLARSSL_FS_IO)
116#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100117 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
118 " default: \"\" (pre-loaded)\n" \
119 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
120 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
121 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
122 " default: \"\" (pre-loaded)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000123 " key_file=%%s default: \"\" (pre-loaded)\n"
124#else
125#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200126 "\n" \
127 " No file operations available (POLARSSL_FS_IO not defined)\n" \
128 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000129#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200130#else
131#define USAGE_IO ""
132#endif /* POLARSSL_X509_PARSE_C */
133
134#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
135#define USAGE_PSK \
136 " psk=%%s default: \"\" (in hex, without 0x)\n" \
137 " psk_identity=%%s default: \"Client_identity\"\n"
138#else
139#define USAGE_PSK ""
140#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000141
142#define USAGE \
143 "\n usage: ssl_server2 param=<>...\n" \
144 "\n acceptable parameters:\n" \
145 " server_port=%%d default: 4433\n" \
146 " debug_level=%%d default: 0 (disabled)\n" \
147 USAGE_IO \
148 " request_page=%%s default: \".\"\n" \
149 " renegotiation=%%d default: 1 (enabled)\n" \
150 " allow_legacy=%%d default: 0 (disabled)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000151 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200152 " max_version=%%s default: \"tls1_2\"\n" \
153 " force_version=%%s default: \"\" (none)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000154 " options: ssl3, tls1, tls1_1, tls1_2\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200155 " auth_mode=%%s default: \"optional\"\n" \
Paul Bakker91ebfb52012-11-23 14:04:08 +0100156 " options: none, optional, required\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200157 USAGE_PSK \
Paul Bakkerfbb17802013-04-17 19:10:21 +0200158 "\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000159 " force_ciphersuite=<name> default: all enabled\n"\
160 " acceptable ciphersuite names:\n"
161
Paul Bakkered27a042013-04-18 22:46:23 +0200162#if !defined(POLARSSL_ENTROPY_C) || \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000163 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
Paul Bakkered27a042013-04-18 22:46:23 +0200164 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000165int main( int argc, char *argv[] )
166{
167 ((void) argc);
168 ((void) argv);
169
Paul Bakkered27a042013-04-18 22:46:23 +0200170 printf("POLARSSL_ENTROPY_C and/or "
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000171 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
Paul Bakkered27a042013-04-18 22:46:23 +0200172 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000173 return( 0 );
174}
175#else
176int main( int argc, char *argv[] )
177{
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200178 int ret = 0, len, written;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000179 int listen_fd;
180 int client_fd = -1;
181 unsigned char buf[1024];
Paul Bakkered27a042013-04-18 22:46:23 +0200182#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Paul Bakkerfbb17802013-04-17 19:10:21 +0200183 unsigned char psk[256];
184 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200185#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200186 const char *pers = "ssl_server2";
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000187
188 entropy_context entropy;
189 ctr_drbg_context ctr_drbg;
190 ssl_context ssl;
Paul Bakkered27a042013-04-18 22:46:23 +0200191#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000192 x509_cert cacert;
193 x509_cert srvcert;
194 rsa_context rsa;
Paul Bakkered27a042013-04-18 22:46:23 +0200195#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000196#if defined(POLARSSL_SSL_CACHE_C)
197 ssl_cache_context cache;
198#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200199#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
200 unsigned char alloc_buf[100000];
201#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000202
203 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000204 char *p, *q;
205 const int *list;
206
Paul Bakker82024bf2013-07-04 11:52:32 +0200207#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
208 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
209#endif
210
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000211 /*
212 * Make sure memory references are valid.
213 */
214 listen_fd = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200215#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000216 memset( &cacert, 0, sizeof( x509_cert ) );
217 memset( &srvcert, 0, sizeof( x509_cert ) );
218 memset( &rsa, 0, sizeof( rsa_context ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200219#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000220#if defined(POLARSSL_SSL_CACHE_C)
221 ssl_cache_init( &cache );
222#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000223
224 if( argc == 0 )
225 {
226 usage:
227 if( ret == 0 )
228 ret = 1;
229
230 printf( USAGE );
231
232 list = ssl_list_ciphersuites();
233 while( *list )
234 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200235 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200236 list++;
237 if( !*list )
238 break;
239 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000240 list++;
241 }
242 printf("\n");
243 goto exit;
244 }
245
246 opt.server_port = DFL_SERVER_PORT;
247 opt.debug_level = DFL_DEBUG_LEVEL;
248 opt.ca_file = DFL_CA_FILE;
249 opt.ca_path = DFL_CA_PATH;
250 opt.crt_file = DFL_CRT_FILE;
251 opt.key_file = DFL_KEY_FILE;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200252 opt.psk = DFL_PSK;
253 opt.psk_identity = DFL_PSK_IDENTITY;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000254 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
255 opt.renegotiation = DFL_RENEGOTIATION;
256 opt.allow_legacy = DFL_ALLOW_LEGACY;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000257 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200258 opt.max_version = DFL_MAX_VERSION;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100259 opt.auth_mode = DFL_AUTH_MODE;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000260
261 for( i = 1; i < argc; i++ )
262 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000263 p = argv[i];
264 if( ( q = strchr( p, '=' ) ) == NULL )
265 goto usage;
266 *q++ = '\0';
267
268 if( strcmp( p, "server_port" ) == 0 )
269 {
270 opt.server_port = atoi( q );
271 if( opt.server_port < 1 || opt.server_port > 65535 )
272 goto usage;
273 }
274 else if( strcmp( p, "debug_level" ) == 0 )
275 {
276 opt.debug_level = atoi( q );
277 if( opt.debug_level < 0 || opt.debug_level > 65535 )
278 goto usage;
279 }
280 else if( strcmp( p, "ca_file" ) == 0 )
281 opt.ca_file = q;
282 else if( strcmp( p, "ca_path" ) == 0 )
283 opt.ca_path = q;
284 else if( strcmp( p, "crt_file" ) == 0 )
285 opt.crt_file = q;
286 else if( strcmp( p, "key_file" ) == 0 )
287 opt.key_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200288 else if( strcmp( p, "psk" ) == 0 )
289 opt.psk = q;
290 else if( strcmp( p, "psk_identity" ) == 0 )
291 opt.psk_identity = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000292 else if( strcmp( p, "force_ciphersuite" ) == 0 )
293 {
294 opt.force_ciphersuite[0] = -1;
295
296 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
297
298 if( opt.force_ciphersuite[0] <= 0 )
299 {
300 ret = 2;
301 goto usage;
302 }
303 opt.force_ciphersuite[1] = 0;
304 }
305 else if( strcmp( p, "renegotiation" ) == 0 )
306 {
307 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
308 SSL_RENEGOTIATION_DISABLED;
309 }
310 else if( strcmp( p, "allow_legacy" ) == 0 )
311 {
312 opt.allow_legacy = atoi( q );
313 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
314 goto usage;
315 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000316 else if( strcmp( p, "min_version" ) == 0 )
317 {
318 if( strcmp( q, "ssl3" ) == 0 )
319 opt.min_version = SSL_MINOR_VERSION_0;
320 else if( strcmp( q, "tls1" ) == 0 )
321 opt.min_version = SSL_MINOR_VERSION_1;
322 else if( strcmp( q, "tls1_1" ) == 0 )
323 opt.min_version = SSL_MINOR_VERSION_2;
324 else if( strcmp( q, "tls1_2" ) == 0 )
325 opt.min_version = SSL_MINOR_VERSION_3;
326 else
327 goto usage;
328 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200329 else if( strcmp( p, "max_version" ) == 0 )
330 {
331 if( strcmp( q, "ssl3" ) == 0 )
332 opt.max_version = SSL_MINOR_VERSION_0;
333 else if( strcmp( q, "tls1" ) == 0 )
334 opt.max_version = SSL_MINOR_VERSION_1;
335 else if( strcmp( q, "tls1_1" ) == 0 )
336 opt.max_version = SSL_MINOR_VERSION_2;
337 else if( strcmp( q, "tls1_2" ) == 0 )
338 opt.max_version = SSL_MINOR_VERSION_3;
339 else
340 goto usage;
341 }
342 else if( strcmp( p, "force_version" ) == 0 )
343 {
344 if( strcmp( q, "ssl3" ) == 0 )
345 {
346 opt.min_version = SSL_MINOR_VERSION_0;
347 opt.max_version = SSL_MINOR_VERSION_0;
348 }
349 else if( strcmp( q, "tls1" ) == 0 )
350 {
351 opt.min_version = SSL_MINOR_VERSION_1;
352 opt.max_version = SSL_MINOR_VERSION_1;
353 }
354 else if( strcmp( q, "tls1_1" ) == 0 )
355 {
356 opt.min_version = SSL_MINOR_VERSION_2;
357 opt.max_version = SSL_MINOR_VERSION_2;
358 }
359 else if( strcmp( q, "tls1_2" ) == 0 )
360 {
361 opt.min_version = SSL_MINOR_VERSION_3;
362 opt.max_version = SSL_MINOR_VERSION_3;
363 }
364 else
365 goto usage;
366 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100367 else if( strcmp( p, "auth_mode" ) == 0 )
368 {
369 if( strcmp( q, "none" ) == 0 )
370 opt.auth_mode = SSL_VERIFY_NONE;
371 else if( strcmp( q, "optional" ) == 0 )
372 opt.auth_mode = SSL_VERIFY_OPTIONAL;
373 else if( strcmp( q, "required" ) == 0 )
374 opt.auth_mode = SSL_VERIFY_REQUIRED;
375 else
376 goto usage;
377 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000378 else
379 goto usage;
380 }
381
Paul Bakkerc1516be2013-06-29 16:01:32 +0200382 if( opt.force_ciphersuite[0] > 0 )
383 {
384 const ssl_ciphersuite_t *ciphersuite_info;
385 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
386
387 if( ciphersuite_info->min_minor_ver > opt.max_version ||
388 ciphersuite_info->max_minor_ver < opt.min_version )
389 {
390 printf("forced ciphersuite not allowed with this protocol version\n");
391 ret = 2;
392 goto usage;
393 }
394 }
395
Paul Bakkered27a042013-04-18 22:46:23 +0200396#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000397 /*
Paul Bakkerfbb17802013-04-17 19:10:21 +0200398 * Unhexify the pre-shared key if any is given
399 */
400 if( strlen( opt.psk ) )
401 {
402 unsigned char c;
403 size_t j;
404
405 if( strlen( opt.psk ) % 2 != 0 )
406 {
407 printf("pre-shared key not valid hex\n");
408 goto exit;
409 }
410
411 psk_len = strlen( opt.psk ) / 2;
412
413 for( j = 0; j < strlen( opt.psk ); j += 2 )
414 {
415 c = opt.psk[j];
416 if( c >= '0' && c <= '9' )
417 c -= '0';
418 else if( c >= 'a' && c <= 'f' )
419 c -= 'a' - 10;
420 else if( c >= 'A' && c <= 'F' )
421 c -= 'A' - 10;
422 else
423 {
424 printf("pre-shared key not valid hex\n");
425 goto exit;
426 }
427 psk[ j / 2 ] = c << 4;
428
429 c = opt.psk[j + 1];
430 if( c >= '0' && c <= '9' )
431 c -= '0';
432 else if( c >= 'a' && c <= 'f' )
433 c -= 'a' - 10;
434 else if( c >= 'A' && c <= 'F' )
435 c -= 'A' - 10;
436 else
437 {
438 printf("pre-shared key not valid hex\n");
439 goto exit;
440 }
441 psk[ j / 2 ] |= c;
442 }
443 }
Paul Bakkered27a042013-04-18 22:46:23 +0200444#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +0200445
446 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000447 * 0. Initialize the RNG and the session data
448 */
449 printf( "\n . Seeding the random number generator..." );
450 fflush( stdout );
451
452 entropy_init( &entropy );
453 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200454 (const unsigned char *) pers,
455 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000456 {
457 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
458 goto exit;
459 }
460
461 printf( " ok\n" );
462
Paul Bakkered27a042013-04-18 22:46:23 +0200463#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000464 /*
465 * 1.1. Load the trusted CA
466 */
467 printf( " . Loading the CA root certificate ..." );
468 fflush( stdout );
469
470#if defined(POLARSSL_FS_IO)
471 if( strlen( opt.ca_path ) )
472 ret = x509parse_crtpath( &cacert, opt.ca_path );
473 else if( strlen( opt.ca_file ) )
474 ret = x509parse_crtfile( &cacert, opt.ca_file );
475 else
476#endif
477#if defined(POLARSSL_CERTS_C)
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200478 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000479 strlen( test_ca_crt ) );
480#else
481 {
482 ret = 1;
483 printf("POLARSSL_CERTS_C not defined.");
484 }
485#endif
486 if( ret < 0 )
487 {
488 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
489 goto exit;
490 }
491
492 printf( " ok (%d skipped)\n", ret );
493
494 /*
495 * 1.2. Load own certificate and private key
496 */
497 printf( " . Loading the server cert. and key..." );
498 fflush( stdout );
499
500#if defined(POLARSSL_FS_IO)
501 if( strlen( opt.crt_file ) )
502 ret = x509parse_crtfile( &srvcert, opt.crt_file );
503 else
504#endif
505#if defined(POLARSSL_CERTS_C)
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200506 ret = x509parse_crt( &srvcert, (const unsigned char *) test_srv_crt,
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000507 strlen( test_srv_crt ) );
508#else
509 {
510 ret = 1;
511 printf("POLARSSL_CERTS_C not defined.");
512 }
513#endif
514 if( ret != 0 )
515 {
516 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
517 goto exit;
518 }
519
520#if defined(POLARSSL_FS_IO)
521 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200522 ret = x509parse_keyfile_rsa( &rsa, opt.key_file, "" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000523 else
524#endif
525#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200526 ret = x509parse_key_rsa( &rsa, (const unsigned char *) test_srv_key,
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000527 strlen( test_srv_key ), NULL, 0 );
528#else
529 {
530 ret = 1;
531 printf("POLARSSL_CERTS_C not defined.");
532 }
533#endif
534 if( ret != 0 )
535 {
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200536 printf( " failed\n ! x509parse_key_rsa returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000537 goto exit;
538 }
539
540 printf( " ok\n" );
Paul Bakkered27a042013-04-18 22:46:23 +0200541#endif /* POLARSSL_X509_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000542
543 /*
544 * 2. Setup the listening TCP socket
545 */
546 printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
547 fflush( stdout );
548
549 if( ( ret = net_bind( &listen_fd, NULL, opt.server_port ) ) != 0 )
550 {
551 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
552 goto exit;
553 }
554
555 printf( " ok\n" );
556
557 /*
558 * 3. Setup stuff
559 */
560 printf( " . Setting up the SSL/TLS structure..." );
561 fflush( stdout );
562
563 if( ( ret = ssl_init( &ssl ) ) != 0 )
564 {
565 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
566 goto exit;
567 }
568
569 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +0100570 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000571
572 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
573 ssl_set_dbg( &ssl, my_debug, stdout );
574
Paul Bakker0a597072012-09-25 21:55:46 +0000575#if defined(POLARSSL_SSL_CACHE_C)
576 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
577 ssl_cache_set, &cache );
578#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000579
Paul Bakker41c83d32013-03-20 14:39:14 +0100580 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000581 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
582
583 ssl_set_renegotiation( &ssl, opt.renegotiation );
584 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
585
Paul Bakkered27a042013-04-18 22:46:23 +0200586#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000587 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
588 ssl_set_own_cert( &ssl, &srvcert, &rsa );
Paul Bakkered27a042013-04-18 22:46:23 +0200589#endif
590
591#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Paul Bakker3c5ef712013-06-25 16:37:45 +0200592 ssl_set_psk( &ssl, psk, psk_len, (const unsigned char *) opt.psk_identity,
Paul Bakkerfbb17802013-04-17 19:10:21 +0200593 strlen( opt.psk_identity ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200594#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000595
596#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +0000597 /*
598 * Use different group than default DHM group
599 */
Paul Bakkerd4324102012-09-26 08:29:38 +0000600 ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
601 POLARSSL_DHM_RFC5114_MODP_2048_G );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000602#endif
603
Paul Bakker1d29fb52012-09-28 13:28:45 +0000604 if( opt.min_version != -1 )
605 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
606
Paul Bakkerc1516be2013-06-29 16:01:32 +0200607 if( opt.max_version != -1 )
608 ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
609
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000610 printf( " ok\n" );
611
612reset:
613#ifdef POLARSSL_ERROR_C
614 if( ret != 0 )
615 {
616 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200617 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000618 printf("Last error was: %d - %s\n\n", ret, error_buf );
619 }
620#endif
621
622 if( client_fd != -1 )
623 net_close( client_fd );
624
625 ssl_session_reset( &ssl );
626
627 /*
628 * 3. Wait until a client connects
629 */
630#if defined(_WIN32_WCE)
631 {
632 SHELLEXECUTEINFO sei;
633
634 ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) );
635
636 sei.cbSize = sizeof( SHELLEXECUTEINFO );
637 sei.fMask = 0;
638 sei.hwnd = 0;
639 sei.lpVerb = _T( "open" );
640 sei.lpFile = _T( "https://localhost:4433/" );
641 sei.lpParameters = NULL;
642 sei.lpDirectory = NULL;
643 sei.nShow = SW_SHOWNORMAL;
644
645 ShellExecuteEx( &sei );
646 }
647#elif defined(_WIN32)
648 ShellExecute( NULL, "open", "https://localhost:4433/",
649 NULL, NULL, SW_SHOWNORMAL );
650#endif
651
652 client_fd = -1;
653
654 printf( " . Waiting for a remote connection ..." );
655 fflush( stdout );
656
657 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
658 {
659 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
660 goto exit;
661 }
662
663 ssl_set_bio( &ssl, net_recv, &client_fd,
664 net_send, &client_fd );
665
666 printf( " ok\n" );
667
668 /*
669 * 4. Handshake
670 */
671 printf( " . Performing the SSL/TLS handshake..." );
672 fflush( stdout );
673
674 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
675 {
676 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
677 {
678 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000679 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000680 }
681 }
682
683 printf( " ok\n [ Ciphersuite is %s ]\n",
684 ssl_get_ciphersuite( &ssl ) );
685
Paul Bakkered27a042013-04-18 22:46:23 +0200686#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000687 /*
688 * 5. Verify the server certificate
689 */
690 printf( " . Verifying peer X.509 certificate..." );
691
692 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
693 {
694 printf( " failed\n" );
695
Paul Bakkerb0550d92012-10-30 07:51:03 +0000696 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000697 printf( " ! no client certificate sent\n" );
698
699 if( ( ret & BADCERT_EXPIRED ) != 0 )
700 printf( " ! client certificate has expired\n" );
701
702 if( ( ret & BADCERT_REVOKED ) != 0 )
703 printf( " ! client certificate has been revoked\n" );
704
705 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
706 printf( " ! self-signed or not signed by a trusted CA\n" );
707
708 printf( "\n" );
709 }
710 else
711 printf( " ok\n" );
712
Paul Bakkerb0550d92012-10-30 07:51:03 +0000713 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000714 {
715 printf( " . Peer certificate information ...\n" );
716 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
Paul Bakkerb0550d92012-10-30 07:51:03 +0000717 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000718 printf( "%s\n", buf );
719 }
Paul Bakkered27a042013-04-18 22:46:23 +0200720#endif /* POLARSSL_X509_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000721
722 /*
723 * 6. Read the HTTP Request
724 */
725 printf( " < Read from client:" );
726 fflush( stdout );
727
728 do
729 {
730 len = sizeof( buf ) - 1;
731 memset( buf, 0, sizeof( buf ) );
732 ret = ssl_read( &ssl, buf, len );
733
734 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
735 continue;
736
737 if( ret <= 0 )
738 {
739 switch( ret )
740 {
741 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
742 printf( " connection was closed gracefully\n" );
743 break;
744
745 case POLARSSL_ERR_NET_CONN_RESET:
746 printf( " connection was reset by peer\n" );
747 break;
748
749 default:
750 printf( " ssl_read returned -0x%x\n", -ret );
751 break;
752 }
753
754 break;
755 }
756
757 len = ret;
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200758 printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000759
Paul Bakker82024bf2013-07-04 11:52:32 +0200760 if( memcmp( buf, "SERVERQUIT", 10 ) == 0 )
761 goto exit;
762
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000763 if( ret > 0 )
764 break;
765 }
766 while( 1 );
767
768 /*
769 * 7. Write the 200 Response
770 */
771 printf( " > Write to client:" );
772 fflush( stdout );
773
774 len = sprintf( (char *) buf, HTTP_RESPONSE,
775 ssl_get_ciphersuite( &ssl ) );
776
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200777 for( written = 0; written < len; written += ret )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000778 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200779 while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000780 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200781 if( ret == POLARSSL_ERR_NET_CONN_RESET )
782 {
783 printf( " failed\n ! peer closed the connection\n\n" );
784 goto reset;
785 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000786
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200787 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
788 {
789 printf( " failed\n ! ssl_write returned %d\n\n", ret );
790 goto exit;
791 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000792 }
793 }
794
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200795 buf[written] = '\0';
796 printf( " %d bytes written\n\n%s\n", written, (char *) buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000797
798 ret = 0;
799 goto reset;
800
801exit:
802
803#ifdef POLARSSL_ERROR_C
804 if( ret != 0 )
805 {
806 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200807 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000808 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
809 }
810#endif
811
812 net_close( client_fd );
Paul Bakkered27a042013-04-18 22:46:23 +0200813#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000814 x509_free( &srvcert );
815 x509_free( &cacert );
816 rsa_free( &rsa );
Paul Bakkered27a042013-04-18 22:46:23 +0200817#endif
818
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000819 ssl_free( &ssl );
820
Paul Bakker0a597072012-09-25 21:55:46 +0000821#if defined(POLARSSL_SSL_CACHE_C)
822 ssl_cache_free( &cache );
823#endif
824
Paul Bakker82024bf2013-07-04 11:52:32 +0200825#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && defined(POLARSSL_MEMORY_DEBUG)
826 memory_buffer_alloc_status();
827#endif
828
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000829#if defined(_WIN32)
830 printf( " + Press Enter to exit this program.\n" );
831 fflush( stdout ); getchar();
832#endif
833
834 return( ret );
835}
836#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
837 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
838 POLARSSL_CTR_DRBG_C */