blob: 024277013f726b165b93199d8942b3ed5eb69bda [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 Bakker508ad5a2011-12-04 17:09:26 +000099 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,
120 (unsigned char *) pers, strlen( pers ) ) ) != 0 )
121 {
122 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
123 goto exit;
124 }
125
126 printf( " ok\n" );
127
128 /*
Paul Bakker896ac222011-05-20 12:33:05 +0000129 * 1. Load the certificates and private RSA key
130 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000131 printf( " . Loading the server cert. and key..." );
Paul Bakker896ac222011-05-20 12:33:05 +0000132 fflush( stdout );
133
134 memset( &srvcert, 0, sizeof( x509_cert ) );
135
136 /*
137 * This demonstration program uses embedded test certificates.
138 * Instead, you may want to use x509parse_crtfile() to read the
139 * server and CA certificates, as well as x509parse_keyfile().
140 */
141 ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000142 strlen( test_srv_crt ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000143 if( ret != 0 )
144 {
145 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
146 goto exit;
147 }
148
149 ret = x509parse_crt( &srvcert, (unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000150 strlen( test_ca_crt ) );
Paul Bakker896ac222011-05-20 12:33:05 +0000151 if( ret != 0 )
152 {
153 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
154 goto exit;
155 }
156
157 rsa_init( &rsa, RSA_PKCS_V15, 0 );
158 ret = x509parse_key( &rsa, (unsigned char *) test_srv_key,
159 strlen( test_srv_key ), NULL, 0 );
160 if( ret != 0 )
161 {
162 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
163 goto exit;
164 }
165
166 printf( " ok\n" );
167
168 /*
169 * 2. Setup the listening TCP socket
170 */
171 printf( " . Bind on https://localhost:4433/ ..." );
172 fflush( stdout );
173
174 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
175 {
176 printf( " failed\n ! net_bind returned %d\n\n", ret );
177 goto exit;
178 }
179
180 printf( " ok\n" );
181
182 while( 1 )
183 {
184 /*
185 * 3. Wait until a client connects
186 */
187 client_fd = -1;
188 memset( &ssl, 0, sizeof( ssl ) );
189
190 printf( " . Waiting for a remote connection ..." );
191 fflush( stdout );
192
193 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
194 {
195 printf( " failed\n ! net_accept returned %d\n\n", ret );
196 goto exit;
197 }
198
199 printf( " ok\n" );
200
201 /*
202 * 3.5. Forking server thread
203 */
204
205 pid = fork();
206
207 printf( " . Forking to handle connection ..." );
208 fflush( stdout );
209
210 if( pid < 0 )
211 {
212 printf(" failed\n ! fork returned %d\n\n", pid );
213 goto exit;
214 }
215
216 printf( " ok\n" );
217
218 if( pid != 0 )
219 {
Paul Bakker508ad5a2011-12-04 17:09:26 +0000220 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
221 (unsigned char* ) "parent", 6 ) ) != 0 )
222 {
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,
240 (unsigned char *) "child", 5 ) ) != 0 )
241 {
242 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
243 goto exit;
244 }
245
Paul Bakker896ac222011-05-20 12:33:05 +0000246 if( ( ret = ssl_init( &ssl ) ) != 0 )
247 {
248 printf( " failed\n ! ssl_init returned %d\n\n", ret );
249 goto exit;
250 }
251
252 printf( " ok\n" );
253
254 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
255 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
256
Paul Bakker508ad5a2011-12-04 17:09:26 +0000257 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker896ac222011-05-20 12:33:05 +0000258 ssl_set_dbg( &ssl, my_debug, stdout );
259 ssl_set_bio( &ssl, net_recv, &client_fd,
260 net_send, &client_fd );
Paul Bakker896ac222011-05-20 12:33:05 +0000261
Paul Bakker896ac222011-05-20 12:33:05 +0000262 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
263 ssl_set_own_cert( &ssl, &srvcert, &rsa );
Paul Bakker896ac222011-05-20 12:33:05 +0000264
265 /*
266 * 5. Handshake
267 */
268 printf( " . Performing the SSL/TLS handshake..." );
269 fflush( stdout );
270
271 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
272 {
273 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
274 {
275 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
276 goto exit;
277 }
278 }
279
280 printf( " ok\n" );
281
282 /*
283 * 6. Read the HTTP Request
284 */
285 printf( " < Read from client:" );
286 fflush( stdout );
287
288 do
289 {
290 len = sizeof( buf ) - 1;
291 memset( buf, 0, sizeof( buf ) );
292 ret = ssl_read( &ssl, buf, len );
293
294 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
295 continue;
296
297 if( ret <= 0 )
298 {
299 switch( ret )
300 {
301 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
302 printf( " connection was closed gracefully\n" );
303 break;
304
305 case POLARSSL_ERR_NET_CONN_RESET:
306 printf( " connection was reset by peer\n" );
307 break;
308
309 default:
310 printf( " ssl_read returned %d\n", ret );
311 break;
312 }
313
314 break;
315 }
316
317 len = ret;
318 printf( " %d bytes read\n\n%s", len, (char *) buf );
319 }
320 while( 0 );
321
322 /*
323 * 7. Write the 200 Response
324 */
325 printf( " > Write to client:" );
326 fflush( stdout );
327
328 len = sprintf( (char *) buf, HTTP_RESPONSE,
329 ssl_get_ciphersuite( &ssl ) );
330
331 while( cnt < 100 )
332 {
333 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
334 {
335 if( ret == POLARSSL_ERR_NET_CONN_RESET )
336 {
337 printf( " failed\n ! peer closed the connection\n\n" );
338 goto exit;
339 }
340
341 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
342 {
343 printf( " failed\n ! ssl_write returned %d\n\n", ret );
344 goto exit;
345 }
346 }
347 len = ret;
348 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
349
350 m_sleep( 1000 );
351 }
352
353 ssl_close_notify( &ssl );
354 goto exit;
355 }
356
357exit:
358
359 net_close( client_fd );
360 x509_free( &srvcert );
361 rsa_free( &rsa );
362 ssl_free( &ssl );
363
Paul Bakkercce9d772011-11-18 14:26:47 +0000364#if defined(_WIN32)
Paul Bakker896ac222011-05-20 12:33:05 +0000365 printf( " Press Enter to exit this program.\n" );
366 fflush( stdout ); getchar();
367#endif
368
369 return( ret );
370}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000371#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000372 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000373 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */