blob: bc5402f8d3163abba01e69fca442bbeaacd4c9c0 [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
48#define DFL_SERVER_PORT 4433
49#define DFL_REQUEST_PAGE "/"
50#define DFL_DEBUG_LEVEL 0
51#define DFL_CA_FILE ""
52#define DFL_CA_PATH ""
53#define DFL_CRT_FILE ""
54#define DFL_KEY_FILE ""
55#define DFL_FORCE_CIPHER 0
56#define DFL_RENEGOTIATION SSL_RENEGOTIATION_ENABLED
57#define DFL_ALLOW_LEGACY SSL_LEGACY_NO_RENEGOTIATION
58
59#define HTTP_RESPONSE \
60 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
61 "<h2>PolarSSL Test Server</h2>\r\n" \
62 "<p>Successful connection using: %s</p>\r\n"
63
64/*
Paul Bakker29b64762012-09-25 09:36:44 +000065 * Computing a "safe" DH prime can take a very
66 * long time. RFC 5114 provides precomputed and standardized
67 * values.
Paul Bakkerb60b95f2012-09-25 09:05:17 +000068 */
Paul Bakker29b64762012-09-25 09:36:44 +000069char *my_dhm_P = POLARSSL_DHM_RFC5114_MODP_2048_P;
70char *my_dhm_G = POLARSSL_DHM_RFC5114_MODP_2048_G;
Paul Bakkerb60b95f2012-09-25 09:05:17 +000071
72/*
73 * global options
74 */
75struct options
76{
77 int server_port; /* port on which the ssl service runs */
78 int debug_level; /* level of debugging */
79 char *ca_file; /* the file with the CA certificate(s) */
80 char *ca_path; /* the path with the CA certificate(s) reside */
81 char *crt_file; /* the file with the client certificate */
82 char *key_file; /* the file with the client key */
83 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
84 int renegotiation; /* enable / disable renegotiation */
85 int allow_legacy; /* allow legacy renegotiation */
86} 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
97/*
98 * These session callbacks use a simple chained list
99 * to store and retrieve the session information.
100 */
101ssl_session *s_list_1st = NULL;
102ssl_session *cur, *prv;
103
104static int my_get_session( ssl_context *ssl )
105{
106 time_t t = time( NULL );
107
108 if( ssl->resume == 0 )
109 return( 1 );
110
111 cur = s_list_1st;
112 prv = NULL;
113
114 while( cur != NULL )
115 {
116 prv = cur;
117 cur = cur->next;
118
119 if( ssl->timeout != 0 && (int) ( t - prv->start ) > ssl->timeout )
120 continue;
121
122 if( ssl->session->ciphersuite != prv->ciphersuite ||
123 ssl->session->length != prv->length )
124 continue;
125
126 if( memcmp( ssl->session->id, prv->id, prv->length ) != 0 )
127 continue;
128
129 memcpy( ssl->session->master, prv->master, 48 );
130 return( 0 );
131 }
132
133 return( 1 );
134}
135
136static int my_set_session( ssl_context *ssl )
137{
138 time_t t = time( NULL );
139
140 cur = s_list_1st;
141 prv = NULL;
142
143 while( cur != NULL )
144 {
145 if( ssl->timeout != 0 && (int) ( t - cur->start ) > ssl->timeout )
146 break; /* expired, reuse this slot */
147
148 if( memcmp( ssl->session->id, cur->id, cur->length ) == 0 )
149 break; /* client reconnected */
150
151 prv = cur;
152 cur = cur->next;
153 }
154
155 if( cur == NULL )
156 {
157 cur = (ssl_session *) malloc( sizeof( ssl_session ) );
158 if( cur == NULL )
159 return( 1 );
160
161 if( prv == NULL )
162 s_list_1st = cur;
163 else prv->next = cur;
164 }
165
166 memcpy( cur, ssl->session, sizeof( ssl_session ) );
167
168 return( 0 );
169}
170
171#if defined(POLARSSL_FS_IO)
172#define USAGE_IO \
173 " ca_file=%%s default: \"\" (pre-loaded)\n" \
174 " ca_path=%%s default: \"\" (pre-loaded) (overrides ca_file)\n" \
175 " crt_file=%%s default: \"\" (pre-loaded)\n" \
176 " key_file=%%s default: \"\" (pre-loaded)\n"
177#else
178#define USAGE_IO \
179 " No file operations available (POLARSSL_FS_IO not defined)\n"
180#endif /* POLARSSL_FS_IO */
181
182#define USAGE \
183 "\n usage: ssl_server2 param=<>...\n" \
184 "\n acceptable parameters:\n" \
185 " server_port=%%d default: 4433\n" \
186 " debug_level=%%d default: 0 (disabled)\n" \
187 USAGE_IO \
188 " request_page=%%s default: \".\"\n" \
189 " renegotiation=%%d default: 1 (enabled)\n" \
190 " allow_legacy=%%d default: 0 (disabled)\n" \
191 " force_ciphersuite=<name> default: all enabled\n"\
192 " acceptable ciphersuite names:\n"
193
194#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
195 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
196 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
197 !defined(POLARSSL_CTR_DRBG_C)
198int main( int argc, char *argv[] )
199{
200 ((void) argc);
201 ((void) argv);
202
203 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
204 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
205 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
206 "POLARSSL_CTR_DRBG_C not defined.\n");
207 return( 0 );
208}
209#else
210int main( int argc, char *argv[] )
211{
212 int ret = 0, len;
213 int listen_fd;
214 int client_fd = -1;
215 unsigned char buf[1024];
216 char *pers = "ssl_server2";
217
218 entropy_context entropy;
219 ctr_drbg_context ctr_drbg;
220 ssl_context ssl;
221 ssl_session ssn;
222 x509_cert cacert;
223 x509_cert srvcert;
224 rsa_context rsa;
225
226 int i;
227 size_t j, n;
228 char *p, *q;
229 const int *list;
230
231 /*
232 * Make sure memory references are valid.
233 */
234 listen_fd = 0;
235 memset( &ssn, 0, sizeof( ssl_session ) );
236 memset( &ssl, 0, sizeof( ssl_context ) );
237 memset( &cacert, 0, sizeof( x509_cert ) );
238 memset( &srvcert, 0, sizeof( x509_cert ) );
239 memset( &rsa, 0, sizeof( rsa_context ) );
240
241 if( argc == 0 )
242 {
243 usage:
244 if( ret == 0 )
245 ret = 1;
246
247 printf( USAGE );
248
249 list = ssl_list_ciphersuites();
250 while( *list )
251 {
252 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
253 list++;
254 }
255 printf("\n");
256 goto exit;
257 }
258
259 opt.server_port = DFL_SERVER_PORT;
260 opt.debug_level = DFL_DEBUG_LEVEL;
261 opt.ca_file = DFL_CA_FILE;
262 opt.ca_path = DFL_CA_PATH;
263 opt.crt_file = DFL_CRT_FILE;
264 opt.key_file = DFL_KEY_FILE;
265 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
266 opt.renegotiation = DFL_RENEGOTIATION;
267 opt.allow_legacy = DFL_ALLOW_LEGACY;
268
269 for( i = 1; i < argc; i++ )
270 {
271 n = strlen( argv[i] );
272
273 for( j = 0; j < n; j++ )
274 {
275 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
276 argv[i][j] |= 0x20;
277 }
278
279 p = argv[i];
280 if( ( q = strchr( p, '=' ) ) == NULL )
281 goto usage;
282 *q++ = '\0';
283
284 if( strcmp( p, "server_port" ) == 0 )
285 {
286 opt.server_port = atoi( q );
287 if( opt.server_port < 1 || opt.server_port > 65535 )
288 goto usage;
289 }
290 else if( strcmp( p, "debug_level" ) == 0 )
291 {
292 opt.debug_level = atoi( q );
293 if( opt.debug_level < 0 || opt.debug_level > 65535 )
294 goto usage;
295 }
296 else if( strcmp( p, "ca_file" ) == 0 )
297 opt.ca_file = q;
298 else if( strcmp( p, "ca_path" ) == 0 )
299 opt.ca_path = q;
300 else if( strcmp( p, "crt_file" ) == 0 )
301 opt.crt_file = q;
302 else if( strcmp( p, "key_file" ) == 0 )
303 opt.key_file = q;
304 else if( strcmp( p, "force_ciphersuite" ) == 0 )
305 {
306 opt.force_ciphersuite[0] = -1;
307
308 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
309
310 if( opt.force_ciphersuite[0] <= 0 )
311 {
312 ret = 2;
313 goto usage;
314 }
315 opt.force_ciphersuite[1] = 0;
316 }
317 else if( strcmp( p, "renegotiation" ) == 0 )
318 {
319 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
320 SSL_RENEGOTIATION_DISABLED;
321 }
322 else if( strcmp( p, "allow_legacy" ) == 0 )
323 {
324 opt.allow_legacy = atoi( q );
325 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
326 goto usage;
327 }
328 else
329 goto usage;
330 }
331
332 /*
333 * 0. Initialize the RNG and the session data
334 */
335 printf( "\n . Seeding the random number generator..." );
336 fflush( stdout );
337
338 entropy_init( &entropy );
339 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
340 (unsigned char *) pers, strlen( pers ) ) ) != 0 )
341 {
342 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
343 goto exit;
344 }
345
346 printf( " ok\n" );
347
348 /*
349 * 1.1. Load the trusted CA
350 */
351 printf( " . Loading the CA root certificate ..." );
352 fflush( stdout );
353
354#if defined(POLARSSL_FS_IO)
355 if( strlen( opt.ca_path ) )
356 ret = x509parse_crtpath( &cacert, opt.ca_path );
357 else if( strlen( opt.ca_file ) )
358 ret = x509parse_crtfile( &cacert, opt.ca_file );
359 else
360#endif
361#if defined(POLARSSL_CERTS_C)
362 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
363 strlen( test_ca_crt ) );
364#else
365 {
366 ret = 1;
367 printf("POLARSSL_CERTS_C not defined.");
368 }
369#endif
370 if( ret < 0 )
371 {
372 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
373 goto exit;
374 }
375
376 printf( " ok (%d skipped)\n", ret );
377
378 /*
379 * 1.2. Load own certificate and private key
380 */
381 printf( " . Loading the server cert. and key..." );
382 fflush( stdout );
383
384#if defined(POLARSSL_FS_IO)
385 if( strlen( opt.crt_file ) )
386 ret = x509parse_crtfile( &srvcert, opt.crt_file );
387 else
388#endif
389#if defined(POLARSSL_CERTS_C)
390 ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt,
391 strlen( test_srv_crt ) );
392#else
393 {
394 ret = 1;
395 printf("POLARSSL_CERTS_C not defined.");
396 }
397#endif
398 if( ret != 0 )
399 {
400 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
401 goto exit;
402 }
403
404#if defined(POLARSSL_FS_IO)
405 if( strlen( opt.key_file ) )
406 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
407 else
408#endif
409#if defined(POLARSSL_CERTS_C)
410 ret = x509parse_key( &rsa, (unsigned char *) test_srv_key,
411 strlen( test_srv_key ), NULL, 0 );
412#else
413 {
414 ret = 1;
415 printf("POLARSSL_CERTS_C not defined.");
416 }
417#endif
418 if( ret != 0 )
419 {
420 printf( " failed\n ! x509parse_key returned -0x%x\n\n", -ret );
421 goto exit;
422 }
423
424 printf( " ok\n" );
425
426 /*
427 * 2. Setup the listening TCP socket
428 */
429 printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
430 fflush( stdout );
431
432 if( ( ret = net_bind( &listen_fd, NULL, opt.server_port ) ) != 0 )
433 {
434 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
435 goto exit;
436 }
437
438 printf( " ok\n" );
439
440 /*
441 * 3. Setup stuff
442 */
443 printf( " . Setting up the SSL/TLS structure..." );
444 fflush( stdout );
445
446 if( ( ret = ssl_init( &ssl ) ) != 0 )
447 {
448 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
449 goto exit;
450 }
451
452 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
453 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
454
455 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
456 ssl_set_dbg( &ssl, my_debug, stdout );
457
458 ssl_set_scb( &ssl, my_get_session,
459 my_set_session );
460
461 if( opt.force_ciphersuite[0] == DFL_FORCE_CIPHER )
462 ssl_set_ciphersuites( &ssl, ssl_default_ciphersuites );
463 else
464 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
465
466 ssl_set_renegotiation( &ssl, opt.renegotiation );
467 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
468
469 ssl_set_session( &ssl, 1, 0, &ssn );
470
471 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
472 ssl_set_own_cert( &ssl, &srvcert, &rsa );
473
474#if defined(POLARSSL_DHM_C)
475 ssl_set_dh_param( &ssl, my_dhm_P, my_dhm_G );
476#endif
477
478 printf( " ok\n" );
479
480reset:
481#ifdef POLARSSL_ERROR_C
482 if( ret != 0 )
483 {
484 char error_buf[100];
485 error_strerror( ret, error_buf, 100 );
486 printf("Last error was: %d - %s\n\n", ret, error_buf );
487 }
488#endif
489
490 if( client_fd != -1 )
491 net_close( client_fd );
492
493 ssl_session_reset( &ssl );
494
495 /*
496 * 3. Wait until a client connects
497 */
498#if defined(_WIN32_WCE)
499 {
500 SHELLEXECUTEINFO sei;
501
502 ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) );
503
504 sei.cbSize = sizeof( SHELLEXECUTEINFO );
505 sei.fMask = 0;
506 sei.hwnd = 0;
507 sei.lpVerb = _T( "open" );
508 sei.lpFile = _T( "https://localhost:4433/" );
509 sei.lpParameters = NULL;
510 sei.lpDirectory = NULL;
511 sei.nShow = SW_SHOWNORMAL;
512
513 ShellExecuteEx( &sei );
514 }
515#elif defined(_WIN32)
516 ShellExecute( NULL, "open", "https://localhost:4433/",
517 NULL, NULL, SW_SHOWNORMAL );
518#endif
519
520 client_fd = -1;
521
522 printf( " . Waiting for a remote connection ..." );
523 fflush( stdout );
524
525 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
526 {
527 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
528 goto exit;
529 }
530
531 ssl_set_bio( &ssl, net_recv, &client_fd,
532 net_send, &client_fd );
533
534 printf( " ok\n" );
535
536 /*
537 * 4. Handshake
538 */
539 printf( " . Performing the SSL/TLS handshake..." );
540 fflush( stdout );
541
542 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
543 {
544 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
545 {
546 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
547 goto exit;
548 }
549 }
550
551 printf( " ok\n [ Ciphersuite is %s ]\n",
552 ssl_get_ciphersuite( &ssl ) );
553
554 /*
555 * 5. Verify the server certificate
556 */
557 printf( " . Verifying peer X.509 certificate..." );
558
559 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
560 {
561 printf( " failed\n" );
562
563 if( !ssl.session->peer_cert )
564 printf( " ! no client certificate sent\n" );
565
566 if( ( ret & BADCERT_EXPIRED ) != 0 )
567 printf( " ! client certificate has expired\n" );
568
569 if( ( ret & BADCERT_REVOKED ) != 0 )
570 printf( " ! client certificate has been revoked\n" );
571
572 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
573 printf( " ! self-signed or not signed by a trusted CA\n" );
574
575 printf( "\n" );
576 }
577 else
578 printf( " ok\n" );
579
580 if( ssl.session->peer_cert )
581 {
582 printf( " . Peer certificate information ...\n" );
583 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
584 ssl.session->peer_cert );
585 printf( "%s\n", buf );
586 }
587
588 /*
589 * 6. Read the HTTP Request
590 */
591 printf( " < Read from client:" );
592 fflush( stdout );
593
594 do
595 {
596 len = sizeof( buf ) - 1;
597 memset( buf, 0, sizeof( buf ) );
598 ret = ssl_read( &ssl, buf, len );
599
600 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
601 continue;
602
603 if( ret <= 0 )
604 {
605 switch( ret )
606 {
607 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
608 printf( " connection was closed gracefully\n" );
609 break;
610
611 case POLARSSL_ERR_NET_CONN_RESET:
612 printf( " connection was reset by peer\n" );
613 break;
614
615 default:
616 printf( " ssl_read returned -0x%x\n", -ret );
617 break;
618 }
619
620 break;
621 }
622
623 len = ret;
624 printf( " %d bytes read\n\n%s", len, (char *) buf );
625
626 if( ret > 0 )
627 break;
628 }
629 while( 1 );
630
631 /*
632 * 7. Write the 200 Response
633 */
634 printf( " > Write to client:" );
635 fflush( stdout );
636
637 len = sprintf( (char *) buf, HTTP_RESPONSE,
638 ssl_get_ciphersuite( &ssl ) );
639
640 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
641 {
642 if( ret == POLARSSL_ERR_NET_CONN_RESET )
643 {
644 printf( " failed\n ! peer closed the connection\n\n" );
645 goto reset;
646 }
647
648 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
649 {
650 printf( " failed\n ! ssl_write returned %d\n\n", ret );
651 goto exit;
652 }
653 }
654
655 len = ret;
656 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
657
658 ret = 0;
659 goto reset;
660
661exit:
662
663#ifdef POLARSSL_ERROR_C
664 if( ret != 0 )
665 {
666 char error_buf[100];
667 error_strerror( ret, error_buf, 100 );
668 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
669 }
670#endif
671
672 net_close( client_fd );
673 x509_free( &srvcert );
674 x509_free( &cacert );
675 rsa_free( &rsa );
676 ssl_session_free( &ssn );
677 ssl_session_free( s_list_1st );
678 ssl_free( &ssl );
679
680#if defined(_WIN32)
681 printf( " + Press Enter to exit this program.\n" );
682 fflush( stdout ); getchar();
683#endif
684
685 return( ret );
686}
687#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
688 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
689 POLARSSL_CTR_DRBG_C */