blob: be92b0dd2c7f3cbd4df84f0811ac7e48e5bfdbb9 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL server demonstration program
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, 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
30#ifdef WIN32
31#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 Bakker5121ce52009-01-03 21:22:43 +000045
46#define HTTP_RESPONSE \
47 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Paul Bakkereaca51d2010-08-16 12:00:14 +000048 "<h2>PolarSSL Test Server</h2>\r\n" \
49 "<p>Successful connection using: %s</p>\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +000050
51/*
52 * Computing a "safe" DH-1024 prime can take a very
53 * long time, so a precomputed value is provided below.
54 * You may run dh_genprime to generate a new value.
55 */
56char *my_dhm_P =
57 "E4004C1F94182000103D883A448B3F80" \
58 "2CE4B44A83301270002C20D0321CFD00" \
59 "11CCEF784C26A400F43DFB901BCA7538" \
60 "F2C6B176001CF5A0FD16D2C48B1D0C1C" \
61 "F6AC8E1DA6BCC3B4E1F96B0564965300" \
62 "FFA1D0B601EB2800F489AA512C4B248C" \
63 "01F76949A60BB7F00A40B1EAB64BDD48" \
64 "E8A700D60B7F1200FA8E77B0A979DABF";
65
66char *my_dhm_G = "4";
67
68/*
69 * Sorted by order of preference
70 */
Paul Bakkere3166ce2011-01-27 17:40:50 +000071int my_ciphersuites[] =
Paul Bakker5121ce52009-01-03 21:22:43 +000072{
73 SSL_EDH_RSA_AES_256_SHA,
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +000074 SSL_EDH_RSA_CAMELLIA_256_SHA,
Paul Bakker77a43582010-06-15 21:32:46 +000075 SSL_EDH_RSA_AES_128_SHA,
76 SSL_EDH_RSA_CAMELLIA_128_SHA,
Paul Bakker5121ce52009-01-03 21:22:43 +000077 SSL_EDH_RSA_DES_168_SHA,
78 SSL_RSA_AES_256_SHA,
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +000079 SSL_RSA_CAMELLIA_256_SHA,
Paul Bakker5121ce52009-01-03 21:22:43 +000080 SSL_RSA_AES_128_SHA,
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +000081 SSL_RSA_CAMELLIA_128_SHA,
Paul Bakker5121ce52009-01-03 21:22:43 +000082 SSL_RSA_DES_168_SHA,
83 SSL_RSA_RC4_128_SHA,
84 SSL_RSA_RC4_128_MD5,
85 0
86};
87
88#define DEBUG_LEVEL 0
89
Paul Bakkerff60ee62010-03-16 21:09:09 +000090void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +000091{
92 if( level < DEBUG_LEVEL )
93 {
94 fprintf( (FILE *) ctx, "%s", str );
95 fflush( (FILE *) ctx );
96 }
97}
98
99/*
100 * These session callbacks use a simple chained list
101 * to store and retrieve the session information.
102 */
103ssl_session *s_list_1st = NULL;
104ssl_session *cur, *prv;
105
106static int my_get_session( ssl_context *ssl )
107{
108 time_t t = time( NULL );
109
110 if( ssl->resume == 0 )
111 return( 1 );
112
113 cur = s_list_1st;
114 prv = NULL;
115
116 while( cur != NULL )
117 {
118 prv = cur;
119 cur = cur->next;
120
121 if( ssl->timeout != 0 && t - prv->start > ssl->timeout )
122 continue;
123
Paul Bakkere3166ce2011-01-27 17:40:50 +0000124 if( ssl->session->ciphersuite != prv->ciphersuite ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 ssl->session->length != prv->length )
126 continue;
127
128 if( memcmp( ssl->session->id, prv->id, prv->length ) != 0 )
129 continue;
130
131 memcpy( ssl->session->master, prv->master, 48 );
132 return( 0 );
133 }
134
135 return( 1 );
136}
137
138static int my_set_session( ssl_context *ssl )
139{
140 time_t t = time( NULL );
141
142 cur = s_list_1st;
143 prv = NULL;
144
145 while( cur != NULL )
146 {
147 if( ssl->timeout != 0 && t - cur->start > ssl->timeout )
148 break; /* expired, reuse this slot */
149
150 if( memcmp( ssl->session->id, cur->id, cur->length ) == 0 )
151 break; /* client reconnected */
152
153 prv = cur;
154 cur = cur->next;
155 }
156
157 if( cur == NULL )
158 {
159 cur = (ssl_session *) malloc( sizeof( ssl_session ) );
160 if( cur == NULL )
161 return( 1 );
162
163 if( prv == NULL )
164 s_list_1st = cur;
165 else prv->next = cur;
166 }
167
168 memcpy( cur, ssl->session, sizeof( ssl_session ) );
169
170 return( 0 );
171}
172
Paul Bakker5690efc2011-05-26 13:16:06 +0000173#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \
174 !defined(POLARSSL_HAVEGE_C) || !defined(POLARSSL_SSL_TLS_C) || \
175 !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \
176 !defined(POLARSSL_RSA_C)
177int main( void )
178{
179 printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_HAVEGE_C "
180 "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
181 "POLARSSL_NET_C and/or POLARSSL_RSA_C not defined.\n");
182 return( 0 );
183}
184#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000185int main( void )
186{
187 int ret, len;
188 int listen_fd;
189 int client_fd;
190 unsigned char buf[1024];
191
192 havege_state hs;
193 ssl_context ssl;
194 ssl_session ssn;
195 x509_cert srvcert;
196 rsa_context rsa;
197
198 /*
199 * 1. Load the certificates and private RSA key
200 */
201 printf( "\n . Loading the server cert. and key..." );
202 fflush( stdout );
203
204 memset( &srvcert, 0, sizeof( x509_cert ) );
205
206 /*
207 * This demonstration program uses embedded test certificates.
208 * Instead, you may want to use x509parse_crtfile() to read the
209 * server and CA certificates, as well as x509parse_keyfile().
210 */
211 ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt,
212 strlen( test_srv_crt ) );
213 if( ret != 0 )
214 {
215 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
216 goto exit;
217 }
218
219 ret = x509parse_crt( &srvcert, (unsigned char *) test_ca_crt,
220 strlen( test_ca_crt ) );
221 if( ret != 0 )
222 {
223 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
224 goto exit;
225 }
226
Paul Bakker91b41592011-05-05 12:01:31 +0000227 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 ret = x509parse_key( &rsa, (unsigned char *) test_srv_key,
229 strlen( test_srv_key ), NULL, 0 );
230 if( ret != 0 )
231 {
232 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
233 goto exit;
234 }
235
236 printf( " ok\n" );
237
238 /*
239 * 2. Setup the listening TCP socket
240 */
241 printf( " . Bind on https://localhost:4433/ ..." );
242 fflush( stdout );
243
244 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
245 {
246 printf( " failed\n ! net_bind returned %d\n\n", ret );
247 goto exit;
248 }
249
250 printf( " ok\n" );
251
252 /*
253 * 3. Wait until a client connects
254 */
255#ifdef WIN32
256 ShellExecute( NULL, "open", "https://localhost:4433/",
257 NULL, NULL, SW_SHOWNORMAL );
258#endif
259
260 client_fd = -1;
261 memset( &ssl, 0, sizeof( ssl ) );
262
263accept:
264
265 net_close( client_fd );
266 ssl_free( &ssl );
267
268 printf( " . Waiting for a remote connection ..." );
269 fflush( stdout );
270
271 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
272 {
273 printf( " failed\n ! net_accept returned %d\n\n", ret );
274 goto exit;
275 }
276
277 printf( " ok\n" );
278
279 /*
280 * 4. Setup stuff
281 */
282 printf( " . Setting up the RNG and SSL data...." );
283 fflush( stdout );
284
285 havege_init( &hs );
286
287 if( ( ret = ssl_init( &ssl ) ) != 0 )
288 {
289 printf( " failed\n ! ssl_init returned %d\n\n", ret );
290 goto accept;
291 }
292
293 printf( " ok\n" );
294
295 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
296 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
297
298 ssl_set_rng( &ssl, havege_rand, &hs );
299 ssl_set_dbg( &ssl, my_debug, stdout );
300 ssl_set_bio( &ssl, net_recv, &client_fd,
301 net_send, &client_fd );
302 ssl_set_scb( &ssl, my_get_session,
303 my_set_session );
304
Paul Bakkere3166ce2011-01-27 17:40:50 +0000305 ssl_set_ciphersuites( &ssl, my_ciphersuites );
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 ssl_set_session( &ssl, 1, 0, &ssn );
307
308 memset( &ssn, 0, sizeof( ssl_session ) );
309
Paul Bakker40ea7de2009-05-03 10:18:48 +0000310 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 ssl_set_own_cert( &ssl, &srvcert, &rsa );
312 ssl_set_dh_param( &ssl, my_dhm_P, my_dhm_G );
313
314 /*
315 * 5. Handshake
316 */
317 printf( " . Performing the SSL/TLS handshake..." );
318 fflush( stdout );
319
320 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
321 {
Paul Bakker831a7552011-05-18 13:32:51 +0000322 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 {
324 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
325 goto accept;
326 }
327 }
328
329 printf( " ok\n" );
330
331 /*
332 * 6. Read the HTTP Request
333 */
334 printf( " < Read from client:" );
335 fflush( stdout );
336
337 do
338 {
339 len = sizeof( buf ) - 1;
340 memset( buf, 0, sizeof( buf ) );
341 ret = ssl_read( &ssl, buf, len );
342
Paul Bakker831a7552011-05-18 13:32:51 +0000343 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 continue;
345
346 if( ret <= 0 )
347 {
348 switch( ret )
349 {
Paul Bakker40e46942009-01-03 21:51:57 +0000350 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 printf( " connection was closed gracefully\n" );
352 break;
353
Paul Bakker40e46942009-01-03 21:51:57 +0000354 case POLARSSL_ERR_NET_CONN_RESET:
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 printf( " connection was reset by peer\n" );
356 break;
357
358 default:
359 printf( " ssl_read returned %d\n", ret );
360 break;
361 }
362
363 break;
364 }
365
366 len = ret;
367 printf( " %d bytes read\n\n%s", len, (char *) buf );
368 }
369 while( 0 );
370
371 /*
372 * 7. Write the 200 Response
373 */
374 printf( " > Write to client:" );
375 fflush( stdout );
376
377 len = sprintf( (char *) buf, HTTP_RESPONSE,
Paul Bakkere3166ce2011-01-27 17:40:50 +0000378 ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379
380 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
381 {
Paul Bakker40e46942009-01-03 21:51:57 +0000382 if( ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 {
384 printf( " failed\n ! peer closed the connection\n\n" );
385 goto accept;
386 }
387
Paul Bakker831a7552011-05-18 13:32:51 +0000388 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000389 {
390 printf( " failed\n ! ssl_write returned %d\n\n", ret );
391 goto exit;
392 }
393 }
394
395 len = ret;
396 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
397
398 ssl_close_notify( &ssl );
399 goto accept;
400
401exit:
402
403 net_close( client_fd );
404 x509_free( &srvcert );
405 rsa_free( &rsa );
406 ssl_free( &ssl );
407
408 cur = s_list_1st;
409 while( cur != NULL )
410 {
411 prv = cur;
412 cur = cur->next;
413 memset( prv, 0, sizeof( ssl_session ) );
414 free( prv );
415 }
416
417 memset( &ssl, 0, sizeof( ssl_context ) );
418
419#ifdef WIN32
420 printf( " Press Enter to exit this program.\n" );
421 fflush( stdout ); getchar();
422#endif
423
424 return( ret );
425}
Paul Bakker5690efc2011-05-26 13:16:06 +0000426#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_HAVEGE_C &&
427 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
428 POLARSSL_RSA_C */