blob: cc94e5ca7d6308e5b91f91a47ed1a6f3f1f09be2 [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
60#define DFL_RENEGOTIATION SSL_RENEGOTIATION_ENABLED
61#define DFL_ALLOW_LEGACY SSL_LEGACY_NO_RENEGOTIATION
Paul Bakker1d29fb52012-09-28 13:28:45 +000062#define DFL_MIN_VERSION -1
Paul Bakkerb60b95f2012-09-25 09:05:17 +000063
64#define HTTP_RESPONSE \
65 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
66 "<h2>PolarSSL Test Server</h2>\r\n" \
67 "<p>Successful connection using: %s</p>\r\n"
68
69/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +000070 * global options
71 */
72struct options
73{
74 int server_port; /* port on which the ssl service runs */
75 int debug_level; /* level of debugging */
76 char *ca_file; /* the file with the CA certificate(s) */
77 char *ca_path; /* the path with the CA certificate(s) reside */
78 char *crt_file; /* the file with the client certificate */
79 char *key_file; /* the file with the client key */
80 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
81 int renegotiation; /* enable / disable renegotiation */
82 int allow_legacy; /* allow legacy renegotiation */
Paul Bakker1d29fb52012-09-28 13:28:45 +000083 int min_version; /* minimum protocol version accepted */
Paul Bakkerb60b95f2012-09-25 09:05:17 +000084} opt;
85
86void my_debug( void *ctx, int level, const char *str )
87{
88 if( level < opt.debug_level )
89 {
90 fprintf( (FILE *) ctx, "%s", str );
91 fflush( (FILE *) ctx );
92 }
93}
94
Paul Bakker645ce3a2012-10-31 12:32:41 +000095/*
96 * Sorted by order of preference
97 */
98int my_ciphersuites[] =
99{
100#if defined(POLARSSL_DHM_C)
101#if defined(POLARSSL_AES_C)
102#if defined(POLARSSL_SHA2_C)
103 TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,
104#endif /* POLARSSL_SHA2_C */
105#if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA4_C)
106 TLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
107#endif
108 TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
109#if defined(POLARSSL_SHA2_C)
110 TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
111#endif
112#if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA2_C)
113 TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
114#endif
115 TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
116#endif
117#if defined(POLARSSL_CAMELLIA_C)
118#if defined(POLARSSL_SHA2_C)
119 TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256,
120#endif /* POLARSSL_SHA2_C */
121 TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA,
122#if defined(POLARSSL_SHA2_C)
123 TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256,
124#endif /* POLARSSL_SHA2_C */
125 TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA,
126#endif
127#if defined(POLARSSL_DES_C)
128 TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA,
129#endif
130#endif
131
132#if defined(POLARSSL_AES_C)
133#if defined(POLARSSL_SHA2_C)
134 TLS_RSA_WITH_AES_256_CBC_SHA256,
135#endif /* POLARSSL_SHA2_C */
136#if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA4_C)
137 TLS_RSA_WITH_AES_256_GCM_SHA384,
138#endif /* POLARSSL_SHA2_C */
139 TLS_RSA_WITH_AES_256_CBC_SHA,
140#endif
141#if defined(POLARSSL_CAMELLIA_C)
142#if defined(POLARSSL_SHA2_C)
143 TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256,
144#endif /* POLARSSL_SHA2_C */
145 TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,
146#endif
147#if defined(POLARSSL_AES_C)
148#if defined(POLARSSL_SHA2_C)
149 TLS_RSA_WITH_AES_128_CBC_SHA256,
150#endif /* POLARSSL_SHA2_C */
151#if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA2_C)
152 TLS_RSA_WITH_AES_128_GCM_SHA256,
153#endif /* POLARSSL_SHA2_C */
154 TLS_RSA_WITH_AES_128_CBC_SHA,
155#endif
156#if defined(POLARSSL_CAMELLIA_C)
157#if defined(POLARSSL_SHA2_C)
158 TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256,
159#endif /* POLARSSL_SHA2_C */
160 TLS_RSA_WITH_CAMELLIA_128_CBC_SHA,
161#endif
162#if defined(POLARSSL_DES_C)
163 TLS_RSA_WITH_3DES_EDE_CBC_SHA,
164#endif
165#if defined(POLARSSL_ARC4_C)
166 TLS_RSA_WITH_RC4_128_SHA,
167 TLS_RSA_WITH_RC4_128_MD5,
168#endif
169
170#if defined(POLARSSL_ENABLE_WEAK_CIPHERSUITES)
171#if defined(POLARSSL_DES_C)
172 TLS_DHE_RSA_WITH_DES_CBC_SHA,
173 TLS_RSA_WITH_DES_CBC_SHA,
174#endif
175#if defined(POLARSSL_CIPHER_NULL_CIPHER)
176 TLS_RSA_WITH_NULL_MD5,
177 TLS_RSA_WITH_NULL_SHA,
178 TLS_RSA_WITH_NULL_SHA256,
179#endif
180#endif
181 0
182};
183
184
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000185#if defined(POLARSSL_FS_IO)
186#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100187 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
188 " default: \"\" (pre-loaded)\n" \
189 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
190 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
191 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
192 " default: \"\" (pre-loaded)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000193 " key_file=%%s default: \"\" (pre-loaded)\n"
194#else
195#define USAGE_IO \
196 " No file operations available (POLARSSL_FS_IO not defined)\n"
197#endif /* POLARSSL_FS_IO */
198
199#define USAGE \
200 "\n usage: ssl_server2 param=<>...\n" \
201 "\n acceptable parameters:\n" \
202 " server_port=%%d default: 4433\n" \
203 " debug_level=%%d default: 0 (disabled)\n" \
204 USAGE_IO \
205 " request_page=%%s default: \".\"\n" \
206 " renegotiation=%%d default: 1 (enabled)\n" \
207 " allow_legacy=%%d default: 0 (disabled)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000208 " min_version=%%s default: \"ssl3\"\n" \
209 " options: ssl3, tls1, tls1_1, tls1_2\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000210 " force_ciphersuite=<name> default: all enabled\n"\
211 " acceptable ciphersuite names:\n"
212
213#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
214 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
215 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
216 !defined(POLARSSL_CTR_DRBG_C)
217int main( int argc, char *argv[] )
218{
219 ((void) argc);
220 ((void) argv);
221
222 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
223 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
224 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
225 "POLARSSL_CTR_DRBG_C not defined.\n");
226 return( 0 );
227}
228#else
229int main( int argc, char *argv[] )
230{
231 int ret = 0, len;
232 int listen_fd;
233 int client_fd = -1;
234 unsigned char buf[1024];
235 char *pers = "ssl_server2";
236
237 entropy_context entropy;
238 ctr_drbg_context ctr_drbg;
239 ssl_context ssl;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000240 x509_cert cacert;
241 x509_cert srvcert;
242 rsa_context rsa;
Paul Bakker0a597072012-09-25 21:55:46 +0000243#if defined(POLARSSL_SSL_CACHE_C)
244 ssl_cache_context cache;
245#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000246
247 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000248 char *p, *q;
249 const int *list;
250
251 /*
252 * Make sure memory references are valid.
253 */
254 listen_fd = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000255 memset( &cacert, 0, sizeof( x509_cert ) );
256 memset( &srvcert, 0, sizeof( x509_cert ) );
257 memset( &rsa, 0, sizeof( rsa_context ) );
Paul Bakker0a597072012-09-25 21:55:46 +0000258#if defined(POLARSSL_SSL_CACHE_C)
259 ssl_cache_init( &cache );
260#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000261
262 if( argc == 0 )
263 {
264 usage:
265 if( ret == 0 )
266 ret = 1;
267
268 printf( USAGE );
269
270 list = ssl_list_ciphersuites();
271 while( *list )
272 {
273 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
274 list++;
275 }
276 printf("\n");
277 goto exit;
278 }
279
280 opt.server_port = DFL_SERVER_PORT;
281 opt.debug_level = DFL_DEBUG_LEVEL;
282 opt.ca_file = DFL_CA_FILE;
283 opt.ca_path = DFL_CA_PATH;
284 opt.crt_file = DFL_CRT_FILE;
285 opt.key_file = DFL_KEY_FILE;
286 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
287 opt.renegotiation = DFL_RENEGOTIATION;
288 opt.allow_legacy = DFL_ALLOW_LEGACY;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000289 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000290
291 for( i = 1; i < argc; i++ )
292 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000293 p = argv[i];
294 if( ( q = strchr( p, '=' ) ) == NULL )
295 goto usage;
296 *q++ = '\0';
297
298 if( strcmp( p, "server_port" ) == 0 )
299 {
300 opt.server_port = atoi( q );
301 if( opt.server_port < 1 || opt.server_port > 65535 )
302 goto usage;
303 }
304 else if( strcmp( p, "debug_level" ) == 0 )
305 {
306 opt.debug_level = atoi( q );
307 if( opt.debug_level < 0 || opt.debug_level > 65535 )
308 goto usage;
309 }
310 else if( strcmp( p, "ca_file" ) == 0 )
311 opt.ca_file = q;
312 else if( strcmp( p, "ca_path" ) == 0 )
313 opt.ca_path = q;
314 else if( strcmp( p, "crt_file" ) == 0 )
315 opt.crt_file = q;
316 else if( strcmp( p, "key_file" ) == 0 )
317 opt.key_file = q;
318 else if( strcmp( p, "force_ciphersuite" ) == 0 )
319 {
320 opt.force_ciphersuite[0] = -1;
321
322 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
323
324 if( opt.force_ciphersuite[0] <= 0 )
325 {
326 ret = 2;
327 goto usage;
328 }
329 opt.force_ciphersuite[1] = 0;
330 }
331 else if( strcmp( p, "renegotiation" ) == 0 )
332 {
333 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
334 SSL_RENEGOTIATION_DISABLED;
335 }
336 else if( strcmp( p, "allow_legacy" ) == 0 )
337 {
338 opt.allow_legacy = atoi( q );
339 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
340 goto usage;
341 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000342 else if( strcmp( p, "min_version" ) == 0 )
343 {
344 if( strcmp( q, "ssl3" ) == 0 )
345 opt.min_version = SSL_MINOR_VERSION_0;
346 else if( strcmp( q, "tls1" ) == 0 )
347 opt.min_version = SSL_MINOR_VERSION_1;
348 else if( strcmp( q, "tls1_1" ) == 0 )
349 opt.min_version = SSL_MINOR_VERSION_2;
350 else if( strcmp( q, "tls1_2" ) == 0 )
351 opt.min_version = SSL_MINOR_VERSION_3;
352 else
353 goto usage;
354 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000355 else
356 goto usage;
357 }
358
359 /*
360 * 0. Initialize the RNG and the session data
361 */
362 printf( "\n . Seeding the random number generator..." );
363 fflush( stdout );
364
365 entropy_init( &entropy );
366 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
367 (unsigned char *) pers, strlen( pers ) ) ) != 0 )
368 {
369 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
370 goto exit;
371 }
372
373 printf( " ok\n" );
374
375 /*
376 * 1.1. Load the trusted CA
377 */
378 printf( " . Loading the CA root certificate ..." );
379 fflush( stdout );
380
381#if defined(POLARSSL_FS_IO)
382 if( strlen( opt.ca_path ) )
383 ret = x509parse_crtpath( &cacert, opt.ca_path );
384 else if( strlen( opt.ca_file ) )
385 ret = x509parse_crtfile( &cacert, opt.ca_file );
386 else
387#endif
388#if defined(POLARSSL_CERTS_C)
389 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
390 strlen( test_ca_crt ) );
391#else
392 {
393 ret = 1;
394 printf("POLARSSL_CERTS_C not defined.");
395 }
396#endif
397 if( ret < 0 )
398 {
399 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
400 goto exit;
401 }
402
403 printf( " ok (%d skipped)\n", ret );
404
405 /*
406 * 1.2. Load own certificate and private key
407 */
408 printf( " . Loading the server cert. and key..." );
409 fflush( stdout );
410
411#if defined(POLARSSL_FS_IO)
412 if( strlen( opt.crt_file ) )
413 ret = x509parse_crtfile( &srvcert, opt.crt_file );
414 else
415#endif
416#if defined(POLARSSL_CERTS_C)
417 ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt,
418 strlen( test_srv_crt ) );
419#else
420 {
421 ret = 1;
422 printf("POLARSSL_CERTS_C not defined.");
423 }
424#endif
425 if( ret != 0 )
426 {
427 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
428 goto exit;
429 }
430
431#if defined(POLARSSL_FS_IO)
432 if( strlen( opt.key_file ) )
433 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
434 else
435#endif
436#if defined(POLARSSL_CERTS_C)
437 ret = x509parse_key( &rsa, (unsigned char *) test_srv_key,
438 strlen( test_srv_key ), NULL, 0 );
439#else
440 {
441 ret = 1;
442 printf("POLARSSL_CERTS_C not defined.");
443 }
444#endif
445 if( ret != 0 )
446 {
447 printf( " failed\n ! x509parse_key returned -0x%x\n\n", -ret );
448 goto exit;
449 }
450
451 printf( " ok\n" );
452
453 /*
454 * 2. Setup the listening TCP socket
455 */
456 printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
457 fflush( stdout );
458
459 if( ( ret = net_bind( &listen_fd, NULL, opt.server_port ) ) != 0 )
460 {
461 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
462 goto exit;
463 }
464
465 printf( " ok\n" );
466
467 /*
468 * 3. Setup stuff
469 */
470 printf( " . Setting up the SSL/TLS structure..." );
471 fflush( stdout );
472
473 if( ( ret = ssl_init( &ssl ) ) != 0 )
474 {
475 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
476 goto exit;
477 }
478
479 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
480 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
481
482 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
483 ssl_set_dbg( &ssl, my_debug, stdout );
484
Paul Bakker0a597072012-09-25 21:55:46 +0000485#if defined(POLARSSL_SSL_CACHE_C)
486 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
487 ssl_cache_set, &cache );
488#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000489
490 if( opt.force_ciphersuite[0] == DFL_FORCE_CIPHER )
Paul Bakker645ce3a2012-10-31 12:32:41 +0000491 ssl_set_ciphersuites( &ssl, my_ciphersuites );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000492 else
493 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
494
495 ssl_set_renegotiation( &ssl, opt.renegotiation );
496 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
497
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000498 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
499 ssl_set_own_cert( &ssl, &srvcert, &rsa );
500
501#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +0000502 /*
503 * Use different group than default DHM group
504 */
Paul Bakkerd4324102012-09-26 08:29:38 +0000505 ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
506 POLARSSL_DHM_RFC5114_MODP_2048_G );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000507#endif
508
Paul Bakker1d29fb52012-09-28 13:28:45 +0000509 if( opt.min_version != -1 )
510 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
511
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000512 printf( " ok\n" );
513
514reset:
515#ifdef POLARSSL_ERROR_C
516 if( ret != 0 )
517 {
518 char error_buf[100];
519 error_strerror( ret, error_buf, 100 );
520 printf("Last error was: %d - %s\n\n", ret, error_buf );
521 }
522#endif
523
524 if( client_fd != -1 )
525 net_close( client_fd );
526
527 ssl_session_reset( &ssl );
528
529 /*
530 * 3. Wait until a client connects
531 */
532#if defined(_WIN32_WCE)
533 {
534 SHELLEXECUTEINFO sei;
535
536 ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) );
537
538 sei.cbSize = sizeof( SHELLEXECUTEINFO );
539 sei.fMask = 0;
540 sei.hwnd = 0;
541 sei.lpVerb = _T( "open" );
542 sei.lpFile = _T( "https://localhost:4433/" );
543 sei.lpParameters = NULL;
544 sei.lpDirectory = NULL;
545 sei.nShow = SW_SHOWNORMAL;
546
547 ShellExecuteEx( &sei );
548 }
549#elif defined(_WIN32)
550 ShellExecute( NULL, "open", "https://localhost:4433/",
551 NULL, NULL, SW_SHOWNORMAL );
552#endif
553
554 client_fd = -1;
555
556 printf( " . Waiting for a remote connection ..." );
557 fflush( stdout );
558
559 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
560 {
561 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
562 goto exit;
563 }
564
565 ssl_set_bio( &ssl, net_recv, &client_fd,
566 net_send, &client_fd );
567
568 printf( " ok\n" );
569
570 /*
571 * 4. Handshake
572 */
573 printf( " . Performing the SSL/TLS handshake..." );
574 fflush( stdout );
575
576 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
577 {
578 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
579 {
580 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000581 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000582 }
583 }
584
585 printf( " ok\n [ Ciphersuite is %s ]\n",
586 ssl_get_ciphersuite( &ssl ) );
587
588 /*
589 * 5. Verify the server certificate
590 */
591 printf( " . Verifying peer X.509 certificate..." );
592
593 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
594 {
595 printf( " failed\n" );
596
Paul Bakkerb0550d92012-10-30 07:51:03 +0000597 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000598 printf( " ! no client certificate sent\n" );
599
600 if( ( ret & BADCERT_EXPIRED ) != 0 )
601 printf( " ! client certificate has expired\n" );
602
603 if( ( ret & BADCERT_REVOKED ) != 0 )
604 printf( " ! client certificate has been revoked\n" );
605
606 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
607 printf( " ! self-signed or not signed by a trusted CA\n" );
608
609 printf( "\n" );
610 }
611 else
612 printf( " ok\n" );
613
Paul Bakkerb0550d92012-10-30 07:51:03 +0000614 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000615 {
616 printf( " . Peer certificate information ...\n" );
617 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
Paul Bakkerb0550d92012-10-30 07:51:03 +0000618 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000619 printf( "%s\n", buf );
620 }
621
622 /*
623 * 6. Read the HTTP Request
624 */
625 printf( " < Read from client:" );
626 fflush( stdout );
627
628 do
629 {
630 len = sizeof( buf ) - 1;
631 memset( buf, 0, sizeof( buf ) );
632 ret = ssl_read( &ssl, buf, len );
633
634 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
635 continue;
636
637 if( ret <= 0 )
638 {
639 switch( ret )
640 {
641 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
642 printf( " connection was closed gracefully\n" );
643 break;
644
645 case POLARSSL_ERR_NET_CONN_RESET:
646 printf( " connection was reset by peer\n" );
647 break;
648
649 default:
650 printf( " ssl_read returned -0x%x\n", -ret );
651 break;
652 }
653
654 break;
655 }
656
657 len = ret;
658 printf( " %d bytes read\n\n%s", len, (char *) buf );
659
660 if( ret > 0 )
661 break;
662 }
663 while( 1 );
664
665 /*
666 * 7. Write the 200 Response
667 */
668 printf( " > Write to client:" );
669 fflush( stdout );
670
671 len = sprintf( (char *) buf, HTTP_RESPONSE,
672 ssl_get_ciphersuite( &ssl ) );
673
674 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
675 {
676 if( ret == POLARSSL_ERR_NET_CONN_RESET )
677 {
678 printf( " failed\n ! peer closed the connection\n\n" );
679 goto reset;
680 }
681
682 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
683 {
684 printf( " failed\n ! ssl_write returned %d\n\n", ret );
685 goto exit;
686 }
687 }
688
689 len = ret;
690 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
691
692 ret = 0;
693 goto reset;
694
695exit:
696
697#ifdef POLARSSL_ERROR_C
698 if( ret != 0 )
699 {
700 char error_buf[100];
701 error_strerror( ret, error_buf, 100 );
702 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
703 }
704#endif
705
706 net_close( client_fd );
707 x509_free( &srvcert );
708 x509_free( &cacert );
709 rsa_free( &rsa );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000710 ssl_free( &ssl );
711
Paul Bakker0a597072012-09-25 21:55:46 +0000712#if defined(POLARSSL_SSL_CACHE_C)
713 ssl_cache_free( &cache );
714#endif
715
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000716#if defined(_WIN32)
717 printf( " + Press Enter to exit this program.\n" );
718 fflush( stdout ); getchar();
719#endif
720
721 return( ret );
722}
723#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
724 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
725 POLARSSL_CTR_DRBG_C */