ssl_pthread_server.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * SSL server demonstration program using pthread for handling multiple
  3. * clients.
  4. *
  5. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. * not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * This file is part of mbed TLS (https://tls.mbed.org)
  21. */
  22. #if !defined(MBEDTLS_CONFIG_FILE)
  23. #include "mbedtls/config.h"
  24. #else
  25. #include MBEDTLS_CONFIG_FILE
  26. #endif
  27. #if defined(MBEDTLS_PLATFORM_C)
  28. #include "mbedtls/platform.h"
  29. #else
  30. #include <stdio.h>
  31. #define mbedtls_fprintf fprintf
  32. #define mbedtls_printf printf
  33. #define mbedtls_snprintf snprintf
  34. #endif
  35. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_CERTS_C) || \
  36. !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_SSL_TLS_C) || \
  37. !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_NET_C) || \
  38. !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
  39. !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
  40. !defined(MBEDTLS_THREADING_C) || !defined(MBEDTLS_THREADING_PTHREAD) || \
  41. !defined(MBEDTLS_PEM_PARSE_C)
  42. int main( void )
  43. {
  44. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_CERTS_C and/or MBEDTLS_ENTROPY_C "
  45. "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
  46. "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
  47. "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
  48. "MBEDTLS_THREADING_C and/or MBEDTLS_THREADING_PTHREAD "
  49. "and/or MBEDTLS_PEM_PARSE_C not defined.\n");
  50. return( 0 );
  51. }
  52. #else
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #if defined(_WIN32)
  56. #include <windows.h>
  57. #endif
  58. #include "mbedtls/entropy.h"
  59. #include "mbedtls/ctr_drbg.h"
  60. #include "mbedtls/certs.h"
  61. #include "mbedtls/x509.h"
  62. #include "mbedtls/ssl.h"
  63. #include "mbedtls/net_sockets.h"
  64. #include "mbedtls/error.h"
  65. #if defined(MBEDTLS_SSL_CACHE_C)
  66. #include "mbedtls/ssl_cache.h"
  67. #endif
  68. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  69. #include "mbedtls/memory_buffer_alloc.h"
  70. #endif
  71. #define HTTP_RESPONSE \
  72. "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
  73. "<h2>mbed TLS Test Server</h2>\r\n" \
  74. "<p>Successful connection using: %s</p>\r\n"
  75. #define DEBUG_LEVEL 0
  76. #define MAX_NUM_THREADS 5
  77. mbedtls_threading_mutex_t debug_mutex;
  78. static void my_mutexed_debug( void *ctx, int level,
  79. const char *file, int line,
  80. const char *str )
  81. {
  82. long int thread_id = (long int) pthread_self();
  83. mbedtls_mutex_lock( &debug_mutex );
  84. ((void) level);
  85. mbedtls_fprintf( (FILE *) ctx, "%s:%04d: [ #%ld ] %s",
  86. file, line, thread_id, str );
  87. fflush( (FILE *) ctx );
  88. mbedtls_mutex_unlock( &debug_mutex );
  89. }
  90. typedef struct {
  91. mbedtls_net_context client_fd;
  92. int thread_complete;
  93. const mbedtls_ssl_config *config;
  94. } thread_info_t;
  95. typedef struct {
  96. int active;
  97. thread_info_t data;
  98. pthread_t thread;
  99. } pthread_info_t;
  100. static thread_info_t base_info;
  101. static pthread_info_t threads[MAX_NUM_THREADS];
  102. static void *handle_ssl_connection( void *data )
  103. {
  104. int ret, len;
  105. thread_info_t *thread_info = (thread_info_t *) data;
  106. mbedtls_net_context *client_fd = &thread_info->client_fd;
  107. long int thread_id = (long int) pthread_self();
  108. unsigned char buf[1024];
  109. mbedtls_ssl_context ssl;
  110. /* Make sure memory references are valid */
  111. mbedtls_ssl_init( &ssl );
  112. mbedtls_printf( " [ #%ld ] Setting up SSL/TLS data\n", thread_id );
  113. /*
  114. * 4. Get the SSL context ready
  115. */
  116. if( ( ret = mbedtls_ssl_setup( &ssl, thread_info->config ) ) != 0 )
  117. {
  118. mbedtls_printf( " [ #%ld ] failed: mbedtls_ssl_setup returned -0x%04x\n",
  119. thread_id, -ret );
  120. goto thread_exit;
  121. }
  122. mbedtls_ssl_set_bio( &ssl, client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
  123. /*
  124. * 5. Handshake
  125. */
  126. mbedtls_printf( " [ #%ld ] Performing the SSL/TLS handshake\n", thread_id );
  127. while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
  128. {
  129. if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  130. {
  131. mbedtls_printf( " [ #%ld ] failed: mbedtls_ssl_handshake returned -0x%04x\n",
  132. thread_id, -ret );
  133. goto thread_exit;
  134. }
  135. }
  136. mbedtls_printf( " [ #%ld ] ok\n", thread_id );
  137. /*
  138. * 6. Read the HTTP Request
  139. */
  140. mbedtls_printf( " [ #%ld ] < Read from client\n", thread_id );
  141. do
  142. {
  143. len = sizeof( buf ) - 1;
  144. memset( buf, 0, sizeof( buf ) );
  145. ret = mbedtls_ssl_read( &ssl, buf, len );
  146. if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
  147. continue;
  148. if( ret <= 0 )
  149. {
  150. switch( ret )
  151. {
  152. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  153. mbedtls_printf( " [ #%ld ] connection was closed gracefully\n",
  154. thread_id );
  155. goto thread_exit;
  156. case MBEDTLS_ERR_NET_CONN_RESET:
  157. mbedtls_printf( " [ #%ld ] connection was reset by peer\n",
  158. thread_id );
  159. goto thread_exit;
  160. default:
  161. mbedtls_printf( " [ #%ld ] mbedtls_ssl_read returned -0x%04x\n",
  162. thread_id, -ret );
  163. goto thread_exit;
  164. }
  165. }
  166. len = ret;
  167. mbedtls_printf( " [ #%ld ] %d bytes read\n=====\n%s\n=====\n",
  168. thread_id, len, (char *) buf );
  169. if( ret > 0 )
  170. break;
  171. }
  172. while( 1 );
  173. /*
  174. * 7. Write the 200 Response
  175. */
  176. mbedtls_printf( " [ #%ld ] > Write to client:\n", thread_id );
  177. len = sprintf( (char *) buf, HTTP_RESPONSE,
  178. mbedtls_ssl_get_ciphersuite( &ssl ) );
  179. while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
  180. {
  181. if( ret == MBEDTLS_ERR_NET_CONN_RESET )
  182. {
  183. mbedtls_printf( " [ #%ld ] failed: peer closed the connection\n",
  184. thread_id );
  185. goto thread_exit;
  186. }
  187. if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  188. {
  189. mbedtls_printf( " [ #%ld ] failed: mbedtls_ssl_write returned -0x%04x\n",
  190. thread_id, ret );
  191. goto thread_exit;
  192. }
  193. }
  194. len = ret;
  195. mbedtls_printf( " [ #%ld ] %d bytes written\n=====\n%s\n=====\n",
  196. thread_id, len, (char *) buf );
  197. mbedtls_printf( " [ #%ld ] . Closing the connection...", thread_id );
  198. while( ( ret = mbedtls_ssl_close_notify( &ssl ) ) < 0 )
  199. {
  200. if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
  201. ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  202. {
  203. mbedtls_printf( " [ #%ld ] failed: mbedtls_ssl_close_notify returned -0x%04x\n",
  204. thread_id, ret );
  205. goto thread_exit;
  206. }
  207. }
  208. mbedtls_printf( " ok\n" );
  209. ret = 0;
  210. thread_exit:
  211. #ifdef MBEDTLS_ERROR_C
  212. if( ret != 0 )
  213. {
  214. char error_buf[100];
  215. mbedtls_strerror( ret, error_buf, 100 );
  216. mbedtls_printf(" [ #%ld ] Last error was: -0x%04x - %s\n\n",
  217. thread_id, -ret, error_buf );
  218. }
  219. #endif
  220. mbedtls_net_free( client_fd );
  221. mbedtls_ssl_free( &ssl );
  222. thread_info->thread_complete = 1;
  223. return( NULL );
  224. }
  225. static int thread_create( mbedtls_net_context *client_fd )
  226. {
  227. int ret, i;
  228. /*
  229. * Find in-active or finished thread slot
  230. */
  231. for( i = 0; i < MAX_NUM_THREADS; i++ )
  232. {
  233. if( threads[i].active == 0 )
  234. break;
  235. if( threads[i].data.thread_complete == 1 )
  236. {
  237. mbedtls_printf( " [ main ] Cleaning up thread %d\n", i );
  238. pthread_join(threads[i].thread, NULL );
  239. memset( &threads[i], 0, sizeof(pthread_info_t) );
  240. break;
  241. }
  242. }
  243. if( i == MAX_NUM_THREADS )
  244. return( -1 );
  245. /*
  246. * Fill thread-info for thread
  247. */
  248. memcpy( &threads[i].data, &base_info, sizeof(base_info) );
  249. threads[i].active = 1;
  250. memcpy( &threads[i].data.client_fd, client_fd, sizeof( mbedtls_net_context ) );
  251. if( ( ret = pthread_create( &threads[i].thread, NULL, handle_ssl_connection,
  252. &threads[i].data ) ) != 0 )
  253. {
  254. return( ret );
  255. }
  256. return( 0 );
  257. }
  258. int main( void )
  259. {
  260. int ret;
  261. mbedtls_net_context listen_fd, client_fd;
  262. const char pers[] = "ssl_pthread_server";
  263. mbedtls_entropy_context entropy;
  264. mbedtls_ctr_drbg_context ctr_drbg;
  265. mbedtls_ssl_config conf;
  266. mbedtls_x509_crt srvcert;
  267. mbedtls_x509_crt cachain;
  268. mbedtls_pk_context pkey;
  269. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  270. unsigned char alloc_buf[100000];
  271. #endif
  272. #if defined(MBEDTLS_SSL_CACHE_C)
  273. mbedtls_ssl_cache_context cache;
  274. #endif
  275. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  276. mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
  277. #endif
  278. #if defined(MBEDTLS_SSL_CACHE_C)
  279. mbedtls_ssl_cache_init( &cache );
  280. #endif
  281. mbedtls_x509_crt_init( &srvcert );
  282. mbedtls_x509_crt_init( &cachain );
  283. mbedtls_ssl_config_init( &conf );
  284. mbedtls_ctr_drbg_init( &ctr_drbg );
  285. memset( threads, 0, sizeof(threads) );
  286. mbedtls_net_init( &listen_fd );
  287. mbedtls_net_init( &client_fd );
  288. mbedtls_mutex_init( &debug_mutex );
  289. base_info.config = &conf;
  290. /*
  291. * We use only a single entropy source that is used in all the threads.
  292. */
  293. mbedtls_entropy_init( &entropy );
  294. /*
  295. * 1. Load the certificates and private RSA key
  296. */
  297. mbedtls_printf( "\n . Loading the server cert. and key..." );
  298. fflush( stdout );
  299. /*
  300. * This demonstration program uses embedded test certificates.
  301. * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
  302. * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
  303. */
  304. ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
  305. mbedtls_test_srv_crt_len );
  306. if( ret != 0 )
  307. {
  308. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
  309. goto exit;
  310. }
  311. ret = mbedtls_x509_crt_parse( &cachain, (const unsigned char *) mbedtls_test_cas_pem,
  312. mbedtls_test_cas_pem_len );
  313. if( ret != 0 )
  314. {
  315. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
  316. goto exit;
  317. }
  318. mbedtls_pk_init( &pkey );
  319. ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
  320. mbedtls_test_srv_key_len, NULL, 0 );
  321. if( ret != 0 )
  322. {
  323. mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned %d\n\n", ret );
  324. goto exit;
  325. }
  326. mbedtls_printf( " ok\n" );
  327. /*
  328. * 1b. Seed the random number generator
  329. */
  330. mbedtls_printf( " . Seeding the random number generator..." );
  331. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  332. (const unsigned char *) pers,
  333. strlen( pers ) ) ) != 0 )
  334. {
  335. mbedtls_printf( " failed: mbedtls_ctr_drbg_seed returned -0x%04x\n",
  336. -ret );
  337. goto exit;
  338. }
  339. mbedtls_printf( " ok\n" );
  340. /*
  341. * 1c. Prepare SSL configuration
  342. */
  343. mbedtls_printf( " . Setting up the SSL data...." );
  344. if( ( ret = mbedtls_ssl_config_defaults( &conf,
  345. MBEDTLS_SSL_IS_SERVER,
  346. MBEDTLS_SSL_TRANSPORT_STREAM,
  347. MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
  348. {
  349. mbedtls_printf( " failed: mbedtls_ssl_config_defaults returned -0x%04x\n",
  350. -ret );
  351. goto exit;
  352. }
  353. mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
  354. mbedtls_ssl_conf_dbg( &conf, my_mutexed_debug, stdout );
  355. /* mbedtls_ssl_cache_get() and mbedtls_ssl_cache_set() are thread-safe if
  356. * MBEDTLS_THREADING_C is set.
  357. */
  358. #if defined(MBEDTLS_SSL_CACHE_C)
  359. mbedtls_ssl_conf_session_cache( &conf, &cache,
  360. mbedtls_ssl_cache_get,
  361. mbedtls_ssl_cache_set );
  362. #endif
  363. mbedtls_ssl_conf_ca_chain( &conf, &cachain, NULL );
  364. if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
  365. {
  366. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
  367. goto exit;
  368. }
  369. mbedtls_printf( " ok\n" );
  370. /*
  371. * 2. Setup the listening TCP socket
  372. */
  373. mbedtls_printf( " . Bind on https://localhost:4433/ ..." );
  374. fflush( stdout );
  375. if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
  376. {
  377. mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
  378. goto exit;
  379. }
  380. mbedtls_printf( " ok\n" );
  381. reset:
  382. #ifdef MBEDTLS_ERROR_C
  383. if( ret != 0 )
  384. {
  385. char error_buf[100];
  386. mbedtls_strerror( ret, error_buf, 100 );
  387. mbedtls_printf( " [ main ] Last error was: -0x%04x - %s\n", -ret, error_buf );
  388. }
  389. #endif
  390. /*
  391. * 3. Wait until a client connects
  392. */
  393. mbedtls_printf( " [ main ] Waiting for a remote connection\n" );
  394. if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
  395. NULL, 0, NULL ) ) != 0 )
  396. {
  397. mbedtls_printf( " [ main ] failed: mbedtls_net_accept returned -0x%04x\n", ret );
  398. goto exit;
  399. }
  400. mbedtls_printf( " [ main ] ok\n" );
  401. mbedtls_printf( " [ main ] Creating a new thread\n" );
  402. if( ( ret = thread_create( &client_fd ) ) != 0 )
  403. {
  404. mbedtls_printf( " [ main ] failed: thread_create returned %d\n", ret );
  405. mbedtls_net_free( &client_fd );
  406. goto reset;
  407. }
  408. ret = 0;
  409. goto reset;
  410. exit:
  411. mbedtls_x509_crt_free( &srvcert );
  412. mbedtls_pk_free( &pkey );
  413. #if defined(MBEDTLS_SSL_CACHE_C)
  414. mbedtls_ssl_cache_free( &cache );
  415. #endif
  416. mbedtls_ctr_drbg_free( &ctr_drbg );
  417. mbedtls_entropy_free( &entropy );
  418. mbedtls_ssl_config_free( &conf );
  419. mbedtls_net_free( &listen_fd );
  420. mbedtls_mutex_free( &debug_mutex );
  421. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  422. mbedtls_memory_buffer_alloc_free();
  423. #endif
  424. #if defined(_WIN32)
  425. mbedtls_printf( " Press Enter to exit this program.\n" );
  426. fflush( stdout ); getchar();
  427. #endif
  428. return( ret );
  429. }
  430. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
  431. MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
  432. MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_THREADING_C &&
  433. MBEDTLS_THREADING_PTHREAD && MBEDTLS_PEM_PARSE_C */