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