blob: 7e489dafabaa174815881dde476f25cdb195150e [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 Bakker39148402014-04-17 16:02:36 +0200113 memset( &ssl, 0, sizeof(ssl_context) );
114
115 entropy_init( &entropy );
116 rsa_init( &rsa, RSA_PKCS_V15, 0 );
117 memset( &srvcert, 0, sizeof( x509_cert ) );
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 Bakkere0225e42013-06-06 12:52:24 +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.
145 * Instead, you may want to use x509parse_crtfile() to read the
146 * server and CA certificates, as well as x509parse_keyfile().
147 */
Paul Bakkere0225e42013-06-06 12:52:24 +0200148 ret = x509parse_crt( &srvcert, (const unsigned char *) test_srv_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000149 strlen( test_srv_crt ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000150 if( ret != 0 )
151 {
152 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
153 goto exit;
154 }
155
Paul Bakkere0225e42013-06-06 12:52:24 +0200156 ret = x509parse_crt( &srvcert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000157 strlen( test_ca_crt ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000158 if( ret != 0 )
159 {
160 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
161 goto exit;
162 }
163
Paul Bakkere0225e42013-06-06 12:52:24 +0200164 ret = x509parse_key( &rsa, (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 {
168 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
169 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 Bakkere0225e42013-06-06 12:52:24 +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 Bakkere0225e42013-06-06 12:52:24 +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 Bakker39148402014-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
Paul Bakker508ad5a2011-12-04 17:09:26 +0000265 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker896ac222011-05-20 12:33:05 +0000266 ssl_set_dbg( &ssl, my_debug, stdout );
267 ssl_set_bio( &ssl, net_recv, &client_fd,
268 net_send, &client_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000269
Paul Bakker896ac222011-05-20 12:33:05 +0000270 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
271 ssl_set_own_cert( &ssl, &srvcert, &rsa );
Paul Bakker896ac222011-05-20 12:33:05 +0000272
273 /*
274 * 5. Handshake
275 */
276 printf( " . Performing the SSL/TLS handshake..." );
277 fflush( stdout );
278
279 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
280 {
281 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
282 {
283 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
284 goto exit;
285 }
286 }
287
288 printf( " ok\n" );
289
290 /*
291 * 6. Read the HTTP Request
292 */
293 printf( " < Read from client:" );
294 fflush( stdout );
295
296 do
297 {
298 len = sizeof( buf ) - 1;
299 memset( buf, 0, sizeof( buf ) );
300 ret = ssl_read( &ssl, buf, len );
301
302 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
303 continue;
304
305 if( ret <= 0 )
306 {
307 switch( ret )
308 {
309 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
310 printf( " connection was closed gracefully\n" );
311 break;
312
313 case POLARSSL_ERR_NET_CONN_RESET:
314 printf( " connection was reset by peer\n" );
315 break;
316
317 default:
318 printf( " ssl_read returned %d\n", ret );
319 break;
320 }
321
322 break;
323 }
324
325 len = ret;
326 printf( " %d bytes read\n\n%s", len, (char *) buf );
327 }
328 while( 0 );
329
330 /*
331 * 7. Write the 200 Response
332 */
333 printf( " > Write to client:" );
334 fflush( stdout );
335
336 len = sprintf( (char *) buf, HTTP_RESPONSE,
337 ssl_get_ciphersuite( &ssl ) );
338
339 while( cnt < 100 )
340 {
341 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
342 {
343 if( ret == POLARSSL_ERR_NET_CONN_RESET )
344 {
345 printf( " failed\n ! peer closed the connection\n\n" );
346 goto exit;
347 }
348
349 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
350 {
351 printf( " failed\n ! ssl_write returned %d\n\n", ret );
352 goto exit;
353 }
354 }
355 len = ret;
356 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
357
358 m_sleep( 1000 );
359 }
360
361 ssl_close_notify( &ssl );
362 goto exit;
363 }
364
365exit:
366
Paul Bakker39148402014-04-17 16:02:36 +0200367 if( client_fd != -1 )
368 net_close( client_fd );
369
Paul Bakker896ac222011-05-20 12:33:05 +0000370 x509_free( &srvcert );
371 rsa_free( &rsa );
372 ssl_free( &ssl );
373
Paul Bakkercce9d772011-11-18 14:26:47 +0000374#if defined(_WIN32)
Paul Bakker896ac222011-05-20 12:33:05 +0000375 printf( " Press Enter to exit this program.\n" );
376 fflush( stdout ); getchar();
377#endif
378
379 return( ret );
380}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000381#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000382 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000383 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */