blob: 30d8502623d6cbf2b1481ed06484712988a4857c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL server demonstration program
3 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00004 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
5 *
6 * Copyright (C) 2009 Paul Bakker
Paul Bakker5121ce52009-01-03 21:22:43 +00007 *
8 * 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,
68 SSL_EDH_RSA_DES_168_SHA,
69 SSL_RSA_AES_256_SHA,
70 SSL_RSA_AES_128_SHA,
71 SSL_RSA_DES_168_SHA,
72 SSL_RSA_RC4_128_SHA,
73 SSL_RSA_RC4_128_MD5,
74 0
75};
76
77#define DEBUG_LEVEL 0
78
79void my_debug( void *ctx, int level, char *str )
80{
81 if( level < DEBUG_LEVEL )
82 {
83 fprintf( (FILE *) ctx, "%s", str );
84 fflush( (FILE *) ctx );
85 }
86}
87
88/*
89 * These session callbacks use a simple chained list
90 * to store and retrieve the session information.
91 */
92ssl_session *s_list_1st = NULL;
93ssl_session *cur, *prv;
94
95static int my_get_session( ssl_context *ssl )
96{
97 time_t t = time( NULL );
98
99 if( ssl->resume == 0 )
100 return( 1 );
101
102 cur = s_list_1st;
103 prv = NULL;
104
105 while( cur != NULL )
106 {
107 prv = cur;
108 cur = cur->next;
109
110 if( ssl->timeout != 0 && t - prv->start > ssl->timeout )
111 continue;
112
113 if( ssl->session->cipher != prv->cipher ||
114 ssl->session->length != prv->length )
115 continue;
116
117 if( memcmp( ssl->session->id, prv->id, prv->length ) != 0 )
118 continue;
119
120 memcpy( ssl->session->master, prv->master, 48 );
121 return( 0 );
122 }
123
124 return( 1 );
125}
126
127static int my_set_session( ssl_context *ssl )
128{
129 time_t t = time( NULL );
130
131 cur = s_list_1st;
132 prv = NULL;
133
134 while( cur != NULL )
135 {
136 if( ssl->timeout != 0 && t - cur->start > ssl->timeout )
137 break; /* expired, reuse this slot */
138
139 if( memcmp( ssl->session->id, cur->id, cur->length ) == 0 )
140 break; /* client reconnected */
141
142 prv = cur;
143 cur = cur->next;
144 }
145
146 if( cur == NULL )
147 {
148 cur = (ssl_session *) malloc( sizeof( ssl_session ) );
149 if( cur == NULL )
150 return( 1 );
151
152 if( prv == NULL )
153 s_list_1st = cur;
154 else prv->next = cur;
155 }
156
157 memcpy( cur, ssl->session, sizeof( ssl_session ) );
158
159 return( 0 );
160}
161
162int main( void )
163{
164 int ret, len;
165 int listen_fd;
166 int client_fd;
167 unsigned char buf[1024];
168
169 havege_state hs;
170 ssl_context ssl;
171 ssl_session ssn;
172 x509_cert srvcert;
173 rsa_context rsa;
174
175 /*
176 * 1. Load the certificates and private RSA key
177 */
178 printf( "\n . Loading the server cert. and key..." );
179 fflush( stdout );
180
181 memset( &srvcert, 0, sizeof( x509_cert ) );
182
183 /*
184 * This demonstration program uses embedded test certificates.
185 * Instead, you may want to use x509parse_crtfile() to read the
186 * server and CA certificates, as well as x509parse_keyfile().
187 */
188 ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt,
189 strlen( test_srv_crt ) );
190 if( ret != 0 )
191 {
192 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
193 goto exit;
194 }
195
196 ret = x509parse_crt( &srvcert, (unsigned char *) test_ca_crt,
197 strlen( test_ca_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_key( &rsa, (unsigned char *) test_srv_key,
205 strlen( test_srv_key ), NULL, 0 );
206 if( ret != 0 )
207 {
208 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
209 goto exit;
210 }
211
212 printf( " ok\n" );
213
214 /*
215 * 2. Setup the listening TCP socket
216 */
217 printf( " . Bind on https://localhost:4433/ ..." );
218 fflush( stdout );
219
220 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
221 {
222 printf( " failed\n ! net_bind returned %d\n\n", ret );
223 goto exit;
224 }
225
226 printf( " ok\n" );
227
228 /*
229 * 3. Wait until a client connects
230 */
231#ifdef WIN32
232 ShellExecute( NULL, "open", "https://localhost:4433/",
233 NULL, NULL, SW_SHOWNORMAL );
234#endif
235
236 client_fd = -1;
237 memset( &ssl, 0, sizeof( ssl ) );
238
239accept:
240
241 net_close( client_fd );
242 ssl_free( &ssl );
243
244 printf( " . Waiting for a remote connection ..." );
245 fflush( stdout );
246
247 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
248 {
249 printf( " failed\n ! net_accept returned %d\n\n", ret );
250 goto exit;
251 }
252
253 printf( " ok\n" );
254
255 /*
256 * 4. Setup stuff
257 */
258 printf( " . Setting up the RNG and SSL data...." );
259 fflush( stdout );
260
261 havege_init( &hs );
262
263 if( ( ret = ssl_init( &ssl ) ) != 0 )
264 {
265 printf( " failed\n ! ssl_init returned %d\n\n", ret );
266 goto accept;
267 }
268
269 printf( " ok\n" );
270
271 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
272 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
273
274 ssl_set_rng( &ssl, havege_rand, &hs );
275 ssl_set_dbg( &ssl, my_debug, stdout );
276 ssl_set_bio( &ssl, net_recv, &client_fd,
277 net_send, &client_fd );
278 ssl_set_scb( &ssl, my_get_session,
279 my_set_session );
280
281 ssl_set_ciphers( &ssl, my_ciphers );
282 ssl_set_session( &ssl, 1, 0, &ssn );
283
284 memset( &ssn, 0, sizeof( ssl_session ) );
285
286 ssl_set_ca_chain( &ssl, srvcert.next, NULL );
287 ssl_set_own_cert( &ssl, &srvcert, &rsa );
288 ssl_set_dh_param( &ssl, my_dhm_P, my_dhm_G );
289
290 /*
291 * 5. Handshake
292 */
293 printf( " . Performing the SSL/TLS handshake..." );
294 fflush( stdout );
295
296 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
297 {
Paul Bakker40e46942009-01-03 21:51:57 +0000298 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 {
300 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
301 goto accept;
302 }
303 }
304
305 printf( " ok\n" );
306
307 /*
308 * 6. Read the HTTP Request
309 */
310 printf( " < Read from client:" );
311 fflush( stdout );
312
313 do
314 {
315 len = sizeof( buf ) - 1;
316 memset( buf, 0, sizeof( buf ) );
317 ret = ssl_read( &ssl, buf, len );
318
Paul Bakker40e46942009-01-03 21:51:57 +0000319 if( ret == POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 continue;
321
322 if( ret <= 0 )
323 {
324 switch( ret )
325 {
Paul Bakker40e46942009-01-03 21:51:57 +0000326 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 printf( " connection was closed gracefully\n" );
328 break;
329
Paul Bakker40e46942009-01-03 21:51:57 +0000330 case POLARSSL_ERR_NET_CONN_RESET:
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 printf( " connection was reset by peer\n" );
332 break;
333
334 default:
335 printf( " ssl_read returned %d\n", ret );
336 break;
337 }
338
339 break;
340 }
341
342 len = ret;
343 printf( " %d bytes read\n\n%s", len, (char *) buf );
344 }
345 while( 0 );
346
347 /*
348 * 7. Write the 200 Response
349 */
350 printf( " > Write to client:" );
351 fflush( stdout );
352
353 len = sprintf( (char *) buf, HTTP_RESPONSE,
354 ssl_get_cipher( &ssl ) );
355
356 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
357 {
Paul Bakker40e46942009-01-03 21:51:57 +0000358 if( ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000359 {
360 printf( " failed\n ! peer closed the connection\n\n" );
361 goto accept;
362 }
363
Paul Bakker40e46942009-01-03 21:51:57 +0000364 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 {
366 printf( " failed\n ! ssl_write returned %d\n\n", ret );
367 goto exit;
368 }
369 }
370
371 len = ret;
372 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
373
374 ssl_close_notify( &ssl );
375 goto accept;
376
377exit:
378
379 net_close( client_fd );
380 x509_free( &srvcert );
381 rsa_free( &rsa );
382 ssl_free( &ssl );
383
384 cur = s_list_1st;
385 while( cur != NULL )
386 {
387 prv = cur;
388 cur = cur->next;
389 memset( prv, 0, sizeof( ssl_session ) );
390 free( prv );
391 }
392
393 memset( &ssl, 0, sizeof( ssl_context ) );
394
395#ifdef WIN32
396 printf( " Press Enter to exit this program.\n" );
397 fflush( stdout ); getchar();
398#endif
399
400 return( ret );
401}