blob: 096482ec237a16579c1cd286ece9485b1c42d4ed [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 Bakker40e46942009-01-03 21:51:57 +000038#include "polarssl/havege.h"
39#include "polarssl/certs.h"
40#include "polarssl/x509.h"
41#include "polarssl/ssl.h"
42#include "polarssl/net.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000043
44#define HTTP_RESPONSE \
45 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Paul Bakkereaca51d2010-08-16 12:00:14 +000046 "<h2>PolarSSL Test Server</h2>\r\n" \
47 "<p>Successful connection using: %s</p>\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +000048
49/*
50 * Computing a "safe" DH-1024 prime can take a very
51 * long time, so a precomputed value is provided below.
52 * You may run dh_genprime to generate a new value.
53 */
54char *my_dhm_P =
55 "E4004C1F94182000103D883A448B3F80" \
56 "2CE4B44A83301270002C20D0321CFD00" \
57 "11CCEF784C26A400F43DFB901BCA7538" \
58 "F2C6B176001CF5A0FD16D2C48B1D0C1C" \
59 "F6AC8E1DA6BCC3B4E1F96B0564965300" \
60 "FFA1D0B601EB2800F489AA512C4B248C" \
61 "01F76949A60BB7F00A40B1EAB64BDD48" \
62 "E8A700D60B7F1200FA8E77B0A979DABF";
63
64char *my_dhm_G = "4";
65
66/*
67 * Sorted by order of preference
68 */
Paul Bakkere3166ce2011-01-27 17:40:50 +000069int my_ciphersuites[] =
Paul Bakker5121ce52009-01-03 21:22:43 +000070{
71 SSL_EDH_RSA_AES_256_SHA,
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +000072 SSL_EDH_RSA_CAMELLIA_256_SHA,
Paul Bakker77a43582010-06-15 21:32:46 +000073 SSL_EDH_RSA_AES_128_SHA,
74 SSL_EDH_RSA_CAMELLIA_128_SHA,
Paul Bakker5121ce52009-01-03 21:22:43 +000075 SSL_EDH_RSA_DES_168_SHA,
76 SSL_RSA_AES_256_SHA,
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +000077 SSL_RSA_CAMELLIA_256_SHA,
Paul Bakker5121ce52009-01-03 21:22:43 +000078 SSL_RSA_AES_128_SHA,
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +000079 SSL_RSA_CAMELLIA_128_SHA,
Paul Bakker5121ce52009-01-03 21:22:43 +000080 SSL_RSA_DES_168_SHA,
81 SSL_RSA_RC4_128_SHA,
82 SSL_RSA_RC4_128_MD5,
83 0
84};
85
86#define DEBUG_LEVEL 0
87
Paul Bakkerff60ee62010-03-16 21:09:09 +000088void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +000089{
90 if( level < DEBUG_LEVEL )
91 {
92 fprintf( (FILE *) ctx, "%s", str );
93 fflush( (FILE *) ctx );
94 }
95}
96
97/*
98 * These session callbacks use a simple chained list
99 * to store and retrieve the session information.
100 */
101ssl_session *s_list_1st = NULL;
102ssl_session *cur, *prv;
103
104static int my_get_session( ssl_context *ssl )
105{
106 time_t t = time( NULL );
107
108 if( ssl->resume == 0 )
109 return( 1 );
110
111 cur = s_list_1st;
112 prv = NULL;
113
114 while( cur != NULL )
115 {
116 prv = cur;
117 cur = cur->next;
118
119 if( ssl->timeout != 0 && t - prv->start > ssl->timeout )
120 continue;
121
Paul Bakkere3166ce2011-01-27 17:40:50 +0000122 if( ssl->session->ciphersuite != prv->ciphersuite ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 ssl->session->length != prv->length )
124 continue;
125
126 if( memcmp( ssl->session->id, prv->id, prv->length ) != 0 )
127 continue;
128
129 memcpy( ssl->session->master, prv->master, 48 );
130 return( 0 );
131 }
132
133 return( 1 );
134}
135
136static int my_set_session( ssl_context *ssl )
137{
138 time_t t = time( NULL );
139
140 cur = s_list_1st;
141 prv = NULL;
142
143 while( cur != NULL )
144 {
145 if( ssl->timeout != 0 && t - cur->start > ssl->timeout )
146 break; /* expired, reuse this slot */
147
148 if( memcmp( ssl->session->id, cur->id, cur->length ) == 0 )
149 break; /* client reconnected */
150
151 prv = cur;
152 cur = cur->next;
153 }
154
155 if( cur == NULL )
156 {
157 cur = (ssl_session *) malloc( sizeof( ssl_session ) );
158 if( cur == NULL )
159 return( 1 );
160
161 if( prv == NULL )
162 s_list_1st = cur;
163 else prv->next = cur;
164 }
165
166 memcpy( cur, ssl->session, sizeof( ssl_session ) );
167
168 return( 0 );
169}
170
171int main( void )
172{
173 int ret, len;
174 int listen_fd;
175 int client_fd;
176 unsigned char buf[1024];
177
178 havege_state hs;
179 ssl_context ssl;
180 ssl_session ssn;
181 x509_cert srvcert;
182 rsa_context rsa;
183
184 /*
185 * 1. Load the certificates and private RSA key
186 */
187 printf( "\n . Loading the server cert. and key..." );
188 fflush( stdout );
189
190 memset( &srvcert, 0, sizeof( x509_cert ) );
191
192 /*
193 * This demonstration program uses embedded test certificates.
194 * Instead, you may want to use x509parse_crtfile() to read the
195 * server and CA certificates, as well as x509parse_keyfile().
196 */
197 ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt,
198 strlen( test_srv_crt ) );
199 if( ret != 0 )
200 {
201 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
202 goto exit;
203 }
204
205 ret = x509parse_crt( &srvcert, (unsigned char *) test_ca_crt,
206 strlen( test_ca_crt ) );
207 if( ret != 0 )
208 {
209 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
210 goto exit;
211 }
212
Paul Bakker91b41592011-05-05 12:01:31 +0000213 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 ret = x509parse_key( &rsa, (unsigned char *) test_srv_key,
215 strlen( test_srv_key ), NULL, 0 );
216 if( ret != 0 )
217 {
218 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
219 goto exit;
220 }
221
222 printf( " ok\n" );
223
224 /*
225 * 2. Setup the listening TCP socket
226 */
227 printf( " . Bind on https://localhost:4433/ ..." );
228 fflush( stdout );
229
230 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
231 {
232 printf( " failed\n ! net_bind returned %d\n\n", ret );
233 goto exit;
234 }
235
236 printf( " ok\n" );
237
238 /*
239 * 3. Wait until a client connects
240 */
241#ifdef WIN32
242 ShellExecute( NULL, "open", "https://localhost:4433/",
243 NULL, NULL, SW_SHOWNORMAL );
244#endif
245
246 client_fd = -1;
247 memset( &ssl, 0, sizeof( ssl ) );
248
249accept:
250
251 net_close( client_fd );
252 ssl_free( &ssl );
253
254 printf( " . Waiting for a remote connection ..." );
255 fflush( stdout );
256
257 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
258 {
259 printf( " failed\n ! net_accept returned %d\n\n", ret );
260 goto exit;
261 }
262
263 printf( " ok\n" );
264
265 /*
266 * 4. Setup stuff
267 */
268 printf( " . Setting up the RNG and SSL data...." );
269 fflush( stdout );
270
271 havege_init( &hs );
272
273 if( ( ret = ssl_init( &ssl ) ) != 0 )
274 {
275 printf( " failed\n ! ssl_init returned %d\n\n", ret );
276 goto accept;
277 }
278
279 printf( " ok\n" );
280
281 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
282 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
283
284 ssl_set_rng( &ssl, havege_rand, &hs );
285 ssl_set_dbg( &ssl, my_debug, stdout );
286 ssl_set_bio( &ssl, net_recv, &client_fd,
287 net_send, &client_fd );
288 ssl_set_scb( &ssl, my_get_session,
289 my_set_session );
290
Paul Bakkere3166ce2011-01-27 17:40:50 +0000291 ssl_set_ciphersuites( &ssl, my_ciphersuites );
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 ssl_set_session( &ssl, 1, 0, &ssn );
293
294 memset( &ssn, 0, sizeof( ssl_session ) );
295
Paul Bakker40ea7de2009-05-03 10:18:48 +0000296 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 ssl_set_own_cert( &ssl, &srvcert, &rsa );
298 ssl_set_dh_param( &ssl, my_dhm_P, my_dhm_G );
299
300 /*
301 * 5. Handshake
302 */
303 printf( " . Performing the SSL/TLS handshake..." );
304 fflush( stdout );
305
306 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
307 {
Paul Bakker40e46942009-01-03 21:51:57 +0000308 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 {
310 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
311 goto accept;
312 }
313 }
314
315 printf( " ok\n" );
316
317 /*
318 * 6. Read the HTTP Request
319 */
320 printf( " < Read from client:" );
321 fflush( stdout );
322
323 do
324 {
325 len = sizeof( buf ) - 1;
326 memset( buf, 0, sizeof( buf ) );
327 ret = ssl_read( &ssl, buf, len );
328
Paul Bakker40e46942009-01-03 21:51:57 +0000329 if( ret == POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 continue;
331
332 if( ret <= 0 )
333 {
334 switch( ret )
335 {
Paul Bakker40e46942009-01-03 21:51:57 +0000336 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 printf( " connection was closed gracefully\n" );
338 break;
339
Paul Bakker40e46942009-01-03 21:51:57 +0000340 case POLARSSL_ERR_NET_CONN_RESET:
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 printf( " connection was reset by peer\n" );
342 break;
343
344 default:
345 printf( " ssl_read returned %d\n", ret );
346 break;
347 }
348
349 break;
350 }
351
352 len = ret;
353 printf( " %d bytes read\n\n%s", len, (char *) buf );
354 }
355 while( 0 );
356
357 /*
358 * 7. Write the 200 Response
359 */
360 printf( " > Write to client:" );
361 fflush( stdout );
362
363 len = sprintf( (char *) buf, HTTP_RESPONSE,
Paul Bakkere3166ce2011-01-27 17:40:50 +0000364 ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000365
366 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
367 {
Paul Bakker40e46942009-01-03 21:51:57 +0000368 if( ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000369 {
370 printf( " failed\n ! peer closed the connection\n\n" );
371 goto accept;
372 }
373
Paul Bakker40e46942009-01-03 21:51:57 +0000374 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000375 {
376 printf( " failed\n ! ssl_write returned %d\n\n", ret );
377 goto exit;
378 }
379 }
380
381 len = ret;
382 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
383
384 ssl_close_notify( &ssl );
385 goto accept;
386
387exit:
388
389 net_close( client_fd );
390 x509_free( &srvcert );
391 rsa_free( &rsa );
392 ssl_free( &ssl );
393
394 cur = s_list_1st;
395 while( cur != NULL )
396 {
397 prv = cur;
398 cur = cur->next;
399 memset( prv, 0, sizeof( ssl_session ) );
400 free( prv );
401 }
402
403 memset( &ssl, 0, sizeof( ssl_context ) );
404
405#ifdef WIN32
406 printf( " Press Enter to exit this program.\n" );
407 fflush( stdout ); getchar();
408#endif
409
410 return( ret );
411}