Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * SSL server demonstration program using pthread for handling multiple |
| 3 | * clients. |
| 4 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame^] | 6 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 10 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 11 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 13 | #endif |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 14 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 15 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 16 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 17 | #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 Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 24 | int main(void) |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 25 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_CERTS_C and/or MBEDTLS_ENTROPY_C " |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 27 | "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 Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 33 | } |
| 34 | #else |
Manuel Pégourié-Gonnard | 4b3e5ef | 2015-03-27 11:24:27 +0100 | [diff] [blame] | 35 | |
| 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 AG | 788aa4a | 2016-09-14 14:32:09 +0100 | [diff] [blame] | 48 | #include "mbedtls/net_sockets.h" |
Manuel Pégourié-Gonnard | 4b3e5ef | 2015-03-27 11:24:27 +0100 | [diff] [blame] | 49 | #include "mbedtls/error.h" |
| 50 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | #if defined(MBEDTLS_SSL_CACHE_C) |
Manuel Pégourié-Gonnard | 4b3e5ef | 2015-03-27 11:24:27 +0100 | [diff] [blame] | 52 | #include "mbedtls/ssl_cache.h" |
| 53 | #endif |
| 54 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
Manuel Pégourié-Gonnard | 4b3e5ef | 2015-03-27 11:24:27 +0100 | [diff] [blame] | 56 | #include "mbedtls/memory_buffer_alloc.h" |
| 57 | #endif |
| 58 | |
Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 59 | |
Manuel Pégourié-Gonnard | 4b3e5ef | 2015-03-27 11:24:27 +0100 | [diff] [blame] | 60 | #define HTTP_RESPONSE \ |
| 61 | "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \ |
Gilles Peskine | f08ca83 | 2023-09-12 19:21:54 +0200 | [diff] [blame] | 62 | "<h2>Mbed TLS Test Server</h2>\r\n" \ |
Manuel Pégourié-Gonnard | 4b3e5ef | 2015-03-27 11:24:27 +0100 | [diff] [blame] | 63 | "<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é-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 69 | mbedtls_threading_mutex_t debug_mutex; |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 70 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 71 | static void my_mutexed_debug(void *ctx, int level, |
| 72 | const char *file, int line, |
| 73 | const char *str) |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 74 | { |
Manuel Pégourié-Gonnard | 61ee351 | 2015-06-23 17:35:03 +0200 | [diff] [blame] | 75 | long int thread_id = (long int) pthread_self(); |
| 76 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 77 | mbedtls_mutex_lock(&debug_mutex); |
Manuel Pégourié-Gonnard | 61ee351 | 2015-06-23 17:35:03 +0200 | [diff] [blame] | 78 | |
| 79 | ((void) level); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 80 | mbedtls_fprintf((FILE *) ctx, "%s:%04d: [ #%ld ] %s", |
| 81 | file, line, thread_id, str); |
| 82 | fflush((FILE *) ctx); |
Manuel Pégourié-Gonnard | 61ee351 | 2015-06-23 17:35:03 +0200 | [diff] [blame] | 83 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 84 | mbedtls_mutex_unlock(&debug_mutex); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | typedef struct { |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 88 | mbedtls_net_context client_fd; |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 89 | int thread_complete; |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 90 | const mbedtls_ssl_config *config; |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 91 | } thread_info_t; |
| 92 | |
| 93 | typedef struct { |
| 94 | int active; |
| 95 | thread_info_t data; |
| 96 | pthread_t thread; |
| 97 | } pthread_info_t; |
| 98 | |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 99 | static thread_info_t base_info; |
| 100 | static pthread_info_t threads[MAX_NUM_THREADS]; |
| 101 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 102 | static void *handle_ssl_connection(void *data) |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 103 | { |
| 104 | int ret, len; |
| 105 | thread_info_t *thread_info = (thread_info_t *) data; |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 106 | mbedtls_net_context *client_fd = &thread_info->client_fd; |
Manuel Pégourié-Gonnard | 30eceb7 | 2015-05-11 14:42:56 +0200 | [diff] [blame] | 107 | long int thread_id = (long int) pthread_self(); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 108 | unsigned char buf[1024]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | mbedtls_ssl_context ssl; |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 110 | |
Manuel Pégourié-Gonnard | 955028f | 2014-07-12 01:27:34 +0200 | [diff] [blame] | 111 | /* Make sure memory references are valid */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 112 | mbedtls_ssl_init(&ssl); |
Manuel Pégourié-Gonnard | 955028f | 2014-07-12 01:27:34 +0200 | [diff] [blame] | 113 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 114 | mbedtls_printf(" [ #%ld ] Setting up SSL/TLS data\n", thread_id); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 115 | |
| 116 | /* |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 117 | * 4. Get the SSL context ready |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 118 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 119 | 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 Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 122 | goto thread_exit; |
| 123 | } |
| 124 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 125 | mbedtls_ssl_set_bio(&ssl, client_fd, mbedtls_net_send, mbedtls_net_recv, NULL); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 126 | |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 127 | /* |
| 128 | * 5. Handshake |
| 129 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 130 | mbedtls_printf(" [ #%ld ] Performing the SSL/TLS handshake\n", thread_id); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 131 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 132 | 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 Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 136 | goto thread_exit; |
| 137 | } |
| 138 | } |
| 139 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 140 | mbedtls_printf(" [ #%ld ] ok\n", thread_id); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * 6. Read the HTTP Request |
| 144 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 145 | mbedtls_printf(" [ #%ld ] < Read from client\n", thread_id); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 146 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 147 | do { |
| 148 | len = sizeof(buf) - 1; |
| 149 | memset(buf, 0, sizeof(buf)); |
| 150 | ret = mbedtls_ssl_read(&ssl, buf, len); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 151 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 152 | if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) { |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 153 | continue; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 154 | } |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 155 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 156 | if (ret <= 0) { |
| 157 | switch (ret) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 159 | mbedtls_printf(" [ #%ld ] connection was closed gracefully\n", |
| 160 | thread_id); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 161 | goto thread_exit; |
| 162 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | case MBEDTLS_ERR_NET_CONN_RESET: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 164 | mbedtls_printf(" [ #%ld ] connection was reset by peer\n", |
| 165 | thread_id); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 166 | goto thread_exit; |
| 167 | |
| 168 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 169 | mbedtls_printf(" [ #%ld ] mbedtls_ssl_read returned -0x%04x\n", |
| 170 | thread_id, (unsigned int) -ret); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 171 | goto thread_exit; |
| 172 | } |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | len = ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 176 | mbedtls_printf(" [ #%ld ] %d bytes read\n=====\n%s\n=====\n", |
| 177 | thread_id, len, (char *) buf); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 178 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 179 | if (ret > 0) { |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 180 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 181 | } |
| 182 | } while (1); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * 7. Write the 200 Response |
| 186 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 187 | mbedtls_printf(" [ #%ld ] > Write to client:\n", thread_id); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 188 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 189 | len = sprintf((char *) buf, HTTP_RESPONSE, |
| 190 | mbedtls_ssl_get_ciphersuite(&ssl)); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 191 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 192 | 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 Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 196 | goto thread_exit; |
| 197 | } |
| 198 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 199 | 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 Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 202 | goto thread_exit; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | len = ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 207 | mbedtls_printf(" [ #%ld ] %d bytes written\n=====\n%s\n=====\n", |
| 208 | thread_id, len, (char *) buf); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 209 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 210 | mbedtls_printf(" [ #%ld ] . Closing the connection...", thread_id); |
Manuel Pégourié-Gonnard | 6b0d268 | 2014-03-25 11:24:43 +0100 | [diff] [blame] | 211 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 212 | 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é-Gonnard | 6b0d268 | 2014-03-25 11:24:43 +0100 | [diff] [blame] | 217 | goto thread_exit; |
| 218 | } |
| 219 | } |
| 220 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 221 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | 6b0d268 | 2014-03-25 11:24:43 +0100 | [diff] [blame] | 222 | |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 223 | ret = 0; |
| 224 | |
| 225 | thread_exit: |
| 226 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | #ifdef MBEDTLS_ERROR_C |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 228 | if (ret != 0) { |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 229 | char error_buf[100]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 230 | mbedtls_strerror(ret, error_buf, 100); |
Manuel Pégourié-Gonnard | 30eceb7 | 2015-05-11 14:42:56 +0200 | [diff] [blame] | 231 | mbedtls_printf(" [ #%ld ] Last error was: -0x%04x - %s\n\n", |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 232 | thread_id, (unsigned int) -ret, error_buf); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 233 | } |
| 234 | #endif |
| 235 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 236 | mbedtls_net_free(client_fd); |
| 237 | mbedtls_ssl_free(&ssl); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 238 | |
| 239 | thread_info->thread_complete = 1; |
| 240 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 241 | return NULL; |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 244 | static int thread_create(mbedtls_net_context *client_fd) |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 245 | { |
| 246 | int ret, i; |
| 247 | |
| 248 | /* |
| 249 | * Find in-active or finished thread slot |
| 250 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 251 | for (i = 0; i < MAX_NUM_THREADS; i++) { |
| 252 | if (threads[i].active == 0) { |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 253 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 254 | } |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 255 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 256 | 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 Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 260 | break; |
| 261 | } |
| 262 | } |
| 263 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 264 | if (i == MAX_NUM_THREADS) { |
| 265 | return -1; |
| 266 | } |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 267 | |
| 268 | /* |
| 269 | * Fill thread-info for thread |
| 270 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 271 | memcpy(&threads[i].data, &base_info, sizeof(base_info)); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 272 | threads[i].active = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 273 | memcpy(&threads[i].data.client_fd, client_fd, sizeof(mbedtls_net_context)); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 274 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 275 | if ((ret = pthread_create(&threads[i].thread, NULL, handle_ssl_connection, |
| 276 | &threads[i].data)) != 0) { |
| 277 | return ret; |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 280 | return 0; |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 281 | } |
| 282 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 283 | int main(void) |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 284 | { |
| 285 | int ret; |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 286 | mbedtls_net_context listen_fd, client_fd; |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 287 | const char pers[] = "ssl_pthread_server"; |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 288 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 289 | mbedtls_entropy_context entropy; |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 290 | mbedtls_ctr_drbg_context ctr_drbg; |
| 291 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 292 | mbedtls_x509_crt srvcert; |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 293 | mbedtls_x509_crt cachain; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 294 | mbedtls_pk_context pkey; |
| 295 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 296 | unsigned char alloc_buf[100000]; |
| 297 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 298 | #if defined(MBEDTLS_SSL_CACHE_C) |
| 299 | mbedtls_ssl_cache_context cache; |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 300 | #endif |
| 301 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 302 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 303 | mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf)); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 304 | #endif |
| 305 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 306 | #if defined(MBEDTLS_SSL_CACHE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 307 | mbedtls_ssl_cache_init(&cache); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 308 | #endif |
| 309 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 310 | mbedtls_x509_crt_init(&srvcert); |
| 311 | mbedtls_x509_crt_init(&cachain); |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 312 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 313 | 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 Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 318 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 319 | mbedtls_mutex_init(&debug_mutex); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 320 | |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 321 | base_info.config = &conf; |
| 322 | |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 323 | /* |
| 324 | * We use only a single entropy source that is used in all the threads. |
| 325 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 326 | mbedtls_entropy_init(&entropy); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 327 | |
Przemek Stekiel | b00688f | 2023-04-17 11:10:05 +0200 | [diff] [blame] | 328 | #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 Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 338 | /* |
| 339 | * 1. Load the certificates and private RSA key |
| 340 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 341 | mbedtls_printf("\n . Loading the server cert. and key..."); |
| 342 | fflush(stdout); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 343 | |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 344 | /* |
| 345 | * This demonstration program uses embedded test certificates. |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 346 | * 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 Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 348 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 349 | 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 Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 353 | goto exit; |
| 354 | } |
| 355 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 356 | 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 Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 360 | goto exit; |
| 361 | } |
| 362 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 363 | 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 Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 368 | goto exit; |
| 369 | } |
| 370 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 371 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 372 | |
| 373 | /* |
| 374 | * 1b. Seed the random number generator |
| 375 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 376 | mbedtls_printf(" . Seeding the random number generator..."); |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 377 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 378 | 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é-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 383 | goto exit; |
| 384 | } |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 385 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 386 | mbedtls_printf(" ok\n"); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 387 | |
| 388 | /* |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 389 | * 1c. Prepare SSL configuration |
| 390 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 391 | mbedtls_printf(" . Setting up the SSL data...."); |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 392 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 393 | 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é-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 399 | goto exit; |
| 400 | } |
| 401 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 402 | mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg); |
| 403 | mbedtls_ssl_conf_dbg(&conf, my_mutexed_debug, stdout); |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 404 | |
| 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 Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 409 | mbedtls_ssl_conf_session_cache(&conf, &cache, |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 410 | mbedtls_ssl_cache_get, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 411 | mbedtls_ssl_cache_set); |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 412 | #endif |
| 413 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 414 | 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é-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 417 | goto exit; |
| 418 | } |
| 419 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 420 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 421 | |
Manuel Pégourié-Gonnard | 0af00e8 | 2015-05-11 11:32:43 +0200 | [diff] [blame] | 422 | /* |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 423 | * 2. Setup the listening TCP socket |
| 424 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 425 | mbedtls_printf(" . Bind on https://localhost:4433/ ..."); |
| 426 | fflush(stdout); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 427 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 428 | 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 Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 430 | goto exit; |
| 431 | } |
| 432 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 433 | mbedtls_printf(" ok\n"); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 434 | |
| 435 | reset: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 436 | #ifdef MBEDTLS_ERROR_C |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 437 | if (ret != 0) { |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 438 | char error_buf[100]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 439 | mbedtls_strerror(ret, error_buf, 100); |
| 440 | mbedtls_printf(" [ main ] Last error was: -0x%04x - %s\n", (unsigned int) -ret, |
| 441 | error_buf); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 442 | } |
| 443 | #endif |
| 444 | |
| 445 | /* |
| 446 | * 3. Wait until a client connects |
| 447 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 448 | mbedtls_printf(" [ main ] Waiting for a remote connection\n"); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 449 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 450 | 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 Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 454 | goto exit; |
| 455 | } |
| 456 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 457 | mbedtls_printf(" [ main ] ok\n"); |
| 458 | mbedtls_printf(" [ main ] Creating a new thread\n"); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 459 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 460 | 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 Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 463 | goto reset; |
| 464 | } |
| 465 | |
| 466 | ret = 0; |
| 467 | goto reset; |
| 468 | |
| 469 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 470 | mbedtls_x509_crt_free(&srvcert); |
| 471 | mbedtls_pk_free(&pkey); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 472 | #if defined(MBEDTLS_SSL_CACHE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 473 | mbedtls_ssl_cache_free(&cache); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 474 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 475 | mbedtls_ctr_drbg_free(&ctr_drbg); |
| 476 | mbedtls_entropy_free(&entropy); |
| 477 | mbedtls_ssl_config_free(&conf); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 478 | mbedtls_net_free(&listen_fd); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 479 | mbedtls_mutex_free(&debug_mutex); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 480 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 481 | mbedtls_memory_buffer_alloc_free(); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 482 | #endif |
Przemek Stekiel | d4d049b | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 483 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemek Stekiel | c4ddf92 | 2023-04-19 10:15:26 +0200 | [diff] [blame] | 484 | mbedtls_psa_crypto_free(); |
Przemek Stekiel | d4d049b | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 485 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 486 | |
| 487 | #if defined(_WIN32) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 488 | mbedtls_printf(" Press Enter to exit this program.\n"); |
| 489 | fflush(stdout); getchar(); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 490 | #endif |
| 491 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 492 | mbedtls_exit(ret); |
Paul Bakker | f9c4953 | 2013-12-19 15:40:58 +0100 | [diff] [blame] | 493 | } |
| 494 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 495 | #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 */ |