blob: efb360c21ad01853dc9acca6d961b0d58ba29f86 [file] [log] [blame]
Paul Bakkerf9c49532013-12-19 15:40:58 +01001/*
2 * SSL server demonstration program using pthread for handling multiple
3 * clients.
4 *
5 * Copyright (C) 2006-2013, Brainspark B.V.
6 *
7 * This file is part of PolarSSL (http://www.polarssl.org)
8 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
27#include "polarssl/config.h"
28
29#if defined(_WIN32)
30#include <windows.h>
31#endif
32
33#include <string.h>
34#include <stdlib.h>
35#include <stdio.h>
36
37#include "polarssl/entropy.h"
38#include "polarssl/ctr_drbg.h"
39#include "polarssl/certs.h"
40#include "polarssl/x509.h"
41#include "polarssl/ssl.h"
42#include "polarssl/net.h"
43#include "polarssl/error.h"
44
45#if defined(POLARSSL_SSL_CACHE_C)
46#include "polarssl/ssl_cache.h"
47#endif
48
49#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
50#include "polarssl/memory.h"
51#endif
52
53#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \
54 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \
55 !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \
56 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
57 !defined(POLARSSL_X509_CRT_PARSE_C) || \
58 !defined(POLARSSL_THREADING_C) || !defined(POLARSSL_THREADING_PTHREAD)
59int main( int argc, char *argv[] )
60{
61 ((void) argc);
62 ((void) argv);
63
64 printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C "
65 "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
66 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
67 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C and/or "
68 "POLARSSL_THREADING_C and/or POLARSSL_THREADING_PTHREAD "
69 "not defined.\n");
70 return( 0 );
71}
72#else
73
74#define HTTP_RESPONSE \
75 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
76 "<h2>PolarSSL Test Server</h2>\r\n" \
77 "<p>Successful connection using: %s</p>\r\n"
78
79#define DEBUG_LEVEL 0
80
81threading_mutex_t debug_mutex;
82
83static void my_mutexed_debug( void *ctx, int level, const char *str )
84{
85 polarssl_mutex_lock( &debug_mutex );
86 if( level < DEBUG_LEVEL )
87 {
88 fprintf( (FILE *) ctx, "%s", str );
89 fflush( (FILE *) ctx );
90 }
91 polarssl_mutex_unlock( &debug_mutex );
92}
93
94typedef struct {
95 int client_fd;
96 int thread_complete;
97 entropy_context *entropy;
98#if defined(POLARSSL_SSL_CACHE_C)
99 ssl_cache_context *cache;
100#endif
101 x509_crt *ca_chain;
102 x509_crt *server_cert;
103 pk_context *server_key;
104} thread_info_t;
105
106typedef struct {
107 int active;
108 thread_info_t data;
109 pthread_t thread;
110} pthread_info_t;
111
112#define MAX_NUM_THREADS 5
113
114static thread_info_t base_info;
115static pthread_info_t threads[MAX_NUM_THREADS];
116
117static void *handle_ssl_connection( void *data )
118{
119 int ret, len;
120 thread_info_t *thread_info = (thread_info_t *) data;
121 int client_fd = thread_info->client_fd;
122 int thread_id = (int) pthread_self();
123 unsigned char buf[1024];
124 char pers[50];
125 ssl_context ssl;
126 ctr_drbg_context ctr_drbg;
127
128 snprintf( pers, sizeof(pers), "SSL Pthread Thread %d", thread_id );
129 printf( " [ #%d ] Client FD %d\n", thread_id, client_fd );
130 printf( " [ #%d ] Seeding the random number generator...\n", thread_id );
131
132 /* entropy_func() is thread-safe if POLARSSL_THREADING_C is set
133 */
134 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, thread_info->entropy,
135 (const unsigned char *) pers,
136 strlen( pers ) ) ) != 0 )
137 {
138 printf( " [ #%d ] failed: ctr_drbg_init returned -0x%04x\n",
139 thread_id, -ret );
140 goto thread_exit;
141 }
142
143 printf( " [ #%d ] ok\n", thread_id );
144
145 /*
146 * 4. Setup stuff
147 */
148 printf( " [ #%d ] Setting up the SSL data....\n", thread_id );
149
150 if( ( ret = ssl_init( &ssl ) ) != 0 )
151 {
152 printf( " [ #%d ] failed: ssl_init returned -0x%04x\n",
153 thread_id, -ret );
154 goto thread_exit;
155 }
156
157 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
158 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
159
160 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
161 ssl_set_dbg( &ssl, my_mutexed_debug, stdout );
162
163 /* ssl_cache_get() and ssl_cache_set() are thread-safe if
164 * POLARSSL_THREADING_C is set.
165 */
166#if defined(POLARSSL_SSL_CACHE_C)
167 ssl_set_session_cache( &ssl, ssl_cache_get, thread_info->cache,
168 ssl_cache_set, thread_info->cache );
169#endif
170
171 ssl_set_ca_chain( &ssl, thread_info->ca_chain, NULL, NULL );
172 ssl_set_own_cert( &ssl, thread_info->server_cert, thread_info->server_key );
173
174 printf( " [ #%d ] ok\n", thread_id );
175
176 ssl_set_bio( &ssl, net_recv, &client_fd,
177 net_send, &client_fd );
178
179 printf( " [ #%d ] ok\n", thread_id );
180
181 /*
182 * 5. Handshake
183 */
184 printf( " [ #%d ] Performing the SSL/TLS handshake\n", thread_id );
185
186 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
187 {
188 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
189 {
190 printf( " [ #%d ] failed: ssl_handshake returned -0x%04x\n",
191 thread_id, -ret );
192 goto thread_exit;
193 }
194 }
195
196 printf( " [ #%d ] ok\n", thread_id );
197
198 /*
199 * 6. Read the HTTP Request
200 */
201 printf( " [ #%d ] < Read from client\n", thread_id );
202
203 do
204 {
205 len = sizeof( buf ) - 1;
206 memset( buf, 0, sizeof( buf ) );
207 ret = ssl_read( &ssl, buf, len );
208
209 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
210 continue;
211
212 if( ret <= 0 )
213 {
214 switch( ret )
215 {
216 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
217 printf( " [ #%d ] connection was closed gracefully\n",
218 thread_id );
219 goto thread_exit;
220
221 case POLARSSL_ERR_NET_CONN_RESET:
222 printf( " [ #%d ] connection was reset by peer\n",
223 thread_id );
224 goto thread_exit;
225
226 default:
227 printf( " [ #%d ] ssl_read returned -0x%04x\n",
228 thread_id, -ret );
229 goto thread_exit;
230 }
231
232 break;
233 }
234
235 len = ret;
236 printf( " [ #%d ] %d bytes read\n=====\n%s\n=====\n",
237 thread_id, len, (char *) buf );
238
239 if( ret > 0 )
240 break;
241 }
242 while( 1 );
243
244 /*
245 * 7. Write the 200 Response
246 */
247 printf( " [ #%d ] > Write to client:\n", thread_id );
248
249 len = sprintf( (char *) buf, HTTP_RESPONSE,
250 ssl_get_ciphersuite( &ssl ) );
251
252 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
253 {
254 if( ret == POLARSSL_ERR_NET_CONN_RESET )
255 {
256 printf( " [ #%d ] failed: peer closed the connection\n",
257 thread_id );
258 goto thread_exit;
259 }
260
261 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
262 {
263 printf( " [ #%d ] failed: ssl_write returned -0x%04x\n",
264 thread_id, ret );
265 goto thread_exit;
266 }
267 }
268
269 len = ret;
270 printf( " [ #%d ] %d bytes written\n=====\n%s\n=====\n",
271 thread_id, len, (char *) buf );
272
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +0100273 printf( " [ #%d ] . Closing the connection...", thread_id );
274
275 while( ( ret = ssl_close_notify( &ssl ) ) < 0 )
276 {
277 if( ret != POLARSSL_ERR_NET_WANT_READ &&
278 ret != POLARSSL_ERR_NET_WANT_WRITE )
279 {
280 printf( " [ #%d ] failed: ssl_close_notify returned -0x%04x\n",
281 thread_id, ret );
282 goto thread_exit;
283 }
284 }
285
286 printf( " ok\n" );
287
Paul Bakkerf9c49532013-12-19 15:40:58 +0100288 ret = 0;
289
290thread_exit:
291
292#ifdef POLARSSL_ERROR_C
293 if( ret != 0 )
294 {
295 char error_buf[100];
296 polarssl_strerror( ret, error_buf, 100 );
297 printf(" [ #%d ] Last error was: -0x%04x - %s\n\n",
298 thread_id, -ret, error_buf );
299 }
300#endif
301
302 net_close( client_fd );
303 ssl_free( &ssl );
304
305 thread_info->thread_complete = 1;
306
307 return( NULL );
308}
309
310static int thread_create( int client_fd )
311{
312 int ret, i;
313
314 /*
315 * Find in-active or finished thread slot
316 */
317 for( i = 0; i < MAX_NUM_THREADS; i++ )
318 {
319 if( threads[i].active == 0 )
320 break;
321
322 if( threads[i].data.thread_complete == 1 )
323 {
324 printf( " [ main ] Cleaning up thread %d\n", i );
325 pthread_join(threads[i].thread, NULL );
326 memset( &threads[i], 0, sizeof(pthread_info_t) );
327 break;
328 }
329 }
330
331 if( i == MAX_NUM_THREADS )
332 return( -1 );
333
334 /*
335 * Fill thread-info for thread
336 */
337 memcpy( &threads[i].data, &base_info, sizeof(base_info) );
338 threads[i].active = 1;
339 threads[i].data.client_fd = client_fd;
340
341 if( ( ret = pthread_create( &threads[i].thread, NULL, handle_ssl_connection, &threads[i].data ) ) != 0 )
342 {
343 return( ret );
344 }
345
346 return( 0 );
347}
348
349int main( int argc, char *argv[] )
350{
351 int ret;
352 int listen_fd;
353 int client_fd = -1;
354
355 entropy_context entropy;
356 x509_crt srvcert;
357 pk_context pkey;
358#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
359 unsigned char alloc_buf[100000];
360#endif
361#if defined(POLARSSL_SSL_CACHE_C)
362 ssl_cache_context cache;
363#endif
364
365 ((void) argc);
366 ((void) argv);
367
368#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
369 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
370#endif
371
372#if defined(POLARSSL_SSL_CACHE_C)
373 ssl_cache_init( &cache );
374 base_info.cache = &cache;
375#endif
376
377 memset( threads, 0, sizeof(threads) );
378
379 polarssl_mutex_init( &debug_mutex );
380
381 /*
382 * We use only a single entropy source that is used in all the threads.
383 */
384 entropy_init( &entropy );
385 base_info.entropy = &entropy;
386
387 /*
388 * 1. Load the certificates and private RSA key
389 */
390 printf( "\n . Loading the server cert. and key..." );
391 fflush( stdout );
392
393 x509_crt_init( &srvcert );
394
395 /*
396 * This demonstration program uses embedded test certificates.
397 * Instead, you may want to use x509_crt_parse_file() to read the
398 * server and CA certificates, as well as pk_parse_keyfile().
399 */
400 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt,
401 strlen( test_srv_crt ) );
402 if( ret != 0 )
403 {
404 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
405 goto exit;
406 }
407
408 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_ca_list,
409 strlen( test_ca_list ) );
410 if( ret != 0 )
411 {
412 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
413 goto exit;
414 }
415
416 pk_init( &pkey );
417 ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key,
418 strlen( test_srv_key ), NULL, 0 );
419 if( ret != 0 )
420 {
421 printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
422 goto exit;
423 }
424
425 base_info.ca_chain = srvcert.next;
426 base_info.server_cert = &srvcert;
427 base_info.server_key = &pkey;
428
429 printf( " ok\n" );
430
431 /*
432 * 2. Setup the listening TCP socket
433 */
434 printf( " . Bind on https://localhost:4433/ ..." );
435 fflush( stdout );
436
437 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
438 {
439 printf( " failed\n ! net_bind returned %d\n\n", ret );
440 goto exit;
441 }
442
443 printf( " ok\n" );
444
445reset:
446#ifdef POLARSSL_ERROR_C
447 if( ret != 0 )
448 {
449 char error_buf[100];
450 polarssl_strerror( ret, error_buf, 100 );
451 printf( " [ main ] Last error was: -0x%04x - %s\n", -ret, error_buf );
452 }
453#endif
454
455 /*
456 * 3. Wait until a client connects
457 */
458 client_fd = -1;
459
460 printf( " [ main ] Waiting for a remote connection\n" );
461
462 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
463 {
464 printf( " [ main ] failed: net_accept returned -0x%04x\n", ret );
465 goto exit;
466 }
467
468 printf( " [ main ] ok\n" );
469 printf( " [ main ] Creating a new thread\n" );
470
471 if( ( ret = thread_create( client_fd ) ) != 0 )
472 {
473 printf( " [ main ] failed: thread_create returned %d\n", ret );
474 net_close( client_fd );
475 goto reset;
476 }
477
478 ret = 0;
479 goto reset;
480
481exit:
482 x509_crt_free( &srvcert );
483 pk_free( &pkey );
484#if defined(POLARSSL_SSL_CACHE_C)
485 ssl_cache_free( &cache );
486#endif
487 entropy_free( &entropy );
488
489 polarssl_mutex_free( &debug_mutex );
490
491#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
492 memory_buffer_alloc_free();
493#endif
494
495#if defined(_WIN32)
496 printf( " Press Enter to exit this program.\n" );
497 fflush( stdout ); getchar();
498#endif
499
500 return( ret );
501}
502
503#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
504 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
505 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C && POLARSSL_THREADING_C &&
506 POLARSSL_THREADING_PTHREAD */