blob: 986458b5e42b3b92d6a96f3b7df1e8f739fd8139 [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 Bakker3c5ef712013-06-25 16:37:45 +02004 * Copyright (C) 2006-2013, 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 Bakkered27a042013-04-18 22:46:23 +020058 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakkerfa9b1002013-07-03 15:31:03 +020059 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_TIMING_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000060int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000061{
Paul Bakkercce9d772011-11-18 14:26:47 +000062 ((void) argc);
63 ((void) argv);
64
Paul Bakker508ad5a2011-12-04 17:09:26 +000065 printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C "
Paul Bakkerb892b132011-10-12 09:19:43 +000066 "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000067 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakkerfa9b1002013-07-03 15:31:03 +020068 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_PARSE_C and/or "
69 "POLARSSL_TIMING_C not defined.\n");
Paul Bakkerb892b132011-10-12 09:19:43 +000070 return( 0 );
71}
Paul Bakkercce9d772011-11-18 14:26:47 +000072#elif defined(_WIN32)
73int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000074{
Paul Bakkercce9d772011-11-18 14:26:47 +000075 ((void) argc);
76 ((void) argv);
77
78 printf("_WIN32 defined. This application requires fork() and signals "
Paul Bakkerb892b132011-10-12 09:19:43 +000079 "to work correctly.\n");
80 return( 0 );
81}
82#else
Paul Bakker896ac222011-05-20 12:33:05 +000083
84#define DEBUG_LEVEL 0
85
Paul Bakker3c5ef712013-06-25 16:37:45 +020086static void my_debug( void *ctx, int level, const char *str )
Paul Bakker896ac222011-05-20 12:33:05 +000087{
88 if( level < DEBUG_LEVEL )
89 {
90 fprintf( (FILE *) ctx, "%s", str );
91 fflush( (FILE *) ctx );
92 }
93}
94
Paul Bakkercce9d772011-11-18 14:26:47 +000095int main( int argc, char *argv[] )
Paul Bakker896ac222011-05-20 12:33:05 +000096{
97 int ret, len, cnt = 0, pid;
98 int listen_fd;
99 int client_fd;
100 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200101 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +0000102
Paul Bakker508ad5a2011-12-04 17:09:26 +0000103 entropy_context entropy;
104 ctr_drbg_context ctr_drbg;
Paul Bakker896ac222011-05-20 12:33:05 +0000105 ssl_context ssl;
Paul Bakker896ac222011-05-20 12:33:05 +0000106 x509_cert srvcert;
107 rsa_context rsa;
108
Paul Bakkercce9d772011-11-18 14:26:47 +0000109 ((void) argc);
110 ((void) argv);
111
Paul Bakker896ac222011-05-20 12:33:05 +0000112 signal( SIGCHLD, SIG_IGN );
113
114 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000115 * 0. Initial seeding of the RNG
116 */
117 printf( "\n . Initial seeding of the random generator..." );
118 fflush( stdout );
119
120 entropy_init( &entropy );
121 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200122 (const unsigned char *) pers,
123 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000124 {
125 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
126 goto exit;
127 }
128
129 printf( " ok\n" );
130
131 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000132 * 1. Load the certificates and private RSA key
133 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000134 printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000135 fflush( stdout );
136
137 memset( &srvcert, 0, sizeof( x509_cert ) );
138
139 /*
140 * This demonstration program uses embedded test certificates.
141 * Instead, you may want to use x509parse_crtfile() to read the
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200142 * server and CA certificates, as well as x509parse_keyfile_rsa().
Paul Bakker896ac222011-05-20 12:33:05 +0000143 */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200144 ret = x509parse_crt( &srvcert, (const unsigned char *) test_srv_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000145 strlen( test_srv_crt ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000146 if( ret != 0 )
147 {
148 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
149 goto exit;
150 }
151
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200152 ret = x509parse_crt( &srvcert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000153 strlen( test_ca_crt ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000154 if( ret != 0 )
155 {
156 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
157 goto exit;
158 }
159
160 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200161 ret = x509parse_key_rsa( &rsa, (const unsigned char *) test_srv_key,
Paul Bakker896ac222011-05-20 12:33:05 +0000162 strlen( test_srv_key ), NULL, 0 );
163 if( ret != 0 )
164 {
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200165 printf( " failed\n ! x509parse_key_rsa returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000166 goto exit;
167 }
168
169 printf( " ok\n" );
170
171 /*
172 * 2. Setup the listening TCP socket
173 */
174 printf( " . Bind on https://localhost:4433/ ..." );
175 fflush( stdout );
176
177 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
178 {
179 printf( " failed\n ! net_bind returned %d\n\n", ret );
180 goto exit;
181 }
182
183 printf( " ok\n" );
184
185 while( 1 )
186 {
187 /*
188 * 3. Wait until a client connects
189 */
190 client_fd = -1;
191 memset( &ssl, 0, sizeof( ssl ) );
192
193 printf( " . Waiting for a remote connection ..." );
194 fflush( stdout );
195
196 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
197 {
198 printf( " failed\n ! net_accept returned %d\n\n", ret );
199 goto exit;
200 }
201
202 printf( " ok\n" );
203
204 /*
205 * 3.5. Forking server thread
206 */
207
208 pid = fork();
209
210 printf( " . Forking to handle connection ..." );
211 fflush( stdout );
212
213 if( pid < 0 )
214 {
215 printf(" failed\n ! fork returned %d\n\n", pid );
216 goto exit;
217 }
218
219 printf( " ok\n" );
220
221 if( pid != 0 )
222 {
Paul Bakker508ad5a2011-12-04 17:09:26 +0000223 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200224 (const unsigned char *) "parent",
225 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000226 {
227 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
228 goto exit;
229 }
230
Paul Bakker896ac222011-05-20 12:33:05 +0000231 close( client_fd );
232 continue;
233 }
234
235 close( listen_fd );
236
237 /*
238 * 4. Setup stuff
239 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000240 printf( " . Setting up the SSL data...." );
Paul Bakker896ac222011-05-20 12:33:05 +0000241 fflush( stdout );
242
Paul Bakker508ad5a2011-12-04 17:09:26 +0000243 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200244 (const unsigned char *) "child",
245 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000246 {
247 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
248 goto exit;
249 }
250
Paul Bakker896ac222011-05-20 12:33:05 +0000251 if( ( ret = ssl_init( &ssl ) ) != 0 )
252 {
253 printf( " failed\n ! ssl_init returned %d\n\n", ret );
254 goto exit;
255 }
256
257 printf( " ok\n" );
258
259 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
260 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
261
Paul Bakker508ad5a2011-12-04 17:09:26 +0000262 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker896ac222011-05-20 12:33:05 +0000263 ssl_set_dbg( &ssl, my_debug, stdout );
264 ssl_set_bio( &ssl, net_recv, &client_fd,
265 net_send, &client_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000266
Paul Bakker896ac222011-05-20 12:33:05 +0000267 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
268 ssl_set_own_cert( &ssl, &srvcert, &rsa );
Paul Bakker896ac222011-05-20 12:33:05 +0000269
270 /*
271 * 5. Handshake
272 */
273 printf( " . Performing the SSL/TLS handshake..." );
274 fflush( stdout );
275
276 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
277 {
278 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
279 {
280 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
281 goto exit;
282 }
283 }
284
285 printf( " ok\n" );
286
287 /*
288 * 6. Read the HTTP Request
289 */
290 printf( " < Read from client:" );
291 fflush( stdout );
292
293 do
294 {
295 len = sizeof( buf ) - 1;
296 memset( buf, 0, sizeof( buf ) );
297 ret = ssl_read( &ssl, buf, len );
298
299 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
300 continue;
301
302 if( ret <= 0 )
303 {
304 switch( ret )
305 {
306 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
307 printf( " connection was closed gracefully\n" );
308 break;
309
310 case POLARSSL_ERR_NET_CONN_RESET:
311 printf( " connection was reset by peer\n" );
312 break;
313
314 default:
315 printf( " ssl_read returned %d\n", ret );
316 break;
317 }
318
319 break;
320 }
321
322 len = ret;
323 printf( " %d bytes read\n\n%s", len, (char *) buf );
324 }
325 while( 0 );
326
327 /*
328 * 7. Write the 200 Response
329 */
330 printf( " > Write to client:" );
331 fflush( stdout );
332
333 len = sprintf( (char *) buf, HTTP_RESPONSE,
334 ssl_get_ciphersuite( &ssl ) );
335
336 while( cnt < 100 )
337 {
338 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
339 {
340 if( ret == POLARSSL_ERR_NET_CONN_RESET )
341 {
342 printf( " failed\n ! peer closed the connection\n\n" );
343 goto exit;
344 }
345
346 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
347 {
348 printf( " failed\n ! ssl_write returned %d\n\n", ret );
349 goto exit;
350 }
351 }
352 len = ret;
353 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
354
355 m_sleep( 1000 );
356 }
357
358 ssl_close_notify( &ssl );
359 goto exit;
360 }
361
362exit:
363
364 net_close( client_fd );
365 x509_free( &srvcert );
366 rsa_free( &rsa );
367 ssl_free( &ssl );
368
Paul Bakkercce9d772011-11-18 14:26:47 +0000369#if defined(_WIN32)
Paul Bakker896ac222011-05-20 12:33:05 +0000370 printf( " Press Enter to exit this program.\n" );
371 fflush( stdout ); getchar();
372#endif
373
374 return( ret );
375}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000376#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000377 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000378 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */