blob: 9e3045113ac95dfb45ec0765d8abe14f00ad8eca [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 Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, 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
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
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>
37#include <unistd.h>
38#include <signal.h>
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;
97 int client_fd;
98 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 Bakker896ac222011-05-20 12:33:05 +0000110 signal( SIGCHLD, SIG_IGN );
111
112 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000113 * 0. Initial seeding of the RNG
114 */
115 printf( "\n . Initial seeding of the random generator..." );
116 fflush( stdout );
117
118 entropy_init( &entropy );
119 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkere0225e42013-06-06 12:52:24 +0200120 (const unsigned char *) pers,
121 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000122 {
123 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
124 goto exit;
125 }
126
127 printf( " ok\n" );
128
129 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000130 * 1. Load the certificates and private RSA key
131 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000132 printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000133 fflush( stdout );
134
135 memset( &srvcert, 0, sizeof( x509_cert ) );
136
137 /*
138 * This demonstration program uses embedded test certificates.
139 * Instead, you may want to use x509parse_crtfile() to read the
140 * server and CA certificates, as well as x509parse_keyfile().
141 */
Paul Bakkere0225e42013-06-06 12:52:24 +0200142 ret = x509parse_crt( &srvcert, (const unsigned char *) test_srv_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000143 strlen( test_srv_crt ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000144 if( ret != 0 )
145 {
146 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
147 goto exit;
148 }
149
Paul Bakkere0225e42013-06-06 12:52:24 +0200150 ret = x509parse_crt( &srvcert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000151 strlen( test_ca_crt ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000152 if( ret != 0 )
153 {
154 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
155 goto exit;
156 }
157
158 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakkere0225e42013-06-06 12:52:24 +0200159 ret = x509parse_key( &rsa, (const unsigned char *) test_srv_key,
Paul Bakker896ac222011-05-20 12:33:05 +0000160 strlen( test_srv_key ), NULL, 0 );
161 if( ret != 0 )
162 {
163 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
164 goto exit;
165 }
166
167 printf( " ok\n" );
168
169 /*
170 * 2. Setup the listening TCP socket
171 */
172 printf( " . Bind on https://localhost:4433/ ..." );
173 fflush( stdout );
174
175 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
176 {
177 printf( " failed\n ! net_bind returned %d\n\n", ret );
178 goto exit;
179 }
180
181 printf( " ok\n" );
182
183 while( 1 )
184 {
185 /*
186 * 3. Wait until a client connects
187 */
188 client_fd = -1;
189 memset( &ssl, 0, sizeof( ssl ) );
190
191 printf( " . Waiting for a remote connection ..." );
192 fflush( stdout );
193
194 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
195 {
196 printf( " failed\n ! net_accept returned %d\n\n", ret );
197 goto exit;
198 }
199
200 printf( " ok\n" );
201
202 /*
203 * 3.5. Forking server thread
204 */
205
206 pid = fork();
207
208 printf( " . Forking to handle connection ..." );
209 fflush( stdout );
210
211 if( pid < 0 )
212 {
213 printf(" failed\n ! fork returned %d\n\n", pid );
214 goto exit;
215 }
216
217 printf( " ok\n" );
218
219 if( pid != 0 )
220 {
Paul Bakker508ad5a2011-12-04 17:09:26 +0000221 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
Paul Bakkere0225e42013-06-06 12:52:24 +0200222 (const unsigned char *) "parent",
223 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000224 {
225 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
226 goto exit;
227 }
228
Paul Bakker896ac222011-05-20 12:33:05 +0000229 close( client_fd );
230 continue;
231 }
232
233 close( listen_fd );
234
235 /*
236 * 4. Setup stuff
237 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000238 printf( " . Setting up the SSL data...." );
Paul Bakker896ac222011-05-20 12:33:05 +0000239 fflush( stdout );
240
Paul Bakker508ad5a2011-12-04 17:09:26 +0000241 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
Paul Bakkere0225e42013-06-06 12:52:24 +0200242 (const unsigned char *) "child",
243 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000244 {
245 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
246 goto exit;
247 }
248
Paul Bakker896ac222011-05-20 12:33:05 +0000249 if( ( ret = ssl_init( &ssl ) ) != 0 )
250 {
251 printf( " failed\n ! ssl_init returned %d\n\n", ret );
252 goto exit;
253 }
254
255 printf( " ok\n" );
256
257 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
258 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
259
Paul Bakker508ad5a2011-12-04 17:09:26 +0000260 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker896ac222011-05-20 12:33:05 +0000261 ssl_set_dbg( &ssl, my_debug, stdout );
262 ssl_set_bio( &ssl, net_recv, &client_fd,
263 net_send, &client_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000264
Paul Bakker896ac222011-05-20 12:33:05 +0000265 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
266 ssl_set_own_cert( &ssl, &srvcert, &rsa );
Paul Bakker896ac222011-05-20 12:33:05 +0000267
268 /*
269 * 5. Handshake
270 */
271 printf( " . Performing the SSL/TLS handshake..." );
272 fflush( stdout );
273
274 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
275 {
276 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
277 {
278 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
279 goto exit;
280 }
281 }
282
283 printf( " ok\n" );
284
285 /*
286 * 6. Read the HTTP Request
287 */
288 printf( " < Read from client:" );
289 fflush( stdout );
290
291 do
292 {
293 len = sizeof( buf ) - 1;
294 memset( buf, 0, sizeof( buf ) );
295 ret = ssl_read( &ssl, buf, len );
296
297 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
298 continue;
299
300 if( ret <= 0 )
301 {
302 switch( ret )
303 {
304 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
305 printf( " connection was closed gracefully\n" );
306 break;
307
308 case POLARSSL_ERR_NET_CONN_RESET:
309 printf( " connection was reset by peer\n" );
310 break;
311
312 default:
313 printf( " ssl_read returned %d\n", ret );
314 break;
315 }
316
317 break;
318 }
319
320 len = ret;
321 printf( " %d bytes read\n\n%s", len, (char *) buf );
322 }
323 while( 0 );
324
325 /*
326 * 7. Write the 200 Response
327 */
328 printf( " > Write to client:" );
329 fflush( stdout );
330
331 len = sprintf( (char *) buf, HTTP_RESPONSE,
332 ssl_get_ciphersuite( &ssl ) );
333
334 while( cnt < 100 )
335 {
336 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
337 {
338 if( ret == POLARSSL_ERR_NET_CONN_RESET )
339 {
340 printf( " failed\n ! peer closed the connection\n\n" );
341 goto exit;
342 }
343
344 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
345 {
346 printf( " failed\n ! ssl_write returned %d\n\n", ret );
347 goto exit;
348 }
349 }
350 len = ret;
351 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
352
353 m_sleep( 1000 );
354 }
355
356 ssl_close_notify( &ssl );
357 goto exit;
358 }
359
360exit:
361
362 net_close( client_fd );
363 x509_free( &srvcert );
364 rsa_free( &rsa );
365 ssl_free( &ssl );
366
Paul Bakkercce9d772011-11-18 14:26:47 +0000367#if defined(_WIN32)
Paul Bakker896ac222011-05-20 12:33:05 +0000368 printf( " Press Enter to exit this program.\n" );
369 fflush( stdout ); getchar();
370#endif
371
372 return( ret );
373}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000374#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000375 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000376 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */