blob: ac90e46788215fae52d88542c29c24df991f6e3e [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é-Gonnard0edee5e2015-01-26 15:29:40 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakker896ac222011-05-20 12:33:05 +00005 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakker896ac222011-05-20 12:33:05 +00007 *
8 * 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
23#ifndef _CRT_SECURE_NO_DEPRECATE
24#define _CRT_SECURE_NO_DEPRECATE 1
25#endif
26
Paul Bakkercce9d772011-11-18 14:26:47 +000027#if defined(_WIN32)
Paul Bakker896ac222011-05-20 12:33:05 +000028#include <windows.h>
29#endif
30
31#include <string.h>
32#include <stdlib.h>
33#include <stdio.h>
Paul Bakker896ac222011-05-20 12:33:05 +000034#include <signal.h>
35
Paul Bakker238be3a2014-07-07 14:55:07 +020036#if !defined(_MSC_VER)
37#include <unistd.h>
38#endif
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 Bakker508ad5a2011-12-04 17:09:26 +000058 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_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 "
67 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakkerb892b132011-10-12 09:19:43 +000068 return( 0 );
69}
Paul Bakkercce9d772011-11-18 14:26:47 +000070#elif defined(_WIN32)
71int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000072{
Paul Bakkercce9d772011-11-18 14:26:47 +000073 ((void) argc);
74 ((void) argv);
75
76 printf("_WIN32 defined. This application requires fork() and signals "
Paul Bakkerb892b132011-10-12 09:19:43 +000077 "to work correctly.\n");
78 return( 0 );
79}
80#else
Paul Bakker896ac222011-05-20 12:33:05 +000081
82#define DEBUG_LEVEL 0
83
84void my_debug( void *ctx, int level, const char *str )
85{
86 if( level < DEBUG_LEVEL )
87 {
88 fprintf( (FILE *) ctx, "%s", str );
89 fflush( (FILE *) ctx );
90 }
91}
92
Paul Bakkercce9d772011-11-18 14:26:47 +000093int main( int argc, char *argv[] )
Paul Bakker896ac222011-05-20 12:33:05 +000094{
95 int ret, len, cnt = 0, pid;
96 int listen_fd;
Paul Bakkera16e7f22014-07-09 14:58:11 +020097 int client_fd = -1;
Paul Bakker896ac222011-05-20 12:33:05 +000098 unsigned char buf[1024];
Paul Bakkere0225e42013-06-06 12:52:24 +020099 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +0000100
Paul Bakker508ad5a2011-12-04 17:09:26 +0000101 entropy_context entropy;
102 ctr_drbg_context ctr_drbg;
Paul Bakker896ac222011-05-20 12:33:05 +0000103 ssl_context ssl;
Paul Bakker896ac222011-05-20 12:33:05 +0000104 x509_cert srvcert;
105 rsa_context rsa;
106
Paul Bakkercce9d772011-11-18 14:26:47 +0000107 ((void) argc);
108 ((void) argv);
109
Paul Bakker39148402014-04-17 16:02:36 +0200110 memset( &ssl, 0, sizeof(ssl_context) );
111
112 entropy_init( &entropy );
113 rsa_init( &rsa, RSA_PKCS_V15, 0 );
114 memset( &srvcert, 0, sizeof( x509_cert ) );
115
Paul Bakker896ac222011-05-20 12:33:05 +0000116 signal( SIGCHLD, SIG_IGN );
117
118 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000119 * 0. Initial seeding of the RNG
120 */
121 printf( "\n . Initial seeding of the random generator..." );
122 fflush( stdout );
123
Paul Bakker508ad5a2011-12-04 17:09:26 +0000124 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkere0225e42013-06-06 12:52:24 +0200125 (const unsigned char *) pers,
126 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000127 {
128 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
129 goto exit;
130 }
131
132 printf( " ok\n" );
133
134 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000135 * 1. Load the certificates and private RSA key
136 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000137 printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000138 fflush( stdout );
139
Paul Bakker896ac222011-05-20 12:33:05 +0000140 /*
141 * This demonstration program uses embedded test certificates.
142 * Instead, you may want to use x509parse_crtfile() to read the
143 * server and CA certificates, as well as x509parse_keyfile().
144 */
Paul Bakkere0225e42013-06-06 12:52:24 +0200145 ret = x509parse_crt( &srvcert, (const unsigned char *) test_srv_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000146 strlen( test_srv_crt ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000147 if( ret != 0 )
148 {
149 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
150 goto exit;
151 }
152
Paul Bakkere0225e42013-06-06 12:52:24 +0200153 ret = x509parse_crt( &srvcert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000154 strlen( test_ca_crt ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000155 if( ret != 0 )
156 {
157 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
158 goto exit;
159 }
160
Paul Bakkere0225e42013-06-06 12:52:24 +0200161 ret = x509parse_key( &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 {
165 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
166 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 Bakkere0225e42013-06-06 12:52:24 +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 Bakkere0225e42013-06-06 12:52:24 +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 }
Paul Bakker39148402014-04-17 16:02:36 +0200250
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
Paul Bakkera2eabad2014-04-17 16:03:23 +0200336 while( cnt++ < 100 )
Paul Bakker896ac222011-05-20 12:33:05 +0000337 {
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
Paul Bakker39148402014-04-17 16:02:36 +0200364 if( client_fd != -1 )
365 net_close( client_fd );
366
Paul Bakker896ac222011-05-20 12:33:05 +0000367 x509_free( &srvcert );
368 rsa_free( &rsa );
369 ssl_free( &ssl );
370
Paul Bakkercce9d772011-11-18 14:26:47 +0000371#if defined(_WIN32)
Paul Bakker896ac222011-05-20 12:33:05 +0000372 printf( " Press Enter to exit this program.\n" );
373 fflush( stdout ); getchar();
374#endif
375
376 return( ret );
377}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000378#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000379 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000380 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */