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