blob: d367c4ec6d079b24606b2b3ed4ac1c59584814a7 [file] [log] [blame]
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001/*
2 * SSL client with options
3 *
Paul Bakker3c5ef712013-06-25 16:37:45 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00005 *
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 Bakker82024bf2013-07-04 11:52:32 +020052#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
53#include "polarssl/memory.h"
54#endif
55
Paul Bakkerb60b95f2012-09-25 09:05:17 +000056#define DFL_SERVER_PORT 4433
57#define DFL_REQUEST_PAGE "/"
58#define DFL_DEBUG_LEVEL 0
59#define DFL_CA_FILE ""
60#define DFL_CA_PATH ""
61#define DFL_CRT_FILE ""
62#define DFL_KEY_FILE ""
Paul Bakkerfbb17802013-04-17 19:10:21 +020063#define DFL_PSK ""
64#define DFL_PSK_IDENTITY "Client_identity"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000065#define DFL_FORCE_CIPHER 0
66#define DFL_RENEGOTIATION SSL_RENEGOTIATION_ENABLED
67#define DFL_ALLOW_LEGACY SSL_LEGACY_NO_RENEGOTIATION
Paul Bakker1d29fb52012-09-28 13:28:45 +000068#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +020069#define DFL_MAX_VERSION -1
Paul Bakker91ebfb52012-11-23 14:04:08 +010070#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000071
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +020072#define LONG_RESPONSE "<p>blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
73 "blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
74 "blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
75 "blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah</p>\r\n"
76
77/* Uncomment LONG_RESPONSE to test sending long paquets */
Paul Bakkerb60b95f2012-09-25 09:05:17 +000078#define HTTP_RESPONSE \
79 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
80 "<h2>PolarSSL Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +020081 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +000082
83/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +000084 * global options
85 */
86struct options
87{
88 int server_port; /* port on which the ssl service runs */
89 int debug_level; /* level of debugging */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020090 const char *ca_file; /* the file with the CA certificate(s) */
91 const char *ca_path; /* the path with the CA certificate(s) reside */
92 const char *crt_file; /* the file with the client certificate */
93 const char *key_file; /* the file with the client key */
94 const char *psk; /* the pre-shared key */
95 const char *psk_identity; /* the pre-shared key identity */
Paul Bakkerb60b95f2012-09-25 09:05:17 +000096 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
97 int renegotiation; /* enable / disable renegotiation */
98 int allow_legacy; /* allow legacy renegotiation */
Paul Bakker1d29fb52012-09-28 13:28:45 +000099 int min_version; /* minimum protocol version accepted */
Paul Bakkerc1516be2013-06-29 16:01:32 +0200100 int max_version; /* maximum protocol version accepted */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100101 int auth_mode; /* verify mode for connection */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000102} opt;
103
Paul Bakker3c5ef712013-06-25 16:37:45 +0200104static void my_debug( void *ctx, int level, const char *str )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000105{
106 if( level < opt.debug_level )
107 {
108 fprintf( (FILE *) ctx, "%s", str );
109 fflush( (FILE *) ctx );
110 }
111}
112
Paul Bakkered27a042013-04-18 22:46:23 +0200113#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000114#if defined(POLARSSL_FS_IO)
115#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100116 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
117 " default: \"\" (pre-loaded)\n" \
118 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
119 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
120 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
121 " default: \"\" (pre-loaded)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000122 " key_file=%%s default: \"\" (pre-loaded)\n"
123#else
124#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200125 "\n" \
126 " No file operations available (POLARSSL_FS_IO not defined)\n" \
127 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000128#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200129#else
130#define USAGE_IO ""
131#endif /* POLARSSL_X509_PARSE_C */
132
133#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
134#define USAGE_PSK \
135 " psk=%%s default: \"\" (in hex, without 0x)\n" \
136 " psk_identity=%%s default: \"Client_identity\"\n"
137#else
138#define USAGE_PSK ""
139#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000140
141#define USAGE \
142 "\n usage: ssl_server2 param=<>...\n" \
143 "\n acceptable parameters:\n" \
144 " server_port=%%d default: 4433\n" \
145 " debug_level=%%d default: 0 (disabled)\n" \
146 USAGE_IO \
147 " request_page=%%s default: \".\"\n" \
148 " renegotiation=%%d default: 1 (enabled)\n" \
149 " allow_legacy=%%d default: 0 (disabled)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000150 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200151 " max_version=%%s default: \"tls1_2\"\n" \
152 " force_version=%%s default: \"\" (none)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000153 " options: ssl3, tls1, tls1_1, tls1_2\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200154 " auth_mode=%%s default: \"optional\"\n" \
Paul Bakker91ebfb52012-11-23 14:04:08 +0100155 " options: none, optional, required\n" \
Paul Bakkered27a042013-04-18 22:46:23 +0200156 USAGE_PSK \
Paul Bakkerfbb17802013-04-17 19:10:21 +0200157 "\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000158 " force_ciphersuite=<name> default: all enabled\n"\
159 " acceptable ciphersuite names:\n"
160
Paul Bakkered27a042013-04-18 22:46:23 +0200161#if !defined(POLARSSL_ENTROPY_C) || \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000162 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
Paul Bakkered27a042013-04-18 22:46:23 +0200163 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000164int main( int argc, char *argv[] )
165{
166 ((void) argc);
167 ((void) argv);
168
Paul Bakkered27a042013-04-18 22:46:23 +0200169 printf("POLARSSL_ENTROPY_C and/or "
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000170 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
Paul Bakkered27a042013-04-18 22:46:23 +0200171 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000172 return( 0 );
173}
174#else
175int main( int argc, char *argv[] )
176{
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200177 int ret = 0, len, written;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000178 int listen_fd;
179 int client_fd = -1;
180 unsigned char buf[1024];
Paul Bakkered27a042013-04-18 22:46:23 +0200181#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Paul Bakkerfbb17802013-04-17 19:10:21 +0200182 unsigned char psk[256];
183 size_t psk_len = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200184#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200185 const char *pers = "ssl_server2";
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000186
187 entropy_context entropy;
188 ctr_drbg_context ctr_drbg;
189 ssl_context ssl;
Paul Bakkered27a042013-04-18 22:46:23 +0200190#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000191 x509_cert cacert;
192 x509_cert srvcert;
193 rsa_context rsa;
Paul Bakkered27a042013-04-18 22:46:23 +0200194#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000195#if defined(POLARSSL_SSL_CACHE_C)
196 ssl_cache_context cache;
197#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200198#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
199 unsigned char alloc_buf[100000];
200#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000201
202 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000203 char *p, *q;
204 const int *list;
205
Paul Bakker82024bf2013-07-04 11:52:32 +0200206#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
207 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
208#endif
209
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000210 /*
211 * Make sure memory references are valid.
212 */
213 listen_fd = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200214#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000215 memset( &cacert, 0, sizeof( x509_cert ) );
216 memset( &srvcert, 0, sizeof( x509_cert ) );
217 memset( &rsa, 0, sizeof( rsa_context ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200218#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000219#if defined(POLARSSL_SSL_CACHE_C)
220 ssl_cache_init( &cache );
221#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000222
223 if( argc == 0 )
224 {
225 usage:
226 if( ret == 0 )
227 ret = 1;
228
229 printf( USAGE );
230
231 list = ssl_list_ciphersuites();
232 while( *list )
233 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200234 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200235 list++;
236 if( !*list )
237 break;
238 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000239 list++;
240 }
241 printf("\n");
242 goto exit;
243 }
244
245 opt.server_port = DFL_SERVER_PORT;
246 opt.debug_level = DFL_DEBUG_LEVEL;
247 opt.ca_file = DFL_CA_FILE;
248 opt.ca_path = DFL_CA_PATH;
249 opt.crt_file = DFL_CRT_FILE;
250 opt.key_file = DFL_KEY_FILE;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200251 opt.psk = DFL_PSK;
252 opt.psk_identity = DFL_PSK_IDENTITY;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000253 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
254 opt.renegotiation = DFL_RENEGOTIATION;
255 opt.allow_legacy = DFL_ALLOW_LEGACY;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000256 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200257 opt.max_version = DFL_MAX_VERSION;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100258 opt.auth_mode = DFL_AUTH_MODE;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000259
260 for( i = 1; i < argc; i++ )
261 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000262 p = argv[i];
263 if( ( q = strchr( p, '=' ) ) == NULL )
264 goto usage;
265 *q++ = '\0';
266
267 if( strcmp( p, "server_port" ) == 0 )
268 {
269 opt.server_port = atoi( q );
270 if( opt.server_port < 1 || opt.server_port > 65535 )
271 goto usage;
272 }
273 else if( strcmp( p, "debug_level" ) == 0 )
274 {
275 opt.debug_level = atoi( q );
276 if( opt.debug_level < 0 || opt.debug_level > 65535 )
277 goto usage;
278 }
279 else if( strcmp( p, "ca_file" ) == 0 )
280 opt.ca_file = q;
281 else if( strcmp( p, "ca_path" ) == 0 )
282 opt.ca_path = q;
283 else if( strcmp( p, "crt_file" ) == 0 )
284 opt.crt_file = q;
285 else if( strcmp( p, "key_file" ) == 0 )
286 opt.key_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200287 else if( strcmp( p, "psk" ) == 0 )
288 opt.psk = q;
289 else if( strcmp( p, "psk_identity" ) == 0 )
290 opt.psk_identity = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000291 else if( strcmp( p, "force_ciphersuite" ) == 0 )
292 {
293 opt.force_ciphersuite[0] = -1;
294
295 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
296
297 if( opt.force_ciphersuite[0] <= 0 )
298 {
299 ret = 2;
300 goto usage;
301 }
302 opt.force_ciphersuite[1] = 0;
303 }
304 else if( strcmp( p, "renegotiation" ) == 0 )
305 {
306 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
307 SSL_RENEGOTIATION_DISABLED;
308 }
309 else if( strcmp( p, "allow_legacy" ) == 0 )
310 {
311 opt.allow_legacy = atoi( q );
312 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
313 goto usage;
314 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000315 else if( strcmp( p, "min_version" ) == 0 )
316 {
317 if( strcmp( q, "ssl3" ) == 0 )
318 opt.min_version = SSL_MINOR_VERSION_0;
319 else if( strcmp( q, "tls1" ) == 0 )
320 opt.min_version = SSL_MINOR_VERSION_1;
321 else if( strcmp( q, "tls1_1" ) == 0 )
322 opt.min_version = SSL_MINOR_VERSION_2;
323 else if( strcmp( q, "tls1_2" ) == 0 )
324 opt.min_version = SSL_MINOR_VERSION_3;
325 else
326 goto usage;
327 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200328 else if( strcmp( p, "max_version" ) == 0 )
329 {
330 if( strcmp( q, "ssl3" ) == 0 )
331 opt.max_version = SSL_MINOR_VERSION_0;
332 else if( strcmp( q, "tls1" ) == 0 )
333 opt.max_version = SSL_MINOR_VERSION_1;
334 else if( strcmp( q, "tls1_1" ) == 0 )
335 opt.max_version = SSL_MINOR_VERSION_2;
336 else if( strcmp( q, "tls1_2" ) == 0 )
337 opt.max_version = SSL_MINOR_VERSION_3;
338 else
339 goto usage;
340 }
341 else if( strcmp( p, "force_version" ) == 0 )
342 {
343 if( strcmp( q, "ssl3" ) == 0 )
344 {
345 opt.min_version = SSL_MINOR_VERSION_0;
346 opt.max_version = SSL_MINOR_VERSION_0;
347 }
348 else if( strcmp( q, "tls1" ) == 0 )
349 {
350 opt.min_version = SSL_MINOR_VERSION_1;
351 opt.max_version = SSL_MINOR_VERSION_1;
352 }
353 else if( strcmp( q, "tls1_1" ) == 0 )
354 {
355 opt.min_version = SSL_MINOR_VERSION_2;
356 opt.max_version = SSL_MINOR_VERSION_2;
357 }
358 else if( strcmp( q, "tls1_2" ) == 0 )
359 {
360 opt.min_version = SSL_MINOR_VERSION_3;
361 opt.max_version = SSL_MINOR_VERSION_3;
362 }
363 else
364 goto usage;
365 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100366 else if( strcmp( p, "auth_mode" ) == 0 )
367 {
368 if( strcmp( q, "none" ) == 0 )
369 opt.auth_mode = SSL_VERIFY_NONE;
370 else if( strcmp( q, "optional" ) == 0 )
371 opt.auth_mode = SSL_VERIFY_OPTIONAL;
372 else if( strcmp( q, "required" ) == 0 )
373 opt.auth_mode = SSL_VERIFY_REQUIRED;
374 else
375 goto usage;
376 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000377 else
378 goto usage;
379 }
380
Paul Bakkerc1516be2013-06-29 16:01:32 +0200381 if( opt.force_ciphersuite[0] > 0 )
382 {
383 const ssl_ciphersuite_t *ciphersuite_info;
384 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
385
386 if( ciphersuite_info->min_minor_ver > opt.max_version ||
387 ciphersuite_info->max_minor_ver < opt.min_version )
388 {
389 printf("forced ciphersuite not allowed with this protocol version\n");
390 ret = 2;
391 goto usage;
392 }
393 }
394
Paul Bakkered27a042013-04-18 22:46:23 +0200395#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000396 /*
Paul Bakkerfbb17802013-04-17 19:10:21 +0200397 * Unhexify the pre-shared key if any is given
398 */
399 if( strlen( opt.psk ) )
400 {
401 unsigned char c;
402 size_t j;
403
404 if( strlen( opt.psk ) % 2 != 0 )
405 {
406 printf("pre-shared key not valid hex\n");
407 goto exit;
408 }
409
410 psk_len = strlen( opt.psk ) / 2;
411
412 for( j = 0; j < strlen( opt.psk ); j += 2 )
413 {
414 c = opt.psk[j];
415 if( c >= '0' && c <= '9' )
416 c -= '0';
417 else if( c >= 'a' && c <= 'f' )
418 c -= 'a' - 10;
419 else if( c >= 'A' && c <= 'F' )
420 c -= 'A' - 10;
421 else
422 {
423 printf("pre-shared key not valid hex\n");
424 goto exit;
425 }
426 psk[ j / 2 ] = c << 4;
427
428 c = opt.psk[j + 1];
429 if( c >= '0' && c <= '9' )
430 c -= '0';
431 else if( c >= 'a' && c <= 'f' )
432 c -= 'a' - 10;
433 else if( c >= 'A' && c <= 'F' )
434 c -= 'A' - 10;
435 else
436 {
437 printf("pre-shared key not valid hex\n");
438 goto exit;
439 }
440 psk[ j / 2 ] |= c;
441 }
442 }
Paul Bakkered27a042013-04-18 22:46:23 +0200443#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +0200444
445 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000446 * 0. Initialize the RNG and the session data
447 */
448 printf( "\n . Seeding the random number generator..." );
449 fflush( stdout );
450
451 entropy_init( &entropy );
452 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200453 (const unsigned char *) pers,
454 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000455 {
456 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
457 goto exit;
458 }
459
460 printf( " ok\n" );
461
Paul Bakkered27a042013-04-18 22:46:23 +0200462#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000463 /*
464 * 1.1. Load the trusted CA
465 */
466 printf( " . Loading the CA root certificate ..." );
467 fflush( stdout );
468
469#if defined(POLARSSL_FS_IO)
470 if( strlen( opt.ca_path ) )
471 ret = x509parse_crtpath( &cacert, opt.ca_path );
472 else if( strlen( opt.ca_file ) )
473 ret = x509parse_crtfile( &cacert, opt.ca_file );
474 else
475#endif
476#if defined(POLARSSL_CERTS_C)
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200477 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000478 strlen( test_ca_crt ) );
479#else
480 {
481 ret = 1;
482 printf("POLARSSL_CERTS_C not defined.");
483 }
484#endif
485 if( ret < 0 )
486 {
487 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
488 goto exit;
489 }
490
491 printf( " ok (%d skipped)\n", ret );
492
493 /*
494 * 1.2. Load own certificate and private key
495 */
496 printf( " . Loading the server cert. and key..." );
497 fflush( stdout );
498
499#if defined(POLARSSL_FS_IO)
500 if( strlen( opt.crt_file ) )
501 ret = x509parse_crtfile( &srvcert, opt.crt_file );
502 else
503#endif
504#if defined(POLARSSL_CERTS_C)
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200505 ret = x509parse_crt( &srvcert, (const unsigned char *) test_srv_crt,
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000506 strlen( test_srv_crt ) );
507#else
508 {
509 ret = 1;
510 printf("POLARSSL_CERTS_C not defined.");
511 }
512#endif
513 if( ret != 0 )
514 {
515 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
516 goto exit;
517 }
518
519#if defined(POLARSSL_FS_IO)
520 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200521 ret = x509parse_keyfile_rsa( &rsa, opt.key_file, "" );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000522 else
523#endif
524#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200525 ret = x509parse_key_rsa( &rsa, (const unsigned char *) test_srv_key,
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000526 strlen( test_srv_key ), NULL, 0 );
527#else
528 {
529 ret = 1;
530 printf("POLARSSL_CERTS_C not defined.");
531 }
532#endif
533 if( ret != 0 )
534 {
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200535 printf( " failed\n ! x509parse_key_rsa returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000536 goto exit;
537 }
538
539 printf( " ok\n" );
Paul Bakkered27a042013-04-18 22:46:23 +0200540#endif /* POLARSSL_X509_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000541
542 /*
543 * 2. Setup the listening TCP socket
544 */
545 printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
546 fflush( stdout );
547
548 if( ( ret = net_bind( &listen_fd, NULL, opt.server_port ) ) != 0 )
549 {
550 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
551 goto exit;
552 }
553
554 printf( " ok\n" );
555
556 /*
557 * 3. Setup stuff
558 */
559 printf( " . Setting up the SSL/TLS structure..." );
560 fflush( stdout );
561
562 if( ( ret = ssl_init( &ssl ) ) != 0 )
563 {
564 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
565 goto exit;
566 }
567
568 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +0100569 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000570
571 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
572 ssl_set_dbg( &ssl, my_debug, stdout );
573
Paul Bakker0a597072012-09-25 21:55:46 +0000574#if defined(POLARSSL_SSL_CACHE_C)
575 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
576 ssl_cache_set, &cache );
577#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000578
Paul Bakker41c83d32013-03-20 14:39:14 +0100579 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000580 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
581
582 ssl_set_renegotiation( &ssl, opt.renegotiation );
583 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
584
Paul Bakkered27a042013-04-18 22:46:23 +0200585#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000586 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
587 ssl_set_own_cert( &ssl, &srvcert, &rsa );
Paul Bakkered27a042013-04-18 22:46:23 +0200588#endif
589
590#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Paul Bakker3c5ef712013-06-25 16:37:45 +0200591 ssl_set_psk( &ssl, psk, psk_len, (const unsigned char *) opt.psk_identity,
Paul Bakkerfbb17802013-04-17 19:10:21 +0200592 strlen( opt.psk_identity ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200593#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000594
595#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +0000596 /*
597 * Use different group than default DHM group
598 */
Paul Bakkerd4324102012-09-26 08:29:38 +0000599 ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
600 POLARSSL_DHM_RFC5114_MODP_2048_G );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000601#endif
602
Paul Bakker1d29fb52012-09-28 13:28:45 +0000603 if( opt.min_version != -1 )
604 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
605
Paul Bakkerc1516be2013-06-29 16:01:32 +0200606 if( opt.max_version != -1 )
607 ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
608
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000609 printf( " ok\n" );
610
611reset:
612#ifdef POLARSSL_ERROR_C
613 if( ret != 0 )
614 {
615 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200616 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000617 printf("Last error was: %d - %s\n\n", ret, error_buf );
618 }
619#endif
620
621 if( client_fd != -1 )
622 net_close( client_fd );
623
624 ssl_session_reset( &ssl );
625
626 /*
627 * 3. Wait until a client connects
628 */
629#if defined(_WIN32_WCE)
630 {
631 SHELLEXECUTEINFO sei;
632
633 ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) );
634
635 sei.cbSize = sizeof( SHELLEXECUTEINFO );
636 sei.fMask = 0;
637 sei.hwnd = 0;
638 sei.lpVerb = _T( "open" );
639 sei.lpFile = _T( "https://localhost:4433/" );
640 sei.lpParameters = NULL;
641 sei.lpDirectory = NULL;
642 sei.nShow = SW_SHOWNORMAL;
643
644 ShellExecuteEx( &sei );
645 }
646#elif defined(_WIN32)
647 ShellExecute( NULL, "open", "https://localhost:4433/",
648 NULL, NULL, SW_SHOWNORMAL );
649#endif
650
651 client_fd = -1;
652
653 printf( " . Waiting for a remote connection ..." );
654 fflush( stdout );
655
656 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
657 {
658 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
659 goto exit;
660 }
661
662 ssl_set_bio( &ssl, net_recv, &client_fd,
663 net_send, &client_fd );
664
665 printf( " ok\n" );
666
667 /*
668 * 4. Handshake
669 */
670 printf( " . Performing the SSL/TLS handshake..." );
671 fflush( stdout );
672
673 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
674 {
675 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
676 {
677 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000678 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000679 }
680 }
681
682 printf( " ok\n [ Ciphersuite is %s ]\n",
683 ssl_get_ciphersuite( &ssl ) );
684
Paul Bakkered27a042013-04-18 22:46:23 +0200685#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000686 /*
687 * 5. Verify the server certificate
688 */
689 printf( " . Verifying peer X.509 certificate..." );
690
691 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
692 {
693 printf( " failed\n" );
694
Paul Bakkerb0550d92012-10-30 07:51:03 +0000695 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000696 printf( " ! no client certificate sent\n" );
697
698 if( ( ret & BADCERT_EXPIRED ) != 0 )
699 printf( " ! client certificate has expired\n" );
700
701 if( ( ret & BADCERT_REVOKED ) != 0 )
702 printf( " ! client certificate has been revoked\n" );
703
704 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
705 printf( " ! self-signed or not signed by a trusted CA\n" );
706
707 printf( "\n" );
708 }
709 else
710 printf( " ok\n" );
711
Paul Bakkerb0550d92012-10-30 07:51:03 +0000712 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000713 {
714 printf( " . Peer certificate information ...\n" );
715 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
Paul Bakkerb0550d92012-10-30 07:51:03 +0000716 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000717 printf( "%s\n", buf );
718 }
Paul Bakkered27a042013-04-18 22:46:23 +0200719#endif /* POLARSSL_X509_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000720
721 /*
722 * 6. Read the HTTP Request
723 */
724 printf( " < Read from client:" );
725 fflush( stdout );
726
727 do
728 {
729 len = sizeof( buf ) - 1;
730 memset( buf, 0, sizeof( buf ) );
731 ret = ssl_read( &ssl, buf, len );
732
733 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
734 continue;
735
736 if( ret <= 0 )
737 {
738 switch( ret )
739 {
740 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
741 printf( " connection was closed gracefully\n" );
742 break;
743
744 case POLARSSL_ERR_NET_CONN_RESET:
745 printf( " connection was reset by peer\n" );
746 break;
747
748 default:
749 printf( " ssl_read returned -0x%x\n", -ret );
750 break;
751 }
752
753 break;
754 }
755
756 len = ret;
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200757 printf( " %d bytes read\n\n%s\n", len, (char *) buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000758
Paul Bakker82024bf2013-07-04 11:52:32 +0200759 if( memcmp( buf, "SERVERQUIT", 10 ) == 0 )
760 goto exit;
761
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000762 if( ret > 0 )
763 break;
764 }
765 while( 1 );
766
767 /*
768 * 7. Write the 200 Response
769 */
770 printf( " > Write to client:" );
771 fflush( stdout );
772
773 len = sprintf( (char *) buf, HTTP_RESPONSE,
774 ssl_get_ciphersuite( &ssl ) );
775
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200776 for( written = 0; written < len; written += ret )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000777 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200778 while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000779 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200780 if( ret == POLARSSL_ERR_NET_CONN_RESET )
781 {
782 printf( " failed\n ! peer closed the connection\n\n" );
783 goto reset;
784 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000785
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200786 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
787 {
788 printf( " failed\n ! ssl_write returned %d\n\n", ret );
789 goto exit;
790 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000791 }
792 }
793
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200794 buf[written] = '\0';
795 printf( " %d bytes written\n\n%s\n", written, (char *) buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000796
797 ret = 0;
798 goto reset;
799
800exit:
801
802#ifdef POLARSSL_ERROR_C
803 if( ret != 0 )
804 {
805 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200806 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000807 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
808 }
809#endif
810
811 net_close( client_fd );
Paul Bakkered27a042013-04-18 22:46:23 +0200812#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000813 x509_free( &srvcert );
814 x509_free( &cacert );
815 rsa_free( &rsa );
Paul Bakkered27a042013-04-18 22:46:23 +0200816#endif
817
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000818 ssl_free( &ssl );
819
Paul Bakker0a597072012-09-25 21:55:46 +0000820#if defined(POLARSSL_SSL_CACHE_C)
821 ssl_cache_free( &cache );
822#endif
823
Paul Bakker82024bf2013-07-04 11:52:32 +0200824#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && defined(POLARSSL_MEMORY_DEBUG)
825 memory_buffer_alloc_status();
826#endif
827
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000828#if defined(_WIN32)
829 printf( " + Press Enter to exit this program.\n" );
830 fflush( stdout ); getchar();
831#endif
832
833 return( ret );
834}
835#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
836 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
837 POLARSSL_CTR_DRBG_C */