blob: f4129459770a336eea8e93ba4e166c33cd3d157f [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02006 * 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.
Paul Bakkerf9c49532013-12-19 15:40:58 +010019 */
20
Bence Szépkútic662b362021-05-27 11:25:03 +020021#include "mbedtls/build_info.h"
Paul Bakkerf9c49532013-12-19 15:40:58 +010022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if defined(MBEDTLS_PLATFORM_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020024# include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000025#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020026# include <stdio.h>
27# include <stdlib.h>
28# define mbedtls_fprintf fprintf
29# define mbedtls_printf printf
30# define mbedtls_snprintf snprintf
31# define mbedtls_exit exit
32# define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
33# define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020036#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010037 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_C) || \
38 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
39 !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
40 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_THREADING_C) || \
41 !defined(MBEDTLS_THREADING_PTHREAD) || !defined(MBEDTLS_PEM_PARSE_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020042int main(void)
Paul Bakkerf9c49532013-12-19 15:40:58 +010043{
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010044 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C "
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020045 "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 mbedtls_exit(0);
Paul Bakkerf9c49532013-12-19 15:40:58 +010051}
52#else
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010053
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020054# include <stdlib.h>
55# include <string.h>
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010056
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020057# if defined(_WIN32)
58# include <windows.h>
59# endif
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010060
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020061# include "mbedtls/entropy.h"
62# include "mbedtls/ctr_drbg.h"
63# include "mbedtls/x509.h"
64# include "mbedtls/ssl.h"
65# include "mbedtls/net_sockets.h"
66# include "mbedtls/error.h"
67# include "test/certs.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010068
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020069# if defined(MBEDTLS_SSL_CACHE_C)
70# include "mbedtls/ssl_cache.h"
71# endif
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010072
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020073# if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
74# include "mbedtls/memory_buffer_alloc.h"
75# endif
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010076
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020077# define HTTP_RESPONSE \
78 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
79 "<h2>mbed TLS Test Server</h2>\r\n" \
80 "<p>Successful connection using: %s</p>\r\n"
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010081
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020082# define DEBUG_LEVEL 0
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010083
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020084# define MAX_NUM_THREADS 5
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086mbedtls_threading_mutex_t debug_mutex;
Paul Bakkerf9c49532013-12-19 15:40:58 +010087
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020088static void my_mutexed_debug(void *ctx,
89 int level,
90 const char *file,
91 int line,
92 const char *str)
Paul Bakkerf9c49532013-12-19 15:40:58 +010093{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020094 long int thread_id = (long int)pthread_self();
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020095
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020096 mbedtls_mutex_lock(&debug_mutex);
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020097
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020098 ((void)level);
99 mbedtls_fprintf((FILE *)ctx, "%s:%04d: [ #%ld ] %s", file, line, thread_id,
100 str);
101 fflush((FILE *)ctx);
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200102
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200103 mbedtls_mutex_unlock(&debug_mutex);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100104}
105
106typedef struct {
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200107 mbedtls_net_context client_fd;
Paul Bakkerf9c49532013-12-19 15:40:58 +0100108 int thread_complete;
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200109 const mbedtls_ssl_config *config;
Paul Bakkerf9c49532013-12-19 15:40:58 +0100110} thread_info_t;
111
112typedef struct {
113 int active;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200114 thread_info_t data;
115 pthread_t thread;
Paul Bakkerf9c49532013-12-19 15:40:58 +0100116} pthread_info_t;
117
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200118static thread_info_t base_info;
119static pthread_info_t threads[MAX_NUM_THREADS];
Paul Bakkerf9c49532013-12-19 15:40:58 +0100120
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200121static void *handle_ssl_connection(void *data)
Paul Bakkerf9c49532013-12-19 15:40:58 +0100122{
123 int ret, len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200124 thread_info_t *thread_info = (thread_info_t *)data;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200125 mbedtls_net_context *client_fd = &thread_info->client_fd;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200126 long int thread_id = (long int)pthread_self();
Paul Bakkerf9c49532013-12-19 15:40:58 +0100127 unsigned char buf[1024];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_ssl_context ssl;
Paul Bakkerf9c49532013-12-19 15:40:58 +0100129
Manuel Pégourié-Gonnard955028f2014-07-12 01:27:34 +0200130 /* Make sure memory references are valid */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200131 mbedtls_ssl_init(&ssl);
Manuel Pégourié-Gonnard955028f2014-07-12 01:27:34 +0200132
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200133 mbedtls_printf(" [ #%ld ] Setting up SSL/TLS data\n", thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100134
135 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200136 * 4. Get the SSL context ready
Paul Bakkerf9c49532013-12-19 15:40:58 +0100137 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200138 if ((ret = mbedtls_ssl_setup(&ssl, thread_info->config)) != 0) {
139 mbedtls_printf(
140 " [ #%ld ] failed: mbedtls_ssl_setup returned -0x%04x\n",
141 thread_id, (unsigned int)-ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100142 goto thread_exit;
143 }
144
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200145 mbedtls_ssl_set_bio(&ssl, client_fd, mbedtls_net_send, mbedtls_net_recv,
146 NULL);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100147
Paul Bakkerf9c49532013-12-19 15:40:58 +0100148 /*
149 * 5. Handshake
150 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200151 mbedtls_printf(" [ #%ld ] Performing the SSL/TLS handshake\n", thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100152
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200153 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
154 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
155 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
156 mbedtls_printf(
157 " [ #%ld ] failed: mbedtls_ssl_handshake returned -0x%04x\n",
158 thread_id, (unsigned int)-ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100159 goto thread_exit;
160 }
161 }
162
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200163 mbedtls_printf(" [ #%ld ] ok\n", thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100164
165 /*
166 * 6. Read the HTTP Request
167 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200168 mbedtls_printf(" [ #%ld ] < Read from client\n", thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100169
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200170 do {
171 len = sizeof(buf) - 1;
172 memset(buf, 0, sizeof(buf));
173 ret = mbedtls_ssl_read(&ssl, buf, len);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100174
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200175 if (ret == MBEDTLS_ERR_SSL_WANT_READ ||
176 ret == MBEDTLS_ERR_SSL_WANT_WRITE)
Paul Bakkerf9c49532013-12-19 15:40:58 +0100177 continue;
178
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200179 if (ret <= 0) {
180 switch (ret) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200182 mbedtls_printf(
183 " [ #%ld ] connection was closed gracefully\n",
184 thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100185 goto thread_exit;
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 case MBEDTLS_ERR_NET_CONN_RESET:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200188 mbedtls_printf(" [ #%ld ] connection was reset by peer\n",
189 thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100190 goto thread_exit;
191
192 default:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200193 mbedtls_printf(
194 " [ #%ld ] mbedtls_ssl_read returned -0x%04x\n",
195 thread_id, (unsigned int)-ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100196 goto thread_exit;
197 }
Paul Bakkerf9c49532013-12-19 15:40:58 +0100198 }
199
200 len = ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200201 mbedtls_printf(" [ #%ld ] %d bytes read\n=====\n%s\n=====\n",
202 thread_id, len, (char *)buf);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100203
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200204 if (ret > 0)
Paul Bakkerf9c49532013-12-19 15:40:58 +0100205 break;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200206 } while (1);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100207
208 /*
209 * 7. Write the 200 Response
210 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200211 mbedtls_printf(" [ #%ld ] > Write to client:\n", thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100212
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200213 len =
214 sprintf((char *)buf, HTTP_RESPONSE, mbedtls_ssl_get_ciphersuite(&ssl));
Paul Bakkerf9c49532013-12-19 15:40:58 +0100215
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200216 while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
217 if (ret == MBEDTLS_ERR_NET_CONN_RESET) {
218 mbedtls_printf(" [ #%ld ] failed: peer closed the connection\n",
219 thread_id);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100220 goto thread_exit;
221 }
222
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200223 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
224 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
225 mbedtls_printf(
226 " [ #%ld ] failed: mbedtls_ssl_write returned -0x%04x\n",
227 thread_id, (unsigned int)ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100228 goto thread_exit;
229 }
230 }
231
232 len = ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200233 mbedtls_printf(" [ #%ld ] %d bytes written\n=====\n%s\n=====\n",
234 thread_id, len, (char *)buf);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100235
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200236 mbedtls_printf(" [ #%ld ] . Closing the connection...", thread_id);
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +0100237
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200238 while ((ret = mbedtls_ssl_close_notify(&ssl)) < 0) {
239 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
240 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
241 mbedtls_printf(
242 " [ #%ld ] failed: mbedtls_ssl_close_notify returned -0x%04x\n",
243 thread_id, (unsigned int)ret);
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +0100244 goto thread_exit;
245 }
246 }
247
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200248 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +0100249
Paul Bakkerf9c49532013-12-19 15:40:58 +0100250 ret = 0;
251
252thread_exit:
253
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200254# ifdef MBEDTLS_ERROR_C
255 if (ret != 0) {
Paul Bakkerf9c49532013-12-19 15:40:58 +0100256 char error_buf[100];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200257 mbedtls_strerror(ret, error_buf, 100);
Manuel Pégourié-Gonnard30eceb72015-05-11 14:42:56 +0200258 mbedtls_printf(" [ #%ld ] Last error was: -0x%04x - %s\n\n",
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200259 thread_id, (unsigned int)-ret, error_buf);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100260 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200261# endif
Paul Bakkerf9c49532013-12-19 15:40:58 +0100262
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200263 mbedtls_net_free(client_fd);
264 mbedtls_ssl_free(&ssl);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100265
266 thread_info->thread_complete = 1;
267
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200268 return NULL;
Paul Bakkerf9c49532013-12-19 15:40:58 +0100269}
270
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200271static int thread_create(mbedtls_net_context *client_fd)
Paul Bakkerf9c49532013-12-19 15:40:58 +0100272{
273 int ret, i;
274
275 /*
276 * Find in-active or finished thread slot
277 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200278 for (i = 0; i < MAX_NUM_THREADS; i++) {
279 if (threads[i].active == 0)
Paul Bakkerf9c49532013-12-19 15:40:58 +0100280 break;
281
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200282 if (threads[i].data.thread_complete == 1) {
283 mbedtls_printf(" [ main ] Cleaning up thread %d\n", i);
284 pthread_join(threads[i].thread, NULL);
285 memset(&threads[i], 0, sizeof(pthread_info_t));
Paul Bakkerf9c49532013-12-19 15:40:58 +0100286 break;
287 }
288 }
289
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200290 if (i == MAX_NUM_THREADS)
291 return -1;
Paul Bakkerf9c49532013-12-19 15:40:58 +0100292
293 /*
294 * Fill thread-info for thread
295 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200296 memcpy(&threads[i].data, &base_info, sizeof(base_info));
Paul Bakkerf9c49532013-12-19 15:40:58 +0100297 threads[i].active = 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200298 memcpy(&threads[i].data.client_fd, client_fd, sizeof(mbedtls_net_context));
Paul Bakkerf9c49532013-12-19 15:40:58 +0100299
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200300 if ((ret = pthread_create(&threads[i].thread, NULL, handle_ssl_connection,
301 &threads[i].data)) != 0) {
302 return ret;
Paul Bakkerf9c49532013-12-19 15:40:58 +0100303 }
304
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200305 return 0;
Paul Bakkerf9c49532013-12-19 15:40:58 +0100306}
307
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200308int main(void)
Paul Bakkerf9c49532013-12-19 15:40:58 +0100309{
310 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200311 mbedtls_net_context listen_fd, client_fd;
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200312 const char pers[] = "ssl_pthread_server";
Paul Bakkerf9c49532013-12-19 15:40:58 +0100313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_entropy_context entropy;
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200315 mbedtls_ctr_drbg_context ctr_drbg;
316 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_x509_crt srvcert;
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200318 mbedtls_x509_crt cachain;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_pk_context pkey;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200320# if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Paul Bakkerf9c49532013-12-19 15:40:58 +0100321 unsigned char alloc_buf[100000];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200322# endif
323# if defined(MBEDTLS_SSL_CACHE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 mbedtls_ssl_cache_context cache;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200325# endif
Paul Bakkerf9c49532013-12-19 15:40:58 +0100326
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200327# if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
328 mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
329# endif
Paul Bakkerf9c49532013-12-19 15:40:58 +0100330
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200331# if defined(MBEDTLS_SSL_CACHE_C)
332 mbedtls_ssl_cache_init(&cache);
333# endif
Paul Bakkerf9c49532013-12-19 15:40:58 +0100334
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200335 mbedtls_x509_crt_init(&srvcert);
336 mbedtls_x509_crt_init(&cachain);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200337
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200338 mbedtls_ssl_config_init(&conf);
339 mbedtls_ctr_drbg_init(&ctr_drbg);
340 memset(threads, 0, sizeof(threads));
341 mbedtls_net_init(&listen_fd);
342 mbedtls_net_init(&client_fd);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100343
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200344 mbedtls_mutex_init(&debug_mutex);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100345
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200346 base_info.config = &conf;
347
Paul Bakkerf9c49532013-12-19 15:40:58 +0100348 /*
349 * We use only a single entropy source that is used in all the threads.
350 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200351 mbedtls_entropy_init(&entropy);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100352
353 /*
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200354 * 1a. Seed the random number generator
355 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200356 mbedtls_printf(" . Seeding the random number generator...");
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200357
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200358 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
359 (const unsigned char *)pers,
360 strlen(pers))) != 0) {
361 mbedtls_printf(" failed: mbedtls_ctr_drbg_seed returned -0x%04x\n",
362 (unsigned int)-ret);
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200363 goto exit;
364 }
365
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200366 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200367
368 /*
369 * 1b. Load the certificates and private RSA key
Paul Bakkerf9c49532013-12-19 15:40:58 +0100370 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200371 mbedtls_printf("\n . Loading the server cert. and key...");
372 fflush(stdout);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100373
Paul Bakkerf9c49532013-12-19 15:40:58 +0100374 /*
375 * This demonstration program uses embedded test certificates.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
377 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
Paul Bakkerf9c49532013-12-19 15:40:58 +0100378 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200379 ret = mbedtls_x509_crt_parse(&srvcert,
380 (const unsigned char *)mbedtls_test_srv_crt,
381 mbedtls_test_srv_crt_len);
382 if (ret != 0) {
383 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned %d\n\n",
384 ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100385 goto exit;
386 }
387
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200388 ret = mbedtls_x509_crt_parse(&cachain,
389 (const unsigned char *)mbedtls_test_cas_pem,
390 mbedtls_test_cas_pem_len);
391 if (ret != 0) {
392 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned %d\n\n",
393 ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100394 goto exit;
395 }
396
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200397 mbedtls_pk_init(&pkey);
398 ret = mbedtls_pk_parse_key(&pkey,
399 (const unsigned char *)mbedtls_test_srv_key,
400 mbedtls_test_srv_key_len, NULL, 0,
401 mbedtls_ctr_drbg_random, &ctr_drbg);
402 if (ret != 0) {
403 mbedtls_printf(" failed\n ! mbedtls_pk_parse_key returned %d\n\n",
404 ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100405 goto exit;
406 }
407
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200408 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200409
410 /*
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200411 * 1c. Prepare SSL configuration
412 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200413 mbedtls_printf(" . Setting up the SSL data....");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200414
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200415 if ((ret = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER,
416 MBEDTLS_SSL_TRANSPORT_STREAM,
417 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
418 mbedtls_printf(
419 " failed: mbedtls_ssl_config_defaults returned -0x%04x\n",
420 (unsigned int)-ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200421 goto exit;
422 }
423
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200424 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
425 mbedtls_ssl_conf_dbg(&conf, my_mutexed_debug, stdout);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200426
427 /* mbedtls_ssl_cache_get() and mbedtls_ssl_cache_set() are thread-safe if
428 * MBEDTLS_THREADING_C is set.
429 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200430# if defined(MBEDTLS_SSL_CACHE_C)
431 mbedtls_ssl_conf_session_cache(&conf, &cache, mbedtls_ssl_cache_get,
432 mbedtls_ssl_cache_set);
433# endif
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200434
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200435 mbedtls_ssl_conf_ca_chain(&conf, &cachain, NULL);
436 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey)) != 0) {
437 mbedtls_printf(" failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
438 ret);
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200439 goto exit;
440 }
441
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200442 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200443
Manuel Pégourié-Gonnard0af00e82015-05-11 11:32:43 +0200444 /*
Paul Bakkerf9c49532013-12-19 15:40:58 +0100445 * 2. Setup the listening TCP socket
446 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200447 mbedtls_printf(" . Bind on https://localhost:4433/ ...");
448 fflush(stdout);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100449
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200450 if ((ret = mbedtls_net_bind(&listen_fd, NULL, "4433",
451 MBEDTLS_NET_PROTO_TCP)) != 0) {
452 mbedtls_printf(" failed\n ! mbedtls_net_bind returned %d\n\n", ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100453 goto exit;
454 }
455
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200456 mbedtls_printf(" ok\n");
Paul Bakkerf9c49532013-12-19 15:40:58 +0100457
458reset:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200459# ifdef MBEDTLS_ERROR_C
460 if (ret != 0) {
Paul Bakkerf9c49532013-12-19 15:40:58 +0100461 char error_buf[100];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200462 mbedtls_strerror(ret, error_buf, 100);
463 mbedtls_printf(" [ main ] Last error was: -0x%04x - %s\n",
464 (unsigned int)-ret, error_buf);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100465 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200466# endif
Paul Bakkerf9c49532013-12-19 15:40:58 +0100467
468 /*
469 * 3. Wait until a client connects
470 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200471 mbedtls_printf(" [ main ] Waiting for a remote connection\n");
Paul Bakkerf9c49532013-12-19 15:40:58 +0100472
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200473 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd, NULL, 0, NULL)) !=
474 0) {
475 mbedtls_printf(
476 " [ main ] failed: mbedtls_net_accept returned -0x%04x\n",
477 (unsigned int)ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100478 goto exit;
479 }
480
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200481 mbedtls_printf(" [ main ] ok\n");
482 mbedtls_printf(" [ main ] Creating a new thread\n");
Paul Bakkerf9c49532013-12-19 15:40:58 +0100483
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200484 if ((ret = thread_create(&client_fd)) != 0) {
485 mbedtls_printf(" [ main ] failed: thread_create returned %d\n", ret);
486 mbedtls_net_free(&client_fd);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100487 goto reset;
488 }
489
490 ret = 0;
491 goto reset;
492
493exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200494 mbedtls_x509_crt_free(&srvcert);
495 mbedtls_pk_free(&pkey);
496# if defined(MBEDTLS_SSL_CACHE_C)
497 mbedtls_ssl_cache_free(&cache);
498# endif
499 mbedtls_ctr_drbg_free(&ctr_drbg);
500 mbedtls_entropy_free(&entropy);
501 mbedtls_ssl_config_free(&conf);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100502
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200503 mbedtls_net_free(&listen_fd);
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200504
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200505 mbedtls_mutex_free(&debug_mutex);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100506
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200507# if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_memory_buffer_alloc_free();
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200509# endif
Paul Bakkerf9c49532013-12-19 15:40:58 +0100510
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200511# if defined(_WIN32)
512 mbedtls_printf(" Press Enter to exit this program.\n");
513 fflush(stdout);
514 getchar();
515# endif
Paul Bakkerf9c49532013-12-19 15:40:58 +0100516
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200517 mbedtls_exit(ret);
Paul Bakkerf9c49532013-12-19 15:40:58 +0100518}
519
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200520#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && \
521 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && \
522 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_THREADING_C && \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 MBEDTLS_THREADING_PTHREAD && MBEDTLS_PEM_PARSE_C */