blob: 0bfdd9db18b2ac4b0b310de4f90a4a96c3442fa3 [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.
5 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00006 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#ifndef _CRT_SECURE_NO_DEPRECATE
24#define _CRT_SECURE_NO_DEPRECATE 1
25#endif
26
27#ifdef WIN32
28#include <windows.h>
29#endif
30
31#include <string.h>
32#include <stdlib.h>
33#include <stdio.h>
34
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/havege.h"
36#include "polarssl/certs.h"
37#include "polarssl/x509.h"
38#include "polarssl/ssl.h"
39#include "polarssl/net.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000040
41#define HTTP_RESPONSE \
42 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
43 "<h2><p><center>Successful connection using: %s\r\n"
44
45/*
46 * Computing a "safe" DH-1024 prime can take a very
47 * long time, so a precomputed value is provided below.
48 * You may run dh_genprime to generate a new value.
49 */
50char *my_dhm_P =
51 "E4004C1F94182000103D883A448B3F80" \
52 "2CE4B44A83301270002C20D0321CFD00" \
53 "11CCEF784C26A400F43DFB901BCA7538" \
54 "F2C6B176001CF5A0FD16D2C48B1D0C1C" \
55 "F6AC8E1DA6BCC3B4E1F96B0564965300" \
56 "FFA1D0B601EB2800F489AA512C4B248C" \
57 "01F76949A60BB7F00A40B1EAB64BDD48" \
58 "E8A700D60B7F1200FA8E77B0A979DABF";
59
60char *my_dhm_G = "4";
61
62/*
63 * Sorted by order of preference
64 */
65int my_ciphers[] =
66{
67 SSL_EDH_RSA_AES_256_SHA,
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +000068 SSL_EDH_RSA_CAMELLIA_256_SHA,
Paul Bakker77a43582010-06-15 21:32:46 +000069 SSL_EDH_RSA_AES_128_SHA,
70 SSL_EDH_RSA_CAMELLIA_128_SHA,
Paul Bakker5121ce52009-01-03 21:22:43 +000071 SSL_EDH_RSA_DES_168_SHA,
72 SSL_RSA_AES_256_SHA,
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +000073 SSL_RSA_CAMELLIA_256_SHA,
Paul Bakker5121ce52009-01-03 21:22:43 +000074 SSL_RSA_AES_128_SHA,
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +000075 SSL_RSA_CAMELLIA_128_SHA,
Paul Bakker5121ce52009-01-03 21:22:43 +000076 SSL_RSA_DES_168_SHA,
77 SSL_RSA_RC4_128_SHA,
78 SSL_RSA_RC4_128_MD5,
79 0
80};
81
82#define DEBUG_LEVEL 0
83
Paul Bakkerff60ee62010-03-16 21:09:09 +000084void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +000085{
86 if( level < DEBUG_LEVEL )
87 {
88 fprintf( (FILE *) ctx, "%s", str );
89 fflush( (FILE *) ctx );
90 }
91}
92
93/*
94 * These session callbacks use a simple chained list
95 * to store and retrieve the session information.
96 */
97ssl_session *s_list_1st = NULL;
98ssl_session *cur, *prv;
99
100static int my_get_session( ssl_context *ssl )
101{
102 time_t t = time( NULL );
103
104 if( ssl->resume == 0 )
105 return( 1 );
106
107 cur = s_list_1st;
108 prv = NULL;
109
110 while( cur != NULL )
111 {
112 prv = cur;
113 cur = cur->next;
114
115 if( ssl->timeout != 0 && t - prv->start > ssl->timeout )
116 continue;
117
118 if( ssl->session->cipher != prv->cipher ||
119 ssl->session->length != prv->length )
120 continue;
121
122 if( memcmp( ssl->session->id, prv->id, prv->length ) != 0 )
123 continue;
124
125 memcpy( ssl->session->master, prv->master, 48 );
126 return( 0 );
127 }
128
129 return( 1 );
130}
131
132static int my_set_session( ssl_context *ssl )
133{
134 time_t t = time( NULL );
135
136 cur = s_list_1st;
137 prv = NULL;
138
139 while( cur != NULL )
140 {
141 if( ssl->timeout != 0 && t - cur->start > ssl->timeout )
142 break; /* expired, reuse this slot */
143
144 if( memcmp( ssl->session->id, cur->id, cur->length ) == 0 )
145 break; /* client reconnected */
146
147 prv = cur;
148 cur = cur->next;
149 }
150
151 if( cur == NULL )
152 {
153 cur = (ssl_session *) malloc( sizeof( ssl_session ) );
154 if( cur == NULL )
155 return( 1 );
156
157 if( prv == NULL )
158 s_list_1st = cur;
159 else prv->next = cur;
160 }
161
162 memcpy( cur, ssl->session, sizeof( ssl_session ) );
163
164 return( 0 );
165}
166
167int main( void )
168{
169 int ret, len;
170 int listen_fd;
171 int client_fd;
172 unsigned char buf[1024];
173
174 havege_state hs;
175 ssl_context ssl;
176 ssl_session ssn;
177 x509_cert srvcert;
178 rsa_context rsa;
179
180 /*
181 * 1. Load the certificates and private RSA key
182 */
183 printf( "\n . Loading the server cert. and key..." );
184 fflush( stdout );
185
186 memset( &srvcert, 0, sizeof( x509_cert ) );
187
188 /*
189 * This demonstration program uses embedded test certificates.
190 * Instead, you may want to use x509parse_crtfile() to read the
191 * server and CA certificates, as well as x509parse_keyfile().
192 */
193 ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt,
194 strlen( test_srv_crt ) );
195 if( ret != 0 )
196 {
197 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
198 goto exit;
199 }
200
201 ret = x509parse_crt( &srvcert, (unsigned char *) test_ca_crt,
202 strlen( test_ca_crt ) );
203 if( ret != 0 )
204 {
205 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
206 goto exit;
207 }
208
209 ret = x509parse_key( &rsa, (unsigned char *) test_srv_key,
210 strlen( test_srv_key ), NULL, 0 );
211 if( ret != 0 )
212 {
213 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
214 goto exit;
215 }
216
217 printf( " ok\n" );
218
219 /*
220 * 2. Setup the listening TCP socket
221 */
222 printf( " . Bind on https://localhost:4433/ ..." );
223 fflush( stdout );
224
225 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
226 {
227 printf( " failed\n ! net_bind returned %d\n\n", ret );
228 goto exit;
229 }
230
231 printf( " ok\n" );
232
233 /*
234 * 3. Wait until a client connects
235 */
236#ifdef WIN32
237 ShellExecute( NULL, "open", "https://localhost:4433/",
238 NULL, NULL, SW_SHOWNORMAL );
239#endif
240
241 client_fd = -1;
242 memset( &ssl, 0, sizeof( ssl ) );
243
244accept:
245
246 net_close( client_fd );
247 ssl_free( &ssl );
248
249 printf( " . Waiting for a remote connection ..." );
250 fflush( stdout );
251
252 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
253 {
254 printf( " failed\n ! net_accept returned %d\n\n", ret );
255 goto exit;
256 }
257
258 printf( " ok\n" );
259
260 /*
261 * 4. Setup stuff
262 */
263 printf( " . Setting up the RNG and SSL data...." );
264 fflush( stdout );
265
266 havege_init( &hs );
267
268 if( ( ret = ssl_init( &ssl ) ) != 0 )
269 {
270 printf( " failed\n ! ssl_init returned %d\n\n", ret );
271 goto accept;
272 }
273
274 printf( " ok\n" );
275
276 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
277 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
278
279 ssl_set_rng( &ssl, havege_rand, &hs );
280 ssl_set_dbg( &ssl, my_debug, stdout );
281 ssl_set_bio( &ssl, net_recv, &client_fd,
282 net_send, &client_fd );
283 ssl_set_scb( &ssl, my_get_session,
284 my_set_session );
285
286 ssl_set_ciphers( &ssl, my_ciphers );
287 ssl_set_session( &ssl, 1, 0, &ssn );
288
289 memset( &ssn, 0, sizeof( ssl_session ) );
290
Paul Bakker40ea7de2009-05-03 10:18:48 +0000291 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 ssl_set_own_cert( &ssl, &srvcert, &rsa );
293 ssl_set_dh_param( &ssl, my_dhm_P, my_dhm_G );
294
295 /*
296 * 5. Handshake
297 */
298 printf( " . Performing the SSL/TLS handshake..." );
299 fflush( stdout );
300
301 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
302 {
Paul Bakker40e46942009-01-03 21:51:57 +0000303 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 {
305 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
306 goto accept;
307 }
308 }
309
310 printf( " ok\n" );
311
312 /*
313 * 6. Read the HTTP Request
314 */
315 printf( " < Read from client:" );
316 fflush( stdout );
317
318 do
319 {
320 len = sizeof( buf ) - 1;
321 memset( buf, 0, sizeof( buf ) );
322 ret = ssl_read( &ssl, buf, len );
323
Paul Bakker40e46942009-01-03 21:51:57 +0000324 if( ret == POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 continue;
326
327 if( ret <= 0 )
328 {
329 switch( ret )
330 {
Paul Bakker40e46942009-01-03 21:51:57 +0000331 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 printf( " connection was closed gracefully\n" );
333 break;
334
Paul Bakker40e46942009-01-03 21:51:57 +0000335 case POLARSSL_ERR_NET_CONN_RESET:
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 printf( " connection was reset by peer\n" );
337 break;
338
339 default:
340 printf( " ssl_read returned %d\n", ret );
341 break;
342 }
343
344 break;
345 }
346
347 len = ret;
348 printf( " %d bytes read\n\n%s", len, (char *) buf );
349 }
350 while( 0 );
351
352 /*
353 * 7. Write the 200 Response
354 */
355 printf( " > Write to client:" );
356 fflush( stdout );
357
358 len = sprintf( (char *) buf, HTTP_RESPONSE,
359 ssl_get_cipher( &ssl ) );
360
361 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
362 {
Paul Bakker40e46942009-01-03 21:51:57 +0000363 if( ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 {
365 printf( " failed\n ! peer closed the connection\n\n" );
366 goto accept;
367 }
368
Paul Bakker40e46942009-01-03 21:51:57 +0000369 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 {
371 printf( " failed\n ! ssl_write returned %d\n\n", ret );
372 goto exit;
373 }
374 }
375
376 len = ret;
377 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
378
379 ssl_close_notify( &ssl );
380 goto accept;
381
382exit:
383
384 net_close( client_fd );
385 x509_free( &srvcert );
386 rsa_free( &rsa );
387 ssl_free( &ssl );
388
389 cur = s_list_1st;
390 while( cur != NULL )
391 {
392 prv = cur;
393 cur = cur->next;
394 memset( prv, 0, sizeof( ssl_session ) );
395 free( prv );
396 }
397
398 memset( &ssl, 0, sizeof( ssl_context ) );
399
400#ifdef WIN32
401 printf( " Press Enter to exit this program.\n" );
402 fflush( stdout ); getchar();
403#endif
404
405 return( ret );
406}