blob: b98bff79cdaa5bbab1655e18be540a11e320d6eb [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;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000151 char *p, *q;
152 const int *list;
153
154 /*
155 * Make sure memory references are valid.
156 */
157 listen_fd = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000158 memset( &cacert, 0, sizeof( x509_cert ) );
159 memset( &srvcert, 0, sizeof( x509_cert ) );
160 memset( &rsa, 0, sizeof( rsa_context ) );
Paul Bakker0a597072012-09-25 21:55:46 +0000161#if defined(POLARSSL_SSL_CACHE_C)
162 ssl_cache_init( &cache );
163#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000164
165 if( argc == 0 )
166 {
167 usage:
168 if( ret == 0 )
169 ret = 1;
170
171 printf( USAGE );
172
173 list = ssl_list_ciphersuites();
174 while( *list )
175 {
176 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
177 list++;
178 }
179 printf("\n");
180 goto exit;
181 }
182
183 opt.server_port = DFL_SERVER_PORT;
184 opt.debug_level = DFL_DEBUG_LEVEL;
185 opt.ca_file = DFL_CA_FILE;
186 opt.ca_path = DFL_CA_PATH;
187 opt.crt_file = DFL_CRT_FILE;
188 opt.key_file = DFL_KEY_FILE;
189 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
190 opt.renegotiation = DFL_RENEGOTIATION;
191 opt.allow_legacy = DFL_ALLOW_LEGACY;
192
193 for( i = 1; i < argc; i++ )
194 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000195 p = argv[i];
196 if( ( q = strchr( p, '=' ) ) == NULL )
197 goto usage;
198 *q++ = '\0';
199
200 if( strcmp( p, "server_port" ) == 0 )
201 {
202 opt.server_port = atoi( q );
203 if( opt.server_port < 1 || opt.server_port > 65535 )
204 goto usage;
205 }
206 else if( strcmp( p, "debug_level" ) == 0 )
207 {
208 opt.debug_level = atoi( q );
209 if( opt.debug_level < 0 || opt.debug_level > 65535 )
210 goto usage;
211 }
212 else if( strcmp( p, "ca_file" ) == 0 )
213 opt.ca_file = q;
214 else if( strcmp( p, "ca_path" ) == 0 )
215 opt.ca_path = q;
216 else if( strcmp( p, "crt_file" ) == 0 )
217 opt.crt_file = q;
218 else if( strcmp( p, "key_file" ) == 0 )
219 opt.key_file = q;
220 else if( strcmp( p, "force_ciphersuite" ) == 0 )
221 {
222 opt.force_ciphersuite[0] = -1;
223
224 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
225
226 if( opt.force_ciphersuite[0] <= 0 )
227 {
228 ret = 2;
229 goto usage;
230 }
231 opt.force_ciphersuite[1] = 0;
232 }
233 else if( strcmp( p, "renegotiation" ) == 0 )
234 {
235 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
236 SSL_RENEGOTIATION_DISABLED;
237 }
238 else if( strcmp( p, "allow_legacy" ) == 0 )
239 {
240 opt.allow_legacy = atoi( q );
241 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
242 goto usage;
243 }
244 else
245 goto usage;
246 }
247
248 /*
249 * 0. Initialize the RNG and the session data
250 */
251 printf( "\n . Seeding the random number generator..." );
252 fflush( stdout );
253
254 entropy_init( &entropy );
255 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
256 (unsigned char *) pers, strlen( pers ) ) ) != 0 )
257 {
258 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
259 goto exit;
260 }
261
262 printf( " ok\n" );
263
264 /*
265 * 1.1. Load the trusted CA
266 */
267 printf( " . Loading the CA root certificate ..." );
268 fflush( stdout );
269
270#if defined(POLARSSL_FS_IO)
271 if( strlen( opt.ca_path ) )
272 ret = x509parse_crtpath( &cacert, opt.ca_path );
273 else if( strlen( opt.ca_file ) )
274 ret = x509parse_crtfile( &cacert, opt.ca_file );
275 else
276#endif
277#if defined(POLARSSL_CERTS_C)
278 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
279 strlen( test_ca_crt ) );
280#else
281 {
282 ret = 1;
283 printf("POLARSSL_CERTS_C not defined.");
284 }
285#endif
286 if( ret < 0 )
287 {
288 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
289 goto exit;
290 }
291
292 printf( " ok (%d skipped)\n", ret );
293
294 /*
295 * 1.2. Load own certificate and private key
296 */
297 printf( " . Loading the server cert. and key..." );
298 fflush( stdout );
299
300#if defined(POLARSSL_FS_IO)
301 if( strlen( opt.crt_file ) )
302 ret = x509parse_crtfile( &srvcert, opt.crt_file );
303 else
304#endif
305#if defined(POLARSSL_CERTS_C)
306 ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt,
307 strlen( test_srv_crt ) );
308#else
309 {
310 ret = 1;
311 printf("POLARSSL_CERTS_C not defined.");
312 }
313#endif
314 if( ret != 0 )
315 {
316 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
317 goto exit;
318 }
319
320#if defined(POLARSSL_FS_IO)
321 if( strlen( opt.key_file ) )
322 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
323 else
324#endif
325#if defined(POLARSSL_CERTS_C)
326 ret = x509parse_key( &rsa, (unsigned char *) test_srv_key,
327 strlen( test_srv_key ), NULL, 0 );
328#else
329 {
330 ret = 1;
331 printf("POLARSSL_CERTS_C not defined.");
332 }
333#endif
334 if( ret != 0 )
335 {
336 printf( " failed\n ! x509parse_key returned -0x%x\n\n", -ret );
337 goto exit;
338 }
339
340 printf( " ok\n" );
341
342 /*
343 * 2. Setup the listening TCP socket
344 */
345 printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
346 fflush( stdout );
347
348 if( ( ret = net_bind( &listen_fd, NULL, opt.server_port ) ) != 0 )
349 {
350 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
351 goto exit;
352 }
353
354 printf( " ok\n" );
355
356 /*
357 * 3. Setup stuff
358 */
359 printf( " . Setting up the SSL/TLS structure..." );
360 fflush( stdout );
361
362 if( ( ret = ssl_init( &ssl ) ) != 0 )
363 {
364 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
365 goto exit;
366 }
367
368 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
369 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
370
371 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
372 ssl_set_dbg( &ssl, my_debug, stdout );
373
Paul Bakker0a597072012-09-25 21:55:46 +0000374#if defined(POLARSSL_SSL_CACHE_C)
375 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
376 ssl_cache_set, &cache );
377#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000378
379 if( opt.force_ciphersuite[0] == DFL_FORCE_CIPHER )
380 ssl_set_ciphersuites( &ssl, ssl_default_ciphersuites );
381 else
382 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
383
384 ssl_set_renegotiation( &ssl, opt.renegotiation );
385 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
386
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000387 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
388 ssl_set_own_cert( &ssl, &srvcert, &rsa );
389
390#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +0000391 /*
392 * Use different group than default DHM group
393 */
Paul Bakkerd4324102012-09-26 08:29:38 +0000394 ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
395 POLARSSL_DHM_RFC5114_MODP_2048_G );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000396#endif
397
398 printf( " ok\n" );
399
400reset:
401#ifdef POLARSSL_ERROR_C
402 if( ret != 0 )
403 {
404 char error_buf[100];
405 error_strerror( ret, error_buf, 100 );
406 printf("Last error was: %d - %s\n\n", ret, error_buf );
407 }
408#endif
409
410 if( client_fd != -1 )
411 net_close( client_fd );
412
413 ssl_session_reset( &ssl );
414
415 /*
416 * 3. Wait until a client connects
417 */
418#if defined(_WIN32_WCE)
419 {
420 SHELLEXECUTEINFO sei;
421
422 ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) );
423
424 sei.cbSize = sizeof( SHELLEXECUTEINFO );
425 sei.fMask = 0;
426 sei.hwnd = 0;
427 sei.lpVerb = _T( "open" );
428 sei.lpFile = _T( "https://localhost:4433/" );
429 sei.lpParameters = NULL;
430 sei.lpDirectory = NULL;
431 sei.nShow = SW_SHOWNORMAL;
432
433 ShellExecuteEx( &sei );
434 }
435#elif defined(_WIN32)
436 ShellExecute( NULL, "open", "https://localhost:4433/",
437 NULL, NULL, SW_SHOWNORMAL );
438#endif
439
440 client_fd = -1;
441
442 printf( " . Waiting for a remote connection ..." );
443 fflush( stdout );
444
445 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
446 {
447 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
448 goto exit;
449 }
450
451 ssl_set_bio( &ssl, net_recv, &client_fd,
452 net_send, &client_fd );
453
454 printf( " ok\n" );
455
456 /*
457 * 4. Handshake
458 */
459 printf( " . Performing the SSL/TLS handshake..." );
460 fflush( stdout );
461
462 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
463 {
464 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
465 {
466 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
467 goto exit;
468 }
469 }
470
471 printf( " ok\n [ Ciphersuite is %s ]\n",
472 ssl_get_ciphersuite( &ssl ) );
473
474 /*
475 * 5. Verify the server certificate
476 */
477 printf( " . Verifying peer X.509 certificate..." );
478
479 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
480 {
481 printf( " failed\n" );
482
483 if( !ssl.session->peer_cert )
484 printf( " ! no client certificate sent\n" );
485
486 if( ( ret & BADCERT_EXPIRED ) != 0 )
487 printf( " ! client certificate has expired\n" );
488
489 if( ( ret & BADCERT_REVOKED ) != 0 )
490 printf( " ! client certificate has been revoked\n" );
491
492 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
493 printf( " ! self-signed or not signed by a trusted CA\n" );
494
495 printf( "\n" );
496 }
497 else
498 printf( " ok\n" );
499
500 if( ssl.session->peer_cert )
501 {
502 printf( " . Peer certificate information ...\n" );
503 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
504 ssl.session->peer_cert );
505 printf( "%s\n", buf );
506 }
507
508 /*
509 * 6. Read the HTTP Request
510 */
511 printf( " < Read from client:" );
512 fflush( stdout );
513
514 do
515 {
516 len = sizeof( buf ) - 1;
517 memset( buf, 0, sizeof( buf ) );
518 ret = ssl_read( &ssl, buf, len );
519
520 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
521 continue;
522
523 if( ret <= 0 )
524 {
525 switch( ret )
526 {
527 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
528 printf( " connection was closed gracefully\n" );
529 break;
530
531 case POLARSSL_ERR_NET_CONN_RESET:
532 printf( " connection was reset by peer\n" );
533 break;
534
535 default:
536 printf( " ssl_read returned -0x%x\n", -ret );
537 break;
538 }
539
540 break;
541 }
542
543 len = ret;
544 printf( " %d bytes read\n\n%s", len, (char *) buf );
545
546 if( ret > 0 )
547 break;
548 }
549 while( 1 );
550
551 /*
552 * 7. Write the 200 Response
553 */
554 printf( " > Write to client:" );
555 fflush( stdout );
556
557 len = sprintf( (char *) buf, HTTP_RESPONSE,
558 ssl_get_ciphersuite( &ssl ) );
559
560 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
561 {
562 if( ret == POLARSSL_ERR_NET_CONN_RESET )
563 {
564 printf( " failed\n ! peer closed the connection\n\n" );
565 goto reset;
566 }
567
568 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
569 {
570 printf( " failed\n ! ssl_write returned %d\n\n", ret );
571 goto exit;
572 }
573 }
574
575 len = ret;
576 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
577
578 ret = 0;
579 goto reset;
580
581exit:
582
583#ifdef POLARSSL_ERROR_C
584 if( ret != 0 )
585 {
586 char error_buf[100];
587 error_strerror( ret, error_buf, 100 );
588 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
589 }
590#endif
591
592 net_close( client_fd );
593 x509_free( &srvcert );
594 x509_free( &cacert );
595 rsa_free( &rsa );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000596 ssl_free( &ssl );
597
Paul Bakker0a597072012-09-25 21:55:46 +0000598#if defined(POLARSSL_SSL_CACHE_C)
599 ssl_cache_free( &cache );
600#endif
601
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000602#if defined(_WIN32)
603 printf( " + Press Enter to exit this program.\n" );
604 fflush( stdout ); getchar();
605#endif
606
607 return( ret );
608}
609#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
610 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
611 POLARSSL_CTR_DRBG_C */