blob: 3cc3f8ab709b227f1efaa75bd4831b98dab26595 [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 Bakkera16e7f22014-07-09 14:58:11 +0200262 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker0a597072012-09-25 21:55:46 +0000263#if defined(POLARSSL_SSL_CACHE_C)
264 ssl_cache_init( &cache );
265#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000266
267 if( argc == 0 )
268 {
269 usage:
270 if( ret == 0 )
271 ret = 1;
272
273 printf( USAGE );
274
275 list = ssl_list_ciphersuites();
276 while( *list )
277 {
278 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
279 list++;
280 }
281 printf("\n");
282 goto exit;
283 }
284
285 opt.server_port = DFL_SERVER_PORT;
286 opt.debug_level = DFL_DEBUG_LEVEL;
287 opt.ca_file = DFL_CA_FILE;
288 opt.ca_path = DFL_CA_PATH;
289 opt.crt_file = DFL_CRT_FILE;
290 opt.key_file = DFL_KEY_FILE;
291 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
292 opt.renegotiation = DFL_RENEGOTIATION;
293 opt.allow_legacy = DFL_ALLOW_LEGACY;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000294 opt.min_version = DFL_MIN_VERSION;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100295 opt.auth_mode = DFL_AUTH_MODE;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000296
297 for( i = 1; i < argc; i++ )
298 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000299 p = argv[i];
300 if( ( q = strchr( p, '=' ) ) == NULL )
301 goto usage;
302 *q++ = '\0';
303
304 if( strcmp( p, "server_port" ) == 0 )
305 {
306 opt.server_port = atoi( q );
307 if( opt.server_port < 1 || opt.server_port > 65535 )
308 goto usage;
309 }
310 else if( strcmp( p, "debug_level" ) == 0 )
311 {
312 opt.debug_level = atoi( q );
313 if( opt.debug_level < 0 || opt.debug_level > 65535 )
314 goto usage;
315 }
316 else if( strcmp( p, "ca_file" ) == 0 )
317 opt.ca_file = q;
318 else if( strcmp( p, "ca_path" ) == 0 )
319 opt.ca_path = q;
320 else if( strcmp( p, "crt_file" ) == 0 )
321 opt.crt_file = q;
322 else if( strcmp( p, "key_file" ) == 0 )
323 opt.key_file = q;
324 else if( strcmp( p, "force_ciphersuite" ) == 0 )
325 {
326 opt.force_ciphersuite[0] = -1;
327
328 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
329
330 if( opt.force_ciphersuite[0] <= 0 )
331 {
332 ret = 2;
333 goto usage;
334 }
335 opt.force_ciphersuite[1] = 0;
336 }
337 else if( strcmp( p, "renegotiation" ) == 0 )
338 {
339 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
340 SSL_RENEGOTIATION_DISABLED;
341 }
342 else if( strcmp( p, "allow_legacy" ) == 0 )
343 {
344 opt.allow_legacy = atoi( q );
345 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
346 goto usage;
347 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000348 else if( strcmp( p, "min_version" ) == 0 )
349 {
350 if( strcmp( q, "ssl3" ) == 0 )
351 opt.min_version = SSL_MINOR_VERSION_0;
352 else if( strcmp( q, "tls1" ) == 0 )
353 opt.min_version = SSL_MINOR_VERSION_1;
354 else if( strcmp( q, "tls1_1" ) == 0 )
355 opt.min_version = SSL_MINOR_VERSION_2;
356 else if( strcmp( q, "tls1_2" ) == 0 )
357 opt.min_version = SSL_MINOR_VERSION_3;
358 else
359 goto usage;
360 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100361 else if( strcmp( p, "auth_mode" ) == 0 )
362 {
363 if( strcmp( q, "none" ) == 0 )
364 opt.auth_mode = SSL_VERIFY_NONE;
365 else if( strcmp( q, "optional" ) == 0 )
366 opt.auth_mode = SSL_VERIFY_OPTIONAL;
367 else if( strcmp( q, "required" ) == 0 )
368 opt.auth_mode = SSL_VERIFY_REQUIRED;
369 else
370 goto usage;
371 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000372 else
373 goto usage;
374 }
375
376 /*
377 * 0. Initialize the RNG and the session data
378 */
379 printf( "\n . Seeding the random number generator..." );
380 fflush( stdout );
381
382 entropy_init( &entropy );
383 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkere0225e42013-06-06 12:52:24 +0200384 (const unsigned char *) pers,
385 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000386 {
387 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
388 goto exit;
389 }
390
391 printf( " ok\n" );
392
393 /*
394 * 1.1. Load the trusted CA
395 */
396 printf( " . Loading the CA root certificate ..." );
397 fflush( stdout );
398
399#if defined(POLARSSL_FS_IO)
400 if( strlen( opt.ca_path ) )
401 ret = x509parse_crtpath( &cacert, opt.ca_path );
402 else if( strlen( opt.ca_file ) )
403 ret = x509parse_crtfile( &cacert, opt.ca_file );
404 else
405#endif
406#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200407 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000408 strlen( test_ca_crt ) );
409#else
410 {
411 ret = 1;
412 printf("POLARSSL_CERTS_C not defined.");
413 }
414#endif
415 if( ret < 0 )
416 {
417 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
418 goto exit;
419 }
420
421 printf( " ok (%d skipped)\n", ret );
422
423 /*
424 * 1.2. Load own certificate and private key
425 */
426 printf( " . Loading the server cert. and key..." );
427 fflush( stdout );
428
429#if defined(POLARSSL_FS_IO)
430 if( strlen( opt.crt_file ) )
431 ret = x509parse_crtfile( &srvcert, opt.crt_file );
432 else
433#endif
434#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200435 ret = x509parse_crt( &srvcert, (const unsigned char *) test_srv_crt,
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000436 strlen( test_srv_crt ) );
437#else
438 {
439 ret = 1;
440 printf("POLARSSL_CERTS_C not defined.");
441 }
442#endif
443 if( ret != 0 )
444 {
445 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
446 goto exit;
447 }
448
449#if defined(POLARSSL_FS_IO)
450 if( strlen( opt.key_file ) )
451 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
452 else
453#endif
454#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200455 ret = x509parse_key( &rsa, (const unsigned char *) test_srv_key,
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000456 strlen( test_srv_key ), NULL, 0 );
457#else
458 {
459 ret = 1;
460 printf("POLARSSL_CERTS_C not defined.");
461 }
462#endif
463 if( ret != 0 )
464 {
465 printf( " failed\n ! x509parse_key returned -0x%x\n\n", -ret );
466 goto exit;
467 }
468
469 printf( " ok\n" );
470
471 /*
472 * 2. Setup the listening TCP socket
473 */
474 printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
475 fflush( stdout );
476
477 if( ( ret = net_bind( &listen_fd, NULL, opt.server_port ) ) != 0 )
478 {
479 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
480 goto exit;
481 }
482
483 printf( " ok\n" );
484
485 /*
486 * 3. Setup stuff
487 */
488 printf( " . Setting up the SSL/TLS structure..." );
489 fflush( stdout );
490
491 if( ( ret = ssl_init( &ssl ) ) != 0 )
492 {
493 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
494 goto exit;
495 }
496
497 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +0100498 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000499
500 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
501 ssl_set_dbg( &ssl, my_debug, stdout );
502
Paul Bakker0a597072012-09-25 21:55:46 +0000503#if defined(POLARSSL_SSL_CACHE_C)
504 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
505 ssl_cache_set, &cache );
506#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000507
508 if( opt.force_ciphersuite[0] == DFL_FORCE_CIPHER )
Paul Bakker645ce3a2012-10-31 12:32:41 +0000509 ssl_set_ciphersuites( &ssl, my_ciphersuites );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000510 else
511 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
512
513 ssl_set_renegotiation( &ssl, opt.renegotiation );
514 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
515
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000516 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
517 ssl_set_own_cert( &ssl, &srvcert, &rsa );
518
519#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +0000520 /*
521 * Use different group than default DHM group
522 */
Paul Bakkerd4324102012-09-26 08:29:38 +0000523 ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
524 POLARSSL_DHM_RFC5114_MODP_2048_G );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000525#endif
526
Paul Bakker1d29fb52012-09-28 13:28:45 +0000527 if( opt.min_version != -1 )
528 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
529
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000530 printf( " ok\n" );
531
532reset:
533#ifdef POLARSSL_ERROR_C
534 if( ret != 0 )
535 {
536 char error_buf[100];
537 error_strerror( ret, error_buf, 100 );
538 printf("Last error was: %d - %s\n\n", ret, error_buf );
539 }
540#endif
541
542 if( client_fd != -1 )
543 net_close( client_fd );
544
545 ssl_session_reset( &ssl );
546
547 /*
548 * 3. Wait until a client connects
549 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000550 client_fd = -1;
551
552 printf( " . Waiting for a remote connection ..." );
553 fflush( stdout );
554
555 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
556 {
557 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
558 goto exit;
559 }
560
561 ssl_set_bio( &ssl, net_recv, &client_fd,
562 net_send, &client_fd );
563
564 printf( " ok\n" );
565
566 /*
567 * 4. Handshake
568 */
569 printf( " . Performing the SSL/TLS handshake..." );
570 fflush( stdout );
571
572 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
573 {
574 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
575 {
576 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000577 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000578 }
579 }
580
581 printf( " ok\n [ Ciphersuite is %s ]\n",
582 ssl_get_ciphersuite( &ssl ) );
583
584 /*
585 * 5. Verify the server certificate
586 */
587 printf( " . Verifying peer X.509 certificate..." );
588
589 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
590 {
591 printf( " failed\n" );
592
Paul Bakkerb0550d92012-10-30 07:51:03 +0000593 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000594 printf( " ! no client certificate sent\n" );
595
596 if( ( ret & BADCERT_EXPIRED ) != 0 )
597 printf( " ! client certificate has expired\n" );
598
599 if( ( ret & BADCERT_REVOKED ) != 0 )
600 printf( " ! client certificate has been revoked\n" );
601
602 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
603 printf( " ! self-signed or not signed by a trusted CA\n" );
604
605 printf( "\n" );
606 }
607 else
608 printf( " ok\n" );
609
Paul Bakkerb0550d92012-10-30 07:51:03 +0000610 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000611 {
612 printf( " . Peer certificate information ...\n" );
613 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
Paul Bakkerb0550d92012-10-30 07:51:03 +0000614 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000615 printf( "%s\n", buf );
616 }
617
618 /*
619 * 6. Read the HTTP Request
620 */
621 printf( " < Read from client:" );
622 fflush( stdout );
623
624 do
625 {
626 len = sizeof( buf ) - 1;
627 memset( buf, 0, sizeof( buf ) );
628 ret = ssl_read( &ssl, buf, len );
629
630 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
631 continue;
632
633 if( ret <= 0 )
634 {
635 switch( ret )
636 {
637 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
638 printf( " connection was closed gracefully\n" );
639 break;
640
641 case POLARSSL_ERR_NET_CONN_RESET:
642 printf( " connection was reset by peer\n" );
643 break;
644
645 default:
646 printf( " ssl_read returned -0x%x\n", -ret );
647 break;
648 }
649
650 break;
651 }
652
653 len = ret;
654 printf( " %d bytes read\n\n%s", len, (char *) buf );
655
656 if( ret > 0 )
657 break;
658 }
659 while( 1 );
660
661 /*
662 * 7. Write the 200 Response
663 */
664 printf( " > Write to client:" );
665 fflush( stdout );
666
667 len = sprintf( (char *) buf, HTTP_RESPONSE,
668 ssl_get_ciphersuite( &ssl ) );
669
670 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
671 {
672 if( ret == POLARSSL_ERR_NET_CONN_RESET )
673 {
674 printf( " failed\n ! peer closed the connection\n\n" );
675 goto reset;
676 }
677
678 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
679 {
680 printf( " failed\n ! ssl_write returned %d\n\n", ret );
681 goto exit;
682 }
683 }
684
685 len = ret;
686 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
687
Paul Bakker3cbaf1e2014-07-08 12:26:02 +0200688 printf( " . Closing the connection..." );
689
690 while( ( ret = ssl_close_notify( &ssl ) ) < 0 )
691 {
692 if( ret != POLARSSL_ERR_NET_WANT_READ &&
693 ret != POLARSSL_ERR_NET_WANT_WRITE )
694 {
695 printf( " failed\n ! ssl_close_notify returned %d\n\n", ret );
696 goto reset;
697 }
698 }
699
700 printf( " ok\n" );
701
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000702 ret = 0;
703 goto reset;
704
705exit:
706
707#ifdef POLARSSL_ERROR_C
708 if( ret != 0 )
709 {
710 char error_buf[100];
711 error_strerror( ret, error_buf, 100 );
712 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
713 }
714#endif
715
Paul Bakker39148402014-04-17 16:02:36 +0200716
717 if( client_fd != -1 )
718 net_close( client_fd );
719
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000720 x509_free( &srvcert );
721 x509_free( &cacert );
722 rsa_free( &rsa );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000723 ssl_free( &ssl );
724
Paul Bakker0a597072012-09-25 21:55:46 +0000725#if defined(POLARSSL_SSL_CACHE_C)
726 ssl_cache_free( &cache );
727#endif
728
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000729#if defined(_WIN32)
730 printf( " + Press Enter to exit this program.\n" );
731 fflush( stdout ); getchar();
732#endif
733
734 return( ret );
735}
736#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
737 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
738 POLARSSL_CTR_DRBG_C */