blob: 40333dc26c815482d5f3b0751830cce410c48c95 [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 ""
Paul Bakkerfbb17802013-04-17 19:10:21 +020059#define DFL_PSK ""
60#define DFL_PSK_IDENTITY "Client_identity"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000061#define DFL_FORCE_CIPHER 0
62#define DFL_RENEGOTIATION SSL_RENEGOTIATION_ENABLED
63#define DFL_ALLOW_LEGACY SSL_LEGACY_NO_RENEGOTIATION
Paul Bakker1d29fb52012-09-28 13:28:45 +000064#define DFL_MIN_VERSION -1
Paul Bakker91ebfb52012-11-23 14:04:08 +010065#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000066
67#define HTTP_RESPONSE \
68 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
69 "<h2>PolarSSL Test Server</h2>\r\n" \
70 "<p>Successful connection using: %s</p>\r\n"
71
72/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +000073 * global options
74 */
75struct options
76{
77 int server_port; /* port on which the ssl service runs */
78 int debug_level; /* level of debugging */
79 char *ca_file; /* the file with the CA certificate(s) */
80 char *ca_path; /* the path with the CA certificate(s) reside */
81 char *crt_file; /* the file with the client certificate */
82 char *key_file; /* the file with the client key */
Paul Bakkerfbb17802013-04-17 19:10:21 +020083 char *psk; /* the pre-shared key */
84 char *psk_identity; /* the pre-shared key identity */
Paul Bakkerb60b95f2012-09-25 09:05:17 +000085 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
86 int renegotiation; /* enable / disable renegotiation */
87 int allow_legacy; /* allow legacy renegotiation */
Paul Bakker1d29fb52012-09-28 13:28:45 +000088 int min_version; /* minimum protocol version accepted */
Paul Bakker91ebfb52012-11-23 14:04:08 +010089 int auth_mode; /* verify mode for connection */
Paul Bakkerb60b95f2012-09-25 09:05:17 +000090} opt;
91
92void my_debug( void *ctx, int level, const char *str )
93{
94 if( level < opt.debug_level )
95 {
96 fprintf( (FILE *) ctx, "%s", str );
97 fflush( (FILE *) ctx );
98 }
99}
100
Paul Bakkered27a042013-04-18 22:46:23 +0200101#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000102#if defined(POLARSSL_FS_IO)
103#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100104 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
105 " default: \"\" (pre-loaded)\n" \
106 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
107 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
108 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
109 " default: \"\" (pre-loaded)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000110 " key_file=%%s default: \"\" (pre-loaded)\n"
111#else
112#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200113 "\n" \
114 " No file operations available (POLARSSL_FS_IO not defined)\n" \
115 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000116#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200117#else
118#define USAGE_IO ""
119#endif /* POLARSSL_X509_PARSE_C */
120
121#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
122#define USAGE_PSK \
123 " psk=%%s default: \"\" (in hex, without 0x)\n" \
124 " psk_identity=%%s default: \"Client_identity\"\n"
125#else
126#define USAGE_PSK ""
127#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000128
129#define USAGE \
130 "\n usage: ssl_server2 param=<>...\n" \
131 "\n acceptable parameters:\n" \
132 " server_port=%%d default: 4433\n" \
133 " debug_level=%%d default: 0 (disabled)\n" \
134 USAGE_IO \
135 " request_page=%%s default: \".\"\n" \
136 " renegotiation=%%d default: 1 (enabled)\n" \
137 " allow_legacy=%%d default: 0 (disabled)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000138 " min_version=%%s default: \"ssl3\"\n" \
139 " options: ssl3, tls1, tls1_1, tls1_2\n" \
Paul Bakker91ebfb52012-11-23 14:04:08 +0100140 " auth_mode=%%s default: \"optional\"\n" \
141 " options: none, optional, required\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200142 USAGE_PSK \
Paul Bakkerfbb17802013-04-17 19:10:21 +0200143 "\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000144 " force_ciphersuite=<name> default: all enabled\n"\
145 " acceptable ciphersuite names:\n"
146
Paul Bakkered27a042013-04-18 22:46:23 +0200147#if !defined(POLARSSL_ENTROPY_C) || \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000148 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
Paul Bakkered27a042013-04-18 22:46:23 +0200149 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000150int main( int argc, char *argv[] )
151{
152 ((void) argc);
153 ((void) argv);
154
Paul Bakkered27a042013-04-18 22:46:23 +0200155 printf("POLARSSL_ENTROPY_C and/or "
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000156 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
Paul Bakkered27a042013-04-18 22:46:23 +0200157 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000158 return( 0 );
159}
160#else
161int main( int argc, char *argv[] )
162{
163 int ret = 0, len;
164 int listen_fd;
165 int client_fd = -1;
166 unsigned char buf[1024];
Paul Bakkered27a042013-04-18 22:46:23 +0200167#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Paul Bakkerfbb17802013-04-17 19:10:21 +0200168 unsigned char psk[256];
169 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200170#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000171 char *pers = "ssl_server2";
172
173 entropy_context entropy;
174 ctr_drbg_context ctr_drbg;
175 ssl_context ssl;
Paul Bakkered27a042013-04-18 22:46:23 +0200176#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000177 x509_cert cacert;
178 x509_cert srvcert;
179 rsa_context rsa;
Paul Bakkered27a042013-04-18 22:46:23 +0200180#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000181#if defined(POLARSSL_SSL_CACHE_C)
182 ssl_cache_context cache;
183#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000184
185 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000186 char *p, *q;
187 const int *list;
188
189 /*
190 * Make sure memory references are valid.
191 */
192 listen_fd = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200193#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000194 memset( &cacert, 0, sizeof( x509_cert ) );
195 memset( &srvcert, 0, sizeof( x509_cert ) );
196 memset( &rsa, 0, sizeof( rsa_context ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200197#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000198#if defined(POLARSSL_SSL_CACHE_C)
199 ssl_cache_init( &cache );
200#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000201
202 if( argc == 0 )
203 {
204 usage:
205 if( ret == 0 )
206 ret = 1;
207
208 printf( USAGE );
209
210 list = ssl_list_ciphersuites();
211 while( *list )
212 {
Paul Bakkered27a042013-04-18 22:46:23 +0200213 printf(" %-40s", ssl_get_ciphersuite_name( *list ) );
214 list++;
215 if( !*list )
216 break;
217 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000218 list++;
219 }
220 printf("\n");
221 goto exit;
222 }
223
224 opt.server_port = DFL_SERVER_PORT;
225 opt.debug_level = DFL_DEBUG_LEVEL;
226 opt.ca_file = DFL_CA_FILE;
227 opt.ca_path = DFL_CA_PATH;
228 opt.crt_file = DFL_CRT_FILE;
229 opt.key_file = DFL_KEY_FILE;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200230 opt.psk = DFL_PSK;
231 opt.psk_identity = DFL_PSK_IDENTITY;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000232 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
233 opt.renegotiation = DFL_RENEGOTIATION;
234 opt.allow_legacy = DFL_ALLOW_LEGACY;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000235 opt.min_version = DFL_MIN_VERSION;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100236 opt.auth_mode = DFL_AUTH_MODE;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000237
238 for( i = 1; i < argc; i++ )
239 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000240 p = argv[i];
241 if( ( q = strchr( p, '=' ) ) == NULL )
242 goto usage;
243 *q++ = '\0';
244
245 if( strcmp( p, "server_port" ) == 0 )
246 {
247 opt.server_port = atoi( q );
248 if( opt.server_port < 1 || opt.server_port > 65535 )
249 goto usage;
250 }
251 else if( strcmp( p, "debug_level" ) == 0 )
252 {
253 opt.debug_level = atoi( q );
254 if( opt.debug_level < 0 || opt.debug_level > 65535 )
255 goto usage;
256 }
257 else if( strcmp( p, "ca_file" ) == 0 )
258 opt.ca_file = q;
259 else if( strcmp( p, "ca_path" ) == 0 )
260 opt.ca_path = q;
261 else if( strcmp( p, "crt_file" ) == 0 )
262 opt.crt_file = q;
263 else if( strcmp( p, "key_file" ) == 0 )
264 opt.key_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200265 else if( strcmp( p, "psk" ) == 0 )
266 opt.psk = q;
267 else if( strcmp( p, "psk_identity" ) == 0 )
268 opt.psk_identity = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000269 else if( strcmp( p, "force_ciphersuite" ) == 0 )
270 {
271 opt.force_ciphersuite[0] = -1;
272
273 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
274
275 if( opt.force_ciphersuite[0] <= 0 )
276 {
277 ret = 2;
278 goto usage;
279 }
280 opt.force_ciphersuite[1] = 0;
281 }
282 else if( strcmp( p, "renegotiation" ) == 0 )
283 {
284 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
285 SSL_RENEGOTIATION_DISABLED;
286 }
287 else if( strcmp( p, "allow_legacy" ) == 0 )
288 {
289 opt.allow_legacy = atoi( q );
290 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
291 goto usage;
292 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000293 else if( strcmp( p, "min_version" ) == 0 )
294 {
295 if( strcmp( q, "ssl3" ) == 0 )
296 opt.min_version = SSL_MINOR_VERSION_0;
297 else if( strcmp( q, "tls1" ) == 0 )
298 opt.min_version = SSL_MINOR_VERSION_1;
299 else if( strcmp( q, "tls1_1" ) == 0 )
300 opt.min_version = SSL_MINOR_VERSION_2;
301 else if( strcmp( q, "tls1_2" ) == 0 )
302 opt.min_version = SSL_MINOR_VERSION_3;
303 else
304 goto usage;
305 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100306 else if( strcmp( p, "auth_mode" ) == 0 )
307 {
308 if( strcmp( q, "none" ) == 0 )
309 opt.auth_mode = SSL_VERIFY_NONE;
310 else if( strcmp( q, "optional" ) == 0 )
311 opt.auth_mode = SSL_VERIFY_OPTIONAL;
312 else if( strcmp( q, "required" ) == 0 )
313 opt.auth_mode = SSL_VERIFY_REQUIRED;
314 else
315 goto usage;
316 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000317 else
318 goto usage;
319 }
320
Paul Bakkered27a042013-04-18 22:46:23 +0200321#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000322 /*
Paul Bakkerfbb17802013-04-17 19:10:21 +0200323 * Unhexify the pre-shared key if any is given
324 */
325 if( strlen( opt.psk ) )
326 {
327 unsigned char c;
328 size_t j;
329
330 if( strlen( opt.psk ) % 2 != 0 )
331 {
332 printf("pre-shared key not valid hex\n");
333 goto exit;
334 }
335
336 psk_len = strlen( opt.psk ) / 2;
337
338 for( j = 0; j < strlen( opt.psk ); j += 2 )
339 {
340 c = opt.psk[j];
341 if( c >= '0' && c <= '9' )
342 c -= '0';
343 else if( c >= 'a' && c <= 'f' )
344 c -= 'a' - 10;
345 else if( c >= 'A' && c <= 'F' )
346 c -= 'A' - 10;
347 else
348 {
349 printf("pre-shared key not valid hex\n");
350 goto exit;
351 }
352 psk[ j / 2 ] = c << 4;
353
354 c = opt.psk[j + 1];
355 if( c >= '0' && c <= '9' )
356 c -= '0';
357 else if( c >= 'a' && c <= 'f' )
358 c -= 'a' - 10;
359 else if( c >= 'A' && c <= 'F' )
360 c -= 'A' - 10;
361 else
362 {
363 printf("pre-shared key not valid hex\n");
364 goto exit;
365 }
366 psk[ j / 2 ] |= c;
367 }
368 }
Paul Bakkered27a042013-04-18 22:46:23 +0200369#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +0200370
371 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000372 * 0. Initialize the RNG and the session data
373 */
374 printf( "\n . Seeding the random number generator..." );
375 fflush( stdout );
376
377 entropy_init( &entropy );
378 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
379 (unsigned char *) pers, strlen( pers ) ) ) != 0 )
380 {
381 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
382 goto exit;
383 }
384
385 printf( " ok\n" );
386
Paul Bakkered27a042013-04-18 22:46:23 +0200387#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000388 /*
389 * 1.1. Load the trusted CA
390 */
391 printf( " . Loading the CA root certificate ..." );
392 fflush( stdout );
393
394#if defined(POLARSSL_FS_IO)
395 if( strlen( opt.ca_path ) )
396 ret = x509parse_crtpath( &cacert, opt.ca_path );
397 else if( strlen( opt.ca_file ) )
398 ret = x509parse_crtfile( &cacert, opt.ca_file );
399 else
400#endif
401#if defined(POLARSSL_CERTS_C)
402 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
403 strlen( test_ca_crt ) );
404#else
405 {
406 ret = 1;
407 printf("POLARSSL_CERTS_C not defined.");
408 }
409#endif
410 if( ret < 0 )
411 {
412 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
413 goto exit;
414 }
415
416 printf( " ok (%d skipped)\n", ret );
417
418 /*
419 * 1.2. Load own certificate and private key
420 */
421 printf( " . Loading the server cert. and key..." );
422 fflush( stdout );
423
424#if defined(POLARSSL_FS_IO)
425 if( strlen( opt.crt_file ) )
426 ret = x509parse_crtfile( &srvcert, opt.crt_file );
427 else
428#endif
429#if defined(POLARSSL_CERTS_C)
430 ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt,
431 strlen( test_srv_crt ) );
432#else
433 {
434 ret = 1;
435 printf("POLARSSL_CERTS_C not defined.");
436 }
437#endif
438 if( ret != 0 )
439 {
440 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
441 goto exit;
442 }
443
444#if defined(POLARSSL_FS_IO)
445 if( strlen( opt.key_file ) )
446 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
447 else
448#endif
449#if defined(POLARSSL_CERTS_C)
450 ret = x509parse_key( &rsa, (unsigned char *) test_srv_key,
451 strlen( test_srv_key ), NULL, 0 );
452#else
453 {
454 ret = 1;
455 printf("POLARSSL_CERTS_C not defined.");
456 }
457#endif
458 if( ret != 0 )
459 {
460 printf( " failed\n ! x509parse_key returned -0x%x\n\n", -ret );
461 goto exit;
462 }
463
464 printf( " ok\n" );
Paul Bakkered27a042013-04-18 22:46:23 +0200465#endif /* POLARSSL_X509_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000466
467 /*
468 * 2. Setup the listening TCP socket
469 */
470 printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
471 fflush( stdout );
472
473 if( ( ret = net_bind( &listen_fd, NULL, opt.server_port ) ) != 0 )
474 {
475 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
476 goto exit;
477 }
478
479 printf( " ok\n" );
480
481 /*
482 * 3. Setup stuff
483 */
484 printf( " . Setting up the SSL/TLS structure..." );
485 fflush( stdout );
486
487 if( ( ret = ssl_init( &ssl ) ) != 0 )
488 {
489 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
490 goto exit;
491 }
492
493 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +0100494 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000495
496 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
497 ssl_set_dbg( &ssl, my_debug, stdout );
498
Paul Bakker0a597072012-09-25 21:55:46 +0000499#if defined(POLARSSL_SSL_CACHE_C)
500 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
501 ssl_cache_set, &cache );
502#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000503
Paul Bakker41c83d32013-03-20 14:39:14 +0100504 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000505 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
506
507 ssl_set_renegotiation( &ssl, opt.renegotiation );
508 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
509
Paul Bakkered27a042013-04-18 22:46:23 +0200510#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000511 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
512 ssl_set_own_cert( &ssl, &srvcert, &rsa );
Paul Bakkered27a042013-04-18 22:46:23 +0200513#endif
514
515#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Paul Bakkerfbb17802013-04-17 19:10:21 +0200516 ssl_set_psk( &ssl, psk, psk_len, (unsigned char *) opt.psk_identity,
517 strlen( opt.psk_identity ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200518#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000519
520#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +0000521 /*
522 * Use different group than default DHM group
523 */
Paul Bakkerd4324102012-09-26 08:29:38 +0000524 ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
525 POLARSSL_DHM_RFC5114_MODP_2048_G );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000526#endif
527
Paul Bakker1d29fb52012-09-28 13:28:45 +0000528 if( opt.min_version != -1 )
529 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
530
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000531 printf( " ok\n" );
532
533reset:
534#ifdef POLARSSL_ERROR_C
535 if( ret != 0 )
536 {
537 char error_buf[100];
538 error_strerror( ret, error_buf, 100 );
539 printf("Last error was: %d - %s\n\n", ret, error_buf );
540 }
541#endif
542
543 if( client_fd != -1 )
544 net_close( client_fd );
545
546 ssl_session_reset( &ssl );
547
548 /*
549 * 3. Wait until a client connects
550 */
551#if defined(_WIN32_WCE)
552 {
553 SHELLEXECUTEINFO sei;
554
555 ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) );
556
557 sei.cbSize = sizeof( SHELLEXECUTEINFO );
558 sei.fMask = 0;
559 sei.hwnd = 0;
560 sei.lpVerb = _T( "open" );
561 sei.lpFile = _T( "https://localhost:4433/" );
562 sei.lpParameters = NULL;
563 sei.lpDirectory = NULL;
564 sei.nShow = SW_SHOWNORMAL;
565
566 ShellExecuteEx( &sei );
567 }
568#elif defined(_WIN32)
569 ShellExecute( NULL, "open", "https://localhost:4433/",
570 NULL, NULL, SW_SHOWNORMAL );
571#endif
572
573 client_fd = -1;
574
575 printf( " . Waiting for a remote connection ..." );
576 fflush( stdout );
577
578 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
579 {
580 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
581 goto exit;
582 }
583
584 ssl_set_bio( &ssl, net_recv, &client_fd,
585 net_send, &client_fd );
586
587 printf( " ok\n" );
588
589 /*
590 * 4. Handshake
591 */
592 printf( " . Performing the SSL/TLS handshake..." );
593 fflush( stdout );
594
595 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
596 {
597 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
598 {
599 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000600 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000601 }
602 }
603
604 printf( " ok\n [ Ciphersuite is %s ]\n",
605 ssl_get_ciphersuite( &ssl ) );
606
Paul Bakkered27a042013-04-18 22:46:23 +0200607#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000608 /*
609 * 5. Verify the server certificate
610 */
611 printf( " . Verifying peer X.509 certificate..." );
612
613 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
614 {
615 printf( " failed\n" );
616
Paul Bakkerb0550d92012-10-30 07:51:03 +0000617 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000618 printf( " ! no client certificate sent\n" );
619
620 if( ( ret & BADCERT_EXPIRED ) != 0 )
621 printf( " ! client certificate has expired\n" );
622
623 if( ( ret & BADCERT_REVOKED ) != 0 )
624 printf( " ! client certificate has been revoked\n" );
625
626 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
627 printf( " ! self-signed or not signed by a trusted CA\n" );
628
629 printf( "\n" );
630 }
631 else
632 printf( " ok\n" );
633
Paul Bakkerb0550d92012-10-30 07:51:03 +0000634 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000635 {
636 printf( " . Peer certificate information ...\n" );
637 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
Paul Bakkerb0550d92012-10-30 07:51:03 +0000638 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000639 printf( "%s\n", buf );
640 }
Paul Bakkered27a042013-04-18 22:46:23 +0200641#endif /* POLARSSL_X509_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000642
643 /*
644 * 6. Read the HTTP Request
645 */
646 printf( " < Read from client:" );
647 fflush( stdout );
648
649 do
650 {
651 len = sizeof( buf ) - 1;
652 memset( buf, 0, sizeof( buf ) );
653 ret = ssl_read( &ssl, buf, len );
654
655 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
656 continue;
657
658 if( ret <= 0 )
659 {
660 switch( ret )
661 {
662 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
663 printf( " connection was closed gracefully\n" );
664 break;
665
666 case POLARSSL_ERR_NET_CONN_RESET:
667 printf( " connection was reset by peer\n" );
668 break;
669
670 default:
671 printf( " ssl_read returned -0x%x\n", -ret );
672 break;
673 }
674
675 break;
676 }
677
678 len = ret;
679 printf( " %d bytes read\n\n%s", len, (char *) buf );
680
681 if( ret > 0 )
682 break;
683 }
684 while( 1 );
685
686 /*
687 * 7. Write the 200 Response
688 */
689 printf( " > Write to client:" );
690 fflush( stdout );
691
692 len = sprintf( (char *) buf, HTTP_RESPONSE,
693 ssl_get_ciphersuite( &ssl ) );
694
695 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
696 {
697 if( ret == POLARSSL_ERR_NET_CONN_RESET )
698 {
699 printf( " failed\n ! peer closed the connection\n\n" );
700 goto reset;
701 }
702
703 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
704 {
705 printf( " failed\n ! ssl_write returned %d\n\n", ret );
706 goto exit;
707 }
708 }
709
710 len = ret;
711 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
712
713 ret = 0;
714 goto reset;
715
716exit:
717
718#ifdef POLARSSL_ERROR_C
719 if( ret != 0 )
720 {
721 char error_buf[100];
722 error_strerror( ret, error_buf, 100 );
723 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
724 }
725#endif
726
727 net_close( client_fd );
Paul Bakkered27a042013-04-18 22:46:23 +0200728#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000729 x509_free( &srvcert );
730 x509_free( &cacert );
731 rsa_free( &rsa );
Paul Bakkered27a042013-04-18 22:46:23 +0200732#endif
733
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000734 ssl_free( &ssl );
735
Paul Bakker0a597072012-09-25 21:55:46 +0000736#if defined(POLARSSL_SSL_CACHE_C)
737 ssl_cache_free( &cache );
738#endif
739
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000740#if defined(_WIN32)
741 printf( " + Press Enter to exit this program.\n" );
742 fflush( stdout ); getchar();
743#endif
744
745 return( ret );
746}
747#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
748 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
749 POLARSSL_CTR_DRBG_C */