blob: f0a3658a3e60b3e72fd08ce4a103fcdd6c566ca6 [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 Rodgman7ff79652023-11-03 12:04:52 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakkerf9c49532013-12-19 15:40:58 +01007 */
8
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020011#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020013#endif
Paul Bakkerf9c49532013-12-19 15:40:58 +010014
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000015#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_CERTS_C) || \
18 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_SSL_TLS_C) || \
19 !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_NET_C) || \
20 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
21 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
22 !defined(MBEDTLS_THREADING_C) || !defined(MBEDTLS_THREADING_PTHREAD) || \
23 !defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010024int main(void)
Paul Bakkerf9c49532013-12-19 15:40:58 +010025{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_CERTS_C and/or MBEDTLS_ENTROPY_C "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010027 "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
28 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
29 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
30 "MBEDTLS_THREADING_C and/or MBEDTLS_THREADING_PTHREAD "
31 "and/or MBEDTLS_PEM_PARSE_C not defined.\n");
32 mbedtls_exit(0);
Paul Bakkerf9c49532013-12-19 15:40:58 +010033}
34#else
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010035
36#include <stdlib.h>
37#include <string.h>
38
39#if defined(_WIN32)
40#include <windows.h>
41#endif
42
43#include "mbedtls/entropy.h"
44#include "mbedtls/ctr_drbg.h"
45#include "mbedtls/certs.h"
46#include "mbedtls/x509.h"
47#include "mbedtls/ssl.h"
Andres AG788aa4a2016-09-14 14:32:09 +010048#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010049#include "mbedtls/error.h"
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010052#include "mbedtls/ssl_cache.h"
53#endif
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010056#include "mbedtls/memory_buffer_alloc.h"
57#endif
58
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010059
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010060#define HTTP_RESPONSE \
61 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Gilles Peskinef08ca832023-09-12 19:21:54 +020062 "<h2>Mbed TLS Test Server</h2>\r\n" \
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010063 "<p>Successful connection using: %s</p>\r\n"
64
65#define DEBUG_LEVEL 0
66
67#define MAX_NUM_THREADS 5
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069mbedtls_threading_mutex_t debug_mutex;
Paul Bakkerf9c49532013-12-19 15:40:58 +010070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071static void my_mutexed_debug(void *ctx, int level,
72 const char *file, int line,
73 const char *str)
Paul Bakkerf9c49532013-12-19 15:40:58 +010074{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020075 long int thread_id = (long int) pthread_self();
76
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 mbedtls_mutex_lock(&debug_mutex);
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020078
79 ((void) level);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 mbedtls_fprintf((FILE *) ctx, "%s:%04d: [ #%ld ] %s",
81 file, line, thread_id, str);
82 fflush((FILE *) ctx);
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020083
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 mbedtls_mutex_unlock(&debug_mutex);
Paul Bakkerf9c49532013-12-19 15:40:58 +010085}
86
87typedef struct {
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020088 mbedtls_net_context client_fd;
Paul Bakkerf9c49532013-12-19 15:40:58 +010089 int thread_complete;
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +020090 const mbedtls_ssl_config *config;
Paul Bakkerf9c49532013-12-19 15:40:58 +010091} thread_info_t;
92
93typedef struct {
94 int active;
95 thread_info_t data;
96 pthread_t thread;
97} pthread_info_t;
98
Paul Bakkerf9c49532013-12-19 15:40:58 +010099static thread_info_t base_info;
100static pthread_info_t threads[MAX_NUM_THREADS];
101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102static void *handle_ssl_connection(void *data)
Paul Bakkerf9c49532013-12-19 15:40:58 +0100103{
104 int ret, len;
105 thread_info_t *thread_info = (thread_info_t *) data;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200106 mbedtls_net_context *client_fd = &thread_info->client_fd;
Manuel Pégourié-Gonnard30eceb72015-05-11 14:42:56 +0200107 long int thread_id = (long int) pthread_self();
Paul Bakkerf9c49532013-12-19 15:40:58 +0100108 unsigned char buf[1024];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_ssl_context ssl;
Paul Bakkerf9c49532013-12-19 15:40:58 +0100110
Manuel Pégourié-Gonnard955028f2014-07-12 01:27:34 +0200111 /* Make sure memory references are valid */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 mbedtls_ssl_init(&ssl);
Manuel Pégourié-Gonnard955028f2014-07-12 01:27:34 +0200113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 mbedtls_printf(" [ #%ld ] Setting up SSL/TLS data\n", thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100115
116 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200117 * 4. Get the SSL context ready
Paul Bakkerf9c49532013-12-19 15:40:58 +0100118 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 if ((ret = mbedtls_ssl_setup(&ssl, thread_info->config)) != 0) {
120 mbedtls_printf(" [ #%ld ] failed: mbedtls_ssl_setup returned -0x%04x\n",
121 thread_id, (unsigned int) -ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100122 goto thread_exit;
123 }
124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 mbedtls_ssl_set_bio(&ssl, client_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100126
Paul Bakkerf9c49532013-12-19 15:40:58 +0100127 /*
128 * 5. Handshake
129 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 mbedtls_printf(" [ #%ld ] Performing the SSL/TLS handshake\n", thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
133 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
134 mbedtls_printf(" [ #%ld ] failed: mbedtls_ssl_handshake returned -0x%04x\n",
135 thread_id, (unsigned int) -ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100136 goto thread_exit;
137 }
138 }
139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140 mbedtls_printf(" [ #%ld ] ok\n", thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100141
142 /*
143 * 6. Read the HTTP Request
144 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145 mbedtls_printf(" [ #%ld ] < Read from client\n", thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100152 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
Paul Bakkerf9c49532013-12-19 15:40:58 +0100153 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154 }
Paul Bakkerf9c49532013-12-19 15:40:58 +0100155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100176 mbedtls_printf(" [ #%ld ] %d bytes read\n=====\n%s\n=====\n",
177 thread_id, len, (char *) buf);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 if (ret > 0) {
Paul Bakkerf9c49532013-12-19 15:40:58 +0100180 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181 }
182 } while (1);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100183
184 /*
185 * 7. Write the 200 Response
186 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 mbedtls_printf(" [ #%ld ] > Write to client:\n", thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 len = sprintf((char *) buf, HTTP_RESPONSE,
190 mbedtls_ssl_get_ciphersuite(&ssl));
Paul Bakkerf9c49532013-12-19 15:40:58 +0100191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
193 if (ret == MBEDTLS_ERR_NET_CONN_RESET) {
194 mbedtls_printf(" [ #%ld ] failed: peer closed the connection\n",
195 thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100196 goto thread_exit;
197 }
198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
200 mbedtls_printf(" [ #%ld ] failed: mbedtls_ssl_write returned -0x%04x\n",
201 thread_id, (unsigned int) ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100202 goto thread_exit;
203 }
204 }
205
206 len = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 mbedtls_printf(" [ #%ld ] %d bytes written\n=====\n%s\n=====\n",
208 thread_id, len, (char *) buf);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 mbedtls_printf(" [ #%ld ] . Closing the connection...", thread_id);
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +0100211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212 while ((ret = mbedtls_ssl_close_notify(&ssl)) < 0) {
213 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
214 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
215 mbedtls_printf(" [ #%ld ] failed: mbedtls_ssl_close_notify returned -0x%04x\n",
216 thread_id, (unsigned int) ret);
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +0100217 goto thread_exit;
218 }
219 }
220
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +0100222
Paul Bakkerf9c49532013-12-19 15:40:58 +0100223 ret = 0;
224
225thread_exit:
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#ifdef MBEDTLS_ERROR_C
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 if (ret != 0) {
Paul Bakkerf9c49532013-12-19 15:40:58 +0100229 char error_buf[100];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230 mbedtls_strerror(ret, error_buf, 100);
Manuel Pégourié-Gonnard30eceb72015-05-11 14:42:56 +0200231 mbedtls_printf(" [ #%ld ] Last error was: -0x%04x - %s\n\n",
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232 thread_id, (unsigned int) -ret, error_buf);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100233 }
234#endif
235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236 mbedtls_net_free(client_fd);
237 mbedtls_ssl_free(&ssl);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100238
239 thread_info->thread_complete = 1;
240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 return NULL;
Paul Bakkerf9c49532013-12-19 15:40:58 +0100242}
243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244static int thread_create(mbedtls_net_context *client_fd)
Paul Bakkerf9c49532013-12-19 15:40:58 +0100245{
246 int ret, i;
247
248 /*
249 * Find in-active or finished thread slot
250 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 for (i = 0; i < MAX_NUM_THREADS; i++) {
252 if (threads[i].active == 0) {
Paul Bakkerf9c49532013-12-19 15:40:58 +0100253 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 }
Paul Bakkerf9c49532013-12-19 15:40:58 +0100255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 if (threads[i].data.thread_complete == 1) {
257 mbedtls_printf(" [ main ] Cleaning up thread %d\n", i);
258 pthread_join(threads[i].thread, NULL);
259 memset(&threads[i], 0, sizeof(pthread_info_t));
Paul Bakkerf9c49532013-12-19 15:40:58 +0100260 break;
261 }
262 }
263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 if (i == MAX_NUM_THREADS) {
265 return -1;
266 }
Paul Bakkerf9c49532013-12-19 15:40:58 +0100267
268 /*
269 * Fill thread-info for thread
270 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 memcpy(&threads[i].data, &base_info, sizeof(base_info));
Paul Bakkerf9c49532013-12-19 15:40:58 +0100272 threads[i].active = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 memcpy(&threads[i].data.client_fd, client_fd, sizeof(mbedtls_net_context));
Paul Bakkerf9c49532013-12-19 15:40:58 +0100274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 if ((ret = pthread_create(&threads[i].thread, NULL, handle_ssl_connection,
276 &threads[i].data)) != 0) {
277 return ret;
Paul Bakkerf9c49532013-12-19 15:40:58 +0100278 }
279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 return 0;
Paul Bakkerf9c49532013-12-19 15:40:58 +0100281}
282
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283int main(void)
Paul Bakkerf9c49532013-12-19 15:40:58 +0100284{
285 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200286 mbedtls_net_context listen_fd, client_fd;
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200287 const char pers[] = "ssl_pthread_server";
Paul Bakkerf9c49532013-12-19 15:40:58 +0100288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 mbedtls_entropy_context entropy;
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200290 mbedtls_ctr_drbg_context ctr_drbg;
291 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_x509_crt srvcert;
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200293 mbedtls_x509_crt cachain;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_pk_context pkey;
295#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Paul Bakkerf9c49532013-12-19 15:40:58 +0100296 unsigned char alloc_buf[100000];
297#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#if defined(MBEDTLS_SSL_CACHE_C)
299 mbedtls_ssl_cache_context cache;
Paul Bakkerf9c49532013-12-19 15:40:58 +0100300#endif
301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
Paul Bakkerf9c49532013-12-19 15:40:58 +0100304#endif
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#if defined(MBEDTLS_SSL_CACHE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 mbedtls_ssl_cache_init(&cache);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100308#endif
309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 mbedtls_x509_crt_init(&srvcert);
311 mbedtls_x509_crt_init(&cachain);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 mbedtls_ssl_config_init(&conf);
314 mbedtls_ctr_drbg_init(&ctr_drbg);
315 memset(threads, 0, sizeof(threads));
316 mbedtls_net_init(&listen_fd);
317 mbedtls_net_init(&client_fd);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319 mbedtls_mutex_init(&debug_mutex);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100320
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200321 base_info.config = &conf;
322
Paul Bakkerf9c49532013-12-19 15:40:58 +0100323 /*
324 * We use only a single entropy source that is used in all the threads.
325 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100326 mbedtls_entropy_init(&entropy);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100327
Przemek Stekielb00688f2023-04-17 11:10:05 +0200328#if defined(MBEDTLS_USE_PSA_CRYPTO)
329 psa_status_t status = psa_crypto_init();
330 if (status != PSA_SUCCESS) {
331 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
332 (int) status);
333 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
334 goto exit;
335 }
336#endif /* MBEDTLS_USE_PSA_CRYPTO */
337
Paul Bakkerf9c49532013-12-19 15:40:58 +0100338 /*
339 * 1. Load the certificates and private RSA key
340 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 mbedtls_printf("\n . Loading the server cert. and key...");
342 fflush(stdout);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100343
Paul Bakkerf9c49532013-12-19 15:40:58 +0100344 /*
345 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
347 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakkerf9c49532013-12-19 15:40:58 +0100348 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_srv_crt,
350 mbedtls_test_srv_crt_len);
351 if (ret != 0) {
352 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100353 goto exit;
354 }
355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 ret = mbedtls_x509_crt_parse(&cachain, (const unsigned char *) mbedtls_test_cas_pem,
357 mbedtls_test_cas_pem_len);
358 if (ret != 0) {
359 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100360 goto exit;
361 }
362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 mbedtls_pk_init(&pkey);
364 ret = mbedtls_pk_parse_key(&pkey, (const unsigned char *) mbedtls_test_srv_key,
365 mbedtls_test_srv_key_len, NULL, 0);
366 if (ret != 0) {
367 mbedtls_printf(" failed\n ! mbedtls_pk_parse_key returned %d\n\n", ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100368 goto exit;
369 }
370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200372
373 /*
374 * 1b. Seed the random number generator
375 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 mbedtls_printf(" . Seeding the random number generator...");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
379 (const unsigned char *) pers,
380 strlen(pers))) != 0) {
381 mbedtls_printf(" failed: mbedtls_ctr_drbg_seed returned -0x%04x\n",
382 (unsigned int) -ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200383 goto exit;
384 }
Paul Bakkerf9c49532013-12-19 15:40:58 +0100385
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100386 mbedtls_printf(" ok\n");
Paul Bakkerf9c49532013-12-19 15:40:58 +0100387
388 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200389 * 1c. Prepare SSL configuration
390 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 mbedtls_printf(" . Setting up the SSL data....");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 if ((ret = mbedtls_ssl_config_defaults(&conf,
394 MBEDTLS_SSL_IS_SERVER,
395 MBEDTLS_SSL_TRANSPORT_STREAM,
396 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
397 mbedtls_printf(" failed: mbedtls_ssl_config_defaults returned -0x%04x\n",
398 (unsigned int) -ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200399 goto exit;
400 }
401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100402 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
403 mbedtls_ssl_conf_dbg(&conf, my_mutexed_debug, stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200404
405 /* mbedtls_ssl_cache_get() and mbedtls_ssl_cache_set() are thread-safe if
406 * MBEDTLS_THREADING_C is set.
407 */
408#if defined(MBEDTLS_SSL_CACHE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 mbedtls_ssl_conf_session_cache(&conf, &cache,
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200410 mbedtls_ssl_cache_get,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 mbedtls_ssl_cache_set);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200412#endif
413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414 mbedtls_ssl_conf_ca_chain(&conf, &cachain, NULL);
415 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey)) != 0) {
416 mbedtls_printf(" failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200417 goto exit;
418 }
419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200421
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200422 /*
Paul Bakkerf9c49532013-12-19 15:40:58 +0100423 * 2. Setup the listening TCP socket
424 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 mbedtls_printf(" . Bind on https://localhost:4433/ ...");
426 fflush(stdout);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100427
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 if ((ret = mbedtls_net_bind(&listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP)) != 0) {
429 mbedtls_printf(" failed\n ! mbedtls_net_bind returned %d\n\n", ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100430 goto exit;
431 }
432
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100433 mbedtls_printf(" ok\n");
Paul Bakkerf9c49532013-12-19 15:40:58 +0100434
435reset:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436#ifdef MBEDTLS_ERROR_C
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100437 if (ret != 0) {
Paul Bakkerf9c49532013-12-19 15:40:58 +0100438 char error_buf[100];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439 mbedtls_strerror(ret, error_buf, 100);
440 mbedtls_printf(" [ main ] Last error was: -0x%04x - %s\n", (unsigned int) -ret,
441 error_buf);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100442 }
443#endif
444
445 /*
446 * 3. Wait until a client connects
447 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448 mbedtls_printf(" [ main ] Waiting for a remote connection\n");
Paul Bakkerf9c49532013-12-19 15:40:58 +0100449
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
451 NULL, 0, NULL)) != 0) {
452 mbedtls_printf(" [ main ] failed: mbedtls_net_accept returned -0x%04x\n",
453 (unsigned int) ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100454 goto exit;
455 }
456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 mbedtls_printf(" [ main ] ok\n");
458 mbedtls_printf(" [ main ] Creating a new thread\n");
Paul Bakkerf9c49532013-12-19 15:40:58 +0100459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100460 if ((ret = thread_create(&client_fd)) != 0) {
461 mbedtls_printf(" [ main ] failed: thread_create returned %d\n", ret);
462 mbedtls_net_free(&client_fd);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100463 goto reset;
464 }
465
466 ret = 0;
467 goto reset;
468
469exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100470 mbedtls_x509_crt_free(&srvcert);
471 mbedtls_pk_free(&pkey);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#if defined(MBEDTLS_SSL_CACHE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100473 mbedtls_ssl_cache_free(&cache);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100474#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100475 mbedtls_ctr_drbg_free(&ctr_drbg);
476 mbedtls_entropy_free(&entropy);
477 mbedtls_ssl_config_free(&conf);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 mbedtls_net_free(&listen_fd);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100479 mbedtls_mutex_free(&debug_mutex);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
481 mbedtls_memory_buffer_alloc_free();
Paul Bakkerf9c49532013-12-19 15:40:58 +0100482#endif
Przemek Stekield4d049b2023-04-19 13:47:43 +0200483#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielc4ddf922023-04-19 10:15:26 +0200484 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200485#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerf9c49532013-12-19 15:40:58 +0100486
487#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488 mbedtls_printf(" Press Enter to exit this program.\n");
489 fflush(stdout); getchar();
Paul Bakkerf9c49532013-12-19 15:40:58 +0100490#endif
491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100492 mbedtls_exit(ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100493}
494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
496 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
497 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_THREADING_C &&
498 MBEDTLS_THREADING_PTHREAD && MBEDTLS_PEM_PARSE_C */