blob: c1b3810c4da74ea77066a6eaa79bbafd7b4d8636 [file] [log] [blame]
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001/*
2 * SSL client with options
3 *
4 * Copyright (C) 2006-2012, Brainspark B.V.
5 *
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 Bakkerb60b95f2012-09-25 09:05:17 +000052#define DFL_SERVER_PORT 4433
53#define DFL_REQUEST_PAGE "/"
54#define DFL_DEBUG_LEVEL 0
55#define DFL_CA_FILE ""
56#define DFL_CA_PATH ""
57#define DFL_CRT_FILE ""
58#define DFL_KEY_FILE ""
59#define DFL_FORCE_CIPHER 0
60#define DFL_RENEGOTIATION SSL_RENEGOTIATION_ENABLED
61#define DFL_ALLOW_LEGACY SSL_LEGACY_NO_RENEGOTIATION
Paul Bakker1d29fb52012-09-28 13:28:45 +000062#define DFL_MIN_VERSION -1
Paul Bakker91ebfb52012-11-23 14:04:08 +010063#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000064
65#define HTTP_RESPONSE \
66 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
67 "<h2>PolarSSL Test Server</h2>\r\n" \
68 "<p>Successful connection using: %s</p>\r\n"
69
70/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +000071 * global options
72 */
73struct options
74{
75 int server_port; /* port on which the ssl service runs */
76 int debug_level; /* level of debugging */
77 char *ca_file; /* the file with the CA certificate(s) */
78 char *ca_path; /* the path with the CA certificate(s) reside */
79 char *crt_file; /* the file with the client certificate */
80 char *key_file; /* the file with the client key */
81 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
82 int renegotiation; /* enable / disable renegotiation */
83 int allow_legacy; /* allow legacy renegotiation */
Paul Bakker1d29fb52012-09-28 13:28:45 +000084 int min_version; /* minimum protocol version accepted */
Paul Bakker91ebfb52012-11-23 14:04:08 +010085 int auth_mode; /* verify mode for connection */
Paul Bakkerb60b95f2012-09-25 09:05:17 +000086} opt;
87
88void my_debug( void *ctx, int level, const char *str )
89{
90 if( level < opt.debug_level )
91 {
92 fprintf( (FILE *) ctx, "%s", str );
93 fflush( (FILE *) ctx );
94 }
95}
96
Paul Bakker645ce3a2012-10-31 12:32:41 +000097/*
98 * Sorted by order of preference
99 */
100int my_ciphersuites[] =
101{
102#if defined(POLARSSL_DHM_C)
103#if defined(POLARSSL_AES_C)
104#if defined(POLARSSL_SHA2_C)
105 TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,
106#endif /* POLARSSL_SHA2_C */
107#if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA4_C)
108 TLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
109#endif
110 TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
111#if defined(POLARSSL_SHA2_C)
112 TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
113#endif
114#if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA2_C)
115 TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
116#endif
117 TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
118#endif
119#if defined(POLARSSL_CAMELLIA_C)
120#if defined(POLARSSL_SHA2_C)
121 TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256,
122#endif /* POLARSSL_SHA2_C */
123 TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA,
124#if defined(POLARSSL_SHA2_C)
125 TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256,
126#endif /* POLARSSL_SHA2_C */
127 TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA,
128#endif
129#if defined(POLARSSL_DES_C)
130 TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA,
131#endif
132#endif
133
134#if defined(POLARSSL_AES_C)
135#if defined(POLARSSL_SHA2_C)
136 TLS_RSA_WITH_AES_256_CBC_SHA256,
137#endif /* POLARSSL_SHA2_C */
138#if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA4_C)
139 TLS_RSA_WITH_AES_256_GCM_SHA384,
140#endif /* POLARSSL_SHA2_C */
141 TLS_RSA_WITH_AES_256_CBC_SHA,
142#endif
143#if defined(POLARSSL_CAMELLIA_C)
144#if defined(POLARSSL_SHA2_C)
145 TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256,
146#endif /* POLARSSL_SHA2_C */
147 TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,
148#endif
149#if defined(POLARSSL_AES_C)
150#if defined(POLARSSL_SHA2_C)
151 TLS_RSA_WITH_AES_128_CBC_SHA256,
152#endif /* POLARSSL_SHA2_C */
153#if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA2_C)
154 TLS_RSA_WITH_AES_128_GCM_SHA256,
155#endif /* POLARSSL_SHA2_C */
156 TLS_RSA_WITH_AES_128_CBC_SHA,
157#endif
158#if defined(POLARSSL_CAMELLIA_C)
159#if defined(POLARSSL_SHA2_C)
160 TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256,
161#endif /* POLARSSL_SHA2_C */
162 TLS_RSA_WITH_CAMELLIA_128_CBC_SHA,
163#endif
164#if defined(POLARSSL_DES_C)
165 TLS_RSA_WITH_3DES_EDE_CBC_SHA,
166#endif
167#if defined(POLARSSL_ARC4_C)
168 TLS_RSA_WITH_RC4_128_SHA,
169 TLS_RSA_WITH_RC4_128_MD5,
170#endif
171
172#if defined(POLARSSL_ENABLE_WEAK_CIPHERSUITES)
173#if defined(POLARSSL_DES_C)
174 TLS_DHE_RSA_WITH_DES_CBC_SHA,
175 TLS_RSA_WITH_DES_CBC_SHA,
176#endif
177#if defined(POLARSSL_CIPHER_NULL_CIPHER)
178 TLS_RSA_WITH_NULL_MD5,
179 TLS_RSA_WITH_NULL_SHA,
180 TLS_RSA_WITH_NULL_SHA256,
181#endif
182#endif
183 0
184};
185
186
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000187#if defined(POLARSSL_FS_IO)
188#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100189 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
190 " default: \"\" (pre-loaded)\n" \
191 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
192 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
193 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
194 " default: \"\" (pre-loaded)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000195 " key_file=%%s default: \"\" (pre-loaded)\n"
196#else
197#define USAGE_IO \
198 " No file operations available (POLARSSL_FS_IO not defined)\n"
199#endif /* POLARSSL_FS_IO */
200
201#define USAGE \
202 "\n usage: ssl_server2 param=<>...\n" \
203 "\n acceptable parameters:\n" \
204 " server_port=%%d default: 4433\n" \
205 " debug_level=%%d default: 0 (disabled)\n" \
206 USAGE_IO \
207 " request_page=%%s default: \".\"\n" \
208 " renegotiation=%%d default: 1 (enabled)\n" \
209 " allow_legacy=%%d default: 0 (disabled)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000210 " min_version=%%s default: \"ssl3\"\n" \
211 " options: ssl3, tls1, tls1_1, tls1_2\n" \
Paul Bakker91ebfb52012-11-23 14:04:08 +0100212 " auth_mode=%%s default: \"optional\"\n" \
213 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000214 " force_ciphersuite=<name> default: all enabled\n"\
215 " acceptable ciphersuite names:\n"
216
217#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
218 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
219 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
220 !defined(POLARSSL_CTR_DRBG_C)
221int main( int argc, char *argv[] )
222{
223 ((void) argc);
224 ((void) argv);
225
226 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
227 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
228 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
229 "POLARSSL_CTR_DRBG_C not defined.\n");
230 return( 0 );
231}
232#else
233int main( int argc, char *argv[] )
234{
235 int ret = 0, len;
236 int listen_fd;
237 int client_fd = -1;
238 unsigned char buf[1024];
239 char *pers = "ssl_server2";
240
241 entropy_context entropy;
242 ctr_drbg_context ctr_drbg;
243 ssl_context ssl;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000244 x509_cert cacert;
245 x509_cert srvcert;
246 rsa_context rsa;
Paul Bakker0a597072012-09-25 21:55:46 +0000247#if defined(POLARSSL_SSL_CACHE_C)
248 ssl_cache_context cache;
249#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000250
251 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000252 char *p, *q;
253 const int *list;
254
255 /*
256 * Make sure memory references are valid.
257 */
258 listen_fd = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000259 memset( &cacert, 0, sizeof( x509_cert ) );
260 memset( &srvcert, 0, sizeof( x509_cert ) );
261 memset( &rsa, 0, sizeof( rsa_context ) );
Paul Bakker0a597072012-09-25 21:55:46 +0000262#if defined(POLARSSL_SSL_CACHE_C)
263 ssl_cache_init( &cache );
264#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000265
266 if( argc == 0 )
267 {
268 usage:
269 if( ret == 0 )
270 ret = 1;
271
272 printf( USAGE );
273
274 list = ssl_list_ciphersuites();
275 while( *list )
276 {
277 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
278 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;
290 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
291 opt.renegotiation = DFL_RENEGOTIATION;
292 opt.allow_legacy = DFL_ALLOW_LEGACY;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000293 opt.min_version = DFL_MIN_VERSION;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100294 opt.auth_mode = DFL_AUTH_MODE;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000295
296 for( i = 1; i < argc; i++ )
297 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000298 p = argv[i];
299 if( ( q = strchr( p, '=' ) ) == NULL )
300 goto usage;
301 *q++ = '\0';
302
303 if( strcmp( p, "server_port" ) == 0 )
304 {
305 opt.server_port = atoi( q );
306 if( opt.server_port < 1 || opt.server_port > 65535 )
307 goto usage;
308 }
309 else if( strcmp( p, "debug_level" ) == 0 )
310 {
311 opt.debug_level = atoi( q );
312 if( opt.debug_level < 0 || opt.debug_level > 65535 )
313 goto usage;
314 }
315 else if( strcmp( p, "ca_file" ) == 0 )
316 opt.ca_file = q;
317 else if( strcmp( p, "ca_path" ) == 0 )
318 opt.ca_path = q;
319 else if( strcmp( p, "crt_file" ) == 0 )
320 opt.crt_file = q;
321 else if( strcmp( p, "key_file" ) == 0 )
322 opt.key_file = q;
323 else if( strcmp( p, "force_ciphersuite" ) == 0 )
324 {
325 opt.force_ciphersuite[0] = -1;
326
327 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
328
329 if( opt.force_ciphersuite[0] <= 0 )
330 {
331 ret = 2;
332 goto usage;
333 }
334 opt.force_ciphersuite[1] = 0;
335 }
336 else if( strcmp( p, "renegotiation" ) == 0 )
337 {
338 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
339 SSL_RENEGOTIATION_DISABLED;
340 }
341 else if( strcmp( p, "allow_legacy" ) == 0 )
342 {
343 opt.allow_legacy = atoi( q );
344 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
345 goto usage;
346 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000347 else if( strcmp( p, "min_version" ) == 0 )
348 {
349 if( strcmp( q, "ssl3" ) == 0 )
350 opt.min_version = SSL_MINOR_VERSION_0;
351 else if( strcmp( q, "tls1" ) == 0 )
352 opt.min_version = SSL_MINOR_VERSION_1;
353 else if( strcmp( q, "tls1_1" ) == 0 )
354 opt.min_version = SSL_MINOR_VERSION_2;
355 else if( strcmp( q, "tls1_2" ) == 0 )
356 opt.min_version = SSL_MINOR_VERSION_3;
357 else
358 goto usage;
359 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100360 else if( strcmp( p, "auth_mode" ) == 0 )
361 {
362 if( strcmp( q, "none" ) == 0 )
363 opt.auth_mode = SSL_VERIFY_NONE;
364 else if( strcmp( q, "optional" ) == 0 )
365 opt.auth_mode = SSL_VERIFY_OPTIONAL;
366 else if( strcmp( q, "required" ) == 0 )
367 opt.auth_mode = SSL_VERIFY_REQUIRED;
368 else
369 goto usage;
370 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000371 else
372 goto usage;
373 }
374
375 /*
376 * 0. Initialize the RNG and the session data
377 */
378 printf( "\n . Seeding the random number generator..." );
379 fflush( stdout );
380
381 entropy_init( &entropy );
382 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
383 (unsigned char *) pers, strlen( pers ) ) ) != 0 )
384 {
385 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
386 goto exit;
387 }
388
389 printf( " ok\n" );
390
391 /*
392 * 1.1. Load the trusted CA
393 */
394 printf( " . Loading the CA root certificate ..." );
395 fflush( stdout );
396
397#if defined(POLARSSL_FS_IO)
398 if( strlen( opt.ca_path ) )
399 ret = x509parse_crtpath( &cacert, opt.ca_path );
400 else if( strlen( opt.ca_file ) )
401 ret = x509parse_crtfile( &cacert, opt.ca_file );
402 else
403#endif
404#if defined(POLARSSL_CERTS_C)
405 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
406 strlen( test_ca_crt ) );
407#else
408 {
409 ret = 1;
410 printf("POLARSSL_CERTS_C not defined.");
411 }
412#endif
413 if( ret < 0 )
414 {
415 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
416 goto exit;
417 }
418
419 printf( " ok (%d skipped)\n", ret );
420
421 /*
422 * 1.2. Load own certificate and private key
423 */
424 printf( " . Loading the server cert. and key..." );
425 fflush( stdout );
426
427#if defined(POLARSSL_FS_IO)
428 if( strlen( opt.crt_file ) )
429 ret = x509parse_crtfile( &srvcert, opt.crt_file );
430 else
431#endif
432#if defined(POLARSSL_CERTS_C)
433 ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt,
434 strlen( test_srv_crt ) );
435#else
436 {
437 ret = 1;
438 printf("POLARSSL_CERTS_C not defined.");
439 }
440#endif
441 if( ret != 0 )
442 {
443 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
444 goto exit;
445 }
446
447#if defined(POLARSSL_FS_IO)
448 if( strlen( opt.key_file ) )
449 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
450 else
451#endif
452#if defined(POLARSSL_CERTS_C)
453 ret = x509parse_key( &rsa, (unsigned char *) test_srv_key,
454 strlen( test_srv_key ), NULL, 0 );
455#else
456 {
457 ret = 1;
458 printf("POLARSSL_CERTS_C not defined.");
459 }
460#endif
461 if( ret != 0 )
462 {
463 printf( " failed\n ! x509parse_key returned -0x%x\n\n", -ret );
464 goto exit;
465 }
466
467 printf( " ok\n" );
468
469 /*
470 * 2. Setup the listening TCP socket
471 */
472 printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
473 fflush( stdout );
474
475 if( ( ret = net_bind( &listen_fd, NULL, opt.server_port ) ) != 0 )
476 {
477 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
478 goto exit;
479 }
480
481 printf( " ok\n" );
482
483 /*
484 * 3. Setup stuff
485 */
486 printf( " . Setting up the SSL/TLS structure..." );
487 fflush( stdout );
488
489 if( ( ret = ssl_init( &ssl ) ) != 0 )
490 {
491 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
492 goto exit;
493 }
494
495 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +0100496 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000497
498 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
499 ssl_set_dbg( &ssl, my_debug, stdout );
500
Paul Bakker0a597072012-09-25 21:55:46 +0000501#if defined(POLARSSL_SSL_CACHE_C)
502 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
503 ssl_cache_set, &cache );
504#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000505
506 if( opt.force_ciphersuite[0] == DFL_FORCE_CIPHER )
Paul Bakker645ce3a2012-10-31 12:32:41 +0000507 ssl_set_ciphersuites( &ssl, my_ciphersuites );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000508 else
509 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
510
511 ssl_set_renegotiation( &ssl, opt.renegotiation );
512 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
513
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000514 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
515 ssl_set_own_cert( &ssl, &srvcert, &rsa );
516
517#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +0000518 /*
519 * Use different group than default DHM group
520 */
Paul Bakkerd4324102012-09-26 08:29:38 +0000521 ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
522 POLARSSL_DHM_RFC5114_MODP_2048_G );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000523#endif
524
Paul Bakker1d29fb52012-09-28 13:28:45 +0000525 if( opt.min_version != -1 )
526 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
527
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000528 printf( " ok\n" );
529
530reset:
531#ifdef POLARSSL_ERROR_C
532 if( ret != 0 )
533 {
534 char error_buf[100];
535 error_strerror( ret, error_buf, 100 );
536 printf("Last error was: %d - %s\n\n", ret, error_buf );
537 }
538#endif
539
540 if( client_fd != -1 )
541 net_close( client_fd );
542
543 ssl_session_reset( &ssl );
544
545 /*
546 * 3. Wait until a client connects
547 */
548#if defined(_WIN32_WCE)
549 {
550 SHELLEXECUTEINFO sei;
551
552 ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) );
553
554 sei.cbSize = sizeof( SHELLEXECUTEINFO );
555 sei.fMask = 0;
556 sei.hwnd = 0;
557 sei.lpVerb = _T( "open" );
558 sei.lpFile = _T( "https://localhost:4433/" );
559 sei.lpParameters = NULL;
560 sei.lpDirectory = NULL;
561 sei.nShow = SW_SHOWNORMAL;
562
563 ShellExecuteEx( &sei );
564 }
565#elif defined(_WIN32)
566 ShellExecute( NULL, "open", "https://localhost:4433/",
567 NULL, NULL, SW_SHOWNORMAL );
568#endif
569
570 client_fd = -1;
571
572 printf( " . Waiting for a remote connection ..." );
573 fflush( stdout );
574
575 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
576 {
577 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
578 goto exit;
579 }
580
581 ssl_set_bio( &ssl, net_recv, &client_fd,
582 net_send, &client_fd );
583
584 printf( " ok\n" );
585
586 /*
587 * 4. Handshake
588 */
589 printf( " . Performing the SSL/TLS handshake..." );
590 fflush( stdout );
591
592 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
593 {
594 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
595 {
596 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000597 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000598 }
599 }
600
601 printf( " ok\n [ Ciphersuite is %s ]\n",
602 ssl_get_ciphersuite( &ssl ) );
603
604 /*
605 * 5. Verify the server certificate
606 */
607 printf( " . Verifying peer X.509 certificate..." );
608
609 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
610 {
611 printf( " failed\n" );
612
Paul Bakkerb0550d92012-10-30 07:51:03 +0000613 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000614 printf( " ! no client certificate sent\n" );
615
616 if( ( ret & BADCERT_EXPIRED ) != 0 )
617 printf( " ! client certificate has expired\n" );
618
619 if( ( ret & BADCERT_REVOKED ) != 0 )
620 printf( " ! client certificate has been revoked\n" );
621
622 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
623 printf( " ! self-signed or not signed by a trusted CA\n" );
624
625 printf( "\n" );
626 }
627 else
628 printf( " ok\n" );
629
Paul Bakkerb0550d92012-10-30 07:51:03 +0000630 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000631 {
632 printf( " . Peer certificate information ...\n" );
633 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
Paul Bakkerb0550d92012-10-30 07:51:03 +0000634 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000635 printf( "%s\n", buf );
636 }
637
638 /*
639 * 6. Read the HTTP Request
640 */
641 printf( " < Read from client:" );
642 fflush( stdout );
643
644 do
645 {
646 len = sizeof( buf ) - 1;
647 memset( buf, 0, sizeof( buf ) );
648 ret = ssl_read( &ssl, buf, len );
649
650 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
651 continue;
652
653 if( ret <= 0 )
654 {
655 switch( ret )
656 {
657 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
658 printf( " connection was closed gracefully\n" );
659 break;
660
661 case POLARSSL_ERR_NET_CONN_RESET:
662 printf( " connection was reset by peer\n" );
663 break;
664
665 default:
666 printf( " ssl_read returned -0x%x\n", -ret );
667 break;
668 }
669
670 break;
671 }
672
673 len = ret;
674 printf( " %d bytes read\n\n%s", len, (char *) buf );
675
676 if( ret > 0 )
677 break;
678 }
679 while( 1 );
680
681 /*
682 * 7. Write the 200 Response
683 */
684 printf( " > Write to client:" );
685 fflush( stdout );
686
687 len = sprintf( (char *) buf, HTTP_RESPONSE,
688 ssl_get_ciphersuite( &ssl ) );
689
690 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
691 {
692 if( ret == POLARSSL_ERR_NET_CONN_RESET )
693 {
694 printf( " failed\n ! peer closed the connection\n\n" );
695 goto reset;
696 }
697
698 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
699 {
700 printf( " failed\n ! ssl_write returned %d\n\n", ret );
701 goto exit;
702 }
703 }
704
705 len = ret;
706 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
707
708 ret = 0;
709 goto reset;
710
711exit:
712
713#ifdef POLARSSL_ERROR_C
714 if( ret != 0 )
715 {
716 char error_buf[100];
717 error_strerror( ret, error_buf, 100 );
718 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
719 }
720#endif
721
722 net_close( client_fd );
723 x509_free( &srvcert );
724 x509_free( &cacert );
725 rsa_free( &rsa );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000726 ssl_free( &ssl );
727
Paul Bakker0a597072012-09-25 21:55:46 +0000728#if defined(POLARSSL_SSL_CACHE_C)
729 ssl_cache_free( &cache );
730#endif
731
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000732#if defined(_WIN32)
733 printf( " + Press Enter to exit this program.\n" );
734 fflush( stdout ); getchar();
735#endif
736
737 return( ret );
738}
739#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
740 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
741 POLARSSL_CTR_DRBG_C */