blob: 76c782b7b738c782ce1bc852b032b5fba917ea1e [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>
Paul Bakker896ac222011-05-20 12:33:05 +000037#include <signal.h>
38
Paul Bakker238be3a2014-07-07 14:55:07 +020039#if !defined(_MSC_VER)
40#include <unistd.h>
41#endif
42
Paul Bakker5690efc2011-05-26 13:16:06 +000043#include "polarssl/config.h"
44
Paul Bakker508ad5a2011-12-04 17:09:26 +000045#include "polarssl/entropy.h"
46#include "polarssl/ctr_drbg.h"
Paul Bakker896ac222011-05-20 12:33:05 +000047#include "polarssl/certs.h"
48#include "polarssl/x509.h"
49#include "polarssl/ssl.h"
50#include "polarssl/net.h"
51#include "polarssl/timing.h"
52
53#define HTTP_RESPONSE \
54 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
55 "<h2>PolarSSL Test Server</h2>\r\n" \
56 "<p>Successful connection using: %s</p>\r\n"
57
Paul Bakkerb892b132011-10-12 09:19:43 +000058#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000059 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \
Paul Bakkerb892b132011-10-12 09:19:43 +000060 !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000061 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000062int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000063{
Paul Bakkercce9d772011-11-18 14:26:47 +000064 ((void) argc);
65 ((void) argv);
66
Paul Bakker508ad5a2011-12-04 17:09:26 +000067 printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C "
Paul Bakkerb892b132011-10-12 09:19:43 +000068 "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000069 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
70 "POLARSSL_CTR_DRBG_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
87void my_debug( void *ctx, int level, const char *str )
88{
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;
100 int client_fd;
101 unsigned char buf[1024];
Paul Bakkere0225e42013-06-06 12:52:24 +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 Bakker896ac222011-05-20 12:33:05 +0000107 x509_cert srvcert;
108 rsa_context rsa;
109
Paul Bakkercce9d772011-11-18 14:26:47 +0000110 ((void) argc);
111 ((void) argv);
112
Paul Bakker896ac222011-05-20 12:33:05 +0000113 signal( SIGCHLD, SIG_IGN );
114
115 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000116 * 0. Initial seeding of the RNG
117 */
118 printf( "\n . Initial seeding of the random generator..." );
119 fflush( stdout );
120
121 entropy_init( &entropy );
122 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkere0225e42013-06-06 12:52:24 +0200123 (const unsigned char *) pers,
124 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000125 {
126 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
127 goto exit;
128 }
129
130 printf( " ok\n" );
131
132 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000133 * 1. Load the certificates and private RSA key
134 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000135 printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000136 fflush( stdout );
137
138 memset( &srvcert, 0, sizeof( x509_cert ) );
139
140 /*
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
161 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakkere0225e42013-06-06 12:52:24 +0200162 ret = x509parse_key( &rsa, (const unsigned char *) test_srv_key,
Paul Bakker896ac222011-05-20 12:33:05 +0000163 strlen( test_srv_key ), NULL, 0 );
164 if( ret != 0 )
165 {
166 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
167 goto exit;
168 }
169
170 printf( " ok\n" );
171
172 /*
173 * 2. Setup the listening TCP socket
174 */
175 printf( " . Bind on https://localhost:4433/ ..." );
176 fflush( stdout );
177
178 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
179 {
180 printf( " failed\n ! net_bind returned %d\n\n", ret );
181 goto exit;
182 }
183
184 printf( " ok\n" );
185
186 while( 1 )
187 {
188 /*
189 * 3. Wait until a client connects
190 */
191 client_fd = -1;
192 memset( &ssl, 0, sizeof( ssl ) );
193
194 printf( " . Waiting for a remote connection ..." );
195 fflush( stdout );
196
197 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
198 {
199 printf( " failed\n ! net_accept returned %d\n\n", ret );
200 goto exit;
201 }
202
203 printf( " ok\n" );
204
205 /*
206 * 3.5. Forking server thread
207 */
208
209 pid = fork();
210
211 printf( " . Forking to handle connection ..." );
212 fflush( stdout );
213
214 if( pid < 0 )
215 {
216 printf(" failed\n ! fork returned %d\n\n", pid );
217 goto exit;
218 }
219
220 printf( " ok\n" );
221
222 if( pid != 0 )
223 {
Paul Bakker508ad5a2011-12-04 17:09:26 +0000224 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
Paul Bakkere0225e42013-06-06 12:52:24 +0200225 (const unsigned char *) "parent",
226 6 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000227 {
228 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
229 goto exit;
230 }
231
Paul Bakker896ac222011-05-20 12:33:05 +0000232 close( client_fd );
233 continue;
234 }
235
236 close( listen_fd );
237
238 /*
239 * 4. Setup stuff
240 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000241 printf( " . Setting up the SSL data...." );
Paul Bakker896ac222011-05-20 12:33:05 +0000242 fflush( stdout );
243
Paul Bakker508ad5a2011-12-04 17:09:26 +0000244 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
Paul Bakkere0225e42013-06-06 12:52:24 +0200245 (const unsigned char *) "child",
246 5 ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000247 {
248 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
249 goto exit;
250 }
251
Paul Bakker896ac222011-05-20 12:33:05 +0000252 if( ( ret = ssl_init( &ssl ) ) != 0 )
253 {
254 printf( " failed\n ! ssl_init returned %d\n\n", ret );
255 goto exit;
256 }
257
258 printf( " ok\n" );
259
260 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
261 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
262
Paul Bakker508ad5a2011-12-04 17:09:26 +0000263 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker896ac222011-05-20 12:33:05 +0000264 ssl_set_dbg( &ssl, my_debug, stdout );
265 ssl_set_bio( &ssl, net_recv, &client_fd,
266 net_send, &client_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000267
Paul Bakker896ac222011-05-20 12:33:05 +0000268 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
269 ssl_set_own_cert( &ssl, &srvcert, &rsa );
Paul Bakker896ac222011-05-20 12:33:05 +0000270
271 /*
272 * 5. Handshake
273 */
274 printf( " . Performing the SSL/TLS handshake..." );
275 fflush( stdout );
276
277 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
278 {
279 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
280 {
281 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
282 goto exit;
283 }
284 }
285
286 printf( " ok\n" );
287
288 /*
289 * 6. Read the HTTP Request
290 */
291 printf( " < Read from client:" );
292 fflush( stdout );
293
294 do
295 {
296 len = sizeof( buf ) - 1;
297 memset( buf, 0, sizeof( buf ) );
298 ret = ssl_read( &ssl, buf, len );
299
300 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
301 continue;
302
303 if( ret <= 0 )
304 {
305 switch( ret )
306 {
307 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
308 printf( " connection was closed gracefully\n" );
309 break;
310
311 case POLARSSL_ERR_NET_CONN_RESET:
312 printf( " connection was reset by peer\n" );
313 break;
314
315 default:
316 printf( " ssl_read returned %d\n", ret );
317 break;
318 }
319
320 break;
321 }
322
323 len = ret;
324 printf( " %d bytes read\n\n%s", len, (char *) buf );
325 }
326 while( 0 );
327
328 /*
329 * 7. Write the 200 Response
330 */
331 printf( " > Write to client:" );
332 fflush( stdout );
333
334 len = sprintf( (char *) buf, HTTP_RESPONSE,
335 ssl_get_ciphersuite( &ssl ) );
336
337 while( cnt < 100 )
338 {
339 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
340 {
341 if( ret == POLARSSL_ERR_NET_CONN_RESET )
342 {
343 printf( " failed\n ! peer closed the connection\n\n" );
344 goto exit;
345 }
346
347 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
348 {
349 printf( " failed\n ! ssl_write returned %d\n\n", ret );
350 goto exit;
351 }
352 }
353 len = ret;
354 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
355
356 m_sleep( 1000 );
357 }
358
359 ssl_close_notify( &ssl );
360 goto exit;
361 }
362
363exit:
364
365 net_close( client_fd );
366 x509_free( &srvcert );
367 rsa_free( &rsa );
368 ssl_free( &ssl );
369
Paul Bakkercce9d772011-11-18 14:26:47 +0000370#if defined(_WIN32)
Paul Bakker896ac222011-05-20 12:33:05 +0000371 printf( " Press Enter to exit this program.\n" );
372 fflush( stdout ); getchar();
373#endif
374
375 return( ret );
376}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000377#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000378 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000379 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */