blob: cd11f1b9da7db73b9296492ab0e17f24877faa12 [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 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakker896ac222011-05-20 12:33:05 +00005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakker896ac222011-05-20 12:33:05 +00007 *
Paul Bakker896ac222011-05-20 12:33:05 +00008 * 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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker896ac222011-05-20 12:33:05 +000028
Paul Bakkercce9d772011-11-18 14:26:47 +000029#if defined(_WIN32)
Paul Bakker896ac222011-05-20 12:33:05 +000030#include <windows.h>
31#endif
32
33#include <string.h>
34#include <stdlib.h>
35#include <stdio.h>
Paul Bakker896ac222011-05-20 12:33:05 +000036#include <signal.h>
37
Paul Bakkerfdda7852013-11-30 15:15:31 +010038#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
39#include <unistd.h>
40#endif
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" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000052 "<h2>mbed TLS Test Server</h2>\r\n" \
Paul Bakker896ac222011-05-20 12:33:05 +000053 "<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 Bakker36713e82013-09-17 13:25:29 +020059 !defined(POLARSSL_X509_CRT_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 Bakker36713e82013-09-17 13:25:29 +020068 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C and/or "
Paul Bakkerfa9b1002013-07-03 15:31:03 +020069 "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;
Manuel Pégourié-Gonnard68821da2013-09-16 12:34:33 +020099 int client_fd = -1;
Paul Bakker896ac222011-05-20 12:33:05 +0000100 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 Bakkerc559c7a2013-09-18 14:13:26 +0200106 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200107 pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +0000108
Paul Bakkercce9d772011-11-18 14:26:47 +0000109 ((void) argc);
110 ((void) argv);
111
Paul Bakker0c226102014-04-17 16:02:36 +0200112 memset( &ssl, 0, sizeof(ssl_context) );
113
114 entropy_init( &entropy );
115 pk_init( &pkey );
116 x509_crt_init( &srvcert );
117
Paul Bakker896ac222011-05-20 12:33:05 +0000118 signal( SIGCHLD, SIG_IGN );
119
120 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000121 * 0. Initial seeding of the RNG
122 */
123 printf( "\n . Initial seeding of the random generator..." );
124 fflush( stdout );
125
Paul Bakker508ad5a2011-12-04 17:09:26 +0000126 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200127 (const unsigned char *) pers,
128 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000129 {
130 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
131 goto exit;
132 }
133
134 printf( " ok\n" );
135
136 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000137 * 1. Load the certificates and private RSA key
138 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000139 printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000140 fflush( stdout );
141
Paul Bakker896ac222011-05-20 12:33:05 +0000142 /*
143 * This demonstration program uses embedded test certificates.
Paul Bakkerddf26b42013-09-18 13:46:23 +0200144 * Instead, you may want to use x509_crt_parse_file() to read the
145 * server and CA certificates, as well as pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000146 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200147 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt,
148 strlen( test_srv_crt ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000149 if( ret != 0 )
150 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200151 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000152 goto exit;
153 }
154
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200155 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_ca_list,
156 strlen( test_ca_list ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000157 if( ret != 0 )
158 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200159 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000160 goto exit;
161 }
162
Paul Bakker1a7550a2013-09-15 13:01:22 +0200163 ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key,
Paul Bakker896ac222011-05-20 12:33:05 +0000164 strlen( test_srv_key ), NULL, 0 );
165 if( ret != 0 )
166 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200167 printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000168 goto exit;
169 }
170
171 printf( " ok\n" );
172
173 /*
174 * 2. Setup the listening TCP socket
175 */
176 printf( " . Bind on https://localhost:4433/ ..." );
177 fflush( stdout );
178
179 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
180 {
181 printf( " failed\n ! net_bind returned %d\n\n", ret );
182 goto exit;
183 }
184
185 printf( " ok\n" );
186
187 while( 1 )
188 {
189 /*
190 * 3. Wait until a client connects
191 */
192 client_fd = -1;
193 memset( &ssl, 0, sizeof( ssl ) );
194
195 printf( " . Waiting for a remote connection ..." );
196 fflush( stdout );
197
198 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
199 {
200 printf( " failed\n ! net_accept returned %d\n\n", ret );
201 goto exit;
202 }
203
204 printf( " ok\n" );
205
206 /*
207 * 3.5. Forking server thread
208 */
209
210 pid = fork();
211
212 printf( " . Forking to handle connection ..." );
213 fflush( stdout );
214
215 if( pid < 0 )
216 {
217 printf(" failed\n ! fork returned %d\n\n", pid );
218 goto exit;
219 }
220
221 printf( " ok\n" );
222
223 if( pid != 0 )
224 {
Paul Bakker508ad5a2011-12-04 17:09:26 +0000225 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200226 (const unsigned char *) "parent",
227 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000228 {
229 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
230 goto exit;
231 }
232
Paul Bakker896ac222011-05-20 12:33:05 +0000233 close( client_fd );
234 continue;
235 }
236
237 close( listen_fd );
238
239 /*
240 * 4. Setup stuff
241 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000242 printf( " . Setting up the SSL data...." );
Paul Bakker896ac222011-05-20 12:33:05 +0000243 fflush( stdout );
244
Paul Bakker508ad5a2011-12-04 17:09:26 +0000245 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200246 (const unsigned char *) "child",
247 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000248 {
249 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
250 goto exit;
251 }
Paul Bakker0c226102014-04-17 16:02:36 +0200252
Paul Bakker896ac222011-05-20 12:33:05 +0000253 if( ( ret = ssl_init( &ssl ) ) != 0 )
254 {
255 printf( " failed\n ! ssl_init returned %d\n\n", ret );
256 goto exit;
257 }
258
259 printf( " ok\n" );
260
261 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
262 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
263
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100264 /* SSLv3 is deprecated, set minimum to TLS 1.0 */
265 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3,
266 SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnardfa065812015-01-12 14:05:33 +0100267 /* RC4 is deprecated, disable it */
268 ssl_set_arc4_support( &ssl, SSL_ARC4_DISABLED );
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100269
Paul Bakker508ad5a2011-12-04 17:09:26 +0000270 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker896ac222011-05-20 12:33:05 +0000271 ssl_set_dbg( &ssl, my_debug, stdout );
272 ssl_set_bio( &ssl, net_recv, &client_fd,
273 net_send, &client_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000274
Paul Bakker896ac222011-05-20 12:33:05 +0000275 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200276 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
277 {
278 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
279 goto exit;
280 }
Paul Bakker896ac222011-05-20 12:33:05 +0000281
282 /*
283 * 5. Handshake
284 */
285 printf( " . Performing the SSL/TLS handshake..." );
286 fflush( stdout );
287
288 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
289 {
290 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
291 {
292 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
293 goto exit;
294 }
295 }
296
297 printf( " ok\n" );
298
299 /*
300 * 6. Read the HTTP Request
301 */
302 printf( " < Read from client:" );
303 fflush( stdout );
304
305 do
306 {
307 len = sizeof( buf ) - 1;
308 memset( buf, 0, sizeof( buf ) );
309 ret = ssl_read( &ssl, buf, len );
310
311 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
312 continue;
313
314 if( ret <= 0 )
315 {
316 switch( ret )
317 {
318 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
319 printf( " connection was closed gracefully\n" );
320 break;
321
322 case POLARSSL_ERR_NET_CONN_RESET:
323 printf( " connection was reset by peer\n" );
324 break;
325
326 default:
327 printf( " ssl_read returned %d\n", ret );
328 break;
329 }
330
331 break;
332 }
333
334 len = ret;
335 printf( " %d bytes read\n\n%s", len, (char *) buf );
336 }
337 while( 0 );
338
339 /*
340 * 7. Write the 200 Response
341 */
342 printf( " > Write to client:" );
343 fflush( stdout );
344
345 len = sprintf( (char *) buf, HTTP_RESPONSE,
346 ssl_get_ciphersuite( &ssl ) );
347
Paul Bakker030decd2014-04-17 16:03:23 +0200348 while( cnt++ < 100 )
Paul Bakker896ac222011-05-20 12:33:05 +0000349 {
350 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
351 {
352 if( ret == POLARSSL_ERR_NET_CONN_RESET )
353 {
354 printf( " failed\n ! peer closed the connection\n\n" );
355 goto exit;
356 }
357
358 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
359 {
360 printf( " failed\n ! ssl_write returned %d\n\n", ret );
361 goto exit;
362 }
363 }
364 len = ret;
365 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
366
367 m_sleep( 1000 );
368 }
369
370 ssl_close_notify( &ssl );
371 goto exit;
372 }
373
374exit:
375
Paul Bakker0c226102014-04-17 16:02:36 +0200376 if( client_fd != -1 )
377 net_close( client_fd );
378
Paul Bakker36713e82013-09-17 13:25:29 +0200379 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200380 pk_free( &pkey );
Paul Bakker896ac222011-05-20 12:33:05 +0000381 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +0200382 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200383 entropy_free( &entropy );
Paul Bakker896ac222011-05-20 12:33:05 +0000384
Paul Bakkercce9d772011-11-18 14:26:47 +0000385#if defined(_WIN32)
Paul Bakker896ac222011-05-20 12:33:05 +0000386 printf( " Press Enter to exit this program.\n" );
387 fflush( stdout ); getchar();
388#endif
389
390 return( ret );
391}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000392#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000393 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000394 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */