blob: f788f5e6a5fb2a4490a254ccd870ecf07f18d94a [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 */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020079 const char *ca_file; /* the file with the CA certificate(s) */
80 const char *ca_path; /* the path with the CA certificate(s) reside */
81 const char *crt_file; /* the file with the client certificate */
82 const char *key_file; /* the file with the client key */
83 const char *psk; /* the pre-shared key */
84 const 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 Bakkeref3f8c72013-06-24 13:01:08 +0200171 const char *pers = "ssl_server2";
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000172
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 Bakkerbcbe2d82013-04-19 09:10:20 +0200213 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200214 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,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200379 (const unsigned char *) pers,
380 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000381 {
382 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
383 goto exit;
384 }
385
386 printf( " ok\n" );
387
Paul Bakkered27a042013-04-18 22:46:23 +0200388#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000389 /*
390 * 1.1. Load the trusted CA
391 */
392 printf( " . Loading the CA root certificate ..." );
393 fflush( stdout );
394
395#if defined(POLARSSL_FS_IO)
396 if( strlen( opt.ca_path ) )
397 ret = x509parse_crtpath( &cacert, opt.ca_path );
398 else if( strlen( opt.ca_file ) )
399 ret = x509parse_crtfile( &cacert, opt.ca_file );
400 else
401#endif
402#if defined(POLARSSL_CERTS_C)
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200403 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000404 strlen( test_ca_crt ) );
405#else
406 {
407 ret = 1;
408 printf("POLARSSL_CERTS_C not defined.");
409 }
410#endif
411 if( ret < 0 )
412 {
413 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
414 goto exit;
415 }
416
417 printf( " ok (%d skipped)\n", ret );
418
419 /*
420 * 1.2. Load own certificate and private key
421 */
422 printf( " . Loading the server cert. and key..." );
423 fflush( stdout );
424
425#if defined(POLARSSL_FS_IO)
426 if( strlen( opt.crt_file ) )
427 ret = x509parse_crtfile( &srvcert, opt.crt_file );
428 else
429#endif
430#if defined(POLARSSL_CERTS_C)
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200431 ret = x509parse_crt( &srvcert, (const unsigned char *) test_srv_crt,
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000432 strlen( test_srv_crt ) );
433#else
434 {
435 ret = 1;
436 printf("POLARSSL_CERTS_C not defined.");
437 }
438#endif
439 if( ret != 0 )
440 {
441 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
442 goto exit;
443 }
444
445#if defined(POLARSSL_FS_IO)
446 if( strlen( opt.key_file ) )
447 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
448 else
449#endif
450#if defined(POLARSSL_CERTS_C)
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200451 ret = x509parse_key( &rsa, (const unsigned char *) test_srv_key,
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000452 strlen( test_srv_key ), NULL, 0 );
453#else
454 {
455 ret = 1;
456 printf("POLARSSL_CERTS_C not defined.");
457 }
458#endif
459 if( ret != 0 )
460 {
461 printf( " failed\n ! x509parse_key returned -0x%x\n\n", -ret );
462 goto exit;
463 }
464
465 printf( " ok\n" );
Paul Bakkered27a042013-04-18 22:46:23 +0200466#endif /* POLARSSL_X509_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000467
468 /*
469 * 2. Setup the listening TCP socket
470 */
471 printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
472 fflush( stdout );
473
474 if( ( ret = net_bind( &listen_fd, NULL, opt.server_port ) ) != 0 )
475 {
476 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
477 goto exit;
478 }
479
480 printf( " ok\n" );
481
482 /*
483 * 3. Setup stuff
484 */
485 printf( " . Setting up the SSL/TLS structure..." );
486 fflush( stdout );
487
488 if( ( ret = ssl_init( &ssl ) ) != 0 )
489 {
490 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
491 goto exit;
492 }
493
494 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +0100495 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000496
497 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
498 ssl_set_dbg( &ssl, my_debug, stdout );
499
Paul Bakker0a597072012-09-25 21:55:46 +0000500#if defined(POLARSSL_SSL_CACHE_C)
501 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
502 ssl_cache_set, &cache );
503#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000504
Paul Bakker41c83d32013-03-20 14:39:14 +0100505 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000506 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
507
508 ssl_set_renegotiation( &ssl, opt.renegotiation );
509 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
510
Paul Bakkered27a042013-04-18 22:46:23 +0200511#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000512 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
513 ssl_set_own_cert( &ssl, &srvcert, &rsa );
Paul Bakkered27a042013-04-18 22:46:23 +0200514#endif
515
516#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Paul Bakkerfbb17802013-04-17 19:10:21 +0200517 ssl_set_psk( &ssl, psk, psk_len, (unsigned char *) opt.psk_identity,
518 strlen( opt.psk_identity ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200519#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000520
521#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +0000522 /*
523 * Use different group than default DHM group
524 */
Paul Bakkerd4324102012-09-26 08:29:38 +0000525 ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
526 POLARSSL_DHM_RFC5114_MODP_2048_G );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000527#endif
528
Paul Bakker1d29fb52012-09-28 13:28:45 +0000529 if( opt.min_version != -1 )
530 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
531
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000532 printf( " ok\n" );
533
534reset:
535#ifdef POLARSSL_ERROR_C
536 if( ret != 0 )
537 {
538 char error_buf[100];
539 error_strerror( ret, error_buf, 100 );
540 printf("Last error was: %d - %s\n\n", ret, error_buf );
541 }
542#endif
543
544 if( client_fd != -1 )
545 net_close( client_fd );
546
547 ssl_session_reset( &ssl );
548
549 /*
550 * 3. Wait until a client connects
551 */
552#if defined(_WIN32_WCE)
553 {
554 SHELLEXECUTEINFO sei;
555
556 ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) );
557
558 sei.cbSize = sizeof( SHELLEXECUTEINFO );
559 sei.fMask = 0;
560 sei.hwnd = 0;
561 sei.lpVerb = _T( "open" );
562 sei.lpFile = _T( "https://localhost:4433/" );
563 sei.lpParameters = NULL;
564 sei.lpDirectory = NULL;
565 sei.nShow = SW_SHOWNORMAL;
566
567 ShellExecuteEx( &sei );
568 }
569#elif defined(_WIN32)
570 ShellExecute( NULL, "open", "https://localhost:4433/",
571 NULL, NULL, SW_SHOWNORMAL );
572#endif
573
574 client_fd = -1;
575
576 printf( " . Waiting for a remote connection ..." );
577 fflush( stdout );
578
579 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
580 {
581 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
582 goto exit;
583 }
584
585 ssl_set_bio( &ssl, net_recv, &client_fd,
586 net_send, &client_fd );
587
588 printf( " ok\n" );
589
590 /*
591 * 4. Handshake
592 */
593 printf( " . Performing the SSL/TLS handshake..." );
594 fflush( stdout );
595
596 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
597 {
598 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
599 {
600 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000601 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000602 }
603 }
604
605 printf( " ok\n [ Ciphersuite is %s ]\n",
606 ssl_get_ciphersuite( &ssl ) );
607
Paul Bakkered27a042013-04-18 22:46:23 +0200608#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000609 /*
610 * 5. Verify the server certificate
611 */
612 printf( " . Verifying peer X.509 certificate..." );
613
614 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
615 {
616 printf( " failed\n" );
617
Paul Bakkerb0550d92012-10-30 07:51:03 +0000618 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000619 printf( " ! no client certificate sent\n" );
620
621 if( ( ret & BADCERT_EXPIRED ) != 0 )
622 printf( " ! client certificate has expired\n" );
623
624 if( ( ret & BADCERT_REVOKED ) != 0 )
625 printf( " ! client certificate has been revoked\n" );
626
627 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
628 printf( " ! self-signed or not signed by a trusted CA\n" );
629
630 printf( "\n" );
631 }
632 else
633 printf( " ok\n" );
634
Paul Bakkerb0550d92012-10-30 07:51:03 +0000635 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000636 {
637 printf( " . Peer certificate information ...\n" );
638 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
Paul Bakkerb0550d92012-10-30 07:51:03 +0000639 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000640 printf( "%s\n", buf );
641 }
Paul Bakkered27a042013-04-18 22:46:23 +0200642#endif /* POLARSSL_X509_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000643
644 /*
645 * 6. Read the HTTP Request
646 */
647 printf( " < Read from client:" );
648 fflush( stdout );
649
650 do
651 {
652 len = sizeof( buf ) - 1;
653 memset( buf, 0, sizeof( buf ) );
654 ret = ssl_read( &ssl, buf, len );
655
656 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
657 continue;
658
659 if( ret <= 0 )
660 {
661 switch( ret )
662 {
663 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
664 printf( " connection was closed gracefully\n" );
665 break;
666
667 case POLARSSL_ERR_NET_CONN_RESET:
668 printf( " connection was reset by peer\n" );
669 break;
670
671 default:
672 printf( " ssl_read returned -0x%x\n", -ret );
673 break;
674 }
675
676 break;
677 }
678
679 len = ret;
680 printf( " %d bytes read\n\n%s", len, (char *) buf );
681
682 if( ret > 0 )
683 break;
684 }
685 while( 1 );
686
687 /*
688 * 7. Write the 200 Response
689 */
690 printf( " > Write to client:" );
691 fflush( stdout );
692
693 len = sprintf( (char *) buf, HTTP_RESPONSE,
694 ssl_get_ciphersuite( &ssl ) );
695
696 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
697 {
698 if( ret == POLARSSL_ERR_NET_CONN_RESET )
699 {
700 printf( " failed\n ! peer closed the connection\n\n" );
701 goto reset;
702 }
703
704 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
705 {
706 printf( " failed\n ! ssl_write returned %d\n\n", ret );
707 goto exit;
708 }
709 }
710
711 len = ret;
712 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
713
714 ret = 0;
715 goto reset;
716
717exit:
718
719#ifdef POLARSSL_ERROR_C
720 if( ret != 0 )
721 {
722 char error_buf[100];
723 error_strerror( ret, error_buf, 100 );
724 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
725 }
726#endif
727
728 net_close( client_fd );
Paul Bakkered27a042013-04-18 22:46:23 +0200729#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000730 x509_free( &srvcert );
731 x509_free( &cacert );
732 rsa_free( &rsa );
Paul Bakkered27a042013-04-18 22:46:23 +0200733#endif
734
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000735 ssl_free( &ssl );
736
Paul Bakker0a597072012-09-25 21:55:46 +0000737#if defined(POLARSSL_SSL_CACHE_C)
738 ssl_cache_free( &cache );
739#endif
740
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000741#if defined(_WIN32)
742 printf( " + Press Enter to exit this program.\n" );
743 fflush( stdout ); getchar();
744#endif
745
746 return( ret );
747}
748#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
749 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
750 POLARSSL_CTR_DRBG_C */