blob: a673f52dc2b16ecff8d3b684ae73e6b20bac3eb2 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL server demonstration program
3 *
Paul Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * 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
Paul Bakkercce9d772011-11-18 14:26:47 +000030#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000031#include <windows.h>
32#endif
33
34#include <string.h>
35#include <stdlib.h>
36#include <stdio.h>
37
Paul Bakker5690efc2011-05-26 13:16:06 +000038#include "polarssl/config.h"
39
Paul Bakker40e46942009-01-03 21:51:57 +000040#include "polarssl/havege.h"
41#include "polarssl/certs.h"
42#include "polarssl/x509.h"
43#include "polarssl/ssl.h"
44#include "polarssl/net.h"
Paul Bakkera3d195c2011-11-27 21:07:34 +000045#include "polarssl/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000046
47#define HTTP_RESPONSE \
48 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Paul Bakkereaca51d2010-08-16 12:00:14 +000049 "<h2>PolarSSL Test Server</h2>\r\n" \
50 "<p>Successful connection using: %s</p>\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +000051
52/*
53 * Computing a "safe" DH-1024 prime can take a very
54 * long time, so a precomputed value is provided below.
55 * You may run dh_genprime to generate a new value.
56 */
57char *my_dhm_P =
58 "E4004C1F94182000103D883A448B3F80" \
59 "2CE4B44A83301270002C20D0321CFD00" \
60 "11CCEF784C26A400F43DFB901BCA7538" \
61 "F2C6B176001CF5A0FD16D2C48B1D0C1C" \
62 "F6AC8E1DA6BCC3B4E1F96B0564965300" \
63 "FFA1D0B601EB2800F489AA512C4B248C" \
64 "01F76949A60BB7F00A40B1EAB64BDD48" \
65 "E8A700D60B7F1200FA8E77B0A979DABF";
66
67char *my_dhm_G = "4";
68
69/*
70 * Sorted by order of preference
71 */
Paul Bakkere3166ce2011-01-27 17:40:50 +000072int my_ciphersuites[] =
Paul Bakker5121ce52009-01-03 21:22:43 +000073{
74 SSL_EDH_RSA_AES_256_SHA,
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +000075 SSL_EDH_RSA_CAMELLIA_256_SHA,
Paul Bakker77a43582010-06-15 21:32:46 +000076 SSL_EDH_RSA_AES_128_SHA,
77 SSL_EDH_RSA_CAMELLIA_128_SHA,
Paul Bakker5121ce52009-01-03 21:22:43 +000078 SSL_EDH_RSA_DES_168_SHA,
79 SSL_RSA_AES_256_SHA,
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +000080 SSL_RSA_CAMELLIA_256_SHA,
Paul Bakker5121ce52009-01-03 21:22:43 +000081 SSL_RSA_AES_128_SHA,
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +000082 SSL_RSA_CAMELLIA_128_SHA,
Paul Bakker5121ce52009-01-03 21:22:43 +000083 SSL_RSA_DES_168_SHA,
84 SSL_RSA_RC4_128_SHA,
85 SSL_RSA_RC4_128_MD5,
86 0
87};
88
89#define DEBUG_LEVEL 0
90
Paul Bakkerff60ee62010-03-16 21:09:09 +000091void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +000092{
93 if( level < DEBUG_LEVEL )
94 {
95 fprintf( (FILE *) ctx, "%s", str );
96 fflush( (FILE *) ctx );
97 }
98}
99
100/*
101 * These session callbacks use a simple chained list
102 * to store and retrieve the session information.
103 */
104ssl_session *s_list_1st = NULL;
105ssl_session *cur, *prv;
106
107static int my_get_session( ssl_context *ssl )
108{
109 time_t t = time( NULL );
110
111 if( ssl->resume == 0 )
112 return( 1 );
113
114 cur = s_list_1st;
115 prv = NULL;
116
117 while( cur != NULL )
118 {
119 prv = cur;
120 cur = cur->next;
121
Paul Bakkercce9d772011-11-18 14:26:47 +0000122 if( ssl->timeout != 0 && (int) ( t - prv->start ) > ssl->timeout )
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 continue;
124
Paul Bakkere3166ce2011-01-27 17:40:50 +0000125 if( ssl->session->ciphersuite != prv->ciphersuite ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 ssl->session->length != prv->length )
127 continue;
128
129 if( memcmp( ssl->session->id, prv->id, prv->length ) != 0 )
130 continue;
131
132 memcpy( ssl->session->master, prv->master, 48 );
133 return( 0 );
134 }
135
136 return( 1 );
137}
138
139static int my_set_session( ssl_context *ssl )
140{
141 time_t t = time( NULL );
142
143 cur = s_list_1st;
144 prv = NULL;
145
146 while( cur != NULL )
147 {
Paul Bakkercce9d772011-11-18 14:26:47 +0000148 if( ssl->timeout != 0 && (int) ( t - cur->start ) > ssl->timeout )
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 break; /* expired, reuse this slot */
150
151 if( memcmp( ssl->session->id, cur->id, cur->length ) == 0 )
152 break; /* client reconnected */
153
154 prv = cur;
155 cur = cur->next;
156 }
157
158 if( cur == NULL )
159 {
160 cur = (ssl_session *) malloc( sizeof( ssl_session ) );
161 if( cur == NULL )
162 return( 1 );
163
164 if( prv == NULL )
165 s_list_1st = cur;
166 else prv->next = cur;
167 }
168
169 memcpy( cur, ssl->session, sizeof( ssl_session ) );
170
171 return( 0 );
172}
173
Paul Bakker5690efc2011-05-26 13:16:06 +0000174#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \
175 !defined(POLARSSL_HAVEGE_C) || !defined(POLARSSL_SSL_TLS_C) || \
176 !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \
177 !defined(POLARSSL_RSA_C)
Paul Bakkercce9d772011-11-18 14:26:47 +0000178int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000179{
Paul Bakkercce9d772011-11-18 14:26:47 +0000180 ((void) argc);
181 ((void) argv);
182
Paul Bakker5690efc2011-05-26 13:16:06 +0000183 printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_HAVEGE_C "
184 "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
185 "POLARSSL_NET_C and/or POLARSSL_RSA_C not defined.\n");
186 return( 0 );
187}
188#else
Paul Bakkercce9d772011-11-18 14:26:47 +0000189int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000190{
191 int ret, len;
192 int listen_fd;
Paul Bakker7eb013f2011-10-06 12:37:39 +0000193 int client_fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 unsigned char buf[1024];
195
196 havege_state hs;
197 ssl_context ssl;
198 ssl_session ssn;
199 x509_cert srvcert;
200 rsa_context rsa;
201
Paul Bakkercce9d772011-11-18 14:26:47 +0000202 ((void) argc);
203 ((void) argv);
204
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 /*
206 * 1. Load the certificates and private RSA key
207 */
208 printf( "\n . Loading the server cert. and key..." );
209 fflush( stdout );
210
211 memset( &srvcert, 0, sizeof( x509_cert ) );
212
213 /*
214 * This demonstration program uses embedded test certificates.
215 * Instead, you may want to use x509parse_crtfile() to read the
216 * server and CA certificates, as well as x509parse_keyfile().
217 */
218 ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt,
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000219 strlen( test_srv_crt ), X509_NON_PERMISSIVE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 if( ret != 0 )
221 {
222 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
223 goto exit;
224 }
225
226 ret = x509parse_crt( &srvcert, (unsigned char *) test_ca_crt,
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000227 strlen( test_ca_crt ), X509_NON_PERMISSIVE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 if( ret != 0 )
229 {
230 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
231 goto exit;
232 }
233
Paul Bakker91b41592011-05-05 12:01:31 +0000234 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 ret = x509parse_key( &rsa, (unsigned char *) test_srv_key,
236 strlen( test_srv_key ), NULL, 0 );
237 if( ret != 0 )
238 {
239 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
240 goto exit;
241 }
242
243 printf( " ok\n" );
244
245 /*
246 * 2. Setup the listening TCP socket
247 */
248 printf( " . Bind on https://localhost:4433/ ..." );
249 fflush( stdout );
250
251 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
252 {
253 printf( " failed\n ! net_bind returned %d\n\n", ret );
254 goto exit;
255 }
256
257 printf( " ok\n" );
258
259 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 * 4. Setup stuff
261 */
262 printf( " . Setting up the RNG and SSL data...." );
263 fflush( stdout );
264
265 havege_init( &hs );
266
267 if( ( ret = ssl_init( &ssl ) ) != 0 )
268 {
269 printf( " failed\n ! ssl_init returned %d\n\n", ret );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000270 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 }
272
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
274 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
275
Paul Bakkera3d195c2011-11-27 21:07:34 +0000276 ssl_set_rng( &ssl, havege_random, &hs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 ssl_set_dbg( &ssl, my_debug, stdout );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000278
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 ssl_set_scb( &ssl, my_get_session,
280 my_set_session );
281
Paul Bakkere3166ce2011-01-27 17:40:50 +0000282 ssl_set_ciphersuites( &ssl, my_ciphersuites );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 ssl_set_session( &ssl, 1, 0, &ssn );
284
285 memset( &ssn, 0, sizeof( ssl_session ) );
286
Paul Bakker40ea7de2009-05-03 10:18:48 +0000287 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 ssl_set_own_cert( &ssl, &srvcert, &rsa );
289 ssl_set_dh_param( &ssl, my_dhm_P, my_dhm_G );
290
Paul Bakker7eb013f2011-10-06 12:37:39 +0000291 printf( " ok\n" );
292
293reset:
Paul Bakkera3d195c2011-11-27 21:07:34 +0000294#ifdef POLARSSL_ERROR_C
295 if( ret != 0 )
296 {
297 char error_buf[100];
298 error_strerror( ret, error_buf, 100 );
299 printf("Last error was: %d - %s\n\n", ret, error_buf );
300 }
301#endif
302
Paul Bakker7eb013f2011-10-06 12:37:39 +0000303 if( client_fd != -1 )
304 net_close( client_fd );
305
306 ssl_session_reset( &ssl );
307
308 /*
309 * 3. Wait until a client connects
310 */
Paul Bakkercce9d772011-11-18 14:26:47 +0000311#if defined(_WIN32_WCE)
312 {
313 SHELLEXECUTEINFO sei;
314
315 ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) );
316
317 sei.cbSize = sizeof( SHELLEXECUTEINFO );
318 sei.fMask = 0;
319 sei.hwnd = 0;
320 sei.lpVerb = _T( "open" );
321 sei.lpFile = _T( "https://localhost:4433/" );
322 sei.lpParameters = NULL;
323 sei.lpDirectory = NULL;
324 sei.nShow = SW_SHOWNORMAL;
325
326 ShellExecuteEx( &sei );
327 }
328#elif defined(_WIN32)
Paul Bakker7eb013f2011-10-06 12:37:39 +0000329 ShellExecute( NULL, "open", "https://localhost:4433/",
330 NULL, NULL, SW_SHOWNORMAL );
331#endif
332
333 client_fd = -1;
334
335 printf( " . Waiting for a remote connection ..." );
336 fflush( stdout );
337
338 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
339 {
340 printf( " failed\n ! net_accept returned %d\n\n", ret );
341 goto exit;
342 }
343
344 ssl_set_bio( &ssl, net_recv, &client_fd,
345 net_send, &client_fd );
346
347 printf( " ok\n" );
348
Paul Bakker5121ce52009-01-03 21:22:43 +0000349 /*
350 * 5. Handshake
351 */
352 printf( " . Performing the SSL/TLS handshake..." );
353 fflush( stdout );
354
355 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
356 {
Paul Bakker831a7552011-05-18 13:32:51 +0000357 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000358 {
359 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000360 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 }
362 }
363
364 printf( " ok\n" );
365
366 /*
367 * 6. Read the HTTP Request
368 */
369 printf( " < Read from client:" );
370 fflush( stdout );
371
372 do
373 {
374 len = sizeof( buf ) - 1;
375 memset( buf, 0, sizeof( buf ) );
376 ret = ssl_read( &ssl, buf, len );
377
Paul Bakker831a7552011-05-18 13:32:51 +0000378 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 continue;
380
381 if( ret <= 0 )
382 {
383 switch( ret )
384 {
Paul Bakker40e46942009-01-03 21:51:57 +0000385 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 printf( " connection was closed gracefully\n" );
387 break;
388
Paul Bakker40e46942009-01-03 21:51:57 +0000389 case POLARSSL_ERR_NET_CONN_RESET:
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 printf( " connection was reset by peer\n" );
391 break;
392
393 default:
394 printf( " ssl_read returned %d\n", ret );
395 break;
396 }
397
398 break;
399 }
400
401 len = ret;
402 printf( " %d bytes read\n\n%s", len, (char *) buf );
403 }
404 while( 0 );
405
406 /*
407 * 7. Write the 200 Response
408 */
409 printf( " > Write to client:" );
410 fflush( stdout );
411
412 len = sprintf( (char *) buf, HTTP_RESPONSE,
Paul Bakkere3166ce2011-01-27 17:40:50 +0000413 ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
415 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
416 {
Paul Bakker40e46942009-01-03 21:51:57 +0000417 if( ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 {
419 printf( " failed\n ! peer closed the connection\n\n" );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000420 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 }
422
Paul Bakker831a7552011-05-18 13:32:51 +0000423 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 {
425 printf( " failed\n ! ssl_write returned %d\n\n", ret );
426 goto exit;
427 }
428 }
429
430 len = ret;
431 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000432
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 ssl_close_notify( &ssl );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000434 ret = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +0000435 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000436
437exit:
438
Paul Bakkera3d195c2011-11-27 21:07:34 +0000439#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
Paul Bakker5121ce52009-01-03 21:22:43 +0000448 net_close( client_fd );
449 x509_free( &srvcert );
450 rsa_free( &rsa );
451 ssl_free( &ssl );
452
453 cur = s_list_1st;
454 while( cur != NULL )
455 {
456 prv = cur;
457 cur = cur->next;
458 memset( prv, 0, sizeof( ssl_session ) );
459 free( prv );
460 }
461
462 memset( &ssl, 0, sizeof( ssl_context ) );
463
Paul Bakkercce9d772011-11-18 14:26:47 +0000464#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000465 printf( " Press Enter to exit this program.\n" );
466 fflush( stdout ); getchar();
467#endif
468
469 return( ret );
470}
Paul Bakker5690efc2011-05-26 13:16:06 +0000471#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_HAVEGE_C &&
472 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
473 POLARSSL_RSA_C */