blob: 1214eb83fa46ae6f1b65f8fc77caebe9065f6b7d [file] [log] [blame]
Paul Bakkerf9c49532013-12-19 15:40:58 +01001/*
2 * SSL server demonstration program using pthread for handling multiple
3 * clients.
4 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakkerf9c49532013-12-19 15:40:58 +01007 */
8
Felix Conway998760a2025-03-24 11:37:33 +00009#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
10
Bence Szépkútic662b362021-05-27 11:25:03 +020011#include "mbedtls/build_info.h"
Paul Bakkerf9c49532013-12-19 15:40:58 +010012
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000013#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000014
Gilles Peskine3abca952024-09-04 16:31:06 +020015#if !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
16 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_SSL_SRV_C) || \
17 !defined(MBEDTLS_PEM_PARSE_C) || !defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010018int main(void)
Paul Bakkerf9c49532013-12-19 15:40:58 +010019{
Gilles Peskine3abca952024-09-04 16:31:06 +020020 mbedtls_printf("MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
21 "MBEDTLS_NET_C and/or MBEDTLS_SSL_SRV_C and/or "
22 "MBEDTLS_PEM_PARSE_C and/or MBEDTLS_X509_CRT_PARSE_C "
23 "not defined.\n");
24 mbedtls_exit(0);
25}
26#elif !defined(MBEDTLS_THREADING_C) || !defined(MBEDTLS_THREADING_PTHREAD)
27int main(void)
28{
29 mbedtls_printf("MBEDTLS_THREADING_PTHREAD not defined.\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010030 mbedtls_exit(0);
Paul Bakkerf9c49532013-12-19 15:40:58 +010031}
32#else
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010033
34#include <stdlib.h>
35#include <string.h>
36
37#if defined(_WIN32)
38#include <windows.h>
39#endif
40
41#include "mbedtls/entropy.h"
42#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010043#include "mbedtls/x509.h"
44#include "mbedtls/ssl.h"
Andres AG788aa4a2016-09-14 14:32:09 +010045#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010046#include "mbedtls/error.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010047#include "test/certs.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010050#include "mbedtls/ssl_cache.h"
51#endif
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010054#include "mbedtls/memory_buffer_alloc.h"
55#endif
56
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010057
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010058#define HTTP_RESPONSE \
59 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Gilles Peskinee820c0a2023-08-03 17:45:20 +020060 "<h2>Mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010061 "<p>Successful connection using: %s</p>\r\n"
62
63#define DEBUG_LEVEL 0
64
65#define MAX_NUM_THREADS 5
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067mbedtls_threading_mutex_t debug_mutex;
Paul Bakkerf9c49532013-12-19 15:40:58 +010068
Gilles Peskine449bd832023-01-11 14:50:10 +010069static void my_mutexed_debug(void *ctx, int level,
70 const char *file, int line,
71 const char *str)
Paul Bakkerf9c49532013-12-19 15:40:58 +010072{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020073 long int thread_id = (long int) pthread_self();
74
Gilles Peskine449bd832023-01-11 14:50:10 +010075 mbedtls_mutex_lock(&debug_mutex);
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020076
77 ((void) level);
Gilles Peskine449bd832023-01-11 14:50:10 +010078 mbedtls_fprintf((FILE *) ctx, "%s:%04d: [ #%ld ] %s",
79 file, line, thread_id, str);
80 fflush((FILE *) ctx);
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 mbedtls_mutex_unlock(&debug_mutex);
Paul Bakkerf9c49532013-12-19 15:40:58 +010083}
84
85typedef struct {
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020086 mbedtls_net_context client_fd;
Paul Bakkerf9c49532013-12-19 15:40:58 +010087 int thread_complete;
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +020088 const mbedtls_ssl_config *config;
Paul Bakkerf9c49532013-12-19 15:40:58 +010089} thread_info_t;
90
91typedef struct {
92 int active;
93 thread_info_t data;
94 pthread_t thread;
95} pthread_info_t;
96
Paul Bakkerf9c49532013-12-19 15:40:58 +010097static thread_info_t base_info;
98static pthread_info_t threads[MAX_NUM_THREADS];
99
Gilles Peskine449bd832023-01-11 14:50:10 +0100100static void *handle_ssl_connection(void *data)
Paul Bakkerf9c49532013-12-19 15:40:58 +0100101{
102 int ret, len;
103 thread_info_t *thread_info = (thread_info_t *) data;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200104 mbedtls_net_context *client_fd = &thread_info->client_fd;
Manuel Pégourié-Gonnard30eceb72015-05-11 14:42:56 +0200105 long int thread_id = (long int) pthread_self();
Paul Bakkerf9c49532013-12-19 15:40:58 +0100106 unsigned char buf[1024];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_ssl_context ssl;
Paul Bakkerf9c49532013-12-19 15:40:58 +0100108
Manuel Pégourié-Gonnard955028f2014-07-12 01:27:34 +0200109 /* Make sure memory references are valid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 mbedtls_ssl_init(&ssl);
Manuel Pégourié-Gonnard955028f2014-07-12 01:27:34 +0200111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 mbedtls_printf(" [ #%ld ] Setting up SSL/TLS data\n", thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100113
114 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200115 * 4. Get the SSL context ready
Paul Bakkerf9c49532013-12-19 15:40:58 +0100116 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 if ((ret = mbedtls_ssl_setup(&ssl, thread_info->config)) != 0) {
118 mbedtls_printf(" [ #%ld ] failed: mbedtls_ssl_setup returned -0x%04x\n",
119 thread_id, (unsigned int) -ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100120 goto thread_exit;
121 }
122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 mbedtls_ssl_set_bio(&ssl, client_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100124
Paul Bakkerf9c49532013-12-19 15:40:58 +0100125 /*
126 * 5. Handshake
127 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 mbedtls_printf(" [ #%ld ] Performing the SSL/TLS handshake\n", thread_id);
Gilles Peskine3abca952024-09-04 16:31:06 +0200129 fflush(stdout);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
132 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
133 mbedtls_printf(" [ #%ld ] failed: mbedtls_ssl_handshake returned -0x%04x\n",
134 thread_id, (unsigned int) -ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100135 goto thread_exit;
136 }
137 }
138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 mbedtls_printf(" [ #%ld ] ok\n", thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100140
141 /*
142 * 6. Read the HTTP Request
143 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 mbedtls_printf(" [ #%ld ] < Read from client\n", thread_id);
Gilles Peskine3abca952024-09-04 16:31:06 +0200145 fflush(stdout);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 do {
148 len = sizeof(buf) - 1;
149 memset(buf, 0, sizeof(buf));
150 ret = mbedtls_ssl_read(&ssl, buf, len);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
Paul Bakkerf9c49532013-12-19 15:40:58 +0100153 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 }
Paul Bakkerf9c49532013-12-19 15:40:58 +0100155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 if (ret <= 0) {
157 switch (ret) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 mbedtls_printf(" [ #%ld ] connection was closed gracefully\n",
160 thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100161 goto thread_exit;
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 case MBEDTLS_ERR_NET_CONN_RESET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 mbedtls_printf(" [ #%ld ] connection was reset by peer\n",
165 thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100166 goto thread_exit;
167
168 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 mbedtls_printf(" [ #%ld ] mbedtls_ssl_read returned -0x%04x\n",
170 thread_id, (unsigned int) -ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100171 goto thread_exit;
172 }
Paul Bakkerf9c49532013-12-19 15:40:58 +0100173 }
174
175 len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 mbedtls_printf(" [ #%ld ] %d bytes read\n=====\n%s\n=====\n",
177 thread_id, len, (char *) buf);
Gilles Peskine3abca952024-09-04 16:31:06 +0200178 fflush(stdout);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (ret > 0) {
Paul Bakkerf9c49532013-12-19 15:40:58 +0100181 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 }
183 } while (1);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100184
185 /*
186 * 7. Write the 200 Response
187 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 mbedtls_printf(" [ #%ld ] > Write to client:\n", thread_id);
Gilles Peskine3abca952024-09-04 16:31:06 +0200189 fflush(stdout);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 len = sprintf((char *) buf, HTTP_RESPONSE,
192 mbedtls_ssl_get_ciphersuite(&ssl));
Paul Bakkerf9c49532013-12-19 15:40:58 +0100193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
195 if (ret == MBEDTLS_ERR_NET_CONN_RESET) {
196 mbedtls_printf(" [ #%ld ] failed: peer closed the connection\n",
197 thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100198 goto thread_exit;
199 }
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
202 mbedtls_printf(" [ #%ld ] failed: mbedtls_ssl_write returned -0x%04x\n",
203 thread_id, (unsigned int) ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100204 goto thread_exit;
205 }
206 }
207
208 len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 mbedtls_printf(" [ #%ld ] %d bytes written\n=====\n%s\n=====\n",
210 thread_id, len, (char *) buf);
Gilles Peskine3abca952024-09-04 16:31:06 +0200211 fflush(stdout);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 mbedtls_printf(" [ #%ld ] . Closing the connection...", thread_id);
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +0100214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 while ((ret = mbedtls_ssl_close_notify(&ssl)) < 0) {
216 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
217 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
218 mbedtls_printf(" [ #%ld ] failed: mbedtls_ssl_close_notify returned -0x%04x\n",
219 thread_id, (unsigned int) ret);
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +0100220 goto thread_exit;
221 }
222 }
223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 mbedtls_printf(" ok\n");
Gilles Peskine3abca952024-09-04 16:31:06 +0200225 fflush(stdout);
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +0100226
Paul Bakkerf9c49532013-12-19 15:40:58 +0100227 ret = 0;
228
229thread_exit:
230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 if (ret != 0) {
Paul Bakkerf9c49532013-12-19 15:40:58 +0100233 char error_buf[100];
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 mbedtls_strerror(ret, error_buf, 100);
Manuel Pégourié-Gonnard30eceb72015-05-11 14:42:56 +0200235 mbedtls_printf(" [ #%ld ] Last error was: -0x%04x - %s\n\n",
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 thread_id, (unsigned int) -ret, error_buf);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100237 }
238#endif
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 mbedtls_net_free(client_fd);
241 mbedtls_ssl_free(&ssl);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100242
243 thread_info->thread_complete = 1;
244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return NULL;
Paul Bakkerf9c49532013-12-19 15:40:58 +0100246}
247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248static int thread_create(mbedtls_net_context *client_fd)
Paul Bakkerf9c49532013-12-19 15:40:58 +0100249{
250 int ret, i;
251
252 /*
253 * Find in-active or finished thread slot
254 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 for (i = 0; i < MAX_NUM_THREADS; i++) {
256 if (threads[i].active == 0) {
Paul Bakkerf9c49532013-12-19 15:40:58 +0100257 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 }
Paul Bakkerf9c49532013-12-19 15:40:58 +0100259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (threads[i].data.thread_complete == 1) {
261 mbedtls_printf(" [ main ] Cleaning up thread %d\n", i);
262 pthread_join(threads[i].thread, NULL);
263 memset(&threads[i], 0, sizeof(pthread_info_t));
Paul Bakkerf9c49532013-12-19 15:40:58 +0100264 break;
265 }
266 }
267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if (i == MAX_NUM_THREADS) {
269 return -1;
270 }
Paul Bakkerf9c49532013-12-19 15:40:58 +0100271
272 /*
273 * Fill thread-info for thread
274 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 memcpy(&threads[i].data, &base_info, sizeof(base_info));
Paul Bakkerf9c49532013-12-19 15:40:58 +0100276 threads[i].active = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 memcpy(&threads[i].data.client_fd, client_fd, sizeof(mbedtls_net_context));
Paul Bakkerf9c49532013-12-19 15:40:58 +0100278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if ((ret = pthread_create(&threads[i].thread, NULL, handle_ssl_connection,
280 &threads[i].data)) != 0) {
281 return ret;
Paul Bakkerf9c49532013-12-19 15:40:58 +0100282 }
283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return 0;
Paul Bakkerf9c49532013-12-19 15:40:58 +0100285}
286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287int main(void)
Paul Bakkerf9c49532013-12-19 15:40:58 +0100288{
289 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200290 mbedtls_net_context listen_fd, client_fd;
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200291 const char pers[] = "ssl_pthread_server";
Paul Bakkerf9c49532013-12-19 15:40:58 +0100292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_entropy_context entropy;
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200294 mbedtls_ctr_drbg_context ctr_drbg;
295 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_x509_crt srvcert;
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200297 mbedtls_x509_crt cachain;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_pk_context pkey;
299#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Paul Bakkerf9c49532013-12-19 15:40:58 +0100300 unsigned char alloc_buf[100000];
301#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#if defined(MBEDTLS_SSL_CACHE_C)
303 mbedtls_ssl_cache_context cache;
Paul Bakkerf9c49532013-12-19 15:40:58 +0100304#endif
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
Paul Bakkerf9c49532013-12-19 15:40:58 +0100308#endif
309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310#if defined(MBEDTLS_SSL_CACHE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 mbedtls_ssl_cache_init(&cache);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100312#endif
313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 mbedtls_x509_crt_init(&srvcert);
315 mbedtls_x509_crt_init(&cachain);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 mbedtls_ssl_config_init(&conf);
318 mbedtls_ctr_drbg_init(&ctr_drbg);
319 memset(threads, 0, sizeof(threads));
320 mbedtls_net_init(&listen_fd);
321 mbedtls_net_init(&client_fd);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 mbedtls_mutex_init(&debug_mutex);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100324
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200325 base_info.config = &conf;
326
Paul Bakkerf9c49532013-12-19 15:40:58 +0100327 /*
328 * We use only a single entropy source that is used in all the threads.
329 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 mbedtls_entropy_init(&entropy);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100331
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +0200332 psa_status_t status = psa_crypto_init();
333 if (status != PSA_SUCCESS) {
334 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
335 (int) status);
336 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
337 goto exit;
338 }
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +0200339
Paul Bakkerf9c49532013-12-19 15:40:58 +0100340 /*
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200341 * 1a. Seed the random number generator
342 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 mbedtls_printf(" . Seeding the random number generator...");
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
346 (const unsigned char *) pers,
347 strlen(pers))) != 0) {
348 mbedtls_printf(" failed: mbedtls_ctr_drbg_seed returned -0x%04x\n",
349 (unsigned int) -ret);
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200350 goto exit;
351 }
352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200354
355 /*
356 * 1b. Load the certificates and private RSA key
Paul Bakkerf9c49532013-12-19 15:40:58 +0100357 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 mbedtls_printf("\n . Loading the server cert. and key...");
359 fflush(stdout);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100360
Paul Bakkerf9c49532013-12-19 15:40:58 +0100361 /*
362 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
364 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakkerf9c49532013-12-19 15:40:58 +0100365 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_srv_crt,
367 mbedtls_test_srv_crt_len);
368 if (ret != 0) {
369 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100370 goto exit;
371 }
372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 ret = mbedtls_x509_crt_parse(&cachain, (const unsigned char *) mbedtls_test_cas_pem,
374 mbedtls_test_cas_pem_len);
375 if (ret != 0) {
376 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100377 goto exit;
378 }
379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 mbedtls_pk_init(&pkey);
381 ret = mbedtls_pk_parse_key(&pkey, (const unsigned char *) mbedtls_test_srv_key,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000382 mbedtls_test_srv_key_len, NULL, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if (ret != 0) {
384 mbedtls_printf(" failed\n ! mbedtls_pk_parse_key returned %d\n\n", ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100385 goto exit;
386 }
387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200389
390 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200391 * 1c. Prepare SSL configuration
392 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 mbedtls_printf(" . Setting up the SSL data....");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if ((ret = mbedtls_ssl_config_defaults(&conf,
396 MBEDTLS_SSL_IS_SERVER,
397 MBEDTLS_SSL_TRANSPORT_STREAM,
398 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
399 mbedtls_printf(" failed: mbedtls_ssl_config_defaults returned -0x%04x\n",
400 (unsigned int) -ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200401 goto exit;
402 }
403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
405 mbedtls_ssl_conf_dbg(&conf, my_mutexed_debug, stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200406
407 /* mbedtls_ssl_cache_get() and mbedtls_ssl_cache_set() are thread-safe if
408 * MBEDTLS_THREADING_C is set.
409 */
410#if defined(MBEDTLS_SSL_CACHE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 mbedtls_ssl_conf_session_cache(&conf, &cache,
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200412 mbedtls_ssl_cache_get,
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 mbedtls_ssl_cache_set);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200414#endif
415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 mbedtls_ssl_conf_ca_chain(&conf, &cachain, NULL);
417 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey)) != 0) {
418 mbedtls_printf(" failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200419 goto exit;
420 }
421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200423
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200424 /*
Paul Bakkerf9c49532013-12-19 15:40:58 +0100425 * 2. Setup the listening TCP socket
426 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 mbedtls_printf(" . Bind on https://localhost:4433/ ...");
428 fflush(stdout);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if ((ret = mbedtls_net_bind(&listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP)) != 0) {
431 mbedtls_printf(" failed\n ! mbedtls_net_bind returned %d\n\n", ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100432 goto exit;
433 }
434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 mbedtls_printf(" ok\n");
Paul Bakkerf9c49532013-12-19 15:40:58 +0100436
437reset:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (ret != 0) {
Paul Bakkerf9c49532013-12-19 15:40:58 +0100440 char error_buf[100];
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 mbedtls_strerror(ret, error_buf, 100);
442 mbedtls_printf(" [ main ] Last error was: -0x%04x - %s\n", (unsigned int) -ret,
443 error_buf);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100444 }
445#endif
446
447 /*
448 * 3. Wait until a client connects
449 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 mbedtls_printf(" [ main ] Waiting for a remote connection\n");
Gilles Peskine3abca952024-09-04 16:31:06 +0200451 fflush(stdout);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
454 NULL, 0, NULL)) != 0) {
455 mbedtls_printf(" [ main ] failed: mbedtls_net_accept returned -0x%04x\n",
456 (unsigned int) ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100457 goto exit;
458 }
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 mbedtls_printf(" [ main ] ok\n");
461 mbedtls_printf(" [ main ] Creating a new thread\n");
Paul Bakkerf9c49532013-12-19 15:40:58 +0100462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if ((ret = thread_create(&client_fd)) != 0) {
464 mbedtls_printf(" [ main ] failed: thread_create returned %d\n", ret);
465 mbedtls_net_free(&client_fd);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100466 goto reset;
467 }
468
469 ret = 0;
470 goto reset;
471
472exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 mbedtls_x509_crt_free(&srvcert);
474 mbedtls_pk_free(&pkey);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475#if defined(MBEDTLS_SSL_CACHE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 mbedtls_ssl_cache_free(&cache);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100477#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 mbedtls_ctr_drbg_free(&ctr_drbg);
479 mbedtls_entropy_free(&entropy);
480 mbedtls_ssl_config_free(&conf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 mbedtls_net_free(&listen_fd);
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 mbedtls_mutex_free(&debug_mutex);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
484 mbedtls_memory_buffer_alloc_free();
Paul Bakkerf9c49532013-12-19 15:40:58 +0100485#endif
Przemek Stekiela8c560a2023-04-19 10:15:26 +0200486 mbedtls_psa_crypto_free();
Paul Bakkerf9c49532013-12-19 15:40:58 +0100487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 mbedtls_exit(ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100489}
490
Gilles Peskine3abca952024-09-04 16:31:06 +0200491#endif /* configuration allows running this program */