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