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