blob: aed2513b60d8295a7012069fdb4b6f8be4bbb6ef [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
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" \
80 "<h2>PolarSSL Test Server</h2>\r\n" \
81 "<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 );
170
Paul Bakkerf9c49532013-12-19 15:40:58 +0100171 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
172 ssl_set_dbg( &ssl, my_mutexed_debug, stdout );
173
174 /* ssl_cache_get() and ssl_cache_set() are thread-safe if
175 * POLARSSL_THREADING_C is set.
176 */
177#if defined(POLARSSL_SSL_CACHE_C)
178 ssl_set_session_cache( &ssl, ssl_cache_get, thread_info->cache,
179 ssl_cache_set, thread_info->cache );
180#endif
181
182 ssl_set_ca_chain( &ssl, thread_info->ca_chain, NULL, NULL );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200183 if( ( ret = ssl_set_own_cert( &ssl, thread_info->server_cert, thread_info->server_key ) ) != 0 )
184 {
185 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnard955028f2014-07-12 01:27:34 +0200186 goto thread_exit;
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200187 }
Paul Bakkerf9c49532013-12-19 15:40:58 +0100188
189 printf( " [ #%d ] ok\n", thread_id );
190
191 ssl_set_bio( &ssl, net_recv, &client_fd,
192 net_send, &client_fd );
193
194 printf( " [ #%d ] ok\n", thread_id );
195
196 /*
197 * 5. Handshake
198 */
199 printf( " [ #%d ] Performing the SSL/TLS handshake\n", thread_id );
200
201 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
202 {
203 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
204 {
205 printf( " [ #%d ] failed: ssl_handshake returned -0x%04x\n",
206 thread_id, -ret );
207 goto thread_exit;
208 }
209 }
210
211 printf( " [ #%d ] ok\n", thread_id );
212
213 /*
214 * 6. Read the HTTP Request
215 */
216 printf( " [ #%d ] < Read from client\n", thread_id );
217
218 do
219 {
220 len = sizeof( buf ) - 1;
221 memset( buf, 0, sizeof( buf ) );
222 ret = ssl_read( &ssl, buf, len );
223
224 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
225 continue;
226
227 if( ret <= 0 )
228 {
229 switch( ret )
230 {
231 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
232 printf( " [ #%d ] connection was closed gracefully\n",
233 thread_id );
234 goto thread_exit;
235
236 case POLARSSL_ERR_NET_CONN_RESET:
237 printf( " [ #%d ] connection was reset by peer\n",
238 thread_id );
239 goto thread_exit;
240
241 default:
242 printf( " [ #%d ] ssl_read returned -0x%04x\n",
243 thread_id, -ret );
244 goto thread_exit;
245 }
Paul Bakkerf9c49532013-12-19 15:40:58 +0100246 }
247
248 len = ret;
249 printf( " [ #%d ] %d bytes read\n=====\n%s\n=====\n",
250 thread_id, len, (char *) buf );
251
252 if( ret > 0 )
253 break;
254 }
255 while( 1 );
256
257 /*
258 * 7. Write the 200 Response
259 */
260 printf( " [ #%d ] > Write to client:\n", thread_id );
261
262 len = sprintf( (char *) buf, HTTP_RESPONSE,
263 ssl_get_ciphersuite( &ssl ) );
264
265 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
266 {
267 if( ret == POLARSSL_ERR_NET_CONN_RESET )
268 {
269 printf( " [ #%d ] failed: peer closed the connection\n",
270 thread_id );
271 goto thread_exit;
272 }
273
274 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
275 {
276 printf( " [ #%d ] failed: ssl_write returned -0x%04x\n",
277 thread_id, ret );
278 goto thread_exit;
279 }
280 }
281
282 len = ret;
283 printf( " [ #%d ] %d bytes written\n=====\n%s\n=====\n",
284 thread_id, len, (char *) buf );
285
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +0100286 printf( " [ #%d ] . Closing the connection...", thread_id );
287
288 while( ( ret = ssl_close_notify( &ssl ) ) < 0 )
289 {
290 if( ret != POLARSSL_ERR_NET_WANT_READ &&
291 ret != POLARSSL_ERR_NET_WANT_WRITE )
292 {
293 printf( " [ #%d ] failed: ssl_close_notify returned -0x%04x\n",
294 thread_id, ret );
295 goto thread_exit;
296 }
297 }
298
299 printf( " ok\n" );
300
Paul Bakkerf9c49532013-12-19 15:40:58 +0100301 ret = 0;
302
303thread_exit:
304
305#ifdef POLARSSL_ERROR_C
306 if( ret != 0 )
307 {
308 char error_buf[100];
309 polarssl_strerror( ret, error_buf, 100 );
310 printf(" [ #%d ] Last error was: -0x%04x - %s\n\n",
311 thread_id, -ret, error_buf );
312 }
313#endif
314
315 net_close( client_fd );
Manuel Pégourié-Gonnard955028f2014-07-12 01:27:34 +0200316 ctr_drbg_free( &ctr_drbg );
Paul Bakkerf9c49532013-12-19 15:40:58 +0100317 ssl_free( &ssl );
318
319 thread_info->thread_complete = 1;
320
321 return( NULL );
322}
323
324static int thread_create( int client_fd )
325{
326 int ret, i;
327
328 /*
329 * Find in-active or finished thread slot
330 */
331 for( i = 0; i < MAX_NUM_THREADS; i++ )
332 {
333 if( threads[i].active == 0 )
334 break;
335
336 if( threads[i].data.thread_complete == 1 )
337 {
338 printf( " [ main ] Cleaning up thread %d\n", i );
339 pthread_join(threads[i].thread, NULL );
340 memset( &threads[i], 0, sizeof(pthread_info_t) );
341 break;
342 }
343 }
344
345 if( i == MAX_NUM_THREADS )
346 return( -1 );
347
348 /*
349 * Fill thread-info for thread
350 */
351 memcpy( &threads[i].data, &base_info, sizeof(base_info) );
352 threads[i].active = 1;
353 threads[i].data.client_fd = client_fd;
354
355 if( ( ret = pthread_create( &threads[i].thread, NULL, handle_ssl_connection, &threads[i].data ) ) != 0 )
356 {
357 return( ret );
358 }
359
360 return( 0 );
361}
362
363int main( int argc, char *argv[] )
364{
365 int ret;
366 int listen_fd;
367 int client_fd = -1;
368
369 entropy_context entropy;
370 x509_crt srvcert;
371 pk_context pkey;
372#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
373 unsigned char alloc_buf[100000];
374#endif
375#if defined(POLARSSL_SSL_CACHE_C)
376 ssl_cache_context cache;
377#endif
378
379 ((void) argc);
380 ((void) argv);
381
382#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
383 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
384#endif
385
386#if defined(POLARSSL_SSL_CACHE_C)
387 ssl_cache_init( &cache );
388 base_info.cache = &cache;
389#endif
390
391 memset( threads, 0, sizeof(threads) );
392
393 polarssl_mutex_init( &debug_mutex );
394
395 /*
396 * We use only a single entropy source that is used in all the threads.
397 */
398 entropy_init( &entropy );
399 base_info.entropy = &entropy;
400
401 /*
402 * 1. Load the certificates and private RSA key
403 */
404 printf( "\n . Loading the server cert. and key..." );
405 fflush( stdout );
406
407 x509_crt_init( &srvcert );
408
409 /*
410 * This demonstration program uses embedded test certificates.
411 * Instead, you may want to use x509_crt_parse_file() to read the
412 * server and CA certificates, as well as pk_parse_keyfile().
413 */
414 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt,
415 strlen( test_srv_crt ) );
416 if( ret != 0 )
417 {
418 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
419 goto exit;
420 }
421
422 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_ca_list,
423 strlen( test_ca_list ) );
424 if( ret != 0 )
425 {
426 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
427 goto exit;
428 }
429
430 pk_init( &pkey );
431 ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key,
432 strlen( test_srv_key ), NULL, 0 );
433 if( ret != 0 )
434 {
435 printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
436 goto exit;
437 }
438
439 base_info.ca_chain = srvcert.next;
440 base_info.server_cert = &srvcert;
441 base_info.server_key = &pkey;
442
443 printf( " ok\n" );
444
445 /*
446 * 2. Setup the listening TCP socket
447 */
448 printf( " . Bind on https://localhost:4433/ ..." );
449 fflush( stdout );
450
451 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
452 {
453 printf( " failed\n ! net_bind returned %d\n\n", ret );
454 goto exit;
455 }
456
457 printf( " ok\n" );
458
459reset:
460#ifdef POLARSSL_ERROR_C
461 if( ret != 0 )
462 {
463 char error_buf[100];
464 polarssl_strerror( ret, error_buf, 100 );
465 printf( " [ main ] Last error was: -0x%04x - %s\n", -ret, error_buf );
466 }
467#endif
468
469 /*
470 * 3. Wait until a client connects
471 */
472 client_fd = -1;
473
474 printf( " [ main ] Waiting for a remote connection\n" );
475
476 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
477 {
478 printf( " [ main ] failed: net_accept returned -0x%04x\n", ret );
479 goto exit;
480 }
481
482 printf( " [ main ] ok\n" );
483 printf( " [ main ] Creating a new thread\n" );
484
485 if( ( ret = thread_create( client_fd ) ) != 0 )
486 {
487 printf( " [ main ] failed: thread_create returned %d\n", ret );
488 net_close( client_fd );
489 goto reset;
490 }
491
492 ret = 0;
493 goto reset;
494
495exit:
496 x509_crt_free( &srvcert );
497 pk_free( &pkey );
498#if defined(POLARSSL_SSL_CACHE_C)
499 ssl_cache_free( &cache );
500#endif
Paul Bakkerf9c49532013-12-19 15:40:58 +0100501 entropy_free( &entropy );
502
503 polarssl_mutex_free( &debug_mutex );
504
505#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
506 memory_buffer_alloc_free();
507#endif
508
509#if defined(_WIN32)
510 printf( " Press Enter to exit this program.\n" );
511 fflush( stdout ); getchar();
512#endif
513
514 return( ret );
515}
516
517#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
518 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
519 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C && POLARSSL_THREADING_C &&
520 POLARSSL_THREADING_PTHREAD */