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