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