blob: f6cf4870afac08caf379e55328324530447b30c4 [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 \
187 " ca_file=%%s default: \"\" (pre-loaded)\n" \
188 " ca_path=%%s default: \"\" (pre-loaded) (overrides ca_file)\n" \
189 " crt_file=%%s default: \"\" (pre-loaded)\n" \
190 " key_file=%%s default: \"\" (pre-loaded)\n"
191#else
192#define USAGE_IO \
193 " No file operations available (POLARSSL_FS_IO not defined)\n"
194#endif /* POLARSSL_FS_IO */
195
196#define USAGE \
197 "\n usage: ssl_server2 param=<>...\n" \
198 "\n acceptable parameters:\n" \
199 " server_port=%%d default: 4433\n" \
200 " debug_level=%%d default: 0 (disabled)\n" \
201 USAGE_IO \
202 " request_page=%%s default: \".\"\n" \
203 " renegotiation=%%d default: 1 (enabled)\n" \
204 " allow_legacy=%%d default: 0 (disabled)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000205 " min_version=%%s default: \"ssl3\"\n" \
206 " options: ssl3, tls1, tls1_1, tls1_2\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000207 " force_ciphersuite=<name> default: all enabled\n"\
208 " acceptable ciphersuite names:\n"
209
210#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
211 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
212 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
213 !defined(POLARSSL_CTR_DRBG_C)
214int main( int argc, char *argv[] )
215{
216 ((void) argc);
217 ((void) argv);
218
219 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
220 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
221 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
222 "POLARSSL_CTR_DRBG_C not defined.\n");
223 return( 0 );
224}
225#else
226int main( int argc, char *argv[] )
227{
228 int ret = 0, len;
229 int listen_fd;
230 int client_fd = -1;
231 unsigned char buf[1024];
232 char *pers = "ssl_server2";
233
234 entropy_context entropy;
235 ctr_drbg_context ctr_drbg;
236 ssl_context ssl;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000237 x509_cert cacert;
238 x509_cert srvcert;
239 rsa_context rsa;
Paul Bakker0a597072012-09-25 21:55:46 +0000240#if defined(POLARSSL_SSL_CACHE_C)
241 ssl_cache_context cache;
242#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000243
244 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000245 char *p, *q;
246 const int *list;
247
248 /*
249 * Make sure memory references are valid.
250 */
251 listen_fd = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000252 memset( &cacert, 0, sizeof( x509_cert ) );
253 memset( &srvcert, 0, sizeof( x509_cert ) );
254 memset( &rsa, 0, sizeof( rsa_context ) );
Paul Bakker0a597072012-09-25 21:55:46 +0000255#if defined(POLARSSL_SSL_CACHE_C)
256 ssl_cache_init( &cache );
257#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000258
259 if( argc == 0 )
260 {
261 usage:
262 if( ret == 0 )
263 ret = 1;
264
265 printf( USAGE );
266
267 list = ssl_list_ciphersuites();
268 while( *list )
269 {
270 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
271 list++;
272 }
273 printf("\n");
274 goto exit;
275 }
276
277 opt.server_port = DFL_SERVER_PORT;
278 opt.debug_level = DFL_DEBUG_LEVEL;
279 opt.ca_file = DFL_CA_FILE;
280 opt.ca_path = DFL_CA_PATH;
281 opt.crt_file = DFL_CRT_FILE;
282 opt.key_file = DFL_KEY_FILE;
283 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
284 opt.renegotiation = DFL_RENEGOTIATION;
285 opt.allow_legacy = DFL_ALLOW_LEGACY;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000286 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000287
288 for( i = 1; i < argc; i++ )
289 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000290 p = argv[i];
291 if( ( q = strchr( p, '=' ) ) == NULL )
292 goto usage;
293 *q++ = '\0';
294
295 if( strcmp( p, "server_port" ) == 0 )
296 {
297 opt.server_port = atoi( q );
298 if( opt.server_port < 1 || opt.server_port > 65535 )
299 goto usage;
300 }
301 else if( strcmp( p, "debug_level" ) == 0 )
302 {
303 opt.debug_level = atoi( q );
304 if( opt.debug_level < 0 || opt.debug_level > 65535 )
305 goto usage;
306 }
307 else if( strcmp( p, "ca_file" ) == 0 )
308 opt.ca_file = q;
309 else if( strcmp( p, "ca_path" ) == 0 )
310 opt.ca_path = q;
311 else if( strcmp( p, "crt_file" ) == 0 )
312 opt.crt_file = q;
313 else if( strcmp( p, "key_file" ) == 0 )
314 opt.key_file = q;
315 else if( strcmp( p, "force_ciphersuite" ) == 0 )
316 {
317 opt.force_ciphersuite[0] = -1;
318
319 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
320
321 if( opt.force_ciphersuite[0] <= 0 )
322 {
323 ret = 2;
324 goto usage;
325 }
326 opt.force_ciphersuite[1] = 0;
327 }
328 else if( strcmp( p, "renegotiation" ) == 0 )
329 {
330 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
331 SSL_RENEGOTIATION_DISABLED;
332 }
333 else if( strcmp( p, "allow_legacy" ) == 0 )
334 {
335 opt.allow_legacy = atoi( q );
336 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
337 goto usage;
338 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000339 else if( strcmp( p, "min_version" ) == 0 )
340 {
341 if( strcmp( q, "ssl3" ) == 0 )
342 opt.min_version = SSL_MINOR_VERSION_0;
343 else if( strcmp( q, "tls1" ) == 0 )
344 opt.min_version = SSL_MINOR_VERSION_1;
345 else if( strcmp( q, "tls1_1" ) == 0 )
346 opt.min_version = SSL_MINOR_VERSION_2;
347 else if( strcmp( q, "tls1_2" ) == 0 )
348 opt.min_version = SSL_MINOR_VERSION_3;
349 else
350 goto usage;
351 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000352 else
353 goto usage;
354 }
355
356 /*
357 * 0. Initialize the RNG and the session data
358 */
359 printf( "\n . Seeding the random number generator..." );
360 fflush( stdout );
361
362 entropy_init( &entropy );
363 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
364 (unsigned char *) pers, strlen( pers ) ) ) != 0 )
365 {
366 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
367 goto exit;
368 }
369
370 printf( " ok\n" );
371
372 /*
373 * 1.1. Load the trusted CA
374 */
375 printf( " . Loading the CA root certificate ..." );
376 fflush( stdout );
377
378#if defined(POLARSSL_FS_IO)
379 if( strlen( opt.ca_path ) )
380 ret = x509parse_crtpath( &cacert, opt.ca_path );
381 else if( strlen( opt.ca_file ) )
382 ret = x509parse_crtfile( &cacert, opt.ca_file );
383 else
384#endif
385#if defined(POLARSSL_CERTS_C)
386 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
387 strlen( test_ca_crt ) );
388#else
389 {
390 ret = 1;
391 printf("POLARSSL_CERTS_C not defined.");
392 }
393#endif
394 if( ret < 0 )
395 {
396 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
397 goto exit;
398 }
399
400 printf( " ok (%d skipped)\n", ret );
401
402 /*
403 * 1.2. Load own certificate and private key
404 */
405 printf( " . Loading the server cert. and key..." );
406 fflush( stdout );
407
408#if defined(POLARSSL_FS_IO)
409 if( strlen( opt.crt_file ) )
410 ret = x509parse_crtfile( &srvcert, opt.crt_file );
411 else
412#endif
413#if defined(POLARSSL_CERTS_C)
414 ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt,
415 strlen( test_srv_crt ) );
416#else
417 {
418 ret = 1;
419 printf("POLARSSL_CERTS_C not defined.");
420 }
421#endif
422 if( ret != 0 )
423 {
424 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
425 goto exit;
426 }
427
428#if defined(POLARSSL_FS_IO)
429 if( strlen( opt.key_file ) )
430 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
431 else
432#endif
433#if defined(POLARSSL_CERTS_C)
434 ret = x509parse_key( &rsa, (unsigned char *) test_srv_key,
435 strlen( test_srv_key ), NULL, 0 );
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_key returned -0x%x\n\n", -ret );
445 goto exit;
446 }
447
448 printf( " ok\n" );
449
450 /*
451 * 2. Setup the listening TCP socket
452 */
453 printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
454 fflush( stdout );
455
456 if( ( ret = net_bind( &listen_fd, NULL, opt.server_port ) ) != 0 )
457 {
458 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
459 goto exit;
460 }
461
462 printf( " ok\n" );
463
464 /*
465 * 3. Setup stuff
466 */
467 printf( " . Setting up the SSL/TLS structure..." );
468 fflush( stdout );
469
470 if( ( ret = ssl_init( &ssl ) ) != 0 )
471 {
472 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
473 goto exit;
474 }
475
476 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
477 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
478
479 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
480 ssl_set_dbg( &ssl, my_debug, stdout );
481
Paul Bakker0a597072012-09-25 21:55:46 +0000482#if defined(POLARSSL_SSL_CACHE_C)
483 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
484 ssl_cache_set, &cache );
485#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000486
487 if( opt.force_ciphersuite[0] == DFL_FORCE_CIPHER )
Paul Bakker645ce3a2012-10-31 12:32:41 +0000488 ssl_set_ciphersuites( &ssl, my_ciphersuites );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000489 else
490 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
491
492 ssl_set_renegotiation( &ssl, opt.renegotiation );
493 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
494
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000495 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
496 ssl_set_own_cert( &ssl, &srvcert, &rsa );
497
498#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +0000499 /*
500 * Use different group than default DHM group
501 */
Paul Bakkerd4324102012-09-26 08:29:38 +0000502 ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
503 POLARSSL_DHM_RFC5114_MODP_2048_G );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000504#endif
505
Paul Bakker1d29fb52012-09-28 13:28:45 +0000506 if( opt.min_version != -1 )
507 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
508
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000509 printf( " ok\n" );
510
511reset:
512#ifdef POLARSSL_ERROR_C
513 if( ret != 0 )
514 {
515 char error_buf[100];
516 error_strerror( ret, error_buf, 100 );
517 printf("Last error was: %d - %s\n\n", ret, error_buf );
518 }
519#endif
520
521 if( client_fd != -1 )
522 net_close( client_fd );
523
524 ssl_session_reset( &ssl );
525
526 /*
527 * 3. Wait until a client connects
528 */
529#if defined(_WIN32_WCE)
530 {
531 SHELLEXECUTEINFO sei;
532
533 ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) );
534
535 sei.cbSize = sizeof( SHELLEXECUTEINFO );
536 sei.fMask = 0;
537 sei.hwnd = 0;
538 sei.lpVerb = _T( "open" );
539 sei.lpFile = _T( "https://localhost:4433/" );
540 sei.lpParameters = NULL;
541 sei.lpDirectory = NULL;
542 sei.nShow = SW_SHOWNORMAL;
543
544 ShellExecuteEx( &sei );
545 }
546#elif defined(_WIN32)
547 ShellExecute( NULL, "open", "https://localhost:4433/",
548 NULL, NULL, SW_SHOWNORMAL );
549#endif
550
551 client_fd = -1;
552
553 printf( " . Waiting for a remote connection ..." );
554 fflush( stdout );
555
556 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
557 {
558 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
559 goto exit;
560 }
561
562 ssl_set_bio( &ssl, net_recv, &client_fd,
563 net_send, &client_fd );
564
565 printf( " ok\n" );
566
567 /*
568 * 4. Handshake
569 */
570 printf( " . Performing the SSL/TLS handshake..." );
571 fflush( stdout );
572
573 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
574 {
575 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
576 {
577 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000578 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000579 }
580 }
581
582 printf( " ok\n [ Ciphersuite is %s ]\n",
583 ssl_get_ciphersuite( &ssl ) );
584
585 /*
586 * 5. Verify the server certificate
587 */
588 printf( " . Verifying peer X.509 certificate..." );
589
590 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
591 {
592 printf( " failed\n" );
593
Paul Bakkerb0550d92012-10-30 07:51:03 +0000594 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000595 printf( " ! no client certificate sent\n" );
596
597 if( ( ret & BADCERT_EXPIRED ) != 0 )
598 printf( " ! client certificate has expired\n" );
599
600 if( ( ret & BADCERT_REVOKED ) != 0 )
601 printf( " ! client certificate has been revoked\n" );
602
603 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
604 printf( " ! self-signed or not signed by a trusted CA\n" );
605
606 printf( "\n" );
607 }
608 else
609 printf( " ok\n" );
610
Paul Bakkerb0550d92012-10-30 07:51:03 +0000611 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000612 {
613 printf( " . Peer certificate information ...\n" );
614 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
Paul Bakkerb0550d92012-10-30 07:51:03 +0000615 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000616 printf( "%s\n", buf );
617 }
618
619 /*
620 * 6. Read the HTTP Request
621 */
622 printf( " < Read from client:" );
623 fflush( stdout );
624
625 do
626 {
627 len = sizeof( buf ) - 1;
628 memset( buf, 0, sizeof( buf ) );
629 ret = ssl_read( &ssl, buf, len );
630
631 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
632 continue;
633
634 if( ret <= 0 )
635 {
636 switch( ret )
637 {
638 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
639 printf( " connection was closed gracefully\n" );
640 break;
641
642 case POLARSSL_ERR_NET_CONN_RESET:
643 printf( " connection was reset by peer\n" );
644 break;
645
646 default:
647 printf( " ssl_read returned -0x%x\n", -ret );
648 break;
649 }
650
651 break;
652 }
653
654 len = ret;
655 printf( " %d bytes read\n\n%s", len, (char *) buf );
656
657 if( ret > 0 )
658 break;
659 }
660 while( 1 );
661
662 /*
663 * 7. Write the 200 Response
664 */
665 printf( " > Write to client:" );
666 fflush( stdout );
667
668 len = sprintf( (char *) buf, HTTP_RESPONSE,
669 ssl_get_ciphersuite( &ssl ) );
670
671 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
672 {
673 if( ret == POLARSSL_ERR_NET_CONN_RESET )
674 {
675 printf( " failed\n ! peer closed the connection\n\n" );
676 goto reset;
677 }
678
679 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
680 {
681 printf( " failed\n ! ssl_write returned %d\n\n", ret );
682 goto exit;
683 }
684 }
685
686 len = ret;
687 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
688
689 ret = 0;
690 goto reset;
691
692exit:
693
694#ifdef POLARSSL_ERROR_C
695 if( ret != 0 )
696 {
697 char error_buf[100];
698 error_strerror( ret, error_buf, 100 );
699 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
700 }
701#endif
702
703 net_close( client_fd );
704 x509_free( &srvcert );
705 x509_free( &cacert );
706 rsa_free( &rsa );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000707 ssl_free( &ssl );
708
Paul Bakker0a597072012-09-25 21:55:46 +0000709#if defined(POLARSSL_SSL_CACHE_C)
710 ssl_cache_free( &cache );
711#endif
712
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000713#if defined(_WIN32)
714 printf( " + Press Enter to exit this program.\n" );
715 fflush( stdout ); getchar();
716#endif
717
718 return( ret );
719}
720#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
721 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
722 POLARSSL_CTR_DRBG_C */