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