blob: 0ef3cf98c01e5a108553eafa978b1266216510eb [file] [log] [blame]
Paul Bakker896ac222011-05-20 12:33:05 +00001/*
Paul Bakkercb79ae0b2011-05-20 12:44:16 +00002 * SSL server demonstration program using fork() for handling multiple clients
Paul Bakker896ac222011-05-20 12:33:05 +00003 *
Paul Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakker896ac222011-05-20 12:33:05 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
Paul Bakkercce9d772011-11-18 14:26:47 +000030#if defined(_WIN32)
Paul Bakker896ac222011-05-20 12:33:05 +000031#include <windows.h>
32#endif
33
34#include <string.h>
35#include <stdlib.h>
36#include <stdio.h>
37#include <unistd.h>
38#include <signal.h>
39
Paul Bakker5690efc2011-05-26 13:16:06 +000040#include "polarssl/config.h"
41
Paul Bakker508ad5a2011-12-04 17:09:26 +000042#include "polarssl/entropy.h"
43#include "polarssl/ctr_drbg.h"
Paul Bakker896ac222011-05-20 12:33:05 +000044#include "polarssl/certs.h"
45#include "polarssl/x509.h"
46#include "polarssl/ssl.h"
47#include "polarssl/net.h"
48#include "polarssl/timing.h"
49
50#define HTTP_RESPONSE \
51 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
52 "<h2>PolarSSL Test Server</h2>\r\n" \
53 "<p>Successful connection using: %s</p>\r\n"
54
Paul Bakkerb892b132011-10-12 09:19:43 +000055#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000056 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \
Paul Bakkerb892b132011-10-12 09:19:43 +000057 !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000058 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000059int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000060{
Paul Bakkercce9d772011-11-18 14:26:47 +000061 ((void) argc);
62 ((void) argv);
63
Paul Bakker508ad5a2011-12-04 17:09:26 +000064 printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C "
Paul Bakkerb892b132011-10-12 09:19:43 +000065 "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000066 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
67 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakkerb892b132011-10-12 09:19:43 +000068 return( 0 );
69}
Paul Bakkercce9d772011-11-18 14:26:47 +000070#elif defined(_WIN32)
71int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000072{
Paul Bakkercce9d772011-11-18 14:26:47 +000073 ((void) argc);
74 ((void) argv);
75
76 printf("_WIN32 defined. This application requires fork() and signals "
Paul Bakkerb892b132011-10-12 09:19:43 +000077 "to work correctly.\n");
78 return( 0 );
79}
80#else
Paul Bakker896ac222011-05-20 12:33:05 +000081/*
82 * Computing a "safe" DH-1024 prime can take a very
83 * long time, so a precomputed value is provided below.
84 * You may run dh_genprime to generate a new value.
85 */
86char *my_dhm_P =
87 "E4004C1F94182000103D883A448B3F80" \
88 "2CE4B44A83301270002C20D0321CFD00" \
89 "11CCEF784C26A400F43DFB901BCA7538" \
90 "F2C6B176001CF5A0FD16D2C48B1D0C1C" \
91 "F6AC8E1DA6BCC3B4E1F96B0564965300" \
92 "FFA1D0B601EB2800F489AA512C4B248C" \
93 "01F76949A60BB7F00A40B1EAB64BDD48" \
94 "E8A700D60B7F1200FA8E77B0A979DABF";
95
96char *my_dhm_G = "4";
97
98/*
99 * Sorted by order of preference
100 */
101int my_ciphersuites[] =
102{
103 SSL_EDH_RSA_AES_256_SHA,
104 SSL_EDH_RSA_CAMELLIA_256_SHA,
105 SSL_EDH_RSA_AES_128_SHA,
106 SSL_EDH_RSA_CAMELLIA_128_SHA,
107 SSL_EDH_RSA_DES_168_SHA,
108 SSL_RSA_AES_256_SHA,
109 SSL_RSA_CAMELLIA_256_SHA,
110 SSL_RSA_AES_128_SHA,
111 SSL_RSA_CAMELLIA_128_SHA,
112 SSL_RSA_DES_168_SHA,
113 SSL_RSA_RC4_128_SHA,
114 SSL_RSA_RC4_128_MD5,
115 0
116};
117
118#define DEBUG_LEVEL 0
119
120void my_debug( void *ctx, int level, const char *str )
121{
122 if( level < DEBUG_LEVEL )
123 {
124 fprintf( (FILE *) ctx, "%s", str );
125 fflush( (FILE *) ctx );
126 }
127}
128
Paul Bakkercce9d772011-11-18 14:26:47 +0000129int main( int argc, char *argv[] )
Paul Bakker896ac222011-05-20 12:33:05 +0000130{
131 int ret, len, cnt = 0, pid;
132 int listen_fd;
133 int client_fd;
134 unsigned char buf[1024];
Paul Bakker508ad5a2011-12-04 17:09:26 +0000135 char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +0000136
Paul Bakker508ad5a2011-12-04 17:09:26 +0000137 entropy_context entropy;
138 ctr_drbg_context ctr_drbg;
Paul Bakker896ac222011-05-20 12:33:05 +0000139 ssl_context ssl;
Paul Bakker896ac222011-05-20 12:33:05 +0000140 x509_cert srvcert;
141 rsa_context rsa;
142
Paul Bakkercce9d772011-11-18 14:26:47 +0000143 ((void) argc);
144 ((void) argv);
145
Paul Bakker896ac222011-05-20 12:33:05 +0000146 signal( SIGCHLD, SIG_IGN );
147
148 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000149 * 0. Initial seeding of the RNG
150 */
151 printf( "\n . Initial seeding of the random generator..." );
152 fflush( stdout );
153
154 entropy_init( &entropy );
155 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
156 (unsigned char *) pers, strlen( pers ) ) ) != 0 )
157 {
158 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
159 goto exit;
160 }
161
162 printf( " ok\n" );
163
164 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000165 * 1. Load the certificates and private RSA key
166 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000167 printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000168 fflush( stdout );
169
170 memset( &srvcert, 0, sizeof( x509_cert ) );
171
172 /*
173 * This demonstration program uses embedded test certificates.
174 * Instead, you may want to use x509parse_crtfile() to read the
175 * server and CA certificates, as well as x509parse_keyfile().
176 */
177 ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000178 strlen( test_srv_crt ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000179 if( ret != 0 )
180 {
181 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
182 goto exit;
183 }
184
185 ret = x509parse_crt( &srvcert, (unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000186 strlen( test_ca_crt ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000187 if( ret != 0 )
188 {
189 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
190 goto exit;
191 }
192
193 rsa_init( &rsa, RSA_PKCS_V15, 0 );
194 ret = x509parse_key( &rsa, (unsigned char *) test_srv_key,
195 strlen( test_srv_key ), NULL, 0 );
196 if( ret != 0 )
197 {
198 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
199 goto exit;
200 }
201
202 printf( " ok\n" );
203
204 /*
205 * 2. Setup the listening TCP socket
206 */
207 printf( " . Bind on https://localhost:4433/ ..." );
208 fflush( stdout );
209
210 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
211 {
212 printf( " failed\n ! net_bind returned %d\n\n", ret );
213 goto exit;
214 }
215
216 printf( " ok\n" );
217
218 while( 1 )
219 {
220 /*
221 * 3. Wait until a client connects
222 */
223 client_fd = -1;
224 memset( &ssl, 0, sizeof( ssl ) );
225
226 printf( " . Waiting for a remote connection ..." );
227 fflush( stdout );
228
229 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
230 {
231 printf( " failed\n ! net_accept returned %d\n\n", ret );
232 goto exit;
233 }
234
235 printf( " ok\n" );
236
237 /*
238 * 3.5. Forking server thread
239 */
240
241 pid = fork();
242
243 printf( " . Forking to handle connection ..." );
244 fflush( stdout );
245
246 if( pid < 0 )
247 {
248 printf(" failed\n ! fork returned %d\n\n", pid );
249 goto exit;
250 }
251
252 printf( " ok\n" );
253
254 if( pid != 0 )
255 {
Paul Bakker508ad5a2011-12-04 17:09:26 +0000256 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
257 (unsigned char* ) "parent", 6 ) ) != 0 )
258 {
259 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
260 goto exit;
261 }
262
Paul Bakker896ac222011-05-20 12:33:05 +0000263 close( client_fd );
264 continue;
265 }
266
267 close( listen_fd );
268
269 /*
270 * 4. Setup stuff
271 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000272 printf( " . Setting up the SSL data...." );
Paul Bakker896ac222011-05-20 12:33:05 +0000273 fflush( stdout );
274
Paul Bakker508ad5a2011-12-04 17:09:26 +0000275 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
276 (unsigned char *) "child", 5 ) ) != 0 )
277 {
278 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
279 goto exit;
280 }
281
Paul Bakker896ac222011-05-20 12:33:05 +0000282 if( ( ret = ssl_init( &ssl ) ) != 0 )
283 {
284 printf( " failed\n ! ssl_init returned %d\n\n", ret );
285 goto exit;
286 }
287
288 printf( " ok\n" );
289
290 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
291 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
292
Paul Bakker508ad5a2011-12-04 17:09:26 +0000293 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker896ac222011-05-20 12:33:05 +0000294 ssl_set_dbg( &ssl, my_debug, stdout );
295 ssl_set_bio( &ssl, net_recv, &client_fd,
296 net_send, &client_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000297
298 ssl_set_ciphersuites( &ssl, my_ciphersuites );
Paul Bakker896ac222011-05-20 12:33:05 +0000299
300 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
301 ssl_set_own_cert( &ssl, &srvcert, &rsa );
Paul Bakker48916f92012-09-16 19:57:18 +0000302#if defined(POLARSSL_DHM_C)
Paul Bakker896ac222011-05-20 12:33:05 +0000303 ssl_set_dh_param( &ssl, my_dhm_P, my_dhm_G );
Paul Bakker48916f92012-09-16 19:57:18 +0000304#endif
Paul Bakker896ac222011-05-20 12:33:05 +0000305
306 /*
307 * 5. Handshake
308 */
309 printf( " . Performing the SSL/TLS handshake..." );
310 fflush( stdout );
311
312 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
313 {
314 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
315 {
316 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
317 goto exit;
318 }
319 }
320
321 printf( " ok\n" );
322
323 /*
324 * 6. Read the HTTP Request
325 */
326 printf( " < Read from client:" );
327 fflush( stdout );
328
329 do
330 {
331 len = sizeof( buf ) - 1;
332 memset( buf, 0, sizeof( buf ) );
333 ret = ssl_read( &ssl, buf, len );
334
335 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
336 continue;
337
338 if( ret <= 0 )
339 {
340 switch( ret )
341 {
342 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
343 printf( " connection was closed gracefully\n" );
344 break;
345
346 case POLARSSL_ERR_NET_CONN_RESET:
347 printf( " connection was reset by peer\n" );
348 break;
349
350 default:
351 printf( " ssl_read returned %d\n", ret );
352 break;
353 }
354
355 break;
356 }
357
358 len = ret;
359 printf( " %d bytes read\n\n%s", len, (char *) buf );
360 }
361 while( 0 );
362
363 /*
364 * 7. Write the 200 Response
365 */
366 printf( " > Write to client:" );
367 fflush( stdout );
368
369 len = sprintf( (char *) buf, HTTP_RESPONSE,
370 ssl_get_ciphersuite( &ssl ) );
371
372 while( cnt < 100 )
373 {
374 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
375 {
376 if( ret == POLARSSL_ERR_NET_CONN_RESET )
377 {
378 printf( " failed\n ! peer closed the connection\n\n" );
379 goto exit;
380 }
381
382 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
383 {
384 printf( " failed\n ! ssl_write returned %d\n\n", ret );
385 goto exit;
386 }
387 }
388 len = ret;
389 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
390
391 m_sleep( 1000 );
392 }
393
394 ssl_close_notify( &ssl );
395 goto exit;
396 }
397
398exit:
399
400 net_close( client_fd );
401 x509_free( &srvcert );
402 rsa_free( &rsa );
403 ssl_free( &ssl );
404
Paul Bakkercce9d772011-11-18 14:26:47 +0000405#if defined(_WIN32)
Paul Bakker896ac222011-05-20 12:33:05 +0000406 printf( " Press Enter to exit this program.\n" );
407 fflush( stdout ); getchar();
408#endif
409
410 return( ret );
411}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000412#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000413 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000414 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */