blob: 92791941636a851cc82f1d9c7970a8222873a93d [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
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakker896ac222011-05-20 12:33:05 +000027
Paul Bakkercce9d772011-11-18 14:26:47 +000028#if defined(_WIN32)
Paul Bakker896ac222011-05-20 12:33:05 +000029#include <windows.h>
30#endif
31
32#include <string.h>
33#include <stdlib.h>
34#include <stdio.h>
Paul Bakker896ac222011-05-20 12:33:05 +000035#include <signal.h>
36
Paul Bakkerfdda7852013-11-30 15:15:31 +010037#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
38#include <unistd.h>
39#endif
40
Paul Bakker508ad5a2011-12-04 17:09:26 +000041#include "polarssl/entropy.h"
42#include "polarssl/ctr_drbg.h"
Paul Bakker896ac222011-05-20 12:33:05 +000043#include "polarssl/certs.h"
44#include "polarssl/x509.h"
45#include "polarssl/ssl.h"
46#include "polarssl/net.h"
47#include "polarssl/timing.h"
48
49#define HTTP_RESPONSE \
50 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
51 "<h2>PolarSSL Test Server</h2>\r\n" \
52 "<p>Successful connection using: %s</p>\r\n"
53
Paul Bakkerb892b132011-10-12 09:19:43 +000054#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000055 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \
Paul Bakkerb892b132011-10-12 09:19:43 +000056 !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \
Paul Bakkered27a042013-04-18 22:46:23 +020057 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020058 !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_TIMING_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 "
Paul Bakker36713e82013-09-17 13:25:29 +020067 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C and/or "
Paul Bakkerfa9b1002013-07-03 15:31:03 +020068 "POLARSSL_TIMING_C not defined.\n");
Paul Bakkerb892b132011-10-12 09:19:43 +000069 return( 0 );
70}
Paul Bakkercce9d772011-11-18 14:26:47 +000071#elif defined(_WIN32)
72int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000073{
Paul Bakkercce9d772011-11-18 14:26:47 +000074 ((void) argc);
75 ((void) argv);
76
77 printf("_WIN32 defined. This application requires fork() and signals "
Paul Bakkerb892b132011-10-12 09:19:43 +000078 "to work correctly.\n");
79 return( 0 );
80}
81#else
Paul Bakker896ac222011-05-20 12:33:05 +000082
83#define DEBUG_LEVEL 0
84
Paul Bakker3c5ef712013-06-25 16:37:45 +020085static void my_debug( void *ctx, int level, const char *str )
Paul Bakker896ac222011-05-20 12:33:05 +000086{
87 if( level < DEBUG_LEVEL )
88 {
89 fprintf( (FILE *) ctx, "%s", str );
90 fflush( (FILE *) ctx );
91 }
92}
93
Paul Bakkercce9d772011-11-18 14:26:47 +000094int main( int argc, char *argv[] )
Paul Bakker896ac222011-05-20 12:33:05 +000095{
96 int ret, len, cnt = 0, pid;
97 int listen_fd;
Manuel Pégourié-Gonnard68821da2013-09-16 12:34:33 +020098 int client_fd = -1;
Paul Bakker896ac222011-05-20 12:33:05 +000099 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200100 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +0000101
Paul Bakker508ad5a2011-12-04 17:09:26 +0000102 entropy_context entropy;
103 ctr_drbg_context ctr_drbg;
Paul Bakker896ac222011-05-20 12:33:05 +0000104 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200105 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200106 pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +0000107
Paul Bakkercce9d772011-11-18 14:26:47 +0000108 ((void) argc);
109 ((void) argv);
110
Paul Bakker0c226102014-04-17 16:02:36 +0200111 memset( &ssl, 0, sizeof(ssl_context) );
112
113 entropy_init( &entropy );
114 pk_init( &pkey );
115 x509_crt_init( &srvcert );
116
Paul Bakker896ac222011-05-20 12:33:05 +0000117 signal( SIGCHLD, SIG_IGN );
118
119 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000120 * 0. Initial seeding of the RNG
121 */
122 printf( "\n . Initial seeding of the random generator..." );
123 fflush( stdout );
124
Paul Bakker508ad5a2011-12-04 17:09:26 +0000125 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200126 (const unsigned char *) pers,
127 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000128 {
129 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
130 goto exit;
131 }
132
133 printf( " ok\n" );
134
135 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000136 * 1. Load the certificates and private RSA key
137 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000138 printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000139 fflush( stdout );
140
Paul Bakker896ac222011-05-20 12:33:05 +0000141 /*
142 * This demonstration program uses embedded test certificates.
Paul Bakkerddf26b42013-09-18 13:46:23 +0200143 * Instead, you may want to use x509_crt_parse_file() to read the
144 * server and CA certificates, as well as pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000145 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200146 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt,
147 strlen( test_srv_crt ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000148 if( ret != 0 )
149 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200150 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000151 goto exit;
152 }
153
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200154 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_ca_list,
155 strlen( test_ca_list ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000156 if( ret != 0 )
157 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200158 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000159 goto exit;
160 }
161
Paul Bakker1a7550a2013-09-15 13:01:22 +0200162 ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key,
Paul Bakker896ac222011-05-20 12:33:05 +0000163 strlen( test_srv_key ), NULL, 0 );
164 if( ret != 0 )
165 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200166 printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000167 goto exit;
168 }
169
170 printf( " ok\n" );
171
172 /*
173 * 2. Setup the listening TCP socket
174 */
175 printf( " . Bind on https://localhost:4433/ ..." );
176 fflush( stdout );
177
178 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
179 {
180 printf( " failed\n ! net_bind returned %d\n\n", ret );
181 goto exit;
182 }
183
184 printf( " ok\n" );
185
186 while( 1 )
187 {
188 /*
189 * 3. Wait until a client connects
190 */
191 client_fd = -1;
192 memset( &ssl, 0, sizeof( ssl ) );
193
194 printf( " . Waiting for a remote connection ..." );
195 fflush( stdout );
196
197 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
198 {
199 printf( " failed\n ! net_accept returned %d\n\n", ret );
200 goto exit;
201 }
202
203 printf( " ok\n" );
204
205 /*
206 * 3.5. Forking server thread
207 */
208
209 pid = fork();
210
211 printf( " . Forking to handle connection ..." );
212 fflush( stdout );
213
214 if( pid < 0 )
215 {
216 printf(" failed\n ! fork returned %d\n\n", pid );
217 goto exit;
218 }
219
220 printf( " ok\n" );
221
222 if( pid != 0 )
223 {
Paul Bakker508ad5a2011-12-04 17:09:26 +0000224 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200225 (const unsigned char *) "parent",
226 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000227 {
228 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
229 goto exit;
230 }
231
Paul Bakker896ac222011-05-20 12:33:05 +0000232 close( client_fd );
233 continue;
234 }
235
236 close( listen_fd );
237
238 /*
239 * 4. Setup stuff
240 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000241 printf( " . Setting up the SSL data...." );
Paul Bakker896ac222011-05-20 12:33:05 +0000242 fflush( stdout );
243
Paul Bakker508ad5a2011-12-04 17:09:26 +0000244 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200245 (const unsigned char *) "child",
246 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000247 {
248 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
249 goto exit;
250 }
Paul Bakker0c226102014-04-17 16:02:36 +0200251
Paul Bakker896ac222011-05-20 12:33:05 +0000252 if( ( ret = ssl_init( &ssl ) ) != 0 )
253 {
254 printf( " failed\n ! ssl_init returned %d\n\n", ret );
255 goto exit;
256 }
257
258 printf( " ok\n" );
259
260 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
261 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
262
Paul Bakker508ad5a2011-12-04 17:09:26 +0000263 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker896ac222011-05-20 12:33:05 +0000264 ssl_set_dbg( &ssl, my_debug, stdout );
265 ssl_set_bio( &ssl, net_recv, &client_fd,
266 net_send, &client_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000267
Paul Bakker896ac222011-05-20 12:33:05 +0000268 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200269 ssl_set_own_cert( &ssl, &srvcert, &pkey );
Paul Bakker896ac222011-05-20 12:33:05 +0000270
271 /*
272 * 5. Handshake
273 */
274 printf( " . Performing the SSL/TLS handshake..." );
275 fflush( stdout );
276
277 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
278 {
279 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
280 {
281 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
282 goto exit;
283 }
284 }
285
286 printf( " ok\n" );
287
288 /*
289 * 6. Read the HTTP Request
290 */
291 printf( " < Read from client:" );
292 fflush( stdout );
293
294 do
295 {
296 len = sizeof( buf ) - 1;
297 memset( buf, 0, sizeof( buf ) );
298 ret = ssl_read( &ssl, buf, len );
299
300 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
301 continue;
302
303 if( ret <= 0 )
304 {
305 switch( ret )
306 {
307 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
308 printf( " connection was closed gracefully\n" );
309 break;
310
311 case POLARSSL_ERR_NET_CONN_RESET:
312 printf( " connection was reset by peer\n" );
313 break;
314
315 default:
316 printf( " ssl_read returned %d\n", ret );
317 break;
318 }
319
320 break;
321 }
322
323 len = ret;
324 printf( " %d bytes read\n\n%s", len, (char *) buf );
325 }
326 while( 0 );
327
328 /*
329 * 7. Write the 200 Response
330 */
331 printf( " > Write to client:" );
332 fflush( stdout );
333
334 len = sprintf( (char *) buf, HTTP_RESPONSE,
335 ssl_get_ciphersuite( &ssl ) );
336
Paul Bakker030decd2014-04-17 16:03:23 +0200337 while( cnt++ < 100 )
Paul Bakker896ac222011-05-20 12:33:05 +0000338 {
339 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
340 {
341 if( ret == POLARSSL_ERR_NET_CONN_RESET )
342 {
343 printf( " failed\n ! peer closed the connection\n\n" );
344 goto exit;
345 }
346
347 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
348 {
349 printf( " failed\n ! ssl_write returned %d\n\n", ret );
350 goto exit;
351 }
352 }
353 len = ret;
354 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
355
356 m_sleep( 1000 );
357 }
358
359 ssl_close_notify( &ssl );
360 goto exit;
361 }
362
363exit:
364
Paul Bakker0c226102014-04-17 16:02:36 +0200365 if( client_fd != -1 )
366 net_close( client_fd );
367
Paul Bakker36713e82013-09-17 13:25:29 +0200368 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200369 pk_free( &pkey );
Paul Bakker896ac222011-05-20 12:33:05 +0000370 ssl_free( &ssl );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200371 entropy_free( &entropy );
Paul Bakker896ac222011-05-20 12:33:05 +0000372
Paul Bakkercce9d772011-11-18 14:26:47 +0000373#if defined(_WIN32)
Paul Bakker896ac222011-05-20 12:33:05 +0000374 printf( " Press Enter to exit this program.\n" );
375 fflush( stdout ); getchar();
376#endif
377
378 return( ret );
379}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000380#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000381 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000382 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */