blob: 770a5ccfeb4b9700982e7eb4650720d649bfdc0f [file] [log] [blame]
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001/*
2 * SSL client with options
3 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00004 * Copyright (C) 2006-2012, ARM Limited, All Rights Reserved
Paul Bakkerb60b95f2012-09-25 09:05:17 +00005 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#ifndef _CRT_SECURE_NO_DEPRECATE
24#define _CRT_SECURE_NO_DEPRECATE 1
25#endif
26
27#if defined(_WIN32)
28#include <windows.h>
29#endif
30
31#include <string.h>
32#include <stdlib.h>
33#include <stdio.h>
34
35#include "polarssl/config.h"
36
37#include "polarssl/net.h"
38#include "polarssl/ssl.h"
39#include "polarssl/entropy.h"
40#include "polarssl/ctr_drbg.h"
41#include "polarssl/certs.h"
42#include "polarssl/x509.h"
43#include "polarssl/error.h"
44
Paul Bakker0a597072012-09-25 21:55:46 +000045#if defined(POLARSSL_SSL_CACHE_C)
46#include "polarssl/ssl_cache.h"
47#endif
48
Paul Bakkerb60b95f2012-09-25 09:05:17 +000049#define DFL_SERVER_PORT 4433
50#define DFL_REQUEST_PAGE "/"
51#define DFL_DEBUG_LEVEL 0
52#define DFL_CA_FILE ""
53#define DFL_CA_PATH ""
54#define DFL_CRT_FILE ""
55#define DFL_KEY_FILE ""
56#define DFL_FORCE_CIPHER 0
Paul Bakker875548c2014-07-08 12:21:41 +020057#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Paul Bakkerb60b95f2012-09-25 09:05:17 +000058#define DFL_ALLOW_LEGACY SSL_LEGACY_NO_RENEGOTIATION
Paul Bakker1d29fb52012-09-28 13:28:45 +000059#define DFL_MIN_VERSION -1
Paul Bakker91ebfb52012-11-23 14:04:08 +010060#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000061
62#define HTTP_RESPONSE \
63 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
64 "<h2>PolarSSL Test Server</h2>\r\n" \
65 "<p>Successful connection using: %s</p>\r\n"
66
67/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +000068 * global options
69 */
70struct options
71{
72 int server_port; /* port on which the ssl service runs */
73 int debug_level; /* level of debugging */
Paul Bakkere0225e42013-06-06 12:52:24 +020074 const char *ca_file; /* the file with the CA certificate(s) */
75 const char *ca_path; /* the path with the CA certificate(s) reside */
76 const char *crt_file; /* the file with the client certificate */
77 const char *key_file; /* the file with the client key */
Paul Bakkerb60b95f2012-09-25 09:05:17 +000078 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
79 int renegotiation; /* enable / disable renegotiation */
80 int allow_legacy; /* allow legacy renegotiation */
Paul Bakker1d29fb52012-09-28 13:28:45 +000081 int min_version; /* minimum protocol version accepted */
Paul Bakker91ebfb52012-11-23 14:04:08 +010082 int auth_mode; /* verify mode for connection */
Paul Bakkerb60b95f2012-09-25 09:05:17 +000083} opt;
84
85void my_debug( void *ctx, int level, const char *str )
86{
87 if( level < opt.debug_level )
88 {
89 fprintf( (FILE *) ctx, "%s", str );
90 fflush( (FILE *) ctx );
91 }
92}
93
Paul Bakker645ce3a2012-10-31 12:32:41 +000094/*
95 * Sorted by order of preference
96 */
97int my_ciphersuites[] =
98{
99#if defined(POLARSSL_DHM_C)
100#if defined(POLARSSL_AES_C)
101#if defined(POLARSSL_SHA2_C)
102 TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,
103#endif /* POLARSSL_SHA2_C */
104#if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA4_C)
105 TLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
106#endif
107 TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
108#if defined(POLARSSL_SHA2_C)
109 TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
110#endif
111#if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA2_C)
112 TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
113#endif
114 TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
115#endif
116#if defined(POLARSSL_CAMELLIA_C)
117#if defined(POLARSSL_SHA2_C)
118 TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256,
119#endif /* POLARSSL_SHA2_C */
120 TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA,
121#if defined(POLARSSL_SHA2_C)
122 TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256,
123#endif /* POLARSSL_SHA2_C */
124 TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA,
125#endif
126#if defined(POLARSSL_DES_C)
127 TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA,
128#endif
129#endif
130
131#if defined(POLARSSL_AES_C)
132#if defined(POLARSSL_SHA2_C)
133 TLS_RSA_WITH_AES_256_CBC_SHA256,
134#endif /* POLARSSL_SHA2_C */
135#if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA4_C)
136 TLS_RSA_WITH_AES_256_GCM_SHA384,
137#endif /* POLARSSL_SHA2_C */
138 TLS_RSA_WITH_AES_256_CBC_SHA,
139#endif
140#if defined(POLARSSL_CAMELLIA_C)
141#if defined(POLARSSL_SHA2_C)
142 TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256,
143#endif /* POLARSSL_SHA2_C */
144 TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,
145#endif
146#if defined(POLARSSL_AES_C)
147#if defined(POLARSSL_SHA2_C)
148 TLS_RSA_WITH_AES_128_CBC_SHA256,
149#endif /* POLARSSL_SHA2_C */
150#if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA2_C)
151 TLS_RSA_WITH_AES_128_GCM_SHA256,
152#endif /* POLARSSL_SHA2_C */
153 TLS_RSA_WITH_AES_128_CBC_SHA,
154#endif
155#if defined(POLARSSL_CAMELLIA_C)
156#if defined(POLARSSL_SHA2_C)
157 TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256,
158#endif /* POLARSSL_SHA2_C */
159 TLS_RSA_WITH_CAMELLIA_128_CBC_SHA,
160#endif
161#if defined(POLARSSL_DES_C)
162 TLS_RSA_WITH_3DES_EDE_CBC_SHA,
163#endif
164#if defined(POLARSSL_ARC4_C)
165 TLS_RSA_WITH_RC4_128_SHA,
166 TLS_RSA_WITH_RC4_128_MD5,
167#endif
168
169#if defined(POLARSSL_ENABLE_WEAK_CIPHERSUITES)
170#if defined(POLARSSL_DES_C)
171 TLS_DHE_RSA_WITH_DES_CBC_SHA,
172 TLS_RSA_WITH_DES_CBC_SHA,
173#endif
174#if defined(POLARSSL_CIPHER_NULL_CIPHER)
175 TLS_RSA_WITH_NULL_MD5,
176 TLS_RSA_WITH_NULL_SHA,
177 TLS_RSA_WITH_NULL_SHA256,
178#endif
179#endif
180 0
181};
182
183
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000184#if defined(POLARSSL_FS_IO)
185#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100186 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
187 " default: \"\" (pre-loaded)\n" \
188 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
189 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
190 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
191 " default: \"\" (pre-loaded)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000192 " key_file=%%s default: \"\" (pre-loaded)\n"
193#else
194#define USAGE_IO \
195 " No file operations available (POLARSSL_FS_IO not defined)\n"
196#endif /* POLARSSL_FS_IO */
197
198#define USAGE \
199 "\n usage: ssl_server2 param=<>...\n" \
200 "\n acceptable parameters:\n" \
201 " server_port=%%d default: 4433\n" \
202 " debug_level=%%d default: 0 (disabled)\n" \
203 USAGE_IO \
204 " request_page=%%s default: \".\"\n" \
205 " renegotiation=%%d default: 1 (enabled)\n" \
206 " allow_legacy=%%d default: 0 (disabled)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000207 " min_version=%%s default: \"ssl3\"\n" \
208 " options: ssl3, tls1, tls1_1, tls1_2\n" \
Paul Bakker91ebfb52012-11-23 14:04:08 +0100209 " auth_mode=%%s default: \"optional\"\n" \
210 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000211 " force_ciphersuite=<name> default: all enabled\n"\
212 " acceptable ciphersuite names:\n"
213
214#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
215 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
216 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
217 !defined(POLARSSL_CTR_DRBG_C)
218int main( int argc, char *argv[] )
219{
220 ((void) argc);
221 ((void) argv);
222
223 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
224 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
225 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
226 "POLARSSL_CTR_DRBG_C not defined.\n");
227 return( 0 );
228}
229#else
230int main( int argc, char *argv[] )
231{
232 int ret = 0, len;
233 int listen_fd;
234 int client_fd = -1;
235 unsigned char buf[1024];
Paul Bakkere0225e42013-06-06 12:52:24 +0200236 const char *pers = "ssl_server2";
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000237
238 entropy_context entropy;
239 ctr_drbg_context ctr_drbg;
240 ssl_context ssl;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000241 x509_cert cacert;
242 x509_cert srvcert;
243 rsa_context rsa;
Paul Bakker0a597072012-09-25 21:55:46 +0000244#if defined(POLARSSL_SSL_CACHE_C)
245 ssl_cache_context cache;
246#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000247
248 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000249 char *p, *q;
250 const int *list;
251
252 /*
253 * Make sure memory references are valid.
254 */
255 listen_fd = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000256 memset( &cacert, 0, sizeof( x509_cert ) );
257 memset( &srvcert, 0, sizeof( x509_cert ) );
258 memset( &rsa, 0, sizeof( rsa_context ) );
Paul Bakkera16e7f22014-07-09 14:58:11 +0200259 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker0a597072012-09-25 21:55:46 +0000260#if defined(POLARSSL_SSL_CACHE_C)
261 ssl_cache_init( &cache );
262#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000263
264 if( argc == 0 )
265 {
266 usage:
267 if( ret == 0 )
268 ret = 1;
269
270 printf( USAGE );
271
272 list = ssl_list_ciphersuites();
273 while( *list )
274 {
275 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
276 list++;
277 }
278 printf("\n");
279 goto exit;
280 }
281
282 opt.server_port = DFL_SERVER_PORT;
283 opt.debug_level = DFL_DEBUG_LEVEL;
284 opt.ca_file = DFL_CA_FILE;
285 opt.ca_path = DFL_CA_PATH;
286 opt.crt_file = DFL_CRT_FILE;
287 opt.key_file = DFL_KEY_FILE;
288 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
289 opt.renegotiation = DFL_RENEGOTIATION;
290 opt.allow_legacy = DFL_ALLOW_LEGACY;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000291 opt.min_version = DFL_MIN_VERSION;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100292 opt.auth_mode = DFL_AUTH_MODE;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000293
294 for( i = 1; i < argc; i++ )
295 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000296 p = argv[i];
297 if( ( q = strchr( p, '=' ) ) == NULL )
298 goto usage;
299 *q++ = '\0';
300
301 if( strcmp( p, "server_port" ) == 0 )
302 {
303 opt.server_port = atoi( q );
304 if( opt.server_port < 1 || opt.server_port > 65535 )
305 goto usage;
306 }
307 else if( strcmp( p, "debug_level" ) == 0 )
308 {
309 opt.debug_level = atoi( q );
310 if( opt.debug_level < 0 || opt.debug_level > 65535 )
311 goto usage;
312 }
313 else if( strcmp( p, "ca_file" ) == 0 )
314 opt.ca_file = q;
315 else if( strcmp( p, "ca_path" ) == 0 )
316 opt.ca_path = q;
317 else if( strcmp( p, "crt_file" ) == 0 )
318 opt.crt_file = q;
319 else if( strcmp( p, "key_file" ) == 0 )
320 opt.key_file = q;
321 else if( strcmp( p, "force_ciphersuite" ) == 0 )
322 {
323 opt.force_ciphersuite[0] = -1;
324
325 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
326
327 if( opt.force_ciphersuite[0] <= 0 )
328 {
329 ret = 2;
330 goto usage;
331 }
332 opt.force_ciphersuite[1] = 0;
333 }
334 else if( strcmp( p, "renegotiation" ) == 0 )
335 {
336 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
337 SSL_RENEGOTIATION_DISABLED;
338 }
339 else if( strcmp( p, "allow_legacy" ) == 0 )
340 {
341 opt.allow_legacy = atoi( q );
342 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
343 goto usage;
344 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000345 else if( strcmp( p, "min_version" ) == 0 )
346 {
347 if( strcmp( q, "ssl3" ) == 0 )
348 opt.min_version = SSL_MINOR_VERSION_0;
349 else if( strcmp( q, "tls1" ) == 0 )
350 opt.min_version = SSL_MINOR_VERSION_1;
351 else if( strcmp( q, "tls1_1" ) == 0 )
352 opt.min_version = SSL_MINOR_VERSION_2;
353 else if( strcmp( q, "tls1_2" ) == 0 )
354 opt.min_version = SSL_MINOR_VERSION_3;
355 else
356 goto usage;
357 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100358 else if( strcmp( p, "auth_mode" ) == 0 )
359 {
360 if( strcmp( q, "none" ) == 0 )
361 opt.auth_mode = SSL_VERIFY_NONE;
362 else if( strcmp( q, "optional" ) == 0 )
363 opt.auth_mode = SSL_VERIFY_OPTIONAL;
364 else if( strcmp( q, "required" ) == 0 )
365 opt.auth_mode = SSL_VERIFY_REQUIRED;
366 else
367 goto usage;
368 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000369 else
370 goto usage;
371 }
372
373 /*
374 * 0. Initialize the RNG and the session data
375 */
376 printf( "\n . Seeding the random number generator..." );
377 fflush( stdout );
378
379 entropy_init( &entropy );
380 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkere0225e42013-06-06 12:52:24 +0200381 (const unsigned char *) pers,
382 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000383 {
384 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
385 goto exit;
386 }
387
388 printf( " ok\n" );
389
390 /*
391 * 1.1. Load the trusted CA
392 */
393 printf( " . Loading the CA root certificate ..." );
394 fflush( stdout );
395
396#if defined(POLARSSL_FS_IO)
397 if( strlen( opt.ca_path ) )
398 ret = x509parse_crtpath( &cacert, opt.ca_path );
399 else if( strlen( opt.ca_file ) )
400 ret = x509parse_crtfile( &cacert, opt.ca_file );
401 else
402#endif
403#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200404 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000405 strlen( test_ca_crt ) );
406#else
407 {
408 ret = 1;
409 printf("POLARSSL_CERTS_C not defined.");
410 }
411#endif
412 if( ret < 0 )
413 {
414 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
415 goto exit;
416 }
417
418 printf( " ok (%d skipped)\n", ret );
419
420 /*
421 * 1.2. Load own certificate and private key
422 */
423 printf( " . Loading the server cert. and key..." );
424 fflush( stdout );
425
426#if defined(POLARSSL_FS_IO)
427 if( strlen( opt.crt_file ) )
428 ret = x509parse_crtfile( &srvcert, opt.crt_file );
429 else
430#endif
431#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200432 ret = x509parse_crt( &srvcert, (const unsigned char *) test_srv_crt,
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000433 strlen( test_srv_crt ) );
434#else
435 {
436 ret = 1;
437 printf("POLARSSL_CERTS_C not defined.");
438 }
439#endif
440 if( ret != 0 )
441 {
442 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
443 goto exit;
444 }
445
446#if defined(POLARSSL_FS_IO)
447 if( strlen( opt.key_file ) )
448 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
449 else
450#endif
451#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200452 ret = x509parse_key( &rsa, (const unsigned char *) test_srv_key,
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000453 strlen( test_srv_key ), NULL, 0 );
454#else
455 {
456 ret = 1;
457 printf("POLARSSL_CERTS_C not defined.");
458 }
459#endif
460 if( ret != 0 )
461 {
462 printf( " failed\n ! x509parse_key returned -0x%x\n\n", -ret );
463 goto exit;
464 }
465
466 printf( " ok\n" );
467
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
505 if( opt.force_ciphersuite[0] == DFL_FORCE_CIPHER )
Paul Bakker645ce3a2012-10-31 12:32:41 +0000506 ssl_set_ciphersuites( &ssl, my_ciphersuites );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000507 else
508 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
509
510 ssl_set_renegotiation( &ssl, opt.renegotiation );
511 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
512
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000513 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
514 ssl_set_own_cert( &ssl, &srvcert, &rsa );
515
516#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +0000517 /*
518 * Use different group than default DHM group
519 */
Paul Bakkerd4324102012-09-26 08:29:38 +0000520 ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
521 POLARSSL_DHM_RFC5114_MODP_2048_G );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000522#endif
523
Paul Bakker1d29fb52012-09-28 13:28:45 +0000524 if( opt.min_version != -1 )
525 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
526
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000527 printf( " ok\n" );
528
529reset:
530#ifdef POLARSSL_ERROR_C
531 if( ret != 0 )
532 {
533 char error_buf[100];
534 error_strerror( ret, error_buf, 100 );
535 printf("Last error was: %d - %s\n\n", ret, error_buf );
536 }
537#endif
538
539 if( client_fd != -1 )
540 net_close( client_fd );
541
542 ssl_session_reset( &ssl );
543
544 /*
545 * 3. Wait until a client connects
546 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000547 client_fd = -1;
548
549 printf( " . Waiting for a remote connection ..." );
550 fflush( stdout );
551
552 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
553 {
554 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
555 goto exit;
556 }
557
558 ssl_set_bio( &ssl, net_recv, &client_fd,
559 net_send, &client_fd );
560
561 printf( " ok\n" );
562
563 /*
564 * 4. Handshake
565 */
566 printf( " . Performing the SSL/TLS handshake..." );
567 fflush( stdout );
568
569 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
570 {
571 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
572 {
573 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000574 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000575 }
576 }
577
578 printf( " ok\n [ Ciphersuite is %s ]\n",
579 ssl_get_ciphersuite( &ssl ) );
580
581 /*
582 * 5. Verify the server certificate
583 */
584 printf( " . Verifying peer X.509 certificate..." );
585
586 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
587 {
588 printf( " failed\n" );
589
Paul Bakkerb0550d92012-10-30 07:51:03 +0000590 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000591 printf( " ! no client certificate sent\n" );
592
593 if( ( ret & BADCERT_EXPIRED ) != 0 )
594 printf( " ! client certificate has expired\n" );
595
596 if( ( ret & BADCERT_REVOKED ) != 0 )
597 printf( " ! client certificate has been revoked\n" );
598
599 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
600 printf( " ! self-signed or not signed by a trusted CA\n" );
601
602 printf( "\n" );
603 }
604 else
605 printf( " ok\n" );
606
Paul Bakkerb0550d92012-10-30 07:51:03 +0000607 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000608 {
609 printf( " . Peer certificate information ...\n" );
610 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
Paul Bakkerb0550d92012-10-30 07:51:03 +0000611 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000612 printf( "%s\n", buf );
613 }
614
615 /*
616 * 6. Read the HTTP Request
617 */
618 printf( " < Read from client:" );
619 fflush( stdout );
620
621 do
622 {
623 len = sizeof( buf ) - 1;
624 memset( buf, 0, sizeof( buf ) );
625 ret = ssl_read( &ssl, buf, len );
626
627 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
628 continue;
629
630 if( ret <= 0 )
631 {
632 switch( ret )
633 {
634 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
635 printf( " connection was closed gracefully\n" );
636 break;
637
638 case POLARSSL_ERR_NET_CONN_RESET:
639 printf( " connection was reset by peer\n" );
640 break;
641
642 default:
643 printf( " ssl_read returned -0x%x\n", -ret );
644 break;
645 }
646
647 break;
648 }
649
650 len = ret;
651 printf( " %d bytes read\n\n%s", len, (char *) buf );
652
Manuel Pégourié-Gonnardaa02dc12014-11-20 17:28:18 +0100653 if( len >= 10 && memcmp( buf, "SERVERQUIT", 10 ) == 0 )
654 {
655 ret = 0;
656 goto exit;
657 }
658
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000659 if( ret > 0 )
660 break;
661 }
662 while( 1 );
663
664 /*
665 * 7. Write the 200 Response
666 */
667 printf( " > Write to client:" );
668 fflush( stdout );
669
670 len = sprintf( (char *) buf, HTTP_RESPONSE,
671 ssl_get_ciphersuite( &ssl ) );
672
673 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
674 {
675 if( ret == POLARSSL_ERR_NET_CONN_RESET )
676 {
677 printf( " failed\n ! peer closed the connection\n\n" );
678 goto reset;
679 }
680
681 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
682 {
683 printf( " failed\n ! ssl_write returned %d\n\n", ret );
684 goto exit;
685 }
686 }
687
688 len = ret;
689 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
690
Paul Bakker3cbaf1e2014-07-08 12:26:02 +0200691 printf( " . Closing the connection..." );
692
693 while( ( ret = ssl_close_notify( &ssl ) ) < 0 )
694 {
695 if( ret != POLARSSL_ERR_NET_WANT_READ &&
696 ret != POLARSSL_ERR_NET_WANT_WRITE )
697 {
698 printf( " failed\n ! ssl_close_notify returned %d\n\n", ret );
699 goto reset;
700 }
701 }
702
703 printf( " ok\n" );
704
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000705 ret = 0;
706 goto reset;
707
708exit:
709
710#ifdef POLARSSL_ERROR_C
711 if( ret != 0 )
712 {
713 char error_buf[100];
714 error_strerror( ret, error_buf, 100 );
715 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
716 }
717#endif
718
Paul Bakker39148402014-04-17 16:02:36 +0200719
720 if( client_fd != -1 )
721 net_close( client_fd );
722
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000723 x509_free( &srvcert );
724 x509_free( &cacert );
725 rsa_free( &rsa );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000726 ssl_free( &ssl );
727
Paul Bakker0a597072012-09-25 21:55:46 +0000728#if defined(POLARSSL_SSL_CACHE_C)
729 ssl_cache_free( &cache );
730#endif
731
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000732#if defined(_WIN32)
733 printf( " + Press Enter to exit this program.\n" );
734 fflush( stdout ); getchar();
735#endif
736
737 return( ret );
738}
739#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
740 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
741 POLARSSL_CTR_DRBG_C */