blob: 02de364ba49a474bd36bbaec4937bbe2efcec361 [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>
35#include <unistd.h>
36#include <signal.h>
37
Paul Bakker508ad5a2011-12-04 17:09:26 +000038#include "polarssl/entropy.h"
39#include "polarssl/ctr_drbg.h"
Paul Bakker896ac222011-05-20 12:33:05 +000040#include "polarssl/certs.h"
41#include "polarssl/x509.h"
42#include "polarssl/ssl.h"
43#include "polarssl/net.h"
44#include "polarssl/timing.h"
45
46#define HTTP_RESPONSE \
47 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
48 "<h2>PolarSSL Test Server</h2>\r\n" \
49 "<p>Successful connection using: %s</p>\r\n"
50
Paul Bakkerb892b132011-10-12 09:19:43 +000051#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000052 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \
Paul Bakkerb892b132011-10-12 09:19:43 +000053 !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \
Paul Bakkered27a042013-04-18 22:46:23 +020054 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020055 !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_TIMING_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000056int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000057{
Paul Bakkercce9d772011-11-18 14:26:47 +000058 ((void) argc);
59 ((void) argv);
60
Paul Bakker508ad5a2011-12-04 17:09:26 +000061 printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C "
Paul Bakkerb892b132011-10-12 09:19:43 +000062 "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000063 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020064 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C and/or "
Paul Bakkerfa9b1002013-07-03 15:31:03 +020065 "POLARSSL_TIMING_C not defined.\n");
Paul Bakkerb892b132011-10-12 09:19:43 +000066 return( 0 );
67}
Paul Bakkercce9d772011-11-18 14:26:47 +000068#elif defined(_WIN32)
69int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000070{
Paul Bakkercce9d772011-11-18 14:26:47 +000071 ((void) argc);
72 ((void) argv);
73
74 printf("_WIN32 defined. This application requires fork() and signals "
Paul Bakkerb892b132011-10-12 09:19:43 +000075 "to work correctly.\n");
76 return( 0 );
77}
78#else
Paul Bakker896ac222011-05-20 12:33:05 +000079
80#define DEBUG_LEVEL 0
81
Paul Bakker3c5ef712013-06-25 16:37:45 +020082static void my_debug( void *ctx, int level, const char *str )
Paul Bakker896ac222011-05-20 12:33:05 +000083{
84 if( level < DEBUG_LEVEL )
85 {
86 fprintf( (FILE *) ctx, "%s", str );
87 fflush( (FILE *) ctx );
88 }
89}
90
Paul Bakkercce9d772011-11-18 14:26:47 +000091int main( int argc, char *argv[] )
Paul Bakker896ac222011-05-20 12:33:05 +000092{
93 int ret, len, cnt = 0, pid;
94 int listen_fd;
Manuel Pégourié-Gonnard68821da2013-09-16 12:34:33 +020095 int client_fd = -1;
Paul Bakker896ac222011-05-20 12:33:05 +000096 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020097 const char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +000098
Paul Bakker508ad5a2011-12-04 17:09:26 +000099 entropy_context entropy;
100 ctr_drbg_context ctr_drbg;
Paul Bakker896ac222011-05-20 12:33:05 +0000101 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200102 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200103 pk_context pkey;
Paul Bakker896ac222011-05-20 12:33:05 +0000104
Paul Bakkercce9d772011-11-18 14:26:47 +0000105 ((void) argc);
106 ((void) argv);
107
Paul Bakker896ac222011-05-20 12:33:05 +0000108 signal( SIGCHLD, SIG_IGN );
109
110 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000111 * 0. Initial seeding of the RNG
112 */
113 printf( "\n . Initial seeding of the random generator..." );
114 fflush( stdout );
115
116 entropy_init( &entropy );
117 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200118 (const unsigned char *) pers,
119 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000120 {
121 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
122 goto exit;
123 }
124
125 printf( " ok\n" );
126
127 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000128 * 1. Load the certificates and private RSA key
129 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000130 printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000131 fflush( stdout );
132
Paul Bakker369d2eb2013-09-18 11:58:25 +0200133 x509_crt_init( &srvcert );
Paul Bakker896ac222011-05-20 12:33:05 +0000134
135 /*
136 * This demonstration program uses embedded test certificates.
Paul Bakkerddf26b42013-09-18 13:46:23 +0200137 * Instead, you may want to use x509_crt_parse_file() to read the
138 * server and CA certificates, as well as pk_parse_keyfile().
Paul Bakker896ac222011-05-20 12:33:05 +0000139 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200140 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt,
141 strlen( test_srv_crt ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000142 if( ret != 0 )
143 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200144 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000145 goto exit;
146 }
147
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200148 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_ca_list,
149 strlen( test_ca_list ) );
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é-Gonnardac755232013-08-19 14:10:16 +0200156 pk_init( &pkey );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200157 ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key,
Paul Bakker896ac222011-05-20 12:33:05 +0000158 strlen( test_srv_key ), NULL, 0 );
159 if( ret != 0 )
160 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200161 printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
Paul Bakker896ac222011-05-20 12:33:05 +0000162 goto exit;
163 }
164
165 printf( " ok\n" );
166
167 /*
168 * 2. Setup the listening TCP socket
169 */
170 printf( " . Bind on https://localhost:4433/ ..." );
171 fflush( stdout );
172
173 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
174 {
175 printf( " failed\n ! net_bind returned %d\n\n", ret );
176 goto exit;
177 }
178
179 printf( " ok\n" );
180
181 while( 1 )
182 {
183 /*
184 * 3. Wait until a client connects
185 */
186 client_fd = -1;
187 memset( &ssl, 0, sizeof( ssl ) );
188
189 printf( " . Waiting for a remote connection ..." );
190 fflush( stdout );
191
192 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
193 {
194 printf( " failed\n ! net_accept returned %d\n\n", ret );
195 goto exit;
196 }
197
198 printf( " ok\n" );
199
200 /*
201 * 3.5. Forking server thread
202 */
203
204 pid = fork();
205
206 printf( " . Forking to handle connection ..." );
207 fflush( stdout );
208
209 if( pid < 0 )
210 {
211 printf(" failed\n ! fork returned %d\n\n", pid );
212 goto exit;
213 }
214
215 printf( " ok\n" );
216
217 if( pid != 0 )
218 {
Paul Bakker508ad5a2011-12-04 17:09:26 +0000219 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200220 (const unsigned char *) "parent",
221 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000222 {
223 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
224 goto exit;
225 }
226
Paul Bakker896ac222011-05-20 12:33:05 +0000227 close( client_fd );
228 continue;
229 }
230
231 close( listen_fd );
232
233 /*
234 * 4. Setup stuff
235 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000236 printf( " . Setting up the SSL data...." );
Paul Bakker896ac222011-05-20 12:33:05 +0000237 fflush( stdout );
238
Paul Bakker508ad5a2011-12-04 17:09:26 +0000239 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200240 (const unsigned char *) "child",
241 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000242 {
243 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
244 goto exit;
245 }
246
Paul Bakker896ac222011-05-20 12:33:05 +0000247 if( ( ret = ssl_init( &ssl ) ) != 0 )
248 {
249 printf( " failed\n ! ssl_init returned %d\n\n", ret );
250 goto exit;
251 }
252
253 printf( " ok\n" );
254
255 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
256 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
257
Paul Bakker508ad5a2011-12-04 17:09:26 +0000258 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker896ac222011-05-20 12:33:05 +0000259 ssl_set_dbg( &ssl, my_debug, stdout );
260 ssl_set_bio( &ssl, net_recv, &client_fd,
261 net_send, &client_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000262
Paul Bakker896ac222011-05-20 12:33:05 +0000263 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200264 ssl_set_own_cert( &ssl, &srvcert, &pkey );
Paul Bakker896ac222011-05-20 12:33:05 +0000265
266 /*
267 * 5. Handshake
268 */
269 printf( " . Performing the SSL/TLS handshake..." );
270 fflush( stdout );
271
272 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
273 {
274 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
275 {
276 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
277 goto exit;
278 }
279 }
280
281 printf( " ok\n" );
282
283 /*
284 * 6. Read the HTTP Request
285 */
286 printf( " < Read from client:" );
287 fflush( stdout );
288
289 do
290 {
291 len = sizeof( buf ) - 1;
292 memset( buf, 0, sizeof( buf ) );
293 ret = ssl_read( &ssl, buf, len );
294
295 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
296 continue;
297
298 if( ret <= 0 )
299 {
300 switch( ret )
301 {
302 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
303 printf( " connection was closed gracefully\n" );
304 break;
305
306 case POLARSSL_ERR_NET_CONN_RESET:
307 printf( " connection was reset by peer\n" );
308 break;
309
310 default:
311 printf( " ssl_read returned %d\n", ret );
312 break;
313 }
314
315 break;
316 }
317
318 len = ret;
319 printf( " %d bytes read\n\n%s", len, (char *) buf );
320 }
321 while( 0 );
322
323 /*
324 * 7. Write the 200 Response
325 */
326 printf( " > Write to client:" );
327 fflush( stdout );
328
329 len = sprintf( (char *) buf, HTTP_RESPONSE,
330 ssl_get_ciphersuite( &ssl ) );
331
332 while( cnt < 100 )
333 {
334 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
335 {
336 if( ret == POLARSSL_ERR_NET_CONN_RESET )
337 {
338 printf( " failed\n ! peer closed the connection\n\n" );
339 goto exit;
340 }
341
342 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
343 {
344 printf( " failed\n ! ssl_write returned %d\n\n", ret );
345 goto exit;
346 }
347 }
348 len = ret;
349 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
350
351 m_sleep( 1000 );
352 }
353
354 ssl_close_notify( &ssl );
355 goto exit;
356 }
357
358exit:
359
360 net_close( client_fd );
Paul Bakker36713e82013-09-17 13:25:29 +0200361 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200362 pk_free( &pkey );
Paul Bakker896ac222011-05-20 12:33:05 +0000363 ssl_free( &ssl );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200364 entropy_free( &entropy );
Paul Bakker896ac222011-05-20 12:33:05 +0000365
Paul Bakkercce9d772011-11-18 14:26:47 +0000366#if defined(_WIN32)
Paul Bakker896ac222011-05-20 12:33:05 +0000367 printf( " Press Enter to exit this program.\n" );
368 fflush( stdout ); getchar();
369#endif
370
371 return( ret );
372}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000373#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000374 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000375 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */