blob: abca186d63f58145b490c27f9c330fa1a914b658 [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
Paul Bakker1d29fb52012-09-28 13:28:45 +000062#define DFL_MIN_VERSION -1
Paul Bakker91ebfb52012-11-23 14:04:08 +010063#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000064
65#define HTTP_RESPONSE \
66 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
67 "<h2>PolarSSL Test Server</h2>\r\n" \
68 "<p>Successful connection using: %s</p>\r\n"
69
70/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +000071 * global options
72 */
73struct options
74{
75 int server_port; /* port on which the ssl service runs */
76 int debug_level; /* level of debugging */
77 char *ca_file; /* the file with the CA certificate(s) */
78 char *ca_path; /* the path with the CA certificate(s) reside */
79 char *crt_file; /* the file with the client certificate */
80 char *key_file; /* the file with the client key */
81 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
82 int renegotiation; /* enable / disable renegotiation */
83 int allow_legacy; /* allow legacy renegotiation */
Paul Bakker1d29fb52012-09-28 13:28:45 +000084 int min_version; /* minimum protocol version accepted */
Paul Bakker91ebfb52012-11-23 14:04:08 +010085 int auth_mode; /* verify mode for connection */
Paul Bakkerb60b95f2012-09-25 09:05:17 +000086} opt;
87
88void my_debug( void *ctx, int level, const char *str )
89{
90 if( level < opt.debug_level )
91 {
92 fprintf( (FILE *) ctx, "%s", str );
93 fflush( (FILE *) ctx );
94 }
95}
96
Paul Bakkerb60b95f2012-09-25 09:05:17 +000097#if defined(POLARSSL_FS_IO)
98#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +010099 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
100 " default: \"\" (pre-loaded)\n" \
101 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
102 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
103 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
104 " default: \"\" (pre-loaded)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000105 " key_file=%%s default: \"\" (pre-loaded)\n"
106#else
107#define USAGE_IO \
108 " No file operations available (POLARSSL_FS_IO not defined)\n"
109#endif /* POLARSSL_FS_IO */
110
111#define USAGE \
112 "\n usage: ssl_server2 param=<>...\n" \
113 "\n acceptable parameters:\n" \
114 " server_port=%%d default: 4433\n" \
115 " debug_level=%%d default: 0 (disabled)\n" \
116 USAGE_IO \
117 " request_page=%%s default: \".\"\n" \
118 " renegotiation=%%d default: 1 (enabled)\n" \
119 " allow_legacy=%%d default: 0 (disabled)\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000120 " min_version=%%s default: \"ssl3\"\n" \
121 " options: ssl3, tls1, tls1_1, tls1_2\n" \
Paul Bakker91ebfb52012-11-23 14:04:08 +0100122 " auth_mode=%%s default: \"optional\"\n" \
123 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000124 " force_ciphersuite=<name> default: all enabled\n"\
125 " acceptable ciphersuite names:\n"
126
127#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
128 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
129 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
130 !defined(POLARSSL_CTR_DRBG_C)
131int main( int argc, char *argv[] )
132{
133 ((void) argc);
134 ((void) argv);
135
136 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
137 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
138 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
139 "POLARSSL_CTR_DRBG_C not defined.\n");
140 return( 0 );
141}
142#else
143int main( int argc, char *argv[] )
144{
145 int ret = 0, len;
146 int listen_fd;
147 int client_fd = -1;
148 unsigned char buf[1024];
149 char *pers = "ssl_server2";
150
151 entropy_context entropy;
152 ctr_drbg_context ctr_drbg;
153 ssl_context ssl;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000154 x509_cert cacert;
155 x509_cert srvcert;
156 rsa_context rsa;
Paul Bakker0a597072012-09-25 21:55:46 +0000157#if defined(POLARSSL_SSL_CACHE_C)
158 ssl_cache_context cache;
159#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000160
161 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000162 char *p, *q;
163 const int *list;
164
165 /*
166 * Make sure memory references are valid.
167 */
168 listen_fd = 0;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000169 memset( &cacert, 0, sizeof( x509_cert ) );
170 memset( &srvcert, 0, sizeof( x509_cert ) );
171 memset( &rsa, 0, sizeof( rsa_context ) );
Paul Bakker0a597072012-09-25 21:55:46 +0000172#if defined(POLARSSL_SSL_CACHE_C)
173 ssl_cache_init( &cache );
174#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000175
176 if( argc == 0 )
177 {
178 usage:
179 if( ret == 0 )
180 ret = 1;
181
182 printf( USAGE );
183
184 list = ssl_list_ciphersuites();
185 while( *list )
186 {
187 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
188 list++;
189 }
190 printf("\n");
191 goto exit;
192 }
193
194 opt.server_port = DFL_SERVER_PORT;
195 opt.debug_level = DFL_DEBUG_LEVEL;
196 opt.ca_file = DFL_CA_FILE;
197 opt.ca_path = DFL_CA_PATH;
198 opt.crt_file = DFL_CRT_FILE;
199 opt.key_file = DFL_KEY_FILE;
200 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
201 opt.renegotiation = DFL_RENEGOTIATION;
202 opt.allow_legacy = DFL_ALLOW_LEGACY;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000203 opt.min_version = DFL_MIN_VERSION;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100204 opt.auth_mode = DFL_AUTH_MODE;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000205
206 for( i = 1; i < argc; i++ )
207 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000208 p = argv[i];
209 if( ( q = strchr( p, '=' ) ) == NULL )
210 goto usage;
211 *q++ = '\0';
212
213 if( strcmp( p, "server_port" ) == 0 )
214 {
215 opt.server_port = atoi( q );
216 if( opt.server_port < 1 || opt.server_port > 65535 )
217 goto usage;
218 }
219 else if( strcmp( p, "debug_level" ) == 0 )
220 {
221 opt.debug_level = atoi( q );
222 if( opt.debug_level < 0 || opt.debug_level > 65535 )
223 goto usage;
224 }
225 else if( strcmp( p, "ca_file" ) == 0 )
226 opt.ca_file = q;
227 else if( strcmp( p, "ca_path" ) == 0 )
228 opt.ca_path = q;
229 else if( strcmp( p, "crt_file" ) == 0 )
230 opt.crt_file = q;
231 else if( strcmp( p, "key_file" ) == 0 )
232 opt.key_file = q;
233 else if( strcmp( p, "force_ciphersuite" ) == 0 )
234 {
235 opt.force_ciphersuite[0] = -1;
236
237 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
238
239 if( opt.force_ciphersuite[0] <= 0 )
240 {
241 ret = 2;
242 goto usage;
243 }
244 opt.force_ciphersuite[1] = 0;
245 }
246 else if( strcmp( p, "renegotiation" ) == 0 )
247 {
248 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
249 SSL_RENEGOTIATION_DISABLED;
250 }
251 else if( strcmp( p, "allow_legacy" ) == 0 )
252 {
253 opt.allow_legacy = atoi( q );
254 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
255 goto usage;
256 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000257 else if( strcmp( p, "min_version" ) == 0 )
258 {
259 if( strcmp( q, "ssl3" ) == 0 )
260 opt.min_version = SSL_MINOR_VERSION_0;
261 else if( strcmp( q, "tls1" ) == 0 )
262 opt.min_version = SSL_MINOR_VERSION_1;
263 else if( strcmp( q, "tls1_1" ) == 0 )
264 opt.min_version = SSL_MINOR_VERSION_2;
265 else if( strcmp( q, "tls1_2" ) == 0 )
266 opt.min_version = SSL_MINOR_VERSION_3;
267 else
268 goto usage;
269 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100270 else if( strcmp( p, "auth_mode" ) == 0 )
271 {
272 if( strcmp( q, "none" ) == 0 )
273 opt.auth_mode = SSL_VERIFY_NONE;
274 else if( strcmp( q, "optional" ) == 0 )
275 opt.auth_mode = SSL_VERIFY_OPTIONAL;
276 else if( strcmp( q, "required" ) == 0 )
277 opt.auth_mode = SSL_VERIFY_REQUIRED;
278 else
279 goto usage;
280 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000281 else
282 goto usage;
283 }
284
285 /*
286 * 0. Initialize the RNG and the session data
287 */
288 printf( "\n . Seeding the random number generator..." );
289 fflush( stdout );
290
291 entropy_init( &entropy );
292 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
293 (unsigned char *) pers, strlen( pers ) ) ) != 0 )
294 {
295 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
296 goto exit;
297 }
298
299 printf( " ok\n" );
300
301 /*
302 * 1.1. Load the trusted CA
303 */
304 printf( " . Loading the CA root certificate ..." );
305 fflush( stdout );
306
307#if defined(POLARSSL_FS_IO)
308 if( strlen( opt.ca_path ) )
309 ret = x509parse_crtpath( &cacert, opt.ca_path );
310 else if( strlen( opt.ca_file ) )
311 ret = x509parse_crtfile( &cacert, opt.ca_file );
312 else
313#endif
314#if defined(POLARSSL_CERTS_C)
315 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
316 strlen( test_ca_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 printf( " ok (%d skipped)\n", ret );
330
331 /*
332 * 1.2. Load own certificate and private key
333 */
334 printf( " . Loading the server cert. and key..." );
335 fflush( stdout );
336
337#if defined(POLARSSL_FS_IO)
338 if( strlen( opt.crt_file ) )
339 ret = x509parse_crtfile( &srvcert, opt.crt_file );
340 else
341#endif
342#if defined(POLARSSL_CERTS_C)
343 ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt,
344 strlen( test_srv_crt ) );
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_crt returned -0x%x\n\n", -ret );
354 goto exit;
355 }
356
357#if defined(POLARSSL_FS_IO)
358 if( strlen( opt.key_file ) )
359 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
360 else
361#endif
362#if defined(POLARSSL_CERTS_C)
363 ret = x509parse_key( &rsa, (unsigned char *) test_srv_key,
364 strlen( test_srv_key ), NULL, 0 );
365#else
366 {
367 ret = 1;
368 printf("POLARSSL_CERTS_C not defined.");
369 }
370#endif
371 if( ret != 0 )
372 {
373 printf( " failed\n ! x509parse_key returned -0x%x\n\n", -ret );
374 goto exit;
375 }
376
377 printf( " ok\n" );
378
379 /*
380 * 2. Setup the listening TCP socket
381 */
382 printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
383 fflush( stdout );
384
385 if( ( ret = net_bind( &listen_fd, NULL, opt.server_port ) ) != 0 )
386 {
387 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
388 goto exit;
389 }
390
391 printf( " ok\n" );
392
393 /*
394 * 3. Setup stuff
395 */
396 printf( " . Setting up the SSL/TLS structure..." );
397 fflush( stdout );
398
399 if( ( ret = ssl_init( &ssl ) ) != 0 )
400 {
401 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
402 goto exit;
403 }
404
405 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +0100406 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000407
408 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
409 ssl_set_dbg( &ssl, my_debug, stdout );
410
Paul Bakker0a597072012-09-25 21:55:46 +0000411#if defined(POLARSSL_SSL_CACHE_C)
412 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
413 ssl_cache_set, &cache );
414#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000415
Paul Bakker41c83d32013-03-20 14:39:14 +0100416 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000417 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
418
419 ssl_set_renegotiation( &ssl, opt.renegotiation );
420 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
421
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000422 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
423 ssl_set_own_cert( &ssl, &srvcert, &rsa );
424
425#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +0000426 /*
427 * Use different group than default DHM group
428 */
Paul Bakkerd4324102012-09-26 08:29:38 +0000429 ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
430 POLARSSL_DHM_RFC5114_MODP_2048_G );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000431#endif
432
Paul Bakker1d29fb52012-09-28 13:28:45 +0000433 if( opt.min_version != -1 )
434 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
435
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000436 printf( " ok\n" );
437
438reset:
439#ifdef POLARSSL_ERROR_C
440 if( ret != 0 )
441 {
442 char error_buf[100];
443 error_strerror( ret, error_buf, 100 );
444 printf("Last error was: %d - %s\n\n", ret, error_buf );
445 }
446#endif
447
448 if( client_fd != -1 )
449 net_close( client_fd );
450
451 ssl_session_reset( &ssl );
452
453 /*
454 * 3. Wait until a client connects
455 */
456#if defined(_WIN32_WCE)
457 {
458 SHELLEXECUTEINFO sei;
459
460 ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) );
461
462 sei.cbSize = sizeof( SHELLEXECUTEINFO );
463 sei.fMask = 0;
464 sei.hwnd = 0;
465 sei.lpVerb = _T( "open" );
466 sei.lpFile = _T( "https://localhost:4433/" );
467 sei.lpParameters = NULL;
468 sei.lpDirectory = NULL;
469 sei.nShow = SW_SHOWNORMAL;
470
471 ShellExecuteEx( &sei );
472 }
473#elif defined(_WIN32)
474 ShellExecute( NULL, "open", "https://localhost:4433/",
475 NULL, NULL, SW_SHOWNORMAL );
476#endif
477
478 client_fd = -1;
479
480 printf( " . Waiting for a remote connection ..." );
481 fflush( stdout );
482
483 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
484 {
485 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
486 goto exit;
487 }
488
489 ssl_set_bio( &ssl, net_recv, &client_fd,
490 net_send, &client_fd );
491
492 printf( " ok\n" );
493
494 /*
495 * 4. Handshake
496 */
497 printf( " . Performing the SSL/TLS handshake..." );
498 fflush( stdout );
499
500 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
501 {
502 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
503 {
504 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000505 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000506 }
507 }
508
509 printf( " ok\n [ Ciphersuite is %s ]\n",
510 ssl_get_ciphersuite( &ssl ) );
511
512 /*
513 * 5. Verify the server certificate
514 */
515 printf( " . Verifying peer X.509 certificate..." );
516
517 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
518 {
519 printf( " failed\n" );
520
Paul Bakkerb0550d92012-10-30 07:51:03 +0000521 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000522 printf( " ! no client certificate sent\n" );
523
524 if( ( ret & BADCERT_EXPIRED ) != 0 )
525 printf( " ! client certificate has expired\n" );
526
527 if( ( ret & BADCERT_REVOKED ) != 0 )
528 printf( " ! client certificate has been revoked\n" );
529
530 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
531 printf( " ! self-signed or not signed by a trusted CA\n" );
532
533 printf( "\n" );
534 }
535 else
536 printf( " ok\n" );
537
Paul Bakkerb0550d92012-10-30 07:51:03 +0000538 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000539 {
540 printf( " . Peer certificate information ...\n" );
541 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
Paul Bakkerb0550d92012-10-30 07:51:03 +0000542 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000543 printf( "%s\n", buf );
544 }
545
546 /*
547 * 6. Read the HTTP Request
548 */
549 printf( " < Read from client:" );
550 fflush( stdout );
551
552 do
553 {
554 len = sizeof( buf ) - 1;
555 memset( buf, 0, sizeof( buf ) );
556 ret = ssl_read( &ssl, buf, len );
557
558 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
559 continue;
560
561 if( ret <= 0 )
562 {
563 switch( ret )
564 {
565 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
566 printf( " connection was closed gracefully\n" );
567 break;
568
569 case POLARSSL_ERR_NET_CONN_RESET:
570 printf( " connection was reset by peer\n" );
571 break;
572
573 default:
574 printf( " ssl_read returned -0x%x\n", -ret );
575 break;
576 }
577
578 break;
579 }
580
581 len = ret;
582 printf( " %d bytes read\n\n%s", len, (char *) buf );
583
584 if( ret > 0 )
585 break;
586 }
587 while( 1 );
588
589 /*
590 * 7. Write the 200 Response
591 */
592 printf( " > Write to client:" );
593 fflush( stdout );
594
595 len = sprintf( (char *) buf, HTTP_RESPONSE,
596 ssl_get_ciphersuite( &ssl ) );
597
598 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
599 {
600 if( ret == POLARSSL_ERR_NET_CONN_RESET )
601 {
602 printf( " failed\n ! peer closed the connection\n\n" );
603 goto reset;
604 }
605
606 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
607 {
608 printf( " failed\n ! ssl_write returned %d\n\n", ret );
609 goto exit;
610 }
611 }
612
613 len = ret;
614 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
615
616 ret = 0;
617 goto reset;
618
619exit:
620
621#ifdef POLARSSL_ERROR_C
622 if( ret != 0 )
623 {
624 char error_buf[100];
625 error_strerror( ret, error_buf, 100 );
626 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
627 }
628#endif
629
630 net_close( client_fd );
631 x509_free( &srvcert );
632 x509_free( &cacert );
633 rsa_free( &rsa );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000634 ssl_free( &ssl );
635
Paul Bakker0a597072012-09-25 21:55:46 +0000636#if defined(POLARSSL_SSL_CACHE_C)
637 ssl_cache_free( &cache );
638#endif
639
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000640#if defined(_WIN32)
641 printf( " + Press Enter to exit this program.\n" );
642 fflush( stdout ); getchar();
643#endif
644
645 return( ret );
646}
647#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
648 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
649 POLARSSL_CTR_DRBG_C */