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