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