blob: 22b88e46aab4d4c5b8d628f516d0a283d26b4914 [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 Bakkerd4324102012-09-26 08:29:38 +0000391 ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
392 POLARSSL_DHM_RFC5114_MODP_2048_G );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000393#endif
394
395 printf( " ok\n" );
396
397reset:
398#ifdef POLARSSL_ERROR_C
399 if( ret != 0 )
400 {
401 char error_buf[100];
402 error_strerror( ret, error_buf, 100 );
403 printf("Last error was: %d - %s\n\n", ret, error_buf );
404 }
405#endif
406
407 if( client_fd != -1 )
408 net_close( client_fd );
409
410 ssl_session_reset( &ssl );
411
412 /*
413 * 3. Wait until a client connects
414 */
415#if defined(_WIN32_WCE)
416 {
417 SHELLEXECUTEINFO sei;
418
419 ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) );
420
421 sei.cbSize = sizeof( SHELLEXECUTEINFO );
422 sei.fMask = 0;
423 sei.hwnd = 0;
424 sei.lpVerb = _T( "open" );
425 sei.lpFile = _T( "https://localhost:4433/" );
426 sei.lpParameters = NULL;
427 sei.lpDirectory = NULL;
428 sei.nShow = SW_SHOWNORMAL;
429
430 ShellExecuteEx( &sei );
431 }
432#elif defined(_WIN32)
433 ShellExecute( NULL, "open", "https://localhost:4433/",
434 NULL, NULL, SW_SHOWNORMAL );
435#endif
436
437 client_fd = -1;
438
439 printf( " . Waiting for a remote connection ..." );
440 fflush( stdout );
441
442 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
443 {
444 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
445 goto exit;
446 }
447
448 ssl_set_bio( &ssl, net_recv, &client_fd,
449 net_send, &client_fd );
450
451 printf( " ok\n" );
452
453 /*
454 * 4. Handshake
455 */
456 printf( " . Performing the SSL/TLS handshake..." );
457 fflush( stdout );
458
459 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
460 {
461 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
462 {
463 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
464 goto exit;
465 }
466 }
467
468 printf( " ok\n [ Ciphersuite is %s ]\n",
469 ssl_get_ciphersuite( &ssl ) );
470
471 /*
472 * 5. Verify the server certificate
473 */
474 printf( " . Verifying peer X.509 certificate..." );
475
476 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
477 {
478 printf( " failed\n" );
479
480 if( !ssl.session->peer_cert )
481 printf( " ! no client certificate sent\n" );
482
483 if( ( ret & BADCERT_EXPIRED ) != 0 )
484 printf( " ! client certificate has expired\n" );
485
486 if( ( ret & BADCERT_REVOKED ) != 0 )
487 printf( " ! client certificate has been revoked\n" );
488
489 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
490 printf( " ! self-signed or not signed by a trusted CA\n" );
491
492 printf( "\n" );
493 }
494 else
495 printf( " ok\n" );
496
497 if( ssl.session->peer_cert )
498 {
499 printf( " . Peer certificate information ...\n" );
500 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
501 ssl.session->peer_cert );
502 printf( "%s\n", buf );
503 }
504
505 /*
506 * 6. Read the HTTP Request
507 */
508 printf( " < Read from client:" );
509 fflush( stdout );
510
511 do
512 {
513 len = sizeof( buf ) - 1;
514 memset( buf, 0, sizeof( buf ) );
515 ret = ssl_read( &ssl, buf, len );
516
517 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
518 continue;
519
520 if( ret <= 0 )
521 {
522 switch( ret )
523 {
524 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
525 printf( " connection was closed gracefully\n" );
526 break;
527
528 case POLARSSL_ERR_NET_CONN_RESET:
529 printf( " connection was reset by peer\n" );
530 break;
531
532 default:
533 printf( " ssl_read returned -0x%x\n", -ret );
534 break;
535 }
536
537 break;
538 }
539
540 len = ret;
541 printf( " %d bytes read\n\n%s", len, (char *) buf );
542
543 if( ret > 0 )
544 break;
545 }
546 while( 1 );
547
548 /*
549 * 7. Write the 200 Response
550 */
551 printf( " > Write to client:" );
552 fflush( stdout );
553
554 len = sprintf( (char *) buf, HTTP_RESPONSE,
555 ssl_get_ciphersuite( &ssl ) );
556
557 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
558 {
559 if( ret == POLARSSL_ERR_NET_CONN_RESET )
560 {
561 printf( " failed\n ! peer closed the connection\n\n" );
562 goto reset;
563 }
564
565 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
566 {
567 printf( " failed\n ! ssl_write returned %d\n\n", ret );
568 goto exit;
569 }
570 }
571
572 len = ret;
573 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
574
575 ret = 0;
576 goto reset;
577
578exit:
579
580#ifdef POLARSSL_ERROR_C
581 if( ret != 0 )
582 {
583 char error_buf[100];
584 error_strerror( ret, error_buf, 100 );
585 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
586 }
587#endif
588
589 net_close( client_fd );
590 x509_free( &srvcert );
591 x509_free( &cacert );
592 rsa_free( &rsa );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000593 ssl_free( &ssl );
594
Paul Bakker0a597072012-09-25 21:55:46 +0000595#if defined(POLARSSL_SSL_CACHE_C)
596 ssl_cache_free( &cache );
597#endif
598
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000599#if defined(_WIN32)
600 printf( " + Press Enter to exit this program.\n" );
601 fflush( stdout ); getchar();
602#endif
603
604 return( ret );
605}
606#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
607 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
608 POLARSSL_CTR_DRBG_C */