blob: e5695e8627c56ce80090a1fea16fa3ea7a51f4bd [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é-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakker896ac222011-05-20 12:33:05 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
Paul Bakker896ac222011-05-20 12:33:05 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020025#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#else
27#include POLARSSL_CONFIG_FILE
28#endif
Paul Bakker896ac222011-05-20 12:33:05 +000029
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>
Paul Bakker896ac222011-05-20 12:33:05 +000037#include <signal.h>
38
Paul Bakkerfdda7852013-11-30 15:15:31 +010039#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
40#include <unistd.h>
41#endif
42
Paul Bakker508ad5a2011-12-04 17:09:26 +000043#include "polarssl/entropy.h"
44#include "polarssl/ctr_drbg.h"
Paul Bakker896ac222011-05-20 12:33:05 +000045#include "polarssl/certs.h"
46#include "polarssl/x509.h"
47#include "polarssl/ssl.h"
48#include "polarssl/net.h"
49#include "polarssl/timing.h"
50
51#define HTTP_RESPONSE \
52 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000053 "<h2>mbed TLS Test Server</h2>\r\n" \
Paul Bakker896ac222011-05-20 12:33:05 +000054 "<p>Successful connection using: %s</p>\r\n"
55
Paul Bakkerb892b132011-10-12 09:19:43 +000056#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000057 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \
Paul Bakkerb892b132011-10-12 09:19:43 +000058 !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \
Paul Bakkered27a042013-04-18 22:46:23 +020059 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020060 !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_TIMING_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000061int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000062{
Paul Bakkercce9d772011-11-18 14:26:47 +000063 ((void) argc);
64 ((void) argv);
65
Paul Bakker508ad5a2011-12-04 17:09:26 +000066 printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C "
Paul Bakkerb892b132011-10-12 09:19:43 +000067 "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000068 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020069 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C and/or "
Paul Bakkerfa9b1002013-07-03 15:31:03 +020070 "POLARSSL_TIMING_C not defined.\n");
Paul Bakkerb892b132011-10-12 09:19:43 +000071 return( 0 );
72}
Paul Bakkercce9d772011-11-18 14:26:47 +000073#elif defined(_WIN32)
74int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000075{
Paul Bakkercce9d772011-11-18 14:26:47 +000076 ((void) argc);
77 ((void) argv);
78
79 printf("_WIN32 defined. This application requires fork() and signals "
Paul Bakkerb892b132011-10-12 09:19:43 +000080 "to work correctly.\n");
81 return( 0 );
82}
83#else
Paul Bakker896ac222011-05-20 12:33:05 +000084
85#define DEBUG_LEVEL 0
86
Paul Bakker3c5ef712013-06-25 16:37:45 +020087static void my_debug( void *ctx, int level, const char *str )
Paul Bakker896ac222011-05-20 12:33:05 +000088{
89 if( level < DEBUG_LEVEL )
90 {
91 fprintf( (FILE *) ctx, "%s", str );
92 fflush( (FILE *) ctx );
93 }
94}
95
Paul Bakkercce9d772011-11-18 14:26:47 +000096int main( int argc, char *argv[] )
Paul Bakker896ac222011-05-20 12:33:05 +000097{
98 int ret, len, cnt = 0, pid;
99 int listen_fd;
Manuel Pégourié-Gonnard68821da2013-09-16 12:34:33 +0200100 int client_fd = -1;
Paul Bakker896ac222011-05-20 12:33:05 +0000101 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200102 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +0000103
Paul Bakker508ad5a2011-12-04 17:09:26 +0000104 entropy_context entropy;
105 ctr_drbg_context ctr_drbg;
Paul Bakker896ac222011-05-20 12:33:05 +0000106 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200107 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200108 pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +0000109
Paul Bakkercce9d772011-11-18 14:26:47 +0000110 ((void) argc);
111 ((void) argv);
112
Paul Bakker0c226102014-04-17 16:02:36 +0200113 memset( &ssl, 0, sizeof(ssl_context) );
114
115 entropy_init( &entropy );
116 pk_init( &pkey );
117 x509_crt_init( &srvcert );
118
Paul Bakker896ac222011-05-20 12:33:05 +0000119 signal( SIGCHLD, SIG_IGN );
120
121 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000122 * 0. Initial seeding of the RNG
123 */
124 printf( "\n . Initial seeding of the random generator..." );
125 fflush( stdout );
126
Paul Bakker508ad5a2011-12-04 17:09:26 +0000127 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200128 (const unsigned char *) pers,
129 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000130 {
131 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
132 goto exit;
133 }
134
135 printf( " ok\n" );
136
137 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000138 * 1. Load the certificates and private RSA key
139 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000140 printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000141 fflush( stdout );
142
Paul Bakker896ac222011-05-20 12:33:05 +0000143 /*
144 * This demonstration program uses embedded test certificates.
Paul Bakkerddf26b42013-09-18 13:46:23 +0200145 * Instead, you may want to use x509_crt_parse_file() to read the
146 * server and CA certificates, as well as pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000147 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200148 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt,
149 strlen( test_srv_crt ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000150 if( ret != 0 )
151 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200152 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000153 goto exit;
154 }
155
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200156 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_ca_list,
157 strlen( test_ca_list ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000158 if( ret != 0 )
159 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200160 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000161 goto exit;
162 }
163
Paul Bakker1a7550a2013-09-15 13:01:22 +0200164 ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key,
Paul Bakker896ac222011-05-20 12:33:05 +0000165 strlen( test_srv_key ), NULL, 0 );
166 if( ret != 0 )
167 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200168 printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000169 goto exit;
170 }
171
172 printf( " ok\n" );
173
174 /*
175 * 2. Setup the listening TCP socket
176 */
177 printf( " . Bind on https://localhost:4433/ ..." );
178 fflush( stdout );
179
180 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
181 {
182 printf( " failed\n ! net_bind returned %d\n\n", ret );
183 goto exit;
184 }
185
186 printf( " ok\n" );
187
188 while( 1 )
189 {
190 /*
191 * 3. Wait until a client connects
192 */
193 client_fd = -1;
194 memset( &ssl, 0, sizeof( ssl ) );
195
196 printf( " . Waiting for a remote connection ..." );
197 fflush( stdout );
198
199 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
200 {
201 printf( " failed\n ! net_accept returned %d\n\n", ret );
202 goto exit;
203 }
204
205 printf( " ok\n" );
206
207 /*
208 * 3.5. Forking server thread
209 */
210
211 pid = fork();
212
213 printf( " . Forking to handle connection ..." );
214 fflush( stdout );
215
216 if( pid < 0 )
217 {
218 printf(" failed\n ! fork returned %d\n\n", pid );
219 goto exit;
220 }
221
222 printf( " ok\n" );
223
224 if( pid != 0 )
225 {
Paul Bakker508ad5a2011-12-04 17:09:26 +0000226 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200227 (const unsigned char *) "parent",
228 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000229 {
230 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
231 goto exit;
232 }
233
Paul Bakker896ac222011-05-20 12:33:05 +0000234 close( client_fd );
235 continue;
236 }
237
238 close( listen_fd );
239
240 /*
241 * 4. Setup stuff
242 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000243 printf( " . Setting up the SSL data...." );
Paul Bakker896ac222011-05-20 12:33:05 +0000244 fflush( stdout );
245
Paul Bakker508ad5a2011-12-04 17:09:26 +0000246 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200247 (const unsigned char *) "child",
248 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000249 {
250 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
251 goto exit;
252 }
Paul Bakker0c226102014-04-17 16:02:36 +0200253
Paul Bakker896ac222011-05-20 12:33:05 +0000254 if( ( ret = ssl_init( &ssl ) ) != 0 )
255 {
256 printf( " failed\n ! ssl_init returned %d\n\n", ret );
257 goto exit;
258 }
259
260 printf( " ok\n" );
261
262 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
263 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
264
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100265 /* SSLv3 is deprecated, set minimum to TLS 1.0 */
266 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3,
267 SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnardfa065812015-01-12 14:05:33 +0100268 /* RC4 is deprecated, disable it */
269 ssl_set_arc4_support( &ssl, SSL_ARC4_DISABLED );
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100270
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 */