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