blob: ae1e155b91a8429bc1e8b64d33b9045622c03df4 [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é-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker896ac222011-05-20 12:33:05 +000031
Paul Bakkercce9d772011-11-18 14:26:47 +000032#if defined(_WIN32)
Paul Bakker896ac222011-05-20 12:33:05 +000033#include <windows.h>
34#endif
35
36#include <string.h>
37#include <stdlib.h>
38#include <stdio.h>
Paul Bakker896ac222011-05-20 12:33:05 +000039#include <signal.h>
40
Paul Bakkerfdda7852013-11-30 15:15:31 +010041#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
42#include <unistd.h>
43#endif
44
Paul Bakker508ad5a2011-12-04 17:09:26 +000045#include "polarssl/entropy.h"
46#include "polarssl/ctr_drbg.h"
Paul Bakker896ac222011-05-20 12:33:05 +000047#include "polarssl/certs.h"
48#include "polarssl/x509.h"
49#include "polarssl/ssl.h"
50#include "polarssl/net.h"
51#include "polarssl/timing.h"
52
53#define HTTP_RESPONSE \
54 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
55 "<h2>PolarSSL Test Server</h2>\r\n" \
56 "<p>Successful connection using: %s</p>\r\n"
57
Paul Bakkerb892b132011-10-12 09:19:43 +000058#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000059 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \
Paul Bakkerb892b132011-10-12 09:19:43 +000060 !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \
Paul Bakkered27a042013-04-18 22:46:23 +020061 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020062 !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_TIMING_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000063int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000064{
Paul Bakkercce9d772011-11-18 14:26:47 +000065 ((void) argc);
66 ((void) argv);
67
Paul Bakker508ad5a2011-12-04 17:09:26 +000068 printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C "
Paul Bakkerb892b132011-10-12 09:19:43 +000069 "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000070 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020071 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C and/or "
Paul Bakkerfa9b1002013-07-03 15:31:03 +020072 "POLARSSL_TIMING_C not defined.\n");
Paul Bakkerb892b132011-10-12 09:19:43 +000073 return( 0 );
74}
Paul Bakkercce9d772011-11-18 14:26:47 +000075#elif defined(_WIN32)
76int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000077{
Paul Bakkercce9d772011-11-18 14:26:47 +000078 ((void) argc);
79 ((void) argv);
80
81 printf("_WIN32 defined. This application requires fork() and signals "
Paul Bakkerb892b132011-10-12 09:19:43 +000082 "to work correctly.\n");
83 return( 0 );
84}
85#else
Paul Bakker896ac222011-05-20 12:33:05 +000086
87#define DEBUG_LEVEL 0
88
Paul Bakker3c5ef712013-06-25 16:37:45 +020089static void my_debug( void *ctx, int level, const char *str )
Paul Bakker896ac222011-05-20 12:33:05 +000090{
91 if( level < DEBUG_LEVEL )
92 {
93 fprintf( (FILE *) ctx, "%s", str );
94 fflush( (FILE *) ctx );
95 }
96}
97
Paul Bakkercce9d772011-11-18 14:26:47 +000098int main( int argc, char *argv[] )
Paul Bakker896ac222011-05-20 12:33:05 +000099{
100 int ret, len, cnt = 0, pid;
101 int listen_fd;
Manuel Pégourié-Gonnard68821da2013-09-16 12:34:33 +0200102 int client_fd = -1;
Paul Bakker896ac222011-05-20 12:33:05 +0000103 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200104 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +0000105
Paul Bakker508ad5a2011-12-04 17:09:26 +0000106 entropy_context entropy;
107 ctr_drbg_context ctr_drbg;
Paul Bakker896ac222011-05-20 12:33:05 +0000108 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200109 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200110 pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +0000111
Paul Bakkercce9d772011-11-18 14:26:47 +0000112 ((void) argc);
113 ((void) argv);
114
Paul Bakker0c226102014-04-17 16:02:36 +0200115 memset( &ssl, 0, sizeof(ssl_context) );
116
117 entropy_init( &entropy );
118 pk_init( &pkey );
119 x509_crt_init( &srvcert );
120
Paul Bakker896ac222011-05-20 12:33:05 +0000121 signal( SIGCHLD, SIG_IGN );
122
123 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000124 * 0. Initial seeding of the RNG
125 */
126 printf( "\n . Initial seeding of the random generator..." );
127 fflush( stdout );
128
Paul Bakker508ad5a2011-12-04 17:09:26 +0000129 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200130 (const unsigned char *) pers,
131 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000132 {
133 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
134 goto exit;
135 }
136
137 printf( " ok\n" );
138
139 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000140 * 1. Load the certificates and private RSA key
141 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000142 printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000143 fflush( stdout );
144
Paul Bakker896ac222011-05-20 12:33:05 +0000145 /*
146 * This demonstration program uses embedded test certificates.
Paul Bakkerddf26b42013-09-18 13:46:23 +0200147 * Instead, you may want to use x509_crt_parse_file() to read the
148 * server and CA certificates, as well as pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000149 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200150 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt,
151 strlen( test_srv_crt ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000152 if( ret != 0 )
153 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200154 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000155 goto exit;
156 }
157
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200158 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_ca_list,
159 strlen( test_ca_list ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000160 if( ret != 0 )
161 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200162 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000163 goto exit;
164 }
165
Paul Bakker1a7550a2013-09-15 13:01:22 +0200166 ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key,
Paul Bakker896ac222011-05-20 12:33:05 +0000167 strlen( test_srv_key ), NULL, 0 );
168 if( ret != 0 )
169 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200170 printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000171 goto exit;
172 }
173
174 printf( " ok\n" );
175
176 /*
177 * 2. Setup the listening TCP socket
178 */
179 printf( " . Bind on https://localhost:4433/ ..." );
180 fflush( stdout );
181
182 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
183 {
184 printf( " failed\n ! net_bind returned %d\n\n", ret );
185 goto exit;
186 }
187
188 printf( " ok\n" );
189
190 while( 1 )
191 {
192 /*
193 * 3. Wait until a client connects
194 */
195 client_fd = -1;
196 memset( &ssl, 0, sizeof( ssl ) );
197
198 printf( " . Waiting for a remote connection ..." );
199 fflush( stdout );
200
201 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
202 {
203 printf( " failed\n ! net_accept returned %d\n\n", ret );
204 goto exit;
205 }
206
207 printf( " ok\n" );
208
209 /*
210 * 3.5. Forking server thread
211 */
212
213 pid = fork();
214
215 printf( " . Forking to handle connection ..." );
216 fflush( stdout );
217
218 if( pid < 0 )
219 {
220 printf(" failed\n ! fork returned %d\n\n", pid );
221 goto exit;
222 }
223
224 printf( " ok\n" );
225
226 if( pid != 0 )
227 {
Paul Bakker508ad5a2011-12-04 17:09:26 +0000228 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200229 (const unsigned char *) "parent",
230 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000231 {
232 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
233 goto exit;
234 }
235
Paul Bakker896ac222011-05-20 12:33:05 +0000236 close( client_fd );
237 continue;
238 }
239
240 close( listen_fd );
241
242 /*
243 * 4. Setup stuff
244 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000245 printf( " . Setting up the SSL data...." );
Paul Bakker896ac222011-05-20 12:33:05 +0000246 fflush( stdout );
247
Paul Bakker508ad5a2011-12-04 17:09:26 +0000248 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200249 (const unsigned char *) "child",
250 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000251 {
252 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
253 goto exit;
254 }
Paul Bakker0c226102014-04-17 16:02:36 +0200255
Paul Bakker896ac222011-05-20 12:33:05 +0000256 if( ( ret = ssl_init( &ssl ) ) != 0 )
257 {
258 printf( " failed\n ! ssl_init returned %d\n\n", ret );
259 goto exit;
260 }
261
262 printf( " ok\n" );
263
264 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
265 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
266
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100267 /* SSLv3 is deprecated, set minimum to TLS 1.0 */
268 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3,
269 SSL_MINOR_VERSION_1 );
270
Paul Bakker508ad5a2011-12-04 17:09:26 +0000271 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker896ac222011-05-20 12:33:05 +0000272 ssl_set_dbg( &ssl, my_debug, stdout );
273 ssl_set_bio( &ssl, net_recv, &client_fd,
274 net_send, &client_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000275
Paul Bakker896ac222011-05-20 12:33:05 +0000276 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200277 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
278 {
279 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
280 goto exit;
281 }
Paul Bakker896ac222011-05-20 12:33:05 +0000282
283 /*
284 * 5. Handshake
285 */
286 printf( " . Performing the SSL/TLS handshake..." );
287 fflush( stdout );
288
289 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
290 {
291 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
292 {
293 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
294 goto exit;
295 }
296 }
297
298 printf( " ok\n" );
299
300 /*
301 * 6. Read the HTTP Request
302 */
303 printf( " < Read from client:" );
304 fflush( stdout );
305
306 do
307 {
308 len = sizeof( buf ) - 1;
309 memset( buf, 0, sizeof( buf ) );
310 ret = ssl_read( &ssl, buf, len );
311
312 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
313 continue;
314
315 if( ret <= 0 )
316 {
317 switch( ret )
318 {
319 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
320 printf( " connection was closed gracefully\n" );
321 break;
322
323 case POLARSSL_ERR_NET_CONN_RESET:
324 printf( " connection was reset by peer\n" );
325 break;
326
327 default:
328 printf( " ssl_read returned %d\n", ret );
329 break;
330 }
331
332 break;
333 }
334
335 len = ret;
336 printf( " %d bytes read\n\n%s", len, (char *) buf );
337 }
338 while( 0 );
339
340 /*
341 * 7. Write the 200 Response
342 */
343 printf( " > Write to client:" );
344 fflush( stdout );
345
346 len = sprintf( (char *) buf, HTTP_RESPONSE,
347 ssl_get_ciphersuite( &ssl ) );
348
Paul Bakker030decd2014-04-17 16:03:23 +0200349 while( cnt++ < 100 )
Paul Bakker896ac222011-05-20 12:33:05 +0000350 {
351 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
352 {
353 if( ret == POLARSSL_ERR_NET_CONN_RESET )
354 {
355 printf( " failed\n ! peer closed the connection\n\n" );
356 goto exit;
357 }
358
359 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
360 {
361 printf( " failed\n ! ssl_write returned %d\n\n", ret );
362 goto exit;
363 }
364 }
365 len = ret;
366 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
367
368 m_sleep( 1000 );
369 }
370
371 ssl_close_notify( &ssl );
372 goto exit;
373 }
374
375exit:
376
Paul Bakker0c226102014-04-17 16:02:36 +0200377 if( client_fd != -1 )
378 net_close( client_fd );
379
Paul Bakker36713e82013-09-17 13:25:29 +0200380 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200381 pk_free( &pkey );
Paul Bakker896ac222011-05-20 12:33:05 +0000382 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +0200383 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200384 entropy_free( &entropy );
Paul Bakker896ac222011-05-20 12:33:05 +0000385
Paul Bakkercce9d772011-11-18 14:26:47 +0000386#if defined(_WIN32)
Paul Bakker896ac222011-05-20 12:33:05 +0000387 printf( " Press Enter to exit this program.\n" );
388 fflush( stdout ); getchar();
389#endif
390
391 return( ret );
392}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000393#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000394 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000395 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */