blob: b8cc16c7e548ff00919f1a53304b3ca134873f22 [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
Paul Bakker875548c2014-07-08 12:21:41 +020060#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Paul Bakkerb60b95f2012-09-25 09:05:17 +000061#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 */
Paul Bakkere0225e42013-06-06 12:52:24 +020077 const char *ca_file; /* the file with the CA certificate(s) */
78 const char *ca_path; /* the path with the CA certificate(s) reside */
79 const char *crt_file; /* the file with the client certificate */
80 const char *key_file; /* the file with the client key */
Paul Bakkerb60b95f2012-09-25 09:05:17 +000081 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];
Paul Bakkere0225e42013-06-06 12:52:24 +0200239 const char *pers = "ssl_server2";
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000240
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,
Paul Bakkere0225e42013-06-06 12:52:24 +0200383 (const unsigned char *) pers,
384 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000385 {
386 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
387 goto exit;
388 }
389
390 printf( " ok\n" );
391
392 /*
393 * 1.1. Load the trusted CA
394 */
395 printf( " . Loading the CA root certificate ..." );
396 fflush( stdout );
397
398#if defined(POLARSSL_FS_IO)
399 if( strlen( opt.ca_path ) )
400 ret = x509parse_crtpath( &cacert, opt.ca_path );
401 else if( strlen( opt.ca_file ) )
402 ret = x509parse_crtfile( &cacert, opt.ca_file );
403 else
404#endif
405#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200406 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000407 strlen( test_ca_crt ) );
408#else
409 {
410 ret = 1;
411 printf("POLARSSL_CERTS_C not defined.");
412 }
413#endif
414 if( ret < 0 )
415 {
416 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
417 goto exit;
418 }
419
420 printf( " ok (%d skipped)\n", ret );
421
422 /*
423 * 1.2. Load own certificate and private key
424 */
425 printf( " . Loading the server cert. and key..." );
426 fflush( stdout );
427
428#if defined(POLARSSL_FS_IO)
429 if( strlen( opt.crt_file ) )
430 ret = x509parse_crtfile( &srvcert, opt.crt_file );
431 else
432#endif
433#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200434 ret = x509parse_crt( &srvcert, (const unsigned char *) test_srv_crt,
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000435 strlen( test_srv_crt ) );
436#else
437 {
438 ret = 1;
439 printf("POLARSSL_CERTS_C not defined.");
440 }
441#endif
442 if( ret != 0 )
443 {
444 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
445 goto exit;
446 }
447
448#if defined(POLARSSL_FS_IO)
449 if( strlen( opt.key_file ) )
450 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
451 else
452#endif
453#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200454 ret = x509parse_key( &rsa, (const unsigned char *) test_srv_key,
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000455 strlen( test_srv_key ), NULL, 0 );
456#else
457 {
458 ret = 1;
459 printf("POLARSSL_CERTS_C not defined.");
460 }
461#endif
462 if( ret != 0 )
463 {
464 printf( " failed\n ! x509parse_key returned -0x%x\n\n", -ret );
465 goto exit;
466 }
467
468 printf( " ok\n" );
469
470 /*
471 * 2. Setup the listening TCP socket
472 */
473 printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
474 fflush( stdout );
475
476 if( ( ret = net_bind( &listen_fd, NULL, opt.server_port ) ) != 0 )
477 {
478 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
479 goto exit;
480 }
481
482 printf( " ok\n" );
483
484 /*
485 * 3. Setup stuff
486 */
487 printf( " . Setting up the SSL/TLS structure..." );
488 fflush( stdout );
489
490 if( ( ret = ssl_init( &ssl ) ) != 0 )
491 {
492 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
493 goto exit;
494 }
495
496 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +0100497 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000498
499 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
500 ssl_set_dbg( &ssl, my_debug, stdout );
501
Paul Bakker0a597072012-09-25 21:55:46 +0000502#if defined(POLARSSL_SSL_CACHE_C)
503 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
504 ssl_cache_set, &cache );
505#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000506
507 if( opt.force_ciphersuite[0] == DFL_FORCE_CIPHER )
Paul Bakker645ce3a2012-10-31 12:32:41 +0000508 ssl_set_ciphersuites( &ssl, my_ciphersuites );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000509 else
510 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
511
512 ssl_set_renegotiation( &ssl, opt.renegotiation );
513 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
514
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000515 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
516 ssl_set_own_cert( &ssl, &srvcert, &rsa );
517
518#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +0000519 /*
520 * Use different group than default DHM group
521 */
Paul Bakkerd4324102012-09-26 08:29:38 +0000522 ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
523 POLARSSL_DHM_RFC5114_MODP_2048_G );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000524#endif
525
Paul Bakker1d29fb52012-09-28 13:28:45 +0000526 if( opt.min_version != -1 )
527 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
528
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000529 printf( " ok\n" );
530
531reset:
532#ifdef POLARSSL_ERROR_C
533 if( ret != 0 )
534 {
535 char error_buf[100];
536 error_strerror( ret, error_buf, 100 );
537 printf("Last error was: %d - %s\n\n", ret, error_buf );
538 }
539#endif
540
541 if( client_fd != -1 )
542 net_close( client_fd );
543
544 ssl_session_reset( &ssl );
545
546 /*
547 * 3. Wait until a client connects
548 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000549 client_fd = -1;
550
551 printf( " . Waiting for a remote connection ..." );
552 fflush( stdout );
553
554 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
555 {
556 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
557 goto exit;
558 }
559
560 ssl_set_bio( &ssl, net_recv, &client_fd,
561 net_send, &client_fd );
562
563 printf( " ok\n" );
564
565 /*
566 * 4. Handshake
567 */
568 printf( " . Performing the SSL/TLS handshake..." );
569 fflush( stdout );
570
571 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
572 {
573 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
574 {
575 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000576 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000577 }
578 }
579
580 printf( " ok\n [ Ciphersuite is %s ]\n",
581 ssl_get_ciphersuite( &ssl ) );
582
583 /*
584 * 5. Verify the server certificate
585 */
586 printf( " . Verifying peer X.509 certificate..." );
587
588 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
589 {
590 printf( " failed\n" );
591
Paul Bakkerb0550d92012-10-30 07:51:03 +0000592 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000593 printf( " ! no client certificate sent\n" );
594
595 if( ( ret & BADCERT_EXPIRED ) != 0 )
596 printf( " ! client certificate has expired\n" );
597
598 if( ( ret & BADCERT_REVOKED ) != 0 )
599 printf( " ! client certificate has been revoked\n" );
600
601 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
602 printf( " ! self-signed or not signed by a trusted CA\n" );
603
604 printf( "\n" );
605 }
606 else
607 printf( " ok\n" );
608
Paul Bakkerb0550d92012-10-30 07:51:03 +0000609 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000610 {
611 printf( " . Peer certificate information ...\n" );
612 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
Paul Bakkerb0550d92012-10-30 07:51:03 +0000613 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000614 printf( "%s\n", buf );
615 }
616
617 /*
618 * 6. Read the HTTP Request
619 */
620 printf( " < Read from client:" );
621 fflush( stdout );
622
623 do
624 {
625 len = sizeof( buf ) - 1;
626 memset( buf, 0, sizeof( buf ) );
627 ret = ssl_read( &ssl, buf, len );
628
629 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
630 continue;
631
632 if( ret <= 0 )
633 {
634 switch( ret )
635 {
636 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
637 printf( " connection was closed gracefully\n" );
638 break;
639
640 case POLARSSL_ERR_NET_CONN_RESET:
641 printf( " connection was reset by peer\n" );
642 break;
643
644 default:
645 printf( " ssl_read returned -0x%x\n", -ret );
646 break;
647 }
648
649 break;
650 }
651
652 len = ret;
653 printf( " %d bytes read\n\n%s", len, (char *) buf );
654
655 if( ret > 0 )
656 break;
657 }
658 while( 1 );
659
660 /*
661 * 7. Write the 200 Response
662 */
663 printf( " > Write to client:" );
664 fflush( stdout );
665
666 len = sprintf( (char *) buf, HTTP_RESPONSE,
667 ssl_get_ciphersuite( &ssl ) );
668
669 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
670 {
671 if( ret == POLARSSL_ERR_NET_CONN_RESET )
672 {
673 printf( " failed\n ! peer closed the connection\n\n" );
674 goto reset;
675 }
676
677 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
678 {
679 printf( " failed\n ! ssl_write returned %d\n\n", ret );
680 goto exit;
681 }
682 }
683
684 len = ret;
685 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
686
687 ret = 0;
688 goto reset;
689
690exit:
691
692#ifdef POLARSSL_ERROR_C
693 if( ret != 0 )
694 {
695 char error_buf[100];
696 error_strerror( ret, error_buf, 100 );
697 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
698 }
699#endif
700
701 net_close( client_fd );
702 x509_free( &srvcert );
703 x509_free( &cacert );
704 rsa_free( &rsa );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000705 ssl_free( &ssl );
706
Paul Bakker0a597072012-09-25 21:55:46 +0000707#if defined(POLARSSL_SSL_CACHE_C)
708 ssl_cache_free( &cache );
709#endif
710
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000711#if defined(_WIN32)
712 printf( " + Press Enter to exit this program.\n" );
713 fflush( stdout ); getchar();
714#endif
715
716 return( ret );
717}
718#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
719 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
720 POLARSSL_CTR_DRBG_C */