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