blob: d90c3fee9a71cbd3a39f3dfc396a195a56a1372c [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 Bakkered27a042013-04-18 22:46:23 +020058 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
59 !defined(POLARSSL_X509_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000060int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000061{
Paul Bakkercce9d772011-11-18 14:26:47 +000062 ((void) argc);
63 ((void) argv);
64
Paul Bakker508ad5a2011-12-04 17:09:26 +000065 printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C "
Paul Bakkerb892b132011-10-12 09:19:43 +000066 "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000067 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakkered27a042013-04-18 22:46:23 +020068 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_PARSE_C not defined.\n");
Paul Bakkerb892b132011-10-12 09:19:43 +000069 return( 0 );
70}
Paul Bakkercce9d772011-11-18 14:26:47 +000071#elif defined(_WIN32)
72int main( int argc, char *argv[] )
Paul Bakkerb892b132011-10-12 09:19:43 +000073{
Paul Bakkercce9d772011-11-18 14:26:47 +000074 ((void) argc);
75 ((void) argv);
76
77 printf("_WIN32 defined. This application requires fork() and signals "
Paul Bakkerb892b132011-10-12 09:19:43 +000078 "to work correctly.\n");
79 return( 0 );
80}
81#else
Paul Bakker896ac222011-05-20 12:33:05 +000082
83#define DEBUG_LEVEL 0
84
85void my_debug( void *ctx, int level, const char *str )
86{
87 if( level < DEBUG_LEVEL )
88 {
89 fprintf( (FILE *) ctx, "%s", str );
90 fflush( (FILE *) ctx );
91 }
92}
93
Paul Bakkercce9d772011-11-18 14:26:47 +000094int main( int argc, char *argv[] )
Paul Bakker896ac222011-05-20 12:33:05 +000095{
96 int ret, len, cnt = 0, pid;
97 int listen_fd;
98 int client_fd;
99 unsigned char buf[1024];
Paul Bakker508ad5a2011-12-04 17:09:26 +0000100 char *pers = "ssl_fork_server";
Paul Bakker896ac222011-05-20 12:33:05 +0000101
Paul Bakker508ad5a2011-12-04 17:09:26 +0000102 entropy_context entropy;
103 ctr_drbg_context ctr_drbg;
Paul Bakker896ac222011-05-20 12:33:05 +0000104 ssl_context ssl;
Paul Bakker896ac222011-05-20 12:33:05 +0000105 x509_cert srvcert;
106 rsa_context rsa;
107
Paul Bakkercce9d772011-11-18 14:26:47 +0000108 ((void) argc);
109 ((void) argv);
110
Paul Bakker896ac222011-05-20 12:33:05 +0000111 signal( SIGCHLD, SIG_IGN );
112
113 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000114 * 0. Initial seeding of the RNG
115 */
116 printf( "\n . Initial seeding of the random generator..." );
117 fflush( stdout );
118
119 entropy_init( &entropy );
120 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
121 (unsigned char *) pers, strlen( pers ) ) ) != 0 )
122 {
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 */
142 ret = x509parse_crt( &srvcert, (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
150 ret = x509parse_crt( &srvcert, (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 );
159 ret = x509parse_key( &rsa, (unsigned char *) test_srv_key,
160 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,
222 (unsigned char* ) "parent", 6 ) ) != 0 )
223 {
224 printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret );
225 goto exit;
226 }
227
Paul Bakker896ac222011-05-20 12:33:05 +0000228 close( client_fd );
229 continue;
230 }
231
232 close( listen_fd );
233
234 /*
235 * 4. Setup stuff
236 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000237 printf( " . Setting up the SSL data...." );
Paul Bakker896ac222011-05-20 12:33:05 +0000238 fflush( stdout );
239
Paul Bakker508ad5a2011-12-04 17:09:26 +0000240 if( ( ret = ctr_drbg_reseed( &ctr_drbg,
241 (unsigned char *) "child", 5 ) ) != 0 )
242 {
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 );
264 ssl_set_own_cert( &ssl, &srvcert, &rsa );
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 );
361 x509_free( &srvcert );
362 rsa_free( &rsa );
363 ssl_free( &ssl );
364
Paul Bakkercce9d772011-11-18 14:26:47 +0000365#if defined(_WIN32)
Paul Bakker896ac222011-05-20 12:33:05 +0000366 printf( " Press Enter to exit this program.\n" );
367 fflush( stdout ); getchar();
368#endif
369
370 return( ret );
371}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000372#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000373 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000374 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */